Skip to content

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

Project panel: a clip dropped on New Item makes a sequence from it (fixes #2855) (#2940)

Marco Arnulfo0b99bafcc46a
diff --git a/crates/ui-egui/src/panels/mod.rs b/crates/ui-egui/src/panels/mod.rs
index ff18ab0f2..0f21158fc 100644
--- a/crates/ui-egui/src/panels/mod.rs
+++ b/crates/ui-egui/src/panels/mod.rs
@@ -180,6 +180,14 @@ pub fn dragged_project_item(ui: &egui::Ui) -> Option<ItemId> {
_ => None,
}
}
+/// An item dragged from the Project panel (or the Media Browser), but not a Source Monitor drag,
+/// whose In/Out range and video/audio choice only a timeline drop keeps.
+pub fn dragged_panel_item(ui: &egui::Ui) -> Option<ItemId> {
+ match payload(ui) {
+ Some(DragPayload::Item(i)) => Some(i),
+ _ => None,
+ }
+}
/// The items a drag from the Project panel carries: the whole selection when the dragged item is
/// one of several selected, otherwise just the dragged item.
pub fn dragged_selection(app: &FilmcraftApp, item: ItemId) -> Vec<ItemId> {
diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs
index e6d95c0e8..6e3487e8d 100644
--- a/crates/ui-egui/src/panels/project.rs
+++ b/crates/ui-egui/src/panels/project.rs
@@ -630,6 +630,17 @@ fn footer(app: &mut FilmcraftApp, ui: &mut egui::Ui, bar: Rect, v: &View, action
}
icons::paint(ui.painter(), r.shrink(5.0), icon, t.icon);
if id == "new-item" {
+ // a clip dropped here makes a sequence that matches it, as in Premiere (#2855)
+ if let Some(item) = crate::panels::dragged_panel_item(ui)
+ && ui.rect_contains_pointer(r)
+ {
+ ui.painter().rect_stroke(r, 3.0, Stroke::new(1.5, app.tokens.accent), egui::StrokeKind::Inside);
+ if ui.input(|i| i.pointer.any_released()) {
+ let items: Vec<u64> = crate::panels::dragged_selection(app, item).iter().map(|i| i.0).collect();
+ actions.push(("file.newSequenceFromClip".into(), json!({"items": items})));
+ crate::panels::clear_drag(ui);
+ }
+ }
egui::Popup::menu(&resp).show(|ui| new_item_menu(app, ui, pre, actions));
} else if resp.clicked() {
match id {
diff --git a/crates/ui-egui/tests/project_panel_ui.rs b/crates/ui-egui/tests/project_panel_ui.rs
index 8fdbb9f0c..10ca52076 100644
--- a/crates/ui-egui/tests/project_panel_ui.rs
+++ b/crates/ui-egui/tests/project_panel_ui.rs
@@ -1181,3 +1181,65 @@ fn escape_takes_back_a_media_browser_drag() {
assert_eq!(d.app().session.project.to_json(), before, "nothing was placed on the Timeline");
let _ = std::fs::remove_dir_all(&dir);
}
+
+/// A clip dragged onto the New Item button makes a sequence from it, as File ▸ New ▸ Sequence
+/// From Clip does (#2855): named after the clip, with its frame size and rate, the clip on the
+/// timeline and the sequence open. One of several selected items brings the whole selection.
+#[test]
+fn dragging_clips_onto_new_item_makes_a_matching_sequence() {
+ let mut d = Driver::demo();
+ let (bin, items) = d.footage();
+ assert!(items.len() >= 3);
+ d.ok("ui.menu.invoke", json!({"id": "projectPanel.openBin", "params": {"bin": bin, "how": "inPlace"}}));
+ let card = |i: u64| format!("project.item.{i}");
+ d.wait_for(&card(items[0]));
+ let sequences = |d: &mut Driver| d.app().session.project.items.values().filter(|i| matches!(i.kind, filmcraft_project::ItemKind::Sequence(_))).count();
+ let clips = |d: &mut Driver| d.app().session.active_sequence().map(|q| q.video_tracks.iter().map(|t| t.items.len()).sum::<usize>()).unwrap_or(0);
+ let drop = json!({"id": "project.button.new-item"});
+ // footage that differs from a new sequence's defaults, so matching it shows
+ if let Some(filmcraft_project::ItemKind::Media(m)) = std::sync::Arc::make_mut(&mut d.app().session.project).item_mut(ItemId(items[0])).map(|i| &mut i.kind)
+ && let Some(v) = m.info.video.as_mut()
+ {
+ (v.width, v.height, v.frame_rate) = (1280, 720, filmcraft_time::FrameRate::FPS_25);
+ }
+ let def = filmcraft_project::SequenceSettings::default();
+ assert_ne!((def.width, def.height, def.frame_rate), (1280, 720, filmcraft_time::FrameRate::FPS_25));
+ let n0 = sequences(&mut d);
+
+ // a drop on another footer button, or a Source Monitor drag, makes nothing
+ d.ok("ui.drag", json!({"from": {"id": card(items[0])}, "to": {"id": "project.button.file.newBin"}, "steps": 16}));
+ d.frames(4);
+ assert_eq!(sequences(&mut d), n0, "New Bin is not a drop target");
+ d.exec("source.open", json!({"item": items[0]}));
+ d.wait_for("source.drag.both");
+ d.ok("ui.drag", json!({"from": {"id": "source.drag.both"}, "to": drop, "steps": 16}));
+ d.frames(4);
+ assert_eq!(sequences(&mut d), n0, "a Source Monitor drag is not taken");
+
+ // an item that is not selected: a sequence of that clip alone
+ d.exec("project.select", json!({"items": [items[2]]}));
+ let steps = d.app().session.history.undo.len();
+ d.ok("ui.drag", json!({"from": {"id": card(items[0])}, "to": drop, "steps": 16}));
+ d.frames(4);
+ assert_eq!(sequences(&mut d), n0 + 1, "the drop makes one sequence");
+ assert!(!d.has("project.newItem.file.newSequence"), "the drop does not open the New Item menu");
+ let app = d.app();
+ let clip = app.session.project.item(ItemId(items[0])).unwrap().clone();
+ let filmcraft_project::ItemKind::Media(m) = &clip.kind else { panic!("footage is media") };
+ let v = m.info.video.clone().expect("footage has video");
+ let q = app.session.active_sequence().expect("the new sequence is active");
+ let seq = app.session.state.active_sequence.unwrap();
+ assert_eq!(app.session.project.item(seq).unwrap().name, clip.name, "named after the clip");
+ assert_eq!((q.settings.width, q.settings.height, q.settings.frame_rate), (v.width, v.height, v.frame_rate), "the clip's frame size and rate");
+ assert_eq!(q.video_tracks[0].items.first().map(|c| c.item), Some(ItemId(items[0])), "the clip is on the timeline");
+ assert_eq!(app.session.history.undo.len(), steps + 1, "one undo step");
+ assert_eq!(clips(&mut d), 1, "an unselected item goes alone");
+
+ // two of the selection: both, back to back, in one sequence
+ d.exec("project.select", json!({"items": [items[0], items[1]]}));
+ d.frames(2);
+ d.ok("ui.drag", json!({"from": {"id": card(items[1])}, "to": drop, "steps": 16}));
+ d.frames(4);
+ assert_eq!(sequences(&mut d), n0 + 2);
+ assert_eq!(clips(&mut d), 2, "the whole selection is placed");
+}