From ac54d7371c5a73f49e1e4dad5d0f3667fcd944fa Mon Sep 17 00:00:00 2001 From: Petard Jonson <41122242+greenhat616@users.noreply.github.com> Date: Tue, 3 Sep 2024 02:48:44 +0800 Subject: [PATCH] feat(dock): try to setup macos dock handler feat(dock): try to setup macos dock handler feat(dock): try to setup macos dock handler --- backend/tauri/src/utils/dock.rs | 48 ++++++++++++++++++++++++++++-- backend/tauri/src/utils/resolve.rs | 2 ++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/backend/tauri/src/utils/dock.rs b/backend/tauri/src/utils/dock.rs index f4e925a8149..3f3f6990851 100644 --- a/backend/tauri/src/utils/dock.rs +++ b/backend/tauri/src/utils/dock.rs @@ -3,9 +3,17 @@ pub mod macos { extern crate cocoa; extern crate objc; - use cocoa::appkit::{NSApp, NSApplication, NSApplicationActivationPolicy}; - use objc::runtime::YES; - + use cocoa::{ + appkit::{NSApp, NSApplication, NSApplicationActivationPolicy, NSWindow}, + base::{id, nil, BOOL, NO, YES}, + }; + use objc::{ + class, + declare::ClassDecl, + msg_send, + runtime::{Object, Sel}, + sel, sel_impl, + }; pub unsafe fn show_dock_icon() { let app = NSApp(); app.setActivationPolicy_( @@ -20,4 +28,38 @@ pub mod macos { NSApplicationActivationPolicy::NSApplicationActivationPolicyAccessory, ); } + + pub fn setup_dock_click_handler() { + unsafe { + let app = NSApp(); + let superclass = class!(NSObject); + let mut decl = ClassDecl::new("AppDelegate", superclass).unwrap(); + decl.add_method( + sel!(applicationShouldHandleReopen:hasVisibleWindows:), + reopen_handler as extern "C" fn(&mut Object, Sel, id, BOOL) -> BOOL, + ); + decl.register(); + let delegate: id = msg_send![class!(AppDelegate), new]; + app.setDelegate_(delegate); + } + } + + extern "C" fn reopen_handler(_: &mut Object, _: Sel, _: id, has_visible_windows: BOOL) -> BOOL { + unsafe { + let app = NSApp(); + if app.activationPolicy() == NSApplicationActivationPolicy::Accessory { + if has_visible_windows == NO { + // resolve crate window + let handle = crate::core::handle::Handle::global(); + let app_handle = handle.app_handle.lock(); + if let Some(app_handle) = app_handle.as_ref() { + crate::utils::resolve::create_window(app_handle); + } + } + NO + } else { + YES + } + } + } } diff --git a/backend/tauri/src/utils/resolve.rs b/backend/tauri/src/utils/resolve.rs index 1fc68092f23..ec6c18ca5d1 100644 --- a/backend/tauri/src/utils/resolve.rs +++ b/backend/tauri/src/utils/resolve.rs @@ -85,6 +85,8 @@ pub fn find_unused_port() -> Result { pub fn resolve_setup(app: &mut App) { #[cfg(target_os = "macos")] app.set_activation_policy(tauri::ActivationPolicy::Accessory); + #[cfg(target_os = "macos")] + crate::utils::dock::macos::setup_dock_click_handler(); app.listen_global("react_app_mounted", move |_| { tracing::debug!("Frontend React App is mounted, reset open window counter"); reset_window_open_counter();