Skip to content

Commit

Permalink
add menu item builders
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Aug 2, 2023
1 parent a9a6850 commit 0349a38
Show file tree
Hide file tree
Showing 6 changed files with 810 additions and 1 deletion.
66 changes: 66 additions & 0 deletions core/tauri/src/menu/builders/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use crate::{menu::CheckMenuItem, Manager, Runtime};

/// A builder type for [`CheckMenuItem`]
pub struct CheckMenuItemBuilder {
text: String,
enabled: bool,
checked: bool,
accelerator: Option<String>,
}

impl Default for CheckMenuItemBuilder {
fn default() -> Self {
Self::new("")
}
}

impl CheckMenuItemBuilder {
/// Create a new menu item builder.
pub fn new<S: AsRef<str>>(text: S) -> Self {
Self {
text: text.as_ref().to_string(),
enabled: true,
checked: true,
accelerator: None,
}
}

/// Set the text for this menu item.
pub fn text<S: AsRef<str>>(mut self, text: S) -> Self {
self.text = text.as_ref().to_string();
self
}

/// Set the enabled state for this menu item.
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}

/// Set the checked state for this menu item.
pub fn checked(mut self, checked: bool) -> Self {
self.checked = checked;
self
}

/// Set the accelerator for this menu item.
pub fn accelerator<S: AsRef<str>>(mut self, accelerator: S) -> Self {
self.accelerator.replace(accelerator.as_ref().to_string());
self
}

/// Build the menu item
pub fn build<R: Runtime, M: Manager<R>>(self, manager: &M) -> CheckMenuItem<R> {
CheckMenuItem::new(
manager,
self.text,
self.enabled,
self.checked,
self.accelerator,
)
}
}
94 changes: 94 additions & 0 deletions core/tauri/src/menu/builders/icon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use tauri_runtime::menu::icon::NativeIcon;

use crate::{menu::IconMenuItem, Icon, Manager, Runtime};

/// A builder type for [`IconMenuItem`]
pub struct IconMenuItemBuilder {
text: String,
enabled: bool,
icon: Option<Icon>,
native_icon: Option<NativeIcon>,
accelerator: Option<String>,
}

impl Default for IconMenuItemBuilder {
fn default() -> Self {
Self::new("")
}
}

impl IconMenuItemBuilder {
/// Create a new menu item builder.
pub fn new<S: AsRef<str>>(text: S) -> Self {
Self {
text: text.as_ref().to_string(),
enabled: true,
icon: None,
native_icon: None,
accelerator: None,
}
}

/// Set the text for this menu item.
pub fn text<S: AsRef<str>>(mut self, text: S) -> Self {
self.text = text.as_ref().to_string();
self
}

/// Set the enabled state for this menu item.
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}

/// Set the accelerator for this menu item.
pub fn accelerator<S: AsRef<str>>(mut self, accelerator: S) -> Self {
self.accelerator.replace(accelerator.as_ref().to_string());
self
}

/// Set the icon for this menu item.
///
/// **Note:** This method conflicts with [`Self::native_icon`]
/// so calling one of them, will reset the other.
pub fn icon(mut self, icon: Icon) -> Self {
self.icon.replace(icon);
self.native_icon = None;
self
}

/// Set the icon for this menu item.
///
/// **Note:** This method conflicts with [`Self::icon`]
/// so calling one of them, will reset the other.
pub fn native_icon(mut self, icon: NativeIcon) -> Self {
self.native_icon.replace(icon);
self.icon = None;
self
}

/// Build the menu item
pub fn build<R: Runtime, M: Manager<R>>(self, manager: &M) -> IconMenuItem<R> {
if self.icon.is_some() {
IconMenuItem::new(
manager,
self.text,
self.enabled,
self.icon,
self.accelerator,
)
} else {
IconMenuItem::with_native_icon(
manager,
self.text,
self.enabled,
self.native_icon,
self.accelerator,
)
}
}
}
Loading

0 comments on commit 0349a38

Please sign in to comment.