Skip to main content

mitsuami_core/
widget.rs

1//! What the core tells backends to create, and the properties they carry.
2
3use std::fmt;
4
5use crate::any_value::Opaque;
6use crate::custom::CustomProps;
7use crate::draw::DisplayList;
8
9/// Stable identity of a node for the lifetime of a [`Ui`](crate::Ui).
10#[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    /// For backends and tools that need to rebuild ids, e.g. when replaying a
19    /// command log.
20    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    /// A layout host: a plain native view that we position children in.
35    Container,
36    /// Core-only grouping used by control flow (`Show`, `For`). Never sent to
37    /// backends; its children are spliced into the nearest native ancestor.
38    Fragment,
39    Text,
40    Button,
41    TextInput,
42    Checkbox,
43    Switch,
44    /// A native scroll container. It has exactly one native child, the
45    /// content, which the core lays out and may be larger than the viewport.
46    ScrollView,
47    /// A custom widget (see [`CustomWidget`](crate::CustomWidget)), named
48    /// after it. Its props travel as [`Prop::Custom`].
49    Custom(&'static str),
50    /// A raw native view supplied by app code. Its factory and updates
51    /// travel as [`Prop::Native`].
52    Native,
53}
54
55impl WidgetKind {
56    pub fn is_native(self) -> bool {
57        self != WidgetKind::Fragment
58    }
59
60    /// Containers lay out children; everything else is measured by the backend.
61    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/// Semantic text styles, mapped to each platform's type ramp.
83#[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/// Semantic button variants, mapped to each platform's native styles.
96#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
97pub enum ButtonVariant {
98    #[default]
99    Default,
100    Primary,
101    Destructive,
102    Plain,
103}
104
105/// Scrolling directions of a `ScrollView`.
106#[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/// A property of a native widget. Which ones apply depends on the kind.
125#[derive(Clone, Debug, PartialEq)]
126pub enum Prop {
127    /// Window title.
128    Title(String),
129    /// Text content of a `Text`.
130    Text(String),
131    /// Caption of a `Button`, `Checkbox` or `Switch`.
132    Label(String),
133    /// Current text of a `TextInput`.
134    Value(String),
135    Placeholder(String),
136    Checked(bool),
137    Enabled(bool),
138    TextStyle(TextStyle),
139    Variant(ButtonVariant),
140    /// Which axes a `ScrollView` scrolls.
141    ScrollAxes(ScrollAxes),
142    /// A custom widget's props, with its renders.
143    Custom(CustomProps),
144    /// What a drawn custom widget shows. Computed by the core after layout,
145    /// so it arrives with the frames.
146    Drawing(DisplayList),
147    /// A `Native` node's factory (on create), then its updates: payloads in
148    /// the backend's own form.
149    Native(Opaque),
150}
151
152impl Prop {
153    /// Two props with the same key replace each other.
154    pub fn key(&self) -> std::mem::Discriminant<Prop> {
155        std::mem::discriminant(self)
156    }
157
158    /// Whether changing this prop can change the widget's intrinsic size.
159    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/// Finds a prop by pattern in a prop list.
174#[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);