forge
Explore
Log in

Don't you dare go Hollow

🔀 merge(B1): the timeline measures seconds in pixels — absolute scale, honest zoom stops

Browse files at this commit

0c3c47668ad321209495f31386a4f3deef49ba51Ahmet Kaan GümüşParent: d9fd7cf8baa710
Scale{pps,start}: a second of timeline is a fixed number of pixels wherever it
is, so a clip's width says how long the clip is and adding a second one does not
redraw the first one narrower. width_frac/frac_at are gone; every box, the
playhead, a seek and a trim go through the one mapping.

Composed with main: the transport state machine reads Transport, the panel and
the render loop take their state from it and their geometry from Scale. The
zoom-out tooltip stops lying about where it stops -- four hours on the bed, which
is what ZOOM_MAX_SECONDS actually is.
crates/app/src/main.rsmodified+470 -265
@@ -681,10 +681,10 @@ struct Player {
681 /// The ruler's own box, recorded at prepaint: a mouse listener is handed681 /// The ruler's own box, recorded at prepaint: a mouse listener is handed
682 /// the window position and nothing else.682 /// the window position and nothing else.
683 ruler: Rc<Cell<Bounds<Pixels>>>,683 ruler: Rc<Cell<Bounds<Pixels>>>,
684- /// How much of the timeline the ruler and the lanes are showing. Held here684+ /// How wide a second of timeline is drawn, and from which moment. Held here
685 /// and nowhere else: every frame-to-pixel answer in the panel comes out of685 /// and nowhere else: every frame-to-pixel answer in the panel comes out of
686 /// it, so the boxes, the playhead and the pointer cannot disagree.686 /// it, so the boxes, the playhead and the pointer cannot disagree.
687- view: View,687+ scale: Scale,
688 /// Which clip the edit keys act on: the lane it is in and its index there.688 /// Which clip the edit keys act on: the lane it is in and its index there.
689 /// The *clicked* half, not the group -- a group is what gets marked on689 /// The *clicked* half, not the group -- a group is what gets marked on
690 /// screen, but Lift has to know which half it was aimed at. Indices move690 /// screen, but Lift has to know which half it was aimed at. Indices move
@@ -1201,23 +1201,37 @@ impl Player {
1201 cx.notify();1201 cx.notify();
1202 }1202 }
1203 1203
1204- /// Magnifies the timeline about a point that stays put: `anchor` is where1204+ /// The scale against the bed it is drawn on and the timeline it is drawn
1205- /// along the bed to hold still (a ctrl+wheel holds the pointer), and with1205+ /// from: every clamp, zoom and scroll is worked out through this, and the
1206- /// none it is the playhead -- so the frame being worked on is still the1206+ /// bed is measured off the ruler's probe rather than remembered, so a
1207+ /// resized window is a resized view on the very next answer.
1208+ fn view(&self) -> View {
1209+ View {
1210+ scale: self.scale,
1211+ bed: f32::from(self.ruler.get().size.width),
1212+ duration: self.drawn_duration(),
1213+ fps: self.fps,
1214+ }
1215+ }
1216+
1217+ /// Magnifies the timeline about a point that stays put: `anchor` is how many
1218+ /// pixels along the bed to hold still (a ctrl+wheel holds the pointer), and
1219+ /// with none it is the playhead -- so the frame being worked on is still the
1207 /// frame on screen after the zoom. Clamped at both ends by [`View`]: out1220 /// frame on screen after the zoom. Clamped at both ends by [`View`]: out
1208- /// stops at the whole timeline, in at a handful of frames.1221+ /// stops at [`ZOOM_MAX_SECONDS`] on the bed, in at a handful of frames.
1209 fn zoom(&mut self, factor: f32, anchor: Option<f32>, cx: &mut Context<Self>) {1222 fn zoom(&mut self, factor: f32, anchor: Option<f32>, cx: &mut Context<Self>) {
1210- let duration = self.drawn_duration();1223+ let view = self.view();
1211- let at = self.playhead(duration);1224+ let at = self.playhead(view.duration);
1212- let anchor = anchor.unwrap_or_else(|| self.view.frac_at(at, duration).clamp(0., 1.));1225+ let anchor = anchor.unwrap_or_else(|| self.scale.px_at(at).clamp(0., view.bed));
1213- self.view = self.view.zoomed(factor, anchor, duration, self.fps);1226+ self.scale = view.zoomed(factor, anchor);
1214 cx.notify();1227 cx.notify();
1215 }1228 }
1216 1229
1217- /// All the way back out: the whole timeline across the bed, which is what1230+ /// All the way back out: the whole timeline across the bed, and the one
1218- /// the panel shows before anyone has zoomed at all.1231+ /// thing that reads the timeline's own length to decide how wide a second
1232+ /// is drawn.
1219 fn zoom_fit(&mut self, cx: &mut Context<Self>) {1233 fn zoom_fit(&mut self, cx: &mut Context<Self>) {
1220- self.view = View::default();1234+ self.scale = self.view().fit();
1221 cx.notify();1235 cx.notify();
1222 }1236 }
1223 1237
@@ -2421,14 +2435,12 @@ impl Player {
2421 /// there is no such clip to move. The engine has the last word on where it2435 /// there is no such clip to move. The engine has the last word on where it
2422 /// may actually go -- this is the ask, not the answer.2436 /// may actually go -- this is the ask, not the answer.
2423 ///2437 ///
2424- /// ponytail: the bed ends where the timeline does, so a clip cannot be2438+ /// ponytail: the bed now runs past the last frame whenever the timeline is
2425- /// dragged *past* the last frame -- the pointer has no pixel out there to2439+ /// shorter than the view ([`Scale::time_at`] clamps at the head only), so a
2426- /// name and [`View::time_at`] clamps to the duration. Room to the right is2440+ /// clip *can* be dragged out there. Zoomed in against the far end it cannot:
2427- /// made by pulling a tail out first ([`Player::drawn_duration`] already2441+ /// the scroll clamp pins the bed's right edge to the duration, and the
2428- /// lengthens the bed for exactly that gesture). The upgrade is to draw the2442+ /// pointer has no pixel past it. The upgrade is to let the scroll clamp
2429- /// bed past the end while a clip drag is in flight, the way a trim already2443+ /// leave a screen of empty bed after the end, the way every NLE does.
2430- /// does: `drawn_duration` would take the dragged clip's length the same way
2431- /// it takes a trim's reach.
2432 fn drop_frame(&self, from: Lane, idx: usize, to: Lane, x: Pixels) -> Option<u32> {2444 fn drop_frame(&self, from: Lane, idx: usize, to: Lane, x: Pixels) -> Option<u32> {
2433 let session = self.session.as_ref()?;2445 let session = self.session.as_ref()?;
2434 let clip = session.lane_clips(from).get(idx).copied()?;2446 let clip = session.lane_clips(from).get(idx).copied()?;
@@ -2451,16 +2463,11 @@ impl Player {
2451 Some(snapped(raw, clip.frames(), self.snap_frames(), &marks))2463 Some(snapped(raw, clip.frames(), self.snap_frames(), &marks))
2452 }2464 }
2453 2465
2454- /// [`SNAP_PX`] in timeline frames at the zoom the bed is drawn at: a snap is2466+ /// [`SNAP_PX`] in timeline frames at the scale the bed is drawn at: the bed's
2455- /// a distance on screen, so zoomed right in it is worth less than a frame2467+ /// own width drops out of it, since a pixel is now worth the same stretch of
2456- /// (no snap at all, which is what a hand placing single frames wants) and2468+ /// timeline wherever the view sits.
2457- /// zoomed out to the whole timeline it is worth many.
2458 fn snap_frames(&self) -> u32 {2469 fn snap_frames(&self) -> u32 {
2459- let bed = f64::from(f32::from(self.ruler.get().size.width));2470+ self.scale.snap_frames(self.fps)
2460- if bed <= 0. {
2461- return 0;
2462- }
2463- (SNAP_PX / bed * self.view.span(self.drawn_duration()) * self.fps) as u32
2464 }2471 }
2465 2472
2466 /// Opens the clip menu on the box under the pointer, from the right button2473 /// Opens the clip menu on the box under the pointer, from the right button
@@ -2529,14 +2536,13 @@ impl Player {
2529 }2536 }
2530 2537
2531 /// The timeline frame a pointer at window x is on: along the same bed the2538 /// The timeline frame a pointer at window x is on: along the same bed the
2532- /// ruler is measured on, through the same [`View`] every box is drawn2539+ /// ruler is measured on, through the same [`Scale`] every box is drawn
2533 /// through, so a zoomed-in panel answers with the frame under the pointer2540 /// through, so a zoomed-in panel answers with the frame under the pointer
2534 /// and not with the one that would have been there unzoomed. The one2541 /// and not with the one that would have been there unzoomed. The one
2535 /// question a trim, a grab and a drop all ask.2542 /// question a trim, a grab and a drop all ask.
2536 fn frame_under(&self, x: Pixels) -> u32 {2543 fn frame_under(&self, x: Pixels) -> u32 {
2537- let duration = self.drawn_duration();
2538 frame_at(2544 frame_at(
2539- self.view.time_at(frac_along(x, self.ruler.get()), duration),2545+ self.scale.time_at(px_along(x, self.ruler.get())),
2540 self.fps,2546 self.fps,
2541 )2547 )
2542 }2548 }
@@ -3195,9 +3201,12 @@ impl Player {
3195 let Some(session) = &self.session else {3201 let Some(session) = &self.session else {
3196 return;3202 return;
3197 };3203 };
3204+ // Clamped to the timeline here rather than in the mapping: there is bed
3205+ // past the last frame now, and a seek out there is a seek to the end.
3198 let t = self3206 let t = self
3199- .view3207+ .scale
3200- .time_at(frac_along(x, self.ruler.get()), session.timeline_duration());3208+ .time_at(px_along(x, self.ruler.get()))
3209+ .clamp(0., session.timeline_duration());
3201 let target = (t * self.fps) as u32;3210 let target = (t * self.fps) as u32;
3202 if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) {3211 if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) {
3203 self.last_target = target;3212 self.last_target = target;
@@ -3513,7 +3522,7 @@ impl Render for Player {
3513 // edit that shortens the timeline moves the far end of the view, and a3522 // edit that shortens the timeline moves the far end of the view, and a
3514 // playhead that has run off the bed pulls the view after it -- which is3523 // playhead that has run off the bed pulls the view after it -- which is
3515 // what makes a zoomed-in timeline scroll while it plays.3524 // what makes a zoomed-in timeline scroll while it plays.
3516- self.view = self.view.following(position, duration, self.fps);3525+ self.scale = self.view().following(position);
3517 3526
3518 div()3527 div()
3519 .track_focus(&self.focus)3528 .track_focus(&self.focus)
@@ -4289,11 +4298,12 @@ impl Player {
4289 state: Transport,4298 state: Transport,
4290 cx: &mut Context<Self>,4299 cx: &mut Context<Self>,
4291 ) -> impl IntoElement {4300 ) -> impl IntoElement {
4292- // Where the playhead is *on the bed*, which at the fit is where along4301+ // Where the playhead is *on the bed*, in pixels from its left edge.
4293- // the timeline it is and zoomed in is where along the visible slice.4302+ // Clamped to the bed because it is drawn as a width as well as an
4294- // Clamped because it is drawn as a width as well as an offset, and the4303+ // offset, and the view follows the playhead anyway, so it is never off
4295- // view follows the playhead anyway, so it is never off the bed.4304+ // the bed for long.
4296- let filled = self.view.frac_at(position, duration).clamp(0., 1.);4305+ let bed_w = f32::from(self.ruler.get().size.width);
4306+ let filled = self.scale.px_at(position).clamp(0., bed_w);
4297 // An export owns the hint slot and the ruler while it runs: the4307 // An export owns the hint slot and the ruler while it runs: the
4298 // percentage and the accent bar are the same number, so the playhead4308 // percentage and the accent bar are the same number, so the playhead
4299 // fill doubles as the progress bar for free.4309 // fill doubles as the progress bar for free.
@@ -4316,7 +4326,7 @@ impl Player {
4316 // closure would hold it for as long as the iterator lives.4326 // closure would hold it for as long as the iterator lives.
4317 let mut rows = Vec::new();4327 let mut rows = Vec::new();
4318 for &lane in &lanes {4328 for &lane in &lanes {
4319- rows.push(self.lane_row(lane, duration, filled, cx));4329+ rows.push(self.lane_row(lane, filled, cx));
4320 }4330 }
4321 let (hint, filled) = if let Some(export) = self.exporting() {4331 let (hint, filled) = if let Some(export) = self.exporting() {
4322 let progress = export.progress();4332 let progress = export.progress();
@@ -4515,14 +4525,14 @@ impl Player {
4515 // How much of the timeline the panel below is showing --4525 // How much of the timeline the panel below is showing --
4516 // beside the resolution, since neither of them edits4526 // beside the resolution, since neither of them edits
4517 // anything: they are both what is being looked at. The4527 // anything: they are both what is being looked at. The
4518- // middle one reads as the state (1.0x is the whole4528+ // middle one says how much of it is on the bed, and is the
4519- // timeline) and is the way back to it.4529+ // way back to the whole of it.
4520 .child(control(4530 .child(control(
4521 "zoom-out",4531 "zoom-out",
4522 None,4532 None,
4523 "−",4533 "−",
4524 format!(4534 format!(
4525- "{} — show more of the timeline; stops at the whole of it",4535+ "{} — show more of the timeline; stops at four hours on the bed",
4526 key(ActionId::ZoomOut)4536 key(ActionId::ZoomOut)
4527 ),4537 ),
4528 live,4538 live,
@@ -4533,13 +4543,17 @@ impl Player {
4533 .child(control(4543 .child(control(
4534 "zoom-fit",4544 "zoom-fit",
4535 None,4545 None,
4536- format!("{:.1}x", self.view.zoom),4546+ // How much timeline is on the bed, not a multiple of
4547+ // "the whole of it": the scale no longer knows what the
4548+ // whole of it is, and a number that changed on every
4549+ // import was a number that lied.
4550+ span_label(self.view().span()),
4537 format!(4551 format!(
4538 "{} — fit the whole timeline; showing {} to {}",4552 "{} — fit the whole timeline; showing {} to {}",
4539 key(ActionId::ZoomFit),4553 key(ActionId::ZoomFit),
4540- timecode(self.view.start, self.fps),4554+ timecode(self.scale.start, self.fps),
4541 timecode(4555 timecode(
4542- (self.view.start + self.view.span(duration)).min(duration),4556+ (self.scale.start + self.view().span()).min(duration),
4543 self.fps4557 self.fps
4544 )4558 )
4545 ),4559 ),
@@ -4690,7 +4704,7 @@ impl Player {
4690 if dy == 0. {4704 if dy == 0. {
4691 return;4705 return;
4692 }4706 }
4693- let anchor = frac_along(event.position.x, this.ruler.get());4707+ let anchor = px_along(event.position.x, this.ruler.get());
4694 let factor = if dy > 0. { ZOOM_STEP } else { 1. / ZOOM_STEP };4708 let factor = if dy > 0. { ZOOM_STEP } else { 1. / ZOOM_STEP };
4695 this.zoom(factor, Some(anchor), cx);4709 this.zoom(factor, Some(anchor), cx);
4696 },4710 },
@@ -4712,7 +4726,7 @@ impl Player {
4712 .child(4726 .child(
4713 div()4727 div()
4714 .h_full()4728 .h_full()
4715- .w(relative(filled))4729+ .w(px(filled))
4716 .rounded(px(3.))4730 .rounded(px(3.))
4717 .bg(rgb(ACCENT)),4731 .bg(rgb(ACCENT)),
4718 ),4732 ),
@@ -6667,21 +6681,18 @@ impl Player {
6667 fn lane_row(6681 fn lane_row(
6668 &self,6682 &self,
6669 lane: Lane,6683 lane: Lane,
6670- duration: f64,6684+ // Where the playhead is on the bed, in pixels: worked out once by the
6685+ // panel so the ruler's line and every lane's draw the same one.
6671 filled: f32,6686 filled: f32,
6672 cx: &mut Context<Self>,6687 cx: &mut Context<Self>,
6673 // Borrows nothing it was given (`use<>`): the rows are built one after6688 // Borrows nothing it was given (`use<>`): the rows are built one after
6674 // another into a list, and a row still holding `cx` would be the only6689 // another into a list, and a row still holding `cx` would be the only
6675 // one that could be built.6690 // one that could be built.
6676 ) -> impl IntoElement + use<> {6691 ) -> impl IntoElement + use<> {
6677- // The bed's own width, measured last repaint off the ruler's bar: the6692+ // The mapping, copied out once: every box in the row is placed through
6678- // two are laid out identically (same header offset, same `flex_1`), so6693+ // it, so all of them move together when it does. No bed width is needed
6679- // one probe answers for both. Zero before the first paint, which only6694+ // to place them any more -- a second is so many pixels wherever it is.
6680- // costs the labels one frame.6695+ let scale = self.scale;
6681- let bed_w = f32::from(self.ruler.get().size.width);
6682- // The slice being shown, copied out once: every box in the row is
6683- // placed through it, so all of them move together when it does.
6684- let view = self.view;
6685 let clips = self6696 let clips = self
6686 .session6697 .session
6687 .as_ref()6698 .as_ref()
@@ -6865,7 +6876,7 @@ impl Player {
6865 .and_then(|s| sources.iter().position(|o| o.path == s.path))6876 .and_then(|s| sources.iter().position(|o| o.path == s.path))
6866 .unwrap_or(clip.source),6877 .unwrap_or(clip.source),
6867 );6878 );
6868- let width = view.width_frac(len, duration);6879+ let width = scale.width_px(len);
6869 let label = sources.get(clip.source).map(|s| file_name(&s.path));6880 let label = sources.get(clip.source).map(|s| file_name(&s.path));
6870 let wave = sources6881 let wave = sources
6871 .get(clip.source)6882 .get(clip.source)
@@ -6897,8 +6908,8 @@ impl Player {
6897 // off the left edge: the bed clips what hangs out6908 // off the left edge: the bed clips what hangs out
6898 // of it, so a half-visible clip is drawn as the6909 // of it, so a half-visible clip is drawn as the
6899 // half of itself that is on screen.6910 // half of itself that is on screen.
6900- .left(relative(view.frac_at(start, duration)))6911+ .left(px(scale.px_at(start)))
6901- .w(relative(width))6912+ .w(px(width))
6902 .overflow_hidden()6913 .overflow_hidden()
6903 .rounded(px(3.))6914 .rounded(px(3.))
6904 .border_1()6915 .border_1()
@@ -7041,7 +7052,7 @@ impl Player {
7041 .child(format!("{}", clip.speed)),7052 .child(format!("{}", clip.speed)),
7042 )7053 )
7043 })7054 })
7044- .when_some(label.filter(|_| show_label(bed_w * width)), |d, label| {7055+ .when_some(label.filter(|_| show_label(width)), |d, label| {
7045 d.child(7056 d.child(
7046 div()7057 div()
7047 .relative()7058 .relative()
@@ -7067,12 +7078,8 @@ impl Player {
7067 .absolute()7078 .absolute()
7068 .top_0()7079 .top_0()
7069 .h_full()7080 .h_full()
7070- .left(relative(7081+ .left(px(scale.px_at(f64::from(at) / self.fps)))
7071- view.frac_at(f64::from(at) / self.fps, duration),7082+ .w(px(scale.width_px(f64::from(len) / self.fps)))
7072- ))
7073- .w(relative(
7074- view.width_frac(f64::from(len) / self.fps, duration),
7075- ))
7076 .bg(rgba(0x4a9effaa))7083 .bg(rgba(0x4a9effaa))
7077 }),7084 }),
7078 )7085 )
@@ -7083,7 +7090,7 @@ impl Player {
7083 .absolute()7090 .absolute()
7084 .top_0()7091 .top_0()
7085 .h_full()7092 .h_full()
7086- .left(relative(filled))7093+ .left(px(filled))
7087 .w(px(1.))7094 .w(px(1.))
7088 .bg(rgb(ACCENT)),7095 .bg(rgb(ACCENT)),
7089 ),7096 ),
@@ -8138,10 +8145,6 @@ fn panel_h(lanes: usize) -> f32 {
8138 PANEL_H + lanes_h(lanes.clamp(2, LANES_MAX)) - lanes_h(2)8145 PANEL_H + lanes_h(lanes.clamp(2, LANES_MAX)) - lanes_h(2)
8139 }8146 }
8140 8147
8141-fn width_frac(len: f64, total: f64) -> f32 {
8142- if total > 0. { (len / total) as f32 } else { 1. }
8143-}
8144-
8145 /// One press of a zoom key, or one notch of ctrl+wheel.8148 /// One press of a zoom key, or one notch of ctrl+wheel.
8146 const ZOOM_STEP: f32 = 1.25;8149 const ZOOM_STEP: f32 = 1.25;
8147 8150
@@ -8150,118 +8153,222 @@ const ZOOM_STEP: f32 = 1.25;
8150 /// not an edit surface.8153 /// not an edit surface.
8151 const ZOOM_MIN_FRAMES: f64 = 8.;8154 const ZOOM_MIN_FRAMES: f64 = 8.;
8152 8155
8153-/// Which slice of the timeline the ruler and the lanes are showing: `zoom` is8156+/// How much timeline the bed may be widened out to hold. The far stop, and the
8154-/// how many times the whole of it the bed is now worth, and `start` the moment8157+/// counterpart of [`ZOOM_MIN_FRAMES`]: both are measured against the bed, never
8155-/// at the bed's left edge. The one place frames become pixels -- every box, the8158+/// against what is on the timeline, so neither moves when a clip is added.
8156-/// playhead, a seek and a trim all go through it, so none of them can drift8159+const ZOOM_MAX_SECONDS: f64 = 4. * 3600.;
8157-/// away from the others when the view moves.8160+
8161+/// How wide a second of timeline is drawn before anyone zooms: a five second
8162+/// import is 200 px of a bed several times that, so a short clip reads as
8163+/// short -- the thing a bed scaled to the content's own length cannot say.
8164+/// [`View::fit`] is the one way back to "the whole timeline across the bed".
8165+const PPS_DEFAULT: f64 = 40.;
8166+
8167+/// What the zoom button says it is showing: how much timeline fits on the bed,
8168+/// in the coarsest unit that still tells two zooms apart.
8169+fn span_label(span: f64) -> String {
8170+ match span {
8171+ s if !s.is_finite() || s <= 0. => "—".to_string(),
8172+ s if s >= 600. => format!("{:.0}m", s / 60.),
8173+ s if s >= 60. => format!("{:.1}m", s / 60.),
8174+ s if s >= 10. => format!("{s:.0}s"),
8175+ s => format!("{s:.1}s"),
8176+ }
8177+}
8178+
8179+/// The mapping the whole panel is drawn and clicked through: `pps` pixels to a
8180+/// second of timeline, `start` the moment at the bed's left edge. Absolute --
8181+/// how wide a clip is drawn depends on how long the clip is and on nothing
8182+/// else, so adding a second clip does not redraw the first one narrower and
8183+/// zooming out always makes every box smaller.
8158 ///8184 ///
8159-/// [`View::default`] is the whole timeline across the bed, which is what the8185+/// The one place seconds become pixels: every box, the playhead, a seek and a
8160-/// panel drew before there was a zoom at all: at zoom 1 `start` is pinned to 08186+/// trim all go through it, so none of them can drift away from the others when
8161-/// and every fraction here is the one [`width_frac`] and `start_frac` gave.8187+/// the view moves. What clamps it -- the stops, the scroll, the fit -- needs the
8188+/// bed it is drawn on and lives on [`View`].
8162 #[derive(Clone, Copy, Debug, PartialEq)]8189 #[derive(Clone, Copy, Debug, PartialEq)]
8163-struct View {8190+struct Scale {
8164- zoom: f32,8191+ pps: f64,
8165 start: f64,8192 start: f64,
8166 }8193 }
8167 8194
8168-impl Default for View {8195+impl Default for Scale {
8169 fn default() -> Self {8196 fn default() -> Self {
8170- View {8197+ Scale {
8171- zoom: 1.,8198+ pps: PPS_DEFAULT,
8172 start: 0.,8199 start: 0.,
8173 }8200 }
8174 }8201 }
8175 }8202 }
8176 8203
8177-impl View {8204+impl Scale {
8178- /// How much of the timeline is on the bed, in seconds.8205+ /// Where a moment sits on the bed, in pixels from its left edge. Negative
8179- fn span(self, duration: f64) -> f64 {8206+ /// for a moment scrolled off to the left, which is exactly the offset a
8180- duration / f64::from(self.zoom)8207+ /// half-visible clip is drawn at.
8208+ fn px_at(self, at: f64) -> f32 {
8209+ ((at - self.start) * self.pps) as f32
8210+ }
8211+
8212+ /// How wide a stretch of `len` seconds is drawn. Wider than the bed once
8213+ /// zoomed in, which is the point of zooming in; never negative, which gpui
8214+ /// has no meaning for.
8215+ fn width_px(self, len: f64) -> f32 {
8216+ (len * self.pps).max(0.) as f32
8217+ }
8218+
8219+ /// The moment `x` pixels along the bed is pointing at: the inverse of
8220+ /// [`Scale::px_at`], and what every seek and every trim reads. Clamped at
8221+ /// the head of the timeline only -- there is bed past the last frame now,
8222+ /// and a tail dragged into it is a longer clip, not an error.
8223+ fn time_at(self, x: f32) -> f64 {
8224+ if self.pps > 0. {
8225+ (self.start + f64::from(x) / self.pps).max(0.)
8226+ } else {
8227+ self.start
8228+ }
8181 }8229 }
8182 8230
8183- /// The tightest this timeline may be zoomed: [`ZOOM_MIN_FRAMES`] across the8231+ /// [`SNAP_PX`] in timeline frames at the scale the bed is drawn at: a snap
8184- /// bed. Never under 1 -- a timeline shorter than that is already at its8232+ /// is a distance on screen, so zoomed right in it is worth less than a frame
8185- /// floor, and a zoom that cannot fit the whole of it is not a zoom.8233+ /// (no snap at all, which is what a hand placing single frames wants) and
8186- fn max_zoom(duration: f64, fps: f64) -> f32 {8234+ /// zoomed out it is worth many.
8187- let frames = duration * fps;8235+ fn snap_frames(self, fps: f64) -> u32 {
8188- if frames > ZOOM_MIN_FRAMES {8236+ if self.pps > 0. {
8189- (frames / ZOOM_MIN_FRAMES) as f328237+ (SNAP_PX / self.pps * fps) as u32
8190 } else {8238 } else {
8191- 1.8239+ 0
8192 }8240 }
8193 }8241 }
8242+}
8194 8243
8195- /// Where a moment sits along the bed, 0 at its left edge and 1 at its8244+/// A [`Scale`] against the bed it is drawn on and the timeline it is drawn
8196- /// right. Outside that range for a moment scrolled off the view, which is8245+/// from. The bed's width is what turns a scale into "how much is on screen",
8197- /// exactly the negative offset a half-visible clip is drawn at.8246+/// and that is all the stops, the scroll clamp and the fit are made of.
8198- fn frac_at(self, at: f64, duration: f64) -> f32 {8247+///
8199- let span = self.span(duration);8248+/// Built per use out of [`Player::view`] and thrown away again -- the state is
8200- if span > 0. {8249+/// the `Scale`, and this is what a bed of `bed` px showing `duration` seconds
8201- ((at - self.start) / span) as f328250+/// at `fps` may do to it. So no call site can measure a moment against a bed or
8251+/// a duration that another one did not.
8252+#[derive(Clone, Copy, Debug, PartialEq)]
8253+struct View {
8254+ scale: Scale,
8255+ bed: f32,
8256+ duration: f64,
8257+ fps: f64,
8258+}
8259+
8260+impl View {
8261+ /// How much of the timeline is on the bed, in seconds.
8262+ fn span(self) -> f64 {
8263+ if self.scale.pps > 0. {
8264+ f64::from(self.bed) / self.scale.pps
8202 } else {8265 } else {
8203 0.8266 0.
8204 }8267 }
8205 }8268 }
8206 8269
8207- /// How wide a stretch of `len` seconds is on the bed. More than the whole8270+ /// The two stops, in pixels per second: [`ZOOM_MIN_FRAMES`] across the bed
8208- /// bed once zoomed in, which is the point of zooming in.8271+ /// is as tight as it goes, [`ZOOM_MAX_SECONDS`] across it as wide. `None`
8209- fn width_frac(self, len: f64, duration: f64) -> f32 {8272+ /// on a bed that was never painted -- there is nothing to measure them
8210- width_frac(len, self.span(duration))8273+ /// against yet, and a stop guessed off a zero width would throw away a zoom
8274+ /// the user asked for.
8275+ fn stops(self) -> Option<(f64, f64)> {
8276+ (self.bed > 0.).then(|| {
8277+ let bed = f64::from(self.bed);
8278+ let min = bed / ZOOM_MAX_SECONDS;
8279+ (min, (bed * self.fps / ZOOM_MIN_FRAMES).max(min))
8280+ })
8211 }8281 }
8212 8282
8213- /// The moment a pointer at `frac` along the bed is pointing at: the inverse8283+ /// Clamped to the bed it draws on: between the two stops, and never
8214- /// of [`View::frac_at`], and what every seek and every trim reads.8284+ /// scrolled past either end of the timeline. Unlike the fractional view
8215- fn time_at(self, frac: f32, duration: f64) -> f64 {8285+ /// this replaced, the *floor* is not the content -- a five second timeline
8216- (self.start + f64::from(frac) * self.span(duration)).clamp(0., duration)8286+ /// zooms out as far as an hour long one does.
8287+ fn settled(self) -> Scale {
8288+ let pps = match self.scale.pps.is_finite() && self.scale.pps > 0. {
8289+ true => self.scale.pps,
8290+ false => PPS_DEFAULT,
8291+ };
8292+ let start = match self.scale.start.is_finite() {
8293+ true => self.scale.start,
8294+ false => 0.,
8295+ };
8296+ let Some((min, max)) = self.stops() else {
8297+ return Scale {
8298+ pps,
8299+ start: start.max(0.),
8300+ };
8301+ };
8302+ let pps = pps.clamp(min, max);
8303+ let span = f64::from(self.bed) / pps;
8304+ Scale {
8305+ pps,
8306+ start: start.clamp(0., (self.duration - span).max(0.)),
8307+ }
8217 }8308 }
8218 8309
8219- /// Zoomed by `factor` about `anchor` (0..1 along the bed): whatever moment8310+ /// Zoomed by `factor` about `anchor` (pixels along the bed): whatever moment
8220 /// was under that point stays under it, so a zoom magnifies what was being8311 /// was under that point stays under it, so a zoom magnifies what was being
8221 /// looked at rather than throwing it off the edge.8312 /// looked at rather than throwing it off the edge.
8222- fn zoomed(self, factor: f32, anchor: f32, duration: f64, fps: f64) -> Self {8313+ fn zoomed(self, factor: f32, anchor: f32) -> Scale {
8223- let at = self.start + f64::from(anchor) * self.span(duration);8314+ let at = self.scale.time_at(anchor);
8224 // Clamped *before* the offset is worked out, not after: a press that8315 // Clamped *before* the offset is worked out, not after: a press that
8225 // runs into either stop must still leave the anchor where it is, and a8316 // runs into either stop must still leave the anchor where it is, and a
8226- // start measured against a zoom the stop then took away would slide it.8317+ // start measured against a scale the stop then took away would slide it.
8227- let zoom = (self.zoom * factor).clamp(1., Self::max_zoom(duration, fps));8318+ let raw = self.scale.pps * f64::from(factor);
8228- let start = at - f64::from(anchor) * View { zoom, start: 0. }.span(duration);8319+ let pps = match self.stops() {
8229- View { zoom, start }.settled(duration, fps)8320+ Some((min, max)) => raw.clamp(min, max),
8230- }8321+ None => raw,
8231-
8232- /// Clamped to the timeline it draws: never wider than the whole of it (the
8233- /// zoom-out floor is the fit), never tighter than [`View::max_zoom`], and
8234- /// never scrolled past either end.
8235- fn settled(self, duration: f64, fps: f64) -> Self {
8236- let zoom = if self.zoom.is_finite() {
8237- self.zoom.clamp(1., Self::max_zoom(duration, fps))
8238- } else {
8239- 1.
8240 };8322 };
8241- let span = View { zoom, start: 0. }.span(duration);8323+ View {
8242- let start = if self.start.is_finite() {8324+ scale: Scale {
8243- self.start.clamp(0., (duration - span).max(0.))8325+ pps,
8244- } else {8326+ start: at - f64::from(anchor) / pps,
8245- 0.8327+ },
8328+ ..self
8329+ }
8330+ .settled()
8331+ }
8332+
8333+ /// The whole timeline across the bed. The one place the content's own
8334+ /// length sets the scale, and the only one -- everywhere else a second is a
8335+ /// second -- because this is a user pressing a key that asks for exactly
8336+ /// that.
8337+ fn fit(self) -> Scale {
8338+ let pps = match self.duration > 0. && self.bed > 0. {
8339+ true => f64::from(self.bed) / self.duration,
8340+ false => PPS_DEFAULT,
8246 };8341 };
8247- View { zoom, start }8342+ View {
8343+ scale: Scale { pps, start: 0. },
8344+ ..self
8345+ }
8346+ .settled()
8248 }8347 }
8249 8348
8250- /// The view a playhead at `at` needs: the same one while it is on the bed,8349+ /// The scale a playhead at `at` needs: the same one while it is on the bed,
8251 /// and one centred on it once it has run off -- which is how a zoomed-in8350 /// and one centred on it once it has run off -- which is how a zoomed-in
8252- /// timeline scrolls, during playback and after a seek alike. At the fit8351+ /// timeline scrolls, during playback and after a seek alike. With the whole
8253- /// this can never fire, so an unzoomed panel is untouched by it.8352+ /// timeline on the bed this can never fire, so a panel showing all of it is
8254- fn following(self, at: f64, duration: f64, fps: f64) -> Self {8353+ /// untouched by it.
8255- let view = self.settled(duration, fps);8354+ fn following(self, at: f64) -> Scale {
8256- let span = view.span(duration);8355+ // Nothing is drawn yet, so nothing has run off anything.
8257- if at < view.start || at > view.start + span {8356+ if self.bed <= 0. {
8357+ return self.scale;
8358+ }
8359+ let scale = self.settled();
8360+ let span = f64::from(self.bed) / scale.pps;
8361+ if at < scale.start || at > scale.start + span {
8258 View {8362 View {
8259- start: at - span / 2.,8363+ scale: Scale {
8260- ..view8364+ start: at - span / 2.,
8365+ ..scale
8366+ },
8367+ ..self
8261 }8368 }
8262- .settled(duration, fps)8369+ .settled()
8263 } else {8370 } else {
8264- view8371+ scale
8265 }8372 }
8266 }8373 }
8267 }8374 }
@@ -8594,6 +8701,15 @@ fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 {
8594 ((x - bounds.left()) / bounds.size.width).clamp(0., 1.)8701 ((x - bounds.left()) / bounds.size.width).clamp(0., 1.)
8595 }8702 }
8596 8703
8704+/// Where along an element a click landed, in pixels from its own left edge:
8705+/// [`frac_along`] in the units the timeline is drawn in, since a [`Scale`]
8706+/// measures in pixels and not in shares of a bed. Clamped to the element -- a
8707+/// drag that slid off the end names its end -- and an element that was never
8708+/// painted reads as its start.
8709+fn px_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 {
8710+ f32::from(x - bounds.left()).clamp(0., f32::from(bounds.size.width).max(0.))
8711+}
8712+
8597 /// The frame a dropped clip's head lands on: `raw`, unless one of `marks` is8713 /// The frame a dropped clip's head lands on: `raw`, unless one of `marks` is
8598 /// within `tol` frames of where its head -- or its tail, `len` frames along --8714 /// within `tol` frames of where its head -- or its tail, `len` frames along --
8599 /// would come to rest, in which case that edge wins. The snap every timeline8715 /// would come to rest, in which case that edge wins. The snap every timeline
@@ -8959,11 +9075,11 @@ mod tests {
8959 keymap, lanes_h, marked, menu_at, next_container, normalise, nothing_to_play, panel_h,9075 keymap, lanes_h, marked, menu_at, next_container, normalise, nothing_to_play, panel_h,
8960 project_path, push_digit, retarget, scrub_due, show_label, silence_rate, snapped,9076 project_path, push_digit, retarget, scrub_due, show_label, silence_rate, snapped,
8961 source_tint, span_partner, speed_at, summary_head, summary_tail, timecode, transport,9077 source_tint, span_partner, speed_at, summary_head, summary_tail, timecode, transport,
8962- unseen_paths, unseen_sources, whole_take, width_frac, window_title,9078+ unseen_paths, unseen_sources, whole_take, window_title,
8963 };9079 };
8964 use super::{9080 use super::{
8965- LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows,9081+ LaneKind, PPS_DEFAULT, Repeat, Scale, View, ZOOM_MAX_SECONDS, ZOOM_MIN_FRAMES, ZOOM_STEP,
8966- repeats, unscannable,9082+ file_name, file_uri, library_rows, px_along, repeats, span_label, unscannable,
8967 };9083 };
8968 9084
8969 /// What the file manager is handed: the parts a path keeps as they are, and9085 /// What the file manager is handed: the parts a path keeps as they are, and
@@ -10020,10 +10136,10 @@ mod tests {
10020 assert_eq!(frac_down(px(50.), Bounds::default()), 0.5);10136 assert_eq!(frac_down(px(50.), Bounds::default()), 0.5);
10021 }10137 }
10022 10138
10023- /// What a drop reads: the frame under the pointer, through the same view10139+ /// What a drop reads: the frame under the pointer, through the same scale
10024 /// the boxes are drawn through. Zoomed in, the same pixel is a different10140 /// the boxes are drawn through. Zoomed in, the same pixel is a different
10025 /// frame -- which is the whole reason `Player::frame_under` goes through10141 /// frame -- which is the whole reason `Player::frame_under` goes through
10026- /// [`View`] rather than through the duration alone.10142+ /// [`Scale`] rather than through the duration alone.
10027 #[test]10143 #[test]
10028 fn a_drop_reads_the_frame_under_the_pointer_at_every_zoom() {10144 fn a_drop_reads_the_frame_under_the_pointer_at_every_zoom() {
10029 // A 200 px bed inset by the panel's padding, 10 seconds at 30 fps.10145 // A 200 px bed inset by the panel's padding, 10 seconds at 30 fps.
@@ -10031,29 +10147,32 @@ mod tests {
10031 origin: point(px(12.), px(400.)),10147 origin: point(px(12.), px(400.)),
10032 size: size(px(200.), px(6.)),10148 size: size(px(200.), px(6.)),
10033 };10149 };
10034- let (duration, fps) = (10., 30.);10150+ let fps = 30.;
10035 // The frame a pointer at window `x` names, exactly as `frame_under`10151 // The frame a pointer at window `x` names, exactly as `frame_under`
10036 // composes it.10152 // composes it.
10037- let under =10153+ let under = |scale: Scale, x: f32| frame_at(scale.time_at(px_along(px(x), bed)), fps);
10038- |view: View, x: f32| frame_at(view.time_at(frac_along(px(x), bed), duration), fps);
10039 10154
10040- // The whole timeline across the bed: halfway along is frame 150.10155+ // The whole 10 s timeline across the 200 px bed: 20 px to the second,
10041- let fit = View::default();10156+ // so halfway along is frame 150.
10157+ let fit = Scale {
10158+ pps: 20.,
10159+ start: 0.,
10160+ };
10042 assert_eq!(under(fit, 12.), 0);10161 assert_eq!(under(fit, 12.), 0);
10043 assert_eq!(under(fit, 112.), 150);10162 assert_eq!(under(fit, 112.), 150);
10044 assert_eq!(under(fit, 212.), 300);10163 assert_eq!(under(fit, 212.), 300);
10045 10164
10046 // Four times in, starting at second 5: the same middle pixel is now the10165 // Four times in, starting at second 5: the same middle pixel is now the
10047 // frame in the middle of seconds 5..7.5, and the left edge is not 0.10166 // frame in the middle of seconds 5..7.5, and the left edge is not 0.
10048- let zoomed = View {10167+ let zoomed = Scale {
10049- zoom: 4.,10168+ pps: 80.,
10050 start: 5.,10169 start: 5.,
10051 };10170 };
10052 assert_eq!(under(zoomed, 12.), 150);10171 assert_eq!(under(zoomed, 12.), 150);
10053 assert_eq!(under(zoomed, 112.), 187);10172 assert_eq!(under(zoomed, 112.), 187);
10054 assert_eq!(under(zoomed, 212.), 225);10173 assert_eq!(under(zoomed, 212.), 225);
10055- // ...and a drop past either end of the bed lands on the timeline, never10174+ // ...and a pointer that slid off either end of the bed names an end of
10056- // outside it: `time_at` clamps.10175+ // the bed, never a pixel outside it.
10057 assert_eq!(under(zoomed, 0.), 150);10176 assert_eq!(under(zoomed, 0.), 150);
10058 assert_eq!(under(fit, 9999.), 300);10177 assert_eq!(under(fit, 9999.), 300);
10059 }10178 }
@@ -10218,50 +10337,63 @@ mod tests {
10218 }10337 }
10219 }10338 }
10220 10339
10340+ /// The bed the view tests are drawn on: 200 px at 30 fps, as the drop test
10341+ /// above uses.
10342+ const TEST_BED: f32 = 200.;
10343+
10344+ /// A scale against that bed and a timeline `duration` seconds long.
10345+ fn test_view(scale: Scale, duration: f64) -> View {
10346+ View {
10347+ scale,
10348+ bed: TEST_BED,
10349+ duration,
10350+ fps: 30.,
10351+ }
10352+ }
10353+
10221 /// The mapping the whole panel is drawn and clicked through: a moment goes10354 /// The mapping the whole panel is drawn and clicked through: a moment goes
10222- /// to a fraction of the bed and comes back the same moment, at every zoom.10355+ /// to a pixel on the bed and comes back the same moment, at every zoom.
10223- /// The fit is the invariant that matters most -- it must be the arithmetic
10224- /// the panel did before there was a zoom at all.
10225 #[test]10356 #[test]
10226 fn a_moment_and_the_place_it_is_drawn_are_the_same_at_every_zoom() {10357 fn a_moment_and_the_place_it_is_drawn_are_the_same_at_every_zoom() {
10227 let duration = 20.;10358 let duration = 20.;
10228- // The fit: what `start_frac`/`width_frac` used to answer, exactly.10359+ // The fit, which is the only scale the content's own length picks: 20 s
10229- let fit = View::default();10360+ // across a 200 px bed is 10 px to the second, and a 5 s clip is a
10361+ // quarter of the bed -- the answer the fractional view gave for it.
10362+ let fit = test_view(Scale::default(), duration).fit();
10363+ assert_eq!(fit.pps, 10.);
10364+ assert_eq!(fit.width_px(5.), TEST_BED * 0.25);
10230 for t in [0., 5., 12.5, 20.] {10365 for t in [0., 5., 12.5, 20.] {
10231- assert_eq!(10366+ assert_eq!(fit.px_at(t), (t / duration) as f32 * TEST_BED, "fit at {t}");
10232- fit.frac_at(t, duration),
10233- (t / duration) as f32,
10234- "fit at {t}"
10235- );
10236 }10367 }
10237- assert_eq!(fit.width_frac(5., duration), width_frac(5., duration));10368+ for scale in [
10238- for view in [
10239 fit,10369 fit,
10240- View {10370+ Scale {
10241- zoom: 2.,10371+ pps: 20.,
10242 start: 4.,10372 start: 4.,
10243 },10373 },
10244- View {10374+ Scale {
10245- zoom: 8.,10375+ pps: 80.,
10246 start: 12.5,10376 start: 12.5,
10247 },10377 },
10248- View {10378+ Scale {
10249- zoom: 37.5,10379+ pps: 375.,
10250 start: 19.,10380 start: 19.,
10251 },10381 },
10252 ] {10382 ] {
10253- let view = view.settled(duration, 30.);10383+ let scale = test_view(scale, duration).settled();
10254- for frac in [0., 0.25, 0.5, 1.] {10384+ for x in [0., 50., 100., TEST_BED] {
10255- let at = view.time_at(frac, duration);10385+ let at = scale.time_at(x);
10256 assert!(10386 assert!(
10257- (f64::from(view.frac_at(at, duration)) - f64::from(frac)).abs() < 1e-6,10387+ (f64::from(scale.px_at(at)) - f64::from(x)).abs() < 1e-3,
10258- "{view:?} round trip at {frac}"10388+ "{scale:?} round trip at {x}"
10259 );10389 );
10260 }10390 }
10261- // A stretch is as wide as the share of the visible slice it takes.10391+ // A stretch as long as what is on the bed is as wide as the bed.
10262 assert!(10392 assert!(
10263- (f64::from(view.width_frac(view.span(duration), duration)) - 1.).abs() < 1e-6,10393+ (f64::from(scale.width_px(test_view(scale, duration).span())) - f64::from(TEST_BED))
10264- "{view:?} spans the bed"10394+ .abs()
10395+ < 1e-3,
10396+ "{scale:?} spans the bed"
10265 );10397 );
10266 }10398 }
10267 }10399 }
@@ -10271,70 +10403,98 @@ mod tests {
10271 /// ctrl+wheel.10403 /// ctrl+wheel.
10272 #[test]10404 #[test]
10273 fn a_zoom_leaves_the_anchor_where_it_was_on_screen() {10405 fn a_zoom_leaves_the_anchor_where_it_was_on_screen() {
10274- let (duration, fps) = (20., 30.);10406+ let duration = 20.;
10275- let mut view = View::default();10407+ let mut scale = test_view(Scale::default(), duration).settled();
10276- for anchor in [0.5f32, 0.25, 0.9] {10408+ // The same three points along the bed the fractional view held: a half,
10409+ // a quarter and nine tenths of 200 px.
10410+ for anchor in [100f32, 50., 180.] {
10277 // Well past the zoom-in stop: the anchor holds at the clamp too,10411 // Well past the zoom-in stop: the anchor holds at the clamp too,
10278 // which is where it used to slide (the clamp came after the offset).10412 // which is where it used to slide (the clamp came after the offset).
10279 for _ in 0..30 {10413 for _ in 0..30 {
10280- let at = view.time_at(anchor, duration);10414+ let at = scale.time_at(anchor);
10281- let zoomed = view.zoomed(ZOOM_STEP, anchor, duration, fps);10415+ let view = test_view(scale, duration);
10416+ let zoomed = view.zoomed(ZOOM_STEP, anchor);
10282 // Only where the anchor is not pinned by an edge of the10417 // Only where the anchor is not pinned by an edge of the
10283 // timeline: a view already against an end cannot slide further.10418 // timeline: a view already against an end cannot slide further.
10284- let pinned = zoomed.start <= 0. || zoomed.start + zoomed.span(duration) >= duration;10419+ let span = test_view(zoomed, duration).span();
10420+ let pinned = zoomed.start <= 0. || zoomed.start + span >= duration;
10285 if !pinned {10421 if !pinned {
10286 assert!(10422 assert!(
10287- (f64::from(zoomed.frac_at(at, duration)) - f64::from(anchor)).abs() < 1e-6,10423+ (f64::from(zoomed.px_at(at)) - f64::from(anchor)).abs() < 1e-3,
10288- "{at} moved: {view:?} -> {zoomed:?}"10424+ "{at} moved: {scale:?} -> {zoomed:?}"
10289 );10425 );
10290 }10426 }
10291- assert!(zoomed.zoom >= view.zoom, "and it did zoom in");10427+ assert!(zoomed.pps >= scale.pps, "and it did zoom in");
10292- view = zoomed;10428+ scale = zoomed;
10293 }10429 }
10294 }10430 }
10295 }10431 }
10296 10432
10297- /// Both stops: out is the whole timeline (which is where the panel starts,10433+ /// Both stops, and the point of the whole mapping: neither is the content's
10298- /// so nothing regressed for anyone who never zooms), in is a handful of10434+ /// own length any more. Out is [`ZOOM_MAX_SECONDS`] across the bed, in is
10299- /// frames -- and neither can scroll off an end.10435+ /// [`ZOOM_MIN_FRAMES`] across it -- so a short import can be zoomed out of
10436+ /// and a long one zoomed into, and neither can scroll off an end.
10300 #[test]10437 #[test]
10301- fn zoom_stops_at_the_whole_timeline_and_at_a_handful_of_frames() {10438+ fn zoom_stops_at_a_bedful_of_time_and_at_a_handful_of_frames() {
10302 let (duration, fps) = (20., 30.);10439 let (duration, fps) = (20., 30.);
10303- let mut view = View::default();10440+ let mut scale = Scale::default();
10304 for _ in 0..200 {10441 for _ in 0..200 {
10305- view = view.zoomed(1. / ZOOM_STEP, 0.5, duration, fps);10442+ scale = test_view(scale, duration).zoomed(1. / ZOOM_STEP, 100.);
10306 }10443 }
10307- assert_eq!(view, View::default(), "zoomed out is the fit");10444+ assert!(
10445+ (test_view(scale, duration).span() - ZOOM_MAX_SECONDS).abs() < 1e-6,
10446+ "widest is a bedful of hours, not the 20 s that happen to be on it"
10447+ );
10308 for _ in 0..200 {10448 for _ in 0..200 {
10309- view = view.zoomed(ZOOM_STEP, 0.5, duration, fps);10449+ scale = test_view(scale, duration).zoomed(ZOOM_STEP, 100.);
10310 }10450 }
10311 assert_eq!(10451 assert_eq!(
10312- (view.span(duration) * fps).round(),10452+ (test_view(scale, duration).span() * fps).round(),
10313 ZOOM_MIN_FRAMES,10453 ZOOM_MIN_FRAMES,
10314 "tightest is a handful of frames"10454 "tightest is a handful of frames"
10315 );10455 );
10316 // Against the far end, the slice still ends at the last frame.10456 // Against the far end, the slice still ends at the last frame.
10317- let end = View {10457+ let end = test_view(
10318- zoom: view.zoom,10458+ Scale {
10319- start: 1e6,10459+ start: 1e6,
10320- }10460+ ..scale
10321- .settled(duration, fps);10461+ },
10322- assert!((end.start + end.span(duration) - duration).abs() < 1e-9);10462+ duration,
10323- assert!(10463+ );
10324- View {10464+ let end = (end.settled(), end.span());
10325- zoom: view.zoom,10465+ assert!((end.0.start + end.1 - duration).abs() < 1e-9);
10326- start: -5.10466+ assert_eq!(
10327- }10467+ test_view(
10328- .settled(duration, fps)10468+ Scale {
10329- .start10469+ start: -5.,
10330- == 0.10470+ ..scale
10471+ },
10472+ duration
10473+ )
10474+ .settled()
10475+ .start,
10476+ 0.
10331 );10477 );
10332- // A timeline too short to zoom into, and one with no length at all,10478+ // The dead zone the fractional view had: a timeline of a handful of
10333- // both stay at the fit rather than dividing by nothing.10479+ // frames could not be zoomed at all, because its own length was the
10334- assert_eq!(View::max_zoom(0.1, fps), 1.);10480+ // floor *and* the ceiling. Both keys work on it now.
10335- assert_eq!(View::default().settled(0., fps), View::default());10481+ let tiny = test_view(Scale::default(), 0.1);
10336- assert_eq!(View::default().frac_at(3., 0.), 0.);10482+ assert!(tiny.zoomed(ZOOM_STEP, 100.).pps > PPS_DEFAULT);
10337- assert_eq!(View::default().time_at(0.5, 0.), 0.);10483+ assert!(tiny.zoomed(1. / ZOOM_STEP, 100.).pps < PPS_DEFAULT);
10484+ // A timeline with no length at all divides by nothing and scrolls
10485+ // nowhere; the fit of one keeps the scale it had.
10486+ let empty = test_view(Scale::default(), 0.);
10487+ assert_eq!(empty.settled(), Scale::default());
10488+ assert_eq!(empty.fit(), Scale::default());
10489+ assert_eq!(Scale::default().px_at(0.), 0.);
10490+ // A bed that was never painted clamps nothing -- there is nothing to
10491+ // clamp against, and a zoom must survive the frame before the probe.
10492+ let unpainted = View {
10493+ bed: 0.,
10494+ ..test_view(Scale { pps: 4e6, start: 3. }, duration)
10495+ };
10496+ assert_eq!(unpainted.settled().pps, 4e6);
10497+ assert_eq!(unpainted.following(19.), unpainted.scale);
10338 }10498 }
10339 10499
10340 /// What makes a zoomed timeline follow the playing head: off the bed at10500 /// What makes a zoomed timeline follow the playing head: off the bed at
@@ -10342,53 +10502,96 @@ mod tests {
10342 /// view that jumped every frame would be unreadable.10502 /// view that jumped every frame would be unreadable.
10343 #[test]10503 #[test]
10344 fn the_view_follows_a_playhead_that_runs_off_the_bed() {10504 fn the_view_follows_a_playhead_that_runs_off_the_bed() {
10345- let (duration, fps) = (20., 30.);10505+ let duration = 20.;
10346- let view = View {10506+ // 5 s on a 200 px bed: 40 px to the second, starting at second 5.
10347- zoom: 4.,10507+ let view = test_view(
10348- start: 5.,10508+ Scale {
10349- }10509+ pps: 40.,
10350- .settled(duration, fps);10510+ start: 5.,
10351- assert_eq!(view.span(duration), 5.);10511+ },
10512+ duration,
10513+ );
10514+ let scale = view.settled();
10515+ assert_eq!(view.span(), 5.);
10352 // Inside: untouched, whichever part of the slice it is in.10516 // Inside: untouched, whichever part of the slice it is in.
10353 for at in [5., 7.5, 10.] {10517 for at in [5., 7.5, 10.] {
10354- assert_eq!(10518+ assert_eq!(view.following(at), scale, "{at} is on the bed");
10355- view.following(at, duration, fps),
10356- view,
10357- "{at} is on the bed"
10358- );
10359 }10519 }
10360 // Past the right edge, as playback does it: the head comes back on the10520 // Past the right edge, as playback does it: the head comes back on the
10361- // bed, and the zoom is not changed by the scroll.10521+ // bed, and the scale is not changed by the scroll.
10362- let moved = view.following(12., duration, fps);10522+ let moved = view.following(12.);
10363- assert_eq!(moved.zoom, view.zoom);10523+ assert_eq!(moved.pps, scale.pps);
10364- assert!(moved.start > view.start, "scrolled forward");10524+ assert!(moved.start > scale.start, "scrolled forward");
10365 assert!(10525 assert!(
10366- moved.frac_at(12., duration) > 0. && moved.frac_at(12., duration) < 1.,10526+ moved.px_at(12.) > 0. && moved.px_at(12.) < TEST_BED,
10367 "and the playhead is on screen"10527 "and the playhead is on screen"
10368 );10528 );
10369 // A seek back behind the slice does the same the other way.10529 // A seek back behind the slice does the same the other way.
10370- let back = view.following(1., duration, fps);10530+ let back = view.following(1.);
10371- assert!(back.start < view.start);10531+ assert!(back.start < scale.start);
10372- assert!(back.frac_at(1., duration) >= 0.);10532+ assert!(back.px_at(1.) >= 0.);
10373- // At the fit there is nothing to follow: the whole timeline is on the10533+ // With the whole timeline on the bed there is nothing to follow.
10374- // bed already, so no playhead can move it.10534+ let whole = test_view(Scale::default(), duration);
10535+ let fit = whole.fit();
10375 for at in [0., 10., 20.] {10536 for at in [0., 10., 20.] {
10376 assert_eq!(10537 assert_eq!(
10377- View::default().following(at, duration, fps),10538+ test_view(fit, duration).following(at),
10378- View::default(),10539+ fit,
10379 "the fit never scrolls"10540 "the fit never scrolls"
10380 );10541 );
10381 }10542 }
10382 }10543 }
10383 10544
10545+ /// The bug this mapping exists for: the first import used to fill the whole
10546+ /// track whatever it was, because the bed *was* the timeline -- so a 5 s
10547+ /// clip was 100% of the lane, zooming out did nothing, and adding a second
10548+ /// clip silently halved the first one's box.
10549+ #[test]
10550+ fn a_clip_is_drawn_the_same_width_whatever_else_is_on_the_timeline() {
10551+ let bed = 900.;
10552+ let of = |duration: f64| View {
10553+ scale: Scale::default(),
10554+ bed,
10555+ duration,
10556+ fps: 30.,
10557+ };
10558+ // One 5 s import, then a second clip after it: 20 s of timeline where
10559+ // there were 5, and the first box has not moved or narrowed.
10560+ let (alone, joined) = (of(5.).settled(), of(20.).settled());
10561+ assert_eq!(alone, joined);
10562+ assert_eq!(alone.width_px(5.), joined.width_px(5.));
10563+ assert_eq!(joined.px_at(5.), alone.width_px(5.));
10564+ // And it does not fill the track: a short clip reads as short.
10565+ assert!(
10566+ alone.width_px(5.) < bed / 2.,
10567+ "5 s at {} px/s is {} px of a {bed} px bed",
10568+ alone.pps,
10569+ alone.width_px(5.)
10570+ );
10571+ // Zooming out visibly shrinks it, however short the timeline is -- the
10572+ // press that used to be a no-op.
10573+ let out = of(5.).zoomed(1. / ZOOM_STEP, 0.);
10574+ assert!(
10575+ out.width_px(5.) < alone.width_px(5.),
10576+ "{} px is not smaller than {} px",
10577+ out.width_px(5.),
10578+ alone.width_px(5.)
10579+ );
10580+ // The way back to "the whole timeline across the bed" is still one key.
10581+ assert_eq!(of(5.).fit().width_px(5.), bed);
10582+ }
10583+
10584+ /// What the zoom button says: how much timeline is on the bed, in a unit
10585+ /// that tells two zooms apart.
10384 #[test]10586 #[test]
10385- fn clip_boxes_split_the_lane_by_duration() {10587+ fn the_zoom_button_says_how_much_is_on_the_bed() {
10386- // 1 s + 3 s of a 4 s timeline: a quarter and three quarters.10588+ assert_eq!(span_label(4.5), "4.5s");
10387- assert_eq!(width_frac(1., 4.), 0.25);10589+ assert_eq!(span_label(22.5), "22s");
10388- assert_eq!(width_frac(3., 4.), 0.75);10590+ assert_eq!(span_label(90.), "1.5m");
10389- assert_eq!(width_frac(4., 4.), 1.);10591+ assert_eq!(span_label(3600.), "60m");
10390- // A timeline with no length must not hand gpui a NaN width.10592+ // Before the first paint there is no bed and so no answer to give.
10391- assert_eq!(width_frac(0., 0.), 1.);10593+ assert_eq!(span_label(0.), "—");
10594+ assert_eq!(span_label(f64::NAN), "—");
10392 }10595 }
10393 10596
10394 #[test]10597 #[test]
@@ -11525,10 +11728,11 @@ mod tests {
11525 assert!(show_label(LABEL_MIN_W));11728 assert!(show_label(LABEL_MIN_W));
11526 assert!(show_label(400.));11729 assert!(show_label(400.));
11527 assert!(!show_label(LABEL_MIN_W - 0.1));11730 assert!(!show_label(LABEL_MIN_W - 0.1));
11528- // A timeline with no length must not put a box outside its lane.11731+ // The label test is the box's own width in pixels now, which is what
11529- let fit = View::default();11732+ // the scale hands it -- no bed width, and so nothing to be zero.
11530- assert_eq!(fit.frac_at(3., 0.), 0.);11733+ let scale = Scale::default();
11531- assert_eq!(fit.frac_at(1., 4.), 0.25);11734+ assert!(show_label(scale.width_px(LABEL_MIN_W as f64 / PPS_DEFAULT)));
11735+ assert!(!show_label(scale.width_px(0.)));
11532 }11736 }
11533 11737
11534 #[test]11738 #[test]
@@ -11894,9 +12098,10 @@ fn main() {
11894 image: None,12098 image: None,
11895 held: None,12099 held: None,
11896 ruler: Rc::default(),12100 ruler: Rc::default(),
11897- // The whole timeline across the bed, which is where a12101+ // A second is [`PPS_DEFAULT`] pixels wide until someone
11898- // project opens: zooming is something the user asks for.12102+ // zooms or asks for the fit: a project opens at a scale, not
11899- view: View::default(),12103+ // at whatever its first import happens to be long.
12104+ scale: Scale::default(),
11900 selected: None,12105 selected: None,
11901 context_menu: None,12106 context_menu: None,
11902 library_menu: None,12107 library_menu: None,