113 lines
4.8 KiB
Rust
113 lines
4.8 KiB
Rust
//! P10 motion tests for the whiteboard.
|
|
//!
|
|
//! Pins the element appear animation (scale 0.85 -> 1.0 about the centre) and
|
|
//! that turning motion off restores instant painting.
|
|
|
|
use egui::{Context, Event, PointerButton, Pos2, RawInput, Rect, Vec2};
|
|
use notescraft_ui_egui::{
|
|
motion,
|
|
whiteboard_view::{WhiteboardTool, WhiteboardView, show_whiteboard},
|
|
};
|
|
use notescraft_whiteboard::Whiteboard;
|
|
|
|
const FRAME_SECONDS: f64 = 1.0 / 60.0;
|
|
|
|
fn tokens() -> notescraft_ui_egui::theme::Tokens {
|
|
let theme = notescraft_themes::builtin("catppuccin-mocha").expect("mocha theme");
|
|
notescraft_ui_egui::theme::Tokens::new(theme)
|
|
}
|
|
|
|
fn input(time: f64, events: Vec<Event>) -> RawInput {
|
|
RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, Vec2::new(1440.0, 900.0))), events, time: Some(time), ..RawInput::default() }
|
|
}
|
|
|
|
/// Union of every painted path point; the drawn rectangle is the only path
|
|
/// painted when the board holds one element and the grid is off.
|
|
fn painted_bounds(shapes: &[egui::epaint::ClippedShape]) -> Option<Rect> {
|
|
shapes
|
|
.iter()
|
|
.filter_map(|clipped| match &clipped.shape {
|
|
egui::Shape::Path(path) => Some(path.points.clone()),
|
|
_ => None,
|
|
})
|
|
.flatten()
|
|
.map(|point| Rect::from_min_max(point, point))
|
|
// The animated toolbar icons are paths too: keep only what lies around the dragged rectangle.
|
|
.filter(|rect| Rect::from_min_max(Pos2::new(200.0, 200.0), Pos2::new(500.0, 500.0)).contains_rect(*rect))
|
|
.reduce(Rect::union)
|
|
}
|
|
|
|
/// Draws a 100x100 rectangle at the document origin with the given tool and
|
|
/// returns the painted bounds on the frame right after the gesture finished.
|
|
fn draw_rectangle_painted(ctx: &Context, view: &mut WhiteboardView, board: &mut Whiteboard, time: f64) -> Option<Rect> {
|
|
let tokens = tokens();
|
|
view.tool = WhiteboardTool::Rectangle;
|
|
let from = Pos2::new(300.0, 300.0);
|
|
let to = Pos2::new(400.0, 400.0);
|
|
let mut output = ctx.run_ui(input(time, Vec::new()), |ui| {
|
|
show_whiteboard(ui, &tokens, view, board);
|
|
});
|
|
output.textures_delta.clear();
|
|
let drag = [
|
|
vec![Event::PointerMoved(from), Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE }],
|
|
vec![Event::PointerMoved(to)],
|
|
vec![Event::PointerButton { pos: to, button: PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }],
|
|
];
|
|
for (index, events) in drag.iter().enumerate() {
|
|
let offset = FRAME_SECONDS * (f64::from(u8::try_from(index).unwrap_or(0)) + 1.0);
|
|
let mut output = ctx.run_ui(input(time + offset, events.clone()), |ui| {
|
|
show_whiteboard(ui, &tokens, view, board);
|
|
});
|
|
output.textures_delta.clear();
|
|
}
|
|
let mut output = ctx.run_ui(input(time + FRAME_SECONDS * 4.0, Vec::new()), |ui| {
|
|
show_whiteboard(ui, &tokens, view, board);
|
|
});
|
|
output.textures_delta.clear();
|
|
painted_bounds(&output.shapes)
|
|
}
|
|
|
|
#[test]
|
|
fn new_element_starts_scaled_up_from_its_centre() {
|
|
let ctx = Context::default();
|
|
motion::set_enabled(&ctx, true);
|
|
let mut view = WhiteboardView::default();
|
|
let mut board = Whiteboard::new();
|
|
|
|
let first = draw_rectangle_painted(&ctx, &mut view, &mut board, 0.0).expect("the rectangle is painted");
|
|
assert!(first.width() < 100.0, "the appear animation starts below full size, got width {}", first.width());
|
|
assert!((first.center().x - 350.0).abs() < 1.0, "it scales about its centre, got x centre {}", first.center().x);
|
|
}
|
|
|
|
#[test]
|
|
fn the_appear_animation_settles_at_full_size() {
|
|
let ctx = Context::default();
|
|
motion::set_enabled(&ctx, true);
|
|
let mut view = WhiteboardView::default();
|
|
let mut board = Whiteboard::new();
|
|
|
|
draw_rectangle_painted(&ctx, &mut view, &mut board, 0.0);
|
|
let tokens = tokens();
|
|
// A second full second of frames: SLOW is 0.26 s, so this is past the end.
|
|
let mut settled = Rect::NOTHING;
|
|
for frame in 5_u32..80 {
|
|
let mut output = ctx.run_ui(input(f64::from(frame) * FRAME_SECONDS, Vec::new()), |ui| {
|
|
show_whiteboard(ui, &tokens, &mut view, &mut board);
|
|
});
|
|
output.textures_delta.clear();
|
|
settled = painted_bounds(&output.shapes).unwrap_or(settled);
|
|
}
|
|
assert!((settled.width() - 100.0).abs() < 2.0, "it settles at full size, got width {}", settled.width());
|
|
}
|
|
|
|
#[test]
|
|
fn motion_off_paints_at_full_size_immediately() {
|
|
let ctx = Context::default();
|
|
motion::set_enabled(&ctx, false);
|
|
let mut view = WhiteboardView::default();
|
|
let mut board = Whiteboard::new();
|
|
|
|
let bounds = draw_rectangle_painted(&ctx, &mut view, &mut board, 0.0).expect("the rectangle is painted");
|
|
assert!((bounds.width() - 100.0).abs() < 2.0, "no animation means no scaling, got width {}", bounds.width());
|
|
}
|