Skip to main content

mitsuami_widgets/
buttons.rs

1//! Push, toggle, menu and split buttons.
2
3use mitsuami_core::services::{Menu, MenuEntries, install_button_menu, install_button_menu_on_open};
4
5use mitsuami_core::{
6    ButtonRole, ButtonStyle, Color, Element, ElementBuilder, EventValue, NodeId, Prop, Tweak, Ui, UiEvent, VectorIcon,
7    View, WidgetKind,
8};
9use mitsuami_reactive::{IntoValue, Signal};
10
11pub struct Button(Element);
12widget!(Button);
13
14impl Button {
15    pub fn new(label: impl IntoValue<String>) -> Button {
16        let mut element = Element::new(WidgetKind::Button);
17        element.prop(label.into_value(), Prop::Label);
18        Button(element)
19    }
20
21    /// What the button does in its window: see [`ButtonRole`].
22    pub fn role(mut self, role: impl IntoValue<ButtonRole>) -> Button {
23        self.0.prop(role.into_value(), Prop::ButtonRole);
24        self
25    }
26
27    /// How the button is drawn: see [`ButtonStyle`].
28    pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> Button {
29        self.0.prop(style.into_value(), Prop::ButtonStyle);
30        self
31    }
32
33    /// An icon before its caption, named in the platform's own set as for
34    /// [`Icon`](crate::Icon); empty: none. Each platform places and sizes it
35    /// its own way.
36    /// The app's own icon before its caption, in place of a named one:
37    /// see [`VectorIcon`]. The platform sizes and colours it as its own.
38    pub fn vector_icon(mut self, icon: impl IntoValue<VectorIcon>) -> Button {
39        self.0.prop(crate::some(icon.into_value()), Prop::VectorIcon);
40        self
41    }
42
43    pub fn icon(mut self, name: impl IntoValue<String>) -> Button {
44        self.0.prop(name.into_value(), Prop::Icon);
45        self
46    }
47
48    /// Shows only its icon. The caption stays its accessible name; a
49    /// tooltip saying it too is up to the app, as platforms leave it.
50    pub fn icon_only(mut self, only: impl IntoValue<bool>) -> Button {
51        self.0.prop(only.into_value(), Prop::IconOnly);
52        self
53    }
54
55    /// Its icon's colour, in place of the one the platform gives button
56    /// icons, as for [`Icon::color`](crate::Icon::color); the caption keeps
57    /// the platform's.
58    pub fn icon_color(mut self, color: impl IntoValue<Color>) -> Button {
59        self.0.prop(color.into_value(), Prop::TextColor);
60        self
61    }
62
63    /// Raw platform settings, past the semantic ones: see [`Tweak`].
64    pub fn native(mut self, tweak: Tweak<Button>) -> Button {
65        tweak.apply(&mut self.0);
66        self
67    }
68
69    pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> Button {
70        self.0.prop(enabled.into_value(), Prop::Enabled);
71        self
72    }
73
74    pub fn on_click(mut self, handler: impl Fn() + 'static) -> Button {
75        self.0.on(move |event| {
76            if *event == UiEvent::Click {
77                handler();
78            }
79        });
80        self
81    }
82}
83
84/// A button that stays pressed until it's clicked again, as the platform
85/// makes one: an `NSButton` that pushes on and off, a `gtk::ToggleButton`,
86/// a `ToggleButton`, a checkable `QQC2.Button`. Its caption, icon and style
87/// are a [`Button`]'s; whether it's pressed is `checked`, as a checkbox's.
88pub struct ToggleButton(Element);
89widget!(ToggleButton);
90
91impl ToggleButton {
92    pub fn new(label: impl IntoValue<String>) -> ToggleButton {
93        let mut element = Element::new(WidgetKind::ToggleButton);
94        element.prop(label.into_value(), Prop::Label);
95        ToggleButton(element)
96    }
97
98    /// How the button is drawn: see [`ButtonStyle`].
99    pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> ToggleButton {
100        self.0.prop(style.into_value(), Prop::ButtonStyle);
101        self
102    }
103
104    /// An icon before its caption, as for [`Button::icon`].
105    /// The app's own icon before its caption, as for
106    /// [`Button::vector_icon`].
107    pub fn vector_icon(mut self, icon: impl IntoValue<VectorIcon>) -> ToggleButton {
108        self.0.prop(crate::some(icon.into_value()), Prop::VectorIcon);
109        self
110    }
111
112    pub fn icon(mut self, name: impl IntoValue<String>) -> ToggleButton {
113        self.0.prop(name.into_value(), Prop::Icon);
114        self
115    }
116
117    /// Shows only its icon. The caption stays its accessible name.
118    pub fn icon_only(mut self, only: impl IntoValue<bool>) -> ToggleButton {
119        self.0.prop(only.into_value(), Prop::IconOnly);
120        self
121    }
122
123    /// Its icon's colour, as for [`Button::icon_color`].
124    pub fn icon_color(mut self, color: impl IntoValue<Color>) -> ToggleButton {
125        self.0.prop(color.into_value(), Prop::TextColor);
126        self
127    }
128
129    /// Raw platform settings, past the semantic ones: see [`Tweak`].
130    pub fn native(mut self, tweak: Tweak<ToggleButton>) -> ToggleButton {
131        tweak.apply(&mut self.0);
132        self
133    }
134}
135
136/// A button that opens a menu of actions, as the platform makes one: a
137/// pull-down `NSPopUpButton`, a `gtk::MenuButton`, a `DropDownButton`, a
138/// `QQC2.Button` that opens a `QQC2.Menu`. Each draws its own arrow. The
139/// menu is built as a context menu is, and an item's `on_select` runs when
140/// it's chosen. It has no click of its own: clicking opens the menu.
141///
142/// ```ignore
143/// MenuButton::new("Add").icon(add_icon).menu((
144///     MenuItem::new("Disc image…").on_select(add_image),
145///     MenuItem::new("Folder…").on_select(add_folder),
146///     MenuSeparator,
147///     MenuItem::new("Guest tools").on_select(add_tools),
148/// ))
149/// ```
150pub struct MenuButton(Element);
151widget!(MenuButton);
152
153impl MenuButton {
154    pub fn new(label: impl IntoValue<String>) -> MenuButton {
155        let mut element = Element::new(WidgetKind::MenuButton);
156        element.prop(label.into_value(), Prop::Label);
157        MenuButton(element)
158    }
159
160    /// Its menu: `MenuItem`s, `MenuSeparator`s and submenus (`Menu`).
161    pub fn menu(mut self, entries: impl MenuEntries + 'static) -> MenuButton {
162        let menu = Menu::new(String::new()).children(entries);
163        self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
164        self
165    }
166
167    /// Its menu, built by `entries`, and built again whenever what it reads
168    /// changes: the folders above this one, say.
169    ///
170    /// ```ignore
171    /// MenuButton::new("Recent").menu_with(move || {
172    ///     recent.get().into_iter().map(|path| MenuItem::new(path.clone()).on_select(move || open(&path))).collect::<Vec<_>>()
173    /// })
174    /// ```
175    pub fn menu_with<E: MenuEntries>(mut self, entries: impl Fn() -> E + 'static) -> MenuButton {
176        let menu = Menu::new(String::new()).children_with(entries);
177        self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
178        self
179    }
180
181    /// Its menu, built by `entries` each time the platform opens it, for
182    /// a menu that depends on what's true at that moment, as
183    /// [`context_menu_on_open`](ElementBuilder::context_menu_on_open)
184    /// builds a context menu. What it reads is read then, not followed.
185    pub fn menu_on_open<E: MenuEntries>(mut self, entries: impl Fn() -> E + 'static) -> MenuButton {
186        let build: std::rc::Rc<dyn Fn() -> Menu> =
187            std::rc::Rc::new(move || Menu::new(String::new()).children(entries()));
188        self.0.after_build(move |ui, id| install_button_menu_on_open(ui, id, build));
189        self
190    }
191
192    /// An icon before its caption, as for [`Button::icon`].
193    /// The app's own icon before its caption, as for
194    /// [`Button::vector_icon`].
195    pub fn vector_icon(mut self, icon: impl IntoValue<VectorIcon>) -> MenuButton {
196        self.0.prop(crate::some(icon.into_value()), Prop::VectorIcon);
197        self
198    }
199
200    pub fn icon(mut self, name: impl IntoValue<String>) -> MenuButton {
201        self.0.prop(name.into_value(), Prop::Icon);
202        self
203    }
204
205    /// Shows only its icon (and the platform's arrow). The caption stays
206    /// its accessible name.
207    pub fn icon_only(mut self, only: impl IntoValue<bool>) -> MenuButton {
208        self.0.prop(only.into_value(), Prop::IconOnly);
209        self
210    }
211
212    /// How the button is drawn: see [`ButtonStyle`].
213    pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> MenuButton {
214        self.0.prop(style.into_value(), Prop::ButtonStyle);
215        self
216    }
217
218    pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> MenuButton {
219        self.0.prop(enabled.into_value(), Prop::Enabled);
220        self
221    }
222
223    /// Raw platform settings, past the semantic ones: see [`Tweak`].
224    pub fn native(mut self, tweak: Tweak<MenuButton>) -> MenuButton {
225        tweak.apply(&mut self.0);
226        self
227    }
228}
229
230/// A button with a menu of other actions beside its own, as the platform
231/// makes one: an `NSComboButton` (split), WinUI's `SplitButton`, an
232/// `adw::SplitButton`; on Kirigami, which has none, a `QQC2.Button` with an
233/// arrow button joined to it. Clicking the button does its action
234/// (`on_click`); its arrow opens the menu, built as a [`MenuButton`]'s.
235///
236/// ```ignore
237/// SplitButton::new("Continue").on_click(proceed).menu((
238///     MenuItem::new("Commit to Edit").on_select(commit),
239/// ))
240/// ```
241pub struct SplitButton(Element);
242widget!(SplitButton);
243
244impl SplitButton {
245    pub fn new(label: impl IntoValue<String>) -> SplitButton {
246        let mut element = Element::new(WidgetKind::SplitButton);
247        element.prop(label.into_value(), Prop::Label);
248        SplitButton(element)
249    }
250
251    /// Its menu: `MenuItem`s, `MenuSeparator`s and submenus (`Menu`).
252    pub fn menu(mut self, entries: impl MenuEntries + 'static) -> SplitButton {
253        let menu = Menu::new(String::new()).children(entries);
254        self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
255        self
256    }
257
258    /// Its menu, built by `entries`, and built again whenever what it reads
259    /// changes, as for [`MenuButton::menu_with`].
260    pub fn menu_with<E: MenuEntries>(mut self, entries: impl Fn() -> E + 'static) -> SplitButton {
261        let menu = Menu::new(String::new()).children_with(entries);
262        self.0.after_build(move |ui, id| install_button_menu(ui, id, menu));
263        self
264    }
265
266    /// An icon before its caption, as for [`Button::icon`].
267    /// The app's own icon before its caption, as for
268    /// [`Button::vector_icon`].
269    pub fn vector_icon(mut self, icon: impl IntoValue<VectorIcon>) -> SplitButton {
270        self.0.prop(crate::some(icon.into_value()), Prop::VectorIcon);
271        self
272    }
273
274    pub fn icon(mut self, name: impl IntoValue<String>) -> SplitButton {
275        self.0.prop(name.into_value(), Prop::Icon);
276        self
277    }
278
279    /// Shows only its icon (and the arrow). The caption stays its
280    /// accessible name.
281    pub fn icon_only(mut self, only: impl IntoValue<bool>) -> SplitButton {
282        self.0.prop(only.into_value(), Prop::IconOnly);
283        self
284    }
285
286    /// How the button is drawn: see [`ButtonStyle`]. AppKit's combo button
287    /// has one look, so it ignores this.
288    pub fn button_style(mut self, style: impl IntoValue<ButtonStyle>) -> SplitButton {
289        self.0.prop(style.into_value(), Prop::ButtonStyle);
290        self
291    }
292
293    pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> SplitButton {
294        self.0.prop(enabled.into_value(), Prop::Enabled);
295        self
296    }
297
298    pub fn on_click(mut self, handler: impl Fn() + 'static) -> SplitButton {
299        self.0.on(move |event| {
300            if *event == UiEvent::Click {
301                handler();
302            }
303        });
304        self
305    }
306
307    /// Raw platform settings, past the semantic ones: see [`Tweak`].
308    pub fn native(mut self, tweak: Tweak<SplitButton>) -> SplitButton {
309        tweak.apply(&mut self.0);
310        self
311    }
312}
313
314// A toggle button's pressed state.
315toggle!(ToggleButton);
316
317text_tag!(Button, Label);
318text_tag!(ToggleButton, Label);
319text_tag!(MenuButton, Label);
320text_tag!(SplitButton, Label);