mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 12:38:18 +00:00
Merge pull request #8682 from storytold/train/carry8554
Canvas: a tool drag held near an edge of the canvas scrolls the view (#8551) (#8554)
This commit is contained in:
@@ -1388,4 +1388,6 @@ fn selection_visuals(
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
#[cfg(test)]
|
||||
mod tests_edge_scroll;
|
||||
#[cfg(test)]
|
||||
mod tests_ui_scaling;
|
||||
|
||||
@@ -5,8 +5,8 @@ use super::drag::{
|
||||
tool_drag_moved, tool_frame_moves, tool_holds, tool_press_doc_id,
|
||||
};
|
||||
use super::view::{
|
||||
HOLD_DELAY, HOLD_ZOOM, SCRUB_SLOP, SCRUB_ZOOM, WheelTurn, animated_zoom, pan_pixels, pan_ppp, rotate_about, scroll_view, touch_gestures,
|
||||
trackpad_turn, wheel, whole_pixels, zoom_about,
|
||||
HOLD_DELAY, HOLD_ZOOM, SCRUB_SLOP, SCRUB_ZOOM, WheelTurn, animated_zoom, edge_scroll, edge_scroll_pixels, pan_pixels, pan_ppp, rotate_about,
|
||||
scroll_view, touch_gestures, trackpad_turn, wheel, whole_pixels, zoom_about,
|
||||
};
|
||||
use super::{Color32, Drag, Mods, Point, PointerEvent, PointerKind, Pos2, Shape, Stroke, Ui, VectorcraftApp, View, Xf, json};
|
||||
|
||||
@@ -363,6 +363,8 @@ fn begin_drag(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, p: Pos2) {
|
||||
if !tool_holds(d) {
|
||||
app.session.tool_hover_ended(view);
|
||||
}
|
||||
// Read before `data_mut`: the context is locked inside it.
|
||||
let edge = edge_scroll_at_press(ui, p);
|
||||
ui.data_mut(|dd| {
|
||||
dd.insert_temp(drag_id(), d);
|
||||
dd.insert_temp(drag_pos_id(), p);
|
||||
@@ -370,6 +372,8 @@ fn begin_drag(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, p: Pos2) {
|
||||
// A new press has not dragged yet, whatever an interrupted one left behind.
|
||||
dd.remove::<Point>(tool_drag_doc_id());
|
||||
dd.insert_temp(tool_press_doc_id(), xf.to_doc(p));
|
||||
// Edge scrolling waits for the pointer to move away from the press ([`edge_scroll_held`]).
|
||||
dd.insert_temp(edge_scroll_id(), edge);
|
||||
// Space held at the press (Cmd+Space lends the Zoom tool) sizes the marquee until
|
||||
// it is let go ([`zoom_marquee_start`]).
|
||||
if matches!(d, Drag::ZoomBox { .. }) {
|
||||
@@ -451,6 +455,11 @@ fn drag_held(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Pos2)
|
||||
// mouse's, a pen's on X11 among them, take the host's latest. Other tools get
|
||||
// the last position only, so a costly drag preview runs once a frame.
|
||||
let (moves, last_pose) = tool_frame_moves(ui, app, false);
|
||||
// Held near an edge of the canvas, the drag scrolls the view so it can go on past
|
||||
// what shows (#8551). The moves above came before the scroll and keep the view they
|
||||
// were made in; the pointer's position takes the scrolled one, so the tool gets a
|
||||
// new document position though the pointer stands still.
|
||||
let xf = edge_scroll_held(app, ui, f, p).unwrap_or(xf);
|
||||
// A frame whose pointer ends where the last one did still moved in between.
|
||||
let backquote = ui.input(|i| i.key_down(egui::Key::Backtick));
|
||||
let mods_changed = tool_drag_mods_changed(ui, mods(m, space), backquote);
|
||||
@@ -477,6 +486,77 @@ fn drag_held(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Pos2)
|
||||
}
|
||||
}
|
||||
|
||||
/// How far the pointer moves on screen from the press, in points, before a held tool drag
|
||||
/// scrolls the view at an edge of the canvas ([`edge_scroll_held`]).
|
||||
const EDGE_SCROLL_SLOP: f32 = 3.0;
|
||||
|
||||
/// A tool press's edge scrolling ([`edge_scroll_held`]), set when the press begins: where on screen
|
||||
/// it was pressed, whether the pointer has since moved away from there, and the part of a device
|
||||
/// pixel the scrolls of this press have left along each axis. Its own remainder, so the scrolls of
|
||||
/// one press neither take nor leave part of a wheel scroll's.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
struct EdgeScroll {
|
||||
press: Pos2,
|
||||
armed: bool,
|
||||
rest: (f64, f64),
|
||||
}
|
||||
|
||||
fn edge_scroll_id() -> egui::Id {
|
||||
egui::Id::unique("canvas-edge-scroll")
|
||||
}
|
||||
|
||||
/// A tool press's edge scrolling as it begins at `p`, the pointer's position at the end of the
|
||||
/// frame: the press is where the primary button went down, which an input batch can leave behind
|
||||
/// `p`, and the moves after it in the same frame may already have gone [`EDGE_SCROLL_SLOP`] from it.
|
||||
fn edge_scroll_at_press(ui: &Ui, p: Pos2) -> EdgeScroll {
|
||||
let (press, moved) = ui.input(|i| {
|
||||
let down = i.events.iter().rposition(|e| matches!(e, egui::Event::PointerButton { button: egui::PointerButton::Primary, pressed: true, .. }));
|
||||
let press = down.and_then(|k| match i.events.get(k) {
|
||||
Some(egui::Event::PointerButton { pos, .. }) => Some(*pos),
|
||||
_ => None,
|
||||
});
|
||||
let press = press.unwrap_or(p);
|
||||
let after = down.map_or(0, |k| k + 1);
|
||||
let moved = i.events.iter().skip(after).any(|e| matches!(e, egui::Event::PointerMoved(q) if (*q - press).length() >= EDGE_SCROLL_SLOP));
|
||||
(press, moved)
|
||||
});
|
||||
EdgeScroll { press, armed: moved || (p - press).length() >= EDGE_SCROLL_SLOP, rest: (0.0, 0.0) }
|
||||
}
|
||||
|
||||
/// Scroll the view for a tool drag held at `p` near an edge of the canvas ([`edge_scroll`]), and
|
||||
/// return the canvas's transform after it; `None` when the view did not move. Only once the pointer
|
||||
/// has moved [`EDGE_SCROLL_SLOP`] from the press on screen, in any move of a frame (a click near the edge stays a click, and
|
||||
/// a view scrolled by the wheel under a still press is not a drag), with the pointer over the
|
||||
/// canvas itself (over a panel the Selection tools hand the art to the panels), and not under a
|
||||
/// modal dialog. The view moves by whole device pixels; the part of a pixel left over waits for the
|
||||
/// next frame on an axis that scrolls on, and stays as it is on one that does not.
|
||||
fn edge_scroll_held(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, p: Pos2) -> Option<Xf> {
|
||||
let mut st = ui.data(|d| d.get_temp::<EdgeScroll>(edge_scroll_id()))?;
|
||||
// Any move of the frame counts, not only where the pointer ends up: one that went away and came
|
||||
// back to the press still dragged.
|
||||
let moved = |q: Pos2| (q - st.press).length() >= EDGE_SCROLL_SLOP;
|
||||
if !st.armed && (moved(p) || ui.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::PointerMoved(q) if moved(*q))))) {
|
||||
st.armed = true;
|
||||
ui.data_mut(|d| d.insert_temp(edge_scroll_id(), st));
|
||||
}
|
||||
if !st.armed || f.modal || !ui.input(|i| i.pointer.has_pointer()) || !on_canvas(ui, f.resp, p) {
|
||||
return None;
|
||||
}
|
||||
let by = edge_scroll(f.rect, p, ui.input(|i| i.unstable_dt))?;
|
||||
let ppp = pan_ppp(app, ui);
|
||||
let ui_zoom = app.canvas_ui_zoom;
|
||||
let ((dx, dy), rest) = edge_scroll_pixels(st.rest, by, ppp);
|
||||
st.rest = rest;
|
||||
ui.data_mut(|d| d.insert_temp(edge_scroll_id(), st));
|
||||
if dx == 0.0 && dy == 0.0 {
|
||||
return None;
|
||||
}
|
||||
let vm = app.view_mut()?;
|
||||
pan_pixels(vm, (ppp, ui_zoom), (dx, dy));
|
||||
let v = *vm;
|
||||
Some(Xf::at(f.rect, &v, ui_zoom))
|
||||
}
|
||||
|
||||
/// A drag for each of this frame's moves before the pointer's position ([`tool_frame_moves`]), each
|
||||
/// with the pressure and the tilt and rotation the host's feed kept for that move, else the latest.
|
||||
fn dispatch_frame_moves(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, backquote: bool, moves: Vec<FrameMove>) {
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
//! A tool drag held near an edge of the canvas scrolls the view so the drag can go on past what
|
||||
//! shows (#8551): a Selection tool move carries the art along, a drawing tool's shape grows, and
|
||||
//! the view stops once the pointer leaves the edge. A click near the edge scrolls nothing.
|
||||
|
||||
use super::view::{EDGE_SCROLL_SPEED, EDGE_SCROLL_ZONE, edge_scroll, edge_scroll_pixels};
|
||||
use super::*;
|
||||
use vectorcraft_engine::Session;
|
||||
|
||||
/// One headless canvas frame on an 800 × 600 window.
|
||||
fn frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec<egui::Event>) {
|
||||
let raw = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0))), events, ..Default::default() };
|
||||
let mut out = ctx.run_ui(raw, |ui| show(app, ui));
|
||||
out.textures_delta.clear();
|
||||
}
|
||||
|
||||
fn button(pos: Pos2, pressed: bool) -> egui::Event {
|
||||
egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }
|
||||
}
|
||||
|
||||
/// A 400 × 300 document with a 100 pt square at (50, 50), zoomed in to 4× about the square's
|
||||
/// centre, with `tool` selected; the frame drawn once.
|
||||
fn zoomed_in(tool: &str) -> (VectorcraftApp, egui::Context, vectorcraft_doc::NodeId) {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
let id = app.session.execute("shape.rectangle", &json!({"x": 50, "y": 50, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap();
|
||||
app.select_tool(tool);
|
||||
let ctx = egui::Context::default();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
let v = app.view_mut().unwrap();
|
||||
v.zoom = 4.0;
|
||||
v.center = Point::new(100.0, 100.0);
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
(app, ctx, vectorcraft_doc::NodeId(id))
|
||||
}
|
||||
|
||||
fn center(app: &VectorcraftApp) -> Point {
|
||||
app.view().unwrap().center
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edge_scroll_moves_the_content_away_from_the_edge_the_pointer_is_near() {
|
||||
let r = egui::Rect::from_min_size(pos2(100.0, 50.0), vec2(600.0, 400.0));
|
||||
let dt = 0.1;
|
||||
let full = EDGE_SCROLL_SPEED * dt;
|
||||
// Away from the edges, or off the canvas, nothing.
|
||||
assert_eq!(edge_scroll(r, r.center(), dt), None);
|
||||
assert_eq!(edge_scroll(r, pos2(r.right() + 5.0, r.center().y), dt), None, "over a panel");
|
||||
assert_eq!(edge_scroll(r, pos2(r.right() - EDGE_SCROLL_ZONE, r.center().y), dt), None, "at the zone's inner side");
|
||||
assert_eq!(edge_scroll(r, pos2(r.right() - 1.0, r.center().y), 0.0), None, "no time passed");
|
||||
assert_eq!(edge_scroll(r, pos2(f32::NAN, r.center().y), dt), None);
|
||||
// At the right edge the content moves left at full speed; half way into the zone, at half.
|
||||
let right = edge_scroll(r, pos2(r.right(), r.center().y), dt).unwrap();
|
||||
assert!((right.x + full).abs() < 1e-3 && right.y == 0.0, "{right:?}");
|
||||
let half = edge_scroll(r, pos2(r.right() - EDGE_SCROLL_ZONE / 2.0, r.center().y), dt).unwrap();
|
||||
assert!((half.x + full / 2.0).abs() < 1e-3, "{half:?}");
|
||||
// Near the left and top edges it moves right and down; in a corner, both ways.
|
||||
assert!(edge_scroll(r, pos2(r.left() + 1.0, r.center().y), dt).unwrap().x > 0.0);
|
||||
let corner = edge_scroll(r, pos2(r.left() + 1.0, r.top() + 1.0), dt).unwrap();
|
||||
assert!(corner.x > 0.0 && corner.y > 0.0, "{corner:?}");
|
||||
// In a corner both ways, but no faster than at one edge.
|
||||
let corner = edge_scroll(r, pos2(r.right() - 1.0, r.bottom() - 1.0), dt).unwrap();
|
||||
assert!(corner.x < 0.0 && corner.y < 0.0 && corner.length() <= full + 1e-3, "{corner:?}");
|
||||
assert!(edge_scroll(r, pos2(r.center().x, r.bottom() - 1.0), dt).unwrap().y < 0.0);
|
||||
// A stalled frame scrolls a tenth of a second's worth at most.
|
||||
assert_eq!(edge_scroll(r, pos2(r.right(), r.center().y), 5.0), Some(right));
|
||||
}
|
||||
|
||||
/// The view moves by whole device pixels, keeping the rest for the press's next scroll; an axis
|
||||
/// the scroll does not move keeps its remainder untouched rather than rounding it each frame.
|
||||
#[test]
|
||||
fn edge_scroll_pixels_keep_the_remainder_of_an_axis_that_does_not_scroll() {
|
||||
// 10.5 pixels: 11 now (rounded away from zero), half a pixel back owed.
|
||||
let (d, rest) = edge_scroll_pixels((0.0, 0.0), vec2(-10.5, 0.0), 1.0);
|
||||
assert_eq!(d, (-11.0, 0.0));
|
||||
assert_eq!(rest, (0.5, 0.0));
|
||||
// Along the top edge only: x keeps its half pixel, frame after frame, and does not move.
|
||||
let mut rest = rest;
|
||||
for _ in 0..4 {
|
||||
let (d, r) = edge_scroll_pixels(rest, vec2(0.0, 7.0), 1.0);
|
||||
assert_eq!(d, (0.0, 7.0));
|
||||
assert_eq!(r.0, 0.5, "x keeps its remainder");
|
||||
rest = r;
|
||||
}
|
||||
// Scrolling that way again, it counts: 0.5 + 1.5 = 2 pixels.
|
||||
assert_eq!(edge_scroll_pixels(rest, vec2(1.5, 0.0), 1.0).0, (2.0, 0.0));
|
||||
// At 2 device pixels a point.
|
||||
assert_eq!(edge_scroll_pixels((0.0, 0.0), vec2(0.25, -1.0), 2.0), ((1.0, -2.0), (-0.5, 0.0)));
|
||||
}
|
||||
|
||||
/// The issue's case: zoomed in, a selected object dragged towards the panels on the right reaches
|
||||
/// the canvas's edge. Held there, the view scrolls right and the object goes on with the pointer;
|
||||
/// back away from the edge, the view stops.
|
||||
#[test]
|
||||
fn a_move_held_near_the_right_edge_scrolls_the_view_and_carries_the_art() {
|
||||
let (mut app, ctx, id) = zoomed_in("selection");
|
||||
let rect = app.canvas_rect.unwrap();
|
||||
let xf = Xf::new(rect, app.view().unwrap());
|
||||
let left = |app: &VectorcraftApp| app.session.active().unwrap().doc.node(id).unwrap().geometric_bounds().unwrap().x0;
|
||||
let start = xf.to_screen(Point::new(100.0, 100.0));
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(start), button(start, true)]);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(start + vec2(20.0, 0.0))]);
|
||||
let edge = pos2(rect.right() - 4.0, start.y);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(edge)]);
|
||||
let (c0, x0) = (center(&app), left(&app));
|
||||
// Held still at the edge, frame after frame the view scrolls right and the art keeps up.
|
||||
let mut last = (c0, x0);
|
||||
for _ in 0..5 {
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
let now = (center(&app), left(&app));
|
||||
assert!(now.0.x > last.0.x, "the view scrolls right: {:?} then {:?}", last.0, now.0);
|
||||
assert!((now.0.y - c0.y).abs() < 1e-9, "and only right");
|
||||
assert!(now.1 > last.1, "the art moves with the pointer: {} then {}", last.1, now.1);
|
||||
last = now;
|
||||
}
|
||||
// The art stays under the pointer: it moved as far as the view (give or take the move's
|
||||
// rounding to the pixel grid).
|
||||
assert!(((last.1 - x0) - (last.0.x - c0.x)).abs() < 1.0, "art {} view {}", last.1 - x0, last.0.x - c0.x);
|
||||
assert!(app.session.active().unwrap().interaction.is_some(), "still the same move");
|
||||
// Back away from the edge the view stops.
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(rect.center())]);
|
||||
let stopped = center(&app);
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
assert_eq!(center(&app), stopped, "the view stops away from the edge");
|
||||
// The release ends the move where the art is.
|
||||
let moved = left(&app);
|
||||
frame(&mut app, &ctx, vec![button(rect.center(), false)]);
|
||||
let st = app.session.active().unwrap();
|
||||
assert!(st.interaction.is_none());
|
||||
assert!((left(&app) - moved).abs() < 1e-6);
|
||||
assert!(moved > 50.0 + (last.0.x - c0.x), "the art went past where the edge showed at first");
|
||||
}
|
||||
|
||||
/// A drawing tool's drag scrolls too: a rectangle dragged to the bottom edge goes on growing.
|
||||
#[test]
|
||||
fn a_rectangle_drag_held_near_the_bottom_edge_keeps_growing() {
|
||||
let (mut app, ctx, _) = zoomed_in("rectangle");
|
||||
let rect = app.canvas_rect.unwrap();
|
||||
// Away from the selected square's centre point, where a press moves the square (#5477).
|
||||
let start = rect.center() - vec2(150.0, 100.0);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(start), button(start, true)]);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(start + vec2(10.0, 10.0))]);
|
||||
let edge = pos2(start.x + 10.0, rect.bottom() - 2.0);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(edge)]);
|
||||
let c0 = center(&app);
|
||||
// The bottom of the rectangle drawn so far: the preview in the document.
|
||||
let preview_bottom = |app: &VectorcraftApp| {
|
||||
let st = app.session.active().unwrap();
|
||||
let mut bottom = f64::MIN;
|
||||
st.doc.walk(|n| {
|
||||
if let Some(b) = n.geometric_bounds() {
|
||||
bottom = bottom.max(b.y1);
|
||||
}
|
||||
});
|
||||
bottom
|
||||
};
|
||||
for _ in 0..4 {
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
// Each held frame the preview reaches the pointer in the view as it is now.
|
||||
let now = Xf::new(rect, app.view().unwrap()).to_doc(edge).y;
|
||||
assert!((preview_bottom(&app) - now).abs() < 0.5, "the preview follows the pointer: {} vs {now}", preview_bottom(&app));
|
||||
}
|
||||
let c1 = center(&app);
|
||||
assert!(c1.y > c0.y && (c1.x - c0.x).abs() < 1e-9, "the view scrolls down: {c0:?} then {c1:?}");
|
||||
let xf = Xf::new(rect, app.view().unwrap());
|
||||
let bottom = xf.to_doc(edge).y;
|
||||
frame(&mut app, &ctx, vec![button(edge, false)]);
|
||||
let st = app.session.active().unwrap();
|
||||
let made = st.selection.objects.first().and_then(|id| st.doc.node(*id)).and_then(|n| n.geometric_bounds()).unwrap();
|
||||
assert!((made.y1 - bottom).abs() < 0.5, "the rectangle reaches the pointer in the scrolled view: {} vs {bottom}", made.y1);
|
||||
}
|
||||
|
||||
/// A press near the edge that has not dragged is a click: the view stays.
|
||||
#[test]
|
||||
fn a_press_held_still_near_the_edge_does_not_scroll() {
|
||||
let (mut app, ctx, _) = zoomed_in("selection");
|
||||
let rect = app.canvas_rect.unwrap();
|
||||
let at = pos2(rect.right() - 4.0, rect.center().y);
|
||||
let c0 = center(&app);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at), button(at, true)]);
|
||||
for _ in 0..4 {
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
}
|
||||
assert_eq!(center(&app), c0);
|
||||
frame(&mut app, &ctx, vec![button(at, false)]);
|
||||
assert_eq!(center(&app), c0);
|
||||
}
|
||||
|
||||
/// The Hand tool's own drag is not a tool drag: held near the edge it pans with the pointer only.
|
||||
#[test]
|
||||
fn a_hand_drag_held_near_the_edge_does_not_scroll_on() {
|
||||
let (mut app, ctx, _) = zoomed_in("hand");
|
||||
let rect = app.canvas_rect.unwrap();
|
||||
let start = rect.center();
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(start), button(start, true)]);
|
||||
let edge = pos2(rect.right() - 4.0, start.y);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(edge)]);
|
||||
let c0 = center(&app);
|
||||
for _ in 0..3 {
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
}
|
||||
assert_eq!(center(&app), c0);
|
||||
frame(&mut app, &ctx, vec![button(edge, false)]);
|
||||
}
|
||||
|
||||
/// A press held still near the edge while the wheel scrolls the view is not a drag: the pointer
|
||||
/// has not moved on screen, so the view does not go on scrolling after the wheel stops.
|
||||
#[test]
|
||||
fn a_wheel_scroll_under_a_still_press_near_the_edge_does_not_start_edge_scrolling() {
|
||||
let (mut app, ctx, _) = zoomed_in("selection");
|
||||
let rect = app.canvas_rect.unwrap();
|
||||
let at = pos2(rect.right() - 4.0, rect.center().y);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at), button(at, true)]);
|
||||
let wheel = egui::Event::MouseWheel {
|
||||
unit: egui::MouseWheelUnit::Point,
|
||||
delta: vec2(0.0, 40.0),
|
||||
phase: egui::TouchPhase::Move,
|
||||
modifiers: Default::default(),
|
||||
source: egui::MouseWheelSource::Unknown,
|
||||
};
|
||||
let c0 = center(&app);
|
||||
frame(&mut app, &ctx, vec![wheel]);
|
||||
let wheeled = center(&app);
|
||||
assert_ne!(wheeled, c0, "the wheel scrolls the view");
|
||||
for _ in 0..4 {
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
}
|
||||
// The wheel's own scroll eases out along y over these frames; the right edge would scroll x.
|
||||
assert_eq!(center(&app).x, wheeled.x, "no edge scrolling");
|
||||
frame(&mut app, &ctx, vec![button(at, false)]);
|
||||
}
|
||||
|
||||
/// The press is where the button went down, not where the frame's input left the pointer: a press
|
||||
/// well inside the canvas and a move to the edge in the same input batch has dragged, so holding
|
||||
/// the pointer still there scrolls.
|
||||
#[test]
|
||||
fn a_press_and_a_move_to_the_edge_in_one_frame_scroll_once_held() {
|
||||
let (mut app, ctx, _) = zoomed_in("selection");
|
||||
let rect = app.canvas_rect.unwrap();
|
||||
let start = pos2(rect.center().x, rect.bottom() - 30.0);
|
||||
let edge = pos2(start.x, rect.bottom() - 4.0);
|
||||
let c0 = center(&app);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(start), button(start, true), egui::Event::PointerMoved(edge)]);
|
||||
for _ in 0..3 {
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
}
|
||||
assert!(center(&app).y > c0.y, "the view scrolls down: {:?} vs {c0:?}", center(&app));
|
||||
frame(&mut app, &ctx, vec![button(edge, false)]);
|
||||
}
|
||||
|
||||
/// A move away from the press and back within one frame still dragged: the press held near the
|
||||
/// edge then scrolls, though the pointer ends the frame where it was pressed.
|
||||
#[test]
|
||||
fn a_move_away_and_back_within_a_frame_still_arms_edge_scrolling() {
|
||||
let (mut app, ctx, _) = zoomed_in("selection");
|
||||
let rect = app.canvas_rect.unwrap();
|
||||
let at = pos2(rect.right() - 4.0, rect.center().y);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at), button(at, true)]);
|
||||
let c0 = center(&app);
|
||||
let away = at - vec2(8.0, 0.0);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(away), egui::Event::PointerMoved(at + vec2(-0.5, 0.0))]);
|
||||
for _ in 0..3 {
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
}
|
||||
assert!(center(&app).x > c0.x, "the view scrolls right: {:?} vs {c0:?}", center(&app));
|
||||
frame(&mut app, &ctx, vec![button(at, false)]);
|
||||
}
|
||||
@@ -125,6 +125,67 @@ pub(super) fn rotate_about(vm: &mut View, rect: egui::Rect, p: Pos2, degrees: f6
|
||||
vm.center += before - Xf::at(rect, vm, ui_zoom).to_doc(p);
|
||||
}
|
||||
|
||||
/// How far in from each edge of the canvas a held tool drag scrolls the view ([`edge_scroll`]), in
|
||||
/// screen points.
|
||||
pub(super) const EDGE_SCROLL_ZONE: f32 = 24.0;
|
||||
|
||||
/// How fast the view scrolls with the pointer at the canvas's very edge ([`edge_scroll`]), in
|
||||
/// screen points a second. Nearer the inner side of the zone it scrolls slower.
|
||||
pub(super) const EDGE_SCROLL_SPEED: f32 = 900.0;
|
||||
|
||||
/// How far a tool drag held at `p` near an edge of the canvas `rect` scrolls the view in `dt`
|
||||
/// seconds (#8551), as the screen points the content moves: away from that edge, so what lies
|
||||
/// past it comes into view, faster the nearer the pointer is to the edge. `None` with the pointer
|
||||
/// outside the canvas or away from its edges, or with no time passed. A stalled frame counts a
|
||||
/// tenth of a second at most, and in a corner the content moves no faster than near one edge.
|
||||
pub(super) fn edge_scroll(rect: egui::Rect, p: Pos2, dt: f32) -> Option<egui::Vec2> {
|
||||
if !rect.contains(p) || !p.x.is_finite() || !p.y.is_finite() || !dt.is_finite() || dt <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
// On a canvas narrower than two zones, each zone reaches the middle at most.
|
||||
let zone_x = EDGE_SCROLL_ZONE.min(rect.width() / 2.0);
|
||||
let zone_y = EDGE_SCROLL_ZONE.min(rect.height() / 2.0);
|
||||
// How deep into a zone the pointer is (0 at its inner side, 1 at the edge), signed by the way
|
||||
// the content moves: near the left or top edge it moves right or down.
|
||||
let depth = |near: f32, far: f32, zone: f32| {
|
||||
if zone <= 0.0 {
|
||||
0.0
|
||||
} else if near < zone {
|
||||
1.0 - near / zone
|
||||
} else if far < zone {
|
||||
far / zone - 1.0
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
};
|
||||
let dx = depth(p.x - rect.left(), rect.right() - p.x, zone_x);
|
||||
let dy = depth(p.y - rect.top(), rect.bottom() - p.y, zone_y);
|
||||
if dx == 0.0 && dy == 0.0 {
|
||||
return None;
|
||||
}
|
||||
// In a corner the scroll goes both ways, no faster than along one edge.
|
||||
let v = egui::vec2(dx, dy);
|
||||
let v = if v.length() > 1.0 { v / v.length() } else { v };
|
||||
Some(v * (EDGE_SCROLL_SPEED * dt.min(0.1)))
|
||||
}
|
||||
|
||||
/// An edge scroll of `by` screen points ([`edge_scroll`]) at `ppp` device pixels a point, after the
|
||||
/// scrolls of the same press left `rest` device pixels along each axis: the whole device pixels to
|
||||
/// move the content by, and what is left. An axis this scroll does not move keeps its remainder as
|
||||
/// it is: rounding a half pixel there would move the view one pixel back and forth each frame.
|
||||
pub(super) fn edge_scroll_pixels(rest: (f64, f64), by: egui::Vec2, ppp: f32) -> ((f64, f64), (f64, f64)) {
|
||||
let step = |rest: f64, by: f32| {
|
||||
if by == 0.0 {
|
||||
return (0.0, rest);
|
||||
}
|
||||
let total = rest + f64::from(by) * f64::from(ppp);
|
||||
let whole = total.round();
|
||||
(whole, total - whole)
|
||||
};
|
||||
let ((dx, rx), (dy, ry)) = (step(rest.0, by.x), step(rest.1, by.y));
|
||||
((dx, dy), (rx, ry))
|
||||
}
|
||||
|
||||
/// Move view `vm`'s content by `(dx, dy)` device pixels along the screen's axes, at `ppp` device
|
||||
/// pixels a point of an interface zoomed by `ui_zoom`: the canvas's frame moves by exactly those
|
||||
/// pixels.
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+2
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user