#![allow(dead_code)]
use telar::{
App, Color, Component, LayoutStyle, RectStyle, Rectangle, SizeDimension, WindowRoot,
reset_layout_runtime,
};
fn fill_root(color: Color) -> Box<dyn Component> {
let rect = Rectangle::new(
LayoutStyle::new()
.width(SizeDimension::Percent(1.0))
.height(SizeDimension::Percent(1.0)),
move || RectStyle::filled(color, 0.0),
)
.unwrap();
Box::new(WindowRoot::new(Box::new(rect)))
}
pub struct FillApp {
pub color: Color,
}
impl App for FillApp {
fn root(&self) -> Box<dyn Component> {
reset_layout_runtime();
fill_root(self.color)
}
fn clear_color(&self) -> Option<Color> {
Some(Color::rgba(0.0, 0.0, 0.0, 1.0))
}
}
pub struct MaybePanicApp {
pub color: Color,
pub panic_on_build: bool,
}
impl App for MaybePanicApp {
fn root(&self) -> Box<dyn Component> {
assert!(
!self.panic_on_build,
"MaybePanicApp: intentional build panic"
);
reset_layout_runtime();
fill_root(self.color)
}
fn clear_color(&self) -> Option<Color> {
Some(Color::rgba(0.0, 0.0, 0.0, 1.0))
}
}
pub fn assert_center_rgb(pixels: &[u8], w: u32, h: u32, expected: [u8; 3], label: &str) {
assert_eq!(
pixels.len(),
(w * h * 4) as usize,
"{label}: read-back must be width*height*4"
);
let center = (((h / 2) * w + (w / 2)) * 4) as usize;
let px = &pixels[center..center + 4];
for c in 0..3 {
assert!(
(px[c] as i32 - expected[c] as i32).abs() <= 4,
"{label} channel {c}: got {} expected ~{} (pixel {px:?})",
px[c],
expected[c],
);
}
assert_ne!(
&px[0..3],
&[0u8, 0, 0],
"{label}: fill did not paint over clear"
);
}
pub fn skip_without_gpu(what: &str) {
telar::testing::require_gpu(what, "no adapter");
}