BlueGit

ict rusty-teams

Files

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

call_transcript.rs

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