Skip to content

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

docking: floating panels open in their own OS windows

aiongg172ccf34560c
diff --git a/crates/ui-egui/src/dock_ui.rs b/crates/ui-egui/src/dock_ui.rs
index d397499f2..eab1379f9 100644
--- a/crates/ui-egui/src/dock_ui.rs
+++ b/crates/ui-egui/src/dock_ui.rs
@@ -218,6 +218,7 @@ impl FilmcraftApp {
active: self.session.state.active_sequence.map(|id| id.0),
};
let style = dock_style(ui, &t);
+ // No `monitors`: the monitor list comes with the egui fork's `ViewportInfo::monitors`.
let mut area =
DockArea::new(area_id()).float_bounds(self.float_bounds(&ctx)).maximized(self.ui.keys.maximized.filter(|panel| self.ui.dock.is_docked(*panel)));
if let Some(panel) = self.pending_panel_raise.take() {
@@ -235,6 +236,7 @@ impl FilmcraftApp {
if let Some(panel) = output.focused {
self.ui.focused = panel;
}
+ self.handle_forwarded_keys(&ctx, output.forwarded_events);
if let Some(error) = output.error {
self.ui.status = error.to_string();
}
@@ -437,6 +439,11 @@ impl DockContent<PanelKind> for FilmContent<'_> {
crate::i18n::t(panel.title()).to_string()
}
+ /// Every floating panel gets its own OS window on desktop.
+ fn allow_native(&self, _panel: &PanelKind) -> bool {
+ true
+ }
+
/// With no sequence open, the Timeline's tab says so.
fn label(&mut self, _ui: &egui::Ui, panel: &PanelKind, logical: &str, _font: &egui::FontId, _color: egui::Color32) -> egui::WidgetText {
if *panel == PanelKind::Timeline && self.seqs.open.is_empty() { self.seqs.label().into() } else { logical.into() }
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 830244b34..d1fc971ca 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1566,60 +1566,100 @@ impl FilmcraftApp {
let _ = self.session.execute("trim.cancelDynamic", json!({}));
}
panels::masks::pen_take_back(self, ctx, text_focused);
+ let fire = ctx.input_mut(|i| self.match_shortcuts(i, text_focused, divider_focused));
+ self.fire_shortcuts(ctx, fire);
+ }
+
+ /// Key, text and clipboard events that a floating panel in its own OS window left over
+ /// (`DockOutput::forwarded_events`): they run the main window's bindings in the frame they
+ /// arrive, so shortcuts work while a floating window has focus. craft-ui forwards nothing
+ /// while that window's text field has focus.
+ pub(crate) fn handle_forwarded_keys(&mut self, ctx: &egui::Context, events: Vec<egui::Event>) {
+ if events.is_empty() || self.dialog == Some(Dialog::Shortcuts) {
+ return;
+ }
+ let mut input = egui::InputState::default();
+ input.modifiers = self.floating_window_modifiers(ctx);
+ input.events = events;
+ if self.session.trim_play.dynamic.is_some() && input.consume_key(egui::Modifiers::NONE, egui::Key::Escape) {
+ let _ = self.session.execute("trim.cancelDynamic", json!({}));
+ }
+ let fire = self.match_shortcuts(&mut input, false, false);
+ self.fire_shortcuts(ctx, fire);
+ }
+
+ /// The modifiers held in the focused native floating window. Copy, Cut and Paste events
+ /// carry none, and egui-winit drops the key press they stand for.
+ fn floating_window_modifiers(&self, ctx: &egui::Context) -> egui::Modifiers {
+ self.ui
+ .dock
+ .floating
+ .iter()
+ .filter(|window| window.native.is_some())
+ .map(|window| craft_ui::docking::DockArea::<PanelKind>::window_viewport(dock_ui::area_id(), window.id))
+ .find_map(|viewport| ctx.input_for(viewport, |i| (i.viewport().focused == Some(true)).then_some(i.modifiers)))
+ .unwrap_or_default()
+ }
+
+ /// The binding ids the key presses in `i` fire, consuming those presses.
+ fn match_shortcuts(&mut self, i: &mut egui::InputState, text_focused: bool, divider_focused: bool) -> Vec<String> {
// Panel shortcuts of the focused panel override application shortcuts on the same chord.
// egui ignores extra Shift and Alt when matching, so the most specific chords go first
// across both scopes (the sort is stable, so the panel still wins a tie): otherwise the
// Timeline's `Delete` (Clear) swallows the app-wide `Shift+Delete` (Ripple Delete) (#680).
let focused = self.ui.focused.title();
let mut fire = Vec::new();
- ctx.input_mut(|i| {
- let modifiers = i.modifiers;
- // With no text on the system clipboard, egui-winit drops the Ctrl+V press but still
- // passes its release: put the press back, so Paste can paste files or an image (#611).
- let mut swallowed = None;
- for e in &i.events {
- match e {
- egui::Event::Key { key: egui::Key::V, pressed, modifiers: m, .. } => {
- if !pressed && !self.v_down {
- swallowed = Some(*m);
- }
- self.v_down = *pressed;
+ let modifiers = i.modifiers;
+ // With no text on the system clipboard, egui-winit drops the Ctrl+V press but still
+ // passes its release: put the press back, so Paste can paste files or an image (#611).
+ let mut swallowed = None;
+ for e in &i.events {
+ match e {
+ egui::Event::Key { key: egui::Key::V, pressed, modifiers: m, .. } => {
+ if !pressed && !self.v_down {
+ swallowed = Some(*m);
}
- egui::Event::Paste(_) if modifiers.command => self.v_down = true,
- _ => {}
+ self.v_down = *pressed;
}
+ egui::Event::Paste(_) if modifiers.command => self.v_down = true,
+ _ => {}
}
- if let Some(m) = swallowed {
- i.events.push(egui::Event::Key {
- key: egui::Key::V,
- physical_key: Some(egui::Key::V),
- pressed: true,
- repeat: false,
- modifiers: m | egui::Modifiers::COMMAND,
- });
+ }
+ if let Some(m) = swallowed {
+ i.events.push(egui::Event::Key {
+ key: egui::Key::V,
+ physical_key: Some(egui::Key::V),
+ pressed: true,
+ repeat: false,
+ modifiers: m | egui::Modifiers::COMMAND,
+ });
+ }
+ clipboard_events_as_keys(&mut i.events, modifiers);
+ let panel = self.bindings.iter().filter(|b| b.3.as_deref() == Some(focused));
+ let app_wide = self.bindings.iter().filter(|b| b.3.is_none());
+ let mut candidates: Vec<&menus::KeyBinding> = panel.chain(app_wide).collect();
+ candidates.sort_by_key(|b| std::cmp::Reverse(menus::specificity(b.0)));
+ for (m, k, id, _) in candidates {
+ // With a text field focused, an unmodified key belongs to the field (and to the input
+ // method composing in it), not to a tool shortcut: `J`, `Z`, `R`, `A`… are the keys
+ // Vietnamese Telex combines into diacritics (#243). Chords with a modifier stay:
+ // Ctrl+S and friends are not something a text field or an IME consumes.
+ if lets_text_field_keep(*m, text_focused) {
+ continue;
}
- clipboard_events_as_keys(&mut i.events, modifiers);
- let panel = self.bindings.iter().filter(|b| b.3.as_deref() == Some(focused));
- let app_wide = self.bindings.iter().filter(|b| b.3.is_none());
- let mut candidates: Vec<&menus::KeyBinding> = panel.chain(app_wide).collect();
- candidates.sort_by_key(|b| std::cmp::Reverse(menus::specificity(b.0)));
- for (m, k, id, _) in candidates {
- // With a text field focused, an unmodified key belongs to the field (and to the input
- // method composing in it), not to a tool shortcut: `J`, `Z`, `R`, `A`… are the keys
- // Vietnamese Telex combines into diacritics (#243). Chords with a modifier stay:
- // Ctrl+S and friends are not something a text field or an IME consumes.
- if lets_text_field_keep(*m, text_focused) {
- continue;
- }
- // The focused divider consumes the arrows itself (egui matches them with any Shift/Alt).
- if divider_focused && matches!(k, egui::Key::ArrowLeft | egui::Key::ArrowRight | egui::Key::ArrowUp | egui::Key::ArrowDown) {
- continue;
- }
- if i.consume_key(*m, *k) {
- fire.push(id.clone());
- }
+ // The focused divider consumes the arrows itself (egui matches them with any Shift/Alt).
+ if divider_focused && matches!(k, egui::Key::ArrowLeft | egui::Key::ArrowRight | egui::Key::ArrowUp | egui::Key::ArrowDown) {
+ continue;
}
- });
+ if i.consume_key(*m, *k) {
+ fire.push(id.clone());
+ }
+ }
+ fire
+ }
+
+ /// Run the commands of the bindings `match_shortcuts` fired, as the focused panel scopes them.
+ fn fire_shortcuts(&mut self, ctx: &egui::Context, fire: Vec<String>) {
for mut id in fire {
// Select All / Deselect All act on the Project panel's items when it has focus (#168).
// Select All takes only what the shown bin lists, not the whole project (#456).
@@ -2365,6 +2405,22 @@ mod clipboard_key_tests {
}
}
+#[cfg(test)]
+mod forwarded_key_tests {
+ use egui::{Event, Key, Modifiers};
+
+ /// A key a floating native window left over runs the main window's bindings.
+ #[test]
+ fn forwarded_keys_fire_shortcuts() {
+ let mut app = super::FilmcraftApp::new(filmcraft_engine::Session::default());
+ app.bindings = super::menus::bindings(&app);
+ app.ui.focused = crate::dock::PanelKind::Timeline;
+ let shift_backtick = Event::Key { key: Key::Backtick, physical_key: Some(Key::Backtick), pressed: true, repeat: false, modifiers: Modifiers::SHIFT };
+ app.handle_forwarded_keys(&egui::Context::default(), vec![Event::Text("~".into()), shift_backtick]);
+ assert_eq!(app.ui.keys.maximized, Some(crate::dock::PanelKind::Timeline), "Shift+` maximizes the focused frame");
+ }
+}
+
#[cfg(test)]
mod mac_ctrl_click_tests {
use egui::{Event, Modifiers, PointerButton, RawInput};