From 84a1d974ce6d09f855c40d42a71cd06a50bca80c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Dec 2026 04:27:31 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20paste=20images=20and=20copi?= =?UTF-8?q?ed=20files=20into=20the=20composer:=20clipboard=20images=20beco?= =?UTF-8?q?me=20pending=20image=20attachments,=20copied=20files=20attach?= =?UTF-8?q?=20like=20the=20picker,=20text=20paste=20unchanged?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/src/gui/chat.rs | 6 +++ crates/client/src/gui/media_view.rs | 61 ++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs index 6b0b299..0423666 100644 --- a/crates/client/src/gui/chat.rs +++ b/crates/client/src/gui/chat.rs @@ -2063,6 +2063,12 @@ impl Yumush { .child( Textarea::new(&self.chat.message_input) .token(super::glyph::token) + .on_paste({ + let entity = entity.clone(); + move |item, window, cx| { + entity.update(cx, |yumush, cx| yumush.paste_files(item, window, cx)) + } + }) .appearance(false) .text_size(text(text::BODY)) .text_color(palette.text) diff --git a/crates/client/src/gui/media_view.rs b/crates/client/src/gui/media_view.rs index 6a42f7a..e56e4e9 100644 --- a/crates/client/src/gui/media_view.rs +++ b/crates/client/src/gui/media_view.rs @@ -11,9 +11,10 @@ use std::{ use common::model::{Attachment, AttachmentKind, Message}; use gpui_kit::{ - App, AppContext, AsyncWindowContext, Context, Entity, FontWeight, Image, ImageFormat, - InteractiveElement, IntoElement, ParentElement, PathPromptOptions, RenderImage, - StatefulInteractiveElement, Styled, Subscription, Task, WeakEntity, Window, + App, AppContext, AsyncWindowContext, ClipboardEntry, ClipboardItem, Context, Entity, + FontWeight, Image, ImageFormat, InteractiveElement, IntoElement, ParentElement, + PathPromptOptions, RenderImage, StatefulInteractiveElement, Styled, Subscription, Task, + WeakEntity, Window, assets::IconName, component::{ Icon, WindowExt, @@ -167,6 +168,16 @@ pub(super) async fn upload_file( uploaded } +fn save_pasted(image: &Image) -> Option { + let folder = std::env::temp_dir() + .join("yumush-paste") + .join(image.id.to_string()); + std::fs::create_dir_all(&folder).ok()?; + let path = folder.join(format!("image.{}", image.format.extension())); + std::fs::write(&path, &image.bytes).ok()?; + Some(path) +} + pub(super) fn into_render_image(frame: crate::media::Frame) -> Option> { let bgra = Arc::try_unwrap(frame.bgra).unwrap_or_else(|shared| (*shared).clone()); let buffer = image::RgbaImage::from_raw(frame.width, frame.height, bgra)?; @@ -283,15 +294,50 @@ impl Yumush { } pub(super) fn attach_files(&mut self, window: &mut Window, cx: &mut Context) { - let Some(community_id) = self.chat.selected_community_id else { - return; - }; let picked = cx.prompt_for_paths(PathPromptOptions { files: true, directories: false, multiple: true, prompt: None, }); + cx.spawn_in(window, async move |this, cx| { + let Ok(Ok(Some(paths))) = picked.await else { + return; + }; + let _ = this.update_in(cx, |yumush, window, cx| { + yumush.attach_paths(paths, window, cx) + }); + }) + .detach(); + } + + pub(super) fn paste_files( + &mut self, + item: &ClipboardItem, + window: &mut Window, + cx: &mut Context, + ) -> bool { + let mut paths = Vec::new(); + for entry in item.entries() { + match entry { + ClipboardEntry::Image(image) => paths.extend(save_pasted(image)), + ClipboardEntry::ExternalPaths(external) => { + paths.extend(external.paths().iter().cloned()) + } + ClipboardEntry::String(_) => {} + } + } + if paths.is_empty() { + return false; + } + self.attach_paths(paths, window, cx); + true + } + + fn attach_paths(&mut self, paths: Vec, window: &mut Window, cx: &mut Context) { + let Some(community_id) = self.chat.selected_community_id else { + return; + }; let network = self.network.clone(); let maximum = self .server @@ -299,9 +345,6 @@ impl Yumush { .map(|settings| settings.max_attachment_bytes); cx.spawn_in(window, async move |this, cx| { - let Ok(Ok(Some(paths))) = picked.await else { - return; - }; for path in paths { let result = upload_file(&this, &network, community_id, path, maximum, cx).await; let _ = this.update(cx, |yumush, cx| {