use gpui_kit::assets::IconName; use gpui_kit::component::{Icon, h_flex, v_flex}; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use std::time::Duration; use super::adaptive_card::cards_view; use super::attachments::{FileActions, ImageActions, attachments_view, message_body}; use super::avatar::{bot_avatar, person_avatar}; use super::call_card::{CallActions, call_card_row}; use super::jumbo_emoji::jumbo_row; use super::link_preview::{link_preview_card, meeting_join_chip}; use super::message_actions::{MessageMenu, message_toolbar}; use super::profile_card::opens_profile; use super::reaction_picker::PickHandler; use super::reaction_pills::{ReactionControls, reaction_pills}; use super::reaction_proposal_pill::{ReactionPill, reaction_pill}; use super::scheduled_toolbar::{ScheduledMenu, scheduled_toolbar}; use super::toolbar_placement::{HoverChange, TimelineBounds, placed_toolbar}; use super::translation_line::{TranslationActions, translation_line}; use super::widgets::{icon, symbol}; use crate::agents::{AgentRun, run_chip}; use crate::card_state::BotIdentity; use crate::data::Directory; use crate::render::Block; use crate::rows::{Delivery, MessageRow, Receipt, ScheduledState, Skeleton, bubble_corners}; use crate::sidebar_model::DELETED_PREVIEW; use crate::theme; use app_ui_kit::layout::ROW_PADDING; pub type RowAction = Option>; pub const BUBBLE_TAGS: [&str; 12] = [ "message-bubble/0", "message-bubble/1", "message-bubble/2", "message-bubble/3", "message-bubble/4", "message-bubble/5", "message-bubble/6", "message-bubble/7", "message-bubble/8", "message-bubble/9", "message-bubble/10", "message-bubble/11", ]; pub(super) const AVATAR_SIZE: f32 = 28.; pub(super) const AVATAR_GAP: f32 = 8.; const SERIES_START_GAP: f32 = 8.; pub(super) const BODY_SIZE: f32 = 13.5; const MAX_WIDTH_RATIO: f32 = 0.7; const NARROW_BUBBLE_WIDTH: f32 = 300.; const REACTION_FOOTER_HEIGHT: f32 = 24.; const META_SIZE: f32 = 11.; const META_CHECK_SIZE: f32 = 15.; const SAVED_MARK_SIZE: f32 = 12.; const FORWARDED_SIZE: f32 = 11.; const FIGURE_SPACE_WIDTH: f32 = 7.4; const PULSE_PERIOD: Duration = Duration::from_millis(1600); const SKELETON_LINE_HEIGHT: f32 = 20.; pub struct DeliveryActions { pub retry: RowAction, pub delete: RowAction, pub send_now: RowAction, } pub struct RowActions { pub bot: Option, pub delivery: DeliveryActions, pub reply: RowAction, pub hovered: Option, pub menu: Option, pub scheduled_menu: Option, pub react: Option, pub reaction_controls: Option, pub files: Option, pub open_image: Option, pub highlighted: bool, pub saved: bool, pub translation: Option, pub timeline: TimelineBounds, pub agent_run: Option, pub call: CallActions, pub reaction_proposal: Option, } const TOOLBAR_RIGHT_INSET: f32 = 0.; fn separator_pill(label: &str, fill: Hsla, text_color: Hsla) -> Div { h_flex() .w_full() .justify_center() .mt(px(12.)) .mb(px(6.)) .child( div() .h(px(24.)) .px(px(8.)) .flex() .items_center() .rounded(px(12.)) .bg(fill) .text_size(px(11.)) .text_color(text_color) .child(label.to_owned()), ) } pub(super) fn day_separator(label: &str) -> Div { separator_pill(label, theme::row_hover(), theme::text_muted()) } pub(super) fn new_marker() -> Div { separator_pill( "New", theme::mention_background(false), theme::accent_text(), ) } fn bubble_max_width(timeline: &TimelineBounds, reserved: f32) -> Length { let Some(bounds) = timeline.get() else { return relative(MAX_WIDTH_RATIO).into(); }; let row_width = (f32::from(bounds.size.width) - 2. * ROW_PADDING - reserved).max(0.); px((row_width * MAX_WIDTH_RATIO).max(row_width.min(NARROW_BUBBLE_WIDTH))).into() } fn bubble( row: &MessageRow, index: usize, own: bool, directory: &Directory, extras: BubbleExtras, cx: &App, ) -> Stateful
{ let corners = bubble_corners(own, row.series); let jumbo = row.jumbo.as_deref().filter(|_| { !row.deleted && matches!(row.delivery, Delivery::Delivered | Delivery::Sending) }); let mut element = div() .id(ElementId::Name(format!("bubble-{index}").into())) .when_some(extras.hover.clone(), |element, hover| { element.on_hover(move |is_hovered, _, cx| hover(*is_hovered, cx)) }) .relative() .max_w_full() .when_some(BUBBLE_TAGS.get(index).copied(), |element, tag| { element.debug_selector(|| tag.into()) }) .when(jumbo.is_none(), |element| { element .rounded_tl(px(corners.top_left)) .rounded_tr(px(corners.top_right)) .rounded_br(px(corners.bottom_right)) .rounded_bl(px(corners.bottom_left)) .px(px(12.)) .py(px(8.)) }) .text_size(px(BODY_SIZE)) .line_height(relative(1.45)); if row.deleted { return element .border_1() .border_color(theme::border_strong()) .py(px(6.)) .text_size(px(13.)) .italic() .text_color(theme::text_muted()) .child(DELETED_PREVIEW); } let failed = matches!(row.delivery, Delivery::Failed(_)); let scheduled = matches!(row.delivery, Delivery::Scheduled(_)); element = element .when(!scheduled && jumbo.is_none(), |bubble| { bubble.bg(if own && !failed { theme::bubble_own() } else { theme::bubble_other() }) }) .when(failed, |bubble| { bubble.border_1().border_color(theme::red()) }) .when(scheduled, |bubble| { bubble.border_1().border_color( if row.delivery == Delivery::Scheduled(ScheduledState::DeliveryFailed) { theme::red() } else { theme::accent() }, ) }); let has_reactions = !row.reactions.is_empty(); let saved = extras.saved; let mut content = v_flex().gap(px(6.)); if row.forwarded { content = content.child(forwarded_header()); } let attachments = attachments_view( &row.blocks, &row.images, &row.local_images, &row.files, &format!("message-{index}"), directory, extras.files.as_ref(), extras.open_image.as_ref(), ); let cards = cards_view(&row.adaptive_cards, &row.conversation_id, &row.key, cx); let trailing_content = attachments.is_some() || row.meeting_link.is_some() || row.link_preview.is_some() || !cards.is_empty(); let padded_blocks = if has_reactions || trailing_content || jumbo.is_some() { None } else { with_meta_room(&row.blocks, meta_room(row, own, saved)) }; let meta_inline = has_text(row) && padded_blocks.is_some(); if let Some(jumbo) = jumbo { content = content.child(jumbo_row(&row.key, jumbo, own)); } else if has_text(row) { content = content.children(message_body( padded_blocks.as_deref().unwrap_or(&row.blocks), &row.images, &row.local_images, &format!("message-{index}"), own, directory, extras.open_image.as_ref(), cx, )); } content = content.children(attachments); content = content.children( row.meeting_link .clone() .map(|url| meeting_join_chip(format!("message-{index}-join"), url)), ); content = content.children(row.link_preview.as_ref().map(|preview| { link_preview_card( preview, format!("message-{index}-link"), directory, false, None, ) })); content = content.children(cards); if has_reactions { content = content.child( h_flex() .gap(px(8.)) .items_end() .child( reaction_pills( &row.reactions, index, own, directory, extras.react.clone(), extras.controls.as_ref(), ) .flex_auto(), ) .child( h_flex() .h(px(REACTION_FOOTER_HEIGHT)) .flex_none() .items_center() .child(bubble_meta(row, own, saved)), ), ); } else if !meta_inline { content = content.child(h_flex().justify_end().child(bubble_meta(row, own, saved))); } element = element.child(content); if meta_inline { element = element.child( div() .absolute() .right(px(8.)) .bottom(px(4.)) .child(bubble_meta(row, own, saved)), ); } if let Some(menu) = extras.menu { element = element.child(placed_toolbar( message_toolbar(menu, cx), extras.timeline.clone(), TOOLBAR_RIGHT_INSET, )); } if let Some(menu) = extras.scheduled_menu { element = element.child(placed_toolbar( scheduled_toolbar(menu), extras.timeline.clone(), TOOLBAR_RIGHT_INSET, )); } element } #[derive(Default)] struct BubbleExtras { hover: Option, menu: Option, scheduled_menu: Option, react: Option, controls: Option, files: Option, open_image: Option, saved: bool, translation: Option, timeline: TimelineBounds, agent_run: Option, reaction_proposal: Option, } fn agent_chip(run: Option) -> Option
{ run.map(|run| div().py(px(2.)).child(run_chip(&run, theme::row_hover()))) } pub(super) fn forwarded_header() -> Div { h_flex() .gap(px(4.)) .items_center() .text_size(px(FORWARDED_SIZE)) .text_color(theme::text_muted()) .child(icon(IconName::Forward, FORWARDED_SIZE, theme::text_muted())) .child("Forwarded") } fn bubble_meta(row: &MessageRow, own: bool, saved: bool) -> Div { let tint = if own { theme::own_meta() } else { theme::text_muted() }; h_flex() .gap(px(4.)) .items_center() .text_size(px(META_SIZE)) .line_height(px(14.)) .italic() .text_color(tint) .when(row.edited, |meta| meta.child("Edited")) .when(saved, |meta| { meta.child(icon( IconName::Bookmark, SAVED_MARK_SIZE, theme::accent_text(), )) }) .child(row.time.clone()) .when(own && row.delivery == Delivery::Sending, |meta| { meta.child(symbol("schedule", META_CHECK_SIZE, tint)) }) .when(matches!(row.delivery, Delivery::Scheduled(_)), |meta| { meta.child(symbol("schedule", META_SIZE + 3., tint)) }) .when( own && row.delivery == Delivery::Delivered, |meta| match row.receipt { Receipt::Hidden => meta, Receipt::Pending => meta.child(receipt_slot(tint).relative().top(px(1.))), Receipt::Sent => meta.child(on_baseline(symbol("done", META_CHECK_SIZE, tint))), Receipt::Read => meta.child(on_baseline(symbol( "done_all", META_CHECK_SIZE, theme::own_read(), ))), }, ) } fn on_baseline(check: Icon) -> Div { div().relative().top(px(1.)).child(check) } fn pulse(id: &'static str, element: E) -> AnimationElement { element.with_animation( id, Animation::new(PULSE_PERIOD) .repeat_synced() .with_easing(pulsating_between(0.35, 1.)), |element, opacity| element.opacity(opacity), ) } fn receipt_slot(tint: Hsla) -> Div { h_flex() .size(px(META_CHECK_SIZE)) .flex_none() .items_center() .justify_center() .child(pulse( "receipt-slot", div() .w(px(13.)) .h(px(4.)) .rounded(px(2.)) .bg(tint.opacity(0.5)), )) } pub fn render_skeleton_row(skeleton: &Skeleton, index: usize) -> AnyElement { let corners = bubble_corners(skeleton.own, Default::default()); let height = 14. + SKELETON_LINE_HEIGHT * f32::from(skeleton.lines); let shape = div() .w(relative(skeleton.width_ratio)) .h(px(height)) .rounded_tl(px(corners.top_left)) .rounded_tr(px(corners.top_right)) .rounded_br(px(corners.bottom_right)) .rounded_bl(px(corners.bottom_left)) .bg(if skeleton.own { theme::bubble_own().opacity(0.55) } else { theme::bubble_other() }); let line = h_flex() .w_full() .when(skeleton.own, |line| line.justify_end()) .when(!skeleton.own, |line| { line.gap(px(AVATAR_GAP)).child( div() .size(px(AVATAR_SIZE)) .flex_none() .rounded_full() .bg(theme::bubble_other()), ) }) .child(shape); div() .id(ElementId::Name(format!("row-{index}").into())) .w_full() .px(px(ROW_PADDING)) .pt(px(SERIES_START_GAP)) .child(pulse("skeleton", line)) .into_any_element() } fn meta_room(row: &MessageRow, own: bool, saved: bool) -> usize { let mut width = 12. + row.time.chars().count() as f32 * 6.; if saved { width += SAVED_MARK_SIZE + 4.; } if row.edited { width += 56.; } if own { width += META_CHECK_SIZE + 4.; } (width / FIGURE_SPACE_WIDTH).ceil() as usize } fn with_meta_room(blocks: &[Block], room: usize) -> Option> { let mut padded = blocks.to_vec(); let inline = padded.last_mut()?.last_inline_mut()?; *inline = inline.with_trailing_room(room); Some(padded) } pub(super) fn has_text(row: &MessageRow) -> bool { !row.blocks.is_empty() || (row.images.is_empty() && row.local_images.is_empty() && row.files.is_empty() && row.adaptive_cards.is_empty()) } fn delivery_link( index: usize, name: &str, label: &'static str, action: RowAction, ) -> Stateful
{ div() .id(ElementId::Name(format!("{name}-{index}").into())) .cursor_pointer() .text_color(theme::red_tint()) .underline() .child(label) .when_some(action, |link, action| { link.on_click(move |_, _, cx| action(cx)) }) } pub(super) fn delivery_note( row: &MessageRow, delivery: DeliveryActions, index: usize, ) -> Option { let DeliveryActions { retry, delete, send_now, } = delivery; let note = |text: &'static str| { h_flex() .gap(px(4.)) .text_size(px(11.)) .text_color(theme::red_soft()) .child(text) }; match &row.delivery { Delivery::Delivered | Delivery::Sending | Delivery::Scheduled(ScheduledState::Waiting) => { None } Delivery::Failed(_) => Some( note("Failed to send.") .child(delivery_link(index, "retry", "Retry", retry)) .child(delivery_link(index, "delete", "Delete", delete)) .into_any_element(), ), Delivery::Scheduled(ScheduledState::DeliveryFailed) => Some( note("Couldn't send at the scheduled time.") .child(delivery_link(index, "send-now", "Send now", send_now)) .child(delivery_link(index, "delete", "Delete", delete)) .into_any_element(), ), Delivery::Scheduled(ScheduledState::ChangeFailed) => Some( note("Couldn't change the scheduled message.") .child(delivery_link(index, "retry", "Retry", retry)) .into_any_element(), ), } } fn others_row( row: &MessageRow, index: usize, directory: &Directory, bot: Option<&BotIdentity>, delivery: DeliveryActions, mut extras: BubbleExtras, cx: &App, ) -> Div { let translation = extras.translation.take(); let agent_run = extras.agent_run.take(); let reaction_proposal = extras.reaction_proposal.take(); let first = !row.series.has_prev; let author = bot.map_or_else(|| row.author.clone(), |bot| bot.name.clone()); let profile_user = row.sender_id.as_deref().filter(|_| bot.is_none()); let mut column = v_flex() .gap(px(2.)) .flex_1() .min_w_0() .max_w(bubble_max_width(&extras.timeline, AVATAR_SIZE + AVATAR_GAP)) .items_start(); if first { column = column.child( h_flex().gap(px(8.)).items_baseline().pl(px(2.)).child( opens_profile( div().id(ElementId::Name(format!("author-{index}").into())), profile_user, ) .text_size(px(12.)) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::text_soft()) .child(author.clone()), ), ); } column = column.child(bubble(row, index, false, directory, extras, cx)); column = column.children(agent_chip(agent_run)); column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key))); column = column.children(translation_line(row, index, translation.as_ref())); if let Some(note) = delivery_note(row, delivery, index) { column = column.child(note); } let lead = if let Some(bot) = bot.filter(|_| first) { bot_avatar(bot, AVATAR_SIZE) } else if first { person_avatar( directory, row.sender_id.as_deref(), &row.author, AVATAR_SIZE, ) } else { div().size(px(AVATAR_SIZE)).into_any_element() }; h_flex() .w_full() .gap(px(AVATAR_GAP)) .items_start() .child( opens_profile( div().id(ElementId::Name(format!("author-avatar-{index}").into())), profile_user.filter(|_| first), ) .w(px(AVATAR_SIZE)) .flex_none() .child(lead), ) .child(column) } fn own_row( row: &MessageRow, index: usize, directory: &Directory, delivery: DeliveryActions, mut extras: BubbleExtras, cx: &App, ) -> Div { let translation = extras.translation.take(); let agent_run = extras.agent_run.take(); let reaction_proposal = extras.reaction_proposal.take(); let mut column = v_flex().w_full().items_end().gap(px(2.)); column = column.child( h_flex() .w_full() .justify_end() .child( div() .min_w_0() .max_w(bubble_max_width(&extras.timeline, 0.)) .child(bubble(row, index, true, directory, extras, cx)), ), ); column = column.children(agent_chip(agent_run)); column = column.children(reaction_proposal.map(|pill| reaction_pill(pill, &row.key))); column = column.children(translation_line(row, index, translation.as_ref())); if let Some(note) = delivery_note(row, delivery, index) { column = column.child(note); } column } pub fn render_message_row( row: &MessageRow, index: usize, actions: RowActions, directory: &Directory, cx: &App, ) -> AnyElement { if let Some(call) = &row.call { return call_card_row(row, call, index, directory, actions.call); } let RowActions { bot, delivery, reply, hovered, menu, scheduled_menu, react, reaction_controls, files, open_image, highlighted, saved, translation, timeline, agent_run, call: _, reaction_proposal, } = actions; let extras = BubbleExtras { hover: hovered, menu, scheduled_menu, react, controls: reaction_controls, files, open_image, saved, translation, timeline, agent_run, reaction_proposal, }; let own = row.own; let spacing = if row.series.has_prev { px(2.) } else { px(SERIES_START_GAP) }; let mut container = v_flex().w_full().px(px(ROW_PADDING)); if let Some(day) = &row.day_header { container = container.child(day_separator(day)); } if row.new_marker { container = container.child(new_marker()); } let body = if own { own_row(row, index, directory, delivery, extras, cx) } else { others_row(row, index, directory, bot.as_ref(), delivery, extras, cx) }; container .child( div() .id(ElementId::Name(format!("row-{index}").into())) .w_full() .pt(spacing) .when_some(reply, |element, reply| { element.on_mouse_down(MouseButton::Right, move |_, _, cx| reply(cx)) }) .child( div() .w_full() .rounded(px(10.)) .when(highlighted, |element| { element.bg(theme::accent().opacity(0.2)) }) .child(body), ), ) .into_any_element() }