Skip to content

Commit ac54d73

Browse files
committed
feat(dock): try to setup macos dock handler
feat(dock): try to setup macos dock handler feat(dock): try to setup macos dock handler
1 parent 56d1523 commit ac54d73

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

backend/tauri/src/utils/dock.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ pub mod macos {
33
extern crate cocoa;
44
extern crate objc;
55

6-
use cocoa::appkit::{NSApp, NSApplication, NSApplicationActivationPolicy};
7-
use objc::runtime::YES;
8-
6+
use cocoa::{
7+
appkit::{NSApp, NSApplication, NSApplicationActivationPolicy, NSWindow},
8+
base::{id, nil, BOOL, NO, YES},
9+
};
10+
use objc::{
11+
class,
12+
declare::ClassDecl,
13+
msg_send,
14+
runtime::{Object, Sel},
15+
sel, sel_impl,
16+
};
917
pub unsafe fn show_dock_icon() {
1018
let app = NSApp();
1119
app.setActivationPolicy_(
@@ -20,4 +28,38 @@ pub mod macos {
2028
NSApplicationActivationPolicy::NSApplicationActivationPolicyAccessory,
2129
);
2230
}
31+
32+
pub fn setup_dock_click_handler() {
33+
unsafe {
34+
let app = NSApp();
35+
let superclass = class!(NSObject);
36+
let mut decl = ClassDecl::new("AppDelegate", superclass).unwrap();
37+
decl.add_method(
38+
sel!(applicationShouldHandleReopen:hasVisibleWindows:),
39+
reopen_handler as extern "C" fn(&mut Object, Sel, id, BOOL) -> BOOL,
40+
);
41+
decl.register();
42+
let delegate: id = msg_send![class!(AppDelegate), new];
43+
app.setDelegate_(delegate);
44+
}
45+
}
46+
47+
extern "C" fn reopen_handler(_: &mut Object, _: Sel, _: id, has_visible_windows: BOOL) -> BOOL {
48+
unsafe {
49+
let app = NSApp();
50+
if app.activationPolicy() == NSApplicationActivationPolicy::Accessory {
51+
if has_visible_windows == NO {
52+
// resolve crate window
53+
let handle = crate::core::handle::Handle::global();
54+
let app_handle = handle.app_handle.lock();
55+
if let Some(app_handle) = app_handle.as_ref() {
56+
crate::utils::resolve::create_window(app_handle);
57+
}
58+
}
59+
NO
60+
} else {
61+
YES
62+
}
63+
}
64+
}
2365
}

backend/tauri/src/utils/resolve.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ pub fn find_unused_port() -> Result<u16> {
8585
pub fn resolve_setup(app: &mut App) {
8686
#[cfg(target_os = "macos")]
8787
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
88+
#[cfg(target_os = "macos")]
89+
crate::utils::dock::macos::setup_dock_click_handler();
8890
app.listen_global("react_app_mounted", move |_| {
8991
tracing::debug!("Frontend React App is mounted, reset open window counter");
9092
reset_window_open_counter();

0 commit comments

Comments
 (0)