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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! The contract every platform backend implements.
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
use crate::a11y::{A11yAction, ActionError};
use crate::app_info::{AppInfo, NativeAppInfo};
use crate::command::{Command, UiEvent};
use crate::geometry::{Insets, Point, Rect, Size};
use crate::services::Shortcut;
use crate::units::SpacingScale;
use crate::widget::{NodeId, Prop, TextStyle, WidgetKind};
/// Space available on one axis while measuring.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AvailableSpace {
Definite(f32),
MinContent,
MaxContent,
}
/// What the layout engine asks when it measures a leaf widget.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MeasureRequest {
/// Sizes that are already fixed; measure the other axis given these.
pub known_width: Option<f32>,
pub known_height: Option<f32>,
pub available_width: AvailableSpace,
pub available_height: AvailableSpace,
}
/// Platform facts the core needs for unit resolution and defaults.
#[derive(Clone, Debug, PartialEq)]
pub struct PlatformMetrics {
pub scale_factor: f32,
pub spacing: SpacingScale,
pub font_sizes: FontSizes,
pub dark_mode: bool,
pub high_contrast: bool,
pub reduced_motion: bool,
/// Where a `Tabs` shows its pages: this far in from its edges, past
/// its tab strip and border.
pub tab_insets: Insets,
/// Where a `Group` shows its content: this far in from its edges,
/// past its border and the platform's margins; `titled_group_insets`
/// with a heading, which is inside the box on some platforms and above
/// it on others.
pub group_insets: Insets,
pub titled_group_insets: Insets,
/// How thick a `Split`'s divider is, between its panes.
pub split_divider: f32,
/// Where a `TextArea`'s first line starts: this far in from its edges,
/// past its border and the text's own margins. What a hint laid over
/// it lines up with.
pub text_area_insets: Insets,
}
/// Light or dark. Backends can force one (tests do, so captures don't
/// depend on the system setting).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Appearance {
Light,
Dark,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FontSizes {
pub large_title: f32,
pub title: f32,
pub headline: f32,
pub body: f32,
pub callout: f32,
pub caption: f32,
pub monospace: f32,
}
impl FontSizes {
pub fn get(&self, style: TextStyle) -> f32 {
match style {
TextStyle::LargeTitle => self.large_title,
TextStyle::Title => self.title,
TextStyle::Headline => self.headline,
TextStyle::Body => self.body,
TextStyle::Callout => self.callout,
TextStyle::Caption => self.caption,
TextStyle::Monospace => self.monospace,
}
}
}
/// Raw input for [`Backend::synthesize`].
#[derive(Clone, Debug, PartialEq)]
pub enum SyntheticInput {
/// A key pressed and released, on its own.
Key(Key),
/// A key pressed and released with modifiers held: what a node's keys
/// (`Prop::Keys`) take, where the focused control doesn't use it.
Shortcut(Shortcut),
/// An input method commits this text on a `GpuSurface` that takes
/// text, as a composition ends.
Text(String),
/// An input method's composition on a `GpuSurface` that takes text
/// shows this text so far (empty: it ends without committing).
Compose(String),
/// Scroll-wheel / trackpad scroll over a `ScrollView` or `List`, in logical units
/// (positive = towards the end of the content).
Scroll { dx: f32, dy: f32 },
/// A primary-button click (down, then up) at this point, in the node's
/// coordinates. Backends support it on drawn custom widgets, whose
/// pointer handling is ours.
Click(Point),
/// The pointer comes over a node with a `Prop::Hover`, through the
/// platform's own hover handling where it can be reached.
PointerEnter,
/// The pointer leaves a node with a `Prop::Hover`.
PointerLeave,
/// A double click on a node with a `Prop::DoubleClick`, through the
/// platform's own handling where it can be reached.
DoubleClick,
/// A click on a node with a `Prop::Click`, as for `DoubleClick`.
SingleClick,
/// Files dragged from the file manager, over a node with a
/// `Prop::FileDrop`: what the platform's drag handling does when they
/// enter it, through the same path.
DragFiles(Vec<std::path::PathBuf>),
/// The drag leaves the node without dropping.
DragLeave,
/// The files are dropped on the node (having entered it first, as a
/// real drop does).
DropFiles(Vec<std::path::PathBuf>),
/// Rows of a list with `Prop::RowMoves` dragged within it and dropped
/// where the platform proposed, through the list's own drop handling.
MoveRows { rows: Vec<crate::RowKey>, to: crate::RowDrop<crate::RowKey> },
}
/// A key, by what it types or does: a character (`Char(' ')` is the
/// space bar), or a key that types none. For shortcuts, key bindings and
/// synthesized input.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Key {
Char(char),
Enter,
Escape,
Tab,
/// The key that deletes backwards: Delete (⌫) on Apple keyboards.
Backspace,
/// The key that deletes forwards: ⌦ on Apple keyboards.
Delete,
Up,
Down,
Left,
Right,
Home,
End,
PageUp,
PageDown,
/// A function key, from F1.
F(u8),
}
impl From<char> for Key {
fn from(c: char) -> Key {
Key::Char(c)
}
}
/// What a native widget actually shows, read back from the platform.
/// Tests compare this with the core's state to catch backend desyncs.
#[derive(Clone, Debug, PartialEq)]
pub struct NativeState {
pub kind: WidgetKind,
pub props: Vec<Prop>,
pub frame: Rect,
pub parent: Option<NodeId>,
pub children: Vec<NodeId>,
/// Has keyboard focus (for text fields: is being edited).
pub focused: bool,
/// `ScrollView`s, `List`s, `Table`s and `TextArea`s only: the current
/// scroll offset (a text area's, in its own scroll view).
pub scroll_offset: Option<Point>,
/// `ScrollView`s only: where the viewport is in the frame, inside the
/// room scroll bars that aren't overlaid take (`UiEvent::ViewportInsets`).
pub viewport_insets: Option<Insets>,
/// A focused text field's or text area's selection, in characters (an
/// empty one is the caret); `None` for anything else.
pub selection: Option<std::ops::Range<usize>>,
}
/// An RGBA8 screenshot in physical pixels.
#[derive(Clone, Debug, PartialEq)]
pub struct Image {
pub width: u32,
pub height: u32,
pub scale_factor: f32,
pub rgba: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CaptureError {
UnknownNode,
Unsupported,
Failed(String),
}
/// How backends report native events. Events are queued; the [`Ui`](crate::Ui)
/// drains them when the backend calls [`Ui::process_events`](crate::Ui::process_events)
/// (or right after a [`Backend::perform`]).
#[derive(Clone, Default)]
pub struct EventSink {
queue: Rc<RefCell<VecDeque<(NodeId, UiEvent)>>>,
}
impl EventSink {
pub fn emit(&self, id: NodeId, event: UiEvent) {
self.queue.borrow_mut().push_back((id, event));
}
pub(crate) fn pop(&self) -> Option<(NodeId, UiEvent)> {
self.queue.borrow_mut().pop_front()
}
pub fn is_empty(&self) -> bool {
self.queue.borrow().is_empty()
}
}
/// What the test harness (`mitsuami-test`) needs from a backend beyond the
/// [`Backend`] contract. Implemented by each backend's shareable handle.
pub trait TestHooks {
/// Short name used in snapshot and visual baseline paths: `"appkit"`,
/// `"gtk"`, `"winui"`, `"kirigami"`.
fn name(&self) -> &'static str;
/// Resizes a window's content area the way the user would, so the
/// platform reports it back as `WindowResized`.
fn resize_window(&self, window: NodeId, size: Size);
/// Clicks the window's close button, the way the user would, so the
/// platform reports `WindowCloseRequested`. Whether it closes is the
/// app's call.
fn close_window(&self, window: NodeId);
/// Commands applied since the last call (record them when asked to).
fn take_command_log(&self) -> Vec<Command>;
/// Live native nodes, as a leak detector.
fn node_count(&self) -> usize;
/// What this window shows of the app's id, name and icon (see
/// [`AppInfo`]), read from the platform.
fn app_info(&self, window: NodeId) -> NativeAppInfo;
/// The files dragging this row out of its `List` or `Table` carries
/// (`Prop::RowFiles`), as the platform's drag source gives them: the
/// selected rows' when the row is selected, else its own. `node` is the
/// row's host (a table's: any of its cells'). `None` if it doesn't drag.
/// Nothing here can start a real drag; this goes through the backend's
/// own drag handling up to where the platform takes the files.
fn dragged_files(&self, node: NodeId) -> Option<Vec<std::path::PathBuf>>;
/// The node a click at `at`, in a window's content, reaches as the
/// platform hit-tests it: the topmost there, past containers clicks
/// pass through (`Prop::ClickThrough`). `None` if it reaches none;
/// `Unsupported` where the backend can't tell yet.
fn hit(&self, _window: NodeId, _at: Point) -> Result<Option<NodeId>, ActionError> {
Err(ActionError::Unsupported)
}
/// Called after every settle: lets the platform catch up on work it
/// does asynchronously (showing windows, allocating, delivering queued
/// notifications) without waiting. Every backend has some: AppKit lays
/// out tables and toolbars, which make their views in a layout pass.
fn settle(&self) {}
}
pub trait Backend {
/// Called once when the backend is attached to a [`Ui`](crate::Ui).
fn init(&mut self, events: EventSink);
fn metrics(&self) -> PlatformMetrics;
/// Where this `Group` puts its content, if not where the metrics'
/// `group_insets` or `titled_group_insets` say: a tweak can move its
/// heading or change its border. Asked when the group is measured.
fn group_insets(&self, _id: NodeId) -> Option<Insets> {
None
}
/// Where this `Tabs` puts its pages, if not where the metrics'
/// `tab_insets` say: its style can give it another strip. Asked when
/// its strip is measured.
fn tab_insets(&self, _id: NodeId) -> Option<Insets> {
None
}
/// Applies a batch of commands to the native tree.
fn apply(&mut self, batch: &[Command]);
/// Intrinsic size of a leaf widget. Called synchronously during layout,
/// after all commands of the current batch have been applied.
fn measure(&mut self, id: NodeId, request: MeasureRequest) -> Size;
/// Performs an accessibility action on the native control, as assistive
/// technology would. Resulting events go through the [`EventSink`].
fn perform(&mut self, id: NodeId, action: &A11yAction) -> Result<(), ActionError>;
/// Synthesizes raw user input on a native control, as close to real input
/// as the platform allows. Used by end-to-end tests.
fn synthesize(&mut self, id: NodeId, input: &SyntheticInput) -> Result<(), ActionError>;
fn native_state(&self, id: NodeId) -> Option<NativeState>;
/// Offscreen screenshot of a window or node. Async because some
/// platforms render asynchronously (WinUI's `RenderTargetBitmap`);
/// reply whenever it's ready, right away if possible.
fn capture(&mut self, id: NodeId, reply: crate::services::Reply<Result<Image, CaptureError>>);
/// The platform's clipboard, dialogs and menus. Called once, when the
/// backend is attached; tests may replace the result.
fn services(&self) -> Box<dyn crate::services::Services>;
/// The app's id, name and icon, set before the app's first window, and
/// perhaps again later (tests). Apply what the platform has a place
/// for, to the windows there are and those to come.
fn set_app_info(&mut self, info: &AppInfo);
/// The user's languages, and how the user's region writes numbers
/// and dates. Called once, when the backend is attached.
fn locale(&self) -> std::rc::Rc<dyn crate::l10n::PlatformLocale>;
/// The app's language, which the core chose from the user's, and
/// whether it's written right to left: set before the app's first
/// window, and again when the app changes it. Make the toolkit's own
/// chrome follow it where the platform lets an app choose (the
/// direction of title bars, menus and dialogs). Native widgets get
/// their direction as `Prop::LayoutDirection`.
fn set_locale(&mut self, language: &crate::l10n::LanguageIdentifier, right_to_left: bool);
}