Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

#1091: Home / End and other jumps scroll the Timeline to the playhead

vangel76d4e4512e7073
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs
index d5766c0bb..40f21962a 100644
--- a/crates/ui-egui/src/panels/timeline.rs
+++ b/crates/ui-egui/src/panels/timeline.rs
@@ -232,6 +232,16 @@ pub fn max_scroll(sequence_seconds: f64, width: f32, pps: f64) -> f64 {
(timeline_extent(sequence_seconds) - width as f64 / pps).max(0.0)
}
+/// Where the left edge goes so the playhead at `ph` seconds shows in a view `width` points wide
+/// (the page-scroll margin from the left), or `None` when it is in view already.
+pub fn scroll_to_reveal(ph: f64, scroll: f64, pps: f64, width: f64) -> Option<f64> {
+ if !ph.is_finite() || !scroll.is_finite() || !(pps > 0.0 && pps.is_finite()) {
+ return None;
+ }
+ let px = (ph - scroll) * pps;
+ (px < 0.0 || px > width).then(|| (ph - width * 0.05 / pps).max(0.0))
+}
+
/// Zoom about a time (keeps it under the same screen x).
pub fn zoom_about(v: &mut TimelineView, factor: f64, anchor_secs: f64, width: f32) {
let old = v.target_pps;
@@ -323,6 +333,22 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
v.scroll = v.target_scroll;
}
}
+ // A jump while stopped (Home, End, edit points, markers) brings the playhead into view.
+ {
+ let v = &mut app.ui.timeline;
+ let ph = app.session.playhead().seconds();
+ // another sequence's tab has its own playhead and keeps its own view
+ let moved = v.seen_playhead.is_some_and(|(id, seen)| id == seq_id && seen != ph);
+ v.seen_playhead = Some((seq_id, ph));
+ if moved
+ && !app.playback.playing
+ && !v.fit_pending
+ && let Some(s) = scroll_to_reveal(ph, v.target_scroll, v.target_pps, content.width() as f64)
+ {
+ v.target_scroll = s;
+ v.scroll = s;
+ }
+ }
// The view stays inside the Timeline's extent, however it was moved (wheel, scroll bar, zoom,
// a script): scrolled past it, nothing is in view and the scroll bar has nowhere to show it.
{
@@ -2823,6 +2849,29 @@ mod trim_preview_tests {
}
}
+#[cfg(test)]
+mod reveal_tests {
+ use super::scroll_to_reveal;
+
+ #[test]
+ fn home_scrolls_back_to_the_start() {
+ assert_eq!(scroll_to_reveal(0.0, 120.0, 40.0, 800.0), Some(0.0));
+ }
+
+ #[test]
+ fn end_scrolls_forward_to_the_playhead() {
+ let s = scroll_to_reveal(300.0, 0.0, 40.0, 800.0).unwrap();
+ assert!((0.0..=300.0).contains(&s) && (300.0 - s) * 40.0 <= 800.0);
+ }
+
+ #[test]
+ fn a_visible_or_hostile_playhead_leaves_the_view() {
+ assert_eq!(scroll_to_reveal(5.0, 0.0, 40.0, 800.0), None);
+ assert_eq!(scroll_to_reveal(f64::NAN, 0.0, 40.0, 800.0), None);
+ assert_eq!(scroll_to_reveal(5.0, 0.0, 0.0, 800.0), None);
+ }
+}
+
#[cfg(test)]
mod waveform_tests {
use super::waveform_display_gain;
diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs
index 2f780e118..3a6f91842 100644
--- a/crates/ui-egui/src/state.rs
+++ b/crates/ui-egui/src/state.rs
@@ -197,6 +197,9 @@ pub struct TimelineView {
/// Follow playhead during playback (page scroll).
pub follow: bool,
pub fit_pending: bool,
+ /// The playhead the view last saw: a jump while stopped (Home, End, edit points) scrolls to it.
+ #[serde(skip)]
+ pub seen_playhead: Option<(filmcraft_project::ItemId, f64)>,
/// The zoom a fit gave an empty sequence (which fits its 1 s minimum): the first clip to arrive
/// fits again, unless the zoom was changed in between.
#[serde(skip)]
@@ -232,6 +235,7 @@ impl Default for TimelineView {
show_waveforms: true,
follow: true,
fit_pending: true,
+ seen_playhead: None,
fit_empty: None,
track_lanes: Default::default(),
}
diff --git a/crates/ui-egui/tests/timeline_scroll_ui.rs b/crates/ui-egui/tests/timeline_scroll_ui.rs
index af20a815f..c20a9c23d 100644
--- a/crates/ui-egui/tests/timeline_scroll_ui.rs
+++ b/crates/ui-egui/tests/timeline_scroll_ui.rs
@@ -285,3 +285,19 @@ fn continuous_video_thumbnails_ask_for_frames_across_each_clip() {
assert_eq!(v["ok"], json!(false), "{v}");
assert!(!d.app().ui.timeline.show_thumbnails);
}
+
+#[test]
+fn home_and_end_bring_the_playhead_into_view() {
+ let mut d = Driver::demo();
+ d.ok("ui.set", json!({"timeline": {"pps": 400.0, "scroll": 3.0}}));
+ d.frames(3);
+ d.exec("playhead.start", json!({}));
+ d.frames(3);
+ assert_eq!(d.app().ui.timeline.target_scroll, 0.0, "Home scrolls back to the start");
+ d.exec("playhead.end", json!({}));
+ d.frames(3);
+ let end = d.app().session.playhead().seconds();
+ let v = &d.app().ui.timeline;
+ let width = 1600.0 / v.pps;
+ assert!(v.target_scroll > 3.0 && v.target_scroll <= end && end - v.target_scroll < width, "End shows the playhead: scroll {} end {end}", v.target_scroll);
+}