muda/builders/
check.rs

1// Copyright 2022-2022 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use crate::{accelerator::Accelerator, CheckMenuItem, MenuId};
6
7/// A builder type for [`CheckMenuItem`]
8#[derive(Clone, Debug, Default)]
9pub struct CheckMenuItemBuilder {
10    text: String,
11    enabled: bool,
12    checked: bool,
13    accelerator: Option<Accelerator>,
14    id: Option<MenuId>,
15}
16
17impl CheckMenuItemBuilder {
18    pub fn new() -> Self {
19        Default::default()
20    }
21
22    /// Set the id this check menu item.
23    pub fn id(mut self, id: MenuId) -> Self {
24        self.id.replace(id);
25        self
26    }
27
28    /// Set the text for this check menu item.
29    ///
30    /// See [`CheckMenuItem::set_text`] for more info.
31    pub fn text<S: Into<String>>(mut self, text: S) -> Self {
32        self.text = text.into();
33        self
34    }
35
36    /// Enable or disable this menu item.
37    pub fn enabled(mut self, enabled: bool) -> Self {
38        self.enabled = enabled;
39        self
40    }
41
42    /// Check or uncheck this menu item.
43    pub fn checked(mut self, checked: bool) -> Self {
44        self.checked = checked;
45        self
46    }
47
48    /// Set this check menu item accelerator.
49    pub fn accelerator<A: TryInto<Accelerator>>(
50        mut self,
51        accelerator: Option<A>,
52    ) -> crate::Result<Self>
53    where
54        crate::Error: From<<A as TryInto<Accelerator>>::Error>,
55    {
56        self.accelerator = accelerator.map(|a| a.try_into()).transpose()?;
57        Ok(self)
58    }
59
60    /// Build this check menu item.
61    pub fn build(self) -> CheckMenuItem {
62        if let Some(id) = self.id {
63            CheckMenuItem::with_id(id, self.text, self.enabled, self.checked, self.accelerator)
64        } else {
65            CheckMenuItem::new(self.text, self.enabled, self.checked, self.accelerator)
66        }
67    }
68}