88 lines
3.4 KiB
Rust
88 lines
3.4 KiB
Rust
//! Panning the whiteboard: the hand tool, the middle and right buttons, and wheel scrolling.
|
|
|
|
use egui::{Context, Event, Modifiers, PointerButton, Pos2, RawInput, Rect, Vec2};
|
|
use notescraft_ui_egui::theme::Tokens;
|
|
use notescraft_ui_egui::whiteboard_view::{WhiteboardTool, WhiteboardView, show_whiteboard};
|
|
use notescraft_whiteboard::Whiteboard;
|
|
|
|
struct Harness {
|
|
ctx: Context,
|
|
tokens: Tokens,
|
|
view: WhiteboardView,
|
|
board: Whiteboard,
|
|
}
|
|
|
|
impl Harness {
|
|
fn new(tool: WhiteboardTool) -> Option<Self> {
|
|
let ctx = Context::default();
|
|
notescraft_ui_egui::fonts::install(&ctx);
|
|
ctx.run_ui(RawInput::default(), |_| {}).textures_delta.clear();
|
|
let mut view = WhiteboardView::default();
|
|
view.tool = tool;
|
|
let mut harness = Self { ctx, tokens: Tokens::new(notescraft_themes::builtin("catppuccin-mocha")?), view, board: Whiteboard::new() };
|
|
harness.frame(Vec::new());
|
|
Some(harness)
|
|
}
|
|
|
|
fn frame(&mut self, events: Vec<Event>) {
|
|
let input = RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, Vec2::new(900.0, 700.0))), events, ..RawInput::default() };
|
|
let (tokens, view, board) = (&self.tokens, &mut self.view, &mut self.board);
|
|
self.ctx
|
|
.run_ui(input, |ui| {
|
|
let _ = show_whiteboard(ui, tokens, view, board);
|
|
})
|
|
.textures_delta
|
|
.clear();
|
|
}
|
|
|
|
fn drag(&mut self, button: PointerButton, from: Pos2, to: Pos2) {
|
|
let at = |pos, pressed| Event::PointerButton { pos, button, pressed, modifiers: Modifiers::NONE };
|
|
self.frame(vec![Event::PointerMoved(from), at(from, true)]);
|
|
self.frame(vec![Event::PointerMoved(from + (to - from) * 0.5)]);
|
|
self.frame(vec![Event::PointerMoved(to)]);
|
|
self.frame(vec![at(to, false)]);
|
|
}
|
|
}
|
|
|
|
fn moved(view: &WhiteboardView) -> (f64, f64) {
|
|
(view.pan[0], view.pan[1])
|
|
}
|
|
|
|
#[test]
|
|
fn the_hand_tool_moves_the_board_with_the_pointer() {
|
|
let Some(mut h) = Harness::new(WhiteboardTool::Hand) else { return };
|
|
h.view.zoom = 2.0;
|
|
h.drag(PointerButton::Primary, Pos2::new(300.0, 300.0), Pos2::new(400.0, 340.0));
|
|
let (x, y) = moved(&h.view);
|
|
assert!((x - 50.0).abs() < 2.0 && (y - 20.0).abs() < 2.0, "100x40 screen points at zoom 2 are 50x20 document units: {x} {y}");
|
|
}
|
|
|
|
#[test]
|
|
fn middle_and_right_buttons_pan_in_any_tool() {
|
|
for button in [PointerButton::Middle, PointerButton::Secondary] {
|
|
let Some(mut h) = Harness::new(WhiteboardTool::Select) else { return };
|
|
h.drag(button, Pos2::new(300.0, 300.0), Pos2::new(260.0, 330.0));
|
|
let (x, y) = moved(&h.view);
|
|
assert!((x + 40.0).abs() < 2.0 && (y - 30.0).abs() < 2.0, "{button:?}: {x} {y}");
|
|
assert_eq!(h.view.selected_ids.len(), 0);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_wheel_pans_and_pinch_zooms() {
|
|
let Some(mut h) = Harness::new(WhiteboardTool::Select) else { return };
|
|
let at = Pos2::new(400.0, 300.0);
|
|
h.frame(vec![Event::PointerMoved(at)]);
|
|
h.frame(vec![Event::MouseWheel {
|
|
unit: egui::MouseWheelUnit::Point,
|
|
delta: Vec2::new(0.0, 40.0),
|
|
phase: egui::TouchPhase::Move,
|
|
modifiers: Modifiers::NONE,
|
|
}]);
|
|
h.frame(Vec::new());
|
|
assert!(h.view.pan[1] > 1.0, "scrolling moves the board: {:?}", h.view.pan);
|
|
let before = h.view.zoom;
|
|
h.frame(vec![Event::Zoom(1.5)]);
|
|
assert!(h.view.zoom > before * 1.4, "pinch zooms: {}", h.view.zoom);
|
|
}
|