tools.setBrush: skip untouched tip/pattern bitmaps; enum variant changes replace

merge_brush no longer base64-round-trips sampled tips and pattern tiles the
patch doesn't name: a Brush Settings edit on a 2500 px imported tip goes from
121 ms to 0.68 ms. A patch switching an externally tagged enum's variant
(texture pattern tile -> procedural) now replaces it instead of merging into
an invalid two-variant object.

Refs #95

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Brandon ThomasandClaude Opus 5.5 committed 2026-10-05 01:36:51 -04:00
1 parent b4b1aab4ad
commit 180185a703
2 files changed
+73 -2

No files matched your search

+34 -2
View File
@@ -84,6 +84,11 @@ pub fn parse_points(p: &Value, cmd: &str) -> Result<Vec<StrokePoint>> {
/// Deep-merge `patch` into `base` (objects merge key by key; anything else replaces).
fn merge(base: &mut Value, patch: &Value) {
match (base, patch) {
// A different variant of an externally tagged enum (`{"tile":…}` → `{"procedural":…}`)
// replaces the old one instead of merging into a two-variant object.
(Value::Object(b), Value::Object(p)) if b.len() == 1 && p.len() == 1 && b.keys().next() != p.keys().next() => {
*b = p.clone();
}
(Value::Object(b), Value::Object(p)) => {
for (k, v) in p {
match b.get_mut(k) {
@@ -100,9 +105,36 @@ fn merge(base: &mut Value, patch: &Value) {
/// Apply a JSON brush patch onto a brush.
pub fn merge_brush(base: &BrushSettings, patch: &Value, cmd: &str) -> Result<BrushSettings> {
let mut v = serde_json::to_value(base).map_err(|e| bad(cmd, e.to_string()))?;
// Bitmaps the patch doesn't touch (sampled tips, pattern tiles: up to megabytes of base64) skip
// the JSON round trip, so a Brush Settings slider on an imported tip stays cheap.
let touches = |path: &[&str]| {
let mut v = patch;
for k in path {
match v.get(k) {
Some(x) => v = x,
// A non-object on the way replaces the whole section.
None => return !v.is_object(),
}
}
true
};
let mut light = base.clone();
let tip = (!touches(&["tip"])).then(|| std::mem::take(&mut light.tip));
let dual_tip = (!touches(&["dualBrush", "tip"])).then(|| std::mem::take(&mut light.dual_brush.tip));
let pattern = (!touches(&["texture", "pattern"])).then(|| std::mem::take(&mut light.texture.pattern));
let mut v = serde_json::to_value(&light).map_err(|e| bad(cmd, e.to_string()))?;
merge(&mut v, patch);
serde_json::from_value(v).map_err(|e| bad(cmd, format!("invalid brush: {e}")))
let mut out: BrushSettings = serde_json::from_value(v).map_err(|e| bad(cmd, format!("invalid brush: {e}")))?;
if let Some(t) = tip {
out.tip = t;
}
if let Some(t) = dual_tip {
out.dual_brush.tip = t;
}
if let Some(p) = pattern {
out.texture.pattern = p;
}
Ok(out)
}
fn find_preset<'a>(s: &'a Session, name: &str, cmd: &str) -> Result<&'a BrushPreset> {
+39
View File
@@ -176,6 +176,45 @@ fn set_brush_merges_fields() {
assert_eq!(s.tools.brush.texture.pattern, chalk_pattern);
}
#[test]
fn set_brush_keeps_untouched_bitmaps_and_replaces_touched_ones() {
let tile = |w, h, k: u32| GrayTile::from_fn(w, h, move |x, y| ((x * k + y) % 5) as f32 / 4.0);
let base = BrushSettings {
tip: TipShape::Sampled(tile(900, 700, 3)),
dual_brush: photocraft_paint::DualBrush { tip: TipShape::Sampled(tile(30, 20, 2)), ..Default::default() },
texture: photocraft_paint::Texture { pattern: photocraft_paint::Pattern::Tile(tile(64, 64, 1)), ..Default::default() },
..Default::default()
};
let cmd = "tools.setBrush";
// Untouched bitmaps survive any patch that doesn't name them.
for patch in [json!({"size": 12}), json!({"dualBrush": {"size": 9}}), json!({"texture": {"depth": 0.5}}), json!({})] {
let b = merge_brush(&base, &patch, cmd).unwrap();
assert_eq!((&b.tip, &b.dual_brush.tip, &b.texture.pattern), (&base.tip, &base.dual_brush.tip, &base.texture.pattern), "{patch}");
}
let b = merge_brush(&base, &json!({"size": 12, "dualBrush": {"size": 9}}), cmd).unwrap();
assert_eq!((b.size, b.dual_brush.size), (12.0, 9.0));
// Named ones are replaced.
let b = merge_brush(
&base,
&json!({"tip": "round", "dualBrush": {"tip": "round"}, "texture": {"pattern": {"procedural": {"style": "dots", "size": 32, "seed": 2}}}}),
cmd,
)
.unwrap();
assert_eq!((&b.tip, &b.dual_brush.tip), (&TipShape::Round, &TipShape::Round));
assert!(matches!(b.texture.pattern, photocraft_paint::Pattern::Procedural { size: 32, .. }));
// A section replaced by a non-object is an error, not a silently kept bitmap.
assert!(merge_brush(&base, &json!({"dualBrush": 3}), cmd).is_err());
assert!(merge_brush(&base, &json!(null), cmd).is_err());
// Speed: a panel edit on a big imported tip doesn't base64 the tip.
let big = BrushSettings { tip: TipShape::Sampled(tile(2500, 2500, 7)), ..Default::default() };
let t0 = std::time::Instant::now();
for i in 0..10 {
merge_brush(&big, &json!({"size": 10 + i}), cmd).unwrap();
}
let ms = t0.elapsed().as_secs_f64() * 100.0;
eprintln!("merge_brush on a 2500 px tip: {ms:.2} ms per edit");
}
#[test]
fn define_brush_from_selection() {
let mut s = session(40, 40);