From e3831b05d42189886358fdc77cc8be93e7da4397 Mon Sep 17 00:00:00 2001 From: bherbruck Date: Sun, 18 Oct 2026 23:30:21 -0400 Subject: [PATCH] Agent chat: Ctrl/Cmd+V attaches copied files and pictures (#2812) (#2854) Files copied in the file manager attach as files, a picture on the clipboard attaches as a picture, text pastes as text. A picture-only or files-only paste is seen through the V key coming up (egui-winit sends nothing when the clipboard has no text); macOS sends no key-up while Cmd is held, so there only pastes that come with text are seen and + > Paste a picture still works. --- crates/ui-egui/src/agent_chat/mod.rs | 4 + crates/ui-egui/src/agent_chat/panel.rs | 5 + crates/ui-egui/src/agent_chat/paste.rs | 223 +++++++++++++++++++++++++ 3 files changed, 232 insertions(+) create mode 100644 crates/ui-egui/src/agent_chat/paste.rs diff --git a/crates/ui-egui/src/agent_chat/mod.rs b/crates/ui-egui/src/agent_chat/mod.rs index 3347c9ff8..42b0ecca4 100644 --- a/crates/ui-egui/src/agent_chat/mod.rs +++ b/crates/ui-egui/src/agent_chat/mod.rs @@ -31,6 +31,8 @@ mod markdown; mod markdown_view; mod panel; #[cfg(not(target_arch = "wasm32"))] +pub mod paste; +#[cfg(not(target_arch = "wasm32"))] mod process; pub mod settings; @@ -333,6 +335,8 @@ pub struct AgentChat { pub(crate) attachments: Vec, /// Pictures pasted so far, to name the next one. pasted: usize, + /// When the last text paste arrived (input time), so the V key coming up after it isn't a second paste. + pub(crate) text_pasted_at: Option, /// The chips' thumbnails, by the picture they show. thumbs: std::collections::HashMap, /// The last id given to a file added to a message. diff --git a/crates/ui-egui/src/agent_chat/panel.rs b/crates/ui-egui/src/agent_chat/panel.rs index 93344ba31..7ad6cd836 100644 --- a/crates/ui-egui/src/agent_chat/panel.rs +++ b/crates/ui-egui/src/agent_chat/panel.rs @@ -597,6 +597,11 @@ fn desktop_body(app: &mut SolveApp, ui: &mut egui::Ui) { ui.separator(); // The files for the next message (#1744), as chips of their own. attachment_chips(app, ui); + // Ctrl/Cmd+V: files and pictures on the clipboard attach; text goes to the box below. + #[cfg(not(target_arch = "wasm32"))] + if focused { + super::paste::handle(app, ui); + } let edit = egui::TextEdit::multiline(&mut app.agent_chat.input) .hint_text(tr(if busy { "ui.agent_chat_placeholder_busy" } else { "ui.agent_chat_placeholder" })) .desired_rows(2) diff --git a/crates/ui-egui/src/agent_chat/paste.rs b/crates/ui-egui/src/agent_chat/paste.rs new file mode 100644 index 000000000..2d722ca9e --- /dev/null +++ b/crates/ui-egui/src/agent_chat/paste.rs @@ -0,0 +1,223 @@ +//! Ctrl/Cmd+V in the chat's text box: files copied in the file manager attach as files, a picture +//! on the clipboard attaches as a picture, and text pastes as text. +//! +//! egui-winit answers a paste shortcut by reading the clipboard's *text*: with text it sends a +//! `Paste` event, without it nothing at all, not even the key press. So a clipboard that holds +//! only a picture or only files (a screenshot, files copied in Explorer) is seen through the V +//! key coming up (Shift+Insert likewise), which is still sent. Files and a picture are read with +//! arboard, which gives a file list on Windows, macOS and Linux (X11 and Wayland data-control). +//! Gap: macOS delivers no key-up while Cmd is held, so there a picture-only or files-only paste +//! doesn't arrive (text and files that come with text do); the + menu's "Paste a picture" works. + +use std::path::{Path, PathBuf}; + +use super::SolveApp; + +/// A picture on the clipboard: RGBA rows, as the system gives them. +pub struct Picture { + pub width: usize, + pub height: usize, + pub rgba: Vec, +} + +/// What the system clipboard can give besides text; read only when a paste needs it. +pub trait Clipboard { + fn files(&mut self) -> Vec; + fn picture(&mut self) -> Option; +} + +/// The system's clipboard. +pub struct System; + +impl Clipboard for System { + fn files(&mut self) -> Vec { + arboard::Clipboard::new().and_then(|mut c| c.get().file_list()).unwrap_or_default() + } + fn picture(&mut self) -> Option { + let i = arboard::Clipboard::new().and_then(|mut c| c.get_image()).ok()?; + Some(Picture { width: i.width, height: i.height, rgba: i.bytes.into_owned() }) + } +} + +/// What a paste does. +pub enum Paste { + /// Leave it to the text box. + Text, + Files(Vec), + Picture(Picture), + Nothing, +} + +/// Decide a paste. `text` is the clipboard's text when the shortcut brought some. Files win over +/// text (file managers put the files' names on the clipboard as text too); text wins over a +/// picture (a spreadsheet's cells come with a picture of them). +pub fn decide(text: Option<&str>, clip: &mut dyn Clipboard) -> Paste { + let files = clip.files(); + if !files.is_empty() { + return Paste::Files(files); + } + if let Some(text) = text { + let listed = files_in_text(text); + return if listed.is_empty() { Paste::Text } else { Paste::Files(listed) }; + } + clip.picture().map_or(Paste::Nothing, Paste::Picture) +} + +/// The files a text names, when it is a list of `file://` links to files that exist (what some +/// file managers put on the clipboard) and nothing else. +pub fn files_in_text(text: &str) -> Vec { + let mut out = Vec::new(); + for line in text.lines().map(str::trim).filter(|l| !l.is_empty() && !l.starts_with('#')) { + let Some(path) = line.strip_prefix("file://").and_then(uri_path) else { return Vec::new() }; + if !path.is_file() { + return Vec::new(); + } + out.push(path); + } + out +} + +/// The path of a `file://` link's rest (`/home/a%20b`, `localhost/home/a`, `/C:/Users/a`). +fn uri_path(rest: &str) -> Option { + let rest = rest.strip_prefix("localhost").unwrap_or(rest); + if !rest.starts_with('/') { + return None; + } + let bytes = rest.as_bytes(); + let mut raw = Vec::with_capacity(bytes.len()); + let mut i = 0; + while let Some(&b) = bytes.get(i) { + if b == b'%' { + let hex = rest.get(i + 1..i + 3)?; + raw.push(u8::from_str_radix(hex, 16).ok()?); + i += 3; + } else { + raw.push(b); + i += 1; + } + } + let s = String::from_utf8(raw).ok()?; + // A Windows drive: `/C:/Users` is `C:/Users`. + let drive = s.as_bytes(); + let s = if drive.get(2) == Some(&b':') && drive.get(1).is_some_and(u8::is_ascii_alphabetic) { s.get(1..)?.to_string() } else { s }; + Some(Path::new(&s).to_path_buf()) +} + +/// How long after a text paste the V key coming up still belongs to it. +const SAME_PASTE_SECS: f64 = 1.5; + +/// The paste shortcut for the focused text box this frame. Call before the box is drawn: a paste +/// that is files is taken out of the input, so the box doesn't type their names. +pub fn handle(app: &mut SolveApp, ui: &egui::Ui) { + let now = ui.input(|i| i.time); + let (text, released) = ui.input(|i| { + let text = i.events.iter().find_map(|e| if let egui::Event::Paste(t) = e { Some(t.clone()) } else { None }); + let released = i.events.iter().any(|e| { + matches!(e, egui::Event::Key { key, pressed: false, modifiers, .. } + if (*key == egui::Key::V && modifiers.command) || (*key == egui::Key::Insert && modifiers.shift)) + }); + (text, released) + }); + let after_text = app.agent_chat.text_pasted_at.is_some_and(|at| (0.0..SAME_PASTE_SECS).contains(&(now - at))); + if text.is_none() && !(released && !after_text) { + if released { + app.agent_chat.text_pasted_at = None; + } + return; + } + let paste = decide(text.as_deref(), &mut System); + if text.is_some() { + app.agent_chat.text_pasted_at = if matches!(paste, Paste::Text) { Some(now) } else { None }; + } else { + app.agent_chat.text_pasted_at = None; + } + apply(app, ui, paste, text.is_some()); +} + +/// Do what [`decide`] said. +fn apply(app: &mut SolveApp, ui: &egui::Ui, paste: Paste, had_text: bool) { + match paste { + Paste::Text | Paste::Nothing => {} + Paste::Files(paths) => { + if had_text { + ui.input_mut(|i| i.events.retain(|e| !matches!(e, egui::Event::Paste(_)))); + } + super::attach_dropped(app, paths.into_iter()); + } + Paste::Picture(p) => { + if let Err(e) = super::attach_rgba(app, p.width, p.height, &p.rgba) { + app.agent_chat.notice(e, true); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Default)] + struct Fake { + files: Vec, + picture: Option<(usize, usize)>, + } + + impl Clipboard for Fake { + fn files(&mut self) -> Vec { + self.files.clone() + } + fn picture(&mut self) -> Option { + self.picture.map(|(width, height)| Picture { width, height, rgba: vec![255; width * height * 4] }) + } + } + + #[test] + fn a_picture_alone_attaches_as_a_picture() { + let mut clip = Fake { picture: Some((2, 2)), ..Fake::default() }; + assert!(matches!(decide(None, &mut clip), Paste::Picture(p) if (p.width, p.height) == (2, 2))); + assert!(matches!(decide(None, &mut Fake::default()), Paste::Nothing), "an empty clipboard does nothing"); + } + + #[test] + fn copied_files_attach_even_when_their_names_came_as_text() { + let files = vec![PathBuf::from("/a/part.step"), PathBuf::from("/a/brief.pdf")]; + let mut clip = Fake { files: files.clone(), picture: Some((1, 1)) }; + assert!(matches!(decide(None, &mut clip), Paste::Files(f) if f == files), "files alone"); + assert!(matches!(decide(Some("part.step\nbrief.pdf"), &mut clip), Paste::Files(f) if f == files), "with their names as text"); + } + + #[test] + fn text_pastes_as_text_even_with_a_picture_beside_it() { + let mut clip = Fake { picture: Some((1, 1)), ..Fake::default() }; + assert!(matches!(decide(Some("wall 2 mm"), &mut clip), Paste::Text)); + } + + #[test] + fn a_text_of_file_links_attaches_the_files_but_other_text_does_not() { + let d = std::env::temp_dir().join(format!("solidcraft-paste-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + let spaced = d.join("my part.stl"); + std::fs::write(&spaced, b"solid").unwrap(); + let plain = d.join("brief.md"); + std::fs::write(&plain, b"x").unwrap(); + let link = |p: &Path| format!("file://{}", p.to_string_lossy().replace(' ', "%20")); + let list = format!("# copied\n{}\r\n{}\n", link(&spaced), link(&plain)); + assert_eq!(files_in_text(&list), vec![spaced.clone(), plain.clone()]); + assert!(matches!(decide(Some(&list), &mut Fake::default()), Paste::Files(f) if f.len() == 2)); + assert!(files_in_text(&format!("{}\nsee also", link(&plain))).is_empty(), "a line that is not a link: it is text"); + assert!(files_in_text(&link(&d.join("missing.png"))).is_empty(), "a file that isn't there: it is text"); + assert!(files_in_text(&link(&d)).is_empty(), "a folder is not a file"); + assert!(files_in_text("https://example.com/a.png").is_empty()); + assert!(files_in_text("file://host/share/a.png").is_empty(), "another machine's file"); + assert!(files_in_text("file:///a/%zz").is_empty(), "a bad escape"); + let _ = std::fs::remove_dir_all(&d); + } + + #[test] + fn links_to_windows_drives_lose_the_leading_slash() { + assert_eq!(uri_path("/C:/Users/a%20b/part.step"), Some(PathBuf::from("C:/Users/a b/part.step"))); + assert_eq!(uri_path("localhost/home/a"), Some(PathBuf::from("/home/a"))); + assert_eq!(uri_path("relative"), None); + } +}