1use 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 pub fn role(mut self, role: impl IntoValue<ButtonRole>) -> Button {
23 self.0.prop(role.into_value(), Prop::ButtonRole);
24 self
25 }
26
27 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 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 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 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 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
84pub 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 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 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 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 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 pub fn native(mut self, tweak: Tweak<ToggleButton>) -> ToggleButton {
131 tweak.apply(&mut self.0);
132 self
133 }
134}
135
136pub 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 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 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 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 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 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 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 pub fn native(mut self, tweak: Tweak<MenuButton>) -> MenuButton {
225 tweak.apply(&mut self.0);
226 self
227 }
228}
229
230pub 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 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 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 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 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 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 pub fn native(mut self, tweak: Tweak<SplitButton>) -> SplitButton {
309 tweak.apply(&mut self.0);
310 self
311 }
312}
313
314toggle!(ToggleButton);
316
317text_tag!(Button, Label);
318text_tag!(ToggleButton, Label);
319text_tag!(MenuButton, Label);
320text_tag!(SplitButton, Label);