Skip to main content

mitsuami_widgets/
buttons.rs

1//! Push, toggle and menu buttons.
2
3use mitsuami_core::services::{Menu, MenuEntries, install_button_menu};
4
5use mitsuami_core::{
6    ButtonRole, ButtonStyle, Element, ElementBuilder, EventValue, NodeId, Prop, Tweak, Ui, UiEvent, View, WidgetKind,
7};
8use mitsuami_reactive::{IntoValue, Signal};
9
10pub struct Button(Element);
11widget!(Button);
12
13impl Button {
14    pub fn new(label: impl IntoValue<String>) -> Button {
15        let mut element = Element::new(WidgetKind::Button);
16        element.prop(label.into_value(), Prop::Label);
17        Button(element)
18    }
19
20    /// What the button does in its window: see [`ButtonRole`].
21    pub fn role(mut self, role: impl IntoValue<ButtonRole>) -> Button {
22        self.0.prop(role.into_value(), Prop::ButtonRole);
23        self
24    }
25
26    /// How the button is drawn: see [`ButtonStyle`].
27    pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> Button {
28        self.0.prop(style.into_value(), Prop::ButtonStyle);
29        self
30    }
31
32    /// An icon before its caption, named in the platform's own set as for
33    /// [`Icon`](crate::Icon); empty: none. Each platform places and sizes it
34    /// its own way.
35    pub fn icon(mut self, name: impl IntoValue<String>) -> Button {
36        self.0.prop(name.into_value(), Prop::Icon);
37        self
38    }
39
40    /// Shows only its icon. The caption stays its accessible name; a
41    /// tooltip saying it too is up to the app, as platforms leave it.
42    pub fn icon_only(mut self, only: impl IntoValue<bool>) -> Button {
43        self.0.prop(only.into_value(), Prop::IconOnly);
44        self
45    }
46
47    /// Raw platform settings, past the semantic ones: see [`Tweak`].
48    pub fn native(mut self, tweak: Tweak<Button>) -> Button {
49        tweak.apply(&mut self.0);
50        self
51    }
52
53    pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> Button {
54        self.0.prop(enabled.into_value(), Prop::Enabled);
55        self
56    }
57
58    pub fn on_click(mut self, handler: impl Fn() + 'static) -> Button {
59        self.0.on(move |event| {
60            if *event == UiEvent::Click {
61                handler();
62            }
63        });
64        self
65    }
66}
67
68/// A button that stays pressed until it's clicked again, as the platform
69/// makes one: an `NSButton` that pushes on and off, a `gtk::ToggleButton`,
70/// a `ToggleButton`, a checkable `QQC2.Button`. Its caption, icon and style
71/// are a [`Button`]'s; whether it's pressed is `checked`, as a checkbox's.
72pub struct ToggleButton(Element);
73widget!(ToggleButton);
74
75impl ToggleButton {
76    pub fn new(label: impl IntoValue<String>) -> ToggleButton {
77        let mut element = Element::new(WidgetKind::ToggleButton);
78        element.prop(label.into_value(), Prop::Label);
79        ToggleButton(element)
80    }
81
82    /// How the button is drawn: see [`ButtonStyle`].
83    pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> ToggleButton {
84        self.0.prop(style.into_value(), Prop::ButtonStyle);
85        self
86    }
87
88    /// An icon before its caption, as for [`Button::icon`].
89    pub fn icon(mut self, name: impl IntoValue<String>) -> ToggleButton {
90        self.0.prop(name.into_value(), Prop::Icon);
91        self
92    }
93
94    /// Shows only its icon. The caption stays its accessible name.
95    pub fn icon_only(mut self, only: impl IntoValue<bool>) -> ToggleButton {
96        self.0.prop(only.into_value(), Prop::IconOnly);
97        self
98    }
99
100    /// Raw platform settings, past the semantic ones: see [`Tweak`].
101    pub fn native(mut self, tweak: Tweak<ToggleButton>) -> ToggleButton {
102        tweak.apply(&mut self.0);
103        self
104    }
105}
106
107/// A button that opens a menu of actions, as the platform makes one: a
108/// pull-down `NSPopUpButton`, a `gtk::MenuButton`, a `DropDownButton`, a
109/// `QQC2.Button` that opens a `QQC2.Menu`. Each draws its own arrow. The
110/// menu is built as a context menu is, and an item's `on_select` runs when
111/// it's chosen. It has no click of its own: clicking opens the menu.
112///
113/// ```ignore
114/// MenuButton::new("Add").icon(add_icon).menu((
115///     MenuItem::new("Disc image…").on_select(add_image),
116///     MenuItem::new("Folder…").on_select(add_folder),
117///     MenuSeparator,
118///     MenuItem::new("Guest tools").on_select(add_tools),
119/// ))
120/// ```
121pub struct MenuButton(Element);
122widget!(MenuButton);
123
124impl MenuButton {
125    pub fn new(label: impl IntoValue<String>) -> MenuButton {
126        let mut element = Element::new(WidgetKind::MenuButton);
127        element.prop(label.into_value(), Prop::Label);
128        MenuButton(element)
129    }
130
131    /// Its menu: `MenuItem`s, `MenuSeparator`s and submenus (`Menu`).
132    pub fn menu(mut self, entries: impl MenuEntries + 'static) -> MenuButton {
133        let menu = Menu::new(String::new()).children(entries);
134        self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
135        self
136    }
137
138    /// Its menu, built by `entries`, and built again whenever what it reads
139    /// changes: the folders above this one, say.
140    ///
141    /// ```ignore
142    /// MenuButton::new("Recent").menu_with(move || {
143    ///     recent.get().into_iter().map(|path| MenuItem::new(path.clone()).on_select(move || open(&path))).collect::<Vec<_>>()
144    /// })
145    /// ```
146    pub fn menu_with<E: MenuEntries>(mut self, entries: impl Fn() -> E + 'static) -> MenuButton {
147        let menu = Menu::new(String::new()).children_with(entries);
148        self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
149        self
150    }
151
152    /// An icon before its caption, as for [`Button::icon`].
153    pub fn icon(mut self, name: impl IntoValue<String>) -> MenuButton {
154        self.0.prop(name.into_value(), Prop::Icon);
155        self
156    }
157
158    /// Shows only its icon (and the platform's arrow). The caption stays
159    /// its accessible name.
160    pub fn icon_only(mut self, only: impl IntoValue<bool>) -> MenuButton {
161        self.0.prop(only.into_value(), Prop::IconOnly);
162        self
163    }
164
165    /// How the button is drawn: see [`ButtonStyle`].
166    pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> MenuButton {
167        self.0.prop(style.into_value(), Prop::ButtonStyle);
168        self
169    }
170
171    pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> MenuButton {
172        self.0.prop(enabled.into_value(), Prop::Enabled);
173        self
174    }
175
176    /// Raw platform settings, past the semantic ones: see [`Tweak`].
177    pub fn native(mut self, tweak: Tweak<MenuButton>) -> MenuButton {
178        tweak.apply(&mut self.0);
179        self
180    }
181}
182
183// A toggle button's pressed state.
184toggle!(ToggleButton);
185
186text_tag!(Button, Label);
187text_tag!(ToggleButton, Label);
188text_tag!(MenuButton, Label);