use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
use lurq::{
app::{App, Tree, events::MouseButton},
components::{Column, Rect, Text},
layout::{Constraints, Size, quad::RichTextSpan, text_style::TextStyle},
node::{Element, color::Color},
};
use crate::support::{RectSnapshot, pointer_click, render_pass_with_app};
const FAMILY: &str = "Lurq Weight Probe";
const REGULAR: &[u8] = include_bytes!("../assets/weight_probe/LurqWeightProbe-Regular.ttf");
const RED: Color = Color::new(200, 30, 60, 255);
const GREEN: Color = Color::new(30, 160, 60, 255);
const LINE: f32 = 20.0;
fn probe_app() -> App {
let mut app = App::new();
app.install_fonts([REGULAR.to_vec()], std::iter::empty::<(&str, &str)>());
app
}
fn style() -> TextStyle {
TextStyle {
font_family: FAMILY.into(),
font_size: 10.0,
line_height: 2.0,
..TextStyle::default()
}
}
fn sentence(object: impl Into<Element>) -> Text {
Text::rich([
RichTextSpan::new("aa ", style()),
RichTextSpan::object(style()),
RichTextSpan::new(" aa", style()),
])
.object(object)
}
struct Scene {
app: App,
tree: Tree,
}
impl Scene {
fn new(root: impl Into<Element>, width: f32) -> Self {
let app = probe_app();
let mut tree = Tree::new();
tree.resize(400, 300);
tree.set_root(root);
tree.set_layout_constraints_override(Some(Constraints::loose(Size::new(width, 300.0))));
let mut scene = Self { app, tree };
scene.render();
scene
}
fn render(&mut self) -> Vec<RectSnapshot> {
render_pass_with_app(&mut self.tree, &mut self.app).rects
}
fn size(&self) -> Size {
self.tree.last_layout().expect("a layout").size
}
fn the_box(&mut self, color: Color) -> RectSnapshot {
let boxes: Vec<_> = self.render().into_iter().filter(|rect| rect.color == color).collect();
assert_eq!(boxes.len(), 1, "one box of {color:?}");
boxes[0].clone()
}
}
fn close(actual: f32, expected: f32, what: &str) {
assert!(
(actual - expected).abs() < 0.6,
"{what}: {actual} where {expected} was expected"
);
}
#[test]
fn an_object_takes_the_width_of_its_element_in_the_line_and_keeps_the_lines_height() {
let mut scene = Scene::new(sentence(Rect::new(30.0, 8.0).background(RED)), 400.0);
close(scene.size().width, 55.0, "the paragraph's width");
close(scene.size().height, LINE, "the paragraph's height: the line's");
let object = scene.the_box(RED);
close(object.x, 12.5, "the object stands after the words before it");
close(object.width, 30.0, "as wide as its element");
close(object.y + object.height / 2.0, LINE / 2.0, "on the middle of its line");
let mut tall = Scene::new(sentence(Rect::new(30.0, 32.0).background(RED)), 400.0);
close(tall.size().height, LINE, "the line keeps its height");
let object = tall.the_box(RED);
close(object.height, 32.0, "drawn whole");
close(object.y, (LINE - 32.0) / 2.0, "over the top of the line");
}
#[test]
fn an_object_wraps_with_the_words_and_is_no_wider_than_the_paragraph() {
let mut scene = Scene::new(sentence(Rect::new(30.0, 8.0).background(RED)), 45.0);
let height = scene.size().height;
assert!(height > 1.5 * LINE && height < 2.5 * LINE, "two lines: {height}");
let object = scene.the_box(RED);
close(object.x, 12.5, "on the first line, after its words");
close(
object.y + object.height / 2.0,
LINE / 2.0,
"on the middle of the first line",
);
let mut narrow = Scene::new(sentence(Rect::new(30.0, 8.0).background(RED)), 32.0);
let object = narrow.the_box(RED);
close(object.x, 0.0, "at the start of its line");
close(object.y + object.height / 2.0, LINE * 1.5, "on the second line");
let scene = Scene::new(sentence(Rect::new(60.0, 8.0).background(GREEN)), 40.0);
assert!(
scene.size().width <= 40.5,
"no wider than the room: {}",
scene.size().width
);
}
#[test]
fn objects_are_given_in_the_order_of_the_text_and_a_span_left_without_one_is_text() {
let spans = [
RichTextSpan::object(style()),
RichTextSpan::new("a", style()),
RichTextSpan::object(style()),
RichTextSpan::new("a", style()),
RichTextSpan::object(style()),
];
let mut scene = Scene::new(
Text::rich(spans)
.object(Rect::new(20.0, 6.0).background(RED))
.object(Rect::new(10.0, 6.0).background(GREEN)),
400.0,
);
close(scene.the_box(RED).x, 0.0, "the first object");
close(scene.the_box(GREEN).x, 25.0, "the second, after 20 and an a");
assert!(scene.size().width >= 39.5, "{}", scene.size().width);
}
#[test]
fn a_press_on_an_object_is_its_elements() {
let pressed = Arc::new(AtomicUsize::new(0));
let counted = pressed.clone();
let button = Column::new().size(30.0, 12.0).background(RED).on_click(move |_| {
counted.fetch_add(1, Ordering::SeqCst);
});
let mut scene = Scene::new(sentence(button), 400.0);
let object = scene.the_box(RED);
pointer_click(
&mut scene.tree,
object.x + object.width / 2.0,
object.y + object.height / 2.0,
MouseButton::Left,
);
scene.render();
assert_eq!(pressed.load(Ordering::SeqCst), 1);
pointer_click(&mut scene.tree, 3.0, LINE / 2.0, MouseButton::Left);
scene.render();
assert_eq!(pressed.load(Ordering::SeqCst), 1);
}
#[test]
fn an_element_that_changes_its_size_lays_its_line_out_again() {
let width = lurq::core::Signal::new(30.0f32);
let held = width.clone();
#[derive(Clone)]
struct Props(lurq::core::Signal<f32>);
impl PartialEq for Props {
fn eq(&self, _: &Self) -> bool {
false
}
}
#[cfg(feature = "devtools")]
impl lurq::app::component::DevtoolsInspectable for Props {}
struct Grows;
impl lurq::app::component::Component for Grows {
type Props = Props;
fn create(_: &mut lurq::app::ctx::Ctx) -> Self {
Self
}
fn render(&self, ctx: &mut lurq::app::ctx::Ctx) -> impl Into<Element> {
let width = ctx.props::<Props>().0.get();
Rect::new(width, 8.0).background(RED)
}
}
let mut app = probe_app();
let mut tree = Tree::new();
tree.resize(400, 300);
struct Root;
impl lurq::app::component::Component for Root {
type Props = Props;
fn create(_: &mut lurq::app::ctx::Ctx) -> Self {
Self
}
fn render(&self, ctx: &mut lurq::app::ctx::Ctx) -> impl Into<Element> {
let props = ctx.props::<Props>().clone();
Column::new()
.child(sentence(Element::component::<Grows>(props)))
.child(Rect::new(4.0, 4.0).background(GREEN))
}
}
tree.mount_root::<Root>(&mut app, Props(held));
let rects = render_pass_with_app(&mut tree, &mut app).rects;
let red = rects.iter().find(|rect| rect.color == RED).expect("the object").clone();
close(red.width, 30.0, "at first");
width.set(50.0);
let rects = render_pass_with_app(&mut tree, &mut app).rects;
let red = rects.iter().find(|rect| rect.color == RED).expect("the object").clone();
close(red.width, 50.0, "grown");
}
#[test]
fn a_highlight_past_an_object_is_on_the_words_it_covers() {
use lurq::components::TextHighlight;
let text = sentence(Rect::new(30.0, 8.0).background(RED)).highlights(vec![TextHighlight::new(7..9, GREEN)]);
let mut scene = Scene::new(text, 400.0);
let mark = scene.the_box(GREEN);
close(mark.x, 45.0, "the highlight starts where its words are drawn");
close(mark.width, 10.0, "and is as wide as they are");
}