forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

overview.rs479 lines, 18.9 KiBHistoryRawDownload
1//! The overview strip and the scrollbar under the lanes (DESIGN §3 "Overview
2//! strip", §5 layout, §7 zoom). The overview is the whole film in one 14px
3//! band above the ruler: each video clip in its film ink across its span,
4//! audio as thin translucent slivers along the bottom, the playhead as a
5//! white tick, and the bench's window as a closed 1px box. It replaces the
6//! contact strip that used to sit in the time band; the export range's two
7//! mark grips moved onto it with the rest of the film minimap.
8//!
9//! The scrollbar is the same window as a thumb, so both strips share
10//! [`window_frac`] and [`grab_for`], and both drag through the one
11//! `Player::scroll_drag` gesture (`player/view.rs::pan_window_to`).
12
13use super::{HEAD_W, ROW_GAP, RULER_H};
14use crate::layout::{SCROLL_HIT_DOWN, SCROLL_HIT_UP, SCROLL_ROW_H};
15use crate::ui::hitmap;
16use crate::*;
17
18/// The overview's own height (REDESIGN §3.6's 12px overview strip).
19pub(crate) const OVERVIEW_H: f32 = crate::layout::redesign::OVERVIEW_H;
20/// An audio clip's sliver: a thin band along the overview's bottom edge.
21const SLIVER_H: f32 = 3.;
22/// The window box never draws narrower than this, however far the view is
23/// zoomed in: a box a pixel wide is a box no hand can find.
24const WINDOW_MIN_W: f32 = 4.;
25/// How far either side of a mark tick the hand still grabs it.
26const MARK_GRAB: f32 = 6.;
27
28thread_local! {
29 /// Which export mark a live drag on the overview is moving (the grips of
30 /// [`mark_grip`]), `None` when no mark drag is up. The same transient
31 /// shape as the `Player` gesture fields, kept here because the mark drag
32 /// is the only thing that reads it.
33 static MARK_DRAG: Cell<Option<ActionId>> = const { Cell::new(None) };
34}
35
36/// The strip rows the bench spends above its lane stack: the overview, the
37/// ruler, the gaps between them and the lanes, and the scrollbar row with its
38/// gap while there is a scrollbar to draw. A `const fn` so the bench floor
39/// (`layout::BENCH_MIN_H`) can take the same numbers `render` does.
40pub(crate) const fn chrome_h(scroll: bool) -> f32 {
41 OVERVIEW_H
42 + ROW_GAP
43 + RULER_H
44 + ROW_GAP
45 + match scroll {
46 true => ROW_GAP + SCROLL_ROW_H,
47 false => 0.,
48 }
49}
50
51/// The bench's window on the whole film, as fractions of the film's length:
52/// where it starts and how much of the film it shows. A film with no length
53/// yet shows all of itself, so the box never vanishes into nothing.
54pub(crate) fn window_frac(start: f64, span: f64, duration: f64) -> (f32, f32) {
55 if duration <= 0. {
56 return (0., 1.);
57 }
58 let w = (span / duration).clamp(0., 1.) as f32;
59 let x = ((start / duration) as f32).clamp(0., 1. - w);
60 (x, w)
61}
62
63/// How far into a window a press grabbed it, in px. A press inside the
64/// window keeps the point it landed on under the hand; a press outside it
65/// names the window's centre, so the view jumps there and the drag carries
66/// on from the middle. `track_w` is the bed's width in px, `window` the
67/// fractions from [`window_frac`], `min_w` the drawn floor of the window.
68pub(crate) fn grab_for(at: f32, track_w: f32, window: (f32, f32), min_w: f32) -> f32 {
69 let (x, w) = window;
70 let (left, width) = (x * track_w, (w * track_w).max(min_w));
71 match at >= left && at <= left + width {
72 true => at - left,
73 false => width / 2.,
74 }
75}
76
77/// The overview strip: the film's clips, the playhead, the window box, and
78/// the export range's two grips. Mounted above the ruler in the bench's own
79/// column, so it shares the ruler's bed (`HEAD_W` in, the bed's width).
80pub(crate) fn overview(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
81 let duration = player.drawn_duration();
82 let view = player.view();
83 let position = player.session.as_ref().map_or(0., PlaybackSession::now);
84 let (video, audio) = spans(player, duration, player.fps);
85 let (win_x, win_w) = window_frac(view.scale.start, view.span(), duration);
86 let at = |t: f64| match duration > 0. {
87 true => (t / duration).clamp(0., 1.) as f32,
88 false => 0.,
89 };
90 div()
91 .id("stance-overview")
92 .relative()
93 .flex_none()
94 .ml(px(HEAD_W))
95 .h(px(OVERVIEW_H))
96 .bg(rgb(crate::ui::theme::tokens().e1))
97 .cursor_pointer()
98 // Pressing the strip outside the window centres the view there; a
99 // press inside it grabs the window where it was taken. The drag then
100 // rides the root's own move and release (`Player::drag_move`), so the
101 // pointer may leave the 14px band and the window keeps following.
102 .on_mouse_down(
103 MouseButton::Left,
104 cx.listener(|this, event: &MouseDownEvent, _, cx| {
105 let view = this.view();
106 if view.duration <= view.span() {
107 return;
108 }
109 let track = this.ruler.get();
110 let at = px_along(event.position.x, track);
111 let window = window_frac(view.scale.start, view.span(), view.duration);
112 let grab = grab_for(at, f32::from(track.size.width), window, WINDOW_MIN_W);
113 this.scroll_drag = Some(grab);
114 this.pan_window_to(at, cx);
115 }),
116 )
117 .on_mouse_move(cx.listener(|this, event: &MouseMoveEvent, _, cx| {
118 if event.pressed_button != Some(MouseButton::Left) {
119 MARK_DRAG.with(|m| m.set(None));
120 return;
121 }
122 // A mark grip is under the hand: the drag sets that mark instead
123 // of anything else (the grip's own press armed this and stopped
124 // propagation).
125 let Some(action) = MARK_DRAG.with(Cell::get) else {
126 return;
127 };
128 let duration = this.drawn_duration();
129 if duration > 0. {
130 let frac = frac_along(event.position.x, this.ruler.get());
131 let at = (f64::from(frac) * duration * this.active_fps())
132 .max(0.)
133 .round() as u32;
134 let (lo, hi) = this.range.unwrap_or((at, at));
135 let (a, b) = if action == ActionId::SetIn {
136 (at, hi)
137 } else {
138 (lo, at)
139 };
140 // Dragged past its partner, the mark under the hand becomes
141 // the OTHER mark (`ordered_range` keeps the pair legal, and
142 // `i`/`o` swap the same way). Without this flip both ticks
143 // walk along with the pointer and re-order on every move.
144 if crate::player::actions::ordered_range(a, b) != (a, b) {
145 MARK_DRAG.with(|m| {
146 m.set(Some(if action == ActionId::SetIn {
147 ActionId::SetOut
148 } else {
149 ActionId::SetIn
150 }))
151 });
152 }
153 this.range = Some(crate::player::actions::ordered_range(a, b));
154 }
155 cx.notify();
156 }))
157 .on_mouse_up(
158 MouseButton::Left,
159 cx.listener(|_, _, _, _| MARK_DRAG.with(|m| m.set(None))),
160 )
161 .tooltip(crate::ui::widgets::tip_hover("Whole timeline. Drag to move the view.", "", None))
162 .children(hitmap::control(
163 "timeline.overview",
164 "Timeline overview",
165 true,
166 ))
167 .children(video.into_iter().map(|(from, to, ink)| {
168 div()
169 .absolute()
170 .left(relative(from))
171 .top_0()
172 .bottom(px(SLIVER_H + 1.))
173 .w(relative((to - from).max(0.002)))
174 .min_w(px(1.))
175 .bg(rgb(ink))
176 }))
177 // Audio as slivers: the source's own ink at ~40% alpha (0x66), so a
178 // sound-only stretch still reads as film without drowning the picture.
179 .children(audio.into_iter().map(|(from, to, ink)| {
180 div()
181 .absolute()
182 .left(relative(from))
183 .bottom_0()
184 .h(px(SLIVER_H))
185 .w(relative((to - from).max(0.002)))
186 .min_w(px(1.))
187 .bg(rgba((ink << 8) | 0x4d))
188 }))
189 // What the window does not show is dimmed, so a window that fills
190 // the strip still reads as "everything is in view" by its box alone.
191 .child(
192 div()
193 .absolute()
194 .left_0()
195 .top_0()
196 .h_full()
197 .w(relative(win_x))
198 .bg(rgba(crate::ui::theme::with_alpha(0x000000, 0x80))),
199 )
200 .child(
201 div()
202 .absolute()
203 .right_0()
204 .top_0()
205 .h_full()
206 .w(relative((1. - win_x - win_w).max(0.)))
207 .bg(rgba(crate::ui::theme::with_alpha(0x000000, 0x80))),
208 )
209 // The closed 2px box marks the window; its inside is clear so the
210 // film shows through it.
211 .child(
212 div()
213 .id("stance-overview-window")
214 .absolute()
215 .left(relative(win_x))
216 .top_0()
217 .h_full()
218 .w(relative(win_w))
219 .min_w(px(WINDOW_MIN_W))
220 .border_2()
221 // An empty timeline has nothing to window: the box is drawn quietly.
222 .border_color(match duration > 0. {
223 true => rgb(INK1()),
224 false => rgba(crate::ui::theme::tokens().line),
225 }),
226 )
227 // The export range's two grips (DESIGN §5 as amended 2026-09-10): the
228 // marks live on the film itself. Nothing is drawn until a mark exists.
229 .when_some(
230 player.range.filter(|_| duration > 0.),
231 |el, (in_f, out_f)| {
232 let at_t = |f: u32| at(f64::from(f) / player.active_fps());
233 el.child(
234 div()
235 .id("stance-overview-range")
236 .absolute()
237 .left(relative(at_t(in_f)))
238 .top_0()
239 .h_full()
240 .w(relative((at_t(out_f) - at_t(in_f)).max(0.)))
241 .bg(rgba(super::ruler::RANGE_SHADE)),
242 )
243 .child(mark_grip(
244 player,
245 "stance-strip-mark-in",
246 at_t(in_f),
247 "Mark in",
248 ActionId::SetIn,
249 ))
250 .child(mark_grip(
251 player,
252 "stance-strip-mark-out",
253 at_t(out_f),
254 "Mark out",
255 ActionId::SetOut,
256 ))
257 },
258 )
259 // The white playhead tick, drawn last so it sits above the film and
260 // the window box: 2px, in a 1px black halo (DESIGN §4.1).
261 .when(duration > 0., |el| {
262 el.child(
263 div()
264 .id("stance-overview-playhead")
265 .absolute()
266 .left(relative(at(position)))
267 .top_0()
268 .h_full()
269 .ml(px(-1.))
270 .w(px(2.))
271 .bg(rgb(LAMP_WHITE())),
272 )
273 })
274}
275
276/// The scrollbar under the lanes: the window as a thumb along a hairline
277/// track, the same bed as the ruler above the lanes. Mounted only while the
278/// film is longer than the window ([`render`](super::render) reserves the row
279/// on the same test). A press on the thumb grabs it; a press on the track
280/// centres the window there. Its hit band is the row plus [`SCROLL_HIT_UP`]
281/// over the bottom of the lanes and [`SCROLL_HIT_DOWN`] of the bench's own
282/// padding below it: 24px (WCAG 2.5.8) with no more than 8px taken from the
283/// lanes.
284pub(crate) fn scrollbar(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
285 let view = player.view();
286 let (win_x, win_w) = window_frac(view.scale.start, view.span(), view.duration);
287 let bar_top = (SCROLL_ROW_H - SCROLL_BAR_H) / 2.;
288 div()
289 .id("stance-scrollbar")
290 .relative()
291 .flex_none()
292 .ml(px(HEAD_W))
293 .h(px(SCROLL_ROW_H))
294 .child(bounds_probe(player.scroll_track.clone()))
295 .child(
296 div()
297 .absolute()
298 .left_0()
299 .right_0()
300 .top(px(bar_top))
301 .h(px(SCROLL_BAR_H))
302 .bg(rgb(DARK_HAIRLINE())),
303 )
304 .child(
305 div()
306 .id("stance-scroll-thumb")
307 .absolute()
308 .left(relative(win_x))
309 .top(px(bar_top))
310 .h(px(SCROLL_BAR_H))
311 .w(relative(win_w))
312 .min_w(px(SCROLL_THUMB_MIN))
313 .bg(rgb(INK3())),
314 )
315 .child(
316 div()
317 .id("stance-scroll-hit")
318 .absolute()
319 .left_0()
320 .right_0()
321 .top(px(-SCROLL_HIT_UP))
322 .h(px(SCROLL_ROW_H + SCROLL_HIT_UP + SCROLL_HIT_DOWN))
323 .cursor_pointer()
324 .tooltip(crate::ui::widgets::tip_hover(
325 "Pan the timeline window",
326 "drag",
327 None,
328 ))
329 .on_mouse_down(
330 MouseButton::Left,
331 cx.listener(|this, event: &MouseDownEvent, _, cx| {
332 let track = this.scroll_track.get();
333 let at = px_along(event.position.x, track);
334 let view = this.view();
335 let window = window_frac(view.scale.start, view.span(), view.duration);
336 this.scroll_drag = Some(grab_for(
337 at,
338 f32::from(track.size.width),
339 window,
340 SCROLL_THUMB_MIN,
341 ));
342 this.pan_window_to(at, cx);
343 }),
344 )
345 .children(hitmap::control(
346 "timeline.scrollbar",
347 "Timeline scrollbar",
348 true,
349 )),
350 )
351}
352
353/// One clip on the overview: its `(from, to)` fractions of the film and its ink.
354type Span = (f32, f32, u32);
355
356/// The film's clips as `(from, to, ink)` fractions of the film, split into
357/// video and audio. Each clip takes its source's tint, the same one the lane
358/// dot and the bench's clip anatomy draw. A film with no length yields none.
359fn spans(player: &Player, duration: f64, fps: f64) -> (Vec<Span>, Vec<Span>) {
360 let mut video = Vec::new();
361 let mut audio = Vec::new();
362 let Some(session) = player.session.as_ref().filter(|_| duration > 0.) else {
363 return (video, audio);
364 };
365 let frac = |f: u32| ((f64::from(f) / fps) / duration).clamp(0., 1.) as f32;
366 for lane in session.lanes() {
367 let out = match lane.kind {
368 LaneKind::Video => &mut video,
369 LaneKind::Audio => &mut audio,
370 _ => continue,
371 };
372 for clip in session.lane_clips(lane) {
373 // A compound clip shows the clips it stands for, in their own inks.
374 if engine::project::is_sequence_source(clip.source) {
375 for p in session.compound_pieces(lane.kind, clip) {
376 out.push((frac(clip.start + p.start), frac(clip.start + p.start + p.frames()), source_tint(p.source)));
377 }
378 continue;
379 }
380 out.push((frac(clip.start), frac(clip.end()), source_tint(clip.source)));
381 }
382 }
383 (video, audio)
384}
385
386/// One export-range mark, drawn on the overview at its own film position: an
387/// `ink1` tick with a ±[`MARK_GRAB`] door around it that takes precedence
388/// over the strip's own press (`stop_propagation` on its press). Dragging it
389/// sets that mark; `i`, `o` and `^u` are unchanged.
390fn mark_grip(
391 player: &Player,
392 id: &'static str,
393 frac: f32,
394 name: &'static str,
395 action: ActionId,
396) -> impl IntoElement {
397 let chord = player.keymap.chord(action);
398 div()
399 .id(id)
400 .absolute()
401 .left(relative(frac))
402 .top_0()
403 .h_full()
404 .ml(px(-MARK_GRAB))
405 .w(px(MARK_GRAB * 2. + 1.))
406 .flex()
407 .justify_center()
408 .cursor_col_resize()
409 .on_mouse_down(MouseButton::Left, move |_: &MouseDownEvent, _, cx| {
410 MARK_DRAG.with(|m| m.set(Some(action)));
411 cx.stop_propagation();
412 })
413 .tooltip(crate::ui::widgets::tip_hover(name, &chord, None))
414 .children(hitmap::action(action, player.enable(action, None).yes()))
415 .child(div().w(px(1.)).h_full().bg(rgb(INK1())))
416}
417
418#[cfg(test)]
419mod tests {
420 use super::*;
421
422 #[test]
423 fn a_window_over_the_whole_film_fills_the_bed_and_a_film_with_no_length_shows_all_of_itself() {
424 assert_eq!(window_frac(0., 100., 100.), (0., 1.));
425 assert_eq!(
426 window_frac(0., 500., 100.),
427 (0., 1.),
428 "a span past the film clamps to it"
429 );
430 assert_eq!(window_frac(30., 10., 0.), (0., 1.));
431 }
432
433 #[test]
434 fn the_window_box_sits_where_its_start_and_span_say_on_the_film() {
435 let (x, w) = window_frac(25., 25., 100.);
436 assert!(
437 (x - 0.25).abs() < 1e-6 && (w - 0.25).abs() < 1e-6,
438 "{x} {w}"
439 );
440 // Parked at the far end, the box stops at the bed's right edge rather
441 // than running off it.
442 let (x, w) = window_frac(90., 25., 100.);
443 assert!(
444 (x - 0.75).abs() < 1e-6 && (x + w - 1.).abs() < 1e-6,
445 "{x} {w}"
446 );
447 let (x, _) = window_frac(-5., 10., 100.);
448 assert_eq!(x, 0., "a start before the film clamps to its head");
449 }
450
451 #[test]
452 fn a_press_inside_the_window_keeps_its_grab_and_one_outside_centres_it() {
453 // A 400px bed, a window at 100..200px.
454 let window = (0.25, 0.25);
455 assert_eq!(grab_for(150., 400., window, 4.), 50.);
456 // Outside, the grab is the window's middle: a press anywhere off the
457 // box centres the view on the press.
458 assert_eq!(grab_for(300., 400., window, 4.), 50.);
459 assert_eq!(grab_for(20., 400., window, 4.), 50.);
460 }
461
462 #[test]
463 fn a_thumb_never_draws_narrower_than_its_floor() {
464 // A 1-second window on a 10-hour film is a sliver; the drawn floor is
465 // what a press lands on, so a press off it centres on that floor.
466 let (_, w) = window_frac(0., 1., 36_000.);
467 assert!(w * 400. < SCROLL_THUMB_MIN);
468 assert_eq!(
469 grab_for(50., 400., (0.5, w), SCROLL_THUMB_MIN),
470 SCROLL_THUMB_MIN / 2.
471 );
472 }
473
474 #[test]
475 fn the_bench_chrome_grows_by_the_scrollbar_row_only_while_there_is_one() {
476 assert_eq!(chrome_h(true) - chrome_h(false), ROW_GAP + SCROLL_ROW_H);
477 assert_eq!(chrome_h(false), OVERVIEW_H + 2. * ROW_GAP + RULER_H);
478 }
479}