PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Tools: double-clicking a layer mask launches Select and Mask with preference (#204) (#2161)
Krzysztof Cieślika0c18f5f756e
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rsindex 06e68e17a..255615625 100644--- a/crates/engine/src/prefs.rs+++ b/crates/engine/src/prefs.rs@@ -859,7 +859,6 @@ pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &["tools.enableFlickPanning","tools.varyRoundBrushHardnessOnHud","tools.showTransformationValues",- "tools.doubleClickLayerMaskLaunchesSelectAndMask","fileHandling.imagePreviews","fileHandling.saveInBackground","fileHandling.ignoreExifProfileTag",diff --git a/crates/engine/src/smartselect_cmds.rs b/crates/engine/src/smartselect_cmds.rsindex d88982190..5871ad609 100644--- a/crates/engine/src/smartselect_cmds.rs+++ b/crates/engine/src/smartselect_cmds.rs@@ -16,8 +16,17 @@ use crate::{EngineError, Result, Session};fn has_doc(s: &Session) -> std::result::Result<(), String> {s.active().map(|_| ()).ok_or_else(|| "no document open".into())}-fn has_selection(s: &Session) -> std::result::Result<(), String> {- s.active().filter(|d| d.doc.selection.is_some()).map(|_| ()).ok_or_else(|| "no selection".into())+fn has_selection_or_mask(s: &Session) -> std::result::Result<(), String> {+ let d = s.active().ok_or_else(|| "no document open".to_string())?;+ if d.doc.selection.is_some() {+ return Ok(());+ }+ if let Some(l) = d.active_layer.and_then(|id| d.doc.layer(id))+ && l.mask.is_some()+ {+ return Ok(());+ }+ Err("no selection or layer mask".into())}fn f(p: &Value, k: &str, d: f32) -> f32 {@@ -157,7 +166,11 @@ fn refine_edge(s: &mut Session, p: &Value) -> Result<Value> {let params = refine_params(p);let decontaminate = b(p, "decontaminate", false);let amount = f(p, "amount", 100.0).clamp(0.0, 100.0);- let mut output = p.get("output").and_then(Value::as_str).unwrap_or("selection").to_string();+ let d = s.active().ok_or(EngineError::NoDocument)?;+ let active = d.active_layer.filter(|id| d.doc.layer(*id).is_some());+ let default_output =+ if d.doc.selection.is_none() && active.and_then(|id| d.doc.layer(id)).and_then(|l| l.mask.as_ref()).is_some() { "layerMask" } else { "selection" };+ let mut output = p.get("output").and_then(Value::as_str).unwrap_or(default_output).to_string();if !matches!(output.as_str(), "selection" | "layerMask" | "newLayer" | "newLayerWithMask") {return Err(bad("select.refineEdge", "output must be selection|layerMask|newLayer|newLayerWithMask"));}@@ -165,7 +178,8 @@ fn refine_edge(s: &mut Session, p: &Value) -> Result<Value> {output = "newLayerWithMask".into();}let region = with_sampler(s, b(p, "sampleAllLayers", false), |smp, doc| {- let sel = doc.selection.as_ref()?;+ let mask_surf = active.and_then(|id| doc.layer(id)).and_then(|l| l.mask.as_ref()).map(|m| &m.surface);+ let sel = doc.selection.as_ref().or(mask_surf)?;matting::refine_mask(smp, &matting::surface_reader(sel), sel.content_bounds(), doc.bounds(), ¶ms)})?;let d = s.active().ok_or(EngineError::NoDocument)?;@@ -259,7 +273,7 @@ pub fn specs() -> Vec<CommandSpec> {"Refine Edge",[],r##"{"radius":px=0,"smartRadius":bool=false,"smooth":0..100=0,"feather":px=0,"contrast":0..100=0,"shiftEdge":-100..100=0,"decontaminate":bool=false,"amount":0..100=100,"output":"selection|layerMask|newLayer|newLayerWithMask"="selection","sampleAllLayers":bool=false}"##,- has_selection,+ has_selection_or_mask,refine_edge),spec!(@@ -526,6 +540,22 @@ mod tests {assert!(cov(&s, 38, 20) == 1.0 && cov(&s, 42, 20) == 0.0);}+ #[test]+ fn refine_edge_works_on_layer_mask_without_selection() {+ let mut s = blurred_edge_selected();+ s.execute("layer.layerMask.revealSelection", json!({})).unwrap();+ s.execute("select.deselect", json!({})).unwrap();+ assert!(s.active().unwrap().doc.selection.is_none());+ assert!(s.active().unwrap().doc.layers[0].mask.is_some());+ assert!(s.is_enabled("select.refineEdge"));++ let r = s.execute("select.refineEdge", json!({"radius": 8})).unwrap();+ assert_eq!(r["output"], "layerMask");+ let d = s.active().unwrap();+ let m = d.doc.layers[0].mask.as_ref().unwrap();+ assert!(soft_count(&m.surface, 20) >= 4);+ }+#[test]fn focus_area_command() {let nz = noise_field(160, 100, 3);diff --git a/crates/ui-egui/src/layer_row_ui/edit_tests.rs b/crates/ui-egui/src/layer_row_ui/edit_tests.rsindex feb5a87b4..6e33e044f 100644--- a/crates/ui-egui/src/layer_row_ui/edit_tests.rs+++ b/crates/ui-egui/src/layer_row_ui/edit_tests.rs@@ -95,6 +95,7 @@ fn text_name_still_renames_and_mask_double_click_does_not_edit_text() {let mut s = session();let id = s.execute("type.create", json!({"text": "Hello", "name": "Title"})).unwrap()["layer"].as_u64().unwrap();s.execute("layer.layerMask.revealAll", json!({})).unwrap();+ s.execute("prefs.set", json!({"values": {"tools.doubleClickLayerMaskLaunchesSelectAndMask": false}})).unwrap();let mut h = harness(s, 1.0);let at = row(&h, id).name.unwrap().center();double_click(&mut h, at);@@ -109,6 +110,21 @@ fn text_name_still_renames_and_mask_double_click_does_not_edit_text() {assert!(h.state().ui.dialogs.is_empty());}+#[test]+fn mask_double_click_launches_select_and_mask_with_preference() {+ let mut s = session();+ let id = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();+ s.execute("layer.layerMask.revealAll", json!({})).unwrap();+ s.execute("prefs.set", json!({"values": {"tools.doubleClickLayerMaskLaunchesSelectAndMask": true}})).unwrap();+ let mut h = harness(s, 1.0);+ let masks = crate::mask_thumbs_ui::recorded(&h.ctx, id).unwrap().0;+ let at = masks.iter().find(|(kind, _)| *kind == crate::mask_thumbs_ui::MaskKind::Pixel).unwrap().1.center();+ double_click(&mut h, at);+ assert!(h.state().ui.mask_target);+ let dialog = h.state().ui.dialogs.last().expect("select and mask dialog should be opened");+ assert_eq!(dialog.fields.get("__command").and_then(|v| v.as_str()), Some("select.refineEdge"));+}+#[test]fn edit_type_command_rejects_missing_or_non_type_layers_without_changes() {let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());diff --git a/crates/ui-egui/src/mask_thumbs_ui.rs b/crates/ui-egui/src/mask_thumbs_ui.rsindex 6755ba5a5..562b2b752 100644--- a/crates/ui-egui/src/mask_thumbs_ui.rs+++ b/crates/ui-egui/src/mask_thumbs_ui.rs@@ -204,6 +204,18 @@ pub fn load_params(l: &Layer, kind: MaskKind, m: egui::Modifiers) -> Value {json!({"channel": channel, "layer": l.id.0, "operation": crate::channels_panel::load_operation(m)})}+/// What double-clicking a mask thumbnail does: pixel mask launches Select and Mask if preference is enabled.+pub fn double_click_command(app: &PhotocraftApp, kind: MaskKind) -> Option<(&'static str, Value)> {+ if kind == MaskKind::Pixel+ && app.session.prefs().tools.double_click_layer_mask_launches_select_and_mask+ && crate::menus::is_enabled(app, "select.selectAndMask")+ {+ Some(("select.selectAndMask", Value::Null))+ } else {+ None+ }+}+/// The active layer of `st` has a vector mask (shape layers' paths are content, not masks).pub fn has_vector_mask(st: &photocraft_engine::DocState) -> bool {st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.vector_mask.is_some() && !matches!(l.content, photocraft_doc::LayerContent::Shape(_)))diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex cbe56c6c8..e9ca97ed1 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -2325,6 +2325,13 @@ fn layer_row(// One rename at a time: starting this one commits any other (#314).actions.push(done);}+ } else if let Some(kind) = pos.and_then(|p| masks.hit(p)) {+ if kind == crate::mask_thumbs_ui::MaskKind::Pixel {+ actions.push(("ui.maskTarget".into(), json!(true)));+ }+ if let Some((cmd, val)) = crate::mask_thumbs_ui::double_click_command(app, kind) {+ actions.push((cmd.into(), val));+ }} else if pos.and_then(|p| masks.hit(p)).is_none() {let id = match &l.content {LayerContent::Adjustment(_) | LayerContent::Fill(_) if on(thumb) => "layer.layerContentOptions",diff --git a/docs/scorecard.md b/docs/scorecard.mdindex ff6c12b78..9dd0fc764 100644--- a/docs/scorecard.md+++ b/docs/scorecard.md@@ -9,7 +9,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js| [Performance](#performance) | 3 | 22 | 22 | 56 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 15 not run, 7 not measurable yet (baseline 2026-10-05) || [File compatibility](#file-compatibility) | 4 | 12 | 11 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 236/309, round trip ≥ 307/309; photoshop oracles: oracle ≥ 133/256, round trip ≥ 258/256 || [Tools](#tools) | 19 | 13 | 7 | |-| [Workspace and UI](#workspace-and-ui) | 5 | 7 | 11 | settings that do nothing: 47 of 146 (target 0) |+| [Workspace and UI](#workspace-and-ui) | 5 | 7 | 11 | settings that do nothing: 46 of 146 (target 0) || [Type](#type) | 0 | 3 | 6 | || [Automation](#automation) | 3 | 2 | 6 | 627 of 627 menu items live (100.0%) || [Reliability](#reliability) | 5 | 0 | 2 | never-crash lints on 28 of 28 crates; panic_hunt present |@@ -172,9 +172,9 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t## Workspace and UI-**Settings that do nothing: 47 of 146** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.+**Settings that do nothing: 46 of 146** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.-Unread: `general.color_picker`, `general.beep_when_done`, `general.resize_image_during_place`, `general.always_create_smart_objects_when_placing`, `general.animated_zoom`, `general.zoom_resizes_windows`, `interface.dynamic_color_sliders`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `tools.enable_flick_panning`, `tools.vary_round_brush_hardness_on_hud`, `tools.show_transformation_values`, `tools.double_click_layer_mask_launches_select_and_mask`, `file_handling.image_previews`, `file_handling.save_in_background`, `file_handling.ignore_exif_profile_tag`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `cursors.brush_preview_color`, `units_and_rulers.type_units`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `units_and_rulers.print_resolution`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.smart_quotes`, `type_.missing_glyph_protection`, `type_.show_font_names_in_english`, `type_.text_engine`, `type_.font_preview`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `enhanced_controls.zoom_with_trackpad_pinch`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.+Unread: `general.color_picker`, `general.beep_when_done`, `general.resize_image_during_place`, `general.always_create_smart_objects_when_placing`, `general.animated_zoom`, `general.zoom_resizes_windows`, `interface.dynamic_color_sliders`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `tools.enable_flick_panning`, `tools.vary_round_brush_hardness_on_hud`, `tools.show_transformation_values`, `file_handling.image_previews`, `file_handling.save_in_background`, `file_handling.ignore_exif_profile_tag`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `cursors.brush_preview_color`, `units_and_rulers.type_units`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `units_and_rulers.print_resolution`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.smart_quotes`, `type_.missing_glyph_protection`, `type_.show_font_names_in_english`, `type_.text_engine`, `type_.font_preview`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `enhanced_controls.zoom_with_trackpad_pinch`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.| Id | Target | Status | Issue | Note ||---|---|---|---|---|