Skip to main content

mitsuami_core/
command.rs

1//! The data flowing between the core and a backend.
2
3use crate::a11y::A11yProps;
4use crate::any_value::AnyValue;
5use crate::geometry::{Point, Rect, Size};
6use crate::widget::{NodeId, Prop, WidgetKind};
7
8/// A change the backend must apply to the native widget tree.
9///
10/// Commands arrive in batches. Within a batch, order matters: a node is
11/// always created before it is inserted, and a subtree root is removed before
12/// it is destroyed.
13#[derive(Clone, Debug, PartialEq)]
14pub enum Command {
15    /// Native views start with a zero frame; the core only sends frames
16    /// that differ from the last one it sent.
17    Create {
18        id: NodeId,
19        kind: WidgetKind,
20        props: Vec<Prop>,
21    },
22    SetProp {
23        id: NodeId,
24        prop: Prop,
25    },
26    Insert {
27        parent: NodeId,
28        child: NodeId,
29        index: usize,
30    },
31    Remove {
32        parent: NodeId,
33        child: NodeId,
34    },
35    /// Frees a node. Sent for every native node of a destroyed subtree,
36    /// children first. The subtree's root has already been removed from its
37    /// parent; nodes inside it may still be attached to each other.
38    Destroy {
39        id: NodeId,
40    },
41    /// Parent-relative, logical units. Never sent for windows.
42    SetFrame {
43        id: NodeId,
44        frame: Rect,
45    },
46    SetA11y {
47        id: NodeId,
48        a11y: A11yProps,
49    },
50    /// Window content size requested by the app. The platform may refuse it.
51    SetWindowSize {
52        id: NodeId,
53        size: Size,
54    },
55    /// The keyboard (Tab) order of a window's focusable controls. Sent when
56    /// it changes. Backends chain focus in this order; which controls can
57    /// actually take focus stays a platform decision (e.g. macOS keyboard
58    /// navigation settings).
59    SetFocusOrder {
60        window: NodeId,
61        order: Vec<NodeId>,
62    },
63    /// Scrolls a `ScrollView` so `offset` (content coordinates) is at its
64    /// top-left. Already clamped by the core. Like user scrolling, it makes
65    /// the backend report `Scrolled`.
66    ScrollTo {
67        id: NodeId,
68        offset: Point,
69    },
70    Focus {
71        id: NodeId,
72    },
73}
74
75#[derive(Clone, Debug, PartialEq)]
76pub enum EventValue {
77    Text(String),
78    Bool(bool),
79    Number(f64),
80}
81
82/// Something that happened in the native UI.
83#[derive(Clone, Debug, PartialEq)]
84pub enum UiEvent {
85    Click,
86    /// The user changed a control's value. The native widget already shows it.
87    Changed(EventValue),
88    Submit,
89    FocusIn,
90    FocusOut,
91    WindowResized(Size),
92    WindowCloseRequested,
93    /// Platform metrics changed (text size, color scheme, …).
94    MetricsChanged,
95    /// A `ScrollView`'s scroll offset changed (by the user or by `ScrollTo`).
96    Scrolled(Point),
97    /// A pointer event on a drawn custom widget, in its coordinates.
98    Pointer(PointerEvent),
99    /// An event of a custom widget or native view, of its own type.
100    Custom(AnyValue),
101}
102
103#[derive(Clone, Copy, Debug, PartialEq, Eq)]
104pub enum PointerKind {
105    Down,
106    Up,
107}
108
109/// A primary-button pointer event. Position in the node's coordinates.
110#[derive(Clone, Copy, Debug, PartialEq)]
111pub struct PointerEvent {
112    pub kind: PointerKind,
113    pub position: Point,
114}