The preview gets a scrub bar
Browse files at this commit7 changed files +159 -0
- crates/app/src/main.rs +8 -0
- crates/app/src/player/library.rs +1 -0
- crates/app/src/player/timeline_edit.rs +14 -0
- crates/app/src/player/transport.rs +20 -0
- crates/app/src/render.rs +6 -0
- crates/app/src/transport.rs +57 -0
- crates/app/src/ui/preview.rs +53 -0
crates/app/src/main.rsmodified+8 -0
| @@ -383,6 +383,12 @@ struct Player { | |||
| 383 | /// arrive at the root, so the bar's own geometry has to be readable there. | 383 | /// arrive at the root, so the bar's own geometry has to be readable there. |
| 384 | volume_bar: Rc<Cell<Bounds<Pixels>>>, | 384 | volume_bar: Rc<Cell<Bounds<Pixels>>>, |
| 385 | volume_dragging: bool, | 385 | volume_dragging: bool, |
| 386 | + /// Where the preview's own seek bar was last painted, and whether a hand | ||
| 387 | + /// is on it -- `volume_bar`'s pair and `scrubbing`'s reason: the bar | ||
| 388 | + /// spans the preview's whole length rather than a zoomed [`Scale`], so | ||
| 389 | + /// it needs no `ruler` of its own, only its geometry. | ||
| 390 | + preview_bar: Rc<Cell<Bounds<Pixels>>>, | ||
| 391 | + preview_scrubbing: bool, | ||
| 386 | /// The two stand-in switches ([`engine::proxy`]) as this window has them | 392 | /// The two stand-in switches ([`engine::proxy`]) as this window has them |
| 387 | /// set: whether the picture is cut on the stand-ins, and whether an | 393 | /// set: whether the picture is cut on the stand-ins, and whether an |
| 388 | /// arriving film gets one made for it. Kept here for the volume's reason | 394 | /// arriving film gets one made for it. Kept here for the volume's reason |
| @@ -720,6 +726,8 @@ fn main() { | |||
| 720 | // handed is already set to: nothing to push at startup. | 726 | // handed is already set to: nothing to push at startup. |
| 721 | volume: Volume::default(), | 727 | volume: Volume::default(), |
| 722 | volume_bar: Rc::default(), | 728 | volume_bar: Rc::default(), |
| 729 | + preview_bar: Rc::default(), | ||
| 730 | + preview_scrubbing: false, | ||
| 723 | volume_dragging: false, | 731 | volume_dragging: false, |
| 724 | // What a session comes up at, so the first one opened is | 732 | // What a session comes up at, so the first one opened is |
| 725 | // pushed the values it already holds. | 733 | // pushed the values it already holds. |
crates/app/src/player/library.rsmodified+1 -0
| @@ -185,6 +185,7 @@ impl Player { | |||
| 185 | // timeline that has just stopped existing. | 185 | // timeline that has just stopped existing. |
| 186 | self.scrubbing = false; | 186 | self.scrubbing = false; |
| 187 | self.volume_dragging = false; | 187 | self.volume_dragging = false; |
| 188 | + self.preview_scrubbing = false; | ||
| 188 | self.eq_dragging = false; | 189 | self.eq_dragging = false; |
| 189 | self.speed_dragging = false; | 190 | self.speed_dragging = false; |
| 190 | self.color_dragging = false; | 191 | self.color_dragging = false; |
crates/app/src/player/timeline_edit.rsmodified+14 -0
| @@ -1398,6 +1398,16 @@ impl Player { | |||
| 1398 | } | 1398 | } |
| 1399 | return; | 1399 | return; |
| 1400 | } | 1400 | } |
| 1401 | + // The preview's own scrub bar, tracked on the root for `scrubbing`'s | ||
| 1402 | + // reason: it is a short bar and the pointer leaves it at once. | ||
| 1403 | + if self.preview_scrubbing { | ||
| 1404 | + if event.pressed_button == Some(MouseButton::Left) { | ||
| 1405 | + self.preview_scrub_to(event.position.x, false, cx); | ||
| 1406 | + } else { | ||
| 1407 | + self.preview_scrubbing = false; | ||
| 1408 | + } | ||
| 1409 | + return; | ||
| 1410 | + } | ||
| 1401 | if !self.scrubbing { | 1411 | if !self.scrubbing { |
| 1402 | return; | 1412 | return; |
| 1403 | } | 1413 | } |
| @@ -1469,6 +1479,10 @@ impl Player { | |||
| 1469 | self.drag_volume(event.position.x, cx); | 1479 | self.drag_volume(event.position.x, cx); |
| 1470 | return; | 1480 | return; |
| 1471 | } | 1481 | } |
| 1482 | + if std::mem::take(&mut self.preview_scrubbing) { | ||
| 1483 | + self.preview_scrub_to(event.position.x, true, cx); | ||
| 1484 | + return; | ||
| 1485 | + } | ||
| 1472 | if std::mem::take(&mut self.scrubbing) { | 1486 | if std::mem::take(&mut self.scrubbing) { |
| 1473 | self.scrub_to(event.position.x, true, cx); | 1487 | self.scrub_to(event.position.x, true, cx); |
| 1474 | } | 1488 | } |
crates/app/src/player/transport.rsmodified+20 -0
| @@ -252,6 +252,26 @@ impl Player { | |||
| 252 | self.seek(target as f64 / fps, cx); | 252 | self.seek(target as f64 / fps, cx); |
| 253 | } | 253 | } |
| 254 | 254 | ||
| 255 | + /// [`scrub_to`](Player::scrub_to) for the preview's own seek bar: the | ||
| 256 | + /// mapping is `preview_seek_seconds` rather than a `Scale`, because a | ||
| 257 | + /// preview bar spans the file's whole length and never zooms. Seeks | ||
| 258 | + /// through the ordinary [`Player::seek`], so it lands on | ||
| 259 | + /// `preview_session` the same way the keyboard step does | ||
| 260 | + /// ([`Player::active_session_mut`]) -- a preview's scrub never reaches | ||
| 261 | + /// the timeline underneath it. | ||
| 262 | + pub(crate) fn preview_scrub_to(&mut self, x: Pixels, commit: bool, cx: &mut Context<Self>) { | ||
| 263 | + let Some(session) = self.preview_session.as_ref() else { | ||
| 264 | + return; | ||
| 265 | + }; | ||
| 266 | + let t = preview_seek_seconds(x, self.preview_bar.get(), session.timeline_duration()); | ||
| 267 | + let target = (t * self.active_fps()) as u32; | ||
| 268 | + if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) { | ||
| 269 | + self.last_target = target; | ||
| 270 | + self.last_scrub = Instant::now(); | ||
| 271 | + self.seek(t, cx); | ||
| 272 | + } | ||
| 273 | + } | ||
| 274 | + | ||
| 255 | /// Seeks to where the pointer sits along the ruler. `commit` is the press | 275 | /// Seeks to where the pointer sits along the ruler. `commit` is the press |
| 256 | /// and the release, which must land exactly even when the throttle below | 276 | /// and the release, which must land exactly even when the throttle below |
| 257 | /// would have skipped them. | 277 | /// would have skipped them. |
crates/app/src/render.rsmodified+6 -0
| @@ -656,6 +656,12 @@ impl Render for Player { | |||
| 656 | // over it rather than under. | 656 | // over it rather than under. |
| 657 | .children(self.subtitle_overlay(position, window)) | 657 | .children(self.subtitle_overlay(position, window)) |
| 658 | .children(self.preview_badge(cx)) | 658 | .children(self.preview_badge(cx)) |
| 659 | + // The preview's own scrub bar, low over | ||
| 660 | + // the picture the badge is drawn over the | ||
| 661 | + // top of: drawn after both, so it sits | ||
| 662 | + // above the subtitle plate and never | ||
| 663 | + // under the badge's banner. | ||
| 664 | + .children(self.preview_scrub_bar(cx)) | ||
| 659 | // The three transient lines hang off the | 665 | // The three transient lines hang off the |
| 660 | // bottom of the picture rather than taking | 666 | // bottom of the picture rather than taking |
| 661 | // a row of the column: a notice that | 667 | // a row of the column: a notice that |
crates/app/src/transport.rsmodified+57 -0
| @@ -214,3 +214,60 @@ pub(crate) fn seek_line(standing: Option<Duration>) -> Option<String> { | |||
| 214 | clock(since.as_secs_f32()) | 214 | clock(since.as_secs_f32()) |
| 215 | )) | 215 | )) |
| 216 | } | 216 | } |
| 217 | + | ||
| 218 | +/// Where along the preview's own seek bar a click landed, in seconds from | ||
| 219 | +/// the start: [`frac_along`]'s fraction of `duration`, since the bar spans | ||
| 220 | +/// the preview's whole length rather than a zoomed timeline `Scale` -- a | ||
| 221 | +/// preview does not zoom, so there is no bed narrower than the file to map | ||
| 222 | +/// into. | ||
| 223 | +pub(crate) fn preview_seek_seconds(x: Pixels, bar: Bounds<Pixels>, duration: f64) -> f64 { | ||
| 224 | + f64::from(frac_along(x, bar)) * duration.max(0.) | ||
| 225 | +} | ||
| 226 | + | ||
| 227 | +/// How full the preview's seek bar is drawn, 0..1: the played share of the | ||
| 228 | +/// duration, clamped for the same reason [`frac_along`] clamps a click -- a | ||
| 229 | +/// clock a frame past the end must not overhang the track it is drawn on. | ||
| 230 | +/// Zero for a duration of nothing, rather than a NaN from dividing by it. | ||
| 231 | +pub(crate) fn preview_progress_along(now: f64, duration: f64) -> f32 { | ||
| 232 | + if duration <= 0. { | ||
| 233 | + return 0.; | ||
| 234 | + } | ||
| 235 | + (now / duration).clamp(0., 1.) as f32 | ||
| 236 | +} | ||
| 237 | + | ||
| 238 | +#[cfg(test)] | ||
| 239 | +mod tests { | ||
| 240 | + use super::*; | ||
| 241 | + | ||
| 242 | + /// A click at either end of the bar lands exactly on the file's own | ||
| 243 | + /// ends, the middle lands at half the duration, and a click past either | ||
| 244 | + /// edge clamps to it rather than reading negative or past the end -- | ||
| 245 | + /// `frac_along`'s own clamp, carried through. | ||
| 246 | + #[test] | ||
| 247 | + fn preview_seek_seconds_maps_the_bar_to_the_file() { | ||
| 248 | + let bar = Bounds { | ||
| 249 | + origin: point(px(100.), px(0.)), | ||
| 250 | + size: size(px(200.), px(HIT_MIN)), | ||
| 251 | + }; | ||
| 252 | + assert_eq!(preview_seek_seconds(px(100.), bar, 60.), 0.); | ||
| 253 | + assert_eq!(preview_seek_seconds(px(300.), bar, 60.), 60.); | ||
| 254 | + assert_eq!(preview_seek_seconds(px(200.), bar, 60.), 30.); | ||
| 255 | + // Off both ends -- the pointer left the bar, and the seek clamps to | ||
| 256 | + // the end it left through. | ||
| 257 | + assert_eq!(preview_seek_seconds(px(0.), bar, 60.), 0.); | ||
| 258 | + assert_eq!(preview_seek_seconds(px(500.), bar, 60.), 60.); | ||
| 259 | + } | ||
| 260 | + | ||
| 261 | + /// The fill tracks the played share exactly, and a duration of nothing | ||
| 262 | + /// -- no file open yet -- reads as an empty bar rather than a divide by | ||
| 263 | + /// zero. | ||
| 264 | + #[test] | ||
| 265 | + fn preview_progress_along_is_the_played_share() { | ||
| 266 | + assert_eq!(preview_progress_along(0., 60.), 0.); | ||
| 267 | + assert_eq!(preview_progress_along(30., 60.), 0.5); | ||
| 268 | + assert_eq!(preview_progress_along(60., 60.), 1.); | ||
| 269 | + // Past the end clamps rather than overhanging the track. | ||
| 270 | + assert_eq!(preview_progress_along(61., 60.), 1.); | ||
| 271 | + assert_eq!(preview_progress_along(5., 0.), 0.); | ||
| 272 | + } | ||
| 273 | +} | ||
crates/app/src/ui/preview.rsmodified+53 -0
| @@ -37,6 +37,59 @@ impl Player { | |||
| 37 | }) | 37 | }) |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | + /// The preview's own scrub bar, drawn low over the picture the way the | ||
| 41 | + /// badge is drawn over its top: a preview has a duration and a playhead | ||
| 42 | + /// like anything else on screen, and unlike the timeline's ruler | ||
| 43 | + /// ([`Player::seek_bar`] is a different bar entirely, the "still | ||
| 44 | + /// seeking" status line) it is the only way to jump around one with a | ||
| 45 | + /// mouse -- `esc` and the arrow keys are the keyboard's own doors. | ||
| 46 | + /// | ||
| 47 | + /// The hit area is `RULER_HIT_H` (`HIT_MIN`, WCAG 2.5.8) tall and centred | ||
| 48 | + /// on a 6 px track, the ruler's own idiom: a bar thin enough to read is | ||
| 49 | + /// too thin to reliably click. | ||
| 50 | + pub(crate) fn preview_scrub_bar(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { | ||
| 51 | + let session = self.preview_session.as_ref()?; | ||
| 52 | + let filled = preview_progress_along(session.now(), session.timeline_duration()); | ||
| 53 | + Some( | ||
| 54 | + div() | ||
| 55 | + .id("preview-scrub") | ||
| 56 | + .absolute() | ||
| 57 | + .left_0() | ||
| 58 | + .right_0() | ||
| 59 | + .bottom_0() | ||
| 60 | + .h(px(RULER_HIT_H)) | ||
| 61 | + .flex() | ||
| 62 | + .flex_col() | ||
| 63 | + .justify_center() | ||
| 64 | + .px(px(8.)) | ||
| 65 | + .cursor_pointer() | ||
| 66 | + .tooltip(|_, cx| cx.new(|_| Tip("Seek — click or drag".into())).into()) | ||
| 67 | + .on_mouse_down( | ||
| 68 | + MouseButton::Left, | ||
| 69 | + cx.listener(|this, event: &MouseDownEvent, _, cx| { | ||
| 70 | + this.preview_scrubbing = true; | ||
| 71 | + this.preview_scrub_to(event.position.x, true, cx); | ||
| 72 | + }), | ||
| 73 | + ) | ||
| 74 | + .child( | ||
| 75 | + div() | ||
| 76 | + .relative() | ||
| 77 | + .w_full() | ||
| 78 | + .h(px(6.)) | ||
| 79 | + .rounded(px(3.)) | ||
| 80 | + .bg(rgba(SUB_SHADE())) | ||
| 81 | + .child(bounds_probe(self.preview_bar.clone())) | ||
| 82 | + .child( | ||
| 83 | + div() | ||
| 84 | + .h_full() | ||
| 85 | + .w(relative(filled)) | ||
| 86 | + .rounded(px(3.)) | ||
| 87 | + .bg(rgb(ACCENT_PRIMARY())), | ||
| 88 | + ), | ||
| 89 | + ), | ||
| 90 | + ) | ||
| 91 | + } | ||
| 92 | + | ||
| 40 | /// The frame on screen right now, written to a PNG: `image` and its | 93 | /// The frame on screen right now, written to a PNG: `image` and its |
| 41 | /// `as_bytes` are gpui's own cached copy of what was last handed to the | 94 | /// `as_bytes` are gpui's own cached copy of what was last handed to the |
| 42 | /// atlas ([`Player::pump`]), so this reads no decoder and races nothing -- | 95 | /// atlas ([`Player::pump`]), so this reads no decoder and races nothing -- |