mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-16 15:14:38 +00:00
Place decodes and fits in the background: dropping a large file no longer freezes the window (#10147)
* Place decodes, fits and renders in a background job File > Place Embedded / Linked started with Session::start, and Session::start_place_bytes for bytes with no path, now run the decode, flatten, resize and SVG render on a worker (prepare_place) and insert the layer when the job is applied (apply_place). Inline callers (execute, the CLI, headless MCP, actions) behave and return exactly as before. Part of #10031. * Dropped and placed files no longer freeze the window on desktop With background jobs on, File > Place and files dropped on the canvas start the place as a job (JobsUi.place). Free Transform begins when the layer has landed, a failed or cancelled place is reported and the drop queue goes on, and a drop that arrives meanwhile waits for it. The place shows its progress in the status bar instead of the modal dialog, so the view stays usable. Part of #10031. * Tests: compare place job results without the layer id Layer ids come from a counter shared by every session, so a job and an inline place in two sessions never get the same one. * Dropped files stay with the document they were dropped on Each queued canvas drop now carries its document's id. A file is placed only while that document is active: files dropped on a document in another tab wait until it is back in view, and are discarded with a message when it is closed. Web drops, read by the shell after the drop, go to the document the drop landed on. Before, the next queued file went into whichever document was active when the previous place finished, failed or was cancelled, in the background and inline paths alike. * Remove the unused file_open import * fix: address background placement review feedback * fix: preserve drop targets and interrupt placement pixel stages * fix: qualify placement inbox document identity * test: wait for background placement in app drop regression * fix: retain queued drops while external document jobs run * test: clone rendered pixels from drop-managed buffers * test: cover band rows channels and queued-drop resumption * Place decodes and fits in the background: dropping a large file no longer freezes the window Placing a file decoded, flattened and resized on the UI thread; a large PNG froze the window for seconds (#10031). `place_bytes` is split into a session read, `prepare_place` (decode, flatten, fit, translate, SVG render at placement scale) and `apply_place`, and runs through `jobs::run`, so File › Place and canvas drops run on a worker with progress and cancel. The window follows the job: status-bar progress, Free Transform when the layer lands, and drops queued per target document. Review fixes on the way: - a path and byte drop queue entry records its target DocId, so files dropped on one document never land in another after a tab switch, and closed documents discard theirs; - the closed-document notice stays up for a frame instead of being cleared by the next queued placement; - the queue is bounded (32 files / 256 MiB) so drops waiting on another tab cannot grow it without limit; - flatten and resize stop at the job's interrupt, and a place skips the modal progress dialog only when the status bar actually shows its progress; - the placement's `doc` is kept only for SVG input, through the importer's `is_svg_input`; - the interrupted-composite test uses an opaque fixture that varies by row and channel, and checks the composite against the layer's stored pixels. * Place reads its file in the job, and refuses a huge canvas as before file.placeEmbedded and file.placeLinked read the whole file in the command before the job started, so a path dropped on the canvas or picked in File > Place still blocked the window for the read. The place now carries the path and reads it in prepare_place: on the job's worker in the background, on the calling thread inline (wasm, background jobs off), with the same read_file limit and error text. Whether the source is a vector one is decided once it has been read. The job's flatten check returned Other("the document canvas is too large to flatten"), where the place used to give flattened_checked's BadParams for file.place naming the canvas size. Both now share check_flattenable, so the old error type and message are back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Path drops aren't capped, and a discarded drop wakes the queue The 32-file cap on the drop queue counted path drops, which hold no file contents while they wait, so dropping 40 files on the canvas placed 32 and refused 8 where main placed them all. The count cap now applies to browser drops only (each holds its whole file); the byte budget still bounds those. When place_next_dropped discarded a closed document's files it returned without asking for another frame, so another document's queued files waited for the next input event. It now requests a repaint when files remain, keeping the one frame that shows the notice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
21 files changed
+1318
-110
No files matched your search
@@ -15,6 +15,7 @@ use photocraft_ui_egui::{FileDialogAnswer, FileDialogRequest, PhotocraftApp, Ser
|
||||
use wasm_bindgen::JsCast as _;
|
||||
|
||||
type Inbox = Arc<Mutex<Vec<(String, Vec<u8>)>>>;
|
||||
type PlaceInbox = photocraft_ui_egui::PlaceInbox;
|
||||
|
||||
/// Everything File › Open reads: PhotoCraft, Photoshop, OpenRaster, Krita and Affinity documents, flat images, and
|
||||
/// Photoshop brushes (.abr), gradients (.grd), layer styles (.asl) and swatches (.aco, .act, .ase), which go to the preset libraries.
|
||||
@@ -95,7 +96,7 @@ pub fn start() {
|
||||
PhotocraftApp::install_type_reset_hooks(&cc.egui_ctx);
|
||||
PhotocraftApp::setup_context(&cc.egui_ctx, ThemeKind::Pro);
|
||||
let inbox: Inbox = Arc::default();
|
||||
let place_inbox: Inbox = Arc::default();
|
||||
let place_inbox: PlaceInbox = Arc::default();
|
||||
let served: Served = Arc::default();
|
||||
load_font_manifest(served.clone(), cc.egui_ctx.clone());
|
||||
let (session, warnings) = crate::preset_bridge::session(presets.clone());
|
||||
@@ -344,7 +345,7 @@ struct WebShell {
|
||||
/// Dropped files to open as documents.
|
||||
inbox: Inbox,
|
||||
/// Dropped files to place on the active document's canvas.
|
||||
place_inbox: Inbox,
|
||||
place_inbox: PlaceInbox,
|
||||
/// Read by the `beforeunload` listener ([`guard_unload`]).
|
||||
unsaved: Arc<AtomicBool>,
|
||||
served: Served,
|
||||
@@ -431,16 +432,22 @@ impl eframe::App for WebShell {
|
||||
// Decided now, where the drop landed: on the document's canvas the files are placed
|
||||
// as layers (as on the desktop), elsewhere opened.
|
||||
let places = self.app.dropped_on_canvas(ctx, &names);
|
||||
let files: Vec<_> = dropped.into_iter().zip(names).zip(places).collect();
|
||||
let target_doc = self.app.session.active().map(|st| st.doc.id);
|
||||
let files: Vec<_> = dropped.into_iter().zip(names).zip(places).map(|((f, name), place)| (f, name, place, target_doc)).collect();
|
||||
let (inbox, place_inbox) = (self.inbox.clone(), self.place_inbox.clone());
|
||||
let ctx = ctx.clone();
|
||||
// One after another, so they open or place in the order they were dropped.
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
for ((f, name), place) in files {
|
||||
for (f, name, place, target_doc) in files {
|
||||
match f.bytes_async().await {
|
||||
Ok(bytes) => {
|
||||
let queue = if place { &place_inbox } else { &inbox };
|
||||
queue.lock().unwrap_or_else(PoisonError::into_inner).push((name, bytes));
|
||||
if place {
|
||||
if let Some(doc) = target_doc {
|
||||
place_inbox.lock().unwrap_or_else(PoisonError::into_inner).push((doc, name, bytes));
|
||||
}
|
||||
} else {
|
||||
inbox.lock().unwrap_or_else(PoisonError::into_inner).push((name, bytes));
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
Err(e) => log::error!("couldn't read dropped file {name}: {e}"),
|
||||
@@ -484,7 +491,7 @@ impl eframe::App for WebShell {
|
||||
}
|
||||
}
|
||||
|
||||
fn services(inbox: Inbox, place_inbox: Inbox, cursor_pos: photocraft_ui_egui::CursorPosFn) -> Services {
|
||||
fn services(inbox: Inbox, place_inbox: PlaceInbox, cursor_pos: photocraft_ui_egui::CursorPosFn) -> Services {
|
||||
Services {
|
||||
screen_pick: screen_color_service(),
|
||||
import: Some(Box::new(|name: &str, bytes: &[u8], opts: &photocraft_io::ImportOptions| {
|
||||
|
||||
@@ -348,10 +348,32 @@ fn resize(s: &Surface, sx: f64, sy: f64, filter: Resample, canvas: Option<Rect>)
|
||||
/// clips it (and rounds it at 8 and 16 bits) as Photoshop does; the smaller scale goes first, so
|
||||
/// the intermediate image is never larger than the result.
|
||||
fn resize_with_budget(s: &Surface, sx: f64, sy: f64, filter: Resample, canvas: Option<Rect>, budget: usize) -> Surface {
|
||||
resize_controlled(s, sx, sy, filter, canvas, budget, &photocraft_raster::Interrupt::NONE)
|
||||
}
|
||||
|
||||
/// Resize with cancellation between passes and bounded groups of output bands.
|
||||
/// A cancelled operation returns no partially resized surface.
|
||||
pub fn resize_surface_with_interrupt(
|
||||
s: &Surface,
|
||||
sx: f64,
|
||||
sy: f64,
|
||||
filter: Resample,
|
||||
ctl: &photocraft_raster::Interrupt,
|
||||
) -> Result<Surface, photocraft_raster::Cancelled> {
|
||||
ctl.check()?;
|
||||
let out = resize_controlled(s, sx, sy, filter, None, READ_BUDGET, ctl);
|
||||
ctl.check()?;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn resize_controlled(s: &Surface, sx: f64, sy: f64, filter: Resample, canvas: Option<Rect>, budget: usize, ctl: &photocraft_raster::Interrupt) -> Surface {
|
||||
let mut cur: Option<Surface> = None;
|
||||
let mut canvas = canvas;
|
||||
for p in plan(filter, sx, sy) {
|
||||
let next = resize_pass(cur.as_ref().unwrap_or(s), p, canvas, budget);
|
||||
if ctl.cancelled() {
|
||||
return Surface::new(s.format());
|
||||
}
|
||||
let next = resize_pass(cur.as_ref().unwrap_or(s), p, canvas, budget, ctl);
|
||||
canvas = canvas.map(|c| scaled_rect(c, p.sx, p.sy));
|
||||
cur = Some(next);
|
||||
}
|
||||
@@ -368,7 +390,7 @@ fn resize_with_budget(s: &Surface, sx: f64, sy: f64, filter: Resample, canvas: O
|
||||
|
||||
/// One pass. With `canvas`, the content and the canvas repeat their edge outwards (see
|
||||
/// [`resize_surface_in_canvas`]).
|
||||
fn resize_pass(s: &Surface, pass: Pass, canvas: Option<Rect>, budget: usize) -> Surface {
|
||||
fn resize_pass(s: &Surface, pass: Pass, canvas: Option<Rect>, budget: usize, ctl: &photocraft_raster::Interrupt) -> Surface {
|
||||
let Pass { sx, sy, kx, ky } = pass;
|
||||
let edge = canvas.map(|c| s.content_bounds().union(&c)).filter(|a| !a.is_empty());
|
||||
let fmt = s.format();
|
||||
@@ -484,6 +506,9 @@ fn resize_pass(s: &Surface, pass: Pass, canvas: Option<Rect>, budget: usize) ->
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let group = 1;
|
||||
for chunk in bands.chunks(group) {
|
||||
if ctl.cancelled() {
|
||||
return Surface::new(fmt);
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let parts: Vec<(Rect, Vec<f32>)> = {
|
||||
use rayon::prelude::*;
|
||||
|
||||
@@ -400,3 +400,18 @@ fn nearest_neighbor_reduction_does_not_average() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn controlled_resize_matches_pixels_and_discards_cancelled_work() {
|
||||
for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
|
||||
let src = ramp(depth);
|
||||
let expected = resize_surface(&src, 1.7, 0.6, Resample::Bicubic);
|
||||
let actual = resize_surface_with_interrupt(&src, 1.7, 0.6, Resample::Bicubic, &photocraft_raster::Interrupt::NONE).unwrap();
|
||||
let bounds = expected.content_bounds();
|
||||
assert_eq!(expected.read_region(bounds), actual.read_region(bounds));
|
||||
let cancelled = || true;
|
||||
let progress = |_| {};
|
||||
let ctl = photocraft_raster::Interrupt::new(&cancelled, &progress);
|
||||
assert!(resize_surface_with_interrupt(&src, 1.7, 0.6, Resample::Bicubic, &ctl).is_err());
|
||||
}
|
||||
}
|
||||
@@ -82,8 +82,8 @@ use render::in_blend_space;
|
||||
use render::render_tiled_with;
|
||||
pub(crate) use render::visible_layers;
|
||||
pub use render::{
|
||||
BAND_PIXELS, MAX_RENDER_PIXELS, band_rows_for, cmyk_space, composite_to_surface, flatten, flatten_to_surface, render, render_bands, render_layer,
|
||||
render_layer_in, render_tiled,
|
||||
BAND_PIXELS, MAX_RENDER_PIXELS, band_rows_for, cmyk_space, composite_to_surface, composite_to_surface_with_interrupt, flatten, flatten_to_surface, render,
|
||||
render_bands, render_layer, render_layer_in, render_tiled,
|
||||
};
|
||||
|
||||
/// Straight-alpha RGBA float buffer covering a rectangle.
|
||||
|
||||
@@ -357,3 +357,30 @@ pub fn render_layer_in(doc: &Document, layer: &Layer, rect: Rect) -> Buffer {
|
||||
buf
|
||||
})
|
||||
}
|
||||
|
||||
/// Composite in bounded bands, retaining native colour-model paths, with cancellation.
|
||||
pub fn composite_to_surface_with_interrupt(
|
||||
doc: &Document,
|
||||
rect: Rect,
|
||||
fmt: photocraft_color::PixelFormat,
|
||||
background: Option<[f32; 3]>,
|
||||
ctl: &photocraft_raster::Interrupt,
|
||||
) -> Result<Surface, photocraft_raster::Cancelled> {
|
||||
ctl.check()?;
|
||||
let mut out = Surface::new(fmt);
|
||||
let rows = band_rows_for(rect.width(), 64);
|
||||
let mut y = rect.y0;
|
||||
while y < rect.y1 {
|
||||
ctl.check()?;
|
||||
let end = y.saturating_add(rows).min(rect.y1);
|
||||
let band = Rect::new(rect.x0, y, rect.x1, end);
|
||||
let part = composite_to_surface(doc, band, fmt, background);
|
||||
ctl.check()?;
|
||||
let mut pixels = Vec::new();
|
||||
part.read_region_into(band, &mut pixels);
|
||||
out.write_region(band, &pixels);
|
||||
y = end;
|
||||
}
|
||||
out.prune();
|
||||
Ok(out)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use photocraft_color::{ColorMode, PixelFormat, SampleType};
|
||||
use photocraft_compose::{composite_to_surface, composite_to_surface_with_interrupt};
|
||||
use photocraft_doc::{Document, Layer};
|
||||
use photocraft_geom::{Rect, Size};
|
||||
use photocraft_raster::Interrupt;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
#[test]
|
||||
fn controlled_composite_keeps_native_pixels_and_cancels_between_bands() {
|
||||
for mode in [ColorMode::Rgb, ColorMode::Cmyk, ColorMode::Lab] {
|
||||
for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
|
||||
let mut doc = Document::new("bands", Size::new(16, 600), mode, depth);
|
||||
let fmt = PixelFormat::new(mode, depth, true);
|
||||
let mut layer = Layer::raster("pixels", fmt);
|
||||
let (channels, colors) = (fmt.channels(), mode.color_channels());
|
||||
for y in 0..600 {
|
||||
// Every row and channel differs, and every pixel is opaque: a band at the wrong
|
||||
// offset or with a swapped channel changes the result.
|
||||
let pixel: Vec<f32> =
|
||||
(0..channels).map(|c| if c == colors { 1.0 } else { (1 + (y as usize % 29) * channels + c * 7) as f32 / 255.0 }).collect();
|
||||
layer.surface_mut().unwrap().fill_rect(Rect::from_xywh(0, y, 16, 1), &pixel);
|
||||
}
|
||||
// What the layer holds: an opaque single raster layer composites to its own pixels.
|
||||
let stored = layer.surface().unwrap().read_region(doc.bounds());
|
||||
doc.layers.push(layer);
|
||||
let expected = composite_to_surface(&doc, doc.bounds(), fmt, None);
|
||||
let actual = composite_to_surface_with_interrupt(&doc, doc.bounds(), fmt, None, &Interrupt::NONE).unwrap();
|
||||
assert_eq!(actual.read_region(doc.bounds()), expected.read_region(doc.bounds()), "{mode:?} {depth:?}");
|
||||
assert_eq!(actual.read_region(doc.bounds()), stored, "the composite keeps the layer's pixels: {mode:?} {depth:?}");
|
||||
let calls = AtomicUsize::new(0);
|
||||
let cancelled = || calls.fetch_add(1, Ordering::Relaxed) >= 3;
|
||||
let progress = |_| {};
|
||||
assert!(composite_to_surface_with_interrupt(&doc, doc.bounds(), fmt, None, &Interrupt::new(&cancelled, &progress)).is_err());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use photocraft_algo::resample::{Resample, resize_surface, translate_surface};
|
||||
use photocraft_algo::resample::{Resample, translate_surface};
|
||||
use photocraft_color::{ColorMode, PixelFormat};
|
||||
use photocraft_doc::{Affine, Document, Layer, LayerContent, LayerId, SmartObject, SmartSource};
|
||||
use photocraft_geom::Rect;
|
||||
@@ -47,8 +47,8 @@ pub use info::{CAMERA_FIELDS, camera_data, read_file_info, write_file_info};
|
||||
use info::{exposure_text, find_element};
|
||||
pub(crate) use open::{BatchOpen, batch_open_notice, batch_open_params, remember_batch_open};
|
||||
pub use open::{PLACE_EMBEDDED, open_bytes_as, open_bytes_as_with_options, place_bytes, raw_defaults};
|
||||
pub(crate) use open::{PlaceInput, apply_place, import, import_with_options, prepare_place, tag_exr_colorspace};
|
||||
use open::{approve_memory, close_all, close_others, open_as, place, revert};
|
||||
pub(crate) use open::{import, import_with_options, tag_exr_colorspace};
|
||||
use paths::is_file;
|
||||
#[cfg(test)]
|
||||
use paths::openable_name;
|
||||
@@ -171,11 +171,18 @@ pub(crate) fn flattened(doc: &Document, fmt: PixelFormat) -> Surface {
|
||||
/// a sparse bundle declares its canvas, and one the size of the format allows would be rendered
|
||||
/// in full, however little it holds. Refused past the compositor's own per-render limit.
|
||||
pub(crate) fn flattened_checked(doc: &Document, fmt: PixelFormat, cmd: &str) -> Result<Surface> {
|
||||
check_flattenable(doc, cmd)?;
|
||||
Ok(flattened(doc, fmt))
|
||||
}
|
||||
|
||||
/// The refusal [`flattened_checked`] gives a canvas past the compositor's per-render limit, for a
|
||||
/// caller that flattens `doc` some other way (a place job, which can be cancelled while it does).
|
||||
pub(crate) fn check_flattenable(doc: &Document, cmd: &str) -> Result<()> {
|
||||
let b = doc.bounds();
|
||||
if u64::from(b.width()) * u64::from(b.height()) > photocraft_compose::MAX_RENDER_PIXELS {
|
||||
return Err(EngineError::BadParams { cmd: cmd.into(), msg: format!("the document's {}×{} canvas is too large to flatten", b.width(), b.height()) });
|
||||
}
|
||||
Ok(flattened(doc, fmt))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_guide_layout(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
@@ -408,6 +415,10 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
#[path = "file_cmds/tests.rs"]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "file_cmds/place_job_tests.rs"]
|
||||
mod place_job_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod aces_exr_tests {
|
||||
use super::*;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use super::paths::{file_name, read_file, stem, template_name};
|
||||
use super::{
|
||||
Affine, Arc, Document, EngineError, Layer, LayerContent, LayerId, Resample, Result, Session, SmartObject, SmartSource, Value, f64_param, flattened_checked,
|
||||
json, resize_surface, str_param, translate_surface,
|
||||
Affine, Arc, Document, EngineError, Layer, LayerContent, LayerId, PixelFormat, Resample, Result, Session, SmartObject, SmartSource, Value, f64_param, json,
|
||||
str_param, translate_surface,
|
||||
};
|
||||
use crate::jobs::JobCtx;
|
||||
|
||||
/// Input and successfully opened document identities of one Override Open scratch session.
|
||||
pub(crate) struct BatchOpen {
|
||||
@@ -312,47 +313,164 @@ pub const PLACE_EMBEDDED: &str = "Place Embedded";
|
||||
/// `linked` makes it a linked smart object that refers to that path instead of embedding the bytes.
|
||||
/// With Preferences ▸ General ▸ Always Create Smart Objects When Placing off, an embedded place
|
||||
/// lands as a plain pixel layer instead (an explicit `"smartObject": false` asks for one).
|
||||
///
|
||||
/// The decoding and resampling are a background job when the command was started with
|
||||
/// [`Session::start`] (see [`crate::jobs`]); inline otherwise. The layer is inserted by the apply
|
||||
/// step on the calling thread, as one undo step.
|
||||
pub fn place_bytes(s: &mut Session, name: &str, bytes: Vec<u8>, linked: Option<String>, p: &Value) -> Result<Value> {
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let frame_id = match p.get("frame") {
|
||||
Some(Value::Bool(false)) => None,
|
||||
Some(v) if !v.is_null() => Some(LayerId(v.as_u64().ok_or_else(|| EngineError::Other("frame must be a frame id or false".into()))?)),
|
||||
_ => d.active_layer.and_then(|id| crate::frame_cmds::frame_of(&d.doc, id)),
|
||||
};
|
||||
let frame = frame_id
|
||||
.map(|id| {
|
||||
d.doc.layer(id).and_then(|l| l.frame.clone()).map(|f| (id, f)).ok_or_else(|| EngineError::Other("the destination frame is unavailable".into()))
|
||||
let input = PlaceInput::new(s, name, bytes, linked, p)?;
|
||||
place_input(s, input)
|
||||
}
|
||||
|
||||
/// Run a place: in the background when the command was started with [`Session::start`].
|
||||
fn place_input(s: &mut Session, input: PlaceInput) -> Result<Value> {
|
||||
let label = input.job_label();
|
||||
crate::jobs::run(s, &label, true, move |ctx| prepare_place(input, ctx), apply_place)
|
||||
}
|
||||
|
||||
/// The file a place reads.
|
||||
enum PlaceFile {
|
||||
/// Its contents, already read (a dropped or picked file with no path, a script's bytes).
|
||||
Bytes(Vec<u8>),
|
||||
/// Its path, read by [`prepare_place`]: on the job's worker when the place runs in the
|
||||
/// background, so a large file doesn't hold up the window while it is read.
|
||||
Path(String),
|
||||
}
|
||||
|
||||
/// Everything a place reads from the session, taken up front so [`prepare_place`] can run on a
|
||||
/// worker that never sees the [`Session`].
|
||||
pub(crate) struct PlaceInput {
|
||||
name: String,
|
||||
file: PlaceFile,
|
||||
linked: Option<String>,
|
||||
params: Value,
|
||||
canvas: (f64, f64),
|
||||
fmt: PixelFormat,
|
||||
opts: photocraft_io::ImportOptions,
|
||||
/// Preferences ▸ General ▸ Resize Image During Place, unless the params say otherwise.
|
||||
fit: bool,
|
||||
/// Preferences ▸ General ▸ Always Create Smart Objects When Placing, unless the params say
|
||||
/// otherwise. A linked place is always a smart object.
|
||||
smart: bool,
|
||||
/// The document as the job sees it, for rendering a vector source at its placement scale. A
|
||||
/// path isn't read yet, so whether it is a vector source is decided once it is.
|
||||
doc: Option<Arc<Document>>,
|
||||
/// The frame the image goes into: the active layer's, `"frame"` in the params, or none
|
||||
/// (`"frame": false`). Its image renders through a smart object.
|
||||
frame: Option<(LayerId, photocraft_doc::Frame)>,
|
||||
}
|
||||
|
||||
impl PlaceInput {
|
||||
pub(crate) fn new(s: &Session, name: &str, bytes: Vec<u8>, linked: Option<String>, p: &Value) -> Result<Self> {
|
||||
Self::with_file(s, name, PlaceFile::Bytes(bytes), linked, p)
|
||||
}
|
||||
|
||||
fn with_file(s: &Session, name: &str, file: PlaceFile, linked: Option<String>, p: &Value) -> Result<Self> {
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let prefs = s.prefs();
|
||||
let frame_id = match p.get("frame") {
|
||||
Some(Value::Bool(false)) => None,
|
||||
Some(v) if !v.is_null() => Some(LayerId(v.as_u64().ok_or_else(|| EngineError::Other("frame must be a frame id or false".into()))?)),
|
||||
_ => d.active_layer.and_then(|id| crate::frame_cmds::frame_of(&d.doc, id)),
|
||||
};
|
||||
let frame = frame_id
|
||||
.map(|id| {
|
||||
d.doc
|
||||
.layer(id)
|
||||
.and_then(|l| l.frame.as_deref().cloned())
|
||||
.map(|f| (id, f))
|
||||
.ok_or_else(|| EngineError::Other("the destination frame is unavailable".into()))
|
||||
})
|
||||
.transpose()?;
|
||||
let canvas = frame.as_ref().map_or((d.doc.size.width as f64, d.doc.size.height as f64), |(_, f)| {
|
||||
let [a, b, c, d, _, _] = f.transform.m;
|
||||
(a.hypot(b), c.hypot(d))
|
||||
});
|
||||
let approve = match p.get("approveHighMemory") {
|
||||
None | Some(Value::Null) => false,
|
||||
Some(Value::Bool(b)) => *b,
|
||||
Some(_) => {
|
||||
return Err(EngineError::BadParams { cmd: "file.placeEmbedded".into(), msg: "expected boolean for \"approveHighMemory\"".into() });
|
||||
}
|
||||
};
|
||||
let mut opts = crate::jobs::import_options(prefs);
|
||||
opts.psd_high_memory_approved = approve;
|
||||
opts.psd_approved_estimate = None;
|
||||
Ok(Self {
|
||||
name: name.to_string(),
|
||||
canvas,
|
||||
fmt: d.doc.pixel_format(),
|
||||
opts,
|
||||
// The implicit `fit` is Preferences ▸ General ▸ Resize Image During Place (on by default).
|
||||
fit: p.get("fit").and_then(Value::as_bool).unwrap_or(prefs.general.resize_image_during_place),
|
||||
// Preferences ▸ General ▸ Always Create Smart Objects When Placing (on by default): off,
|
||||
// an embedded place lands as a plain pixel layer.
|
||||
smart: linked.is_some() || p.get("smartObject").and_then(Value::as_bool).unwrap_or(prefs.general.always_create_smart_objects_when_placing),
|
||||
doc: match &file {
|
||||
PlaceFile::Bytes(bytes) => (frame.is_some() || photocraft_io::is_svg_input(name, bytes)).then(|| Arc::clone(&d.doc)),
|
||||
PlaceFile::Path(_) => Some(Arc::clone(&d.doc)),
|
||||
},
|
||||
frame,
|
||||
params: p.clone(),
|
||||
file,
|
||||
linked,
|
||||
})
|
||||
.transpose()?;
|
||||
let (cw, ch) = frame.as_ref().map_or((d.doc.size.width as f64, d.doc.size.height as f64), |(_, f)| {
|
||||
let [a, b, c, d, _, _] = f.transform.m;
|
||||
(a.hypot(b), c.hypot(d))
|
||||
});
|
||||
let fmt = d.doc.pixel_format();
|
||||
let vector = photocraft_io::svg::is_svg(&bytes);
|
||||
// Refuse before decoding if the operation cannot fit.
|
||||
crate::memory_gate::ensure(&format!("place \u{201c}{}\u{201d}", file_name(name)), crate::memory_gate::place_need(&bytes, fmt.bytes_per_pixel()))?;
|
||||
let approve = match p.get("approveHighMemory") {
|
||||
None | Some(Value::Null) => false,
|
||||
Some(Value::Bool(b)) => *b,
|
||||
Some(_) => {
|
||||
return Err(EngineError::BadParams { cmd: "file.placeEmbedded".into(), msg: "expected boolean for \"approveHighMemory\"".into() });
|
||||
}
|
||||
|
||||
/// The job's name in the progress UI.
|
||||
pub(crate) fn job_label(&self) -> String {
|
||||
format!("Placing {}", file_name(&self.name))
|
||||
}
|
||||
|
||||
/// The command this place stands for.
|
||||
pub(crate) fn command(&self) -> &'static str {
|
||||
if self.linked.is_some() { "file.placeLinked" } else { "file.placeEmbedded" }
|
||||
}
|
||||
}
|
||||
|
||||
/// A place decoded, flattened and fitted, ready to be inserted by [`apply_place`].
|
||||
pub(crate) struct Prepared {
|
||||
content: LayerContent,
|
||||
layer_name: String,
|
||||
/// The history step's name.
|
||||
label: &'static str,
|
||||
scale: f64,
|
||||
bounds: [f64; 4],
|
||||
smart: bool,
|
||||
/// The frame the layer fills, if any (it becomes the frame's content instead of a new layer).
|
||||
frame: Option<LayerId>,
|
||||
}
|
||||
|
||||
/// The heavy part of a place: read (a path), decode, flatten, resize and translate (and render a
|
||||
/// vector source at its placement scale). Touches no [`Session`], so it can run on a worker; `ctx`
|
||||
/// reports progress and stops it between stages when cancelled.
|
||||
pub(crate) fn prepare_place(input: PlaceInput, ctx: &JobCtx) -> Result<Prepared> {
|
||||
let PlaceInput { name, file, linked, params: p, canvas: (cw, ch), fmt, opts, fit, smart, doc, frame } = input;
|
||||
let bytes = match file {
|
||||
PlaceFile::Bytes(bytes) => bytes,
|
||||
PlaceFile::Path(path) => {
|
||||
ctx.progress(0.0, "Reading");
|
||||
let bytes = read_file(&path)?;
|
||||
ctx.check()?;
|
||||
bytes
|
||||
}
|
||||
};
|
||||
let mut opts = crate::jobs::import_options(s.prefs());
|
||||
opts.psd_high_memory_approved = approve;
|
||||
opts.psd_approved_estimate = None;
|
||||
let imported = photocraft_io::import_with_options(name, &bytes, &opts).map_err(|e| crate::error::io_open_error(name, e))?;
|
||||
// Refuse before decoding if the operation cannot fit.
|
||||
crate::memory_gate::ensure(&format!("place \u{201c}{}\u{201d}", file_name(&name)), crate::memory_gate::place_need(&bytes, fmt.bytes_per_pixel()))?;
|
||||
let doc = doc.filter(|_| frame.is_some() || photocraft_io::is_svg_input(&name, &bytes));
|
||||
let imported = ctx.stage(0.0, 0.6, "Decoding", |ctl| photocraft_io::import_with_options_and_interrupt(&name, &bytes, &opts, ctl)).map_err(|e| match e {
|
||||
photocraft_io::IoError::Cancelled => EngineError::Cancelled,
|
||||
e => crate::error::io_open_error(&name, e),
|
||||
})?;
|
||||
if imported.preview_only {
|
||||
return Err(EngineError::Other(format!(
|
||||
"{name}: only this Affinity file's embedded preview could be read; open it with File › Open to see the warning, or export PSD or PNG from Affinity before using it here"
|
||||
)));
|
||||
}
|
||||
ctx.check()?;
|
||||
let src = imported.document;
|
||||
let (w, h) = (src.size.width as f64, src.size.height as f64);
|
||||
// The implicit `fit` is Preferences ▸ General ▸ Resize Image During Place (on by default).
|
||||
let fit = p.get("fit").and_then(Value::as_bool).unwrap_or(s.prefs().general.resize_image_during_place);
|
||||
let scale = match f64_param(p, "scale") {
|
||||
let scale = match f64_param(&p, "scale") {
|
||||
Some(k) => (k / 100.0).max(1e-4),
|
||||
None if frame.is_some() && fit => (cw / w).max(ch / h),
|
||||
None if fit && (w > cw || h > ch) => (cw / w).min(ch / h),
|
||||
@@ -366,14 +484,25 @@ pub fn place_bytes(s: &mut Session, name: &str, bytes: Vec<u8>, linked: Option<S
|
||||
// A frame's image renders through its smart object below, which refuses a placement past the
|
||||
// memory budget; resampling it to the frame's size first would allocate that surface twice.
|
||||
let px = if frame.is_none() {
|
||||
let mut px = flattened_checked(&src, fmt, "file.place")?;
|
||||
ctx.progress(0.6, "Flattening");
|
||||
super::check_flattenable(&src, "file.place")?;
|
||||
let bounds = src.bounds();
|
||||
let mut px = ctx
|
||||
.stage(0.6, 0.7, "Flattening", |ctl| photocraft_compose::composite_to_surface_with_interrupt(&src, bounds, fmt, None, ctl))
|
||||
.map_err(|_| EngineError::Cancelled)?;
|
||||
ctx.check()?;
|
||||
if (scale - 1.0).abs() > 1e-9 {
|
||||
px = resize_surface(&px, scale, scale, Resample::Bicubic);
|
||||
ctx.progress(0.7, "Resizing");
|
||||
px = ctx
|
||||
.stage(0.7, 0.9, "Resizing", |ctl| photocraft_algo::resample::resize_surface_with_interrupt(&px, scale, scale, Resample::Bicubic, ctl))
|
||||
.map_err(|_| EngineError::Cancelled)?;
|
||||
ctx.check()?;
|
||||
}
|
||||
Some(translate_surface(&px, dx as i32, dy as i32))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
drop(src);
|
||||
let placement = frame
|
||||
.as_ref()
|
||||
.map_or(Affine { m: [scale, 0.0, 0.0, scale, dx, dy] }, |(_, f)| f.transform.mul(&Affine { m: [scale / cw, 0.0, 0.0, scale / ch, dx / cw, dy / ch] }));
|
||||
@@ -384,45 +513,50 @@ pub fn place_bytes(s: &mut Session, name: &str, bytes: Vec<u8>, linked: Option<S
|
||||
corners.iter().map(|q| q.x).fold(f64::NEG_INFINITY, f64::max),
|
||||
corners.iter().map(|q| q.y).fold(f64::NEG_INFINITY, f64::max),
|
||||
];
|
||||
// Preferences ▸ General ▸ Always Create Smart Objects When Placing (on by default): off, an
|
||||
// embedded place lands as a plain pixel layer. A linked place is always a smart object.
|
||||
let smart = frame.is_some()
|
||||
|| linked.is_some()
|
||||
|| p.get("smartObject").and_then(Value::as_bool).unwrap_or(s.prefs().general.always_create_smart_objects_when_placing);
|
||||
let source = match linked {
|
||||
Some(path) => SmartSource::Linked { path },
|
||||
None => SmartSource::Embedded { file_name: file_name(name), bytes: Arc::new(bytes) },
|
||||
};
|
||||
let layer_name = stem(name);
|
||||
let label = if matches!(&source, SmartSource::Linked { .. }) { "Place Linked" } else { PLACE_EMBEDDED };
|
||||
let id = s.edit(label, |doc, active| {
|
||||
let layer = if smart {
|
||||
let mut so = SmartObject::new(source, placement, px);
|
||||
// A vector source (SVG) renders at its placement scale instead of as a resampled raster.
|
||||
if (vector || frame.is_some())
|
||||
&& let Some(sharp) = crate::smart_cmds::render(doc, &so)?
|
||||
{
|
||||
// A frame's image is always a smart object.
|
||||
let smart = smart || frame.is_some();
|
||||
let label = if linked.is_some() { "Place Linked" } else { PLACE_EMBEDDED };
|
||||
let content = if smart {
|
||||
let source = match linked {
|
||||
Some(path) => SmartSource::Linked { path },
|
||||
None => SmartSource::Embedded { file_name: file_name(&name), bytes: Arc::new(bytes) },
|
||||
};
|
||||
let mut so = SmartObject::new(source, placement, px);
|
||||
// A vector source (SVG) and a frame's image render at their placement scale instead of as
|
||||
// a resampled raster.
|
||||
if let Some(doc) = doc {
|
||||
ctx.progress(0.9, "Rendering");
|
||||
ctx.check()?;
|
||||
if let Some(sharp) = crate::smart_cmds::render(&doc, &so)? {
|
||||
so.cache = Some(sharp);
|
||||
}
|
||||
if so.cache.is_none() {
|
||||
return Err(EngineError::Other(format!("{name}: the placed image could not be rendered")));
|
||||
}
|
||||
Layer::new(layer_name, LayerContent::Smart(so))
|
||||
} else {
|
||||
let px = px.ok_or_else(|| EngineError::Other(format!("{name}: a frame's image must be a smart object")))?;
|
||||
Layer::new(layer_name, LayerContent::Raster(px))
|
||||
};
|
||||
let id = layer.id;
|
||||
if let Some((frame_id, _)) = &frame {
|
||||
crate::frame_cmds::put_content(doc, *frame_id, layer)?;
|
||||
*active = Some(*frame_id);
|
||||
}
|
||||
if so.cache.is_none() {
|
||||
return Err(EngineError::Other(format!("{name}: the placed image could not be rendered")));
|
||||
}
|
||||
LayerContent::Smart(so)
|
||||
} else {
|
||||
LayerContent::Raster(px.ok_or_else(|| EngineError::Other(format!("{name}: a frame's image must be a smart object")))?)
|
||||
};
|
||||
Ok(Prepared { content, layer_name: stem(&name), label, scale, bounds, smart, frame: frame.map(|(id, _)| id) })
|
||||
}
|
||||
|
||||
/// Insert the prepared layer as one undo step and report the place. Runs on the session's thread.
|
||||
pub(crate) fn apply_place(s: &mut Session, p: Prepared) -> Result<Value> {
|
||||
let Prepared { content, layer_name, label, scale, bounds, smart, frame } = p;
|
||||
let layer = Layer::new(layer_name, content);
|
||||
let id = layer.id;
|
||||
s.edit(label, move |doc, active| {
|
||||
if let Some(frame_id) = frame {
|
||||
crate::frame_cmds::put_content(doc, frame_id, layer)?;
|
||||
*active = Some(frame_id);
|
||||
} else {
|
||||
crate::layer_place::insert_new(doc, *active, layer, crate::layer_place::Place::AutoEditable);
|
||||
*active = Some(id);
|
||||
}
|
||||
Ok(id)
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(json!({"layer": id.0, "frame": frame.as_ref().map(|(id,_)|id.0), "scale": scale * 100.0, "bounds": bounds, "smartObject": smart}))
|
||||
Ok(json!({"layer": id.0, "frame": frame.map(|f| f.0), "scale": scale * 100.0, "bounds": bounds, "smartObject": smart}))
|
||||
}
|
||||
|
||||
pub(super) fn place(s: &mut Session, p: &Value, linked: bool) -> Result<Value> {
|
||||
@@ -431,7 +565,6 @@ pub(super) fn place(s: &mut Session, p: &Value, linked: bool) -> Result<Value> {
|
||||
return Err(EngineError::BadParams { cmd: cmd.into(), msg: "expected boolean for approveHighMemory".into() });
|
||||
}
|
||||
let path = str_param(p, "path", cmd)?.to_string();
|
||||
let bytes = read_file(&path)?;
|
||||
// The link is stored absolute: a relative one would later be taken from the document's
|
||||
// folder (`doc_path`), not the working directory the path was given in.
|
||||
let link = linked.then(|| {
|
||||
@@ -441,5 +574,7 @@ pub(super) fn place(s: &mut Session, p: &Value, linked: bool) -> Result<Value> {
|
||||
path.clone()
|
||||
}
|
||||
});
|
||||
place_bytes(s, &path, bytes, link, p)
|
||||
// The file is read by the place itself, on the job's worker when it runs in the background.
|
||||
let input = PlaceInput::with_file(s, &path, PlaceFile::Path(path.clone()), link, p)?;
|
||||
place_input(s, input)
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
//! Place as a background job (#10031): the decode, flatten and resize run on a worker and the
|
||||
//! layer is inserted when the job is applied. Inline callers see no difference.
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use super::*;
|
||||
use crate::jobs::{JobEvent, JobId, JobOutcome, Started};
|
||||
|
||||
fn tmp(name: &str) -> String {
|
||||
let d = std::env::temp_dir().join(format!("photocraft-place-job-{}-{name}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
std::fs::create_dir_all(&d).unwrap();
|
||||
d.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
fn session(w: u32, h: u32, depth: u32) -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": w, "height": h, "depth": depth})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
/// A small RGB gradient PNG, so a wrong pixel anywhere shows.
|
||||
fn gradient_png(w: u32, h: u32) -> Vec<u8> {
|
||||
let px: Vec<u8> = (0..h).flat_map(|y| (0..w).flat_map(move |x| [(x * 255 / w) as u8, (y * 255 / h) as u8, ((x + y) % 256) as u8])).collect();
|
||||
let img = photocraft_codecs::Image::from_u8(w, h, photocraft_codecs::ChannelLayout::Rgb, px).unwrap();
|
||||
photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap()
|
||||
}
|
||||
|
||||
fn job(r: Started) -> JobId {
|
||||
match r {
|
||||
Started::Job(id) => id,
|
||||
Started::Done(v) => panic!("expected a background job, ran inline: {v}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Poll until job `id` ends (or 60 s pass); returns its event.
|
||||
fn wait_event(s: &mut Session, id: JobId) -> JobEvent {
|
||||
let t = Instant::now();
|
||||
loop {
|
||||
if let Some(e) = s.poll_jobs().into_iter().find(|e| e.id == id) {
|
||||
return e;
|
||||
}
|
||||
assert!(t.elapsed() < Duration::from_secs(60), "job {id:?} did not finish");
|
||||
std::thread::sleep(Duration::from_millis(2));
|
||||
}
|
||||
}
|
||||
|
||||
/// The active layer's pixels over the canvas and its content.
|
||||
fn layer_pixels(s: &Session) -> Vec<f32> {
|
||||
let d = s.active().unwrap();
|
||||
let surface = d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap();
|
||||
surface.read_region(d.doc.bounds().union(&surface.content_bounds()))
|
||||
}
|
||||
|
||||
fn canvas_pixels(s: &Session) -> Vec<[f32; 4]> {
|
||||
photocraft_compose::render(&s.active().unwrap().doc, s.active().unwrap().doc.bounds()).px.clone()
|
||||
}
|
||||
|
||||
fn steps(s: &Session) -> usize {
|
||||
s.active().unwrap().history.past_len()
|
||||
}
|
||||
|
||||
/// The place a job does is the place the command does inline: the same result, layer, pixels and
|
||||
/// one history step, at every depth, for a smart object and for a pixel layer.
|
||||
#[test]
|
||||
fn a_place_job_equals_the_inline_place() {
|
||||
let dir = tmp("equal");
|
||||
let path = join(&dir, "grad.png");
|
||||
std::fs::write(&path, gradient_png(160, 80)).unwrap();
|
||||
for depth in [8, 16, 32] {
|
||||
for smart in [true, false] {
|
||||
let params = json!({"path": path, "smartObject": smart});
|
||||
let mut inline = session(100, 100, depth);
|
||||
let want = inline.execute("file.placeEmbedded", params.clone()).unwrap();
|
||||
|
||||
let mut s = session(100, 100, depth);
|
||||
let before = steps(&s);
|
||||
let id = job(s.start("file.placeEmbedded", params).unwrap());
|
||||
assert_eq!(steps(&s), before, "nothing is inserted before the job is applied");
|
||||
let e = wait_event(&mut s, id);
|
||||
let JobOutcome::Done(got) = &e.outcome else { panic!("{depth} {smart}: {e:?}") };
|
||||
assert_eq!(e.command, "file.placeEmbedded");
|
||||
assert_eq!(e.label, "Placing grad.png");
|
||||
same_but_layer(got, &want);
|
||||
assert_eq!(steps(&s), before + 1, "one undo step");
|
||||
assert_eq!(s.active().unwrap().history.undo_label(), Some(PLACE_EMBEDDED));
|
||||
assert_eq!(layer_pixels(&s), layer_pixels(&inline), "layer pixels at {depth}-bit, smart {smart}");
|
||||
assert_eq!(canvas_pixels(&s), canvas_pixels(&inline), "composite at {depth}-bit, smart {smart}");
|
||||
assert!(s.undo());
|
||||
assert_eq!(s.active().unwrap().doc.layers.len(), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Place Linked as a job keeps the link and its history label.
|
||||
#[test]
|
||||
fn a_linked_place_job_keeps_the_link() {
|
||||
let dir = tmp("linked");
|
||||
let path = join(&dir, "grad.png");
|
||||
std::fs::write(&path, gradient_png(40, 40)).unwrap();
|
||||
let mut s = session(100, 100, 8);
|
||||
let id = s.start("file.placeLinked", json!({"path": path})).map(job).unwrap();
|
||||
assert!(matches!(wait_event(&mut s, id).outcome, JobOutcome::Done(_)));
|
||||
let d = s.active().unwrap();
|
||||
let l = d.doc.layer(d.active_layer.unwrap()).unwrap();
|
||||
assert!(matches!(&l.content, LayerContent::Smart(so) if matches!(&so.source, SmartSource::Linked { path: p } if *p == path)));
|
||||
assert_eq!(d.history.undo_label(), Some("Place Linked"));
|
||||
}
|
||||
|
||||
/// Bytes with no path (a web or dropped file) start the same job through `start_place_bytes`.
|
||||
#[test]
|
||||
fn start_place_bytes_matches_place_bytes() {
|
||||
let bytes = gradient_png(160, 80);
|
||||
let mut inline = session(100, 100, 16);
|
||||
let want = place_bytes(&mut inline, "drop.png", bytes.clone(), None, &json!({})).unwrap();
|
||||
|
||||
let mut s = session(100, 100, 16);
|
||||
let id = job(s.start_place_bytes("drop.png", bytes.clone(), None, json!({})).unwrap());
|
||||
// The document is locked meanwhile: another place (or edit) is refused with the job's name.
|
||||
let refused = s.start_place_bytes("again.png", bytes, None, json!({})).unwrap_err().to_string();
|
||||
assert!(refused.contains("Placing drop.png"), "{refused}");
|
||||
assert!(s.execute("layer.new.layer", json!({})).is_err());
|
||||
assert!(s.execute("jobs.list", json!({})).is_ok(), "queries still run");
|
||||
let e = wait_event(&mut s, id);
|
||||
assert_eq!(e.command, "file.placeEmbedded");
|
||||
let JobOutcome::Done(got) = &e.outcome else { panic!("{e:?}") };
|
||||
same_but_layer(got, &want);
|
||||
assert_eq!(got["layer"], json!(s.active().unwrap().active_layer.unwrap().0), "the result names the placed layer");
|
||||
assert_eq!(layer_pixels(&s), layer_pixels(&inline));
|
||||
assert_eq!(steps(&s), steps(&inline));
|
||||
}
|
||||
|
||||
/// Assert a place result equals `want` except for its `layer`: layer ids come from a counter
|
||||
/// shared by every session, so two sessions never hand out the same one.
|
||||
fn same_but_layer(got: &Value, want: &Value) {
|
||||
let strip = |v: &Value| {
|
||||
let mut v = v.clone();
|
||||
v.as_object_mut().map(|o| o.remove("layer"));
|
||||
v
|
||||
};
|
||||
assert!(got["layer"].is_u64(), "{got}");
|
||||
assert_eq!(strip(got), strip(want));
|
||||
}
|
||||
|
||||
/// Cancelling stops the place: nothing is inserted and the document is the very same one.
|
||||
#[test]
|
||||
fn cancelling_a_place_job_leaves_the_document_unchanged() {
|
||||
let mut s = session(100, 100, 8);
|
||||
let (doc, before) = (s.active().unwrap().doc.clone(), steps(&s));
|
||||
let id = job(s.start_place_bytes("drop.png", gradient_png(160, 80), None, json!({})).unwrap());
|
||||
assert!(s.cancel_job(id));
|
||||
s.join_cancelled_jobs();
|
||||
assert_eq!(wait_event(&mut s, id).outcome, JobOutcome::Cancelled);
|
||||
assert!(Arc::ptr_eq(&doc, &s.active().unwrap().doc));
|
||||
assert_eq!(steps(&s), before);
|
||||
assert!(!s.active().unwrap().history.can_redo());
|
||||
assert!(matches!(s.wait_job(id), Err(EngineError::Cancelled)));
|
||||
assert!(s.active_job().is_none(), "the document is unlocked");
|
||||
}
|
||||
|
||||
/// An edit made while the job runs wins: applying fails and no layer is placed.
|
||||
#[test]
|
||||
fn an_edit_during_a_place_job_discards_the_place() {
|
||||
let mut s = session(100, 100, 8);
|
||||
let id = job(s.start_place_bytes("drop.png", gradient_png(160, 80), None, json!({})).unwrap());
|
||||
// Commands are refused while the job runs; a direct edit (the shell's, say) is not.
|
||||
s.edit("Poke", |doc, _| {
|
||||
doc.name = "poked".into();
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let e = wait_event(&mut s, id);
|
||||
let JobOutcome::Failed(err) = &e.outcome else { panic!("{e:?}") };
|
||||
assert!(err.contains("Placing drop.png") && err.contains("document changed"), "{err}");
|
||||
let d = s.active().unwrap();
|
||||
assert_eq!(d.doc.name, "poked", "the edit is kept");
|
||||
assert_eq!(d.doc.layers.len(), 1, "nothing was placed");
|
||||
assert_eq!(d.history.undo_label(), Some("Poke"));
|
||||
}
|
||||
|
||||
/// Hostile input as a job is an error, never a panic, and the document is left alone.
|
||||
#[test]
|
||||
fn bad_input_as_a_place_job_fails_without_panicking() {
|
||||
let dir = tmp("bad");
|
||||
let good = gradient_png(32, 32);
|
||||
let empty = join(&dir, "empty.png");
|
||||
std::fs::write(&empty, b"").unwrap();
|
||||
let truncated = join(&dir, "truncated.png");
|
||||
std::fs::write(&truncated, good.get(..good.len() / 2).unwrap()).unwrap();
|
||||
let garbage = join(&dir, "garbage.png");
|
||||
std::fs::write(&garbage, [0x89u8, b'P', b'N', b'G', 0xff, 0x00, 0x13, 0x37, 0x42]).unwrap();
|
||||
|
||||
let mut s = session(64, 64, 8);
|
||||
let (doc, before) = (s.active().unwrap().doc.clone(), steps(&s));
|
||||
for path in [&empty, &truncated, &garbage] {
|
||||
let id = job(s.start("file.placeEmbedded", json!({"path": path})).unwrap());
|
||||
let e = wait_event(&mut s, id);
|
||||
let JobOutcome::Failed(err) = &e.outcome else { panic!("{path}: {e:?}") };
|
||||
assert!(err.contains("png"), "the file is named: {err}");
|
||||
}
|
||||
for bytes in [Vec::new(), good.get(..10).unwrap().to_vec(), vec![0u8; 3]] {
|
||||
let id = job(s.start_place_bytes("bad.png", bytes, None, json!({})).unwrap());
|
||||
assert!(matches!(wait_event(&mut s, id).outcome, JobOutcome::Failed(_)));
|
||||
}
|
||||
assert!(Arc::ptr_eq(&doc, &s.active().unwrap().doc), "a failed place changes nothing");
|
||||
assert_eq!(steps(&s), before);
|
||||
// Params of the wrong type fall back to the defaults or are refused, but never panic.
|
||||
let params = json!({"scale": "big", "center": "middle", "fit": "yes", "smartObject": 3});
|
||||
if let Ok(Started::Job(id)) = s.start_place_bytes("ok.png", good, None, params) {
|
||||
assert!(matches!(wait_event(&mut s, id).outcome, JobOutcome::Done(_)));
|
||||
assert!(s.undo());
|
||||
}
|
||||
assert_eq!(steps(&s), before);
|
||||
assert_eq!(s.active().unwrap().doc.layers.len(), 1);
|
||||
// Inline, a missing document is the same error as before.
|
||||
let mut none = Session::new();
|
||||
assert!(matches!(place_bytes(&mut none, "a.png", Vec::new(), None, &json!({})), Err(EngineError::NoDocument)));
|
||||
assert!(matches!(none.start_place_bytes("a.png", Vec::new(), None, json!({})), Err(EngineError::NoDocument)));
|
||||
}
|
||||
|
||||
/// A place from a path reads the file in the job, not in the command that starts it: a large file
|
||||
/// doesn't hold up the window while it is read. A file that can't be read fails the job with the
|
||||
/// error the inline place gives, and changes nothing.
|
||||
#[test]
|
||||
fn a_place_job_reads_its_file_on_the_worker() {
|
||||
let dir = tmp("read");
|
||||
let missing = join(&dir, "missing.png");
|
||||
let mut inline = session(64, 64, 8);
|
||||
let want = inline.execute("file.placeEmbedded", json!({"path": missing})).unwrap_err().to_string();
|
||||
assert!(want.contains(&missing), "{want}");
|
||||
|
||||
let mut s = session(64, 64, 8);
|
||||
let (doc, before) = (s.active().unwrap().doc.clone(), steps(&s));
|
||||
let id = job(s.start("file.placeEmbedded", json!({"path": missing})).expect("the job starts before the file is read"));
|
||||
let e = wait_event(&mut s, id);
|
||||
let JobOutcome::Failed(err) = &e.outcome else { panic!("{e:?}") };
|
||||
assert_eq!(err, &want);
|
||||
assert!(Arc::ptr_eq(&doc, &s.active().unwrap().doc));
|
||||
assert_eq!(steps(&s), before);
|
||||
}
|
||||
|
||||
/// A placed file whose canvas is past the compositor's limit is refused with the same error the
|
||||
/// place gave before it ran as a job: bad params naming the canvas.
|
||||
#[test]
|
||||
fn a_canvas_too_large_to_flatten_is_refused_as_before() {
|
||||
let huge = Document::new("huge", photocraft_doc::Size::new(40_000, 30_000), ColorMode::Rgb, photocraft_color::SampleType::U8);
|
||||
let bytes = photocraft_format::save_to_bytes(&huge, &Default::default()).unwrap();
|
||||
let mut s = session(100, 100, 8);
|
||||
let before = steps(&s);
|
||||
match place_bytes(&mut s, "huge.pcraft", bytes.clone(), None, &json!({})) {
|
||||
Err(EngineError::BadParams { cmd, msg }) => {
|
||||
assert_eq!(cmd, "file.place");
|
||||
assert_eq!(msg, "the document's 40000×30000 canvas is too large to flatten");
|
||||
}
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
let id = job(s.start_place_bytes("huge.pcraft", bytes, None, json!({})).unwrap());
|
||||
let e = wait_event(&mut s, id);
|
||||
let JobOutcome::Failed(err) = &e.outcome else { panic!("{e:?}") };
|
||||
assert!(err.contains("40000×30000 canvas is too large to flatten"), "{err}");
|
||||
assert_eq!(steps(&s), before, "nothing was placed");
|
||||
}
|
||||
@@ -1084,6 +1084,22 @@ impl Session {
|
||||
self.start_open_inner(name, source, true, false, None, None, background)
|
||||
}
|
||||
|
||||
/// Place a file's bytes in the background, like `file.placeEmbedded` (`linked`: `file.placeLinked`
|
||||
/// to that path) for bytes that have no path to read: decode, flatten and fit on a worker, then
|
||||
/// insert the layer as one undo step when [`Session::poll_jobs`] applies it. `name` names the
|
||||
/// layer and the file; `params` are the command's (`scale`, `center`, `fit`, `smartObject`).
|
||||
/// The job's result is the command's: `{layer, frame, scale, bounds, smartObject}`. The active
|
||||
/// document is locked meanwhile, so this is refused while another job runs on it. Runs inline
|
||||
/// on wasm.
|
||||
pub fn start_place_bytes(&mut self, name: &str, bytes: Vec<u8>, linked: Option<String>, params: Value) -> Result<Started> {
|
||||
let input = crate::file_cmds::PlaceInput::new(self, name, bytes, linked, ¶ms)?;
|
||||
let (command, label) = (input.command(), input.job_label());
|
||||
if let Some(conflict) = self.job_conflict(command, true) {
|
||||
return Err(EngineError::Other(conflict));
|
||||
}
|
||||
self.start_job(command, json!({"name": name}), &label, true, move |ctx| crate::file_cmds::prepare_place(input, ctx), crate::file_cmds::apply_place)
|
||||
}
|
||||
|
||||
/// Decodes a file in a job and optionally holds damaged PSD content for
|
||||
/// interactive confirmation while leaving headless opens non-interactive.
|
||||
// Keep the worker and apply closures together with their execution options.
|
||||
|
||||
@@ -62,3 +62,41 @@ fn re_placing_at_another_scale_re_renders_the_vector_source() {
|
||||
assert!(alpha(&surf, 268, 60) > 0.99 && alpha(&surf, 271, 60) < 0.01);
|
||||
assert!(red(&surf, 150, 81) < 0.02 && red(&surf, 150, 75) > 0.98 && red(&surf, 150, 87) > 0.98);
|
||||
}
|
||||
|
||||
/// Placed through a background job, an SVG is rendered at its placement scale on the worker and
|
||||
/// lands exactly as the inline place does: still sharp, not a resampled raster.
|
||||
#[test]
|
||||
fn an_svg_placed_as_a_job_is_as_sharp_as_the_inline_place() {
|
||||
use photocraft_engine::jobs::Started;
|
||||
let dir = std::env::temp_dir().join(format!("photocraft-svg-place-job-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("logo.svg");
|
||||
std::fs::write(&path, SVG).unwrap();
|
||||
let params = json!({"path": path.to_string_lossy(), "scale": 1000});
|
||||
let new_doc = || {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 400, "height": 400, "background": "white"})).unwrap();
|
||||
s
|
||||
};
|
||||
let mut inline = new_doc();
|
||||
let want = inline.execute("file.placeEmbedded", params.clone()).unwrap();
|
||||
|
||||
let mut s = new_doc();
|
||||
let Started::Job(id) = s.start("file.placeEmbedded", params).unwrap() else { panic!("expected a background job") };
|
||||
let mut got = s.wait_job(id).unwrap();
|
||||
// Layer ids come from a counter shared by every session: the rest of the result must match.
|
||||
let mut want = want;
|
||||
assert!(got.as_object_mut().and_then(|o| o.remove("layer")).is_some() && want.as_object_mut().and_then(|o| o.remove("layer")).is_some());
|
||||
assert_eq!(got, want);
|
||||
assert_eq!(got["bounds"], json!([100.0, 150.0, 300.0, 250.0]), "{got}");
|
||||
let doc = s.active().unwrap().doc.clone();
|
||||
let layer = doc.layers.last().unwrap();
|
||||
assert!(matches!(layer.content, LayerContent::Smart(_)));
|
||||
let surf = layer.surface().expect("rendered");
|
||||
assert!(alpha(surf, 200, 148) < 0.01 && alpha(surf, 200, 151) > 0.99, "a pixel-exact top edge");
|
||||
assert!(red(surf, 200, 192) < 0.02, "the thin line is a crisp band: {}", red(surf, 200, 192));
|
||||
assert!(red(surf, 200, 187) > 0.98 && red(surf, 200, 198) > 0.98);
|
||||
let region = Rect::new(90, 140, 310, 260);
|
||||
let inline_doc = inline.active().unwrap().doc.clone();
|
||||
assert_eq!(surf.read_region(region), inline_doc.layers.last().unwrap().surface().unwrap().read_region(region), "same pixels as the inline place");
|
||||
}
|
||||
@@ -952,7 +952,7 @@ fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, o
|
||||
/// Extension names can be wrong (for example, an image downloaded as WebP and renamed `.svg`).
|
||||
/// Valid plain SVG is recognized by its XML content; an otherwise unknown `.svg` is passed to the
|
||||
/// SVG parser for a useful malformed-file error, and `.svgz` disambiguates gzip streams.
|
||||
fn is_svg_input(name: &str, bytes: &[u8]) -> bool {
|
||||
pub fn is_svg_input(name: &str, bytes: &[u8]) -> bool {
|
||||
// Keep the SVG extension as a fallback for malformed files so the SVG parser can return an
|
||||
// actionable SVG error. A recognized raster signature still wins over a misleading `.svg`
|
||||
// suffix (for example, a WebP downloaded as Firefox_logo.svg).
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use super::{
|
||||
PhotocraftApp, analysis_ui, brush_panel, brush_picker, camera_raw_ui, canvas, dds_options_ui, dialogs, discard_ui, distort_ui, field_wheel, file_open,
|
||||
gpu_canvas, gpu_status, gradient_ui, i18n, jobs_ui, jpeg_options_ui, kys_import, last_brush, layers_panel_ui, monitor_status, native_menu, notices,
|
||||
paint_mouse, palette, panels, prefs_ui, preset_panels, rasterize_prompt, screen_picker, shape_stroke_ui, shortcuts, smart_svg_ui, targa_options_ui,
|
||||
text_field_menu, theme, tiff_options_ui, timeline_ui, titlebar, tool_cursor, tool_memory, transform_tool, type_panels_ui, type_tool, updates_ui, vector_ui,
|
||||
view_cmds, wide_angle_ui, work_area, workspace_ui,
|
||||
PhotocraftApp, analysis_ui, brush_panel, brush_picker, camera_raw_ui, canvas, dds_options_ui, dialogs, discard_ui, distort_ui, field_wheel, gpu_canvas,
|
||||
gpu_status, gradient_ui, i18n, jobs_ui, jpeg_options_ui, kys_import, last_brush, layers_panel_ui, monitor_status, native_menu, notices, paint_mouse,
|
||||
palette, panels, prefs_ui, preset_panels, rasterize_prompt, screen_picker, shape_stroke_ui, shortcuts, smart_svg_ui, targa_options_ui, text_field_menu,
|
||||
theme, tiff_options_ui, timeline_ui, titlebar, tool_cursor, tool_memory, transform_tool, type_panels_ui, type_tool, updates_ui, vector_ui, view_cmds,
|
||||
wide_angle_ui, work_area, workspace_ui,
|
||||
};
|
||||
|
||||
impl PhotocraftApp {
|
||||
@@ -111,9 +111,7 @@ impl PhotocraftApp {
|
||||
}
|
||||
}
|
||||
// Web drops onto the canvas, read since the last frame: placed below, one at a time.
|
||||
let to_place: Vec<(String, Vec<u8>)> =
|
||||
self.services.place_inbox.as_ref().map(|q| std::mem::take(&mut *q.lock().unwrap_or_else(|e| e.into_inner()))).unwrap_or_default();
|
||||
self.drop_places.extend(to_place.into_iter().map(|(name, bytes)| file_open::DropPlace::Bytes(name, bytes)));
|
||||
self.queue_web_places();
|
||||
// Finder double-click / Open With / Dock drops (macOS open-documents events).
|
||||
self.drain_os_events(ctx);
|
||||
// While files are dragged over the window it gets no pointer events: keep frames coming so
|
||||
|
||||
@@ -322,6 +322,39 @@ fn menu_place_starts_transform_for_each_chosen_image() {
|
||||
}
|
||||
}
|
||||
|
||||
/// #10031: File › Place decodes in the background; Free Transform starts when the layer lands, and
|
||||
/// a second place is refused while the first runs.
|
||||
#[test]
|
||||
fn menu_place_runs_in_the_background_and_starts_transform_when_it_lands() {
|
||||
let (mut app, open, _) = app();
|
||||
app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
|
||||
let bytes = photocraft_io::export(&app.session.active().unwrap().doc, "source.png", &Default::default()).unwrap().bytes;
|
||||
app.background_jobs = true;
|
||||
let ctx = egui::Context::default();
|
||||
let layers = app.session.active().unwrap().doc.layer_count();
|
||||
menus::invoke(&mut app, &ctx, "file.placeEmbedded", json!({})).unwrap();
|
||||
app.poll_file_dialog(&ctx, None);
|
||||
answer(&open, Some(FileDialogAnswer::Contents("drawing.png".into(), bytes.clone())));
|
||||
app.poll_file_dialog(&ctx, None);
|
||||
let place = app.jobs.place.clone().expect("being placed");
|
||||
assert_eq!(place.name, "drawing.png");
|
||||
assert!(app.ui.transform.is_none(), "Free Transform waits for the layer");
|
||||
assert_eq!(app.session.active().unwrap().doc.layer_count(), layers);
|
||||
// Another place is refused while this one runs, and the first one stays tracked.
|
||||
assert!(crate::jobs_ui::start_place_bytes(&mut app, "other.png", bytes, None, &serde_json::json!({})).is_err());
|
||||
assert_eq!(app.jobs.place.as_ref().map(|p| p.job), Some(place.job));
|
||||
let t = std::time::Instant::now();
|
||||
while app.jobs.place.is_some() {
|
||||
assert!(t.elapsed() < std::time::Duration::from_secs(30), "the place did not finish");
|
||||
crate::jobs_ui::tick(&mut app, &ctx);
|
||||
std::thread::sleep(std::time::Duration::from_millis(2));
|
||||
}
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!(st.doc.layer_count(), layers + 1);
|
||||
assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().name, "drawing");
|
||||
assert_eq!(app.ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelled_or_invalid_menu_place_does_not_start_transform() {
|
||||
for command in ["file.placeEmbedded", "file.placeLinked"] {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//! are shown as errors (status bar + notice); import warnings as a notice.
|
||||
|
||||
use crate::{PhotocraftApp, notices};
|
||||
use photocraft_doc::DocId;
|
||||
use photocraft_engine::file_cmds::{is_template, untitled_name};
|
||||
|
||||
/// A request from the operating system, delivered by the platform shell through
|
||||
@@ -33,7 +34,8 @@ pub enum DropTarget {
|
||||
Elsewhere,
|
||||
}
|
||||
|
||||
/// A file dropped on the canvas, waiting its turn to be placed.
|
||||
/// A file dropped on the canvas, waiting its turn to be placed in the document it was dropped on
|
||||
/// (queued with its [`DocId`] in `drop_places`).
|
||||
pub(crate) enum DropPlace {
|
||||
/// Destination captured when the file is dropped, before a later pointer move.
|
||||
At { source: Box<DropPlace>, frame: Option<photocraft_doc::LayerId> },
|
||||
@@ -44,6 +46,41 @@ pub(crate) enum DropPlace {
|
||||
Bytes(String, Vec<u8>),
|
||||
}
|
||||
|
||||
impl DropPlace {
|
||||
/// The file contents held while it waits for its document (a path drop holds none yet).
|
||||
fn queued_bytes(&self) -> usize {
|
||||
match self {
|
||||
Self::At { source, .. } => source.queued_bytes(),
|
||||
Self::File(_) => 0,
|
||||
Self::Bytes(_, bytes) => bytes.len(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether it holds its file's contents while it waits (a browser drop); a path drop is read
|
||||
/// only when its turn comes.
|
||||
fn holds_bytes(&self) -> bool {
|
||||
match self {
|
||||
Self::At { source, .. } => source.holds_bytes(),
|
||||
Self::File(_) => false,
|
||||
Self::Bytes(..) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The most browser drops, each holding its whole file, that can wait for their document to come
|
||||
/// back in view. Path drops hold only their path and aren't counted: dropping a folder's worth of
|
||||
/// files on the canvas places them all.
|
||||
const MAX_QUEUED_DROPS: usize = 32;
|
||||
/// The most dropped file contents kept in memory while they wait: a browser drop carries its whole
|
||||
/// file, and anything queued on a document in another tab stays until it is back in view.
|
||||
const MAX_QUEUED_DROP_BYTES: usize = 256 * 1024 * 1024;
|
||||
|
||||
/// Whether the drop queue takes a browser drop of `incoming` bytes when it holds `queued` of them
|
||||
/// and `bytes` in all.
|
||||
fn queue_takes_drop(queued: usize, bytes: usize, incoming: usize) -> bool {
|
||||
queued < MAX_QUEUED_DROPS && bytes.saturating_add(incoming) <= MAX_QUEUED_DROP_BYTES
|
||||
}
|
||||
|
||||
/// `warnings` from an import, plus one for each font family the active document's type uses that
|
||||
/// isn't installed (#4559): Photoshop warns about missing fonts as a file opens, and the CLI and
|
||||
/// automation already report them; the type would otherwise silently render in a fallback face.
|
||||
@@ -247,6 +284,11 @@ impl PhotocraftApp {
|
||||
});
|
||||
}
|
||||
|
||||
/// Show "Couldn't place <name>: <error>" as an error (status bar + notice).
|
||||
pub fn place_failed(&mut self, name: &str, err: &str) {
|
||||
self.failed(crate::i18n::fmt(tl!("Couldn't place {name}: {error}"), &[("name", name), ("error", err)]));
|
||||
}
|
||||
|
||||
/// Log `msg` and show it as an error (status bar + notice).
|
||||
fn failed(&mut self, msg: String) {
|
||||
log::warn!("{msg}");
|
||||
@@ -272,7 +314,9 @@ impl PhotocraftApp {
|
||||
for f in files {
|
||||
let name = dropped_name(&f);
|
||||
if target == DropTarget::Canvas && !crate::preset_files_ui::is_preset_file(&name) {
|
||||
self.drop_places.push_back(DropPlace::At { source: Box::new(DropPlace::File(f)), frame });
|
||||
if let Some(doc) = self.session.active().map(|st| st.doc.id) {
|
||||
self.queue_drop(doc, DropPlace::At { source: Box::new(DropPlace::File(f)), frame });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
match self.open_dropped_file(&f, &name, slot) {
|
||||
@@ -295,8 +339,12 @@ impl PhotocraftApp {
|
||||
let at = self.services.cursor_pos.as_mut().and_then(|f| f(ctx));
|
||||
let canvas = self.drop_target(ctx, at) == DropTarget::Canvas;
|
||||
let places: Vec<bool> = names.iter().map(|n| canvas && !crate::preset_files_ui::is_preset_file(n)).collect();
|
||||
if places.contains(&true) && self.ui.transform.is_some() {
|
||||
crate::transform_tool::commit(self);
|
||||
if places.contains(&true) {
|
||||
// The shell reads the files after this returns: they go to this document even if the
|
||||
// tab changes before they arrive.
|
||||
if self.ui.transform.is_some() {
|
||||
crate::transform_tool::commit(self);
|
||||
}
|
||||
}
|
||||
places
|
||||
}
|
||||
@@ -337,9 +385,27 @@ impl PhotocraftApp {
|
||||
|
||||
/// Place the next file dropped on the canvas and start Free Transform on it, once no other
|
||||
/// transform is in progress (so several files are placed and transformed one after another).
|
||||
/// With background jobs on, a file is placed by a job: the next one waits until that job has
|
||||
/// landed and its Free Transform has been applied (see `jobs_ui::finish_place`).
|
||||
///
|
||||
/// Each file goes into the document it was dropped on, never into whichever one is active now:
|
||||
/// files dropped on a document in another tab wait until it is back in view, and are discarded
|
||||
/// (with a message) when it is closed.
|
||||
pub(crate) fn place_next_dropped(&mut self, ctx: &egui::Context) {
|
||||
while self.ui.transform.is_none() {
|
||||
let Some(next) = self.drop_places.pop_front() else { return };
|
||||
// Leave the closed-document notice up for this frame: starting the next queued placement
|
||||
// would clear the status bar. The next frame retries the queue, so ask for one at once:
|
||||
// another document's files would otherwise wait for the next input event.
|
||||
if self.discard_drops_of_closed_documents() {
|
||||
if !self.drop_places.is_empty() {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
return;
|
||||
}
|
||||
while self.ui.transform.is_none() && self.jobs.place.is_none() && self.session.active_job().is_none() {
|
||||
let Some(active) = self.session.active().map(|st| st.doc.id) else { return };
|
||||
let Some((_, next)) = self.drop_places.iter().position(|(doc, _)| *doc == active).and_then(|i| self.drop_places.remove(i)) else {
|
||||
return;
|
||||
};
|
||||
let (next, frame) = match next {
|
||||
DropPlace::At { source, frame } => (*source, Some(frame)),
|
||||
next => (next, None),
|
||||
@@ -372,6 +438,8 @@ impl PhotocraftApp {
|
||||
}
|
||||
};
|
||||
match placed {
|
||||
// Running in the background: Free Transform starts when the layer has landed.
|
||||
Ok(started) if crate::jobs_ui::track_place(self, &name, &started) => return,
|
||||
Ok(result) => {
|
||||
if result.get("frame").and_then(serde_json::Value::as_u64).is_some() {
|
||||
continue;
|
||||
@@ -380,18 +448,66 @@ impl PhotocraftApp {
|
||||
self.ui.status = e;
|
||||
}
|
||||
}
|
||||
Err(e) => self.failed(crate::i18n::fmt(tl!("Couldn't place {name}: {error}"), &[("name", &name), ("error", &e)])),
|
||||
Err(e) => self.place_failed(&name, &e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Queue the web drops onto the canvas that the shell has read since the last frame
|
||||
/// ([`Services::place_inbox`](crate::Services::place_inbox)), for the document they were
|
||||
/// dropped on.
|
||||
pub(crate) fn queue_web_places(&mut self) {
|
||||
let arrived: Vec<(DocId, String, Vec<u8>)> =
|
||||
self.services.place_inbox.as_ref().map(|q| std::mem::take(&mut *q.lock().unwrap_or_else(|e| e.into_inner()))).unwrap_or_default();
|
||||
for (doc, name, bytes) in arrived {
|
||||
self.queue_drop(doc, DropPlace::Bytes(name, bytes));
|
||||
}
|
||||
}
|
||||
|
||||
/// Queue a canvas drop for the document it was dropped on, keeping the file contents it holds
|
||||
/// bounded: files dropped on a document in another tab wait here, and a browser drop carries
|
||||
/// its whole file. A path drop holds no contents, so any number of them queue.
|
||||
fn queue_drop(&mut self, doc: DocId, place: DropPlace) {
|
||||
let held = self.drop_places.iter().filter(|(_, p)| p.holds_bytes()).count();
|
||||
let bytes = self.drop_places.iter().fold(0usize, |sum, (_, p)| sum.saturating_add(p.queued_bytes()));
|
||||
if place.holds_bytes() && !queue_takes_drop(held, bytes, place.queued_bytes()) {
|
||||
self.ui.status = tl!("Dropped files weren't placed: too many are waiting for their document.").into();
|
||||
self.ui.status_error = true;
|
||||
return;
|
||||
}
|
||||
self.drop_places.push_back((doc, place));
|
||||
}
|
||||
|
||||
/// Drop the queued files whose document has been closed, saying so. True when one was dropped
|
||||
/// (the caller lets the notice stand).
|
||||
fn discard_drops_of_closed_documents(&mut self) -> bool {
|
||||
if self.drop_places.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let before = self.drop_places.len();
|
||||
let open: Vec<DocId> = self.session.documents().iter().map(|st| st.doc.id).collect();
|
||||
self.drop_places.retain(|(doc, _)| open.contains(doc));
|
||||
let discarded = self.drop_places.len() < before;
|
||||
if discarded {
|
||||
self.ui.status = tl!("Dropped files weren't placed: their document was closed.").into();
|
||||
self.ui.status_error = false;
|
||||
}
|
||||
discarded
|
||||
}
|
||||
|
||||
/// Place a file's bytes as a smart object layer (File › Place; `linked`: Place Linked, to that
|
||||
/// path), for files that weren't read from a path the command can read.
|
||||
/// path), for files that weren't read from a path the command can read. With background jobs
|
||||
/// on it starts a job (`{"job", "pending": true}`; see [`crate::jobs_ui::track_place`]).
|
||||
pub(crate) fn place_bytes(&mut self, name: &str, bytes: Vec<u8>, linked: Option<String>) -> Result<serde_json::Value, String> {
|
||||
self.place_bytes_with(name, bytes, linked, &serde_json::json!({}))
|
||||
}
|
||||
|
||||
fn place_bytes_with(&mut self, name: &str, bytes: Vec<u8>, linked: Option<String>, params: &serde_json::Value) -> Result<serde_json::Value, String> {
|
||||
let r = photocraft_engine::file_cmds::place_bytes(&mut self.session, name, bytes, linked, params).map_err(|e| e.to_string());
|
||||
let r = if self.background_jobs {
|
||||
crate::jobs_ui::start_place_bytes(self, name, bytes, linked, params)
|
||||
} else {
|
||||
photocraft_engine::file_cmds::place_bytes(&mut self.session, name, bytes, linked, params).map_err(|e| e.to_string())
|
||||
};
|
||||
self.sync_views();
|
||||
r
|
||||
}
|
||||
|
||||
@@ -189,8 +189,9 @@ fn web_drops_onto_the_canvas_are_placed_from_their_bytes() {
|
||||
at.set(egui::pos2(20.0, 20.0));
|
||||
assert_eq!(app.dropped_on_canvas(&ctx, &names), [false, false], "off the canvas: opened");
|
||||
let layers = layer_count(&app);
|
||||
app.drop_places.push_back(crate::file_open::DropPlace::Bytes("photo.png".into(), png_bytes(4, 3)));
|
||||
app.drop_places.push_back(crate::file_open::DropPlace::Bytes("bad.png".into(), b"bad".to_vec()));
|
||||
let doc = app.session.active().unwrap().doc.id;
|
||||
app.drop_places.push_back((doc, crate::file_open::DropPlace::Bytes("photo.png".into(), png_bytes(4, 3))));
|
||||
app.drop_places.push_back((doc, crate::file_open::DropPlace::Bytes("bad.png".into(), b"bad".to_vec())));
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(layer_count(&app), layers + 1);
|
||||
assert_eq!(app.ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
@@ -538,6 +539,366 @@ fn a_separate_drop_applies_the_open_transform_and_places_at_once() {
|
||||
assert_eq!(history_steps(&app), steps + 2);
|
||||
}
|
||||
|
||||
/// Apply the jobs that have finished, frame by frame, until `done` holds (or 30 s pass).
|
||||
fn tick_until(app: &mut PhotocraftApp, ctx: &egui::Context, done: impl Fn(&PhotocraftApp) -> bool) {
|
||||
let t = std::time::Instant::now();
|
||||
while !done(app) {
|
||||
assert!(t.elapsed() < std::time::Duration::from_secs(30), "not done after 30 s");
|
||||
crate::jobs_ui::tick(app, ctx);
|
||||
app.place_next_dropped(ctx);
|
||||
std::thread::sleep(std::time::Duration::from_millis(2));
|
||||
}
|
||||
}
|
||||
|
||||
/// #10031: with background jobs a dropped file is decoded on a worker. Free Transform starts when
|
||||
/// the layer has landed, and the next file is started only after that transform is applied.
|
||||
#[test]
|
||||
fn background_drops_are_placed_by_a_job_one_after_another() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
app.background_jobs = true;
|
||||
let (layers, steps) = (layer_count(&app), history_steps(&app));
|
||||
let at = Some(egui::pos2(400.0, 300.0));
|
||||
app.open_dropped(&ctx, vec![dropped("first.png", Ok(png_bytes(4, 4))), dropped("second.png", Ok(png_bytes(2, 2)))], at);
|
||||
app.place_next_dropped(&ctx);
|
||||
let place = app.jobs.place.clone().expect("the first file is being placed");
|
||||
assert_eq!(place.name, "first.png");
|
||||
assert!(app.session.job(place.job).is_some(), "as a background job");
|
||||
assert_eq!((layer_count(&app), history_steps(&app)), (layers, steps), "nothing lands before the job is applied");
|
||||
assert!(app.ui.transform.is_none(), "Free Transform waits for the layer");
|
||||
// Frames while it runs don't start the second file next to it.
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(app.drop_places.len(), 1);
|
||||
assert_eq!(app.jobs.place.as_ref().map(|p| p.job), Some(place.job));
|
||||
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
assert_eq!(layer_count(&app), layers + 1);
|
||||
assert_eq!(app.ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().name, "first");
|
||||
// The second file waits for the first transform.
|
||||
app.place_next_dropped(&ctx);
|
||||
assert!(app.jobs.place.is_none() && app.drop_places.len() == 1);
|
||||
crate::transform_tool::commit(&mut app);
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(app.jobs.place.as_ref().map(|p| p.name.as_str()), Some("second.png"));
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().name, "second");
|
||||
assert_eq!(app.ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
crate::transform_tool::commit(&mut app);
|
||||
assert_eq!((layer_count(&app), history_steps(&app)), (layers + 2, steps + 2));
|
||||
assert!(app.drop_places.is_empty());
|
||||
}
|
||||
|
||||
/// A file that can't be decoded is reported by name and the files dropped after it go on.
|
||||
#[test]
|
||||
fn a_background_place_that_fails_reports_it_and_the_queue_continues() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
app.background_jobs = true;
|
||||
let layers = layer_count(&app);
|
||||
app.open_dropped(&ctx, vec![dropped("bad.png", Ok(b"bad".to_vec())), dropped("good.png", Ok(png_bytes(2, 2)))], Some(egui::pos2(400.0, 300.0)));
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(app.jobs.place.as_ref().map(|p| p.name.as_str()), Some("bad.png"));
|
||||
tick_until(&mut app, &ctx, |app| app.ui.notices.iter().any(|n| n.error && n.title.contains("bad.png")));
|
||||
assert_eq!(layer_count(&app), layers, "nothing was placed");
|
||||
assert_eq!(app.jobs.place.as_ref().map(|p| p.name.as_str()), Some("good.png"), "the next file started at once");
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
assert_eq!(layer_count(&app), layers + 1);
|
||||
assert_eq!(app.ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
assert_eq!(app.ui.notices.iter().filter(|n| n.error).count(), 1);
|
||||
}
|
||||
|
||||
/// Cancelling the place says so, places nothing and goes on with the queue.
|
||||
#[test]
|
||||
fn cancelling_a_background_place_continues_the_queue() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
app.background_jobs = true;
|
||||
let (layers, steps) = (layer_count(&app), history_steps(&app));
|
||||
app.open_dropped(&ctx, vec![dropped("first.png", Ok(png_bytes(4, 4))), dropped("second.png", Ok(png_bytes(2, 2)))], Some(egui::pos2(400.0, 300.0)));
|
||||
app.place_next_dropped(&ctx);
|
||||
let first = app.jobs.place.clone().expect("being placed");
|
||||
assert!(crate::jobs_ui::cancel(&mut app, first.job));
|
||||
crate::jobs_ui::tick(&mut app, &ctx);
|
||||
assert_eq!((layer_count(&app), history_steps(&app)), (layers, steps));
|
||||
let second = app.jobs.place.clone().expect("the queue went on");
|
||||
assert_eq!(second.name, "second.png");
|
||||
assert_ne!(second.job, first.job);
|
||||
assert!(!app.ui.status_error);
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().name, "second");
|
||||
assert_eq!(layer_count(&app), layers + 1);
|
||||
}
|
||||
|
||||
/// Frames with nothing to wait for: jobs applied and the drop queue served.
|
||||
fn idle_frames(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
for _ in 0..3 {
|
||||
crate::jobs_ui::tick(app, ctx);
|
||||
app.place_next_dropped(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/// Drops two files on the canvas of document A (the app's only one) with background jobs on, starts
|
||||
/// placing the first, then opens document B, which becomes active. Returns A's id and layer count.
|
||||
fn two_drops_then_switch(app: &mut PhotocraftApp, ctx: &egui::Context) -> (photocraft_doc::DocId, usize) {
|
||||
app.background_jobs = true;
|
||||
let (a, layers) = (app.session.active().unwrap().doc.id, layer_count(app));
|
||||
app.open_dropped(ctx, vec![dropped("first.png", Ok(png_bytes(4, 4))), dropped("second.png", Ok(png_bytes(2, 2)))], Some(egui::pos2(400.0, 300.0)));
|
||||
app.place_next_dropped(ctx);
|
||||
assert_eq!(app.jobs.place.as_ref().map(|p| (p.name.as_str(), p.doc)), Some(("first.png", a)));
|
||||
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
assert_ne!(app.session.active().unwrap().doc.id, a);
|
||||
(a, layers)
|
||||
}
|
||||
|
||||
/// FredApps' review of #10147: files dropped on A stay A's when the tab changes while the first is
|
||||
/// placed. It lands in A, no Free Transform opens over B, the second waits rather than going into B,
|
||||
/// and goes on in A when A is back in view.
|
||||
#[test]
|
||||
fn background_drops_stay_with_their_document_when_the_tab_changes() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
let (a, layers) = two_drops_then_switch(&mut app, &ctx);
|
||||
let (b_layers, b_steps) = (layer_count(&app), history_steps(&app));
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
idle_frames(&mut app, &ctx);
|
||||
assert!(app.jobs.place.is_none() && app.ui.transform.is_none());
|
||||
assert_eq!((layer_count(&app), history_steps(&app)), (b_layers, b_steps), "nothing placed in B");
|
||||
assert_eq!(app.session.documents()[0].doc.layers.len(), layers + 1, "the first landed in A");
|
||||
assert_eq!(app.drop_places.len(), 1, "the second waits for A");
|
||||
|
||||
assert!(app.session.set_active(0));
|
||||
app.sync_views();
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(app.jobs.place.as_ref().map(|p| (p.name.as_str(), p.doc)), Some(("second.png", a)));
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
assert_eq!(layer_count(&app), layers + 2);
|
||||
assert_eq!(app.ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
assert!(app.drop_places.is_empty());
|
||||
assert_eq!(app.session.documents()[1].doc.layers.len(), b_layers);
|
||||
}
|
||||
|
||||
/// Cancelling A's place while B is in view doesn't hand A's next file to B.
|
||||
#[test]
|
||||
fn cancelling_a_background_place_from_another_tab_keeps_the_queue_in_its_document() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
let (a, layers) = two_drops_then_switch(&mut app, &ctx);
|
||||
let (b_layers, b_steps) = (layer_count(&app), history_steps(&app));
|
||||
let first = app.jobs.place.clone().unwrap();
|
||||
assert!(crate::jobs_ui::cancel(&mut app, first.job));
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
idle_frames(&mut app, &ctx);
|
||||
assert!(app.jobs.place.is_none());
|
||||
assert_eq!((layer_count(&app), history_steps(&app)), (b_layers, b_steps), "nothing placed in B");
|
||||
assert_eq!(app.session.documents()[0].doc.layers.len(), layers, "the cancelled file wasn't placed");
|
||||
assert_eq!(app.drop_places.len(), 1);
|
||||
assert!(app.session.set_active(0));
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(app.jobs.place.as_ref().map(|p| (p.name.as_str(), p.doc)), Some(("second.png", a)));
|
||||
}
|
||||
|
||||
/// Closing A drops its waiting files with a message; none goes into B.
|
||||
#[test]
|
||||
fn closing_the_document_discards_its_dropped_files() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
two_drops_then_switch(&mut app, &ctx);
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
app.run("file.close", json!({"document": 0})).unwrap();
|
||||
let (b_layers, b_steps) = (layer_count(&app), history_steps(&app));
|
||||
app.place_next_dropped(&ctx);
|
||||
assert!(app.drop_places.is_empty());
|
||||
assert!(app.ui.status.contains("closed") && !app.ui.status_error, "{}", app.ui.status);
|
||||
idle_frames(&mut app, &ctx);
|
||||
assert!(app.jobs.place.is_none() && app.ui.transform.is_none());
|
||||
assert_eq!((layer_count(&app), history_steps(&app)), (b_layers, b_steps));
|
||||
}
|
||||
|
||||
/// The closed-document notice survives the same call that would start the next queued file: the
|
||||
/// placement waits one frame, so the message is drawn. That frame is asked for at once, so B's file
|
||||
/// doesn't wait for the next input event.
|
||||
#[test]
|
||||
fn the_closed_document_notice_stays_when_the_next_queued_file_starts() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
let a = app.session.active().unwrap().doc.id;
|
||||
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
let b = app.session.active().unwrap().doc.id;
|
||||
let b_layers = layer_count(&app);
|
||||
app.drop_places.push_back((a, crate::file_open::DropPlace::Bytes("a.png".into(), png_bytes(4, 3))));
|
||||
app.drop_places.push_back((b, crate::file_open::DropPlace::Bytes("b.png".into(), png_bytes(4, 3))));
|
||||
app.run("file.close", json!({"document": 0})).unwrap();
|
||||
for _ in 0..4 {
|
||||
ctx.run_ui(egui::RawInput::default(), |_| {}).textures_delta.clear();
|
||||
}
|
||||
assert!(!ctx.has_requested_repaint(), "idle");
|
||||
app.place_next_dropped(&ctx);
|
||||
assert!(app.ui.status.contains("closed"), "{}", app.ui.status);
|
||||
assert_eq!(app.drop_places.len(), 1, "B's file waits for the next frame");
|
||||
assert!(ctx.has_requested_repaint(), "and asks for it");
|
||||
assert!(app.jobs.place.is_none() && app.ui.transform.is_none());
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(layer_count(&app), b_layers + 1);
|
||||
assert!(app.drop_places.is_empty());
|
||||
}
|
||||
|
||||
/// The drop queue is bounded: files dropped on another tab wait, and a browser drop holds its whole
|
||||
/// file, so neither a flood of drops nor a huge one grows it without limit.
|
||||
#[test]
|
||||
fn the_drop_queue_is_bounded() {
|
||||
let mut app = app_with_canvas();
|
||||
let doc = app.session.active().unwrap().doc.id;
|
||||
for _ in 0..crate::file_open::MAX_QUEUED_DROPS {
|
||||
app.queue_drop(doc, crate::file_open::DropPlace::Bytes("p.png".into(), vec![0]));
|
||||
}
|
||||
assert_eq!(app.drop_places.len(), crate::file_open::MAX_QUEUED_DROPS);
|
||||
app.queue_drop(doc, crate::file_open::DropPlace::Bytes("over.png".into(), vec![0]));
|
||||
assert_eq!(app.drop_places.len(), crate::file_open::MAX_QUEUED_DROPS, "the queue keeps its cap");
|
||||
assert!(app.ui.status.contains("too many") && app.ui.status_error, "{}", app.ui.status);
|
||||
assert!(crate::file_open::queue_takes_drop(0, crate::file_open::MAX_QUEUED_DROP_BYTES, 0));
|
||||
assert!(!crate::file_open::queue_takes_drop(0, crate::file_open::MAX_QUEUED_DROP_BYTES, 1));
|
||||
assert!(!crate::file_open::queue_takes_drop(crate::file_open::MAX_QUEUED_DROPS, 0, 0));
|
||||
}
|
||||
|
||||
/// Path drops hold no file contents while they wait, so the cap on browser drops doesn't apply to
|
||||
/// them: dropping 40 files on the canvas queues all of them, and a browser drop after them still fits.
|
||||
#[test]
|
||||
fn many_path_drops_all_queue() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
let doc = app.session.active().unwrap().doc.id;
|
||||
// More than the browser drops' cap; none is read while it waits.
|
||||
let n = crate::file_open::MAX_QUEUED_DROPS + 8;
|
||||
app.open_dropped(&ctx, (0..n).map(|i| dropped(format!("p{i}.png"), Ok(Vec::new()))).collect(), Some(egui::pos2(400.0, 300.0)));
|
||||
assert_eq!(app.drop_places.len(), n);
|
||||
assert!(!app.ui.status_error, "{}", app.ui.status);
|
||||
app.queue_drop(doc, crate::file_open::DropPlace::Bytes("web.png".into(), vec![0]));
|
||||
assert_eq!(app.drop_places.len(), n + 1, "path drops don't use up the browser drops' room");
|
||||
}
|
||||
|
||||
/// Closing A while its file is being placed: nothing lands anywhere, and A's next file is dropped.
|
||||
#[test]
|
||||
fn closing_the_document_while_its_file_is_placed_places_nothing_elsewhere() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
two_drops_then_switch(&mut app, &ctx);
|
||||
let (b_layers, b_steps) = (layer_count(&app), history_steps(&app));
|
||||
app.run("file.close", json!({"document": 0})).unwrap();
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
idle_frames(&mut app, &ctx);
|
||||
assert!(app.drop_places.is_empty() && app.jobs.place.is_none() && app.ui.transform.is_none());
|
||||
assert_eq!(app.session.documents().len(), 1);
|
||||
assert_eq!((layer_count(&app), history_steps(&app)), (b_layers, b_steps));
|
||||
}
|
||||
|
||||
/// Without background jobs too: leaving A's Free Transform for B cancels that place (#1099) and A's
|
||||
/// next file waits for A instead of going into B.
|
||||
#[test]
|
||||
fn inline_drops_stay_with_their_document_when_the_tab_changes() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
let layers = layer_count(&app);
|
||||
app.open_dropped(&ctx, vec![dropped("first.png", Ok(png_bytes(4, 4))), dropped("second.png", Ok(png_bytes(2, 2)))], Some(egui::pos2(400.0, 300.0)));
|
||||
app.place_next_dropped(&ctx);
|
||||
assert!(app.ui.transform.is_some());
|
||||
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
crate::transform_tool::end_if_left(&mut app);
|
||||
let (b_layers, b_steps) = (layer_count(&app), history_steps(&app));
|
||||
app.place_next_dropped(&ctx);
|
||||
assert!(app.ui.transform.is_none());
|
||||
assert_eq!((layer_count(&app), history_steps(&app)), (b_layers, b_steps), "nothing placed in B");
|
||||
assert_eq!(app.drop_places.len(), 1);
|
||||
assert!(app.session.set_active(0));
|
||||
app.sync_views();
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(app.ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
assert_eq!(layer_count(&app), layers + 1, "the first was cancelled by leaving it, the second placed");
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().name, "second");
|
||||
}
|
||||
|
||||
/// A web drop goes to the document it was dropped on, even when the tab changes before the shell
|
||||
/// has read the files.
|
||||
#[test]
|
||||
fn web_drops_go_to_the_document_they_were_dropped_on() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
let layers = layer_count(&app);
|
||||
let inbox: crate::PlaceInbox = Default::default();
|
||||
app.services.place_inbox = Some(inbox.clone());
|
||||
app.services.cursor_pos = Some(Box::new(|_: &egui::Context| Some(egui::pos2(400.0, 300.0))));
|
||||
assert_eq!(app.dropped_on_canvas(&ctx, &["photo.png".to_string()]), [true]);
|
||||
let target = app.session.active().map(|st| st.doc.id).unwrap();
|
||||
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
let (b_layers, b_steps) = (layer_count(&app), history_steps(&app));
|
||||
inbox.lock().unwrap().push((target, "photo.png".into(), png_bytes(4, 3)));
|
||||
app.queue_web_places();
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!((layer_count(&app), history_steps(&app)), (b_layers, b_steps), "nothing placed in B");
|
||||
assert!(app.session.set_active(0));
|
||||
app.sync_views();
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(layer_count(&app), layers + 1);
|
||||
assert_eq!(app.ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlapping_web_drop_deliveries_keep_each_target() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
let a = app.session.active().unwrap().doc.id;
|
||||
let a_layers = layer_count(&app);
|
||||
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
let b = app.session.active().unwrap().doc.id;
|
||||
let b_layers = layer_count(&app);
|
||||
let inbox: crate::PlaceInbox = Default::default();
|
||||
app.services.place_inbox = Some(inbox.clone());
|
||||
// B's read finishes before A's, although A was dropped first.
|
||||
inbox.lock().unwrap().extend([(b, "b.png".into(), png_bytes(3, 2)), (a, "a.png".into(), png_bytes(4, 3))]);
|
||||
app.queue_web_places();
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(layer_count(&app), b_layers + 1);
|
||||
crate::transform_tool::commit(&mut app);
|
||||
assert!(app.session.set_active(0));
|
||||
app.sync_views();
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(layer_count(&app), a_layers + 1);
|
||||
assert!(app.drop_places.is_empty());
|
||||
}
|
||||
|
||||
/// A file dropped from a path is placed by the `file.placeEmbedded` job, read on its worker, and a
|
||||
/// drop that arrives meanwhile queues behind it.
|
||||
#[test]
|
||||
fn a_background_drop_from_a_path_is_tracked_and_later_drops_queue() {
|
||||
let dir = std::env::temp_dir().join(format!("photocraft-drop-place-job-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let abs = dir.join("photo.png");
|
||||
std::fs::write(&abs, png_bytes(40, 20)).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_canvas();
|
||||
app.background_jobs = true;
|
||||
let layers = layer_count(&app);
|
||||
let at = Some(egui::pos2(400.0, 300.0));
|
||||
app.open_dropped(&ctx, vec![dropped(abs, Err("egui's reader must not be used".into()))], at);
|
||||
app.place_next_dropped(&ctx);
|
||||
let place = app.jobs.place.clone().expect("being placed");
|
||||
assert_eq!(place.name, "photo.png");
|
||||
assert_eq!(app.jobs.last_started, Some(place.job));
|
||||
// A drop while it runs is queued, not started beside it.
|
||||
app.open_dropped(&ctx, vec![dropped("later.png", Ok(png_bytes(2, 2)))], at);
|
||||
app.place_next_dropped(&ctx);
|
||||
assert_eq!(app.drop_places.len(), 1);
|
||||
assert_eq!(app.session.jobs().len(), 1);
|
||||
tick_until(&mut app, &ctx, |app| app.jobs.place.is_none());
|
||||
assert_eq!(layer_count(&app), layers + 1);
|
||||
assert_eq!(app.drop_places.len(), 1, "waiting for the transform");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// #1099: switching tabs after an actual canvas drop cancels the place in its source document.
|
||||
#[test]
|
||||
fn switching_documents_after_a_canvas_drop_undoes_only_the_place() {
|
||||
@@ -694,6 +1055,12 @@ fn drag_and_drop_in_the_running_app() {
|
||||
pointer.set(h.state().drop_canvas_rect.expect("the canvas is shown").center());
|
||||
h.input_mut().dropped_files.push(dropped(file, Err("read from the path".into())));
|
||||
h.run_steps(2);
|
||||
// Placement runs on a worker; frame count does not imply completion.
|
||||
let started = std::time::Instant::now();
|
||||
while h.state().jobs.place.is_some() && started.elapsed() < std::time::Duration::from_secs(30) {
|
||||
h.step();
|
||||
}
|
||||
assert!(h.state().jobs.place.is_none(), "placement completed");
|
||||
assert_eq!(h.state().session.documents().len(), 3, "no new document");
|
||||
assert_eq!(h.state().session.active().unwrap().doc.layers.len(), layers + 1);
|
||||
assert_eq!(h.state().ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
@@ -1127,3 +1494,21 @@ fn opening_an_open_file_again_shows_its_tab() {
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queued_drop_waits_for_an_external_document_job() {
|
||||
let mut app = app_with_canvas();
|
||||
let doc = app.session.active().unwrap().doc.id;
|
||||
app.session.start_job("test.external", json!({}), "External job", true, |_| Ok(()), |_, ()| Ok(json!(null))).unwrap();
|
||||
let job = app.session.active_job().unwrap().id;
|
||||
app.drop_places.push_back((doc, crate::file_open::DropPlace::Bytes("queued.png".into(), png_bytes(4, 3))));
|
||||
app.place_next_dropped(&egui::Context::default());
|
||||
assert_eq!(app.drop_places.len(), 1, "busy document retains its drop");
|
||||
assert!(app.jobs.place.is_none());
|
||||
let layers = layer_count(&app);
|
||||
app.session.wait_job(job).unwrap();
|
||||
app.place_next_dropped(&egui::Context::default());
|
||||
assert_eq!(layer_count(&app), layers + 1);
|
||||
assert_eq!(app.ui.transform.as_ref().and_then(|t| t.made), Some(crate::state::MadeLayer::Place));
|
||||
assert!(app.drop_places.is_empty());
|
||||
}
|
||||
@@ -70,6 +70,17 @@ pub(crate) struct OpenBatch {
|
||||
opened: Vec<usize>,
|
||||
}
|
||||
|
||||
/// A file being placed in the background (File › Place, or a file dropped on the canvas): Free
|
||||
/// Transform starts on its layer when the job lands, and the files dropped after it follow.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PlaceJob {
|
||||
pub job: JobId,
|
||||
/// Display name (the file name).
|
||||
pub name: String,
|
||||
/// The document it lands in: Free Transform starts only while that one is in view.
|
||||
pub doc: DocId,
|
||||
}
|
||||
|
||||
/// The job command of a background save.
|
||||
pub const SAVE_JOB: &str = "file.save";
|
||||
|
||||
@@ -165,6 +176,9 @@ pub struct JobsUi {
|
||||
pub focus: Option<JobId>,
|
||||
/// The job the latest [`run`] started (the control channel waits on it).
|
||||
pub(crate) last_started: Option<JobId>,
|
||||
/// The file being placed in the background, if any. One at a time: files dropped meanwhile
|
||||
/// wait in the app's drop queue.
|
||||
pub place: Option<PlaceJob>,
|
||||
/// Cached read-only canvas image of the current folder Batch input.
|
||||
batch_preview_texture: Option<(JobId, u64, egui::TextureHandle)>,
|
||||
}
|
||||
@@ -188,6 +202,62 @@ pub fn run(app: &mut PhotocraftApp, id: &str, params: Value) -> Result<Value, St
|
||||
}
|
||||
}
|
||||
|
||||
/// Place a file's bytes in the background (File › Place for a file with no path to read, a dropped
|
||||
/// file read by the browser): like [`run`] for `file.placeEmbedded`, the result is
|
||||
/// `{"job": id, "pending": true}` and the layer lands when [`tick`] applies the job. `linked`
|
||||
/// makes it Place Linked to that path. Hand the result to [`track_place`].
|
||||
pub(crate) fn start_place_bytes(app: &mut PhotocraftApp, name: &str, bytes: Vec<u8>, linked: Option<String>, params: &Value) -> Result<Value, String> {
|
||||
if app.jobs.place.is_some() {
|
||||
return Err(tl!("A file is still being placed; try again when it has finished.").into());
|
||||
}
|
||||
match app.session.start_place_bytes(name, bytes, linked, params.clone()).map_err(|e| e.to_string())? {
|
||||
Started::Done(v) => Ok(v),
|
||||
Started::Job(job) => {
|
||||
app.jobs.last_started = Some(job);
|
||||
// The status bar's progress readout names the job; clear any old message.
|
||||
app.ui.status.clear();
|
||||
app.ui.status_error = false;
|
||||
Ok(json!({"job": job.0, "pending": true}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Follow the place that `started` (the result of [`run`] or [`start_place_bytes`] for the file
|
||||
/// `name`) began in the background: when it lands, Free Transform starts on its layer and the
|
||||
/// next dropped file is placed (see `on_event`). False when it already ran (nothing to follow).
|
||||
pub(crate) fn track_place(app: &mut PhotocraftApp, name: &str, started: &Value) -> bool {
|
||||
let job = started.get("pending").and_then(Value::as_bool).filter(|pending| *pending).and_then(|_| started.get("job")?.as_u64());
|
||||
let (Some(job), Some(doc)) = (job, app.session.active().map(|st| st.doc.id)) else { return false };
|
||||
app.jobs.place = Some(PlaceJob { job: JobId(job), name: crate::file_open::display_name(name), doc });
|
||||
true
|
||||
}
|
||||
|
||||
/// A background place ended: start Free Transform on the layer it made, or report why not, then
|
||||
/// go on with the files dropped meanwhile.
|
||||
fn finish_place(app: &mut PhotocraftApp, ctx: &egui::Context, place: PlaceJob, outcome: JobOutcome) {
|
||||
app.sync_views();
|
||||
match outcome {
|
||||
JobOutcome::Done(result) => {
|
||||
// The tab can change while the file is placed: the box opens only on the document it
|
||||
// was placed in, and never over another one. An image placed into a frame has none.
|
||||
if app.ui.transform.is_none()
|
||||
&& result.get("frame").and_then(Value::as_u64).is_none()
|
||||
&& app.session.active().is_some_and(|st| st.doc.id == place.doc)
|
||||
&& let Err(e) = crate::transform_tool::begin_placed(app, ctx)
|
||||
{
|
||||
app.ui.status = e;
|
||||
}
|
||||
}
|
||||
JobOutcome::Failed(err) => app.place_failed(&place.name, &err),
|
||||
JobOutcome::Cancelled if app.session.documents().iter().any(|st| st.doc.id == place.doc) => {
|
||||
app.ui.status = crate::i18n::fmt(tl!("Cancelled placing {name}"), &[("name", &place.name)]);
|
||||
app.ui.status_error = false;
|
||||
}
|
||||
JobOutcome::Cancelled => {}
|
||||
}
|
||||
app.place_next_dropped(ctx);
|
||||
}
|
||||
|
||||
/// Open a file in the background: a tab appears at once and shows the progress; the document
|
||||
/// replaces it when decoded. `path` is remembered for File › Save and Open Recent. `slot` (a file
|
||||
/// dropped on the tabs) and `order` (its place in a set opened together) survive an inline
|
||||
@@ -509,6 +579,12 @@ fn on_event(app: &mut PhotocraftApp, ctx: &egui::Context, e: JobEvent) {
|
||||
if crate::camera_raw_ui::on_redevelop_event(app, &e) {
|
||||
return;
|
||||
}
|
||||
if app.jobs.place.as_ref().is_some_and(|p| p.job == e.id)
|
||||
&& let Some(place) = app.jobs.place.take()
|
||||
{
|
||||
finish_place(app, ctx, place, e.outcome);
|
||||
return;
|
||||
}
|
||||
if e.command == SAVE_JOB
|
||||
&& let Some(i) = app.jobs.saves.iter().position(|s| s.job == e.id)
|
||||
{
|
||||
@@ -845,7 +921,13 @@ pub fn dialog(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
Some(j) => j,
|
||||
None => {
|
||||
let Some(j) = app.session.active_job() else { return };
|
||||
if j.command == "file.automate.batch" || j.elapsed_ms < DIALOG_DELAY_MS || app.jobs.saves.iter().any(|s| s.job == j.id) {
|
||||
// A save shows its progress in the tab and the status bar; so does a place, which
|
||||
// leaves the view free to zoom and pan while the file is decoded.
|
||||
let place = place_uses_status_bar(
|
||||
&j.command,
|
||||
!app.ui.view.hides_chrome() && app.ui.panels.status_bar && job_in_view(app).is_some_and(|shown| shown.id == j.id),
|
||||
);
|
||||
if j.command == "file.automate.batch" || j.elapsed_ms < DIALOG_DELAY_MS || place || app.jobs.saves.iter().any(|s| s.job == j.id) {
|
||||
return;
|
||||
}
|
||||
j
|
||||
@@ -1008,6 +1090,7 @@ pub fn inspect(app: &PhotocraftApp) -> Value {
|
||||
"running": app.session.jobs(),
|
||||
"opening": app.jobs.opens.iter().map(|o| json!({"job": o.job.0, "name": o.name, "path": o.path})).collect::<Vec<_>>(),
|
||||
"saving": app.jobs.saves.iter().map(|s| json!({"job": s.job.0, "path": s.path})).collect::<Vec<_>>(),
|
||||
"placing": app.jobs.place.as_ref().map(|p| json!({"job": p.job.0, "name": p.name})),
|
||||
"focus": app.jobs.focus.map(|j| j.0),
|
||||
"background": app.background_jobs,
|
||||
})
|
||||
@@ -1020,3 +1103,20 @@ pub fn bytes(b: &[u8]) -> OpenSource {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Placements keep the canvas accessible when progress and Cancel are visible in the status bar.
|
||||
fn place_uses_status_bar(command: &str, status_bar_visible: bool) -> bool {
|
||||
status_bar_visible && matches!(command, "file.placeEmbedded" | "file.placeLinked")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod place_progress_tests {
|
||||
#[test]
|
||||
fn placement_modal_depends_on_command_and_visible_progress() {
|
||||
for command in ["file.placeEmbedded", "file.placeLinked"] {
|
||||
assert!(super::place_uses_status_bar(command, true));
|
||||
assert!(!super::place_uses_status_bar(command, false));
|
||||
}
|
||||
assert!(!super::place_uses_status_bar("filter.blur", true));
|
||||
}
|
||||
}
|
||||
@@ -260,8 +260,8 @@ pub use services::{
|
||||
AdoptAutosaveFn, AppendTextFn, AutomationCommandFn, AutomationOpenPcraftFn, AutomationReadFn, AutomationWriteFn, AutosaveFn, AutosaveResultsFn, CapsLockFn,
|
||||
CaptionStyleFn, ClipboardGetFn, ClipboardGetSvgFn, ClipboardSetFn, ClipboardUnreadableFn, CursorPosFn, DiscardAutosaveFn, EncodePngFn, ExportFn,
|
||||
ExportSettings, ExternalSvgEditor, ExternalSvgSession, ExternalVectorSave, ExternalVectorSource, FileStamp, FileStampFn, FormatChoices, ImportFn, Inbox,
|
||||
LoadTextFn, OpenExternalSvgFn, OpenUrlFn, OsEventsFn, PhotoshopShortcutsFn, QuitFn, RecoverFn, Recoverable, RightButtonDownFn, SameFileFn, SaveFileFn,
|
||||
SaveTextFn, Services, SystemThemeFn, WindowFrameFn, WriteFn,
|
||||
LoadTextFn, OpenExternalSvgFn, OpenUrlFn, OsEventsFn, PhotoshopShortcutsFn, PlaceInbox, QuitFn, RecoverFn, Recoverable, RightButtonDownFn, SameFileFn,
|
||||
SaveFileFn, SaveTextFn, Services, SystemThemeFn, WindowFrameFn, WriteFn,
|
||||
};
|
||||
pub use text::bidi_text;
|
||||
pub use text::cjk_fonts;
|
||||
@@ -486,10 +486,11 @@ pub struct PhotocraftApp {
|
||||
pub(crate) drop_canvas_rect: Option<egui::Rect>,
|
||||
/// The document tab strip last frame: a drop there opens the file at the slot under it.
|
||||
pub(crate) tab_strip: Option<canvas::TabStrip>,
|
||||
/// Files dropped on the canvas still to place, one Free Transform at a time.
|
||||
pub(crate) frame_tool_ctx: Option<egui::Context>,
|
||||
pub(crate) frame_preview: Option<frame_tool::Preview>,
|
||||
pub(crate) drop_places: std::collections::VecDeque<file_open::DropPlace>,
|
||||
/// Files dropped on the canvas still to place, one Free Transform at a time, each with the
|
||||
/// document it was dropped on.
|
||||
pub(crate) drop_places: std::collections::VecDeque<(DocId, file_open::DropPlace)>,
|
||||
/// The document each of `ui.views` belongs to, as of the last [`Self::sync_views`].
|
||||
view_docs: Vec<DocId>,
|
||||
/// The document `ui.selected_path` belongs to, as of the last [`Self::sync_views`]: a Paths
|
||||
|
||||
@@ -323,7 +323,8 @@ pub(super) fn front(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, para
|
||||
app.refocus(doc)?;
|
||||
let linked = linked.then(|| name.clone());
|
||||
let result = app.place_bytes(&name, bytes, linked)?;
|
||||
if result.get("frame").and_then(Value::as_u64).is_none() {
|
||||
// In the background, Free Transform starts when the layer has landed.
|
||||
if result.get("frame").and_then(Value::as_u64).is_none() && !crate::jobs_ui::track_place(app, &name, &result) {
|
||||
crate::transform_tool::begin_placed(app, &ctx)?;
|
||||
}
|
||||
Ok(result)
|
||||
|
||||
@@ -331,6 +331,8 @@ pub struct ExternalVectorSource {
|
||||
pub type OpenExternalSvgFn = Box<dyn FnMut(ExternalSvgEditor, ExternalVectorSource) -> Result<Box<dyn ExternalSvgSession>, String>>;
|
||||
/// Shared queue of (file name, bytes) delivered asynchronously.
|
||||
pub type Inbox = std::sync::Arc<std::sync::Mutex<Vec<(String, Vec<u8>)>>>;
|
||||
/// Shared queue of browser canvas drops with the document captured at the drop event.
|
||||
pub type PlaceInbox = std::sync::Arc<std::sync::Mutex<Vec<(photocraft_doc::DocId, String, Vec<u8>)>>>;
|
||||
|
||||
/// Read the saved preferences text (`None` when there is none yet).
|
||||
pub type LoadTextFn = Box<dyn FnMut() -> Option<String>>;
|
||||
@@ -461,7 +463,7 @@ pub struct Services {
|
||||
/// Files delivered asynchronously for a drop onto the active document's canvas (web
|
||||
/// drag-and-drop, routed by [`PhotocraftApp::dropped_on_canvas`]): placed as layers like a
|
||||
/// desktop canvas drop (Place Embedded, each in Free Transform).
|
||||
pub place_inbox: Option<Inbox>,
|
||||
pub place_inbox: Option<PlaceInbox>,
|
||||
/// OS clipboard images: copies go out, screenshots and images from other apps come in.
|
||||
pub clipboard_set_image: Option<ClipboardSetFn>,
|
||||
pub clipboard_get_image: Option<ClipboardGetFn>,
|
||||
|
||||
@@ -251,7 +251,7 @@ The shape is pinned by a test in `crates/automation/src/rpc.rs` (#3684).
|
||||
|
||||
### Background jobs (#210)
|
||||
|
||||
Long commands (every `filter.*`, `edit.contentAwareFill`, `edit.contentAwareScale`, `file.automate.photomerge`, `brush.presets.importAbr`), file opens and interactive saves (File › Save, Save As and the TIFF Options continuation, #2017) run as background jobs in the desktop app: the window keeps drawing, the status bar shows progress with a Cancel button, and jobs that lock the active document show a modal progress dialog (Esc cancels). `engine.execute`, `ui.menu.invoke`, `ui.dialog.confirm` and `app.save` still wait for the result by default; pass `"wait": false` to get `{job, pending: true}` at once, then poll `jobs.list` (`state`: running, done, failed, cancelled; `progress` 0–1; the result or error) and stop it with `jobs.cancel {job}`. A cancelled or failed job leaves the document unchanged; a folder Batch can already have written earlier output files. A cancelled Batch has `state: "cancelled"` with its partial `result` in `jobs.list`; a waiting control request receives `{"ok":false,"error":"cancelled","result":…}`, and an MCP call through the app bridge gets the error `cancelled; partial result: {…}`. A Batch already asked to stop, or past its last input, is listed with `reason: "finishing"`. A save that has started replacing its file can no longer be cancelled: `jobs.cancel` leaves it running and lists it in `notCancelled: [{job, reason: "committing"}]` (next to `cancelled`, the number of jobs it did cancel); the save then ends as done or failed like any other. Closing its document doesn't stop it either. While a job runs, commands that would edit its document fail with "… is still running on this document". `ui.inspect` reports `jobs` (running jobs, opening files, saving files). Set `PHOTOCRAFT_INLINE_JOBS=1` to run everything inline.
|
||||
Long commands (every `filter.*`, `edit.contentAwareFill`, `edit.contentAwareScale`, `file.automate.photomerge`, `brush.presets.importAbr`), file opens, file places (`file.placeEmbedded` / `file.placeLinked`, File › Place and files dropped on the canvas: the file is read, decoded and resized on the worker, the layer is inserted when the job lands and Free Transform starts then) and interactive saves (File › Save, Save As and the TIFF Options continuation, #2017) run as background jobs in the desktop app: the window keeps drawing, the status bar shows progress with a Cancel button, and jobs that lock the active document show a modal progress dialog (Esc cancels; a place shows its progress in the status bar only, so the view stays free to pan and zoom). `engine.execute`, `ui.menu.invoke`, `ui.dialog.confirm` and `app.save` still wait for the result by default; pass `"wait": false` to get `{job, pending: true}` at once, then poll `jobs.list` (`state`: running, done, failed, cancelled; `progress` 0–1; the result or error) and stop it with `jobs.cancel {job}`. A cancelled or failed job leaves the document unchanged; a folder Batch can already have written earlier output files. A cancelled Batch has `state: "cancelled"` with its partial `result` in `jobs.list`; a waiting control request receives `{"ok":false,"error":"cancelled","result":…}`, and an MCP call through the app bridge gets the error `cancelled; partial result: {…}`. A Batch already asked to stop, or past its last input, is listed with `reason: "finishing"`. A save that has started replacing its file can no longer be cancelled: `jobs.cancel` leaves it running and lists it in `notCancelled: [{job, reason: "committing"}]` (next to `cancelled`, the number of jobs it did cancel); the save then ends as done or failed like any other. Closing its document doesn't stop it either. While a job runs, commands that would edit its document fail with "… is still running on this document". `ui.inspect` reports `jobs` (running jobs, opening files, saving files, the file being placed). Set `PHOTOCRAFT_INLINE_JOBS=1` to run everything inline.
|
||||
|
||||
## Preferences
|
||||
|
||||
|
||||
Reference in new issue
Block a user