// Attachments in the window: the chips above the message box, the + button, dropping files on // the window, pasting a picture with ⌘V, and the thumbnails in sent messages. use super::*; use crate::attach::{self, Attachment}; #[derive(Default)] pub struct AttachUi { pub pending: HashMap>, picked: Option)>>, pub(super) thumbs: HashMap>, pub error: Option, pub(super) voice: Option<(u64, crate::voice::Recording)>, pub(super) voice_rx: Option)>>, /// The recording is a meeting (longer; becomes a transcript to summarize). pub meeting: bool, /// 通話模式: the bot being talked to, and where the call is. pub call: Option<(u64, CallPhase)>, pub(super) call_rec: Option, pub(super) call_rx: Option>>, /// What was just said: the chat's length just before it was sent (its answer comes after), /// when it was sent, and whether the bot has been seen working on it since. pub(super) call_from: usize, pub(super) call_asked: Option, pub(super) call_seen: bool, } #[derive(Clone, Copy, PartialEq, Debug)] pub enum CallPhase { Listening, Transcribing, Thinking, Speaking, } impl CallPhase { pub fn label(self) -> &'static str { match self { CallPhase::Listening => "正在聽…", CallPhase::Transcribing => "聽懂中…", CallPhase::Thinking => "想一下…", CallPhase::Speaking => "正在說…", } } } impl App { fn add_attachment_paths(&mut self, bot_id: u64, paths: Vec) { for path in paths { match attach::store(bot_id, &path) { Ok(a) => self.att.pending.entry(bot_id).or_default().push(a), Err(e) => self.att.error = Some(e), } } } /// Files dropped on the window, the picker's answer, and ⌘V with a picture on the clipboard. pub(super) fn attachments_poll(&mut self, ctx: &egui::Context, bot_id: u64, composer_focused: bool) { // (While 傳送到其他裝置 is open, dropped files are for sending, not for this chat.) let dropped: Vec = if self.drop_page.is_some() { vec![] } else { ctx.input(|i| i.raw.dropped_files.iter().map(|f| f.path().to_path_buf()).filter(|p| !p.as_os_str().is_empty()).collect()) }; if !dropped.is_empty() { self.add_attachment_paths(bot_id, dropped); } if let Some(rx) = &self.att.picked { if let Ok((id, paths)) = rx.try_recv() { self.att.picked = None; self.add_attachment_paths(id, paths); } } // ⌘V with no text on the clipboard: maybe a picture. The window answers the paste keys // itself and hands over the picture (no V key arrives for ⌘V); a V key with ⌘ that does // arrive (⌘⇧V) looks at the clipboard as before. let pasted_text = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Paste(t) if !t.is_empty()))); let pasted_image = ctx.input(|i| i.events.iter().find_map(|e| if let egui::Event::PasteImage(img) = e { Some(img.clone()) } else { None })); let cmd_v = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { key: Key::V, pressed: true, modifiers, .. } if modifiers.command))); if !composer_focused || self.demo { return; } if let Some(img) = pasted_image { let rgba: Vec = img.pixels.iter().flat_map(|c| c.to_srgba_unmultiplied()).collect(); match attach::store_pasted(bot_id, img.size, rgba) { Ok(a) => self.att.pending.entry(bot_id).or_default().push(a), Err(e) => self.att.error = Some(e), } } else if cmd_v && !pasted_text { if let Some(a) = attach::paste_image(bot_id) { self.att.pending.entry(bot_id).or_default().push(a); } } } pub(super) fn pick_attachments(&mut self, ctx: &egui::Context, bot_id: u64) { let (tx, rx) = channel(); self.att.picked = Some(rx); let c = ctx.clone(); std::thread::spawn(move || { let paths = attach::pick_files(); let _ = tx.send((bot_id, paths)); c.request_repaint(); }); } /// "Drop to attach" while files hover over the window. pub(super) fn drop_overlay(&self, ctx: &egui::Context) { if self.drop_page.is_some() || !ctx.input(|i| !i.raw.hovered_files.is_empty()) { return; } let p = self.pal; let screen = ctx.content_rect(); let painter = ctx.layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::unique("drop-overlay"))); painter.rect_filled(screen, 0, p.pane.gamma_multiply(0.85)); let inner = screen.shrink(24.0); painter.rect_stroke(inner, 18, Stroke::new(2.0, p.accent), egui::StrokeKind::Inside); painter.text(inner.center(), egui::Align2::CENTER_CENTER, "放開以附加檔案", egui::FontId::proportional(FONT_XL), p.accent); } fn thumb(&mut self, ctx: &egui::Context, path: &str) -> Option { if let Some(t) = self.att.thumbs.get(path) { return t.clone(); } let t = image::open(path).ok().map(|img| { let img = img.thumbnail(240, 240).to_rgba8(); let size = [img.width() as usize, img.height() as usize]; ctx.load_texture(format!("thumb-{path}"), egui::ColorImage::from_rgba_unmultiplied(size, img.as_raw()), egui::TextureOptions::LINEAR) }); self.att.thumbs.insert(path.to_string(), t.clone()); t } /// One attachment: a picture thumbnail, or a file chip. Returns true when its × was clicked. pub(super) fn attachment_chip(&mut self, ui: &mut egui::Ui, a: &Attachment, removable: bool) -> bool { let p = self.pal; let ctx = ui.ctx().clone(); let mut remove = false; let thumb = if a.image { self.thumb(&ctx, &a.path) } else { None }; let frame = Frame::new().fill(p.hover).corner_radius(10).inner_margin(if thumb.is_some() { Margin::same(3) } else { Margin::symmetric(10, 6) }); let r = frame .show(ui, |ui| { ui.horizontal(|ui| { if let Some(t) = &thumb { let s = t.size_vec2(); let k = (64.0 / s.y).min(120.0 / s.x); ui.add(egui::Image::new((t.id(), s * k)).corner_radius(8)); } else { let (rect, _) = ui.allocate_exact_size(vec2(14.0, 16.0), egui::Sense::hover()); let r = rect.shrink2(vec2(1.5, 1.0)); ui.painter().rect_stroke(r, 2.0, Stroke::new(1.3, p.text2), egui::StrokeKind::Middle); ui.painter().line_segment([r.left_top() + vec2(3.0, 5.0), r.right_top() + vec2(-3.0, 5.0)], Stroke::new(1.0, p.text3)); ui.painter().line_segment([r.left_top() + vec2(3.0, 8.5), r.right_top() + vec2(-3.0, 8.5)], Stroke::new(1.0, p.text3)); ui.vertical(|ui| { ui.spacing_mut().item_spacing.y = 0.0; let name: String = a.name.chars().take(28).collect(); ui.label(RichText::new(name).size(FONT_XS).color(p.text)); ui.label(RichText::new(attach::human(a.bytes)).size(10.5).color(p.text3)); }); } if removable && ui.add(egui::Button::new(RichText::new("×").size(13.0).color(p.text3)).frame(false)).on_hover_text(t("移除")).clicked() { remove = true; } }); }) .response; if !removable && r.interact(egui::Sense::click()).on_hover_text(t("打開")).clicked() { crate::preview::open_with_system(&a.path); } remove } /// The chips above the message box. pub(super) fn pending_chips(&mut self, ui: &mut egui::Ui, bot_id: u64) { let list = self.att.pending.get(&bot_id).cloned().unwrap_or_default(); if list.is_empty() && self.att.error.is_none() { return; } let mut remove = None; ui.horizontal_wrapped(|ui| { for (i, a) in list.iter().enumerate() { if self.attachment_chip(ui, a, true) { remove = Some(i); } } }); if let Some(i) = remove { if let Some(v) = self.att.pending.get_mut(&bot_id) { let a = v.remove(i); let _ = std::fs::remove_file(&a.path); } } if let Some(e) = self.att.error.clone() { ui.horizontal(|ui| { ui.label(RichText::new(e).size(FONT_XS).color(self.pal.danger)); if ui.add(egui::Button::new(RichText::new(t("知道了")).size(FONT_XS)).frame(false)).clicked() { self.att.error = None; } }); } ui.add_space(4.0); } /// Take this bot's pending attachments for a message (copying them onto its own computer when /// it's running). pub(super) fn take_attachments(&mut self, bot_id: u64) -> Vec { let mut list = self.att.pending.remove(&bot_id).unwrap_or_default(); for a in list.iter_mut() { if let Ok(dst) = crate::cloudbox::copy_in(&a.path, bot_id, &a.name) { a.on_box = dst; } } list } pub(super) fn has_pending_attachments(&self, bot_id: u64) -> bool { self.att.pending.get(&bot_id).map(|v| !v.is_empty()).unwrap_or(false) } }