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 color_well;
78mod containers;
79mod document_tabs;
80mod media;
81mod popover;
82mod progress;
83mod ranges;
84mod scroll;
85mod separator;
86mod sidebar;
87mod tabs;
88mod text;
89mod text_inputs;
90mod toggles;
91mod toolbar;
92mod window;
93
94pub use buttons::*;
95pub use choices::*;
96pub use color_well::*;
97pub use containers::*;
98pub use document_tabs::*;
99pub use media::*;
100pub use popover::*;
101pub use progress::*;
102pub use ranges::*;
103pub use scroll::*;
104pub use separator::*;
105pub use sidebar::*;
106pub use tabs::*;
107pub use text::*;
108pub use text_inputs::*;
109pub use toggles::*;
110pub use toolbar::*;
111pub use window::*;
112
113/// A value as an optional one, for props that take `None` for unset.
114pub(crate) fn some<T: Clone + 'static>(value: mitsuami_reactive::Value<T>) -> mitsuami_reactive::Value<Option<T>> {
115    match value {
116        mitsuami_reactive::Value::Static(v) => mitsuami_reactive::Value::Static(Some(v)),
117        mitsuami_reactive::Value::Dynamic(f) => mitsuami_reactive::Value::Dynamic(std::rc::Rc::new(move || Some(f()))),
118    }
119}