| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::scroll::ScrollableElement as _; |
| 3 | use gpui_kit::component::{ |
| 4 | h_flex, |
| 5 | input::{Input, InputEvent, InputState}, |
| 6 | v_flex, |
| 7 | }; |
| 8 | use gpui_kit::prelude::FluentBuilder as _; |
| 9 | use gpui_kit::*; |
| 10 | |
| 11 | use super::call_view::side_panel; |
| 12 | use super::scroll_edge::with_top_edge; |
| 13 | use super::widgets::icon; |
| 14 | use crate::app_state::AppState; |
| 15 | use crate::call::clock_label; |
| 16 | use crate::theme; |
| 17 | |
| 18 | const TRANSCRIPT_SCROLL_DIVIDER: &str = "shell/islands/main/call-transcript/divider-scroll"; |
| 19 | const SEARCH_HEIGHT: f32 = 34.; |
| 20 | const AT_BOTTOM_SLACK: f32 = 4.; |
| 21 | const EMPTY_TEXT: &str = "Nothing said yet. Spoken words appear here."; |
| 22 | const NO_MATCH_TEXT: &str = "No entry matches the search."; |
| 23 | |
| 24 | pub struct TranscriptPanel { |
| 25 | app: Entity<AppState>, |
| 26 | search: Entity<InputState>, |
| 27 | scroll: ScrollHandle, |
| 28 | following: bool, |
| 29 | scrolled: bool, |
| 30 | _subscriptions: Vec<Subscription>, |
| 31 | } |
| 32 | |
| 33 | impl TranscriptPanel { |
| 34 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 35 | let search = cx.new(|cx| InputState::new(window, cx).placeholder("Search transcript")); |
| 36 | let subscriptions = vec![ |
| 37 | cx.observe(&app, |_, _, cx| cx.notify()), |
| 38 | cx.subscribe_in(&search, window, |_, _, event: &InputEvent, _, cx| { |
| 39 | if matches!(event, InputEvent::Change) { |
| 40 | cx.notify(); |
| 41 | } |
| 42 | }), |
| 43 | ]; |
| 44 | TranscriptPanel { |
| 45 | app, |
| 46 | search, |
| 47 | scroll: ScrollHandle::new(), |
| 48 | following: true, |
| 49 | scrolled: false, |
| 50 | _subscriptions: subscriptions, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn at_bottom(&self) -> bool { |
| 55 | let offset = self.scroll.offset().y; |
| 56 | let reach = self.scroll.max_offset().y; |
| 57 | f32::from(offset + reach) <= AT_BOTTOM_SLACK |
| 58 | } |
| 59 | |
| 60 | fn jump_to_latest(&mut self, cx: &mut Context<Self>) { |
| 61 | self.following = true; |
| 62 | self.scroll.scroll_to_bottom(); |
| 63 | cx.notify(); |
| 64 | } |
| 65 | |
| 66 | fn jump_chip(&self, cx: &mut Context<Self>) -> Stateful<Div> { |
| 67 | div() |
| 68 | .id("call-transcript-jump") |
| 69 | .absolute() |
| 70 | .bottom(px(12.)) |
| 71 | .left_0() |
| 72 | .right_0() |
| 73 | .flex() |
| 74 | .justify_center() |
| 75 | .child( |
| 76 | h_flex() |
| 77 | .id("call-transcript-jump-chip") |
| 78 | .h(px(28.)) |
| 79 | .px(px(12.)) |
| 80 | .gap(px(6.)) |
| 81 | .items_center() |
| 82 | .rounded_full() |
| 83 | .bg(theme::accent()) |
| 84 | .text_color(theme::white()) |
| 85 | .text_size(px(12.)) |
| 86 | .font_weight(FontWeight::SEMIBOLD) |
| 87 | .cursor_pointer() |
| 88 | .child(icon(IconName::ArrowDown, 13., theme::white())) |
| 89 | .child("Jump to latest") |
| 90 | .on_click(cx.listener(|this, _, _, cx| this.jump_to_latest(cx))), |
| 91 | ) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl Render for TranscriptPanel { |
| 96 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 97 | if std::mem::take(&mut self.scrolled) { |
| 98 | self.following = self.at_bottom(); |
| 99 | } |
| 100 | if self.following { |
| 101 | self.scroll.scroll_to_bottom(); |
| 102 | } |
| 103 | let query = self.search.read(cx).value().to_string(); |
| 104 | let state = self.app.read(cx); |
| 105 | let transcript = state.call.as_ref().map(|call| &call.model.transcript); |
| 106 | let empty = transcript.is_none_or(|transcript| transcript.is_empty()); |
| 107 | let rows: Vec<AnyElement> = transcript |
| 108 | .map(|transcript| transcript.filtered(&query)) |
| 109 | .unwrap_or_default() |
| 110 | .into_iter() |
| 111 | .map(|entry| { |
| 112 | v_flex() |
| 113 | .gap(px(2.)) |
| 114 | .child( |
| 115 | h_flex() |
| 116 | .gap(px(8.)) |
| 117 | .items_baseline() |
| 118 | .child( |
| 119 | div() |
| 120 | .min_w_0() |
| 121 | .truncate() |
| 122 | .text_size(px(13.)) |
| 123 | .font_weight(FontWeight::BOLD) |
| 124 | .child(entry.speaker.clone()), |
| 125 | ) |
| 126 | .child( |
| 127 | div() |
| 128 | .flex_none() |
| 129 | .text_size(px(11.)) |
| 130 | .text_color(theme::text_muted()) |
| 131 | .child(clock_label(entry.offset)), |
| 132 | ), |
| 133 | ) |
| 134 | .child(div().text_size(px(13.)).child(entry.text.clone())) |
| 135 | .into_any_element() |
| 136 | }) |
| 137 | .collect(); |
| 138 | let hint = rows |
| 139 | .is_empty() |
| 140 | .then_some(if empty { EMPTY_TEXT } else { NO_MATCH_TEXT }); |
| 141 | let app = self.app.clone(); |
| 142 | let search = h_flex() |
| 143 | .h(px(SEARCH_HEIGHT)) |
| 144 | .mx(px(12.)) |
| 145 | .px(px(10.)) |
| 146 | .gap(px(8.)) |
| 147 | .flex_none() |
| 148 | .items_center() |
| 149 | .rounded(px(6.)) |
| 150 | .border_1() |
| 151 | .border_color(theme::border()) |
| 152 | .child(icon(IconName::Search, 14., theme::text_muted())) |
| 153 | .child( |
| 154 | div() |
| 155 | .flex_1() |
| 156 | .child(Input::new(&self.search).appearance(false).bordered(false)), |
| 157 | ); |
| 158 | let list = div() |
| 159 | .id("call-transcript-list") |
| 160 | .flex_1() |
| 161 | .min_h_0() |
| 162 | .overflow_y_scrollbar() |
| 163 | .track_scroll(&self.scroll) |
| 164 | .on_scroll_wheel(cx.listener(|this, _, _, cx| { |
| 165 | this.scrolled = true; |
| 166 | cx.notify(); |
| 167 | })) |
| 168 | .child( |
| 169 | v_flex() |
| 170 | .px(px(20.)) |
| 171 | .py(px(12.)) |
| 172 | .gap(px(12.)) |
| 173 | .children(rows) |
| 174 | .children(hint.map(|text| { |
| 175 | div() |
| 176 | .text_size(px(13.)) |
| 177 | .text_color(theme::text_muted()) |
| 178 | .child(text) |
| 179 | })), |
| 180 | ); |
| 181 | let body = v_flex() |
| 182 | .size_full() |
| 183 | .min_h_0() |
| 184 | .gap(px(8.)) |
| 185 | .child(search) |
| 186 | .child( |
| 187 | with_top_edge(list, &self.scroll, TRANSCRIPT_SCROLL_DIVIDER) |
| 188 | .when(!self.following, |area| area.child(self.jump_chip(cx))), |
| 189 | ); |
| 190 | side_panel( |
| 191 | "call-transcript-panel", |
| 192 | "Transcript", |
| 193 | "Close the transcript", |
| 194 | move |cx| app.update(cx, |state, cx| state.close_call_transcript(cx)), |
| 195 | body, |
| 196 | ) |
| 197 | .debug_selector(|| "shell/islands/main/call-transcript".into()) |
| 198 | } |
| 199 | } |