forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

viewport.rs313 lines, 12.0 KiBHistoryRawDownload
1//! How much timeline is on the bed, and how a time is written.
2
3use crate::*;
4
5/// A span of seconds as a person reads it: one decimal above a second, two
6/// below -- [`scaled`]'s rule, for its reason. The tightest zoom is
7/// [`ZOOM_MIN_FRAMES`] across the bed, which on the 240 fps slow-motion a phone
8/// writes is 0.03s, and a single frame of quiet at 60 fps is 0.02s: one decimal
9/// prints both as "0.0s", a pill and a notice saying the thing they are about
10/// has no length at all.
11pub(crate) fn secs_label(secs: f64) -> String {
12 match secs >= 1. {
13 true => format!("{secs:.1}s"),
14 false => format!("{secs:.2}s"),
15 }
16}
17
18/// The mapping the whole panel is drawn and clicked through: `pps` pixels to a
19/// second of timeline, `start` the moment at the bed's left edge. Absolute --
20/// how wide a clip is drawn depends on how long the clip is and on nothing
21/// else, so adding a second clip does not redraw the first one narrower and
22/// zooming out always makes every box smaller.
23///
24/// The one place seconds become pixels: every box, the playhead, a seek and a
25/// trim all go through it, so none of them can drift away from the others when
26/// the view moves. What clamps it -- the stops, the scroll, the fit -- needs the
27/// bed it is drawn on and lives on [`View`].
28#[derive(Clone, Copy, Debug, PartialEq)]
29pub(crate) struct Scale {
30 pub(crate) pps: f64,
31 pub(crate) start: f64,
32}
33
34impl Default for Scale {
35 fn default() -> Self {
36 Scale {
37 pps: PPS_DEFAULT,
38 start: 0.,
39 }
40 }
41}
42
43impl Scale {
44 /// Where a moment sits on the bed, in pixels from its left edge. Negative
45 /// for a moment scrolled off to the left, which is exactly the offset a
46 /// half-visible clip is drawn at.
47 pub(crate) fn px_at(self, at: f64) -> f32 {
48 ((at - self.start) * self.pps) as f32
49 }
50
51 /// How wide a stretch of `len` seconds is drawn. Wider than the bed once
52 /// zoomed in, which is the point of zooming in; never negative, which gpui
53 /// has no meaning for.
54 pub(crate) fn width_px(self, len: f64) -> f32 {
55 (len * self.pps).max(0.) as f32
56 }
57
58 /// The moment `x` pixels along the bed is pointing at: the inverse of
59 /// [`Scale::px_at`], and what every seek and every trim reads. Clamped at
60 /// the head of the timeline only -- there is bed past the last frame now,
61 /// and a tail dragged into it is a longer clip, not an error.
62 pub(crate) fn time_at(self, x: f32) -> f64 {
63 if self.pps > 0. {
64 (self.start + f64::from(x) / self.pps).max(0.)
65 } else {
66 self.start
67 }
68 }
69
70 /// [`SNAP_PX`] in timeline frames at the scale the bed is drawn at: a snap
71 /// is a distance on screen, so zoomed right in it is worth less than a frame
72 /// (no snap at all, which is what a hand placing single frames wants) and
73 /// zoomed out it is worth many.
74 pub(crate) fn snap_frames(self, fps: f64) -> u32 {
75 if self.pps > 0. {
76 (SNAP_PX / self.pps * fps) as u32
77 } else {
78 0
79 }
80 }
81
82 /// [`DROP_SNAP_PX`] in timeline frames, [`Scale::snap_frames`]'s twin for a
83 /// drag's *release* rather than a trim: a dropped clip is aimed with a
84 /// coarser hand than an edge nudged one frame at a time, and a pixel-exact
85 /// release is what the user asked never to need.
86 pub(crate) fn drop_snap_frames(self, fps: f64) -> u32 {
87 if self.pps > 0. {
88 (DROP_SNAP_PX / self.pps * fps) as u32
89 } else {
90 0
91 }
92 }
93}
94
95/// A [`Scale`] against the bed it is drawn on and the timeline it is drawn
96/// from. The bed's width is what turns a scale into "how much is on screen",
97/// and that is all the stops, the scroll clamp and the fit are made of.
98///
99/// Built per use out of [`Player::view`] and thrown away again -- the state is
100/// the `Scale`, and this is what a bed of `bed` px showing `duration` seconds
101/// at `fps` may do to it. So no call site can measure a moment against a bed or
102/// a duration that another one did not.
103#[derive(Clone, Copy, Debug, PartialEq)]
104pub(crate) struct View {
105 pub(crate) scale: Scale,
106 pub(crate) bed: f32,
107 pub(crate) duration: f64,
108 pub(crate) fps: f64,
109}
110
111impl View {
112 /// How much of the timeline is on the bed, in seconds.
113 pub(crate) fn span(self) -> f64 {
114 if self.scale.pps > 0. {
115 f64::from(self.bed) / self.scale.pps
116 } else {
117 0.
118 }
119 }
120
121 /// The two stops, in pixels per second: [`ZOOM_MIN_FRAMES`] across the bed
122 /// is as tight as it goes, and as wide as it goes is the whole timeline on
123 /// the bed with [`ZOOM_OUT_MARGIN`] to spare -- the far stop is *relative*,
124 /// so the end of a five hour timeline is reachable for the same reason the
125 /// end of a five minute one is. A fixed far stop could not say that: a
126 /// timeline longer than it had an end no zoom could reach, which is the bug
127 /// this is. Short of [`PPS_MIN`] the timeline's own length is not worth
128 /// widening to and the pixel takes over, so a short import can still be
129 /// zoomed out of and the resting scale is nobody's content.
130 ///
131 /// `None` on a bed that was never painted -- there is nothing to measure
132 /// them against yet, and a stop guessed off a zero width would throw away a
133 /// zoom the user asked for.
134 pub(crate) fn stops(self) -> Option<(f64, f64)> {
135 (self.bed > 0.).then(|| {
136 let bed = f64::from(self.bed);
137 let whole = match self.duration > 0. {
138 true => bed / (self.duration * ZOOM_OUT_MARGIN),
139 false => PPS_MIN,
140 };
141 let min = whole.min(PPS_MIN);
142 (min, (bed * self.fps / ZOOM_MIN_FRAMES).max(min))
143 })
144 }
145
146 /// Clamped to the bed it draws on: between the two stops, and never
147 /// scrolled past either end of the timeline. Unlike the fractional view
148 /// this replaced, the *resting* scale is not the content -- a five second
149 /// timeline zooms out as far as an hour long one does, and both are drawn
150 /// at [`PPS_DEFAULT`] until someone zooms. Only the far stop knows the
151 /// length, and only once the length is worth more than [`PPS_MIN`].
152 pub(crate) fn settled(self) -> Scale {
153 let pps = match self.scale.pps.is_finite() && self.scale.pps > 0. {
154 true => self.scale.pps,
155 false => PPS_DEFAULT,
156 };
157 let start = match self.scale.start.is_finite() {
158 true => self.scale.start,
159 false => 0.,
160 };
161 let Some((min, max)) = self.stops() else {
162 return Scale {
163 pps,
164 start: start.max(0.),
165 };
166 };
167 let pps = pps.clamp(min, max);
168 let span = f64::from(self.bed) / pps;
169 Scale {
170 pps,
171 start: start.clamp(0., (self.duration - span).max(0.)),
172 }
173 }
174
175 /// Zoomed by `factor` about `anchor` (pixels along the bed): whatever moment
176 /// was under that point stays under it, so a zoom magnifies what was being
177 /// looked at rather than throwing it off the edge.
178 pub(crate) fn zoomed(self, factor: f32, anchor: f32) -> Scale {
179 let at = self.scale.time_at(anchor);
180 // Clamped *before* the offset is worked out, not after: a press that
181 // runs into either stop must still leave the anchor where it is, and a
182 // start measured against a scale the stop then took away would slide it.
183 let raw = self.scale.pps * f64::from(factor);
184 let pps = match self.stops() {
185 Some((min, max)) => raw.clamp(min, max),
186 None => raw,
187 };
188 View {
189 scale: Scale {
190 pps,
191 start: at - f64::from(anchor) / pps,
192 },
193 ..self
194 }
195 .settled()
196 }
197
198 /// Slid along by `by` pixels, later in the timeline for a positive one: the
199 /// one move that changes what is on the bed without changing how wide a
200 /// second is drawn, which is what a bare wheel does. Clamped by
201 /// [`View::settled`] like every other move, so a run at either end stops at
202 /// the end rather than scrolling the timeline off the bed -- and the extent
203 /// it stops against is the content's own length, never a number.
204 pub(crate) fn scrolled(self, by: f32) -> Scale {
205 let pps = self.settled().pps;
206 View {
207 scale: Scale {
208 pps,
209 start: self.scale.start + f64::from(by) / pps,
210 },
211 ..self
212 }
213 .settled()
214 }
215
216 /// The whole timeline across the bed. The one place the content's own
217 /// length sets the scale, and the only one -- everywhere else a second is a
218 /// second -- because this is a user pressing a key that asks for exactly
219 /// that.
220 pub(crate) fn fit(self) -> Scale {
221 let pps = match self.duration > 0. && self.bed > 0. {
222 true => f64::from(self.bed) / self.duration,
223 false => PPS_DEFAULT,
224 };
225 View {
226 scale: Scale { pps, start: 0. },
227 ..self
228 }
229 .settled()
230 }
231
232 /// The scale a playhead at `at` needs: the same one while it is on the bed,
233 /// and one centred on it once it has run off -- which is how a zoomed-in
234 /// timeline scrolls, during playback and after a seek alike. With the whole
235 /// timeline on the bed this can never fire, so a panel showing all of it is
236 /// untouched by it.
237 pub(crate) fn following(self, at: f64) -> Scale {
238 // Nothing is drawn yet, so nothing has run off anything.
239 if self.bed <= 0. {
240 return self.scale;
241 }
242 let scale = self.settled();
243 if self.shows(at) {
244 return scale;
245 }
246 let span = f64::from(self.bed) / scale.pps;
247 View {
248 scale: Scale {
249 start: at - span / 2.,
250 ..scale
251 },
252 ..self
253 }
254 .settled()
255 }
256
257 /// Whether the moment `at` is on the bed as it is drawn now. The one
258 /// question both halves of the follow ask -- [`View::following`] to decide
259 /// whether to chase a head that has run off, and the render to decide when
260 /// a hand's own scroll ([`Player::panned`]) has been caught up with and the
261 /// follow may have the view back -- so the two can never disagree about
262 /// where the edge of the bed is.
263 pub(crate) fn shows(self, at: f64) -> bool {
264 if self.bed <= 0. {
265 return false;
266 }
267 let scale = self.settled();
268 at >= scale.start && at <= scale.start + f64::from(self.bed) / scale.pps
269 }
270}
271
272/// NLE timecode: `HH:MM:SS:FF`, the frame counted inside its own second.
273pub(crate) fn timecode(t: f64, fps: f64) -> String {
274 let t = t.max(0.);
275 let secs = t as u64;
276 let last = (fps.ceil() as u64).saturating_sub(1);
277 let frame = ((t - secs as f64) * fps) as u64;
278 format!(
279 "{:02}:{:02}:{:02}:{:02}",
280 secs / 3600,
281 secs / 60 % 60,
282 secs % 60,
283 frame.min(last)
284 )
285}
286
287/// NLE timecode for a *duration* given as a frame count: the same
288/// `HH:MM:SS:FF` shape as [`timecode`], but built from the count directly
289/// rather than a float number of seconds. `timecode` truncates `(t - secs) *
290/// fps`, which loses a whole frame to float error on a duration derived from
291/// dividing a frame count by `fps` first (346 frames at 30 fps printed
292/// `00:00:11:15`, one short of the true `:16`); integer frame math has no
293/// such rounding to lose.
294pub(crate) fn frames_timecode(frames: u32, fps: f64) -> String {
295 let per_sec = (fps.ceil() as u64).max(1);
296 let frames = u64::from(frames);
297 let secs = frames / per_sec;
298 let frame = frames % per_sec;
299 format!(
300 "{:02}:{:02}:{:02}:{:02}",
301 secs / 3600,
302 secs / 60 % 60,
303 secs % 60,
304 frame
305 )
306}
307
308/// Wall clock for a progress line: `M:SS`, minutes past the hour included
309/// rather than an hours field nobody reads on an export.
310pub(crate) fn clock(secs: f32) -> String {
311 let secs = secs.max(0.) as u64;
312 format!("{}:{:02}", secs / 60, secs % 60)
313}