From 8824661f478daf78d3cce59fcc790ad1aa29a2f4 Mon Sep 17 00:00:00 2001 From: rmsff <17902130+rmsff@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:08:14 +0300 Subject: [PATCH 1/2] Make Selection: Rendering and Operation groups; the dialog's step is Selection Change (#12917) The Pen's Make Selection dialog (canvas menu, options bar) now has Photoshop's layout: Rendering with Feather Radius in pixels and Anti-aliased, then Operation with New / Add to / Subtract from / Intersect with Selection as radio buttons. The last three are greyed out without a selection. The fields and params are the same as before. OK with New Selection names the step it added "Selection Change". Cmd/Ctrl+Enter, the Paths panel and the combining operations keep "Make Selection" (not measured). --- crates/ui-egui/src/canvas/canvas_tool_menu.rs | 58 +++++++++++++++++++ crates/ui-egui/src/dialogs.rs | 7 +++ 2 files changed, 65 insertions(+) diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index a3bbe242943..f544ebd6b00 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -1261,6 +1261,7 @@ fn pen_dialog(app: &mut PhotocraftApp, command: &str, label: &str, name: &str) { if command == "path.toSelection" { // OK deselects and hides the path, as ⌘↩ does (#6079). fields.insert("__deselectPath".into(), json!(true)); + fields.insert(MAKE_SELECTION.into(), json!(true)); } if command == "path.fill" { let fg = app.session.tools.foreground; @@ -1281,6 +1282,63 @@ fn pen_dialog(app: &mut PhotocraftApp, command: &str, label: &str, name: &str) { spec_dialog(app, command, label, spec, fields); } +/// Marks the Make Selection dialog [`pen_dialog`] opens. It keeps the schema dialog's fields, so OK +/// and Alt+Reset work as before; only the body is Photoshop's layout (#12917). +const MAKE_SELECTION: &str = "__makeSelection"; + +/// The Make Selection operations, in Photoshop's order: (`path.toSelection` mode, label). +const MAKE_SELECTION_OPERATIONS: [(&str, &str); 4] = + [("replace", "New Selection"), ("add", "Add to Selection"), ("subtract", "Subtract from Selection"), ("intersect", "Intersect with Selection")]; + +/// Is this dialog the Make Selection dialog? +pub(crate) fn owns_make_selection(fields: &serde_json::Map) -> bool { + fields.contains_key(MAKE_SELECTION) +} + +/// Photoshop's Make Selection dialog: Rendering (Feather Radius, Anti-aliased) and Operation. +/// Without a selection there is nothing to combine with, so only New Selection is available. +pub(crate) fn make_selection_body(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut serde_json::Map) { + let has_selection = app.session.active().is_some_and(|st| st.doc.selection.is_some()); + ui.spacing_mut().item_spacing.y = 6.0; + crate::widgets::section_label(ui, tl!("Rendering")); + crate::widgets::hairline(ui); + ui.horizontal(|ui| { + ui.label(tl!("Feather Radius:")); + let mut feather = f.get("feather").and_then(Value::as_f64).unwrap_or(0.0) as f32; + crate::widgets::value_field(ui, &mut feather, 0.0..=250.0, "", 64.0).accessible_name(tl!("Feather Radius:")); + ui.label(tl!("pixels")); + // The schema dialog's rounding, so OK sends what it sent. + f.insert("feather".into(), json!((feather * 10.0).round() / 10.0)); + }); + let mut anti_alias = f.get("antiAlias").and_then(Value::as_bool).unwrap_or(true); + crate::widgets::checkbox(ui, &mut anti_alias, tl!("Anti-aliased")); + f.insert("antiAlias".into(), json!(anti_alias)); + ui.add_space(4.0); + crate::widgets::section_label(ui, tl!("Operation")); + crate::widgets::hairline(ui); + let mut mode = f.get("mode").and_then(Value::as_str).unwrap_or("replace").to_string(); + // A combining mode set from outside, or a selection gone while the dialog was open. + if !has_selection { + mode = "replace".into(); + } + for (key, label) in MAKE_SELECTION_OPERATIONS { + ui.add_enabled_ui(key == "replace" || has_selection, |ui| ui.radio_value(&mut mode, key.to_string(), tl!(label))); + } + f.insert("mode".into(), json!(mode)); +} + +/// OK in the Make Selection dialog: a new selection is the History step Selection Change, as in +/// Photoshop (#12917). ⌘↩, the Paths panel and the combining operations keep the engine's Make +/// Selection (not measured). Only a step this run added is renamed: `revision` is from before it. +pub(crate) fn name_make_selection_step(app: &mut PhotocraftApp, fields: &serde_json::Map, params: &Value, revision: Option) { + if !owns_make_selection(fields) || params.get("mode").and_then(Value::as_str).is_some_and(|m| m != "replace") { + return; + } + if let Some(st) = app.session.active_mut().filter(|st| Some(st.revision) != revision) { + st.history.set_current_label("Selection Change"); + } +} + /// The Pen options bar's Make Selection (Path mode, #5703): the canvas menu's Make Selection… /// dialog for the path being drawn (committed first, as the menu does) or the active path. pub(crate) fn make_selection(app: &mut PhotocraftApp) -> Result<(), String> { diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 4ab8a2eeb3f..abc59bcfa72 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -357,6 +357,9 @@ fn command_body_more(app: &mut PhotocraftApp, ui: &mut egui::Ui, d: &Dialog, fie _ if crate::smart_filter_dialog::owns(fields) => crate::smart_filter_dialog::body(app, ui, fields), _ if crate::adjust_dialog::owns(fields) => crate::adjust_dialog::body(app, ui, fields), _ if crate::blur_pins::owns(fields) => crate::blur_pins::body(ui, fields), + _ if crate::canvas_tool_menu::owns_make_selection(fields) => { + first_field_focused(ui, id, |ui| crate::canvas_tool_menu::make_selection_body(app, ui, fields)) + } _ if fields.contains_key("__filter") => filter_body(app, ui, d, fields, id), _ if crate::new_guide_ui::owns(fields) => crate::new_guide_ui::body(ui, fields), _ if crate::equalize_ui::owns(fields) => crate::equalize_ui::body(ui, fields), @@ -1008,6 +1011,7 @@ fn confirm_generic_command(app: &mut PhotocraftApp, d: Dialog) -> Result Result p.committing = true, _ => app.filter_preview = None, } + if result.is_ok() && cmd == "path.toSelection" { + crate::canvas_tool_menu::name_make_selection_step(app, &d.fields, ¶ms, revision); + } if result.is_ok() && cmd == "path.toSelection" && d.fields.contains_key("__deselectPath") From 8122d83e7ccedee7654a29dc95acaf41578e26de Mon Sep 17 00:00:00 2001 From: rmsff <17902130+rmsff@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:08:14 +0300 Subject: [PATCH 2/2] Test the Make Selection dialog's groups, greyed operations and History step (#12917) --- crates/ui-egui/src/canvas/canvas_tool_menu.rs | 3 + .../make_selection_dialog_tests.rs | 210 ++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 crates/ui-egui/src/canvas/canvas_tool_menu/make_selection_dialog_tests.rs diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index f544ebd6b00..17393483f1a 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -1497,3 +1497,6 @@ mod hand_menu_entries_tests; #[cfg(test)] mod pen_free_transform_label_tests; + +#[cfg(test)] +mod make_selection_dialog_tests; diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/make_selection_dialog_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/make_selection_dialog_tests.rs new file mode 100644 index 00000000000..183303632a3 --- /dev/null +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/make_selection_dialog_tests.rs @@ -0,0 +1,210 @@ +//! #12917: Make Selection from a path is Photoshop's dialog: Rendering (Feather Radius in pixels, +//! Anti-aliased) and Operation (New / Add to / Subtract from / Intersect with Selection, the last +//! three greyed without a selection). OK with the defaults selects inside the path in one +//! Selection Change History step; each Operation runs `path.toSelection` with its mode. ⌘↩ and the +//! combining operations keep today's Make Selection step (not measured). + +use egui::accesskit::{Role, Toggled}; +use egui_kittest::kittest::{NodeT, Queryable}; +use egui_kittest::{Harness, Node}; +use serde_json::{Value, json}; + +use super::make_selection; +use super::tests::choose_open; +use crate::PhotocraftApp; +use crate::state::Tool; + +const OPERATIONS: [&str; 4] = ["New Selection", "Add to Selection", "Subtract from Selection", "Intersect with Selection"]; +const MODES: [&str; 4] = ["replace", "add", "subtract", "intersect"]; + +/// A document with the reference's closed triangle as the Work Path, with or without a selection +/// (a rectangle the triangle overlaps). +fn app(selection: bool) -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 500})).unwrap(); + let knots = json!([[100, 300], [300, 300], [200, 450]]); + app.run("path.set", json!({"name": "work", "path": {"subpaths": [{"closed": true, "knots": knots}]}})).unwrap(); + if selection { + app.run("select.rect", json!({"x": 20, "y": 20, "width": 200, "height": 320})).unwrap(); + } + app.ui.tool = Tool::Pen; + app +} + +fn draw(ui: &mut egui::Ui, app: &mut PhotocraftApp) { + if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + crate::dialogs::show(app, ui.ctx()); + } +} + +/// The Pen canvas menu's Make Selection… dialog, drawn as the window shows it. +fn dialog(mut app: PhotocraftApp) -> Harness<'static, PhotocraftApp> { + choose_open(&mut app, "path.toSelection"); + assert_eq!(app.ui.dialogs.len(), 1, "Make Selection… opened its dialog"); + let mut h = Harness::builder().with_size(egui::vec2(900.0, 700.0)).build_ui_state(draw, app); + PhotocraftApp::setup_context(&h.ctx, Default::default()); + h.run_steps(4); + h +} + +fn click(h: &mut Harness<'_, PhotocraftApp>, label: &str) { + h.get_by_label(label).click(); + h.run_steps(3); +} + +fn steps(app: &PhotocraftApp) -> Vec { + app.session.active().unwrap().history.entries().iter().map(|e| e.to_string()).collect() +} + +fn last_step(app: &PhotocraftApp) -> Option { + steps(app).last().cloned() +} + +fn selection_bounds(app: &PhotocraftApp) -> Option<[i32; 4]> { + let r = app.session.active().unwrap().doc.selection.as_ref()?.content_bounds(); + Some([r.x0, r.y0, r.x1, r.y1]) +} + +/// The params of the last command run. +fn last_params(app: &PhotocraftApp) -> Value { + let (id, params) = app.session.journal.last().cloned().unwrap(); + assert_eq!(id, "path.toSelection"); + params +} + +fn enabled(h: &Harness<'_, PhotocraftApp>, label: &str) -> bool { + !h.get_by_label(label).accesskit_node().is_disabled() +} + +fn feather_field<'h>(h: &'h Harness<'_, PhotocraftApp>) -> Node<'h> { + h.get_all_by_label("Feather Radius:").find(|n| n.accesskit_node().role() == Role::SpinButton).expect("the Feather Radius number field") +} + +#[test] +fn the_dialog_shows_the_rendering_and_operation_groups() { + let h = dialog(app(false)); + for label in ["Make Selection", "Rendering", "pixels", "Anti-aliased", "Operation", "OK", "Cancel"] { + assert!(h.query_by_label(label).is_some(), "{label} is shown"); + } + for old in ["Feather", "Anti Alias", "Mode"] { + assert!(h.query_by_label(old).is_none(), "the schema dialog's {old} is gone"); + } + assert_eq!(h.get_by_label("Anti-aliased").accesskit_node().toggled(), Some(Toggled::True), "Anti-aliased is on"); + assert_eq!(h.get_by_label("New Selection").accesskit_node().toggled(), Some(Toggled::True), "New Selection is chosen"); + assert_eq!(feather_field(&h).accesskit_node().numeric_value(), Some(0.0), "Feather Radius is 0"); + let tops: Vec = OPERATIONS.iter().map(|label| h.get_by_label(label).rect().top()).collect(); + assert!(tops.windows(2).all(|w| w[0] < w[1]), "the operations are listed in order: {tops:?}"); + let rendering = h.get_by_label("Rendering").rect().top(); + let operation = h.get_by_label("Operation").rect().top(); + let feather = feather_field(&h).rect().top(); + assert!(rendering < feather && feather < operation && operation < tops[0], "Rendering holds Feather Radius, Operation the radio buttons"); +} + +#[test] +fn the_options_bar_button_opens_the_same_dialog() { + let mut app = app(false); + make_selection(&mut app).unwrap(); + let fields = &app.ui.dialogs.last().unwrap().fields; + assert!(super::owns_make_selection(fields)); + assert_eq!(fields.get("__command"), Some(&json!("path.toSelection"))); + assert!(fields.contains_key("__deselectPath"), "OK deselects the path, as from the canvas menu"); +} + +#[test] +fn without_a_selection_only_new_selection_is_available() { + let h = dialog(app(false)); + assert!(enabled(&h, "New Selection")); + for label in &OPERATIONS[1..] { + assert!(!enabled(&h, label), "{label} is greyed out without a selection"); + } +} + +#[test] +fn with_a_selection_every_operation_is_available() { + let h = dialog(app(true)); + for label in OPERATIONS { + assert!(enabled(&h, label), "{label} is available with a selection"); + } +} + +/// A combining mode the dialog was given (by automation, or before the selection went) falls +/// back to New Selection when there is nothing to combine with. +#[test] +fn without_a_selection_a_combining_mode_falls_back_to_new_selection() { + let mut h = dialog(app(false)); + h.state_mut().ui.dialogs.last_mut().unwrap().fields.insert("mode".into(), json!("subtract")); + h.run_steps(2); + assert_eq!(h.get_by_label("New Selection").accesskit_node().toggled(), Some(Toggled::True)); + click(&mut h, "OK"); + assert_eq!(last_params(h.state()).get("mode"), Some(&json!("replace"))); + assert_eq!(selection_bounds(h.state()), Some([100, 300, 300, 450])); +} + +#[test] +fn ok_with_the_defaults_selects_the_triangle_in_one_selection_change_step() { + let mut h = dialog(app(false)); + let before = steps(h.state()); + click(&mut h, "OK"); + assert!(h.state().ui.dialogs.is_empty(), "OK closed the dialog"); + assert_eq!(selection_bounds(h.state()), Some([100, 300, 300, 450])); + let after = steps(h.state()); + assert_eq!(after.len(), before.len() + 1, "one History step: {after:?}"); + assert_eq!(after[..before.len()], before[..], "the earlier steps keep their names"); + assert_eq!(after.last().map(String::as_str), Some("Selection Change")); + assert_eq!(h.state().ui.selected_path, None, "OK deselects the path, as ⌘↩ does"); +} + +#[test] +fn each_operation_runs_make_selection_with_its_mode() { + for (label, mode) in OPERATIONS.into_iter().zip(MODES) { + let mut h = dialog(app(true)); + click(&mut h, label); + click(&mut h, "OK"); + assert_eq!(last_params(h.state()).get("mode").and_then(Value::as_str), Some(mode), "{label}"); + let want = if mode == "replace" { "Selection Change" } else { "Make Selection" }; + assert_eq!(last_step(h.state()).as_deref(), Some(want), "{label}: the combining operations keep today's step name"); + } +} + +#[test] +fn a_typed_feather_radius_is_sent() { + let mut h = dialog(app(false)); + feather_field(&h).click(); + h.run_steps(1); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.event(egui::Event::Text("4".into())); + h.key_press(egui::Key::Tab); + h.run_steps(3); + click(&mut h, "OK"); + assert_eq!(last_params(h.state()).get("feather").and_then(Value::as_f64), Some(4.0)); +} + +#[test] +fn cancel_changes_nothing() { + let mut h = dialog(app(true)); + let (selection, before) = (selection_bounds(h.state()), steps(h.state())); + click(&mut h, "Intersect with Selection"); + click(&mut h, "Cancel"); + assert!(h.state().ui.dialogs.is_empty(), "Cancel closed the dialog"); + assert_eq!(selection_bounds(h.state()), selection); + assert_eq!(steps(h.state()), before); +} + +/// ⌘↩ runs the same command without the dialog: its step keeps today's name (not measured). +#[test] +fn cmd_enter_keeps_the_make_selection_step() { + let mut app = app(false); + crate::vector_ui::path_to_selection(&mut app, json!({})).unwrap(); + assert_eq!(selection_bounds(&app), Some([100, 300, 300, 450])); + assert_eq!(last_step(&app).as_deref(), Some("Make Selection")); +} + +/// OK that adds no step (the path went while the dialog was open) renames nothing. +#[test] +fn ok_that_fails_does_not_rename_the_previous_step() { + let mut h = dialog(app(true)); + h.state_mut().run("path.delete", json!({"name": "work"})).unwrap(); + let before = steps(h.state()); + click(&mut h, "OK"); + assert_eq!(steps(h.state()), before); +}