forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBb02f499Tahinli1 month ago1000 commits

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