diff --git a/.changes/macos-sytem-tray-title.md b/.changes/macos-sytem-tray-title.md new file mode 100644 index 000000000..642d6f4c4 --- /dev/null +++ b/.changes/macos-sytem-tray-title.md @@ -0,0 +1,5 @@ +--- +"tao": "minor" +--- + +Added `SystemTrayExtMacOS::set_title` to `SystemTray` and `SystemTrayBuilderExtMacOS::with_title` to set the tray icon title on MacOS diff --git a/examples/system_tray.rs b/examples/system_tray.rs index a33040f4c..aa65e1c9c 100644 --- a/examples/system_tray.rs +++ b/examples/system_tray.rs @@ -17,6 +17,9 @@ fn main() { TrayId, }; + #[cfg(target_os = "macos")] + use tao::platform::macos::{SystemTrayBuilderExtMacOS, SystemTrayExtMacOS}; + env_logger::init(); let event_loop = EventLoop::new(); @@ -31,11 +34,12 @@ fn main() { let second_tray_id = TrayId::new("2nd-tray"); let icon = load_icon(std::path::Path::new(path)); let mut tray_menu = Menu::new(); + let menu_item = tray_menu.add_item(MenuItemAttributes::new("Set tray title (macos)")); #[cfg(target_os = "macos")] { tray_menu - .add_item(MenuItemAttributes::new("Item 1")) + .add_item(MenuItemAttributes::new("Menu Item with icon")) .set_icon(icon.clone()); } @@ -48,10 +52,18 @@ fn main() { .build(&event_loop) .unwrap(); - #[cfg(not(target_os = "linux"))] + #[cfg(target_os = "windows")] + let system_tray = SystemTrayBuilder::new(icon.clone(), Some(tray_menu)) + .with_id(main_tray_id) + .with_tooltip("tao - windowing creation library") + .build(&event_loop) + .unwrap(); + + #[cfg(target_os = "macos")] let system_tray = SystemTrayBuilder::new(icon.clone(), Some(tray_menu)) .with_id(main_tray_id) .with_tooltip("tao - windowing creation library") + .with_title("Tao") .build(&event_loop) .unwrap(); @@ -86,6 +98,13 @@ fn main() { *control_flow = ControlFlow::Exit; } else if menu_id == log.clone().id() { println!("Log clicked"); + } else if menu_id == menu_item.clone().id() { + #[cfg(target_os = "macos")] + { + if let Some(tray) = system_tray.as_mut() { + tray.set_title("Tao - clicked"); + } + } } } Event::TrayEvent { diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 643fe3e0b..c5b47b8bc 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -515,6 +515,9 @@ pub trait SystemTrayBuilderExtMacOS { /// Enables or disables showing the tray menu on left click, default is true. fn with_menu_on_left_click(self, enable: bool) -> Self; + + /// Sets the tray icon title + fn with_title(self, title: &str) -> Self; } #[cfg(feature = "tray")] @@ -528,6 +531,11 @@ impl SystemTrayBuilderExtMacOS for SystemTrayBuilder { self.platform_tray_builder.system_tray.menu_on_left_click = enable; self } + + fn with_title(mut self, title: &str) -> Self { + self.platform_tray_builder.system_tray.title = Some(title.to_owned()); + self + } } #[cfg(feature = "tray")] @@ -540,6 +548,9 @@ pub trait SystemTrayExtMacOS { /// Enables or disables showing the tray menu on left click, default is true. fn enable_menu_on_left_click(&mut self, enable: bool); + + /// Sets the tray icon title + fn set_title(&mut self, title: &str); } #[cfg(feature = "tray")] @@ -551,4 +562,8 @@ impl SystemTrayExtMacOS for SystemTray { fn enable_menu_on_left_click(&mut self, enable: bool) { self.0.menu_on_left_click = enable } + + fn set_title(&mut self, title: &str) { + self.0.set_title(title) + } } diff --git a/src/platform_impl/macos/system_tray.rs b/src/platform_impl/macos/system_tray.rs index 8a6694d46..c51763e0a 100644 --- a/src/platform_impl/macos/system_tray.rs +++ b/src/platform_impl/macos/system_tray.rs @@ -18,8 +18,8 @@ use crate::{ }; use cocoa::{ appkit::{ - NSButton, NSEventMask, NSEventModifierFlags, NSEventType, NSImage, NSSquareStatusItemLength, - NSStatusBar, NSStatusItem, NSWindow, + NSButton, NSEventMask, NSEventModifierFlags, NSEventType, NSImage, NSStatusBar, NSStatusItem, + NSVariableStatusItemLength, NSWindow, }, base::{id, nil, NO, YES}, foundation::{NSData, NSPoint, NSSize, NSString}, @@ -40,7 +40,7 @@ impl SystemTrayBuilder { pub fn new(icon: Icon, tray_menu: Option