Skip to content

Commit

Permalink
feat(api/tray): add TrayIcon.setShowMenuOnLeftClick method (tauri-a…
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Nov 21, 2024
1 parent 7a9b920 commit fc30b20
Show file tree
Hide file tree
Showing 19 changed files with 203 additions and 74 deletions.
6 changes: 6 additions & 0 deletions .changes/set-show-menu-on-left-click-windows-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tauri-apps/api": "minor:feat"
---

Add `TrayIcon.setShowMenuOnLeftClick` method and deprecate `TrayIcon.setMenuOnLeftClick` to match the Rust API.

6 changes: 6 additions & 0 deletions .changes/show-menu-on-left-click-windows-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tauri-apps/api": "minor:feat"
---

Add `TrayIconOptions.showMenuOnLeftClick` field and deprecate `TrayIconOptions.menuOnLeftClick` to match the Rust API.

6 changes: 6 additions & 0 deletions .changes/show-menu-on-left-click-windows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": "minor:feat"
---

Add `TrayIconBuilder::show_menu_on_left_click` method and deprecate `TrayIconBuilder::menu_on_left_click` for consistent naming and clarity.

6 changes: 6 additions & 0 deletions .changes/tray-icon-menu-on-left-click-windows-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tauri-apps/api": "minor:enhance"
---

Add support for `TrayIconOptions.menuOnLeftClick` option and `TrayIcon.setMenuOnLeftClick` on Windows.

6 changes: 3 additions & 3 deletions .changes/tray-icon-menu-on-left-click-windows.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
"tauri": patch:bug
"tauri": "minor:enhance"
---

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

8 changes: 7 additions & 1 deletion crates/tauri-cli/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,13 @@
"type": "boolean"
},
"menuOnLeftClick": {
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.",
"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.",
"default": true,
"deprecated": true,
"type": "boolean"
},
"showMenuOnLeftClick": {
"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.",
"default": true,
"type": "boolean"
},
Expand Down
8 changes: 7 additions & 1 deletion crates/tauri-schema-generator/schemas/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,13 @@
"type": "boolean"
},
"menuOnLeftClick": {
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.",
"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.",
"default": true,
"deprecated": true,
"type": "boolean"
},
"showMenuOnLeftClick": {
"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.",
"default": true,
"type": "boolean"
},
Expand Down
17 changes: 16 additions & 1 deletion crates/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2431,9 +2431,21 @@ pub struct TrayIconConfig {
/// 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.
#[serde(default, alias = "icon-as-template")]
pub icon_as_template: bool,
/// A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.
/// A Boolean value that determines whether the menu should appear when the tray icon receives a left click.
///
/// ## Platform-specific:
///
/// - **Linux**: Unsupported.
#[serde(default = "default_true", alias = "menu-on-left-click")]
#[deprecated(since = "2.2.0", note = "Use `show_menu_on_left_click` instead.")]
pub menu_on_left_click: bool,
/// A Boolean value that determines whether the menu should appear when the tray icon receives a left click.
///
/// ## Platform-specific:
///
/// - **Linux**: Unsupported.
#[serde(default = "default_true", alias = "show-menu-on-left-click")]
pub show_menu_on_left_click: bool,
/// Title for MacOS tray
pub title: Option<String>,
/// Tray icon tooltip on Windows and macOS
Expand Down Expand Up @@ -3351,7 +3363,9 @@ mod build {
fn to_tokens(&self, tokens: &mut TokenStream) {
let id = opt_str_lit(self.id.as_ref());
let icon_as_template = self.icon_as_template;
#[allow(deprecated)]
let menu_on_left_click = self.menu_on_left_click;
let show_menu_on_left_click = self.show_menu_on_left_click;
let icon_path = path_buf_lit(&self.icon_path);
let title = opt_str_lit(self.title.as_ref());
let tooltip = opt_str_lit(self.tooltip.as_ref());
Expand All @@ -3362,6 +3376,7 @@ mod build {
icon_path,
icon_as_template,
menu_on_left_click,
show_menu_on_left_click,
title,
tooltip
);
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,10 +2042,12 @@ tauri::Builder::default()
{
let config = app.config();
if let Some(tray_config) = &config.app.tray_icon {
#[allow(deprecated)]
let mut tray =
TrayIconBuilder::with_id(tray_config.id.clone().unwrap_or_else(|| "main".into()))
.icon_as_template(tray_config.icon_as_template)
.menu_on_left_click(tray_config.menu_on_left_click);
.menu_on_left_click(tray_config.menu_on_left_click)
.show_menu_on_left_click(tray_config.show_menu_on_left_click);
if let Some(icon) = &app.manager.tray.icon {
tray = tray.icon(icon.clone());
}
Expand Down
6 changes: 3 additions & 3 deletions crates/tauri/src/menu/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ impl<R: Runtime> CheckMenuItem<R> {
/// Set this menu item accelerator.
pub fn set_accelerator<S: AsRef<str>>(&self, accelerator: Option<S>) -> crate::Result<()> {
let accel = accelerator.and_then(|s| s.as_ref().parse().ok());
run_item_main_thread!(self, |self_: Self| (*self_.0)
.as_ref()
.set_accelerator(accel))?
run_item_main_thread!(self, |self_: Self| {
(*self_.0).as_ref().set_accelerator(accel)
})?
.map_err(Into::into)
}

Expand Down
12 changes: 6 additions & 6 deletions crates/tauri/src/menu/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ impl<R: Runtime> IconMenuItem<R> {
/// Set this menu item accelerator.
pub fn set_accelerator<S: AsRef<str>>(&self, accelerator: Option<S>) -> crate::Result<()> {
let accel = accelerator.and_then(|s| s.as_ref().parse().ok());
run_item_main_thread!(self, |self_: Self| (*self_.0)
.as_ref()
.set_accelerator(accel))?
run_item_main_thread!(self, |self_: Self| {
(*self_.0).as_ref().set_accelerator(accel)
})?
.map_err(Into::into)
}

Expand All @@ -228,9 +228,9 @@ impl<R: Runtime> IconMenuItem<R> {
/// - **Windows / Linux**: Unsupported.
pub fn set_native_icon(&self, _icon: Option<NativeIcon>) -> crate::Result<()> {
#[cfg(target_os = "macos")]
return run_item_main_thread!(self, |self_: Self| (*self_.0)
.as_ref()
.set_native_icon(_icon.map(Into::into)));
return run_item_main_thread!(self, |self_: Self| {
(*self_.0).as_ref().set_native_icon(_icon.map(Into::into))
});
#[allow(unreachable_code)]
Ok(())
}
Expand Down
42 changes: 23 additions & 19 deletions crates/tauri/src/menu/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ impl<R: Runtime> Menu<R> {
/// [`Submenu`]: super::Submenu
pub fn append(&self, item: &dyn IsMenuItem<R>) -> crate::Result<()> {
let kind = item.kind();
run_item_main_thread!(self, |self_: Self| (*self_.0)
.as_ref()
.append(kind.inner().inner_muda()))?
run_item_main_thread!(self, |self_: Self| {
(*self_.0).as_ref().append(kind.inner().inner_muda())
})?
.map_err(Into::into)
}

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

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

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

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

/// Returns a list of menu items that has been added to this menu.
pub fn items(&self) -> crate::Result<Vec<MenuItemKind<R>>> {
run_item_main_thread!(self, |self_: Self| (*self_.0)
.as_ref()
.items()
.into_iter()
.map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i))
.collect::<Vec<_>>())
run_item_main_thread!(self, |self_: Self| {
(*self_.0)
.as_ref()
.items()
.into_iter()
.map(|i| MenuItemKind::from_muda(self_.0.app_handle.clone(), i))
.collect::<Vec<_>>()
})
}

/// Set this menu as the application menu.
Expand Down
6 changes: 3 additions & 3 deletions crates/tauri/src/menu/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ impl<R: Runtime> MenuItem<R> {
/// Set this menu item accelerator.
pub fn set_accelerator<S: AsRef<str>>(&self, accelerator: Option<S>) -> crate::Result<()> {
let accel = accelerator.and_then(|s| s.as_ref().parse().ok());
run_item_main_thread!(self, |self_: Self| (*self_.0)
.as_ref()
.set_accelerator(accel))?
run_item_main_thread!(self, |self_: Self| {
(*self_.0).as_ref().set_accelerator(accel)
})?
.map_err(Into::into)
}
}
34 changes: 18 additions & 16 deletions crates/tauri/src/menu/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ impl<R: Runtime> Submenu<R> {
/// Add a menu item to the end of this submenu.
pub fn append(&self, item: &dyn IsMenuItem<R>) -> crate::Result<()> {
let kind = item.kind();
run_item_main_thread!(self, |self_: Self| (*self_.0)
.as_ref()
.append(kind.inner().inner_muda()))?
run_item_main_thread!(self, |self_: Self| {
(*self_.0).as_ref().append(kind.inner().inner_muda())
})?
.map_err(Into::into)
}

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

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

/// Retrieves the menu item matching the given identifier.
Expand Down Expand Up @@ -293,9 +295,9 @@ impl<R: Runtime> Submenu<R> {
/// certain other items to the menu.
#[cfg(target_os = "macos")]
pub fn set_as_windows_menu_for_nsapp(&self) -> crate::Result<()> {
run_item_main_thread!(self, |self_: Self| (*self_.0)
.as_ref()
.set_as_windows_menu_for_nsapp())?;
run_item_main_thread!(self, |self_: Self| {
(*self_.0).as_ref().set_as_windows_menu_for_nsapp()
})?;
Ok(())
}

Expand All @@ -307,9 +309,9 @@ impl<R: Runtime> Submenu<R> {
/// which has a title matching the localized word "Help".
#[cfg(target_os = "macos")]
pub fn set_as_help_menu_for_nsapp(&self) -> crate::Result<()> {
run_item_main_thread!(self, |self_: Self| (*self_.0)
.as_ref()
.set_as_help_menu_for_nsapp())?;
run_item_main_thread!(self, |self_: Self| {
(*self_.0).as_ref().set_as_help_menu_for_nsapp()
})?;
Ok(())
}
}
Loading

0 comments on commit fc30b20

Please sign in to comment.