Skip to content

Commit fc30b20

Browse files
authored
feat(api/tray): add TrayIcon.setShowMenuOnLeftClick method (#11726)
1 parent 7a9b920 commit fc30b20

19 files changed

+203
-74
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": "minor:feat"
3+
---
4+
5+
Add `TrayIcon.setShowMenuOnLeftClick` method and deprecate `TrayIcon.setMenuOnLeftClick` to match the Rust API.
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": "minor:feat"
3+
---
4+
5+
Add `TrayIconOptions.showMenuOnLeftClick` field and deprecate `TrayIconOptions.menuOnLeftClick` to match the Rust API.
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": "minor:feat"
3+
---
4+
5+
Add `TrayIconBuilder::show_menu_on_left_click` method and deprecate `TrayIconBuilder::menu_on_left_click` for consistent naming and clarity.
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": "minor:enhance"
3+
---
4+
5+
Add support for `TrayIconOptions.menuOnLeftClick` option and `TrayIcon.setMenuOnLeftClick` on Windows.
6+
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
"tauri": patch:bug
2+
"tauri": "minor:enhance"
33
---
44

5-
Support for `set_show_menu_on_left_click` was implemented on Windows too. So it
6-
should not be macOS anymore. [tauri-apps/tray-icon#199](https://github.com/tauri-apps/tray-icon/pull/199)
5+
Add support for `TrayIconBuilder::menu_on_left_click` and `TrayIcon::set_show_menu_on_left_click` on Windows.
6+

crates/tauri-cli/config.schema.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,13 @@
16241624
"type": "boolean"
16251625
},
16261626
"menuOnLeftClick": {
1627-
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.",
1627+
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click.\n\n ## Platform-specific:\n\n - **Linux**: Unsupported.",
1628+
"default": true,
1629+
"deprecated": true,
1630+
"type": "boolean"
1631+
},
1632+
"showMenuOnLeftClick": {
1633+
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click.\n\n ## Platform-specific:\n\n - **Linux**: Unsupported.",
16281634
"default": true,
16291635
"type": "boolean"
16301636
},

crates/tauri-schema-generator/schemas/config.schema.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,13 @@
16241624
"type": "boolean"
16251625
},
16261626
"menuOnLeftClick": {
1627-
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.",
1627+
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click.\n\n ## Platform-specific:\n\n - **Linux**: Unsupported.",
1628+
"default": true,
1629+
"deprecated": true,
1630+
"type": "boolean"
1631+
},
1632+
"showMenuOnLeftClick": {
1633+
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click.\n\n ## Platform-specific:\n\n - **Linux**: Unsupported.",
16281634
"default": true,
16291635
"type": "boolean"
16301636
},

crates/tauri-utils/src/config.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2431,9 +2431,21 @@ pub struct TrayIconConfig {
24312431
/// A Boolean value that determines whether the image represents a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc) image on macOS.
24322432
#[serde(default, alias = "icon-as-template")]
24332433
pub icon_as_template: bool,
2434-
/// A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.
2434+
/// A Boolean value that determines whether the menu should appear when the tray icon receives a left click.
2435+
///
2436+
/// ## Platform-specific:
2437+
///
2438+
/// - **Linux**: Unsupported.
24352439
#[serde(default = "default_true", alias = "menu-on-left-click")]
2440+
#[deprecated(since = "2.2.0", note = "Use `show_menu_on_left_click` instead.")]
24362441
pub menu_on_left_click: bool,
2442+
/// A Boolean value that determines whether the menu should appear when the tray icon receives a left click.
2443+
///
2444+
/// ## Platform-specific:
2445+
///
2446+
/// - **Linux**: Unsupported.
2447+
#[serde(default = "default_true", alias = "show-menu-on-left-click")]
2448+
pub show_menu_on_left_click: bool,
24372449
/// Title for MacOS tray
24382450
pub title: Option<String>,
24392451
/// Tray icon tooltip on Windows and macOS
@@ -3351,7 +3363,9 @@ mod build {
33513363
fn to_tokens(&self, tokens: &mut TokenStream) {
33523364
let id = opt_str_lit(self.id.as_ref());
33533365
let icon_as_template = self.icon_as_template;
3366+
#[allow(deprecated)]
33543367
let menu_on_left_click = self.menu_on_left_click;
3368+
let show_menu_on_left_click = self.show_menu_on_left_click;
33553369
let icon_path = path_buf_lit(&self.icon_path);
33563370
let title = opt_str_lit(self.title.as_ref());
33573371
let tooltip = opt_str_lit(self.tooltip.as_ref());
@@ -3362,6 +3376,7 @@ mod build {
33623376
icon_path,
33633377
icon_as_template,
33643378
menu_on_left_click,
3379+
show_menu_on_left_click,
33653380
title,
33663381
tooltip
33673382
);

crates/tauri/scripts/bundle.global.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri/src/app.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2042,10 +2042,12 @@ tauri::Builder::default()
20422042
{
20432043
let config = app.config();
20442044
if let Some(tray_config) = &config.app.tray_icon {
2045+
#[allow(deprecated)]
20452046
let mut tray =
20462047
TrayIconBuilder::with_id(tray_config.id.clone().unwrap_or_else(|| "main".into()))
20472048
.icon_as_template(tray_config.icon_as_template)
2048-
.menu_on_left_click(tray_config.menu_on_left_click);
2049+
.menu_on_left_click(tray_config.menu_on_left_click)
2050+
.show_menu_on_left_click(tray_config.show_menu_on_left_click);
20492051
if let Some(icon) = &app.manager.tray.icon {
20502052
tray = tray.icon(icon.clone());
20512053
}

crates/tauri/src/menu/check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ impl<R: Runtime> CheckMenuItem<R> {
119119
/// Set this menu item accelerator.
120120
pub fn set_accelerator<S: AsRef<str>>(&self, accelerator: Option<S>) -> crate::Result<()> {
121121
let accel = accelerator.and_then(|s| s.as_ref().parse().ok());
122-
run_item_main_thread!(self, |self_: Self| (*self_.0)
123-
.as_ref()
124-
.set_accelerator(accel))?
122+
run_item_main_thread!(self, |self_: Self| {
123+
(*self_.0).as_ref().set_accelerator(accel)
124+
})?
125125
.map_err(Into::into)
126126
}
127127

crates/tauri/src/menu/icon.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ impl<R: Runtime> IconMenuItem<R> {
206206
/// Set this menu item accelerator.
207207
pub fn set_accelerator<S: AsRef<str>>(&self, accelerator: Option<S>) -> crate::Result<()> {
208208
let accel = accelerator.and_then(|s| s.as_ref().parse().ok());
209-
run_item_main_thread!(self, |self_: Self| (*self_.0)
210-
.as_ref()
211-
.set_accelerator(accel))?
209+
run_item_main_thread!(self, |self_: Self| {
210+
(*self_.0).as_ref().set_accelerator(accel)
211+
})?
212212
.map_err(Into::into)
213213
}
214214

@@ -228,9 +228,9 @@ impl<R: Runtime> IconMenuItem<R> {
228228
/// - **Windows / Linux**: Unsupported.
229229
pub fn set_native_icon(&self, _icon: Option<NativeIcon>) -> crate::Result<()> {
230230
#[cfg(target_os = "macos")]
231-
return run_item_main_thread!(self, |self_: Self| (*self_.0)
232-
.as_ref()
233-
.set_native_icon(_icon.map(Into::into)));
231+
return run_item_main_thread!(self, |self_: Self| {
232+
(*self_.0).as_ref().set_native_icon(_icon.map(Into::into))
233+
});
234234
#[allow(unreachable_code)]
235235
Ok(())
236236
}

crates/tauri/src/menu/menu.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ impl<R: Runtime> Menu<R> {
271271
/// [`Submenu`]: super::Submenu
272272
pub fn append(&self, item: &dyn IsMenuItem<R>) -> crate::Result<()> {
273273
let kind = item.kind();
274-
run_item_main_thread!(self, |self_: Self| (*self_.0)
275-
.as_ref()
276-
.append(kind.inner().inner_muda()))?
274+
run_item_main_thread!(self, |self_: Self| {
275+
(*self_.0).as_ref().append(kind.inner().inner_muda())
276+
})?
277277
.map_err(Into::into)
278278
}
279279

@@ -301,9 +301,9 @@ impl<R: Runtime> Menu<R> {
301301
/// [`Submenu`]: super::Submenu
302302
pub fn prepend(&self, item: &dyn IsMenuItem<R>) -> crate::Result<()> {
303303
let kind = item.kind();
304-
run_item_main_thread!(self, |self_: Self| (*self_.0)
305-
.as_ref()
306-
.prepend(kind.inner().inner_muda()))?
304+
run_item_main_thread!(self, |self_: Self| {
305+
(*self_.0).as_ref().prepend(kind.inner().inner_muda())
306+
})?
307307
.map_err(Into::into)
308308
}
309309

@@ -351,18 +351,20 @@ impl<R: Runtime> Menu<R> {
351351
/// Remove a menu item from this menu.
352352
pub fn remove(&self, item: &dyn IsMenuItem<R>) -> crate::Result<()> {
353353
let kind = item.kind();
354-
run_item_main_thread!(self, |self_: Self| (*self_.0)
355-
.as_ref()
356-
.remove(kind.inner().inner_muda()))?
354+
run_item_main_thread!(self, |self_: Self| {
355+
(*self_.0).as_ref().remove(kind.inner().inner_muda())
356+
})?
357357
.map_err(Into::into)
358358
}
359359

360360
/// Remove the menu item at the specified position from this menu and returns it.
361361
pub fn remove_at(&self, position: usize) -> crate::Result<Option<MenuItemKind<R>>> {
362-
run_item_main_thread!(self, |self_: Self| (*self_.0)
363-
.as_ref()
364-
.remove_at(position)
365-
.map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i)))
362+
run_item_main_thread!(self, |self_: Self| {
363+
(*self_.0)
364+
.as_ref()
365+
.remove_at(position)
366+
.map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i))
367+
})
366368
}
367369

368370
/// Retrieves the menu item matching the given identifier.
@@ -380,12 +382,14 @@ impl<R: Runtime> Menu<R> {
380382

381383
/// Returns a list of menu items that has been added to this menu.
382384
pub fn items(&self) -> crate::Result<Vec<MenuItemKind<R>>> {
383-
run_item_main_thread!(self, |self_: Self| (*self_.0)
384-
.as_ref()
385-
.items()
386-
.into_iter()
387-
.map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i))
388-
.collect::<Vec<_>>())
385+
run_item_main_thread!(self, |self_: Self| {
386+
(*self_.0)
387+
.as_ref()
388+
.items()
389+
.into_iter()
390+
.map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i))
391+
.collect::<Vec<_>>()
392+
})
389393
}
390394

391395
/// Set this menu as the application menu.

crates/tauri/src/menu/normal.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ impl<R: Runtime> MenuItem<R> {
117117
/// Set this menu item accelerator.
118118
pub fn set_accelerator<S: AsRef<str>>(&self, accelerator: Option<S>) -> crate::Result<()> {
119119
let accel = accelerator.and_then(|s| s.as_ref().parse().ok());
120-
run_item_main_thread!(self, |self_: Self| (*self_.0)
121-
.as_ref()
122-
.set_accelerator(accel))?
120+
run_item_main_thread!(self, |self_: Self| {
121+
(*self_.0).as_ref().set_accelerator(accel)
122+
})?
123123
.map_err(Into::into)
124124
}
125125
}

crates/tauri/src/menu/submenu.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ impl<R: Runtime> Submenu<R> {
173173
/// Add a menu item to the end of this submenu.
174174
pub fn append(&self, item: &dyn IsMenuItem<R>) -> crate::Result<()> {
175175
let kind = item.kind();
176-
run_item_main_thread!(self, |self_: Self| (*self_.0)
177-
.as_ref()
178-
.append(kind.inner().inner_muda()))?
176+
run_item_main_thread!(self, |self_: Self| {
177+
(*self_.0).as_ref().append(kind.inner().inner_muda())
178+
})?
179179
.map_err(Into::into)
180180
}
181181

@@ -225,18 +225,20 @@ impl<R: Runtime> Submenu<R> {
225225
/// Remove a menu item from this submenu.
226226
pub fn remove(&self, item: &dyn IsMenuItem<R>) -> crate::Result<()> {
227227
let kind = item.kind();
228-
run_item_main_thread!(self, |self_: Self| (*self_.0)
229-
.as_ref()
230-
.remove(kind.inner().inner_muda()))?
228+
run_item_main_thread!(self, |self_: Self| {
229+
(*self_.0).as_ref().remove(kind.inner().inner_muda())
230+
})?
231231
.map_err(Into::into)
232232
}
233233

234234
/// Remove the menu item at the specified position from this submenu and returns it.
235235
pub fn remove_at(&self, position: usize) -> crate::Result<Option<MenuItemKind<R>>> {
236-
run_item_main_thread!(self, |self_: Self| (*self_.0)
237-
.as_ref()
238-
.remove_at(position)
239-
.map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i)))
236+
run_item_main_thread!(self, |self_: Self| {
237+
(*self_.0)
238+
.as_ref()
239+
.remove_at(position)
240+
.map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i))
241+
})
240242
}
241243

242244
/// Retrieves the menu item matching the given identifier.
@@ -293,9 +295,9 @@ impl<R: Runtime> Submenu<R> {
293295
/// certain other items to the menu.
294296
#[cfg(target_os = "macos")]
295297
pub fn set_as_windows_menu_for_nsapp(&self) -> crate::Result<()> {
296-
run_item_main_thread!(self, |self_: Self| (*self_.0)
297-
.as_ref()
298-
.set_as_windows_menu_for_nsapp())?;
298+
run_item_main_thread!(self, |self_: Self| {
299+
(*self_.0).as_ref().set_as_windows_menu_for_nsapp()
300+
})?;
299301
Ok(())
300302
}
301303

@@ -307,9 +309,9 @@ impl<R: Runtime> Submenu<R> {
307309
/// which has a title matching the localized word "Help".
308310
#[cfg(target_os = "macos")]
309311
pub fn set_as_help_menu_for_nsapp(&self) -> crate::Result<()> {
310-
run_item_main_thread!(self, |self_: Self| (*self_.0)
311-
.as_ref()
312-
.set_as_help_menu_for_nsapp())?;
312+
run_item_main_thread!(self, |self_: Self| {
313+
(*self_.0).as_ref().set_as_help_menu_for_nsapp()
314+
})?;
313315
Ok(())
314316
}
315317
}

0 commit comments

Comments
 (0)