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};
#[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,
pub tab_insets: Insets,
pub group_insets: Insets,
pub titled_group_insets: Insets,
}
#[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,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum SyntheticInput {
Key(Key),
Shortcut(Shortcut),
Text(String),
Compose(String),
Scroll { dx: f32, dy: f32 },
Click(Point),
PointerEnter,
PointerLeave,
DoubleClick,
DragFiles(Vec<std::path::PathBuf>),
DragLeave,
DropFiles(Vec<std::path::PathBuf>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Key {
Char(char),
Enter,
Escape,
Tab,
Backspace,
Delete,
Up,
Down,
Left,
Right,
Home,
End,
PageUp,
PageDown,
F(u8),
}
impl From<char> for Key {
fn from(c: char) -> Key {
Key::Char(c)
}
}
#[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>,
pub viewport_insets: Option<Insets>,
pub selection: Option<std::ops::Range<usize>>,
}
#[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 close_window(&self, window: NodeId);
fn take_command_log(&self) -> Vec<Command>;
fn node_count(&self) -> usize;
fn app_info(&self, window: NodeId) -> NativeAppInfo;
fn dragged_files(&self, node: NodeId) -> Option<Vec<std::path::PathBuf>>;
fn settle(&self) {}
}
pub trait Backend {
fn init(&mut self, events: EventSink);
fn metrics(&self) -> PlatformMetrics;
fn group_insets(&self, _id: NodeId) -> Option<Insets> {
None
}
fn tab_insets(&self, _id: NodeId) -> Option<Insets> {
None
}
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>;
fn set_app_info(&mut self, info: &AppInfo);
fn locale(&self) -> std::rc::Rc<dyn crate::l10n::PlatformLocale>;
fn set_locale(&mut self, language: &crate::l10n::LanguageIdentifier, right_to_left: bool);
}