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::{Insets, Point, Rect, Size};
6use crate::input::SurfaceInput;
7use crate::services::Shortcut;
8use crate::surface::{SurfaceHandle, SurfaceSize};
9use crate::widget::{ColumnSort, NodeId, Prop, RowKey, WidgetKind};
10
11/// A change the backend must apply to the native widget tree.
12///
13/// Commands arrive in batches. Within a batch, order matters: a node is
14/// always created before it is inserted, and a subtree root is removed before
15/// it is destroyed.
16#[derive(Clone, Debug, PartialEq)]
17pub enum Command {
18 /// Native views start with a zero frame; the core only sends frames
19 /// that differ from the last one it sent.
20 Create {
21 id: NodeId,
22 kind: WidgetKind,
23 props: Vec<Prop>,
24 },
25 SetProp {
26 id: NodeId,
27 prop: Prop,
28 },
29 Insert {
30 parent: NodeId,
31 child: NodeId,
32 index: usize,
33 },
34 Remove {
35 parent: NodeId,
36 child: NodeId,
37 },
38 /// Frees a node. Sent for every native node of a destroyed subtree,
39 /// children first. The subtree's root has already been removed from its
40 /// parent; nodes inside it may still be attached to each other.
41 Destroy {
42 id: NodeId,
43 },
44 /// Parent-relative, logical units. Never sent for windows.
45 SetFrame {
46 id: NodeId,
47 frame: Rect,
48 },
49 SetA11y {
50 id: NodeId,
51 a11y: A11yProps,
52 },
53 /// Window content size requested by the app. The platform may refuse it
54 /// (a window in full screen keeps the screen's), or give it no smaller
55 /// than the window's `MinSize`; it reports the size it gives as
56 /// `WindowResized`.
57 SetWindowSize {
58 id: NodeId,
59 size: Size,
60 },
61 /// The keyboard (Tab) order of a window's focusable controls. Sent when
62 /// it changes. Backends chain focus in this order; which controls can
63 /// actually take focus stays a platform decision (e.g. macOS keyboard
64 /// navigation settings).
65 SetFocusOrder {
66 window: NodeId,
67 order: Vec<NodeId>,
68 },
69 /// Scrolls a `ScrollView` or `List` so `offset` (content coordinates) is at its
70 /// top-left. Already clamped by the core. Like user scrolling, it makes
71 /// the backend report `Scrolled`.
72 ScrollTo {
73 id: NodeId,
74 offset: Point,
75 },
76 Focus {
77 id: NodeId,
78 },
79 /// Selects these characters of a text field's or text area's text,
80 /// counted in Unicode scalar values, within it and on grapheme
81 /// boundaries (the app counts graphemes). A `Focus` for the
82 /// field comes first in the batch: a selection is the focused field's.
83 SelectText {
84 id: NodeId,
85 range: std::ops::Range<usize>,
86 },
87 /// Scrolls a `List` or `Table` just enough to show a row, as the platform's own
88 /// "scroll to row" does. The platform then reports `Scrolled`, and the
89 /// rows it shows.
90 ScrollToRow {
91 id: NodeId,
92 row: RowKey,
93 },
94}
95
96#[derive(Clone, Debug, PartialEq)]
97pub enum EventValue {
98 Text(String),
99 Bool(bool),
100 /// A `Slider`'s or `NumberInput`'s new value.
101 Number(f64),
102 /// A `List`'s or `Table`'s selected rows.
103 Rows(Vec<RowKey>),
104 /// The column a `Table` is sorted by now, and which way.
105 Sort(ColumnSort),
106 /// The option chosen in a `Select` or `RadioGroup`, the item or tab
107 /// chosen in a `Sidebar`, `Tabs` or `DocumentTabs`.
108 Index(usize),
109}
110
111/// Something that happened in the native UI.
112#[derive(Clone, Debug, PartialEq)]
113pub enum UiEvent {
114 Click,
115 /// The user changed a control's value. The native widget already shows it.
116 Changed(EventValue),
117 Submit,
118 /// A `SearchInput` asks for a search for its text, when the platform
119 /// does: as the user types (after a pause, where the platform waits for
120 /// one), on Return, and when the field is cleared. Not for text the
121 /// core set.
122 Search(String),
123 FocusIn,
124 FocusOut,
125 WindowResized(Size),
126 WindowCloseRequested,
127 /// The user put a window in full screen, or took it out (the title
128 /// bar's button, the window manager's key), or the platform did. Not
129 /// reported for the app's own `FullScreen`. The core absorbs it as
130 /// `FullScreen`.
131 FullScreenChanged(bool),
132 /// The user maximized a window or restored it (the title bar's button,
133 /// a double-click on it, the window manager's key), or the platform
134 /// did. Not reported for the app's own `Maximized`. The core absorbs
135 /// it as `Maximized`.
136 MaximizedChanged(bool),
137 /// The user showed or hid a `Sidebar` (the platform's own toggle, its
138 /// divider dragged away), or the platform did. Not reported for the
139 /// app's own `SidebarShown`. The core absorbs it as `SidebarShown`.
140 SidebarShownChanged(bool),
141 /// A `Sidebar`'s item was activated, as a list's row is (double-clicked,
142 /// or Enter pressed on it), where the platform's sidebar has that: its
143 /// index across the sections.
144 SidebarItemActivated(usize),
145 /// The user asked to close one of `DocumentTabs`' tabs (its close
146 /// button, a middle click, the platform's own close): its index. The
147 /// tab stays until the app takes it away.
148 TabCloseRequested(usize),
149 /// The user moved one of `DocumentTabs`' tabs from `from` to `to`
150 /// (dragged it, or moved it from the keyboard). The platform already
151 /// shows it there; the core absorbs it into `TabTitles` and
152 /// `SelectedIndex`.
153 TabMoved {
154 from: usize,
155 to: usize,
156 },
157 /// The user asked `DocumentTabs` for a new tab (the platform's new-tab
158 /// button).
159 NewTabRequested,
160 /// Platform metrics changed (text size, color scheme, …).
161 MetricsChanged,
162 /// A `ScrollView`'s or `List`'s scroll offset changed (by the user or
163 /// by `ScrollTo`).
164 Scrolled(Point),
165 /// A `List` or `Table` realised a row: it's in view, or about to be.
166 /// The core mounts the row and sends its host (a table's: its cells').
167 RowShown(RowKey),
168 /// A `List` or `Table` let go of a row it had shown. The core disposes it.
169 RowHidden(RowKey),
170 /// A `List`'s or `Table`'s row was activated: double-clicked, or Enter
171 /// pressed on it.
172 RowActivated(RowKey),
173 /// The width a `List` gives its rows, when it isn't the list's own
174 /// width (scroll bars that take room from the rows, list insets). Rows
175 /// are laid out at that width.
176 RowWidth(f32),
177 /// Where a `ScrollView`'s viewport is in its frame, when its scroll
178 /// bars or frame take room from its content (classic scroll bars, not
179 /// overlay ones). Its content is laid out in what's left.
180 ViewportInsets(Insets),
181 /// The widths a `Table`'s columns give their cells, in column order:
182 /// once they're known, and whenever they change (the user resized a
183 /// column, the table was resized). Cells are laid out at them.
184 ColumnWidths(Vec<f32>),
185 /// A pointer event on a drawn custom widget, in its coordinates.
186 Pointer(PointerEvent),
187 /// A widget's natural size changed on its own (an image finished
188 /// loading); the core measures it again.
189 Remeasure,
190 /// An event of a custom widget or native view, of its own type.
191 Custom(AnyValue),
192 /// The item of the node's context menu with this id was chosen.
193 ContextMenuItem(u32),
194 /// The pointer came over a node that reports it (`Prop::Hover`), or
195 /// over anything inside it (`true`), or left it (`false`).
196 Hover(bool),
197 /// A node that reports it (`Prop::DoubleClick`) was double-clicked.
198 DoubleClick,
199 /// The item of a `MenuButton`'s menu with this id was chosen.
200 MenuItem(u32),
201 /// A key the node takes (`Prop::Keys`) was pressed while it, or a
202 /// control inside it that doesn't use the key, had keyboard focus.
203 Key(Shortcut),
204 /// Files a node takes (`Prop::FileDrop`) are being dragged over it
205 /// (`true`), or no longer are: they left, or were dropped.
206 DropHover(bool),
207 /// Files and folders were dropped on a node: the ones it takes, in
208 /// the order the platform gave them.
209 FilesDropped(Vec<std::path::PathBuf>),
210 /// A `GpuSurface`'s native surface exists: the app can make its GPU
211 /// surface on it. Reported once, before any `SurfaceResized`; its size
212 /// may still be empty.
213 SurfaceReady(SurfaceHandle),
214 /// A `GpuSurface`'s size in pixels, or its scale, changed.
215 SurfaceResized(SurfaceSize),
216 /// Keys or the pointer on a `GpuSurface` that takes input.
217 SurfaceInput(SurfaceInput),
218 /// The platform ended a `GpuSurface`'s pointer lock, or couldn't make
219 /// it: its window stopped being the active one. The core absorbs it
220 /// as `PointerLock(false)`.
221 PointerLockEnded,
222 /// The platform ended a `GpuSurface`'s keyboard grab, or couldn't make
223 /// it: the surface lost focus, or its window stopped being the active
224 /// one. The core absorbs it as `KeyboardGrab(false)`.
225 KeyboardGrabEnded,
226}
227
228#[derive(Clone, Copy, Debug, PartialEq, Eq)]
229pub enum PointerKind {
230 Down,
231 Up,
232}
233
234/// A primary-button pointer event. Position in the node's coordinates.
235#[derive(Clone, Copy, Debug, PartialEq)]
236pub struct PointerEvent {
237 pub kind: PointerKind,
238 pub position: Point,
239}