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