pub mod component;
pub mod event;
pub mod image;
pub mod layout;
mod render;
pub mod scroll;
#[cfg(test)]
mod state_tests;
pub mod surface;
pub mod theme;
pub mod text;
pub mod textbox;
pub mod widget;
pub use event::UiEvent;
pub use layout::{Align, FlexDir, GridLines, LayoutNode, Rect, hit_path, node_at, set_focus};
pub use scroll::ScrollState;
pub use surface::{Pointer, Surface};
pub use textbox::{TextBoxEvent, TextBoxState};
pub use widget::{
Action, Cell, Dock, FocusStyle, GridLength, GridSpec, HAlign, StateFlags, StateStyles, Stretch,
StylePatch, Track, Units, VAlign, Widget,
};
use yog_gfx::GfxContext;
pub struct UiRoot {
pub id: String,
pub root: Widget,
pub layout_root: LayoutNode,
pub needs_layout: bool,
}
impl UiRoot {
pub fn new(id: impl Into<String>, root: Widget) -> Self {
Self {
id: id.into(),
root,
layout_root: LayoutNode::default(),
needs_layout: true,
}
}
pub fn layout(&mut self, screen_w: f32, screen_h: f32) {
self.layout_root = layout::compute(&self.root, screen_w, screen_h);
self.needs_layout = false;
}
pub fn render(&self, ctx: &GfxContext) {
text::use_host_metrics(ctx);
image::use_host_images(ctx);
let d2d = ctx.draw2d();
render::render_node(ctx, &d2d, &self.root, &self.layout_root);
}
pub fn tooltip_at(&self, mx: f32, my: f32) -> Option<&str> {
let path = layout::deepest_path(&self.layout_root, mx, my)?;
let mut w = &self.root;
let mut found = w.tooltip.as_deref();
for &i in &path {
let Some(c) = w.children.get(i as usize) else { break };
w = c;
found = w.tooltip.as_deref().or(found);
}
found
}
pub fn render_tooltip(ctx: &GfxContext, text: &str, mx: f32, my: f32) {
text::use_host_metrics(ctx);
let (sw, sh) = ctx.screen_size();
render::tooltip(&ctx.draw2d(), text, mx, my, sw as f32, sh as f32);
}
pub fn hit_test(&self, mx: f32, my: f32) -> Option<&LayoutNode> {
layout::hit_test(&self.layout_root, mx, my)
}
pub fn find_rect(&self, id: &str) -> Option<Rect> {
layout::find_by_id(&self.layout_root, id)
}
pub fn find_node(&self, id: &str) -> Option<&LayoutNode> {
layout::find_node(&self.layout_root, id)
}
}