diff --git a/crates/ui-egui/src/app/control.rs b/crates/ui-egui/src/app/control.rs index f63ab334a..b76ac91aa 100644 --- a/crates/ui-egui/src/app/control.rs +++ b/crates/ui-egui/src/app/control.rs @@ -90,6 +90,8 @@ impl FigApp { "quality": if self.canvas.tex_coarse { "preview" } else { "refined" }, "previewLatencyMs": self.canvas.preview_latency_ms, "refineLatencyMs": self.canvas.refine_latency_ms, + // The share of the canvas only the page colour covered last frame (no render reached it). + "uncovered": self.canvas.uncovered, }, "live": live::inspect(self), "window": self.windows.current, diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 51507b0a5..5de28c832 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -62,6 +62,9 @@ pub struct CanvasState { /// shows its image at once while it renders again, instead of a blank canvas (and, where /// renders are slow, a coarse preview). kept: Vec, + /// The coarse image of the area around the view ([`render_worker::Surround`]), shown where + /// the texture doesn't reach, and the file and page it is of. + surround: Option<(egui::TextureHandle, figcraft_render::View, (u64, NodeId))>, submitted: Option, /// The view of the last render asked for. want_view: Option, @@ -71,6 +74,8 @@ pub struct CanvasState { /// canvas texture (ms; the texture is uploaded when that frame is painted). pub preview_latency_ms: Option, pub refine_latency_ms: Option, + /// The share of the canvas no render covered last frame: the page background showed there. + pub uncovered: f32, dragging: bool, space_pan: bool, middle_pan: Option<(Pos2, f64, f64)>, @@ -104,11 +109,13 @@ impl Default for CanvasState { tex_coarse: false, tex_seq: 0, kept: Vec::new(), + surround: None, submitted: None, want_view: None, render_work: None, preview_latency_ms: None, refine_latency_ms: None, + uncovered: 0.0, dragging: false, space_pan: false, middle_pan: None, @@ -339,6 +346,21 @@ pub fn show(app: &mut FigApp, ui: &mut Ui, worker: &mut Worker) { app.canvas.tex_coarse = done.coarse; app.canvas.tex_seq = done.seq; } + if let Some(around) = worker.surround() + && shown == Some((around.uid, around.page)) + { + let of = (around.uid, around.page); + match &mut app.canvas.surround { + Some((t, view, at)) => { + t.set(around.image, egui::TextureOptions::LINEAR); + (*view, *at) = (around.view, of); + } + None => { + let t = ctx.load_texture("figcraft-canvas-surround", around.image, egui::TextureOptions::LINEAR); + app.canvas.surround = Some((t, around.view, of)); + } + } + } let painter = ui.painter_at(rect); crate::theme::set_canvas_page(&ctx, bg); let image = if let (Some(tex), Some(v), true) = (&app.canvas.texture, &app.canvas.tex_view, shown.is_some() && app.canvas.tex_of == shown) { @@ -351,7 +373,16 @@ pub fn show(app: &mut FigApp, ui: &mut Ui, worker: &mut Worker) { } else { None }; - background::paint(&painter, rect, bg, image.map(|(_, r)| r)); + // Not in pixel preview: its pixels are the point, and it keeps no image around the view. + let pixel_preview = app.session.ds().ok().and_then(|ds| ds.view.pixel_preview_scale()).is_some(); + let surround = match (&app.canvas.surround, pixel_preview) { + (Some((tex, v, of)), false) if shown == Some(*of) => { + let zoom = app.session.ds().map(|d| d.view.zoom).unwrap_or(1.0); + Some((tex.id(), presented_image_rect(app.to_screen(v.origin.0, v.origin.1), v, zoom, ppp, false))) + } + _ => None, + }; + app.canvas.uncovered = background::paint(&painter, rect, bg, image.map(|(_, r)| r), surround); if let Some((texture, image_rect)) = image { painter.image(texture, image_rect, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE); } @@ -461,5 +492,8 @@ mod wheel_zoom_tests; #[cfg(test)] mod zoom_preview_tests; +#[cfg(test)] +mod surround_tests; + #[cfg(test)] mod tab_switch_tests; diff --git a/crates/ui-egui/src/canvas/background.rs b/crates/ui-egui/src/canvas/background.rs index f7eadd640..cac9e2179 100644 --- a/crates/ui-egui/src/canvas/background.rs +++ b/crates/ui-egui/src/canvas/background.rs @@ -1,6 +1,6 @@ //! The transparency grid behind the page; rendered pixels already contain the page colour. -use egui::{Color32, CornerRadius, Id, Painter, Rect, TextureHandle, pos2}; +use egui::{Color32, CornerRadius, Id, Painter, Rect, TextureHandle, TextureId, pos2}; /// Reuse the checkerboard texture, updating its pixels when the active theme changes. fn grid_texture(ctx: &egui::Context) -> TextureHandle { @@ -25,18 +25,40 @@ fn grid_texture(ctx: &egui::Context) -> TextureHandle { texture } -/// Draw the transparency grid and apply page colour only outside the rendered image. -pub(super) fn paint(painter: &Painter, rect: Rect, background: Color32, rendered: Option) { +/// Draw the transparency grid, and where the rendered image doesn't reach the coarse image around +/// the view (`surround`: its texture and screen rect), and the page colour past that. Returns the +/// share of `rect` that only the page colour covers. +pub(super) fn paint(painter: &Painter, rect: Rect, background: Color32, rendered: Option, surround: Option<(TextureId, Rect)>) -> f32 { if background.a() < 255 { // Repeat four pixels instead of allocating one shape per square on a large canvas. let texture = grid_texture(painter.ctx()); let uv = Rect::from_min_size(egui::Pos2::ZERO, rect.size() / 16.0); painter.image(texture.id(), rect, uv, Color32::WHITE); } - // The render contains the page colour and its alpha. Painting that colour beneath it - // applies alpha twice (50% becomes 75%). Only uncovered areas need the fallback colour. - let covered = rendered.map(|r| r.intersect(rect)).filter(|r| r.is_positive()); - let areas = match covered { + // Renders contain the page colour and its alpha. Painting that colour (or the coarse image) + // beneath one applies alpha twice (50% becomes 75%): each area gets exactly one of them. + let mut bare = 0.0; + for area in outside(rect, rendered) { + let rest = match surround { + Some((texture, at)) => { + if area.intersect(at).is_positive() { + painter.with_clip_rect(area).image(texture, at, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE); + } + outside(area, Some(at)) + } + None => outside(area, None), + }; + for r in rest { + painter.rect_filled(r, CornerRadius::ZERO, background); + bare += r.area(); + } + } + if rect.area() > 0.0 { (bare / rect.area()).clamp(0.0, 1.0) } else { 0.0 } +} + +/// The parts of `rect` outside `covered`: at most four strips that don't overlap. +fn outside(rect: Rect, covered: Option) -> impl Iterator { + let areas = match covered.map(|r| r.intersect(rect)).filter(|r| r.is_positive()) { Some(r) => [ Rect::from_min_max(rect.min, pos2(rect.right(), r.top())), Rect::from_min_max(pos2(rect.left(), r.bottom()), rect.max), @@ -45,9 +67,7 @@ pub(super) fn paint(painter: &Painter, rect: Rect, background: Color32, rendered ], None => [rect, Rect::NOTHING, Rect::NOTHING, Rect::NOTHING], }; - for area in areas.into_iter().filter(|r| r.is_positive()) { - painter.rect_filled(area, CornerRadius::ZERO, background); - } + areas.into_iter().filter(|r| r.is_positive()) } #[cfg(test)] @@ -59,7 +79,7 @@ mod tests { fn shapes(alpha: u8, rendered: Option) -> egui::FullOutput { let ctx = egui::Context::default(); let mut out = ctx.run_ui(Default::default(), |ui| { - paint(ui.painter(), canvas(), Color32::from_rgba_unmultiplied(255, 0, 0, alpha), rendered); + paint(ui.painter(), canvas(), Color32::from_rgba_unmultiplied(255, 0, 0, alpha), rendered, None); }); out.textures_delta.clear(); out @@ -169,7 +189,9 @@ mod tests { let mut previous_id = None; for tokens in [crate::theme::Tokens::light(), crate::theme::Tokens::dark(), crate::theme::Tokens::light()] { crate::theme::apply(&ctx, tokens); - let mut out = ctx.run_ui(Default::default(), |ui| paint(ui.painter(), canvas(), Color32::TRANSPARENT, None)); + let mut out = ctx.run_ui(Default::default(), |ui| { + paint(ui.painter(), canvas(), Color32::TRANSPARENT, None, None); + }); let (tile, _) = ctx.data(|d| d.get_temp::<(TextureHandle, [Color32; 2])>(Id::unique("canvas_transparency_grid"))).expect("drawn checkerboard"); let grid_index = assert_grid(&out, tile.id(), canvas()); @@ -227,4 +249,47 @@ mod tests { } } } + + /// Past the rendered image the coarse image around the view shows, clipped to outside the + /// image, and the page colour only past both: nothing is painted twice, and what is left bare + /// is what the call returns. + #[test] + fn the_image_around_the_view_fills_what_the_render_leaves_and_the_page_colour_the_rest() { + let ctx = egui::Context::default(); + let around = TextureId::User(7); + // The render covers the left half; the coarse image reaches 80 to the right. + let rendered = Rect::from_min_max(pos2(0.0, 0.0), pos2(50.0, 100.0)); + let at = Rect::from_min_max(pos2(-100.0, -100.0), pos2(80.0, 200.0)); + let mut bare = -1.0; + let mut out = ctx.run_ui(Default::default(), |ui| { + bare = paint(ui.painter(), canvas(), Color32::RED, Some(rendered), Some((around, at))); + }); + out.textures_delta.clear(); + let coarse: Vec<_> = out.shapes.iter().filter(|s| matches!(&s.shape, egui::Shape::Mesh(m) if m.texture_id == around)).collect(); + assert_eq!(coarse.len(), 1, "one strip of the canvas lies past the render"); + assert_eq!(coarse[0].clip_rect, Rect::from_min_max(pos2(50.0, 0.0), pos2(100.0, 100.0)), "clipped to past the render"); + let fills: Vec = out + .shapes + .iter() + .filter_map(|s| match &s.shape { + egui::Shape::Rect(r) => Some(r.rect), + _ => None, + }) + .collect(); + assert_eq!(fills, [Rect::from_min_max(pos2(80.0, 0.0), pos2(100.0, 100.0))], "the page colour only past both"); + assert!((bare - 0.2).abs() < 1e-6, "a fifth of the canvas is bare: {bare}"); + // Without one the page colour fills all of it; with one reaching everywhere nothing. + let mut out = ctx.run_ui(Default::default(), |ui| { + bare = paint(ui.painter(), canvas(), Color32::RED, Some(rendered), None); + }); + out.textures_delta.clear(); + assert!((bare - 0.5).abs() < 1e-6, "{bare}"); + let everywhere = Rect::from_min_max(pos2(-100.0, -100.0), pos2(200.0, 200.0)); + let mut out = ctx.run_ui(Default::default(), |ui| { + bare = paint(ui.painter(), canvas(), Color32::RED, Some(rendered), Some((around, everywhere))); + }); + out.textures_delta.clear(); + assert_eq!(bare, 0.0); + assert!(!out.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Rect(_))), "no page colour under the coarse image"); + } } diff --git a/crates/ui-egui/src/canvas/render_worker.rs b/crates/ui-egui/src/canvas/render_worker.rs index 27c9cc32b..c0fc8f312 100644 --- a/crates/ui-egui/src/canvas/render_worker.rs +++ b/crates/ui-egui/src/canvas/render_worker.rs @@ -19,6 +19,9 @@ //! ([`figcraft_render::Steps`]), at least a band a frame and more while they fit in [`STEP_MS`], //! so input is handled while it draws (on a page whose render takes seconds, a band is a //! fraction of that: eight at most). +//! On the desktop a third thread keeps a coarse image of the area around the view +//! ([`Surround`]): where the canvas image doesn't reach (a pan past its edge before the strips are +//! drawn, a zoom out) the canvas shows that, blurred, instead of the bare page colour. use std::sync::Arc; @@ -145,6 +148,78 @@ fn done(job: &Job, view: View, pix: &Pixmap, work: Work, ms: f64, coarse: bool) Done { image, view, ms, work, uid: job.uid, page: job.page, coarse, at: job.at, seq: 0 } } +/// The coarse image around the view is this many views wide and tall, centred on it. +const SURROUND_SPAN: f64 = 3.0; + +/// The area [`SURROUND_SPAN`] times as wide and tall as `view`, centred on it, with +/// [`PREVIEW_FACTOR`] times fewer pixels per page unit (`view` is at most as large as a texture, +/// so this is too). +pub fn surround_view(view: &View) -> View { + let c = coarse_view(view); + let s = c.scale.max(1e-9); + let (w, h) = (f64::from(c.width), f64::from(c.height)); + let pad = (SURROUND_SPAN - 1.0) / 2.0; + let side = |n: f64| (n * SURROUND_SPAN).ceil().max(1.0) as u32; + View { origin: (view.origin.0 - pad * w / s, view.origin.1 - pad * h / s), scale: c.scale, width: side(w), height: side(h) } +} + +/// Whether `around` reaches a third of `view`'s size past each of its sides: a pan that far shows +/// no edge. +fn reaches(around: &View, view: &View) -> bool { + let (a, v) = (around.page_rect(), view.page_rect()); + let (mx, my) = (v.width() / 3.0, v.height() / 3.0); + a.x0 <= v.x0 - mx && a.y0 <= v.y0 - my && a.x1 >= v.x1 + mx && a.y1 >= v.y1 + my +} + +/// Whether `a` and `b` are the same view, bit for bit. +fn same_view(a: &View, b: &View) -> bool { + (a.origin.0.to_bits(), a.origin.1.to_bits(), a.scale.to_bits(), a.width, a.height) + == (b.origin.0.to_bits(), b.origin.1.to_bits(), b.scale.to_bits(), b.width, b.height) +} + +/// The coarse image of the area around the view ([`surround_view`]), kept between requests. +#[derive(Default)] +pub struct Surround { + retained: Retained, + /// The file, page and view of the image handed over last, and whether it was soft-proofed + /// (marked after the render, so the kept image can't tell). + of: Option<(u64, NodeId, View, bool)>, +} + +impl Surround { + /// The image around `job`'s view when the one handed over last no longer stands for it (of + /// another file, page or zoom, short of a third of a view past an edge, or the document + /// changed), drawn on the kept one: a pan shifts it and redraws the strips, an edit the areas + /// that changed. `None` in pixel preview, whose pixels are the point. + pub fn render(&mut self, job: &Job) -> Option { + if !job.zoom_preview { + return None; + } + let want = surround_view(&job.view); + let view = match &self.of { + Some((uid, page, have, _)) + if *uid == job.uid && *page == job.page && have.scale.to_bits() == want.scale.to_bits() && reaches(have, &job.view) => + { + have.clone() + } + _ => want, + }; + let same = self + .of + .as_ref() + .is_some_and(|(uid, page, have, proof)| *uid == job.uid && *page == job.page && *proof == job.proof && same_view(have, &view)); + let t0 = Instant::now(); + let d = guarded(&mut self.retained, |r| { + let (snapped, pix, work) = r.render(&job.doc, job.page, &view, &options(job))?; + (!(same && work == Work::Reused)).then(|| done(job, snapped, pix, work, t0.elapsed().as_secs_f64() * 1000.0, true)) + }); + if d.is_some() { + self.of = Some((job.uid, job.page, view, job.proof)); + } + d + } +} + /// `view` with [`PREVIEW_FACTOR`] times fewer pixels each way, showing at least the same area. pub fn coarse_view(view: &View) -> View { let shrink = |n: u32| (f64::from(n) / PREVIEW_FACTOR).ceil().max(1.0) as u32; @@ -413,6 +488,12 @@ pub struct Worker { finished: Arc, /// The watchdog is drawing a late preview, or has one to hand over and wake the UI for. previewing: Arc, + /// The images around the view ([`Surround`]) from their thread. + surround_rx: std::sync::mpsc::Receiver, + /// The newest request handed to the surround thread, and the newest it is done with (its image + /// handed over and the UI woken, or none needed). + surround_asked: Arc, + surround_done: Arc, } #[cfg(not(target_arch = "wasm32"))] @@ -467,6 +548,33 @@ impl Worker { if let Err(e) = watchdog { log::error!("render preview thread: {e}"); } + // The image around the view, on a thread of its own so it never holds up the canvas image: + // the render thread hands it each request it has refined, the newest queued one wins. + let (around_tx, around) = std::sync::mpsc::channel::<(u64, Job)>(); + let (surround_tx, surround_rx) = std::sync::mpsc::channel::(); + let surround_ctx = ctx.clone(); + let surround_asked = Arc::new(std::sync::atomic::AtomicU64::new(0)); + let surround_done = Arc::new(std::sync::atomic::AtomicU64::new(0)); + let (asked, surrounded) = (surround_asked.clone(), surround_done.clone()); + let surrounding = + std::thread::Builder::new().name("figcraft-render-surround".into()).stack_size(figcraft_render::RENDER_STACK).spawn(move || { + let mut surround = Surround::default(); + while let Ok((mut seq, mut job)) = around.recv() { + while let Ok(newer) = around.try_recv() { + (seq, job) = newer; + } + if let Some(d) = surround.render(&job) { + if surround_tx.send(d).is_err() { + break; + } + surround_ctx.request_repaint_of(viewport); + } + surrounded.store(seq, std::sync::atomic::Ordering::SeqCst); + } + }); + if let Err(e) = surrounding { + log::error!("render surround thread: {e}"); + } let spawned = std::thread::Builder::new().name("figcraft-render".into()).stack_size(figcraft_render::RENDER_STACK).spawn(move || { let mut renders = Renders::default(); let mut next = None; @@ -502,11 +610,21 @@ impl Worker { } let done = renders.refine(&job); refined.store(seq, std::sync::atomic::Ordering::SeqCst); + let refined_job = done.is_some(); if let Some(d) = done && done_tx.send(answering(d, seq)).is_err() { break; } + if refined_job { + // Asked before the send (and before this request counts as done), so `in_flight` + // holds until the surround thread is done with it. Without that thread (it failed + // to start) the page colour shows past the edge. + asked.store(seq, std::sync::atomic::Ordering::SeqCst); + if around_tx.send((seq, job)).is_err() { + asked.store(0, std::sync::atomic::Ordering::SeqCst); + } + } ctx.request_repaint_of(viewport); done_with.store(seq, std::sync::atomic::Ordering::SeqCst); } @@ -514,7 +632,7 @@ impl Worker { if let Err(e) = spawned { log::error!("render thread: {e}"); } - Self { tx, rx, submitted: 0, finished, previewing } + Self { tx, rx, submitted: 0, finished, previewing, surround_rx, surround_asked, surround_done } } pub fn submit(&mut self, job: Job) { @@ -524,9 +642,13 @@ impl Worker { } /// Whether a submitted job is still rendering or hasn't woken the UI yet, the watchdog's late - /// preview included (a finished image may still wait for [`Self::poll`]). + /// preview and the image around the view included (a finished image may still wait for + /// [`Self::poll`] or [`Self::surround`]). pub fn in_flight(&self) -> bool { - self.finished.load(std::sync::atomic::Ordering::SeqCst) < self.submitted || self.previewing.load(std::sync::atomic::Ordering::SeqCst) + use std::sync::atomic::Ordering::SeqCst; + self.finished.load(SeqCst) < self.submitted + || self.previewing.load(SeqCst) + || self.surround_done.load(SeqCst) < self.surround_asked.load(SeqCst) } /// The best of the renders finished since the last poll ([`better`]). @@ -537,6 +659,11 @@ impl Worker { } best } + + /// The newest image around the view ([`Surround`]) finished since the last call. + pub fn surround(&mut self) -> Option { + self.surround_rx.try_iter().last() + } } /// The browser's worker: renders inline, one image a frame: a preview first when one is due, the @@ -599,6 +726,11 @@ impl Inline { pub fn in_flight(&self) -> bool { self.pending.is_some() } + /// shortcut: the browser renders on the page's own thread, so it keeps no image around the view + /// (one would hold the page up); draw one there once the canvas renders on a wasm thread (#1500). + pub fn surround(&mut self) -> Option { + None + } pub fn poll(&mut self) -> Option { self.poll_at(Instant::now()) } diff --git a/crates/ui-egui/src/canvas/surround_tests.rs b/crates/ui-egui/src/canvas/surround_tests.rs new file mode 100644 index 000000000..f869c860c --- /dev/null +++ b/crates/ui-egui/src/canvas/surround_tests.rs @@ -0,0 +1,83 @@ +//! The coarse image around the view ([`Surround`]): three views wide and tall at a quarter of the +//! pixels each way, kept while the view stays a third of a view inside it, moved (a shift and the +//! strips) when a pan goes further, patched after an edit, none in pixel preview; and the desktop +//! worker hands one over after each refined render, so a pan past the canvas image shows that +//! instead of the bare page colour. + +use std::time::Duration; + +use figcraft_render::{View, Work}; +use serde_json::json; + +use super::zoom_preview_tests::{document, job, view_at}; +use crate::render_worker::{Job, PREVIEW_FACTOR, Surround, Worker, surround_view}; + +/// Whether `outer` shows all of the page `inner` does, give or take the snapping to whole pixels. +fn contains(outer: &View, inner: &View) -> bool { + let (o, i) = (outer.page_rect(), inner.page_rect()); + let eps = 2.0 / outer.scale; + o.x0 <= i.x0 + eps && o.y0 <= i.y0 + eps && o.x1 >= i.x1 - eps && o.y1 >= i.y1 - eps +} + +#[test] +fn the_area_around_a_view_is_three_views_wide_at_a_quarter_of_the_pixels() { + let v = view_at((400.0, 300.0), 2.0, 800, 600); + let a = surround_view(&v); + assert_eq!(a.scale, v.scale / PREVIEW_FACTOR); + assert_eq!((a.width, a.height), (600, 450), "3 × 800 / 4 by 3 × 600 / 4"); + let (pa, pv) = (a.page_rect(), v.page_rect()); + assert!((pa.center().x - pv.center().x).abs() < 1e-9 && (pa.center().y - pv.center().y).abs() < 1e-9, "centred on the view"); + assert!((pa.width() - 3.0 * pv.width()).abs() < 1e-9 && (pa.height() - 3.0 * pv.height()).abs() < 1e-9); +} + +#[test] +fn the_image_around_the_view_is_kept_for_small_pans_and_moved_for_far_ones() { + let (s, rect) = document(); + let ds = s.ds().unwrap(); + let at = |c: (f64, f64)| job(&ds.doc, ds.page, ds.uid, view_at(c, 1.0, 400, 300)); + let mut around = Surround::default(); + let first = around.render(&at((250.0, 150.0))).expect("the first one"); + assert_eq!(first.work, Work::Full); + assert!(first.coarse && contains(&first.view, &surround_view(&at((250.0, 150.0)).view))); + assert!(around.render(&at((250.0, 150.0))).is_none(), "the same request: nothing new"); + // A quarter of a view: it still reaches more than a third of a view past every side. + assert!(around.render(&at((350.0, 225.0))).is_none(), "kept for a small pan"); + // Most of a view: moved to centre on the view again, built on the kept image. + let moved = around.render(&at((570.0, 150.0))).expect("moved for a far pan"); + assert!(matches!(moved.work, Work::Partial { .. }), "a shift and the strips: {:?}", moved.work); + assert!(contains(&moved.view, &surround_view(&at((570.0, 150.0)).view))); + // An edit redraws its area. + let mut s = s; + s.execute("node.set", &json!({"id": rect, "props": {"x": 140}})).unwrap(); + let ds = s.ds().unwrap(); + let edited = job(&ds.doc, ds.page, ds.uid, view_at((570.0, 150.0), 1.0, 400, 300)); + assert!(matches!(around.render(&edited).expect("after an edit").work, Work::Partial { .. })); + // Soft proof marks the image after it renders: turning it on or off hands over a new one. + assert!(around.render(&Job { proof: true, ..edited.clone() }).is_some(), "proof on"); + assert!(around.render(&Job { proof: true, ..edited.clone() }).is_none()); + assert!(around.render(&edited).is_some(), "proof off"); + // Another zoom starts over; pixel preview has none. + let zoomed = job(&ds.doc, ds.page, ds.uid, view_at((570.0, 150.0), 2.0, 400, 300)); + assert_eq!(around.render(&zoomed).expect("another zoom").work, Work::Full); + assert!(around.render(&Job { zoom_preview: false, ..job(&ds.doc, ds.page, ds.uid, view_at((0.0, 0.0), 8.0, 400, 300)) }).is_none()); +} + +#[test] +fn the_worker_hands_over_the_image_around_each_refined_view() { + let (s, _) = document(); + let ds = s.ds().unwrap(); + let j = job(&ds.doc, ds.page, ds.uid, view_at((250.0, 150.0), 1.0, 400, 300)); + let mut w = Worker::new(egui::Context::default()); + w.submit(j.clone()); + let start = std::time::Instant::now(); + let around = loop { + assert!(start.elapsed() < Duration::from_secs(60), "no image around the view came"); + if let Some(d) = w.surround() { + break d; + } + std::thread::sleep(Duration::from_millis(2)); + }; + assert_eq!((around.uid, around.page), (ds.uid, ds.page)); + assert!(contains(&around.view, &surround_view(&j.view)), "it reaches a view past every side"); + assert!(w.poll().is_some_and(|d| !d.coarse), "the refined image came through as before"); +} diff --git a/crates/ui-egui/src/canvas/zoom_preview_tests.rs b/crates/ui-egui/src/canvas/zoom_preview_tests.rs index bef17fe59..af9914b74 100644 --- a/crates/ui-egui/src/canvas/zoom_preview_tests.rs +++ b/crates/ui-egui/src/canvas/zoom_preview_tests.rs @@ -27,7 +27,7 @@ use crate::render_worker::{ /// scanned first (once per process, and a concurrent caller waits for it): the scan another test /// starts bumps the global font generation, and a render kept from before it is stale, so a pan /// or an edit meanwhile would redraw in full instead of patching. -fn document() -> (Session, NodeId) { +pub(super) fn document() -> (Session, NodeId) { figcraft_text::scan_system_fonts(); let mut s = Session::new(); for k in 0..12 { @@ -69,11 +69,11 @@ fn slow_scene(views: &[View], ms: f64) -> Session { } /// A `w` × `h` device-pixel view at `scale` centred on page point `c`. -fn view_at(c: (f64, f64), scale: f64, w: u32, h: u32) -> View { +pub(super) fn view_at(c: (f64, f64), scale: f64, w: u32, h: u32) -> View { View { origin: (c.0 - f64::from(w) / scale / 2.0, c.1 - f64::from(h) / scale / 2.0), scale, width: w, height: h } } -fn job(doc: &Arc, page: NodeId, uid: u64, view: View) -> Job { +pub(super) fn job(doc: &Arc, page: NodeId, uid: u64, view: View) -> Job { Job { doc: doc.clone(), page, diff --git a/docs/control-protocol.md b/docs/control-protocol.md index b06d79932..381992c55 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -236,7 +236,9 @@ zoom preview or the refined image (`canvas.quality`: `preview` or `refined`) and asking for a render to its preview and its refined image being handed to egui as the canvas texture (`canvas.previewLatencyMs`, `canvas.refineLatencyMs`, on a monotonic clock, rendering included in the browser too; the GPU upload and presenting the frame come after; `null` until -there was one). `importReport` holds the Import report window's content (`{name, pages, layers, images, appended, placed, warnings, warningCount}`, +there was one), and the share of the canvas only the page colour covered last frame, no render +reaching it (`canvas.uncovered`, 0 to 1: past the canvas image the desktop shows a coarse image of the +area around the view). `importReport` holds the Import report window's content (`{name, pages, layers, images, appended, placed, warnings, warningCount}`, `null` while none is open). The window opens only for a file the user opened or imported in the app (File ▸ Open or Import, Open recent, a recent file on the home screen, a drop, a paste, the system's open event). Emulated user input counts as the user's action: `ui.input` and `ui.key` (for example a Cmd+V paste or a click diff --git a/docs/gaps.md b/docs/gaps.md index 584d7f3a0..2971d9bf0 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where FigCraft falls short of Figma -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-17 · **Change:** minor (gap 7: the strips a pan uncovers render in parallel bands; gap 7: the desktop refines a zoom's preview once the zoom has paused; gap 1: the `.fig` export writes vector region paints (#2302); gap 13: a boolean group's corner radius (#2222); gap 24: mind maps, first step: a first node with three children on curved branches, laid out again after every edit (`board.mindMap`, `board.mindMapAdd`, #2226); gap 10: library analytics list how many layers and files use each style and variable; the Controller menu names each button as PlayStation and Switch controllers do; gap 6: widgets draw lines and SVG pictures (plugin API 6); gap 7: text reads only the fallback fonts it draws with; a board connector's path can be reshaped by dragging its path handles (#1710); gap 10: library analytics count insertions and show insertions and detachments over time; gap 6: a widget opens its plugin's panel (plugin API 5), whose callbacks redraw it; the keyboard's Menu key opens the context menu, as Shift+F10 does; gap 14: a collection of this file extends, following its edits at once; colour picker review fixes for text undo, fitted sidebars, moving controls and gradient variables; gap 6: plugin widgets have hover styles; gap 7: the browser draws a zoom's full render a band at a time; `.fig` import keeps the file's saved line breaks while the text and its box are unedited (#1236); FigJam outer shape outlines and sticky body fallback beside author text (#1341, #1397); Arabic draft catalog; gap 7: the browser refines a zoom's preview once the zoom has paused; gap 17: branches in live sessions, branch history entries, the list-merge rows brought up to date; gap 6: plugin widgets take text input and a property menu (plugin API 4); gap 24: board templates and tables; gap 24: a shared timer and voting sessions; gap 24: the board dot grid, connector ends dragged to re-attach, curved connectors; egui/eframe 0.37 verified for gap 18; known gaps from the #1311 review in gaps 4, 14, 15 and 16; gap 16: measurements kept on the canvas, a read-only layers panel in Dev Mode; gap 24: boards and their tail; Dev Mode tail: import lines in layer trees, compare with versions, an inspect-only canvas; gap 5: reviewed Turkish, Czech and Indonesian drafts, the Japanese draft completed; reviewed Russian catalog and integer plurals; `.fig` export: instance sizes, variables with modes, shared styles, variable-width strokes; sharing with a join code; text box sizes measured against documents re-saved by other software; live sessions per document; .fig export checked by opening written files in other software; experimental .fig export; keyboard shortcut parity suite and bindings; Simplified Chinese draft catalog; Latin American Spanish overlay; prototyping tail: gamepad triggers, widgets in prototypes, mode lookups; plugin directory on figcraft-server; Japanese and Korean draft catalogs and CJK UI fonts; plugin widgets and the plugin panel tail; French, German and Brazilian Portuguese draft catalogs; cursor chat and spotlight in live sessions; layers and assets panels behaviour suite and fixes; vector multi-edit and edit tail; dev resources links; extended collections tail; branch version history and finer list merges; comments and version history behaviour suite and fixes; canvas navigation, guides and grids behaviour suite and fixes; `.fig` import: editable vector networks, more layer kinds, variable prototype actions; `.fig` import, best effort: gap 1 down to its tail; Windows QA pass: everyday workflows on Windows 11, eight fixes incl. unsaved work kept after a crash or a forced quit; Display P3: gamut warning and soft-proof (true P3 blocked upstream); several windows; localization layer with a draft Spanish catalog; hosting figcraft-server: wss in the desktop app, server releases, deployment guide; plugin panels and settings; canvas fixes: boolean hit testing on the result, group drags, frame outlines and slices; code connect; stretch and scatter brushes, pen pressure; text on a path and pattern fills; team libraries on the server and swap library; branches: create, update from main, review and merge; animated GIF fills and TIFF images; Open recent, outlines for the selection and mask outlines: menu breadth 100%; extended collections, variables import/export, gradient stop and variant bindings: gap 14 down to its tail; the 2026-10-17 landings: `.fig` import fidelity, budgets for large files, the import report, a `.fig` copy from Save as and paste from another design app (gap 1); menus, the Actions palette, editor fixes, Selection colors and smart-selection spacing (gap 4); catalog counts re-measured (gap 5); bounded canvas pan overscan) · **Target:** Figma Design (desktop app 126.10.7) +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-17 · **Change:** minor (gap 7: a pan past the canvas image shows a coarse image of the area around the view on the desktop, not the bare page colour; gap 7: the strips a pan uncovers render in parallel bands; gap 7: the desktop refines a zoom's preview once the zoom has paused; gap 1: the `.fig` export writes vector region paints (#2302); gap 13: a boolean group's corner radius (#2222); gap 24: mind maps, first step: a first node with three children on curved branches, laid out again after every edit (`board.mindMap`, `board.mindMapAdd`, #2226); gap 10: library analytics list how many layers and files use each style and variable; the Controller menu names each button as PlayStation and Switch controllers do; gap 6: widgets draw lines and SVG pictures (plugin API 6); gap 7: text reads only the fallback fonts it draws with; a board connector's path can be reshaped by dragging its path handles (#1710); gap 10: library analytics count insertions and show insertions and detachments over time; gap 6: a widget opens its plugin's panel (plugin API 5), whose callbacks redraw it; the keyboard's Menu key opens the context menu, as Shift+F10 does; gap 14: a collection of this file extends, following its edits at once; colour picker review fixes for text undo, fitted sidebars, moving controls and gradient variables; gap 6: plugin widgets have hover styles; gap 7: the browser draws a zoom's full render a band at a time; `.fig` import keeps the file's saved line breaks while the text and its box are unedited (#1236); FigJam outer shape outlines and sticky body fallback beside author text (#1341, #1397); Arabic draft catalog; gap 7: the browser refines a zoom's preview once the zoom has paused; gap 17: branches in live sessions, branch history entries, the list-merge rows brought up to date; gap 6: plugin widgets take text input and a property menu (plugin API 4); gap 24: board templates and tables; gap 24: a shared timer and voting sessions; gap 24: the board dot grid, connector ends dragged to re-attach, curved connectors; egui/eframe 0.37 verified for gap 18; known gaps from the #1311 review in gaps 4, 14, 15 and 16; gap 16: measurements kept on the canvas, a read-only layers panel in Dev Mode; gap 24: boards and their tail; Dev Mode tail: import lines in layer trees, compare with versions, an inspect-only canvas; gap 5: reviewed Turkish, Czech and Indonesian drafts, the Japanese draft completed; reviewed Russian catalog and integer plurals; `.fig` export: instance sizes, variables with modes, shared styles, variable-width strokes; sharing with a join code; text box sizes measured against documents re-saved by other software; live sessions per document; .fig export checked by opening written files in other software; experimental .fig export; keyboard shortcut parity suite and bindings; Simplified Chinese draft catalog; Latin American Spanish overlay; prototyping tail: gamepad triggers, widgets in prototypes, mode lookups; plugin directory on figcraft-server; Japanese and Korean draft catalogs and CJK UI fonts; plugin widgets and the plugin panel tail; French, German and Brazilian Portuguese draft catalogs; cursor chat and spotlight in live sessions; layers and assets panels behaviour suite and fixes; vector multi-edit and edit tail; dev resources links; extended collections tail; branch version history and finer list merges; comments and version history behaviour suite and fixes; canvas navigation, guides and grids behaviour suite and fixes; `.fig` import: editable vector networks, more layer kinds, variable prototype actions; `.fig` import, best effort: gap 1 down to its tail; Windows QA pass: everyday workflows on Windows 11, eight fixes incl. unsaved work kept after a crash or a forced quit; Display P3: gamut warning and soft-proof (true P3 blocked upstream); several windows; localization layer with a draft Spanish catalog; hosting figcraft-server: wss in the desktop app, server releases, deployment guide; plugin panels and settings; canvas fixes: boolean hit testing on the result, group drags, frame outlines and slices; code connect; stretch and scatter brushes, pen pressure; text on a path and pattern fills; team libraries on the server and swap library; branches: create, update from main, review and merge; animated GIF fills and TIFF images; Open recent, outlines for the selection and mask outlines: menu breadth 100%; extended collections, variables import/export, gradient stop and variant bindings: gap 14 down to its tail; the 2026-10-17 landings: `.fig` import fidelity, budgets for large files, the import report, a `.fig` copy from Save as and paste from another design app (gap 1); menus, the Actions palette, editor fixes, Selection colors and smart-selection spacing (gap 4); catalog counts re-measured (gap 5); bounded canvas pan overscan) · **Target:** Figma Design (desktop app 126.10.7) Every known shortfall against Figma, one entry each, ranked by how much it stops a Figma user from doing real work in FigCraft. Agents: pick work from the top unless the owner says otherwise. The @@ -312,6 +312,14 @@ No alpha blockers: the alpha gate in [`roadmap.md`](roadmap.md#alpha-gate) passe wait for it: on a 29,000 × 12,600 page of 9,000 layers with images and shadows (M1 Pro, 2x display) full renders took 110–520 ms, and the preview stood in for that long. Pans and edits are refined at once, as before. +- **Landed (2026-11-17):** the desktop keeps a coarse image of the area around the view, three + views wide and tall at a quarter of the pixels each way (`render_worker::Surround`, on a thread of + its own after each refined render, built on the last one: a pan shifts it, an edit patches it). + Where the canvas image doesn't reach, a pan past its edge before the strips are drawn or a zoom + out, the canvas shows that, blurred, then sharp, instead of the bare page colour. On a page of + 3,600 cards with shadows panned at 60 pt a step (debug build), the canvas was 36% bare on + average and at 77 of 80 steps; now none of it is. `ui.inspect` reports the bare share + (`canvas.uncovered`). The browser keeps none (it renders on the page's thread). - **Landed (2026-11-17):** the strips a pan uncovers, and the areas an edit redraws, render in parallel bands as full renders do (`render_part`): rows of a tall strip, columns of a wide one, however few pixels they have. A strip costs the layers it crosses, not its pixels (a card's @@ -609,6 +617,7 @@ No alpha blockers: the alpha gate in [`roadmap.md`](roadmap.md#alpha-gate) passe | Date | Change | Summary | |---|---|---| +| 2026-11-17 | minor | Gap 7: a pan past the canvas image shows a coarse image of the area around the view (desktop), not the bare page colour; `canvas.uncovered` in `ui.inspect`. Numbers unchanged. Tests: `surround_tests`, `background::tests::the_image_around_the_view_fills_what_the_render_leaves_and_the_page_colour_the_rest` | | 2026-11-17 | minor | Gap 7: the strips a pan uncovers render in parallel bands (rows of a tall strip, columns of a wide one), 3–5× quicker pans on effect-heavy pages. Numbers unchanged. Tests: `band_tests::columns_match_one_piece_rendering`, `band_tests::a_tall_or_wide_part_of_a_view_matches_one_piece_rendering`; timings: `pan_bench_tests` | | 2026-11-17 | minor | Gap 7: the desktop refines a zoom's preview only once the zoom has paused, as the browser does, so a full render that can't be stopped no longer holds up the next steps of a held zoom. Numbers unchanged. Test: `zoom_preview_tests::the_render_thread_refines_a_zoom_only_once_it_pauses` | | 2026-11-12 | minor | Gap 1: the `.fig` export writes vector region paints (#2302); the 2026-10-26 "Landed" bullet no longer says the export drops them |