Skip to content

Commit 8f1ace7

Browse files
vojtylucasfernog
andauthored
feat: expose set_title for MacOS tray (#5182)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 63011ca commit 8f1ace7

File tree

11 files changed

+130
-5
lines changed

11 files changed

+130
-5
lines changed

.changes/tray-title-config.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-utils": minor
3+
---
4+
5+
Added `title` option on the system tray configuration (macOS only).

.changes/tray-title.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": minor
3+
"tauri-runtime": minor
4+
"tauri-runtime-wry": minor
5+
---
6+
7+
Added methods to set the system tray title on macOS.

core/tauri-runtime-wry/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,8 @@ pub enum TrayMessage {
10791079
UpdateIcon(Icon),
10801080
#[cfg(target_os = "macos")]
10811081
UpdateIconAsTemplate(bool),
1082+
#[cfg(target_os = "macos")]
1083+
UpdateTitle(String),
10821084
Create(SystemTray, Sender<Result<()>>),
10831085
Destroy,
10841086
}
@@ -2488,6 +2490,12 @@ fn handle_user_message<T: UserEvent>(
24882490
tray.set_icon_as_template(is_template);
24892491
}
24902492
}
2493+
#[cfg(target_os = "macos")]
2494+
TrayMessage::UpdateTitle(title) => {
2495+
if let Some(tray) = &mut *tray_context.tray.lock().unwrap() {
2496+
tray.set_title(&title);
2497+
}
2498+
}
24912499
TrayMessage::Create(_tray, _tx) => {
24922500
// already handled
24932501
}

core/tauri-runtime-wry/src/system_tray.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ pub fn create_tray<T>(
107107
{
108108
builder = builder
109109
.with_icon_as_template(system_tray.icon_as_template)
110-
.with_menu_on_left_click(system_tray.menu_on_left_click)
110+
.with_menu_on_left_click(system_tray.menu_on_left_click);
111+
112+
if let Some(title) = system_tray.title {
113+
builder = builder.with_title(&title);
114+
}
111115
}
112116

113117
let tray = builder
@@ -156,6 +160,17 @@ impl<T: UserEvent> TrayHandle for SystemTrayHandle<T> {
156160
.map_err(|_| Error::FailedToSendMessage)
157161
}
158162

163+
#[cfg(target_os = "macos")]
164+
fn set_title(&self, title: &str) -> tauri_runtime::Result<()> {
165+
self
166+
.proxy
167+
.send_event(Message::Tray(
168+
self.id,
169+
TrayMessage::UpdateTitle(title.to_owned()),
170+
))
171+
.map_err(|_| Error::FailedToSendMessage)
172+
}
173+
159174
fn destroy(&self) -> Result<()> {
160175
self
161176
.proxy

core/tauri-runtime/src/lib.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub struct SystemTray {
5050
pub icon_as_template: bool,
5151
#[cfg(target_os = "macos")]
5252
pub menu_on_left_click: bool,
53+
#[cfg(target_os = "macos")]
54+
pub title: Option<String>,
5355
pub on_event: Option<Box<TrayEventHandler>>,
5456
}
5557

@@ -63,7 +65,8 @@ impl fmt::Debug for SystemTray {
6365
#[cfg(target_os = "macos")]
6466
{
6567
d.field("icon_as_template", &self.icon_as_template)
66-
.field("menu_on_left_click", &self.menu_on_left_click);
68+
.field("menu_on_left_click", &self.menu_on_left_click)
69+
.field("title", &self.title);
6770
}
6871
d.finish()
6972
}
@@ -81,6 +84,8 @@ impl Clone for SystemTray {
8184
icon_as_template: self.icon_as_template,
8285
#[cfg(target_os = "macos")]
8386
menu_on_left_click: self.menu_on_left_click,
87+
#[cfg(target_os = "macos")]
88+
title: self.title.clone(),
8489
}
8590
}
8691
}
@@ -96,6 +101,8 @@ impl Default for SystemTray {
96101
icon_as_template: false,
97102
#[cfg(target_os = "macos")]
98103
menu_on_left_click: false,
104+
#[cfg(target_os = "macos")]
105+
title: None,
99106
on_event: None,
100107
}
101108
}
@@ -142,6 +149,13 @@ impl SystemTray {
142149
self
143150
}
144151

152+
#[cfg(target_os = "macos")]
153+
#[must_use]
154+
pub fn with_title(mut self, title: &str) -> Self {
155+
self.title = Some(title.to_owned());
156+
self
157+
}
158+
145159
/// Sets the menu to show when the system tray is right clicked.
146160
#[must_use]
147161
pub fn with_menu(mut self, menu: menu::SystemTrayMenu) -> Self {

core/tauri-runtime/src/menu.rs

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ pub trait TrayHandle: fmt::Debug + Clone + Send + Sync {
152152
fn update_item(&self, id: u16, update: MenuUpdate) -> crate::Result<()>;
153153
#[cfg(target_os = "macos")]
154154
fn set_icon_as_template(&self, is_template: bool) -> crate::Result<()>;
155+
#[cfg(target_os = "macos")]
156+
fn set_title(&self, title: &str) -> crate::Result<()>;
155157
fn destroy(&self) -> crate::Result<()>;
156158
}
157159

core/tauri-utils/src/config.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2391,6 +2391,8 @@ pub struct SystemTrayConfig {
23912391
alias = "menu-on-left-click"
23922392
)]
23932393
pub menu_on_left_click: bool,
2394+
/// Title for MacOS tray
2395+
pub title: Option<String>,
23942396
}
23952397

23962398
fn default_tray_menu_on_left_click() -> bool {
@@ -3350,12 +3352,14 @@ mod build {
33503352
let icon_as_template = self.icon_as_template;
33513353
let menu_on_left_click = self.menu_on_left_click;
33523354
let icon_path = path_buf_lit(&self.icon_path);
3355+
let title = opt_str_lit(self.title.as_ref());
33533356
literal_struct!(
33543357
tokens,
33553358
SystemTrayConfig,
33563359
icon_path,
33573360
icon_as_template,
3358-
menu_on_left_click
3361+
menu_on_left_click,
3362+
title
33593363
);
33603364
}
33613365
}

core/tauri/src/app/tray.rs

+46
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub struct SystemTray {
6262
menu_on_left_click_set: bool,
6363
#[cfg(target_os = "macos")]
6464
icon_as_template_set: bool,
65+
#[cfg(target_os = "macos")]
66+
title: Option<String>,
6567
}
6668

6769
impl fmt::Debug for SystemTray {
@@ -94,6 +96,8 @@ impl Default for SystemTray {
9496
icon_as_template_set: false,
9597
#[cfg(target_os = "macos")]
9698
menu_on_left_click_set: false,
99+
#[cfg(target_os = "macos")]
100+
title: None,
97101
}
98102
}
99103
}
@@ -228,6 +232,31 @@ impl SystemTray {
228232
self
229233
}
230234

235+
/// Sets the menu title`
236+
///
237+
/// # Examples
238+
///
239+
/// ```
240+
/// use tauri::SystemTray;
241+
///
242+
/// tauri::Builder::default()
243+
/// .setup(|app| {
244+
/// let mut tray_builder = SystemTray::new();
245+
/// #[cfg(target_os = "macos")]
246+
/// {
247+
/// tray_builder = tray_builder.with_title("My App");
248+
/// }
249+
/// let tray_handle = tray_builder.build(app)?;
250+
/// Ok(())
251+
/// });
252+
/// ```
253+
#[cfg(target_os = "macos")]
254+
#[must_use]
255+
pub fn with_title(mut self, title: &str) -> Self {
256+
self.title = Some(title.to_owned());
257+
self
258+
}
259+
231260
/// Sets the event listener for this system tray.
232261
///
233262
/// # Examples
@@ -342,6 +371,14 @@ impl SystemTray {
342371
.as_ref()
343372
.map_or(false, |t| t.menu_on_left_click);
344373
}
374+
if self.title.is_none() {
375+
self.title = manager
376+
.config()
377+
.tauri
378+
.system_tray
379+
.as_ref()
380+
.and_then(|t| t.title.clone())
381+
}
345382
}
346383

347384
let tray_id = self.id.clone();
@@ -372,6 +409,9 @@ impl SystemTray {
372409
{
373410
runtime_tray = runtime_tray.with_icon_as_template(self.icon_as_template);
374411
runtime_tray = runtime_tray.with_menu_on_left_click(self.menu_on_left_click);
412+
if let Some(title) = self.title {
413+
runtime_tray = runtime_tray.with_title(&title);
414+
}
375415
}
376416

377417
let id = runtime_tray.id;
@@ -564,6 +604,12 @@ impl<R: Runtime> SystemTrayHandle<R> {
564604
.map_err(Into::into)
565605
}
566606

607+
/// Adds the title to the tray menu
608+
#[cfg(target_os = "macos")]
609+
pub fn set_title(&self, title: &str) -> crate::Result<()> {
610+
self.inner.set_title(title).map_err(Into::into)
611+
}
612+
567613
/// Destroys this system tray.
568614
pub fn destroy(&self) -> crate::Result<()> {
569615
self.inner.destroy().map_err(Into::into)

core/tauri/src/test/mock_runtime.rs

+5
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ impl TrayHandle for MockTrayHandler {
539539
Ok(())
540540
}
541541

542+
#[cfg(target_os = "macos")]
543+
fn set_title(&self, title: &str) -> tauri_runtime::Result<()> {
544+
Ok(())
545+
}
546+
542547
fn destroy(&self) -> Result<()> {
543548
Ok(())
544549
}

examples/api/src-tauri/src/desktop.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,22 @@ pub fn main() {
5858
}
5959

6060
fn create_tray(app: &tauri::App) -> tauri::Result<()> {
61-
let tray_menu1 = SystemTrayMenu::new()
61+
let mut tray_menu1 = SystemTrayMenu::new()
6262
.add_item(CustomMenuItem::new("toggle", "Toggle"))
6363
.add_item(CustomMenuItem::new("new", "New window"))
6464
.add_item(CustomMenuItem::new("icon_1", "Tray Icon 1"))
65-
.add_item(CustomMenuItem::new("icon_2", "Tray Icon 2"))
65+
.add_item(CustomMenuItem::new("icon_2", "Tray Icon 2"));
66+
67+
#[cfg(target_os = "macos")]
68+
{
69+
tray_menu1 = tray_menu1.add_item(CustomMenuItem::new("set_title", "Set Title"));
70+
}
71+
72+
tray_menu1 = tray_menu1
6673
.add_item(CustomMenuItem::new("switch_menu", "Switch Menu"))
6774
.add_item(CustomMenuItem::new("exit_app", "Quit"))
6875
.add_item(CustomMenuItem::new("destroy", "Destroy"));
76+
6977
let tray_menu2 = SystemTrayMenu::new()
7078
.add_item(CustomMenuItem::new("toggle", "Toggle"))
7179
.add_item(CustomMenuItem::new("new", "New window"))
@@ -118,6 +126,10 @@ fn create_tray(app: &tauri::App) -> tauri::Result<()> {
118126
.build()
119127
.unwrap();
120128
}
129+
"set_title" => {
130+
#[cfg(target_os = "macos")]
131+
tray_handle.set_title("Tauri").unwrap();
132+
}
121133
"icon_1" => {
122134
#[cfg(target_os = "macos")]
123135
tray_handle.set_icon_as_template(true).unwrap();

tooling/cli/schema.json

+7
Original file line numberDiff line numberDiff line change
@@ -2470,6 +2470,13 @@
24702470
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.",
24712471
"default": true,
24722472
"type": "boolean"
2473+
},
2474+
"title": {
2475+
"description": "Title for MacOS tray",
2476+
"type": [
2477+
"string",
2478+
"null"
2479+
]
24732480
}
24742481
},
24752482
"additionalProperties": false

0 commit comments

Comments
 (0)