Skip to content

Commit

Permalink
remove extranouse result return from MenuBuilder and `SubmenuBuilde…
Browse files Browse the repository at this point in the history
…r` methods

thanks to the awesome @lucasfernog

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
amrbashir and lucasfernog committed Aug 2, 2023
1 parent 575f5c8 commit c846285
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 238 deletions.
231 changes: 115 additions & 116 deletions core/tauri/src/menu/builders/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use crate::{menu::*, AppHandle, Icon, Manager, Runtime};
use crate::{menu::*, Icon, Manager, Runtime};

/// A builder type for [`Menu`]
///
Expand All @@ -19,7 +19,7 @@ use crate::{menu::*, AppHandle, Icon, Manager, Runtime};
/// # height: 0,
/// # };
/// # let icon2 = icon1.clone();
/// let menu = MenuBuilder::new(&handle)
/// let menu = MenuBuilder::new(&handle, "File")
/// .item(&MenuItem::new(&handle, "MenuItem 1", true, None))?
/// .items(&[
/// &CheckMenuItem::new(&handle, "CheckMenuItem 1", true, true, None),
Expand All @@ -38,261 +38,260 @@ use crate::{menu::*, AppHandle, Icon, Manager, Runtime};
/// Ok(())
/// });
/// ```
pub struct MenuBuilder<R: Runtime> {
menu: Menu<R>,
app_handle: AppHandle<R>,
pub struct MenuBuilder<'m, R: Runtime, M: Manager<R>> {
manager: &'m M,
items: Vec<MenuItemKind<R>>,
}

impl<R: Runtime> MenuBuilder<R> {
impl<'m, R: Runtime, M: Manager<R>> MenuBuilder<'m, R, M> {
/// Create a new menu builder.
pub fn new<M: Manager<R>>(manager: &M) -> Self {
pub fn new<S: AsRef<str>>(manager: &'m M) -> Self {
Self {
menu: Menu::new(manager),
app_handle: manager.app_handle(),
items: Vec::new(),
manager,
}
}

/// Add this item to the menu.
pub fn item(self, item: &dyn IsMenuItem<R>) -> crate::Result<Self> {
self.menu.append(item)?;
Ok(self)
pub fn item(mut self, item: &dyn IsMenuItem<R>) -> Self {
self.items.push(item.kind());
self
}

/// Add these items to the menu.
pub fn items(self, items: &[&dyn IsMenuItem<R>]) -> crate::Result<Self> {
self.menu.append_items(items)?;
Ok(self)
pub fn items(mut self, items: &[&dyn IsMenuItem<R>]) -> Self {
for item in items {
self = self.item(*item);
}
self
}

/// Add a [MenuItem] to the menu.
pub fn text<S: AsRef<str>>(self, text: S) -> crate::Result<Self> {
pub fn text<S: AsRef<str>>(mut self, text: S) -> Self {
self
.items
.push(MenuItem::new(self.manager, text, true, None).kind());
self
.menu
.append(&MenuItem::new(&self.app_handle, text, true, None))?;
Ok(self)
}

/// Add a [CheckMenuItem] to the menu.
pub fn check<S: AsRef<str>>(self, text: S) -> crate::Result<Self> {
self.menu.append(&CheckMenuItem::new(
&self.app_handle,
text,
true,
true,
None,
))?;
Ok(self)
pub fn check<S: AsRef<str>>(mut self, text: S) -> Self {
self
.items
.push(CheckMenuItem::new(self.manager, text, true, true, None).kind());
self
}

/// Add an [IconMenuItem] to the menu.
pub fn icon<S: AsRef<str>>(self, text: S, icon: Icon) -> crate::Result<Self> {
self.menu.append(&IconMenuItem::new(
&self.app_handle,
text,
true,
Some(icon),
None,
))?;
Ok(self)
pub fn icon<S: AsRef<str>>(mut self, text: S, icon: Icon) -> Self {
self
.items
.push(IconMenuItem::new(self.manager, text, true, Some(icon), None).kind());
self
}

/// Add an [IconMenuItem] with a native icon to the menu.
///
/// ## Platform-specific:
///
/// - **Windows / Linux**: Unsupported.
pub fn native_icon<S: AsRef<str>>(self, text: S, icon: NativeIcon) -> crate::Result<Self> {
self.menu.append(&IconMenuItem::with_native_icon(
&self.app_handle,
text,
true,
Some(icon),
None,
))?;
Ok(self)
pub fn native_icon<S: AsRef<str>>(mut self, text: S, icon: NativeIcon) -> Self {
self
.items
.push(IconMenuItem::with_native_icon(self.manager, text, true, Some(icon), None).kind());
self
}

/// Add Separator menu item to the menu.
pub fn separator(self) -> crate::Result<Self> {
pub fn separator(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::separator(self.manager).kind());
self
.menu
.append(&PredefinedMenuItem::separator(&self.app_handle))?;
Ok(self)
}

/// Add Copy menu item to the menu.
pub fn copy(self) -> crate::Result<Self> {
pub fn copy(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::copy(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::copy(&self.app_handle, None))?;
Ok(self)
}

/// Add Cut menu item to the menu.
pub fn cut(self) -> crate::Result<Self> {
pub fn cut(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::cut(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::cut(&self.app_handle, None))?;
Ok(self)
}

/// Add Paste menu item to the menu.
pub fn paste(self) -> crate::Result<Self> {
pub fn paste(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::paste(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::paste(&self.app_handle, None))?;
Ok(self)
}

/// Add SelectAll menu item to the menu.
pub fn select_all(self) -> crate::Result<Self> {
pub fn select_all(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::select_all(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::select_all(&self.app_handle, None))?;
Ok(self)
}

/// Add Undo menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Windows / Linux:** Unsupported.
pub fn undo(self) -> crate::Result<Self> {
pub fn undo(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::undo(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::undo(&self.app_handle, None))?;
Ok(self)
}
/// Add Redo menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Windows / Linux:** Unsupported.
pub fn redo(self) -> crate::Result<Self> {
pub fn redo(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::redo(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::redo(&self.app_handle, None))?;
Ok(self)
}

/// Add Minimize window menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Linux:** Unsupported.
pub fn minimize(self) -> crate::Result<Self> {
pub fn minimize(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::minimize(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::minimize(&self.app_handle, None))?;
Ok(self)
}

/// Add Maximize window menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Linux:** Unsupported.
pub fn maximize(self) -> crate::Result<Self> {
pub fn maximize(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::maximize(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::maximize(&self.app_handle, None))?;
Ok(self)
}

/// Add Fullscreen menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Windows / Linux:** Unsupported.
pub fn fullscreen(self) -> crate::Result<Self> {
pub fn fullscreen(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::fullscreen(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::fullscreen(&self.app_handle, None))?;
Ok(self)
}

/// Add Hide window menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Linux:** Unsupported.
pub fn hide(self) -> crate::Result<Self> {
pub fn hide(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::hide(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::hide(&self.app_handle, None))?;
Ok(self)
}

/// Add Hide other windows menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Linux:** Unsupported.
pub fn hide_others(self) -> crate::Result<Self> {
pub fn hide_others(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::hide_others(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::hide_others(&self.app_handle, None))?;
Ok(self)
}

/// Add Show all app windows menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Windows / Linux:** Unsupported.
pub fn show_all(self) -> crate::Result<Self> {
pub fn show_all(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::show_all(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::show_all(&self.app_handle, None))?;
Ok(self)
}

/// Add Close window menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Linux:** Unsupported.
pub fn close_window(self) -> crate::Result<Self> {
pub fn close_window(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::close_window(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::close_window(&self.app_handle, None))?;
Ok(self)
}

/// Add Quit app menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Linux:** Unsupported.
pub fn quit(self) -> crate::Result<Self> {
pub fn quit(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::quit(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::quit(&self.app_handle, None))?;
Ok(self)
}

/// Add About app menu item to the menu.
pub fn about(self, metadata: Option<AboutMetadata>) -> crate::Result<Self> {
pub fn about(mut self, metadata: Option<AboutMetadata>) -> Self {
self
.items
.push(PredefinedMenuItem::about(self.manager, None, metadata).kind());
self
.menu
.append(&PredefinedMenuItem::about(&self.app_handle, None, metadata))?;
Ok(self)
}

/// Add Services menu item to the menu.
///
/// ## Platform-specific:
///
/// - **Windows / Linux:** Unsupported.
pub fn services(self) -> crate::Result<Self> {
pub fn services(mut self) -> Self {
self
.items
.push(PredefinedMenuItem::services(self.manager, None).kind());
self
.menu
.append(&PredefinedMenuItem::services(&self.app_handle, None))?;
Ok(self)
}

/// Builds this menu
pub fn build(self) -> Menu<R> {
self.menu
pub fn build(self) -> crate::Result<Menu<R>> {
if self.items.is_empty() {
Ok(Menu::new(self.manager))
} else {
let items = self
.items
.iter()
.map(|i| i as &dyn IsMenuItem<R>)
.collect::<Vec<_>>();
Menu::with_items(self.manager, &items)
}
}
}
Loading

0 comments on commit c846285

Please sign in to comment.