Canvas: edge scrolling arms from the press and from every move of a frame

Review follow-up for #8551:
- The press position is where the primary button went down, not where
  the frame's input left the pointer: a press and a move to the edge in
  one input batch has dragged, and holding still there now scrolls.
- Any move of a frame that goes 3 points from the press arms edge
  scrolling, not only the frame's last position: a move away and back
  within one frame still dragged.
- docs/roadmap.md: the M11 row, status line and revision history mention
  the edge scrolling; the View & navigation row points to its evidence.
This commit is contained in:
HassanKhan
2026-11-19 11:41:31 +05:00
parent 04b216cbc9
commit 6ec76d7513
4 changed files with 66 additions and 6 deletions
+26 -3
View File
@@ -359,6 +359,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);
@@ -367,7 +369,7 @@ fn begin_drag(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, p: Pos2) {
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(), EdgeScroll { press: p, armed: false, rest: (0.0, 0.0) });
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 { .. }) {
@@ -499,16 +501,37 @@ 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 (a click near the edge stays a click, and
/// 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()))?;
if !st.armed && (p - st.press).length() >= EDGE_SCROLL_SLOP {
// 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));
}
@@ -229,3 +229,39 @@ fn a_wheel_scroll_under_a_still_press_near_the_edge_does_not_start_edge_scrollin
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)]);
}
+3 -2
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long