mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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 <[email protected]>
This commit is contained in:
1 parent
91b4cd689d
commit
254e334dd0
4 files changed
+89
-8
No files matched your search
@@ -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<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 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
|
||||
|
||||
@@ -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,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<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 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");
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user