mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 14:17:14 +00:00
Clipboard: Edit > Paste is enabled when the clipboard holds only copied files
On X11 and the Wayland data-control clipboard (through arboard), the Paste menu check read only the clipboard's text, so a file manager copy with no text left Edit > Paste disabled while the paste keys pasted the first copied file that is art. After a text miss, the check now reads the copied file list when the paste can take files, without opening a file. Copied files already enabled Paste through the core Wayland check and the Windows check. A paste of text alone does not read the list.
This commit is contained in:
@@ -155,6 +155,12 @@ fn copied_file(paths: Vec<PathBuf>, mimes: &[&'static str]) -> Option<Flavour> {
|
||||
copied_file_within(paths, mimes, MAX_BYTES)
|
||||
}
|
||||
|
||||
/// Whether a paste of one of `mimes` can take a copied file: one of them is not text.
|
||||
#[cfg_attr(windows, allow(dead_code))]
|
||||
fn takes_files(mimes: &[&'static str]) -> bool {
|
||||
mimes.iter().any(|&m| m != TEXT)
|
||||
}
|
||||
|
||||
/// [`copied_file`] with a file of more than `limit` bytes skipped.
|
||||
fn copied_file_within(paths: Vec<PathBuf>, mimes: &[&'static str], limit: usize) -> Option<Flavour> {
|
||||
paths.into_iter().find_map(|path| {
|
||||
@@ -727,7 +733,7 @@ mod portable {
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use super::{BITMAP, ClipboardRead, Flavour, PNG, SystemClipboard, TEXT, copied_file, decode_png, png_flavour, text_of};
|
||||
use super::{BITMAP, ClipboardRead, Flavour, PNG, SystemClipboard, TEXT, copied_file, decode_png, png_flavour, takes_files, text_of};
|
||||
|
||||
/// What our last write put on the clipboard.
|
||||
#[derive(Clone)]
|
||||
@@ -741,7 +747,7 @@ mod portable {
|
||||
}
|
||||
|
||||
/// A read of arboard's clipboard.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
enum Request {
|
||||
Text,
|
||||
Image,
|
||||
@@ -813,6 +819,19 @@ mod portable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a paste of one of `mimes` can take what `cb` holds: text that is not empty when one
|
||||
/// of `mimes` is text (`text` reads it), else files copied in a file manager when one of `mimes`
|
||||
/// is not text (`files` reads the list). The list is read only when the text doesn't do, and no
|
||||
/// copied file is opened: Paste checks the files themselves.
|
||||
fn arboard_has<B>(
|
||||
mimes: &[&'static str],
|
||||
cb: &mut B,
|
||||
text: impl FnOnce(&mut B) -> Result<String, String>,
|
||||
files: impl FnOnce(&mut B) -> Vec<PathBuf>,
|
||||
) -> bool {
|
||||
(mimes.contains(&TEXT) && text(cb).is_ok_and(|t| !t.is_empty())) || (takes_files(mimes) && !files(cb).is_empty())
|
||||
}
|
||||
|
||||
/// `request` read on `reads`' worker, waiting `wait` at most.
|
||||
#[cfg(test)]
|
||||
fn read_within(reads: &mut super::loads::Loads<Request, Answer>, request: Request, wait: Duration) -> Answer {
|
||||
@@ -1441,10 +1460,11 @@ mod portable {
|
||||
read
|
||||
}
|
||||
|
||||
/// Text, on macOS vector art or a bitmap (by the pasteboard's types), and on the Wayland
|
||||
/// core protocol copied files, SVG, text or PNG (by the types offered, nothing read).
|
||||
/// Text or files copied in a file manager (their list read, no file opened:
|
||||
/// [`arboard_has`]), on macOS vector art or a bitmap (by the pasteboard's types), and on the
|
||||
/// Wayland core protocol copied files, SVG, text or PNG (by the types offered, nothing read).
|
||||
/// arboard has no cheap way to ask for a bitmap (reading one costs as much as pasting it):
|
||||
/// the paste keys take it, the menu item waits for text.
|
||||
/// the paste keys take it, the menu item waits for text or copied files.
|
||||
fn has(&mut self, mimes: &[&'static str]) -> bool {
|
||||
#[cfg(target_os = "macos")]
|
||||
if super::mac::has(mimes) {
|
||||
@@ -1455,7 +1475,7 @@ mod portable {
|
||||
if let Backend::Wayland(wl) = cb {
|
||||
return wl.has(mimes);
|
||||
}
|
||||
mimes.contains(&TEXT) && cb.get_text().is_ok_and(|t| !t.is_empty())
|
||||
arboard_has(mimes, cb, Backend::get_text, Backend::file_list)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1619,6 +1639,40 @@ mod portable {
|
||||
assert!(Answer::Text(Ok("art.svg".into())).files().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paste_is_available_for_files_copied_in_a_file_manager() {
|
||||
use vectorcraft_engine::cmd::clipboard::{PASTE_ORDER, SVG};
|
||||
/// What the clipboard holds, and which reads [`arboard_has`] made.
|
||||
struct Held {
|
||||
text: Option<String>,
|
||||
files: Vec<PathBuf>,
|
||||
reads: Vec<Request>,
|
||||
}
|
||||
fn has(mimes: &[&'static str], text: Option<&str>, files: &[&str]) -> (bool, Vec<Request>) {
|
||||
let mut held = Held { text: text.map(String::from), files: files.iter().map(PathBuf::from).collect(), reads: vec![] };
|
||||
let text = |h: &mut Held| {
|
||||
h.reads.push(Request::Text);
|
||||
h.text.clone().ok_or_else(|| "no text".to_string())
|
||||
};
|
||||
let files = |h: &mut Held| {
|
||||
h.reads.push(Request::Files);
|
||||
h.files.clone()
|
||||
};
|
||||
let has = arboard_has(mimes, &mut held, text, files);
|
||||
(has, held.reads)
|
||||
}
|
||||
// Files a file manager copied, with no text: the Paste menu item is available.
|
||||
assert_eq!(has(&PASTE_ORDER, None, &["/tmp/art.svg"]), (true, vec![Request::Text, Request::Files]));
|
||||
assert_eq!(has(&PASTE_ORDER, Some(""), &["/tmp/art.svg"]), (true, vec![Request::Text, Request::Files]));
|
||||
assert_eq!(has(&[SVG], None, &["/tmp/art.svg"]), (true, vec![Request::Files]));
|
||||
// Text does, and the list is not read.
|
||||
assert_eq!(has(&PASTE_ORDER, Some("hello"), &[]), (true, vec![Request::Text]));
|
||||
// An empty list and no text: nothing to paste.
|
||||
assert_eq!(has(&PASTE_ORDER, None, &[]), (false, vec![Request::Text, Request::Files]));
|
||||
// A paste of text alone can't take copied files, and their list is not read.
|
||||
assert_eq!(has(&[TEXT], None, &["/tmp/art.svg"]), (false, vec![Request::Text]));
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))]
|
||||
#[test]
|
||||
fn arboards_reads_run_on_its_reader_and_wait_by_kind() {
|
||||
@@ -1925,7 +1979,7 @@ mod wayland {
|
||||
use wayland_client::{Connection, Dispatch, Proxy, QueueHandle, WEnum};
|
||||
|
||||
use super::loads::{Asker, GONE, LOAD_WAIT, Loads, wait_for_answer};
|
||||
use super::{BITMAP, Flavour, MAX_BYTES, PNG, PathBuf, SVG, TEXT, copied_file, gnome_copied_paths, uri_list_paths};
|
||||
use super::{BITMAP, Flavour, MAX_BYTES, PNG, PathBuf, SVG, TEXT, copied_file, gnome_copied_paths, takes_files, uri_list_paths};
|
||||
|
||||
/// The types text is offered under and read from, best first: `text/plain;charset=utf-8` (GTK
|
||||
/// and Qt apps), `UTF8_STRING` (X11 apps through XWayland) and `text/plain`.
|
||||
@@ -2016,11 +2070,6 @@ mod wayland {
|
||||
Some(first)
|
||||
}
|
||||
|
||||
/// Whether a paste of one of `mimes` can take a copied file: one of them is not text.
|
||||
fn takes_files(mimes: &[&'static str]) -> bool {
|
||||
mimes.iter().any(|&m| m != TEXT)
|
||||
}
|
||||
|
||||
/// The first type among `offered` that lists copied files ([`FILE_LISTS`]), in any letter case.
|
||||
fn file_list_type(offered: &[String]) -> Option<&str> {
|
||||
FILE_LISTS.iter().find_map(|(list, _)| offered.iter().find(|o| o.eq_ignore_ascii_case(list))).map(String::as_str)
|
||||
|
||||
@@ -1956,6 +1956,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- In the Linux Flatpak on GNOME, where the clipboard goes through the compositor's core Wayland protocol, one Copy offers the PNG (`image/png`), the SVG (`image/svg+xml`, with Include SVG Code on) and the text, and Paste takes another app's SVG, text or PNG in that order, a picture over blank text or text that only names it; the Paste menu item is enabled by the types offered (`clipboard::wayland` tests in `apps/vectorcraft/src/clipboard.rs`; not yet run in the Flatpak on a GNOME session).
|
||||
- On that core Wayland clipboard, a paste from an app that leaves its pipe open and sends no data for 2 s fails and pastes nothing, and a paste of VectorCraft's Copy by an app that takes no data for 2 s is stopped and its pipe closed, so later pastes and copies are read and sent again; the 2 s limit is VectorCraft's own and starts again after each byte that moves (`clipboard::wayland::tests::a_read_from_an_app_that_stops_sending_fails_and_closes_the_pipe` and `a_send_to_an_app_that_stops_reading_fails_and_frees_its_thread`).
|
||||
- On that core Wayland clipboard, files copied in a file manager paste as the first one that is art, before the clipboard's other types, as on the other platforms: Paste reads the `text/uri-list` the file manager offers (RFC 2483), else GNOME Files' `x-special/gnome-copied-files`, off the UI thread, and takes the `file:` URIs with no host or `localhost` (RFC 8089), percent escapes decoded; other URIs are skipped, and a list of more than 1,024 URIs, VectorCraft's own limit, names no file. The Paste menu item is enabled when a file list is offered (`clipboard::wayland::tests::files_copied_in_a_file_manager_paste_as_their_art`; not yet run in the Flatpak on a GNOME session).
|
||||
- Through arboard (X11 and the Wayland data-control clipboard, and macOS), Edit › Paste is also enabled when the clipboard holds only files copied in a file manager, which the paste keys already pasted: the check reads the file list after the text, without opening a file, and a paste of text alone does not count the files (`clipboard::portable::tests::paste_is_available_for_files_copied_in_a_file_manager`; not yet run on macOS).
|
||||
- On the desktop with Enable Background Export on (Preferences › File Handling), File › Package… collects the linked files and fonts and writes the package from the document as it was at OK on the thread that writes background saves and exports, with "Packaging" in the status bar and the busy spinner until it is written; a save of unsaved changes that Package starts is written first. `file.package` over MCP and scripts replies as before.
|
||||
- On the desktop with Enable Background Export on, Export for Screens and the Asset Export panel's Export encode and write their files (into the folder, or one ZIP in it) from the document as it was at OK on the thread that writes background saves and exports, with "Exporting" in the status bar and the export progress dialog, which counts the files and whose Cancel stops before the next file. `document.exportForScreens` and `assets.export` over MCP and scripts reply as before.
|
||||
- With Enable Background Export on, an Export for Screens with no folder of its own also writes in the background into the folder its `exportPreset` keeps, with the same progress dialog and Cancel, and the status bar names that folder. When that folder is outside an MCP client's read roots, the reply, its errors and the status bar name it by its own name, as for an export made at once (`dialogs::tests_screens::an_export_into_a_presets_folder_writes_in_the_background`, `fileio::tests_screens::a_prepared_export_into_a_presets_folder_outside_the_read_root_leaves_it_out`). Behavior reference: the help page, https://helpx.adobe.com/illustrator/using/exporting-artwork.html (exports run in the background by default).
|
||||
|
||||
Reference in New Issue
Block a user