use geometry_core::Rect;
use renderer_core::DrawCommand;
use ui_core::{Component, ComponentList};
pub fn mount<C: Component + 'static>(root: C, width: u32, height: u32) -> ComponentList {
let mut tree = ComponentList::new(root);
tree.on_event(&platform_core::Event::WindowResized { width, height });
tree
}
pub fn texts(tree: &ComponentList) -> Vec<String> {
tree.commands()
.iter()
.filter_map(|command| match command {
DrawCommand::Text { text, .. } => Some(text.to_string()),
DrawCommand::RichText { runs, .. } => {
Some(runs.iter().map(|run| run.text.to_string()).collect())
}
_ => None,
})
.collect()
}
pub fn find_text(tree: &ComponentList, needle: &str) -> bool {
texts(tree).iter().any(|text| text.contains(needle))
}
pub fn rect_of(tree: &ComponentList, needle: &str) -> Option<Rect> {
tree.commands().iter().find_map(|command| match command {
DrawCommand::Text { rect, text, .. } if text.contains(needle) => Some(*rect),
DrawCommand::RichText { rect, runs, .. }
if runs.iter().any(|run| run.text.contains(needle)) =>
{
Some(*rect)
}
_ => None,
})
}
pub fn painted_rect(command: &DrawCommand) -> Option<Rect> {
match command {
DrawCommand::Rect { rect, .. } | DrawCommand::Image { rect, .. } => Some(*rect),
DrawCommand::Text { rect, text, .. } => (!text.is_empty()).then_some(*rect),
DrawCommand::RichText { rect, runs, .. } => {
runs.iter().any(|run| !run.text.is_empty()).then_some(*rect)
}
DrawCommand::PushClip { rect, .. } => Some(*rect),
_ => None,
}
}
pub fn paints(command: &DrawCommand) -> bool {
match command {
DrawCommand::Path { data, .. } => data.bounds().is_some(),
DrawCommand::Line { .. } => true,
other => painted_rect(other).is_some(),
}
}
fn gpu_required() -> bool {
std::env::var("TELAR_REQUIRE_GPU").is_ok_and(|v| !v.is_empty())
}
pub fn require_gpu(what: &str, error: impl std::fmt::Debug) {
assert!(
!gpu_required(),
"{what}: TELAR_REQUIRE_GPU is set, so an adapter was expected: {error:?}"
);
eprintln!("skipping {what}: no GPU adapter available: {error:?}");
}