muda_win/builders/
check.rs

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