mac tray work

This commit is contained in:
rustdesk 2022-04-29 16:21:18 +08:00
parent 8df1e42977
commit e2c9b78254
6 changed files with 54 additions and 61 deletions

View File

@ -33,11 +33,6 @@ def main():
os.system('python3 inline-sciter.py') os.system('python3 inline-sciter.py')
if os.path.isfile('/usr/bin/pacman'): if os.path.isfile('/usr/bin/pacman'):
os.system('git checkout src/ui/common.tis') 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() version = get_version()
if windows: if windows:
os.system('cargo build --release --features inline') os.system('cargo build --release --features inline')

View File

@ -145,6 +145,14 @@ pub fn is_installed_daemon(prompt: bool) -> bool {
.args(&["load", "-w", &agent_plist_file]) .args(&["load", "-w", &agent_plist_file])
.status() .status()
.ok(); .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(); quit_gui();
} }
} }
@ -166,7 +174,6 @@ pub fn uninstall() -> bool {
match std::process::Command::new("osascript") match std::process::Command::new("osascript")
.arg("-e") .arg("-e")
.arg(script_body) .arg(script_body)
.arg(&get_active_username())
.status() .status()
{ {
Err(e) => { Err(e) => {
@ -189,18 +196,11 @@ pub fn uninstall() -> bool {
.args(&["remove", &format!("{}_server", crate::get_full_name())]) .args(&["remove", &format!("{}_server", crate::get_full_name())])
.status() .status()
.ok(); .ok();
std::process::Command::new("osascript") std::process::Command::new("sh")
.arg("-e") .arg("-c")
.arg(
PRIVILEGES_SCRIPTS_DIR
.get_file("run.scpt")
.unwrap()
.contents_utf8()
.unwrap(),
)
.arg(&format!( .arg(&format!(
"sleep 0.5; open /Applications/{}.app", "sleep 0.5; open /Applications/{}.app",
crate::get_full_name(), crate::get_app_name(),
)) ))
.spawn() .spawn()
.ok(); .ok();
@ -433,7 +433,6 @@ pub fn start_os_service() {
]) ])
.status() .status()
.ok(); .ok();
// if above spawn, we may need sleep for a while here.
std::process::exit(0); std::process::exit(0);
} }
} }

View File

@ -1,6 +0,0 @@
on run {cmd}
set sh to "" & cmd
do shell script sh
end run

View File

@ -38,21 +38,12 @@ struct UI(
struct UIHostHandler; struct UIHostHandler;
pub fn start(args: &mut [String]) { pub fn start(args: &mut [String]) {
let is_server = args.len() == 1 && args[0] == "--server";
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
if is_server && LocalConfig::get_option("service-as-tray") == "Y" { if args.len() == 1 && args[0] == "--server" {
macos::make_tray(); macos::make_tray();
return; return;
} } else {
let is_index = args.is_empty() || is_server; macos::show_dock();
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));
}
}
} }
#[cfg(all(target_os = "linux", feature = "inline"))] #[cfg(all(target_os = "linux", feature = "inline"))]
sciter::set_library("/usr/lib/rustdesk/libsciter-gtk.so").ok(); 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(); let mut frame = sciter::WindowBuilder::main_window().create();
#[cfg(windows)] #[cfg(windows)]
allow_err!(sciter::set_options(sciter::RuntimeOptions::UxTheming(true))); 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")] #[cfg(target_os = "macos")]
macos::make_menubar(frame.get_host(), is_index); macos::make_menubar(frame.get_host(), args.is_empty());
let page; let page;
if args.len() > 1 && args[0] == "--play" { if args.len() > 1 && args[0] == "--play" {
args[0] = "--connect".to_owned(); args[0] = "--connect".to_owned();
@ -99,7 +90,7 @@ pub fn start(args: &mut [String]) {
.to_owned(); .to_owned();
args[1] = id; args[1] = id;
} }
if is_index { if args.is_empty() {
let childs: Childs = Default::default(); let childs: Childs = Default::default();
let cloned = childs.clone(); let cloned = childs.clone();
std::thread::spawn(move || check_zombie(cloned)); std::thread::spawn(move || check_zombie(cloned));
@ -157,13 +148,8 @@ pub fn start(args: &mut [String]) {
.unwrap_or("".to_owned()), .unwrap_or("".to_owned()),
page page
)); ));
if is_server {
frame.collapse(true);
frame.run_loop();
} else {
frame.run_app(); frame.run_app();
} }
}
#[cfg(windows)] #[cfg(windows)]
fn start_tray() -> hbb_common::ResultType<()> { fn start_tray() -> hbb_common::ResultType<()> {

View File

@ -745,10 +745,6 @@ function self.closing() {
// return false; // can prevent window close // return false; // can prevent window close
var (x, y, w, h) = view.box(#rectw, #border, #screen); var (x, y, w, h) = view.box(#rectw, #border, #screen);
handler.closing(x, y, w, h); 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; return true;
} }

View File

@ -42,7 +42,7 @@ impl DelegateState {
} }
} }
static mut FIRST_TIME: bool = true; static mut LAUCHED: bool = false;
impl AppHandler for Rc<Host> { impl AppHandler for Rc<Host> {
fn command(&mut self, cmd: u32) { fn command(&mut self, cmd: u32) {
@ -53,12 +53,6 @@ impl AppHandler for Rc<Host> {
let _ = self.call_function("awake", &make_args![]); let _ = self.call_function("awake", &make_args![]);
let _ = self.call_function("showSettings", &make_args![]); let _ = self.call_function("showSettings", &make_args![]);
} else if cmd == AWAKE { } 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![]); let _ = self.call_function("awake", &make_args![]);
} }
} }
@ -114,6 +108,9 @@ unsafe fn set_delegate(handler: Option<Box<dyn AppHandler>>) {
} }
extern "C" fn application_did_finish_launching(_this: &mut Object, _: Sel, _notification: id) { extern "C" fn application_did_finish_launching(_this: &mut Object, _: Sel, _notification: id) {
unsafe {
LAUCHED = true;
}
unsafe { unsafe {
let () = msg_send![NSApp(), activateIgnoringOtherApps: YES]; let () = msg_send![NSApp(), activateIgnoringOtherApps: YES];
} }
@ -125,6 +122,13 @@ extern "C" fn application_should_handle_open_untitled_file(
_sender: id, _sender: id,
) -> BOOL { ) -> BOOL {
unsafe { 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 c_void = *this.get_ivar(APP_HANDLER_IVAR);
let inner = &mut *(inner as *mut DelegateState); let inner = &mut *(inner as *mut DelegateState);
(*inner).command(AWAKE); (*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 { extern "C" fn application_should_handle_reopen(_this: &mut Object, _: Sel, _sender: id) -> BOOL {
hbb_common::log::debug!("reopen");
YES YES
} }
extern "C" fn application_did_become_active(_this: &mut Object, _: Sel, _sender: id) -> BOOL { extern "C" fn application_did_become_active(_this: &mut Object, _: Sel, _sender: id) -> BOOL {
hbb_common::log::debug!("active");
YES YES
} }
extern "C" fn application_did_become_unhide(_this: &mut Object, _: Sel, _sender: id) -> BOOL { extern "C" fn application_did_become_unhide(_this: &mut Object, _: Sel, _sender: id) -> BOOL {
hbb_common::log::debug!("unhide");
YES YES
} }
@ -221,14 +222,16 @@ pub fn make_menubar(host: Rc<Host>, is_index: bool) {
} }
} }
pub fn hide_dock() { pub fn show_dock() {
unsafe { unsafe {
NSApp().setActivationPolicy_(NSApplicationActivationPolicyAccessory); NSApp().setActivationPolicy_(NSApplicationActivationPolicyRegular);
} }
} }
pub fn make_tray() { pub fn make_tray() {
hide_dock(); unsafe {
set_delegate(None);
}
use tray_item::TrayItem; use tray_item::TrayItem;
if let Ok(mut tray) = TrayItem::new(&crate::get_app_name(), "mac-tray.png") { if let Ok(mut tray) = TrayItem::new(&crate::get_app_name(), "mac-tray.png") {
tray.add_label(&format!( 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();
}