use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
use crate::a11y::{A11yAction, ActionError};
use crate::command::{Command, UiEvent};
use crate::geometry::{Point, Rect, Size};
use crate::units::SpacingScale;
use crate::widget::{NodeId, Prop, TextStyle, WidgetKind};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AvailableSpace {
Definite(f32),
MinContent,
MaxContent,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MeasureRequest {
pub known_width: Option<f32>,
pub known_height: Option<f32>,
pub available_width: AvailableSpace,
pub available_height: AvailableSpace,
}
#[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,
}
#[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,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum SyntheticInput {
Key(Key),
Scroll {
dx: f32,
dy: f32,
},
Click(Point),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Key {
Char(char),
Enter,
Escape,
Tab,
Backspace,
}
#[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>,
pub focused: bool,
pub scroll_offset: Option<Point>,
}
#[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),
}
#[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()
}
}
pub trait TestHooks {
fn name(&self) -> &'static str;
fn resize_window(&self, window: NodeId, size: Size);
fn take_command_log(&self) -> Vec<Command>;
fn node_count(&self) -> usize;
fn settle(&self) {}
}
pub trait Backend {
fn init(&mut self, events: EventSink);
fn metrics(&self) -> PlatformMetrics;
fn apply(&mut self, batch: &[Command]);
fn measure(&mut self, id: NodeId, request: MeasureRequest) -> Size;
fn perform(&mut self, id: NodeId, action: &A11yAction) -> Result<(), ActionError>;
fn synthesize(&mut self, id: NodeId, input: &SyntheticInput) -> Result<(), ActionError>;
fn native_state(&self, id: NodeId) -> Option<NativeState>;
fn capture(&mut self, id: NodeId, reply: crate::services::Reply<Result<Image, CaptureError>>);
fn services(&self) -> Box<dyn crate::services::Services>;
}