A second of timeline is so many pixels, wherever it is
Browse files at this commit8baa7108e0af370530451a05b1aff6acf780a24cParent: dea8a27- refactor
- a second of timeline is so many pixels, wherever it is
1 changed file +467 -262
- crates/app/src/main.rs +467 -262
crates/app/src/main.rsmodified+467 -262
| @@ -636,10 +636,10 @@ struct Player { | |||
| 636 | /// The ruler's own box, recorded at prepaint: a mouse listener is handed | 636 | /// The ruler's own box, recorded at prepaint: a mouse listener is handed |
| 637 | /// the window position and nothing else. | 637 | /// the window position and nothing else. |
| 638 | ruler: Rc<Cell<Bounds<Pixels>>>, | 638 | ruler: Rc<Cell<Bounds<Pixels>>>, |
| 639 | - /// How much of the timeline the ruler and the lanes are showing. Held here | 639 | + /// How wide a second of timeline is drawn, and from which moment. Held here |
| 640 | /// and nowhere else: every frame-to-pixel answer in the panel comes out of | 640 | /// and nowhere else: every frame-to-pixel answer in the panel comes out of |
| 641 | /// it, so the boxes, the playhead and the pointer cannot disagree. | 641 | /// it, so the boxes, the playhead and the pointer cannot disagree. |
| 642 | - view: View, | 642 | + scale: Scale, |
| 643 | /// Which clip the edit keys act on: the lane it is in and its index there. | 643 | /// Which clip the edit keys act on: the lane it is in and its index there. |
| 644 | /// The *clicked* half, not the group -- a group is what gets marked on | 644 | /// The *clicked* half, not the group -- a group is what gets marked on |
| 645 | /// screen, but Lift has to know which half it was aimed at. Indices move | 645 | /// screen, but Lift has to know which half it was aimed at. Indices move |
| @@ -1129,23 +1129,37 @@ impl Player { | |||
| 1129 | cx.notify(); | 1129 | cx.notify(); |
| 1130 | } | 1130 | } |
| 1131 | 1131 | ||
| 1132 | - /// Magnifies the timeline about a point that stays put: `anchor` is where | 1132 | + /// The scale against the bed it is drawn on and the timeline it is drawn |
| 1133 | - /// along the bed to hold still (a ctrl+wheel holds the pointer), and with | 1133 | + /// from: every clamp, zoom and scroll is worked out through this, and the |
| 1134 | - /// none it is the playhead -- so the frame being worked on is still the | 1134 | + /// bed is measured off the ruler's probe rather than remembered, so a |
| 1135 | + /// resized window is a resized view on the very next answer. | ||
| 1136 | + fn view(&self) -> View { | ||
| 1137 | + View { | ||
| 1138 | + scale: self.scale, | ||
| 1139 | + bed: f32::from(self.ruler.get().size.width), | ||
| 1140 | + duration: self.drawn_duration(), | ||
| 1141 | + fps: self.fps, | ||
| 1142 | + } | ||
| 1143 | + } | ||
| 1144 | + | ||
| 1145 | + /// Magnifies the timeline about a point that stays put: `anchor` is how many | ||
| 1146 | + /// pixels along the bed to hold still (a ctrl+wheel holds the pointer), and | ||
| 1147 | + /// with none it is the playhead -- so the frame being worked on is still the | ||
| 1135 | /// frame on screen after the zoom. Clamped at both ends by [`View`]: out | 1148 | /// frame on screen after the zoom. Clamped at both ends by [`View`]: out |
| 1136 | - /// stops at the whole timeline, in at a handful of frames. | 1149 | + /// stops at [`ZOOM_MAX_SECONDS`] on the bed, in at a handful of frames. |
| 1137 | fn zoom(&mut self, factor: f32, anchor: Option<f32>, cx: &mut Context<Self>) { | 1150 | fn zoom(&mut self, factor: f32, anchor: Option<f32>, cx: &mut Context<Self>) { |
| 1138 | - let duration = self.drawn_duration(); | 1151 | + let view = self.view(); |
| 1139 | - let at = self.playhead(duration); | 1152 | + let at = self.playhead(view.duration); |
| 1140 | - let anchor = anchor.unwrap_or_else(|| self.view.frac_at(at, duration).clamp(0., 1.)); | 1153 | + let anchor = anchor.unwrap_or_else(|| self.scale.px_at(at).clamp(0., view.bed)); |
| 1141 | - self.view = self.view.zoomed(factor, anchor, duration, self.fps); | 1154 | + self.scale = view.zoomed(factor, anchor); |
| 1142 | cx.notify(); | 1155 | cx.notify(); |
| 1143 | } | 1156 | } |
| 1144 | 1157 | ||
| 1145 | - /// All the way back out: the whole timeline across the bed, which is what | 1158 | + /// All the way back out: the whole timeline across the bed, and the one |
| 1146 | - /// the panel shows before anyone has zoomed at all. | 1159 | + /// thing that reads the timeline's own length to decide how wide a second |
| 1160 | + /// is drawn. | ||
| 1147 | fn zoom_fit(&mut self, cx: &mut Context<Self>) { | 1161 | fn zoom_fit(&mut self, cx: &mut Context<Self>) { |
| 1148 | - self.view = View::default(); | 1162 | + self.scale = self.view().fit(); |
| 1149 | cx.notify(); | 1163 | cx.notify(); |
| 1150 | } | 1164 | } |
| 1151 | 1165 | ||
| @@ -2346,14 +2360,12 @@ impl Player { | |||
| 2346 | /// there is no such clip to move. The engine has the last word on where it | 2360 | /// there is no such clip to move. The engine has the last word on where it |
| 2347 | /// may actually go -- this is the ask, not the answer. | 2361 | /// may actually go -- this is the ask, not the answer. |
| 2348 | /// | 2362 | /// |
| 2349 | - /// ponytail: the bed ends where the timeline does, so a clip cannot be | 2363 | + /// ponytail: the bed now runs past the last frame whenever the timeline is |
| 2350 | - /// dragged *past* the last frame -- the pointer has no pixel out there to | 2364 | + /// shorter than the view ([`Scale::time_at`] clamps at the head only), so a |
| 2351 | - /// name and [`View::time_at`] clamps to the duration. Room to the right is | 2365 | + /// clip *can* be dragged out there. Zoomed in against the far end it cannot: |
| 2352 | - /// made by pulling a tail out first ([`Player::drawn_duration`] already | 2366 | + /// the scroll clamp pins the bed's right edge to the duration, and the |
| 2353 | - /// lengthens the bed for exactly that gesture). The upgrade is to draw the | 2367 | + /// pointer has no pixel past it. The upgrade is to let the scroll clamp |
| 2354 | - /// bed past the end while a clip drag is in flight, the way a trim already | 2368 | + /// leave a screen of empty bed after the end, the way every NLE does. |
| 2355 | - /// does: `drawn_duration` would take the dragged clip's length the same way | ||
| 2356 | - /// it takes a trim's reach. | ||
| 2357 | fn drop_frame(&self, from: Lane, idx: usize, to: Lane, x: Pixels) -> Option<u32> { | 2369 | fn drop_frame(&self, from: Lane, idx: usize, to: Lane, x: Pixels) -> Option<u32> { |
| 2358 | let session = self.session.as_ref()?; | 2370 | let session = self.session.as_ref()?; |
| 2359 | let clip = session.lane_clips(from).get(idx).copied()?; | 2371 | let clip = session.lane_clips(from).get(idx).copied()?; |
| @@ -2376,16 +2388,11 @@ impl Player { | |||
| 2376 | Some(snapped(raw, clip.frames(), self.snap_frames(), &marks)) | 2388 | Some(snapped(raw, clip.frames(), self.snap_frames(), &marks)) |
| 2377 | } | 2389 | } |
| 2378 | 2390 | ||
| 2379 | - /// [`SNAP_PX`] in timeline frames at the zoom the bed is drawn at: a snap is | 2391 | + /// [`SNAP_PX`] in timeline frames at the scale the bed is drawn at: the bed's |
| 2380 | - /// a distance on screen, so zoomed right in it is worth less than a frame | 2392 | + /// own width drops out of it, since a pixel is now worth the same stretch of |
| 2381 | - /// (no snap at all, which is what a hand placing single frames wants) and | 2393 | + /// timeline wherever the view sits. |
| 2382 | - /// zoomed out to the whole timeline it is worth many. | ||
| 2383 | fn snap_frames(&self) -> u32 { | 2394 | fn snap_frames(&self) -> u32 { |
| 2384 | - let bed = f64::from(f32::from(self.ruler.get().size.width)); | 2395 | + self.scale.snap_frames(self.fps) |
| 2385 | - if bed <= 0. { | ||
| 2386 | - return 0; | ||
| 2387 | - } | ||
| 2388 | - (SNAP_PX / bed * self.view.span(self.drawn_duration()) * self.fps) as u32 | ||
| 2389 | } | 2396 | } |
| 2390 | 2397 | ||
| 2391 | /// Opens the clip menu on the box under the pointer, from the right button | 2398 | /// Opens the clip menu on the box under the pointer, from the right button |
| @@ -2454,14 +2461,13 @@ impl Player { | |||
| 2454 | } | 2461 | } |
| 2455 | 2462 | ||
| 2456 | /// The timeline frame a pointer at window x is on: along the same bed the | 2463 | /// The timeline frame a pointer at window x is on: along the same bed the |
| 2457 | - /// ruler is measured on, through the same [`View`] every box is drawn | 2464 | + /// ruler is measured on, through the same [`Scale`] every box is drawn |
| 2458 | /// through, so a zoomed-in panel answers with the frame under the pointer | 2465 | /// through, so a zoomed-in panel answers with the frame under the pointer |
| 2459 | /// and not with the one that would have been there unzoomed. The one | 2466 | /// and not with the one that would have been there unzoomed. The one |
| 2460 | /// question a trim, a grab and a drop all ask. | 2467 | /// question a trim, a grab and a drop all ask. |
| 2461 | fn frame_under(&self, x: Pixels) -> u32 { | 2468 | fn frame_under(&self, x: Pixels) -> u32 { |
| 2462 | - let duration = self.drawn_duration(); | ||
| 2463 | frame_at( | 2469 | frame_at( |
| 2464 | - self.view.time_at(frac_along(x, self.ruler.get()), duration), | 2470 | + self.scale.time_at(px_along(x, self.ruler.get())), |
| 2465 | self.fps, | 2471 | self.fps, |
| 2466 | ) | 2472 | ) |
| 2467 | } | 2473 | } |
| @@ -3121,9 +3127,12 @@ impl Player { | |||
| 3121 | let Some(session) = &self.session else { | 3127 | let Some(session) = &self.session else { |
| 3122 | return; | 3128 | return; |
| 3123 | }; | 3129 | }; |
| 3130 | + // Clamped to the timeline here rather than in the mapping: there is bed | ||
| 3131 | + // past the last frame now, and a seek out there is a seek to the end. | ||
| 3124 | let t = self | 3132 | let t = self |
| 3125 | - .view | 3133 | + .scale |
| 3126 | - .time_at(frac_along(x, self.ruler.get()), session.timeline_duration()); | 3134 | + .time_at(px_along(x, self.ruler.get())) |
| 3135 | + .clamp(0., session.timeline_duration()); | ||
| 3127 | let target = (t * self.fps) as u32; | 3136 | let target = (t * self.fps) as u32; |
| 3128 | if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) { | 3137 | if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) { |
| 3129 | self.last_target = target; | 3138 | self.last_target = target; |
| @@ -3431,7 +3440,7 @@ impl Render for Player { | |||
| 3431 | // edit that shortens the timeline moves the far end of the view, and a | 3440 | // edit that shortens the timeline moves the far end of the view, and a |
| 3432 | // playhead that has run off the bed pulls the view after it -- which is | 3441 | // playhead that has run off the bed pulls the view after it -- which is |
| 3433 | // what makes a zoomed-in timeline scroll while it plays. | 3442 | // what makes a zoomed-in timeline scroll while it plays. |
| 3434 | - self.view = self.view.following(position, duration, self.fps); | 3443 | + self.scale = self.view().following(position); |
| 3435 | 3444 | ||
| 3436 | div() | 3445 | div() |
| 3437 | .track_focus(&self.focus) | 3446 | .track_focus(&self.focus) |
| @@ -4207,11 +4216,12 @@ impl Player { | |||
| 4207 | playing: bool, | 4216 | playing: bool, |
| 4208 | cx: &mut Context<Self>, | 4217 | cx: &mut Context<Self>, |
| 4209 | ) -> impl IntoElement { | 4218 | ) -> impl IntoElement { |
| 4210 | - // Where the playhead is *on the bed*, which at the fit is where along | 4219 | + // Where the playhead is *on the bed*, in pixels from its left edge. |
| 4211 | - // the timeline it is and zoomed in is where along the visible slice. | 4220 | + // Clamped to the bed because it is drawn as a width as well as an |
| 4212 | - // Clamped because it is drawn as a width as well as an offset, and the | 4221 | + // offset, and the view follows the playhead anyway, so it is never off |
| 4213 | - // view follows the playhead anyway, so it is never off the bed. | 4222 | + // the bed for long. |
| 4214 | - let filled = self.view.frac_at(position, duration).clamp(0., 1.); | 4223 | + let bed_w = f32::from(self.ruler.get().size.width); |
| 4224 | + let filled = self.scale.px_at(position).clamp(0., bed_w); | ||
| 4215 | // An export owns the hint slot and the ruler while it runs: the | 4225 | // An export owns the hint slot and the ruler while it runs: the |
| 4216 | // percentage and the accent bar are the same number, so the playhead | 4226 | // percentage and the accent bar are the same number, so the playhead |
| 4217 | // fill doubles as the progress bar for free. | 4227 | // fill doubles as the progress bar for free. |
| @@ -4234,7 +4244,7 @@ impl Player { | |||
| 4234 | // closure would hold it for as long as the iterator lives. | 4244 | // closure would hold it for as long as the iterator lives. |
| 4235 | let mut rows = Vec::new(); | 4245 | let mut rows = Vec::new(); |
| 4236 | for &lane in &lanes { | 4246 | for &lane in &lanes { |
| 4237 | - rows.push(self.lane_row(lane, duration, filled, cx)); | 4247 | + rows.push(self.lane_row(lane, filled, cx)); |
| 4238 | } | 4248 | } |
| 4239 | let (hint, filled) = if let Some(export) = self.exporting() { | 4249 | let (hint, filled) = if let Some(export) = self.exporting() { |
| 4240 | let progress = export.progress(); | 4250 | let progress = export.progress(); |
| @@ -4451,13 +4461,17 @@ impl Player { | |||
| 4451 | .child(control( | 4461 | .child(control( |
| 4452 | "zoom-fit", | 4462 | "zoom-fit", |
| 4453 | None, | 4463 | None, |
| 4454 | - format!("{:.1}x", self.view.zoom), | 4464 | + // How much timeline is on the bed, not a multiple of |
| 4465 | + // "the whole of it": the scale no longer knows what the | ||
| 4466 | + // whole of it is, and a number that changed on every | ||
| 4467 | + // import was a number that lied. | ||
| 4468 | + span_label(self.view().span()), | ||
| 4455 | format!( | 4469 | format!( |
| 4456 | "{} — fit the whole timeline; showing {} to {}", | 4470 | "{} — fit the whole timeline; showing {} to {}", |
| 4457 | key(ActionId::ZoomFit), | 4471 | key(ActionId::ZoomFit), |
| 4458 | - timecode(self.view.start, self.fps), | 4472 | + timecode(self.scale.start, self.fps), |
| 4459 | timecode( | 4473 | timecode( |
| 4460 | - (self.view.start + self.view.span(duration)).min(duration), | 4474 | + (self.scale.start + self.view().span()).min(duration), |
| 4461 | self.fps | 4475 | self.fps |
| 4462 | ) | 4476 | ) |
| 4463 | ), | 4477 | ), |
| @@ -4608,7 +4622,7 @@ impl Player { | |||
| 4608 | if dy == 0. { | 4622 | if dy == 0. { |
| 4609 | return; | 4623 | return; |
| 4610 | } | 4624 | } |
| 4611 | - let anchor = frac_along(event.position.x, this.ruler.get()); | 4625 | + let anchor = px_along(event.position.x, this.ruler.get()); |
| 4612 | let factor = if dy > 0. { ZOOM_STEP } else { 1. / ZOOM_STEP }; | 4626 | let factor = if dy > 0. { ZOOM_STEP } else { 1. / ZOOM_STEP }; |
| 4613 | this.zoom(factor, Some(anchor), cx); | 4627 | this.zoom(factor, Some(anchor), cx); |
| 4614 | }, | 4628 | }, |
| @@ -4630,7 +4644,7 @@ impl Player { | |||
| 4630 | .child( | 4644 | .child( |
| 4631 | div() | 4645 | div() |
| 4632 | .h_full() | 4646 | .h_full() |
| 4633 | - .w(relative(filled)) | 4647 | + .w(px(filled)) |
| 4634 | .rounded(px(3.)) | 4648 | .rounded(px(3.)) |
| 4635 | .bg(rgb(ACCENT)), | 4649 | .bg(rgb(ACCENT)), |
| 4636 | ), | 4650 | ), |
| @@ -6585,21 +6599,18 @@ impl Player { | |||
| 6585 | fn lane_row( | 6599 | fn lane_row( |
| 6586 | &self, | 6600 | &self, |
| 6587 | lane: Lane, | 6601 | lane: Lane, |
| 6588 | - duration: f64, | 6602 | + // Where the playhead is on the bed, in pixels: worked out once by the |
| 6603 | + // panel so the ruler's line and every lane's draw the same one. | ||
| 6589 | filled: f32, | 6604 | filled: f32, |
| 6590 | cx: &mut Context<Self>, | 6605 | cx: &mut Context<Self>, |
| 6591 | // Borrows nothing it was given (`use<>`): the rows are built one after | 6606 | // Borrows nothing it was given (`use<>`): the rows are built one after |
| 6592 | // another into a list, and a row still holding `cx` would be the only | 6607 | // another into a list, and a row still holding `cx` would be the only |
| 6593 | // one that could be built. | 6608 | // one that could be built. |
| 6594 | ) -> impl IntoElement + use<> { | 6609 | ) -> impl IntoElement + use<> { |
| 6595 | - // The bed's own width, measured last repaint off the ruler's bar: the | 6610 | + // The mapping, copied out once: every box in the row is placed through |
| 6596 | - // two are laid out identically (same header offset, same `flex_1`), so | 6611 | + // it, so all of them move together when it does. No bed width is needed |
| 6597 | - // one probe answers for both. Zero before the first paint, which only | 6612 | + // to place them any more -- a second is so many pixels wherever it is. |
| 6598 | - // costs the labels one frame. | 6613 | + let scale = self.scale; |
| 6599 | - let bed_w = f32::from(self.ruler.get().size.width); | ||
| 6600 | - // The slice being shown, copied out once: every box in the row is | ||
| 6601 | - // placed through it, so all of them move together when it does. | ||
| 6602 | - let view = self.view; | ||
| 6603 | let clips = self | 6614 | let clips = self |
| 6604 | .session | 6615 | .session |
| 6605 | .as_ref() | 6616 | .as_ref() |
| @@ -6783,7 +6794,7 @@ impl Player { | |||
| 6783 | .and_then(|s| sources.iter().position(|o| o.path == s.path)) | 6794 | .and_then(|s| sources.iter().position(|o| o.path == s.path)) |
| 6784 | .unwrap_or(clip.source), | 6795 | .unwrap_or(clip.source), |
| 6785 | ); | 6796 | ); |
| 6786 | - let width = view.width_frac(len, duration); | 6797 | + let width = scale.width_px(len); |
| 6787 | let label = sources.get(clip.source).map(|s| file_name(&s.path)); | 6798 | let label = sources.get(clip.source).map(|s| file_name(&s.path)); |
| 6788 | let wave = sources | 6799 | let wave = sources |
| 6789 | .get(clip.source) | 6800 | .get(clip.source) |
| @@ -6815,8 +6826,8 @@ impl Player { | |||
| 6815 | // off the left edge: the bed clips what hangs out | 6826 | // off the left edge: the bed clips what hangs out |
| 6816 | // of it, so a half-visible clip is drawn as the | 6827 | // of it, so a half-visible clip is drawn as the |
| 6817 | // half of itself that is on screen. | 6828 | // half of itself that is on screen. |
| 6818 | - .left(relative(view.frac_at(start, duration))) | 6829 | + .left(px(scale.px_at(start))) |
| 6819 | - .w(relative(width)) | 6830 | + .w(px(width)) |
| 6820 | .overflow_hidden() | 6831 | .overflow_hidden() |
| 6821 | .rounded(px(3.)) | 6832 | .rounded(px(3.)) |
| 6822 | .border_1() | 6833 | .border_1() |
| @@ -6945,7 +6956,7 @@ impl Player { | |||
| 6945 | .child(format!("{}", clip.speed)), | 6956 | .child(format!("{}", clip.speed)), |
| 6946 | ) | 6957 | ) |
| 6947 | }) | 6958 | }) |
| 6948 | - .when_some(label.filter(|_| show_label(bed_w * width)), |d, label| { | 6959 | + .when_some(label.filter(|_| show_label(width)), |d, label| { |
| 6949 | d.child( | 6960 | d.child( |
| 6950 | div() | 6961 | div() |
| 6951 | .relative() | 6962 | .relative() |
| @@ -6971,12 +6982,8 @@ impl Player { | |||
| 6971 | .absolute() | 6982 | .absolute() |
| 6972 | .top_0() | 6983 | .top_0() |
| 6973 | .h_full() | 6984 | .h_full() |
| 6974 | - .left(relative( | 6985 | + .left(px(scale.px_at(f64::from(at) / self.fps))) |
| 6975 | - view.frac_at(f64::from(at) / self.fps, duration), | 6986 | + .w(px(scale.width_px(f64::from(len) / self.fps))) |
| 6976 | - )) | ||
| 6977 | - .w(relative( | ||
| 6978 | - view.width_frac(f64::from(len) / self.fps, duration), | ||
| 6979 | - )) | ||
| 6980 | .bg(rgba(0x4a9effaa)) | 6987 | .bg(rgba(0x4a9effaa)) |
| 6981 | }), | 6988 | }), |
| 6982 | ) | 6989 | ) |
| @@ -6987,7 +6994,7 @@ impl Player { | |||
| 6987 | .absolute() | 6994 | .absolute() |
| 6988 | .top_0() | 6995 | .top_0() |
| 6989 | .h_full() | 6996 | .h_full() |
| 6990 | - .left(relative(filled)) | 6997 | + .left(px(filled)) |
| 6991 | .w(px(1.)) | 6998 | .w(px(1.)) |
| 6992 | .bg(rgb(ACCENT)), | 6999 | .bg(rgb(ACCENT)), |
| 6993 | ), | 7000 | ), |
| @@ -8042,10 +8049,6 @@ fn panel_h(lanes: usize) -> f32 { | |||
| 8042 | PANEL_H + lanes_h(lanes.clamp(2, LANES_MAX)) - lanes_h(2) | 8049 | PANEL_H + lanes_h(lanes.clamp(2, LANES_MAX)) - lanes_h(2) |
| 8043 | } | 8050 | } |
| 8044 | 8051 | ||
| 8045 | -fn width_frac(len: f64, total: f64) -> f32 { | ||
| 8046 | - if total > 0. { (len / total) as f32 } else { 1. } | ||
| 8047 | -} | ||
| 8048 | - | ||
| 8049 | /// One press of a zoom key, or one notch of ctrl+wheel. | 8052 | /// One press of a zoom key, or one notch of ctrl+wheel. |
| 8050 | const ZOOM_STEP: f32 = 1.25; | 8053 | const ZOOM_STEP: f32 = 1.25; |
| 8051 | 8054 | ||
| @@ -8054,118 +8057,222 @@ const ZOOM_STEP: f32 = 1.25; | |||
| 8054 | /// not an edit surface. | 8057 | /// not an edit surface. |
| 8055 | const ZOOM_MIN_FRAMES: f64 = 8.; | 8058 | const ZOOM_MIN_FRAMES: f64 = 8.; |
| 8056 | 8059 | ||
| 8057 | -/// Which slice of the timeline the ruler and the lanes are showing: `zoom` is | 8060 | +/// How much timeline the bed may be widened out to hold. The far stop, and the |
| 8058 | -/// how many times the whole of it the bed is now worth, and `start` the moment | 8061 | +/// counterpart of [`ZOOM_MIN_FRAMES`]: both are measured against the bed, never |
| 8059 | -/// at the bed's left edge. The one place frames become pixels -- every box, the | 8062 | +/// against what is on the timeline, so neither moves when a clip is added. |
| 8060 | -/// playhead, a seek and a trim all go through it, so none of them can drift | 8063 | +const ZOOM_MAX_SECONDS: f64 = 4. * 3600.; |
| 8061 | -/// away from the others when the view moves. | 8064 | + |
| 8065 | +/// How wide a second of timeline is drawn before anyone zooms: a five second | ||
| 8066 | +/// import is 200 px of a bed several times that, so a short clip reads as | ||
| 8067 | +/// short -- the thing a bed scaled to the content's own length cannot say. | ||
| 8068 | +/// [`View::fit`] is the one way back to "the whole timeline across the bed". | ||
| 8069 | +const PPS_DEFAULT: f64 = 40.; | ||
| 8070 | + | ||
| 8071 | +/// What the zoom button says it is showing: how much timeline fits on the bed, | ||
| 8072 | +/// in the coarsest unit that still tells two zooms apart. | ||
| 8073 | +fn span_label(span: f64) -> String { | ||
| 8074 | + match span { | ||
| 8075 | + s if !s.is_finite() || s <= 0. => "—".to_string(), | ||
| 8076 | + s if s >= 600. => format!("{:.0}m", s / 60.), | ||
| 8077 | + s if s >= 60. => format!("{:.1}m", s / 60.), | ||
| 8078 | + s if s >= 10. => format!("{s:.0}s"), | ||
| 8079 | + s => format!("{s:.1}s"), | ||
| 8080 | + } | ||
| 8081 | +} | ||
| 8082 | + | ||
| 8083 | +/// The mapping the whole panel is drawn and clicked through: `pps` pixels to a | ||
| 8084 | +/// second of timeline, `start` the moment at the bed's left edge. Absolute -- | ||
| 8085 | +/// how wide a clip is drawn depends on how long the clip is and on nothing | ||
| 8086 | +/// else, so adding a second clip does not redraw the first one narrower and | ||
| 8087 | +/// zooming out always makes every box smaller. | ||
| 8062 | /// | 8088 | /// |
| 8063 | -/// [`View::default`] is the whole timeline across the bed, which is what the | 8089 | +/// The one place seconds become pixels: every box, the playhead, a seek and a |
| 8064 | -/// panel drew before there was a zoom at all: at zoom 1 `start` is pinned to 0 | 8090 | +/// trim all go through it, so none of them can drift away from the others when |
| 8065 | -/// and every fraction here is the one [`width_frac`] and `start_frac` gave. | 8091 | +/// the view moves. What clamps it -- the stops, the scroll, the fit -- needs the |
| 8092 | +/// bed it is drawn on and lives on [`View`]. | ||
| 8066 | #[derive(Clone, Copy, Debug, PartialEq)] | 8093 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 8067 | -struct View { | 8094 | +struct Scale { |
| 8068 | - zoom: f32, | 8095 | + pps: f64, |
| 8069 | start: f64, | 8096 | start: f64, |
| 8070 | } | 8097 | } |
| 8071 | 8098 | ||
| 8072 | -impl Default for View { | 8099 | +impl Default for Scale { |
| 8073 | fn default() -> Self { | 8100 | fn default() -> Self { |
| 8074 | - View { | 8101 | + Scale { |
| 8075 | - zoom: 1., | 8102 | + pps: PPS_DEFAULT, |
| 8076 | start: 0., | 8103 | start: 0., |
| 8077 | } | 8104 | } |
| 8078 | } | 8105 | } |
| 8079 | } | 8106 | } |
| 8080 | 8107 | ||
| 8081 | -impl View { | 8108 | +impl Scale { |
| 8082 | - /// How much of the timeline is on the bed, in seconds. | 8109 | + /// Where a moment sits on the bed, in pixels from its left edge. Negative |
| 8083 | - fn span(self, duration: f64) -> f64 { | 8110 | + /// for a moment scrolled off to the left, which is exactly the offset a |
| 8084 | - duration / f64::from(self.zoom) | 8111 | + /// half-visible clip is drawn at. |
| 8112 | + fn px_at(self, at: f64) -> f32 { | ||
| 8113 | + ((at - self.start) * self.pps) as f32 | ||
| 8114 | + } | ||
| 8115 | + | ||
| 8116 | + /// How wide a stretch of `len` seconds is drawn. Wider than the bed once | ||
| 8117 | + /// zoomed in, which is the point of zooming in; never negative, which gpui | ||
| 8118 | + /// has no meaning for. | ||
| 8119 | + fn width_px(self, len: f64) -> f32 { | ||
| 8120 | + (len * self.pps).max(0.) as f32 | ||
| 8121 | + } | ||
| 8122 | + | ||
| 8123 | + /// The moment `x` pixels along the bed is pointing at: the inverse of | ||
| 8124 | + /// [`Scale::px_at`], and what every seek and every trim reads. Clamped at | ||
| 8125 | + /// the head of the timeline only -- there is bed past the last frame now, | ||
| 8126 | + /// and a tail dragged into it is a longer clip, not an error. | ||
| 8127 | + fn time_at(self, x: f32) -> f64 { | ||
| 8128 | + if self.pps > 0. { | ||
| 8129 | + (self.start + f64::from(x) / self.pps).max(0.) | ||
| 8130 | + } else { | ||
| 8131 | + self.start | ||
| 8132 | + } | ||
| 8085 | } | 8133 | } |
| 8086 | 8134 | ||
| 8087 | - /// The tightest this timeline may be zoomed: [`ZOOM_MIN_FRAMES`] across the | 8135 | + /// [`SNAP_PX`] in timeline frames at the scale the bed is drawn at: a snap |
| 8088 | - /// bed. Never under 1 -- a timeline shorter than that is already at its | 8136 | + /// is a distance on screen, so zoomed right in it is worth less than a frame |
| 8089 | - /// floor, and a zoom that cannot fit the whole of it is not a zoom. | 8137 | + /// (no snap at all, which is what a hand placing single frames wants) and |
| 8090 | - fn max_zoom(duration: f64, fps: f64) -> f32 { | 8138 | + /// zoomed out it is worth many. |
| 8091 | - let frames = duration * fps; | 8139 | + fn snap_frames(self, fps: f64) -> u32 { |
| 8092 | - if frames > ZOOM_MIN_FRAMES { | 8140 | + if self.pps > 0. { |
| 8093 | - (frames / ZOOM_MIN_FRAMES) as f32 | 8141 | + (SNAP_PX / self.pps * fps) as u32 |
| 8094 | } else { | 8142 | } else { |
| 8095 | - 1. | 8143 | + 0 |
| 8096 | } | 8144 | } |
| 8097 | } | 8145 | } |
| 8146 | +} | ||
| 8098 | 8147 | ||
| 8099 | - /// Where a moment sits along the bed, 0 at its left edge and 1 at its | 8148 | +/// A [`Scale`] against the bed it is drawn on and the timeline it is drawn |
| 8100 | - /// right. Outside that range for a moment scrolled off the view, which is | 8149 | +/// from. The bed's width is what turns a scale into "how much is on screen", |
| 8101 | - /// exactly the negative offset a half-visible clip is drawn at. | 8150 | +/// and that is all the stops, the scroll clamp and the fit are made of. |
| 8102 | - fn frac_at(self, at: f64, duration: f64) -> f32 { | 8151 | +/// |
| 8103 | - let span = self.span(duration); | 8152 | +/// Built per use out of [`Player::view`] and thrown away again -- the state is |
| 8104 | - if span > 0. { | 8153 | +/// the `Scale`, and this is what a bed of `bed` px showing `duration` seconds |
| 8105 | - ((at - self.start) / span) as f32 | 8154 | +/// at `fps` may do to it. So no call site can measure a moment against a bed or |
| 8155 | +/// a duration that another one did not. | ||
| 8156 | +#[derive(Clone, Copy, Debug, PartialEq)] | ||
| 8157 | +struct View { | ||
| 8158 | + scale: Scale, | ||
| 8159 | + bed: f32, | ||
| 8160 | + duration: f64, | ||
| 8161 | + fps: f64, | ||
| 8162 | +} | ||
| 8163 | + | ||
| 8164 | +impl View { | ||
| 8165 | + /// How much of the timeline is on the bed, in seconds. | ||
| 8166 | + fn span(self) -> f64 { | ||
| 8167 | + if self.scale.pps > 0. { | ||
| 8168 | + f64::from(self.bed) / self.scale.pps | ||
| 8106 | } else { | 8169 | } else { |
| 8107 | 0. | 8170 | 0. |
| 8108 | } | 8171 | } |
| 8109 | } | 8172 | } |
| 8110 | 8173 | ||
| 8111 | - /// How wide a stretch of `len` seconds is on the bed. More than the whole | 8174 | + /// The two stops, in pixels per second: [`ZOOM_MIN_FRAMES`] across the bed |
| 8112 | - /// bed once zoomed in, which is the point of zooming in. | 8175 | + /// is as tight as it goes, [`ZOOM_MAX_SECONDS`] across it as wide. `None` |
| 8113 | - fn width_frac(self, len: f64, duration: f64) -> f32 { | 8176 | + /// on a bed that was never painted -- there is nothing to measure them |
| 8114 | - width_frac(len, self.span(duration)) | 8177 | + /// against yet, and a stop guessed off a zero width would throw away a zoom |
| 8178 | + /// the user asked for. | ||
| 8179 | + fn stops(self) -> Option<(f64, f64)> { | ||
| 8180 | + (self.bed > 0.).then(|| { | ||
| 8181 | + let bed = f64::from(self.bed); | ||
| 8182 | + let min = bed / ZOOM_MAX_SECONDS; | ||
| 8183 | + (min, (bed * self.fps / ZOOM_MIN_FRAMES).max(min)) | ||
| 8184 | + }) | ||
| 8115 | } | 8185 | } |
| 8116 | 8186 | ||
| 8117 | - /// The moment a pointer at `frac` along the bed is pointing at: the inverse | 8187 | + /// Clamped to the bed it draws on: between the two stops, and never |
| 8118 | - /// of [`View::frac_at`], and what every seek and every trim reads. | 8188 | + /// scrolled past either end of the timeline. Unlike the fractional view |
| 8119 | - fn time_at(self, frac: f32, duration: f64) -> f64 { | 8189 | + /// this replaced, the *floor* is not the content -- a five second timeline |
| 8120 | - (self.start + f64::from(frac) * self.span(duration)).clamp(0., duration) | 8190 | + /// zooms out as far as an hour long one does. |
| 8191 | + fn settled(self) -> Scale { | ||
| 8192 | + let pps = match self.scale.pps.is_finite() && self.scale.pps > 0. { | ||
| 8193 | + true => self.scale.pps, | ||
| 8194 | + false => PPS_DEFAULT, | ||
| 8195 | + }; | ||
| 8196 | + let start = match self.scale.start.is_finite() { | ||
| 8197 | + true => self.scale.start, | ||
| 8198 | + false => 0., | ||
| 8199 | + }; | ||
| 8200 | + let Some((min, max)) = self.stops() else { | ||
| 8201 | + return Scale { | ||
| 8202 | + pps, | ||
| 8203 | + start: start.max(0.), | ||
| 8204 | + }; | ||
| 8205 | + }; | ||
| 8206 | + let pps = pps.clamp(min, max); | ||
| 8207 | + let span = f64::from(self.bed) / pps; | ||
| 8208 | + Scale { | ||
| 8209 | + pps, | ||
| 8210 | + start: start.clamp(0., (self.duration - span).max(0.)), | ||
| 8211 | + } | ||
| 8121 | } | 8212 | } |
| 8122 | 8213 | ||
| 8123 | - /// Zoomed by `factor` about `anchor` (0..1 along the bed): whatever moment | 8214 | + /// Zoomed by `factor` about `anchor` (pixels along the bed): whatever moment |
| 8124 | /// was under that point stays under it, so a zoom magnifies what was being | 8215 | /// was under that point stays under it, so a zoom magnifies what was being |
| 8125 | /// looked at rather than throwing it off the edge. | 8216 | /// looked at rather than throwing it off the edge. |
| 8126 | - fn zoomed(self, factor: f32, anchor: f32, duration: f64, fps: f64) -> Self { | 8217 | + fn zoomed(self, factor: f32, anchor: f32) -> Scale { |
| 8127 | - let at = self.start + f64::from(anchor) * self.span(duration); | 8218 | + let at = self.scale.time_at(anchor); |
| 8128 | // Clamped *before* the offset is worked out, not after: a press that | 8219 | // Clamped *before* the offset is worked out, not after: a press that |
| 8129 | // runs into either stop must still leave the anchor where it is, and a | 8220 | // runs into either stop must still leave the anchor where it is, and a |
| 8130 | - // start measured against a zoom the stop then took away would slide it. | 8221 | + // start measured against a scale the stop then took away would slide it. |
| 8131 | - let zoom = (self.zoom * factor).clamp(1., Self::max_zoom(duration, fps)); | 8222 | + let raw = self.scale.pps * f64::from(factor); |
| 8132 | - let start = at - f64::from(anchor) * View { zoom, start: 0. }.span(duration); | 8223 | + let pps = match self.stops() { |
| 8133 | - View { zoom, start }.settled(duration, fps) | 8224 | + Some((min, max)) => raw.clamp(min, max), |
| 8134 | - } | 8225 | + None => raw, |
| 8135 | - | ||
| 8136 | - /// Clamped to the timeline it draws: never wider than the whole of it (the | ||
| 8137 | - /// zoom-out floor is the fit), never tighter than [`View::max_zoom`], and | ||
| 8138 | - /// never scrolled past either end. | ||
| 8139 | - fn settled(self, duration: f64, fps: f64) -> Self { | ||
| 8140 | - let zoom = if self.zoom.is_finite() { | ||
| 8141 | - self.zoom.clamp(1., Self::max_zoom(duration, fps)) | ||
| 8142 | - } else { | ||
| 8143 | - 1. | ||
| 8144 | }; | 8226 | }; |
| 8145 | - let span = View { zoom, start: 0. }.span(duration); | 8227 | + View { |
| 8146 | - let start = if self.start.is_finite() { | 8228 | + scale: Scale { |
| 8147 | - self.start.clamp(0., (duration - span).max(0.)) | 8229 | + pps, |
| 8148 | - } else { | 8230 | + start: at - f64::from(anchor) / pps, |
| 8149 | - 0. | 8231 | + }, |
| 8232 | + ..self | ||
| 8233 | + } | ||
| 8234 | + .settled() | ||
| 8235 | + } | ||
| 8236 | + | ||
| 8237 | + /// The whole timeline across the bed. The one place the content's own | ||
| 8238 | + /// length sets the scale, and the only one -- everywhere else a second is a | ||
| 8239 | + /// second -- because this is a user pressing a key that asks for exactly | ||
| 8240 | + /// that. | ||
| 8241 | + fn fit(self) -> Scale { | ||
| 8242 | + let pps = match self.duration > 0. && self.bed > 0. { | ||
| 8243 | + true => f64::from(self.bed) / self.duration, | ||
| 8244 | + false => PPS_DEFAULT, | ||
| 8150 | }; | 8245 | }; |
| 8151 | - View { zoom, start } | 8246 | + View { |
| 8247 | + scale: Scale { pps, start: 0. }, | ||
| 8248 | + ..self | ||
| 8249 | + } | ||
| 8250 | + .settled() | ||
| 8152 | } | 8251 | } |
| 8153 | 8252 | ||
| 8154 | - /// The view a playhead at `at` needs: the same one while it is on the bed, | 8253 | + /// The scale a playhead at `at` needs: the same one while it is on the bed, |
| 8155 | /// and one centred on it once it has run off -- which is how a zoomed-in | 8254 | /// and one centred on it once it has run off -- which is how a zoomed-in |
| 8156 | - /// timeline scrolls, during playback and after a seek alike. At the fit | 8255 | + /// timeline scrolls, during playback and after a seek alike. With the whole |
| 8157 | - /// this can never fire, so an unzoomed panel is untouched by it. | 8256 | + /// timeline on the bed this can never fire, so a panel showing all of it is |
| 8158 | - fn following(self, at: f64, duration: f64, fps: f64) -> Self { | 8257 | + /// untouched by it. |
| 8159 | - let view = self.settled(duration, fps); | 8258 | + fn following(self, at: f64) -> Scale { |
| 8160 | - let span = view.span(duration); | 8259 | + // Nothing is drawn yet, so nothing has run off anything. |
| 8161 | - if at < view.start || at > view.start + span { | 8260 | + if self.bed <= 0. { |
| 8261 | + return self.scale; | ||
| 8262 | + } | ||
| 8263 | + let scale = self.settled(); | ||
| 8264 | + let span = f64::from(self.bed) / scale.pps; | ||
| 8265 | + if at < scale.start || at > scale.start + span { | ||
| 8162 | View { | 8266 | View { |
| 8163 | - start: at - span / 2., | 8267 | + scale: Scale { |
| 8164 | - ..view | 8268 | + start: at - span / 2., |
| 8269 | + ..scale | ||
| 8270 | + }, | ||
| 8271 | + ..self | ||
| 8165 | } | 8272 | } |
| 8166 | - .settled(duration, fps) | 8273 | + .settled() |
| 8167 | } else { | 8274 | } else { |
| 8168 | - view | 8275 | + scale |
| 8169 | } | 8276 | } |
| 8170 | } | 8277 | } |
| 8171 | } | 8278 | } |
| @@ -8498,6 +8605,15 @@ fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 { | |||
| 8498 | ((x - bounds.left()) / bounds.size.width).clamp(0., 1.) | 8605 | ((x - bounds.left()) / bounds.size.width).clamp(0., 1.) |
| 8499 | } | 8606 | } |
| 8500 | 8607 | ||
| 8608 | +/// Where along an element a click landed, in pixels from its own left edge: | ||
| 8609 | +/// [`frac_along`] in the units the timeline is drawn in, since a [`Scale`] | ||
| 8610 | +/// measures in pixels and not in shares of a bed. Clamped to the element -- a | ||
| 8611 | +/// drag that slid off the end names its end -- and an element that was never | ||
| 8612 | +/// painted reads as its start. | ||
| 8613 | +fn px_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 { | ||
| 8614 | + f32::from(x - bounds.left()).clamp(0., f32::from(bounds.size.width).max(0.)) | ||
| 8615 | +} | ||
| 8616 | + | ||
| 8501 | /// The frame a dropped clip's head lands on: `raw`, unless one of `marks` is | 8617 | /// The frame a dropped clip's head lands on: `raw`, unless one of `marks` is |
| 8502 | /// within `tol` frames of where its head -- or its tail, `len` frames along -- | 8618 | /// within `tol` frames of where its head -- or its tail, `len` frames along -- |
| 8503 | /// would come to rest, in which case that edge wins. The snap every timeline | 8619 | /// would come to rest, in which case that edge wins. The snap every timeline |
| @@ -8860,11 +8976,11 @@ mod tests { | |||
| 8860 | keymap, lanes_h, marked, menu_at, next_container, normalise, nothing_to_play, panel_h, | 8976 | keymap, lanes_h, marked, menu_at, next_container, normalise, nothing_to_play, panel_h, |
| 8861 | project_path, push_digit, retarget, scrub_due, show_label, silence_rate, snapped, | 8977 | project_path, push_digit, retarget, scrub_due, show_label, silence_rate, snapped, |
| 8862 | source_tint, span_partner, speed_at, summary_head, summary_tail, timecode, unseen_paths, | 8978 | source_tint, span_partner, speed_at, summary_head, summary_tail, timecode, unseen_paths, |
| 8863 | - unseen_sources, whole_take, width_frac, window_title, | 8979 | + unseen_sources, whole_take, window_title, |
| 8864 | }; | 8980 | }; |
| 8865 | use super::{ | 8981 | use super::{ |
| 8866 | - LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows, | 8982 | + LaneKind, PPS_DEFAULT, Repeat, Scale, View, ZOOM_MAX_SECONDS, ZOOM_MIN_FRAMES, ZOOM_STEP, |
| 8867 | - repeats, unscannable, | 8983 | + file_name, file_uri, library_rows, px_along, repeats, span_label, unscannable, |
| 8868 | }; | 8984 | }; |
| 8869 | 8985 | ||
| 8870 | /// What the file manager is handed: the parts a path keeps as they are, and | 8986 | /// What the file manager is handed: the parts a path keeps as they are, and |
| @@ -9921,10 +10037,10 @@ mod tests { | |||
| 9921 | assert_eq!(frac_down(px(50.), Bounds::default()), 0.5); | 10037 | assert_eq!(frac_down(px(50.), Bounds::default()), 0.5); |
| 9922 | } | 10038 | } |
| 9923 | 10039 | ||
| 9924 | - /// What a drop reads: the frame under the pointer, through the same view | 10040 | + /// What a drop reads: the frame under the pointer, through the same scale |
| 9925 | /// the boxes are drawn through. Zoomed in, the same pixel is a different | 10041 | /// the boxes are drawn through. Zoomed in, the same pixel is a different |
| 9926 | /// frame -- which is the whole reason `Player::frame_under` goes through | 10042 | /// frame -- which is the whole reason `Player::frame_under` goes through |
| 9927 | - /// [`View`] rather than through the duration alone. | 10043 | + /// [`Scale`] rather than through the duration alone. |
| 9928 | #[test] | 10044 | #[test] |
| 9929 | fn a_drop_reads_the_frame_under_the_pointer_at_every_zoom() { | 10045 | fn a_drop_reads_the_frame_under_the_pointer_at_every_zoom() { |
| 9930 | // A 200 px bed inset by the panel's padding, 10 seconds at 30 fps. | 10046 | // A 200 px bed inset by the panel's padding, 10 seconds at 30 fps. |
| @@ -9932,29 +10048,32 @@ mod tests { | |||
| 9932 | origin: point(px(12.), px(400.)), | 10048 | origin: point(px(12.), px(400.)), |
| 9933 | size: size(px(200.), px(6.)), | 10049 | size: size(px(200.), px(6.)), |
| 9934 | }; | 10050 | }; |
| 9935 | - let (duration, fps) = (10., 30.); | 10051 | + let fps = 30.; |
| 9936 | // The frame a pointer at window `x` names, exactly as `frame_under` | 10052 | // The frame a pointer at window `x` names, exactly as `frame_under` |
| 9937 | // composes it. | 10053 | // composes it. |
| 9938 | - let under = | 10054 | + let under = |scale: Scale, x: f32| frame_at(scale.time_at(px_along(px(x), bed)), fps); |
| 9939 | - |view: View, x: f32| frame_at(view.time_at(frac_along(px(x), bed), duration), fps); | ||
| 9940 | 10055 | ||
| 9941 | - // The whole timeline across the bed: halfway along is frame 150. | 10056 | + // The whole 10 s timeline across the 200 px bed: 20 px to the second, |
| 9942 | - let fit = View::default(); | 10057 | + // so halfway along is frame 150. |
| 10058 | + let fit = Scale { | ||
| 10059 | + pps: 20., | ||
| 10060 | + start: 0., | ||
| 10061 | + }; | ||
| 9943 | assert_eq!(under(fit, 12.), 0); | 10062 | assert_eq!(under(fit, 12.), 0); |
| 9944 | assert_eq!(under(fit, 112.), 150); | 10063 | assert_eq!(under(fit, 112.), 150); |
| 9945 | assert_eq!(under(fit, 212.), 300); | 10064 | assert_eq!(under(fit, 212.), 300); |
| 9946 | 10065 | ||
| 9947 | // Four times in, starting at second 5: the same middle pixel is now the | 10066 | // Four times in, starting at second 5: the same middle pixel is now the |
| 9948 | // frame in the middle of seconds 5..7.5, and the left edge is not 0. | 10067 | // frame in the middle of seconds 5..7.5, and the left edge is not 0. |
| 9949 | - let zoomed = View { | 10068 | + let zoomed = Scale { |
| 9950 | - zoom: 4., | 10069 | + pps: 80., |
| 9951 | start: 5., | 10070 | start: 5., |
| 9952 | }; | 10071 | }; |
| 9953 | assert_eq!(under(zoomed, 12.), 150); | 10072 | assert_eq!(under(zoomed, 12.), 150); |
| 9954 | assert_eq!(under(zoomed, 112.), 187); | 10073 | assert_eq!(under(zoomed, 112.), 187); |
| 9955 | assert_eq!(under(zoomed, 212.), 225); | 10074 | assert_eq!(under(zoomed, 212.), 225); |
| 9956 | - // ...and a drop past either end of the bed lands on the timeline, never | 10075 | + // ...and a pointer that slid off either end of the bed names an end of |
| 9957 | - // outside it: `time_at` clamps. | 10076 | + // the bed, never a pixel outside it. |
| 9958 | assert_eq!(under(zoomed, 0.), 150); | 10077 | assert_eq!(under(zoomed, 0.), 150); |
| 9959 | assert_eq!(under(fit, 9999.), 300); | 10078 | assert_eq!(under(fit, 9999.), 300); |
| 9960 | } | 10079 | } |
| @@ -10119,50 +10238,63 @@ mod tests { | |||
| 10119 | } | 10238 | } |
| 10120 | } | 10239 | } |
| 10121 | 10240 | ||
| 10241 | + /// The bed the view tests are drawn on: 200 px at 30 fps, as the drop test | ||
| 10242 | + /// above uses. | ||
| 10243 | + const TEST_BED: f32 = 200.; | ||
| 10244 | + | ||
| 10245 | + /// A scale against that bed and a timeline `duration` seconds long. | ||
| 10246 | + fn test_view(scale: Scale, duration: f64) -> View { | ||
| 10247 | + View { | ||
| 10248 | + scale, | ||
| 10249 | + bed: TEST_BED, | ||
| 10250 | + duration, | ||
| 10251 | + fps: 30., | ||
| 10252 | + } | ||
| 10253 | + } | ||
| 10254 | + | ||
| 10122 | /// The mapping the whole panel is drawn and clicked through: a moment goes | 10255 | /// The mapping the whole panel is drawn and clicked through: a moment goes |
| 10123 | - /// to a fraction of the bed and comes back the same moment, at every zoom. | 10256 | + /// to a pixel on the bed and comes back the same moment, at every zoom. |
| 10124 | - /// The fit is the invariant that matters most -- it must be the arithmetic | ||
| 10125 | - /// the panel did before there was a zoom at all. | ||
| 10126 | #[test] | 10257 | #[test] |
| 10127 | fn a_moment_and_the_place_it_is_drawn_are_the_same_at_every_zoom() { | 10258 | fn a_moment_and_the_place_it_is_drawn_are_the_same_at_every_zoom() { |
| 10128 | let duration = 20.; | 10259 | let duration = 20.; |
| 10129 | - // The fit: what `start_frac`/`width_frac` used to answer, exactly. | 10260 | + // The fit, which is the only scale the content's own length picks: 20 s |
| 10130 | - let fit = View::default(); | 10261 | + // across a 200 px bed is 10 px to the second, and a 5 s clip is a |
| 10262 | + // quarter of the bed -- the answer the fractional view gave for it. | ||
| 10263 | + let fit = test_view(Scale::default(), duration).fit(); | ||
| 10264 | + assert_eq!(fit.pps, 10.); | ||
| 10265 | + assert_eq!(fit.width_px(5.), TEST_BED * 0.25); | ||
| 10131 | for t in [0., 5., 12.5, 20.] { | 10266 | for t in [0., 5., 12.5, 20.] { |
| 10132 | - assert_eq!( | 10267 | + assert_eq!(fit.px_at(t), (t / duration) as f32 * TEST_BED, "fit at {t}"); |
| 10133 | - fit.frac_at(t, duration), | ||
| 10134 | - (t / duration) as f32, | ||
| 10135 | - "fit at {t}" | ||
| 10136 | - ); | ||
| 10137 | } | 10268 | } |
| 10138 | - assert_eq!(fit.width_frac(5., duration), width_frac(5., duration)); | 10269 | + for scale in [ |
| 10139 | - for view in [ | ||
| 10140 | fit, | 10270 | fit, |
| 10141 | - View { | 10271 | + Scale { |
| 10142 | - zoom: 2., | 10272 | + pps: 20., |
| 10143 | start: 4., | 10273 | start: 4., |
| 10144 | }, | 10274 | }, |
| 10145 | - View { | 10275 | + Scale { |
| 10146 | - zoom: 8., | 10276 | + pps: 80., |
| 10147 | start: 12.5, | 10277 | start: 12.5, |
| 10148 | }, | 10278 | }, |
| 10149 | - View { | 10279 | + Scale { |
| 10150 | - zoom: 37.5, | 10280 | + pps: 375., |
| 10151 | start: 19., | 10281 | start: 19., |
| 10152 | }, | 10282 | }, |
| 10153 | ] { | 10283 | ] { |
| 10154 | - let view = view.settled(duration, 30.); | 10284 | + let scale = test_view(scale, duration).settled(); |
| 10155 | - for frac in [0., 0.25, 0.5, 1.] { | 10285 | + for x in [0., 50., 100., TEST_BED] { |
| 10156 | - let at = view.time_at(frac, duration); | 10286 | + let at = scale.time_at(x); |
| 10157 | assert!( | 10287 | assert!( |
| 10158 | - (f64::from(view.frac_at(at, duration)) - f64::from(frac)).abs() < 1e-6, | 10288 | + (f64::from(scale.px_at(at)) - f64::from(x)).abs() < 1e-3, |
| 10159 | - "{view:?} round trip at {frac}" | 10289 | + "{scale:?} round trip at {x}" |
| 10160 | ); | 10290 | ); |
| 10161 | } | 10291 | } |
| 10162 | - // A stretch is as wide as the share of the visible slice it takes. | 10292 | + // A stretch as long as what is on the bed is as wide as the bed. |
| 10163 | assert!( | 10293 | assert!( |
| 10164 | - (f64::from(view.width_frac(view.span(duration), duration)) - 1.).abs() < 1e-6, | 10294 | + (f64::from(scale.width_px(test_view(scale, duration).span())) - f64::from(TEST_BED)) |
| 10165 | - "{view:?} spans the bed" | 10295 | + .abs() |
| 10296 | + < 1e-3, | ||
| 10297 | + "{scale:?} spans the bed" | ||
| 10166 | ); | 10298 | ); |
| 10167 | } | 10299 | } |
| 10168 | } | 10300 | } |
| @@ -10172,70 +10304,98 @@ mod tests { | |||
| 10172 | /// ctrl+wheel. | 10304 | /// ctrl+wheel. |
| 10173 | #[test] | 10305 | #[test] |
| 10174 | fn a_zoom_leaves_the_anchor_where_it_was_on_screen() { | 10306 | fn a_zoom_leaves_the_anchor_where_it_was_on_screen() { |
| 10175 | - let (duration, fps) = (20., 30.); | 10307 | + let duration = 20.; |
| 10176 | - let mut view = View::default(); | 10308 | + let mut scale = test_view(Scale::default(), duration).settled(); |
| 10177 | - for anchor in [0.5f32, 0.25, 0.9] { | 10309 | + // The same three points along the bed the fractional view held: a half, |
| 10310 | + // a quarter and nine tenths of 200 px. | ||
| 10311 | + for anchor in [100f32, 50., 180.] { | ||
| 10178 | // Well past the zoom-in stop: the anchor holds at the clamp too, | 10312 | // Well past the zoom-in stop: the anchor holds at the clamp too, |
| 10179 | // which is where it used to slide (the clamp came after the offset). | 10313 | // which is where it used to slide (the clamp came after the offset). |
| 10180 | for _ in 0..30 { | 10314 | for _ in 0..30 { |
| 10181 | - let at = view.time_at(anchor, duration); | 10315 | + let at = scale.time_at(anchor); |
| 10182 | - let zoomed = view.zoomed(ZOOM_STEP, anchor, duration, fps); | 10316 | + let view = test_view(scale, duration); |
| 10317 | + let zoomed = view.zoomed(ZOOM_STEP, anchor); | ||
| 10183 | // Only where the anchor is not pinned by an edge of the | 10318 | // Only where the anchor is not pinned by an edge of the |
| 10184 | // timeline: a view already against an end cannot slide further. | 10319 | // timeline: a view already against an end cannot slide further. |
| 10185 | - let pinned = zoomed.start <= 0. || zoomed.start + zoomed.span(duration) >= duration; | 10320 | + let span = test_view(zoomed, duration).span(); |
| 10321 | + let pinned = zoomed.start <= 0. || zoomed.start + span >= duration; | ||
| 10186 | if !pinned { | 10322 | if !pinned { |
| 10187 | assert!( | 10323 | assert!( |
| 10188 | - (f64::from(zoomed.frac_at(at, duration)) - f64::from(anchor)).abs() < 1e-6, | 10324 | + (f64::from(zoomed.px_at(at)) - f64::from(anchor)).abs() < 1e-3, |
| 10189 | - "{at} moved: {view:?} -> {zoomed:?}" | 10325 | + "{at} moved: {scale:?} -> {zoomed:?}" |
| 10190 | ); | 10326 | ); |
| 10191 | } | 10327 | } |
| 10192 | - assert!(zoomed.zoom >= view.zoom, "and it did zoom in"); | 10328 | + assert!(zoomed.pps >= scale.pps, "and it did zoom in"); |
| 10193 | - view = zoomed; | 10329 | + scale = zoomed; |
| 10194 | } | 10330 | } |
| 10195 | } | 10331 | } |
| 10196 | } | 10332 | } |
| 10197 | 10333 | ||
| 10198 | - /// Both stops: out is the whole timeline (which is where the panel starts, | 10334 | + /// Both stops, and the point of the whole mapping: neither is the content's |
| 10199 | - /// so nothing regressed for anyone who never zooms), in is a handful of | 10335 | + /// own length any more. Out is [`ZOOM_MAX_SECONDS`] across the bed, in is |
| 10200 | - /// frames -- and neither can scroll off an end. | 10336 | + /// [`ZOOM_MIN_FRAMES`] across it -- so a short import can be zoomed out of |
| 10337 | + /// and a long one zoomed into, and neither can scroll off an end. | ||
| 10201 | #[test] | 10338 | #[test] |
| 10202 | - fn zoom_stops_at_the_whole_timeline_and_at_a_handful_of_frames() { | 10339 | + fn zoom_stops_at_a_bedful_of_time_and_at_a_handful_of_frames() { |
| 10203 | let (duration, fps) = (20., 30.); | 10340 | let (duration, fps) = (20., 30.); |
| 10204 | - let mut view = View::default(); | 10341 | + let mut scale = Scale::default(); |
| 10205 | for _ in 0..200 { | 10342 | for _ in 0..200 { |
| 10206 | - view = view.zoomed(1. / ZOOM_STEP, 0.5, duration, fps); | 10343 | + scale = test_view(scale, duration).zoomed(1. / ZOOM_STEP, 100.); |
| 10207 | } | 10344 | } |
| 10208 | - assert_eq!(view, View::default(), "zoomed out is the fit"); | 10345 | + assert!( |
| 10346 | + (test_view(scale, duration).span() - ZOOM_MAX_SECONDS).abs() < 1e-6, | ||
| 10347 | + "widest is a bedful of hours, not the 20 s that happen to be on it" | ||
| 10348 | + ); | ||
| 10209 | for _ in 0..200 { | 10349 | for _ in 0..200 { |
| 10210 | - view = view.zoomed(ZOOM_STEP, 0.5, duration, fps); | 10350 | + scale = test_view(scale, duration).zoomed(ZOOM_STEP, 100.); |
| 10211 | } | 10351 | } |
| 10212 | assert_eq!( | 10352 | assert_eq!( |
| 10213 | - (view.span(duration) * fps).round(), | 10353 | + (test_view(scale, duration).span() * fps).round(), |
| 10214 | ZOOM_MIN_FRAMES, | 10354 | ZOOM_MIN_FRAMES, |
| 10215 | "tightest is a handful of frames" | 10355 | "tightest is a handful of frames" |
| 10216 | ); | 10356 | ); |
| 10217 | // Against the far end, the slice still ends at the last frame. | 10357 | // Against the far end, the slice still ends at the last frame. |
| 10218 | - let end = View { | 10358 | + let end = test_view( |
| 10219 | - zoom: view.zoom, | 10359 | + Scale { |
| 10220 | - start: 1e6, | 10360 | + start: 1e6, |
| 10221 | - } | 10361 | + ..scale |
| 10222 | - .settled(duration, fps); | 10362 | + }, |
| 10223 | - assert!((end.start + end.span(duration) - duration).abs() < 1e-9); | 10363 | + duration, |
| 10224 | - assert!( | 10364 | + ); |
| 10225 | - View { | 10365 | + let end = (end.settled(), end.span()); |
| 10226 | - zoom: view.zoom, | 10366 | + assert!((end.0.start + end.1 - duration).abs() < 1e-9); |
| 10227 | - start: -5. | 10367 | + assert_eq!( |
| 10228 | - } | 10368 | + test_view( |
| 10229 | - .settled(duration, fps) | 10369 | + Scale { |
| 10230 | - .start | 10370 | + start: -5., |
| 10231 | - == 0. | 10371 | + ..scale |
| 10372 | + }, | ||
| 10373 | + duration | ||
| 10374 | + ) | ||
| 10375 | + .settled() | ||
| 10376 | + .start, | ||
| 10377 | + 0. | ||
| 10232 | ); | 10378 | ); |
| 10233 | - // A timeline too short to zoom into, and one with no length at all, | 10379 | + // The dead zone the fractional view had: a timeline of a handful of |
| 10234 | - // both stay at the fit rather than dividing by nothing. | 10380 | + // frames could not be zoomed at all, because its own length was the |
| 10235 | - assert_eq!(View::max_zoom(0.1, fps), 1.); | 10381 | + // floor *and* the ceiling. Both keys work on it now. |
| 10236 | - assert_eq!(View::default().settled(0., fps), View::default()); | 10382 | + let tiny = test_view(Scale::default(), 0.1); |
| 10237 | - assert_eq!(View::default().frac_at(3., 0.), 0.); | 10383 | + assert!(tiny.zoomed(ZOOM_STEP, 100.).pps > PPS_DEFAULT); |
| 10238 | - assert_eq!(View::default().time_at(0.5, 0.), 0.); | 10384 | + assert!(tiny.zoomed(1. / ZOOM_STEP, 100.).pps < PPS_DEFAULT); |
| 10385 | + // A timeline with no length at all divides by nothing and scrolls | ||
| 10386 | + // nowhere; the fit of one keeps the scale it had. | ||
| 10387 | + let empty = test_view(Scale::default(), 0.); | ||
| 10388 | + assert_eq!(empty.settled(), Scale::default()); | ||
| 10389 | + assert_eq!(empty.fit(), Scale::default()); | ||
| 10390 | + assert_eq!(Scale::default().px_at(0.), 0.); | ||
| 10391 | + // A bed that was never painted clamps nothing -- there is nothing to | ||
| 10392 | + // clamp against, and a zoom must survive the frame before the probe. | ||
| 10393 | + let unpainted = View { | ||
| 10394 | + bed: 0., | ||
| 10395 | + ..test_view(Scale { pps: 4e6, start: 3. }, duration) | ||
| 10396 | + }; | ||
| 10397 | + assert_eq!(unpainted.settled().pps, 4e6); | ||
| 10398 | + assert_eq!(unpainted.following(19.), unpainted.scale); | ||
| 10239 | } | 10399 | } |
| 10240 | 10400 | ||
| 10241 | /// What makes a zoomed timeline follow the playing head: off the bed at | 10401 | /// What makes a zoomed timeline follow the playing head: off the bed at |
| @@ -10243,53 +10403,96 @@ mod tests { | |||
| 10243 | /// view that jumped every frame would be unreadable. | 10403 | /// view that jumped every frame would be unreadable. |
| 10244 | #[test] | 10404 | #[test] |
| 10245 | fn the_view_follows_a_playhead_that_runs_off_the_bed() { | 10405 | fn the_view_follows_a_playhead_that_runs_off_the_bed() { |
| 10246 | - let (duration, fps) = (20., 30.); | 10406 | + let duration = 20.; |
| 10247 | - let view = View { | 10407 | + // 5 s on a 200 px bed: 40 px to the second, starting at second 5. |
| 10248 | - zoom: 4., | 10408 | + let view = test_view( |
| 10249 | - start: 5., | 10409 | + Scale { |
| 10250 | - } | 10410 | + pps: 40., |
| 10251 | - .settled(duration, fps); | 10411 | + start: 5., |
| 10252 | - assert_eq!(view.span(duration), 5.); | 10412 | + }, |
| 10413 | + duration, | ||
| 10414 | + ); | ||
| 10415 | + let scale = view.settled(); | ||
| 10416 | + assert_eq!(view.span(), 5.); | ||
| 10253 | // Inside: untouched, whichever part of the slice it is in. | 10417 | // Inside: untouched, whichever part of the slice it is in. |
| 10254 | for at in [5., 7.5, 10.] { | 10418 | for at in [5., 7.5, 10.] { |
| 10255 | - assert_eq!( | 10419 | + assert_eq!(view.following(at), scale, "{at} is on the bed"); |
| 10256 | - view.following(at, duration, fps), | ||
| 10257 | - view, | ||
| 10258 | - "{at} is on the bed" | ||
| 10259 | - ); | ||
| 10260 | } | 10420 | } |
| 10261 | // Past the right edge, as playback does it: the head comes back on the | 10421 | // Past the right edge, as playback does it: the head comes back on the |
| 10262 | - // bed, and the zoom is not changed by the scroll. | 10422 | + // bed, and the scale is not changed by the scroll. |
| 10263 | - let moved = view.following(12., duration, fps); | 10423 | + let moved = view.following(12.); |
| 10264 | - assert_eq!(moved.zoom, view.zoom); | 10424 | + assert_eq!(moved.pps, scale.pps); |
| 10265 | - assert!(moved.start > view.start, "scrolled forward"); | 10425 | + assert!(moved.start > scale.start, "scrolled forward"); |
| 10266 | assert!( | 10426 | assert!( |
| 10267 | - moved.frac_at(12., duration) > 0. && moved.frac_at(12., duration) < 1., | 10427 | + moved.px_at(12.) > 0. && moved.px_at(12.) < TEST_BED, |
| 10268 | "and the playhead is on screen" | 10428 | "and the playhead is on screen" |
| 10269 | ); | 10429 | ); |
| 10270 | // A seek back behind the slice does the same the other way. | 10430 | // A seek back behind the slice does the same the other way. |
| 10271 | - let back = view.following(1., duration, fps); | 10431 | + let back = view.following(1.); |
| 10272 | - assert!(back.start < view.start); | 10432 | + assert!(back.start < scale.start); |
| 10273 | - assert!(back.frac_at(1., duration) >= 0.); | 10433 | + assert!(back.px_at(1.) >= 0.); |
| 10274 | - // At the fit there is nothing to follow: the whole timeline is on the | 10434 | + // With the whole timeline on the bed there is nothing to follow. |
| 10275 | - // bed already, so no playhead can move it. | 10435 | + let whole = test_view(Scale::default(), duration); |
| 10436 | + let fit = whole.fit(); | ||
| 10276 | for at in [0., 10., 20.] { | 10437 | for at in [0., 10., 20.] { |
| 10277 | assert_eq!( | 10438 | assert_eq!( |
| 10278 | - View::default().following(at, duration, fps), | 10439 | + test_view(fit, duration).following(at), |
| 10279 | - View::default(), | 10440 | + fit, |
| 10280 | "the fit never scrolls" | 10441 | "the fit never scrolls" |
| 10281 | ); | 10442 | ); |
| 10282 | } | 10443 | } |
| 10283 | } | 10444 | } |
| 10284 | 10445 | ||
| 10446 | + /// The bug this mapping exists for: the first import used to fill the whole | ||
| 10447 | + /// track whatever it was, because the bed *was* the timeline -- so a 5 s | ||
| 10448 | + /// clip was 100% of the lane, zooming out did nothing, and adding a second | ||
| 10449 | + /// clip silently halved the first one's box. | ||
| 10450 | + #[test] | ||
| 10451 | + fn a_clip_is_drawn_the_same_width_whatever_else_is_on_the_timeline() { | ||
| 10452 | + let bed = 900.; | ||
| 10453 | + let of = |duration: f64| View { | ||
| 10454 | + scale: Scale::default(), | ||
| 10455 | + bed, | ||
| 10456 | + duration, | ||
| 10457 | + fps: 30., | ||
| 10458 | + }; | ||
| 10459 | + // One 5 s import, then a second clip after it: 20 s of timeline where | ||
| 10460 | + // there were 5, and the first box has not moved or narrowed. | ||
| 10461 | + let (alone, joined) = (of(5.).settled(), of(20.).settled()); | ||
| 10462 | + assert_eq!(alone, joined); | ||
| 10463 | + assert_eq!(alone.width_px(5.), joined.width_px(5.)); | ||
| 10464 | + assert_eq!(joined.px_at(5.), alone.width_px(5.)); | ||
| 10465 | + // And it does not fill the track: a short clip reads as short. | ||
| 10466 | + assert!( | ||
| 10467 | + alone.width_px(5.) < bed / 2., | ||
| 10468 | + "5 s at {} px/s is {} px of a {bed} px bed", | ||
| 10469 | + alone.pps, | ||
| 10470 | + alone.width_px(5.) | ||
| 10471 | + ); | ||
| 10472 | + // Zooming out visibly shrinks it, however short the timeline is -- the | ||
| 10473 | + // press that used to be a no-op. | ||
| 10474 | + let out = of(5.).zoomed(1. / ZOOM_STEP, 0.); | ||
| 10475 | + assert!( | ||
| 10476 | + out.width_px(5.) < alone.width_px(5.), | ||
| 10477 | + "{} px is not smaller than {} px", | ||
| 10478 | + out.width_px(5.), | ||
| 10479 | + alone.width_px(5.) | ||
| 10480 | + ); | ||
| 10481 | + // The way back to "the whole timeline across the bed" is still one key. | ||
| 10482 | + assert_eq!(of(5.).fit().width_px(5.), bed); | ||
| 10483 | + } | ||
| 10484 | + | ||
| 10485 | + /// What the zoom button says: how much timeline is on the bed, in a unit | ||
| 10486 | + /// that tells two zooms apart. | ||
| 10285 | #[test] | 10487 | #[test] |
| 10286 | - fn clip_boxes_split_the_lane_by_duration() { | 10488 | + fn the_zoom_button_says_how_much_is_on_the_bed() { |
| 10287 | - // 1 s + 3 s of a 4 s timeline: a quarter and three quarters. | 10489 | + assert_eq!(span_label(4.5), "4.5s"); |
| 10288 | - assert_eq!(width_frac(1., 4.), 0.25); | 10490 | + assert_eq!(span_label(22.5), "22s"); |
| 10289 | - assert_eq!(width_frac(3., 4.), 0.75); | 10491 | + assert_eq!(span_label(90.), "1.5m"); |
| 10290 | - assert_eq!(width_frac(4., 4.), 1.); | 10492 | + assert_eq!(span_label(3600.), "60m"); |
| 10291 | - // A timeline with no length must not hand gpui a NaN width. | 10493 | + // Before the first paint there is no bed and so no answer to give. |
| 10292 | - assert_eq!(width_frac(0., 0.), 1.); | 10494 | + assert_eq!(span_label(0.), "—"); |
| 10495 | + assert_eq!(span_label(f64::NAN), "—"); | ||
| 10293 | } | 10496 | } |
| 10294 | 10497 | ||
| 10295 | #[test] | 10498 | #[test] |
| @@ -11337,10 +11540,11 @@ mod tests { | |||
| 11337 | assert!(show_label(LABEL_MIN_W)); | 11540 | assert!(show_label(LABEL_MIN_W)); |
| 11338 | assert!(show_label(400.)); | 11541 | assert!(show_label(400.)); |
| 11339 | assert!(!show_label(LABEL_MIN_W - 0.1)); | 11542 | assert!(!show_label(LABEL_MIN_W - 0.1)); |
| 11340 | - // A timeline with no length must not put a box outside its lane. | 11543 | + // The label test is the box's own width in pixels now, which is what |
| 11341 | - let fit = View::default(); | 11544 | + // the scale hands it -- no bed width, and so nothing to be zero. |
| 11342 | - assert_eq!(fit.frac_at(3., 0.), 0.); | 11545 | + let scale = Scale::default(); |
| 11343 | - assert_eq!(fit.frac_at(1., 4.), 0.25); | 11546 | + assert!(show_label(scale.width_px(LABEL_MIN_W as f64 / PPS_DEFAULT))); |
| 11547 | + assert!(!show_label(scale.width_px(0.))); | ||
| 11344 | } | 11548 | } |
| 11345 | 11549 | ||
| 11346 | #[test] | 11550 | #[test] |
| @@ -11708,9 +11912,10 @@ fn main() { | |||
| 11708 | eos: false, | 11912 | eos: false, |
| 11709 | done: false, | 11913 | done: false, |
| 11710 | ruler: Rc::default(), | 11914 | ruler: Rc::default(), |
| 11711 | - // The whole timeline across the bed, which is where a | 11915 | + // A second is [`PPS_DEFAULT`] pixels wide until someone |
| 11712 | - // project opens: zooming is something the user asks for. | 11916 | + // zooms or asks for the fit: a project opens at a scale, not |
| 11713 | - view: View::default(), | 11917 | + // at whatever its first import happens to be long. |
| 11918 | + scale: Scale::default(), | ||
| 11714 | selected: None, | 11919 | selected: None, |
| 11715 | context_menu: None, | 11920 | context_menu: None, |
| 11716 | library_menu: None, | 11921 | library_menu: None, |