Skip to content

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

Timeline: the wheel scrolls as in Premiere, and the view cannot be lost

justjohn123450bcfcfff162b
diff --git a/crates/engine/src/settings.rs b/crates/engine/src/settings.rs
index 88ceeae8c..c9a5d4307 100644
--- a/crates/engine/src/settings.rs
+++ b/crates/engine/src/settings.rs
@@ -975,7 +975,7 @@ static CATEGORIES: &[Category] = &[
f("timeline.audioTransitionDuration", "Audio Transition Default Duration", Kind::Duration { unit_key: "timeline.audioTransitionUnit" }, true),
f("timeline.stillImageDuration", "Still Image Default Duration", Kind::Duration { unit_key: "timeline.stillImageUnit" }, true),
f("timeline.autoScroll", "Timeline Playback Auto-Scrolling", Kind::Choice(AUTO_SCROLL), true),
- f("timeline.mouseScrolling", "Timeline Mouse Scrolling", Kind::Choice(MOUSE_SCROLL), false),
+ f("timeline.mouseScrolling", "Timeline Mouse Scrolling", Kind::Choice(MOUSE_SCROLL), true),
Row::Group(
"Default Audio Tracks",
&[
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs
index 5ca911b61..277945e36 100644
--- a/crates/ui-egui/src/panels/timeline.rs
+++ b/crates/ui-egui/src/panels/timeline.rs
@@ -103,7 +103,9 @@ pub enum Drag {
},
Divider,
ZoomBar {
+ /// Where the pointer was pressed, and the view's left edge (seconds) at that moment.
grab: f32,
+ start: f64,
mode: u8,
},
}
@@ -124,6 +126,8 @@ pub struct Layout {
pub rows: Vec<Row>,
pub pps: f64,
pub scroll: f64,
+ /// The y of the divider between the video tracks (above) and the audio tracks (below).
+ pub split_y: f32,
}
impl Layout {
@@ -138,6 +142,21 @@ impl Layout {
}
}
+/// How far the Timeline reaches, in seconds: ten minutes past the end of the sequence. Premiere
+/// Pro's Timeline does the same (on a 6 s sequence its view stops with 00:10:06 at the right
+/// edge). The view cannot be scrolled past it and the scroll bar spans it.
+pub fn timeline_extent(sequence_seconds: f64) -> f64 {
+ sequence_seconds.max(0.0) + 600.0
+}
+
+/// The latest time the left edge of a view `width` points wide can show at `pps` points a second.
+pub fn max_scroll(sequence_seconds: f64, width: f32, pps: f64) -> f64 {
+ if pps <= 0.0 || !pps.is_finite() {
+ return 0.0;
+ }
+ (timeline_extent(sequence_seconds) - width as f64 / 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;
@@ -229,6 +248,17 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
v.scroll = v.target_scroll;
}
}
+ // 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.
+ {
+ let v = &mut app.ui.timeline;
+ let seconds = seq.duration().seconds();
+ if !v.scroll.is_finite() || !v.target_scroll.is_finite() {
+ (v.scroll, v.target_scroll) = (0.0, 0.0);
+ }
+ v.scroll = v.scroll.clamp(0.0, max_scroll(seconds, content.width(), v.pps));
+ v.target_scroll = v.target_scroll.clamp(0.0, max_scroll(seconds, content.width(), v.target_pps));
+ }
let pps = app.ui.timeline.pps;
let scroll = app.ui.timeline.scroll;
@@ -261,7 +291,7 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
let top = audio_area.min.y + i as f32 * ah - app.ui.timeline.a_scroll;
rows.push(Row { track: tr.id, kind: TrackKind::Audio, index: i, rect: Rect::from_min_max(pos2(content.min.x, top), pos2(content.max.x, top + ah)) });
}
- let layout = Layout { content, ruler, rows: rows.clone(), pps, scroll };
+ let layout = Layout { content, ruler, rows: rows.clone(), pps, scroll, split_y };
app.tl.layout = Some(layout.clone());
// ---- backgrounds
@@ -405,7 +435,13 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
}
// ---- scroll bars
- zoom_scrollbar(app, ui, Rect::from_min_max(pos2(content.min.x, rect.max.y - SCROLLBAR_H + 2.0), pos2(content.max.x, rect.max.y - 2.0)), dur_s, &t);
+ zoom_scrollbar(
+ app,
+ ui,
+ Rect::from_min_max(pos2(content.min.x, rect.max.y - SCROLLBAR_H + 2.0), pos2(content.max.x, rect.max.y - 2.0)),
+ seq.duration().seconds(),
+ &t,
+ );
vertical_scrollbar(
ui,
Rect::from_min_max(pos2(content.max.x + 2.0, video_area.min.y), pos2(rect.max.x - 1.0, video_area.max.y)),
@@ -1109,15 +1145,32 @@ fn draw_top(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect, seq: &Sequenc
app.auto.add("timeline.ruler", ruler, "time ruler");
}
-fn zoom_scrollbar(app: &mut FilmcraftApp, ui: &mut egui::Ui, bar: Rect, dur_s: f64, t: &Tokens) {
+/// The narrowest the zoom scroll bar's thumb gets: room for its two handles and a part between
+/// them to grab.
+const MIN_THUMB_W: f32 = 34.0;
+
+/// Where the zoom scroll bar's thumb is: (left edge, width, seconds of scroll per point of thumb
+/// travel). The thumb is as wide as the share of the Timeline in view (never narrower than
+/// [`MIN_THUMB_W`]) and its travel covers the whole scroll range, so it reaches both ends of the
+/// bar and moves with the pointer when dragged.
+fn zoom_thumb(bar: Rect, seq_seconds: f64, scroll: f64, pps: f64) -> (f32, f32, f64) {
+ let vis = bar.width() as f64 / pps.max(1e-9);
+ let total = timeline_extent(seq_seconds).max(vis);
+ let w = ((vis / total) as f32 * bar.width()).clamp(MIN_THUMB_W.min(bar.width()), bar.width());
+ let travel = (bar.width() - w).max(0.0);
+ let range = (total - vis).max(0.0);
+ let at = if range > 0.0 { (scroll / range).clamp(0.0, 1.0) as f32 * travel } else { 0.0 };
+ (bar.min.x + at, w, if travel > 0.5 { range / travel as f64 } else { 0.0 })
+}
+
+fn zoom_scrollbar(app: &mut FilmcraftApp, ui: &mut egui::Ui, bar: Rect, seq_seconds: f64, t: &Tokens) {
let p = ui.painter();
p.rect_filled(bar, bar.height() / 2.0, t.separator);
let v = &mut app.ui.timeline;
- let total = (dur_s * 1.15).max(v.scroll + bar.width() as f64 / v.pps);
let vis = bar.width() as f64 / v.pps;
- let a = bar.min.x + (v.scroll / total) as f32 * bar.width();
- let b = bar.min.x + ((v.scroll + vis) / total).min(1.0) as f32 * bar.width();
- let thumb = Rect::from_min_max(pos2(a, bar.min.y), pos2(b.max(a + 20.0), bar.max.y));
+ let total = timeline_extent(seq_seconds).max(vis);
+ let (left, w, per_point) = zoom_thumb(bar, seq_seconds, v.scroll, v.pps);
+ let thumb = Rect::from_min_max(pos2(left, bar.min.y), pos2(left + w, bar.max.y));
let hover = ui.rect_contains_pointer(thumb);
p.rect_filled(thumb, thumb.height() / 2.0, if hover { Color32::from_rgb(0x6a, 0x6a, 0x6a) } else { Color32::from_rgb(0x4b, 0x4b, 0x4b) });
for x in [thumb.min.x + thumb.height() / 2.0, thumb.max.x - thumb.height() / 2.0] {
@@ -1125,40 +1178,64 @@ fn zoom_scrollbar(app: &mut FilmcraftApp, ui: &mut egui::Ui, bar: Rect, dur_s: f
p.circle_stroke(pos2(x, thumb.center().y), 4.5, Stroke::new(1.5, Color32::from_rgb(0xd1, 0xd1, 0xd1)));
}
app.auto.add("timeline.zoomBar", thumb, "zoom scroll bar");
- let resp = ui.interact(bar, egui::Id::new("tl-zoombar"), Sense::drag());
+ app.auto.add("timeline.zoomBar.track", bar, "zoom scroll bar track");
+ let resp = ui.interact(bar, egui::Id::new("tl-zoombar"), Sense::click_and_drag());
+ let range = (total - vis).max(0.0);
+ // a press beside the thumb brings the thumb's middle under the pointer (and a drag goes on
+ // from there)
+ let jump = |v: &mut crate::state::TimelineView, x: f32| {
+ if per_point > 0.0 {
+ v.target_scroll = ((x - bar.min.x - w / 2.0) as f64 * per_point).clamp(0.0, range);
+ v.scroll = v.target_scroll;
+ }
+ };
+ if resp.clicked()
+ && let Some(pos) = resp.interact_pointer_pos().filter(|pos| !thumb.contains(*pos))
+ {
+ jump(&mut app.ui.timeline, pos.x);
+ }
+ // (a drag is known to be one only after the pointer has moved: what was grabbed is what was
+ // under the pointer when the button went down)
if resp.drag_started()
- && let Some(pos) = resp.interact_pointer_pos()
+ && let Some(pos) = ui.input(|i| i.pointer.press_origin()).or(resp.interact_pointer_pos())
{
- let mode = if (pos.x - thumb.min.x).abs() < 9.0 {
+ let mode = if !thumb.expand2(vec2(0.0, 4.0)).contains(pos) {
+ jump(&mut app.ui.timeline, pos.x);
+ 0
+ } else if (pos.x - thumb.min.x).abs() < 9.0 {
1
} else if (pos.x - thumb.max.x).abs() < 9.0 {
2
} else {
0
};
- app.tl.drag = Some(Drag::ZoomBar { grab: pos.x, mode });
+ app.tl.drag = Some(Drag::ZoomBar { grab: pos.x, start: app.ui.timeline.target_scroll, mode });
}
- if let Some(Drag::ZoomBar { mode, .. }) = app.tl.drag.clone()
+ if let Some(Drag::ZoomBar { grab, start, mode }) = app.tl.drag.clone()
&& resp.dragged()
{
- let dx = resp.drag_delta().x as f64 / bar.width() as f64 * total;
let v = &mut app.ui.timeline;
match mode {
+ // the thumb's middle: scroll, the thumb staying under the pointer
0 => {
- v.target_scroll = (v.target_scroll + dx).max(0.0);
- v.scroll = v.target_scroll;
- }
- 1 => {
- let end = v.scroll + vis;
- let ns = (v.scroll + dx).clamp(0.0, end - 0.05);
- v.pps = bar.width() as f64 / (end - ns);
- v.target_pps = v.pps;
- v.scroll = ns;
- v.target_scroll = ns;
+ if let Some(pos) = resp.interact_pointer_pos() {
+ v.target_scroll = (start + (pos.x - grab) as f64 * per_point).clamp(0.0, range);
+ v.scroll = v.target_scroll;
+ }
}
+ // a handle: zoom, the other end of the view staying where it is
_ => {
- let ne = (v.scroll + vis + dx).max(v.scroll + 0.05);
- v.pps = bar.width() as f64 / (ne - v.scroll);
+ let dx = resp.drag_delta().x as f64 / bar.width().max(1.0) as f64 * total;
+ if mode == 1 {
+ let end = v.scroll + vis;
+ let ns = (v.scroll + dx).clamp(0.0, (end - 0.05).max(0.0));
+ v.pps = (bar.width() as f64 / (end - ns).max(0.05)).clamp(0.05, 24_000.0);
+ v.scroll = ns;
+ v.target_scroll = ns;
+ } else {
+ let ne = (v.scroll + vis + dx).max(v.scroll + 0.05);
+ v.pps = (bar.width() as f64 / (ne - v.scroll)).clamp(0.05, 24_000.0);
+ }
v.target_pps = v.pps;
}
}
@@ -1386,6 +1463,46 @@ fn shift_track(seq: &Sequence, tid: TrackId, delta: i32) -> Option<TrackId> {
Some(tid)
}
+/// What the wheel and the trackpad did this frame.
+struct WheelInput {
+ /// Scroll in points (wheel lines and pages converted), positive = content moves right / down.
+ delta: egui::Vec2,
+ /// Pinch factor (1 = none).
+ pinch: f32,
+ command: bool,
+ alt: bool,
+ shift: bool,
+}
+
+/// The wheel events of this frame, read from the events themselves: egui turns Cmd + wheel into a
+/// zoom and Shift + wheel into a sideways scroll before anyone asks, and the Timeline has its own
+/// meaning for both. The modifiers are those of the events (a script's wheel carries its own).
+fn wheel_input(ctx: &egui::Context) -> WheelInput {
+ ctx.input(|i| {
+ let mut w = WheelInput { delta: egui::Vec2::ZERO, pinch: 1.0, command: false, alt: false, shift: false };
+ for e in &i.events {
+ match e {
+ egui::Event::MouseWheel { unit, delta, modifiers, .. } => {
+ let points = match unit {
+ egui::MouseWheelUnit::Point => 1.0,
+ egui::MouseWheelUnit::Line => 40.0,
+ egui::MouseWheelUnit::Page => 400.0,
+ };
+ if delta.x.is_finite() && delta.y.is_finite() {
+ w.delta += *delta * points;
+ }
+ w.command |= modifiers.command || modifiers.mac_cmd || modifiers.ctrl;
+ w.alt |= modifiers.alt;
+ w.shift |= modifiers.shift;
+ }
+ egui::Event::Zoom(z) if z.is_finite() && *z > 0.0 => w.pinch *= *z,
+ _ => {}
+ }
+ }
+ w
+ })
+}
+
fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &Layout, rect: Rect) {
let ctx = ui.ctx().clone();
let area = Rect::from_min_max(pos2(layout.content.min.x, layout.ruler.min.y), layout.content.max);
@@ -1395,27 +1512,42 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
let tool = app.ui.tool;
let rate = seq.settings.frame_rate;
- // ---- wheel: horizontal scroll; Alt/Cmd+wheel zoom about cursor; Shift+wheel vertical
- if ui.rect_contains_pointer(rect) {
- let (scroll, zoom) = ctx.input(|i| (i.smooth_scroll_delta, i.zoom_delta()));
- if let Some(p) = ctx.pointer_hover_pos() {
- if (zoom - 1.0).abs() > 1e-4 || ((mods.alt || mods.command) && scroll.y.abs() > 0.0) {
- let f = if (zoom - 1.0).abs() > 1e-4 { zoom as f64 } else { (1.0 + scroll.y as f64 * 0.01).clamp(0.5, 2.0) };
- let anchor_t = layout.tick_at(p.x).seconds();
- let v = &mut app.ui.timeline;
- v.target_pps = (v.target_pps * f).clamp(0.05, 24_000.0);
- app.tl.zoom_anchor = Some((anchor_t, p.x));
- } else if mods.shift && scroll.y.abs() > 0.0 {
- if layout.rows.iter().find(|r| r.kind == TrackKind::Video).is_some_and(|r| p.y < r.rect.max.y + 200.0) && p.y < layout.content.center().y {
- app.ui.timeline.v_scroll -= scroll.y;
- } else {
- app.ui.timeline.a_scroll -= scroll.y;
+ // ---- wheel, as in Premiere Pro on macOS (checked in 26.5.2):
+ // wheel the tracks under the pointer, up and down
+ // Cmd + wheel the Timeline, sideways
+ // Option + wheel zoom about the pointer
+ // Settings ▸ Timeline ▸ Timeline Mouse Scrolling "Horizontal" swaps the first two. A sideways
+ // gesture (trackpad swipe, tilt wheel, Shift + wheel) always scrolls sideways; a pinch zooms.
+ if ui.rect_contains_pointer(rect)
+ && let Some(p) = ctx.pointer_hover_pos()
+ {
+ let w = wheel_input(&ctx);
+ if (w.pinch - 1.0).abs() > 1e-4 || (w.alt && w.delta.y != 0.0) {
+ let f = if (w.pinch - 1.0).abs() > 1e-4 { w.pinch as f64 } else { (1.0_f64 + w.delta.y as f64 * 0.01).clamp(0.5, 2.0) };
+ let anchor_t = layout.tick_at(p.x).seconds();
+ let v = &mut app.ui.timeline;
+ v.target_pps = (v.target_pps * f).clamp(0.05, 24_000.0);
+ app.tl.zoom_anchor = Some((anchor_t, p.x));
+ } else if w.delta != egui::Vec2::ZERO {
+ let wheel_sideways = (app.session.prefs.timeline.mouse_scrolling == "horizontal") != w.command;
+ let sideways = if w.delta.x.abs() > w.delta.y.abs() {
+ Some(w.delta.x)
+ } else if wheel_sideways || w.shift {
+ Some(w.delta.y)
+ } else {
+ None
+ };
+ match sideways {
+ Some(d) => {
+ let v = &mut app.ui.timeline;
+ let limit = max_scroll(seq.duration().seconds(), layout.content.width(), v.pps);
+ v.target_scroll = (v.target_scroll - d as f64 / v.pps).clamp(0.0, limit);
+ v.scroll = v.target_scroll;
}
- } else if scroll.x.abs() > 0.0 || scroll.y.abs() > 0.0 {
- let d = if scroll.x.abs() > scroll.y.abs() { scroll.x } else { scroll.y };
- let v = &mut app.ui.timeline;
- v.target_scroll = (v.target_scroll - d as f64 / v.pps).max(0.0);
- v.scroll = v.target_scroll;
+ // up and down: the video tracks above the divider, the audio tracks below it (the
+ // lowest video track is at the bottom, so wheeling up brings higher ones in)
+ None if p.y < layout.split_y => app.ui.timeline.v_scroll += w.delta.y,
+ None => app.ui.timeline.a_scroll -= w.delta.y,
}
}
}
diff --git a/crates/ui-egui/tests/timeline_scroll_ui.rs b/crates/ui-egui/tests/timeline_scroll_ui.rs
new file mode 100644
index 000000000..ef23d50f4
--- /dev/null
+++ b/crates/ui-egui/tests/timeline_scroll_ui.rs
@@ -0,0 +1,265 @@
+//! Headless UI tests of scrolling the Timeline: what the wheel does with each modifier, how far
+//! the view can go, and the zoom scroll bar. Premiere Pro 26.5.2 on macOS, checked in the app: the
+//! wheel moves the tracks up and down, Cmd + wheel moves the Timeline sideways, Option + wheel
+//! zooms, and the view stops ten minutes past the end of the sequence.
+
+use std::sync::mpsc::{Sender, channel};
+
+use egui_kittest::Harness;
+use filmcraft_engine::Session;
+use filmcraft_ui_egui::FilmcraftApp;
+use filmcraft_ui_egui::control::ControlRequest;
+use filmcraft_ui_egui::panels::timeline::{max_scroll, timeline_extent};
+use serde_json::{Value, json};
+
+struct Driver {
+ harness: Harness<'static, FilmcraftApp>,
+ tx: Sender<ControlRequest>,
+}
+
+impl Driver {
+ fn demo() -> Self {
+ let mut session = Session::default();
+ session.execute("file.openDemoProject", json!({})).expect("demo project");
+ let (tx, rx) = channel();
+ let app = FilmcraftApp::new(session).with_control(rx);
+ let harness = Harness::builder().with_size(egui::vec2(1600.0, 980.0)).with_max_steps(10_000).build_eframe(move |_cc| app);
+ let mut d = Driver { harness, tx };
+ d.frames(4);
+ d
+ }
+
+ fn frames(&mut self, n: usize) {
+ for _ in 0..n {
+ let ctx = self.harness.ctx.clone();
+ let mut raw = std::mem::take(self.harness.input_mut());
+ eframe::App::raw_input_hook(self.harness.state_mut(), &ctx, &mut raw);
+ *self.harness.input_mut() = raw;
+ self.harness.step();
+ }
+ }
+
+ fn call(&mut self, method: &str, params: Value) -> Value {
+ let (req, reply) = ControlRequest::new(method, params.clone());
+ self.tx.send(req).unwrap();
+ for _ in 0..600 {
+ self.frames(1);
+ if let Ok(v) = reply.try_recv() {
+ return v;
+ }
+ }
+ panic!("no reply to {method} {params}");
+ }
+
+ fn ok(&mut self, method: &str, params: Value) -> Value {
+ let v = self.call(method, params.clone());
+ assert_eq!(v["ok"], json!(true), "{method} {params} failed: {v}");
+ v["result"].clone()
+ }
+
+ fn exec(&mut self, command: &str, params: Value) -> Value {
+ self.ok("engine.execute", json!({"command": command, "params": params}))
+ }
+}
+
+impl Driver {
+ fn app(&mut self) -> &mut FilmcraftApp {
+ self.harness.state_mut()
+ }
+
+ fn rect(&mut self, id: &str) -> [f32; 4] {
+ let v = self.ok("ui.elements", json!({"prefix": id}));
+ let e = v.as_array().unwrap().iter().find(|e| e["id"] == json!(id)).unwrap_or_else(|| panic!("no element {id}")).clone();
+ let r = e["rect"].as_array().unwrap();
+ [0, 1, 2, 3].map(|k| r[k].as_f64().unwrap() as f32)
+ }
+
+ /// Turn the wheel over the tracks (`fy`: 0.2 is among the video tracks, 0.8 among the audio
+ /// tracks), with modifiers.
+ fn wheel(&mut self, fy: f64, dx: f64, dy: f64, modifiers: Value) {
+ self.ok("ui.scroll", json!({"id": "timeline.tracks", "fx": 0.5, "fy": fy, "dx": dx, "dy": dy, "modifiers": modifiers}));
+ self.frames(3);
+ }
+
+ /// (time at the left edge, zoom, video scroll, audio scroll), once the view has settled.
+ fn view(&mut self) -> (f64, f64, f32, f32) {
+ for _ in 0..200 {
+ let v = &self.app().ui.timeline;
+ if v.pps == v.target_pps && v.scroll == v.target_scroll {
+ break;
+ }
+ self.frames(1);
+ }
+ let v = &self.app().ui.timeline;
+ (v.target_scroll, v.target_pps, v.v_scroll, v.a_scroll)
+ }
+
+ /// The demo sequence with enough tracks to scroll both halves, at a known zoom.
+ fn tall() -> Driver {
+ let mut d = Driver::demo();
+ d.exec("sequence.addTracks", json!({"video": 12, "audio": 12}));
+ d.ok("ui.set", json!({"timeline": {"pps": 50.0, "scroll": 4.0}}));
+ d.frames(4);
+ assert_eq!(d.view(), (4.0, 50.0, 0.0, 0.0));
+ d
+ }
+
+ fn seconds(&mut self) -> f64 {
+ self.app().session.active_sequence().unwrap().duration().seconds()
+ }
+}
+
+fn none() -> Value {
+ json!({})
+}
+
+#[test]
+fn the_wheel_moves_the_tracks_up_and_down() {
+ let mut d = Driver::tall();
+ // over the video tracks: wheeling up brings the higher tracks in (Video 1 is at the bottom)
+ d.wheel(0.2, 0.0, 40.0, none());
+ assert_eq!(d.view(), (4.0, 50.0, 40.0, 0.0), "the video tracks moved, nothing else");
+ d.wheel(0.2, 0.0, -25.0, none());
+ assert_eq!(d.view(), (4.0, 50.0, 15.0, 0.0));
+ // over the audio tracks: wheeling down brings the lower tracks in
+ d.wheel(0.8, 0.0, -30.0, none());
+ assert_eq!(d.view(), (4.0, 50.0, 15.0, 30.0));
+ // neither half goes past its first or last track
+ d.wheel(0.2, 0.0, -1e6, none());
+ d.wheel(0.8, 0.0, 1e6, none());
+ assert_eq!(d.view(), (4.0, 50.0, 0.0, 0.0));
+ d.wheel(0.8, 0.0, -1e6, none());
+ let (_, _, _, lowest) = d.view();
+ assert!(lowest > 100.0 && lowest < 15.0 * 200.0, "{lowest}");
+}
+
+#[test]
+fn cmd_wheel_moves_the_timeline_sideways_and_option_wheel_zooms() {
+ let mut d = Driver::tall();
+ // Cmd + wheel: sideways, a point of wheel for a point of Timeline; the tracks stay
+ d.wheel(0.2, 0.0, -100.0, json!({"command": true}));
+ assert_eq!(d.view(), (6.0, 50.0, 0.0, 0.0), "100 points at 50 points a second: 2 s later");
+ d.wheel(0.8, 0.0, 250.0, json!({"command": true}));
+ assert_eq!(d.view(), (1.0, 50.0, 0.0, 0.0));
+ d.wheel(0.8, 0.0, 1e5, json!({"command": true}));
+ assert_eq!(d.view().0, 0.0, "not before the start");
+ // Option + wheel: zoom about the pointer; it is not what Cmd + wheel does
+ d.ok("ui.set", json!({"timeline": {"scroll": 4.0}}));
+ d.frames(3);
+ let tracks = d.rect("timeline.tracks");
+ let under_pointer = 4.0 + (tracks[2] as f64 * 0.5) / 50.0;
+ d.wheel(0.2, 0.0, 20.0, json!({"alt": true}));
+ let (scroll, pps, v, a) = d.view();
+ assert!((pps - 60.0).abs() < 1e-6, "zoomed in by a fifth: {pps}");
+ assert!((scroll + (tracks[2] as f64 * 0.5) / pps - under_pointer).abs() < 0.02, "the time under the pointer stays there: {scroll}");
+ assert_eq!((v, a), (0.0, 0.0));
+ d.wheel(0.2, 0.0, -20.0, json!({"alt": true}));
+ assert!((d.view().1 - 48.0).abs() < 1e-6, "and out again");
+ // a sideways gesture (trackpad swipe, tilt wheel) is sideways whatever is held; so is Shift
+ d.ok("ui.set", json!({"timeline": {"pps": 50.0, "scroll": 4.0}}));
+ d.frames(3);
+ d.wheel(0.2, -50.0, 5.0, none());
+ assert_eq!(d.view(), (5.0, 50.0, 0.0, 0.0));
+ d.wheel(0.2, 0.0, -50.0, json!({"shift": true}));
+ assert_eq!(d.view(), (6.0, 50.0, 0.0, 0.0));
+}
+
+/// Settings ▸ Timeline ▸ Timeline Mouse Scrolling "Horizontal": the wheel moves the Timeline
+/// sideways and Cmd + wheel moves the tracks.
+#[test]
+fn the_mouse_scrolling_setting_swaps_wheel_and_cmd_wheel() {
+ let mut d = Driver::tall();
+ assert_eq!(d.app().session.prefs.timeline.mouse_scrolling, "vertical", "Premiere's default on macOS");
+ d.app().session.prefs.timeline.mouse_scrolling = "horizontal".into();
+ d.wheel(0.2, 0.0, -100.0, none());
+ assert_eq!(d.view(), (6.0, 50.0, 0.0, 0.0));
+ d.wheel(0.2, 0.0, 40.0, json!({"command": true}));
+ assert_eq!(d.view(), (6.0, 50.0, 40.0, 0.0));
+ d.wheel(0.2, 0.0, 20.0, json!({"alt": true}));
+ assert!((d.view().1 - 60.0).abs() < 1e-6, "Option + wheel zooms either way");
+}
+
+/// The view cannot leave the Timeline: scrolled 39 minutes past a 26 second sequence, nothing was
+/// in view and the scroll bar's thumb sat at the right end whatever was done to it.
+#[test]
+fn the_view_stops_ten_minutes_past_the_end_of_the_sequence() {
+ let mut d = Driver::tall();
+ let seconds = d.seconds();
+ let width = d.rect("timeline.tracks")[2];
+ assert_eq!(timeline_extent(seconds), seconds + 600.0);
+ let limit = max_scroll(seconds, width, 50.0);
+ assert!((limit - (seconds + 600.0 - width as f64 / 50.0)).abs() < 1e-9);
+ // by the wheel
+ d.wheel(0.2, 0.0, -1e7, json!({"command": true}));
+ assert!((d.view().0 - limit).abs() < 1e-6, "{} vs {limit}", d.view().0);
+ d.wheel(0.2, 0.0, -500.0, json!({"command": true}));
+ assert!((d.view().0 - limit).abs() < 1e-6);
+ // by anything else that moves the view (a script here: the state that was reported stuck)
+ d.ok("ui.set", json!({"timeline": {"pps": 2.9863201066269047, "scroll": 2325.538505030118}}));
+ d.frames(4);
+ let (scroll, pps, ..) = d.view();
+ assert!((scroll - max_scroll(seconds, width, pps)).abs() < 1e-6 && scroll < 400.0, "{scroll}");
+ // zoomed out until the whole Timeline is in view, there is nowhere to scroll to
+ d.ok("ui.set", json!({"timeline": {"pps": 0.5, "scroll": 50.0}}));
+ d.frames(4);
+ assert_eq!(d.view().0, 0.0);
+ // numbers that are not numbers do no harm
+ d.app().ui.timeline.target_scroll = f64::NAN;
+ d.app().ui.timeline.scroll = f64::INFINITY;
+ d.frames(3);
+ assert_eq!(d.view().0, 0.0);
+}
+
+#[test]
+fn the_scroll_bar_thumb_follows_the_pointer_and_reaches_both_ends() {
+ let mut d = Driver::tall();
+ let seconds = d.seconds();
+ let track = d.rect("timeline.zoomBar.track");
+ let limit = max_scroll(seconds, d.rect("timeline.tracks")[2], 50.0);
+ // at the start the thumb is at the left end of the bar, and wide enough to grab
+ d.ok("ui.set", json!({"timeline": {"scroll": 0.0}}));
+ d.frames(3);
+ let thumb = d.rect("timeline.zoomBar");
+ assert!((thumb[0] - track[0]).abs() < 0.5 && thumb[2] >= 30.0 && thumb[2] < track[2] / 4.0, "{thumb:?} in {track:?}");
+ // dragged by its middle it moves with the pointer, and the view with it
+ let y = thumb[1] + thumb[3] / 2.0;
+ let grab = thumb[0] + thumb[2] / 2.0;
+ d.ok("ui.drag", json!({"from": {"x": grab, "y": y}, "to": {"x": grab + 300.0, "y": y}, "steps": 10}));
+ d.frames(3);
+ let moved = d.rect("timeline.zoomBar");
+ assert!((moved[0] - thumb[0] - 300.0).abs() < 2.0, "the thumb went {} points for a 300 point drag", moved[0] - thumb[0]);
+ let travel = (track[2] - thumb[2]) as f64;
+ assert!((d.view().0 - 300.0 / travel * limit).abs() < limit * 0.01, "{}", d.view().0);
+ assert_eq!(d.view().1, 50.0, "a drag by the middle does not zoom");
+ // dragged past the right end it stops there: the view is at its limit
+ let grab = moved[0] + moved[2] / 2.0;
+ d.ok("ui.drag", json!({"from": {"x": grab, "y": y}, "to": {"x": track[0] + track[2] + 400.0, "y": y}, "steps": 10}));
+ d.frames(3);
+ let right = d.rect("timeline.zoomBar");
+ assert!((right[0] + right[2] - (track[0] + track[2])).abs() < 0.5, "{right:?}");
+ assert!((d.view().0 - limit).abs() < 1e-6);
+ // and from the right end it comes back: dragged left it leaves the end and follows again
+ let grab = right[0] + right[2] / 2.0;
+ d.ok("ui.drag", json!({"from": {"x": grab, "y": y}, "to": {"x": grab - 250.0, "y": y}, "steps": 10}));
+ d.frames(3);
+ let back = d.rect("timeline.zoomBar");
+ assert!((right[0] - back[0] - 250.0).abs() < 2.0, "{back:?} from {right:?}");
+ // all the way left: the start of the sequence
+ let grab = back[0] + back[2] / 2.0;
+ d.ok("ui.drag", json!({"from": {"x": grab, "y": y}, "to": {"x": track[0] - 500.0, "y": y}, "steps": 10}));
+ d.frames(3);
+ assert_eq!(d.view().0, 0.0);
+ assert!((d.rect("timeline.zoomBar")[0] - track[0]).abs() < 0.5);
+ // a click on the bar beside the thumb brings the thumb there
+ let target = track[0] + track[2] * 0.6;
+ d.ok("ui.click", json!({"x": target, "y": y}));
+ d.frames(3);
+ let jumped = d.rect("timeline.zoomBar");
+ assert!((jumped[0] + jumped[2] / 2.0 - target).abs() < 2.0, "{jumped:?}");
+ // the handles at its ends still zoom: the right one out, the left edge of the view staying
+ let at = d.view().0;
+ d.ok("ui.drag", json!({"from": {"x": jumped[0] + jumped[2] - 3.0, "y": y}, "to": {"x": jumped[0] + jumped[2] + 60.0, "y": y}, "steps": 8}));
+ d.frames(3);
+ let (scroll, pps, ..) = d.view();
+ assert!(pps < 50.0 && (scroll - at).abs() < 1e-6, "{pps} {scroll}");
+}