edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! The caption panel of the Clip tab: what a selected caption says and where it sits. The words |
| 2 | //! are typed in place, the start and end are number fields that scrub or type, the length reads |
| 3 | //! off them, and the caption's own actions sit under the numbers. The Subtitle style section |
| 4 | //! sits under the panel. Every write is one cue edit, so one undo step. |
| 5 | |
| 6 | use super::common::*; |
| 7 | use crate::player::caption_cues::{CueMenu, CueNum, cue_secs}; |
| 8 | use crate::ui::icons::{Icon, IconSize}; |
| 9 | use crate::ui::kit::{self, ButtonSize, ButtonStyle, NumberEdit}; |
| 10 | use crate::ui::type_scale::Typeset; |
| 11 | use crate::*; |
| 12 | |
| 13 | /// The label column's width, as in the style card, so the two sections line up. |
| 14 | const LABEL_W: f32 = 112.; |
| 15 | |
| 16 | /// How many words the header names the caption by. |
| 17 | const HEAD_WORDS: usize = 4; |
| 18 | |
| 19 | /// One caption action button, a third of the row: the caller's run when it can run, its reason on |
| 20 | /// hover when not. The short `label` fits the row; the tooltip names the action in full. A button |
| 21 | /// that cannot run ignores the press. |
| 22 | #[allow(clippy::too_many_arguments)] |
| 23 | fn caption_action( |
| 24 | id: &'static str, |
| 25 | label: &'static str, |
| 26 | full: &'static str, |
| 27 | icon: Icon, |
| 28 | why: Option<&'static str>, |
| 29 | m: CueMenu, |
| 30 | run: fn(&mut Player, CueMenu, &mut Context<Player>), |
| 31 | cx: &mut Context<Player>, |
| 32 | ) -> Div { |
| 33 | let button = kit::Button::new(id) |
| 34 | .label(label) |
| 35 | .icon(icon) |
| 36 | .style(ButtonStyle::Secondary) |
| 37 | .size(ButtonSize::Small) |
| 38 | .enabled(why.is_none()) |
| 39 | .build() |
| 40 | .w_full() |
| 41 | .tooltip(crate::ui::widgets::tip_hover(full, "", why)); |
| 42 | let button = match why { |
| 43 | Some(_) => button, |
| 44 | None => button.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 45 | cx.stop_propagation(); |
| 46 | run(this, m, cx); |
| 47 | })), |
| 48 | }; |
| 49 | div().flex_1().min_w(px(0.)).child(button) |
| 50 | } |
| 51 | |
| 52 | impl Player { |
| 53 | /// The panel of the selected caption, under a header like a clip's. `None` while no caption |
| 54 | /// is selected. |
| 55 | pub(crate) fn caption_card(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 56 | let (lane, track, cue, start, end) = self.sel_cue_span()?; |
| 57 | let t = crate::ui::theme::tokens(); |
| 58 | let session = self.session.as_ref()?; |
| 59 | let subtitles = session.subtitles().get(track)?; |
| 60 | let c = subtitles.cues.get(cue)?; |
| 61 | let count = subtitles.cues.len(); |
| 62 | let picture = c.image.is_some(); |
| 63 | let typing = self.cue_edit.as_ref().filter(|e| (e.track, e.cue) == (track, cue) && e.in_panel); |
| 64 | let words = match typing { |
| 65 | Some(e) => e.text.clone(), |
| 66 | None => c.text.clone(), |
| 67 | }; |
| 68 | let locked = session.lane_locked(lane); |
| 69 | let shown: AnyElement = match (picture, typing.is_some(), words.is_empty()) { |
| 70 | (true, _, _) => SharedString::from("Picture caption").into_any_element(), |
| 71 | (_, true, _) if typing.is_some_and(|e| e.all) => div().bg(rgba(t.accent_wash)).child(words.clone()).into_any_element(), |
| 72 | (_, true, _) => SharedString::from(format!("{words}\u{258f}")).into_any_element(), |
| 73 | (_, false, true) => SharedString::from("Type the words").into_any_element(), |
| 74 | _ => SharedString::from(words.clone()).into_any_element(), |
| 75 | }; |
| 76 | let field = div() |
| 77 | .id("caption-text") |
| 78 | .w_full() |
| 79 | .min_h(px(56.)) |
| 80 | .px(px(8.)) |
| 81 | .py(px(6.)) |
| 82 | .rounded(px(crate::layout::redesign::R_CONTROL)) |
| 83 | .bg(rgb(t.e2)) |
| 84 | .border_1() |
| 85 | .border_color(if typing.is_some() { rgb(t.accent).into() } else { gpui::transparent_black() }) |
| 86 | .when(!picture, |d| d.cursor(CursorStyle::IBeam)) |
| 87 | .type_style(crate::ui::type_scale::body()) |
| 88 | .text_color(rgb(if words.is_empty() && typing.is_none() { t.ink3 } else { t.ink1 })) |
| 89 | .child(shown) |
| 90 | .when(!picture, |d| { |
| 91 | d.tooltip(crate::ui::widgets::tip_hover("Words", "enter saves, esc cancels", Some("shift+enter starts a new line"))) |
| 92 | .on_mouse_down( |
| 93 | MouseButton::Left, |
| 94 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 95 | cx.stop_propagation(); |
| 96 | this.open_panel_cue_edit(cx); |
| 97 | }), |
| 98 | ) |
| 99 | }); |
| 100 | // A number being dragged shows the value it would take; at rest it shows the caption's own. |
| 101 | let scrub = self.cue_num_scrub; |
| 102 | let num = |id: &'static str, label: &'static str, which: CueNum, us: i64, cx: &mut Context<Self>| { |
| 103 | let shown = scrub.filter(|s| s.which == which).map_or(us, |s| s.cur); |
| 104 | let typing: Option<&NumberEdit> = self.cue_num.as_ref().filter(|(w, _)| *w == which).map(|(_, e)| e); |
| 105 | let enabled = !picture && !locked; |
| 106 | let field = kit::number_field(id, 96., cue_secs(shown), Some("s"), typing, enabled); |
| 107 | match enabled { |
| 108 | false => field.cursor_not_allowed(), |
| 109 | true => field |
| 110 | .tooltip(crate::ui::widgets::tip_hover(label, "", Some("Drag to change, click to type"))) |
| 111 | .on_mouse_down( |
| 112 | MouseButton::Left, |
| 113 | cx.listener(move |this, event: &MouseDownEvent, _, cx| { |
| 114 | cx.stop_propagation(); |
| 115 | this.begin_cue_num_scrub(which, f32::from(event.position.x), cx); |
| 116 | }), |
| 117 | ) |
| 118 | .on_click(cx.listener(move |this, event: &ClickEvent, _, cx| { |
| 119 | // A press that never moved is a click: the number opens for typing, |
| 120 | // unless it is open already. |
| 121 | let moved = match event { |
| 122 | ClickEvent::Mouse(m) => (m.up.position.x - m.down.position.x).abs() > px(3.), |
| 123 | _ => false, |
| 124 | }; |
| 125 | let open = this.cue_num.as_ref().is_some_and(|(w, _)| *w == which); |
| 126 | if !moved && !open { |
| 127 | this.begin_cue_num(which, cx); |
| 128 | } |
| 129 | })), |
| 130 | } |
| 131 | }; |
| 132 | let row = |label: &'static str, control: AnyElement| { |
| 133 | div() |
| 134 | .flex() |
| 135 | .flex_none() |
| 136 | .items_center() |
| 137 | .gap(px(8.)) |
| 138 | .h(px(crate::layout::redesign::CONTROL_M)) |
| 139 | .px(px(6.)) |
| 140 | .child(div().flex_none().w(px(LABEL_W)).child(dark_row_label(label, false))) |
| 141 | .child(control) |
| 142 | }; |
| 143 | let length = format!("{} s", cue_secs(end - start)); |
| 144 | let header = div() |
| 145 | .id("dock-caption-header") |
| 146 | .flex_none() |
| 147 | .h(px(44.)) |
| 148 | .px(px(12.)) |
| 149 | .flex() |
| 150 | .items_center() |
| 151 | .gap(px(8.)) |
| 152 | .child(Icon::Captions.el(IconSize::Inline, t.ink2)) |
| 153 | .child( |
| 154 | div() |
| 155 | .flex_1() |
| 156 | .min_w(px(0.)) |
| 157 | .flex() |
| 158 | .flex_col() |
| 159 | .child({ |
| 160 | let first: Vec<&str> = words.split_whitespace().take(HEAD_WORDS).collect(); |
| 161 | let title = match (picture, first.is_empty()) { |
| 162 | (true, _) => "Picture caption".to_string(), |
| 163 | (_, true) => "No words yet".to_string(), |
| 164 | _ => first.join(" "), |
| 165 | }; |
| 166 | div() |
| 167 | .truncate() |
| 168 | .type_style(crate::ui::type_scale::emph()) |
| 169 | .text_color(rgb(t.ink1)) |
| 170 | .child(title) |
| 171 | }) |
| 172 | .child( |
| 173 | div() |
| 174 | .truncate() |
| 175 | .type_style(crate::ui::type_scale::caption()) |
| 176 | .text_color(rgb(t.ink3)) |
| 177 | .child(format!("Caption {} of {} \u{b7} {}", cue + 1, count, length)), |
| 178 | ), |
| 179 | ); |
| 180 | // The caption's own actions reuse the caption menu's, so a reason shows the same way. |
| 181 | let m = CueMenu { lane, track, cue, at: gpui::point(px(0.), px(0.)) }; |
| 182 | let blocked = |why: Option<&'static str>| match locked { |
| 183 | true => Some("This lane is locked"), |
| 184 | false => why, |
| 185 | }; |
| 186 | let split_why = blocked(self.cue_split_why(m)); |
| 187 | let merge_why = blocked(self.cue_merge_why(m)); |
| 188 | let delete_why = blocked(None); |
| 189 | let actions = div() |
| 190 | .id("caption-actions") |
| 191 | .flex() |
| 192 | .flex_nowrap() |
| 193 | .gap(px(6.)) |
| 194 | .px(px(6.)) |
| 195 | .pt(px(6.)) |
| 196 | .child(caption_action("caption-split", "Split", "Split at playhead", Icon::Scissors, split_why, m, Player::split_cue_at_playhead, cx)) |
| 197 | .child(caption_action("caption-merge", "Merge", "Merge with next", Icon::Link, merge_why, m, Player::merge_cue_with_next, cx)) |
| 198 | .child(caption_action("caption-delete", "Delete", "Delete", Icon::Trash2, delete_why, m, Player::delete_cue_at, cx)); |
| 199 | // The length is the difference of the two numbers, so it reads only. |
| 200 | let length_row = div() |
| 201 | .id("caption-length") |
| 202 | .flex() |
| 203 | .flex_none() |
| 204 | .items_center() |
| 205 | .gap(px(8.)) |
| 206 | .h(px(crate::layout::redesign::CONTROL_M)) |
| 207 | .px(px(6.)) |
| 208 | .child(div().flex_none().w(px(LABEL_W)).child(dark_row_label("Length", false))) |
| 209 | .child(div().type_style(crate::ui::type_scale::body()).text_color(rgb(t.ink2)).child(length)); |
| 210 | Some( |
| 211 | div() |
| 212 | .id("dock-caption") |
| 213 | .flex_none() |
| 214 | .flex() |
| 215 | .flex_col() |
| 216 | .child(header) |
| 217 | .child( |
| 218 | div() |
| 219 | .flex_none() |
| 220 | .flex() |
| 221 | .flex_col() |
| 222 | .gap(px(4.)) |
| 223 | .px(px(12.)) |
| 224 | .pb(px(6.)) |
| 225 | .child(field) |
| 226 | .child(row("Start", num("caption-start", "Start", CueNum::Start, start, cx).into_any_element())) |
| 227 | .child(row("End", num("caption-end", "End", CueNum::End, end, cx).into_any_element())) |
| 228 | .child(length_row) |
| 229 | .child(actions), |
| 230 | ), |
| 231 | ) |
| 232 | } |
| 233 | } |