1use std::fmt;
4
5use crate::any_value::Opaque;
6use crate::custom::CustomProps;
7use crate::draw::DisplayList;
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct NodeId(pub(crate) u32);
12
13impl NodeId {
14 pub fn raw(self) -> u32 {
15 self.0
16 }
17
18 pub fn from_raw(raw: u32) -> NodeId {
21 NodeId(raw)
22 }
23}
24
25impl fmt::Display for NodeId {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 write!(f, "#{}", self.0)
28 }
29}
30
31#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
32pub enum WidgetKind {
33 Window,
34 Container,
36 Fragment,
39 Text,
40 Button,
41 TextInput,
42 Checkbox,
43 Switch,
44 ScrollView,
47 Custom(&'static str),
50 Native,
53}
54
55impl WidgetKind {
56 pub fn is_native(self) -> bool {
57 self != WidgetKind::Fragment
58 }
59
60 pub fn is_container(self) -> bool {
62 matches!(self, WidgetKind::Window | WidgetKind::Container | WidgetKind::ScrollView)
63 }
64
65 pub fn name(self) -> &'static str {
66 match self {
67 WidgetKind::Window => "Window",
68 WidgetKind::Container => "Container",
69 WidgetKind::ScrollView => "ScrollView",
70 WidgetKind::Fragment => "Fragment",
71 WidgetKind::Text => "Text",
72 WidgetKind::Button => "Button",
73 WidgetKind::TextInput => "TextInput",
74 WidgetKind::Checkbox => "Checkbox",
75 WidgetKind::Switch => "Switch",
76 WidgetKind::Custom(name) => name,
77 WidgetKind::Native => "Native",
78 }
79 }
80}
81
82#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
84pub enum TextStyle {
85 LargeTitle,
86 Title,
87 Headline,
88 #[default]
89 Body,
90 Callout,
91 Caption,
92 Monospace,
93}
94
95#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
97pub enum ButtonVariant {
98 #[default]
99 Default,
100 Primary,
101 Destructive,
102 Plain,
103}
104
105#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
107pub enum ScrollAxes {
108 #[default]
109 Vertical,
110 Horizontal,
111 Both,
112}
113
114impl ScrollAxes {
115 pub fn horizontal(self) -> bool {
116 matches!(self, ScrollAxes::Horizontal | ScrollAxes::Both)
117 }
118
119 pub fn vertical(self) -> bool {
120 matches!(self, ScrollAxes::Vertical | ScrollAxes::Both)
121 }
122}
123
124#[derive(Clone, Debug, PartialEq)]
126pub enum Prop {
127 Title(String),
129 Text(String),
131 Label(String),
133 Value(String),
135 Placeholder(String),
136 Checked(bool),
137 Enabled(bool),
138 TextStyle(TextStyle),
139 Variant(ButtonVariant),
140 ScrollAxes(ScrollAxes),
142 Custom(CustomProps),
144 Drawing(DisplayList),
147 Native(Opaque),
150}
151
152impl Prop {
153 pub fn key(&self) -> std::mem::Discriminant<Prop> {
155 std::mem::discriminant(self)
156 }
157
158 pub fn affects_measure(&self) -> bool {
160 matches!(
161 self,
162 Prop::Text(_)
163 | Prop::Label(_)
164 | Prop::Placeholder(_)
165 | Prop::TextStyle(_)
166 | Prop::Variant(_)
167 | Prop::Custom(_)
168 | Prop::Native(_)
169 )
170 }
171}
172
173#[macro_export]
175macro_rules! find_prop {
176 ($props:expr, $variant:ident) => {
177 $props.iter().find_map(|p| match p {
178 $crate::Prop::$variant(v) => Some(v.clone()),
179 _ => None,
180 })
181 };
182}
183
184macro_rules! static_value {
185 ($($t:ty),*) => {$(
186 impl mitsuami_reactive::IntoValue<$t> for $t {
187 fn into_value(self) -> mitsuami_reactive::Value<$t> {
188 mitsuami_reactive::Value::Static(self)
189 }
190 }
191 )*};
192}
193
194static_value!(TextStyle, ButtonVariant, ScrollAxes);