https://github.com/rustdesk/rustdesk/issues/77 mac tray, still no find good way to open rustdesk from finder after tray

shown
This commit is contained in:
rustdesk
2022-04-28 21:32:44 +08:00
parent de44e45ed6
commit c1f5c20e94
13 changed files with 631 additions and 125 deletions

View File

@@ -744,11 +744,12 @@ $(body).content(<App />);
function self.closing() {
// return false; // can prevent window close
var (x, y, w, h) = view.box(#rectw, #border, #screen);
handler.save_size(x, y, w, h);
if (is_osx) {
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;
}
function self.ready() {

View File

@@ -1,6 +1,6 @@
#[cfg(target_os = "macos")]
use cocoa::{
appkit::{NSApp, NSApplication, NSMenu, NSMenuItem},
appkit::{NSApp, NSApplication, NSApplicationActivationPolicy::*, NSMenu, NSMenuItem},
base::{id, nil, YES},
foundation::{NSAutoreleasePool, NSString},
};
@@ -56,7 +56,10 @@ impl AppHandler for Rc<Host> {
let _ = self.call_function("showSettings", &make_args![]);
} else if cmd == AWAKE {
if START_TM.lock().unwrap().elapsed().as_millis() < 1000 {
hbb_common::log::debug!("First click on docker icon {:?}", START_TM.lock().unwrap().elapsed());
hbb_common::log::debug!(
"First click on docker icon {:?}",
START_TM.lock().unwrap().elapsed()
);
return;
}
let _ = self.call_function("awake", &make_args![]);
@@ -81,6 +84,26 @@ unsafe fn set_delegate(handler: Option<Box<dyn AppHandler>>) {
application_should_handle_open_untitled_file as extern "C" fn(&mut Object, Sel, id) -> BOOL,
);
decl.add_method(
sel!(applicationDidBecomeActive:),
application_did_become_active as extern "C" fn(&mut Object, Sel, id) -> BOOL,
);
decl.add_method(
sel!(applicationDidUnhide:),
application_did_become_unhide as extern "C" fn(&mut Object, Sel, id) -> BOOL,
);
decl.add_method(
sel!(applicationShouldHandleReopen:),
application_should_handle_reopen as extern "C" fn(&mut Object, Sel, id) -> BOOL,
);
decl.add_method(
sel!(applicationWillTerminate:),
application_will_terminate as extern "C" fn(&mut Object, Sel, id) -> BOOL,
);
decl.add_method(
sel!(handleMenuItem:),
handle_menu_item as extern "C" fn(&mut Object, Sel, id),
@@ -113,6 +136,40 @@ extern "C" fn application_should_handle_open_untitled_file(
YES
}
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
}
extern "C" fn application_will_terminate(_this: &mut Object, _: Sel, _sender: id) -> BOOL {
if std::env::args().len() == 1 || std::env::args().nth(1) == Some("--server".to_owned()) {
hide_dock();
}
YES
}
/// This handles menu items in the case that all windows are closed.
extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
unsafe {
@@ -182,3 +239,30 @@ pub fn make_menubar(host: Rc<Host>, is_index: bool) {
NSApp().setMainMenu_(menubar);
}
}
pub fn hide_dock() {
unsafe {
NSApp().setActivationPolicy_(NSApplicationActivationPolicyAccessory);
}
}
pub fn make_tray() {
hide_dock();
use tray_item::TrayItem;
if let Ok(mut tray) = TrayItem::new(&crate::get_app_name(), "mac-tray.png") {
tray.add_label(&format!(
"{} {}",
crate::get_app_name(),
crate::lang::translate("Service is running".to_owned())
))
.ok();
let inner = tray.inner_mut();
inner.add_quit_item(&crate::lang::translate("Quit".to_owned()));
inner.display();
} else {
loop {
std::thread::sleep(std::time::Duration::from_secs(3));
}
}
}