forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

transcript_panel.rs451 lines, 19.7 KiBHistoryRawDownload
1//! The dock's Transcript tab: the timeline's speech as paragraphs of words. A press on a word
2//! takes the playhead there and starts a selection; drag or shift-click extends it; Delete cuts
3//! it from every lane. Fillers are dimmed, the word being spoken is washed, search matches are
4//! lit. Every state is a background on a word whose box never changes, so nothing moves.
5
6use crate::layout::redesign as r;
7use crate::player::transcript;
8use crate::ui::hitmap;
9use crate::ui::icons::{Icon, IconSize};
10use crate::ui::kit::{Button, ButtonSize, ButtonStyle};
11use crate::ui::stance::{Surface, is_focus_cycle_key};
12use crate::ui::theme::{tokens, with_alpha};
13use crate::ui::type_scale::{self, Typeset};
14use crate::*;
15use engine::speech;
16
17pub(crate) fn transcript_tab(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
18 let t = tokens();
19 let tr = &player.transcript;
20 let fps = player.fps;
21 let frame = player.transcript_frame();
22 let playing = player.session.as_ref().is_some_and(|s| s.is_playing());
23 let words = tr.words.as_deref();
24 let editing = tr.search_edit;
25 let has_query = !tr.search.is_empty();
26 let fillers = words.map_or(0, |w| w.iter().filter(|x| speech::is_filler(&x.text)).count());
27 let match_list = tr.match_list();
28 let matches = match_list.len();
29 let at = tr.search_at.and_then(|c| match_list.iter().position(|&i| i == c));
30 let selection = tr.selection();
31 let live = words.is_some() && !tr.stale;
32
33 let search = div()
34 .id("transcript-search")
35 .flex_1()
36 .min_w(px(0.))
37 .cursor_text()
38 .h(px(r::CONTROL_M))
39 .px(px(8.))
40 .flex()
41 .items_center()
42 .gap(px(6.))
43 .rounded(px(r::R_CONTROL))
44 .border_1()
45 .border_color(if editing { rgb(t.accent).into() } else { gpui::transparent_black() })
46 .bg(rgb(t.e2))
47 .type_style(type_scale::body())
48 .text_color(rgb(if editing || has_query { t.ink1 } else { t.ink3 }))
49 .on_click(cx.listener(|this, _: &ClickEvent, window, cx| {
50 window.focus(&this.focus_inspector);
51 this.transcript.search_edit = true;
52 cx.notify();
53 }))
54 .tooltip(crate::ui::widgets::tip_hover("Search words", "type to find, ↵ jumps to the next", None))
55 .children(hitmap::control("transcript.search", "Search words", true))
56 .child(Icon::Search.el(IconSize::Inline, t.ink3))
57 .child(div().flex_1().min_w(px(0.)).truncate().child(match has_query {
58 false => "Search words".to_string(),
59 true => tr.search.clone(),
60 }))
61 .children(has_query.then(|| {
62 div()
63 .id("transcript-search-clear")
64 .flex_none()
65 .cursor_pointer()
66 .rounded(px(3.))
67 .hover(|s| s.bg(rgba(with_alpha(0xffffff, 0x14))))
68 .child(Icon::X.el(IconSize::Inline, t.ink3))
69 .tooltip(crate::ui::widgets::tip_hover("Clear the search", "Esc", None))
70 .children(hitmap::control("transcript.search.clear", "Clear search", true))
71 .on_mouse_down(MouseButton::Left, cx.listener(|this, _: &MouseDownEvent, _, cx| {
72 this.clear_search();
73 cx.stop_propagation();
74 cx.notify();
75 }))
76 }));
77
78 let step = |id: &'static str, icon: Icon, forward: bool, cx: &mut Context<Player>| {
79 let (tip, keys, name) = match forward {
80 true => ("Next match", "Enter", "Next match"),
81 false => ("Previous match", "Shift+Enter", "Previous match"),
82 };
83 Button::new(id)
84 .icon(icon)
85 .style(ButtonStyle::Ghost)
86 .size(ButtonSize::Small)
87 .enabled(live && matches > 0)
88 .build()
89 .tooltip(crate::ui::widgets::tip_hover(tip, keys, (matches == 0).then_some("Type a word to search for first")))
90 .children(hitmap::control(id, name, live && matches > 0))
91 .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
92 window.focus(&this.focus_inspector);
93 this.step_match(forward, cx);
94 }))
95 };
96 let prev = step("transcript-prev", Icon::ChevronUp, false, cx);
97 let next = step("transcript-next", Icon::ChevronDown, true, cx);
98
99 let delete = Button::new("transcript-delete")
100 .icon(Icon::Trash2)
101 .style(ButtonStyle::Ghost)
102 .enabled(live && selection.is_some())
103 .build()
104 .tooltip(crate::ui::widgets::tip_hover("Cut the selected words", "Delete", Some("Ripples every track as one undo step")))
105 .children(hitmap::control("transcript.delete", "Cut selected words", live && selection.is_some()))
106 .on_click(cx.listener(|this, _: &ClickEvent, window, cx| {
107 window.focus(&this.focus_inspector);
108 this.delete_selected_words(cx);
109 }));
110
111 let remove = Button::new("transcript-fillers")
112 .label(if fillers > 0 { format!("Remove fillers ({fillers})") } else { "Remove fillers".to_string() })
113 .size(ButtonSize::Small)
114 .enabled(live && fillers > 0)
115 .build()
116 .tooltip(crate::ui::widgets::tip_hover(
117 "Remove filler words",
118 "",
119 Some(if fillers > 0 { "um, uh and the like, from every track" } else { "No filler words left" }),
120 ))
121 .children(hitmap::control("transcript.fillers", "Remove fillers", live && fillers > 0))
122 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.remove_transcript_fillers(cx)));
123
124 let again = Button::new("transcript-again")
125 .label("Transcribe again")
126 .size(ButtonSize::Small)
127 .min_w(128.)
128 .build()
129 .tooltip(crate::ui::widgets::tip_hover("Transcribe again", "", Some("The timeline changed, so these words no longer line up")))
130 .children(hitmap::control("transcript.again", "Transcribe again", true))
131 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.start_transcribing(cx)));
132
133 let summary = match (words, tr.stale) {
134 (None, _) => String::new(),
135 (Some(_), true) => "Timeline changed since transcribing".to_string(),
136 (Some(_), false) if has_query => match (matches, at) {
137 (0, _) => "No matches".to_string(),
138 (n, Some(p)) => format!("{} of {n}", p + 1),
139 (n, None) => format!("{n} match{}", if n == 1 { "" } else { "es" }),
140 },
141 (Some(w), false) => match selection {
142 Some((a, b)) => format!("{} selected", b - a + 1),
143 None => format!("{} words", w.len()),
144 },
145 };
146
147 let body: AnyElement = match words {
148 _ if tr.running.is_some() => running_view(player, cx).into_any_element(),
149 None => empty_view(player, cx).into_any_element(),
150 Some(words) => {
151 let active = (playing || frame > 0).then(|| tr.playing_word(frame, fps)).flatten();
152 let paras = tr.paragraphs(fps);
153 if playing
154 && let Some(i) = active
155 && let Some(p) = paras.iter().position(|&(a, b)| (a..=b).contains(&i))
156 && tr.followed.get() != Some(p)
157 {
158 tr.followed.set(Some(p));
159 tr.scroll.scroll_to_item(p);
160 }
161 let rows = paras.iter().map(|&(a, b)| paragraph(player, words, a, b, active, selection, cx).into_any_element());
162 div()
163 .id("transcript-words")
164 .flex_1()
165 .min_h(px(0.))
166 .overflow_y_scroll()
167 .track_scroll(&tr.scroll)
168 .flex()
169 .flex_col()
170 .gap(px(10.))
171 .pb(px(12.))
172 .children(rows)
173 .into_any_element()
174 }
175 };
176
177 div()
178 .id("dock-transcript")
179 .track_focus(&player.focus_inspector)
180 .on_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| {
181 if is_focus_cycle_key(event.keystroke.key.as_str()) {
182 this.focus_surface(Surface::Bench, window, cx);
183 cx.stop_propagation();
184 }
185 }))
186 .on_mouse_up(MouseButton::Left, cx.listener(|this, _: &MouseUpEvent, _, cx| this.release_word(cx)))
187 .on_mouse_up_out(MouseButton::Left, cx.listener(|this, _: &MouseUpEvent, _, cx| this.release_word(cx)))
188 .border_1()
189 .border_color(gpui::transparent_black())
190 .flex_1()
191 .min_h(px(0.))
192 .flex()
193 .flex_col()
194 .gap(px(8.))
195 .px(px(12.))
196 .pt(px(8.))
197 .when(words.is_some() && tr.running.is_none(), |d| {
198 d.child(div().flex_none().flex().items_center().gap(px(4.)).child(search).child(prev).child(next).child(delete)).child(
199 div()
200 .flex_none()
201 .min_h(px(r::CONTROL_S))
202 .flex()
203 .items_center()
204 .justify_between()
205 .gap(px(8.))
206 .child(
207 // A stale line may wrap rather than cut its words short.
208 div()
209 .flex_1()
210 .min_w(px(0.))
211 .type_style(type_scale::caption())
212 .text_color(rgb(if tr.stale { t.warn } else { t.ink3 }))
213 .child(summary),
214 )
215 .child(if tr.stale { again.into_any_element() } else { remove.into_any_element() }),
216 )
217 })
218 .child(body)
219}
220
221/// One paragraph: its start time, then the words wrapping like text.
222fn paragraph(
223 player: &Player,
224 words: &[engine::speech::Spoken],
225 first: usize,
226 last: usize,
227 active: Option<usize>,
228 selection: Option<(usize, usize)>,
229 cx: &mut Context<Player>,
230) -> impl IntoElement {
231 let t = tokens();
232 let fps = player.fps;
233 let secs = f64::from(words[first].start_f) / fps;
234 let stamp = format!("{}:{:02}", (secs / 60.0) as u32, (secs % 60.0) as u32);
235 let stale = player.transcript.stale;
236 div()
237 .id(("transcript-para", first))
238 .flex_none()
239 .flex()
240 .flex_col()
241 .gap(px(2.))
242 .child(div().type_style(type_scale::caption()).text_color(rgb(t.ink3)).child(stamp))
243 .child(
244 div().flex().flex_wrap().children((first..=last).map(|i| {
245 let w = &words[i];
246 let filler = speech::is_filler(&w.text);
247 let selected = selection.is_some_and(|(a, b)| (a..=b).contains(&i));
248 let hit = player.transcript.matches(i);
249 let now = active == Some(i);
250 let fill = if selected {
251 Some(with_alpha(t.accent, 0x66))
252 } else if now {
253 Some(with_alpha(t.accent, 0x3d))
254 } else if hit {
255 Some(with_alpha(t.warn, if player.transcript.search_at == Some(i) { 0xaa } else { 0x55 }))
256 } else {
257 None
258 };
259 let ink = match (stale, filler, now || selected) {
260 (true, ..) => t.ink4,
261 (_, _, true) => t.ink1,
262 (_, true, _) => t.ink4,
263 _ => t.ink2,
264 };
265 div()
266 .id(("transcript-word", i))
267 .flex_none()
268 .px(px(3.))
269 .py(px(1.))
270 // A selected run is one wash with square ends, so the words join like text.
271 .when(!selected, |d| d.rounded(px(3.)))
272 .cursor_pointer()
273 .type_style(type_scale::body())
274 .text_color(rgb(ink))
275 .when_some(fill, |d, f| d.bg(rgba(f)))
276 .when(filler && !stale, |d| d.italic())
277 .when(fill.is_none(), |d| d.hover(move |s| s.bg(rgba(with_alpha(0xffffff, 0x14)))))
278 .when(stale, |d| d.tooltip(crate::ui::widgets::tip_hover("Transcribe again", "", Some("Transcribe again to use the words"))))
279 .when(!stale, |d| {
280 d.on_mouse_down(
281 MouseButton::Left,
282 cx.listener(move |this, ev: &MouseDownEvent, window, cx| {
283 window.focus(&this.focus_inspector);
284 this.transcript.search_edit = false;
285 this.press_word(i, ev.modifiers.shift, cx);
286 }),
287 )
288 .on_mouse_move(cx.listener(move |this, ev: &MouseMoveEvent, _, cx| {
289 if ev.dragging() {
290 this.drag_word(i, cx);
291 }
292 }))
293 })
294 .child(w.text.trim().to_string())
295 })),
296 )
297}
298
299/// Before the first listen: what this does, and the one button that starts it. With no speech
300/// model on the computer it says so plainly and offers to download one.
301fn empty_view(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
302 let t = tokens();
303 let target = transcript::speech_target(player.session.as_ref());
304 let ready = match &target {
305 Some((_, device)) => speech::available(*device),
306 None => speech::available(transcript::speech_download(player.session.as_ref()).1),
307 };
308 let no_model = speech::stt_bin().is_ok() && target.is_none();
309 let not_built = speech::stt_bin().is_err();
310 if let Some((job, _)) = &player.transcript.download {
311 return download_view(player.session.as_ref(), job.fraction(), cx).into_any_element();
312 }
313 let note = match (&ready, player.transcript.status.as_str()) {
314 (Err(_), status) if no_model && !status.is_empty() => format!("{status}. You can also put a model in the models folder yourself."),
315 (Err(_), _) if no_model => {
316 "Transcribing needs a speech model on this computer. It is a one-time download of about 290 MB, kept here, and nothing you edit ever leaves the machine.".to_string()
317 }
318 (Err(_), _) if not_built => "Speech transcription isn't included in this copy of edith.".to_string(),
319 (Err(why), _) => why.clone(),
320 (_, "") => "Listen to the timeline once, then edit it by its words: click a word to go there, select and delete to cut.".to_string(),
321 (_, status) => status.to_string(),
322 };
323 div()
324 .id("transcript-empty")
325 .flex_1()
326 .flex()
327 .flex_col()
328 .items_center()
329 .justify_center()
330 .gap(px(12.))
331 .px(px(12.))
332 .child(Icon::Captions.el(IconSize::Hero, t.ink3))
333 .child(div().text_center().type_style(type_scale::body()).text_color(rgb(t.ink2)).child(note))
334 .children((!not_built).then(|| match no_model {
335 true => Button::new("transcript-download")
336 .label("Download model")
337 .style(ButtonStyle::Primary)
338 .size(ButtonSize::Large)
339 .build()
340 .tooltip(crate::ui::widgets::tip_hover("Download the speech model", "", Some("About 290 MB, once; it stays on this computer")))
341 .children(hitmap::control("transcript.download", "Download model", true))
342 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.start_model_download(cx)))
343 .into_any_element(),
344 false => Button::new("transcript-start")
345 .label("Transcribe")
346 .style(ButtonStyle::Primary)
347 .size(ButtonSize::Large)
348 .enabled(ready.is_ok() && player.session.is_some())
349 .build()
350 .tooltip(crate::ui::widgets::tip_hover("Transcribe the timeline", "", Some("Runs the speech model here; nothing leaves the machine")))
351 .children(hitmap::control("transcript.start", "Transcribe", ready.is_ok()))
352 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.start_transcribing(cx)))
353 .into_any_element(),
354 }))
355 .children(no_model.then(|| {
356 Button::new("transcript-models-folder")
357 .label("Open models folder")
358 .style(ButtonStyle::Ghost)
359 .build()
360 .tooltip(crate::ui::widgets::tip_hover("Open the models folder", "", Some("Put a model folder here yourself instead of downloading")))
361 .children(hitmap::control("transcript.models", "Open models folder", true))
362 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.open_models_folder(cx)))
363 }))
364 .into_any_element()
365}
366
367/// While the speech model downloads: how far, and Cancel.
368fn download_view(session: Option<&PlaybackSession>, fraction: f32, cx: &mut Context<Player>) -> impl IntoElement {
369 let t = tokens();
370 let (model, device) = transcript::speech_download(session);
371 let total = speech::model_bytes(&model, device) as f32;
372 let mb = total * fraction / 1_000_000.;
373 div()
374 .id("transcript-downloading")
375 .flex_1()
376 .flex()
377 .flex_col()
378 .items_center()
379 .justify_center()
380 .gap(px(12.))
381 .px(px(12.))
382 .child(
383 div()
384 .w_full()
385 .flex()
386 .items_center()
387 .justify_between()
388 .type_style(type_scale::body())
389 .text_color(rgb(t.ink2))
390 .child("Downloading the speech model")
391 .child(format!("{mb:.0} of {:.0} MB", total / 1_000_000.)),
392 )
393 .child(
394 div()
395 .w_full()
396 .h(px(6.))
397 .rounded(px(2.))
398 .bg(rgb(t.e2))
399 .child(div().h_full().w(relative(fraction.clamp(0.0, 1.0))).rounded(px(2.)).bg(rgb(t.accent))),
400 )
401 .child(
402 Button::new("transcript-download-cancel")
403 .label("Cancel")
404 .build()
405 .tooltip(crate::ui::widgets::tip_hover("Stop downloading", "", None))
406 .children(hitmap::control("transcript.download.cancel", "Cancel download", true))
407 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.cancel_model_download(cx))),
408 )
409}
410
411/// While the model runs: the same top-aligned progress block the export shows, then Cancel.
412fn running_view(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
413 let t = tokens();
414 let fraction = player.transcript.running.as_ref().map_or(0.0, |r| r.job.fraction()).clamp(0., 1.);
415 div()
416 .id("transcript-running")
417 .flex_1()
418 .flex()
419 .flex_col()
420 .gap(px(10.))
421 .py(px(8.))
422 .child(
423 div()
424 .flex()
425 .items_center()
426 .gap(px(8.))
427 .type_style(type_scale::title())
428 .text_color(rgb(t.ink1))
429 .child(Icon::Captions.el(IconSize::Toolbar, t.accent))
430 .child("Transcribing"),
431 )
432 .child(
433 div().h(px(6.)).rounded(px(3.)).bg(rgb(t.e3)).child(div().h_full().w(relative(fraction)).rounded(px(3.)).bg(rgb(t.accent))),
434 )
435 .child(
436 div()
437 .type_style(type_scale::body())
438 .text_color(rgb(t.ink1))
439 .child(format!("Transcribing \u{b7} {:.0}%", fraction * 100.)),
440 )
441 .child(
442 div().flex().pt(px(6.)).child(
443 Button::new("transcript-cancel")
444 .label("Cancel")
445 .build()
446 .tooltip(crate::ui::widgets::tip_hover("Stop transcribing", "", None))
447 .children(hitmap::control("transcript.cancel", "Cancel", true))
448 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.cancel_transcribing(cx))),
449 ),
450 )
451}