forge
Explore
Log in

Don't you dare go Hollow

🔀 merge(T17): timeline zoom

Browse files at this commit

c97244fa6b2c80ca09080aca03646279a2be6480Ahmet Kaan GümüşParent: eeebfb56dd5951
crates/app/src/keymap.rsmodified+32 -6
@@ -63,6 +63,9 @@ pub enum ActionId {
6363 Color,
6464 Fit,
6565 Resolution,
66+ ZoomIn,
67+ ZoomOut,
68+ ZoomFit,
6669 Undo,
6770 AddVideoLane,
6871 AddAudioLane,
@@ -80,7 +83,7 @@ pub enum ActionId {
8083 impl ActionId {
8184 /// Display order everywhere -- the editor lists them in it and
8285 /// [`Keymap::defaults`] binds them in it.
83- pub const ALL: [ActionId; 35] = [
86+ pub const ALL: [ActionId; 38] = [
8487 ActionId::Play,
8588 ActionId::StepBack,
8689 ActionId::StepForward,
@@ -104,6 +107,9 @@ impl ActionId {
104107 ActionId::Color,
105108 ActionId::Fit,
106109 ActionId::Resolution,
110+ ActionId::ZoomIn,
111+ ActionId::ZoomOut,
112+ ActionId::ZoomFit,
107113 ActionId::Undo,
108114 ActionId::AddVideoLane,
109115 ActionId::AddAudioLane,
@@ -144,6 +150,9 @@ impl ActionId {
144150 ActionId::Color => "Colour…",
145151 ActionId::Fit => "Fit policy: fit → fill → stretch → centre",
146152 ActionId::Resolution => "Project resolution: source → 2160p → 1080p → 720p → 480p",
153+ ActionId::ZoomIn => "Zoom in on the timeline (around the playhead)",
154+ ActionId::ZoomOut => "Zoom out of the timeline",
155+ ActionId::ZoomFit => "Fit the whole timeline on screen",
147156 ActionId::Undo => "Undo",
148157 ActionId::AddVideoLane => "Add a video track",
149158 ActionId::AddAudioLane => "Add an audio track",
@@ -186,6 +195,9 @@ impl ActionId {
186195 ActionId::Color => "color",
187196 ActionId::Fit => "fit",
188197 ActionId::Resolution => "resolution",
198+ ActionId::ZoomIn => "zoom-in",
199+ ActionId::ZoomOut => "zoom-out",
200+ ActionId::ZoomFit => "zoom-fit",
189201 ActionId::Undo => "undo",
190202 ActionId::AddVideoLane => "add-video-lane",
191203 ActionId::AddAudioLane => "add-audio-lane",
@@ -236,7 +248,11 @@ impl ActionId {
236248 | ActionId::Silence => Category::Clips,
237249 // The project's own picture size is not a clip's business, and not
238250 // a file operation either: it is what the viewer is looking at.
239- ActionId::Resolution => Category::View,
251+ // Neither is how much of the timeline the panel shows: a zoom edits
252+ // nothing, it magnifies.
253+ ActionId::Resolution | ActionId::ZoomIn | ActionId::ZoomOut | ActionId::ZoomFit => {
254+ Category::View
255+ }
240256 ActionId::Cut
241257 | ActionId::Regroup
242258 | ActionId::Detach
@@ -307,7 +323,7 @@ pub const FIXED: [Fixed; 16] = [
307323 // holding anything else still does what one press did.
308324 Fixed {
309325 chord: "hold ← → ↑ ↓",
310- label: "Run a card's slider, or the volume keys, while held",
326+ label: "Run a card's slider, or the volume and zoom keys, while held",
311327 category: Category::View,
312328 },
313329 Fixed {
@@ -544,6 +560,14 @@ impl Keymap {
544560 // it changes what every clip is composed onto.
545561 b(ActionId::Fit, "p", false),
546562 b(ActionId::Resolution, "r", true),
563+ // The zoom pair, on the chords every editor and every browser
564+ // magnifies with. The bare "=" and "-" are the volume keys, so
565+ // these take the ctrl versions of them -- and ctrl+0 is the
566+ // "back to 100%" of the same family, which here is the whole
567+ // timeline across the bed.
568+ b(ActionId::ZoomIn, "=", true),
569+ b(ActionId::ZoomOut, "-", true),
570+ b(ActionId::ZoomFit, "0", true),
547571 b(ActionId::Undo, "z", false),
548572 b(ActionId::Undo, "z", true),
549573 // The unshifted initials of what they add. Both were free --
@@ -839,7 +863,7 @@ mod tests {
839863 #[test]
840864 fn every_default_stroke_reaches_its_action() {
841865 let k = Keymap::defaults();
842- assert_eq!(k.entries().len(), 37);
866+ assert_eq!(k.entries().len(), 40);
843867 assert_eq!(k.lookup("space", false), Some(ActionId::Play));
844868 // The seek keys: bare arrows a frame, ctrl arrows a second, and the two
845869 // ends of the timeline.
@@ -1015,7 +1039,8 @@ mod tests {
10151039 // A bindable chord is one `rebind_action` may keep: what it writes is
10161040 // what the next load reads, for every stroke that passes here.
10171041 let mut k = Keymap::defaults();
1018- k.rebind_action(ActionId::Play, chord("=", true)).unwrap();
1042+ // Any free chord will do here; ctrl+= is the zoom's.
1043+ k.rebind_action(ActionId::Play, chord("w", true)).unwrap();
10191044 assert_eq!(whole(&emit(&k)), k);
10201045 }
10211046
@@ -1072,7 +1097,8 @@ mod tests {
10721097 assert_eq!(whole("edith-keys 1\n").display(ActionId::Cut), "unbound");
10731098 // The label is the editor's column, and never the file's word for it.
10741099 assert_eq!(ActionId::CancelExport.label(), "Cancel export");
1075- assert_eq!(ActionId::ALL.len(), 35);
1100+ assert_eq!(ActionId::ALL.len(), 38);
1101+ assert_eq!(k.display(ActionId::ZoomIn), "ctrl+=");
10761102 }
10771103
10781104 #[test]
crates/app/src/main.rsmodified+471 -44
@@ -19,9 +19,9 @@ use engine::{Clip, ExportHandle, Frame, PlaybackSession};
1919 use gpui::{
2020 AnyElement, App, Application, Bounds, ClickEvent, Context, CursorStyle, Div, FocusHandle,
2121 KeyDownEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels,
22- Point, RenderImage, SharedString, Size, Stateful, TextAlign, TitlebarOptions, Window,
23- WindowBounds, WindowOptions, canvas, div, img, point, prelude::*, px, relative, rgb, rgba,
24- size,
22+ Point, RenderImage, ScrollDelta, ScrollWheelEvent, SharedString, Size, Stateful, TextAlign,
23+ TitlebarOptions, Window, WindowBounds, WindowOptions, canvas, div, img, point, prelude::*, px,
24+ relative, rgb, rgba, size,
2525 };
2626
2727 /// Editor chrome: three grays and one accent, all darker than the picture so the
@@ -74,6 +74,11 @@ const LABEL_MIN_W: f32 = 36.;
7474 /// allocates one bucket per window, so a rate taken from anything the user can
7575 /// influence is an allocation bomb -- and 40 is already finer than the pixels a
7676 /// clip is ever given.
77+///
78+/// ponytail: "finer than the pixels" stops being true once the timeline is
79+/// zoomed -- past ~25 ms per bucket the envelope reads as steps rather than as
80+/// a shape. Ceiling: [`View::max_zoom`]'s 8 frames across the bed. The upgrade
81+/// is a second, finer pass over the visible span only, cached like `waves` is.
7782 const WAVE_BPS: u32 = 40;
7883 /// Pixels per envelope column. Coarser than a pixel: the eye reads the shape,
7984 /// and a path with a point per pixel is a path per repaint.
@@ -594,6 +599,10 @@ struct Player {
594599 /// The ruler's own box, recorded at prepaint: a mouse listener is handed
595600 /// the window position and nothing else.
596601 ruler: Rc<Cell<Bounds<Pixels>>>,
602+ /// How much of the timeline the ruler and the lanes are showing. Held here
603+ /// and nowhere else: every frame-to-pixel answer in the panel comes out of
604+ /// it, so the boxes, the playhead and the pointer cannot disagree.
605+ view: View,
597606 /// Which clip the edit keys act on: the lane it is in and its index there.
598607 /// The *clicked* half, not the group -- a group is what gets marked on
599608 /// screen, but Lift has to know which half it was aimed at. Indices move
@@ -916,6 +925,12 @@ impl Player {
916925 ActionId::Color => self.open_color(cx),
917926 ActionId::Fit => self.cycle_fit(cx),
918927 ActionId::Resolution => self.cycle_resolution(cx),
928+ // The playhead is what a key zoom is aimed at: it is the one place
929+ // on the timeline the user is certainly looking at, and keeping it
930+ // still is what every editor does.
931+ ActionId::ZoomIn => self.zoom(ZOOM_STEP, None, cx),
932+ ActionId::ZoomOut => self.zoom(1. / ZOOM_STEP, None, cx),
933+ ActionId::ZoomFit => self.zoom_fit(cx),
919934 ActionId::Undo => self.undo(cx),
920935 ActionId::AddVideoLane => self.add_lane(LaneKind::Video, cx),
921936 ActionId::AddAudioLane => self.add_lane(LaneKind::Audio, cx),
@@ -1039,6 +1054,26 @@ impl Player {
10391054 cx.notify();
10401055 }
10411056
1057+ /// Magnifies the timeline about a point that stays put: `anchor` is where
1058+ /// along the bed to hold still (a ctrl+wheel holds the pointer), and with
1059+ /// none it is the playhead -- so the frame being worked on is still the
1060+ /// frame on screen after the zoom. Clamped at both ends by [`View`]: out
1061+ /// stops at the whole timeline, in at a handful of frames.
1062+ fn zoom(&mut self, factor: f32, anchor: Option<f32>, cx: &mut Context<Self>) {
1063+ let duration = self.drawn_duration();
1064+ let at = self.playhead(duration);
1065+ let anchor = anchor.unwrap_or_else(|| self.view.frac_at(at, duration).clamp(0., 1.));
1066+ self.view = self.view.zoomed(factor, anchor, duration, self.fps);
1067+ cx.notify();
1068+ }
1069+
1070+ /// All the way back out: the whole timeline across the bed, which is what
1071+ /// the panel shows before anyone has zoomed at all.
1072+ fn zoom_fit(&mut self, cx: &mut Context<Self>) {
1073+ self.view = View::default();
1074+ cx.notify();
1075+ }
1076+
10421077 /// Cycles the *project's* resolution through [`RESOLUTIONS`], starting from
10431078 /// the media's own -- the one size that must stay reachable, since a project
10441079 /// moved off it has no other way back (the resolution is not an undo step).
@@ -2108,14 +2143,16 @@ impl Player {
21082143 /// pointer exactly.
21092144 fn trim_to(&mut self, x: Pixels, cx: &mut Context<Self>) {
21102145 let (duration, fps) = (self.drawn_duration(), self.fps);
2111- let frac = frac_along(x, self.ruler.get());
2146+ let at = self
2147+ .view
2148+ .time_at(frac_along(x, self.ruler.get()), duration);
21122149 let (Some(trim), Some(session)) = (&mut self.trim, &self.session) else {
21132150 return;
21142151 };
21152152 let Some((lo, hi)) = session.trim_room(trim.lane, trim.idx, trim.edge) else {
21162153 return;
21172154 };
2118- trim.to = frame_at(f64::from(frac) * duration, fps).clamp(lo, hi);
2155+ trim.to = frame_at(at, fps).clamp(lo, hi);
21192156 cx.notify();
21202157 }
21212158
@@ -2201,6 +2238,21 @@ impl Player {
22012238 }
22022239 }
22032240
2241+ /// Where the playhead is, as the panel draws it. The clock keeps running
2242+ /// after the last frame (wall time takes over at audio EOF) while the
2243+ /// picture is frozen, so what the UI shows is the clamped one, pinned to
2244+ /// the out-point once playback is done.
2245+ fn playhead(&self, duration: f64) -> f64 {
2246+ if self.done {
2247+ duration
2248+ } else {
2249+ self.session
2250+ .as_ref()
2251+ .map_or(0., PlaybackSession::now)
2252+ .clamp(0., duration)
2253+ }
2254+ }
2255+
22042256 /// The one way a library row reaches the timeline: the Add button and a row
22052257 /// dragged onto a lane both come here, so there is a single answer to what
22062258 /// "add this source" does. The whole source goes in at the playhead as one
@@ -2657,7 +2709,9 @@ impl Player {
26572709 let Some(session) = &self.session else {
26582710 return;
26592711 };
2660- let t = f64::from(frac_along(x, self.ruler.get())) * session.timeline_duration();
2712+ let t = self
2713+ .view
2714+ .time_at(frac_along(x, self.ruler.get()), session.timeline_duration());
26612715 let target = (t * self.fps) as u32;
26622716 if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) {
26632717 self.last_target = target;
@@ -2927,17 +2981,12 @@ impl Render for Player {
29272981 // timecode, the ruler and the clamp below all have to follow it -- and
29282982 // so does the room a tail being dragged needs to grow into.
29292983 let duration = self.drawn_duration();
2930- // The clock keeps running after the last frame (wall time takes over at
2931- // audio EOF) while the picture is frozen, so the timeline the UI shows is
2932- // the clamped one, pinned to the out-point once playback is done.
2933- let position = if self.done {
2934- duration
2935- } else {
2936- self.session
2937- .as_ref()
2938- .map_or(0., PlaybackSession::now)
2939- .clamp(0., duration)
2940- };
2984+ let position = self.playhead(duration);
2985+ // Re-settled every frame against the duration this one is drawing: an
2986+ // edit that shortens the timeline moves the far end of the view, and a
2987+ // playhead that has run off the bed pulls the view after it -- which is
2988+ // what makes a zoomed-in timeline scroll while it plays.
2989+ self.view = self.view.following(position, duration, self.fps);
29412990
29422991 div()
29432992 .track_focus(&self.focus)
@@ -3638,11 +3687,11 @@ impl Player {
36383687 playing: bool,
36393688 cx: &mut Context<Self>,
36403689 ) -> impl IntoElement {
3641- let filled = if duration > 0. {
3642- (position / duration) as f32
3643- } else {
3644- 0.
3645- };
3690+ // Where the playhead is *on the bed*, which at the fit is where along
3691+ // the timeline it is and zoomed in is where along the visible slice.
3692+ // Clamped because it is drawn as a width as well as an offset, and the
3693+ // view follows the playhead anyway, so it is never off the bed.
3694+ let filled = self.view.frac_at(position, duration).clamp(0., 1.);
36463695 // An export owns the hint slot and the ruler while it runs: the
36473696 // percentage and the accent bar are the same number, so the playhead
36483697 // fill doubles as the progress bar for free.
@@ -3846,6 +3895,51 @@ impl Player {
38463895 live,
38473896 cx.listener(|this, _: &ClickEvent, _, cx| this.cycle_resolution(cx)),
38483897 ))
3898+ // How much of the timeline the panel below is showing --
3899+ // beside the resolution, since neither of them edits
3900+ // anything: they are both what is being looked at. The
3901+ // middle one reads as the state (1.0x is the whole
3902+ // timeline) and is the way back to it.
3903+ .child(control(
3904+ "zoom-out",
3905+ None,
3906+ "−",
3907+ format!(
3908+ "{} — show more of the timeline; stops at the whole of it",
3909+ key(ActionId::ZoomOut)
3910+ ),
3911+ live,
3912+ cx.listener(|this, _: &ClickEvent, _, cx| {
3913+ this.zoom(1. / ZOOM_STEP, None, cx)
3914+ }),
3915+ ))
3916+ .child(control(
3917+ "zoom-fit",
3918+ None,
3919+ format!("{:.1}x", self.view.zoom),
3920+ format!(
3921+ "{} — fit the whole timeline; showing {} to {}",
3922+ key(ActionId::ZoomFit),
3923+ timecode(self.view.start, self.fps),
3924+ timecode(
3925+ (self.view.start + self.view.span(duration)).min(duration),
3926+ self.fps
3927+ )
3928+ ),
3929+ live,
3930+ cx.listener(|this, _: &ClickEvent, _, cx| this.zoom_fit(cx)),
3931+ ))
3932+ .child(control(
3933+ "zoom-in",
3934+ None,
3935+ "+",
3936+ format!(
3937+ "{} — magnify around the playhead; ctrl+wheel on the bar zooms at the pointer",
3938+ key(ActionId::ZoomIn)
3939+ ),
3940+ live,
3941+ cx.listener(|this, _: &ClickEvent, _, cx| this.zoom(ZOOM_STEP, None, cx)),
3942+ ))
38493943 // Import is not here: it belongs to the media list it adds
38503944 // to, and two doors into one action is a question about
38513945 // which one is the real one.
@@ -3958,7 +4052,32 @@ impl Player {
39584052 .hover(|s| s.bg(rgb(HOVER_DIM)))
39594053 // The strip carries no text, so the tooltip is the only
39604054 // place it can say what it is.
3961- .tooltip(|_, cx| cx.new(|_| Tip("Seek — click or drag".into())).into())
4055+ .tooltip(|_, cx| {
4056+ cx.new(|_| Tip("Seek — click or drag; ctrl+wheel zooms".into()))
4057+ .into()
4058+ })
4059+ // Ctrl+wheel is what every timeline zooms with, and
4060+ // the point held still is the one under the pointer
4061+ // rather than the playhead. Only with ctrl: a bare
4062+ // wheel here is the window's to scroll, and the
4063+ // controls row above scrolls on exactly that.
4064+ .on_scroll_wheel(cx.listener(
4065+ |this, event: &ScrollWheelEvent, _, cx| {
4066+ if !event.modifiers.control {
4067+ return;
4068+ }
4069+ let dy = match event.delta {
4070+ ScrollDelta::Lines(d) => d.y,
4071+ ScrollDelta::Pixels(d) => f32::from(d.y),
4072+ };
4073+ if dy == 0. {
4074+ return;
4075+ }
4076+ let anchor = frac_along(event.position.x, this.ruler.get());
4077+ let factor = if dy > 0. { ZOOM_STEP } else { 1. / ZOOM_STEP };
4078+ this.zoom(factor, Some(anchor), cx);
4079+ },
4080+ ))
39624081 .on_mouse_down(
39634082 MouseButton::Left,
39644083 cx.listener(|this, event: &MouseDownEvent, _, cx| {
@@ -5550,6 +5669,9 @@ impl Player {
55505669 // one probe answers for both. Zero before the first paint, which only
55515670 // costs the labels one frame.
55525671 let bed_w = f32::from(self.ruler.get().size.width);
5672+ // The slice being shown, copied out once: every box in the row is
5673+ // placed through it, so all of them move together when it does.
5674+ let view = self.view;
55535675 let clips = self
55545676 .session
55555677 .as_ref()
@@ -5687,7 +5809,7 @@ impl Player {
56875809 .and_then(|s| sources.iter().position(|o| o.path == s.path))
56885810 .unwrap_or(clip.source),
56895811 );
5690- let width = width_frac(len, duration);
5812+ let width = view.width_frac(len, duration);
56915813 let label = sources.get(clip.source).map(|s| file_name(&s.path));
56925814 let wave = sources
56935815 .get(clip.source)
@@ -5711,7 +5833,11 @@ impl Player {
57115833 .absolute()
57125834 .top_0()
57135835 .h_full()
5714- .left(relative(start_frac(start, duration)))
5836+ // Negative once the clip's head has been scrolled
5837+ // off the left edge: the bed clips what hangs out
5838+ // of it, so a half-visible clip is drawn as the
5839+ // half of itself that is on screen.
5840+ .left(relative(view.frac_at(start, duration)))
57155841 .w(relative(width))
57165842 .overflow_hidden()
57175843 .rounded(px(3.))
@@ -5860,8 +5986,12 @@ impl Player {
58605986 .absolute()
58615987 .top_0()
58625988 .h_full()
5863- .left(relative(start_frac(f64::from(at) / self.fps, duration)))
5864- .w(relative(width_frac(f64::from(len) / self.fps, duration)))
5989+ .left(relative(
5990+ view.frac_at(f64::from(at) / self.fps, duration),
5991+ ))
5992+ .w(relative(
5993+ view.width_frac(f64::from(len) / self.fps, duration),
5994+ ))
58655995 .bg(rgba(0x4a9effaa))
58665996 }),
58675997 )
@@ -6691,7 +6821,18 @@ enum Repeat {
66916821 fn repeats(scope: Repeat, key: &str, action: Option<ActionId>) -> bool {
66926822 match scope {
66936823 Repeat::Card => matches!(key, "up" | "down" | "left" | "right"),
6694- Repeat::Keymap => matches!(action, Some(ActionId::VolumeUp | ActionId::VolumeDown)),
6824+ Repeat::Keymap => matches!(
6825+ action,
6826+ Some(
6827+ ActionId::VolumeUp
6828+ | ActionId::VolumeDown
6829+ // A zoom is a value being moved as much as a level is:
6830+ // held, it runs from the whole timeline down to a handful
6831+ // of frames and stops there. The fit is one press.
6832+ | ActionId::ZoomIn
6833+ | ActionId::ZoomOut
6834+ )
6835+ ),
66956836 Repeat::Nothing => false,
66966837 }
66976838 }
@@ -6789,14 +6930,127 @@ fn width_frac(len: f64, total: f64) -> f32 {
67896930 if total > 0. { (len / total) as f32 } else { 1. }
67906931 }
67916932
6792-/// Where along the lane a clip starts, 0..1. Unlike [`width_frac`] a timeline
6793-/// with no length pins this to the left edge -- a full-width offset would push
6794-/// the box out of the lane it belongs to.
6795-fn start_frac(start: f64, total: f64) -> f32 {
6796- if total > 0. {
6797- (start / total).clamp(0., 1.) as f32
6798- } else {
6799- 0.
6933+/// One press of a zoom key, or one notch of ctrl+wheel.
6934+const ZOOM_STEP: f32 = 1.25;
6935+
6936+/// How few frames the bed may be narrowed down to. Past this there is nothing
6937+/// left to aim at -- a single frame across a whole window is a wall of colour,
6938+/// not an edit surface.
6939+const ZOOM_MIN_FRAMES: f64 = 8.;
6940+
6941+/// Which slice of the timeline the ruler and the lanes are showing: `zoom` is
6942+/// how many times the whole of it the bed is now worth, and `start` the moment
6943+/// at the bed's left edge. The one place frames become pixels -- every box, the
6944+/// playhead, a seek and a trim all go through it, so none of them can drift
6945+/// away from the others when the view moves.
6946+///
6947+/// [`View::default`] is the whole timeline across the bed, which is what the
6948+/// panel drew before there was a zoom at all: at zoom 1 `start` is pinned to 0
6949+/// and every fraction here is the one [`width_frac`] and `start_frac` gave.
6950+#[derive(Clone, Copy, Debug, PartialEq)]
6951+struct View {
6952+ zoom: f32,
6953+ start: f64,
6954+}
6955+
6956+impl Default for View {
6957+ fn default() -> Self {
6958+ View {
6959+ zoom: 1.,
6960+ start: 0.,
6961+ }
6962+ }
6963+}
6964+
6965+impl View {
6966+ /// How much of the timeline is on the bed, in seconds.
6967+ fn span(self, duration: f64) -> f64 {
6968+ duration / f64::from(self.zoom)
6969+ }
6970+
6971+ /// The tightest this timeline may be zoomed: [`ZOOM_MIN_FRAMES`] across the
6972+ /// bed. Never under 1 -- a timeline shorter than that is already at its
6973+ /// floor, and a zoom that cannot fit the whole of it is not a zoom.
6974+ fn max_zoom(duration: f64, fps: f64) -> f32 {
6975+ let frames = duration * fps;
6976+ if frames > ZOOM_MIN_FRAMES {
6977+ (frames / ZOOM_MIN_FRAMES) as f32
6978+ } else {
6979+ 1.
6980+ }
6981+ }
6982+
6983+ /// Where a moment sits along the bed, 0 at its left edge and 1 at its
6984+ /// right. Outside that range for a moment scrolled off the view, which is
6985+ /// exactly the negative offset a half-visible clip is drawn at.
6986+ fn frac_at(self, at: f64, duration: f64) -> f32 {
6987+ let span = self.span(duration);
6988+ if span > 0. {
6989+ ((at - self.start) / span) as f32
6990+ } else {
6991+ 0.
6992+ }
6993+ }
6994+
6995+ /// How wide a stretch of `len` seconds is on the bed. More than the whole
6996+ /// bed once zoomed in, which is the point of zooming in.
6997+ fn width_frac(self, len: f64, duration: f64) -> f32 {
6998+ width_frac(len, self.span(duration))
6999+ }
7000+
7001+ /// The moment a pointer at `frac` along the bed is pointing at: the inverse
7002+ /// of [`View::frac_at`], and what every seek and every trim reads.
7003+ fn time_at(self, frac: f32, duration: f64) -> f64 {
7004+ (self.start + f64::from(frac) * self.span(duration)).clamp(0., duration)
7005+ }
7006+
7007+ /// Zoomed by `factor` about `anchor` (0..1 along the bed): whatever moment
7008+ /// was under that point stays under it, so a zoom magnifies what was being
7009+ /// looked at rather than throwing it off the edge.
7010+ fn zoomed(self, factor: f32, anchor: f32, duration: f64, fps: f64) -> Self {
7011+ let at = self.start + f64::from(anchor) * self.span(duration);
7012+ // Clamped *before* the offset is worked out, not after: a press that
7013+ // runs into either stop must still leave the anchor where it is, and a
7014+ // start measured against a zoom the stop then took away would slide it.
7015+ let zoom = (self.zoom * factor).clamp(1., Self::max_zoom(duration, fps));
7016+ let start = at - f64::from(anchor) * View { zoom, start: 0. }.span(duration);
7017+ View { zoom, start }.settled(duration, fps)
7018+ }
7019+
7020+ /// Clamped to the timeline it draws: never wider than the whole of it (the
7021+ /// zoom-out floor is the fit), never tighter than [`View::max_zoom`], and
7022+ /// never scrolled past either end.
7023+ fn settled(self, duration: f64, fps: f64) -> Self {
7024+ let zoom = if self.zoom.is_finite() {
7025+ self.zoom.clamp(1., Self::max_zoom(duration, fps))
7026+ } else {
7027+ 1.
7028+ };
7029+ let span = View { zoom, start: 0. }.span(duration);
7030+ let start = if self.start.is_finite() {
7031+ self.start.clamp(0., (duration - span).max(0.))
7032+ } else {
7033+ 0.
7034+ };
7035+ View { zoom, start }
7036+ }
7037+
7038+ /// The view a playhead at `at` needs: the same one while it is on the bed,
7039+ /// and one centred on it once it has run off -- which is how a zoomed-in
7040+ /// timeline scrolls, during playback and after a seek alike. At the fit
7041+ /// this can never fire, so an unzoomed panel is untouched by it.
7042+ fn following(self, at: f64, duration: f64, fps: f64) -> Self {
7043+ let view = self.settled(duration, fps);
7044+ let span = view.span(duration);
7045+ if at < view.start || at > view.start + span {
7046+ View {
7047+ start: at - span / 2.,
7048+ ..view
7049+ }
7050+ .settled(duration, fps)
7051+ } else {
7052+ view
7053+ }
68007054 }
68017055 }
68027056
@@ -7465,10 +7719,13 @@ mod tests {
74657719 export_settings, format_key, format_line, format_refusal, frac_along, frac_down, frame_at,
74667720 histogram, is_bare_modifier, is_project, keymap, lanes_h, marked, menu_at, normalise,
74677721 nothing_to_play, panel_h, project_path, push_digit, retarget, scrub_due, show_label,
7468- silence_rate, source_tint, span_partner, speed_at, start_frac, timecode, unseen_paths,
7469- unseen_sources, whole_take, width_frac, window_title,
7722+ silence_rate, source_tint, span_partner, speed_at, timecode, unseen_paths, unseen_sources,
7723+ whole_take, width_frac, window_title,
7724+ };
7725+ use super::{
7726+ LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows,
7727+ repeats, unscannable,
74707728 };
7471- use super::{LaneKind, Repeat, file_name, file_uri, library_rows, repeats, unscannable};
74727729
74737730 /// What the file manager is handed: the parts a path keeps as they are, and
74747731 /// the ones the bus would otherwise read as something else.
@@ -7856,6 +8113,10 @@ mod tests {
78568113 ActionId::Save => element("save"),
78578114 ActionId::Undo => element("undo"),
78588115 ActionId::Resolution => element("resolution"),
8116+ // Three buttons beside it, and the ctrl+wheel on the bar.
8117+ ActionId::ZoomIn => element("zoom-in"),
8118+ ActionId::ZoomOut => element("zoom-out"),
8119+ ActionId::ZoomFit => element("zoom-fit"),
78598120 ActionId::AddVideoLane => element("add-video-lane"),
78608121 ActionId::AddAudioLane => element("add-audio-lane"),
78618122 // The × in a lane's own header, which takes any track and
@@ -8576,6 +8837,161 @@ mod tests {
85768837 }
85778838 }
85788839
8840+ /// The mapping the whole panel is drawn and clicked through: a moment goes
8841+ /// to a fraction of the bed and comes back the same moment, at every zoom.
8842+ /// The fit is the invariant that matters most -- it must be the arithmetic
8843+ /// the panel did before there was a zoom at all.
8844+ #[test]
8845+ fn a_moment_and_the_place_it_is_drawn_are_the_same_at_every_zoom() {
8846+ let duration = 20.;
8847+ // The fit: what `start_frac`/`width_frac` used to answer, exactly.
8848+ let fit = View::default();
8849+ for t in [0., 5., 12.5, 20.] {
8850+ assert_eq!(fit.frac_at(t, duration), (t / duration) as f32, "fit at {t}");
8851+ }
8852+ assert_eq!(fit.width_frac(5., duration), width_frac(5., duration));
8853+ for view in [
8854+ fit,
8855+ View {
8856+ zoom: 2.,
8857+ start: 4.,
8858+ },
8859+ View {
8860+ zoom: 8.,
8861+ start: 12.5,
8862+ },
8863+ View {
8864+ zoom: 37.5,
8865+ start: 19.,
8866+ },
8867+ ] {
8868+ let view = view.settled(duration, 30.);
8869+ for frac in [0., 0.25, 0.5, 1.] {
8870+ let at = view.time_at(frac, duration);
8871+ assert!(
8872+ (f64::from(view.frac_at(at, duration)) - f64::from(frac)).abs() < 1e-6,
8873+ "{view:?} round trip at {frac}"
8874+ );
8875+ }
8876+ // A stretch is as wide as the share of the visible slice it takes.
8877+ assert!(
8878+ (f64::from(view.width_frac(view.span(duration), duration)) - 1.).abs() < 1e-6,
8879+ "{view:?} spans the bed"
8880+ );
8881+ }
8882+ }
8883+
8884+ /// The rule that makes a zoom usable: whatever was under the anchor is
8885+ /// still under it afterwards -- the playhead for a key, the pointer for a
8886+ /// ctrl+wheel.
8887+ #[test]
8888+ fn a_zoom_leaves_the_anchor_where_it_was_on_screen() {
8889+ let (duration, fps) = (20., 30.);
8890+ let mut view = View::default();
8891+ for anchor in [0.5f32, 0.25, 0.9] {
8892+ // Well past the zoom-in stop: the anchor holds at the clamp too,
8893+ // which is where it used to slide (the clamp came after the offset).
8894+ for _ in 0..30 {
8895+ let at = view.time_at(anchor, duration);
8896+ let zoomed = view.zoomed(ZOOM_STEP, anchor, duration, fps);
8897+ // Only where the anchor is not pinned by an edge of the
8898+ // timeline: a view already against an end cannot slide further.
8899+ let pinned = zoomed.start <= 0. || zoomed.start + zoomed.span(duration) >= duration;
8900+ if !pinned {
8901+ assert!(
8902+ (f64::from(zoomed.frac_at(at, duration)) - f64::from(anchor)).abs() < 1e-6,
8903+ "{at} moved: {view:?} -> {zoomed:?}"
8904+ );
8905+ }
8906+ assert!(zoomed.zoom >= view.zoom, "and it did zoom in");
8907+ view = zoomed;
8908+ }
8909+ }
8910+ }
8911+
8912+ /// Both stops: out is the whole timeline (which is where the panel starts,
8913+ /// so nothing regressed for anyone who never zooms), in is a handful of
8914+ /// frames -- and neither can scroll off an end.
8915+ #[test]
8916+ fn zoom_stops_at_the_whole_timeline_and_at_a_handful_of_frames() {
8917+ let (duration, fps) = (20., 30.);
8918+ let mut view = View::default();
8919+ for _ in 0..200 {
8920+ view = view.zoomed(1. / ZOOM_STEP, 0.5, duration, fps);
8921+ }
8922+ assert_eq!(view, View::default(), "zoomed out is the fit");
8923+ for _ in 0..200 {
8924+ view = view.zoomed(ZOOM_STEP, 0.5, duration, fps);
8925+ }
8926+ assert_eq!(
8927+ (view.span(duration) * fps).round(),
8928+ ZOOM_MIN_FRAMES,
8929+ "tightest is a handful of frames"
8930+ );
8931+ // Against the far end, the slice still ends at the last frame.
8932+ let end = View {
8933+ zoom: view.zoom,
8934+ start: 1e6,
8935+ }
8936+ .settled(duration, fps);
8937+ assert!((end.start + end.span(duration) - duration).abs() < 1e-9);
8938+ assert!(
8939+ View {
8940+ zoom: view.zoom,
8941+ start: -5.
8942+ }
8943+ .settled(duration, fps)
8944+ .start
8945+ == 0.
8946+ );
8947+ // A timeline too short to zoom into, and one with no length at all,
8948+ // both stay at the fit rather than dividing by nothing.
8949+ assert_eq!(View::max_zoom(0.1, fps), 1.);
8950+ assert_eq!(View::default().settled(0., fps), View::default());
8951+ assert_eq!(View::default().frac_at(3., 0.), 0.);
8952+ assert_eq!(View::default().time_at(0.5, 0.), 0.);
8953+ }
8954+
8955+ /// What makes a zoomed timeline follow the playing head: off the bed at
8956+ /// either end pulls the view onto it, and on the bed it never moves -- a
8957+ /// view that jumped every frame would be unreadable.
8958+ #[test]
8959+ fn the_view_follows_a_playhead_that_runs_off_the_bed() {
8960+ let (duration, fps) = (20., 30.);
8961+ let view = View {
8962+ zoom: 4.,
8963+ start: 5.,
8964+ }
8965+ .settled(duration, fps);
8966+ assert_eq!(view.span(duration), 5.);
8967+ // Inside: untouched, whichever part of the slice it is in.
8968+ for at in [5., 7.5, 10.] {
8969+ assert_eq!(view.following(at, duration, fps), view, "{at} is on the bed");
8970+ }
8971+ // Past the right edge, as playback does it: the head comes back on the
8972+ // bed, and the zoom is not changed by the scroll.
8973+ let moved = view.following(12., duration, fps);
8974+ assert_eq!(moved.zoom, view.zoom);
8975+ assert!(moved.start > view.start, "scrolled forward");
8976+ assert!(
8977+ moved.frac_at(12., duration) > 0. && moved.frac_at(12., duration) < 1.,
8978+ "and the playhead is on screen"
8979+ );
8980+ // A seek back behind the slice does the same the other way.
8981+ let back = view.following(1., duration, fps);
8982+ assert!(back.start < view.start);
8983+ assert!(back.frac_at(1., duration) >= 0.);
8984+ // At the fit there is nothing to follow: the whole timeline is on the
8985+ // bed already, so no playhead can move it.
8986+ for at in [0., 10., 20.] {
8987+ assert_eq!(
8988+ View::default().following(at, duration, fps),
8989+ View::default(),
8990+ "the fit never scrolls"
8991+ );
8992+ }
8993+ }
8994+
85798995 #[test]
85808996 fn clip_boxes_split_the_lane_by_duration() {
85818997 // 1 s + 3 s of a 4 s timeline: a quarter and three quarters.
@@ -9158,11 +9574,20 @@ mod tests {
91589574 // value being moved.
91599575 assert!(repeats(Repeat::Keymap, "up", Some(ActionId::VolumeUp)));
91609576 assert!(repeats(Repeat::Keymap, "down", Some(ActionId::VolumeDown)));
9577+ // ...and the zoom pair, which runs the view the way they run the level.
9578+ assert!(repeats(Repeat::Keymap, "=", Some(ActionId::ZoomIn)));
9579+ assert!(!repeats(Repeat::Keymap, "0", Some(ActionId::ZoomFit)));
91619580 for action in ActionId::ALL {
91629581 let held = repeats(Repeat::Keymap, "k", Some(action));
91639582 assert_eq!(
91649583 held,
9165- matches!(action, ActionId::VolumeUp | ActionId::VolumeDown),
9584+ matches!(
9585+ action,
9586+ ActionId::VolumeUp
9587+ | ActionId::VolumeDown
9588+ | ActionId::ZoomIn
9589+ | ActionId::ZoomOut
9590+ ),
91669591 "{action:?} on a hold"
91679592 );
91689593 }
@@ -9386,10 +9811,9 @@ mod tests {
93869811 assert!(show_label(400.));
93879812 assert!(!show_label(LABEL_MIN_W - 0.1));
93889813 // A timeline with no length must not put a box outside its lane.
9389- assert_eq!(start_frac(3., 0.), 0.);
9390- assert_eq!(start_frac(1., 4.), 0.25);
9391- // Past the end clamps rather than running off the bed.
9392- assert_eq!(start_frac(8., 4.), 1.);
9814+ let fit = View::default();
9815+ assert_eq!(fit.frac_at(3., 0.), 0.);
9816+ assert_eq!(fit.frac_at(1., 4.), 0.25);
93939817 }
93949818
93959819 #[test]
@@ -9754,6 +10178,9 @@ fn main() {
975410178 eos: false,
975510179 done: false,
975610180 ruler: Rc::default(),
10181+ // The whole timeline across the bed, which is where a
10182+ // project opens: zooming is something the user asks for.
10183+ view: View::default(),
975710184 selected: None,
975810185 context_menu: None,
975910186 library_menu: None,