Skip to content

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

#313: double-clicking a Timeline clip opens it in the Source monitor

vangel76ad8080159632
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index d5618b99f..31645e875 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -2118,7 +2118,10 @@ fn build() -> Vec<CommandSpec> {
Ok(Value::Null)
}),
// ================= Source monitor =================
- cmd!("source.open", "Open in Source Monitor", [], None, r#"{"item":id?}"#, always, |s, p| {
+ cmd!("source.open", "Open in Source Monitor", [], None, r#"{"item":id?,"clip":id?}"#, always, |s, p| {
+ if let Some(clip) = clip_p(p, "clip") {
+ return crate::source_monitor::open_clip(s, clip);
+ }
let id = item_p(p, "item")
.or(s.state.project_selection.first().copied())
.ok_or_else(|| bad("source.open", "need `item` (or a Project panel selection)"))?;
diff --git a/crates/engine/src/source_monitor.rs b/crates/engine/src/source_monitor.rs
index cdbcf35f7..84790a524 100644
--- a/crates/engine/src/source_monitor.rs
+++ b/crates/engine/src/source_monitor.rs
@@ -3,7 +3,7 @@
use crate::clip_ops::source_view;
use crate::commands::bad;
use crate::{EngineError, Result, Session};
-use filmcraft_project::{ItemId, Label, Marker, MarkerId, MarkerKind, Project};
+use filmcraft_project::{ClipId, ItemId, ItemKind, Label, Marker, MarkerId, MarkerKind, Project};
use filmcraft_time::{FrameRate, TICKS_PER_SECOND, Tick, TimeRange};
use serde_json::{Value, json};
@@ -203,6 +203,27 @@ pub(crate) fn route(s: &mut Session, id: &str, p: &Value) -> Result<Option<Value
Ok(Some(Value::Null))
}
+/// `source.open {"clip"}` (double-clicking a Timeline clip, as in Premiere): the clip's media
+/// opens in the Source monitor, parked on the frame shown under the playhead when the playhead
+/// is over the clip, otherwise on the clip's first frame.
+pub(crate) fn open_clip(s: &mut Session, clip: ClipId) -> Result<Value> {
+ let id = "source.open";
+ let seq = s.active_sequence().ok_or(EngineError::NoSequence)?;
+ let (_, it) = seq.find_item(clip).ok_or_else(|| bad(id, "no such clip in the active sequence"))?;
+ let t = s.playhead();
+ let time = it.source_time_at(if t >= it.start && t < it.end() { t } else { it.start });
+ let item = it.item;
+ match s.project.item(item).map(|i| &i.kind) {
+ None => return Err(bad(id, "the clip's source is unavailable")),
+ Some(ItemKind::Graphic { .. }) => return Err(bad(id, "a graphic clip has no source to open")),
+ Some(_) => {}
+ }
+ s.execute(id, json!({"item": item.0}))?;
+ // source.setPlayhead snaps to the source frame grid and keeps the playhead inside a subclip
+ s.execute("source.setPlayhead", json!({"time": time.0}))?;
+ Ok(json!({"item": item.0, "time": s.state.source_playhead.0}))
+}
+
fn text<'a>(p: &'a Value, key: &str, id: &str) -> Result<Option<&'a str>> {
p.get(key).map(|v| v.as_str().ok_or_else(|| bad(id, format!("{key} must be text")))).transpose()
}
@@ -289,6 +310,30 @@ mod tests {
assert!(source_view(&s, ItemId(5)).unwrap().markers.is_empty());
}
#[test]
+ fn timeline_clip_opens_in_source_at_the_matching_frame() {
+ let mut s = fixture();
+ let seq = s.active_sequence().unwrap();
+ let it = seq
+ .all_tracks()
+ .flat_map(|t| t.items.iter())
+ .find(|i| i.source_in > Tick::ZERO && s.project.item(i.item).is_some_and(|p| p.as_media().is_some()))
+ .unwrap()
+ .clone();
+ let rate = s.sequence_rate();
+ let inside = it.start + rate.tick_of(3);
+ s.set_playhead(inside);
+ let r = s.execute("source.open", json!({"clip": it.id.0})).unwrap();
+ assert_eq!(r["item"], it.item.0);
+ assert_eq!(s.state.source_item, Some(it.item));
+ let view = source_view(&s, it.item).unwrap();
+ assert_eq!(s.state.source_playhead, view.rate.snap(it.source_time_at(inside)));
+ // playhead off the clip: the clip's first frame
+ s.set_playhead(it.end() + rate.tick_of(1));
+ s.execute("source.open", json!({"clip": it.id.0})).unwrap();
+ assert_eq!(s.state.source_playhead, view.rate.snap(it.source_in));
+ assert!(s.execute("source.open", json!({"clip": u64::MAX})).is_err());
+ }
+ #[test]
fn invalid_marker_parameters_do_not_edit_the_project() {
let mut s = fixture();
let before = s.project.to_json();
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs
index 9e17453a1..7f434c70a 100644
--- a/crates/ui-egui/src/panels/timeline.rs
+++ b/crates/ui-egui/src/panels/timeline.rs
@@ -2289,6 +2289,21 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
}
}
+ // ---- double-click any other clip (not on its edge): open its media in the Source monitor at
+ // the matching frame (Premiere); graphic clips have no source to show
+ if resp.double_clicked()
+ && tool == Tool::Selection
+ && let Some(p) = resp.interact_pointer_pos()
+ && let Hit::Clip { clip, edge: None, .. } = hit(seq, layout, p)
+ && seq
+ .find_item(clip)
+ .and_then(|(_, it)| app.session.project.item(it.item))
+ .is_some_and(|i| !matches!(i.kind, filmcraft_project::ItemKind::Sequence(_) | filmcraft_project::ItemKind::Graphic { .. }))
+ && let Err(e) = app.session.execute("source.open", json!({"clip": clip.0}))
+ {
+ app.ui.status = e.to_string();
+ }
+
// ---- context menu on clips (right-clicking an unselected clip selects it first); on a clip's
// edge, the edit point menu instead (right-clicking an unselected edit point selects it first)
let edit_point_menu_id = egui::Id::new("timeline.editPointMenu.target");