Merge pull request #18648

This commit is contained in:
autocraft-bot[bot] committed 2026-12-01 15:26:27 +00:00
commit ba9e98a32f
4 files changed
+150 -2

No files matched your search

+1 -1
View File
@@ -276,7 +276,7 @@ pub(crate) fn choose(app: &mut PhotocraftApp, item: Item) {
crate::brush_picker::open(app, at);
}
}
Item::Eraser(m) => app.ui.tool_options.eraser_mode = m.into(),
Item::Eraser(m) => crate::canvas::strokes::set_eraser_mode(app, m),
Item::Range(r) => app.ui.tool_options.tone_range = r.into(),
Item::Sponge(m) => app.ui.tool_options.sponge_mode = m.into(),
// Replace is a tool option: it runs no command and adds no History step (#12101).
@@ -0,0 +1,136 @@
//! #18630: Changing Eraser mode forgets the previous Shift-click endpoint.
use egui::accesskit::Role;
use egui::{Modifiers, vec2};
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use serde_json::json;
use std::ops::RangeInclusive;
use crate::PhotocraftApp;
use crate::app::framework::BuildApp as _;
use crate::brush_mode_menu::{self, Item};
use crate::canvas::testing;
use crate::state::Tool;
// Click inside the named pixels, away from the even-sized Block tip's nearest-corner tie.
const START: [f64; 2] = [100.75, 300.75];
const END: [f64; 2] = [300.75, 300.75];
fn document(mode: &str) -> Box<PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
app.background_jobs = false;
app.run("file.new", json!({"width": 800, "height": 600, "background": "white"})).unwrap();
app.run("layer.new.layer", json!({})).unwrap();
app.run("edit.fill", json!({"color": "#dc1e1e"})).unwrap();
app.run("tools.setBrush", json!({"brush": {"size": 16, "hardness": 1.0}})).unwrap();
app.session.tools.brush.smoothing.amount = 0.0;
app.ui.tool = Tool::Eraser;
app.ui.tool_options.eraser_mode = mode.into();
app.sync_views();
testing::shown(&mut app);
let scale = app.canvas_ppp();
let view = &mut app.ui.views[0];
view.zoom = scale;
view.clear_pending();
testing::shown(&mut app);
assert_eq!(app.point_zoom(), 1.0, "the Block tests use one canvas point per image pixel");
app
}
fn alpha(app: &PhotocraftApp, x: i32, y: i32) -> f32 {
let st = app.session.active().unwrap();
st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(x, y)[3]
}
fn block_pixels(app: &PhotocraftApp, xs: RangeInclusive<i32>) -> Vec<f32> {
(280..=320).flat_map(|y| xs.clone().map(move |x| alpha(app, x, y))).collect()
}
fn mode_dropdown(mode: &str) -> Harness<'static, Box<PhotocraftApp>> {
let mut h =
Harness::builder().with_size(vec2(1800.0, 300.0)).with_step_dt(1.0 / 60.0).build_app(|ui, app| crate::panels::options_bar(app, ui), document(mode));
h.run_steps(4);
h
}
fn choose_mode(h: &mut Harness<'_, Box<PhotocraftApp>>, label: &str) {
h.get_by_role_and_label(Role::ComboBox, "Mode").click();
h.run_steps(2);
h.get_by_label(label).click();
h.run_steps(3);
}
#[test]
fn changing_eraser_mode_in_the_dropdown_starts_a_separate_mark() {
let mut control = document("block");
testing::click(&mut control, END, Modifiers::NONE);
let block = block_pixels(&control, 280..=320);
let erased_x: Vec<_> = (280..=320).filter(|&x| alpha(&control, x, 300) == 0.0).collect();
let erased_y: Vec<_> = (280..=320).filter(|&y| alpha(&control, 300, y) == 0.0).collect();
assert_eq!((erased_x.len(), erased_y.len()), (16, 16));
assert_eq!(erased_x[15] - erased_x[0], 15);
assert_eq!(erased_y[15] - erased_y[0], 15);
for y in 280..=320 {
for x in 280..=320 {
let want = if erased_x.contains(&x) && erased_y.contains(&y) { 0.0 } else { 1.0 };
assert_eq!(alpha(&control, x, y), want, "the ordinary Block dab at ({x}, {y})");
}
}
let modes = [("brush", "Brush"), ("pencil", "Pencil"), ("block", "Block")];
for (from, _) in modes {
for (to, label) in modes.into_iter().filter(|(to, _)| *to != from) {
let mut h = mode_dropdown(from);
testing::click(h.state_mut(), START, Modifiers::NONE);
assert!(alpha(h.state(), 100, 300) < 0.1, "{from} marks the first click");
let history = h.state().session.active().unwrap().history.past_len();
choose_mode(&mut h, label);
assert_eq!(h.state().ui.tool_options.eraser_mode, to);
assert_eq!(h.state().session.active().unwrap().history.past_len(), history);
testing::click(h.state_mut(), END, Modifiers::SHIFT);
assert!(alpha(h.state(), 300, 300) < 0.1, "{from} to {to} marks the second click");
assert!((110..=290).all(|x| alpha(h.state(), x, 300) == 1.0), "{from} to {to} leaves the gap opaque");
if to == "block" {
assert!(block_pixels(h.state(), 280..=320) == block, "{from} to {to} matches the ordinary Block dab exactly");
}
}
}
}
#[test]
fn picking_the_current_eraser_mode_or_leaving_it_alone_keeps_the_line() {
let mut ordinary = None;
for pick_current_mode in [false, true] {
let mut h = mode_dropdown("block");
testing::click(h.state_mut(), START, Modifiers::NONE);
if pick_current_mode {
choose_mode(&mut h, "Block");
}
testing::click(h.state_mut(), END, Modifiers::SHIFT);
let erased_rows: Vec<_> = (280..=320).filter(|&y| alpha(h.state(), 200, y) == 0.0).collect();
assert_eq!(erased_rows.len(), 16);
assert_eq!(erased_rows[15] - erased_rows[0], 15);
assert!(erased_rows.iter().all(|&y| (100..=300).all(|x| alpha(h.state(), x, y) == 0.0)), "the same mode keeps the whole line");
let band = block_pixels(h.state(), 80..=320);
if let Some(ordinary) = &ordinary {
assert!(&band == ordinary, "reselecting Block preserves every pixel of the ordinary line");
} else {
ordinary = Some(band);
}
}
}
#[test]
fn choosing_a_different_eraser_mode_from_the_canvas_menu_also_forgets_the_endpoint() {
let mut app = document("brush");
testing::click(&mut app, START, Modifiers::NONE);
testing::secondary_click(&mut app, START, Modifiers::SHIFT);
assert!(app.ui.canvas_tool_menu.is_some(), "Shift-right-click opens the canvas menu");
brush_mode_menu::choose(&mut app, Item::Eraser("block"));
assert_eq!(app.ui.tool_options.eraser_mode, "block");
testing::click(&mut app, END, Modifiers::SHIFT);
assert_eq!(alpha(&app, 300, 300), 0.0);
assert_eq!(alpha(&app, 200, 300), 1.0);
}
+12
View File
@@ -867,3 +867,15 @@ pub(crate) fn multiple_color_target(app: &PhotocraftApp) -> bool {
&& photocraft_engine::mask_view_cmds::current(st).is_none()
&& photocraft_engine::color_channels::selected(&app.session).is_some_and(|colors| colors.len() > 1)
}
/// A real Eraser mode change starts a new sequence of Shift-click strokes.
pub(crate) fn set_eraser_mode(app: &mut PhotocraftApp, mode: &str) {
if app.ui.tool_options.eraser_mode != mode {
app.ui.tool_options.eraser_mode = mode.into();
app.last_stroke_end = None;
}
}
#[cfg(test)]
#[path = "eraser_shift_click_mode_change_tests.rs"]
mod eraser_shift_click_mode_change_tests;
@@ -305,7 +305,7 @@ pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) ->
apply_values(app, tool, &mut brush, &before, &after);
crate::brush_panel::commit_keyed(app, &brush_before, &brush, key);
}
PaintEvent::EraserMode(mode) => app.ui.tool_options.eraser_mode = eraser_mode_name(mode).into(),
PaintEvent::EraserMode(mode) => crate::canvas::strokes::set_eraser_mode(app, eraser_mode_name(mode)),
PaintEvent::MixerLoad => {
let result = app.run("brush.mixer.load", json!({}));
report(app, result);