mitsuami_widgets/
buttons.rs1use 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 pub fn role(mut self, role: impl IntoValue<ButtonRole>) -> Button {
22 self.0.prop(role.into_value(), Prop::ButtonRole);
23 self
24 }
25
26 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 pub fn icon(mut self, name: impl IntoValue<String>) -> Button {
36 self.0.prop(name.into_value(), Prop::Icon);
37 self
38 }
39
40 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 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
68pub 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 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 pub fn icon(mut self, name: impl IntoValue<String>) -> ToggleButton {
90 self.0.prop(name.into_value(), Prop::Icon);
91 self
92 }
93
94 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 pub fn native(mut self, tweak: Tweak<ToggleButton>) -> ToggleButton {
102 tweak.apply(&mut self.0);
103 self
104 }
105}
106
107pub 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 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 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 pub fn icon(mut self, name: impl IntoValue<String>) -> MenuButton {
154 self.0.prop(name.into_value(), Prop::Icon);
155 self
156 }
157
158 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 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 pub fn native(mut self, tweak: Tweak<MenuButton>) -> MenuButton {
178 tweak.apply(&mut self.0);
179 self
180 }
181}
182
183toggle!(ToggleButton);
185
186text_tag!(Button, Label);
187text_tag!(ToggleButton, Label);
188text_tag!(MenuButton, Label);