Merge #18019 into the merge queue batch

Eraser: Shift+drag locks to horizontal or vertical only
This commit is contained in:
David Mann committed 2026-11-30 00:25:22 +01:00
commit 790c02fdfa
2 files changed
+117 -11

No files matched your search

@@ -0,0 +1,101 @@
//! #17668: Shift+drag with the Eraser locks to horizontal or vertical only, in Brush, Pencil and
//! Block mode, as the Brush does (#11889). Driven through the real canvas input path; the
//! assertions read the erased layer's alpha.
use egui::Modifiers;
use serde_json::json;
use crate::PhotocraftApp;
use crate::canvas::{ToolEvent, tool_event};
use crate::state::Tool;
const MODES: [&str; 3] = ["brush", "pencil", "block"];
fn app(mode: &str) -> Box<PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.background_jobs = false;
app.run("file.new", json!({"width": 600, "height": 500, "background": "white"})).unwrap();
app.run("layer.new.layer", json!({})).unwrap();
app.run("edit.fill", json!({"color": "#dc1e1e"})).unwrap();
app.ui.tool = Tool::Eraser;
app.ui.tool_options.eraser_mode = mode.into();
app.session.tools.brush.smoothing.amount = 0.0;
app.sync_views();
app.ui.views[0].zoom = 1.0;
app.ui.views[0].center = [300.0, 250.0];
app.ui.views[0].clear_pending();
app
}
/// A Shift+drag through `points` (the first is the press, the last the release).
fn shift_drag(app: &mut PhotocraftApp, points: &[[f64; 2]]) {
let shift = Modifiers::SHIFT;
let [x, y] = points[0];
tool_event(app, ToolEvent::Down { x, y, pressure: 1.0 }, shift);
for &[x, y] in &points[1..] {
tool_event(app, ToolEvent::Move { x, y, pressure: 1.0 }, shift);
}
let [x, y] = points[points.len() - 1];
tool_event(app, ToolEvent::Up { x, y }, shift);
}
/// Whether the pixel at (x, y) of the active layer is erased.
fn erased(app: &PhotocraftApp, x: i32, y: i32) -> bool {
let state = app.session.active().unwrap();
let path = state.doc.layers.index().path(state.active_layer.unwrap()).unwrap();
state.doc.layer_at(path).unwrap().surface().unwrap().rgba(x, y)[3] < 0.5
}
#[test]
fn a_mostly_horizontal_or_vertical_shift_drag_erases_only_that_axis() {
for mode in MODES {
let mut app = app(mode);
shift_drag(&mut app, &[[100.0, 200.0], [200.0, 230.0], [300.0, 260.0]]);
assert!(erased(&app, 100, 200) && erased(&app, 200, 200) && erased(&app, 299, 200), "{mode}: the horizontal band");
assert!(!erased(&app, 200, 230) && !erased(&app, 299, 259), "{mode}: nothing towards the diagonal");
let mut app = self::app(mode);
shift_drag(&mut app, &[[500.0, 100.0], [520.0, 250.0], [540.0, 400.0]]);
assert!(erased(&app, 500, 100) && erased(&app, 500, 250) && erased(&app, 500, 399), "{mode}: the vertical band");
assert!(!erased(&app, 520, 250) && !erased(&app, 539, 399), "{mode}: nothing towards the diagonal");
}
}
#[test]
fn a_near_diagonal_shift_drag_erases_no_45_degree_band() {
for mode in MODES {
// 42° below the horizontal: the wider axis wins.
let mut app = app(mode);
shift_drag(&mut app, &[[100.0, 100.0], [150.0, 145.0], [200.0, 190.0]]);
assert!(erased(&app, 150, 100) && erased(&app, 199, 100), "{mode}: the horizontal band");
assert!(!erased(&app, 150, 150) && !erased(&app, 190, 190), "{mode}: no diagonal band");
// 48°: the taller axis wins.
let mut app = self::app(mode);
shift_drag(&mut app, &[[300.0, 100.0], [345.0, 150.0], [390.0, 200.0]]);
assert!(erased(&app, 300, 150) && erased(&app, 300, 199), "{mode}: the vertical band");
assert!(!erased(&app, 350, 150) && !erased(&app, 390, 199), "{mode}: no diagonal band");
}
}
#[test]
fn the_axis_comes_from_the_first_movement_and_is_kept() {
for mode in MODES {
let mut app = app(mode);
shift_drag(&mut app, &[[100.0, 200.0], [150.0, 200.0], [150.0, 275.0], [150.0, 350.0]]);
assert!(erased(&app, 100, 200) && erased(&app, 149, 200), "{mode}: the horizontal band");
assert!(!erased(&app, 150, 275) && !erased(&app, 150, 349), "{mode}: no vertical band");
}
}
#[test]
fn an_exact_45_degree_shift_drag_erases_only_its_start() {
for mode in MODES {
let mut app = app(mode);
shift_drag(&mut app, &[[100.0, 100.0], [175.0, 175.0], [250.0, 250.0]]);
assert!(erased(&app, 100, 100), "{mode}: the start");
for (x, y) in [(140, 140), (175, 175), (249, 249), (175, 100), (100, 175)] {
assert!(!erased(&app, x, y), "{mode}: ({x}, {y}) stays");
}
}
}
+16 -11
View File
@@ -3,10 +3,10 @@
//! - ⇧ while dragging a painting stroke keeps it on a straight line at 0°, 45° or 90° from where
//! the constraint began (the stroke's start, or the point where ⇧ went down mid-stroke). The
//! direction locks once the pointer has moved a few screen points.
//! - ⇧ while dragging with the **Brush** only ever goes horizontal or vertical (#11889): the axis
//! that dominates the movement is chosen, and a drag that stays exactly on the 45° diagonal
//! paints nothing but its start dab. The other painting tools and the Freeform Pen keep the
//! 45° snap above.
//! - ⇧ while dragging with the **Brush** (#11889) or the **Eraser** in any mode (#17668) only ever
//! goes horizontal or vertical: the axis that dominates the movement is chosen, and a drag that
//! stays exactly on the 45° diagonal paints nothing but its start dab. The other painting tools
//! keep the 45° snap above.
//! - ⇧-click after a stroke paints a straight line from the end of the previous stroke to the
//! click. The line is part of the new stroke, so it is one `paint.stroke` and one undo step.
//! The endpoint remembers pen pressure so size and opacity interpolate between the strokes.
@@ -59,11 +59,12 @@ pub enum Snap {
Axis,
}
/// The ⇧ drag lock `tool` uses. The Brush takes the reference application's horizontal/vertical
/// lock (#11889). The Freeform Pen keeps its 45° snap (#7304); the other painting tools keep
/// #178's behaviour, which has not been measured against the reference.
/// The ⇧ drag lock `tool` uses. The Brush (#11889) and the Eraser in all its modes (#17668) take
/// the reference application's horizontal/vertical lock. The Freeform Pen keeps its 45° snap
/// (#7304); the other painting tools keep #178's behaviour, which has not been measured against
/// the reference.
pub fn snap_mode(tool: Tool) -> Snap {
if tool == Tool::Brush { Snap::Axis } else { Snap::Degrees45 }
if matches!(tool, Tool::Brush | Tool::Eraser) { Snap::Axis } else { Snap::Degrees45 }
}
/// The unit direction from `origin` towards `p`, snapped to the nearest multiple of `step`
@@ -118,8 +119,8 @@ pub fn snap45(origin: [f64; 2], p: [f64; 2]) -> [f64; 2] {
/// Constrain the pointer at `p` for a drag of `tool` that started at `start` and has reached
/// `last`. `axis` carries the stroke's lock between events; `zoom` converts the lock distance
/// to document pixels. The lock follows the tool's [`snap_mode`]: horizontal/vertical for the
/// Brush, a multiple of 45° for the other ⇧-constrained drags. Without ⇧ the point passes
/// through and the lock is dropped.
/// Brush and the Eraser, a multiple of 45° for the other ⇧-constrained drags. Without ⇧ the
/// point passes through and the lock is dropped.
pub fn constrain(tool: Tool, axis: &mut Option<Axis>, start: [f64; 2], last: [f64; 2], p: [f64; 2], shift: bool, zoom: f32) -> [f64; 2] {
if !shift || !applies(tool) {
*axis = None;
@@ -209,7 +210,7 @@ mod tests {
let mut axis = None;
assert!(close(p([40.0, 43.0], &mut axis), [10.0, 43.0]), "the Brush's near-diagonal goes vertical");
// Every other ⇧-constrained drag keeps the 45° lock (#178).
for tool in [Tool::Pencil, Tool::Eraser] {
for tool in [Tool::Pencil, Tool::BackgroundEraser] {
let mut axis = None;
let q = constrain(tool, &mut axis, o, o, [40.0, 43.0], true, 1.0);
assert!((q[0] - 10.0 - (q[1] - 10.0)).abs() < 1e-9, "{tool:?} should lock to 45°: {q:?}");
@@ -462,3 +463,7 @@ mod tests {
assert_eq!(line_preview_start(&app, Tool::Brush, true), None);
}
}
#[cfg(test)]
#[path = "eraser_shift_drag_axis_tests.rs"]
mod eraser_shift_drag_axis_tests;