@@ -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}
0 commit comments