mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
* Include tool presets in export and import so crop presets are portable. * Pass presets_io_body to the harness without a redundant closure (#6560). CI clippy -D warnings failed on clippy::redundant_closure in the presets export/import regression.
This commit is contained in:
1 parent
aa8621788e
commit
4bf0625b04
4 files changed
+105
-8
No files matched your search
@@ -1354,6 +1354,7 @@ pub struct PresetFile {
|
||||
pub version: u32,
|
||||
pub brushes: Vec<photocraft_paint::BrushPreset>,
|
||||
pub custom_shapes: Vec<CustomShape>,
|
||||
pub tool_presets: Vec<crate::presets::tools::ToolPreset>,
|
||||
}
|
||||
|
||||
pub const PRESET_FORMAT: &str = "photocraft-presets";
|
||||
@@ -1364,7 +1365,7 @@ fn export_import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.get("kinds")
|
||||
.and_then(Value::as_array)
|
||||
.map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect())
|
||||
.unwrap_or_else(|| ["brushes", "customShapes"].iter().map(|k| k.to_string()).collect());
|
||||
.unwrap_or_else(|| ["brushes", "customShapes", "tools"].iter().map(|k| k.to_string()).collect());
|
||||
let want = |k: &str| kinds.iter().any(|x| x == k);
|
||||
match str_or(p, "action", "export") {
|
||||
"export" => {
|
||||
@@ -1383,8 +1384,14 @@ fn export_import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Vec::new()
|
||||
},
|
||||
custom_shapes: if want("customShapes") { s.edit_state.custom_shapes.clone() } else { Vec::new() },
|
||||
tool_presets: if want("tools") {
|
||||
let builtins = crate::presets::tools::builtin();
|
||||
s.presets.tool_presets.iter().filter(|preset| bool_or(p, "includeBuiltins", false) || !builtins.contains(preset)).cloned().collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
},
|
||||
};
|
||||
Ok(json!({"data": file, "brushes": file.brushes.len(), "customShapes": file.custom_shapes.len()}))
|
||||
Ok(json!({"data": file, "brushes": file.brushes.len(), "customShapes": file.custom_shapes.len(), "tools": file.tool_presets.len()}))
|
||||
}
|
||||
"import" => {
|
||||
let data = p.get("data").cloned().ok_or_else(|| bad(cmd, "missing `data` (an exported preset file)"))?;
|
||||
@@ -1396,6 +1403,29 @@ fn export_import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if file.format != PRESET_FORMAT {
|
||||
return Err(bad(cmd, format!("not a preset file (format `{}`)", file.format)));
|
||||
}
|
||||
// Validate the whole tool import before changing any preset library.
|
||||
let mut tools = s.presets.tool_presets.clone();
|
||||
let nt = if want("tools") { file.tool_presets.len() } else { 0 };
|
||||
if nt > crate::presets::tools::MAX_PRESETS {
|
||||
return Err(bad(cmd, "too many tool presets"));
|
||||
}
|
||||
if want("tools") {
|
||||
for mut preset in file.tool_presets {
|
||||
preset.name = crate::presets::tools::checked_name(&preset.name, cmd)?;
|
||||
if preset.tool.trim().is_empty() || !preset.options.is_object() {
|
||||
return Err(bad(cmd, "tool presets require a non-empty tool and an options object"));
|
||||
}
|
||||
if let Some(old) = tools.iter_mut().find(|old| old.name == preset.name && old.tool.eq_ignore_ascii_case(&preset.tool)) {
|
||||
*old = preset;
|
||||
} else if tools.iter().any(|old| old.name == preset.name) {
|
||||
return Err(bad(cmd, "a preset of that name belongs to another tool; rename it before importing"));
|
||||
} else if tools.len() < crate::presets::tools::MAX_PRESETS {
|
||||
tools.push(preset);
|
||||
} else {
|
||||
return Err(bad(cmd, "too many tool presets"));
|
||||
}
|
||||
}
|
||||
}
|
||||
let (mut nb, mut ns) = (0, 0);
|
||||
if want("brushes") {
|
||||
for mut b in file.brushes {
|
||||
@@ -1420,7 +1450,11 @@ fn export_import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
ns += 1;
|
||||
}
|
||||
}
|
||||
Ok(json!({"brushes": nb, "customShapes": ns}))
|
||||
if want("tools") {
|
||||
s.presets.tool_presets = tools;
|
||||
s.presets_changed();
|
||||
}
|
||||
Ok(json!({"brushes": nb, "customShapes": ns, "tools": nt}))
|
||||
}
|
||||
other => Err(bad(cmd, format!("unknown action `{other}` (export|import)"))),
|
||||
}
|
||||
@@ -1512,7 +1546,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Export/Import Presets…",
|
||||
["Edit", "Presets"],
|
||||
None,
|
||||
r##"{"action":"export|import","kinds":["brushes","customShapes"]?,"data":json (import),"includeBuiltins":bool=false}"##,
|
||||
r##"{"action":"export|import","kinds":["brushes","customShapes","tools"]?,"data":json (import),"includeBuiltins":bool=false}"##,
|
||||
always,
|
||||
export_import
|
||||
),
|
||||
|
||||
@@ -693,3 +693,49 @@ fn fade_allocation_failure_keeps_document_and_history() {
|
||||
assert_eq!(*s.active().unwrap().doc, *before);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_presets_round_trip_through_export_import() {
|
||||
let mut source = Session::new();
|
||||
source.execute("tool.presets.new", json!({"name":"2 : 3", "tool":"crop", "options":{"toolOptions":{"crop_ratio":"2:3"}}})).unwrap();
|
||||
let exported = source.execute("edit.presets.exportImportPresets", json!({"action":"export"})).unwrap();
|
||||
assert_eq!(exported["tools"], 1);
|
||||
assert_eq!(exported["data"]["toolPresets"][0]["name"], "2 : 3");
|
||||
let mut target = Session::new();
|
||||
target.execute("tool.presets.new", json!({"name":"2 : 3", "tool":"crop", "options":{"toolOptions":{"crop_ratio":"1:1"}}})).unwrap();
|
||||
for _ in 0..2 {
|
||||
let result = target.execute("edit.presets.exportImportPresets", json!({"action":"import", "data":exported["data"]})).unwrap();
|
||||
assert_eq!(result["tools"], 1);
|
||||
}
|
||||
let selected = target.execute("tool.presets.select", json!({"preset":"2 : 3"})).unwrap();
|
||||
assert_eq!(selected["options"]["toolOptions"]["crop_ratio"], "2:3");
|
||||
assert_eq!(target.presets.tool_presets.iter().filter(|p| p.name == "2 : 3").count(), 1);
|
||||
let only_brushes = source.execute("edit.presets.exportImportPresets", json!({"kinds":["brushes"]})).unwrap();
|
||||
assert_eq!(only_brushes["tools"], 0);
|
||||
let with_builtins = source.execute("edit.presets.exportImportPresets", json!({"kinds":["tools"], "includeBuiltins":true})).unwrap();
|
||||
assert_eq!(with_builtins["tools"].as_u64().unwrap() as usize, source.presets.tool_presets.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_preset_import_is_validated_before_mutation_and_accepts_old_files() {
|
||||
let mut s = Session::new();
|
||||
let before = s.presets.tool_presets.clone();
|
||||
for preset in [
|
||||
json!({"name":"", "tool":"crop", "options":{}}),
|
||||
json!({"name":"x".repeat(256), "tool":"crop", "options":{}}),
|
||||
json!({"name":"Crop", "tool":" ", "options":{}}),
|
||||
json!({"name":"Crop", "tool":"crop", "options":[]}),
|
||||
] {
|
||||
let result = s.execute(
|
||||
"edit.presets.exportImportPresets",
|
||||
json!({"action":"import", "data":{"format":PRESET_FORMAT, "toolPresets":[{"name":"Valid", "tool":"crop", "options":{}}, preset]}}),
|
||||
);
|
||||
assert!(result.is_err());
|
||||
assert_eq!(s.presets.tool_presets, before);
|
||||
}
|
||||
let too_many: Vec<_> = (0..crate::presets::tools::MAX_PRESETS + 1).map(|i| json!({"name":format!("Crop {i}"), "tool":"crop", "options":{}})).collect();
|
||||
assert!(s.execute("edit.presets.exportImportPresets", json!({"action":"import", "data":{"format":PRESET_FORMAT, "toolPresets":too_many}})).is_err());
|
||||
assert_eq!(s.presets.tool_presets, before);
|
||||
let result = s.execute("edit.presets.exportImportPresets", json!({"action":"import", "data":{"format":PRESET_FORMAT, "version":1}})).unwrap();
|
||||
assert_eq!(result["tools"], 0);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ impl Named for ToolPreset {
|
||||
pub const MAX_PRESETS: usize = 1000;
|
||||
|
||||
/// A preset name: trimmed, 1–[`MAX_NAME`](crate::brush_preset_cmds::MAX_NAME) characters.
|
||||
fn checked_name(name: &str, cmd: &str) -> Result<String> {
|
||||
pub(crate) fn checked_name(name: &str, cmd: &str) -> Result<String> {
|
||||
let name = name.trim();
|
||||
if name.is_empty() {
|
||||
return Err(bad(cmd, "the preset name is empty"));
|
||||
|
||||
@@ -916,7 +916,7 @@ pub fn invoke(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: &V
|
||||
Some(Ok(json!({"dialog": open_kind(app, "presets", "Preset Manager", json!({"kind": "brushes", "selected": 0, "newName": ""}))})))
|
||||
}
|
||||
"edit.presets.exportImportPresets" => Some(Ok(
|
||||
json!({"dialog": open_kind(app, "presetsIO", "Export/Import Presets", json!({"action": "export", "brushes": true, "customShapes": true}))}),
|
||||
json!({"dialog": open_kind(app, "presetsIO", "Export/Import Presets", json!({"action": "export", "brushes": true, "customShapes": true, "tools": true}))}),
|
||||
)),
|
||||
_ => None,
|
||||
}
|
||||
@@ -2318,7 +2318,7 @@ fn presets_io_body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
|
||||
180.0,
|
||||
);
|
||||
});
|
||||
for (k, label) in [("brushes", tl!("Brushes")), ("customShapes", tl!("Custom Shapes"))] {
|
||||
for (k, label) in [("brushes", tl!("Brushes")), ("customShapes", tl!("Custom Shapes")), ("tools", tl!("Tool Presets"))] {
|
||||
let mut on = f.get(k).and_then(Value::as_bool).unwrap_or(true);
|
||||
crate::widgets::checkbox(ui, &mut on, label);
|
||||
f.insert(k.into(), json!(on));
|
||||
@@ -2442,7 +2442,7 @@ pub fn confirm(app: &mut PhotocraftApp, f: &Map<String, Value>) -> Result<Value,
|
||||
}
|
||||
"presets" => Ok(Value::Null),
|
||||
"presetsIO" => {
|
||||
let kinds: Vec<&str> = ["brushes", "customShapes"].into_iter().filter(|k| f.get(*k).and_then(Value::as_bool).unwrap_or(true)).collect();
|
||||
let kinds: Vec<&str> = ["brushes", "customShapes", "tools"].into_iter().filter(|k| f.get(*k).and_then(Value::as_bool).unwrap_or(true)).collect();
|
||||
if f.get("action").and_then(Value::as_str) == Some("import") {
|
||||
app.pick_file_bytes(move |app, name, bytes| {
|
||||
let text = String::from_utf8(bytes).map_err(|_| format!("{name} is not a preset file"))?;
|
||||
@@ -3551,6 +3551,23 @@ mod tests {
|
||||
assert!(store2.lock().unwrap().as_ref().unwrap().contains("classic"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preset_export_dialog_offers_tool_presets() {
|
||||
use egui_kittest::{Harness, kittest::Queryable};
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
let ctx = egui::Context::default();
|
||||
let opened = crate::menus::invoke(&mut app, &ctx, "edit.presets.exportImportPresets", json!({})).unwrap();
|
||||
let fields = app.ui.dialog_mut(opened["dialog"].as_u64().unwrap()).unwrap().fields.clone();
|
||||
assert_eq!(fields["tools"], true);
|
||||
let mut h = Harness::builder().with_size(vec2(420.0, 260.0)).build_ui_state(presets_io_body, fields);
|
||||
h.run_steps(2);
|
||||
h.get_by_label("Tool Presets").click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state()["tools"], false);
|
||||
assert_eq!(h.state()["brushes"], true);
|
||||
assert_eq!(h.state()["customShapes"], true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn brush_preset_store_attaches_when_loaded_and_persists() {
|
||||
use photocraft_engine::preset_store::{MemBackend, open};
|
||||
|
||||
Reference in new issue
Block a user