fix mac tray icon to use template icon, no need to know theme any more

This commit is contained in:
rustdesk
2024-02-16 12:29:32 +08:00
parent 51c603a3a6
commit f7f3bc8bee
5 changed files with 273 additions and 206 deletions

View File

@@ -18,32 +18,12 @@ pub fn make_tray() -> hbb_common::ResultType<()> {
use tao::event_loop::{ControlFlow, EventLoopBuilder};
use tray_icon::{
menu::{Menu, MenuEvent, MenuItem},
TrayEvent, TrayIconBuilder,
TrayIconBuilder, TrayIconEvent as TrayEvent,
};
let icon;
#[cfg(target_os = "macos")]
{
const DARK: &[u8] = include_bytes!("../res/mac-tray-dark-x2.png");
const LIGHT: &[u8] = include_bytes!("../res/mac-tray-light-x2.png");
let output = std::process::Command::new("sw_vers")
.args(&["-productVersion"])
.output()
.map(|x| x.stdout)
.unwrap_or_default();
let version: f64 = String::from_utf8_lossy(output.as_slice())
.trim()
.parse()
.unwrap_or_default();
icon = if version >= 14. {
// workaround for Sonoma, always light menubar
DARK
} else {
let mode = dark_light::detect();
match mode {
dark_light::Mode::Dark => LIGHT,
_ => DARK,
}
};
icon = include_bytes!("../res/mac-tray-dark-x2.png"); // use as template, so color is not important
}
#[cfg(not(target_os = "macos"))]
{
@@ -57,7 +37,7 @@ pub fn make_tray() -> hbb_common::ResultType<()> {
let rgba = image.into_raw();
(rgba, width, height)
};
let icon = tray_icon::icon::Icon::from_rgba(icon_rgba, icon_width, icon_height)
let icon = tray_icon::Icon::from_rgba(icon_rgba, icon_width, icon_height)
.context("Failed to open icon")?;
let event_loop = EventLoopBuilder::new().build();
@@ -87,6 +67,7 @@ pub fn make_tray() -> hbb_common::ResultType<()> {
.with_menu(Box::new(tray_menu))
.with_tooltip(tooltip(0))
.with_icon(icon)
.with_icon_as_template(true) // mac only
.build()?,
);
let _tray_icon = Arc::new(Mutex::new(_tray_icon));