Skip to main content

mitsuami_widgets/
lib.rs

1//! Built-in widgets. Platform-free: each is an
2//! [`Element`](mitsuami_core::Element) with typed props, events and
3//! accessibility defaults. Backends decide how they look.
4
5macro_rules! widget {
6    ($t:ty) => {
7        impl ElementBuilder for $t {
8            fn element(&mut self) -> &mut Element {
9                &mut self.0
10            }
11        }
12
13        impl View for $t {
14            fn build(self, ui: &Ui) -> NodeId {
15                self.0.build(ui)
16            }
17        }
18    };
19}
20
21macro_rules! toggle {
22    ($t:ident) => {
23        impl $t {
24            pub fn checked(mut self, checked: impl IntoValue<bool>) -> $t {
25                self.0.prop(checked.into_value(), Prop::Checked);
26                self
27            }
28
29            /// Two-way binding, Vue's `v-model`.
30            pub fn bind(self, signal: Signal<bool>) -> $t {
31                self.checked(signal).on_change(move |checked| signal.set(checked))
32            }
33
34            pub fn enabled(mut self, enabled: impl IntoValue<bool>) -> $t {
35                self.0.prop(enabled.into_value(), Prop::Enabled);
36                self
37            }
38
39            pub fn on_change(mut self, handler: impl Fn(bool) + 'static) -> $t {
40                self.0.on(move |event| {
41                    if let UiEvent::Changed(EventValue::Bool(checked)) = event {
42                        handler(*checked);
43                    }
44                });
45                self
46            }
47        }
48    };
49}
50
51// ----------------------------------------------------------- view! tags
52//
53// `view!` builds `<Tag …>children</Tag>` as
54// `Tag::__tag().….__children(move || children)`: containers take their
55// children, text and buttons their text.
56
57/// Widgets whose one child is their text: `<Text>"Hello"</Text>`.
58macro_rules! text_tag {
59    ($t:ident, $prop:ident) => {
60        impl $t {
61            #[doc(hidden)]
62            pub fn __tag() -> $t {
63                $t(Element::new(WidgetKind::$t))
64            }
65
66            #[doc(hidden)]
67            pub fn __children<S: IntoValue<String>>(mut self, text: impl FnOnce() -> S) -> $t {
68                self.0.prop(text().into_value(), Prop::$prop);
69                self
70            }
71        }
72    };
73}
74
75mod buttons;
76mod choices;
77mod containers;
78mod document_tabs;
79mod media;
80mod progress;
81mod ranges;
82mod scroll;
83mod separator;
84mod sidebar;
85mod tabs;
86mod text;
87mod text_inputs;
88mod toggles;
89mod toolbar;
90mod window;
91
92pub use buttons::*;
93pub use choices::*;
94pub use containers::*;
95pub use document_tabs::*;
96pub use media::*;
97pub use progress::*;
98pub use ranges::*;
99pub use scroll::*;
100pub use separator::*;
101pub use sidebar::*;
102pub use tabs::*;
103pub use text::*;
104pub use text_inputs::*;
105pub use toggles::*;
106pub use toolbar::*;
107pub use window::*;