Skip to content

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

UI.9: tab menus carry FilmCraft's panel items; closing the Timeline closes its sequence

aionggb35b463e8d31
diff --git a/crates/ui-egui/src/dock_ui.rs b/crates/ui-egui/src/dock_ui.rs
index d9334a863..a95e85f8d 100644
--- a/crates/ui-egui/src/dock_ui.rs
+++ b/crates/ui-egui/src/dock_ui.rs
@@ -1,6 +1,7 @@
//! FilmCraft's panels in craft-ui's shared docking, Frame profile. craft-ui draws the frames, tab
//! strips, floating windows and drag previews and owns the gestures; FilmCraft draws the panel
-//! bodies, the ≡ panel menu and the Timeline's sequence tabs, and maps its control commands onto
+//! bodies, the ≡ panel menu, the panel items of each tab's context menu and the Timeline's
+//! sequence tabs, and maps its control commands onto
//! craft-ui's `DockCommand`.
use craft_ui::docking::{Action, Divider, DockArea, DockCommand, DockContent, DockEvent, DockStyle, FloatSpace, Hit, Layout, Profile, ResizeEdge, Workspace};
@@ -217,9 +218,10 @@ impl FilmcraftApp {
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(body).id_salt("filmcraft-dock-area"));
child.set_clip_rect(body.intersect(ui.clip_rect()));
let seq_width = dock::sequence_tabs_width(ui, &seqs);
- let mut content = FilmContent { app: self, seqs, seq_width, tokens: t, seq_actions: Vec::new(), menu: None, bodies: Vec::new() };
+ let mut content =
+ FilmContent { app: self, seqs, seq_width, tokens: t, seq_actions: Vec::new(), menu: None, bodies: Vec::new(), restore_workspace: false };
let output = area.show_content(&mut child, &layout, &style, &mut content);
- let FilmContent { seq_actions, mut menu, bodies, .. } = content;
+ let FilmContent { seq_actions, mut menu, bodies, restore_workspace, .. } = content;
ctx.data_mut(|data| data.insert_temp(bodies_id(), bodies));
if let Some(panel) = output.focused {
self.ui.focused = panel;
@@ -232,19 +234,17 @@ impl FilmcraftApp {
panels::keyboard::toggle_maximize(self, panel);
}
}
- let middle_click = ctx.input(|input| input.pointer.button_clicked(egui::PointerButton::Middle));
for action in output.actions {
- // A middle click on the Timeline's tab closes its active sequence; the panel stays.
- if middle_click && action == (Action::Close { panel: PanelKind::Timeline }) && self.session.state.active_sequence.is_some() {
- if let Err(error) = self.session.execute("sequence.close", json!({})) {
- self.ui.status = error.to_string();
+ for action in self.close_keeping_timeline(action) {
+ if let Err(error) = self.dock_command(&ctx, action) {
+ self.ui.status = error;
}
- continue;
- }
- if let Err(error) = self.dock_command(&ctx, action) {
- self.ui.status = error;
}
}
+ if restore_workspace {
+ let workspace = self.ui.workspace.clone();
+ self.set_workspace(&workspace);
+ }
for action in seq_actions {
let result = match action {
SeqAction::Open(id) => {
@@ -285,6 +285,24 @@ impl FilmcraftApp {
self.cancel_value_drag(ui.ctx());
}
+ /// The Timeline's tabs stand for its open sequences: a close from the dock area (a tab's
+ /// middle click or Close Panel, a group, window or Close Other Panels item) that would
+ /// remove the Timeline while a sequence is open closes the active sequence instead, and the
+ /// Timeline stays. The other panels the action covers still close.
+ fn close_keeping_timeline(&mut self, action: Action<PanelKind>) -> Vec<Action<PanelKind>> {
+ if self.session.state.active_sequence.is_none() {
+ return vec![action];
+ }
+ let closed = match self.ui.dock.closed_by(&action) {
+ Ok(closed) if closed.contains(&PanelKind::Timeline) => closed,
+ _ => return vec![action],
+ };
+ if let Err(error) = self.session.execute("sequence.close", json!({})) {
+ self.ui.status = error.to_string();
+ }
+ closed.into_iter().filter(|panel| *panel != PanelKind::Timeline).map(|panel| Action::Close { panel }).collect()
+ }
+
/// Automation ids for craft-ui's gesture sources: `panel.tab.<Panel>`, `panel.drag.<Panel>`
/// (the group's drag bar), `panel.tabs.more.<Panel>`, `panel.float.<n>.move` and `.resize`,
/// and `dock.gutter.<path>` (`a`/`b` per split level).
@@ -351,6 +369,8 @@ struct FilmContent<'a> {
/// The ≡ button clicked: its panel and where its menu opens.
menu: Option<(PanelKind, egui::Pos2)>,
bodies: Vec<(PanelKind, Rect, egui::LayerId)>,
+ /// Restore Workspace chosen in a tab's menu; it replaces the layout after the dock area is drawn.
+ restore_workspace: bool,
}
impl DockContent<PanelKind> for FilmContent<'_> {
@@ -393,6 +413,34 @@ impl DockContent<PanelKind> for FilmContent<'_> {
if *panel == PanelKind::Timeline && self.seqs.open.is_empty() { self.seqs.label().into() } else { logical.into() }
}
+ /// FilmCraft's own panel items head a tab's context menu, as in its ≡ menu; craft-ui adds
+ /// Undock, Move, Close and the group items after them.
+ fn panel_menu(&mut self, ui: &mut egui::Ui, panel: &PanelKind) {
+ let app = &mut *self.app;
+ let mut close = false;
+ if *panel == PanelKind::Timeline && app.session.state.active_sequence.is_some() {
+ let response = ui.add_enabled(app.session.state.open_sequences.len() > 1, egui::Button::new(tl!("Close Other Timeline Panels")));
+ app.auto.add_response("panel.tabMenu.Timeline.closeOthers", &response, "Close Other Timeline Panels");
+ if response.clicked() {
+ if let Err(error) = app.session.execute("sequence.closeOthers", json!({})) {
+ app.ui.status = error.to_string();
+ }
+ close = true;
+ }
+ }
+ let response = ui.button(tl!("Restore Workspace"));
+ app.auto.add_response(&format!("panel.tabMenu.{}.restoreWorkspace", panel.id()), &response, "Restore Workspace");
+ if response.clicked() {
+ self.restore_workspace = true;
+ close = true;
+ }
+ close |= panels::panel_items(app, ui, *panel);
+ ui.separator();
+ if close {
+ ui.close();
+ }
+ }
+
/// Tools and the audio meters are narrow strips without a tab strip.
fn bare(&self, panel: &PanelKind) -> bool {
panel.compact()
diff --git a/crates/ui-egui/src/panels/mod.rs b/crates/ui-egui/src/panels/mod.rs
index 7a7f7a0be..515b08572 100644
--- a/crates/ui-egui/src/panels/mod.rs
+++ b/crates/ui-egui/src/panels/mod.rs
@@ -321,29 +321,7 @@ pub fn panel_menu_popup(app: &mut FilmcraftApp, ui: &mut egui::Ui) {
app.set_workspace(&w);
close = true;
}
- if p == PanelKind::Timeline {
- ui.separator();
- let r = ui.add_enabled(app.session.state.active_sequence.is_some(), egui::Button::new(tl!("Reveal Sequence in Project")));
- app.auto.add("panel.menu.Timeline.revealSequence", r.rect, "Reveal Sequence in Project");
- if r.clicked() {
- let _ = app.session.execute("sequence.revealInProject", serde_json::json!({}));
- close = true;
- }
- ui.checkbox(&mut app.ui.timeline.show_thumbnails, tl!("Video Thumbnails"));
- ui.checkbox(&mut app.ui.timeline.show_waveforms, tl!("Audio Waveforms"));
- }
- if p == PanelKind::Project {
- ui.separator();
- close |= project::panel_menu(app, ui);
- }
- if p == PanelKind::MediaBrowser {
- ui.separator();
- close |= media_browser::panel_menu(app, ui);
- }
- if p == PanelKind::Source {
- ui.separator();
- close |= monitor::source_panel_menu(app, ui);
- }
+ close |= panel_items(app, ui, p);
});
app.auto.add("panel.menu.scroll", scroll.inner_rect, "Panel menu");
});
@@ -356,4 +334,35 @@ pub fn panel_menu_popup(app: &mut FilmcraftApp, ui: &mut egui::Ui) {
}
}
+/// The panel's own items, each section after a separator: the Timeline's sequence and track
+/// display items, and the Project, Media Browser and Source menus. Shared by the ≡ menu and the
+/// tab's context menu. True when an item asks the menu to close.
+pub(crate) fn panel_items(app: &mut FilmcraftApp, ui: &mut egui::Ui, p: PanelKind) -> bool {
+ let mut close = false;
+ if p == PanelKind::Timeline {
+ ui.separator();
+ let r = ui.add_enabled(app.session.state.active_sequence.is_some(), egui::Button::new(tl!("Reveal Sequence in Project")));
+ app.auto.add("panel.menu.Timeline.revealSequence", r.rect, "Reveal Sequence in Project");
+ if r.clicked() {
+ let _ = app.session.execute("sequence.revealInProject", serde_json::json!({}));
+ close = true;
+ }
+ ui.checkbox(&mut app.ui.timeline.show_thumbnails, tl!("Video Thumbnails"));
+ ui.checkbox(&mut app.ui.timeline.show_waveforms, tl!("Audio Waveforms"));
+ }
+ if p == PanelKind::Project {
+ ui.separator();
+ close |= project::panel_menu(app, ui);
+ }
+ if p == PanelKind::MediaBrowser {
+ ui.separator();
+ close |= media_browser::panel_menu(app, ui);
+ }
+ if p == PanelKind::Source {
+ ui.separator();
+ close |= monitor::source_panel_menu(app, ui);
+ }
+ close
+}
+
pub(crate) mod transcript_cache;
diff --git a/crates/ui-egui/tests/shared_docking_ui.rs b/crates/ui-egui/tests/shared_docking_ui.rs
index c2e1bc35e..e51a21cbb 100644
--- a/crates/ui-egui/tests/shared_docking_ui.rs
+++ b/crates/ui-egui/tests/shared_docking_ui.rs
@@ -757,3 +757,60 @@ fn activate_reopens_a_closed_panel() {
menus::invoke(&mut app, &ctx, "window.panel.activate", json!({"panel": "Info"})).unwrap();
assert!(app.ui.dock.is_visible(PanelKind::Info));
}
+
+/// Press and release `button` at `pos`.
+fn click_at(h: &mut Harness<'_, FilmcraftApp>, pos: egui::Pos2, button: PointerButton) {
+ h.event(Event::PointerMoved(pos));
+ step(h);
+ for pressed in [true, false] {
+ h.event(Event::PointerButton { pos, button, pressed, modifiers: Modifiers::NONE });
+ step(h);
+ }
+ for _ in 0..3 {
+ step(h);
+ }
+}
+
+/// Right-click the tab `tab` and choose the context-menu item labeled `item`.
+fn tab_menu(h: &mut Harness<'_, FilmcraftApp>, tab: &str, item: &str) {
+ let pos = rect(h, tab).center();
+ click_at(h, pos, PointerButton::Secondary);
+ let item = h.get_by_label(item).rect().center();
+ click_at(h, item, PointerButton::Primary);
+}
+
+/// A Timeline tab's context menu carries FilmCraft's Timeline items, and its Close Panel closes
+/// the active sequence: the Timeline stays.
+#[test]
+fn close_panel_on_the_timeline_tab_closes_the_sequence() {
+ let mut h = demo_harness();
+ let active = h.state().session.state.active_sequence.unwrap();
+ let pos = rect(&h, "panel.tab.Timeline").center();
+ click_at(&mut h, pos, PointerButton::Secondary);
+ for id in ["panel.menu.Timeline.revealSequence", "panel.tabMenu.Timeline.closeOthers", "panel.tabMenu.Timeline.restoreWorkspace"] {
+ assert!(h.state().auto.find(id).is_some(), "{id} is missing from the tab menu");
+ }
+ let close = h.get_by_label("Close Panel").rect().center();
+ click_at(&mut h, close, PointerButton::Primary);
+ assert!(!h.state().session.state.open_sequences.contains(&active), "the sequence stays open");
+ assert!(h.state().ui.dock.is_visible(PanelKind::Timeline), "the Timeline closed");
+}
+
+/// Close Panel Group on a group holding the Timeline closes the active sequence and the group's
+/// other panels; the Timeline stays.
+#[test]
+fn close_panel_group_keeps_the_timeline_and_closes_the_sequence() {
+ let mut h = demo_harness();
+ let ctx = h.ctx.clone();
+ menus::invoke(h.state_mut(), &ctx, "window.dockPanel", json!({"panel": "Info", "anchor": "Timeline", "zone": "center"})).unwrap();
+ for _ in 0..3 {
+ step(&mut h);
+ }
+ assert!(same_group(&h.state().ui.dock, PanelKind::Info, PanelKind::Timeline));
+ let active = h.state().session.state.active_sequence.unwrap();
+ tab_menu(&mut h, "panel.tab.Info", "Close Panel Group");
+ let state = h.state();
+ assert!(!state.session.state.open_sequences.contains(&active), "the sequence stays open");
+ assert!(state.ui.dock.is_visible(PanelKind::Timeline), "the Timeline closed");
+ assert!(!state.ui.dock.contains(&PanelKind::Info), "Info stays open");
+}