forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

title.rs559 lines, 25.0 KiBHistoryRawDownload
1//! The Title section of the Clip tab: the words, a one-click style, where it sits, how it is
2//! drawn, when it is up and how it fades. It is in the inspector while a title is selected (from
3//! the timeline's title row, the picture, or File > New > Title); every control writes one undo
4//! step through [`Player::write_title`], and the viewer shows the words and numbers live.
5
6use super::common::*;
7use super::curve::CurveTarget;
8use super::keys::diamond;
9use crate::player::titling::{HexTarget, ScrubField, anim_default_frames, get_field, hex_of, preset_label};
10use engine::titleanim::TitleAnim;
11use engine::project::TitlePreset as Preset;
12use crate::ui::icons::Icon;
13use crate::player::generators::GenPaint;
14use crate::ui::kit::{self, NumberEdit, ScrubLook, Segment};
15use crate::ui::type_scale::Typeset;
16use crate::*;
17use engine::text::{Align, HPlace, Outline, Place, Weight};
18
19fn hsla_of(c: u32) -> gpui::Hsla {
20 rgb(c).into()
21}
22
23/// A label with its control at the right, the shape every row of the section has.
24fn row(label: &'static str, control: impl IntoElement) -> Div {
25 div()
26 .w_full()
27 .flex()
28 .items_center()
29 .justify_between()
30 .gap(px(8.))
31 .child(dark_row_label(label, false))
32 .child(control)
33}
34
35/// What a number row says when it can change: the drag and the typing.
36const DRAG_HINT: &str = "Drag to change, click to type";
37/// The size row's tooltip: what the number is.
38const SIZE_HINT: &str = "Text height as a share of the picture height";
39
40/// The letters' swatches: white, the warm and cool tones a lower third wears, and black.
41const INK_SWATCHES: [[u8; 3]; 8] = [
42 [0xff, 0xff, 0xff],
43 [0xff, 0xcc, 0x00],
44 [0xff, 0x8a, 0x3d],
45 [0xff, 0x4d, 0x4d],
46 [0x4d, 0xd2, 0xff],
47 [0x7b, 0xe0, 0x7b],
48 [0xc9, 0xa0, 0xff],
49 [0x11, 0x11, 0x11],
50];
51
52/// The animation names the Entrance and Exit lists show, in [`TitleAnim::ALL`]'s order.
53const ANIM_NAMES: [&str; 7] = ["None", "Fade", "Typewriter", "Slide up", "Slide left", "Pop", "Word by word"];
54
55/// Which side of a title an animation list sets.
56#[derive(Clone, Copy, PartialEq, Eq)]
57enum AnimSide {
58 In,
59 Out,
60}
61
62/// The box's swatches: black (the default), deep tones, white and yellow.
63const BOX_SWATCHES: [[u8; 3]; 8] = [
64 [0x00, 0x00, 0x00],
65 [0x11, 0x22, 0x33],
66 [0x1f, 0x3a, 0x5f],
67 [0x5a, 0x1a, 0x24],
68 [0x1d, 0x4d, 0x3a],
69 [0x44, 0x44, 0x44],
70 [0xff, 0xff, 0xff],
71 [0xff, 0xcc, 0x00],
72];
73
74impl Player {
75 /// An Entrance or Exit row: the animation in force in the field, and a list of the eight
76 /// beneath it. Picking one sets it and moves the playhead to the title's start, so the
77 /// animation shows in the viewer at once.
78 fn title_anim_row(&self, side: AnimSide, current: TitleAnim, fps: f64, start: u32, cx: &mut Context<Self>) -> Div {
79 let (label, id, list_id) = match side {
80 AnimSide::In => ("Entrance", "title-in", "title-in-list"),
81 AnimSide::Out => ("Exit", "title-out", "title-out-list"),
82 };
83 let open = match side {
84 AnimSide::In => self.title_in_open,
85 AnimSide::Out => self.title_out_open,
86 };
87 let at = TitleAnim::ALL.iter().position(|a| *a == current).unwrap_or(0);
88 let field = kit::dropdown_field(id, ANIM_NAMES[at], true).on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
89 this.title_in_open = side == AnimSide::In && !this.title_in_open;
90 this.title_out_open = side == AnimSide::Out && !this.title_out_open;
91 cx.notify();
92 }));
93 let list = open.then(|| {
94 let backdrop = div().absolute().top(px(-4000.)).left(px(-4000.)).size(px(8000.)).on_mouse_down(
95 MouseButton::Left,
96 cx.listener(|this, _: &MouseDownEvent, _, cx| {
97 this.title_in_open = false;
98 this.title_out_open = false;
99 cx.stop_propagation();
100 cx.notify();
101 }),
102 );
103 let names: Vec<SharedString> = ANIM_NAMES.iter().map(|&n| n.into()).collect();
104 // The list opens above the field when the window has no room below it.
105 let above = kit::list_opens_above(id, names.len());
106 let me = cx.entity();
107 let choices = kit::choice_list(list_id, 160., &names, at, move |pick, _, app| {
108 me.update(app, |this, cx| {
109 this.title_in_open = false;
110 this.title_out_open = false;
111 let anim = TitleAnim::ALL[pick];
112 let frames = anim_default_frames(fps);
113 this.write_title(
114 |t| {
115 t.adopt_fades();
116 match side {
117 AnimSide::In => t.in_anim = anim,
118 AnimSide::Out => t.out_anim = anim,
119 }
120 if t.anim_frames == 0 {
121 t.anim_frames = frames.min(t.frames);
122 }
123 },
124 cx,
125 );
126 this.seek(f64::from(start) / fps, cx);
127 });
128 });
129 let gap = px(crate::layout::redesign::CONTROL_M + 2.);
130 let at = div().absolute().right(px(0.)).child(choices);
131 let at = if above { at.bottom(gap) } else { at.top(gap) };
132 gpui::deferred(div().absolute().top(px(0.)).right(px(0.)).child(backdrop).child(at))
133 });
134 row(label, div().relative().w(px(150.)).child(field).children(list))
135 }
136
137 /// A colour row: the hex field at the right, with a line of swatches under the label. The
138 /// swatch of the current colour wears the accent ring (every swatch has a ring, transparent
139 /// when off, so choosing one moves nothing). `enabled` false dims it and ignores clicks.
140 #[allow(clippy::too_many_arguments)]
141 fn title_colour_rows(
142 &self,
143 id: &'static str,
144 label: &'static str,
145 swatches: &'static [[u8; 3]],
146 current: [u8; 3],
147 target: HexTarget,
148 enabled: bool,
149 cx: &mut Context<Self>,
150 ) -> Div {
151 let tk = crate::ui::theme::tokens();
152 let editing = self.title_hex.as_ref().filter(|(t, _)| *t == target);
153 let value = match editing {
154 Some((_, f)) => f.text.clone(),
155 None => format!("#{}", hex_of(current).to_uppercase()),
156 };
157 let hex_id = if target == HexTarget::Ink { "title-ink-hex" } else { "title-box-hex" };
158 let field = kit::scrub_field(
159 hex_id,
160 76.,
161 value,
162 None,
163 // Dimmed by the row, not the field, so the reason stays readable.
164 true,
165 if editing.is_some() { Some(ScrubLook::Typing) } else { None },
166 )
167 .cursor(CursorStyle::IBeam)
168 .tooltip(crate::ui::widgets::tip_hover(label, "type hex", Some("Six hex digits such as ffcc00; enter to apply")))
169 .when(enabled, |el| {
170 el.on_mouse_down(
171 MouseButton::Left,
172 cx.listener(move |this, _: &MouseDownEvent, _, cx| {
173 cx.stop_propagation();
174 this.commit_gen_hex(cx);
175 this.begin_title_hex(target, cx);
176 }),
177 )
178 });
179 let dots = div().w_full().flex().items_center().justify_between().children(swatches.iter().enumerate().map(|(i, rgb)| {
180 let rgb = *rgb;
181 let on = rgb == current && editing.is_none();
182 let [r, g, b] = rgb;
183 div()
184 .id((id, i))
185 .flex_none()
186 .size(px(22.))
187 .p(px(2.))
188 .rounded_full()
189 .border_2()
190 .border_color(if on { hsla_of(tk.accent) } else { gpui::transparent_black() })
191 .when(enabled, |el| el.cursor_pointer())
192 .child(
193 div()
194 .size_full()
195 .rounded_full()
196 .border_1()
197 .border_color(hsla_of(tk.line))
198 .bg(hsla_of(u32::from_be_bytes([0, r, g, b]))),
199 )
200 .tooltip(crate::ui::widgets::tip_hover(&format!("#{}", hex_of(rgb)), "", None))
201 .when(enabled, |el| {
202 el.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
203 this.commit_title_hex(cx);
204 this.write_title(
205 |t| match target {
206 HexTarget::Ink => t.style.ink = rgb,
207 HexTarget::Box => t.style.box_color = [rgb[0], rgb[1], rgb[2], t.style.box_color[3]],
208 },
209 cx,
210 );
211 }))
212 })
213 }));
214 div()
215 .w_full()
216 .flex()
217 .flex_col()
218 .gap(px(CARD_GAP))
219 .child(row(label, field).when(!enabled, |d| d.opacity(0.5)))
220 .child(dots.when(!enabled, |d| d.opacity(0.4)))
221 }
222
223 /// One number: drag it sideways to scrub, click it to type. `why` greys the row out.
224 #[allow(clippy::too_many_arguments)]
225 fn title_scrub_row(
226 &self,
227 id: &'static str,
228 label: &'static str,
229 value: String,
230 unit: Option<&'static str>,
231 field: ScrubField,
232 why: Option<&'static str>,
233 hint: &'static str,
234 cx: &mut Context<Self>,
235 ) -> Div {
236 let live = self.title_scrub.is_some_and(|s| s.field == field);
237 let enabled = why.is_none();
238 let typing = self.gen_hex.as_ref().filter(|(p, _)| *p == GenPaint::Title(field)).map(|(_, f)| NumberEdit::begin(&f.text));
239 let control = match &typing {
240 Some(_) => kit::number_field(id, 64., value.clone(), unit, typing.as_ref(), true),
241 None => kit::scrub_field(id, 64., value.clone(), unit, true, live.then_some(ScrubLook::Hover)),
242 }
243 .tooltip(crate::ui::widgets::overlay_tip_hover(label, "", Some(why.unwrap_or(hint))));
244 let line = row(
245 label,
246 if enabled {
247 control
248 .on_mouse_down(
249 MouseButton::Left,
250 cx.listener(move |this, event: &MouseDownEvent, _, cx| {
251 this.begin_title_scrub(field, f32::from(event.position.x), cx);
252 }),
253 )
254 .on_click(cx.listener(move |this, event: &ClickEvent, _, cx| {
255 // A press that never moved is a click: the number opens for typing.
256 let moved = match event {
257 ClickEvent::Mouse(m) => (m.up.position.x - m.down.position.x).abs() > px(3.),
258 _ => false,
259 };
260 if !moved {
261 this.begin_title_num(field, value.clone(), cx);
262 }
263 }))
264 } else {
265 control.cursor_not_allowed()
266 },
267 );
268 line.when(!enabled, |d| d.opacity(0.5))
269 }
270
271 /// Opens a Title number for typing on `shown`, all of it selected.
272 fn begin_title_num(&mut self, field: ScrubField, shown: String, cx: &mut Context<Self>) {
273 self.commit_title_draft(cx);
274 self.commit_gen_hex(cx);
275 self.gen_hex = Some((GenPaint::Title(field), crate::player::text_field::TextField::selected(shown)));
276 cx.notify();
277 }
278
279 /// The Title section, or nothing while no title is selected.
280 pub(crate) fn title_card(&self, room: Size<Pixels>, cx: &mut Context<Self>) -> Option<impl IntoElement> {
281 let (idx, stored) = self.title_target()?;
282 let t = self.shown_title(&stored);
283 let fps = self.session.as_ref().map_or(25., |s| s.meta().frame_rate).max(1.);
284 // Every time reads in seconds with one decimal and its unit.
285 let secs = move |frames: u32| format!("{:.1}", f64::from(frames) / fps);
286 let me = cx.entity();
287 let typing = self.title_draft.is_some();
288 let words = match &self.title_draft {
289 Some(f) => f.text.clone(),
290 None => t.text.clone(),
291 };
292
293 let presets = div().w_full().flex().flex_wrap().gap(px(4.)).children(Preset::ALL.into_iter().enumerate().map(|(i, p)| {
294 kit::Button::new(["title-preset-0", "title-preset-1", "title-preset-2"][i])
295 .label(preset_label(p))
296 .style(kit::ButtonStyle::Secondary)
297 .size(kit::ButtonSize::Small)
298 .build()
299 .tooltip(crate::ui::widgets::tip_hover(preset_label(p), "", Some("Replaces the look; keeps the words and timing")))
300 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| this.apply_title_preset(p, cx)))
301 .into_any_element()
302 }));
303
304 let tk = crate::ui::theme::tokens();
305 // Words selected for typing over (a new title, ctrl+A) wear the accent wash.
306 let all = self.title_draft.as_ref().is_some_and(|f| f.all && !f.text.is_empty());
307 let shown: AnyElement = match (words.is_empty(), typing) {
308 (true, false) => SharedString::from("Type the words").into_any_element(),
309 (_, true) if all => div().bg(rgba(tk.accent_wash)).child(words.clone()).into_any_element(),
310 (_, true) => SharedString::from(format!("{words}▏")).into_any_element(),
311 _ => SharedString::from(words.clone()).into_any_element(),
312 };
313 let field = div()
314 .id("title-text")
315 .w_full()
316 .min_h(px(72.))
317 .px(px(8.))
318 .py(px(6.))
319 .rounded(px(crate::layout::redesign::R_CONTROL))
320 .bg(rgb(tk.e2))
321 .border_1()
322 .border_color(if typing { rgb(tk.accent).into() } else { gpui::transparent_black() })
323 .cursor(CursorStyle::IBeam)
324 .type_style(crate::ui::type_scale::body())
325 .text_color(rgb(if words.is_empty() && !typing { tk.ink3 } else { tk.ink1 }))
326 .child(shown)
327 .tooltip(crate::ui::widgets::tip_hover("Words", "esc to finish", Some("enter starts a new line")))
328 .on_mouse_down(
329 MouseButton::Left,
330 cx.listener(|this, _: &MouseDownEvent, _, cx| {
331 cx.stop_propagation();
332 this.commit_gen_hex(cx);
333 if this.title_draft.is_none()
334 && let Some((_, t)) = this.title_target()
335 {
336 this.title_draft = Some(crate::player::text_field::TextField { text: t.text, all: false });
337 cx.notify();
338 }
339 }),
340 );
341
342 let place = kit::segmented(
343 "title-place",
344 &[Segment::text("Top"), Segment::text("Centre"), Segment::text("Bottom")],
345 match t.style.place {
346 Place::Top => 0,
347 Place::Centre => 1,
348 Place::Bottom => 2,
349 },
350 true,
351 None,
352 {
353 let me = me.clone();
354 move |pick, _, app| {
355 me.update(app, |this, cx| {
356 let place = [Place::Top, Place::Centre, Place::Bottom][pick];
357 this.write_title(|t| t.style.place = place, cx);
358 })
359 }
360 },
361 );
362 let outline = kit::segmented(
363 "title-outline",
364 &[Segment::text("None"), Segment::text("Thin"), Segment::text("Thick")],
365 match t.style.outline {
366 Outline::None => 0,
367 Outline::Thin => 1,
368 Outline::Thick => 2,
369 },
370 true,
371 None,
372 {
373 let me = me.clone();
374 move |pick, _, app| {
375 me.update(app, |this, cx| {
376 let outline = [Outline::None, Outline::Thin, Outline::Thick][pick];
377 this.write_title(|t| t.style.outline = outline, cx);
378 })
379 }
380 },
381 );
382 let side = kit::segmented(
383 "title-side",
384 &[Segment::text("Left"), Segment::text("Centre"), Segment::text("Right")],
385 match t.style.hplace {
386 HPlace::Left => 0,
387 HPlace::Centre => 1,
388 HPlace::Right => 2,
389 },
390 true,
391 None,
392 {
393 let me = me.clone();
394 move |pick, _, app| {
395 me.update(app, |this, cx| {
396 let hplace = [HPlace::Left, HPlace::Centre, HPlace::Right][pick];
397 this.write_title(|t| t.style.hplace = hplace, cx);
398 })
399 }
400 },
401 );
402 let weight = kit::segmented(
403 "title-weight",
404 &[Segment::text("Regular"), Segment::text("Bold")],
405 usize::from(t.style.weight == Weight::Bold),
406 true,
407 None,
408 {
409 let me = me.clone();
410 move |pick, _, app| {
411 me.update(app, |this, cx| {
412 let weight = [Weight::Regular, Weight::Bold][pick];
413 this.write_title(|t| t.style.weight = weight, cx);
414 })
415 }
416 },
417 );
418 let align = kit::segmented(
419 "title-align",
420 &[Segment::text("Left"), Segment::text("Centre"), Segment::text("Right")],
421 match t.style.align {
422 Align::Left => 0,
423 Align::Centre => 1,
424 Align::Right => 2,
425 },
426 true,
427 None,
428 {
429 let me = me.clone();
430 move |pick, _, app| {
431 me.update(app, |this, cx| {
432 let align = [Align::Left, Align::Centre, Align::Right][pick];
433 this.write_title(|t| t.style.align = align, cx);
434 })
435 }
436 },
437 );
438 let box_rgb = [t.style.box_color[0], t.style.box_color[1], t.style.box_color[2]];
439 let boxed = kit::switch("title-box", t.style.boxed, true, None)
440 .tooltip(crate::ui::widgets::tip_hover("Background box", "", Some("A translucent black plate behind the words")))
441 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
442 this.write_title(|t| t.style.boxed = !t.style.boxed, cx);
443 }));
444
445 // The title's keys: the section reads and writes the key at the playhead, so the playhead has
446 // to be inside the title. Outside it the section dims and says why on hover.
447 let at = self.title_key_frame(&t);
448 let why_out: Option<&'static str> = at.is_none().then_some("Move the playhead into the title");
449 let keyed = self.title_key_here(idx).is_some();
450 let ink = match (keyed, !t.keys.is_empty()) {
451 (true, _) => tk.accent,
452 (false, true) => tk.ink1,
453 _ => tk.ink3,
454 };
455 let tip: &str = match (at.is_some(), keyed) {
456 (false, _) => "Move the playhead into the title",
457 (true, true) => "Remove the title's key at the playhead",
458 (true, false) => "Key the title at the playhead",
459 };
460 let key_dot = div()
461 .id("title-key-diamond")
462 .flex_none()
463 .size(px(crate::layout::redesign::CONTROL_M))
464 .flex()
465 .items_center()
466 .justify_center()
467 .rounded(px(crate::layout::redesign::R_CONTROL))
468 .cursor_pointer()
469 .hover(|s| s.bg(rgb(tk.e3)))
470 .tooltip(crate::ui::widgets::overlay_tip_hover(tip, "", None))
471 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_title_key(cx)))
472 .child(diamond(12., ink, keyed));
473 let (m_prev, m_next) = (cx.entity(), cx.entity());
474 let nav = div()
475 .flex()
476 .items_center()
477 .gap(px(2.))
478 .child(kit::icon_ghost("title-key-prev", Icon::ChevronLeft, "Previous key", why_out, move |_, app| {
479 m_prev.update(app, |this, cx| this.jump_title_key(false, cx));
480 }))
481 .child(key_dot)
482 .child(kit::icon_ghost("title-key-next", Icon::ChevronRight, "Next key", why_out, move |_, app| {
483 m_next.update(app, |this, cx| this.jump_title_key(true, cx));
484 }))
485 .when(at.is_none(), |d| d.opacity(0.5));
486 let keys_row = row("Keyframes", nav);
487 let eases = div().w_full().when(at.is_none(), |d| d.opacity(0.5)).child(self.ease_picker(
488 CurveTarget::Title(idx),
489 move |this, ease, cx| this.set_title_key_ease_here(idx, ease, cx),
490 cx,
491 ));
492 // The four numbers read the key at the playhead, or the rest a title holds with no key there.
493 let key_val = |field: ScrubField| match at {
494 Some(at) => get_field(&t, field, at).to_string(),
495 None => "-".to_string(),
496 };
497
498 // The header (icon, words, length) is the dock's, like a clip's; the remove verb ends the card.
499 let remove = kit::Button::new("title-remove")
500 .label("Remove title")
501 .icon(Icon::Trash2)
502 .style(kit::ButtonStyle::Ghost)
503 .build()
504 .tooltip(crate::ui::widgets::tip_hover("Remove title", &self.keymap.chord(ActionId::Delete), None))
505 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.remove_title_selected(cx)));
506
507 Some(
508 self.card_outer(room, cx).child(
509 self.card_surface(div().w_full().flex().flex_col().gap(px(CARD_GAP)).px(px(6.)).py(px(6.)))
510 .id("title-card")
511 .child(presets)
512 .child(field)
513 .child(row("Position", place))
514 .child(row("Side", side))
515 .child(row("Align lines", align))
516 .child(row("Weight", weight))
517 .child(self.title_colour_rows("title-ink", "Colour", &INK_SWATCHES, t.style.ink, HexTarget::Ink, true, cx))
518 .child(row("Outline", outline))
519 .child(row("Background box", boxed))
520 .child(self.title_colour_rows("title-boxc", "Box colour", &BOX_SWATCHES, box_rgb, HexTarget::Box, t.style.boxed, cx))
521 .child(
522 self.title_scrub_row(
523 "title-box-opacity",
524 "Box opacity",
525 format!("{}", (u32::from(t.style.box_color[3]) * 100 + 127) / 255),
526 Some("%"),
527 ScrubField::BoxOpacity,
528 (!t.style.boxed).then_some("Turn on Background box"),
529 DRAG_HINT,
530 cx,
531 )
532 )
533 // The engine keeps the size per mille of the picture height; the card shows the share.
534 .child(self.title_scrub_row("title-size", "Size", format!("{:.1}", f64::from(t.style.size) / 10.), Some("%"), ScrubField::Size, None, SIZE_HINT, cx))
535 .child(self.title_scrub_row("title-start", "Start", secs(t.start), Some("s"), ScrubField::Start, None, DRAG_HINT, cx))
536 .child(self.title_scrub_row("title-length", "Length", secs(t.frames), Some("s"), ScrubField::Length, None, DRAG_HINT, cx))
537 .child(self.title_anim_row(AnimSide::In, t.entrance().0, fps, t.start, cx))
538 .child(self.title_anim_row(AnimSide::Out, t.exit().0, fps, t.start, cx))
539 .child(self.title_scrub_row(
540 "title-anim-length",
541 "Animation length",
542 secs(if t.anim_frames == 0 { anim_default_frames(fps) } else { t.anim_frames }),
543 Some("s"),
544 ScrubField::AnimLength,
545 (t.entrance().0 == TitleAnim::None && t.exit().0 == TitleAnim::None).then_some("Pick an entrance or an exit first"),
546 DRAG_HINT,
547 cx,
548 ))
549 .child(keys_row)
550 .child(eases)
551 .child(self.title_scrub_row("title-key-x", "Move across", key_val(ScrubField::KeyX), Some("%"), ScrubField::KeyX, why_out, DRAG_HINT, cx))
552 .child(self.title_scrub_row("title-key-y", "Move down", key_val(ScrubField::KeyY), Some("%"), ScrubField::KeyY, why_out, DRAG_HINT, cx))
553 .child(self.title_scrub_row("title-key-scale", "Scale", key_val(ScrubField::KeyScale), Some("%"), ScrubField::KeyScale, why_out, DRAG_HINT, cx))
554 .child(self.title_scrub_row("title-key-opacity", "Opacity", key_val(ScrubField::KeyOpacity), Some("%"), ScrubField::KeyOpacity, why_out, DRAG_HINT, cx))
555 .child(div().flex().justify_start().child(remove)),
556 ),
557 )
558 }
559}