From e2c9b782547e536c505d6ca5fbbcc203243e8acc Mon Sep 17 00:00:00 2001 From: rustdesk Date: Fri, 29 Apr 2022 16:21:18 +0800 Subject: [PATCH] mac tray work --- build.py | 5 --- src/platform/macos.rs | 23 ++++++----- src/platform/privileges_scripts/run.scpt | 6 --- src/ui.rs | 28 ++++---------- src/ui/index.tis | 4 -- src/ui/macos.rs | 49 +++++++++++++++++------- 6 files changed, 54 insertions(+), 61 deletions(-) delete mode 100644 src/platform/privileges_scripts/run.scpt diff --git a/build.py b/build.py index cf19b1d72..9ced9933f 100644 --- a/build.py +++ b/build.py @@ -33,11 +33,6 @@ def main(): os.system('python3 inline-sciter.py') if os.path.isfile('/usr/bin/pacman'): os.system('git checkout src/ui/common.tis') - txt = open('Cargo.toml').read() - with open('Cargo.toml', 'wt') as fh: - fh.write(txt.replace('#lto', 'lto') - .replace('#codegen', 'codegen') - .replace('#panic', 'panic')) version = get_version() if windows: os.system('cargo build --release --features inline') diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 777071dc0..066a4520e 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -145,6 +145,14 @@ pub fn is_installed_daemon(prompt: bool) -> bool { .args(&["load", "-w", &agent_plist_file]) .status() .ok(); + std::process::Command::new("sh") + .arg("-c") + .arg(&format!( + "sleep 0.5; open -n /Applications/{}.app", + crate::get_app_name(), + )) + .spawn() + .ok(); quit_gui(); } } @@ -166,7 +174,6 @@ pub fn uninstall() -> bool { match std::process::Command::new("osascript") .arg("-e") .arg(script_body) - .arg(&get_active_username()) .status() { Err(e) => { @@ -189,18 +196,11 @@ pub fn uninstall() -> bool { .args(&["remove", &format!("{}_server", crate::get_full_name())]) .status() .ok(); - std::process::Command::new("osascript") - .arg("-e") - .arg( - PRIVILEGES_SCRIPTS_DIR - .get_file("run.scpt") - .unwrap() - .contents_utf8() - .unwrap(), - ) + std::process::Command::new("sh") + .arg("-c") .arg(&format!( "sleep 0.5; open /Applications/{}.app", - crate::get_full_name(), + crate::get_app_name(), )) .spawn() .ok(); @@ -433,7 +433,6 @@ pub fn start_os_service() { ]) .status() .ok(); - // if above spawn, we may need sleep for a while here. std::process::exit(0); } } diff --git a/src/platform/privileges_scripts/run.scpt b/src/platform/privileges_scripts/run.scpt deleted file mode 100644 index 7c6d156e7..000000000 --- a/src/platform/privileges_scripts/run.scpt +++ /dev/null @@ -1,6 +0,0 @@ -on run {cmd} - - set sh to "" & cmd - do shell script sh - -end run diff --git a/src/ui.rs b/src/ui.rs index 87008aac7..47b1968b9 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -38,21 +38,12 @@ struct UI( struct UIHostHandler; pub fn start(args: &mut [String]) { - let is_server = args.len() == 1 && args[0] == "--server"; #[cfg(target_os = "macos")] - if is_server && LocalConfig::get_option("service-as-tray") == "Y" { + if args.len() == 1 && args[0] == "--server" { macos::make_tray(); return; - } - let is_index = args.is_empty() || is_server; - if is_server { - // wait a moment for server's ipc check to avoid sciter crash - std::thread::sleep(std::time::Duration::from_millis(300)); - if crate::platform::is_prelogin() { - loop { - std::thread::sleep(std::time::Duration::from_secs(3)); - } - } + } else { + macos::show_dock(); } #[cfg(all(target_os = "linux", feature = "inline"))] sciter::set_library("/usr/lib/rustdesk/libsciter-gtk.so").ok(); @@ -85,9 +76,9 @@ pub fn start(args: &mut [String]) { let mut frame = sciter::WindowBuilder::main_window().create(); #[cfg(windows)] allow_err!(sciter::set_options(sciter::RuntimeOptions::UxTheming(true))); - frame.set_title(APP_NAME); + frame.set_title(&crate::get_app_name()); #[cfg(target_os = "macos")] - macos::make_menubar(frame.get_host(), is_index); + macos::make_menubar(frame.get_host(), args.is_empty()); let page; if args.len() > 1 && args[0] == "--play" { args[0] = "--connect".to_owned(); @@ -99,7 +90,7 @@ pub fn start(args: &mut [String]) { .to_owned(); args[1] = id; } - if is_index { + if args.is_empty() { let childs: Childs = Default::default(); let cloned = childs.clone(); std::thread::spawn(move || check_zombie(cloned)); @@ -157,12 +148,7 @@ pub fn start(args: &mut [String]) { .unwrap_or("".to_owned()), page )); - if is_server { - frame.collapse(true); - frame.run_loop(); - } else { - frame.run_app(); - } + frame.run_app(); } #[cfg(windows)] diff --git a/src/ui/index.tis b/src/ui/index.tis index 3c16246ee..4bb9fdae3 100644 --- a/src/ui/index.tis +++ b/src/ui/index.tis @@ -745,10 +745,6 @@ function self.closing() { // return false; // can prevent window close var (x, y, w, h) = view.box(#rectw, #border, #screen); handler.closing(x, y, w, h); - if (is_osx && handler.get_local_option("service-as-tray") != "Y") { - view.windowState = View.WINDOW_HIDDEN; - return false; - } return true; } diff --git a/src/ui/macos.rs b/src/ui/macos.rs index 559a6c558..bb11e2750 100644 --- a/src/ui/macos.rs +++ b/src/ui/macos.rs @@ -42,7 +42,7 @@ impl DelegateState { } } -static mut FIRST_TIME: bool = true; +static mut LAUCHED: bool = false; impl AppHandler for Rc { fn command(&mut self, cmd: u32) { @@ -53,12 +53,6 @@ impl AppHandler for Rc { let _ = self.call_function("awake", &make_args![]); let _ = self.call_function("showSettings", &make_args![]); } else if cmd == AWAKE { - unsafe { - if std::env::args().nth(1) == Some("--server".to_owned()) && FIRST_TIME { - FIRST_TIME = false; - return; - } - } let _ = self.call_function("awake", &make_args![]); } } @@ -114,6 +108,9 @@ unsafe fn set_delegate(handler: Option>) { } extern "C" fn application_did_finish_launching(_this: &mut Object, _: Sel, _notification: id) { + unsafe { + LAUCHED = true; + } unsafe { let () = msg_send![NSApp(), activateIgnoringOtherApps: YES]; } @@ -125,6 +122,13 @@ extern "C" fn application_should_handle_open_untitled_file( _sender: id, ) -> BOOL { unsafe { + if !LAUCHED { + return YES; + } + hbb_common::log::debug!("icon clicked on finder"); + if std::env::args().nth(1) == Some("--server".to_owned()) { + check_main_window(); + } let inner: *mut c_void = *this.get_ivar(APP_HANDLER_IVAR); let inner = &mut *(inner as *mut DelegateState); (*inner).command(AWAKE); @@ -133,17 +137,14 @@ extern "C" fn application_should_handle_open_untitled_file( } extern "C" fn application_should_handle_reopen(_this: &mut Object, _: Sel, _sender: id) -> BOOL { - hbb_common::log::debug!("reopen"); YES } extern "C" fn application_did_become_active(_this: &mut Object, _: Sel, _sender: id) -> BOOL { - hbb_common::log::debug!("active"); YES } extern "C" fn application_did_become_unhide(_this: &mut Object, _: Sel, _sender: id) -> BOOL { - hbb_common::log::debug!("unhide"); YES } @@ -221,14 +222,16 @@ pub fn make_menubar(host: Rc, is_index: bool) { } } -pub fn hide_dock() { +pub fn show_dock() { unsafe { - NSApp().setActivationPolicy_(NSApplicationActivationPolicyAccessory); + NSApp().setActivationPolicy_(NSApplicationActivationPolicyRegular); } } pub fn make_tray() { - hide_dock(); + unsafe { + set_delegate(None); + } use tray_item::TrayItem; if let Ok(mut tray) = TrayItem::new(&crate::get_app_name(), "mac-tray.png") { tray.add_label(&format!( @@ -247,3 +250,23 @@ pub fn make_tray() { } } } + +pub fn check_main_window() { + use sysinfo::{ProcessExt, System, SystemExt}; + let mut sys = System::new(); + sys.refresh_processes(); + let app = format!("/Applications/{}.app", crate::get_app_name()); + let my_uid = sys + .process((std::process::id() as i32).into()) + .map(|x| x.uid) + .unwrap_or_default(); + for (_, p) in sys.processes().iter() { + if p.cmd().len() == 1 && p.uid == my_uid && p.cmd()[0].contains(&app) { + return; + } + } + std::process::Command::new("open") + .args(["-n", &app]) + .status() + .ok(); +}