Skip to content

Commit

Permalink
feat: added text support to system tray for macos, closes #65 (#554)
Browse files Browse the repository at this point in the history
* feat: added text support to system tray for macos, closes #65

* extract image position into variable

* boring
  • Loading branch information
vojty committed Sep 10, 2022
1 parent 4524d5d commit 972307d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/macos-sytem-tray-title.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "minor"
---

Added `SystemTrayExtMacOS::set_title` to `SystemTray` and `SystemTrayBuilderExtMacOS::with_title` to set the tray icon title on MacOS
23 changes: 21 additions & 2 deletions examples/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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());
}

Expand All @@ -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();

Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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")]
Expand All @@ -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")]
Expand All @@ -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)
}
}
25 changes: 22 additions & 3 deletions src/platform_impl/macos/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -40,7 +40,7 @@ impl SystemTrayBuilder {
pub fn new(icon: Icon, tray_menu: Option<Menu>) -> Self {
unsafe {
let ns_status_bar =
NSStatusBar::systemStatusBar(nil).statusItemWithLength_(NSSquareStatusItemLength);
NSStatusBar::systemStatusBar(nil).statusItemWithLength_(NSVariableStatusItemLength);
let _: () = msg_send![ns_status_bar, retain];

Self {
Expand All @@ -50,6 +50,7 @@ impl SystemTrayBuilder {
menu_on_left_click: true,
tray_menu,
ns_status_bar,
title: None,
},
}
}
Expand Down Expand Up @@ -100,6 +101,11 @@ impl SystemTrayBuilder {
if let Some(tooltip) = tooltip {
self.system_tray.set_tooltip(&tooltip);
}

// set up title if provided
if let Some(title) = &self.system_tray.title {
self.system_tray.set_title(title);
}
}

Ok(RootSystemTray(self.system_tray))
Expand All @@ -114,6 +120,7 @@ pub struct SystemTray {
pub(crate) menu_on_left_click: bool,
pub(crate) tray_menu: Option<Menu>,
pub(crate) ns_status_bar: id,
pub(crate) title: Option<String>,
}

impl Drop for SystemTray {
Expand Down Expand Up @@ -149,9 +156,20 @@ impl SystemTray {
}
}

pub fn set_title(&self, title: &str) {
unsafe {
NSButton::setTitle_(
self.ns_status_bar.button(),
NSString::alloc(nil).init_str(title),
);
}
}

fn create_button_with_icon(&self) {
const ICON_WIDTH: f64 = 18.0;
const ICON_HEIGHT: f64 = 18.0;
// The image is to the right of the title https://developer.apple.com/documentation/appkit/nscellimageposition/nsimageleft
const NSIMAGE_LEFT: i32 = 2;

let icon = self.icon.inner.to_png();

Expand All @@ -171,6 +189,7 @@ impl SystemTray {

button.setImage_(nsimage);
let _: () = msg_send![nsimage, setSize: new_size];
let _: () = msg_send![button, setImagePosition: NSIMAGE_LEFT];
let is_template = match self.icon_is_template {
true => YES,
false => NO,
Expand Down

0 comments on commit 972307d

Please sign in to comment.