use std::assert_matches;
use crate::core::{NewWidget, PropertySet, Widget, WidgetTag};
use crate::kurbo::{Circle, Dashes, Point, Stroke, Vec2};
use crate::layout::{AsUnit, Length, SizeDef, UnitPoint};
use crate::palette::css::{BLUE, GREEN, RED};
use crate::peniko::Color;
use crate::peniko::color::{AlphaColor, Srgb};
use crate::properties::types::MainAxisAlignment;
use crate::properties::{Background, Dimensions, Gap, Padding};
use crate::testing::{ModularWidget, Record, TestHarness, TestWidgetExt, assert_render_snapshot};
use crate::theme::test_property_set;
use crate::widgets::{Align, ChildAlignment, Flex, Grid, GridParams, Label, SizedBox, ZStack};
#[test]
fn request_paint() {
let target_tag = WidgetTag::named("target");
let parent_tag = WidgetTag::named("parent");
let child = NewWidget::new(SizedBox::empty().record()).with_tag(target_tag);
let parent = NewWidget::new(ModularWidget::new_parent(child).record()).with_tag(parent_tag);
let grandparent = NewWidget::new(ModularWidget::new_parent(parent));
let mut harness = TestHarness::create(test_property_set(), grandparent);
let _ = harness.render();
harness.flush_records_of(target_tag);
harness.flush_records_of(parent_tag);
harness.edit_widget(target_tag, |mut widget| {
widget.ctx.request_paint_only();
});
let _ = harness.render();
assert_matches!(harness.take_records_of(target_tag)[..], [Record::Paint]);
assert_matches!(harness.take_records_of(parent_tag)[..], []);
harness.edit_widget(target_tag, |mut widget| {
widget.ctx.request_post_paint();
});
let _ = harness.render();
assert_matches!(harness.take_records_of(target_tag)[..], [Record::PostPaint]);
assert_matches!(harness.take_records_of(parent_tag)[..], []);
let _ = harness.render();
assert_matches!(harness.take_records_of(parent_tag)[..], []);
assert_matches!(harness.take_records_of(target_tag)[..], []);
}
#[test]
fn paint_order() {
const SQUARE_SIZE: f64 = 30.;
const SQUARE_LENGTH: Length = Length::const_px(SQUARE_SIZE);
let child1 = NewWidget::new(SizedBox::empty().width(SQUARE_LENGTH).height(SQUARE_LENGTH))
.with_props(Background::Color(RED));
let child2 = NewWidget::new(SizedBox::empty().width(SQUARE_LENGTH).height(SQUARE_LENGTH))
.with_props(Background::Color(GREEN));
let child3 = NewWidget::new(SizedBox::empty().width(SQUARE_LENGTH).height(SQUARE_LENGTH))
.with_props(Background::Color(BLUE));
let children = vec![child1, child2, child3];
let parent = NewWidget::new(
ModularWidget::new_multi_parent(children)
.measure_fn(|_, _, _, _, _, _| (SQUARE_SIZE * 2.).px())
.layout_fn(move |children, ctx, _props, size| {
let mut pos = Point::ZERO;
for child in children {
let child_size = ctx.compute_size(child, SizeDef::fit(size), size.into());
ctx.run_layout(child, child_size);
ctx.place_child(child, pos);
pos += Vec2::new(SQUARE_SIZE / 2., SQUARE_SIZE / 2.);
}
})
.paint_fn(|_, ctx, _, scene| {
scene.fill(ctx.content_box(), Color::WHITE).draw();
})
.post_paint_fn(|_, ctx, _, scene| {
let rect = ctx.content_box().inset(-0.5);
scene.stroke(rect, &Stroke::new(1.0), Color::BLACK).draw();
}),
);
let grandparent = NewWidget::new(
Flex::column()
.main_axis_alignment(MainAxisAlignment::Center)
.with_fixed(parent),
);
let mut harness = TestHarness::create_with_size(
test_property_set(),
grandparent,
(SQUARE_SIZE as u32 * 3, SQUARE_SIZE as u32 * 3),
);
assert_render_snapshot!(harness, "paint_order");
}
#[test]
fn paint_clipping() {
const SQUARE_SIZE: f64 = 80.;
let circle = Circle::new((SQUARE_SIZE / 2., SQUARE_SIZE / 2.), SQUARE_SIZE * 0.60);
let parent = NewWidget::new(
ModularWidget::new(())
.measure_fn(|_, _, _, _, _, _| SQUARE_SIZE.px())
.layout_fn(|_, ctx, _, size| {
ctx.set_clip_path(size.to_rect());
})
.paint_fn(move |_, ctx, _, scene| {
scene.fill(ctx.content_box(), Color::WHITE).draw();
scene.fill(circle, RED).draw();
})
.post_paint_fn(move |_, _, _, painter| {
let style = Stroke {
width: 4.0,
dash_pattern: Dashes::from_slice(&[12.0, 12.0]),
..Default::default()
};
painter.stroke(circle, &style, Color::BLACK).draw();
}),
);
let parent = NewWidget::new(
Flex::column()
.main_axis_alignment(MainAxisAlignment::Center)
.with_fixed(parent),
);
let mut harness = TestHarness::create_with_size(
test_property_set(),
parent,
(SQUARE_SIZE as u32 * 2, SQUARE_SIZE as u32 * 2),
);
assert_render_snapshot!(harness, "paint_clipping");
}
#[test]
fn paint_transparency() {
fn child(
text: &str,
align: UnitPoint,
bg_color: impl Into<Option<AlphaColor<Srgb>>>,
) -> NewWidget<Align> {
let bg_color = bg_color.into();
let label = Label::new(text).prepare().with_props((
Background::Color(Color::TRANSPARENT),
Dimensions::fixed(20.px(), 20.px()),
));
let mut props = PropertySet::new();
if let Some(bg_color) = bg_color {
props = props.with(Background::Color(bg_color));
}
Align::new(align, label).prepare().with_props(props)
}
let align_a = UnitPoint::TOP_LEFT;
let align_b = UnitPoint::BOTTOM_LEFT;
let mut grid_a = Grid::with_dimensions(4, 1);
grid_a = grid_a.with(child("AAAA", align_a, None), GridParams::pos(0, 0));
grid_a = grid_a.with(
child("AABB", align_a, Color::TRANSPARENT),
GridParams::pos(1, 0),
);
grid_a = grid_a.with(
child("AACC", align_a, Color::from_rgba8(66, 117, 245, 127)),
GridParams::pos(2, 0),
);
let typo = concat!("AA", "DD");
grid_a = grid_a.with(
child(typo, align_a, Color::from_rgba8(66, 117, 245, 255)),
GridParams::pos(3, 0),
);
let mut grid_b = Grid::with_dimensions(16, 1);
grid_b = grid_b.with(
child("BBAA", align_b, None),
GridParams::pos(1, 0).with_width(3),
);
grid_b = grid_b.with(
child("BBBB", align_b, Color::TRANSPARENT),
GridParams::pos(5, 0).with_width(3),
);
grid_b = grid_b.with(
child("BBCC", align_b, Color::from_rgba8(245, 66, 191, 127)),
GridParams::pos(9, 0).with_width(3),
);
grid_b = grid_b.with(
child("BBDD", align_b, Color::from_rgba8(245, 66, 191, 255)),
GridParams::pos(13, 0).with_width(3),
);
let props = (Padding::all(20.px()), Gap::new(10.px()));
let grid_a = grid_a.prepare().with_props(props);
let grid_b = grid_b.prepare().with_props(props);
let mut root = ZStack::new();
root = root.with(grid_a, ChildAlignment::ParentAligned);
root = root.with(grid_b, ChildAlignment::ParentAligned);
let mut harness = TestHarness::create_with_size(test_property_set(), root.prepare(), (350, 80));
assert_render_snapshot!(harness, "paint_transparency");
}