Paste images and copied files into the composer: clipboard images become pending image attachments, copied files attach like the picker, text paste unchanged
Browse files at this commit84a1d974ce6d09f855c40d42a71cd06a50bca80cParent: 38b4cd2- feat
- paste images and copied files into the composer: clipboard images become pending image attachments, copied files attach like the picker, text paste unchanged
2 changed files +58 -9
crates/client/src/gui/chat.rsmodified+6 -0
| @@ -2063,6 +2063,12 @@ impl Yumush { | |||
| 2063 | .child( | 2063 | .child( |
| 2064 | Textarea::new(&self.chat.message_input) | 2064 | Textarea::new(&self.chat.message_input) |
| 2065 | .token(super::glyph::token) | 2065 | .token(super::glyph::token) |
| 2066 | + .on_paste({ | ||
| 2067 | + let entity = entity.clone(); | ||
| 2068 | + move |item, window, cx| { | ||
| 2069 | + entity.update(cx, |yumush, cx| yumush.paste_files(item, window, cx)) | ||
| 2070 | + } | ||
| 2071 | + }) | ||
| 2066 | .appearance(false) | 2072 | .appearance(false) |
| 2067 | .text_size(text(text::BODY)) | 2073 | .text_size(text(text::BODY)) |
| 2068 | .text_color(palette.text) | 2074 | .text_color(palette.text) |
crates/client/src/gui/media_view.rsmodified+52 -9
| @@ -11,9 +11,10 @@ use std::{ | |||
| 11 | 11 | ||
| 12 | use common::model::{Attachment, AttachmentKind, Message}; | 12 | use common::model::{Attachment, AttachmentKind, Message}; |
| 13 | use gpui_kit::{ | 13 | use gpui_kit::{ |
| 14 | - App, AppContext, AsyncWindowContext, Context, Entity, FontWeight, Image, ImageFormat, | 14 | + App, AppContext, AsyncWindowContext, ClipboardEntry, ClipboardItem, Context, Entity, |
| 15 | - InteractiveElement, IntoElement, ParentElement, PathPromptOptions, RenderImage, | 15 | + FontWeight, Image, ImageFormat, InteractiveElement, IntoElement, ParentElement, |
| 16 | - StatefulInteractiveElement, Styled, Subscription, Task, WeakEntity, Window, | 16 | + PathPromptOptions, RenderImage, StatefulInteractiveElement, Styled, Subscription, Task, |
| 17 | + WeakEntity, Window, | ||
| 17 | assets::IconName, | 18 | assets::IconName, |
| 18 | component::{ | 19 | component::{ |
| 19 | Icon, WindowExt, | 20 | Icon, WindowExt, |
| @@ -167,6 +168,16 @@ pub(super) async fn upload_file( | |||
| 167 | uploaded | 168 | uploaded |
| 168 | } | 169 | } |
| 169 | 170 | ||
| 171 | +fn save_pasted(image: &Image) -> Option<PathBuf> { | ||
| 172 | + let folder = std::env::temp_dir() | ||
| 173 | + .join("yumush-paste") | ||
| 174 | + .join(image.id.to_string()); | ||
| 175 | + std::fs::create_dir_all(&folder).ok()?; | ||
| 176 | + let path = folder.join(format!("image.{}", image.format.extension())); | ||
| 177 | + std::fs::write(&path, &image.bytes).ok()?; | ||
| 178 | + Some(path) | ||
| 179 | +} | ||
| 180 | + | ||
| 170 | pub(super) fn into_render_image(frame: crate::media::Frame) -> Option<Arc<RenderImage>> { | 181 | pub(super) fn into_render_image(frame: crate::media::Frame) -> Option<Arc<RenderImage>> { |
| 171 | let bgra = Arc::try_unwrap(frame.bgra).unwrap_or_else(|shared| (*shared).clone()); | 182 | let bgra = Arc::try_unwrap(frame.bgra).unwrap_or_else(|shared| (*shared).clone()); |
| 172 | let buffer = image::RgbaImage::from_raw(frame.width, frame.height, bgra)?; | 183 | let buffer = image::RgbaImage::from_raw(frame.width, frame.height, bgra)?; |
| @@ -283,15 +294,50 @@ impl Yumush { | |||
| 283 | } | 294 | } |
| 284 | 295 | ||
| 285 | pub(super) fn attach_files(&mut self, window: &mut Window, cx: &mut Context<Self>) { | 296 | pub(super) fn attach_files(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 286 | - let Some(community_id) = self.chat.selected_community_id else { | ||
| 287 | - return; | ||
| 288 | - }; | ||
| 289 | let picked = cx.prompt_for_paths(PathPromptOptions { | 297 | let picked = cx.prompt_for_paths(PathPromptOptions { |
| 290 | files: true, | 298 | files: true, |
| 291 | directories: false, | 299 | directories: false, |
| 292 | multiple: true, | 300 | multiple: true, |
| 293 | prompt: None, | 301 | prompt: None, |
| 294 | }); | 302 | }); |
| 303 | + cx.spawn_in(window, async move |this, cx| { | ||
| 304 | + let Ok(Ok(Some(paths))) = picked.await else { | ||
| 305 | + return; | ||
| 306 | + }; | ||
| 307 | + let _ = this.update_in(cx, |yumush, window, cx| { | ||
| 308 | + yumush.attach_paths(paths, window, cx) | ||
| 309 | + }); | ||
| 310 | + }) | ||
| 311 | + .detach(); | ||
| 312 | + } | ||
| 313 | + | ||
| 314 | + pub(super) fn paste_files( | ||
| 315 | + &mut self, | ||
| 316 | + item: &ClipboardItem, | ||
| 317 | + window: &mut Window, | ||
| 318 | + cx: &mut Context<Self>, | ||
| 319 | + ) -> bool { | ||
| 320 | + let mut paths = Vec::new(); | ||
| 321 | + for entry in item.entries() { | ||
| 322 | + match entry { | ||
| 323 | + ClipboardEntry::Image(image) => paths.extend(save_pasted(image)), | ||
| 324 | + ClipboardEntry::ExternalPaths(external) => { | ||
| 325 | + paths.extend(external.paths().iter().cloned()) | ||
| 326 | + } | ||
| 327 | + ClipboardEntry::String(_) => {} | ||
| 328 | + } | ||
| 329 | + } | ||
| 330 | + if paths.is_empty() { | ||
| 331 | + return false; | ||
| 332 | + } | ||
| 333 | + self.attach_paths(paths, window, cx); | ||
| 334 | + true | ||
| 335 | + } | ||
| 336 | + | ||
| 337 | + fn attach_paths(&mut self, paths: Vec<PathBuf>, window: &mut Window, cx: &mut Context<Self>) { | ||
| 338 | + let Some(community_id) = self.chat.selected_community_id else { | ||
| 339 | + return; | ||
| 340 | + }; | ||
| 295 | let network = self.network.clone(); | 341 | let network = self.network.clone(); |
| 296 | let maximum = self | 342 | let maximum = self |
| 297 | .server | 343 | .server |
| @@ -299,9 +345,6 @@ impl Yumush { | |||
| 299 | .map(|settings| settings.max_attachment_bytes); | 345 | .map(|settings| settings.max_attachment_bytes); |
| 300 | 346 | ||
| 301 | cx.spawn_in(window, async move |this, cx| { | 347 | cx.spawn_in(window, async move |this, cx| { |
| 302 | - let Ok(Ok(Some(paths))) = picked.await else { | ||
| 303 | - return; | ||
| 304 | - }; | ||
| 305 | for path in paths { | 348 | for path in paths { |
| 306 | let result = upload_file(&this, &network, community_id, path, maximum, cx).await; | 349 | let result = upload_file(&this, &network, community_id, path, maximum, cx).await; |
| 307 | let _ = this.update(cx, |yumush, cx| { | 350 | let _ = this.update(cx, |yumush, cx| { |