diff --git a/crates/engine-core/src/cmd/channel_cmds/selection.rs b/crates/engine-core/src/cmd/channel_cmds/selection.rs index f5b1dc3f30f..7a87df36b7c 100644 --- a/crates/engine-core/src/cmd/channel_cmds/selection.rs +++ b/crates/engine-core/src/cmd/channel_cmds/selection.rs @@ -175,12 +175,16 @@ pub(super) fn quick_mask(s: &mut Session, p: &Value) -> Result { st.channel_view.quick_mask_hidden = false; } } else { - s.edit("Exit Quick Mask", |doc, _| { - if let Some(q) = doc.quick_mask.take() { - doc.selection = selection_from(doc, &q); - } - Ok(()) + // A channel that selects nothing, after entering with a selection, leaves "no pixels + // selected"; the reply says so for the UI's warning (#17492). + let no_pixels = s.edit("Exit Quick Mask", |doc, _| { + let Some(q) = doc.quick_mask.take() else { return Ok(false) }; + doc.selection = selection_from(doc, &q); + Ok(!doc.quick_mask_unselected && doc.selection.is_none() && uniform(&read_plane(&q.surface, doc.bounds()), 0.0)) })?; + if no_pixels { + return Ok(json!({ "quickMask": false, "noPixels": true })); + } } Ok(json!({ "quickMask": want })) } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 2e4a1071db5..30328b7616f 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1149,3 +1149,6 @@ mod place_visible_centre_tests; #[cfg(test)] mod file_info_categories_tests; + +#[cfg(test)] +mod quick_mask_exit_alert_tests; diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 3deb7c87090..7db099df4fc 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -129,6 +129,9 @@ fn invoke_early_commands(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, if id == "layer.frame.convert" && params.as_object().is_none_or(|o| o.is_empty()) { return Some(frame_convert_dialog(app, id)); } + if id == crate::panels::QUICK_MASK { + return Some(crate::panels::run_quick_mask(app, params.clone())); + } if id == "help.checkForUpdates" { return Some(crate::updates_ui::check(app, ctx, params.clone())); } diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index bea440a6d59..226ea1f08ca 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -78,10 +78,10 @@ pub use options_bar::options_bar; pub(crate) use status_bar::document_window_status; pub use status_bar::status_bar; pub use title_bar::{title_bar, window_title}; -pub(crate) use toolbar::QUICK_MASK; #[cfg(test)] use toolbar::color_chips; pub use toolbar::toolbar; +pub(crate) use toolbar::{QUICK_MASK, run_quick_mask}; /// Photoshop's Shift+Alt letter position of blend mode `m`; `None` for the modes without a letter (Darker Color, /// Lighter Color, Subtract, Divide) and for PhotoCraft's own extra modes. diff --git a/crates/ui-egui/src/panels/toolbar.rs b/crates/ui-egui/src/panels/toolbar.rs index eace041f508..df5fe3d80d5 100644 --- a/crates/ui-egui/src/panels/toolbar.rs +++ b/crates/ui-egui/src/panels/toolbar.rs @@ -157,6 +157,20 @@ pub(crate) fn cells(app: &PhotocraftApp) -> usize { /// The Edit in Quick Mask Mode command, which the rail's footer shows as a rail button. pub(crate) const QUICK_MASK: &str = "select.editInQuickMaskMode"; +/// The Quick Mask "Don't show again" for [`run_quick_mask`]'s warning. +pub(crate) const QUICK_MASK_NO_PIXELS_PREF: &str = "quickMask.noPixelsSelected"; + +/// Edit in Quick Mask Mode as the button, Q and the menu run it: leaving with a mask that selects nothing, after +/// entering with a selection, warns "No pixels are selected." with OK and "Don't show again", as the reference +/// application does (#17492). The command channel's `engine.execute` doesn't come here and stays non-modal. +pub(crate) fn run_quick_mask(app: &mut PhotocraftApp, params: serde_json::Value) -> Result { + let reply = app.run(QUICK_MASK, params)?; + if reply.get("noPixels").and_then(serde_json::Value::as_bool) == Some(true) { + let _ = crate::dialogs::alerts::alert_once(app, tl!("No pixels are selected."), QUICK_MASK_NO_PIXELS_PREF); + } + Ok(reply) +} + /// Quick Mask as a rail button, the size of the tool buttons: a click runs Edit in Quick Mask Mode, and the button shows /// on while the active document is in Quick Mask. fn quick_mask_button(app: &PhotocraftApp, ui: &mut egui::Ui) { diff --git a/crates/ui-egui/src/quick_mask_exit_alert_tests.rs b/crates/ui-egui/src/quick_mask_exit_alert_tests.rs new file mode 100644 index 00000000000..67be4d2974f --- /dev/null +++ b/crates/ui-egui/src/quick_mask_exit_alert_tests.rs @@ -0,0 +1,122 @@ +//! #17492: leaving Quick Mask with a mask that selects nothing, after entering with a selection, warns +//! "No pixels are selected." with OK and "Don't show again", from the Q key and the toolbar button. After +//! OK the document is out of Quick Mask with no selection and one "Exit Quick Mask" state. Entering +//! without a selection warns nothing, and the command channel stays non-modal. + +use egui::{Key, Modifiers}; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::shortcut_dispatch::tests::{app_harness, tap}; + +/// The whole app around a 200 x 100 white document, with 50,25-150,75 selected when `selected`. +fn harness(selected: bool) -> Harness<'static, Box> { + let mut s = photocraft_engine::Session::new(); + s.execute("file.new", json!({"width": 200, "height": 100, "background": "white"})).unwrap(); + if selected { + s.execute("select.rect", json!({"x": 50, "y": 25, "width": 100, "height": 50})).unwrap(); + } + app_harness(PhotocraftApp::new(s, crate::Services::default())) +} + +/// The open "Don't show again" alerts. +fn alerts(h: &Harness<'static, Box>) -> Vec { + crate::dialogs::alerts::alerts(h.state()) +} + +fn in_quick_mask(h: &Harness<'static, Box>) -> bool { + h.state().session.active().unwrap().doc.quick_mask.is_some() +} + +fn selected(h: &Harness<'static, Box>) -> bool { + h.state().session.active().unwrap().doc.selection.is_some() +} + +/// The History state count and the last state's name. +fn history(h: &Harness<'static, Box>) -> (usize, Option) { + let history = &h.state().session.active().unwrap().history; + (history.past_len(), history.undo_label().map(str::to_owned)) +} + +/// Enters Quick Mask with Q and fills the mask with `color`. +fn enter_and_fill(h: &mut Harness<'static, Box>, color: &str) { + tap(h, Key::Q, Modifiers::NONE); + assert!(in_quick_mask(h), "Q enters Quick Mask"); + h.state_mut().run("edit.fill", json!({"color": color})).unwrap(); + h.run_steps(2); +} + +/// Clicks the toolbar's Quick Mask button. +fn click_quick_mask_button(h: &mut Harness<'static, Box>) { + h.get_by_label_contains("Edit in Quick Mask Mode").click(); + h.run_steps(3); +} + +/// The alert is up with "Don't show again"; OK closes it and leaves the exit as it was. +fn ok_leaves_no_selection(h: &mut Harness<'static, Box>, before: usize) { + assert_eq!(alerts(h).len(), 1, "the alert"); + assert!(crate::dialogs::alerts::prompt_tagged::(h.state()).is_some(), "with Don't show again"); + assert!(h.query_by_label_contains("No pixels are selected.").is_some(), "the alert says no pixels are selected"); + let ctx = h.state().ctx().clone(); + let id = alerts(h)[0]; + assert!(h.state_mut().framework.dialogs.confirm_by_id(&ctx, id)); + h.run_steps(3); + assert!(alerts(h).is_empty(), "OK closes it"); + assert!(!in_quick_mask(h), "out of Quick Mask"); + assert!(!selected(h), "no selection"); + assert_eq!(history(h), (before + 1, Some("Exit Quick Mask".into())), "one Exit Quick Mask state"); +} + +#[test] +fn q_with_an_all_black_mask_warns() { + let mut h = harness(true); + enter_and_fill(&mut h, "#000000"); + let (before, _) = history(&h); + tap(&mut h, Key::Q, Modifiers::NONE); + ok_leaves_no_selection(&mut h, before); +} + +#[test] +fn the_toolbar_button_with_an_all_black_mask_warns() { + let mut h = harness(true); + enter_and_fill(&mut h, "#000000"); + let (before, _) = history(&h); + click_quick_mask_button(&mut h); + ok_leaves_no_selection(&mut h, before); +} + +#[test] +fn no_alert_without_a_selection_on_entering_or_with_pixels_left() { + // (selection on entering, fill, selection after leaving) + for (entered_selected, contents, keeps) in [(false, "#000000", false), (true, "#808080", true)] { + let mut h = harness(entered_selected); + enter_and_fill(&mut h, contents); + tap(&mut h, Key::Q, Modifiers::NONE); + assert!(alerts(&h).is_empty(), "{entered_selected} {contents}: no alert"); + assert!(!in_quick_mask(&h), "{entered_selected} {contents}: left Quick Mask"); + assert_eq!(selected(&h), keeps, "{entered_selected} {contents}: selection"); + } +} + +#[test] +fn dont_show_again_leaves_quietly() { + let mut h = harness(true); + crate::dialogs::alerts::remember_skip(h.state_mut(), crate::panels::toolbar::QUICK_MASK_NO_PIXELS_PREF); + enter_and_fill(&mut h, "#000000"); + tap(&mut h, Key::Q, Modifiers::NONE); + assert!(alerts(&h).is_empty(), "the box was ticked: no alert"); + assert!(!in_quick_mask(&h) && !selected(&h), "still out of Quick Mask with no selection"); +} + +#[test] +fn the_command_channel_stays_non_modal() { + let mut h = harness(true); + enter_and_fill(&mut h, "#000000"); + let reply = h.state_mut().run(crate::panels::QUICK_MASK, json!({})).unwrap(); + h.run_steps(2); + assert_eq!(reply["noPixels"], true, "the reply says no pixels are selected"); + assert!(alerts(&h).is_empty(), "no dialog"); + assert!(!in_quick_mask(&h) && !selected(&h)); +}