mirror of
https://github.com/storytold/figcraft.git
synced 2026-12-05 19:51:38 -05:00
Merge pull request #3009 from storytold/fix/3002-edit-image-fill-links
M1: Edit in PhotoCraft waits instead of guessing a fill, keeps links on refused writes and failed batches (#3002)
This commit is contained in:
+1
-1
@@ -374,7 +374,7 @@ Ranked; detail and milestones in [`docs/roadmap.md`](docs/roadmap.md).
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-12-04 | minor | M12, #173: session restore. Each window's place, size, state, screen, panels and tabs (saved files, branches and your online files, with their page and view) are kept with the preferences as they change and open again at the next start; off-screen windows move onto a screen; a crash while restoring makes the next start offer Restore last session; Start on the Home screen and `--fresh` start without it. Numbers unchanged. |
|
||||
| 2026-12-04 | minor | M1, #3002: Edit in PhotoCraft. The right-click menu on one image layer and Object ▸ Edit in PhotoCraft (`image.editInPhotoCraft`) write the layer's top image fill as its original file (PNG, JPEG, GIF, WebP, TIFF or BMP, never re-encoded) to a new file in a `FigCraft image edits` folder only this user can reach: the temporary folder, or on Linux and the BSDs the runtime folder (`XDG_RUNTIME_DIR`) or `~/.cache` and open it in PhotoCraft: the program `CRAFT_PHOTOCRAFT_BIN` names, else the one its installer registered (Windows), its app bundle (macOS) or `photocraft` on the `PATH`. Each save of that file comes back into the same fill (`image.syncEdits`, run by the desktop app's file watcher), one undo step each, keeping the fill's mode, crop and adjustments and the layer's size and place; a half-written save is read again at the next one, and a missing file keeps its link until it is saved again, a removed fill unlinks its file, and when several fills show the image and the linked one can't be told apart the save waits for the fill to be chosen again rather than guessing; each case says so. Without PhotoCraft the file is shown in its folder and a save from any editor updates the layer. Not in the browser, which can't start another app or see the file. Not tried against an installed PhotoCraft |
|
||||
| 2026-12-04 | minor | M1, #3002: Edit in PhotoCraft. The right-click menu on one image layer and Object ▸ Edit in PhotoCraft (`image.editInPhotoCraft`) write the layer's top image fill as its original file (PNG, JPEG, GIF, WebP, TIFF or BMP, never re-encoded) to a new file in a `FigCraft image edits` folder only this user can reach: the temporary folder, or on Linux and the BSDs the runtime folder (`XDG_RUNTIME_DIR`) or `~/.cache` and open it in PhotoCraft: the program `CRAFT_PHOTOCRAFT_BIN` names, else the one its installer registered (Windows), its app bundle (macOS) or `photocraft` on the `PATH`. Each save of that file comes back into the same fill (`image.syncEdits`, run by the desktop app's file watcher), one undo step each, keeping the fill's mode, crop and adjustments and the layer's size and place; a half-written save is read again at the next one, and a missing file keeps its link until it is saved again, a removed fill unlinks its file, and when another fill shows the image and the layer's fills changed since, the save waits for the fill to be chosen again rather than guessing; each case says so. Without PhotoCraft the file is shown in its folder and a save from any editor updates the layer. Not in the browser, which can't start another app or see the file. Not tried against an installed PhotoCraft |
|
||||
| 2026-12-04 | minor | M1, #614: Text ▸ Replace math with result (`text.calculate {ids?}`) turns each selected text layer, or text inside a selected frame, whose text is an expression into its value in one undo step: arithmetic with brackets (`(1920 - 2 * 24) / 3` → 624), number and string variables read in the layer's modes (`gap * 2`), joined text (`"Total: " + 3 * 5`) and comparisons. Text that isn't math stays as it was and is listed in `skipped`; the app says how many layers it left; with none that is math the command is refused. It uses the prototype expression reader, so it runs no scripts |
|
||||
| 2026-12-03 | minor | M1, #2946: notifications. Every message shown at the bottom of the window is kept for the session (the newest 200; the same message again moves to the top). A bell left of the theme toggle carries a dot while some are unread and opens the list: each message with how long ago it showed, selectable text and Copy; closing the list marks them read, Clear all empties it. Control channel: `ui.inspect` reports `notices`, `ui.set {notices}` opens, closes or clears it. Numbers unchanged |
|
||||
| 2026-12-02 | minor | M7, #1300: an Icons tab in the left sidebar (desktop) downloads openly licensed icon libraries on request, Phosphor Icons (six weights) and Bootstrap Icons (outline and fill), each a pinned, checksum-verified npm archive; its icons are searched by name and placed with a click at a chosen size and colour, as editable vector layers. `icons.libraries`, `icons.download`, `icons.search` and `icons.insert` work from the CLI, control channel and MCP; no command can allow downloading, which the first Download does. No drag onto the canvas, no removing a library from FigCraft, not in the browser. Numbers unchanged |
|
||||
|
||||
@@ -36,7 +36,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
spec!(
|
||||
"image.syncEdits",
|
||||
"Update edited images",
|
||||
"{} → {updated: [{id, path}], errors, busy?}: puts each file linked by `image.editInPhotoCraft` that was saved since into its fill, in one undo step, keeping the fill's mode, crop and adjustments. A missing file keeps its link until it is saved again; a removed fill unlinks it; when several fills show the image and the linked one can't be told apart, the save waits until the fill is linked again (an error says each). One that can't be read yet is read again at its next save. `busy`: a gesture, a text edit or an earlier version is on the canvas, so nothing was read",
|
||||
"{} → {updated: [{id, path}], errors, busy?}: puts each file linked by `image.editInPhotoCraft` that was saved since into its fill, in one undo step, keeping the fill's mode, crop and adjustments. A missing file keeps its link until it is saved again; a removed fill unlinks it; when another fill shows the image and the layer's fills changed since, so the linked one can't be told apart, the save waits until that fill is linked again (an error says each). One that can't be read yet is read again at its next save. `busy`: a gesture, a text edit or an earlier version is on the canvas, so nothing was read",
|
||||
true,
|
||||
sync_edits
|
||||
),
|
||||
@@ -241,14 +241,12 @@ pub struct ImageEdit {
|
||||
id: NodeId,
|
||||
/// Where the fill was when last put or linked.
|
||||
index: usize,
|
||||
/// The fill as last put or linked.
|
||||
paint: Paint,
|
||||
/// The layer's fills when last put or linked.
|
||||
fills: Vec<Paint>,
|
||||
/// Every image this link put in the fill, or found there: an undo can bring back any of them.
|
||||
images: Vec<String>,
|
||||
/// When last put or linked, another fill showed one of `images` (`shared`), or was `paint` itself
|
||||
/// (`twin`): then only an unmoved, unchanged fill is surely this one.
|
||||
/// Another fill of the layer showed one of `images` when last put or linked.
|
||||
shared: bool,
|
||||
twin: bool,
|
||||
/// A save waits, as the fill couldn't be told apart: linking the fill again puts it in.
|
||||
held: bool,
|
||||
path: String,
|
||||
@@ -259,16 +257,12 @@ pub struct ImageEdit {
|
||||
impl ImageEdit {
|
||||
/// Mark fill `index` of `fills` as this link's fill, as it is now.
|
||||
fn relink(&mut self, fills: &[Paint], index: usize) {
|
||||
let Some(fill) = fills.get(index) else { return };
|
||||
if let Some(key) = image_key(fill)
|
||||
&& !self.images.iter().any(|k| k == key)
|
||||
{
|
||||
let Some(key) = fills.get(index).and_then(image_key) else { return };
|
||||
if !self.images.iter().any(|k| k == key) {
|
||||
self.images.push(key.to_string());
|
||||
}
|
||||
let others = || fills.iter().enumerate().filter(move |(i, _)| *i != index).map(|(_, p)| p);
|
||||
self.shared = others().any(|p| image_key(p).is_some_and(|k| self.images.iter().any(|i| i == k)));
|
||||
self.twin = others().any(|p| p == fill);
|
||||
(self.index, self.paint, self.held) = (index, fill.clone(), false);
|
||||
self.shared = fills.iter().enumerate().any(|(i, p)| i != index && image_key(p).is_some_and(|k| self.images.iter().any(|i| i == k)));
|
||||
(self.index, self.fills, self.held) = (index, fills.to_vec(), false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,34 +283,33 @@ enum Found {
|
||||
Unclear,
|
||||
}
|
||||
|
||||
/// The fill of `fills` that `edit` links: the only one showing one of its images, unless another
|
||||
/// did when it was linked; else the one unchanged at its place; else the only one unchanged
|
||||
/// anywhere (a reorder), unless another was its twin. Anything else is unclear, and no fill is
|
||||
/// The fill of `fills` that `edit` links. Paints carry no identity, so only two cases are sure: the
|
||||
/// only fill showing one of its images, when no other fill showed one; or its place, while the
|
||||
/// layer's fills are all as they were when it was linked (two fills alike in every way can then
|
||||
/// swap places unseen, but they also look the same). Anything else is unclear, and no fill is
|
||||
/// guessed.
|
||||
fn linked_fill(fills: &[Paint], edit: &ImageEdit) -> Found {
|
||||
let ours: Vec<usize> = (0..fills.len()).filter(|&i| image_key(&fills[i]).is_some_and(|k| edit.images.iter().any(|i| i == k))).collect();
|
||||
let unchanged: Vec<usize> = ours.iter().copied().filter(|&i| fills[i] == edit.paint).collect();
|
||||
match (&ours[..], &unchanged[..]) {
|
||||
([], _) => Found::Gone,
|
||||
([only], _) if !edit.shared => Found::At(*only),
|
||||
_ if unchanged.contains(&edit.index) => Found::At(edit.index),
|
||||
(_, [only]) if !edit.twin => Found::At(*only),
|
||||
match &ours[..] {
|
||||
[] => Found::Gone,
|
||||
[only] if !edit.shared => Found::At(*only),
|
||||
_ if fills == edit.fills.as_slice() => Found::At(edit.index),
|
||||
_ => Found::Unclear,
|
||||
}
|
||||
}
|
||||
|
||||
/// The folder Edit in PhotoCraft writes images to, which only this user can reach: on Linux and
|
||||
/// the BSDs, where the temporary folder is shared by every user, the user's runtime or cache folder.
|
||||
fn edits_dir() -> std::path::PathBuf {
|
||||
/// `None` when there is none (no runtime folder or home).
|
||||
fn edits_dir() -> Option<std::path::PathBuf> {
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
let base = std::env::var_os("XDG_RUNTIME_DIR")
|
||||
.map(std::path::PathBuf::from)
|
||||
.filter(|d| d.is_absolute())
|
||||
.or_else(|| std::env::var_os("HOME").map(|h| std::path::PathBuf::from(h).join(".cache")))
|
||||
.unwrap_or_else(std::env::temp_dir);
|
||||
.or_else(|| std::env::var_os("HOME").map(|h| std::path::PathBuf::from(h).join(".cache")).filter(|d| d.is_absolute()))?;
|
||||
#[cfg(not(all(unix, not(target_os = "macos"))))]
|
||||
let base = std::env::temp_dir();
|
||||
base.join("FigCraft image edits")
|
||||
Some(base.join("FigCraft image edits"))
|
||||
}
|
||||
|
||||
fn edit_in_photocraft(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
@@ -333,20 +326,33 @@ fn edit_in_photocraft(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
let file = crate::asset_handoff::image_file(doc, image).map_err(|why| bad(cmd, why))?;
|
||||
let ds = s.ds()?;
|
||||
// This fill's link, wherever a reorder left it; or one waiting because it lost track of its fill.
|
||||
let linked = ds.image_edits.iter().position(|e| e.id == id && linked_fill(fills, e) == Found::At(index)).or_else(|| {
|
||||
ds.image_edits
|
||||
.iter()
|
||||
.position(|e| e.id == id && (e.held || linked_fill(fills, e) != Found::At(e.index)) && e.images.iter().any(|k| k == image))
|
||||
// This fill's link; or the only one of this image that lost track of its fill, which the user
|
||||
// now points at. With several such links none is guessed: the fill gets a new file.
|
||||
let lost: Vec<usize> = (0..ds.image_edits.len())
|
||||
.filter(|&i| {
|
||||
let e = &ds.image_edits[i];
|
||||
e.id == id && linked_fill(fills, e) == Found::Unclear && e.images.iter().any(|k| k == image)
|
||||
})
|
||||
.collect();
|
||||
let linked = ds.image_edits.iter().position(|e| e.id == id && linked_fill(fills, e) == Found::At(index)).or(match lost[..] {
|
||||
[only] => Some(only),
|
||||
_ => None,
|
||||
});
|
||||
let path = match (string(p, "path"), linked) {
|
||||
(Some(path), _) => path,
|
||||
(None, Some(i)) => ds.image_edits[i].path.clone(),
|
||||
(None, None) => {
|
||||
let dir = edits_dir();
|
||||
let dir = edits_dir().ok_or_else(|| bad(cmd, "there's no folder of your own to write the image to: pass `path`"))?;
|
||||
crate::file_access::create_dir_all(&dir)?;
|
||||
// A file of its own, never one left by another run: an editor may still have it open.
|
||||
let stem = format!("{}-{}-{}-{index}", crate::asset_handoff::file_stem(&file.hash), ds.uid, id.0);
|
||||
// Only this user may read or replace the images in it.
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt as _;
|
||||
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).map_err(|e| bad(cmd, e.to_string()))?;
|
||||
}
|
||||
// A file of its own: the process keeps two running apps apart, and a name left by an
|
||||
// earlier run (an editor may still have it open) is skipped.
|
||||
let stem = format!("{}-{}-{}-{}-{index}", crate::asset_handoff::file_stem(&file.hash), std::process::id(), ds.uid, id.0);
|
||||
let taken = |p: &str| crate::file_access::exists(p) || ds.image_edits.iter().any(|e| e.path == p);
|
||||
(0..)
|
||||
.map(|n| {
|
||||
@@ -356,38 +362,38 @@ fn edit_in_photocraft(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
};
|
||||
let (image, format, bytes) = (image.to_string(), file.ext, file.bytes.clone());
|
||||
let (image, format) = (image.to_string(), file.ext);
|
||||
let fills = fills.clone();
|
||||
let mut edits = std::mem::take(&mut s.ds_mut()?.image_edits);
|
||||
let mut edit = match linked.map(|i| edits.remove(i)) {
|
||||
// Editing the same fill again reopens its file: writing over it would lose a save not taken in
|
||||
// yet, and one held back goes in now.
|
||||
Some(mut link) if link.path == path && crate::file_access::exists(&path) => {
|
||||
// Editing the same fill again reopens its file: writing over it would lose a save not taken in
|
||||
// yet, and one held back goes in now. Otherwise the file is written before any link changes,
|
||||
// so a refused write leaves every link as it was.
|
||||
let reopen = linked.filter(|&i| ds.image_edits[i].path == path && crate::file_access::exists(&path));
|
||||
let written = match reopen {
|
||||
Some(_) => None,
|
||||
None => {
|
||||
crate::file_access::write(&path, &file.bytes)?;
|
||||
Some(crate::file_access::stamp(&path).ok())
|
||||
}
|
||||
};
|
||||
let edits = &mut s.ds_mut()?.image_edits;
|
||||
let mut edit = match (reopen, written) {
|
||||
(Some(i), _) => {
|
||||
let mut link = edits.remove(i);
|
||||
if link.held {
|
||||
link.stamp = None;
|
||||
}
|
||||
link
|
||||
}
|
||||
_ => {
|
||||
crate::file_access::write(&path, &bytes)?;
|
||||
let stamp = crate::file_access::stamp(&path).ok();
|
||||
ImageEdit {
|
||||
id,
|
||||
index,
|
||||
paint: fills[index].clone(),
|
||||
images: Vec::new(),
|
||||
shared: false,
|
||||
twin: false,
|
||||
held: false,
|
||||
path: path.clone(),
|
||||
stamp,
|
||||
(None, stamp) => {
|
||||
if let Some(i) = linked {
|
||||
edits.remove(i);
|
||||
}
|
||||
ImageEdit { id, index, fills: Vec::new(), images: Vec::new(), shared: false, held: false, path: path.clone(), stamp: stamp.flatten() }
|
||||
}
|
||||
};
|
||||
edit.relink(&fills, index);
|
||||
edits.retain(|e| e.path != path);
|
||||
edits.push(edit);
|
||||
s.ds_mut()?.image_edits = edits;
|
||||
s.request_ui("open.photocraft", json!({"path": path}));
|
||||
Ok(json!({"path": path, "image": image, "format": format}))
|
||||
}
|
||||
@@ -410,14 +416,18 @@ fn sync_edits(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
if ds.interaction.is_some() || ds.text_edit.is_some() || ds.preview.is_some() || read_only {
|
||||
return Ok(json!({"updated": [], "errors": [], "busy": true}));
|
||||
}
|
||||
let mut edits = std::mem::take(&mut ds.image_edits);
|
||||
let edits = std::mem::take(&mut ds.image_edits);
|
||||
let before = ds.doc.clone();
|
||||
let (mut updated, mut errors) = (Vec::new(), Vec::new());
|
||||
edits.retain_mut(|edit| {
|
||||
// A deleted layer has nothing to update.
|
||||
if ds.doc.node(edit.id).is_none() {
|
||||
return false;
|
||||
}
|
||||
// Each link's fill is found before any save changes the fills; a deleted layer unlinks.
|
||||
let mut kept: Vec<(ImageEdit, Found)> = edits
|
||||
.into_iter()
|
||||
.filter_map(|e| {
|
||||
let found = ds.doc.node(e.id).map(|n| linked_fill(&n.fills, &e))?;
|
||||
Some((e, found))
|
||||
})
|
||||
.collect();
|
||||
kept.retain_mut(|(edit, found)| {
|
||||
let stamp = crate::file_access::stamp(&edit.path).ok();
|
||||
if stamp == edit.stamp {
|
||||
return true;
|
||||
@@ -430,9 +440,8 @@ fn sync_edits(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
}
|
||||
// A half-written save fails here and is read again once the editor finishes it (a new stamp).
|
||||
edit.stamp = stamp;
|
||||
let fills = ds.doc.node(edit.id).map(|n| &n.fills[..]).unwrap_or_default();
|
||||
let index = match linked_fill(fills, edit) {
|
||||
Found::At(index) => index,
|
||||
let index = match found {
|
||||
Found::At(index) => *index,
|
||||
Found::Gone => {
|
||||
errors.push(format!("{}: the layer no longer has that image fill, so saving it no longer updates the layer", edit.path));
|
||||
return false;
|
||||
@@ -452,21 +461,22 @@ fn sync_edits(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
.and_then(|bytes| super::create::add_readable_image(doc, bytes))
|
||||
.and_then(|(image, ..)| put_image(doc, &[edit.id], false, Some(index), &image).map_err(|e| e.to_string()));
|
||||
match put {
|
||||
Ok(_) => {
|
||||
if let Some(n) = doc.node(edit.id) {
|
||||
edit.relink(&n.fills, index);
|
||||
}
|
||||
updated.push(json!({"id": edit.id, "path": edit.path}));
|
||||
}
|
||||
Ok(_) => updated.push(json!({"id": edit.id, "path": edit.path})),
|
||||
Err(e) => errors.push(format!("{}: {e}", edit.path)),
|
||||
}
|
||||
true
|
||||
});
|
||||
ds.image_edits = edits;
|
||||
// Saves that all failed leave no undo step: the document goes back to the one it was.
|
||||
if updated.is_empty() {
|
||||
ds.doc = before;
|
||||
}
|
||||
// A link sure of its fill stays sure after these saves changed the layer's fills.
|
||||
for (edit, found) in &mut kept {
|
||||
if let (Found::At(index), Some(n)) = (&*found, ds.doc.node(edit.id)) {
|
||||
edit.relink(&n.fills, *index);
|
||||
}
|
||||
}
|
||||
ds.image_edits = kept.into_iter().map(|(e, _)| e).collect();
|
||||
Ok(json!({"updated": updated, "errors": errors}))
|
||||
}
|
||||
|
||||
|
||||
@@ -233,7 +233,7 @@ fn each_fill_of_one_image_has_its_own_file_and_takes_only_its_saves() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fill_moved_past_another_of_the_same_image_keeps_its_saves() {
|
||||
fn a_fill_moved_past_another_of_its_image_waits_then_takes_the_save_once_chosen() {
|
||||
let dir = folder("same-image-moved");
|
||||
let (mut s, id) = two_fills_of_one_image();
|
||||
let path = dir.join("edit.png").to_string_lossy().into_owned();
|
||||
@@ -242,12 +242,19 @@ fn a_fill_moved_past_another_of_the_same_image_keeps_its_saves() {
|
||||
s.execute("fill.move", &json!({"ids": [id], "from": 0, "to": 1})).unwrap();
|
||||
let original = images(&s, id);
|
||||
save(&path, &png(2));
|
||||
sync(&mut s);
|
||||
assert!(sync(&mut s)["errors"][0].as_str().unwrap().contains("the save waits"));
|
||||
assert_eq!(images(&s, id), original, "the plain fill now at its old place is left alone");
|
||||
assert_eq!(
|
||||
s.execute("image.editInPhotoCraft", &json!({"ids": [id], "index": 1})).unwrap()["path"],
|
||||
path.as_str(),
|
||||
"choosing it reopens its file"
|
||||
);
|
||||
assert_eq!(sync(&mut s)["updated"].as_array().unwrap().len(), 1);
|
||||
let fills = &s.doc().unwrap().node(id).unwrap().fills;
|
||||
let PaintKind::Image(top) = &fills[1].kind else { panic!() };
|
||||
assert_ne!(top.filters, figcraft_color::ImageFilters::default(), "the vivid fill is on top");
|
||||
assert_eq!(top.image, figcraft_format::image_hash(&png(2)), "and took the save");
|
||||
assert_eq!(images(&s, id)[0], original[0], "the plain fill now at its old place is left alone");
|
||||
assert_eq!(images(&s, id)[0], original[0]);
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
@@ -288,19 +295,96 @@ fn a_changed_fill_moved_past_another_of_its_image_takes_no_save_by_guess() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reopening_a_moved_fill_opens_its_own_file() {
|
||||
fn reopening_one_of_two_moved_fills_never_opens_the_others_file() {
|
||||
let (mut s, id) = two_fills_of_one_image();
|
||||
let vivid = s.execute("image.editInPhotoCraft", &json!({"ids": [id], "index": 0})).unwrap()["path"].as_str().unwrap().to_string();
|
||||
let plain = s.execute("image.editInPhotoCraft", &json!({"ids": [id], "index": 1})).unwrap()["path"].as_str().unwrap().to_string();
|
||||
s.execute("fill.move", &json!({"ids": [id], "from": 0, "to": 1})).unwrap();
|
||||
// Both links lost track of their fills: the vivid one, now on top, gets a file of its own.
|
||||
let reopened = s.execute("image.editInPhotoCraft", &json!({"ids": [id], "index": 1})).unwrap()["path"].as_str().unwrap().to_string();
|
||||
assert_eq!(reopened, vivid, "the vivid fill, now on top, keeps its file");
|
||||
assert_ne!(reopened, plain);
|
||||
save(&reopened, &png(2));
|
||||
sync(&mut s);
|
||||
let fills = &s.doc().unwrap().node(id).unwrap().fills;
|
||||
let PaintKind::Image(top) = &fills[1].kind else { panic!() };
|
||||
assert_eq!(top.image, figcraft_format::image_hash(&png(2)), "its save goes into it");
|
||||
for p in [vivid, plain] {
|
||||
assert_ne!(images(&s, id)[0], top.image, "and not into the other");
|
||||
for p in [vivid, plain, reopened] {
|
||||
let _ = std::fs::remove_file(p);
|
||||
}
|
||||
}
|
||||
|
||||
/// A layer showing the same image in two fills alike in every way.
|
||||
fn two_identical_fills() -> (Session, figcraft_doc::NodeId) {
|
||||
let (mut s, id) = placed();
|
||||
let top = s.doc().unwrap().node(id).unwrap().fills[0].clone();
|
||||
s.execute("fill.add", &json!({"ids": [id], "paint": top})).unwrap();
|
||||
(s, id)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn when_the_linked_fill_is_removed_an_identical_one_doesn_t_take_its_save() {
|
||||
let dir = folder("identical-removed");
|
||||
let (mut s, id) = two_identical_fills();
|
||||
let path = dir.join("edit.png").to_string_lossy().into_owned();
|
||||
s.execute("image.editInPhotoCraft", &json!({"ids": [id], "index": 0, "path": path})).unwrap();
|
||||
s.execute("fill.remove", &json!({"ids": [id], "index": 0})).unwrap();
|
||||
let original = images(&s, id);
|
||||
save(&path, &png(2));
|
||||
assert!(!sync(&mut s)["errors"].as_array().unwrap().is_empty());
|
||||
assert_eq!(images(&s, id), original, "the remaining fill keeps its image");
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_traded_between_two_fills_of_one_image_make_the_save_wait() {
|
||||
let dir = folder("traded");
|
||||
let (mut s, id) = placed();
|
||||
let top = s.doc().unwrap().node(id).unwrap().fills[0].clone();
|
||||
s.execute("fill.add", &json!({"ids": [id], "paint": top})).unwrap();
|
||||
s.execute("image.adjust", &json!({"ids": [id], "index": 1, "preset": "vivid"})).unwrap();
|
||||
let path = dir.join("edit.png").to_string_lossy().into_owned();
|
||||
s.execute("image.editInPhotoCraft", &json!({"ids": [id], "index": 1, "path": path})).unwrap();
|
||||
s.execute("image.adjust", &json!({"ids": [id], "index": 1, "preset": "muted"})).unwrap();
|
||||
s.execute("image.adjust", &json!({"ids": [id], "index": 0, "preset": "vivid"})).unwrap();
|
||||
let original = images(&s, id);
|
||||
save(&path, &png(2));
|
||||
assert!(sync(&mut s)["errors"][0].as_str().unwrap().contains("the save waits"));
|
||||
assert_eq!(images(&s, id), original, "the fill that took the old settings isn't taken for it");
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_refused_write_keeps_the_other_links() {
|
||||
let dir = folder("refused");
|
||||
let (mut s, id) = placed();
|
||||
let path = dir.join("edit.png").to_string_lossy().into_owned();
|
||||
s.execute("image.editInPhotoCraft", &json!({"ids": [id], "path": path})).unwrap();
|
||||
let other = id_of(&s.execute("image.place", &json!({"data": base64::engine::general_purpose::STANDARD.encode(png(4))})).unwrap()["id"]).unwrap();
|
||||
// A folder can't be written as a file.
|
||||
assert!(s.execute("image.editInPhotoCraft", &json!({"ids": [other], "path": dir.to_string_lossy()})).is_err());
|
||||
assert_eq!(edit_paths(&s), std::slice::from_ref(&path), "the first link stays");
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_batch_leaves_its_save_to_be_taken_in_again() {
|
||||
let dir = folder("batch");
|
||||
let (mut s, id) = placed();
|
||||
let path = dir.join("edit.png").to_string_lossy().into_owned();
|
||||
s.execute("image.editInPhotoCraft", &json!({"ids": [id], "path": path})).unwrap();
|
||||
let original = shown(&s, id);
|
||||
save(&path, &png(2));
|
||||
let failed = s.execute(
|
||||
"command.batch",
|
||||
&json!({"commands": [
|
||||
{"command": "image.syncEdits", "params": {}},
|
||||
{"command": "node.set", "params": {"id": 999999, "props": {"name": "must fail"}}}
|
||||
]}),
|
||||
);
|
||||
assert!(failed.is_err());
|
||||
assert_eq!(shown(&s, id), original, "the batch rolled the save back");
|
||||
assert!(edits_saved(&s), "so the file still counts as saved");
|
||||
assert_eq!(sync(&mut s)["updated"].as_array().unwrap().len(), 1);
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ struct GroupDocCheckpoint {
|
||||
redo: Vec<HistoryEntry>,
|
||||
limit: usize,
|
||||
group_step: Option<usize>,
|
||||
/// A rolled-back `image.syncEdits` must leave its saves to be taken in again.
|
||||
image_edits: Vec<cmd::image::ImageEdit>,
|
||||
}
|
||||
|
||||
struct GroupCheckpoint {
|
||||
@@ -336,6 +338,7 @@ impl Session {
|
||||
redo: ds.history.redo.clone(),
|
||||
limit: ds.history.limit,
|
||||
group_step: ds.history.group_step,
|
||||
image_edits: ds.image_edits.clone(),
|
||||
})
|
||||
.collect();
|
||||
let before = GroupCheckpoint {
|
||||
@@ -380,6 +383,7 @@ impl Session {
|
||||
ds.history.undo = saved.undo;
|
||||
ds.history.limit = saved.limit;
|
||||
ds.history.group_step = saved.group_step;
|
||||
ds.image_edits = saved.image_edits;
|
||||
}
|
||||
}
|
||||
self.active = before.active;
|
||||
|
||||
+3
-3
@@ -421,9 +421,9 @@ ArtCraft Launcher's agents switch for FigCraft.
|
||||
agent edits the file itself. `image.syncEdits` then puts every linked file saved since into its
|
||||
fill, one undo step, keeping the fill's mode, crop and adjustments, and returns
|
||||
`{updated: [{id, path}], errors}`. A missing file keeps its link until it is saved again; a
|
||||
removed fill unlinks its file; when several fills show the image and the linked one changed or
|
||||
moved so it can't be told apart, no fill is guessed: the save waits until that fill is linked
|
||||
again. A file that can't be decoded yet is read again at its next save. The desktop app runs it when it notices the save.
|
||||
removed fill unlinks its file; fills have no identity, so when another fill of the layer shows the
|
||||
image and the layer's fills changed since, no fill is guessed: the save waits until that fill is
|
||||
linked again, which reopens its file. A file that can't be decoded yet is read again at its next save. The desktop app runs it when it notices the save.
|
||||
- Text props (`fontFamily`, `fontStyle`, `fontSize`, `lineHeight`, `letterSpacing`, `textDecoration`,
|
||||
`textCase`, `textFills`, `fontVariations`…) apply to the whole layer, or to chars `start`..`end`
|
||||
when given (`text.style {id, start, end, …}` does the same). Styled ranges come back as
|
||||
|
||||
Reference in New Issue
Block a user