1use std::cell::RefCell;
4use std::collections::VecDeque;
5use std::rc::Rc;
6
7use crate::a11y::{A11yAction, ActionError};
8use crate::command::{Command, UiEvent};
9use crate::geometry::{Point, Rect, Size};
10use crate::units::SpacingScale;
11use crate::widget::{NodeId, Prop, TextStyle, WidgetKind};
12
13#[derive(Clone, Copy, Debug, PartialEq)]
15pub enum AvailableSpace {
16 Definite(f32),
17 MinContent,
18 MaxContent,
19}
20
21#[derive(Clone, Copy, Debug, PartialEq)]
23pub struct MeasureRequest {
24 pub known_width: Option<f32>,
26 pub known_height: Option<f32>,
27 pub available_width: AvailableSpace,
28 pub available_height: AvailableSpace,
29}
30
31#[derive(Clone, Debug, PartialEq)]
33pub struct PlatformMetrics {
34 pub scale_factor: f32,
35 pub spacing: SpacingScale,
36 pub font_sizes: FontSizes,
37 pub dark_mode: bool,
38 pub high_contrast: bool,
39 pub reduced_motion: bool,
40}
41
42#[derive(Clone, Debug, PartialEq)]
43pub struct FontSizes {
44 pub large_title: f32,
45 pub title: f32,
46 pub headline: f32,
47 pub body: f32,
48 pub callout: f32,
49 pub caption: f32,
50 pub monospace: f32,
51}
52
53impl FontSizes {
54 pub fn get(&self, style: TextStyle) -> f32 {
55 match style {
56 TextStyle::LargeTitle => self.large_title,
57 TextStyle::Title => self.title,
58 TextStyle::Headline => self.headline,
59 TextStyle::Body => self.body,
60 TextStyle::Callout => self.callout,
61 TextStyle::Caption => self.caption,
62 TextStyle::Monospace => self.monospace,
63 }
64 }
65}
66
67#[derive(Clone, Debug, PartialEq)]
69pub enum SyntheticInput {
70 Key(Key),
71 Scroll {
74 dx: f32,
75 dy: f32,
76 },
77 Click(Point),
81}
82
83#[derive(Clone, Copy, Debug, PartialEq, Eq)]
84pub enum Key {
85 Char(char),
86 Enter,
87 Escape,
88 Tab,
89 Backspace,
90}
91
92#[derive(Clone, Debug, PartialEq)]
95pub struct NativeState {
96 pub kind: WidgetKind,
97 pub props: Vec<Prop>,
98 pub frame: Rect,
99 pub parent: Option<NodeId>,
100 pub children: Vec<NodeId>,
101 pub focused: bool,
103 pub scroll_offset: Option<Point>,
105}
106
107#[derive(Clone, Debug, PartialEq)]
109pub struct Image {
110 pub width: u32,
111 pub height: u32,
112 pub scale_factor: f32,
113 pub rgba: Vec<u8>,
114}
115
116#[derive(Clone, Debug, PartialEq, Eq)]
117pub enum CaptureError {
118 UnknownNode,
119 Unsupported,
120 Failed(String),
121}
122
123#[derive(Clone, Default)]
127pub struct EventSink {
128 queue: Rc<RefCell<VecDeque<(NodeId, UiEvent)>>>,
129}
130
131impl EventSink {
132 pub fn emit(&self, id: NodeId, event: UiEvent) {
133 self.queue.borrow_mut().push_back((id, event));
134 }
135
136 pub(crate) fn pop(&self) -> Option<(NodeId, UiEvent)> {
137 self.queue.borrow_mut().pop_front()
138 }
139
140 pub fn is_empty(&self) -> bool {
141 self.queue.borrow().is_empty()
142 }
143}
144
145pub trait TestHooks {
148 fn name(&self) -> &'static str;
151 fn resize_window(&self, window: NodeId, size: Size);
154 fn take_command_log(&self) -> Vec<Command>;
156 fn node_count(&self) -> usize;
158 fn settle(&self) {}
163}
164
165pub trait Backend {
166 fn init(&mut self, events: EventSink);
168
169 fn metrics(&self) -> PlatformMetrics;
170
171 fn apply(&mut self, batch: &[Command]);
173
174 fn measure(&mut self, id: NodeId, request: MeasureRequest) -> Size;
177
178 fn perform(&mut self, id: NodeId, action: &A11yAction) -> Result<(), ActionError>;
181
182 fn synthesize(&mut self, id: NodeId, input: &SyntheticInput) -> Result<(), ActionError>;
185
186 fn native_state(&self, id: NodeId) -> Option<NativeState>;
187
188 fn capture(&mut self, id: NodeId, reply: crate::services::Reply<Result<Image, CaptureError>>);
192
193 fn services(&self) -> Box<dyn crate::services::Services>;
196}