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 <[email protected]>
This commit is contained in:
dholzricandClaude Opus 5.5 committed 2026-12-01 16:57:20 -06:00
1 parent 254e334dd0
commit f525f28080
2 files changed
+26 -7

No files matched your search

@@ -6,11 +6,16 @@ use photocraft_doc::vector::PathOp;
pub(super) fn define_brush_preset(s: &mut Session, p: &Value) -> Result<Value> {
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)
}
@@ -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::<Vec<_>>();
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() {