Merge pull request #18820

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4andautocraft-bot[bot] authored and autocraft-bot[bot] committed 2026-12-02 01:07:07 +00:00
commit a802dfb5c6
4 files changed
+116 -8

No files matched your search

@@ -5,14 +5,20 @@ use photocraft_doc::vector::PathOp;
// ------------------------------------------------------------------ Define Brush / Custom Shape
pub(super) fn define_brush_preset(s: &mut Session, p: &Value) -> Result<Value> {
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 raise it (#18228). A
// 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.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);
let defined = s.execute("brush.defineFromSelection", json!({"name": name}))?;
if typed.is_none() {
s.tools.sampled_brushes = number;
}
Ok(defined)
}
/// The path Define Custom Shape uses: `"work"`, a saved path name, else the work path, the
+4
View File
@@ -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,
}
}
}
@@ -0,0 +1,97 @@
//! #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, 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};
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<String> = (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 raise the number.
#[test]
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!({})), "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));
// 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.
#[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");
}
+1
View File
@@ -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;