Skip to content

Commit

Permalink
feat(dock): try to setup macos dock handler
Browse files Browse the repository at this point in the history
feat(dock): try to setup macos dock handler

feat(dock): try to setup macos dock handler
  • Loading branch information
greenhat616 committed Sep 2, 2024
1 parent 56d1523 commit ac54d73
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
48 changes: 45 additions & 3 deletions backend/tauri/src/utils/dock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_(
Expand All @@ -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
}
}
}
}
2 changes: 2 additions & 0 deletions backend/tauri/src/utils/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub fn find_unused_port() -> Result<u16> {
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();
Expand Down

0 comments on commit ac54d73

Please sign in to comment.