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