muda_win/builders/
icon.rs

1use crate::{
2    accelerator::Accelerator,
3    icon::{Icon, NativeIcon},
4    IconMenuItem, MenuId,
5};
6
7/// A builder type for [`IconMenuItem`]
8#[derive(Clone, Debug, Default)]
9pub struct IconMenuItemBuilder {
10    text: String,
11    enabled: bool,
12    id: Option<MenuId>,
13    accelerator: Option<Accelerator>,
14    icon: Option<Icon>,
15    native_icon: Option<NativeIcon>,
16}
17
18impl IconMenuItemBuilder {
19    pub fn new() -> Self {
20        Default::default()
21    }
22
23    /// Set the id this icon menu item.
24    pub fn id(mut self, id: MenuId) -> Self {
25        self.id.replace(id);
26        self
27    }
28
29    /// Set the text for this icon menu item.
30    ///
31    /// See [`IconMenuItem::set_text`] for more info.
32    pub fn text<S: Into<String>>(mut self, text: S) -> Self {
33        self.text = text.into();
34        self
35    }
36
37    /// Enable or disable this menu item.
38    pub fn enabled(mut self, enabled: bool) -> Self {
39        self.enabled = enabled;
40        self
41    }
42
43    /// Set this icon menu item icon.
44    pub fn icon(mut self, icon: Option<Icon>) -> Self {
45        self.icon = icon;
46        self.native_icon = None;
47        self
48    }
49
50    /// Set this icon menu item native icon.
51    pub fn native_icon(mut self, icon: Option<NativeIcon>) -> Self {
52        self.native_icon = icon;
53        self.icon = None;
54        self
55    }
56
57    /// Set this icon menu item accelerator.
58    pub fn accelerator<A: TryInto<Accelerator>>(
59        mut self,
60        accelerator: Option<A>,
61    ) -> crate::Result<Self>
62    where
63        crate::Error: From<<A as TryInto<Accelerator>>::Error>,
64    {
65        self.accelerator = accelerator.map(|a| a.try_into()).transpose()?;
66        Ok(self)
67    }
68
69    /// Build this icon menu item.
70    pub fn build(self) -> IconMenuItem {
71        if let Some(id) = self.id {
72            if self.icon.is_some() {
73                IconMenuItem::with_id(id, self.text, self.enabled, self.icon, self.accelerator)
74            } else {
75                IconMenuItem::with_id_and_native_icon(
76                    id,
77                    self.text,
78                    self.enabled,
79                    self.native_icon,
80                    self.accelerator,
81                )
82            }
83        } else if self.icon.is_some() {
84            IconMenuItem::new(self.text, self.enabled, self.icon, self.accelerator)
85        } else {
86            IconMenuItem::with_native_icon(
87                self.text,
88                self.enabled,
89                self.native_icon,
90                self.accelerator,
91            )
92        }
93    }
94}