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