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 yog_gfx::{Color, Vec2};
39pub use scroll::ScrollState;
40pub use surface::{Pointer, Surface};
41pub use textbox::{TextBoxEvent, TextBoxState};
42pub use widget::{
43 Action, Cell, Dock, FocusStyle, GridLength, GridSpec, HAlign, StateFlags, StateStyles, Stretch,
44 StylePatch, Track, Units, VAlign, Widget,
45};
46
47use yog_gfx::GfxContext;
48
49pub struct UiRoot {
51 pub id: String,
52 pub root: Widget,
53 pub layout_root: LayoutNode,
54 pub needs_layout: bool,
55}
56
57impl UiRoot {
58 pub fn new(id: impl Into<String>, root: Widget) -> Self {
59 Self {
60 id: id.into(),
61 root,
62 layout_root: LayoutNode::default(),
63 needs_layout: true,
64 }
65 }
66
67 pub fn layout(&mut self, screen_w: f32, screen_h: f32) {
69 self.layout_root = layout::compute(&self.root, screen_w, screen_h);
70 self.needs_layout = false;
71 }
72
73 pub fn render(&self, ctx: &GfxContext) {
76 text::use_host_metrics(ctx);
77 image::use_host_images(ctx);
78 let d2d = ctx.draw2d();
79 render::render_node(ctx, &d2d, &self.root, &self.layout_root);
80 }
81
82 pub fn tooltip_at(&self, mx: f32, my: f32) -> Option<&str> {
85 let path = layout::deepest_path(&self.layout_root, mx, my)?;
86 let mut w = &self.root;
87 let mut found = w.tooltip.as_deref();
88 for &i in &path {
89 let Some(c) = w.children.get(i as usize) else { break };
90 w = c;
91 found = w.tooltip.as_deref().or(found);
92 }
93 found
94 }
95
96 pub fn render_tooltip(ctx: &GfxContext, text: &str, mx: f32, my: f32) {
99 text::use_host_metrics(ctx);
100 let (sw, sh) = ctx.screen_size();
101 render::tooltip(&ctx.draw2d(), text, mx, my, sw as f32, sh as f32);
102 }
103
104 pub fn hit_test(&self, mx: f32, my: f32) -> Option<&LayoutNode> {
106 layout::hit_test(&self.layout_root, mx, my)
107 }
108
109 pub fn find_rect(&self, id: &str) -> Option<Rect> {
112 layout::find_by_id(&self.layout_root, id)
113 }
114
115 pub fn find_node(&self, id: &str) -> Option<&LayoutNode> {
118 layout::find_node(&self.layout_root, id)
119 }
120}