Brush: an idle Escape keeps the selection (#17609)

With the Brush tool and no stroke, Escape no longer deselects and adds
no History state, as in the reference application. It still resets a
rotated view and leaves full screen first; other tools keep the idle
Escape deselect (#2674).
This commit is contained in:
qvest-ssels committed 2026-11-29 11:03:50 +01:00
1 parent a3288779f1
commit 6f36e86cb0
3 files changed
+85 -2

No files matched your search

@@ -0,0 +1,72 @@
//! #17609: with the Brush tool and no stroke, Escape leaves the selection alone and adds no
//! History state. It still resets a rotated view and leaves a full screen mode first; other tools
//! keep the idle Escape deselect (#2674).
use egui::{Key, vec2};
use egui_kittest::Harness;
use serde_json::json;
use crate::PhotocraftApp;
use crate::state::Tool;
/// A 600x500 document with a selection of 100,100 - 500,400 and `tool` active.
fn harness(tool: Tool) -> Harness<'static, Box<PhotocraftApp>> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 600, "height": 500})).unwrap();
app.run("select.rect", json!({"x": 100, "y": 100, "width": 400, "height": 300})).unwrap();
app.ui.tool = tool;
app.sync_views();
let ctx = app.ctx().clone();
let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_context(ctx).with_max_steps(64).build_eframe(move |_cc| app);
h.run_steps(4);
h
}
fn selection_bounds(h: &Harness<'static, Box<PhotocraftApp>>) -> Option<photocraft_geom::Rect> {
h.state().session.active().unwrap().doc.selection.as_ref().map(|m| m.content_bounds())
}
fn history(h: &Harness<'static, Box<PhotocraftApp>>) -> usize {
h.state().session.active().unwrap().history.entries().len()
}
fn escape(h: &mut Harness<'static, Box<PhotocraftApp>>) {
h.key_press(Key::Escape);
h.run_steps(2);
}
const SELECTED: photocraft_geom::Rect = photocraft_geom::Rect { x0: 100, y0: 100, x1: 500, y1: 400 };
#[test]
fn brush_escape_keeps_the_selection_and_adds_no_history_state() {
let mut h = harness(Tool::Brush);
let steps = history(&h);
escape(&mut h);
assert_eq!(selection_bounds(&h), Some(SELECTED), "the selection stays");
assert_eq!(history(&h), steps, "no Deselect state");
escape(&mut h);
assert_eq!(selection_bounds(&h), Some(SELECTED), "a second Escape changes nothing either");
assert!(!h.state().ui.status_error, "no error: {}", h.state().ui.status);
}
#[test]
fn brush_escape_still_resets_a_rotated_view_and_leaves_full_screen() {
let mut h = harness(Tool::Brush);
h.state_mut().ui.views[0].rotation = 40.0;
escape(&mut h);
assert_eq!(h.state().ui.views[0].rotation, 0.0, "the rotated view is reset");
h.state_mut().ui.view.screen_mode = craft::ui::shell::ScreenMode::FullScreen;
h.run_steps(2);
escape(&mut h);
assert_eq!(h.state().ui.view.screen_mode, craft::ui::shell::ScreenMode::Standard, "full screen is left");
assert_eq!(selection_bounds(&h), Some(SELECTED), "the selection stays");
}
#[test]
fn other_tools_still_deselect_on_escape() {
for tool in [Tool::Pencil, Tool::Move, Tool::RectMarquee] {
let mut h = harness(tool);
escape(&mut h);
assert_eq!(selection_bounds(&h), None, "{tool:?}: Escape deselects as before");
}
}
+10 -1
View File
@@ -359,7 +359,12 @@ fn handle_escape_and_view_keys(app: &mut PhotocraftApp, ctx: &egui::Context) {
app.ui.view.screen_mode = craft::ui::shell::ScreenMode::Standard;
return;
}
if !esc_taken && app.session.active().is_some_and(|st| st.doc.selection.is_some()) && crate::window_keys::take_escape(ctx) {
// The Brush leaves the selection alone on an idle Esc, as the reference application does (#17609).
if !esc_taken
&& app.ui.tool != Tool::Brush
&& app.session.active().is_some_and(|st| st.doc.selection.is_some())
&& crate::window_keys::take_escape(ctx)
{
if let Err(e) = app.run("select.deselect", json!({})) {
app.ui.status = e;
app.ui.status_error = true;
@@ -416,3 +421,7 @@ mod tests;
#[cfg(test)]
#[path = "tool_key_default_tests.rs"]
mod tool_key_default_tests;
#[cfg(test)]
#[path = "brush_escape_keeps_selection_tests.rs"]
mod brush_escape_keeps_selection_tests;
+3 -1
View File
@@ -680,7 +680,9 @@ fn escape_resets_rotation_before_idle_deselection_for_paint_tools() {
assert_eq!(h.state().session.active().unwrap().history.entries().len(), history, "view reset is not an edit");
h.key_press(Key::Escape);
h.run_steps(2);
assert!(h.state().session.active().unwrap().doc.selection.is_none(), "second Esc deselects normally");
// The Brush keeps the selection on an idle Esc (#17609); the Pencil deselects.
let kept = h.state().session.active().unwrap().doc.selection.is_some();
assert_eq!(kept, tool == crate::state::Tool::Brush, "{tool:?}: second Esc");
}
}