From 5c95152c76391607746f6da942ec57d23c89e89e Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Wed, 16 Aug 2023 17:55:36 +0300 Subject: [PATCH] refactor!: take id for text/check/icon on menu builders (#7621) Co-authored-by: Lucas Nogueira --- .../submenu-and-menu-builders-item-and-id.md | 5 +++ core/tauri/src/menu/builders/menu.rs | 31 +++++++++++-------- core/tauri/src/menu/builders/submenu.rs | 31 +++++++++++-------- 3 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 .changes/submenu-and-menu-builders-item-and-id.md diff --git a/.changes/submenu-and-menu-builders-item-and-id.md b/.changes/submenu-and-menu-builders-item-and-id.md new file mode 100644 index 000000000..37889a40e --- /dev/null +++ b/.changes/submenu-and-menu-builders-item-and-id.md @@ -0,0 +1,5 @@ +--- +'tauri': 'patch:breaking' +--- + +Changed `MenuBuilder\SubmenuBuilder::text`, `MenuBuilder\SubmenuBuilder::check`, `MenuBuilder\SubmenuBuilder::icon` and `MenuBuilder\SubmenuBuilder::native_icon` to take an `id` as the first argument. diff --git a/core/tauri/src/menu/builders/menu.rs b/core/tauri/src/menu/builders/menu.rs index 215596fa8..f8674b86e 100644 --- a/core/tauri/src/menu/builders/menu.rs +++ b/core/tauri/src/menu/builders/menu.rs @@ -29,9 +29,9 @@ use crate::{menu::*, Icon, Manager, Runtime}; /// .copy() /// .paste() /// .separator() -/// .text("MenuItem 2") -/// .check("CheckMenuItem 2") -/// .icon("IconMenuItem 2", app.default_window_icon().cloned().unwrap()) +/// .text("item2", "MenuItem 2") +/// .check("checkitem2", "CheckMenuItem 2") +/// .icon("iconitem2", "IconMenuItem 2", app.default_window_icon().cloned().unwrap()) /// .build()?; /// app.set_menu(menu); /// Ok(()) @@ -83,26 +83,26 @@ impl<'m, R: Runtime, M: Manager> MenuBuilder<'m, R, M> { } /// Add a [MenuItem] to the menu. - pub fn text>(mut self, text: S) -> Self { + pub fn text, S: AsRef>(mut self, id: I, text: S) -> Self { self .items - .push(MenuItem::new(self.manager, text, true, None).kind()); + .push(MenuItem::with_id(self.manager, id, text, true, None).kind()); self } /// Add a [CheckMenuItem] to the menu. - pub fn check>(mut self, text: S) -> Self { + pub fn check, S: AsRef>(mut self, id: I, text: S) -> Self { self .items - .push(CheckMenuItem::new(self.manager, text, true, true, None).kind()); + .push(CheckMenuItem::with_id(self.manager, id, text, true, true, None).kind()); self } /// Add an [IconMenuItem] to the menu. - pub fn icon>(mut self, text: S, icon: Icon) -> Self { + pub fn icon, S: AsRef>(mut self, id: I, text: S, icon: Icon) -> Self { self .items - .push(IconMenuItem::new(self.manager, text, true, Some(icon), None).kind()); + .push(IconMenuItem::with_id(self.manager, id, text, true, Some(icon), None).kind()); self } @@ -111,10 +111,15 @@ impl<'m, R: Runtime, M: Manager> MenuBuilder<'m, R, M> { /// ## Platform-specific: /// /// - **Windows / Linux**: Unsupported. - pub fn native_icon>(mut self, text: S, icon: NativeIcon) -> Self { - self - .items - .push(IconMenuItem::with_native_icon(self.manager, text, true, Some(icon), None).kind()); + pub fn native_icon, S: AsRef>( + mut self, + id: I, + text: S, + icon: NativeIcon, + ) -> Self { + self.items.push( + IconMenuItem::with_id_and_native_icon(self.manager, id, text, true, Some(icon), None).kind(), + ); self } diff --git a/core/tauri/src/menu/builders/submenu.rs b/core/tauri/src/menu/builders/submenu.rs index ee06420de..719ff1fb5 100644 --- a/core/tauri/src/menu/builders/submenu.rs +++ b/core/tauri/src/menu/builders/submenu.rs @@ -31,9 +31,9 @@ use crate::{menu::*, Icon, Manager, Runtime}; /// .copy() /// .paste() /// .separator() -/// .text("MenuItem 2") -/// .check("CheckMenuItem 2") -/// .icon("IconMenuItem 2", app.default_window_icon().cloned().unwrap()) +/// .text("item2", "MenuItem 2") +/// .check("checkitem2", "CheckMenuItem 2") +/// .icon("iconitem2", "IconMenuItem 2", app.default_window_icon().cloned().unwrap()) /// .build()?; /// menu.append(&submenu)?; /// app.set_menu(menu); @@ -104,26 +104,26 @@ impl<'m, R: Runtime, M: Manager> SubmenuBuilder<'m, R, M> { } /// Add a [MenuItem] to the submenu. - pub fn text>(mut self, text: S) -> Self { + pub fn text, S: AsRef>(mut self, id: I, text: S) -> Self { self .items - .push(MenuItem::new(self.manager, text, true, None).kind()); + .push(MenuItem::with_id(self.manager, id, text, true, None).kind()); self } /// Add a [CheckMenuItem] to the submenu. - pub fn check>(mut self, text: S) -> Self { + pub fn check, S: AsRef>(mut self, id: I, text: S) -> Self { self .items - .push(CheckMenuItem::new(self.manager, text, true, true, None).kind()); + .push(CheckMenuItem::with_id(self.manager, id, text, true, true, None).kind()); self } /// Add an [IconMenuItem] to the submenu. - pub fn icon>(mut self, text: S, icon: Icon) -> Self { + pub fn icon, S: AsRef>(mut self, id: I, text: S, icon: Icon) -> Self { self .items - .push(IconMenuItem::new(self.manager, text, true, Some(icon), None).kind()); + .push(IconMenuItem::with_id(self.manager, id, text, true, Some(icon), None).kind()); self } @@ -132,10 +132,15 @@ impl<'m, R: Runtime, M: Manager> SubmenuBuilder<'m, R, M> { /// ## Platform-specific: /// /// - **Windows / Linux**: Unsupported. - pub fn native_icon>(mut self, text: S, icon: NativeIcon) -> Self { - self - .items - .push(IconMenuItem::with_native_icon(self.manager, text, true, Some(icon), None).kind()); + pub fn native_icon, S: AsRef>( + mut self, + id: I, + text: S, + icon: NativeIcon, + ) -> Self { + self.items.push( + IconMenuItem::with_id_and_native_icon(self.manager, id, text, true, Some(icon), None).kind(), + ); self }