Exit in mac tray

This commit is contained in:
rustdesk 2023-02-23 20:01:50 +08:00
parent fdc04266f6
commit bb26ba3384
3 changed files with 38 additions and 13 deletions

View File

@ -171,7 +171,7 @@ pub fn is_installed_daemon(prompt: bool) -> bool {
false 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 // to-do: do together with win/linux about refactory start/stop service
if !is_installed_daemon(false) { if !is_installed_daemon(false) {
return false; return false;
@ -206,14 +206,21 @@ 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("sh") if show_new_window {
.arg("-c") std::process::Command::new("sh")
.arg(&format!( .arg("-c")
"sleep 0.5; open /Applications/{}.app", .arg(&format!(
crate::get_app_name(), "sleep 0.5; open /Applications/{}.app",
)) crate::get_app_name(),
.spawn() ))
.ok(); .spawn()
.ok();
} else {
std::process::Command::new("pkill")
.arg(crate::get_app_name())
.status()
.ok();
}
quit_gui(); quit_gui();
} }
} }

View File

@ -107,7 +107,10 @@ pub fn make_tray() -> hbb_common::ResultType<()> {
// https://github.com/tauri-apps/tray-icon/blob/dev/examples/tao.rs // https://github.com/tauri-apps/tray-icon/blob/dev/examples/tao.rs
use hbb_common::anyhow::Context; use hbb_common::anyhow::Context;
use tao::event_loop::{ControlFlow, EventLoopBuilder}; 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(); let mode = dark_light::detect();
const LIGHT: &[u8] = include_bytes!("../res/mac-tray-light-x2.png"); const LIGHT: &[u8] = include_bytes!("../res/mac-tray-light-x2.png");
const DARK: &[u8] = include_bytes!("../res/mac-tray-dark-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 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( let _tray_icon = Some(
TrayIconBuilder::new() TrayIconBuilder::new()
.with_menu(Box::new(tray_menu))
.with_tooltip(format!( .with_tooltip(format!(
"{} {}", "{} {}",
crate::get_app_name(), crate::get_app_name(),
@ -139,6 +147,7 @@ pub fn make_tray() -> hbb_common::ResultType<()> {
.build()?, .build()?,
); );
let menu_channel = MenuEvent::receiver();
let tray_channel = TrayEvent::receiver(); let tray_channel = TrayEvent::receiver();
let mut docker_hiden = false; let mut docker_hiden = false;
@ -149,8 +158,17 @@ pub fn make_tray() -> hbb_common::ResultType<()> {
} }
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
if tray_channel.try_recv().is_ok() { if let Ok(event) = menu_channel.try_recv() {
crate::platform::macos::handle_application_should_open_untitled_file(); 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();
}
} }
}); });
} }

View File

@ -295,7 +295,7 @@ pub fn set_option(key: String, value: String) {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
if &key == "stop-service" { if &key == "stop-service" {
let is_stop = value == "Y"; let is_stop = value == "Y";
if is_stop && crate::platform::macos::uninstall() { if is_stop && crate::platform::macos::uninstall(true) {
return; return;
} }
} }