use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::rc::{Rc, Weak};
use taffy::TaffyTree;
use crate::a11y::{A11yAction, A11yNode, A11yProps, ActionError, Role};
use crate::backend::{AvailableSpace, Backend, EventSink, MeasureRequest, NativeState, PlatformMetrics};
use crate::command::{Command, EventValue, UiEvent};
use crate::custom::CustomProps;
use crate::geometry::{Point, Rect, Size, WindowSize};
use crate::services::{Alert, MenuBar, OpenFile, SaveFile, ServiceError, Services, reply_future};
use crate::style::{Align, Display, FlexDirection, Style, TextDirection};
use crate::task::{Clock, Executor, Sleep, TaskHandle};
use crate::units::ResolveContext;
use crate::widget::{NodeId, Prop, WidgetKind};
pub(crate) type Handler = Rc<dyn Fn(&UiEvent)>;
type Handler0 = Rc<dyn Fn()>;
struct Node {
kind: WidgetKind,
parent: Option<NodeId>,
children: Vec<NodeId>,
native_children: Vec<NodeId>,
native_parent: Option<NodeId>,
props: Vec<Prop>,
style: Style,
a11y: A11yProps,
test_id: Option<String>,
tab_index: Option<u32>,
handlers: Vec<Handler>,
taffy: Option<taffy::NodeId>,
frame: Rect,
window_size: Size,
fit_height: bool,
scroll_offset: Point,
}
impl Node {
fn prop(&self, key: &Prop) -> Option<&Prop> {
self.props.iter().find(|p| p.key() == key.key())
}
fn custom(&self) -> Option<&CustomProps> {
self.props.iter().find_map(|p| match p {
Prop::Custom(custom) => Some(custom),
_ => None,
})
}
fn drawn(&self) -> Option<&CustomProps> {
self.custom().filter(|c| c.is_drawn())
}
}
struct Inner {
backend: Box<dyn Backend>,
events: EventSink,
metrics: PlatformMetrics,
nodes: BTreeMap<NodeId, Node>,
taffy: TaffyTree<NodeId>,
next_id: u32,
pending: Vec<Command>,
pending_focus: Vec<NodeId>,
windows: Vec<NodeId>,
styles_dirty: bool,
resync: BTreeSet<NodeId>,
menu_handlers: std::collections::HashMap<u32, Handler0>,
menu_queue: std::collections::VecDeque<u32>,
focus_orders: BTreeMap<NodeId, Vec<NodeId>>,
focused: BTreeMap<NodeId, NodeId>,
commit_scheduler: Option<Rc<dyn Fn()>>,
commit_scheduled: bool,
}
#[derive(Clone)]
pub struct Ui {
inner: Rc<RefCell<Inner>>,
executor: Rc<Executor>,
services: Rc<RefCell<Box<dyn Services>>>,
}
#[derive(Clone)]
pub struct WeakUi {
inner: Weak<RefCell<Inner>>,
executor: Weak<Executor>,
services: Weak<RefCell<Box<dyn Services>>>,
}
impl WeakUi {
pub fn upgrade(&self) -> Option<Ui> {
Some(Ui {
inner: self.inner.upgrade()?,
executor: self.executor.upgrade()?,
services: self.services.upgrade()?,
})
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct NodeInfo {
pub id: NodeId,
pub kind: WidgetKind,
pub props: Vec<Prop>,
pub test_id: Option<String>,
pub frame: Rect,
pub children: Vec<NodeInfo>,
}
impl Ui {
pub fn new(backend: impl Backend + 'static) -> Ui {
let mut backend: Box<dyn Backend> = Box::new(backend);
let events = EventSink::default();
backend.init(events.clone());
let metrics = backend.metrics();
let services = Rc::new(RefCell::new(backend.services()));
Ui {
executor: Rc::default(),
services,
inner: Rc::new(RefCell::new(Inner {
backend,
events,
metrics,
nodes: BTreeMap::new(),
taffy: TaffyTree::new(),
next_id: 1,
pending: Vec::new(),
pending_focus: Vec::new(),
windows: Vec::new(),
styles_dirty: true,
resync: BTreeSet::new(),
focus_orders: BTreeMap::new(),
focused: BTreeMap::new(),
menu_handlers: Default::default(),
menu_queue: Default::default(),
commit_scheduler: None,
commit_scheduled: false,
})),
}
}
pub fn downgrade(&self) -> WeakUi {
WeakUi {
inner: Rc::downgrade(&self.inner),
executor: Rc::downgrade(&self.executor),
services: Rc::downgrade(&self.services),
}
}
pub(crate) fn executor(&self) -> &Executor {
&self.executor
}
pub fn spawn_local(&self, future: impl std::future::Future<Output = ()> + 'static) -> TaskHandle {
self.spawn_in(future, None)
}
pub(crate) fn spawn_in(
&self,
future: impl std::future::Future<Output = ()> + 'static,
owner: Option<mitsuami_reactive::Owner>,
) -> TaskHandle {
let id = self.executor.spawn(Box::pin(future), owner);
self.changed();
TaskHandle::new(id, self.downgrade())
}
pub fn sleep(&self, duration: std::time::Duration) -> Sleep {
Sleep::new(self.clone(), duration)
}
pub fn set_clock(&self, clock: Rc<dyn Clock>) {
self.executor.set_clock(clock);
}
pub fn set_waker(&self, wake: std::sync::Arc<dyn Fn() + Send + Sync>) {
self.executor.set_wake_ui(wake);
}
pub fn time_to_next_timer(&self) -> Option<std::time::Duration> {
let now = self.executor.now();
self.executor.next_deadline().map(|deadline| deadline.saturating_sub(now))
}
pub fn set_services(&self, services: Box<dyn Services>) {
*self.services.borrow_mut() = services;
}
pub fn clipboard_text(&self) -> impl std::future::Future<Output = Option<String>> + use<> {
let (reply, text) = reply_future();
self.services.borrow_mut().clipboard_text(reply);
text
}
pub fn set_clipboard_text(
&self,
text: &str,
) -> impl std::future::Future<Output = Result<(), ServiceError>> + use<> {
let (reply, done) = reply_future();
self.services.borrow_mut().set_clipboard_text(text, reply);
done
}
pub fn alert(&self, parent: Option<NodeId>, alert: Alert) -> impl std::future::Future<Output = usize> + use<> {
let (reply, answer) = reply_future();
self.services.borrow_mut().alert(parent, &alert, reply);
answer
}
pub fn open_file(
&self,
parent: Option<NodeId>,
request: OpenFile,
) -> impl std::future::Future<Output = Option<Vec<std::path::PathBuf>>> + use<> {
let (reply, answer) = reply_future();
self.services.borrow_mut().open_file(parent, &request, reply);
answer
}
pub fn save_file(
&self,
parent: Option<NodeId>,
request: SaveFile,
) -> impl std::future::Future<Output = Option<std::path::PathBuf>> + use<> {
let (reply, answer) = reply_future();
self.services.borrow_mut().save_file(parent, &request, reply);
answer
}
pub fn set_menu(&self, menu: MenuBar) {
let (data, handlers) = menu.into_parts();
self.inner.borrow_mut().menu_handlers = handlers
.into_iter()
.map(|(id, handler)| {
let handler = crate::task::in_current_scope(move |()| handler());
(id, Rc::new(move || handler(())) as Handler0)
})
.collect();
let weak = self.downgrade();
let activate: Rc<dyn Fn(u32)> = Rc::new(move |id| {
if let Some(ui) = weak.upgrade() {
ui.inner.borrow_mut().menu_queue.push_back(id);
ui.changed();
}
});
let services = self.services.clone();
mitsuami_reactive::effect(move || {
let data = data();
services.borrow_mut().set_menu(&data, activate.clone());
});
}
pub fn pending_tasks(&self) -> usize {
self.executor.task_count()
}
pub fn events(&self) -> EventSink {
self.inner.borrow().events.clone()
}
pub fn metrics(&self) -> PlatformMetrics {
self.inner.borrow().metrics.clone()
}
pub fn set_commit_scheduler(&self, schedule: impl Fn() + 'static) {
self.inner.borrow_mut().commit_scheduler = Some(Rc::new(schedule));
}
fn changed(&self) {
let schedule = {
let mut inner = self.inner.borrow_mut();
if inner.commit_scheduled {
return;
}
inner.commit_scheduled = true;
inner.commit_scheduler.clone()
};
if let Some(schedule) = schedule {
schedule();
}
}
pub fn create(&self, kind: WidgetKind, props: Vec<Prop>) -> NodeId {
let id = {
let mut inner = self.inner.borrow_mut();
let id = NodeId(inner.next_id);
inner.next_id += 1;
let taffy = if kind.is_native() {
let t = if kind.is_container() {
inner.taffy.new_leaf(taffy::Style::default())
} else {
inner.taffy.new_leaf_with_context(taffy::Style::default(), id)
};
Some(t.expect("taffy: failed to create node"))
} else {
None
};
if kind.is_native() {
inner.pending.push(Command::Create { id, kind, props: props.clone() });
}
inner.nodes.insert(
id,
Node {
kind,
parent: None,
children: Vec::new(),
native_children: Vec::new(),
native_parent: None,
props,
style: Style::default(),
a11y: A11yProps::default(),
test_id: None,
tab_index: None,
handlers: Vec::new(),
taffy,
frame: Rect::ZERO,
window_size: Size::ZERO,
fit_height: false,
scroll_offset: Point::ZERO,
},
);
inner.styles_dirty = true;
id
};
self.changed();
id
}
pub fn create_window(&self, title: impl Into<String>, size: impl Into<WindowSize>) -> NodeId {
let id = self.create(WidgetKind::Window, vec![Prop::Title(title.into())]);
let mut inner = self.inner.borrow_mut();
let node = inner.nodes.get_mut(&id).expect("just created");
let (size, fit_height) = match size.into() {
WindowSize::Fixed(size) => (size, false),
WindowSize::FitHeight(width) => (Size::new(width, 0.0), true),
};
node.window_size = size;
node.fit_height = fit_height;
node.frame = Rect { origin: Point::ZERO, size };
inner.windows.push(id);
if !fit_height {
inner.pending.push(Command::SetWindowSize { id, size });
}
id
}
pub fn set_prop(&self, id: NodeId, prop: Prop) {
{
let mut inner = self.inner.borrow_mut();
let inner = &mut *inner;
let Some(node) = inner.nodes.get_mut(&id) else { return };
if node.prop(&prop) == Some(&prop) {
return;
}
node.props.retain(|p| p.key() != prop.key());
node.props.push(prop.clone());
if prop.affects_measure()
&& let Some(t) = node.taffy
{
let _ = inner.taffy.mark_dirty(t);
}
if matches!(prop, Prop::TextStyle(_)) {
inner.styles_dirty = true;
}
if node.kind.is_native() {
inner.queue_prop(id, prop);
}
}
self.changed();
}
pub fn set_style(&self, id: NodeId, style: Style) {
{
let mut inner = self.inner.borrow_mut();
let Some(node) = inner.nodes.get_mut(&id) else { return };
if node.style == style {
return;
}
node.style = style;
inner.styles_dirty = true;
}
self.changed();
}
pub fn update_style(&self, id: NodeId, f: impl FnOnce(&mut Style)) {
let style = {
let inner = self.inner.borrow();
let Some(node) = inner.nodes.get(&id) else { return };
let mut style = node.style.clone();
f(&mut style);
style
};
self.set_style(id, style);
}
pub fn set_a11y(&self, id: NodeId, a11y: A11yProps) {
{
let mut inner = self.inner.borrow_mut();
let Some(node) = inner.nodes.get_mut(&id) else { return };
if node.a11y == a11y {
return;
}
node.a11y = a11y.clone();
if node.kind.is_native() {
inner.pending.push(Command::SetA11y { id, a11y });
}
}
self.changed();
}
pub fn set_tab_index(&self, id: NodeId, index: Option<u32>) {
if let Some(node) = self.inner.borrow_mut().nodes.get_mut(&id) {
node.tab_index = index;
}
self.changed();
}
pub fn focused(&self, window: NodeId) -> Option<NodeId> {
self.inner.borrow().focused.get(&window).copied()
}
pub fn focus_order(&self, window: NodeId) -> Vec<NodeId> {
self.inner.borrow().focus_order(window)
}
pub fn set_test_id(&self, id: NodeId, test_id: impl Into<String>) {
if let Some(node) = self.inner.borrow_mut().nodes.get_mut(&id) {
node.test_id = Some(test_id.into());
}
}
pub fn on_event(&self, id: NodeId, handler: impl Fn(&UiEvent) + 'static) {
let owner = mitsuami_reactive::Owner::current();
let handler = move |event: &UiEvent| match owner {
Some(owner) if owner.is_alive() => owner.with(|| handler(event)),
_ => handler(event),
};
if let Some(node) = self.inner.borrow_mut().nodes.get_mut(&id) {
node.handlers.push(Rc::new(handler));
}
}
pub fn focus(&self, id: NodeId) {
self.inner.borrow_mut().pending_focus.push(id);
self.changed();
}
pub fn insert_child(&self, parent: NodeId, child: NodeId, index: Option<usize>) {
{
let mut inner = self.inner.borrow_mut();
if !inner.nodes.contains_key(&parent) || !inner.nodes.contains_key(&child) {
return;
}
inner.detach(child);
let siblings = &mut inner.nodes.get_mut(&parent).unwrap().children;
let index = index.unwrap_or(siblings.len()).min(siblings.len());
siblings.insert(index, child);
inner.nodes.get_mut(&child).unwrap().parent = Some(parent);
inner.mark_resync(parent);
inner.styles_dirty = true;
}
self.changed();
}
pub fn append_child(&self, parent: NodeId, child: NodeId) {
self.insert_child(parent, child, None);
}
pub fn set_children(&self, parent: NodeId, children: Vec<NodeId>) {
{
let mut inner = self.inner.borrow_mut();
if !inner.nodes.contains_key(&parent) {
return;
}
let old = inner.nodes[&parent].children.clone();
for child in old.iter().filter(|c| !children.contains(c)) {
inner.detach(*child);
}
for child in &children {
if inner.nodes.get(child).and_then(|n| n.parent) != Some(parent) {
inner.detach(*child);
}
if let Some(node) = inner.nodes.get_mut(child) {
node.parent = Some(parent);
}
}
inner.nodes.get_mut(&parent).unwrap().children = children;
inner.mark_resync(parent);
inner.styles_dirty = true;
}
self.changed();
}
pub fn destroy(&self, id: NodeId) {
{
let mut inner = self.inner.borrow_mut();
if !inner.nodes.contains_key(&id) {
return;
}
inner.detach(id);
let roots = inner.native_roots(id);
for root in &roots {
inner.detach_native(*root);
}
let mut subtree = Vec::new();
inner.collect_subtree(id, &mut subtree);
for node_id in subtree {
let node = inner.nodes.remove(&node_id).expect("in subtree");
if let Some(t) = node.taffy {
let _ = inner.taffy.remove(t);
}
inner.resync.remove(&node_id);
inner.windows.retain(|w| *w != node_id);
inner.focus_orders.remove(&node_id);
inner.focused.retain(|window, focused| *window != node_id && *focused != node_id);
if node.kind.is_native() {
inner.pending.push(Command::Destroy { id: node_id });
}
}
}
self.changed();
}
pub fn process_events(&self) {
crate::task::with_current(self, || self.dispatch_events());
}
fn dispatch_events(&self) {
loop {
let chosen = {
let mut inner = self.inner.borrow_mut();
let id = inner.menu_queue.pop_front();
id.and_then(|id| inner.menu_handlers.get(&id).cloned())
};
if let Some(handler) = chosen {
mitsuami_reactive::batch(|| handler());
continue;
}
let next = self.inner.borrow().events.pop();
let Some((id, event)) = next else { break };
let (handlers, meaning) = {
let mut inner = self.inner.borrow_mut();
inner.absorb(id, &event);
let node = inner.nodes.get(&id);
let meaning = match (&event, node.and_then(|n| n.drawn().map(|c| (c, n.frame.size)))) {
(UiEvent::Pointer(pointer), Some((custom, size))) => custom.pointer(size, pointer),
_ => None,
};
(node.map(|n| n.handlers.clone()).unwrap_or_default(), meaning)
};
self.changed();
for event in std::iter::once(event).chain(meaning.map(UiEvent::Custom)) {
mitsuami_reactive::batch(|| {
for handler in &handlers {
handler(&event);
}
});
}
}
}
pub fn commit(&self) {
let mut inner = self.inner.borrow_mut();
inner.commit_scheduled = false;
if inner.styles_dirty {
inner.resolve_styles();
}
inner.resync_all();
inner.sync_focus_orders();
for id in std::mem::take(&mut inner.pending_focus) {
if inner.nodes.contains_key(&id) {
inner.pending.push(Command::Focus { id });
}
}
let batch = std::mem::take(&mut inner.pending);
if !batch.is_empty() {
inner.backend.apply(&batch);
}
inner.layout();
let frames = std::mem::take(&mut inner.pending);
if !frames.is_empty() {
inner.backend.apply(&frames);
}
}
pub fn tick(&self) {
const MAX_TURNS: usize = 64;
for _ in 0..MAX_TURNS {
self.executor.run_ready(self);
self.process_events();
self.commit();
let idle = {
let inner = self.inner.borrow();
inner.events.is_empty() && inner.menu_queue.is_empty()
};
if idle && !self.executor.has_ready() {
return;
}
}
}
pub fn perform(&self, id: NodeId, action: &A11yAction) -> Result<(), ActionError> {
let result = {
let mut inner = self.inner.borrow_mut();
let result = inner.backend.perform(id, action);
match (result, inner.nodes.get(&id).and_then(|n| n.custom())) {
(Err(ActionError::Unsupported), Some(custom)) => match custom.action(action) {
Some(event) => {
inner.events.emit(id, UiEvent::Custom(event));
Ok(())
}
None => Err(ActionError::Unsupported),
},
(result, _) => result,
}
};
self.process_events();
result
}
pub fn synthesize(&self, id: NodeId, input: &crate::backend::SyntheticInput) -> Result<(), ActionError> {
let result = self.inner.borrow_mut().backend.synthesize(id, input);
self.process_events();
result
}
pub fn windows(&self) -> Vec<NodeId> {
self.inner.borrow().windows.clone()
}
pub fn exists(&self, id: NodeId) -> bool {
self.inner.borrow().nodes.contains_key(&id)
}
pub fn kind(&self, id: NodeId) -> Option<WidgetKind> {
self.inner.borrow().nodes.get(&id).map(|n| n.kind)
}
pub fn props(&self, id: NodeId) -> Vec<Prop> {
self.inner.borrow().nodes.get(&id).map(|n| n.props.clone()).unwrap_or_default()
}
pub fn parent(&self, id: NodeId) -> Option<NodeId> {
self.inner.borrow().nodes.get(&id).and_then(|n| n.parent)
}
pub fn children(&self, id: NodeId) -> Vec<NodeId> {
self.inner.borrow().nodes.get(&id).map(|n| n.children.clone()).unwrap_or_default()
}
pub fn native_children(&self, id: NodeId) -> Vec<NodeId> {
self.inner.borrow().nodes.get(&id).map(|n| n.native_children.clone()).unwrap_or_default()
}
pub fn frame(&self, id: NodeId) -> Option<Rect> {
self.inner.borrow().nodes.get(&id).map(|n| n.frame)
}
pub fn window_frame(&self, id: NodeId) -> Option<Rect> {
let inner = self.inner.borrow();
let node = inner.nodes.get(&id)?;
if node.kind == WidgetKind::Window {
return Some(node.frame);
}
Some(node.frame.offset(inner.window_origin(id)?))
}
pub fn visible_rect(&self, id: NodeId) -> Option<Rect> {
let window = self.window_of(id)?;
let size = self.window_size(window)?;
let mut visible = self.window_frame(id)?.intersection(&Rect::new(0.0, 0.0, size.width, size.height))?;
let mut current = self.inner.borrow().nodes.get(&id)?.native_parent;
while let Some(ancestor) = current {
if self.kind(ancestor) == Some(WidgetKind::ScrollView) {
visible = visible.intersection(&self.window_frame(ancestor)?)?;
}
current = self.inner.borrow().nodes.get(&ancestor)?.native_parent;
}
Some(visible)
}
pub fn scroll_offset(&self, id: NodeId) -> Option<Point> {
let inner = self.inner.borrow();
let node = inner.nodes.get(&id)?;
(node.kind == WidgetKind::ScrollView).then_some(node.scroll_offset)
}
pub fn scroll_to(&self, id: NodeId, offset: Point) {
{
let mut inner = self.inner.borrow_mut();
let Some(offset) = inner.clamp_scroll(id, offset) else { return };
let node = inner.nodes.get_mut(&id).unwrap();
if node.scroll_offset == offset {
return;
}
node.scroll_offset = offset;
inner.pending.push(Command::ScrollTo { id, offset });
}
self.changed();
}
pub fn scroll_into_view(&self, id: NodeId) {
let mut target = id;
loop {
let scroll_view = {
let inner = self.inner.borrow();
let mut current = inner.nodes.get(&target).and_then(|n| n.native_parent);
while let Some(ancestor) = current {
if inner.nodes[&ancestor].kind == WidgetKind::ScrollView {
break;
}
current = inner.nodes[&ancestor].native_parent;
}
current
};
let Some(scroll_view) = scroll_view else { return };
let (Some(rect), Some(viewport)) = (self.window_frame(target), self.window_frame(scroll_view)) else {
return;
};
let offset = self.scroll_offset(scroll_view).unwrap_or_default();
let x = rect.x() - viewport.x() + offset.x;
let y = rect.y() - viewport.y() + offset.y;
let fit = |start: f32, len: f32, current: f32, visible: f32| {
if start < current {
start
} else if start + len > current + visible {
(start + len - visible).min(start)
} else {
current
}
};
let next = Point::new(
fit(x, rect.width(), offset.x, viewport.width()),
fit(y, rect.height(), offset.y, viewport.height()),
);
self.scroll_to(scroll_view, next);
target = scroll_view;
}
}
pub fn window_of(&self, id: NodeId) -> Option<NodeId> {
self.inner.borrow().window_of(id)
}
pub fn window_size(&self, window: NodeId) -> Option<Size> {
self.inner.borrow().nodes.get(&window).map(|n| n.window_size)
}
pub fn native_state(&self, id: NodeId) -> Option<NativeState> {
self.inner.borrow().backend.native_state(id)
}
pub fn capture(
&self,
id: NodeId,
) -> impl std::future::Future<Output = Result<crate::backend::Image, crate::backend::CaptureError>> + use<> {
let (reply, image) = reply_future();
self.inner.borrow_mut().backend.capture(id, reply);
image
}
pub fn inspect(&self, id: NodeId) -> Option<NodeInfo> {
let inner = self.inner.borrow();
let origin = inner.window_origin(id)?;
Some(inner.inspect(id, origin))
}
pub fn a11y_tree(&self, window: NodeId) -> Option<A11yNode> {
let inner = self.inner.borrow();
let mut nodes = inner.a11y(window, Point::ZERO);
(nodes.len() == 1).then(|| nodes.remove(0))
}
}
impl Inner {
fn queue_prop(&mut self, id: NodeId, prop: Prop) {
let create = self.pending.iter_mut().rev().find_map(|command| match command {
Command::Create { id: created, props, .. } if *created == id => Some(props),
_ => None,
});
match create {
Some(props) => {
props.retain(|p| p.key() != prop.key());
props.push(prop);
}
None => self.pending.push(Command::SetProp { id, prop }),
}
}
fn mark_resync(&mut self, id: NodeId) {
if let Some(native) = self.native_ancestor_or_self(id) {
self.resync.insert(native);
}
}
fn native_ancestor_or_self(&self, id: NodeId) -> Option<NodeId> {
let mut current = Some(id);
while let Some(node_id) = current {
let node = self.nodes.get(&node_id)?;
if node.kind.is_native() {
return Some(node_id);
}
current = node.parent;
}
None
}
fn detach(&mut self, child: NodeId) {
let Some(parent) = self.nodes.get(&child).and_then(|n| n.parent) else { return };
if let Some(node) = self.nodes.get_mut(&parent) {
node.children.retain(|c| *c != child);
}
self.nodes.get_mut(&child).unwrap().parent = None;
self.mark_resync(parent);
}
fn detach_native(&mut self, child: NodeId) {
let Some(parent) = self.nodes.get(&child).and_then(|n| n.native_parent) else { return };
self.pending.push(Command::Remove { parent, child });
let child_taffy = self.nodes[&child].taffy;
if let Some(node) = self.nodes.get_mut(&parent) {
node.native_children.retain(|c| *c != child);
if let (Some(p), Some(c)) = (node.taffy, child_taffy) {
let _ = self.taffy.remove_child(p, c);
}
}
self.nodes.get_mut(&child).unwrap().native_parent = None;
}
fn native_roots(&self, id: NodeId) -> Vec<NodeId> {
let node = &self.nodes[&id];
if node.kind.is_native() {
return vec![id];
}
node.children.iter().flat_map(|c| self.native_roots(*c)).collect()
}
fn flattened_children(&self, id: NodeId) -> Vec<NodeId> {
self.nodes[&id].children.iter().flat_map(|c| self.native_roots(*c)).collect()
}
fn collect_subtree(&self, id: NodeId, out: &mut Vec<NodeId>) {
for child in &self.nodes[&id].children {
self.collect_subtree(*child, out);
}
out.push(id);
}
fn resync_all(&mut self) {
let dirty = std::mem::take(&mut self.resync);
for id in dirty {
if self.nodes.contains_key(&id) {
self.resync_node(id);
}
}
}
fn resync_node(&mut self, parent: NodeId) {
let desired = self.flattened_children(parent);
let current = self.nodes[&parent].native_children.clone();
for child in current.iter().filter(|c| !desired.contains(c)) {
self.detach_native(*child);
}
let mut working: Vec<NodeId> = self.nodes[&parent].native_children.clone();
for (index, child) in desired.iter().enumerate() {
if working.get(index) == Some(child) {
continue;
}
if let Some(pos) = working.iter().position(|c| c == child) {
working.remove(pos);
self.pending.push(Command::Remove { parent, child: *child });
} else if let Some(other) = self.nodes[child].native_parent
&& other != parent
{
self.detach_native(*child);
}
working.insert(index, *child);
self.pending.push(Command::Insert { parent, child: *child, index });
}
for child in &desired {
self.nodes.get_mut(child).unwrap().native_parent = Some(parent);
}
let taffy_children: Vec<_> = desired.iter().filter_map(|c| self.nodes[c].taffy).collect();
if let Some(t) = self.nodes[&parent].taffy {
let _ = self.taffy.set_children(t, &taffy_children);
}
self.nodes.get_mut(&parent).unwrap().native_children = desired;
}
fn focus_order(&self, window: NodeId) -> Vec<NodeId> {
fn walk(inner: &Inner, id: NodeId, out: &mut Vec<(Option<u32>, NodeId)>) {
let node = &inner.nodes[&id];
if node.style.is_hidden() {
return;
}
if matches!(
node.kind,
WidgetKind::Button | WidgetKind::TextInput | WidgetKind::Checkbox | WidgetKind::Switch
) {
out.push((node.tab_index, id));
}
for child in &node.native_children {
walk(inner, *child, out);
}
}
let mut entries = Vec::new();
if self.nodes.contains_key(&window) {
walk(self, window, &mut entries);
}
entries.sort_by_key(|(index, _)| index.unwrap_or(u32::MAX));
entries.into_iter().map(|(_, id)| id).collect()
}
fn sync_focus_orders(&mut self) {
for window in self.windows.clone() {
let order = self.focus_order(window);
if self.focus_orders.get(&window) != Some(&order) {
self.focus_orders.insert(window, order.clone());
self.pending.push(Command::SetFocusOrder { window, order });
}
}
}
fn resolve_styles(&mut self) {
self.styles_dirty = false;
let body = self.metrics.font_sizes.body;
for window in self.windows.clone() {
let viewport = self.nodes[&window].window_size;
self.resolve_node(window, body, false, viewport, false);
}
}
fn resolve_node(
&mut self,
id: NodeId,
inherited_font: f32,
inherited_rtl: bool,
viewport: Size,
in_stretching_column: bool,
) {
let node = &self.nodes[&id];
let font_size = match crate::find_prop!(node.props, TextStyle) {
Some(style) => self.metrics.font_sizes.get(style),
None => inherited_font,
};
let rtl = match node.style.direction {
TextDirection::Inherit => inherited_rtl,
TextDirection::Ltr => false,
TextDirection::Rtl => true,
};
if let Some(t) = node.taffy {
let cx = ResolveContext {
font_size,
root_font_size: self.metrics.font_sizes.body,
viewport,
spacing: &self.metrics.spacing,
};
let mut style = node.style.to_taffy(&cx, rtl);
if matches!(node.kind, WidgetKind::Checkbox | WidgetKind::Switch) {
style.justify_self.get_or_insert(taffy::AlignSelf::START);
if in_stretching_column {
style.align_self.get_or_insert(taffy::AlignSelf::START);
}
}
if node.kind == WidgetKind::Window {
style.size = taffy::Size {
width: taffy::Dimension::length(node.window_size.width),
height: if node.fit_height {
taffy::Dimension::auto()
} else {
taffy::Dimension::length(node.window_size.height)
},
};
}
if self.taffy.style(t).ok() != Some(&style) {
let _ = self.taffy.set_style(t, style);
}
}
let in_stretching_column = match node.taffy {
Some(_) => {
let s = &node.style;
s.display == Display::Flex
&& matches!(s.flex_direction, FlexDirection::Column | FlexDirection::ColumnReverse)
&& matches!(s.align_items, None | Some(Align::Stretch))
}
None => in_stretching_column,
};
for child in self.nodes[&id].children.clone() {
self.resolve_node(child, font_size, rtl, viewport, in_stretching_column);
}
}
fn layout(&mut self) {
for window in self.windows.clone() {
let node = &self.nodes[&window];
let Some(root) = node.taffy else { continue };
let (mut size, fit_height) = (node.window_size, node.fit_height);
let Inner { taffy, backend, nodes, metrics, .. } = self;
let available = taffy::Size {
width: taffy::AvailableSpace::Definite(size.width),
height: if fit_height {
taffy::AvailableSpace::MaxContent
} else {
taffy::AvailableSpace::Definite(size.height)
},
};
let _ = taffy.compute_layout_with_measure(root, available, |input, _, context, style| {
taffy::compute_leaf_layout(
input,
style,
|_, _| 0.0,
|known, available| match context {
Some(id) => {
let request = MeasureRequest {
known_width: known.width,
known_height: known.height,
available_width: space(available.width),
available_height: space(available.height),
};
let size = match nodes.get(id).and_then(|n| n.drawn()) {
Some(custom) => {
let size = custom.measure_drawn(&request, metrics).unwrap_or(Size::ZERO);
Size::new(known.width.unwrap_or(size.width), known.height.unwrap_or(size.height))
}
None => backend.measure(*id, request),
};
taffy::Size { width: size.width, height: size.height }
}
None => taffy::Size::ZERO,
},
)
});
if fit_height {
size.height = self.taffy.layout(root).map_or(0.0, |l| l.size.height).ceil();
let node = self.nodes.get_mut(&window).unwrap();
node.window_size = size;
node.fit_height = false;
self.styles_dirty = true;
self.pending.push(Command::SetWindowSize { id: window, size });
}
self.nodes.get_mut(&window).unwrap().frame = Rect { origin: Point::ZERO, size };
self.collect_frames(window);
}
self.update_drawings();
}
fn update_drawings(&mut self) {
let mut changed = Vec::new();
for (id, node) in &self.nodes {
let Some(drawing) = node.drawn().and_then(|c| c.draw(node.frame.size, &self.metrics)) else { continue };
let drawing = Prop::Drawing(drawing);
if node.prop(&drawing) != Some(&drawing) {
changed.push((*id, drawing));
}
}
for (id, prop) in changed {
let node = self.nodes.get_mut(&id).unwrap();
node.props.retain(|p| p.key() != prop.key());
node.props.push(prop.clone());
self.pending.push(Command::SetProp { id, prop });
}
}
fn collect_frames(&mut self, parent: NodeId) {
for child in self.nodes[&parent].native_children.clone() {
let node = &self.nodes[&child];
if let Some(t) = node.taffy
&& let Ok(layout) = self.taffy.layout(t)
{
let frame = Rect::new(layout.location.x, layout.location.y, layout.size.width, layout.size.height);
if frame != node.frame {
self.nodes.get_mut(&child).unwrap().frame = frame;
self.pending.push(Command::SetFrame { id: child, frame });
}
}
self.collect_frames(child);
}
}
fn absorb(&mut self, id: NodeId, event: &UiEvent) {
match event {
UiEvent::Changed(value) => {
let Some(node) = self.nodes.get_mut(&id) else { return };
let prop = match (node.kind, value) {
(WidgetKind::TextInput, EventValue::Text(text)) => Prop::Value(text.clone()),
(WidgetKind::Checkbox | WidgetKind::Switch, EventValue::Bool(b)) => Prop::Checked(*b),
_ => return,
};
node.props.retain(|p| p.key() != prop.key());
node.props.push(prop);
}
UiEvent::FocusIn => {
if let Some(window) = self.window_of(id) {
self.focused.insert(window, id);
}
}
UiEvent::FocusOut => self.focused.retain(|_, focused| *focused != id),
UiEvent::Scrolled(offset) => {
if let Some(node) = self.nodes.get_mut(&id) {
node.scroll_offset = *offset;
}
}
UiEvent::WindowResized(size) => {
if let Some(node) = self.nodes.get_mut(&id) {
node.window_size = *size;
self.styles_dirty = true;
}
}
UiEvent::MetricsChanged => {
self.metrics = self.backend.metrics();
self.styles_dirty = true;
for node in self.nodes.values() {
if let Some(t) = node.taffy {
let _ = self.taffy.mark_dirty(t);
}
}
}
_ => {}
}
}
fn window_of(&self, id: NodeId) -> Option<NodeId> {
let mut current = Some(id);
while let Some(node_id) = current {
let node = self.nodes.get(&node_id)?;
if node.kind == WidgetKind::Window {
return Some(node_id);
}
current = node.parent;
}
None
}
fn content_origin(&self, id: NodeId) -> Point {
let node = &self.nodes[&id];
if node.kind == WidgetKind::Window {
return Point::ZERO;
}
let parent = node.native_parent.map_or(Point::ZERO, |p| self.content_origin(p));
Point::new(
parent.x + node.frame.origin.x - node.scroll_offset.x,
parent.y + node.frame.origin.y - node.scroll_offset.y,
)
}
fn window_origin(&self, id: NodeId) -> Option<Point> {
Some(self.nodes.get(&id)?.native_parent.map_or(Point::ZERO, |p| self.content_origin(p)))
}
fn clamp_scroll(&self, id: NodeId, offset: Point) -> Option<Point> {
let node = self.nodes.get(&id)?;
if node.kind != WidgetKind::ScrollView {
return None;
}
let axes = crate::find_prop!(node.props, ScrollAxes).unwrap_or_default();
let content = node.native_children.first().map_or(Size::ZERO, |c| self.nodes[c].frame.size);
let viewport = node.frame.size;
let clamp = |v: f32, content: f32, viewport: f32, enabled: bool| {
if enabled { v.clamp(0.0, (content - viewport).max(0.0)) } else { 0.0 }
};
Some(Point::new(
clamp(offset.x, content.width, viewport.width, axes.horizontal()),
clamp(offset.y, content.height, viewport.height, axes.vertical()),
))
}
fn child_origin(node: &Node, frame: Rect) -> Point {
match node.kind {
WidgetKind::Window => Point::ZERO,
_ => Point::new(frame.origin.x - node.scroll_offset.x, frame.origin.y - node.scroll_offset.y),
}
}
fn inspect(&self, id: NodeId, parent_origin: Point) -> NodeInfo {
let node = &self.nodes[&id];
let frame = if node.kind == WidgetKind::Window { node.frame } else { node.frame.offset(parent_origin) };
let origin = Inner::child_origin(node, frame);
NodeInfo {
id,
kind: node.kind,
props: node.props.clone(),
test_id: node.test_id.clone(),
frame,
children: node.native_children.iter().map(|c| self.inspect(*c, origin)).collect(),
}
}
fn text_of(&self, id: NodeId) -> Option<String> {
let node = self.nodes.get(&id)?;
crate::find_prop!(node.props, Text).or_else(|| crate::find_prop!(node.props, Label))
}
fn a11y(&self, id: NodeId, parent_origin: Point) -> Vec<A11yNode> {
let node = &self.nodes[&id];
let a11y = match node.custom() {
Some(custom) => custom.a11y().overridden_by(&node.a11y),
None => node.a11y.clone(),
};
if a11y.hidden || node.style.is_hidden() {
return Vec::new();
}
let frame = if node.kind == WidgetKind::Window { node.frame } else { node.frame.offset(parent_origin) };
let origin = Inner::child_origin(node, frame);
let children: Vec<A11yNode> = node.native_children.iter().flat_map(|c| self.a11y(*c, origin)).collect();
let labelled = a11y.label.is_some() || a11y.labelled_by.is_some();
let role = a11y.role.unwrap_or(match node.kind {
WidgetKind::Window => Role::Window,
WidgetKind::Container if labelled => Role::Group,
WidgetKind::Container | WidgetKind::Fragment => Role::None,
WidgetKind::ScrollView => Role::ScrollArea,
WidgetKind::Text => Role::StaticText,
WidgetKind::Button => Role::Button,
WidgetKind::TextInput => Role::TextField,
WidgetKind::Checkbox => Role::Checkbox,
WidgetKind::Switch => Role::Switch,
WidgetKind::Custom(_) | WidgetKind::Native => Role::Group,
});
if role == Role::None {
return children;
}
let props = &node.props;
let name =
a11y.label.clone().or_else(|| a11y.labelled_by.and_then(|l| self.text_of(l))).or_else(|| match node.kind {
WidgetKind::Window => crate::find_prop!(props, Title),
WidgetKind::Text => crate::find_prop!(props, Text),
WidgetKind::Button | WidgetKind::Checkbox | WidgetKind::Switch => crate::find_prop!(props, Label),
WidgetKind::TextInput => crate::find_prop!(props, Placeholder),
_ => None,
});
vec![A11yNode {
id,
role,
name,
description: a11y.description,
value: match node.kind {
WidgetKind::TextInput => Some(crate::find_prop!(props, Value).unwrap_or_default()),
_ => a11y.value,
},
checked: match node.kind {
WidgetKind::Checkbox | WidgetKind::Switch => Some(crate::find_prop!(props, Checked).unwrap_or(false)),
_ => None,
},
enabled: crate::find_prop!(props, Enabled).unwrap_or(true),
test_id: node.test_id.clone(),
frame,
children,
}]
}
}
fn space(s: taffy::AvailableSpace) -> AvailableSpace {
match s {
taffy::AvailableSpace::Definite(v) => AvailableSpace::Definite(v),
taffy::AvailableSpace::MinContent => AvailableSpace::MinContent,
taffy::AvailableSpace::MaxContent => AvailableSpace::MaxContent,
}
}