use std::time::Instant; use gpui_kit::component::{ h_flex, input::{InputEvent, Textarea, TextareaState}, v_flex, }; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use super::agent_toast::agent_toast_content; use super::center::NotificationCenter; use super::layout::{ ACTION_BUTTON, AVATAR_GAP, AVATAR_SIZE, PREVIEW_MAX_LINES, PREVIEW_SIZE, SEND_BUTTON, TOAST_PADDING, }; use super::platform; use super::ring_view::toast_radius; use super::rules::ChatKind; use super::stack::{ReplyState, ToastModel}; use super::text::{ToastText, describe, mention_ranges}; use crate::sidebar_model::{AvatarSpec, Face}; use crate::theme; use crate::views::avatar::{spec_avatar, with_presence}; use crate::views::composer::submitted_text; use crate::views::widgets::symbol; const CLOSE_SIZE: f32 = 24.; pub struct ToastView { center: Entity, id: u64, input: Entity, focus_handle: FocusHandle, last_title: String, _subscriptions: Vec, } impl ToastView { pub fn new( center: Entity, id: u64, window: &mut Window, cx: &mut Context, ) -> Self { let input = cx.new(|cx| { TextareaState::new(window, cx) .auto_grow(1, 1) .submit_on_enter(true) .placeholder("Reply ...") }); let subscriptions = vec![ cx.subscribe_in( &input, window, |this, input, event: &InputEvent, window, cx| { let value = input.read(cx).value(); if submitted_text(event, &value).is_some() { this.submit(window, cx); } }, ), cx.observe(¢er, |_, _, cx| cx.notify()), ]; ToastView { center, id, input, focus_handle: cx.focus_handle(), last_title: String::new(), _subscriptions: subscriptions, } } fn submit(&mut self, window: &mut Window, cx: &mut Context) { let text = self.input.read(cx).value().trim().to_owned(); if text.is_empty() { return; } self.input .update(cx, |state, cx| state.set_value("", window, cx)); let id = self.id; self.center .update(cx, |center, cx| center.submit_reply(id, text, cx)); } fn open_reply(&mut self, window: &mut Window, cx: &mut Context) { let id = self.id; let draft = self .center .update(cx, |center, cx| center.open_reply(id, cx)); if let Some(native) = platform::native_handle(window) { platform::set_activatable(native, true); } let input = self.input.clone(); cx.defer_in(window, move |_, window, cx| { input.update(cx, |state, cx| { if let Some(draft) = draft.filter(|draft| !draft.is_empty()) { state.set_value(&draft, window, cx); } state.focus(window, cx); }); }); } fn close_reply(&mut self, window: &mut Window, cx: &mut Context) { self.input .update(cx, |state, cx| state.set_value("", window, cx)); let id = self.id; self.center .update(cx, |center, cx| center.close_reply(id, cx)); window.focus(&self.focus_handle, cx); } fn retry(&mut self, cx: &mut Context) { let id = self.id; self.center.update(cx, |center, cx| { if let Some(text) = center.reply_text(id) { center.submit_reply(id, text, cx); } }); } } fn flat_button( label: &'static str, symbol_name: &'static str, accent: bool, mention: bool, ) -> Stateful
{ let color = if accent { theme::accent_text() } else { theme::text_soft() }; let hover_fill = if mention { theme::bubble_own() } else { theme::border_strong() }; h_flex() .id(label) .h(px(ACTION_BUTTON)) .px(px(10.)) .gap(px(6.)) .items_center() .rounded(px(6.)) .cursor_pointer() .text_size(px(12.5)) .font_weight(FontWeight::SEMIBOLD) .text_color(color) .hover(move |button| button.bg(hover_fill)) .child(symbol(symbol_name, 15., color)) .child(label) } fn keep_clicks_in_toast(area: Stateful
) -> Stateful
{ area.on_click(|_, _, cx| cx.stop_propagation()) } fn highlighted_preview(text: &str, mention: bool) -> AnyElement { let mut styled = StyledText::new(text.to_owned()); if mention { let style = HighlightStyle { color: Some(theme::accent_soft()), font_weight: Some(FontWeight::SEMIBOLD), ..Default::default() }; let highlights: Vec<_> = mention_ranges(text) .into_iter() .map(|range| (range, style)) .collect(); styled = styled.with_highlights(highlights); } div() .text_size(px(PREVIEW_SIZE)) .line_height(px(18.)) .text_color(theme::text()) .line_clamp(PREVIEW_MAX_LINES) .text_ellipsis() .child(styled) .into_any_element() } fn side_chip(label: String, mention: bool) -> Div { div() .h(px(20.)) .px(px(8.)) .flex() .items_center() .rounded(px(10.)) .bg(if mention { theme::bubble_own() } else { theme::border_strong() }) .text_color(if mention { theme::accent_soft() } else { theme::text() }) .text_size(px(11.)) .line_height(px(20.)) .font_weight(FontWeight::SEMIBOLD) .child(label) } impl ToastView { fn avatar(&self, model: &ToastModel, fill: Hsla, cx: &App) -> AnyElement { let center = self.center.read(cx); let directory = center.directory(cx); let sender = Face { user_id: model.sender_id.clone(), name: model.sender_name.clone(), }; match &model.kind { ChatKind::Group { .. } => { let chat = Face { user_id: None, name: model.chat_title.clone(), }; spec_avatar( directory, &AvatarSpec::Pair(sender, chat), AVATAR_SIZE, fill, ) } _ => { let presence = model .sender_id .as_deref() .map(|user_id| directory.presence_of(user_id)) .unwrap_or(crate::data::Presence::Live( crate::data::PresenceKind::Unknown, )); let face = spec_avatar(directory, &AvatarSpec::Single(sender), AVATAR_SIZE, fill); with_presence(face, presence, AVATAR_SIZE, fill).into_any_element() } } } fn side_slot(&self, model: &ToastModel, hovered: bool) -> AnyElement { let id = self.id; if hovered { let center = self.center.clone(); return div() .id("toast-close") .size(px(CLOSE_SIZE)) .flex_none() .flex() .items_center() .justify_center() .rounded_full() .bg(theme::border_strong()) .cursor_pointer() .hover(|button| button.bg(theme::text_faint())) .child(symbol("close", 16., theme::text())) .on_click(move |_, _, cx| { cx.stop_propagation(); center.update(cx, |center, cx| center.dismiss(id, cx)); }) .into_any_element(); } if model.mentions_me { return side_chip("Mention".to_owned(), true).into_any_element(); } if model.count > 1 { return side_chip(format!("{} new", model.count), false).into_any_element(); } div() .text_size(px(12.)) .text_color(theme::text_muted()) .child(model.time.clone()) .into_any_element() } fn body(&self, model: &ToastModel, text: &ToastText, hovered: bool) -> Div { let subtitle_icon = match &model.kind { ChatKind::Group { .. } => Some("group"), ChatKind::Channel { .. } => Some("tag"), ChatKind::Direct => None, }; let preview = if text.image_only { h_flex() .gap(px(6.)) .items_center() .child(symbol("image", 16., theme::text_muted())) .child( div() .text_size(px(13.)) .text_color(theme::text()) .child(text.preview.clone()), ) .into_any_element() } else { highlighted_preview(&text.preview, model.mentions_me) }; v_flex() .flex_1() .min_w_0() .gap(px(2.)) .child( h_flex() .w_full() .h(px(20.)) .gap(px(8.)) .items_center() .justify_between() .child( div() .flex_1() .min_w_0() .truncate() .text_size(px(14.)) .line_height(px(20.)) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::text()) .child(text.title.clone()), ) .child(div().flex_none().child(self.side_slot(model, hovered))), ) .child( h_flex() .gap(px(4.)) .items_center() .h(px(16.)) .text_size(px(12.)) .text_color(theme::text_muted()) .children(subtitle_icon.map(|name| symbol(name, 14., theme::text_muted()))) .child(div().truncate().child(text.subtitle.clone())), ) .child(preview) } fn action_row(&self, mention: bool, cx: &mut Context) -> Div { let id = self.id; let center = self.center.clone(); h_flex() .w_full() .mt(px(4.)) .gap(px(4.)) .justify_end() .child( flat_button("Mark as read", "done", false, mention).on_click(move |_, _, cx| { cx.stop_propagation(); center.update(cx, |center, cx| center.mark_read(id, cx)); }), ) .child( flat_button("Reply", "reply", true, mention).on_click(cx.listener( |this, _, window, cx| { cx.stop_propagation(); this.open_reply(window, cx); }, )), ) } fn reply_field(&self, window: &Window, cx: &mut Context) -> Stateful
{ let focused = self.input.focus_handle(cx).contains_focused(window, cx); let empty = self.input.read(cx).value().trim().is_empty(); let send = div() .id("toast-send") .size(px(SEND_BUTTON)) .flex_none() .flex() .items_center() .justify_center() .rounded(px(8.)) .child(symbol("send", 20., theme::text())) .when(empty, |button| button.opacity(0.4)) .when(!empty, |button| { button .cursor_pointer() .hover(|button| button.bg(theme::popup_hover())) .on_click(cx.listener(|this, _, window, cx| { cx.stop_propagation(); this.submit(window, cx); })) }); let hint = |keys: &'static str, action: &'static str| { h_flex() .gap(px(4.)) .child(div().text_color(theme::text_soft()).child(keys)) .child(action) }; keep_clicks_in_toast(v_flex().id("toast-reply-field")) .w_full() .mt(px(8.)) .gap(px(4.)) .child( h_flex() .w_full() .items_end() .gap(px(6.)) .py(px(6.)) .pl(px(12.)) .pr(px(6.)) .rounded(px(10.)) .bg(theme::surface()) .border_1() .border_color(if focused { theme::accent() } else { theme::border_strong() }) .child( div() .flex_1() .min_w_0() .child(Textarea::new(&self.input).appearance(false).bordered(false)), ) .child(send), ) .child( h_flex() .h(px(16.)) .gap(px(12.)) .justify_end() .text_size(px(11.)) .text_color(theme::text_muted()) .child(hint("Enter", "send")) .child(hint("Shift+Enter", "new line")) .child(hint("Esc", "close")), ) } fn confirmation(&self, model: &ToastModel, cx: &mut Context) -> Div { let sent = model.reply == ReplyState::Sent; let label = if sent { format!("Sent to {}", target_name(model)) } else { "Couldn't send".to_owned() }; let (icon_name, tone) = if sent { ("check_circle", theme::green()) } else { ("error", theme::red_soft()) }; h_flex() .size_full() .gap(px(10.)) .items_center() .child(symbol(icon_name, 22., tone)) .child( div() .flex_1() .min_w_0() .truncate() .text_size(px(14.)) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::text()) .child(label), ) .when(sent, |row| { row.child( div() .text_size(px(12.)) .text_color(theme::text_muted()) .child(model.time.clone()), ) }) .when(!sent, |row| { row.child( flat_button("Retry", "reply", true, false).on_click(cx.listener( |this, _, _, cx| { cx.stop_propagation(); this.retry(cx); }, )), ) }) } } fn target_name(model: &ToastModel) -> String { match &model.kind { ChatKind::Group { .. } => model.chat_title.clone(), _ => model.sender_name.clone(), } } impl Render for ToastView { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { let id = self.id; let (model, preview_on) = { let center = self.center.read(cx); ( center.model(id).cloned(), center.settings().account.show_preview, ) }; let Some(model) = model else { return div().size_full().bg(theme::popup()).into_any_element(); }; let text = describe(&model, preview_on); let narration = model .agent .as_ref() .map_or_else(|| text.narration(), |agent| agent.title.clone()); if narration != self.last_title { window.set_window_title(&narration); self.last_title = narration; } let mention = model.mentions_me; let fill = if mention { theme::toast_mention_fill() } else { theme::popup() }; let hovered = model.hovered; let content = match model.reply { _ if model.agent.is_some() => { let agent = model.agent.as_ref().expect("checked above"); agent_toast_content(&self.center, id, agent, hovered) } ReplyState::Sent | ReplyState::Failed => self.confirmation(&model, cx), ReplyState::Open => v_flex() .w_full() .child( h_flex() .w_full() .gap(px(AVATAR_GAP)) .items_start() .child(self.avatar(&model, fill, cx)) .child(self.body(&model, &text, true)), ) .child(self.reply_field(window, cx)), ReplyState::Closed => v_flex() .w_full() .child( h_flex() .w_full() .gap(px(AVATAR_GAP)) .items_start() .child(self.avatar(&model, fill, cx)) .child(self.body(&model, &text, hovered)), ) .when(!model.status_change && model.org.is_none(), |content| { content.child(self.action_row(mention, cx)) }), }; let center = self.center.clone(); let hover_center = self.center.clone(); div() .id(("toast", id)) .track_focus(&self.focus_handle) .relative() .size_full() .opacity(model.opacity(Instant::now())) .rounded(toast_radius(window)) .bg(fill) .overflow_hidden() .cursor_pointer() .on_hover(move |is_hovered, _, cx| { hover_center.update(cx, |center, cx| center.set_hover(id, *is_hovered, cx)); }) .on_click(move |_, _, cx| { center.update(cx, |center, cx| center.activate(id, cx)); }) .capture_action(cx.listener( |this, _: &gpui_kit::component::input::Escape, window, cx| { this.close_reply(window, cx); cx.stop_propagation(); }, )) .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| { if event.keystroke.key == "escape" { let id = this.id; this.center.update(cx, |center, cx| center.dismiss(id, cx)); } })) .child(div().size_full().p(px(TOAST_PADDING)).child(content)) .into_any_element() } } pub struct PillView { center: Entity, _subscription: Subscription, } impl PillView { pub fn new(center: Entity, cx: &mut Context) -> Self { let subscription = cx.observe(¢er, |_, _, cx| cx.notify()); PillView { center, _subscription: subscription, } } } impl Render for PillView { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { let (queued, opacity) = { let center = self.center.read(cx); (center.queued_count(), center.stack_opacity()) }; let center = self.center.clone(); h_flex() .id("toast-hide-all") .size_full() .gap(px(6.)) .items_center() .justify_center() .opacity(opacity) .rounded(toast_radius(window)) .bg(theme::popup()) .cursor_pointer() .hover(|bar| bar.bg(theme::popup_hover())) .text_size(px(12.5)) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::text_soft()) .child("Hide all") .when(queued > 0, |bar| { bar.child( div() .text_color(theme::text_muted()) .child(format!("{queued} more waiting")), ) }) .on_click(move |_, _, cx| { center.update(cx, |center, cx| center.hide_all(cx)); }) } } #[cfg(test)] mod tests { use std::cell::Cell; use std::rc::Rc; use gpui_kit::component::input::{Textarea, TextareaState}; use gpui_kit::{ AppContext as _, Context, Entity, Focusable as _, InteractiveElement as _, IntoElement, Modifiers, ParentElement as _, Render, StatefulInteractiveElement as _, Styled as _, TestAppContext, Window, div, point, px, }; use super::keep_clicks_in_toast; struct ToastHarness { input: Entity, card_clicks: Rc>, } impl Render for ToastHarness { fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { let card_clicks = self.card_clicks.clone(); div() .id("toast-card") .size_full() .on_click(move |_, _, _| card_clicks.set(card_clicks.get() + 1)) .child( keep_clicks_in_toast(div().id("toast-reply-field")) .w(px(300.)) .child(Textarea::new(&self.input)), ) } } #[gpui_kit::test] fn clicking_the_reply_input_does_not_open_the_chat(cx: &mut TestAppContext) { cx.update(gpui_kit::init); let card_clicks = Rc::new(Cell::new(0)); let (harness, window_cx) = cx.add_window_view({ let card_clicks = card_clicks.clone(); move |window, cx| ToastHarness { input: cx.new(|cx| TextareaState::new(window, cx).auto_grow(1, 1)), card_clicks, } }); window_cx.update(|window, cx| window.draw(cx).clear(cx)); window_cx.simulate_click(point(px(20.), px(12.)), Modifiers::default()); assert_eq!(card_clicks.get(), 0); let focused = window_cx.update(|window, cx| { let input = harness.read(cx).input.clone(); input.focus_handle(cx).contains_focused(window, cx) }); assert!(focused); window_cx.simulate_click(point(px(20.), px(200.)), Modifiers::default()); assert_eq!(card_clicks.get(), 1); } }