From 238d922e1566465a22b112802bb89c024ccc4bd1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 23 Nov 2026 07:42:32 -0500 Subject: [PATCH] fw sync: L-316: copied files are a Files primary in the clipboard offer plan (arboard's file list); trial a_file_list_is_written_as_files --- crates/platform/src/clipboard/offer/macos.rs | 2 + crates/platform/src/clipboard/offer/mod.rs | 46 ++++++++++++++----- crates/platform/src/clipboard/offer/tests.rs | 32 +++++++++++++ crates/platform/src/clipboard/offer/unix.rs | 2 + .../platform/src/clipboard/offer/windows.rs | 2 + 5 files changed, 72 insertions(+), 12 deletions(-) diff --git a/crates/platform/src/clipboard/offer/macos.rs b/crates/platform/src/clipboard/offer/macos.rs index 7faa00a61d..ed4153b98b 100644 --- a/crates/platform/src/clipboard/offer/macos.rs +++ b/crates/platform/src/clipboard/offer/macos.rs @@ -68,6 +68,8 @@ pub(super) fn write(handle: &mut arboard::Clipboard, state: &mut State, plan: &P string("public.html", &html.text); } } + // arboard's file list is the whole copy. + Primary::Files => {} } state.change = Some(board.changeCount()); Ok(index) diff --git a/crates/platform/src/clipboard/offer/mod.rs b/crates/platform/src/clipboard/offer/mod.rs index cb7fe0736d..085469882b 100644 --- a/crates/platform/src/clipboard/offer/mod.rs +++ b/crates/platform/src/clipboard/offer/mod.rs @@ -26,8 +26,8 @@ use self::windows as platform; use std::borrow::Cow; use super::backend::{Decode, system_error}; -use super::convert::rgba8_image; -use super::types::{ClipboardError, ClipboardImage, Flavor, HTML, MATHML, PNG, RGBA8, RTF, TEXT}; +use super::convert::{parse_uri_list, rgba8_image}; +use super::types::{ClipboardError, ClipboardImage, Flavor, HTML, MATHML, PNG, RGBA8, RTF, TEXT, URI_LIST}; /// The platform's offer state: the cached X11 `TARGETS` connection and the macOS change count recorded at a write. pub(super) struct Offer { @@ -55,6 +55,12 @@ struct Picture<'a> { png: Option<&'a [u8]>, } +/// The copied files a write offers: the local paths of the first file list that names any. +struct FileList { + index: usize, + paths: Vec, +} + /// What a write puts on the clipboard: the first flavour of each kind among the caller's. struct Plan<'a> { text: Option>, @@ -62,13 +68,16 @@ struct Plan<'a> { rtf: Option>, mathml: Option>, picture: Option>, + files: Option, } -/// Which of a plan's two shapes a write takes: arboard holds a bitmap or rich text, not both. -#[derive(Clone, Copy, PartialEq, Eq)] +/// Which of a plan's shapes a write takes: arboard holds a bitmap, rich text or a file list, one of them. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] enum Primary { Picture, Rich, + /// Copied files, the way a file manager's Copy puts them. + Files, } impl<'a> Plan<'a> { @@ -89,18 +98,26 @@ impl<'a> Plan<'a> { }?; pixels.is_whole().then_some(Picture { index, pixels, png }) }); - Self { text: part(TEXT), html: part(HTML), rtf: part(RTF), mathml: part(MATHML), picture } + // Only `file:` URIs become files; a list that names none leaves the write to the other flavours. + let files = flavors.iter().enumerate().find_map(|(index, flavor)| { + let paths = (flavor.mime == URI_LIST).then(|| parse_uri_list(&String::from_utf8_lossy(&flavor.data)))?; + (!paths.is_empty()).then_some(FileList { index, paths }) + }); + Self { text: part(TEXT), html: part(HTML), rtf: part(RTF), mathml: part(MATHML), picture, files } } - /// The shape of the write: whichever of the bitmap and the HTML or text the caller put first. + /// The shape of the write: whichever of the bitmap, the HTML or text and the file list the caller put first. fn primary(&self) -> Option { let rich = [&self.html, &self.text].into_iter().flatten().map(|part| part.index).min(); - match (self.picture.as_ref().map(|picture| picture.index), rich) { - (Some(picture), Some(rich)) if rich < picture => Some(Primary::Rich), - (Some(_), _) => Some(Primary::Picture), - (None, Some(_)) => Some(Primary::Rich), - (None, None) => None, - } + [ + self.picture.as_ref().map(|picture| (picture.index, Primary::Picture)), + rich.map(|index| (index, Primary::Rich)), + self.files.as_ref().map(|files| (files.index, Primary::Files)), + ] + .into_iter() + .flatten() + .min_by_key(|&(index, _)| index) + .map(|(_, primary)| primary) } } @@ -127,6 +144,11 @@ fn arboard_write(handle: &mut arboard::Clipboard, plan: &Plan<'_>) -> Result<(us } (None, None) => Err(ClipboardError::NoFormat), }, + Primary::Files => { + let files = plan.files.as_ref().ok_or(ClipboardError::NoFormat)?; + handle.set().file_list(&files.paths).map_err(system_error)?; + Ok((files.index, Primary::Files)) + } } } diff --git a/crates/platform/src/clipboard/offer/tests.rs b/crates/platform/src/clipboard/offer/tests.rs index cd667d8f60..d102c6e452 100644 --- a/crates/platform/src/clipboard/offer/tests.rs +++ b/crates/platform/src/clipboard/offer/tests.rs @@ -26,3 +26,35 @@ fn cf_html_offsets_delimit_the_fragment_bytes() { assert_eq!(&data[end_html..], b"\0", "EndHTML excludes the trailing NUL"); assert_eq!(end_html, data.len() - 1); } + +// Copied files reach the system clipboard as a file list holding the same paths, spaces and +// non-ASCII names included; a URI that names no local file is never written as one, and a list +// with no file in it leaves the write to the next flavour. +#[test] +fn a_file_list_is_written_as_files() { + use super::{Plan, Primary}; + use crate::clipboard::types::{Flavor, URI_LIST}; + + let paths = vec![std::env::temp_dir().join("a b#1.pdf"), std::env::temp_dir().join("café 100%.png")]; + let flavors = [Flavor::files(&paths), Flavor::text("names")]; + let plan = Plan::new(&flavors, None); + assert_eq!(plan.primary(), Some(Primary::Files)); + assert_eq!(plan.files.as_ref().map(|files| (files.index, files.paths.clone())), Some((0, paths.clone()))); + + let mut mixed = b"# copied\r\nhttps://example.com/a.pdf\r\n".to_vec(); + mixed.extend(Flavor::files(&paths[..1]).data); + let flavors = [Flavor::new(URI_LIST, mixed)]; + let plan = Plan::new(&flavors, None); + assert_eq!(plan.primary(), Some(Primary::Files)); + assert_eq!(plan.files.as_ref().map(|files| files.paths.clone()), Some(paths[..1].to_vec())); + + let flavors = [Flavor::new(URI_LIST, b"https://example.com/a.pdf\r\n".to_vec()), Flavor::text("names")]; + let plan = Plan::new(&flavors, None); + assert!(plan.files.is_none()); + assert_eq!(plan.primary(), Some(Primary::Rich)); + assert_eq!(plan.text.as_ref().map(|text| text.index), Some(1)); + + // Text put first stays the write's shape even with files beside it. + let flavors = [Flavor::text("names"), Flavor::files(&paths)]; + assert_eq!(Plan::new(&flavors, None).primary(), Some(Primary::Rich)); +} diff --git a/crates/platform/src/clipboard/offer/unix.rs b/crates/platform/src/clipboard/offer/unix.rs index 9539d4200e..ab8eef8a54 100644 --- a/crates/platform/src/clipboard/offer/unix.rs +++ b/crates/platform/src/clipboard/offer/unix.rs @@ -127,6 +127,8 @@ fn wayland_write(plan: &Plan<'_>, owner: &[u8]) -> Result, Clipboa let Some((index, png)) = plan.picture.as_ref().and_then(|picture| Some((picture.index, picture.png?))) else { return Ok(None) }; [Some((index, PNG, png.to_vec())), text(&plan.html, HTML), text(&plan.text, UTF8_TEXT)].into_iter().flatten().collect() } + // arboard offers a file list with the names file managers read (`text/uri-list` and GNOME's own). + Some(Primary::Files) => return Ok(None), // The text first: the plain-text aliases serve the first text type offered. _ => [text(&plan.text, UTF8_TEXT), text(&plan.html, HTML), text(&plan.rtf, RTF), text(&plan.mathml, MATHML)].into_iter().flatten().collect(), }; diff --git a/crates/platform/src/clipboard/offer/windows.rs b/crates/platform/src/clipboard/offer/windows.rs index 6439749062..770a7847dd 100644 --- a/crates/platform/src/clipboard/offer/windows.rs +++ b/crates/platform/src/clipboard/offer/windows.rs @@ -94,6 +94,8 @@ pub(super) fn write(handle: &mut arboard::Clipboard, state: &mut State, plan: &P add(HTML_FORMAT, &wrap_cf_html(&html.text)); } } + // arboard's file list is the whole copy. + Primary::Files => {} } state.unmarked = !add(OWNER_FORMAT, owner); Ok(index)