Skip to content

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

#313: the Source monitor remembers its clips; Close and Close All

vangel763190eab4efc9
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index c1f2f56de..d56a03b34 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -2081,6 +2081,9 @@ fn build() -> Vec<CommandSpec> {
s.events.push(crate::Event::OpenSource(id));
Ok(Value::Null)
}),
+ // Source panel: Close / Close All, for the clip shown and the list of recent clips (#313)
+ cmd!("source.close", "Close", [], None, "{}", crate::source_monitor::has_source_clip, |s, _| crate::source_monitor::close(s, false)),
+ cmd!("source.closeAll", "Close All", [], None, "{}", crate::source_monitor::has_source_clip, |s, _| crate::source_monitor::close(s, true)),
cmd!("source.setPlayhead", "Set Source Playhead", [], None, r#"{"time":ticks|"frame":i64|"seconds":f64}"#, always, |s, p| {
let item = s.state.source_item.ok_or_else(|| EngineError::Other("no source clip".into()))?;
let view = crate::clip_ops::source_view(s, item);
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index fe6f94be9..01166df8c 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -231,6 +231,10 @@ pub struct EditorState {
/// Item loaded in the Source monitor and its playhead (media time).
pub source_item: Option<ItemId>,
pub source_playhead: Tick,
+ /// Clips opened in the Source monitor, most recent first: the Source panel's list of recent
+ /// clips (#313). Kept up to date after every command; items gone from the project drop out.
+ #[serde(default)]
+ pub source_history: Vec<ItemId>,
/// Selected timeline items.
pub selection: Vec<ClipId>,
/// Selected project panel items.
@@ -646,6 +650,9 @@ impl Session {
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (spec.run)(self, &params)))
.unwrap_or_else(|_| Err(EngineError::Other(format!("internal error in `{id}` (see the crash log); the command did not complete"))));
self.exec_depth -= 1;
+ if self.exec_depth == 0 {
+ source_monitor::track_history(self);
+ }
// source graphics: an edited instance updates the shared layers and the other instances
if r.is_ok() && spec.journal && id.starts_with("graphics.") && !self.project.source_graphics.is_empty() {
graphic_templates::sync_source_graphics(self);
@@ -881,6 +888,7 @@ impl Session {
{
self.state.source_item = None;
}
+ source_monitor::track_history(self);
if let Some(seq) = self.state.active_sequence.and_then(|s| p.sequence(s)) {
self.state.selection.retain(|c| seq.find_item(*c).is_some());
self.state.caption_selection.retain(|c| seq.find_caption(*c).is_some());
diff --git a/crates/engine/src/source_monitor.rs b/crates/engine/src/source_monitor.rs
index cdbcf35f7..5c1de07fe 100644
--- a/crates/engine/src/source_monitor.rs
+++ b/crates/engine/src/source_monitor.rs
@@ -90,6 +90,52 @@ pub fn source_command(id: &str) -> bool {
)
}
+/// Most clips the Source monitor's list of recent clips keeps.
+pub const SOURCE_HISTORY_MAX: usize = 20;
+
+/// Keep the Source monitor's recent clips in step with the clip it shows: the open clip moves to
+/// the front, items no longer in the project drop out.
+pub(crate) fn track_history(s: &mut Session) {
+ let current = s.state.source_item;
+ let project = &s.project;
+ let history = &mut s.state.source_history;
+ history.retain(|i| project.item(*i).is_some());
+ if let Some(cur) = current.filter(|i| project.item(*i).is_some())
+ && history.first() != Some(&cur)
+ {
+ history.retain(|i| *i != cur);
+ history.insert(0, cur);
+ }
+ history.truncate(SOURCE_HISTORY_MAX);
+}
+
+/// Enablement of Close / Close All: a clip is open in the Source monitor.
+pub(crate) fn has_source_clip(s: &Session) -> std::result::Result<(), String> {
+ s.state.source_item.map(|_| ()).ok_or_else(|| "no clip is open in the Source monitor".into())
+}
+
+/// Source panel ▸ Close (`all`: Close All). The open clip leaves the Source monitor and its list
+/// of recent clips, and the next most recent one opens in its place, as in Premiere; Close All
+/// empties the list and the monitor.
+pub(crate) fn close(s: &mut Session, all: bool) -> Result<Value> {
+ if all {
+ s.state.source_history.clear();
+ } else if let Some(cur) = s.state.source_item {
+ s.state.source_history.retain(|i| *i != cur);
+ }
+ let next = s.state.source_history.iter().copied().find(|i| s.project.item(*i).is_some());
+ match next {
+ Some(i) => {
+ s.execute("source.open", json!({"item": i.0}))?;
+ }
+ None => {
+ s.state.source_item = None;
+ s.state.source_playhead = Tick::ZERO;
+ }
+ }
+ Ok(json!({"item": next.map(|i| i.0)}))
+}
+
fn marker_list(p: &mut Project, item: ItemId) -> Result<&mut Vec<Marker>> {
let it = p.item_mut(item).ok_or_else(|| EngineError::Other("Source item is unavailable".into()))?;
match &mut it.kind {
@@ -288,6 +334,29 @@ mod tests {
s.execute("markers.clearCurrent", json!({"target":"source"})).unwrap();
assert!(source_view(&s, ItemId(5)).unwrap().markers.is_empty());
}
+ /// #313: the Source monitor remembers the clips opened in it, most recent first; Close shows
+ /// the next most recent one and Close All empties the monitor.
+ #[test]
+ fn source_clips_are_remembered_and_closed_in_turn() {
+ let mut s = fixture();
+ let a = ItemId(5);
+ let mut others: Vec<ItemId> = s.project.items.values().filter(|i| i.as_media().is_some() && i.id != a).map(|i| i.id).collect();
+ others.sort();
+ let [b, c] = [others[0], others[1]];
+ for i in [b, c, a] {
+ s.execute("source.open", json!({"item": i.0})).unwrap();
+ }
+ assert_eq!(s.state.source_history, [a, c, b]);
+ assert_eq!(s.execute("source.close", json!({})).unwrap()["item"], c.0);
+ assert_eq!((s.state.source_item, s.state.source_history.clone()), (Some(c), vec![c, b]));
+ // an item deleted from the project leaves the list
+ s.execute("project.delete", json!({"items": [b.0]})).unwrap();
+ assert!(s.project.item(b).is_none());
+ assert!(!s.state.source_history.contains(&b));
+ s.execute("source.closeAll", json!({})).unwrap();
+ assert_eq!((s.state.source_item, s.state.source_history.len()), (None, 0));
+ assert!(s.execute("source.close", json!({})).is_err(), "nothing left to close");
+ }
#[test]
fn invalid_marker_parameters_do_not_edit_the_project() {
let mut s = fixture();
diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv
index 3f45ce9da..2af7e2a24 100644
--- a/crates/ui-egui/src/i18n/es.tsv
+++ b/crates/ui-egui/src/i18n/es.tsv
@@ -496,6 +496,7 @@
{panel} — coming in a later milestone {panel}: llegará en una versión posterior
Close Panel Cerrar panel
Close Other Timeline Panels Cerrar los demás paneles de línea de tiempo
+ Close All Cerrar todo
Maximize Frame Maximizar marco
Restore Workspace Restaurar espacio de trabajo
Reveal Sequence in Project Mostrar secuencia en el proyecto
diff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsv
index 3d54f8737..d32893095 100644
--- a/crates/ui-egui/src/i18n/ja.tsv
+++ b/crates/ui-egui/src/i18n/ja.tsv
@@ -494,6 +494,7 @@
{panel} — coming in a later milestone {panel} — 今後のマイルストーンで実装予定
Close Panel パネルを閉じる
Close Other Timeline Panels 他のタイムラインパネルを閉じる
+ Close All すべて閉じる
Maximize Frame パネルを最大化
Restore Workspace ワークスペースを復元
Reveal Sequence in Project プロジェクトでシーケンスを表示
diff --git a/crates/ui-egui/src/i18n/zh-cn.tsv b/crates/ui-egui/src/i18n/zh-cn.tsv
index e504e3416..e161fd526 100644
--- a/crates/ui-egui/src/i18n/zh-cn.tsv
+++ b/crates/ui-egui/src/i18n/zh-cn.tsv
@@ -487,6 +487,7 @@
{panel} — coming in a later milestone {panel} — 将在后续里程碑中推出
Close Panel 关闭面板
Close Other Timeline Panels 关闭其他时间轴面板
+ Close All 全部关闭
Maximize Frame 最大化帧
Restore Workspace 恢复工作区
Reveal Sequence in Project 在项目面板中显示序列
diff --git a/crates/ui-egui/src/panels/mod.rs b/crates/ui-egui/src/panels/mod.rs
index bcb386fbb..66d0d4754 100644
--- a/crates/ui-egui/src/panels/mod.rs
+++ b/crates/ui-egui/src/panels/mod.rs
@@ -272,6 +272,10 @@ pub fn panel_menu_popup(app: &mut FilmcraftApp, ui: &mut egui::Ui) {
ui.separator();
close |= media_browser::panel_menu(app, ui);
}
+ if p == PanelKind::Source {
+ ui.separator();
+ close |= monitor::source_panel_menu(app, ui);
+ }
});
});
// A click elsewhere or Escape closes the menu. The click that opened it is over the tab, not
diff --git a/crates/ui-egui/src/panels/monitor.rs b/crates/ui-egui/src/panels/monitor.rs
index d94867f39..a57d0483a 100644
--- a/crates/ui-egui/src/panels/monitor.rs
+++ b/crates/ui-egui/src/panels/monitor.rs
@@ -822,6 +822,40 @@ fn transport(app: &mut FilmcraftApp, ui: &mut egui::Ui, row: Rect, which: Which)
}
}
+/// The Source panel menu's clips (#313): the recently opened clips, the shown one ticked, to
+/// switch between, then Close / Close All. Returns whether the menu is done.
+pub fn source_panel_menu(app: &mut FilmcraftApp, ui: &mut egui::Ui) -> bool {
+ let mut done = false;
+ let current = app.session.state.source_item;
+ let recent: Vec<(u64, String)> =
+ app.session.state.source_history.iter().filter_map(|i| app.session.project.item(*i).map(|it| (i.0, it.name.clone()))).collect();
+ for (id, name) in &recent {
+ let shown = current.is_some_and(|c| c.0 == *id);
+ let r = ui.button(if shown { format!("✓ {name}") } else { name.clone() });
+ app.auto.add(&format!("panel.menu.Source.recent.{id}"), r.rect, name);
+ if r.clicked() {
+ if !shown && let Err(e) = app.session.execute("source.open", json!({"item": id})) {
+ app.ui.status = e.to_string();
+ }
+ done = true;
+ }
+ }
+ if !recent.is_empty() {
+ ui.separator();
+ }
+ for (cmd, id, label) in [("source.close", "close", tl!("Close")), ("source.closeAll", "closeAll", tl!("Close All"))] {
+ let r = ui.add_enabled(current.is_some(), egui::Button::new(label));
+ app.auto.add(&format!("panel.menu.Source.{id}"), r.rect, label);
+ if r.clicked() {
+ if let Err(e) = app.session.execute(cmd, json!({})) {
+ app.ui.status = e.to_string();
+ }
+ done = true;
+ }
+ }
+ done
+}
+
fn source_nav(app: &mut FilmcraftApp, cmd: &str) {
let Some(item) = app.session.state.source_item else { return };
let Some(v) = filmcraft_engine::clip_ops::source_view(&app.session, item) else { return };