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 | 3 | use std::sync::Arc; |
| 2 | 4 | use std::sync::mpsc::TryRecvError; |
| 3 | 5 | use std::time::Instant; |
| @@ -5,8 +7,8 @@ use std::time::Instant; | ||
| 5 | 7 | use engine::{Frame, PlaybackSession}; |
| 6 | 8 | use gpui::{ |
| 7 | 9 | App, Application, Bounds, Context, FocusHandle, KeyDownEvent, MouseButton, MouseDownEvent, |
| 8 | - PathBuilder, RenderImage, SharedString, TitlebarOptions, Window, WindowBounds, WindowOptions, | |
| 9 | - canvas, div, img, point, prelude::*, px, relative, rgb, size, | |
| 10 | + PathBuilder, Pixels, RenderImage, SharedString, TitlebarOptions, Window, WindowBounds, | |
| 11 | + WindowOptions, canvas, div, img, point, prelude::*, px, relative, rgb, size, | |
| 10 | 12 | }; |
| 11 | 13 | |
| 12 | 14 | /// Editor chrome: three grays and one accent, all darker than the picture so the |
| @@ -42,6 +44,12 @@ struct Player { | ||
| 42 | 44 | eos: bool, |
| 43 | 45 | /// Last frame shown; nothing left to animate. |
| 44 | 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 | 53 | /// Playback was started once, on the first render. Space owns it after that. |
| 46 | 54 | launched: bool, |
| 47 | 55 | displayed: u32, |
| @@ -81,6 +89,7 @@ impl Player { | ||
| 81 | 89 | |
| 82 | 90 | if let Some(frame) = newest { |
| 83 | 91 | self.displayed += 1; |
| 92 | + self.pending_seek = false; | |
| 84 | 93 | self.started.get_or_insert_with(|| { |
| 85 | 94 | eprintln!("first frame displayed (index {})", frame.index); |
| 86 | 95 | Instant::now() |
| @@ -97,6 +106,9 @@ impl Player { | ||
| 97 | 106 | |
| 98 | 107 | if self.eos && self.held.is_none() && !self.done { |
| 99 | 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 | 112 | let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64()); |
| 101 | 113 | eprintln!( |
| 102 | 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 | 148 | impl Render for Player { |
| @@ -121,8 +158,9 @@ impl Render for Player { | ||
| 121 | 158 | // No shadow flag: the clock is the only truth about play state. |
| 122 | 159 | let playing = self.session.is_playing(); |
| 123 | 160 | // A paused timeline has nothing to animate; the toggle handlers notify, |
| 124 | - // which is what starts the loop again. | |
| 125 | - if playing && !self.done { | |
| 161 | + // which is what starts the loop again. A paused seek keeps the loop | |
| 162 | + // running by itself until `pump` has the frame it asked for. | |
| 163 | + if (playing && !self.done) || self.pending_seek { | |
| 126 | 164 | window.request_animation_frame(); |
| 127 | 165 | } |
| 128 | 166 | |
| @@ -141,9 +179,7 @@ impl Render for Player { | ||
| 141 | 179 | // `is_held` filters auto-repeat, which would otherwise toggle |
| 142 | 180 | // playback many times a second. |
| 143 | 181 | if event.keystroke.key == "space" && !event.is_held { |
| 144 | - this.session.toggle(); | |
| 145 | - // Past EOF nothing else asks for a repaint. | |
| 146 | - cx.notify(); | |
| 182 | + this.toggle_or_restart(cx); | |
| 147 | 183 | } |
| 148 | 184 | })) |
| 149 | 185 | .size_full() |
| @@ -217,8 +253,7 @@ impl Player { | ||
| 217 | 253 | .on_mouse_down( |
| 218 | 254 | MouseButton::Left, |
| 219 | 255 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 220 | - this.session.toggle(); | |
| 221 | - cx.notify(); | |
| 256 | + this.toggle_or_restart(cx); | |
| 222 | 257 | }), |
| 223 | 258 | ) |
| 224 | 259 | .child(transport_glyph(playing)), |
| @@ -229,14 +264,22 @@ impl Player { | ||
| 229 | 264 | timecode(self.duration, self.fps) |
| 230 | 265 | ))), |
| 231 | 266 | ) |
| 232 | - // Read-only: no cursor, no hover, no listener -- a clip slice turns | |
| 233 | - // this into a scrubber, today it only reports. | |
| 267 | + // Click to seek. Still not a scrubber: no drag, no hover preview. | |
| 234 | 268 | .child( |
| 235 | 269 | div() |
| 236 | 270 | .flex_none() |
| 237 | 271 | .h(px(6.)) |
| 238 | 272 | .rounded(px(3.)) |
| 239 | 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 | 283 | .child( |
| 241 | 284 | div() |
| 242 | 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 | 314 | /// Placeholder chrome for the clips slice: deliberately empty. |
| 254 | 315 | fn track_lane() -> impl IntoElement { |
| 255 | 316 | div() |
| @@ -309,7 +370,25 @@ fn timecode(t: f64, fps: f64) -> String { | ||
| 309 | 370 | |
| 310 | 371 | #[cfg(test)] |
| 311 | 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 | 393 | #[test] |
| 315 | 394 | fn timecode_counts_frames_inside_the_second() { |
| @@ -371,6 +450,8 @@ fn main() { | ||
| 371 | 450 | held: None, |
| 372 | 451 | eos: false, |
| 373 | 452 | done: false, |
| 453 | + pending_seek: false, | |
| 454 | + ruler: Rc::default(), | |
| 374 | 455 | launched: false, |
| 375 | 456 | displayed: 0, |
| 376 | 457 | dropped: 0, |