Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added text support to system tray - macos, closes #65 #369

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/system_tray_title.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "minor"
---

* `SystemTray` has new `set_title(title: &str)` method to set title, only works on macos.
24 changes: 23 additions & 1 deletion examples/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ fn main() {
// inject submenu into tray_menu
tray_menu.add_submenu("Sub menu", true, submenu);

// tray macos text submenus
let mut text_submenu = Menu::new();
let only_text_element = text_submenu.add_item(MenuItemAttributes::new("Only text"));
let only_icon_element = text_submenu.add_item(MenuItemAttributes::new("Only icon"));
let icon_and_text_element = text_submenu.add_item(MenuItemAttributes::new("Icon and text"));

#[cfg(target_os = "macos")]
tray_menu.add_submenu("Icon/Text options", true, text_submenu);

// add quit button
let quit_element = tray_menu.add_item(MenuItemAttributes::new("Quit"));

Expand Down Expand Up @@ -156,8 +165,21 @@ fn main() {
origin: MenuType::ContextMenu,
..
} => {
if menu_id == only_icon_element.clone().id() {
#[cfg(target_os = "macos")]
system_tray.set_title("");
system_tray.set_icon(icon.clone());
} else if menu_id == only_text_element.clone().id() {
#[cfg(target_os = "macos")]
system_tray.set_title("Tao Menu Title");
system_tray.set_icon(vec![]);
} else if menu_id == icon_and_text_element.clone().id() {
#[cfg(target_os = "macos")]
system_tray.set_title("Tao Menu Title");
system_tray.set_icon(icon.clone());
}
// Click on Open new window or focus item
if menu_id == open_new_window_element.clone().id()
else if menu_id == open_new_window_element.clone().id()
|| menu_id == focus_all_window.clone().id()
{
create_window_or_focus();
Expand Down
18 changes: 14 additions & 4 deletions src/platform_impl/macos/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ 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::{NSAutoreleasePool, NSData, NSPoint, NSSize},
foundation::{NSAutoreleasePool, NSData, NSPoint, NSSize, NSString},
};
use objc::{
declare::ClassDecl,
Expand All @@ -38,7 +38,7 @@ impl SystemTrayBuilder {
pub fn new(icon: Vec<u8>, tray_menu: Option<Menu>) -> Self {
unsafe {
let ns_status_bar = NSStatusBar::systemStatusBar(nil)
.statusItemWithLength_(NSSquareStatusItemLength)
.statusItemWithLength_(NSVariableStatusItemLength)
.autorelease();

Self {
Expand Down Expand Up @@ -120,6 +120,15 @@ impl SystemTray {
}
}

pub fn set_title(&mut 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;
Expand All @@ -140,6 +149,7 @@ impl SystemTray {

button.setImage_(nsimage);
let _: () = msg_send![nsimage, setSize: new_size];
let _: () = msg_send![button, setImagePosition: 2]; // https://developer.apple.com/documentation/appkit/nscellimageposition/nsimageleft
let is_template = match self.icon_is_template {
true => YES,
false => NO,
Expand Down
10 changes: 10 additions & 0 deletions src/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ impl SystemTray {
self.0.set_icon(icon)
}

/// Set new tray menu title.
///
/// ## Platform-specific
///
/// Only works on **macOS**
#[cfg(target_os = "macos")]
pub fn set_title(&mut self, title: &str) {
self.0.set_title(title)
}

Comment on lines +108 to +117
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in SystemTrayExtMacOS trait and you should make a builder variant for SystemTrayBuilderExtMacOS.

You can find them in src/platform/macos.rs

/// Set new tray menu.
pub fn set_menu(&mut self, tray_menu: &ContextMenu) {
self.0.set_menu(&tray_menu.0.menu_platform)
Expand Down