Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(desktop-gpui): repaint only the playhead views on a paused ruler scrub step

Richie McIlroy44e4b9420b4e
diff --git a/apps/desktop-gpui/src/editor_panels.rs b/apps/desktop-gpui/src/editor_panels.rs
index 8410fc8fc..e6825afee 100644
--- a/apps/desktop-gpui/src/editor_panels.rs
+++ b/apps/desktop-gpui/src/editor_panels.rs
@@ -1591,6 +1591,30 @@ pub fn camera3d_pose_seek_time(segment: &Camera3DSegment, end: bool, fps: u32) -
camera3d_frame_before(segment.end, fps).max(start_frame)
}
+/// "The playhead is on this pose" is a frame's tolerance, which is as close
+/// as a seek can land.
+fn camera3d_pose_on(playhead: f64, time: f64, fps: u32) -> bool {
+ (playhead - time).abs() < 1. / f64::from(fps.max(1))
+}
+
+/// Every `(shot, end)` pose the playhead is on: what the 3D panel's pose cards
+/// mark, so a scrub can tell when the sidebar has to redraw.
+pub(crate) fn camera3d_poses_at(
+ segments: &[Camera3DSegment],
+ playhead: f64,
+ fps: u32,
+) -> Vec<(usize, bool)> {
+ let mut poses = Vec::new();
+ for (index, segment) in segments.iter().enumerate() {
+ for end in [false, true] {
+ if camera3d_pose_on(playhead, camera3d_pose_seek_time(segment, end, fps), fps) {
+ poses.push((index, end));
+ }
+ }
+ }
+ poses
+}
+
/// How many shots the Auto scene picker offers.
pub const AUTO_CAMERA3D_MAX_SHOTS: usize = 6;
@@ -6846,9 +6870,7 @@ impl EditorWindow {
let fps = crate::editor_window::EDITOR_PREVIEW_FPS;
let start_time = camera3d_pose_seek_time(segment, false, fps);
let end_time = camera3d_pose_seek_time(segment, true, fps);
- // "The playhead is on this pose" is a frame's tolerance, which is as
- // close as a seek can land.
- let on = |time: f64| (self.playhead_time() - time).abs() < 1. / f64::from(fps.max(1));
+ let on = |time: f64| camera3d_pose_on(self.playhead_time(), time, fps);
div()
.flex()
@@ -8708,6 +8730,22 @@ mod tests {
);
}
+ #[test]
+ fn a_paused_scrub_sees_the_poses_the_playhead_lands_on() {
+ // A paused scrub redraws the sidebar only when this list changes, and
+ // the pose cards mark exactly these poses.
+ let fps = 60;
+ let shots = [camera3d(1., 2.5), camera3d(4.02, 6.)];
+ assert_eq!(camera3d_poses_at(&shots, 1., fps), [(0, false)]);
+ assert_eq!(camera3d_poses_at(&shots, 149. / 60., fps), [(0, true)]);
+ assert_eq!(camera3d_poses_at(&shots, 242. / 60., fps), [(1, false)]);
+ assert_eq!(camera3d_poses_at(&shots, 359. / 60., fps), [(1, true)]);
+ for playhead in [0., 1.75, 3., 5., 7.] {
+ assert!(camera3d_poses_at(&shots, playhead, fps).is_empty());
+ }
+ assert!(camera3d_poses_at(&[], 1., fps).is_empty());
+ }
+
#[test]
fn a_sequence_stays_inside_the_shot_it_replaces() {
// The panel replaces one shot with the whole chain, so the chain has
diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs
index 33916aa10..894652d9c 100644
--- a/apps/desktop-gpui/src/editor_window.rs
+++ b/apps/desktop-gpui/src/editor_window.rs
@@ -3199,11 +3199,53 @@ impl EditorWindow {
/// (`PlaybackHandle::seek`), so a scrub during playback holds the last
/// picture for one decode instead of paying a warmup per tick.
pub fn seek_to_time(&mut self, time: f64, cx: &mut Context<Self>) {
+ if self.apply_seek(time) {
+ cx.notify();
+ }
+ }
+
+ /// One pointer move of a ruler scrub. While paused, the playhead is read
+ /// by the timeline, the transport clock and what the window itself renders
+ /// (the canvas overlay, the clip popover), so this invalidates those the
+ /// way [`Self::invalidate_hover_chrome`] does instead of rebuilding the
+ /// header, toolbar and sidebar on every move. The one sidebar reader is
+ /// the 3D panel's "on the playhead" pose marker, which is only redrawn
+ /// when it changes. Playing and handoff seeks keep the full invalidation.
+ fn scrub_to_time(&mut self, time: f64, cx: &mut Context<Self>) {
+ let poses = self.camera3d_poses_at_playhead();
+ if !self.apply_seek(time) {
+ return;
+ }
+ if self.playing || self.ordinary_handoff_frame.is_some() {
+ cx.notify();
+ return;
+ }
+ self.invalidate_hover_chrome(cx);
+ if self.camera3d_poses_at_playhead() != poses {
+ self.sidebar_view.update(cx, |_, cx| cx.notify());
+ }
+ }
+
+ fn camera3d_poses_at_playhead(&self) -> Vec<(usize, bool)> {
+ crate::editor_panels::camera3d_poses_at(
+ self.project
+ .timeline
+ .as_ref()
+ .map(|timeline| timeline.camera3d_segments.as_slice())
+ .unwrap_or(&[]),
+ self.playhead_time(),
+ EDITOR_PREVIEW_FPS,
+ )
+ }
+
+ /// `seek_to_time` minus the invalidation. False when the seek went to the
+ /// preparing transport, which redraws through its own events.
+ fn apply_seek(&mut self, time: f64) -> bool {
// Moving the playhead by hand ends any shot audition.
self.play_until = None;
if self.preparing_transport_active() {
self.preparing_command(Some(time), None);
- return;
+ return false;
}
let Some(transport) = &self.transport else {
if let Some(target) = self
@@ -3213,7 +3255,7 @@ impl EditorWindow {
{
self.preparing_command(Some(target), None);
}
- return;
+ return false;
};
let time = time.clamp(0.0, self.total_duration());
// `Math.round(newTime * FPS)` -- "round to nearest frame to prevent
@@ -3245,7 +3287,7 @@ impl EditorWindow {
self.playhead = time;
self.view.playhead = time;
self.view.playing = self.playing;
- cx.notify();
+ true
}
/// The prev button (`Player.tsx:370-381`): stop, playhead to 0, and the
@@ -3916,7 +3958,7 @@ impl EditorWindow {
// supersedes is simply overwritten, and the preview renderer cancels
// in-flight work when it sees a newer instruction
// (`editor_instance.rs:563-575`).
- self.seek_to_time(time, cx);
+ self.scrub_to_time(time, cx);
}
/// The **timeline's own** mouseup, the one the source registers on