FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
NEST12: every sequence keeps its own Timeline zoom, scroll and track heights
justjohn123456aefdef661d4
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 4c084d01d..c8c913bb7 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -240,6 +240,11 @@ pub struct EditorState {pub sequences_as_clips: bool,/// Open sequences (timeline tabs), in tab order.pub open_sequences: Vec<ItemId>,+ /// How each sequence is shown in the Timeline panel (zoom, scroll, track heights): every+ /// sequence keeps its own. The frontend writes the active sequence's view here as it changes+ /// and takes a sequence's view from here when it becomes the active one.+ #[serde(default)]+ pub timeline_views: std::collections::BTreeMap<ItemId, filmcraft_project::SequenceView>,/// Timeline clipboard (serialized track items with their relative track index).#[serde(skip)]pub clipboard: Vec<(TrackKind, usize, filmcraft_project::TrackItem)>,@@ -802,6 +807,7 @@ impl Session {self.state.active_sequence = p.sequences().next().map(|i| i.id);}self.state.open_sequences.retain(|s| p.sequence(*s).is_some());+ self.state.timeline_views.retain(|s, _| p.sequence(*s).is_some());if let Some(s) = self.state.source_item&& p.item(s).is_none(){@@ -990,6 +996,8 @@ mod scopes_tests;#[cfg(test)]mod sequence_inspect_tests;#[cfg(test)]+mod sequence_tabs_tests;+#[cfg(test)]mod sequence_tools_tests;#[cfg(test)]mod settings_tests;diff --git a/crates/engine/src/sequence_tabs_tests.rs b/crates/engine/src/sequence_tabs_tests.rsnew file mode 100644index 000000000..abccd31f8--- /dev/null+++ b/crates/engine/src/sequence_tabs_tests.rs@@ -0,0 +1,33 @@+//! The Timeline's sequence tabs: every sequence has its own view (zoom, scroll, track heights).+//! Premiere's behaviour was observed in Premiere Pro 26.5.2.++use super::*;+use filmcraft_project::SequenceView;+use serde_json::json;++/// The demo project with two more sequences open: tabs [main, b, c], `c` active.+fn three_tabs() -> (Session, [ItemId; 3]) {+ let mut s = Session::default();+ s.execute("file.openDemoProject", json!({})).unwrap();+ let main = s.state.active_sequence.unwrap();+ let mut new = |name: &str| ItemId(s.execute("file.newSequence", json!({"name": name})).unwrap()["sequence"].as_u64().unwrap());+ let (b, c) = (new("B"), new("C"));+ assert_eq!(s.state.open_sequences, [main, b, c]);+ assert_eq!(s.state.active_sequence, Some(c));+ (s, [main, b, c])+}++fn view(pps: f64, scroll: f64) -> SequenceView {+ SequenceView { pps, scroll, v_scroll: 12.0, a_scroll: 7.0, video_track_h: 90.0, audio_track_h: 40.0 }+}++#[test]+fn the_view_of_a_deleted_sequence_is_forgotten() {+ let (mut s, [main, b, c]) = three_tabs();+ for id in [main, b, c] {+ s.state.timeline_views.insert(id, view(40.0, 0.0));+ }+ s.execute("project.delete", json!({"items": [b.0]})).unwrap();+ assert_eq!(s.state.timeline_views.keys().copied().collect::<Vec<_>>(), [main, c]);+ assert_eq!(s.state.open_sequences, [main, c]);+}diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rsindex 980049270..66154ecd7 100644--- a/crates/project/src/lib.rs+++ b/crates/project/src/lib.rs@@ -1100,6 +1100,42 @@ pub struct Project {pub source_graphics: BTreeMap<ItemId, SourceGraphic>,}+/// How a sequence is shown in the Timeline panel: its zoom, scroll position and track heights.+/// Each open sequence keeps its own (as Premiere's sequence tabs do).+#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]+pub struct SequenceView {+ /// Zoom: points per second.+ pub pps: f64,+ /// Time at the left edge, in seconds.+ pub scroll: f64,+ /// Vertical scroll of the video and audio halves, in points.+ #[serde(default)]+ pub v_scroll: f32,+ #[serde(default)]+ pub a_scroll: f32,+ pub video_track_h: f32,+ pub audio_track_h: f32,+}++impl SequenceView {+ /// The view with every number brought into a usable range, or `None` when a number is not+ /// finite. Views are read from project files, so nothing in them is trusted.+ pub fn checked(self) -> Option<SequenceView> {+ let all = [self.pps, self.scroll, self.v_scroll as f64, self.a_scroll as f64, self.video_track_h as f64, self.audio_track_h as f64];+ if all.iter().any(|x| !x.is_finite()) {+ return None;+ }+ Some(SequenceView {+ pps: self.pps.clamp(1e-3, 1e5),+ scroll: self.scroll.clamp(0.0, 1e7),+ v_scroll: self.v_scroll.clamp(0.0, 1e6),+ a_scroll: self.a_scroll.clamp(0.0, 1e6),+ video_track_h: self.video_track_h.clamp(8.0, 600.0),+ audio_track_h: self.audio_track_h.clamp(8.0, 600.0),+ })+ }+}+/// A LUT imported into the project (`lut.import`). Lumetri refers to it as `lib:<id>`.#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]pub struct ProjectLut {diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 1bf8feb12..2b28740d0 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -178,6 +178,10 @@ pub struct FilmcraftApp {fonts_ready: bool,pub integrated_titlebar: bool,pub last_timeline_width: f32,+ /// The sequence whose view `ui.timeline` holds, and that view as it was last exchanged with+ /// `session.state.timeline_views` (see `sync_timeline_view`).+ timeline_view_of: Option<filmcraft_engine::project::ItemId>,+ timeline_view_last: Option<filmcraft_engine::project::SequenceView>,pub fps: f32,last_time: f64,bindings: Vec<menus::KeyBinding>,@@ -358,6 +362,8 @@ impl FilmcraftApp {fonts_ready: false,integrated_titlebar: false,last_timeline_width: 1000.0,+ timeline_view_of: None,+ timeline_view_last: None,fps: 60.0,last_time: 0.0,bindings: Vec::new(),@@ -440,6 +446,60 @@ impl FilmcraftApp {self.applied_prefs = Some(p);}+ /// Every sequence has its own Timeline view (zoom, scroll, track heights), as Premiere's+ /// sequence tabs do. `ui.timeline` holds the active sequence's; the views of all sequences are+ /// in `session.state.timeline_views`, which is saved with the project. Each frame this+ /// - gives `ui.timeline` the view of a sequence that has just become active (a sequence shown+ /// for the first time is fitted, with default track heights),+ /// - writes a change made in the panel to the session,+ /// - and takes over a view that was changed in the session (by a command or the control+ /// channel).+ fn sync_timeline_view(&mut self) {+ let Some(active) = self.session.state.active_sequence else {+ self.timeline_view_of = None;+ return;+ };+ let stored = self.session.state.timeline_views.get(&active).copied();+ let v = &mut self.ui.timeline;+ let shown = filmcraft_engine::project::SequenceView {+ pps: v.target_pps,+ scroll: v.target_scroll,+ v_scroll: v.v_scroll,+ a_scroll: v.a_scroll,+ video_track_h: v.video_track_h,+ audio_track_h: v.audio_track_h,+ };+ let show = |v: &mut state::TimelineView, s: filmcraft_engine::project::SequenceView| {+ (v.pps, v.target_pps, v.scroll, v.target_scroll) = (s.pps, s.pps, s.scroll, s.scroll);+ (v.v_scroll, v.a_scroll, v.video_track_h, v.audio_track_h) = (s.v_scroll, s.a_scroll, s.video_track_h, s.audio_track_h);+ (v.fit_pending, v.fit_empty) = (false, None);+ };+ if self.timeline_view_of != Some(active) {+ self.timeline_view_of = Some(active);+ match stored.and_then(|s| s.checked()) {+ Some(s) => {+ show(v, s);+ self.timeline_view_last = Some(s);+ }+ None => {+ let d = state::TimelineView::default();+ (v.v_scroll, v.a_scroll, v.video_track_h, v.audio_track_h) = (0.0, 0.0, d.video_track_h, d.audio_track_h);+ (v.fit_pending, v.fit_empty) = (true, None);+ self.timeline_view_last = None;+ }+ }+ } else if v.fit_pending {+ // the panel has not fitted the sequence yet: nothing to keep+ } else if self.timeline_view_last != Some(shown) {+ self.session.state.timeline_views.insert(active, shown);+ self.timeline_view_last = Some(shown);+ } else if let Some(s) = stored.filter(|s| *s != shown).and_then(|s| s.checked()) {+ show(v, s);+ self.timeline_view_last = Some(s);+ self.session.state.timeline_views.insert(active, s);+ }+ }+pub fn set_workspace(&mut self, name: &str) {self.ui.workspace = name.to_string();self.ui.dock = dock::workspace(name);@@ -1067,7 +1127,8 @@ impl FilmcraftApp {for ev in self.session.drain_events() {match ev {filmcraft_engine::Event::OpenSequence(_) => {- self.ui.timeline.fit_pending = true;+ // show the sequence with its own view (or fitted, the first time)+ self.timeline_view_of = None;self.ui.dock.restore_timeline();self.ui.dock.activate(PanelKind::Timeline);}@@ -1079,6 +1140,7 @@ impl FilmcraftApp {filmcraft_engine::Event::ProjectChanged { .. } => {}}}+ self.sync_timeline_view();self.handle_drops(&ctx);if let Some(rx) = self.command_inbox.take() {while let Ok(id) = rx.try_recv() {diff --git a/crates/ui-egui/tests/timeline_tabs_ui.rs b/crates/ui-egui/tests/timeline_tabs_ui.rsnew file mode 100644index 000000000..4cda8bbf6--- /dev/null+++ b/crates/ui-egui/tests/timeline_tabs_ui.rs@@ -0,0 +1,119 @@+//! Headless UI tests of the Timeline's sequence tabs: every sequence keeps its own zoom, scroll+//! and track heights. Premiere's behaviour was observed in Premiere Pro 26.5.2.++use std::sync::mpsc::{Sender, channel};++use egui_kittest::Harness;+use filmcraft_engine::Session;+use filmcraft_engine::project::ItemId;+use filmcraft_ui_egui::FilmcraftApp;+use filmcraft_ui_egui::control::ControlRequest;+use serde_json::{Value, json};++struct Driver {+ harness: Harness<'static, FilmcraftApp>,+ tx: Sender<ControlRequest>,+}++impl Driver {+ fn new() -> Self {+ let mut s = Session::default();+ s.execute("file.openDemoProject", json!({})).unwrap();+ let (tx, rx) = channel();+ let app = FilmcraftApp::new(s).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 {+ // the control channel's clicks and keys enter through the input hook+ 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}))+ }++ fn app(&mut self) -> &mut FilmcraftApp {+ self.harness.state_mut()+ }+}++impl Driver {+ fn click(&mut self, id: &str) {+ self.ok("ui.click", json!({"id": id}));+ self.frames(3);+ }++ fn new_sequence(&mut self, name: &str) -> u64 {+ let id = self.exec("file.newSequence", json!({"name": name}))["sequence"].as_u64().unwrap();+ self.frames(3);+ id+ }++ fn active(&mut self) -> u64 {+ self.app().session.state.active_sequence.unwrap().0+ }++ /// (zoom, scroll, video track height) the Timeline panel is showing.+ fn view(&mut self) -> (f64, f64, f32) {+ let v = &self.app().ui.timeline;+ (v.target_pps, v.target_scroll, v.video_track_h)+ }+}++#[test]+fn each_sequence_tab_keeps_its_own_zoom_scroll_and_track_heights() {+ let mut d = Driver::new();+ let main = d.active();+ d.ok("ui.set", json!({"timeline": {"pps": 333.0, "scroll": 4.5, "videoTrackHeight": 96.0}}));+ d.frames(3);+ assert_eq!(d.view(), (333.0, 4.5, 96.0));+ // a sequence shown for the first time is fitted, at the default track height+ let other = d.new_sequence("Other");+ let fitted = d.view();+ assert!(fitted.0 != 333.0 && fitted.1 == 0.0 && fitted.2 == 60.0, "{fitted:?}");+ d.ok("ui.set", json!({"timeline": {"pps": 20.0, "scroll": 1.0, "videoTrackHeight": 30.0}}));+ d.frames(3);+ // back and forth by clicking the tabs: each comes back as it was left+ d.click(&format!("timeline.tab.{main}"));+ assert_eq!(d.view(), (333.0, 4.5, 96.0));+ d.click(&format!("timeline.tab.{other}"));+ assert_eq!(d.view(), (20.0, 1.0, 30.0));+ // the session holds both (it is what a saved project keeps)+ let views = d.app().session.state.timeline_views.clone();+ assert_eq!((views[&ItemId(main)].pps, views[&ItemId(other)].pps), (333.0, 20.0));+ // closing the shown tab shows the other one with its view+ d.click(&format!("timeline.tab.{other}.close"));+ assert_eq!((d.active(), d.view()), (main, (333.0, 4.5, 96.0)));+ // a view changed in the session (a command, the control channel) is taken over by the panel+ d.app().session.state.timeline_views.get_mut(&ItemId(main)).unwrap().pps = 55.0;+ d.frames(2);+ assert_eq!(d.view().0, 55.0);+}