1pub mod component;
22pub mod event;
23pub mod image;
24pub mod layout;
25mod render;
26pub mod rich;
27pub mod scroll;
28#[cfg(test)]
29mod state_tests;
30pub mod surface;
31pub mod theme;
32pub mod text;
33pub mod textbox;
34pub mod widget;
35
36pub use event::UiEvent;
37pub use layout::{Align, FlexDir, GridLines, LayoutNode, Rect, hit_path, node_at, set_focus};
38pub use scroll::ScrollState;
39pub use surface::{Pointer, Surface};
40pub use textbox::{TextBoxEvent, TextBoxState};
41pub use widget::{
42 Action, Cell, Dock, FocusStyle, GridLength, GridSpec, HAlign, StateFlags, StateStyles, Stretch,
43 StylePatch, Track, Units, VAlign, Widget,
44};
45
46use yog_gfx::GfxContext;
47
48pub struct UiRoot {
50 pub id: String,
51 pub root: Widget,
52 pub layout_root: LayoutNode,
53 pub needs_layout: bool,
54}
55
56impl UiRoot {
57 pub fn new(id: impl Into<String>, root: Widget) -> Self {
58 Self {
59 id: id.into(),
60 root,
61 layout_root: LayoutNode::default(),
62 needs_layout: true,
63 }
64 }
65
66 pub fn layout(&mut self, screen_w: f32, screen_h: f32) {
68 self.layout_root = layout::compute(&self.root, screen_w, screen_h);
69 self.needs_layout = false;
70 }
71
72 pub fn render(&self, ctx: &GfxContext) {
75 text::use_host_metrics(ctx);
76 image::use_host_images(ctx);
77 let d2d = ctx.draw2d();
78 render::render_node(ctx, &d2d, &self.root, &self.layout_root);
79 }
80
81 pub fn tooltip_at(&self, mx: f32, my: f32) -> Option<&str> {
84 let path = layout::deepest_path(&self.layout_root, mx, my)?;
85 let mut w = &self.root;
86 let mut found = w.tooltip.as_deref();
87 for &i in &path {
88 let Some(c) = w.children.get(i as usize) else { break };
89 w = c;
90 found = w.tooltip.as_deref().or(found);
91 }
92 found
93 }
94
95 pub fn render_tooltip(ctx: &GfxContext, text: &str, mx: f32, my: f32) {
98 text::use_host_metrics(ctx);
99 let (sw, sh) = ctx.screen_size();
100 render::tooltip(&ctx.draw2d(), text, mx, my, sw as f32, sh as f32);
101 }
102
103 pub fn hit_test(&self, mx: f32, my: f32) -> Option<&LayoutNode> {
105 layout::hit_test(&self.layout_root, mx, my)
106 }
107
108 pub fn find_rect(&self, id: &str) -> Option<Rect> {
111 layout::find_by_id(&self.layout_root, id)
112 }
113
114 pub fn find_node(&self, id: &str) -> Option<&LayoutNode> {
117 layout::find_node(&self.layout_root, id)
118 }
119}