mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Share built-in brush presets across sessions
This commit is contained in:
1 parent
856f865cf9
commit
6b207c5b55
16 files changed
+73
-51
No files matched your search
@@ -52,7 +52,7 @@ fn library_tip_bytes(s: &Session) -> (usize, u64, u64) {
|
||||
previews += g.data.len() as u64 * 2;
|
||||
}
|
||||
};
|
||||
for p in &s.tools.presets {
|
||||
for p in s.tools.presets.iter() {
|
||||
let b = &p.brush;
|
||||
for t in [&b.tip, &b.dual_brush.tip] {
|
||||
match t {
|
||||
|
||||
@@ -980,9 +980,9 @@ fn name_param(p: &Value, cmd: &str) -> Result<String> {
|
||||
|
||||
/// Replace the preset of that name, or append it at the end of the list.
|
||||
fn upsert(s: &mut Session, preset: BrushPreset) {
|
||||
match s.tools.presets.iter_mut().find(|x| x.name.eq_ignore_ascii_case(&preset.name)) {
|
||||
match std::sync::Arc::make_mut(&mut s.tools.presets).iter_mut().find(|x| x.name.eq_ignore_ascii_case(&preset.name)) {
|
||||
Some(x) => *x = preset,
|
||||
None => s.tools.presets.push(preset),
|
||||
None => std::sync::Arc::make_mut(&mut s.tools.presets).push(preset),
|
||||
}
|
||||
s.brush_presets_changed();
|
||||
}
|
||||
@@ -1010,7 +1010,7 @@ fn presets_delete(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "brush.presets.delete";
|
||||
let name = name_param(p, cmd)?;
|
||||
let before = s.tools.presets.len();
|
||||
s.tools.presets.retain(|x| !x.name.eq_ignore_ascii_case(&name));
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).retain(|x| !x.name.eq_ignore_ascii_case(&name));
|
||||
if s.tools.presets.len() == before {
|
||||
return Err(bad(cmd, format!("no brush preset named `{name}`")));
|
||||
}
|
||||
@@ -1043,7 +1043,10 @@ fn presets_update(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some(patch) => merge_brush(&s.tools.brush, patch, cmd)?,
|
||||
None => s.tools.brush.clone(),
|
||||
};
|
||||
let pr = s.tools.presets.iter_mut().find(|x| x.name.eq_ignore_ascii_case(&name)).ok_or_else(|| bad(cmd, format!("no brush preset named `{name}`")))?;
|
||||
let pr = std::sync::Arc::make_mut(&mut s.tools.presets)
|
||||
.iter_mut()
|
||||
.find(|x| x.name.eq_ignore_ascii_case(&name))
|
||||
.ok_or_else(|| bad(cmd, format!("no brush preset named `{name}`")))?;
|
||||
pr.brush = brush;
|
||||
pr.builtin = false;
|
||||
let (name, group) = (pr.name.clone(), pr.group.clone());
|
||||
|
||||
@@ -263,7 +263,7 @@ mod tests {
|
||||
#[test]
|
||||
fn previous_and_next_brush_walk_the_presets_and_wrap() {
|
||||
let mut s = Session::new();
|
||||
s.tools.presets = vec![preset("One", "", &[], 11.0), preset("Two", "", &[], 22.0), preset("Three", "", &[], 33.0)];
|
||||
s.tools.presets = std::sync::Arc::new(vec![preset("One", "", &[], 11.0), preset("Two", "", &[], 22.0), preset("Three", "", &[], 33.0)]);
|
||||
s.tools.current_preset = None;
|
||||
let picked = |s: &Session| (s.tools.current_preset.clone().unwrap_or_default(), s.tools.brush.size);
|
||||
s.execute("tools.nextBrush", Value::Null).unwrap();
|
||||
@@ -284,7 +284,7 @@ mod tests {
|
||||
[Some(","), Some("."), Some("Shift+,"), Some("Shift+.")]
|
||||
);
|
||||
// No presets: an error, and the brush and the current preset stay as they were.
|
||||
s.tools.presets.clear();
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).clear();
|
||||
let (brush, current) = (s.tools.brush.clone(), s.tools.current_preset.clone());
|
||||
for id in ["tools.previousBrush", "tools.nextBrush", "tools.firstBrush", "tools.lastBrush"] {
|
||||
assert!(s.execute(id, Value::Null).is_err(), "{id}");
|
||||
|
||||
@@ -153,7 +153,7 @@ fn move_preset(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
let folder = folder_param(p, cmd)?;
|
||||
let group = group_param(p, "group", cmd)?;
|
||||
let lib = &mut s.tools.presets;
|
||||
let lib = std::sync::Arc::make_mut(&mut s.tools.presets);
|
||||
let (Some(moving), before_pr) = (lib.get(i), before.and_then(|b| lib.get(b))) else {
|
||||
return Err(bad(cmd, "no such preset"));
|
||||
};
|
||||
@@ -233,7 +233,7 @@ fn move_folder(s: &mut Session, p: &Value, g: String, f: Vec<String>, cmd: &str)
|
||||
None | Some(Value::Null) => None,
|
||||
Some(v) => Some(v.as_u64().ok_or_else(|| bad(cmd, "`index` must be a non-negative integer"))?),
|
||||
};
|
||||
let lib = std::mem::take(&mut s.tools.presets);
|
||||
let lib = std::mem::take(std::sync::Arc::make_mut(&mut s.tools.presets));
|
||||
// Where the block sits among the other presets (its place when nothing else is in the parent).
|
||||
let start = lib.iter().position(|x| in_folder(x, &g, &f)).unwrap_or(0);
|
||||
let (block, mut rest): (Vec<BrushPreset>, Vec<BrushPreset>) = lib.into_iter().partition(|x| in_folder(x, &g, &f));
|
||||
@@ -252,7 +252,7 @@ fn move_folder(s: &mut Session, p: &Value, g: String, f: Vec<String>, cmd: &str)
|
||||
let tail = rest.split_off(pos.min(rest.len()));
|
||||
rest.extend(block);
|
||||
rest.extend(tail);
|
||||
s.tools.presets = rest;
|
||||
s.tools.presets = std::sync::Arc::new(rest);
|
||||
s.brush_presets_changed();
|
||||
let at = subfolder_order(&s.tools.presets, &g, parent).iter().position(|x| x == leaf).unwrap_or(0);
|
||||
Ok(json!({ "group": g, "folder": f, "index": at }))
|
||||
@@ -332,7 +332,7 @@ fn move_into(s: &mut Session, p: &Value, g: String, f: Vec<String>, dest: Dest,
|
||||
if deepest > MAX_FOLDER_DEPTH {
|
||||
return Err(bad(cmd, format!("folders can be nested at most {MAX_FOLDER_DEPTH} deep")));
|
||||
}
|
||||
let lib = std::mem::take(&mut s.tools.presets);
|
||||
let lib = std::mem::take(std::sync::Arc::make_mut(&mut s.tools.presets));
|
||||
let (mut block, mut rest): (Vec<BrushPreset>, Vec<BrushPreset>) = lib.into_iter().partition(|x| in_folder(x, &g, &f));
|
||||
for x in &mut block {
|
||||
let tail = rest_of(x);
|
||||
@@ -352,7 +352,7 @@ fn move_into(s: &mut Session, p: &Value, g: String, f: Vec<String>, dest: Dest,
|
||||
let tail = rest.split_off(pos.unwrap_or(rest.len()).min(rest.len()));
|
||||
rest.extend(block);
|
||||
rest.extend(tail);
|
||||
s.tools.presets = rest;
|
||||
s.tools.presets = std::sync::Arc::new(rest);
|
||||
s.brush_presets_changed();
|
||||
let folder = new_prefix.get(..new_prefix.len().saturating_sub(1)).unwrap_or_default();
|
||||
let at = match &dest {
|
||||
@@ -382,7 +382,7 @@ fn move_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(v) => Some(v.as_u64().ok_or_else(|| bad(cmd, "`index` must be a non-negative integer"))?),
|
||||
};
|
||||
let lib = std::mem::take(&mut s.tools.presets);
|
||||
let lib = std::mem::take(std::sync::Arc::make_mut(&mut s.tools.presets));
|
||||
let (block, mut rest): (Vec<BrushPreset>, Vec<BrushPreset>) = lib.into_iter().partition(|x| x.group == g);
|
||||
let order = group_order(&rest);
|
||||
let first = |name: &str, rest: &[BrushPreset]| rest.iter().position(|x| x.group == name).unwrap_or(rest.len());
|
||||
@@ -394,7 +394,7 @@ fn move_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let tail = rest.split_off(pos.min(rest.len()));
|
||||
rest.extend(block);
|
||||
rest.extend(tail);
|
||||
s.tools.presets = rest;
|
||||
s.tools.presets = std::sync::Arc::new(rest);
|
||||
s.brush_presets_changed();
|
||||
let at = group_order(&s.tools.presets).iter().position(|x| *x == g).unwrap_or(0);
|
||||
Ok(json!({ "group": g, "index": at }))
|
||||
@@ -410,7 +410,7 @@ fn rename_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(bad(cmd, format!("a folder named `{new}` already exists there")));
|
||||
}
|
||||
let depth = parent.len();
|
||||
for x in s.tools.presets.iter_mut().filter(|x| in_folder(x, &g, &f)) {
|
||||
for x in std::sync::Arc::make_mut(&mut s.tools.presets).iter_mut().filter(|x| in_folder(x, &g, &f)) {
|
||||
if let Some(n) = x.folder.get_mut(depth) {
|
||||
n.clone_from(&new);
|
||||
}
|
||||
@@ -426,7 +426,7 @@ fn rename_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(bad(cmd, format!("a group named `{new}` already exists")));
|
||||
}
|
||||
if new != g {
|
||||
for x in s.tools.presets.iter_mut().filter(|x| x.group == g) {
|
||||
for x in std::sync::Arc::make_mut(&mut s.tools.presets).iter_mut().filter(|x| x.group == g) {
|
||||
x.group = new.clone();
|
||||
x.builtin = false;
|
||||
}
|
||||
@@ -439,7 +439,7 @@ fn delete_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "brush.presets.deleteGroup";
|
||||
let (g, f) = target_folder(s, p, cmd)?;
|
||||
let before = s.tools.presets.len();
|
||||
s.tools.presets.retain(|x| !in_folder(x, &g, &f));
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).retain(|x| !in_folder(x, &g, &f));
|
||||
if s.tools.current_preset.as_ref().is_some_and(|c| !s.tools.presets.iter().any(|x| x.name.eq_ignore_ascii_case(c))) {
|
||||
s.tools.current_preset = None;
|
||||
s.tools.brush_changed();
|
||||
|
||||
@@ -7,7 +7,7 @@ fn names(s: &Session, g: &str) -> Vec<String> {
|
||||
fn with_groups() -> Session {
|
||||
let mut s = Session::new();
|
||||
for (n, g) in [("A1", "Alpha"), ("A2", "Alpha"), ("A3", "Alpha"), ("B1", "Beta"), ("B2", "Beta")] {
|
||||
s.tools.presets.push(BrushPreset {
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(BrushPreset {
|
||||
name: n.into(),
|
||||
brush: Default::default(),
|
||||
builtin: false,
|
||||
@@ -119,7 +119,7 @@ fn with_folders() -> Session {
|
||||
let mut s = Session::new();
|
||||
for (n, f) in [("S0", &[][..]), ("I1", &["Ink"][..]), ("F1", &["Ink", "Fine"][..]), ("I2", &["Ink"][..]), ("D1", &["Dry"][..])] {
|
||||
let folder = f.iter().map(|x| x.to_string()).collect();
|
||||
s.tools.presets.push(BrushPreset {
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(BrushPreset {
|
||||
name: n.into(),
|
||||
brush: Default::default(),
|
||||
builtin: false,
|
||||
@@ -226,7 +226,7 @@ fn with_two_groups() -> Session {
|
||||
let mut s = with_folders();
|
||||
for (n, f) in [("O1", &[][..]), ("B1", &["Box"][..])] {
|
||||
let folder = f.iter().map(|x| x.to_string()).collect();
|
||||
s.tools.presets.push(BrushPreset {
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(BrushPreset {
|
||||
name: n.into(),
|
||||
brush: Default::default(),
|
||||
builtin: false,
|
||||
@@ -266,7 +266,7 @@ fn bad_moves_into_folders_fail_without_changes() {
|
||||
let mut s = with_two_groups();
|
||||
// A chain 31 deep under Set/Deep.
|
||||
let deep: Vec<String> = std::iter::once("Deep".to_string()).chain((0..30).map(|i| format!("d{i}"))).collect();
|
||||
s.tools.presets.push(BrushPreset {
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(BrushPreset {
|
||||
name: "Z".into(),
|
||||
brush: Default::default(),
|
||||
builtin: false,
|
||||
@@ -274,7 +274,7 @@ fn bad_moves_into_folders_fail_without_changes() {
|
||||
folder: deep.clone(),
|
||||
captures: Default::default(),
|
||||
});
|
||||
s.tools.presets.push(BrushPreset {
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(BrushPreset {
|
||||
name: "Y".into(),
|
||||
brush: Default::default(),
|
||||
builtin: false,
|
||||
@@ -314,7 +314,7 @@ fn bad_moves_into_folders_fail_without_changes() {
|
||||
s.execute("brush.presets.moveGroup", json!({"group": "Set", "folder": ["Ink", "Fine"], "into": {"group": "Set"}})).unwrap();
|
||||
assert_eq!(folder_of(&s, "F1"), ["Fine"]);
|
||||
// An ungrouped preset's group can't move.
|
||||
s.tools.presets.push(BrushPreset {
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(BrushPreset {
|
||||
name: "U".into(),
|
||||
brush: Default::default(),
|
||||
builtin: false,
|
||||
|
||||
@@ -314,9 +314,9 @@ fn export_import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// absurd tree).
|
||||
b.folder.retain(|f| !f.trim().is_empty());
|
||||
b.folder.truncate(crate::brush_preset_cmds::MAX_FOLDER_DEPTH);
|
||||
match s.tools.presets.iter_mut().find(|x| x.name == b.name) {
|
||||
match std::sync::Arc::make_mut(&mut s.tools.presets).iter_mut().find(|x| x.name == b.name) {
|
||||
Some(x) => *x = b,
|
||||
None => s.tools.presets.push(b),
|
||||
None => std::sync::Arc::make_mut(&mut s.tools.presets).push(b),
|
||||
}
|
||||
nb += 1;
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ pub(super) fn preset_manager(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
"delete" => match kind.as_str() {
|
||||
"brushes" => {
|
||||
let gone = s.tools.presets.remove(i);
|
||||
let gone = std::sync::Arc::make_mut(&mut s.tools.presets).remove(i);
|
||||
crate::brush_cmds::forget_deleted_preset(s, &gone.name);
|
||||
}
|
||||
"patterns" => drop(s.patterns.items.remove(i)),
|
||||
@@ -123,7 +123,7 @@ pub(super) fn preset_manager(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
"move" => {
|
||||
let to = p.get("to").and_then(Value::as_u64).ok_or_else(|| bad(cmd, "missing `to`"))? as usize;
|
||||
match kind.as_str() {
|
||||
"brushes" => crate::move_item(&mut s.tools.presets, i, to),
|
||||
"brushes" => crate::move_item(std::sync::Arc::make_mut(&mut s.tools.presets), i, to),
|
||||
"patterns" => crate::move_item(&mut s.patterns.items, i, to),
|
||||
_ => crate::move_item(&mut s.edit_state.custom_shapes, i, to),
|
||||
};
|
||||
|
||||
@@ -155,7 +155,7 @@ fn add_abr_presets(s: &mut Session, p: &Value, group: String, imp: photocraft_io
|
||||
// in Photoshop, unless `replace` asks to drop that group's own presets first: the group name
|
||||
// is only the file name, and the library is outside document history (#10640).
|
||||
let group = if p.get("replace").and_then(Value::as_bool).unwrap_or(false) {
|
||||
s.tools.presets.retain(|x| x.builtin || x.group != group);
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).retain(|x| x.builtin || x.group != group);
|
||||
group
|
||||
} else {
|
||||
// One pass over the library, not one per numbered candidate.
|
||||
@@ -168,7 +168,7 @@ fn add_abr_presets(s: &mut Session, p: &Value, group: String, imp: photocraft_io
|
||||
preset.group.clone_from(&group);
|
||||
preset.name = taken.unique(&preset.name);
|
||||
names.push(preset.name.clone());
|
||||
s.tools.presets.push(preset);
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(preset);
|
||||
}
|
||||
s.brush_presets_changed();
|
||||
if p.get("select").and_then(Value::as_bool).unwrap_or(false)
|
||||
@@ -262,7 +262,7 @@ fn import_image(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (v, w, h) = image_paint(&doc, cmd)?;
|
||||
let (brush, tw, th) = crate::brush_cmds::sampled_brush(cmd, &v, w, h)?;
|
||||
let name = UniqueNames::new(s.tools.presets.iter().map(|x| x.name.as_str())).unique(&name);
|
||||
s.tools.presets.push(photocraft_paint::BrushPreset {
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(photocraft_paint::BrushPreset {
|
||||
name: name.clone(),
|
||||
brush: brush.clone(),
|
||||
builtin: false,
|
||||
@@ -541,7 +541,7 @@ mod tests {
|
||||
// The user's own brush moved into the group.
|
||||
let mut own = s.tools.presets.iter().find(|p| p.group == "Kit").unwrap().clone();
|
||||
own.name = "My Ink".into();
|
||||
s.tools.presets.push(own);
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(own);
|
||||
let r = s.execute("brush.presets.importAbr", json!({"data": data, "group": "Kit"})).unwrap();
|
||||
assert_eq!(r["group"], "Kit (2)");
|
||||
let in_group = |s: &Session, g: &str| s.tools.presets.iter().filter(|p| p.group == g).count();
|
||||
|
||||
@@ -183,7 +183,7 @@ pub struct ToolState {
|
||||
pub background: [f32; 4],
|
||||
pub brush: photocraft_paint::BrushSettings,
|
||||
/// Brush presets (built-ins plus user presets; see `brush.presets.*`).
|
||||
pub presets: Vec<photocraft_paint::BrushPreset>,
|
||||
pub presets: Arc<Vec<photocraft_paint::BrushPreset>>,
|
||||
/// Bumped whenever `presets` changes ([`Session::brush_presets_changed`]); the preset store
|
||||
/// syncs when it moves.
|
||||
pub presets_rev: u64,
|
||||
@@ -284,7 +284,7 @@ impl Default for ToolState {
|
||||
smoothing: photocraft_paint::brush::Smoothing { amount: 0.1, ..Default::default() },
|
||||
..Default::default()
|
||||
},
|
||||
presets: photocraft_paint::presets::builtin(),
|
||||
presets: photocraft_paint::presets::builtin_shared(),
|
||||
presets_rev: 0,
|
||||
mixer: Default::default(),
|
||||
brush_gesture: None,
|
||||
|
||||
@@ -1103,7 +1103,7 @@ impl PresetStore {
|
||||
|
||||
fn builtin_names() -> &'static [String] {
|
||||
static NAMES: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
|
||||
NAMES.get_or_init(|| photocraft_paint::presets::builtin().into_iter().map(|p| p.name).collect())
|
||||
NAMES.get_or_init(|| photocraft_paint::presets::builtin_shared().iter().map(|p| p.name.clone()).collect())
|
||||
}
|
||||
|
||||
impl Session {
|
||||
@@ -1141,7 +1141,7 @@ impl Session {
|
||||
}
|
||||
}
|
||||
}
|
||||
let lib = &mut self.tools.presets;
|
||||
let lib = std::sync::Arc::make_mut(&mut self.tools.presets);
|
||||
lib.retain(|p| !(p.builtin && hidden_builtins.iter().any(|h| h.eq_ignore_ascii_case(&p.name))));
|
||||
for p in presets {
|
||||
match lib.iter_mut().find(|x| x.name.eq_ignore_ascii_case(&p.name)) {
|
||||
@@ -1204,7 +1204,7 @@ impl Session {
|
||||
let rev = self.tools.presets_rev;
|
||||
if let Some(st) = self.preset_store.as_mut() {
|
||||
if st.synced_rev != Some(rev) {
|
||||
st.sync(&mut self.tools.presets);
|
||||
st.sync(std::sync::Arc::make_mut(&mut self.tools.presets));
|
||||
st.synced_rev = Some(rev);
|
||||
if link_stored_tips(st, &mut self.tools.brush) {
|
||||
// An embedded tip became a store reference: what the last-brush
|
||||
|
||||
@@ -119,7 +119,7 @@ fn group_with_sampled_8_and_16_bit_tips_round_trips() {
|
||||
let b = sampled("Sixteen", "Inks", tip16(50, 70, 2));
|
||||
let mut c = sampled("Shared tip", "Other", tip8(64, 48, 1));
|
||||
c.brush.size = 9.0;
|
||||
s.tools.presets.extend([a, b, c]);
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).extend([a, b, c]);
|
||||
s.brush_presets_changed();
|
||||
s.sync_preset_store();
|
||||
let files = dir.files();
|
||||
@@ -243,7 +243,7 @@ fn startup_with_many_groups_loads_them_all() {
|
||||
let (mut s, _) = session(&dir);
|
||||
for g in 0..25 {
|
||||
for i in 0..4 {
|
||||
s.tools.presets.push(sampled(&format!("P{g}-{i}"), &format!("Group {g}"), tip8(16 + i, 16, g)));
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(sampled(&format!("P{g}-{i}"), &format!("Group {g}"), tip8(16 + i, 16, g)));
|
||||
}
|
||||
}
|
||||
s.brush_presets_changed();
|
||||
@@ -267,7 +267,7 @@ fn reordering_presets_and_groups_persists() {
|
||||
let (mut s, _) = session(&dir);
|
||||
for (n, g) in [("Ink 1", "Inks"), ("Ink 2", "Inks"), ("Wash", "Washes")] {
|
||||
s.execute("brush.presets.save", json!({"name": n})).unwrap();
|
||||
s.tools.presets.iter_mut().find(|p| p.name == n).unwrap().group = g.into();
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).iter_mut().find(|p| p.name == n).unwrap().group = g.into();
|
||||
}
|
||||
s.brush_presets_changed();
|
||||
s.sync_preset_store();
|
||||
@@ -299,8 +299,8 @@ fn reordering_presets_and_groups_persists() {
|
||||
fn corrupt_and_oversized_files_are_skipped_with_a_warning() {
|
||||
let dir = TempDir::new("corrupt");
|
||||
let (mut s, _) = session(&dir);
|
||||
s.tools.presets.push(sampled("Good", "Fine", tip8(8, 8, 1)));
|
||||
s.tools.presets.push(sampled("Lost", "Broken tip", tip16(9, 9, 2)));
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(sampled("Good", "Fine", tip8(8, 8, 1)));
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(sampled("Lost", "Broken tip", tip16(9, 9, 2)));
|
||||
s.brush_presets_changed();
|
||||
s.sync_preset_store();
|
||||
let be = DirBackend::new(&dir.0);
|
||||
@@ -370,7 +370,7 @@ fn bench_load_500_sampled_presets() {
|
||||
// A mix of 8-bit (most .abr tips) and 16-bit tips, 64..319 px.
|
||||
let side = 64 + (i * 37) % 256;
|
||||
let t = if i % 4 == 0 { tip16(side, side, i) } else { tip8(side, side, i) };
|
||||
s.tools.presets.push(sampled(&format!("Brush {i}"), &format!("Set {}", i / 50), t));
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(sampled(&format!("Brush {i}"), &format!("Set {}", i / 50), t));
|
||||
}
|
||||
s.brush_presets_changed();
|
||||
let t0 = std::time::Instant::now();
|
||||
@@ -411,7 +411,7 @@ fn library(n: u32, side: u32) -> (Session, MemBackend) {
|
||||
let mut s = Session::new();
|
||||
s.attach_preset_store(open(Box::new(mem.clone())));
|
||||
for i in 0..n {
|
||||
s.tools.presets.push(sampled(&format!("Big {i}"), "Big Set", tip8(side, side, i)));
|
||||
std::sync::Arc::make_mut(&mut s.tools.presets).push(sampled(&format!("Big {i}"), "Big Set", tip8(side, side, i)));
|
||||
}
|
||||
s.brush_presets_changed();
|
||||
s.sync_preset_store();
|
||||
@@ -519,7 +519,7 @@ fn missing_corrupt_or_unknown_tips_fail_when_used_without_panicking() {
|
||||
let k0 = key(&t, "Big 0");
|
||||
mem.remove(&tip_file(&k0)).unwrap();
|
||||
t.execute("tools.setBrush", json!({"preset": "Hard Round"})).unwrap();
|
||||
let broken = t.tools.presets.iter_mut().find(|p| p.name == "Big 0").unwrap();
|
||||
let broken = std::sync::Arc::make_mut(&mut t.tools.presets).iter_mut().find(|p| p.name == "Big 0").unwrap();
|
||||
broken.captures.color = true;
|
||||
broken.brush.color = [1.0, 0.0, 0.0, 1.0];
|
||||
t.tools.foreground = [0.0, 0.0, 1.0, 1.0];
|
||||
|
||||
@@ -2063,3 +2063,16 @@ fn the_active_layer_marker_advances_for_a_new_document_and_not_for_a_same_layer_
|
||||
s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
|
||||
assert!(s.active_layer_changes() > before, "a new active document advances the marker");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sessions_share_builtin_brushes_until_edited() {
|
||||
let mut first = Session::new();
|
||||
let second = Session::new();
|
||||
assert!(std::sync::Arc::ptr_eq(&first.tools.presets, &second.tools.presets));
|
||||
let count = second.tools.presets.len();
|
||||
first.execute("brush.presets.save", serde_json::json!({"name": "Session-only test brush"})).unwrap();
|
||||
assert_eq!(second.tools.presets.len(), count);
|
||||
assert!(second.tools.presets.iter().all(|preset| preset.name != "Session-only test brush"));
|
||||
assert!(first.tools.presets.iter().any(|preset| preset.name == "Session-only test brush"));
|
||||
assert!(!std::sync::Arc::ptr_eq(&first.tools.presets, &second.tools.presets));
|
||||
}
|
||||
@@ -448,6 +448,12 @@ pub fn builtin() -> Vec<BrushPreset> {
|
||||
v
|
||||
}
|
||||
|
||||
/// The built-in set shared by new sessions until one is edited.
|
||||
pub fn builtin_shared() -> std::sync::Arc<Vec<BrushPreset>> {
|
||||
static BUILTIN: std::sync::OnceLock<std::sync::Arc<Vec<BrushPreset>>> = std::sync::OnceLock::new();
|
||||
BUILTIN.get_or_init(|| std::sync::Arc::new(builtin())).clone()
|
||||
}
|
||||
|
||||
/// Find a preset by name (case-insensitive).
|
||||
pub fn find<'a>(presets: &'a [BrushPreset], name: &str) -> Option<&'a BrushPreset> {
|
||||
presets.iter().find(|p| p.name.eq_ignore_ascii_case(name))
|
||||
|
||||
@@ -38,7 +38,7 @@ fn group(app: &PhotocraftApp, kind: Kind, name: &str) -> Value {
|
||||
}
|
||||
|
||||
fn library(app: &PhotocraftApp) -> Value {
|
||||
json!({"brushes": app.session.tools.presets, "gradients": app.session.presets.gradients, "selectedBrush": app.session.tools.brush})
|
||||
json!({"brushes": app.session.tools.presets.as_ref(), "gradients": app.session.presets.gradients, "selectedBrush": app.session.tools.brush})
|
||||
}
|
||||
|
||||
impl Fixture {
|
||||
@@ -74,7 +74,7 @@ impl Fixture {
|
||||
match kind {
|
||||
Kind::Brushes => {
|
||||
assert_eq!(original.as_array().unwrap().len(), 2);
|
||||
app.session.tools.presets.iter_mut().find(|p| p.group == stem).unwrap().brush.size = 77.0;
|
||||
std::sync::Arc::make_mut(&mut app.session.tools.presets).iter_mut().find(|p| p.group == stem).unwrap().brush.size = 77.0;
|
||||
}
|
||||
Kind::Gradients => {
|
||||
assert_eq!(original[0]["items"].as_array().unwrap().len(), 2);
|
||||
|
||||
@@ -487,14 +487,14 @@ fn preset_rows_nest_presets_in_panel_order() {
|
||||
let max = photocraft_engine::brush_preset_cmds::MAX_FOLDER_DEPTH;
|
||||
let deep: Vec<&str> = vec!["d"; max + 5];
|
||||
let mut app = app();
|
||||
app.session.tools.presets = vec![
|
||||
app.session.tools.presets = std::sync::Arc::new(vec![
|
||||
kit("Top", "Kit", &[]),
|
||||
kit("Pen", "Kit", &["Inks"]),
|
||||
kit("Chalk", "Kit", &["Dry"]),
|
||||
kit("Nib", "Kit", &["Inks", "Fine"]),
|
||||
kit("Quill", "Kit", &["Inks"]),
|
||||
kit("Deep", "Kit", &deep),
|
||||
];
|
||||
]);
|
||||
let out: Vec<String> = rows_of(app, PresetView::List)
|
||||
.iter()
|
||||
.map(|r| match r.count {
|
||||
@@ -509,7 +509,7 @@ fn preset_rows_nest_presets_in_panel_order() {
|
||||
|
||||
fn fixture() -> Box<PhotocraftApp> {
|
||||
let mut app = app();
|
||||
app.session.tools.presets = vec![
|
||||
app.session.tools.presets = std::sync::Arc::new(vec![
|
||||
kit("A1", "Alpha", &[]),
|
||||
kit("A2", "Alpha", &[]),
|
||||
kit("A3", "Alpha", &[]),
|
||||
@@ -518,7 +518,7 @@ fn fixture() -> Box<PhotocraftApp> {
|
||||
kit("Kit Pen", "Kit", &["Inks"]),
|
||||
kit("Kit Nib", "Kit", &["Inks", "Fine"]),
|
||||
kit("Kit Chalk", "Kit", &["Dry"]),
|
||||
];
|
||||
]);
|
||||
app
|
||||
}
|
||||
|
||||
|
||||
@@ -211,7 +211,7 @@ mod tests {
|
||||
assert!(prompt(&app).is_none(), "a new group imports without asking");
|
||||
assert!(app.ui.status.ends_with("brushes from Kit"), "{}", app.ui.status);
|
||||
// The user edits one of its brushes.
|
||||
let edited = app.session.tools.presets.iter_mut().find(|p| p.group == "Kit").unwrap();
|
||||
let edited = std::sync::Arc::make_mut(&mut app.session.tools.presets).iter_mut().find(|p| p.group == "Kit").unwrap();
|
||||
edited.brush.size = 77.0;
|
||||
let edited = edited.name.clone();
|
||||
|
||||
|
||||
Reference in new issue
Block a user