BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / call_transcript.rs

call_transcript.rs

call_transcript.rs 6.8 KiB Download Raw
1use gpui_kit::component::scroll::ScrollableElement as _;
2use gpui_kit::assets::IconName;
3use gpui_kit::component::{
4 h_flex,
5 input::{Input, InputEvent, InputState},
6 v_flex,
7};
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10
11use super::call_view::side_panel;
12use super::scroll_edge::with_top_edge;
13use super::widgets::icon;
14use crate::app_state::AppState;
15use crate::call::clock_label;
16use crate::theme;
17
18const TRANSCRIPT_SCROLL_DIVIDER: &str = "shell/islands/main/call-transcript/divider-scroll";
19const SEARCH_HEIGHT: f32 = 34.;
20const AT_BOTTOM_SLACK: f32 = 4.;
21const EMPTY_TEXT: &str = "Nothing said yet. Spoken words appear here.";
22const NO_MATCH_TEXT: &str = "No entry matches the search.";
23
24pub 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
33impl 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
95impl 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.is_empty().then_some(if empty { EMPTY_TEXT } else { NO_MATCH_TEXT });
139 let app = self.app.clone();
140 let search = h_flex()
141 .h(px(SEARCH_HEIGHT))
142 .mx(px(12.))
143 .px(px(10.))
144 .gap(px(8.))
145 .flex_none()
146 .items_center()
147 .rounded(px(6.))
148 .border_1()
149 .border_color(theme::border())
150 .child(icon(IconName::Search, 14., theme::text_muted()))
151 .child(
152 div()
153 .flex_1()
154 .child(Input::new(&self.search).appearance(false).bordered(false)),
155 );
156 let list = div()
157 .id("call-transcript-list")
158 .flex_1()
159 .min_h_0()
160 .overflow_y_scrollbar()
161 .track_scroll(&self.scroll)
162 .on_scroll_wheel(cx.listener(|this, _, _, cx| {
163 this.scrolled = true;
164 cx.notify();
165 }))
166 .child(
167 v_flex()
168 .px(px(20.))
169 .py(px(12.))
170 .gap(px(12.))
171 .children(rows)
172 .children(hint.map(|text| {
173 div()
174 .text_size(px(13.))
175 .text_color(theme::text_muted())
176 .child(text)
177 })),
178 );
179 let body = v_flex()
180 .size_full()
181 .min_h_0()
182 .gap(px(8.))
183 .child(search)
184 .child(
185 with_top_edge(list, &self.scroll, TRANSCRIPT_SCROLL_DIVIDER)
186 .when(!self.following, |area| area.child(self.jump_chip(cx))),
187 );
188 side_panel(
189 "call-transcript-panel",
190 "Transcript",
191 "Close the transcript",
192 move |cx| app.update(cx, |state, cx| state.close_call_transcript(cx)),
193 body,
194 )
195 .debug_selector(|| "shell/islands/main/call-transcript".into())
196 }
197}