diff --git a/ROADMAP.md b/ROADMAP.md index b2221910..df38638f 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ VectorCraft is a clean-room, open-source, pure-Rust reimplementation of the Adob This file tracks **how far we are and what's left**. Time estimates are wall-clock hours of continuous Claude Opus 5.5 agent work (including builds and the CI gate), given both for **one agent** and for **4–6 parallel agents** on disjoint crates. They are counted from the remaining work (see [Parity estimate](#parity-estimate)), calibrated against measured throughput, and updated as work lands. -_Last updated: 2026-10-08 (after M4.14–M4.98, M8.1–M8.20, M14.4–M14.7, the MCP prompts, resource templates, completions and logging, the 2026-10-07 issue fixes, the Layers panel rework and the 2026-10-08 community contributions: Pen and shape-tool modifiers, path editing, Layers, Artboards, Swatches/Brushes and dialogs, Japanese composition and PDF text, Hebrew/Arabic type, interface languages (Spanish among them), the font menu, saved selections, MCP and EPS, the missing-font export warning (#319), open paths in the Shape Builder (#374), PDF/`.ai` import keeping strokes live and hidden and nested layers (#372), per-corner Live Corners (#381), SVG type opening at the size it draws at (#396), the Wayland clipboard with copied-file paste (#398), ruler guides that can be selected, moved and deleted (#414), two batches of Preferences options that were stored but unused (#394), skeleton reads with `document.find` for large documents over MCP (#416) and anchors dragged with Direct Selection snapping to points, segments and smart guides, clipped SVG layers opening as clipping layers (#420), PDF/`.ai` layers read on pages of tens of millions of operators (#419), Document Color Mode putting RGB greys on the black plate on request and converting pattern tiles (#421) and movable dialogs with Offset Path's Joins dropdown (#423) and numeric fields scrubbed by dragging their label (#400), stroke weight presets in the stroke unit (#426), the Appearance panel's Opacity popup (#430), type on a path brackets, flip and Type on a Path Options that keep the type in place (#429), Layers rows dragged with a ghost and dropped on the trash or New Layer (#435), tool flyouts opening on a right or corner press (#436), tool flyouts torn off into floating toolbars (#437), the Shape Builder's erase mode deleting edges too (#439), installed fonts registered outside the font folders and fonts installed while the app runs listed in the font menus (#443), alike circular corners on rectangles that aren't square (#442), tool cursors as OS cursors so the pointer no longer trails over the canvas (#444), artboards copied and pasted with their art (#446), guides dragged out of the rulers snapping to the art's side midpoints and the artboards, artboard guides (#451), the toolbar's double arrow and Window › Toolbars › Double Column switching between one and two columns in any window height (#455), the Smart Guides display options and Snapping Tolerance (#394), Font Size and Leading combo boxes with preset lists in the Character and Properties panels and the Control bar (#458), screenshots pasted from the clipboard on macOS, Windows and the web (#457), the Properties panel's Fill and Stroke matching the Control bar's, Illustrator's Window menu panel keys (Shift+F7 for Align…) and Alt-wheel zoom whatever the wheel preference (Discord feedback), the point/area type widget (Discord feedback), groups in legacy Illustrator .ai files (Discord feedback), a rotating desktop log file (#465), image controls in the Control bar and Properties panel: Image Trace with its presets, Mask, Crop Image, Embed/Unembed, and an Image Trace object's preset, Expand and Release (Discord feedback), the Pen working as the Anchor Point tool with Alt held over a selected path's handles and anchors, and anchor buttons that convert, connect and cut paths at the selected anchors (Discord feedback), Japanese hanging punctuation (burasagari, #463), Escape leaving Presentation Mode with no screen mode saved across restarts, documents dropped on the desktop app opening as tabs of their own and a note that an `.ai` file's art off its artboards isn't in the part read (#472), Add/Delete Anchor Point tool icons drawn from the Pen tool's (#479), EPS files from Illustrator 2026, Inkscape/cairo, matplotlib, CorelDRAW, Affinity, LibreOffice and Ghostscript opening as vectors, and a preview that stands in saying why (Discord feedback, #466, #469, #474), chords with modifiers recorded in Edit › Keyboard Shortcuts (#487), Type Object Selection by Path Only (#394), Anti-aliased Artwork (#394), Show Pixel Grid (#394), pen and touch taps pressed and lifted within one frame clicking where they tapped and the Linux/Wayland pen-tablet workaround (#491), bounding-box resizes landing on the other objects' anchors and centres with labels, and the Rotate, Scale, Shear and Reflect tools snapping their drags (#482), Shift-drag marquees that toggle the objects or anchors they reach and a Lasso whose Alt subtraction keeps the rest of the selection (#483), the mouse wheel stepping the focused numeric field, the dialogs' and preferences' drag values made such fields (#485), Show The Home Screen When No Documents Are Open (#394), Numbers Without Units Are Points (#394), Auto-Collapse Iconic Panels (#394), the Contextual Task Bar moved by its handle and its More Options menu pinning and resetting its position, a designed macOS DMG window (#493), panels, panel groups and the Tools panel undocking into floating panels that move, stack and dock again (#495), the 0.5.0 startup crash on dual-GPU Linux desktops under Wayland fixed: the desktop's own GPU by default and a restart on the next GPU when one can't show the window (#502), path editing while drawing: Cmd/Ctrl with the Pen borrowing Direct Selection and drawing on afterwards, Pen+Alt reshaping segments, handles of a whole selected path dragging, and handle drags no longer re-gathering snap targets every move (#494), the Control bar's and Properties panel's Convert buttons for a path selected as a whole with Direct Selection, the Pen and the anchor tools (#504), the Pen closing a path dragged out of its first anchor every time, without flicker (#501); see [Honest assessment](#honest-assessment-2026-10-05))._ +_Last updated: 2026-10-08 (after M4.14–M4.98, M8.1–M8.20, M14.4–M14.7, the MCP prompts, resource templates, completions and logging, the 2026-10-07 issue fixes, the Layers panel rework and the 2026-10-08 community contributions: Pen and shape-tool modifiers, path editing, Layers, Artboards, Swatches/Brushes and dialogs, Japanese composition and PDF text, Hebrew/Arabic type, interface languages (Spanish among them), the font menu, saved selections, MCP and EPS, the missing-font export warning (#319), open paths in the Shape Builder (#374), PDF/`.ai` import keeping strokes live and hidden and nested layers (#372), per-corner Live Corners (#381), SVG type opening at the size it draws at (#396), the Wayland clipboard with copied-file paste (#398), ruler guides that can be selected, moved and deleted (#414), two batches of Preferences options that were stored but unused (#394), skeleton reads with `document.find` for large documents over MCP (#416) and anchors dragged with Direct Selection snapping to points, segments and smart guides, clipped SVG layers opening as clipping layers (#420), PDF/`.ai` layers read on pages of tens of millions of operators (#419), Document Color Mode putting RGB greys on the black plate on request and converting pattern tiles (#421) and movable dialogs with Offset Path's Joins dropdown (#423) and numeric fields scrubbed by dragging their label (#400), stroke weight presets in the stroke unit (#426), the Appearance panel's Opacity popup (#430), type on a path brackets, flip and Type on a Path Options that keep the type in place (#429), Layers rows dragged with a ghost and dropped on the trash or New Layer (#435), tool flyouts opening on a right or corner press (#436), tool flyouts torn off into floating toolbars (#437), the Shape Builder's erase mode deleting edges too (#439), installed fonts registered outside the font folders and fonts installed while the app runs listed in the font menus (#443), alike circular corners on rectangles that aren't square (#442), tool cursors as OS cursors so the pointer no longer trails over the canvas (#444), artboards copied and pasted with their art (#446), guides dragged out of the rulers snapping to the art's side midpoints and the artboards, artboard guides (#451), the toolbar's double arrow and Window › Toolbars › Double Column switching between one and two columns in any window height (#455), the Smart Guides display options and Snapping Tolerance (#394), Font Size and Leading combo boxes with preset lists in the Character and Properties panels and the Control bar (#458), screenshots pasted from the clipboard on macOS, Windows and the web (#457), the Properties panel's Fill and Stroke matching the Control bar's, Illustrator's Window menu panel keys (Shift+F7 for Align…) and Alt-wheel zoom whatever the wheel preference (Discord feedback), the point/area type widget (Discord feedback), groups in legacy Illustrator .ai files (Discord feedback), a rotating desktop log file (#465), image controls in the Control bar and Properties panel: Image Trace with its presets, Mask, Crop Image, Embed/Unembed, and an Image Trace object's preset, Expand and Release (Discord feedback), the Pen working as the Anchor Point tool with Alt held over a selected path's handles and anchors, and anchor buttons that convert, connect and cut paths at the selected anchors (Discord feedback), Japanese hanging punctuation (burasagari, #463), Escape leaving Presentation Mode with no screen mode saved across restarts, documents dropped on the desktop app opening as tabs of their own and a note that an `.ai` file's art off its artboards isn't in the part read (#472), Add/Delete Anchor Point tool icons drawn from the Pen tool's (#479), EPS files from Illustrator 2026, Inkscape/cairo, matplotlib, CorelDRAW, Affinity, LibreOffice and Ghostscript opening as vectors, and a preview that stands in saying why (Discord feedback, #466, #469, #474), chords with modifiers recorded in Edit › Keyboard Shortcuts (#487), Type Object Selection by Path Only (#394), Anti-aliased Artwork (#394), Show Pixel Grid (#394), pen and touch taps pressed and lifted within one frame clicking where they tapped and the Linux/Wayland pen-tablet workaround (#491), bounding-box resizes landing on the other objects' anchors and centres with labels, and the Rotate, Scale, Shear and Reflect tools snapping their drags (#482), Shift-drag marquees that toggle the objects or anchors they reach and a Lasso whose Alt subtraction keeps the rest of the selection (#483), the mouse wheel stepping the focused numeric field, the dialogs' and preferences' drag values made such fields (#485), Show The Home Screen When No Documents Are Open (#394), Numbers Without Units Are Points (#394), Auto-Collapse Iconic Panels (#394), the Contextual Task Bar moved by its handle and its More Options menu pinning and resetting its position, a designed macOS DMG window (#493), panels, panel groups and the Tools panel undocking into floating panels that move, stack and dock again (#495), the 0.5.0 startup crash on dual-GPU Linux desktops under Wayland fixed: the desktop's own GPU by default and a restart on the next GPU when one can't show the window (#502), path editing while drawing: Cmd/Ctrl with the Pen borrowing Direct Selection and drawing on afterwards, Pen+Alt reshaping segments, handles of a whole selected path dragging, and handle drags no longer re-gathering snap targets every move (#494), the Control bar's and Properties panel's Convert buttons for a path selected as a whole with Direct Selection, the Pen and the anchor tools (#504), the Pen closing a path dragged out of its first anchor every time, without flicker (#501), the Paste menu checking the system clipboard on a background thread on Linux so an X11 clipboard owner that never answers no longer freezes the window (#510); see [Honest assessment](#honest-assessment-2026-10-05))._ ## Where we are @@ -42,7 +42,7 @@ since 2026-10-01. | **Bundled content:** brush, symbol, style and swatch libraries | ~30% | Ours are original and generated in code, and far fewer than Illustrator ships; brush and symbol libraries are still missing | | **Performance** | unverified | Multithreaded, off-thread rendering and caches are in place. The last budget run (2026-10-01, loaded machine) measured 290 ms for a 50k-path fit against a 16 ms budget. Re-run `vectorcraft-cli perf` on an idle machine | | **Robustness** | good, new | ~2,840 tests, property tests, no panics in shipped code (lints, the `guard` safety net, import fuzzing), Data Recovery. Missing: a corpus of real-world files, Windows/Linux/browser QA | -| **Platforms and 1.0 polish** | ~60% | The macOS app and the web build work, and Finder opens every readable format with the macOS app (#295, #354); hybrid-graphics laptops render on the power-saving GPU by default on Windows and macOS (#306), and Linux on the GPU the desktop runs on, since a Wayland compositor may refuse frames from the other one; a window that fails on its GPU while starting up starts again on the next one instead of crashing (#502; the dual-GPU Wayland case is verified by unit tests and cross-compilation, not on such hardware); web drops land in the document they were dropped on (#359); desktop drops carry no pointer position (winit 0.30), so a dropped document opens as a tab of its own and a picture or text is placed in the middle of the view, while the web places anything dropped on the canvas and opens what is dropped off it, as Illustrator does (#472); a web page that loses its WebGPU device or WebGL context restarts its graphics with the same documents (#369); on Linux/Wayland the clipboard reads the compositor's own (data-control) and copied image files paste, but files can't be dropped on the window under Wayland until eframe moves to winit 0.31 (#398; the Wayland side is verified by compilation only); on KDE Plasma 6.3+ under Wayland, pens and pen displays reach the app only under XWayland (`WAYLAND_DISPLAY=`) or with KWin's `KWIN_WAYLAND_EMULATE_TABLET=1`, since winit 0.30 has no Wayland tablet input (#491, not verified on hardware); Windows/Linux packaging and accessibility are pending; an experimental, unsupported Windows 7 x64 OpenGL build exists (community-contributed, not verified by maintainers) | +| **Platforms and 1.0 polish** | ~60% | The macOS app and the web build work, and Finder opens every readable format with the macOS app (#295, #354); hybrid-graphics laptops render on the power-saving GPU by default on Windows and macOS (#306), and Linux on the GPU the desktop runs on, since a Wayland compositor may refuse frames from the other one; a window that fails on its GPU while starting up starts again on the next one instead of crashing (#502; the dual-GPU Wayland case is verified by unit tests and cross-compilation, not on such hardware); web drops land in the document they were dropped on (#359); desktop drops carry no pointer position (winit 0.30), so a dropped document opens as a tab of its own and a picture or text is placed in the middle of the view, while the web places anything dropped on the canvas and opens what is dropped off it, as Illustrator does (#472); a web page that loses its WebGPU device or WebGL context restarts its graphics with the same documents (#369); on Linux/Wayland the clipboard reads the compositor's own (data-control) and copied image files paste, but files can't be dropped on the window under Wayland until eframe moves to winit 0.31 (#398; the Wayland side is verified by compilation only); on Linux the Paste menu checks the system clipboard on a background thread, so a clipboard owner that never answers no longer freezes the window, while Windows and macOS keep their quick in-line check (#510; the X11 hang verified by the contributor, here by headless tests); on KDE Plasma 6.3+ under Wayland, pens and pen displays reach the app only under XWayland (`WAYLAND_DISPLAY=`) or with KWin's `KWIN_WAYLAND_EMULATE_TABLET=1`, since winit 0.30 has no Wayland tablet input (#491, not verified on hardware); Windows/Linux packaging and accessibility are pending; an experimental, unsupported Windows 7 x64 OpenGL build exists (community-contributed, not verified by maintainers) | | **Agent automation** | beyond Illustrator | Every command, gesture and dialog is drivable over MCP, the CLI and the control channel | | **Scrubby numeric fields** | beyond Illustrator | As in Photoshop, dragging a numeric field's label (`W:`, `Opacity:`, the Character panel's icons) left or right steps its value live; one undo step per drag, Escape cancels (#400) | @@ -156,7 +156,7 @@ grade by behaviour against `plan/illustrator/`, not by whether a menu item exist - `.vectorcraft` (lossless JSON, compressed, atomic saves, save down to v1), Save As with a format chooser, Save a Copy, Revert, templates, Data Recovery after a crash (browser tabs hold their copies with Web Locks too, so a background tab's copies are never taken for a crash's, and a tab repairs copies another removed when it resumes: #367), background save and export. The desktop app writes its warnings and errors (graphics adapter, a lost graphics device, panics the guard recovered from) to standard error and to `logs/vectorcraft.log` next to the preferences, the two previous runs kept and each file capped at 16 MiB, so a launch without a terminal still leaves something to attach to a bug report (#465). - SVG/SVGZ in and out with SVG Options, Preserve Editing, symbols, filters, rich text and physical units (SVG type under a transform or `viewBox` scale opens at the size it draws at, the scale moved into its size, leading, baseline shift and strokes, a stretch, skew or rotation kept as its transform: #396; a top-level group with a clip path, such as a clipped Inkscape layer, opens as a layer with a clipping mask, its name kept: #420); PDF and PDF-compatible `.ai` in and out (layers, with hidden ones kept hidden with their art and nested ones as sublayers, on pages of up to 64 million operators, art whose layer can't be told going to a hidden, non-printing layer rather than showing or printing: #419; strokes kept live, a fill and a stroke of one outline as one object; masks, editable text with type set along a curve coming back as type on a path, each artboard's art on its own page only, an `.ai`'s white page under its layers left out rather than coming in as art, spot colours, presets, security, marks and bleed, ICC output intents, raster effects, subset fonts, PDF/X, PDF layers, overprint, page thumbnails, fast web view, PDF 1.3). CMYK images keep their ink amounts from PDF import (and from placed CMYK TIFFs) to PDF export: a CMYK JPEG passes through unchanged, others stay CMYK (DeviceCMYK, or ICC-based CMYK in PDF/X-3/X-4) unless the Output settings convert them; the canvas shows them through the working CMYK profile (#320). - EPS and DXF in and out, EMF/WMF in and out; PNG (with PNG-8), JPEG, WebP, GIF, TIFF, BMP, Targa and layered PSD export. EPS import runs Illustrator's AGM/CoolType prologs (resource categories, `resourceforall`, `clipsave`/`cliprestore`, subarrays and substrings that share storage, the standard dictionaries in `systemdict`, `dictstack`, `internaldict`, executable filters, `cshow`, metadata skipped with `flushfile`), so Illustrator 30 EPS files open as vectors. It also runs what other generators write (Discord feedback, #466, #469): Illustrator 2026's procsets (`gcheck`, `rootfont`, `currentcolorrendering`: every Illustrator 2026 EPS fell back to its preview before), cairo/Inkscape (`status`/`flushfile`, images with a mask, `ImageType 3`, reusable streams), matplotlib's Type 3 fonts, Illustrator 8 EPS (stock art's usual format: executable strings such as its gradients' `ed`, `load` by integer keys, #474), Ghostscript's `//name` immediate names, `pdfmark`, `token`, `execform` and user paths. Tiling patterns come in as pattern swatches, uncoloured ones too; Type 3 fonts' glyphs as their outlines, spaced by `ashow`/`widthshow`/`xshow`/`kshow`; mesh and function-based shadings (types 1, 4–7) as gradient meshes; sampled functions (type 0) and Flate/LZW predictors are read. When a file's PostScript still can't be read, it opens as its TIFF preview, its Windows metafile preview (as vectors) or its EPSI preview, and the warning (in the status bar, `document.open`/`app.open`/MCP `open_file` replies and `vectorcraft-cli info`) names the error, the operator that raised it and the procedures it ran in. Legacy PostScript `.ai` files (Illustrator 3–8, written with the prolog that defines their operators) keep their groups and nested groups (`u` … `U`), between and inside clipping groups; EPS and EMF/WMF import nest clips (and groups) at most 128 deep, so a hostile file can't exhaust the stack. DXF TEXT justified Fit or Aligned spans its two points (stretched, or scaled as a whole). - - Place and the Links panel, Package, File Info, Print with print presets, PostScript output and print tiling, slices and Save for Web, Export for Screens and Asset Export, CSS Properties, PNG/PDF/SVG/text clipboard flavours; files copied in a file manager paste as art (SVG, PDF, EMF/WMF, bitmaps) on every desktop platform, and Linux/Wayland reads the compositor's clipboard (#398); screenshots and other copied pictures paste as embedded images at the view centre everywhere, the web included (#457). + - Place and the Links panel, Package, File Info, Print with print presets, PostScript output and print tiling, slices and Save for Web, Export for Screens and Asset Export, CSS Properties, PNG/PDF/SVG/text clipboard flavours; files copied in a file manager paste as art (SVG, PDF, EMF/WMF, bitmaps) on every desktop platform, and Linux/Wayland reads the compositor's clipboard (#398); on Linux the Paste menu checks the clipboard off the UI thread, so an X11 owner that never answers can't freeze the window (#510); screenshots and other copied pictures paste as embedded images at the view centre everywhere, the web included (#457). - **Performance:** 20k shapes + 1k texts render in 27 ms per full-retina frame (7.8 ms zoomed), 7× faster than the first version. The UI thread never blocks. The web build is 7.1 MB gzipped. - **Graphics processor:** the canvas is rasterized on the CPU and the GPU only composites it, so any GPU that can show the window will do. Preferences › Performance › Graphics Processor (`gpuPreference`) is Automatic by default: the power-saving GPU on Windows and macOS (the integrated one on hybrid-graphics laptops, where presenting from the discrete GPU made the window flicker, #306), and the GPU the desktop runs on under Linux, where KWin on an NVIDIA GPU refused frames from a Ryzen's integrated GPU and 0.5.0 crashed at startup (#502). Power Saving and High Performance pick a kind of GPU; `WGPU_POWER_PREF`, `WGPU_ADAPTER_NAME` and `WGPU_BACKEND` override them. The app picks among the adapters that can present to the window (native backends before OpenGL, hardware before software), and if the window fails on one while starting up it starts again without it instead of crashing; Help › About, `ui.inspect` and the log name the adapter in use. When the browser drops the page's WebGPU device or WebGL context, the web build writes recovery copies, starts again on new graphics with the same app and uploads its textures again; if it can't, it removes the stale canvas and says to reload, which offers the copies back. The desktop app writes the copies and says to restart (#369). On Windows and Linux the tools' cursors are OS cursors (bitmaps rasterized once per display scale), which the system moves itself: painted into the window, they trailed the pointer by the frames waiting to be shown, and the window now queues one frame instead of two (#444). macOS and the web still paint them (macOS sizes cursor bitmaps in points, eframe has no bitmap cursors on the web). Pens and touch drive the tools as the mouse does, and a tap pressed and lifted within one frame clicks where it tapped (#491); pen pressure reaches the Liquify tools' Use Pressure Pen on Windows, while winit 0.30 reports none on macOS or Linux and egui none on the web. On Linux, see `docs/development.md` for pens under Wayland. - **Tests and robustness:** ~2,840 automated tests: model-based property tests, a junk-parameter sweep over every command, import fuzzing (SVG, PDF, libraries), golden renders, and MCP end-to-end tests over stdio. Shipped code never panics (workspace lints and a rollback safety net; see `docs/development.md`). diff --git a/apps/vectorcraft/src/main.rs b/apps/vectorcraft/src/main.rs index 3ffee76c..64fc2049 100644 --- a/apps/vectorcraft/src/main.rs +++ b/apps/vectorcraft/src/main.rs @@ -29,7 +29,7 @@ mod window; use vectorcraft_engine::Session; use vectorcraft_engine::cmd::fileio; use vectorcraft_ui_egui::graphics::GraphicsLoss; -use vectorcraft_ui_egui::{FilePick, Services, VectorcraftApp}; +use vectorcraft_ui_egui::{ClipboardProbeFactory, FilePick, Services, VectorcraftApp}; struct App { app: VectorcraftApp, @@ -256,6 +256,10 @@ fn services() -> Services { write_shared: Some(std::sync::Arc::new(write_file)), // Every format Copy offers and Paste reads (menu-bar Paste never sees egui's Paste event). system_clipboard: Some(clipboard::system_clipboard()), + // Linux checks whether Paste has something to take on a background thread: an X11 clipboard + // owner that never answers holds a read for up to 4 s. Windows only asks which formats the + // clipboard holds and macOS asks the pasteboard server, so they check in line. + clipboard_probe: cfg!(target_os = "linux").then(|| Box::new(clipboard::system_clipboard) as ClipboardProbeFactory), // Help → Discord / website / GitHub, the Discord button, About and Home links. open_url: Some(Box::new(|url: &str| { let _ = webbrowser::open(url); diff --git a/crates/ui-egui/src/clipboard_probe.rs b/crates/ui-egui/src/clipboard_probe.rs new file mode 100644 index 00000000..902024ba --- /dev/null +++ b/crates/ui-egui/src/clipboard_probe.rs @@ -0,0 +1,99 @@ +//! Checks the system clipboard for Paste off the UI thread. `has()` can block for seconds when the +//! clipboard owner never answers (an unresponsive X11 client), so it must not run on the thread that +//! draws the window: a worker owns a second clipboard handle, sends its verdict when it changes, and +//! the UI only reads the latest one. Without a worker the check stays in line, as before (the web, +//! and hosts that don't install a factory). + +use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender, TryRecvError}; +use std::time::{Duration, Instant}; + +use vectorcraft_engine::cmd::clipboard::PASTE_ORDER; + +use crate::sysclip::ClipboardProbeFactory; + +/// How often the worker looks at the clipboard while the UI wants to know. +const PROBE_INTERVAL: Duration = Duration::from_millis(250); + +/// How long dropping the probe waits for the worker to let go of its clipboard handle. +const STOP_WAIT: Duration = Duration::from_millis(250); + +/// The background clipboard check ([`crate::VectorcraftApp`]'s `clipboard_probe`). +pub(crate) struct Probe { + /// Tells the worker whether Paste depends on the system clipboard; dropped to stop it. + wants: Option>, + wanted: bool, + verdicts: Receiver, + /// The worker's last verdict. + last: bool, +} + +impl Probe { + /// Start the worker. `None` on wasm, or when the thread can't be spawned: the caller then falls + /// back to the in-line check. + pub(crate) fn start(make: ClipboardProbeFactory, ctx: egui::Context) -> Option { + if cfg!(target_arch = "wasm32") { + return None; + } + let (wants, wanted_rx) = std::sync::mpsc::channel(); + let (tx, verdicts) = std::sync::mpsc::channel(); + std::thread::Builder::new() + .name("vectorcraft-clip".into()) + .spawn(move || { + let mut clipboard = make(); + let (mut wanted, mut sent) = (false, None); + loop { + if wanted { + // This thread may wait here for a slow owner; the UI's never does. + let verdict = clipboard.has(&PASTE_ORDER); + if sent != Some(verdict) { + if tx.send(verdict).is_err() { + break; + } + sent = Some(verdict); + ctx.request_repaint(); + } + } + // Sleep until the next look (no looks while unwanted) or the UI's next word. + let next = + if wanted { wanted_rx.recv_timeout(PROBE_INTERVAL) } else { wanted_rx.recv().map_err(|_| RecvTimeoutError::Disconnected) }; + match next { + Ok(w) => wanted = w, + Err(RecvTimeoutError::Timeout) => {} + Err(RecvTimeoutError::Disconnected) => break, + } + } + // The handle goes before the app stops waiting (`Drop`): on X11 the last one alive + // hands what the app copied to the clipboard manager, which must finish before exit. + drop(clipboard); + drop(tx); + }) + .ok()?; + Some(Self { wants: Some(wants), wanted: false, verdicts, last: false }) + } + + /// Paste can take from the system clipboard: `wanted` (nothing copied in the app, a document + /// open) and the worker's latest verdict says so. Never blocks. `None` once the worker is gone. + pub(crate) fn pasteable(&mut self, wanted: bool) -> Option { + if wanted != self.wanted { + self.wants.as_ref()?.send(wanted).ok()?; + self.wanted = wanted; + } + loop { + match self.verdicts.try_recv() { + Ok(verdict) => self.last = verdict, + Err(TryRecvError::Empty) => return Some(wanted && self.last), + Err(TryRecvError::Disconnected) => return None, + } + } + } +} + +impl Drop for Probe { + /// Wake the worker to stop and wait (a moment at most: it may be stuck on an owner that never + /// answers) until it has let go of its clipboard handle. + fn drop(&mut self) { + self.wants = None; + let end = Instant::now() + STOP_WAIT; + while self.verdicts.recv_timeout(end.saturating_duration_since(Instant::now())).is_ok() {} + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 08f05485..8cf17d93 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -17,6 +17,7 @@ pub mod background; mod brand; pub mod canvas; pub mod chrome; +mod clipboard_probe; pub mod community; pub mod control; pub mod credits; @@ -129,6 +130,8 @@ mod tests_sysclip; #[cfg(test)] mod tests_sysclip_emf; #[cfg(test)] +mod tests_sysclip_probe; +#[cfg(test)] mod tests_transparencygrid; #[cfg(test)] mod tests_widthtool; @@ -142,7 +145,7 @@ use vectorcraft_engine::{Session, ViewInfo}; pub use control::{ControlRequest, ControlResponse}; pub use state::{UiState, View}; -pub use sysclip::SystemClipboard; +pub use sysclip::{ClipboardProbeFactory, SystemClipboard}; /// What a file dialog shows: a suggested file name (save dialogs), the folder to start in and the /// file-type filters. @@ -205,6 +208,10 @@ pub struct Services { /// Paste takes SVG, PDF, text and bitmaps from other apps. Without it, SVG text only (through /// egui and `clipboard_read`). pub system_clipboard: Option>, + /// A second system-clipboard handle, for checking whether Paste has something to take on a + /// background thread (Linux: an X11 clipboard owner that never answers would freeze the UI). + /// Taken on the first frame. Without it the check runs on the UI thread. + pub clipboard_probe: Option, /// File → Show in Folder: select a file in the system file manager (desktop). pub reveal: Option, /// Write a file from any thread (desktop): lets Background Save and Export write off the UI @@ -327,8 +334,11 @@ pub struct VectorcraftApp { /// (SVG without a `system_clipboard`) while the internal clipboard is empty. It enables the /// Paste menu items. pub(crate) system_paste: bool, - /// When `system_paste` was last checked (app time, s; at most once per frame). + /// When `system_paste` was last checked in line (app time, s; at most once per frame). system_paste_at: f64, + /// The thread that checks the system clipboard for `system_paste` instead + /// ([`Services::clipboard_probe`]). + clipboard_probe: Option, /// Keyboard pastes of something other than text (see [`shortcuts::PasteChord`]). pub(crate) paste_chord: shortcuts::PasteChord, /// Saves and exports running in the background (Preferences → File Handling). @@ -404,6 +414,7 @@ impl VectorcraftApp { place: Default::default(), system_paste: false, system_paste_at: f64::NEG_INFINITY, + clipboard_probe: None, paste_chord: Default::default(), background: Default::default(), recovery: Default::default(), @@ -729,11 +740,20 @@ impl VectorcraftApp { } self.last_time = now; self.sync_views(); - // Read the system clipboard only when that alone decides whether Paste is enabled, and at - // most a few times a second (opening it locks it against other apps on some systems). - if !(0.0..SYSTEM_CLIPBOARD_POLL).contains(&(now - self.system_paste_at)) { + // Read the system clipboard only when that alone decides whether Paste is enabled. A + // background thread reads it where the host installs one (an unresponsive owner must never + // stall the frame loop); otherwise, or once that thread is gone, it is read here, at most a + // few times a second (opening it locks it against other apps on some systems). + let wanted = self.session.clipboard.is_empty() && self.session.active().is_some(); + if let Some(make) = self.services.clipboard_probe.take() { + self.clipboard_probe = clipboard_probe::Probe::start(make, ctx.clone()); + } + if let Some(pasteable) = self.clipboard_probe.as_mut().and_then(|p| p.pasteable(wanted)) { + self.system_paste = pasteable; + } else if !(0.0..SYSTEM_CLIPBOARD_POLL).contains(&(now - self.system_paste_at)) { + self.clipboard_probe = None; self.system_paste_at = now; - self.system_paste = self.session.clipboard.is_empty() && self.session.active().is_some() && self.system_clipboard_pasteable(); + self.system_paste = wanted && self.system_clipboard_pasteable(); } background::poll(self); if !self.background.jobs.is_empty() { diff --git a/crates/ui-egui/src/sysclip.rs b/crates/ui-egui/src/sysclip.rs index a63cd5df..aefd3b86 100644 --- a/crates/ui-egui/src/sysclip.rs +++ b/crates/ui-egui/src/sysclip.rs @@ -24,6 +24,11 @@ pub trait SystemClipboard { fn has(&mut self, mimes: &[&'static str]) -> bool; } +/// Makes a second system-clipboard handle, to check off the UI thread whether Paste has something +/// to take ([`Services::clipboard_probe`](crate::Services::clipboard_probe)). It is called on that +/// thread and the handle lives and dies there, so the handle needn't be `Send`. +pub type ClipboardProbeFactory = Box Box + Send>; + /// The command (and its params) that loads `f` into the internal clipboard, centred on `center`. pub(crate) fn import_command(f: &Flavour, center: Option<[f64; 2]>) -> (&'static str, Value) { let b64 = || vectorcraft_format::base64_encode(&f.data); diff --git a/crates/ui-egui/src/tests_sysclip.rs b/crates/ui-egui/src/tests_sysclip.rs index 854d6a49..3b793843 100644 --- a/crates/ui-egui/src/tests_sysclip.rs +++ b/crates/ui-egui/src/tests_sysclip.rs @@ -66,7 +66,7 @@ pub(super) fn run(app: &mut VectorcraftApp, id: &str, p: Value) -> Value { app.run(id, p).unwrap_or_else(|e| panic!("{id}: {e}")) } -fn copy_rect(app: &mut VectorcraftApp) { +pub(super) fn copy_rect(app: &mut VectorcraftApp) { let id = run(app, "shape.rectangle", json!({"x": 10, "y": 10, "width": 40, "height": 20}))["id"].clone(); run(app, "paint.setFill", json!({"color": "#ff0000"})); run(app, "select.set", json!({"ids": [id]})); diff --git a/crates/ui-egui/src/tests_sysclip_probe.rs b/crates/ui-egui/src/tests_sysclip_probe.rs new file mode 100644 index 00000000..e09ba3a0 --- /dev/null +++ b/crates/ui-egui/src/tests_sysclip_probe.rs @@ -0,0 +1,156 @@ +//! The system clipboard is checked for Paste off the UI thread where the host installs a probe +//! (Linux): an X11 clipboard owner that never answers used to block the frame loop for seconds, so +//! the window stopped drawing and could not even be closed. The in-line check (no probe: the web, +//! Windows, macOS) is covered by `tests_clipboard` and `tests_sysclip`. + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use serde_json::json; +use vectorcraft_engine::Session; +use vectorcraft_engine::cmd::clipboard::Flavour; + +use crate::tests_sysclip::{copy_rect, run}; +use crate::{Services, SystemClipboard, VectorcraftApp, menus}; + +/// What the clipboard handles saw: the thread each `has()` ran on, and how many handles are alive. +#[derive(Default)] +struct Recorder { + threads: Mutex>>, + alive: AtomicUsize, +} + +impl Recorder { + fn looks(&self) -> usize { + self.threads.lock().unwrap().len() + } +} + +/// A system clipboard whose `has()` takes `delay` (an owner slow to answer) and answers `verdict`, +/// or panics without one (a bug in the platform code). +struct Slow { + rec: Arc, + delay: Duration, + verdict: Option, +} + +impl Slow { + fn boxed(rec: &Arc, delay: Duration, verdict: Option) -> Box { + rec.alive.fetch_add(1, Ordering::SeqCst); + Box::new(Self { rec: Arc::clone(rec), delay, verdict }) + } +} + +impl Drop for Slow { + fn drop(&mut self) { + self.rec.alive.fetch_sub(1, Ordering::SeqCst); + } +} + +impl SystemClipboard for Slow { + fn write(&mut self, _flavours: &[Flavour]) -> Result<(), String> { + Ok(()) + } + fn holds_ours(&mut self) -> bool { + false + } + fn read(&mut self, _mimes: &[&'static str]) -> Option { + None + } + fn has(&mut self, _mimes: &[&'static str]) -> bool { + self.rec.threads.lock().unwrap().push(std::thread::current().name().map(str::to_owned)); + std::thread::sleep(self.delay); + self.verdict.expect("the clipboard failed") + } +} + +/// An app whose two clipboard handles record into `rec`: the UI's says `inline`, the probe's +/// `probe`, each after `delay`. +fn app(rec: &Arc, delay: Duration, inline: bool, probe: Option) -> VectorcraftApp { + let make = { + let rec = Arc::clone(rec); + move || Slow::boxed(&rec, delay, probe) + }; + let services = + Services { system_clipboard: Some(Slow::boxed(rec, delay, Some(inline))), clipboard_probe: Some(Box::new(make)), ..Default::default() }; + VectorcraftApp::new(Session::new(), services) +} + +/// One headless frame of app logic in the test's one context (a new one would restart the fonts), +/// half a second after the last: past the in-line check's throttle, so it would run every frame. +fn frame(app: &mut VectorcraftApp) { + thread_local! { + static CTX: egui::Context = egui::Context::default(); + static NOW: std::cell::Cell = const { std::cell::Cell::new(0.0) }; + } + let time = NOW.with(|t| { + t.set(t.get() + 0.5); + t.get() + }); + let input = egui::RawInput { time: Some(time), ..Default::default() }; + CTX.with(|ctx| ctx.run_ui(input, |ui| app.logic(ui.ctx())).textures_delta.clear()); +} + +/// Frames for `wait`, or until `done` holds; whether it does. +fn frames_until(app: &mut VectorcraftApp, wait: Duration, done: impl Fn(&VectorcraftApp) -> bool) -> bool { + let end = Instant::now() + wait; + loop { + frame(app); + if done(app) || Instant::now() > end { + return done(app); + } + std::thread::sleep(Duration::from_millis(5)); + } +} + +#[test] +fn a_slow_clipboard_is_read_off_the_ui_thread() { + let rec = Arc::new(Recorder::default()); + let mut app = app(&rec, Duration::from_secs(1), false, Some(true)); + // The first frames install the UI fonts. Without a document Paste is off whatever the clipboard + // holds: nobody looks. + assert!(!frames_until(&mut app, Duration::from_millis(100), |_| rec.looks() > 0)); + run(&mut app, "file.new", json!({"width": 100, "height": 100})); + frame(&mut app); + let t0 = Instant::now(); + frame(&mut app); + assert!(t0.elapsed() < Duration::from_millis(300), "the frame waited on the clipboard: {:?}", t0.elapsed()); + // The probe's verdict (the UI's own handle would say no) enables Paste once it arrives. + assert!(frames_until(&mut app, Duration::from_secs(3), |app| app.system_paste), "the probe's verdict never arrived"); + assert!(menus::enabled(&app, "edit.paste")); + let threads = rec.threads.lock().unwrap().clone(); + assert!(threads.iter().all(|n| n.as_deref() == Some("vectorcraft-clip")), "checked on: {threads:?}"); +} + +#[test] +fn the_probe_rests_after_a_copy_and_stops_with_the_app() { + let rec = Arc::new(Recorder::default()); + let mut app = app(&rec, Duration::ZERO, false, Some(true)); + run(&mut app, "file.new", json!({"width": 100, "height": 100})); + assert!(frames_until(&mut app, Duration::from_secs(2), |app| app.system_paste)); + // Something copied in the app enables Paste alone: the clipboard isn't read any more. + copy_rect(&mut app); + frames_until(&mut app, Duration::from_millis(50), |_| false); + let looks = rec.looks(); + frames_until(&mut app, Duration::from_millis(600), |_| false); + assert_eq!(rec.looks(), looks); + assert!(!app.system_paste && menus::enabled(&app, "edit.paste")); + // Quitting waits until the probe's thread has let go of its handle: on X11 the last handle + // alive hands what the app copied to the clipboard manager. + assert_eq!(rec.alive.load(Ordering::SeqCst), 2); + drop(app); + assert_eq!(rec.alive.load(Ordering::SeqCst), 0, "the probe's thread kept its clipboard"); +} + +#[test] +fn without_its_thread_the_check_runs_in_line_again() { + let rec = Arc::new(Recorder::default()); + // The probe's clipboard panics: its thread ends. + let mut app = app(&rec, Duration::ZERO, true, None); + run(&mut app, "file.new", json!({"width": 100, "height": 100})); + assert!(frames_until(&mut app, Duration::from_secs(2), |app| app.system_paste), "no fallback"); + assert!(app.clipboard_probe.is_none()); + let threads = rec.threads.lock().unwrap().clone(); + assert_eq!(threads.first().cloned().flatten().as_deref(), Some("vectorcraft-clip"), "{threads:?}"); +} diff --git a/docs/development.md b/docs/development.md index 4bea9821..5f9487af 100644 --- a/docs/development.md +++ b/docs/development.md @@ -74,6 +74,7 @@ By default VectorCraft's own crates log at `info` and everything else at `warn`. The window runs natively on Wayland (eframe's `wayland` feature) and on X11. On machines with two GPUs it renders on the one the desktop runs on unless Preferences say otherwise; see [Desktop graphics processor](#desktop-graphics-processor) (#502). The system clipboard (`apps/vectorcraft/src/clipboard.rs`) is arboard with its `wayland-data-control` feature: - **Clipboard:** under Wayland arboard talks to the compositor through the data-control protocol (wlroots compositors such as Sway and Hyprland, KDE Plasma), so bitmaps (`image/png`), SVG markup and text copied in any app paste (#398). Where the compositor lacks the protocol (GNOME's Mutter, [arboard#223](https://github.com/1Password/arboard/issues/223)), arboard falls back to the X11 clipboard through XWayland, which only holds what X11 apps copied. egui's own text paste into fields goes through smithay-clipboard and works either way. +- **Clipboard owners that never answer (#510):** reading the clipboard waits on the app that owns it: arboard gives up on an X11 owner after 4 s, and a Wayland read waits for the owner to send its data. So on Linux the check that enables the Paste menu items runs on a background thread with a second clipboard handle (`crates/ui-egui/src/clipboard_probe.rs`, installed as `Services::clipboard_probe`), only while nothing is copied in the app and a document is open, and the window keeps drawing. A paste itself still reads on the UI thread, so pasting from such an owner waits for it once. Windows and macOS keep the check on the UI thread: Windows only asks which formats the clipboard holds, macOS asks the pasteboard server. - **Copied files:** files copied in a file manager (`text/uri-list`; `CF_HDROP` on Windows, file URLs on macOS) paste as the first one that is art (SVG, PDF, EMF/WMF or a bitmap), before the clipboard's other formats (which include the files' paths as text). - **Dropping files on the window does not work under Wayland:** winit 0.30, which eframe 0.36 runs on, has no Wayland drag and drop ([winit#1881](https://github.com/rust-windowing/winit/issues/1881), added in winit 0.31; [egui#1563](https://github.com/emilk/egui/issues/1563)), so no `dropped_files` arrive. Until eframe moves to winit 0.31: copy the files in the file manager and paste them, use File › Place or Open, or run the app under XWayland, where drops work (`WAYLAND_DISPLAY= vectorcraft`). - **Pens and pen displays under Wayland (#491):** winit 0.30 doesn't bind the Wayland tablet protocol (`zwp_tablet_v2`; tablet input arrives in winit 0.31, [winit#4318](https://github.com/rust-windowing/winit/pull/4318), [egui#7731](https://github.com/emilk/egui/pull/7731)). Since Plasma 6.3, KWin no longer turns tablet input into pointer input for such apps ([kwin!6336](https://invent.kde.org/plasma/kwin/-/merge_requests/6336)): the pen moves the compositor's cursor over the window, but the app gets no motion or presses and shows none of its own cursors. Until eframe moves to winit 0.31, either run the app under XWayland, which supports the tablet protocol and hands the pen to X11 apps as a pointer (`WAYLAND_DISPLAY= vectorcraft`, or `env -u WAYLAND_DISPLAY vectorcraft`; for a desktop launcher, put `env WAYLAND_DISPLAY=` before the command in its `Exec=` line), or turn KWin's deprecated emulation back on for the whole session by setting `KWIN_WAYLAND_EMULATE_TABLET=1` in KWin's environment (for example in a file under `~/.config/environment.d/` or in `/etc/environment`) and logging in again. The pen then works as a mouse, without pressure: winit 0.30 reads pen pressure on Windows only (Windows Ink, which reaches the Liquify tools' Use Pressure Pen). Other compositors that don't emulate a pointer for tablets behave the same way.