Skip to content

Commit 2265e09

Browse files
luoffeiamrbashirlucasfernog
authored
feat(windows): implement with_tooltip (#5938)
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com> Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 6d5dc94 commit 2265e09

File tree

9 files changed

+96
-7
lines changed

9 files changed

+96
-7
lines changed

.changes/tray-tooltip-runtime.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": minor
3+
"tauri-runtime-wry": minor
4+
---
5+
6+
Added `TrayHandle::set_tooltip` and `SystemTray::with_tooltip`.

.changes/tray-tooltip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Implement `SystemTray::with_tooltip` and `SystemTrayHandle::set_tooltip` for Windows and macOS.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@ pub enum TrayMessage {
11241124
UpdateIconAsTemplate(bool),
11251125
#[cfg(target_os = "macos")]
11261126
UpdateTitle(String),
1127+
UpdateTooltip(String),
11271128
Create(SystemTray, Sender<Result<()>>),
11281129
Destroy(Sender<Result<()>>),
11291130
}
@@ -2636,6 +2637,11 @@ fn handle_user_message<T: UserEvent>(
26362637
tray.set_title(&title);
26372638
}
26382639
}
2640+
TrayMessage::UpdateTooltip(tooltip) => {
2641+
if let Some(tray) = &mut *tray_context.tray.lock().unwrap() {
2642+
tray.set_tooltip(&tooltip);
2643+
}
2644+
}
26392645
TrayMessage::Create(_tray, _tx) => {
26402646
// already handled
26412647
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ pub fn create_tray<T>(
114114
}
115115
}
116116

117+
if let Some(tooltip) = system_tray.tooltip {
118+
builder = builder.with_tooltip(&tooltip);
119+
}
120+
117121
let tray = builder
118122
.build(event_loop)
119123
.map_err(|e| Error::SystemTray(Box::new(e)))?;
@@ -172,6 +176,16 @@ impl<T: UserEvent> TrayHandle for SystemTrayHandle<T> {
172176
.map_err(|_| Error::FailedToSendMessage)
173177
}
174178

179+
fn set_tooltip(&self, tooltip: &str) -> Result<()> {
180+
self
181+
.proxy
182+
.send_event(Message::Tray(
183+
self.id,
184+
TrayMessage::UpdateTooltip(tooltip.to_owned()),
185+
))
186+
.map_err(|_| Error::FailedToSendMessage)
187+
}
188+
175189
fn destroy(&self) -> Result<()> {
176190
let (tx, rx) = std::sync::mpsc::channel();
177191
send_user_message(

core/tauri-runtime/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct SystemTray {
5454
#[cfg(target_os = "macos")]
5555
pub title: Option<String>,
5656
pub on_event: Option<Box<TrayEventHandler>>,
57+
pub tooltip: Option<String>,
5758
}
5859

5960
#[cfg(all(desktop, feature = "system-tray"))]
@@ -87,6 +88,7 @@ impl Clone for SystemTray {
8788
menu_on_left_click: self.menu_on_left_click,
8889
#[cfg(target_os = "macos")]
8990
title: self.title.clone(),
91+
tooltip: self.tooltip.clone(),
9092
}
9193
}
9294
}
@@ -105,6 +107,7 @@ impl Default for SystemTray {
105107
#[cfg(target_os = "macos")]
106108
title: None,
107109
on_event: None,
110+
tooltip: None,
108111
}
109112
}
110113
}
@@ -157,6 +160,17 @@ impl SystemTray {
157160
self
158161
}
159162

163+
/// Sets the tray icon tooltip.
164+
///
165+
/// ## Platform-specific:
166+
///
167+
/// - **Linux:** Unsupported
168+
#[must_use]
169+
pub fn with_tooltip(mut self, tooltip: &str) -> Self {
170+
self.tooltip = Some(tooltip.to_owned());
171+
self
172+
}
173+
160174
/// Sets the menu to show when the system tray is right clicked.
161175
#[must_use]
162176
pub fn with_menu(mut self, menu: menu::SystemTrayMenu) -> Self {

core/tauri-runtime/src/menu.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ pub trait TrayHandle: fmt::Debug + Clone + Send + Sync {
154154
fn set_icon_as_template(&self, is_template: bool) -> crate::Result<()>;
155155
#[cfg(target_os = "macos")]
156156
fn set_title(&self, title: &str) -> crate::Result<()>;
157+
fn set_tooltip(&self, tooltip: &str) -> crate::Result<()>;
157158
fn destroy(&self) -> crate::Result<()>;
158159
}
159160

core/tauri/src/app/tray.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub struct SystemTray {
6464
icon_as_template_set: bool,
6565
#[cfg(target_os = "macos")]
6666
title: Option<String>,
67+
tooltip: Option<String>,
6768
}
6869

6970
impl fmt::Debug for SystemTray {
@@ -98,6 +99,7 @@ impl Default for SystemTray {
9899
menu_on_left_click_set: false,
99100
#[cfg(target_os = "macos")]
100101
title: None,
102+
tooltip: None,
101103
}
102104
}
103105
}
@@ -257,6 +259,29 @@ impl SystemTray {
257259
self
258260
}
259261

262+
/// Sets the tray icon tooltip.
263+
///
264+
/// ## Platform-specific:
265+
///
266+
/// - **Linux:** Unsupported
267+
///
268+
/// # Examples
269+
///
270+
/// ```
271+
/// use tauri::SystemTray;
272+
///
273+
/// tauri::Builder::default()
274+
/// .setup(|app| {
275+
/// let tray_handle = SystemTray::new().with_tooltip("My App").build(app)?;
276+
/// Ok(())
277+
/// });
278+
/// ```
279+
#[must_use]
280+
pub fn with_tooltip(mut self, tooltip: &str) -> Self {
281+
self.tooltip = Some(tooltip.to_owned());
282+
self
283+
}
284+
260285
/// Sets the event listener for this system tray.
261286
///
262287
/// # Examples
@@ -414,6 +439,10 @@ impl SystemTray {
414439
}
415440
}
416441

442+
if let Some(tooltip) = self.tooltip {
443+
runtime_tray = runtime_tray.with_tooltip(&tooltip);
444+
}
445+
417446
let id = runtime_tray.id;
418447
let tray_handler = match manager.runtime() {
419448
RuntimeOrDispatch::Runtime(r) => r.system_tray(runtime_tray),
@@ -610,6 +639,15 @@ impl<R: Runtime> SystemTrayHandle<R> {
610639
self.inner.set_title(title).map_err(Into::into)
611640
}
612641

642+
/// Set the tooltip for this tray icon.
643+
///
644+
/// ## Platform-specific:
645+
///
646+
/// - **Linux:** Unsupported
647+
pub fn set_tooltip(&self, tooltip: &str) -> crate::Result<()> {
648+
self.inner.set_tooltip(tooltip).map_err(Into::into)
649+
}
650+
613651
/// Destroys this system tray.
614652
pub fn destroy(&self) -> crate::Result<()> {
615653
self.inner.destroy().map_err(Into::into)

core/tauri/src/test/mock_runtime.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ impl TrayHandle for MockTrayHandler {
593593
Ok(())
594594
}
595595

596+
fn set_tooltip(&self, tooltip: &str) -> Result<()> {
597+
Ok(())
598+
}
599+
596600
fn destroy(&self) -> Result<()> {
597601
Ok(())
598602
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ fn create_tray(app: &tauri::App) -> tauri::Result<()> {
9393
SystemTray::new()
9494
.with_id(&tray_id)
9595
.with_menu(tray_menu1.clone())
96+
.with_tooltip("Tauri")
9697
.on_event(move |event| {
9798
let tray_handle = handle.tray_handle_by_id(&tray_id).unwrap();
9899
match event {
@@ -158,13 +159,13 @@ fn create_tray(app: &tauri::App) -> tauri::Result<()> {
158159
}
159160
"switch_menu" => {
160161
let flag = is_menu1.load(Ordering::Relaxed);
161-
tray_handle
162-
.set_menu(if flag {
163-
tray_menu2.clone()
164-
} else {
165-
tray_menu1.clone()
166-
})
167-
.unwrap();
162+
let (menu, tooltip) = if flag {
163+
(tray_menu2.clone(), "Menu 2")
164+
} else {
165+
(tray_menu1.clone(), "Tauri")
166+
};
167+
tray_handle.set_menu(menu).unwrap();
168+
tray_handle.set_tooltip(tooltip).unwrap();
168169
is_menu1.store(!flag, Ordering::Relaxed);
169170
}
170171
"about" => {

0 commit comments

Comments
 (0)