forge
Explore
Log in

Don't you dare go Hollow

Click-to-seek ruler, post-EOF restart

Browse files at this commit

14821abcf2c511ce5fefb267f84e20bebdb76470Ahmet Kaan GümüşParent: e4ca3bd
  • feat
    • click-to-seek ruler, post-EOF restart
Show full message
✨ feat(app): click-to-seek ruler, post-EOF restart

Ruler takes a left click and seeks to the fraction of its own box that was
hit; an invisible overlay canvas records those bounds at prepaint because a
mouse listener only ever sees the window position. Player::seek clears the
app-side EOF flags (held/eos/done) and raises pending_seek, which keeps the
repaint loop alive while paused until the target frame is displayed. Space
and the transport button restart from zero once the timeline is finished.
crates/app/src/main.rsmodified+93 -12
@@ -1,3 +1,5 @@
1+use std::cell::Cell;
2+use std::rc::Rc;
13 use std::sync::Arc;
24 use std::sync::mpsc::TryRecvError;
35 use std::time::Instant;
@@ -5,8 +7,8 @@ use std::time::Instant;
57 use engine::{Frame, PlaybackSession};
68 use gpui::{
79 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,
1012 };
1113
1214 /// Editor chrome: three grays and one accent, all darker than the picture so the
@@ -42,6 +44,12 @@ struct Player {
4244 eos: bool,
4345 /// Last frame shown; nothing left to animate.
4446 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>>>,
4553 /// Playback was started once, on the first render. Space owns it after that.
4654 launched: bool,
4755 displayed: u32,
@@ -81,6 +89,7 @@ impl Player {
8189
8290 if let Some(frame) = newest {
8391 self.displayed += 1;
92+ self.pending_seek = false;
8493 self.started.get_or_insert_with(|| {
8594 eprintln!("first frame displayed (index {})", frame.index);
8695 Instant::now()
@@ -97,6 +106,9 @@ impl Player {
97106
98107 if self.eos && self.held.is_none() && !self.done {
99108 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;
100112 let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64());
101113 eprintln!(
102114 "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s",
@@ -106,6 +118,31 @@ impl Player {
106118 );
107119 }
108120 }
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+ }
109146 }
110147
111148 impl Render for Player {
@@ -121,8 +158,9 @@ impl Render for Player {
121158 // No shadow flag: the clock is the only truth about play state.
122159 let playing = self.session.is_playing();
123160 // 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 {
126164 window.request_animation_frame();
127165 }
128166
@@ -141,9 +179,7 @@ impl Render for Player {
141179 // `is_held` filters auto-repeat, which would otherwise toggle
142180 // playback many times a second.
143181 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);
147183 }
148184 }))
149185 .size_full()
@@ -217,8 +253,7 @@ impl Player {
217253 .on_mouse_down(
218254 MouseButton::Left,
219255 cx.listener(|this, _: &MouseDownEvent, _, cx| {
220- this.session.toggle();
221- cx.notify();
256+ this.toggle_or_restart(cx);
222257 }),
223258 )
224259 .child(transport_glyph(playing)),
@@ -229,14 +264,22 @@ impl Player {
229264 timecode(self.duration, self.fps)
230265 ))),
231266 )
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.
234268 .child(
235269 div()
236270 .flex_none()
237271 .h(px(6.))
238272 .rounded(px(3.))
239273 .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()))
240283 .child(
241284 div()
242285 .h_full()
@@ -250,6 +293,24 @@ impl Player {
250293 }
251294 }
252295
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+
253314 /// Placeholder chrome for the clips slice: deliberately empty.
254315 fn track_lane() -> impl IntoElement {
255316 div()
@@ -309,7 +370,25 @@ fn timecode(t: f64, fps: f64) -> String {
309370
310371 #[cfg(test)]
311372 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+ }
313392
314393 #[test]
315394 fn timecode_counts_frames_inside_the_second() {
@@ -371,6 +450,8 @@ fn main() {
371450 held: None,
372451 eos: false,
373452 done: false,
453+ pending_seek: false,
454+ ruler: Rc::default(),
374455 launched: false,
375456 displayed: 0,
376457 dropped: 0,