| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 3 | use gpui_kit::component::h_flex; |
| 4 | use gpui_kit::*; |
| 5 | |
| 6 | use super::ConversationView; |
| 7 | use crate::forward_selection::{CANCEL_LABEL, ForwardSelection, NEXT_LABEL, selected_count_label}; |
| 8 | use crate::rows::MessageRow; |
| 9 | use crate::theme; |
| 10 | use crate::views::widgets::icon; |
| 11 | use app_ui_kit::layout::ROW_PADDING; |
| 12 | |
| 13 | pub const FORWARD_FOOTER_TAG: &str = "forward-footer"; |
| 14 | pub const FORWARD_FOOTER_BAR_TAG: &str = "forward-footer/bar"; |
| 15 | pub const FORWARD_FOOTER_COUNT_TAG: &str = "forward-footer/bar/count"; |
| 16 | pub const FORWARD_FOOTER_NEXT_TAG: &str = "forward-footer/bar/next"; |
| 17 | const FOOTER_TOP: f32 = 8.; |
| 18 | const FOOTER_BOTTOM: f32 = 16.; |
| 19 | const BAR_PADDING: f32 = 8.; |
| 20 | const BAR_RADIUS: f32 = 12.; |
| 21 | const COUNT_INSET: f32 = 4.; |
| 22 | const ICON_SIZE: f32 = 16.; |
| 23 | const BUTTON_HEIGHT: f32 = 32.; |
| 24 | const BUTTON_RADIUS: f32 = 6.; |
| 25 | const DISABLED_OPACITY: f32 = 0.4; |
| 26 | |
| 27 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 28 | pub(super) struct SelectionSnapshot { |
| 29 | pub selection: ForwardSelection, |
| 30 | pub image_count: usize, |
| 31 | } |
| 32 | |
| 33 | impl ConversationView { |
| 34 | pub(super) fn active_forward_selection(&self, cx: &App) -> Option<SelectionSnapshot> { |
| 35 | let chat_id = self |
| 36 | .chat_id() |
| 37 | .filter(|_| !self.draft_active && !self.in_feed())?; |
| 38 | let selection = self |
| 39 | .app |
| 40 | .read(cx) |
| 41 | .forward_selection |
| 42 | .clone() |
| 43 | .filter(|selection| selection.conversation_id() == chat_id)?; |
| 44 | let image_count = self |
| 45 | .rows |
| 46 | .borrow() |
| 47 | .iter() |
| 48 | .flat_map(|row| row.messages()) |
| 49 | .filter(|message| selection.contains(&message.key)) |
| 50 | .map(|message| message.images.len()) |
| 51 | .sum(); |
| 52 | Some(SelectionSnapshot { |
| 53 | selection, |
| 54 | image_count, |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | pub(super) fn start_forward_selection(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 59 | if let Some(chat_id) = self.chat_id() { |
| 60 | self.app.update(cx, |state, cx| { |
| 61 | state.start_forward_selection(&chat_id, message_id, cx) |
| 62 | }); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub(super) fn on_forward_selection_changed( |
| 67 | &mut self, |
| 68 | window: &mut Window, |
| 69 | cx: &mut Context<Self>, |
| 70 | ) { |
| 71 | let row_count = self.rows.borrow().len(); |
| 72 | self.scroller.update(cx, |scroller, cx| { |
| 73 | scroller.remeasure_items(0..row_count, cx) |
| 74 | }); |
| 75 | if self.active_forward_selection(cx).is_some() { |
| 76 | window.focus(&self.focus_handle, cx); |
| 77 | } else if !self.draft_active && !self.in_feed() { |
| 78 | self.composer |
| 79 | .update(cx, |composer, cx| composer.focus(window, cx)); |
| 80 | } |
| 81 | cx.notify(); |
| 82 | } |
| 83 | |
| 84 | pub(super) fn cancel_forward_selection(&mut self, cx: &mut Context<Self>) -> bool { |
| 85 | if self.active_forward_selection(cx).is_none() { |
| 86 | return false; |
| 87 | } |
| 88 | self.app |
| 89 | .update(cx, |state, cx| state.cancel_forward_selection(cx)); |
| 90 | true |
| 91 | } |
| 92 | |
| 93 | fn forward_next(&mut self, cx: &mut Context<Self>) { |
| 94 | let Some(snapshot) = self.active_forward_selection(cx) else { |
| 95 | return; |
| 96 | }; |
| 97 | let ordered_ids = { |
| 98 | let rows = self.rows.borrow(); |
| 99 | let messages: Vec<&MessageRow> = rows.iter().flat_map(|row| row.messages()).collect(); |
| 100 | snapshot.selection.ordered_ids(messages) |
| 101 | }; |
| 102 | self.app |
| 103 | .update(cx, |state, cx| state.forward_selected(&ordered_ids, cx)); |
| 104 | } |
| 105 | |
| 106 | pub(super) fn render_forward_footer( |
| 107 | &self, |
| 108 | snapshot: &SelectionSnapshot, |
| 109 | cx: &mut Context<Self>, |
| 110 | ) -> Div { |
| 111 | let count = snapshot.selection.len(); |
| 112 | div() |
| 113 | .debug_selector(|| FORWARD_FOOTER_TAG.into()) |
| 114 | .w_full() |
| 115 | .flex_none() |
| 116 | .px(px(ROW_PADDING)) |
| 117 | .pt(px(FOOTER_TOP)) |
| 118 | .pb(px(FOOTER_BOTTOM)) |
| 119 | .child( |
| 120 | h_flex() |
| 121 | .debug_selector(|| FORWARD_FOOTER_BAR_TAG.into()) |
| 122 | .w_full() |
| 123 | .p(px(BAR_PADDING)) |
| 124 | .gap(px(BAR_PADDING)) |
| 125 | .items_center() |
| 126 | .rounded(px(BAR_RADIUS)) |
| 127 | .bg(theme::surface_raised()) |
| 128 | .child( |
| 129 | h_flex() |
| 130 | .debug_selector(|| FORWARD_FOOTER_COUNT_TAG.into()) |
| 131 | .flex_1() |
| 132 | .min_w_0() |
| 133 | .pl(px(COUNT_INSET)) |
| 134 | .gap(px(BAR_PADDING)) |
| 135 | .items_center() |
| 136 | .child(icon(IconName::Forward, ICON_SIZE, theme::text_soft())) |
| 137 | .child( |
| 138 | div() |
| 139 | .truncate() |
| 140 | .text_size(px(13.)) |
| 141 | .text_color(theme::text()) |
| 142 | .child(selected_count_label(count)), |
| 143 | ), |
| 144 | ) |
| 145 | .child( |
| 146 | Button::new("forward-footer-cancel") |
| 147 | .ghost() |
| 148 | .label(CANCEL_LABEL) |
| 149 | .on_click(cx.listener(|this, _, _, cx| { |
| 150 | this.cancel_forward_selection(cx); |
| 151 | })), |
| 152 | ) |
| 153 | .child(next_button(count > 0, cx)), |
| 154 | ) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | fn next_button(enabled: bool, cx: &mut Context<ConversationView>) -> Stateful<Div> { |
| 159 | let button = h_flex() |
| 160 | .id("forward-footer-next") |
| 161 | .debug_selector(|| FORWARD_FOOTER_NEXT_TAG.into()) |
| 162 | .h(px(BUTTON_HEIGHT)) |
| 163 | .flex_none() |
| 164 | .px(px(16.)) |
| 165 | .items_center() |
| 166 | .rounded(px(BUTTON_RADIUS)) |
| 167 | .bg(theme::accent()) |
| 168 | .text_color(theme::on_accent()) |
| 169 | .text_size(px(13.)) |
| 170 | .font_weight(FontWeight::SEMIBOLD) |
| 171 | .child(NEXT_LABEL); |
| 172 | if enabled { |
| 173 | button |
| 174 | .cursor_pointer() |
| 175 | .hover(|button| button.bg(theme::accent_text())) |
| 176 | .on_click(cx.listener(|this, _, _, cx| this.forward_next(cx))) |
| 177 | } else { |
| 178 | button.opacity(DISABLED_OPACITY) |
| 179 | } |
| 180 | } |