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, TreeIndent, 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 /// Brings a window forward as the platform's apps do when asked to
80 /// show one that's open already: the app activated, the window
81 /// restored if minimized, raised and made the one taking keys. Before
82 /// any `Focus` in the batch. A window not shown yet (created in this
83 /// batch) comes forward when it is.
84 Present {
85 window: NodeId,
86 },
87 /// Selects these characters of a text field's or text area's text,
88 /// counted in Unicode scalar values, within it and on grapheme
89 /// boundaries (the app counts graphemes). A `Focus` for the
90 /// field comes first in the batch: a selection is the focused field's.
91 SelectText {
92 id: NodeId,
93 range: std::ops::Range<usize>,
94 },
95 /// Scrolls a `List` or `Table` just enough to show a row, as the platform's own
96 /// "scroll to row" does. The platform then reports `Scrolled`, and the
97 /// rows it shows.
98 ScrollToRow {
99 id: NodeId,
100 row: RowKey,
101 },
102}
103
104#[derive(Clone, Debug, PartialEq)]
105pub enum EventValue {
106 Text(String),
107 Bool(bool),
108 /// A `Slider`'s or `NumberInput`'s new value.
109 Number(f64),
110 /// A `ColorWell`'s new colour, an opaque [`Color::Rgba`].
111 Color(crate::draw::Color),
112 /// A `List`'s or `Table`'s selected rows.
113 Rows(Vec<RowKey>),
114 /// The column a `Table` is sorted by now, and which way.
115 Sort(ColumnSort),
116 /// The option chosen in a `Select` or `RadioGroup`, the item or tab
117 /// chosen in a `Sidebar`, `Tabs` or `DocumentTabs`.
118 Index(usize),
119}
120
121/// Something that happened in the native UI.
122#[derive(Clone, Debug, PartialEq)]
123pub enum UiEvent {
124 Click,
125 /// The user changed a control's value. The native widget already shows it.
126 Changed(EventValue),
127 Submit,
128 /// A `SearchInput` asks for a search for its text, when the platform
129 /// does: as the user types (after a pause, where the platform waits for
130 /// one), on Return, and when the field is cleared. Not for text the
131 /// core set.
132 Search(String),
133 /// The user picked a `SearchInput`'s suggestion (a click, or Return on
134 /// the one the arrows reached): its index in `Suggestions`. The field's
135 /// text is the app's to set.
136 SuggestionChosen(usize),
137 FocusIn,
138 FocusOut,
139 WindowResized(Size),
140 WindowCloseRequested,
141 /// A window became the active one, the one taking keys, or stopped being it. It keeps its
142 /// focused control through this, so no `FocusOut` comes. The core passes it on to the
143 /// `GpuSurface`s in the window, which paint their own carets.
144 WindowActiveChanged(bool),
145 /// The user put a window in full screen, or took it out (the title
146 /// bar's button, the window manager's key), or the platform did. Not
147 /// reported for the app's own `FullScreen`. The core absorbs it as
148 /// `FullScreen`.
149 FullScreenChanged(bool),
150 /// The user maximized a window or restored it (the title bar's button,
151 /// a double-click on it, the window manager's key), or the platform
152 /// did. Not reported for the app's own `Maximized`. The core absorbs
153 /// it as `Maximized`.
154 MaximizedChanged(bool),
155 /// The user showed or hid a `Sidebar` (the platform's own toggle, its
156 /// divider dragged away), or the platform did. Not reported for the
157 /// app's own `SidebarShown`. The core absorbs it as `SidebarShown`.
158 SidebarShownChanged(bool),
159 /// The user dismissed a `Popover` (a click outside, Escape), or the
160 /// platform did. Not reported for the app's own `PopoverShown`. The
161 /// core absorbs it as `PopoverShown`.
162 PopoverShownChanged(bool),
163 /// A `Sidebar`'s item was activated, as a list's row is (double-clicked,
164 /// or Enter pressed on it), where the platform's sidebar has that: its
165 /// index across the sections.
166 SidebarItemActivated(usize),
167 /// The user asked to close one of `DocumentTabs`' tabs (its close
168 /// button, a middle click, the platform's own close): its index. The
169 /// tab stays until the app takes it away.
170 TabCloseRequested(usize),
171 /// The user moved one of `DocumentTabs`' tabs from `from` to `to`
172 /// (dragged it, or moved it from the keyboard). The platform already
173 /// shows it there; the core absorbs it into `TabTitles` and
174 /// `SelectedIndex`.
175 TabMoved {
176 from: usize,
177 to: usize,
178 },
179 /// The user asked `DocumentTabs` for a new tab (the platform's new-tab
180 /// button).
181 NewTabRequested,
182 /// Platform metrics changed (text size, color scheme, …).
183 MetricsChanged,
184 /// A `ScrollView`'s or `List`'s scroll offset changed (by the user or
185 /// by `ScrollTo`).
186 Scrolled(Point),
187 /// A `List` or `Table` realised a row: it's in view, or about to be.
188 /// The core mounts the row and sends its host (a table's: its cells').
189 RowShown(RowKey),
190 /// A `List` or `Table` let go of a row it had shown. The core disposes it.
191 RowHidden(RowKey),
192 /// A `List`'s or `Table`'s row was activated: double-clicked, or Enter
193 /// pressed on it.
194 RowActivated(RowKey),
195 /// The width a `List` gives its rows, when it isn't the list's own
196 /// width (scroll bars that take room from the rows, list insets). Rows
197 /// are laid out at that width.
198 RowWidth(f32),
199 /// Where a `ScrollView`'s viewport is in its frame, when its scroll
200 /// bars or frame take room from its content (classic scroll bars, not
201 /// overlay ones). Its content is laid out in what's left.
202 ViewportInsets(Insets),
203 /// The widths a `Table`'s columns give their cells, in column order:
204 /// once they're known, and whenever they change (the user resized a
205 /// column, the table was resized). Cells are laid out at them.
206 ColumnWidths(Vec<f32>),
207 /// How high a `Table`'s row is without its cells that fill their row
208 /// (`ColumnData::fills_row`): as its other cells make it, and at least
209 /// the platform's row height. Sent for tables with such a column, once
210 /// known and whenever it changes; those cells are laid out at least
211 /// this high, so they fill the row the platform makes.
212 RowHeight(RowKey, f32),
213 /// The user opened (`true`) or closed a `Tree`'s row: its disclosure,
214 /// the platform's keys, assistive technology. The core takes it into
215 /// the open set and sends the rows again. A platform that opens
216 /// several rows at once reports each.
217 RowExpanded(RowKey, bool),
218 /// How far a `Tree` indents its rows' content: once known, and
219 /// whenever it changes. Rows are laid out at the rows' width less it.
220 TreeIndent(TreeIndent),
221 /// A pointer event on a drawn custom widget, in its coordinates.
222 Pointer(PointerEvent),
223 /// A widget's natural size changed on its own (an image finished
224 /// loading); the core measures it again.
225 Remeasure,
226 /// An event of a custom widget or native view, of its own type.
227 Custom(AnyValue),
228 /// The item of the node's context menu with this id was chosen.
229 ContextMenuItem(u32),
230 /// The pointer came over a node that reports it (`Prop::Hover`), or
231 /// over anything inside it (`true`), or left it (`false`).
232 Hover(bool),
233 /// A node that reports it (`Prop::DoubleClick`) was double-clicked.
234 DoubleClick,
235 /// The item of a `MenuButton`'s or `SplitButton`'s menu with this id
236 /// was chosen.
237 MenuItem(u32),
238 /// A key the node takes (`Prop::Keys`) was pressed while it, or a
239 /// control inside it that doesn't use the key, had keyboard focus.
240 Key(Shortcut),
241 /// Files a node takes (`Prop::FileDrop`) are being dragged over it
242 /// (`true`), or no longer are: they left, or were dropped.
243 DropHover(bool),
244 /// Files and folders were dropped on a node: the ones it takes, in
245 /// the order the platform gave them.
246 FilesDropped(Vec<std::path::PathBuf>),
247 /// Rows dragged within a list were dropped (`Prop::RowMoves`): the
248 /// rows, in the list's order, and where, as `RowMoves::target` put it.
249 /// The list shows nothing moved: the app moves its data.
250 RowsMoved {
251 rows: Vec<crate::RowKey>,
252 to: crate::RowDrop<crate::RowKey>,
253 },
254 /// A `GpuSurface`'s native surface exists: the app can make its GPU
255 /// surface on it. Reported once, before any `SurfaceResized`; its size
256 /// may still be empty.
257 SurfaceReady(SurfaceHandle),
258 /// A `GpuSurface`'s size in pixels, or its scale, changed.
259 SurfaceResized(SurfaceSize),
260 /// Keys or the pointer on a `GpuSurface` that takes input.
261 SurfaceInput(SurfaceInput),
262 /// The platform ended a `GpuSurface`'s pointer lock, or couldn't make
263 /// it: its window stopped being the active one. The core absorbs it
264 /// as `PointerLock(false)`.
265 PointerLockEnded,
266 /// The platform ended a `GpuSurface`'s keyboard grab, or couldn't make
267 /// it: the surface lost focus, or its window stopped being the active
268 /// one. The core absorbs it as `KeyboardGrab(false)`.
269 KeyboardGrabEnded,
270}
271
272#[derive(Clone, Copy, Debug, PartialEq, Eq)]
273pub enum PointerKind {
274 Down,
275 Up,
276}
277
278/// A primary-button pointer event. Position in the node's coordinates.
279#[derive(Clone, Copy, Debug, PartialEq)]
280pub struct PointerEvent {
281 pub kind: PointerKind,
282 pub position: Point,
283}