From 254e334dd0b093e96e070f4022750b422d7ccf6e Mon Sep 17 00:00:00 2001 From: dholzric Date: Tue, 1 Dec 2026 14:40:47 -0600 Subject: [PATCH 1/3] Define Brush Preset: number "Sampled Brush N" from a session counter (#18228) The default name was numbered from the presets whose name starts with "Sampled Brush", so deleting the defaults restarted the count and a typed or imported "Sampled Brush" name raised it. ToolState now counts the defaults stored this session; the counter rises only after the preset is stored and is not saved to settings. Co-Authored-By: Claude Opus 5.5 --- .../src/cmd/edit_menu_cmds/presets.rs | 16 ++-- crates/engine-core/src/doc_state.rs | 4 + .../tests/it/define_brush_sampled_number.rs | 76 +++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + 4 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 crates/engine/tests/it/define_brush_sampled_number.rs diff --git a/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs b/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs index a40737910f..5310f4f7f3 100644 --- a/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs +++ b/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs @@ -5,14 +5,14 @@ use photocraft_doc::vector::PathOp; // ------------------------------------------------------------------ Define Brush / Custom Shape pub(super) fn define_brush_preset(s: &mut Session, p: &Value) -> Result { - let name = match p.get("name").and_then(Value::as_str).map(str::trim).filter(|n| !n.is_empty()) { - Some(n) => n.to_string(), - None => { - let n = s.tools.presets.iter().filter(|b| b.name.starts_with("Sampled Brush")).count() + 1; - format!("Sampled Brush {n}") - } - }; - s.execute("brush.defineFromSelection", json!({"name": name})) + let typed = p.get("name").and_then(Value::as_str).map(str::trim).filter(|n| !n.is_empty()); + // The default counts the defaults stored this session; presets that merely share the name don't count (#18228). + let name = typed.map_or_else(|| format!("Sampled Brush {}", s.tools.sampled_brushes.saturating_add(1)), str::to_string); + let defined = s.execute("brush.defineFromSelection", json!({"name": name}))?; + if typed.is_none() { + s.tools.sampled_brushes = s.tools.sampled_brushes.saturating_add(1); + } + Ok(defined) } /// The path Define Custom Shape uses: `"work"`, a saved path name, else the work path, the diff --git a/crates/engine-core/src/doc_state.rs b/crates/engine-core/src/doc_state.rs index 836c5837b6..8a5f7604c6 100644 --- a/crates/engine-core/src/doc_state.rs +++ b/crates/engine-core/src/doc_state.rs @@ -412,6 +412,9 @@ pub struct ToolState { /// The pair Quick Mask put aside, with its CMYK roles: the Quick Mask pair outside Quick /// Mask, the pixel (or layer mask) pair inside it (#17490). pub quick_mask_aside: ([[f32; 4]; 2], CmykPair), + /// How many "Sampled Brush N" defaults Define Brush Preset stored in this session, which gives the next N. Deleting + /// or renaming those presets doesn't lower it, and is not saved to settings (#18228). + pub sampled_brushes: u32, } /// Native CMYK inks associated with each color role. @@ -565,6 +568,7 @@ impl Default for ToolState { cmyk_roles: [[None; 2]; 2], quick_mask_on: false, quick_mask_aside: (Self::MASK_COLORS, [None; 2]), + sampled_brushes: 0, } } } diff --git a/crates/engine/tests/it/define_brush_sampled_number.rs b/crates/engine/tests/it/define_brush_sampled_number.rs new file mode 100644 index 0000000000..7eabea6fde --- /dev/null +++ b/crates/engine/tests/it/define_brush_sampled_number.rs @@ -0,0 +1,76 @@ +//! #18228 (Edit › Define Brush Preset runs the brush command of `photocraft-engine-paint`, so this runs here): Define Brush Preset's "Sampled Brush N" default counts the defaults stored in this session. Deleting or +//! renaming presets does not lower N, and a preset with such a name from elsewhere (a typed name, an imported set) does +//! not raise it. A call that stores nothing leaves N as it is. + +use serde_json::{Value, json}; + +use photocraft_engine::Session; + +const DEFINE: &str = "edit.defineBrushPreset"; + +/// Opens a new white document with a black rectangle on a layer, ready for Define Brush Preset. +fn new_document(s: &mut Session) { + s.execute("file.new", json!({"width": 40, "height": 30, "background": "#ffffff"})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.edit("BrushSrc", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(5, 5, 20, 15), &[0.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); +} + +/// Defines a brush with `params` and returns the name it was stored under. +fn define(s: &mut Session, params: Value) -> String { + s.execute(DEFINE, params).unwrap(); + s.tools.current_preset.clone().expect("the new preset is the current brush") +} + +fn session() -> Session { + let mut s = Session::new(); + new_document(&mut s); + s +} + +#[test] +fn each_stored_default_raises_the_number() { + let mut s = session(); + let mut names = vec![define(&mut s, json!({}))]; + for _ in 0..2 { + new_document(&mut s); + names.push(define(&mut s, json!({}))); + } + assert_eq!(names, ["Sampled Brush 1", "Sampled Brush 2", "Sampled Brush 3"]); +} + +#[test] +fn deleting_or_renaming_the_presets_does_not_lower_it() { + let mut s = session(); + let names: Vec = (0..3).map(|_| define(&mut s, json!({}))).collect(); + for name in &names { + s.execute("brush.presets.delete", json!({"name": name})).unwrap(); + } + assert_eq!(define(&mut s, json!({})), "Sampled Brush 4", "after deleting presets 1-3"); + + let mut s = session(); + let first = define(&mut s, json!({})); + s.execute("brush.presets.rename", json!({"name": first, "newName": "Renamed"})).unwrap(); + assert_eq!(define(&mut s, json!({})), "Sampled Brush 2", "after renaming preset 1"); +} + +/// A preset named "Sampled Brush …" that the default did not make, such as a typed name, does not count. +#[test] +fn a_typed_sampled_brush_name_does_not_move_it() { + let mut s = session(); + assert_eq!(define(&mut s, json!({"name": "Sampled Brush 7"})), "Sampled Brush 7"); + assert_eq!(define(&mut s, json!({"name": "Sampled Brush 1"})), "Sampled Brush 1"); + assert_eq!(define(&mut s, json!({})), "Sampled Brush 1", "the first default"); +} + +/// A call that stores no preset leaves the number as it is. +#[test] +fn a_refused_call_does_not_raise_it() { + let mut s = Session::new(); + assert!(s.execute(DEFINE, json!({})).is_err(), "no document"); + new_document(&mut s); + assert_eq!(define(&mut s, json!({})), "Sampled Brush 1"); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 74e8fba6dd..4bc6b06e83 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -66,6 +66,7 @@ mod crop_content_aware_layers; mod crop_delete_off_background; mod csh_import; mod current_affinity_artboards; +mod define_brush_sampled_number; mod duplicate_layer_comps; mod duplicate_slices; mod duplicate_variables; From f525f280807c164f578716ed1a5bb37adc2e679b Mon Sep 17 00:00:00 2001 From: dholzric Date: Tue, 1 Dec 2026 16:57:20 -0600 Subject: [PATCH 2/3] Define Brush Preset: skip a "Sampled Brush N" a preset already uses (#18228) Storing under a taken name replaced that preset's tip. The default now skips used numbers and the counter moves to the number it stored. Co-Authored-By: Claude Opus 5.5 --- .../src/cmd/edit_menu_cmds/presets.rs | 11 +++++++--- .../tests/it/define_brush_sampled_number.rs | 22 +++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs b/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs index 5310f4f7f3..c8fa58be90 100644 --- a/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs +++ b/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs @@ -6,11 +6,16 @@ use photocraft_doc::vector::PathOp; pub(super) fn define_brush_preset(s: &mut Session, p: &Value) -> Result { let typed = p.get("name").and_then(Value::as_str).map(str::trim).filter(|n| !n.is_empty()); - // The default counts the defaults stored this session; presets that merely share the name don't count (#18228). - let name = typed.map_or_else(|| format!("Sampled Brush {}", s.tools.sampled_brushes.saturating_add(1)), str::to_string); + // The default counts the defaults stored this session; presets that merely share the name don't raise it (#18228). A + // number a preset already uses is skipped, since storing under that name would replace that preset. + let mut number = s.tools.sampled_brushes.saturating_add(1); + while typed.is_none() && s.tools.presets.iter().any(|b| b.name == format!("Sampled Brush {number}")) { + number = number.saturating_add(1); + } + let name = typed.map_or_else(|| format!("Sampled Brush {number}"), str::to_string); let defined = s.execute("brush.defineFromSelection", json!({"name": name}))?; if typed.is_none() { - s.tools.sampled_brushes = s.tools.sampled_brushes.saturating_add(1); + s.tools.sampled_brushes = number; } Ok(defined) } diff --git a/crates/engine/tests/it/define_brush_sampled_number.rs b/crates/engine/tests/it/define_brush_sampled_number.rs index 7eabea6fde..508a0408ef 100644 --- a/crates/engine/tests/it/define_brush_sampled_number.rs +++ b/crates/engine/tests/it/define_brush_sampled_number.rs @@ -1,6 +1,7 @@ //! #18228 (Edit › Define Brush Preset runs the brush command of `photocraft-engine-paint`, so this runs here): Define Brush Preset's "Sampled Brush N" default counts the defaults stored in this session. Deleting or //! renaming presets does not lower N, and a preset with such a name from elsewhere (a typed name, an imported set) does -//! not raise it. A call that stores nothing leaves N as it is. +//! not raise it, but N skips a number such a preset already uses instead of replacing that preset. A call that stores +//! nothing leaves N as it is. use serde_json::{Value, json}; @@ -57,15 +58,28 @@ fn deleting_or_renaming_the_presets_does_not_lower_it() { assert_eq!(define(&mut s, json!({})), "Sampled Brush 2", "after renaming preset 1"); } -/// A preset named "Sampled Brush …" that the default did not make, such as a typed name, does not count. +/// A preset named "Sampled Brush …" that the default did not make, such as a typed name, does not raise the number. #[test] -fn a_typed_sampled_brush_name_does_not_move_it() { +fn a_typed_sampled_brush_name_does_not_raise_it() { let mut s = session(); assert_eq!(define(&mut s, json!({"name": "Sampled Brush 7"})), "Sampled Brush 7"); - assert_eq!(define(&mut s, json!({"name": "Sampled Brush 1"})), "Sampled Brush 1"); assert_eq!(define(&mut s, json!({})), "Sampled Brush 1", "the first default"); } +/// The default never takes a name a preset already has: storing under it would replace that preset's tip. +#[test] +fn a_taken_number_is_skipped_and_the_preset_is_kept() { + let mut s = session(); + let user_presets = |s: &Session| s.tools.presets.iter().filter(|b| !b.builtin).map(|b| b.name.clone()).collect::>(); + define(&mut s, json!({"name": "Sampled Brush 1"})); + let first = define(&mut s, json!({})); + assert_ne!(first, "Sampled Brush 1", "the typed preset's name"); + assert_eq!(user_presets(&s).len(), 2, "both presets are kept: {:?}", user_presets(&s)); + let second = define(&mut s, json!({})); + assert!(![first.as_str(), "Sampled Brush 1"].contains(&second.as_str()), "{second}"); + assert_eq!(user_presets(&s).len(), 3, "{:?}", user_presets(&s)); +} + /// A call that stores no preset leaves the number as it is. #[test] fn a_refused_call_does_not_raise_it() { From 71e3c51be4219d82ecf0352e3a0649fb8d52cfd7 Mon Sep 17 00:00:00 2001 From: dholzric Date: Tue, 1 Dec 2026 18:43:23 -0600 Subject: [PATCH 3/3] Define Brush Preset: skip a taken "Sampled Brush N" ignoring case (#18228) The preset store matches names ignoring ASCII case, so a typed "sampled brush 1" was still replaced by the default "Sampled Brush 1". Co-Authored-By: Claude Opus 5.5 --- crates/engine-core/src/cmd/edit_menu_cmds/presets.rs | 5 +++-- crates/engine/tests/it/define_brush_sampled_number.rs | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs b/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs index c8fa58be90..8a2e9fe971 100644 --- a/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs +++ b/crates/engine-core/src/cmd/edit_menu_cmds/presets.rs @@ -7,9 +7,10 @@ use photocraft_doc::vector::PathOp; pub(super) fn define_brush_preset(s: &mut Session, p: &Value) -> Result { let typed = p.get("name").and_then(Value::as_str).map(str::trim).filter(|n| !n.is_empty()); // The default counts the defaults stored this session; presets that merely share the name don't raise it (#18228). A - // number a preset already uses is skipped, since storing under that name would replace that preset. + // number a preset already uses is skipped, since storing under that name would replace that preset; the store matches + // names ignoring ASCII case, so this does too. let mut number = s.tools.sampled_brushes.saturating_add(1); - while typed.is_none() && s.tools.presets.iter().any(|b| b.name == format!("Sampled Brush {number}")) { + while typed.is_none() && s.tools.presets.iter().any(|b| b.name.eq_ignore_ascii_case(&format!("Sampled Brush {number}"))) { number = number.saturating_add(1); } let name = typed.map_or_else(|| format!("Sampled Brush {number}"), str::to_string); diff --git a/crates/engine/tests/it/define_brush_sampled_number.rs b/crates/engine/tests/it/define_brush_sampled_number.rs index 508a0408ef..d082a66282 100644 --- a/crates/engine/tests/it/define_brush_sampled_number.rs +++ b/crates/engine/tests/it/define_brush_sampled_number.rs @@ -78,6 +78,13 @@ fn a_taken_number_is_skipped_and_the_preset_is_kept() { let second = define(&mut s, json!({})); assert!(![first.as_str(), "Sampled Brush 1"].contains(&second.as_str()), "{second}"); assert_eq!(user_presets(&s).len(), 3, "{:?}", user_presets(&s)); + + // The store matches names ignoring case, so a typed "sampled brush 1" is taken too. + let mut s = session(); + define(&mut s, json!({"name": "sampled brush 1"})); + let first = define(&mut s, json!({})); + assert!(!first.eq_ignore_ascii_case("sampled brush 1"), "{first}"); + assert_eq!(user_presets(&s).len(), 2, "{:?}", user_presets(&s)); } /// A call that stores no preset leaves the number as it is.