Click-to-seek ruler, post-EOF restart
Browse files at this commit14821abcf2c511ce5fefb267f84e20bebdb76470Parent: e4ca3bd- feat
- click-to-seek ruler, post-EOF restart
1 changed file +93 -12
- crates/app/src/main.rs +93 -12
crates/app/src/main.rsmodified+93 -12
| @@ -1,3 +1,5 @@ | |||
| 1 | +use std::cell::Cell; | ||
| 2 | +use std::rc::Rc; | ||
| 1 | use std::sync::Arc; | 3 | use std::sync::Arc; |
| 2 | use std::sync::mpsc::TryRecvError; | 4 | use std::sync::mpsc::TryRecvError; |
| 3 | use std::time::Instant; | 5 | use std::time::Instant; |
| @@ -5,8 +7,8 @@ use std::time::Instant; | |||
| 5 | use engine::{Frame, PlaybackSession}; | 7 | use engine::{Frame, PlaybackSession}; |
| 6 | use gpui::{ | 8 | use gpui::{ |
| 7 | App, Application, Bounds, Context, FocusHandle, KeyDownEvent, MouseButton, MouseDownEvent, | 9 | App, Application, Bounds, Context, FocusHandle, KeyDownEvent, MouseButton, MouseDownEvent, |
| 8 | - PathBuilder, RenderImage, SharedString, TitlebarOptions, Window, WindowBounds, WindowOptions, | 10 | + PathBuilder, Pixels, RenderImage, SharedString, TitlebarOptions, Window, WindowBounds, |
| 9 | - canvas, div, img, point, prelude::*, px, relative, rgb, size, | 11 | + WindowOptions, canvas, div, img, point, prelude::*, px, relative, rgb, size, |
| 10 | }; | 12 | }; |
| 11 | 13 | ||
| 12 | /// Editor chrome: three grays and one accent, all darker than the picture so the | 14 | /// Editor chrome: three grays and one accent, all darker than the picture so the |
| @@ -42,6 +44,12 @@ struct Player { | |||
| 42 | eos: bool, | 44 | eos: bool, |
| 43 | /// Last frame shown; nothing left to animate. | 45 | /// Last frame shown; nothing left to animate. |
| 44 | done: bool, | 46 | done: bool, |
| 47 | + /// A seek is waiting for its frame. Keeps the repaint loop alive while | ||
| 48 | + /// paused, which is the only way the new still ever reaches the screen. | ||
| 49 | + pending_seek: bool, | ||
| 50 | + /// The ruler's own box, recorded at prepaint: a mouse listener is handed | ||
| 51 | + /// the window position and nothing else. | ||
| 52 | + ruler: Rc<Cell<Bounds<Pixels>>>, | ||
| 45 | /// Playback was started once, on the first render. Space owns it after that. | 53 | /// Playback was started once, on the first render. Space owns it after that. |
| 46 | launched: bool, | 54 | launched: bool, |
| 47 | displayed: u32, | 55 | displayed: u32, |
| @@ -81,6 +89,7 @@ impl Player { | |||
| 81 | 89 | ||
| 82 | if let Some(frame) = newest { | 90 | if let Some(frame) = newest { |
| 83 | self.displayed += 1; | 91 | self.displayed += 1; |
| 92 | + self.pending_seek = false; | ||
| 84 | self.started.get_or_insert_with(|| { | 93 | self.started.get_or_insert_with(|| { |
| 85 | eprintln!("first frame displayed (index {})", frame.index); | 94 | eprintln!("first frame displayed (index {})", frame.index); |
| 86 | Instant::now() | 95 | Instant::now() |
| @@ -97,6 +106,9 @@ impl Player { | |||
| 97 | 106 | ||
| 98 | if self.eos && self.held.is_none() && !self.done { | 107 | if self.eos && self.held.is_none() && !self.done { |
| 99 | self.done = true; | 108 | self.done = true; |
| 109 | + // A seek whose worker never produced a frame (vanished file) would | ||
| 110 | + // otherwise repaint at vsync forever. | ||
| 111 | + self.pending_seek = false; | ||
| 100 | let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64()); | 112 | let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64()); |
| 101 | eprintln!( | 113 | eprintln!( |
| 102 | "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s", | 114 | "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s", |
| @@ -106,6 +118,31 @@ impl Player { | |||
| 106 | ); | 118 | ); |
| 107 | } | 119 | } |
| 108 | } | 120 | } |
| 121 | + | ||
| 122 | + /// Jumps the timeline. Every end-of-stream flag is app state and the | ||
| 123 | + /// engine knows nothing about it, so clearing them here is what stops the | ||
| 124 | + /// picture from staying frozen on the old last frame. | ||
| 125 | + fn seek(&mut self, t: f64, cx: &mut Context<Self>) { | ||
| 126 | + self.session.seek(t); | ||
| 127 | + self.held = None; | ||
| 128 | + self.eos = false; | ||
| 129 | + self.done = false; | ||
| 130 | + self.pending_seek = true; | ||
| 131 | + cx.notify(); | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + /// Space and the transport button share it: once the timeline is finished | ||
| 135 | + /// the only sensible "play" is from the top. | ||
| 136 | + fn toggle_or_restart(&mut self, cx: &mut Context<Self>) { | ||
| 137 | + if self.done { | ||
| 138 | + self.seek(0., cx); | ||
| 139 | + self.session.play(); | ||
| 140 | + } else { | ||
| 141 | + self.session.toggle(); | ||
| 142 | + // Past EOF nothing else asks for a repaint. | ||
| 143 | + cx.notify(); | ||
| 144 | + } | ||
| 145 | + } | ||
| 109 | } | 146 | } |
| 110 | 147 | ||
| 111 | impl Render for Player { | 148 | impl Render for Player { |
| @@ -121,8 +158,9 @@ impl Render for Player { | |||
| 121 | // No shadow flag: the clock is the only truth about play state. | 158 | // No shadow flag: the clock is the only truth about play state. |
| 122 | let playing = self.session.is_playing(); | 159 | let playing = self.session.is_playing(); |
| 123 | // A paused timeline has nothing to animate; the toggle handlers notify, | 160 | // A paused timeline has nothing to animate; the toggle handlers notify, |
| 124 | - // which is what starts the loop again. | 161 | + // which is what starts the loop again. A paused seek keeps the loop |
| 125 | - if playing && !self.done { | 162 | + // running by itself until `pump` has the frame it asked for. |
| 163 | + if (playing && !self.done) || self.pending_seek { | ||
| 126 | window.request_animation_frame(); | 164 | window.request_animation_frame(); |
| 127 | } | 165 | } |
| 128 | 166 | ||
| @@ -141,9 +179,7 @@ impl Render for Player { | |||
| 141 | // `is_held` filters auto-repeat, which would otherwise toggle | 179 | // `is_held` filters auto-repeat, which would otherwise toggle |
| 142 | // playback many times a second. | 180 | // playback many times a second. |
| 143 | if event.keystroke.key == "space" && !event.is_held { | 181 | if event.keystroke.key == "space" && !event.is_held { |
| 144 | - this.session.toggle(); | 182 | + this.toggle_or_restart(cx); |
| 145 | - // Past EOF nothing else asks for a repaint. | ||
| 146 | - cx.notify(); | ||
| 147 | } | 183 | } |
| 148 | })) | 184 | })) |
| 149 | .size_full() | 185 | .size_full() |
| @@ -217,8 +253,7 @@ impl Player { | |||
| 217 | .on_mouse_down( | 253 | .on_mouse_down( |
| 218 | MouseButton::Left, | 254 | MouseButton::Left, |
| 219 | cx.listener(|this, _: &MouseDownEvent, _, cx| { | 255 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 220 | - this.session.toggle(); | 256 | + this.toggle_or_restart(cx); |
| 221 | - cx.notify(); | ||
| 222 | }), | 257 | }), |
| 223 | ) | 258 | ) |
| 224 | .child(transport_glyph(playing)), | 259 | .child(transport_glyph(playing)), |
| @@ -229,14 +264,22 @@ impl Player { | |||
| 229 | timecode(self.duration, self.fps) | 264 | timecode(self.duration, self.fps) |
| 230 | ))), | 265 | ))), |
| 231 | ) | 266 | ) |
| 232 | - // Read-only: no cursor, no hover, no listener -- a clip slice turns | 267 | + // Click to seek. Still not a scrubber: no drag, no hover preview. |
| 233 | - // this into a scrubber, today it only reports. | ||
| 234 | .child( | 268 | .child( |
| 235 | div() | 269 | div() |
| 236 | .flex_none() | 270 | .flex_none() |
| 237 | .h(px(6.)) | 271 | .h(px(6.)) |
| 238 | .rounded(px(3.)) | 272 | .rounded(px(3.)) |
| 239 | .bg(rgb(SURFACE)) | 273 | .bg(rgb(SURFACE)) |
| 274 | + .cursor_pointer() | ||
| 275 | + .on_mouse_down( | ||
| 276 | + MouseButton::Left, | ||
| 277 | + cx.listener(|this, event: &MouseDownEvent, _, cx| { | ||
| 278 | + let frac = frac_along(event.position.x, this.ruler.get()); | ||
| 279 | + this.seek(f64::from(frac) * this.duration, cx); | ||
| 280 | + }), | ||
| 281 | + ) | ||
| 282 | + .child(bounds_probe(self.ruler.clone())) | ||
| 240 | .child( | 283 | .child( |
| 241 | div() | 284 | div() |
| 242 | .h_full() | 285 | .h_full() |
| @@ -250,6 +293,24 @@ impl Player { | |||
| 250 | } | 293 | } |
| 251 | } | 294 | } |
| 252 | 295 | ||
| 296 | +/// Records its parent's laid-out box: gpui hands a mouse listener the window | ||
| 297 | +/// position only, and the ruler sits behind the panel's padding. Paints | ||
| 298 | +/// nothing and takes no hitbox of its own, so the click still reaches the bar. | ||
| 299 | +fn bounds_probe(into: Rc<Cell<Bounds<Pixels>>>) -> impl IntoElement { | ||
| 300 | + canvas(move |bounds, _, _| into.set(bounds), |_, _, _, _| ()) | ||
| 301 | + .absolute() | ||
| 302 | + .size_full() | ||
| 303 | +} | ||
| 304 | + | ||
| 305 | +/// Where along an element a click landed, 0..1. An element that was never | ||
| 306 | +/// painted has no width, and reads as its start rather than as NaN. | ||
| 307 | +fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 { | ||
| 308 | + if bounds.size.width <= px(0.) { | ||
| 309 | + return 0.; | ||
| 310 | + } | ||
| 311 | + ((x - bounds.left()) / bounds.size.width).clamp(0., 1.) | ||
| 312 | +} | ||
| 313 | + | ||
| 253 | /// Placeholder chrome for the clips slice: deliberately empty. | 314 | /// Placeholder chrome for the clips slice: deliberately empty. |
| 254 | fn track_lane() -> impl IntoElement { | 315 | fn track_lane() -> impl IntoElement { |
| 255 | div() | 316 | div() |
| @@ -309,7 +370,25 @@ fn timecode(t: f64, fps: f64) -> String { | |||
| 309 | 370 | ||
| 310 | #[cfg(test)] | 371 | #[cfg(test)] |
| 311 | mod tests { | 372 | mod tests { |
| 312 | - use super::timecode; | 373 | + use super::{frac_along, timecode}; |
| 374 | + use gpui::{Bounds, Pixels, point, px, size}; | ||
| 375 | + | ||
| 376 | + #[test] | ||
| 377 | + fn frac_along_measures_from_the_elements_own_left_edge() { | ||
| 378 | + // A ruler inset by the panel's 12 px padding: window x is not bar x. | ||
| 379 | + let bar: Bounds<Pixels> = Bounds { | ||
| 380 | + origin: point(px(12.), px(400.)), | ||
| 381 | + size: size(px(200.), px(6.)), | ||
| 382 | + }; | ||
| 383 | + assert_eq!(frac_along(px(12.), bar), 0.); | ||
| 384 | + assert_eq!(frac_along(px(112.), bar), 0.5); | ||
| 385 | + assert_eq!(frac_along(px(212.), bar), 1.); | ||
| 386 | + // Outside the bar (a click that slid off) clamps, never extrapolates. | ||
| 387 | + assert_eq!(frac_along(px(0.), bar), 0.); | ||
| 388 | + assert_eq!(frac_along(px(9999.), bar), 1.); | ||
| 389 | + // Never painted: no division by zero, no NaN reaching seek(). | ||
| 390 | + assert_eq!(frac_along(px(50.), Bounds::default()), 0.); | ||
| 391 | + } | ||
| 313 | 392 | ||
| 314 | #[test] | 393 | #[test] |
| 315 | fn timecode_counts_frames_inside_the_second() { | 394 | fn timecode_counts_frames_inside_the_second() { |
| @@ -371,6 +450,8 @@ fn main() { | |||
| 371 | held: None, | 450 | held: None, |
| 372 | eos: false, | 451 | eos: false, |
| 373 | done: false, | 452 | done: false, |
| 453 | + pending_seek: false, | ||
| 454 | + ruler: Rc::default(), | ||
| 374 | launched: false, | 455 | launched: false, |
| 375 | displayed: 0, | 456 | displayed: 0, |
| 376 | dropped: 0, | 457 | dropped: 0, |