diff --git a/package.json b/package.json index 4ed99f0..e95de35 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@tauri-apps/plugin-fs": "~2.0.2", "@tauri-apps/plugin-global-shortcut": "~2", "@tauri-apps/plugin-os": "~2", + "@tauri-apps/plugin-positioner": "~2", "@tauri-apps/plugin-shell": "^2.0.1", "@tauri-apps/plugin-store": "~2", "@vueuse/core": "^11.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4a54890..aeb70c5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,6 +29,9 @@ importers: '@tauri-apps/plugin-os': specifier: ~2 version: 2.0.0 + '@tauri-apps/plugin-positioner': + specifier: ~2 + version: 2.0.1 '@tauri-apps/plugin-shell': specifier: ^2.0.1 version: 2.0.1 @@ -773,6 +776,9 @@ packages: '@tauri-apps/plugin-os@2.0.0': resolution: {integrity: sha512-M7hG/nNyQYTJxVG/UhTKhp9mpXriwWzrs9mqDreB8mIgqA3ek5nHLdwRZJWhkKjZrnDT4v9CpA9BhYeplTlAiA==} + '@tauri-apps/plugin-positioner@2.0.1': + resolution: {integrity: sha512-qZY3qiFnt4XU7pYiew17pso9Wg+lgfr6UBva4uvAuWaeW+6KaZCrGJKyr6GLHDar/Lc5KQV392jKMswfrwOplw==} + '@tauri-apps/plugin-shell@2.0.1': resolution: {integrity: sha512-akU1b77sw3qHiynrK0s930y8zKmcdrSD60htjH+mFZqv5WaakZA/XxHR3/sF1nNv9Mgmt/Shls37HwnOr00aSw==} @@ -2876,6 +2882,10 @@ snapshots: dependencies: '@tauri-apps/api': 2.1.1 + '@tauri-apps/plugin-positioner@2.0.1': + dependencies: + '@tauri-apps/api': 2.1.1 + '@tauri-apps/plugin-shell@2.0.1': dependencies: '@tauri-apps/api': 2.1.1 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 4cc9db4..5d3915d 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -965,6 +965,7 @@ dependencies = [ "tauri-plugin-fs", "tauri-plugin-global-shortcut", "tauri-plugin-os", + "tauri-plugin-positioner", "tauri-plugin-shell", "tauri-plugin-store", "tracing", @@ -4721,6 +4722,21 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "tauri-plugin-positioner" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "647253ed516eb1dc5a7eefbfadf594ea5ec9cd8b506ef8896ed64621f9f1d264" +dependencies = [ + "log", + "serde", + "serde_json", + "serde_repr", + "tauri", + "tauri-plugin", + "thiserror 1.0.69", +] + [[package]] name = "tauri-plugin-shell" version = "2.0.2" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 77e3622..78192b7 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -51,6 +51,7 @@ custom-protocol = ["tauri/custom-protocol"] [target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies] tauri-plugin-autostart = "2" tauri-plugin-global-shortcut = "2" +tauri-plugin-positioner = { version = "2.0.0", features = ["tray-icon"] } [target."cfg(target_os = \"macos\")".dependencies] cocoa = "0.25" diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index 390413c..d302e09 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -10,6 +10,8 @@ "core:event:allow-emit", "core:event:allow-emit-to", "core:event:allow-listen", - "core:event:allow-unlisten" + "core:event:allow-unlisten", + "positioner:allow-move-window", + "positioner:allow-set-tray-icon-state" ] } \ No newline at end of file diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 320ef16..5ebcc38 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -13,7 +13,7 @@ mod window; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() - .plugin(tauri_plugin_updater::Builder::new().build()) + .plugin(tauri_plugin_positioner::init()) .plugin(tauri_plugin_os::init()) .setup(|app| { #[cfg(desktop)] diff --git a/src-tauri/src/menu.rs b/src-tauri/src/menu.rs index 03e1df5..a7cf900 100644 --- a/src-tauri/src/menu.rs +++ b/src-tauri/src/menu.rs @@ -148,6 +148,7 @@ pub fn get_app_menu(app: &AppHandle) -> Result, tauri::Error> { } fn handle_tray_icon_events(_tray: &TrayIcon, event: TrayIconEvent) { + tauri_plugin_positioner::on_tray_event(_tray.app_handle(), &event); if let TrayIconEvent::DoubleClick { .. } = event { info!("Double click"); } diff --git a/src-tauri/src/platform/mac/window.rs b/src-tauri/src/platform/mac/window.rs index 7ae4e71..9f7e74a 100644 --- a/src-tauri/src/platform/mac/window.rs +++ b/src-tauri/src/platform/mac/window.rs @@ -1,5 +1,8 @@ +use std::{thread::sleep, time::Duration}; + use crate::window; use tauri::{PhysicalSize, WebviewWindow}; +use tauri_plugin_positioner::{Position, WindowExt}; pub fn show_preview_window(window: &WebviewWindow) { let _ = window.show(); @@ -10,11 +13,10 @@ pub fn show_preview_window(window: &WebviewWindow) { } pub fn update_preview_window(window: &WebviewWindow) { - if (!window.is_visible().unwrap_or_default()) { + if !window.is_visible().unwrap_or_default() { show_preview_window(window); } let _ = window.unminimize(); - window::center_position(window); let _ = window.set_decorations(true); let _ = window.set_focus(); let _ = window.set_resizable(true); @@ -22,10 +24,16 @@ pub fn update_preview_window(window: &WebviewWindow) { if let Some(monitor) = window::find_monitor(window) { let screen_size = monitor.size(); let size = PhysicalSize { - width: screen_size.width - 360, - height: screen_size.height - 860, + width: screen_size.width / 2, + height: screen_size.height / 2, }; let _ = window.set_size(tauri::Size::Physical(size)); + // sleep 0.3 + let window = window.clone(); + tauri::async_runtime::spawn(async move { + sleep(Duration::from_millis(100)); + let _ = window.move_window(Position::Center); + }); } } diff --git a/src-tauri/src/window.rs b/src-tauri/src/window.rs index 1595dc4..2112faf 100644 --- a/src-tauri/src/window.rs +++ b/src-tauri/src/window.rs @@ -93,10 +93,10 @@ pub fn get_main_window(app: &AppHandle) -> WebviewWindow { unsafe { let bg_color = NSColor::colorWithRed_green_blue_alpha_( nil, - 50.0 / 255.0, - 158.0 / 255.0, - 163.5 / 255.0, - 0.5, + 33.0 / 255.0, + 54.0 / 255.0, + 201.0 / 255.0, + 0.1, ); ns_window.setBackgroundColor_(bg_color); } @@ -131,10 +131,10 @@ pub fn get_setting_window(app: &AppHandle) -> WebviewWindow { unsafe { let bg_color = NSColor::colorWithRed_green_blue_alpha_( nil, - 50.0 / 255.0, - 158.0 / 255.0, - 163.5 / 255.0, - 0.5, + 33.0 / 255.0, + 54.0 / 255.0, + 201.0 / 255.0, + 0.1, ); ns_window.setBackgroundColor_(bg_color); } @@ -171,10 +171,10 @@ pub fn get_preview_window(app: &AppHandle) -> WebviewWindow { let bg_color = NSColor::colorWithRed_green_blue_alpha_( nil, - 50.0 / 255.0, - 158.0 / 255.0, - 163.5 / 255.0, - 0.5, + 33.0 / 255.0, + 54.0 / 255.0, + 201.0 / 255.0, + 0.0, ); ns_window.setBackgroundColor_(bg_color); } @@ -211,10 +211,10 @@ pub fn get_startup_window(app: &AppHandle) -> WebviewWindow { unsafe { let bg_color = NSColor::colorWithRed_green_blue_alpha_( nil, - 50.0 / 255.0, - 158.0 / 255.0, - 163.5 / 255.0, - 0.5, + 33.0 / 255.0, + 54.0 / 255.0, + 201.0 / 255.0, + 0.1, ); ns_window.setBackgroundColor_(bg_color); }