Skip to content

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

#648, #668: click a gap in the Timeline, Delete closes it

Oskarca44d854505d
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs
index d59978393..ca2276061 100644
--- a/crates/ui-egui/src/panels/timeline.rs
+++ b/crates/ui-egui/src/panels/timeline.rs
@@ -30,6 +30,8 @@ const RULER_H: f32 = 44.0;
const SCROLLBAR_H: f32 = 17.0;
const DIVIDER_H: f32 = 5.0;
const MASTER_H: f32 = 34.0;
+/// The selected gap between two clips (#648, #668).
+const GAP_FILL: Color32 = Color32::from_gray(224);
const SNAP_PX: f32 = 9.0;
/// Transient interaction state.
@@ -380,6 +382,16 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
}
let p = painter.with_clip_rect(Rect::from_min_max(pos2(content.min.x, row.min.y), pos2(content.max.x, row.max.y)));
let volume_line = r.kind == TrackKind::Audio && !r.lane;
+ // the selected gap (#648, #668): a light block across the empty time, as in Premiere Pro
+ if let Some(g) = app.session.state.gap_selection.filter(|g| g.track == r.track) {
+ let x0 = layout.x_of(g.range.start).max(content.min.x - 1.0);
+ let x1 = layout.x_of(g.range.end()).min(content.max.x + 1.0);
+ if x1 > x0 {
+ let gap = Rect::from_min_max(pos2(x0, r.rect.min.y + 1.0), pos2(x1, r.rect.max.y - 1.0));
+ p.rect_filled(gap, 0.0, GAP_FILL);
+ app.auto.add("timeline.gap", gap.intersect(content), "Selected gap");
+ }
+ }
for it in &tr.items {
let (start, dur, moved_track) = previews.get(&it.id).copied().unwrap_or((it.start, it.duration, None));
if moved_track.is_some_and(|m| m != r.track) {
@@ -1695,6 +1707,19 @@ fn edit_point_menu(app: &mut FilmcraftApp, ctx: &egui::Context, ui: &mut egui::U
}
}
+/// The menu of a right-clicked gap: Ripple Delete closes it.
+fn gap_menu(app: &mut FilmcraftApp, ctx: &egui::Context, ui: &mut egui::Ui) {
+ let label = tl!("Ripple Delete");
+ let r = ui.add_enabled(app.session.state.gap_selection.is_some(), egui::Button::new(label));
+ app.auto.add("timeline.gapMenu.edit.rippleDelete", r.rect, label);
+ if r.clicked() {
+ if let Err(e) = crate::menus::invoke(app, ctx, "edit.rippleDelete", json!({})) {
+ app.ui.status = e;
+ }
+ ui.close();
+ }
+}
+
/// The clip menu's Multi-Camera submenu: Enable, Flatten, and the cameras of the selected nested
/// sequence clips (greyed out when none of the selected clips is a nested sequence).
fn multicam_menu(app: &mut FilmcraftApp, ui: &mut egui::Ui, picked: &[&TrackItem]) {
@@ -1968,6 +1993,7 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
if mods.alt {
app.session.state.selection = vec![clip];
app.session.state.transition_selection.clear();
+ app.session.state.gap_selection = None;
} else {
let _ = app.session.execute("timeline.select", json!({"clips": [clip.0]}));
}
@@ -1996,13 +2022,17 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
}
}
(_, Grab::Other(Hit::Transition { .. })) => None,
- (_, Grab::Other(Hit::Empty { .. })) => {
+ (_, Grab::Other(Hit::Empty { track })) => {
if resp.drag_started() {
Some(Drag::Marquee { start: p })
} else {
app.session.state.selection.clear();
app.session.state.edit_points.clear();
app.session.state.transition_selection.clear();
+ // empty time between clips: select the gap, for Delete to close it (#648, #668)
+ if app.session.execute("timeline.selectGap", json!({"track": track.0, "time": t.0})).is_err() {
+ app.session.state.gap_selection = None;
+ }
None
}
}
@@ -2199,6 +2229,21 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
let edit_point_menu_id = egui::Id::new("timeline.editPointMenu.target");
// the clip right-clicked, remembered while its menu is open: Unlink leaves only it selected
let menu_clip_id = egui::Id::new("timeline.clipMenu.clip");
+ // right-clicking empty time between clips selects the gap and opens its menu (#648, #668)
+ let gap_menu_id = egui::Id::new("timeline.gapMenu.open");
+ if resp.secondary_clicked()
+ && let Some(p) = resp.interact_pointer_pos()
+ {
+ let on_gap = match hit(seq, layout, p) {
+ Hit::Empty { track } => app.session.execute("timeline.selectGap", json!({"track": track.0, "time": layout.tick_at(p.x).0})).is_ok(),
+ _ => false,
+ };
+ ctx.data_mut(|d| d.insert_temp(gap_menu_id, on_gap));
+ if on_gap {
+ // not the edit point menu of a cut right-clicked before
+ ctx.data_mut(|d| d.insert_temp::<Option<EditPointTarget>>(edit_point_menu_id, None));
+ }
+ }
if resp.secondary_clicked()
&& let Some(p) = resp.interact_pointer_pos()
&& let Hit::Clip { clip, edge, track } = hit(seq, layout, p)
@@ -2230,6 +2275,10 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
edit_point_menu(app, &ctx, ui, seq, target);
return;
}
+ if ctx.data(|d| d.get_temp::<bool>(gap_menu_id)).unwrap_or(false) {
+ gap_menu(app, &ctx, ui);
+ return;
+ }
let sel = app.session.state.selection.clone();
let picked: Vec<&filmcraft_project::TrackItem> = sel.iter().filter_map(|c| seq.find_item(*c).map(|(_, it)| it)).collect();
let all_enabled = !picked.is_empty() && picked.iter().all(|it| it.enabled);
diff --git a/crates/ui-egui/tests/timeline_gap_ui.rs b/crates/ui-egui/tests/timeline_gap_ui.rs
new file mode 100644
index 000000000..930f291dc
--- /dev/null
+++ b/crates/ui-egui/tests/timeline_gap_ui.rs
@@ -0,0 +1,167 @@
+//! Headless UI test of gaps in the Timeline (#648, #668): clicking empty time between two clips
+//! selects the gap (drawn as a light block), and Delete, Backspace or the gap's right-click menu
+//! (Ripple Delete) closes it, as in Premiere Pro. The real `FilmcraftApp` under `egui_kittest`,
+//! driven over the control channel.
+
+use std::sync::mpsc::{Sender, channel};
+
+use egui_kittest::Harness;
+use filmcraft_engine::Session;
+use filmcraft_engine::project::ClipId;
+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 {
+ /// A sequence with two 48-frame clips on V1 (and their sounds on A1) at frames 0 and 96.
+ fn two_clips() -> (Self, ClipId, ClipId) {
+ let mut s = Session::default();
+ s.execute("file.openDemoProject", json!({})).unwrap();
+ s.execute("file.newSequence", json!({"name": "Gaps", "video": 1, "audio": 1})).unwrap();
+ let rate = s.sequence_rate();
+ let item = s.project.items.values().find(|i| i.has_video() && i.has_audio() && i.as_media().is_some()).map(|i| i.id).unwrap();
+ for f in [0, 96] {
+ s.execute("timeline.place", json!({"item": item.0, "frame": f, "sourceIn": rate.tick_of(48).0, "duration": rate.tick_of(48).0})).unwrap();
+ }
+ s.execute("edit.deselectAll", json!({})).unwrap();
+ let v1 = &s.active_sequence().unwrap().video_tracks[0];
+ let (a, b) = (v1.items[0].id, v1.items[1].id);
+ 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, a, b)
+ }
+
+ 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 ok(&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() {
+ assert_eq!(v["ok"], json!(true), "{method} {params} failed: {v}");
+ return v["result"].clone();
+ }
+ }
+ panic!("no reply to {method} {params}");
+ }
+
+ fn rect(&mut self, id: &str) -> Option<[f64; 4]> {
+ self.frames(2);
+ let v = self.ok("ui.elements", json!({"prefix": id}));
+ let e = v.as_array().unwrap().iter().find(|e| e["id"] == json!(id))?.clone();
+ let r: Vec<f64> = e["rect"].as_array().unwrap().iter().map(|x| x.as_f64().unwrap()).collect();
+ Some([r[0], r[1], r[2], r[3]])
+ }
+
+ /// Click (`button`: "left" or "right") in the middle of the empty time between `a` and `b`.
+ fn click_between(&mut self, a: ClipId, b: ClipId, button: &str) {
+ let ra = self.rect(&format!("timeline.clip.{}", a.0)).expect("the first clip is shown");
+ let rb = self.rect(&format!("timeline.clip.{}", b.0)).expect("the second clip is shown");
+ let (gap_l, gap_r) = (ra[0] + ra[2], rb[0]);
+ assert!(gap_r - gap_l > 10.0, "the gap is wide enough to click: {ra:?} {rb:?}");
+ self.ok("ui.click", json!({"x": (gap_l + gap_r) / 2.0, "y": ra[1] + ra[3] / 2.0, "button": button}));
+ self.frames(3);
+ }
+
+ fn start(&mut self, c: ClipId) -> i64 {
+ self.harness.state_mut().session.active_sequence().unwrap().find_item(c).map(|(_, i)| i.start.0).unwrap()
+ }
+
+ fn frame(&mut self, n: i64) -> i64 {
+ self.harness.state_mut().session.sequence_rate().tick_of(n).0
+ }
+
+ fn gap(&mut self) -> Value {
+ self.ok("engine.execute", json!({"command": "sequence.inspect", "params": {}}))["gapSelection"].clone()
+ }
+}
+
+#[test]
+fn clicking_a_gap_selects_it_and_delete_closes_it() {
+ let (mut d, a, b) = Driver::two_clips();
+ let (f48, f96) = (d.frame(48), d.frame(96));
+ d.click_between(a, b, "left");
+ let g = d.gap();
+ assert_eq!((g["start"].as_i64(), g["end"].as_i64()), (Some(f48), Some(f96)), "the gap is selected: {g}");
+ assert!(d.rect("timeline.gap").is_some(), "and drawn");
+ d.ok("ui.key", json!({"key": "Delete"}));
+ d.frames(3);
+ assert_eq!(d.start(b), f48, "Delete closes the gap");
+ assert_eq!(d.start(a), 0, "the clip before stays");
+ assert_eq!(d.gap(), Value::Null);
+ assert!(d.rect("timeline.gap").is_none(), "nothing is drawn any more");
+}
+
+#[test]
+fn backspace_closes_a_selected_gap_too() {
+ let (mut d, a, b) = Driver::two_clips();
+ let f48 = d.frame(48);
+ d.click_between(a, b, "left");
+ d.ok("ui.key", json!({"key": "Backspace"}));
+ d.frames(3);
+ assert_eq!(d.start(b), f48);
+}
+
+#[test]
+fn right_clicking_a_gap_offers_ripple_delete() {
+ let (mut d, a, b) = Driver::two_clips();
+ let f48 = d.frame(48);
+ d.click_between(a, b, "right");
+ assert!(d.gap().is_object(), "right-clicking selects the gap");
+ assert!(d.rect("timeline.gapMenu.edit.rippleDelete").is_some(), "its menu offers Ripple Delete");
+ assert!(d.rect("timeline.clipMenu.edit.cut").is_none(), "not the clip menu");
+ d.ok("ui.click", json!({"id": "timeline.gapMenu.edit.rippleDelete"}));
+ d.frames(3);
+ assert_eq!(d.start(b), f48, "Ripple Delete closes the gap");
+}
+
+#[test]
+fn clicking_a_clip_or_after_the_last_clip_drops_the_gap() {
+ let (mut d, a, b) = Driver::two_clips();
+ let f96 = d.frame(96);
+ d.click_between(a, b, "left");
+ d.ok("ui.click", json!({"id": format!("timeline.clip.{}", a.0)}));
+ d.frames(3);
+ assert_eq!(d.gap(), Value::Null, "a clip selection replaces the gap");
+ d.click_between(a, b, "left");
+ // empty time after the last clip is not a gap
+ let rb = d.rect(&format!("timeline.clip.{}", b.0)).unwrap();
+ d.ok("ui.click", json!({"x": rb[0] + rb[2] + 30.0, "y": rb[1] + rb[3] / 2.0}));
+ d.frames(3);
+ assert_eq!(d.gap(), Value::Null);
+ d.ok("ui.key", json!({"key": "Delete"}));
+ d.frames(3);
+ assert_eq!(d.start(b), f96, "Delete with nothing selected moves nothing");
+}
+
+/// A cut's edit point menu, right-clicked before, does not stand in for the gap's menu.
+#[test]
+fn the_gap_menu_follows_an_edit_point_menu() {
+ let (mut d, a, b) = Driver::two_clips();
+ d.ok("ui.click", json!({"id": format!("timeline.clip.{}", b.0), "fx": 0.0, "button": "right"}));
+ d.frames(3);
+ assert!(d.rect("timeline.editPointMenu.rippleIn").is_some(), "the edge's edit point menu");
+ d.ok("ui.key", json!({"key": "Escape"}));
+ d.frames(3);
+ d.click_between(a, b, "right");
+ assert!(d.rect("timeline.gapMenu.edit.rippleDelete").is_some(), "the gap's menu");
+ assert!(d.rect("timeline.editPointMenu.rippleIn").is_none(), "not the edit point menu");
+}