🔀 merge(B3): drags stop lying — previews equal commits at every speed, drops move the clip you picked up, waveforms stay bounded and painted
Browse files at this commit1 changed file +370 -65
- crates/app/src/main.rs +370 -65
crates/app/src/main.rsmodified+370 -65
| @@ -85,6 +85,13 @@ const WAVE_BPS: u32 = 40; | |||
| 85 | /// Pixels per envelope column. Coarser than a pixel: the eye reads the shape, | 85 | /// Pixels per envelope column. Coarser than a pixel: the eye reads the shape, |
| 86 | /// and a path with a point per pixel is a path per repaint. | 86 | /// and a path with a point per pixel is a path per repaint. |
| 87 | const WAVE_COL: f32 = 2.; | 87 | const WAVE_COL: f32 = 2.; |
| 88 | +/// The most columns one envelope is ever built from. A waveform is drawn into | ||
| 89 | +/// the slice of its box that is on the bed ([`visible_slice`]), and a bed is a | ||
| 90 | +/// screen wide, so nothing on screen ever reaches this -- it is the backstop for | ||
| 91 | +/// a box laid out wider than any screen, whose path would otherwise cost a point | ||
| 92 | +/// per two pixels of a width nobody can see and stall the repaint that was | ||
| 93 | +/// meant to draw the wave. | ||
| 94 | +const WAVE_COLS_MAX: usize = 4096; | ||
| 88 | /// WCAG 2.5.8: nothing clickable is smaller than this. The scrub bar stays 6 px | 95 | /// WCAG 2.5.8: nothing clickable is smaller than this. The scrub bar stays 6 px |
| 89 | /// to look at -- `RULER_HIT_H` is the strip that has to be hit. | 96 | /// to look at -- `RULER_HIT_H` is the strip that has to be hit. |
| 90 | const HIT_MIN: f32 = 24.; | 97 | const HIT_MIN: f32 = 24.; |
| @@ -281,6 +288,13 @@ struct AssetDrag(PathBuf, usize); | |||
| 281 | struct ClipDrag { | 288 | struct ClipDrag { |
| 282 | lane: Lane, | 289 | lane: Lane, |
| 283 | idx: usize, | 290 | idx: usize, |
| 291 | + /// The clip that was picked up, so the drop can find it again: gpui freezes | ||
| 292 | + /// the payload for the whole gesture, and an edit made *during* one -- a | ||
| 293 | + /// stroke deletes, undoes or pastes, none of which a drag blocks -- ripples | ||
| 294 | + /// the indices under it. The index alone would then name a different take at | ||
| 295 | + /// the release, and the drag would move a clip nobody touched (see | ||
| 296 | + /// [`live_idx`]). | ||
| 297 | + clip: Clip, | ||
| 284 | } | 298 | } |
| 285 | 299 | ||
| 286 | /// A clip edge being dragged: which end of which clip, and the timeline frame | 300 | /// A clip edge being dragged: which end of which clip, and the timeline frame |
| @@ -2610,6 +2624,16 @@ impl Player { | |||
| 2610 | Some(self.snap_to(raw, clip.frames(), &marks)) | 2624 | Some(self.snap_to(raw, clip.frames(), &marks)) |
| 2611 | } | 2625 | } |
| 2612 | 2626 | ||
| 2627 | + /// Which index the clip in the hand is at *now*: [`live_idx`] against the | ||
| 2628 | + /// lane the drag named, since a stroke during the gesture moves the indices | ||
| 2629 | + /// gpui froze into the payload. Both halves of a drag ask it -- the line | ||
| 2630 | + /// drawn in flight and the drop that commits -- so the promise and the | ||
| 2631 | + /// landing are made about one clip. | ||
| 2632 | + fn dragged(&self, drag: &ClipDrag) -> Option<usize> { | ||
| 2633 | + let session = self.session.as_ref()?; | ||
| 2634 | + live_idx(session.lane_clips(drag.lane), drag.idx, drag.clip) | ||
| 2635 | + } | ||
| 2636 | + | ||
| 2613 | /// The line while the clip is still in the hand: the very answer | 2637 | /// The line while the clip is still in the hand: the very answer |
| 2614 | /// [`Player::drop_frame`] will commit, worked out on every move of the drag, | 2638 | /// [`Player::drop_frame`] will commit, worked out on every move of the drag, |
| 2615 | /// so what the eye was promised is where the release puts it. A pointer that | 2639 | /// so what the eye was promised is where the release puts it. A pointer that |
| @@ -2780,39 +2804,13 @@ impl Player { | |||
| 2780 | }) else { | 2804 | }) else { |
| 2781 | return clip; | 2805 | return clip; |
| 2782 | }; | 2806 | }; |
| 2783 | - match trim.edge { | 2807 | + let still = self.session.as_ref().is_some_and(|session| { |
| 2784 | - // A still grows forward from source frame 0 instead, exactly as | 2808 | + session |
| 2785 | - // `Project::trim` writes it: with the in-point clamped at 0 the box | 2809 | + .sources() |
| 2786 | - // below would slide left rather than stretch, and the release would | 2810 | + .get(clip.source) |
| 2787 | - // then commit a wider clip than the drag drew. | 2811 | + .is_some_and(|s| engine::is_image(&s.path)) |
| 2788 | - Edge::Start | 2812 | + }); |
| 2789 | - if self.session.as_ref().is_some_and(|session| { | 2813 | + trimmed_clip(clip, trim.edge, trim.to, still) |
| 2790 | - session | ||
| 2791 | - .sources() | ||
| 2792 | - .get(clip.source) | ||
| 2793 | - .is_some_and(|s| engine::is_image(&s.path)) | ||
| 2794 | - }) => | ||
| 2795 | - { | ||
| 2796 | - Clip { | ||
| 2797 | - in_frame: 0, | ||
| 2798 | - out_frame: clip.end().saturating_sub(trim.to).max(1), | ||
| 2799 | - start: trim.to, | ||
| 2800 | - ..clip | ||
| 2801 | - } | ||
| 2802 | - } | ||
| 2803 | - // The in-point follows the head, exactly as `Project::trim` moves | ||
| 2804 | - // it: what stays on screen plays what it always played. | ||
| 2805 | - Edge::Start => Clip { | ||
| 2806 | - in_frame: (i64::from(clip.in_frame) + i64::from(trim.to) - i64::from(clip.start)) | ||
| 2807 | - .clamp(0, i64::from(clip.out_frame - 1)) as u32, | ||
| 2808 | - start: trim.to, | ||
| 2809 | - ..clip | ||
| 2810 | - }, | ||
| 2811 | - Edge::End => Clip { | ||
| 2812 | - out_frame: clip.in_frame + trim.to.saturating_sub(clip.start).max(1), | ||
| 2813 | - ..clip | ||
| 2814 | - }, | ||
| 2815 | - } | ||
| 2816 | } | 2814 | } |
| 2817 | 2815 | ||
| 2818 | /// How long the timeline is *drawn* as: its own length, and while a tail is | 2816 | /// How long the timeline is *drawn* as: its own length, and while a tail is |
| @@ -2820,6 +2818,15 @@ impl Player { | |||
| 2820 | /// at the last frame has nowhere to put a pointer that means "longer", so | 2818 | /// at the last frame has nowhere to put a pointer that means "longer", so |
| 2821 | /// without this the last clip on the timeline could be pulled in and never | 2819 | /// without this the last clip on the timeline could be pulled in and never |
| 2822 | /// let back out. | 2820 | /// let back out. |
| 2821 | + /// | ||
| 2822 | + /// Scroll room only, now that a second is an absolute number of pixels | ||
| 2823 | + /// ([`Scale`]): the extra length loosens [`View::settled`]'s clamp, which is | ||
| 2824 | + /// where the pixels past the last frame come from, and moves no box by a | ||
| 2825 | + /// pixel. It is still the *only* headroom at the tail -- zoomed in against | ||
| 2826 | + /// the end, that clamp pins the bed's right edge to the duration and an | ||
| 2827 | + /// End-trim of the last clip would have nowhere to be dragged to. What it | ||
| 2828 | + /// must not do is be read as a length anyone is told: the timecode reads | ||
| 2829 | + /// `PlaybackSession::timeline_duration` for exactly that reason. | ||
| 2823 | fn drawn_duration(&self) -> f64 { | 2830 | fn drawn_duration(&self) -> f64 { |
| 2824 | let Some(session) = &self.session else { | 2831 | let Some(session) = &self.session else { |
| 2825 | return 0.; | 2832 | return 0.; |
| @@ -4044,8 +4051,13 @@ impl Render for Player { | |||
| 4044 | // the window: it starts on a clip or on a library row and ends over | 4051 | // the window: it starts on a clip or on a library row and ends over |
| 4045 | // a lane, and only an ancestor of both hears all of it. | 4052 | // a lane, and only an ancestor of both hears all of it. |
| 4046 | .on_drag_move(cx.listener(|this, event: &DragMoveEvent<ClipDrag>, _, cx| { | 4053 | .on_drag_move(cx.listener(|this, event: &DragMoveEvent<ClipDrag>, _, cx| { |
| 4047 | - let ClipDrag { lane, idx } = *event.drag(cx); | 4054 | + // The clip the payload named, wherever an edit mid-drag has |
| 4048 | - this.preview_drop(lane, idx, event.event.position.x, cx); | 4055 | + // since put it ([`Player::dragged`]): the line has to promise a |
| 4056 | + // landing for the take actually in the hand. | ||
| 4057 | + let drag = *event.drag(cx); | ||
| 4058 | + if let Some(idx) = this.dragged(&drag) { | ||
| 4059 | + this.preview_drop(drag.lane, idx, event.event.position.x, cx); | ||
| 4060 | + } | ||
| 4049 | })) | 4061 | })) |
| 4050 | .on_drag_move( | 4062 | .on_drag_move( |
| 4051 | cx.listener(|this, event: &DragMoveEvent<AssetDrag>, _, cx| { | 4063 | cx.listener(|this, event: &DragMoveEvent<AssetDrag>, _, cx| { |
| @@ -4908,10 +4920,20 @@ impl Player { | |||
| 4908 | .gap(px(12.)) | 4920 | .gap(px(12.)) |
| 4909 | // Fixed width and one line: changing digits must not push | 4921 | // Fixed width and one line: changing digits must not push |
| 4910 | // the row around, nor wrap and change its height. | 4922 | // the row around, nor wrap and change its height. |
| 4923 | + // The timeline's own length, never the drawn one: a tail | ||
| 4924 | + // being dragged inflates the bed to the room that edge has | ||
| 4925 | + // ([`Player::drawn_duration`]), and for a still that room is | ||
| 4926 | + // ten minutes -- a total that jumped to 10:00:00 the moment | ||
| 4927 | + // a picture's edge was pressed, and back on release. | ||
| 4911 | .child(div().flex_none().w(px(TIME_W)).truncate().child(format!( | 4928 | .child(div().flex_none().w(px(TIME_W)).truncate().child(format!( |
| 4912 | "{} / {}", | 4929 | "{} / {}", |
| 4913 | timecode(position, self.fps), | 4930 | timecode(position, self.fps), |
| 4914 | - timecode(duration, self.fps) | 4931 | + timecode( |
| 4932 | + self.session | ||
| 4933 | + .as_ref() | ||
| 4934 | + .map_or(0., PlaybackSession::timeline_duration), | ||
| 4935 | + self.fps | ||
| 4936 | + ) | ||
| 4915 | ))) | 4937 | ))) |
| 4916 | .child( | 4938 | .child( |
| 4917 | div() | 4939 | div() |
| @@ -7144,6 +7166,11 @@ impl Player { | |||
| 7144 | // it, so all of them move together when it does. No bed width is needed | 7166 | // it, so all of them move together when it does. No bed width is needed |
| 7145 | // to place them any more -- a second is so many pixels wherever it is. | 7167 | // to place them any more -- a second is so many pixels wherever it is. |
| 7146 | let scale = self.scale; | 7168 | let scale = self.scale; |
| 7169 | + // How much bed there is to be seen on, measured off the ruler's own | ||
| 7170 | + // probe like every other question about it: what a box draws *inside* | ||
| 7171 | + // itself is clipped to this ([`visible_slice`]), because a box at a deep | ||
| 7172 | + // zoom is far wider than the strip it is being watched through. | ||
| 7173 | + let bed = f32::from(self.ruler.get().size.width); | ||
| 7147 | // Where the snap line stands, in the same pixels every box is placed | 7174 | // Where the snap line stands, in the same pixels every box is placed |
| 7148 | // through -- and only while a gesture is actually live: gpui drops a | 7175 | // through -- and only while a gesture is actually live: gpui drops a |
| 7149 | // drag without telling anyone, so this asks whether one is in flight | 7176 | // drag without telling anyone, so this asks whether one is in flight |
| @@ -7312,10 +7339,21 @@ impl Player { | |||
| 7312 | // to -- its own included, which is the drag that moves a | 7339 | // to -- its own included, which is the drag that moves a |
| 7313 | // take along its track. | 7340 | // take along its track. |
| 7314 | .on_drop(cx.listener(move |this, drag: &ClipDrag, window, cx| { | 7341 | .on_drop(cx.listener(move |this, drag: &ClipDrag, window, cx| { |
| 7315 | - this.move_clip(drag.lane, drag.idx, lane, window.mouse_position().x, cx) | 7342 | + // Against the lane as it is *now* ([`Player::dragged`]), |
| 7343 | + // and then snapped by `move_clip` like any other drop: | ||
| 7344 | + // which clip is being moved and where it lands are two | ||
| 7345 | + // questions, and this is the first one. | ||
| 7346 | + let Some(idx) = this.dragged(drag) else { | ||
| 7347 | + return; | ||
| 7348 | + }; | ||
| 7349 | + this.move_clip(drag.lane, idx, lane, window.mouse_position().x, cx) | ||
| 7316 | })) | 7350 | })) |
| 7317 | .drag_over::<ClipDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM))) | 7351 | .drag_over::<ClipDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM))) |
| 7318 | .children(clips.iter().enumerate().map(|(i, clip)| { | 7352 | .children(clips.iter().enumerate().map(|(i, clip)| { |
| 7353 | + // The clip as the lane holds it, for the drag payload: | ||
| 7354 | + // what a drop looks itself up by has to be the placed | ||
| 7355 | + // clip, never the preview an edge drag is drawing. | ||
| 7356 | + let placed = *clip; | ||
| 7319 | // What a drag on an edge is showing, which is the clip | 7357 | // What a drag on an edge is showing, which is the clip |
| 7320 | // itself while nothing is being dragged. | 7358 | // itself while nothing is being dragged. |
| 7321 | let clip = &self.trimmed(lane, i, *clip); | 7359 | let clip = &self.trimmed(lane, i, *clip); |
| @@ -7341,15 +7379,30 @@ impl Player { | |||
| 7341 | .unwrap_or(clip.source), | 7379 | .unwrap_or(clip.source), |
| 7342 | ); | 7380 | ); |
| 7343 | let width = scale.width_px(len); | 7381 | let width = scale.width_px(len); |
| 7382 | + let left = scale.px_at(start); | ||
| 7383 | + // The slice of this box that is on the bed: where its | ||
| 7384 | + // name, its badge and its waveform go, so none of the | ||
| 7385 | + // three is drawn out at a zoomed-in box's own edges. | ||
| 7386 | + let (vis_x, vis_w) = visible_slice(left, width, bed); | ||
| 7344 | let label = sources.get(clip.source).map(|s| file_name(&s.path)); | 7387 | let label = sources.get(clip.source).map(|s| file_name(&s.path)); |
| 7345 | let wave = sources | 7388 | let wave = sources |
| 7346 | .get(clip.source) | 7389 | .get(clip.source) |
| 7347 | .and_then(|s| self.waves.get(&(s.path.clone(), s.audio_stream))) | 7390 | .and_then(|s| self.waves.get(&(s.path.clone(), s.audio_stream))) |
| 7348 | .cloned(); | 7391 | .cloned(); |
| 7349 | - let (from, to) = ( | 7392 | + // The source seconds that slice plays -- not the clip's |
| 7350 | - f64::from(clip.in_frame) / self.fps, | 7393 | + // whole range: the envelope is drawn for the part of the |
| 7351 | - f64::from(clip.out_frame) / self.fps, | 7394 | + // box that can be seen, at the resolution of the pixels |
| 7352 | - ); | 7395 | + // it actually has, and never one column per two pixels |
| 7396 | + // of a box millions of pixels wide. | ||
| 7397 | + let along = |x: f32| match width > 0. { | ||
| 7398 | + true => { | ||
| 7399 | + f64::from(clip.in_frame) | ||
| 7400 | + + f64::from(clip.out_frame - clip.in_frame) | ||
| 7401 | + * f64::from(x / width) | ||
| 7402 | + } | ||
| 7403 | + false => f64::from(clip.in_frame), | ||
| 7404 | + }; | ||
| 7405 | + let (from, to) = (along(vis_x) / self.fps, along(vis_x + vis_w) / self.fps); | ||
| 7353 | let tip = tip.clone(); | 7406 | let tip = tip.clone(); |
| 7354 | // What the pointer carries on the way to another lane: | 7407 | // What the pointer carries on the way to another lane: |
| 7355 | // the file the box is showing, the same ghost a library | 7408 | // the file the box is showing, the same ghost a library |
| @@ -7372,7 +7425,7 @@ impl Player { | |||
| 7372 | // off the left edge: the bed clips what hangs out | 7425 | // off the left edge: the bed clips what hangs out |
| 7373 | // of it, so a half-visible clip is drawn as the | 7426 | // of it, so a half-visible clip is drawn as the |
| 7374 | // half of itself that is on screen. | 7427 | // half of itself that is on screen. |
| 7375 | - .left(px(scale.px_at(start))) | 7428 | + .left(px(left)) |
| 7376 | .w(px(width)) | 7429 | .w(px(width)) |
| 7377 | .overflow_hidden() | 7430 | .overflow_hidden() |
| 7378 | .rounded(px(3.)) | 7431 | .rounded(px(3.)) |
| @@ -7393,9 +7446,14 @@ impl Player { | |||
| 7393 | // starts the drag still selects, so picking a clip | 7446 | // starts the drag still selects, so picking a clip |
| 7394 | // up and putting it back down where it was is | 7447 | // up and putting it back down where it was is |
| 7395 | // exactly a click. | 7448 | // exactly a click. |
| 7396 | - .on_drag(ClipDrag { lane, idx: i }, move |_, _, _, cx| { | 7449 | + .on_drag( |
| 7397 | - cx.new(|_| Tip(ghost.clone())) | 7450 | + ClipDrag { |
| 7398 | - }) | 7451 | + lane, |
| 7452 | + idx: i, | ||
| 7453 | + clip: placed, | ||
| 7454 | + }, | ||
| 7455 | + move |_, _, _, cx| cx.new(|_| Tip(ghost.clone())), | ||
| 7456 | + ) | ||
| 7399 | .on_mouse_down( | 7457 | .on_mouse_down( |
| 7400 | MouseButton::Left, | 7458 | MouseButton::Left, |
| 7401 | cx.listener(move |this, event: &MouseDownEvent, _, cx| { | 7459 | cx.listener(move |this, event: &MouseDownEvent, _, cx| { |
| @@ -7454,7 +7512,7 @@ impl Player { | |||
| 7454 | zone | 7512 | zone |
| 7455 | })) | 7513 | })) |
| 7456 | // Under the label row, never through it. | 7514 | // Under the label row, never through it. |
| 7457 | - .children(wave.filter(|_| audio).and_then(|wave| { | 7515 | + .children(wave.filter(|_| audio && vis_w > 0.).and_then(|wave| { |
| 7458 | let inner: AnyElement = match wave { | 7516 | let inner: AnyElement = match wave { |
| 7459 | Wave::Peaks(peaks) => { | 7517 | Wave::Peaks(peaks) => { |
| 7460 | waveform(peaks, from, to).into_any_element() | 7518 | waveform(peaks, from, to).into_any_element() |
| @@ -7490,8 +7548,8 @@ impl Player { | |||
| 7490 | Some( | 7548 | Some( |
| 7491 | div() | 7549 | div() |
| 7492 | .absolute() | 7550 | .absolute() |
| 7493 | - .left_0() | 7551 | + .left(px(vis_x)) |
| 7494 | - .right_0() | 7552 | + .w(px(vis_w)) |
| 7495 | .top(px(LABEL_H)) | 7553 | .top(px(LABEL_H)) |
| 7496 | .bottom_0() | 7554 | .bottom_0() |
| 7497 | .child(inner), | 7555 | .child(inner), |
| @@ -7502,24 +7560,44 @@ impl Player { | |||
| 7502 | // cannot say whether a short clip is a trim or a | 7560 | // cannot say whether a short clip is a trim or a |
| 7503 | // clip at 4x, and that is the difference between a | 7561 | // clip at 4x, and that is the difference between a |
| 7504 | // cut and a re-time. | 7562 | // cut and a re-time. |
| 7505 | - .when(!clip.speed.is_normal(), |d| { | 7563 | + // Against the right edge of what is *visible* of the |
| 7564 | + // box, not of the box: zoomed in, the box's own | ||
| 7565 | + // right edge is off the screen and the badge with | ||
| 7566 | + // it, which is a clip that stops saying it is | ||
| 7567 | + // speeded exactly when it is being looked at | ||
| 7568 | + // closely. | ||
| 7569 | + .when(!clip.speed.is_normal() && vis_w > 0., |d| { | ||
| 7506 | d.child( | 7570 | d.child( |
| 7507 | div() | 7571 | div() |
| 7508 | .absolute() | 7572 | .absolute() |
| 7509 | .top_0() | 7573 | .top_0() |
| 7510 | - .right_0() | 7574 | + .left(px(vis_x)) |
| 7511 | - .px(px(3.)) | 7575 | + .w(px(vis_w)) |
| 7512 | - .rounded(px(3.)) | 7576 | + .flex() |
| 7513 | - .bg(rgb(ACCENT)) | 7577 | + .justify_end() |
| 7514 | - .text_size(px(9.)) | 7578 | + .overflow_hidden() |
| 7515 | - .text_color(rgb(SURFACE)) | 7579 | + .child( |
| 7516 | - .child(format!("{}", clip.speed)), | 7580 | + div() |
| 7581 | + .flex_none() | ||
| 7582 | + .px(px(3.)) | ||
| 7583 | + .rounded(px(3.)) | ||
| 7584 | + .bg(rgb(ACCENT)) | ||
| 7585 | + .text_size(px(9.)) | ||
| 7586 | + .text_color(rgb(SURFACE)) | ||
| 7587 | + .child(format!("{}", clip.speed)), | ||
| 7588 | + ), | ||
| 7517 | ) | 7589 | ) |
| 7518 | }) | 7590 | }) |
| 7519 | - .when_some(label.filter(|_| show_label(width)), |d, label| { | 7591 | + // ...and the name sits at the left edge of the same |
| 7592 | + // slice, for the same reason: a box scrolled half | ||
| 7593 | + // off names itself on the half that is on screen. | ||
| 7594 | + .when_some(label.filter(|_| show_label(vis_w)), |d, label| { | ||
| 7520 | d.child( | 7595 | d.child( |
| 7521 | div() | 7596 | div() |
| 7522 | - .relative() | 7597 | + .absolute() |
| 7598 | + .top_0() | ||
| 7599 | + .left(px(vis_x)) | ||
| 7600 | + .w(px(vis_w)) | ||
| 7523 | .h(px(LABEL_H)) | 7601 | .h(px(LABEL_H)) |
| 7524 | .px(px(4.)) | 7602 | .px(px(4.)) |
| 7525 | .truncate() | 7603 | .truncate() |
| @@ -9072,6 +9150,97 @@ fn show_label(w: f32) -> bool { | |||
| 9072 | w >= LABEL_MIN_W | 9150 | w >= LABEL_MIN_W |
| 9073 | } | 9151 | } |
| 9074 | 9152 | ||
| 9153 | +/// The clip a trim is *showing*, worked out the way `Project::trim` will write | ||
| 9154 | +/// it: the timeline room the edge leaves is turned into source frames by the one | ||
| 9155 | +/// conversion that exists for it ([`Speed::fit`]), and the box is drawn from | ||
| 9156 | +/// that. The preview and the commit are then the same arithmetic -- a box let go | ||
| 9157 | +/// of stays where the hand left it at every rate. Assigning the timeline count | ||
| 9158 | +/// straight to the source field, as this used to, drew a speeded clip's tail | ||
| 9159 | +/// moving at the wrong rate (it snapped on release) and drew a *head* trim | ||
| 9160 | +/// moving the clip's other edge, since the length it implied was not the length | ||
| 9161 | +/// the release would commit. | ||
| 9162 | +/// | ||
| 9163 | +/// A still grows forward from source frame 0 instead: every frame of it is the | ||
| 9164 | +/// same picture, so there is no earlier one for an in-point to walk back to. | ||
| 9165 | +/// | ||
| 9166 | +/// Room too narrow to hold one source frame is the edit the engine refuses, and | ||
| 9167 | +/// the box is drawn unchanged rather than as something that will not be | ||
| 9168 | +/// committed. | ||
| 9169 | +fn trimmed_clip(clip: Clip, edge: Edge, to: u32, still: bool) -> Clip { | ||
| 9170 | + // An edge that has not moved is not an edit, and `Project::trim` refuses it | ||
| 9171 | + // as one: the press that starts a drag must draw the clip it pressed, not a | ||
| 9172 | + // clip a rounding narrower. | ||
| 9173 | + if to | ||
| 9174 | + == match edge { | ||
| 9175 | + Edge::Start => clip.start, | ||
| 9176 | + Edge::End => clip.end(), | ||
| 9177 | + } | ||
| 9178 | + { | ||
| 9179 | + return clip; | ||
| 9180 | + } | ||
| 9181 | + match edge { | ||
| 9182 | + Edge::Start => { | ||
| 9183 | + // What survives is measured from the *end* -- the frames that stay | ||
| 9184 | + // play what they always played, which is what makes this a trim. | ||
| 9185 | + let Some(keep) = clip.speed.fit(clip.end().saturating_sub(to)) else { | ||
| 9186 | + return clip; | ||
| 9187 | + }; | ||
| 9188 | + match still { | ||
| 9189 | + true => Clip { | ||
| 9190 | + in_frame: 0, | ||
| 9191 | + out_frame: keep, | ||
| 9192 | + start: to, | ||
| 9193 | + ..clip | ||
| 9194 | + }, | ||
| 9195 | + false => Clip { | ||
| 9196 | + in_frame: clip.out_frame - keep.min(clip.out_frame), | ||
| 9197 | + start: to, | ||
| 9198 | + ..clip | ||
| 9199 | + }, | ||
| 9200 | + } | ||
| 9201 | + } | ||
| 9202 | + Edge::End => match clip.speed.fit(to.saturating_sub(clip.start)) { | ||
| 9203 | + Some(keep) => Clip { | ||
| 9204 | + out_frame: clip.in_frame + keep, | ||
| 9205 | + ..clip | ||
| 9206 | + }, | ||
| 9207 | + None => clip, | ||
| 9208 | + }, | ||
| 9209 | + } | ||
| 9210 | +} | ||
| 9211 | + | ||
| 9212 | +/// Which index on its own lane a dragged clip is at *now*: the one it was picked | ||
| 9213 | +/// up at while nothing has moved, and wherever the clip itself has slid to when | ||
| 9214 | +/// an edit during the drag rippled the lane's indices -- a delete, an undo or a | ||
| 9215 | +/// paste from a stroke, none of which gpui's frozen drag payload hears about. | ||
| 9216 | +/// `None` when the clip is gone altogether, and then the drop is not an edit at | ||
| 9217 | +/// all: moving whatever slid into its place is the one thing the hand did not | ||
| 9218 | +/// ask for. A lane's clips are disjoint and sorted, so at most one of them can | ||
| 9219 | +/// be the clip that was picked up. | ||
| 9220 | +fn live_idx(clips: &[Clip], idx: usize, clip: Clip) -> Option<usize> { | ||
| 9221 | + match clips.get(idx) { | ||
| 9222 | + Some(&at) if at == clip => Some(idx), | ||
| 9223 | + _ => clips.iter().position(|&c| c == clip), | ||
| 9224 | + } | ||
| 9225 | +} | ||
| 9226 | + | ||
| 9227 | +/// The part of a clip's box that is on the bed, in the box's own pixels: | ||
| 9228 | +/// `(left, width)` of its intersection with the visible strip. Everything drawn | ||
| 9229 | +/// *inside* a box -- its waveform, its name, its speed badge -- is placed in | ||
| 9230 | +/// here rather than at the box's own edges, which at a deep zoom sit thousands | ||
| 9231 | +/// of pixels off either side of the screen: a label out there is a label nobody | ||
| 9232 | +/// can read, and a waveform out there is a path with a point per two pixels of a | ||
| 9233 | +/// width nobody can see. A bed that has not been measured yet answers with the | ||
| 9234 | +/// whole box, which is what was drawn before there was a bed to clip to. | ||
| 9235 | +fn visible_slice(left: f32, width: f32, bed: f32) -> (f32, f32) { | ||
| 9236 | + if bed <= 0. { | ||
| 9237 | + return (0., width); | ||
| 9238 | + } | ||
| 9239 | + let from = (-left).clamp(0., width); | ||
| 9240 | + let to = (bed - left).clamp(from, width); | ||
| 9241 | + (from, to - from) | ||
| 9242 | +} | ||
| 9243 | + | ||
| 9075 | /// Scales an envelope to its own loudest sample, so a quietly mastered source | 9244 | /// Scales an envelope to its own loudest sample, so a quietly mastered source |
| 9076 | /// still draws as a shape. The fixtures peak around an eighth of full scale, and | 9245 | /// still draws as a shape. The fixtures peak around an eighth of full scale, and |
| 9077 | /// an eighth of a 30 px lane is a flat line -- which says "silent" about a file | 9246 | /// an eighth of a 30 px lane is a flat line -- which says "silent" about a file |
| @@ -9096,7 +9265,7 @@ fn envelope(peaks: &[(f32, f32)], from: f64, to: f64, w: f32, h: f32) -> Vec<(f3 | |||
| 9096 | if peaks.is_empty() || w <= 0. || h <= 0. { | 9265 | if peaks.is_empty() || w <= 0. || h <= 0. { |
| 9097 | return Vec::new(); | 9266 | return Vec::new(); |
| 9098 | } | 9267 | } |
| 9099 | - let cols = (w / WAVE_COL).ceil().max(1.) as usize; | 9268 | + let cols = ((w / WAVE_COL).ceil().max(1.) as usize).min(WAVE_COLS_MAX); |
| 9100 | let mid = h / 2.; | 9269 | let mid = h / 2.; |
| 9101 | (0..=cols) | 9270 | (0..=cols) |
| 9102 | .map(|col| { | 9271 | .map(|col| { |
| @@ -9761,10 +9930,10 @@ mod tests { | |||
| 9761 | LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_PAD, MENU_ROW_H, | 9930 | LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_PAD, MENU_ROW_H, |
| 9762 | MENU_ROWS_H, MENU_W, NO_FILE, PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, | 9931 | MENU_ROWS_H, MENU_W, NO_FILE, PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, |
| 9763 | SOURCE_TINTS, SPEED_PRESETS, SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, | 9932 | SOURCE_TINTS, SPEED_PRESETS, SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, |
| 9764 | - Transport, VOLUME_W, Volume, WAVE_BPS, WAVE_COL, Wave, band_label, bitrate_refusal, | 9933 | + Transport, VOLUME_W, Volume, WAVE_BPS, WAVE_COL, WAVE_COLS_MAX, Wave, band_label, |
| 9765 | - can_add, cancels_export, clipboard_after_remove, color_snap, containers, enable, envelope, | 9934 | + bitrate_refusal, can_add, cancels_export, clipboard_after_remove, color_snap, containers, |
| 9766 | - eq_card_w, eq_freq, eq_freq_label, eq_spectrum, eq_x, eq_y, estimated_mb, export_path, | 9935 | + enable, envelope, eq_card_w, eq_freq, eq_freq_label, eq_spectrum, eq_x, eq_y, |
| 9767 | - export_settings, format_key, | 9936 | + estimated_mb, export_path, export_settings, format_key, |
| 9768 | format_line, format_refusal, fps_label, frac_along, frac_down, frame_at, histogram, | 9937 | format_line, format_refusal, fps_label, frac_along, frac_down, frame_at, histogram, |
| 9769 | inserted_band, is_bare_modifier, is_project, keymap, keys_rows, lanes_h, marked, menu_at, | 9938 | inserted_band, is_bare_modifier, is_project, keymap, keys_rows, lanes_h, marked, menu_at, |
| 9770 | next_container, | 9939 | next_container, |
| @@ -9774,8 +9943,9 @@ mod tests { | |||
| 9774 | whole_take, window_title, | 9943 | whole_take, window_title, |
| 9775 | }; | 9944 | }; |
| 9776 | use super::{ | 9945 | use super::{ |
| 9777 | - LaneKind, PPS_DEFAULT, Repeat, Scale, View, ZOOM_MAX_SECONDS, ZOOM_MIN_FRAMES, ZOOM_STEP, | 9946 | + Edge, LaneKind, PPS_DEFAULT, Repeat, Scale, View, ZOOM_MAX_SECONDS, ZOOM_MIN_FRAMES, |
| 9778 | - file_name, file_uri, library_rows, px_along, repeats, span_label, unscannable, | 9947 | + ZOOM_STEP, file_name, file_uri, library_rows, live_idx, px_along, repeats, span_label, |
| 9948 | + trimmed_clip, unscannable, visible_slice, | ||
| 9779 | }; | 9949 | }; |
| 9780 | 9950 | ||
| 9781 | /// What the file manager is handed: the parts a path keeps as they are, and | 9951 | /// What the file manager is handed: the parts a path keeps as they are, and |
| @@ -12735,6 +12905,141 @@ mod tests { | |||
| 12735 | assert!(!envelope(&peaks, 0., 99., w, h).is_empty()); | 12905 | assert!(!envelope(&peaks, 0., 99., w, h).is_empty()); |
| 12736 | } | 12906 | } |
| 12737 | 12907 | ||
| 12908 | + /// A box laid out wider than any screen -- a long clip at a deep zoom -- is | ||
| 12909 | + /// still one screen's worth of columns: the path a repaint has to build is | ||
| 12910 | + /// bounded by what can be seen, not by what the layout says the box is. | ||
| 12911 | + /// Unbounded, a 5 s clip zoomed to the frame is a path of millions of points | ||
| 12912 | + /// per frame, and the repaint that stalls on it is the waveform that | ||
| 12913 | + /// "disappeared". | ||
| 12914 | + #[test] | ||
| 12915 | + fn an_envelope_never_costs_more_points_than_a_screen_can_show() { | ||
| 12916 | + let peaks: Vec<(f32, f32)> = (0..200).map(|i| (-(i as f32) / 199., 1.)).collect(); | ||
| 12917 | + // The width a 5 s clip is laid out at when the bed shows 8 frames of it. | ||
| 12918 | + let huge = 5. * 30. / 8. * 1200.; | ||
| 12919 | + let cols = envelope(&peaks, 0., 5., huge, 30.); | ||
| 12920 | + assert!( | ||
| 12921 | + cols.len() <= WAVE_COLS_MAX + 1, | ||
| 12922 | + "{} columns for a {huge} px box", | ||
| 12923 | + cols.len() | ||
| 12924 | + ); | ||
| 12925 | + // ...and the slice actually painted is the part of the box on the bed, | ||
| 12926 | + // which is where that width stops mattering: a column per two visible | ||
| 12927 | + // pixels, at every zoom. | ||
| 12928 | + let (x, w) = visible_slice(-huge / 2., huge, 1200.); | ||
| 12929 | + assert_eq!((x, w), (huge / 2., 1200.)); | ||
| 12930 | + assert_eq!(envelope(&peaks, 0., 5., w, 30.).len(), 601); | ||
| 12931 | + // A box entirely off the bed has no slice, and one that has never been | ||
| 12932 | + // measured is drawn whole -- what was drawn before there was a bed. | ||
| 12933 | + assert_eq!(visible_slice(2000., 500., 1200.), (0., 0.)); | ||
| 12934 | + assert_eq!(visible_slice(-3000., 500., 1200.), (500., 0.)); | ||
| 12935 | + assert_eq!(visible_slice(-40., 500., 0.), (0., 500.)); | ||
| 12936 | + // Half on, at either edge. | ||
| 12937 | + assert_eq!(visible_slice(-100., 500., 1200.), (100., 400.)); | ||
| 12938 | + assert_eq!(visible_slice(1000., 500., 1200.), (0., 200.)); | ||
| 12939 | + } | ||
| 12940 | + | ||
| 12941 | + /// The box a trim draws is the box its release commits, at every speed. The | ||
| 12942 | + /// preview used to hand the *timeline* frame count to a source-frame field: | ||
| 12943 | + /// at 2x a tail moved twice as fast as the pointer and snapped back on | ||
| 12944 | + /// release, and a head drag moved the clip's other edge. | ||
| 12945 | + #[test] | ||
| 12946 | + fn a_trim_preview_lands_where_the_release_commits() { | ||
| 12947 | + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); | ||
| 12948 | + session.set_gain(0.0); | ||
| 12949 | + for permille in SPEED_PRESETS { | ||
| 12950 | + // Live, so the loop owes it no undo step: the speeds are the axis | ||
| 12951 | + // this walks, and the trims below are what is undone. | ||
| 12952 | + session | ||
| 12953 | + .set_speed_live(Lane::V1, 0, Speed::from_permille(permille)) | ||
| 12954 | + .expect("a clip alone on its lane may be speeded"); | ||
| 12955 | + for edge in [Edge::Start, Edge::End] { | ||
| 12956 | + let clip = session.lane_clips(Lane::V1)[0]; | ||
| 12957 | + let (lo, hi) = session | ||
| 12958 | + .trim_room(Lane::V1, 0, edge) | ||
| 12959 | + .expect("clip 0 is there"); | ||
| 12960 | + // Both walls and the middle of the room: the whole range a | ||
| 12961 | + // pointer can be clamped to. | ||
| 12962 | + for to in [lo, (lo + hi) / 2, hi] { | ||
| 12963 | + let preview = trimmed_clip(clip, edge, to, false); | ||
| 12964 | + // The drag is one edit and one undo step, so the next `to` | ||
| 12965 | + // is measured from the same clip this one was. | ||
| 12966 | + if session.trim_clip(Lane::V1, 0, edge, to) { | ||
| 12967 | + assert_eq!( | ||
| 12968 | + preview, | ||
| 12969 | + session.lane_clips(Lane::V1)[0], | ||
| 12970 | + "{edge:?} to {to} at {permille} per mille" | ||
| 12971 | + ); | ||
| 12972 | + assert!(session.undo(), "the trim is one undo step"); | ||
| 12973 | + } else { | ||
| 12974 | + // An edge already where it was asked to go is not an | ||
| 12975 | + // edit, and the preview draws the clip unchanged. | ||
| 12976 | + assert_eq!(preview, clip, "{edge:?} to {to} at {permille} per mille"); | ||
| 12977 | + } | ||
| 12978 | + assert_eq!(session.lane_clips(Lane::V1)[0], clip, "back where it was"); | ||
| 12979 | + } | ||
| 12980 | + } | ||
| 12981 | + } | ||
| 12982 | + } | ||
| 12983 | + | ||
| 12984 | + /// A still trims the same way, and the preview knows it: its head grows | ||
| 12985 | + /// forward from source frame 0 -- every frame of it is the same picture -- | ||
| 12986 | + /// so the box stretches instead of sliding left. | ||
| 12987 | + #[test] | ||
| 12988 | + fn a_stills_trim_preview_grows_forward_like_the_commit() { | ||
| 12989 | + let mut session = PlaybackSession::open(asset("test_still.png")).expect("a picture opens"); | ||
| 12990 | + for edge in [Edge::Start, Edge::End] { | ||
| 12991 | + let clip = session.lane_clips(Lane::V1)[0]; | ||
| 12992 | + let (lo, hi) = session | ||
| 12993 | + .trim_room(Lane::V1, 0, edge) | ||
| 12994 | + .expect("clip 0 is there"); | ||
| 12995 | + for to in [lo, (lo + hi) / 2, hi] { | ||
| 12996 | + let preview = trimmed_clip(clip, edge, to, true); | ||
| 12997 | + match session.trim_clip(Lane::V1, 0, edge, to) { | ||
| 12998 | + true => { | ||
| 12999 | + assert_eq!( | ||
| 13000 | + preview, | ||
| 13001 | + session.lane_clips(Lane::V1)[0], | ||
| 13002 | + "a still {edge:?} to {to}" | ||
| 13003 | + ); | ||
| 13004 | + assert!(session.undo(), "the trim is one undo step"); | ||
| 13005 | + } | ||
| 13006 | + false => assert_eq!(preview, clip, "a still {edge:?} to {to}"), | ||
| 13007 | + } | ||
| 13008 | + assert_eq!(session.lane_clips(Lane::V1)[0], clip, "back where it was"); | ||
| 13009 | + } | ||
| 13010 | + } | ||
| 13011 | + } | ||
| 13012 | + | ||
| 13013 | + /// gpui freezes a drag's payload for the whole gesture, and nothing stops a | ||
| 13014 | + /// stroke from editing the lane under it: the drop has to find the clip that | ||
| 13015 | + /// was picked up, not whatever slid into its index. | ||
| 13016 | + #[test] | ||
| 13017 | + fn a_drop_moves_the_clip_that_was_picked_up_not_its_old_index() { | ||
| 13018 | + let at = |start: u32| Clip { | ||
| 13019 | + start, | ||
| 13020 | + in_frame: 0, | ||
| 13021 | + out_frame: 30, | ||
| 13022 | + source: 0, | ||
| 13023 | + link: None, | ||
| 13024 | + eq: None, | ||
| 13025 | + color: None, | ||
| 13026 | + fit: FitPolicy::Fit, | ||
| 13027 | + speed: Speed::NORMAL, | ||
| 13028 | + }; | ||
| 13029 | + let lane = [at(0), at(30), at(60)]; | ||
| 13030 | + let dragged = lane[2]; | ||
| 13031 | + assert_eq!(live_idx(&lane, 2, dragged), Some(2), "nothing moved"); | ||
| 13032 | + // A delete in front of it: the clip is now index 1, and the index the | ||
| 13033 | + // drag froze names a clip nobody grabbed. | ||
| 13034 | + let after = [at(0), at(60)]; | ||
| 13035 | + assert_eq!(live_idx(&after, 2, dragged), Some(1)); | ||
| 13036 | + assert_eq!(live_idx(&after, 1, dragged), Some(1)); | ||
| 13037 | + // Deleted mid-drag: there is nothing to move, and moving its neighbour | ||
| 13038 | + // instead is exactly the bug this exists for. | ||
| 13039 | + assert_eq!(live_idx(&[at(0)], 2, dragged), None); | ||
| 13040 | + assert_eq!(live_idx(&[], 0, dragged), None); | ||
| 13041 | + } | ||
| 13042 | + | ||
| 12738 | #[test] | 13043 | #[test] |
| 12739 | fn a_quiet_source_still_draws_as_a_shape() { | 13044 | fn a_quiet_source_still_draws_as_a_shape() { |
| 12740 | // An eighth of full scale, which is about where the fixtures sit. | 13045 | // An eighth of full scale, which is about where the fixtures sit. |