From bb26ba3384bde03c5a45c931a14d0cfcd4d5cea4 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Thu, 23 Feb 2023 20:01:50 +0800 Subject: [PATCH] Exit in mac tray --- src/platform/macos.rs | 25 ++++++++++++++++--------- src/tray.rs | 24 +++++++++++++++++++++--- src/ui_interface.rs | 2 +- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 910c26982..3e19cca28 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -171,7 +171,7 @@ pub fn is_installed_daemon(prompt: bool) -> bool { false } -pub fn uninstall() -> bool { +pub fn uninstall(show_new_window: bool) -> bool { // to-do: do together with win/linux about refactory start/stop service if !is_installed_daemon(false) { return false; @@ -206,14 +206,21 @@ pub fn uninstall() -> bool { .args(&["remove", &format!("{}_server", crate::get_full_name())]) .status() .ok(); - std::process::Command::new("sh") - .arg("-c") - .arg(&format!( - "sleep 0.5; open /Applications/{}.app", - crate::get_app_name(), - )) - .spawn() - .ok(); + if show_new_window { + std::process::Command::new("sh") + .arg("-c") + .arg(&format!( + "sleep 0.5; open /Applications/{}.app", + crate::get_app_name(), + )) + .spawn() + .ok(); + } else { + std::process::Command::new("pkill") + .arg(crate::get_app_name()) + .status() + .ok(); + } quit_gui(); } } diff --git a/src/tray.rs b/src/tray.rs index 5e1620036..617ec2c93 100644 --- a/src/tray.rs +++ b/src/tray.rs @@ -107,7 +107,10 @@ pub fn make_tray() -> hbb_common::ResultType<()> { // https://github.com/tauri-apps/tray-icon/blob/dev/examples/tao.rs use hbb_common::anyhow::Context; use tao::event_loop::{ControlFlow, EventLoopBuilder}; - use tray_icon::{TrayEvent, TrayIconBuilder}; + use tray_icon::{ + menu::{Menu, MenuEvent, MenuItem}, + ClickEvent, TrayEvent, TrayIconBuilder, + }; let mode = dark_light::detect(); const LIGHT: &[u8] = include_bytes!("../res/mac-tray-light-x2.png"); const DARK: &[u8] = include_bytes!("../res/mac-tray-dark-x2.png"); @@ -128,8 +131,13 @@ pub fn make_tray() -> hbb_common::ResultType<()> { let event_loop = EventLoopBuilder::new().build(); + let tray_menu = Menu::new(); + let quit_i = MenuItem::new(crate::client::translate("Exit".to_owned()), true, None); + tray_menu.append_items(&[&quit_i]); + let _tray_icon = Some( TrayIconBuilder::new() + .with_menu(Box::new(tray_menu)) .with_tooltip(format!( "{} {}", crate::get_app_name(), @@ -139,6 +147,7 @@ pub fn make_tray() -> hbb_common::ResultType<()> { .build()?, ); + let menu_channel = MenuEvent::receiver(); let tray_channel = TrayEvent::receiver(); let mut docker_hiden = false; @@ -149,8 +158,17 @@ pub fn make_tray() -> hbb_common::ResultType<()> { } *control_flow = ControlFlow::Wait; - if tray_channel.try_recv().is_ok() { - crate::platform::macos::handle_application_should_open_untitled_file(); + if let Ok(event) = menu_channel.try_recv() { + if event.id == quit_i.id() { + crate::platform::macos::uninstall(false); + } + println!("{event:?}"); + } + + if let Ok(event) = tray_channel.try_recv() { + if event.event == ClickEvent::Double { + crate::platform::macos::handle_application_should_open_untitled_file(); + } } }); } diff --git a/src/ui_interface.rs b/src/ui_interface.rs index f44bb4eea..dd111f86e 100644 --- a/src/ui_interface.rs +++ b/src/ui_interface.rs @@ -295,7 +295,7 @@ pub fn set_option(key: String, value: String) { #[cfg(target_os = "macos")] if &key == "stop-service" { let is_stop = value == "Y"; - if is_stop && crate::platform::macos::uninstall() { + if is_stop && crate::platform::macos::uninstall(true) { return; } }