diff --git a/crates/engine/src/cmd/type_cmds.rs b/crates/engine/src/cmd/type_cmds.rs index f1f8aa53c4..633c6930e7 100644 --- a/crates/engine/src/cmd/type_cmds.rs +++ b/crates/engine/src/cmd/type_cmds.rs @@ -655,6 +655,22 @@ pub fn refresh_at(dpi: f32, format: PixelFormat, t: &mut TextLayer) { t.psd_raw = Some(Arc::new(photocraft_text::psd::build_tysh(t, dpi, layout.bounds()))); } +/// Makes `t` paragraph text in the box `[x, y, width, height]`: the box sits at the text-space +/// origin and the layer transform moves it to `(x, y)`, keeping its linear part. `x` and `y` are +/// document px (the transform's translation); `width` and `height` are text-space px, so under a +/// scaled transform they are scaled by its linear part. +/// `type.edit`'s `box` param and the Type tool's live box-drag preview both use it, so the preview +/// shows exactly the layer the command would make. Non-finite numbers count as 0, as a JSON +/// `null` does in the command (a width or height of 0 becomes the 1 px minimum). The stored size +/// is clamped to `1.0..=f32::MAX` so a huge input can't become an infinite `f32`. +pub fn apply_box(t: &mut TextLayer, b: [f64; 4]) { + let [x, y, w, h] = b.map(|v| if v.is_finite() { v } else { 0.0 }); + let size = |v: f64| v.clamp(1.0, f64::from(f32::MAX)) as f32; + let lin = t.transform.m; + t.shape = TextShape::Box { x: 0.0, y: 0.0, width: size(w), height: size(h) }; + t.transform = Affine { m: [lin[0], lin[1], lin[2], lin[3], x, y] }; +} + /// Refuses to change a type layer under Lock All (its own or an enclosing group's), with /// Photoshop's message. Every command that edits a type layer's text calls it first (#3132, #4188). pub(crate) fn check_text_unlocked(doc: &Document, id: LayerId) -> Result<()> { @@ -989,6 +1005,7 @@ pub fn name_selection_commit(s: &mut Session) { const CHAR_PARAMS: &str = r##""font":str,"fontStyle":str,"weight":100..900,"italic":bool,"size":0.1..=1296 pt,"color":"#rrggbb"|[r,g,b,a],"tracking":-1000..=10000 (1/1000 em),"leading":pt|"auto","baselineShift":pt,"horizontalScale":%,"verticalScale":%,"underline":bool,"strikethrough":bool,"fauxBold":bool,"fauxItalic":bool,"kerning":1/1000em (manual, after each character)|"metrics"|"optical"|"off","caps":"normal|small|all","baseline":"normal|superscript|subscript","ligatures":bool,"discretionaryLigatures":bool,"features":{"ss01":1},"variations":{"wght":650},"language":str"##; +/// The Type commands: create and edit type layers, set styles, fill paths, and the queries about them. pub fn specs() -> Vec { vec![ CommandSpec { @@ -1113,9 +1130,11 @@ pub fn specs() -> Vec { } let lin = t.transform.m; if let Some(b) = p.get("box").and_then(Value::as_array).filter(|b| b.len() == 4) { - let v: Vec = b.iter().map(|x| x.as_f64().unwrap_or(0.0)).collect(); - t.shape = TextShape::Box { x: 0.0, y: 0.0, width: v[2].max(1.0) as f32, height: v[3].max(1.0) as f32 }; - t.transform = Affine { m: [lin[0], lin[1], lin[2], lin[3], v[0], v[1]] }; + let mut v = [0.0; 4]; + for (slot, x) in v.iter_mut().zip(b) { + *slot = x.as_f64().unwrap_or(0.0); + } + apply_box(t, v); } if let Some(pt) = p.get("point").and_then(Value::as_array).filter(|b| b.len() == 2) { t.shape = TextShape::Point; diff --git a/crates/engine/src/cmd/type_cmds/tests.rs b/crates/engine/src/cmd/type_cmds/tests.rs index 066e0f5a8a..1fa8bb89df 100644 --- a/crates/engine/src/cmd/type_cmds/tests.rs +++ b/crates/engine/src/cmd/type_cmds/tests.rs @@ -1012,3 +1012,43 @@ fn relative_steps_move_each_character_from_its_own_value() { s.execute("type.setStyle", json!({"layer": empty, "range": [0, 0], "leadingBy": -2})).unwrap(); assert_eq!(text_layer(&s, empty).char_runs()[0].style.leading_pt, Some(10.0)); } + +/// The `(width, height)` of a layer that is paragraph text in a box. +fn box_size(t: &TextLayer) -> (f32, f32) { + match t.shape { + TextShape::Box { width, height, .. } => (width, height), + ref s => panic!("not a box: {s:?}"), + } +} + +/// A huge `box` once became an infinite `f32` size; it stays finite now. +#[test] +fn a_huge_box_keeps_a_finite_size() { + let mut s = session(); + let id = s.execute("type.create", json!({"text": "Hello", "size": 24})).unwrap()["layer"].as_u64().unwrap(); + // Ok or a clean error are both fine; a panic is not. On success the box must have been applied. + let result = s.execute("type.edit", json!({"layer": id, "box": [0, 0, 1e300, 1e300]})); + if result.is_ok() { + assert!(matches!(text_layer(&s, id).shape, TextShape::Box { .. }), "a successful box edit makes a box"); + } + if let TextShape::Box { .. } = text_layer(&s, id).shape { + let (w, h) = box_size(&text_layer(&s, id)); + assert!(w.is_finite() && h.is_finite() && w >= 1.0 && h >= 1.0, "{w} x {h}"); + } +} + +/// `apply_box` keeps the stored size between 1 px and `f32::MAX`, and non-finite input becomes the minimum. +#[test] +fn apply_box_clamps_sizes_to_a_finite_minimum_and_maximum() { + let mut s = session(); + let id = s.execute("type.create", json!({"text": "Hello", "size": 24})).unwrap()["layer"].as_u64().unwrap(); + let mut t = text_layer(&s, id); + apply_box(&mut t, [f64::NAN, f64::INFINITY, f64::NAN, f64::NEG_INFINITY]); + assert_eq!(box_size(&t), (1.0, 1.0), "non-finite counts as 0, then the 1 px minimum"); + assert_eq!((t.transform.m[4], t.transform.m[5]), (0.0, 0.0)); + apply_box(&mut t, [5.0, 6.0, 1e300, f64::MAX]); + assert_eq!(box_size(&t), (f32::MAX, f32::MAX)); + assert_eq!((t.transform.m[4], t.transform.m[5]), (5.0, 6.0)); + apply_box(&mut t, [0.0, 0.0, -20.0, 0.25]); + assert_eq!(box_size(&t), (1.0, 1.0)); +} diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 09f9d70c78..11fa0dc23e 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -144,6 +144,7 @@ impl PhotocraftApp { type_clicks: None, type_transform_preview: None, type_path_damage: None, + type_box_preview: None, type_font_preview: None, color_picker_preview: None, guide_drag: None, diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index 29ca67ac15..f007102018 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -66,6 +66,9 @@ impl PhotocraftApp { self.drain_control(ctx); native_menu::run(self, ctx); view_cmds::leave_full_screen_without_document(self, ctx); + // A box drag that ended without a release reaching the Type tool keeps its box, before + // the commit below can end the edit. + crate::type_box_preview::settle(self); // Switching tools commits, also between the Type and Type Mask tools. if self.ui.text_edit.as_ref().is_some_and(|ed| !self.ui.tool.is_type() || ed.mask != self.ui.tool.is_type_mask()) { type_tool::commit(self); diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 5502aac741..a954865efc 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -1589,6 +1589,7 @@ struct CanvasOverlayInput<'a> { zoom_out: &'a dyn Fn(bool) -> bool, } +/// Paints what sits over the canvas image: the grid, guides, selection ants, and the active tool's previews and handles. fn paint_primary_overlays(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &mut View, input: &CanvasOverlayInput<'_>, ants: Vec) { let ctx = input.ctx; let painter = input.painter; @@ -1650,6 +1651,7 @@ fn paint_primary_overlays(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &mut crate::transform_tool::draw_overlay(app, painter, xf); crate::cam_transform::draw(app, painter, xf); crate::cam_preview::wake(app, painter.ctx()); + crate::type_box_preview::wake(app, painter.ctx()); crate::distort_ui::draw_overlay(app, painter, xf); crate::perspective_crop_ui::draw_overlay(app, painter, xf); crate::retouch_ui::draw_source_marker(app, painter, xf); diff --git a/crates/ui-egui/src/canvas/display.rs b/crates/ui-egui/src/canvas/display.rs index 63cd812590..a0a4a75f9a 100644 --- a/crates/ui-egui/src/canvas/display.rs +++ b/crates/ui-egui/src/canvas/display.rs @@ -134,6 +134,10 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> (std::sync::Ar if let Some(shown) = crate::type_font_preview::display_doc(app, idx) { return shown; } + // A paragraph-box handle drag, rendered in the background (#7032). + if let Some(shown) = crate::type_box_preview::display_doc(app, idx) { + return shown; + } // Vanishing Point's strokes over the document as it is now (the Layers panel may have // changed it since the options bar synced them this frame). crate::vp_ui::sync_preview(app); diff --git a/crates/ui-egui/src/canvas/textures.rs b/crates/ui-egui/src/canvas/textures.rs index 7cc11a4a41..d022a0d976 100644 --- a/crates/ui-egui/src/canvas/textures.rs +++ b/crates/ui-egui/src/canvas/textures.rs @@ -421,6 +421,13 @@ pub(crate) fn damage_since( { return Some(if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }); } + // Between a dragged paragraph box's renders (and the document): the layer's old/new ink bounds. + if seen.0 == now.0 + && let Some(st) = app.session.documents().get(idx) + && let Some(r) = crate::type_box_preview::damage(app, st.doc.id, now.0, seen.1 ^ display_key, now.1) + { + return Some(if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }); + } let l = live_stroke(app, idx).filter(|l| seen.0 == now.0 && l.display_key() == now.1)?; let r = l.since(seen.1 ^ display_key)?; Some(if r.is_empty() { r } else { r.inflate(effect_reach(&l.stroke.doc().layers)) }) diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs index 639d29f463..e53e0938c1 100644 --- a/crates/ui-egui/src/canvas/tool_input.rs +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -677,6 +677,8 @@ pub(super) fn tool_event_with_view(app: &mut PhotocraftApp, ev: ToolEvent, mods: feed_live_stroke(app); } ToolEvent::Up { x, y } => { + // Before `resize` is cleared: a background box drag applies its box now. + crate::type_box_preview::flush(app); if tool.is_type() && let Some(e) = app.ui.text_edit.as_mut() { diff --git a/crates/ui-egui/src/control/dispatch.rs b/crates/ui-egui/src/control/dispatch.rs index 8d37a986fd..f8189aac85 100644 --- a/crates/ui-egui/src/control/dispatch.rs +++ b/crates/ui-egui/src/control/dispatch.rs @@ -86,6 +86,7 @@ fn mode_menu_choose(app: &mut PhotocraftApp, ctx: &egui::Context, label: &str) - ok(json!({"choice": label, "brushPicker": app.ui.brush_picker.is_some()})) } +/// `engine.execute` and `ui.menu.invoke`: run command `command` (or `id`) with `params`, as the menu would or, for `engine.execute`, directly. fn execute_command(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest) -> Outcome { let p = &req.params; let Some(id) = str_param(p, "command").or(str_param(p, "id")) else { return err("missing `command`") }; @@ -117,6 +118,8 @@ fn execute_command(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRe // result). `ui.menu.invoke` behaves like a menu click, so it may open the dialog. if req.method == "engine.execute" && photocraft_engine::commands::find(id).is_some() { return run_waiting(app, wait, |app| { + // These bypass `menus::invoke`: a Type box drag in progress applies its box first. + crate::type_box_preview::end_drag(app); let previous = std::mem::replace(&mut app.programmatic_playback, true); let result = app.run(id, params); app.programmatic_playback = previous; @@ -1540,6 +1543,7 @@ fn forward_pointer_event(app: &mut PhotocraftApp, g: &PointerGesture, s: Pointer // ---- the application: open, save, quit ---- +/// The application-level control methods: open, save and quit. fn app_methods(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest) -> Option { let p = &req.params; Some(match req.method.as_str() { @@ -1554,7 +1558,11 @@ fn app_methods(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlReques "app.save" => match crate::FormatChoices::from_params(p, "app.save") { Ok(choices) => { let path = str_param(p, "path").map(str::to_string); - run_waiting(app, wait_param(p), move |app| app.save_automation_with(path, choices)) + run_waiting(app, wait_param(p), move |app| { + // Saving bypasses `menus::invoke`: a Type box drag in progress applies its box first. + crate::type_box_preview::end_drag(app); + app.save_automation_with(path, choices) + }) } Err(e) => err(e), }, diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 0f8c925bbf..52768f3680 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -327,6 +327,7 @@ use tools::tool_reset; pub use tools::transform_fx; pub use tools::transform_tex; pub use tools::transform_tool; +use tools::type_box_preview; use tools::type_font_preview; use tools::type_path_placement; pub use tools::type_tool; @@ -673,6 +674,8 @@ pub struct PhotocraftApp { pub(crate) type_clicks: Option<(u64, u32)>, pub(crate) type_transform_preview: Option, pub(crate) type_path_damage: Option, + /// A paragraph-box handle drag rendered in the background (`type_box_preview`). + pub(crate) type_box_preview: Option, /// A font hovered in the Type tool's font picker, shown live (`type_font_preview`). pub(crate) type_font_preview: Option, /// The colour under a text or gradient stop Color Picker, shown live before OK (`color_picker_preview`). diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index cbe858eba6..7afbb6c2f2 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -39,6 +39,29 @@ pub(super) fn workspace_name(id: &str) -> Option<&'static str> { }) } +/// Whether command `id` leaves a Type box-handle drag going: only the ones that change what is +/// on screen and nothing else. `UI_COMMANDS` is every command the shell carries out itself, so it +/// can't tell them apart: it holds `edit.freeTransform` and `file.save`, which must see the dragged +/// box, `view.clearSelectedGuides`, which edits the document, and `window.nextDocument`, which +/// switches to another one mid-drag. So the commands that cannot touch either are listed one by +/// one, and the panel ones (open, close, arrange: `window.panel.*`, `window.toggle.*` and +/// `type.panels.*`, the Character panel's Cmd+T among them) by prefix, since they only lay out +/// windows. Everything else applies the box first, which is harmless when no drag is going. +fn keeps_box_drag(id: &str) -> bool { + const VIEW: &[&str] = &[ + "view.zoomIn", + "view.zoomOut", + "view.fitOnScreen", + "view.actualPixels", + "view.rotateView", + "view.resetView", + "view.screenMode.cycle", + "view.darkenInterface", + "view.lightenInterface", + ]; + VIEW.contains(&id) || ["window.panel.", "window.toggle.", "type.panels."].iter().any(|prefix| id.starts_with(prefix)) +} + /// Run a command id from any source (menu, shortcut, palette, automation). pub fn invoke(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: Value) -> Result { if app.automation_input @@ -46,6 +69,11 @@ pub fn invoke(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: Va { authorize(id, ¶ms)?; } + // A held Type box handle applies its box before any command that may read or edit the + // document runs; a command that edits it first would make the drag's box stale and lose it. + if !keeps_box_drag(id) { + crate::type_box_preview::end_drag(app); + } if crate::discard_ui::intercept(app, id, ¶ms) { return Ok(Value::Null); } diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 50af2995f0..f578d9c23d 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -51,6 +51,7 @@ pub(super) mod tool_reset; pub mod transform_fx; pub mod transform_tex; pub mod transform_tool; +pub(super) mod type_box_preview; pub(super) mod type_font_preview; pub mod type_path_placement; pub mod type_tool; diff --git a/crates/ui-egui/src/tools/type_box_preview.rs b/crates/ui-egui/src/tools/type_box_preview.rs new file mode 100644 index 0000000000..31adca7605 --- /dev/null +++ b/crates/ui-egui/src/tools/type_box_preview.rs @@ -0,0 +1,907 @@ +//! Paragraph-box handle drags of the Type tool, rendered off the UI thread (#7032). +//! +//! Applying a dragged box with `type.edit` lays the text out and rasterizes it, which on a big +//! paragraph takes tens of milliseconds, and the canvas sends one pointer move per frame. So while +//! a handle is dragged the document is left alone: the moves only say which box is wanted, a +//! worker lays out and rasterizes a copy of the layer in that box, and the canvas shows the +//! document with that copy. One render runs at a time and the newest box wins: a result for an +//! older box is still shown (it is newer than what is on screen), then the next render starts for +//! the box wanted now. Releasing (or anything else that ends the drag) runs the same `type.edit` +//! a synchronous drag would have run for its last move, so the finished document and its history +//! are exactly what they were before this preview existed. +//! +//! A command that starts while a handle is held (Free Transform, a menu item, a shortcut) ends the +//! drag first ([`end_drag`]), so it sees the box applied. If the document changed some other way +//! during the drag, the preview is dropped and the gesture ends: the press-time box is never +//! applied over that change. +//! +//! Only with background jobs on a native build, and not for Type Mask edits (their overlay is +//! drawn from the document's layout); otherwise every move runs `type.edit` as before. + +use std::collections::VecDeque; +use std::panic::AssertUnwindSafe; +use std::sync::mpsc::{Receiver, TryRecvError}; +use std::sync::{Arc, PoisonError}; + +use photocraft_color::PixelFormat; +use photocraft_doc::{DocId, Document, LayerContent, LayerId, TextLayer}; +use photocraft_geom::{Affine, Rect}; +use photocraft_raster::Surface; +use serde_json::json; + +use crate::PhotocraftApp; + +/// Preview keys: `BASE + n`, one per result shown (see `canvas::display_doc`). +const BASE: u64 = 1 << 46; +/// How often a running render is polled for its result when its thread can't wake the canvas. +const POLL_MS: u64 = 30; +/// Results whose damage areas are remembered for canvas invalidation. +const MAX_AREAS: usize = 128; + +/// Test-only controls of the worker. Empty (and free) outside tests. +#[derive(Clone, Default)] +struct Hooks { + /// While `Some` and closed, a render waits before computing, so a test can hold one in flight. + #[cfg(test)] + gate: Option, std::sync::Condvar)>>, +} + +impl Hooks { + /// In tests, holds a render back until the gate opens; does nothing otherwise. + #[allow(clippy::unused_self)] + fn wait(&self) { + #[cfg(test)] + { + if let Some(gate) = &self.gate { + let mut open = gate.0.lock().unwrap_or_else(PoisonError::into_inner); + while !*open { + open = gate.1.wait(open).unwrap_or_else(PoisonError::into_inner); + } + } + } + } +} + +/// The render that is computing: the box it draws and where its surface arrives. +struct Running { + boxed: [f64; 4], + rx: Receiver>, + /// Test-only notification sent after the worker has sent its result. + #[cfg(test)] + finished: Receiver<()>, +} + +/// The newest result: the box it shows, the document with it, and its canvas key. +struct Shown { + boxed: [f64; 4], + doc: Arc, + key: u64, +} + +pub(crate) struct TypeBoxPreview { + /// The document, its revision and the layer at the press: if any of them changes, the drag's + /// box is not applied to something else. + document: DocId, + revision: u64, + layer: LayerId, + /// The edit session's coalescing key, for the `type.edit` that applies the box. + session: String, + /// The box (`[x, y, width, height]`, document px, as `type.edit`'s `box`) the pointer wants. + wanted: Option<[f64; 4]>, + /// A box whose render failed: not tried again (the release still renders it). + failed: Option<[f64; 4]>, + running: Option, + shown: Option, + serial: u64, + /// The layer's pixel bounds in the document (key 0), and in each result shown, for damage. + original_area: Rect, + areas: VecDeque<(u64, Rect)>, + /// To wake the canvas when a render finishes; set by [`wake`] on a canvas frame. + ctx: Option, + hooks: Hooks, + /// Test-only count of renders started. + #[cfg(test)] + spawned: usize, +} + +/// Whether handle drags render in the background here. +fn enabled(app: &PhotocraftApp) -> bool { + cfg!(not(target_arch = "wasm32")) && app.background_jobs +} + +/// A handle drag of `p`'s layer is in progress. +fn dragging(app: &PhotocraftApp, p: &TypeBoxPreview) -> bool { + app.ui.tool.is_type() && app.ui.text_edit.as_ref().is_some_and(|e| e.layer == p.layer.0 && e.resize.is_some()) +} + +/// The text layer `layer` of `doc`, if it is one. +fn text_of(doc: &Document, layer: LayerId) -> Option<&TextLayer> { + match &doc.layer(layer)?.content { + LayerContent::Text(t) => Some(t), + _ => None, + } +} + +/// A handle drag of text layer `layer` starts: remember the document as it is now. Does nothing +/// when drags apply the box as they go. +pub(crate) fn begin(app: &mut PhotocraftApp, layer: LayerId, session: String) { + // A drag left over from before (its release never came) still owes its box to the document. + flush(app); + // The mask overlay is drawn from the document's layout, which a background drag leaves stale. + if !enabled(app) || app.ui.text_edit.as_ref().is_some_and(|e| e.mask) { + return; + } + let Some(st) = app.session.active() else { return }; + if text_of(&st.doc, layer).is_none() { + return; + } + let original_area = st.doc.layer(layer).and_then(|l| l.surface()).map_or(Rect::EMPTY, |s| s.tile_bounds()); + app.type_box_preview = Some(TypeBoxPreview { + document: st.doc.id, + revision: st.revision, + layer, + session, + wanted: None, + failed: None, + running: None, + shown: None, + serial: 0, + original_area, + areas: VecDeque::new(), + ctx: None, + hooks: Hooks::default(), + #[cfg(test)] + spawned: 0, + }); +} + +/// End the handle-drag gesture of `layer`'s edit session, as a release does. +fn end_gesture(app: &mut PhotocraftApp, layer: LayerId) { + if let Some(e) = app.ui.text_edit.as_mut().filter(|e| e.layer == layer.0) { + e.resize = None; + e.dragging = false; + } +} + +/// The drag moved the box to `boxed`. Returns true when the move is dealt with: it is recorded for +/// the preview, or the preview was stale and the drag ended with it. Returns false when no +/// background drag is in progress for `layer` (or the box isn't a plain finite one): the caller +/// then applies it with `type.edit`. +pub(crate) fn want(app: &mut PhotocraftApp, layer: LayerId, boxed: [f64; 4]) -> bool { + let Some(p) = app.type_box_preview.as_ref() else { return false }; + let current = + p.layer == layer && app.session.documents().iter().any(|st| st.doc.id == p.document && st.revision == p.revision && text_of(&st.doc, layer).is_some()); + if !current { + // Something else changed the document or layer since the press. The preview would show a + // stale copy and `type.edit` from the press-time box would run over that change, so drop + // the preview and end the drag; the rest of the gesture does nothing. + app.type_box_preview = None; + end_gesture(app, layer); + return true; + } + if !boxed.iter().all(|v| v.is_finite()) { + // Not a box the preview can draw: the command decides what it means. + app.type_box_preview = None; + return false; + } + if let Some(p) = app.type_box_preview.as_mut() { + p.wanted = Some(boxed); + } + spawn_if_idle(app); + true +} + +/// The box the pointer wants while a background drag of `layer` is in progress, for the overlay. +/// The text layout, caret and overflow mark still come from the document, which has not changed. +pub(crate) fn wanted_box(app: &PhotocraftApp, layer: LayerId) -> Option<[f64; 4]> { + let p = app.type_box_preview.as_ref().filter(|p| p.layer == layer && dragging(app, p))?; + p.wanted +} + +/// Start a render of the wanted box if none runs and it isn't what is shown already. +fn spawn_if_idle(app: &mut PhotocraftApp) { + let Some(p) = app.type_box_preview.as_ref() else { return }; + let Some(boxed) = p.wanted else { return }; + if p.running.is_some() || p.failed == Some(boxed) || p.shown.as_ref().is_some_and(|s| s.boxed == boxed) { + return; + } + let Some(st) = app.session.documents().iter().find(|st| st.doc.id == p.document && st.revision == p.revision) else { return }; + let Some(t) = text_of(&st.doc, p.layer) else { return }; + let mut layer = t.clone(); + photocraft_engine::type_cmds::apply_box(&mut layer, boxed); + let (dpi, format) = (st.doc.resolution_dpi, st.doc.pixel_format()); + let (ctx, hooks) = (p.ctx.clone(), p.hooks.clone()); + let running = spawn(layer, dpi, format, boxed, ctx, hooks); + if let Some(p) = app.type_box_preview.as_mut() { + #[cfg(test)] + { + p.spawned += 1; + } + if running.is_none() { + p.failed = Some(boxed); + } + p.running = running; + } +} + +/// Lay out and rasterize `layer` like `TextEngine::render`, but holding the shared engine only +/// for the layout: the UI thread locks it every frame, and the rasterizing is most of the time. +fn render(layer: &TextLayer, dpi: f32, format: PixelFormat) -> Surface { + let layout = { + let mut engine = photocraft_text::shared().lock().unwrap_or_else(PoisonError::into_inner); + engine.layout(layer, dpi) + }; + let warp = photocraft_text::render::layout_deform(&layout, layer); + photocraft_text::render::rasterize_warped(&layout, &layer.transform, format, layer.antialias, warp.as_ref()).surface +} + +/// Starts a worker thread that renders `layer` and sends back its surface; `None` if no thread could be started. +fn spawn(layer: TextLayer, dpi: f32, format: PixelFormat, boxed: [f64; 4], ctx: Option, hooks: Hooks) -> Option { + let (tx, rx) = std::sync::mpsc::channel(); + #[cfg(test)] + let (finished_tx, finished) = std::sync::mpsc::channel(); + std::thread::Builder::new() + .name("type-box-preview".into()) + .spawn(move || { + hooks.wait(); + // A panic leaves no preview; the release still renders the box on the UI thread. + let surface = std::panic::catch_unwind(AssertUnwindSafe(|| render(&layer, dpi, format))).ok(); + // The receiver is gone when the drag ended meanwhile. + let _ = tx.send(surface); + #[cfg(test)] + let _ = finished_tx.send(()); + if let Some(ctx) = ctx { + ctx.request_repaint(); + } + }) + .ok()?; + Some(Running { + boxed, + rx, + #[cfg(test)] + finished, + }) +} + +/// Take a finished render, if there is one, and make its document the one shown. +fn poll(app: &mut PhotocraftApp) { + let Some(p) = app.type_box_preview.as_mut() else { return }; + let Some(r) = p.running.as_ref() else { return }; + let result = match r.rx.try_recv() { + Ok(surface) => surface, + Err(TryRecvError::Disconnected) => None, + Err(TryRecvError::Empty) => return, + }; + let Some(done) = p.running.take() else { return }; + match result { + Some(surface) => publish(app, done.boxed, surface), + None => p.failed = Some(done.boxed), + } +} + +/// Show the session document with its layer in `boxed`, drawn as `surface`. +fn publish(app: &mut PhotocraftApp, boxed: [f64; 4], surface: Surface) { + let Some(p) = app.type_box_preview.as_ref() else { return }; + let (document, revision, layer) = (p.document, p.revision, p.layer); + let Some(st) = app.session.documents().iter().find(|st| st.doc.id == document && st.revision == revision) else { return }; + let mut doc = (*st.doc).clone(); + let area = surface.tile_bounds(); + { + let Some(l) = doc.layer_mut(layer) else { return }; + let LayerContent::Text(t) = &mut l.content else { return }; + photocraft_engine::type_cmds::apply_box(t, boxed); + t.cache = Some(surface); + } + let doc = Arc::new(photocraft_engine::mode_cmds::display_document(&doc).unwrap_or(doc)); + let Some(p) = app.type_box_preview.as_mut() else { return }; + p.serial += 1; + let key = BASE + p.serial; + if p.areas.len() >= MAX_AREAS { + p.areas.pop_front(); + } + p.areas.push_back((key, area)); + p.shown = Some(Shown { boxed, doc, key }); +} + +/// The document to show for document `idx`: the session document with the dragged box applied, +/// once a render of one has arrived. `None` (and the preview dropped, if it is stale) otherwise. +pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc, u64)> { + let p = app.type_box_preview.as_ref()?; + let st = app.session.documents().get(idx)?; + if st.doc.id != p.document { + return None; + } + let stale = st.revision != p.revision || text_of(&st.doc, p.layer).is_none(); + let drag = dragging(app, p); + if stale { + // As in `want`: the document moved on, so the held handle no longer has a box to apply. + let layer = p.layer; + app.type_box_preview = None; + end_gesture(app, layer); + return None; + } + // A drag that ended some other way is settled by `settle`, not here: this runs while the + // canvas is being drawn. + if !drag { + return None; + } + poll(app); + spawn_if_idle(app); + let s = app.type_box_preview.as_ref()?.shown.as_ref()?; + Some((s.doc.clone(), s.key)) +} + +/// Keep frames coming while a render computes, and remember the context to wake from the worker. +pub(crate) fn wake(app: &mut PhotocraftApp, ctx: &egui::Context) { + let Some(p) = app.type_box_preview.as_mut() else { return }; + if p.ctx.is_none() { + p.ctx = Some(ctx.clone()); + } + if p.running.is_some() { + ctx.request_repaint_after(std::time::Duration::from_millis(POLL_MS)); + } +} + +/// The drag ends: apply the box it wanted with `type.edit`, exactly as a synchronous drag has by +/// now (one render, in the edit session's history step), and drop the preview. Nothing happens +/// without a preview, if it never got a box, or if the document, its revision or the layer is no +/// longer the one the drag started on. +pub(crate) fn flush(app: &mut PhotocraftApp) { + let Some(p) = app.type_box_preview.take() else { return }; + let Some(boxed) = p.wanted else { return }; + let Some(i) = app.session.documents().iter().position(|st| st.doc.id == p.document && st.revision == p.revision && text_of(&st.doc, p.layer).is_some()) + else { + return; + }; + // The drag's document may no longer be the active one (a tab switched mid-drag). + let current = app.session.active_index(); + if current != Some(i) { + app.session.set_active(i); + } + let _ = app.run("type.edit", json!({"layer": p.layer.0, "box": boxed, "coalesce": p.session})); + if let Some(c) = current.filter(|c| *c != i) { + app.session.set_active(c); + } +} + +/// A command other than a pure view one starts while a handle may be held: apply the box the drag +/// reached and end the gesture, so the command (Free Transform capturing the document, say) works +/// on the box, not the press-time one. Does nothing without a background drag. +pub(crate) fn end_drag(app: &mut PhotocraftApp) { + let Some(layer) = app.type_box_preview.as_ref().map(|p| p.layer) else { return }; + flush(app); + end_gesture(app, layer); +} + +/// The edit was cancelled: its session is undone, so the box the drag wanted is not applied. +pub(crate) fn discard(app: &mut PhotocraftApp) { + app.type_box_preview = None; +} + +/// Once a frame: a drag that ended without a release reaching the Type tool (focus lost, a Free +/// Transform started, the tool switched, the edit replaced) keeps the box it had reached, as it +/// did when every move was applied. If the document changed meanwhile, the box is dropped. +pub(crate) fn settle(app: &mut PhotocraftApp) { + if app.type_box_preview.as_ref().is_some_and(|p| !dragging(app, p)) { + flush(app); + } +} + +/// Where the layer's pixels were and are, between two keys the canvas showed (0: the document). +pub(crate) fn damage(app: &PhotocraftApp, doc: DocId, revision: u64, before: u64, after: u64) -> Option { + if before == after { + return None; + } + let p = app.type_box_preview.as_ref().filter(|p| p.document == doc && p.revision == revision)?; + let area = |key| if key == 0 { Some(p.original_area) } else { p.areas.iter().find(|(k, _)| *k == key).map(|(_, r)| *r) }; + Some(area(before)?.union(&area(after)?)) +} + +/// The preview's box as the overlay draws it: the layer's transform moved to the box's origin. +pub(crate) fn overlay_affine(aff: Affine, boxed: [f64; 4]) -> Affine { + Affine { m: [aff.m[0], aff.m[1], aff.m[2], aff.m[3], boxed[0], boxed[1]] } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::canvas::{ToolEvent, tool_event}; + use crate::state::Tool; + use egui::Modifiers; + use photocraft_doc::text::TextShape; + + /// An app with one paragraph text layer in a box, open for editing. + fn app_with_box() -> (PhotocraftApp, LayerId) { + app_with_box_and(crate::Services::default()) + } + + /// [`app_with_box`] with the given shell services. + fn app_with_box_and(services: crate::Services) -> (PhotocraftApp, LayerId) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services); + app.run("file.new", json!({"width": 1600, "height": 900})).unwrap(); + app.sync_views(); + app.ui.extras.rulers = false; + app.ui.extras.snap = false; + app.ui.tool = Tool::Type; + let id = LayerId( + app.run("type.create", json!({"text": "Hello é世界\nSecond line", "size": 32, "x": 300, "y": 420, "box": [300, 300, 240, 180]})).unwrap()["layer"] + .as_u64() + .unwrap(), + ); + app.run("type.edit", json!({"layer": id.0, "transform": [1.0, 0.0, 0.0, 1.0, 300.0, 300.0]})).unwrap(); + crate::type_tool::edit_active(&mut app).unwrap(); + (app, id) + } + + // The bottom edge's handle sits at the midpoint of the box's lower edge. + /// Press the bottom-edge handle of the box. + fn press(app: &mut PhotocraftApp) { + tool_event(app, ToolEvent::Down { x: 420.0, y: 480.0, pressure: 1.0 }, Modifiers::NONE); + } + /// Move the held bottom-edge handle to document `y`. + fn to(app: &mut PhotocraftApp, y: f64) { + tool_event(app, ToolEvent::Move { x: 420.0, y, pressure: 1.0 }, Modifiers::NONE); + } + /// Release the bottom-edge handle at document `y`. + fn release(app: &mut PhotocraftApp, y: f64) { + tool_event(app, ToolEvent::Up { x: 420.0, y }, Modifiers::NONE); + } + // The right edge's handle sits at the midpoint of the box's right edge. + /// Press the right-edge handle of the box. + fn press_right(app: &mut PhotocraftApp) { + tool_event(app, ToolEvent::Down { x: 540.0, y: 390.0, pressure: 1.0 }, Modifiers::NONE); + } + /// Move the held right-edge handle to document `x`. + fn to_x(app: &mut PhotocraftApp, x: f64) { + tool_event(app, ToolEvent::Move { x, y: 390.0, pressure: 1.0 }, Modifiers::NONE); + } + /// Release the right-edge handle at document `x`. + fn release_x(app: &mut PhotocraftApp, x: f64) { + tool_event(app, ToolEvent::Up { x, y: 390.0 }, Modifiers::NONE); + } + /// The active document's revision. + fn revision(app: &PhotocraftApp) -> u64 { + app.session.active().unwrap().revision + } + /// The text layer `id` of `doc`. + fn layer_of(doc: &Document, id: LayerId) -> TextLayer { + text_of(doc, id).unwrap().clone() + } + /// The text layer `id` of the active document. + fn session_layer(app: &PhotocraftApp, id: LayerId) -> TextLayer { + layer_of(&app.session.active().unwrap().doc, id) + } + /// The height of a box-shaped text layer. + fn height(t: &TextLayer) -> f32 { + match t.shape { + TextShape::Box { height, .. } => height, + ref s => panic!("not a box: {s:?}"), + } + } + /// The width of a box-shaped text layer. + fn width(t: &TextLayer) -> f32 { + match t.shape { + TextShape::Box { width, .. } => width, + ref s => panic!("not a box: {s:?}"), + } + } + /// Whether two layers' cached pixels are identical. + fn same_pixels(a: &TextLayer, b: &TextLayer) -> bool { + let (a, b) = (a.cache.as_ref().unwrap(), b.cache.as_ref().unwrap()); + let r = a.content_bounds(); + r == b.content_bounds() && (r.y0..r.y1).all(|y| (r.x0..r.x1).all(|x| a.rgba(x, y) == b.rgba(x, y))) + } + /// Turn background jobs on, as the desktop app does. + fn background(app: &mut PhotocraftApp) { + app.background_jobs = true; + } + /// Let held renders run. + fn open_gate(gate: &Arc<(std::sync::Mutex, std::sync::Condvar)>) { + *gate.0.lock().unwrap() = true; + gate.1.notify_all(); + } + /// Hold every render back until the returned gate opens. + fn hold(app: &mut PhotocraftApp) -> Arc<(std::sync::Mutex, std::sync::Condvar)> { + let gate = Arc::new((std::sync::Mutex::new(false), std::sync::Condvar::new())); + app.type_box_preview.as_mut().unwrap().hooks.gate = Some(gate.clone()); + gate + } + /// Wait for each render in turn (their threads signal when done), polling after each, until + /// none runs: the document shown then is for the newest box. + fn wait_idle(app: &mut PhotocraftApp) -> Option<(Arc, u64)> { + let mut out = None; + for _ in 0..16 { + if let Some(r) = app.type_box_preview.as_ref().and_then(|p| p.running.as_ref()) { + let _ = r.finished.recv_timeout(std::time::Duration::from_secs(300)); + } + out = display_doc(app, 0); + if app.type_box_preview.as_ref().is_none_or(|p| p.running.is_none()) { + break; + } + } + out + } + + /// A background drag changes nothing until release, then equals the synchronous drag. + #[test] + fn an_async_drag_leaves_the_document_alone_until_release_and_ends_as_the_sync_drag() { + let (mut sync, id) = app_with_box(); + press(&mut sync); + to(&mut sync, 520.0); + to(&mut sync, 560.0); + release(&mut sync, 560.0); + let want = session_layer(&sync, id); + assert_eq!(height(&want), 260.0); + + let (mut app, id) = app_with_box(); + background(&mut app); + let (rev, steps) = (revision(&app), app.session.active().unwrap().history.past_len()); + press(&mut app); + to(&mut app, 520.0); + to(&mut app, 560.0); + assert_eq!(revision(&app), rev, "moves leave the document alone"); + assert_eq!(height(&session_layer(&app, id)), 180.0); + let (shown, key) = wait_idle(&mut app).expect("a preview once the render arrived"); + assert!(key > BASE); + let preview = layer_of(&shown, id); + assert_eq!(height(&preview), 260.0, "the newest box"); + assert!(preview.cache.is_some()); + assert_eq!(revision(&app), rev, "showing it changes nothing"); + + release(&mut app, 560.0); + assert_eq!(revision(&app), rev + 1, "release is one edit"); + assert_eq!(app.session.active().unwrap().history.past_len(), steps + 1); + assert!(app.type_box_preview.is_none()); + let got = session_layer(&app, id); + assert_eq!(got.shape, want.shape); + assert_eq!(got.transform.m, want.transform.m); + assert!(same_pixels(&got, &want), "the finished layer is the sync drag's"); + assert!(same_pixels(&got, &preview), "and what the preview showed"); + } + + /// Moves while a render runs start no second one, and the next render draws the newest box. + #[test] + fn the_newest_box_wins_with_one_render_at_a_time() { + let (mut app, id) = app_with_box(); + background(&mut app); + press(&mut app); + let gate = hold(&mut app); + to(&mut app, 500.0); + to(&mut app, 520.0); + to(&mut app, 560.0); + assert_eq!(app.type_box_preview.as_ref().unwrap().spawned, 1, "moves while a render runs start no other"); + open_gate(&gate); + let (shown, _) = wait_idle(&mut app).expect("a preview"); + assert_eq!(app.type_box_preview.as_ref().unwrap().spawned, 2, "the first render, then one for the newest box"); + assert_eq!(height(&layer_of(&shown, id)), 260.0); + release(&mut app, 560.0); + } + + /// Without background jobs every move runs `type.edit` at once. + #[test] + fn a_sync_drag_applies_every_move_at_once() { + let (mut app, id) = app_with_box(); + let rev = revision(&app); + press(&mut app); + assert!(app.type_box_preview.is_none()); + to(&mut app, 520.0); + assert_eq!(revision(&app), rev + 1); + to(&mut app, 560.0); + assert_eq!(revision(&app), rev + 2); + assert_eq!(height(&session_layer(&app, id)), 260.0); + release(&mut app, 560.0); + } + + /// A background drag is one history step. + #[test] + fn one_undo_restores_the_box_after_an_async_drag() { + let (mut app, id) = app_with_box(); + background(&mut app); + let steps = app.session.active().unwrap().history.past_len(); + press(&mut app); + to(&mut app, 560.0); + wait_idle(&mut app); + release(&mut app, 560.0); + assert_eq!(height(&session_layer(&app, id)), 260.0); + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(height(&session_layer(&app, id)), 180.0); + assert_eq!(app.session.active().unwrap().history.past_len(), steps); + } + + /// Commit keeps the box the drag reached; cancel undoes the session. + #[test] + fn committing_mid_drag_keeps_the_dragged_box_and_cancelling_drops_it() { + let (mut app, id) = app_with_box(); + background(&mut app); + press(&mut app); + to(&mut app, 560.0); + crate::type_tool::commit(&mut app); + assert!(app.type_box_preview.is_none() && app.ui.text_edit.is_none()); + assert_eq!(height(&session_layer(&app, id)), 260.0, "the box the drag reached"); + + let (mut app, id) = app_with_box(); + background(&mut app); + press(&mut app); + to(&mut app, 560.0); + crate::type_tool::cancel(&mut app); + assert!(app.type_box_preview.is_none() && app.ui.text_edit.is_none()); + assert_eq!(height(&session_layer(&app, id)), 180.0, "cancelling leaves the box"); + } + + /// `settle` applies the box of a drag that ended without a release, unless the document changed. + #[test] + fn a_drag_that_ends_without_a_release_is_applied_by_the_frame_step() { + let (mut app, id) = app_with_box(); + background(&mut app); + press(&mut app); + to(&mut app, 560.0); + settle(&mut app); + assert!(app.type_box_preview.is_some(), "still dragging"); + assert_eq!(height(&session_layer(&app, id)), 180.0); + // As `cancel_drag` does when the window loses focus. + app.ui.text_edit.as_mut().unwrap().resize = None; + assert!(display_doc(&mut app, 0).is_none()); + settle(&mut app); + assert!(app.type_box_preview.is_none()); + assert_eq!(height(&session_layer(&app, id)), 260.0); + + // Something else edited the document meanwhile: the box isn't applied over it. + let (mut app, id) = app_with_box(); + background(&mut app); + press(&mut app); + to(&mut app, 560.0); + app.run("layer.setProps", json!({"layer": id.0, "opacity": 0.5})).unwrap(); + app.ui.text_edit.as_mut().unwrap().resize = None; + settle(&mut app); + assert!(app.type_box_preview.is_none()); + assert_eq!(height(&session_layer(&app, id)), 180.0); + } + + /// A layer deleted mid-drag clears the preview and nothing panics. + #[test] + fn deleting_the_layer_mid_drag_clears_the_preview_without_a_panic() { + let (mut app, id) = app_with_box(); + background(&mut app); + press(&mut app); + to(&mut app, 520.0); + wait_idle(&mut app); + app.session.execute("layer.delete", json!({"layer": id.0})).unwrap(); + assert!(display_doc(&mut app, 0).is_none()); + assert!(app.type_box_preview.is_none()); + to(&mut app, 560.0); + release(&mut app, 560.0); + assert!(app.type_box_preview.is_none()); + } + + /// A width drag rewraps the text in the preview and ends as the synchronous drag does. + #[test] + fn a_width_drag_rewraps_the_preview_and_ends_as_the_sync_drag() { + let (mut sync, id) = app_with_box(); + press_right(&mut sync); + to_x(&mut sync, 480.0); + to_x(&mut sync, 420.0); + release_x(&mut sync, 420.0); + let want = session_layer(&sync, id); + assert_eq!(width(&want), 120.0); + + let (mut app, id) = app_with_box(); + background(&mut app); + let original = session_layer(&app, id); + let rev = revision(&app); + press_right(&mut app); + assert!(app.type_box_preview.is_some(), "a background drag started"); + to_x(&mut app, 480.0); + to_x(&mut app, 420.0); + assert_eq!(revision(&app), rev, "moves leave the document alone"); + let (shown, _) = wait_idle(&mut app).expect("a preview once the render arrived"); + let preview = layer_of(&shown, id); + assert_eq!(width(&preview), 120.0, "the newest box"); + assert!(!same_pixels(&preview, &original), "the narrower box wraps the text differently"); + + release_x(&mut app, 420.0); + let got = session_layer(&app, id); + assert_eq!(got.shape, want.shape); + assert_eq!(got.transform.m, want.transform.m); + assert!(same_pixels(&got, &want), "the finished layer is the sync drag's"); + } + + /// Commands apply the box and end the drag first; zoom commands leave it going. + #[test] + fn a_command_mid_drag_applies_the_box_and_ends_the_drag_but_a_view_command_does_not() { + let (mut app, id) = app_with_box(); + background(&mut app); + let ctx = egui::Context::default(); + let steps = app.session.active().unwrap().history.past_len(); + press(&mut app); + to(&mut app, 560.0); + assert_eq!(height(&session_layer(&app, id)), 180.0, "not applied yet"); + + // A pure view command leaves the drag going. + crate::menus::invoke(&mut app, &ctx, "view.zoomIn", json!({})).unwrap(); + assert!(app.type_box_preview.is_some()); + assert!(app.ui.text_edit.as_ref().unwrap().resize.is_some()); + assert_eq!(height(&session_layer(&app, id)), 180.0); + + // Any other command sees the box applied, in the drag's one history step. + crate::menus::invoke(&mut app, &ctx, "layer.setProps", json!({"layer": id.0, "opacity": 0.5})).unwrap(); + let t = session_layer(&app, id); + assert_eq!(height(&t), 260.0, "the dragged box was applied before the command ran"); + assert_eq!(app.session.active().unwrap().history.past_len(), steps + 2, "the drag's step, then the command"); + assert!(app.type_box_preview.is_none()); + let e = app.ui.text_edit.as_ref().unwrap(); + assert!(e.resize.is_none() && !e.dragging); + + // Without a background drag nothing changes. + let (mut sync, _) = app_with_box(); + press(&mut sync); + let rev = revision(&sync); + end_drag(&mut sync); + assert_eq!(revision(&sync), rev); + assert!(sync.ui.text_edit.as_ref().unwrap().resize.is_some(), "a sync drag is left going"); + } + + /// Opening or closing a panel (Cmd+T toggles the Character panel while typing) only lays out + /// windows, so a held handle keeps dragging. + #[test] + fn toggling_a_panel_mid_drag_leaves_the_drag_going() { + let (mut app, id) = app_with_box(); + background(&mut app); + let ctx = egui::Context::default(); + press(&mut app); + to(&mut app, 520.0); + for command in ["type.panels.character", "window.toggle.layers", "window.panel.close"] { + let _ = crate::menus::invoke(&mut app, &ctx, command, json!({})); + assert!(app.type_box_preview.is_some(), "{command} ended the drag"); + assert!(app.ui.text_edit.as_ref().unwrap().resize.is_some(), "{command} ended the drag"); + } + to(&mut app, 560.0); + release(&mut app, 560.0); + assert_eq!(height(&session_layer(&app, id)), 260.0, "the drag carried on to its last move"); + } + + /// The control channel's `engine.execute` runs engine commands without `menus::invoke`, and + /// `app.save` writes the document directly: both must see the dragged box too. + #[test] + fn control_commands_mid_drag_see_the_dragged_box() { + use crate::control::{ControlRequest, Outcome, handle}; + use std::cell::RefCell; + use std::rc::Rc; + let call = |app: &mut PhotocraftApp, method: &str, params: serde_json::Value| { + let (req, _rx) = ControlRequest::new(method, params); + match handle(app, &egui::Context::default(), &req) { + Outcome::Done(v) => v, + _ => panic!("{method}: expected an immediate reply"), + } + }; + + // engine.execute of a command that edits the document. + let (mut app, id) = app_with_box(); + background(&mut app); + press(&mut app); + to(&mut app, 560.0); + assert_eq!(height(&session_layer(&app, id)), 180.0, "not applied yet"); + let r = call(&mut app, "engine.execute", json!({"command": "layer.setProps", "params": {"layer": id.0, "opacity": 0.5}})); + assert_eq!(r["ok"], true, "{r}"); + assert_eq!(height(&session_layer(&app, id)), 260.0, "applied before the command ran, not lost as stale"); + assert!(app.type_box_preview.is_none()); + + // app.save writes the document as it is, which has to hold the dragged box. + let saved: Rc>> = Rc::default(); + let s = saved.clone(); + let services = crate::Services { + export: Some(Box::new(move |d: &Document, _p: &str, _s: &crate::ExportSettings| { + *s.borrow_mut() = Some(d.clone()); + Ok((b"out".to_vec(), Vec::new())) + })), + automation_write: Some(Box::new(|_path: &str, _b: &[u8]| Ok(()))), + ..Default::default() + }; + let (mut app, id) = app_with_box_and(services); + background(&mut app); + press(&mut app); + to(&mut app, 560.0); + let r = call(&mut app, "app.save", json!({"path": "a.png"})); + assert_eq!(r["ok"], true, "{r}"); + let doc = saved.borrow(); + let doc = doc.as_ref().expect("the document was exported"); + assert_eq!(height(text_of(doc, id).unwrap()), 260.0, "the saved layer has the dragged box"); + assert!(app.type_box_preview.is_none()); + } + + /// `window.nextDocument` switches documents: it must not leave a drag going, or the new + /// canvas would move the old document's box with its own view's coordinates. + #[test] + fn switching_documents_mid_drag_ends_the_drag() { + let (mut app, id) = app_with_box(); + background(&mut app); + let ctx = egui::Context::default(); + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + app.session.set_active(0); + press(&mut app); + to(&mut app, 560.0); + crate::menus::invoke(&mut app, &ctx, "window.nextDocument", json!({})).unwrap(); + assert!(app.type_box_preview.is_none(), "the drag ended"); + assert!(app.ui.text_edit.as_ref().is_none_or(|e| e.resize.is_none() && !e.dragging)); + let first = &app.session.documents()[0].doc; + assert_eq!(height(text_of(first, id).unwrap()), 260.0, "the first document kept the dragged box"); + } + + /// `view.clearSelectedGuides` is a view command that edits the document. It must not leave + /// the drag going: its edit would make the preview stale and the dragged box would be lost. + #[test] + fn a_view_command_that_edits_the_document_ends_the_drag_first() { + let (mut app, id) = app_with_box(); + background(&mut app); + let ctx = egui::Context::default(); + press(&mut app); + to(&mut app, 560.0); + assert_eq!(height(&session_layer(&app, id)), 180.0, "not applied yet"); + let _ = crate::menus::invoke(&mut app, &ctx, "view.clearSelectedGuides", json!({})); + assert!(app.type_box_preview.is_none(), "the drag ended"); + assert_eq!(height(&session_layer(&app, id)), 260.0, "the box was applied, not lost"); + } + + /// `edit.freeTransform` is a shell command (in `UI_COMMANDS`), yet it captures the document, + /// so it must see the dragged box like any other command does: its frame is the resized + /// layer's, not the one from before the drag. + #[test] + fn free_transform_mid_drag_sees_the_dragged_box() { + let (mut app, id) = app_with_box(); + background(&mut app); + let ctx = egui::Context::default(); + let frame = |app: &PhotocraftApp| { + let st = app.session.active().unwrap(); + let b = photocraft_engine::transform_cmds::transform_frame(&st.doc, st.doc.layer(id).unwrap()); + [b.x0 as f64, b.y0 as f64, b.x1 as f64, b.y1 as f64] + }; + let before = frame(&app); + // A narrower box wraps the text differently, so the layer's frame changes with it. + press_right(&mut app); + to_x(&mut app, 420.0); + assert_eq!(width(&session_layer(&app, id)), 240.0, "not applied yet"); + crate::menus::invoke(&mut app, &ctx, "edit.freeTransform", json!({})).expect("Free Transform opens"); + assert_eq!(width(&session_layer(&app, id)), 120.0, "applied before Free Transform captured the document"); + assert!(app.type_box_preview.is_none()); + let after = frame(&app); + assert_ne!(after, before, "the resized text has another frame"); + let t = app.ui.transform.as_ref().expect("a Free Transform session"); + assert_eq!(t.rect, after, "its frame is the resized layer's"); + } + + /// If the document changed during the drag, the press-time box is never applied over it. + #[test] + fn a_stale_preview_ends_the_drag_instead_of_applying_the_press_time_box() { + let (mut app, id) = app_with_box(); + background(&mut app); + press(&mut app); + to(&mut app, 520.0); + app.run("layer.setProps", json!({"layer": id.0, "opacity": 0.5})).unwrap(); + let (rev, steps) = (revision(&app), app.session.active().unwrap().history.past_len()); + to(&mut app, 560.0); + assert_eq!(revision(&app), rev, "the move applied nothing over the other edit"); + assert_eq!(app.session.active().unwrap().history.past_len(), steps); + assert_eq!(height(&session_layer(&app, id)), 180.0); + assert!(app.type_box_preview.is_none()); + let e = app.ui.text_edit.as_ref().unwrap(); + assert!(e.resize.is_none() && !e.dragging, "the gesture ended"); + // The rest of the gesture does nothing either. + to(&mut app, 600.0); + release(&mut app, 600.0); + assert_eq!(revision(&app), rev); + } + + /// Type Mask edits drag synchronously, so their overlay matches the box. + #[test] + fn a_type_mask_edit_keeps_the_synchronous_drag() { + let (mut app, id) = app_with_box(); + background(&mut app); + app.ui.text_edit.as_mut().unwrap().mask = true; + let rev = revision(&app); + press(&mut app); + assert!(app.type_box_preview.is_none(), "no background drag for a mask edit"); + to(&mut app, 520.0); + assert_eq!(revision(&app), rev + 1, "the move was applied"); + assert_eq!(height(&session_layer(&app, id)), 220.0); + release(&mut app, 520.0); + } +} diff --git a/crates/ui-egui/src/tools/type_tool/editing.rs b/crates/ui-egui/src/tools/type_tool/editing.rs index 02ba991308..33d80b8bdb 100644 --- a/crates/ui-egui/src/tools/type_tool/editing.rs +++ b/crates/ui-egui/src/tools/type_tool/editing.rs @@ -410,6 +410,8 @@ pub(crate) fn commit_shortcut(app: &mut PhotocraftApp, ctx: &egui::Context) -> b /// callers don't change. pub fn commit_with(app: &mut PhotocraftApp, _mods: egui::Modifiers) { app.ui.type_insertion_style = None; + // A box drag in progress keeps the box it reached, as when every move was applied. + crate::type_box_preview::flush(app); crate::type_font_preview::clear(app); let current = app.session.active_index(); let Some(home) = app.ui.text_edit.as_ref().map(|ed| edit_home(app, ed)) else { @@ -536,6 +538,8 @@ pub fn end_if_left(app: &mut PhotocraftApp) { /// Cancel the editing session: undo it back to where it started (removes a new layer). pub fn cancel(app: &mut PhotocraftApp) { app.ui.type_insertion_style = None; + // The session is undone below, which took the dragged box with it before too. + crate::type_box_preview::discard(app); crate::type_transform::reset(app); let Some(ed) = app.ui.text_edit.take() else { return }; app.type_ime.clear_preedit(); diff --git a/crates/ui-egui/src/tools/type_tool/overlay.rs b/crates/ui-egui/src/tools/type_tool/overlay.rs index e710d9055d..1d1cbd5489 100644 --- a/crates/ui-egui/src/tools/type_tool/overlay.rs +++ b/crates/ui-egui/src/tools/type_tool/overlay.rs @@ -16,6 +16,11 @@ pub fn draw_overlay(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewX } let id = LayerId(ed.layer); let Some((l, aff, text)) = layout(app, id) else { return }; + // A handle drag rendered in the background has not changed the document: draw the box where + // the pointer has it. The caret, selection and overflow mark still come from the document's + // layout, so the first two are hidden for the drag and the overflow mark follows on release. + let dragged = crate::type_box_preview::wanted_box(app, id); + let aff = dragged.map_or(aff, |b| crate::type_box_preview::overlay_affine(aff, b)); if ed.mask { draw_mask_overlay(app, painter, xf, id); } @@ -25,7 +30,10 @@ pub fn draw_overlay(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewX let p = aff.apply(Point::new(x as f64, y as f64)); xf.to_screen(p.x as f32, p.y as f32) }; - let shape = app.session.active().and_then(|s| text_layer(&s.doc, id).map(|t| t.shape.clone())); + let mut shape = app.session.active().and_then(|s| text_layer(&s.doc, id).map(|t| t.shape.clone())); + if let Some(b) = dragged { + shape = Some(photocraft_doc::text::TextShape::Box { x: 0.0, y: 0.0, width: b[2].max(1.0) as f32, height: b[3].max(1.0) as f32 }); + } // Type on a path: the caret, selection and underline follow the curve the glyphs sit on. let text_start = l.line_bounds().map_or(0.0, |bounds| f64::from(bounds[0])); let bend = shape.as_ref().and_then(|shape| photocraft_text::warp::Warp::on_path_oriented(shape, text_start, l.vertical)); @@ -95,6 +103,9 @@ pub fn draw_overlay(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewX } } } + if dragged.is_some() { + return; + } // Uncommitted IME text is underlined, but only while the stored range still holds exactly // the composition: an application edit may have shifted it onto committed characters. if let Some(offsets) = ime_offsets(&text, &ed) diff --git a/crates/ui-egui/src/tools/type_tool/pointer.rs b/crates/ui-egui/src/tools/type_tool/pointer.rs index f6c35566ad..4e092190a8 100644 --- a/crates/ui-egui/src/tools/type_tool/pointer.rs +++ b/crates/ui-egui/src/tools/type_tool/pointer.rs @@ -98,6 +98,10 @@ fn resize_box(app: &mut PhotocraftApp, ed: &TextEdit, r: BoxResize, x: f64, y: f } // type.edit places the box's top-left at the given document point. let o = r.aff.apply(Point::new(f64::from(x0), f64::from(y0))); + // Rendered in the background where that is on; the release applies the box. + if crate::type_box_preview::want(app, LayerId(ed.layer), [o.x, o.y, f64::from(x1 - x0), f64::from(y1 - y0)]) { + return; + } let _ = app.run("type.edit", json!({"layer": ed.layer, "box": [o.x, o.y, x1 - x0, y1 - y0], "coalesce": ed.session})); } @@ -122,6 +126,9 @@ pub fn pointer_down(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifie if let (Some(snap), Some(e)) = (snap, app.ui.text_edit.as_mut()) { e.resize = Some(snap); } + if app.ui.text_edit.as_ref().is_some_and(|e| e.resize.is_some()) { + crate::type_box_preview::begin(app, id, ed.session.clone()); + } return true; } // Photoshop: anywhere inside the paragraph box places the caret, even off the glyphs @@ -304,6 +311,7 @@ fn makes_box(app: &PhotocraftApp, start: [f64; 2], end: [f64; 2]) -> bool { /// Pointer up. `rect` is the dragged box (document px) when no edit session consumed the press. pub fn pointer_up(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) { + crate::type_box_preview::flush(app); if let Some(e) = app.ui.text_edit.as_mut() { e.dragging = false; e.resize = None;