From 121be81389416e59dc4d1822bf7fdf99d62f0322 Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 25 Nov 2026 14:22:08 +0000 Subject: [PATCH] The focused Composition viewer leaves the keyboard to the app --- crates/ui-egui/src/framework.rs | 30 ++++++++++++-- crates/ui-egui/src/menus/shortcuts.rs | 4 +- crates/ui-egui/src/native_menu.rs | 4 +- crates/ui-egui/src/panels.rs | 2 +- .../ui-egui/src/panels/camera_tracker_ui.rs | 2 +- crates/ui-egui/src/panels/effects_presets.rs | 2 +- crates/ui-egui/src/panels/flowchart.rs | 2 +- crates/ui-egui/src/panels/project.rs | 6 +-- crates/ui-egui/src/panels/render_queue.rs | 4 +- crates/ui-egui/src/panels/viewer.rs | 6 +-- crates/ui-egui/src/panels/viewer_tools.rs | 2 +- crates/ui-egui/tests/it/ui_viewer.rs | 39 +++++++++++++++++++ 12 files changed, 82 insertions(+), 21 deletions(-) diff --git a/crates/ui-egui/src/framework.rs b/crates/ui-egui/src/framework.rs index 0e54530ae..de2981220 100644 --- a/crates/ui-egui/src/framework.rs +++ b/crates/ui-egui/src/framework.rs @@ -115,12 +115,34 @@ impl EffectcraftApp { } } -/// Declares the Composition viewer `id`, drawn in this pass, as the window's document canvas. With nothing focused it +/// The Composition viewer's widget, its window's document canvas. +pub(crate) fn viewer_id() -> egui::Id { + egui::Id::unique("viewer-interact") +} + +/// Declares the Composition viewer, drawn in this pass, as the window's document canvas. With nothing focused it /// is the window's key target: the edit chords and the clipboard events stay in -/// egui's input for EffectCraft's shortcuts. -pub(crate) fn host_canvas(ctx: &egui::Context, id: egui::Id) { +/// egui's input for EffectCraft's shortcuts. Escape with the viewer focused is the viewer's own: it lets go of the +/// keyboard, as egui's other widgets do. +pub(crate) fn host_canvas(ctx: &egui::Context) { const KEYS: KeyUse = KeyUse::CANVAS.with_edits(EditSet::ALL); - craft_ui::runtime::focus::host_canvas(ctx, id, KEYS); + craft_ui::runtime::focus::host_canvas(ctx, viewer_id(), KEYS); + // Read from the raw input, so an Escape that a popup consumed earlier in the pass counts. + let escape = |event: &egui::Event| matches!(event, egui::Event::Key { key: egui::Key::Escape, pressed: true, modifiers, .. } if !modifiers.any()); + if ctx.input(|i| i.raw.events.iter().any(escape)) { + ctx.memory_mut(|m| m.surrender_focus(viewer_id())); + } +} + +/// The widget other than the Composition viewer that has the keyboard. The viewer as the focused Tab stop leaves the +/// keys to EffectCraft, as with nothing focused. +pub(crate) fn focused_widget(ctx: &egui::Context) -> Option { + ctx.memory(|m| m.focused()).filter(|id| *id != viewer_id()) +} + +/// Whether a widget other than the Composition viewer has the keyboard ([`focused_widget`]). +pub(crate) fn widget_has_keyboard(ctx: &egui::Context) -> bool { + focused_widget(ctx).is_some() } /// Puts the window's Escape (the key the runtime routes to no component while nothing has framework focus) back into diff --git a/crates/ui-egui/src/menus/shortcuts.rs b/crates/ui-egui/src/menus/shortcuts.rs index eaca5568a..655d5c0b9 100644 --- a/crates/ui-egui/src/menus/shortcuts.rs +++ b/crates/ui-egui/src/menus/shortcuts.rs @@ -356,12 +356,12 @@ fn handle_shortcuts_impl(app: &mut EffectcraftApp, ctx: &egui::Context, tab_only } // A mouse button down while Spacebar is held belongs to the Hand tool: its release won't // preview. - if ctx.input(|i| i.pointer.any_down()) || ctx.egui_wants_keyboard_input() { + if ctx.input(|i| i.pointer.any_down()) || crate::framework::widget_has_keyboard(ctx) { ctx.data_mut(|d| d.remove::(space_tap_id())); } // While a menu or popup list is open, keys go to it, not to the app (Space must not start a // preview behind the menu, #279). - if ctx.egui_wants_keyboard_input() || crate::widgets::any_menu_open(ctx) { + if crate::framework::widget_has_keyboard(ctx) || crate::widgets::any_menu_open(ctx) { return; } // Text editing in the viewer takes the clipboard events itself. diff --git a/crates/ui-egui/src/native_menu.rs b/crates/ui-egui/src/native_menu.rs index 440a3a4cd..23e2a3512 100644 --- a/crates/ui-egui/src/native_menu.rs +++ b/crates/ui-egui/src/native_menu.rs @@ -441,12 +441,12 @@ impl MenuSync { /// The window whose text field has the keyboard: a floating panel's own OS window, or the main /// window, where any widget that holds the keyboard counts (the Viewer's text editor is not an -/// egui text field). +/// egui text field) except the Composition viewer itself. pub fn text_field_window(ctx: &egui::Context) -> Option { let host = craft_ui::runtime::windows::WindowHost::of(ctx); let window = host.key_window(); let floating = (window != egui::ViewportId::ROOT && host.typing()).then_some(window); - floating.or_else(|| ctx.egui_wants_keyboard_input().then_some(egui::ViewportId::ROOT)) + floating.or_else(|| crate::framework::widget_has_keyboard(ctx).then_some(egui::ViewportId::ROOT)) } /// Whether a focused text field takes the menu command `command` ([`activate`]): the native menu diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index b47492f53..d1bc695c0 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -184,7 +184,7 @@ pub(crate) fn toggle_time_display(app: &mut EffectcraftApp, resp: &egui::Respons /// Spacebar held outside a text field: the Hand tool (drags pan the viewer and scroll the /// Timeline). pub(crate) fn space_hand(ctx: &egui::Context) -> bool { - ctx.input(|i| i.key_down(egui::Key::Space)) && !ctx.egui_wants_keyboard_input() + ctx.input(|i| i.key_down(egui::Key::Space)) && !crate::framework::widget_has_keyboard(ctx) } /// What a scroll gesture over a viewer (Composition, Layer) does this frame. diff --git a/crates/ui-egui/src/panels/camera_tracker_ui.rs b/crates/ui-egui/src/panels/camera_tracker_ui.rs index fdda5f5d2..75344e79a 100644 --- a/crates/ui-egui/src/panels/camera_tracker_ui.rs +++ b/crates/ui-egui/src/panels/camera_tracker_ui.rs @@ -347,7 +347,7 @@ pub fn viewer_hook( && app.dialog.is_none() && !selected.is_empty() && ui.input(|i| i.key_pressed(egui::Key::Delete) || i.key_pressed(egui::Key::Backspace)) - && !ctx.egui_wants_keyboard_input() + && !crate::framework::widget_has_keyboard(&ctx) { run(app, &ctx, "camera.deletePoints", json!({"layer": a.layer.0, "effect": a.uid, "points": selected})); } diff --git a/crates/ui-egui/src/panels/effects_presets.rs b/crates/ui-egui/src/panels/effects_presets.rs index 165c7fb1b..9d4981a58 100644 --- a/crates/ui-egui/src/panels/effects_presets.rs +++ b/crates/ui-egui/src/panels/effects_presets.rs @@ -422,7 +422,7 @@ fn list_keys(app: &mut EffectcraftApp, ctx: &egui::Context, nav: &Nav, list: Rec ctx.data_mut(|d| d.remove::(cursor_id())); ctx.request_repaint(); } - let focused = ctx.memory(|m| m.focused()); + let focused = crate::framework::focused_widget(ctx); let on_row = focused.is_some_and(|f| nav.rows.iter().any(|r| r.id == f || r.star == Some(f))); // Enter / Space on the star are the star button's (it toggles the favourite), not the row's. let on_star = focused.is_some_and(|f| nav.rows.iter().any(|r| r.star == Some(f))); diff --git a/crates/ui-egui/src/panels/flowchart.rs b/crates/ui-egui/src/panels/flowchart.rs index d6d6fca01..6ad58bbb8 100644 --- a/crates/ui-egui/src/panels/flowchart.rs +++ b/crates/ui-egui/src/panels/flowchart.rs @@ -349,7 +349,7 @@ fn flow_keys( rects: &[Rect], cmds: &mut Vec<(String, Value)>, ) { - let focused = ctx.memory(|m| m.focused()); + let focused = crate::framework::focused_widget(ctx); let on_node = focused.is_some_and(|f| g.nodes.iter().any(|n| node_id(&n.id) == f)); let by_mouse = focused.is_none() && app.ui.focused == crate::dock::PanelKind::Flowchart; if (on_node || by_mouse) && app.dialog.is_none() && !widgets::any_menu_open(ctx) { diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs index 3567e2dcc..d22ec7d0e 100644 --- a/crates/ui-egui/src/panels/project.rs +++ b/crates/ui-egui/src/panels/project.rs @@ -293,10 +293,10 @@ fn selection_seen_id() -> egui::Id { egui::Id::unique("proj-sel-seen") } -/// The focused widget is one of the panel's rows, or nothing has keyboard focus: the panel's keys +/// The focused widget is one of the panel's rows, or no widget has the keyboard: the panel's keys /// apply. Any other focused widget (a text field, a button reached with Tab) keeps them. fn keys_reach_rows(app: &EffectcraftApp, ctx: &egui::Context) -> bool { - match ctx.memory(|m| m.focused()) { + match crate::framework::focused_widget(ctx) { None => app.ui.focused == crate::dock::PanelKind::Project, Some(f) => ctx.data(|d| d.get_temp::(focused_row_id())).is_some_and(|row| row_id(row) == f), } @@ -330,7 +330,7 @@ fn keyboard_nav(app: &mut EffectcraftApp, ctx: &egui::Context) -> Option { return None; } - let row_focused = ctx.memory(|m| m.focused()).is_some(); + let row_focused = crate::framework::widget_has_keyboard(ctx); let mut moved = None; for (key, m) in keys { let (plain, shift) = (!m.any(), m.shift_only()); diff --git a/crates/ui-egui/src/panels/render_queue.rs b/crates/ui-egui/src/panels/render_queue.rs index 6c5963908..2b1ac4826 100644 --- a/crates/ui-egui/src/panels/render_queue.rs +++ b/crates/ui-egui/src/panels/render_queue.rs @@ -746,7 +746,7 @@ fn delete_selected(app: &EffectcraftApp, ui: &egui::Ui, allowed: bool, on_row: b && !crate::widgets::any_menu_open(ui.ctx()) && ui.input(|i| i.key_pressed(egui::Key::Delete) || i.key_pressed(egui::Key::Backspace)) // (A focused queue row is not typing; any other focused widget may be.) - && (on_row || !ui.ctx().egui_wants_keyboard_input()) + && (on_row || !crate::framework::widget_has_keyboard(ui.ctx())) { actions.push(("renderQueue.remove", json!({"item": id}))); *selected = None; @@ -767,7 +767,7 @@ fn list_keys( tops: &[(u64, f32)], list_h: f32, ) -> bool { - let focused = ctx.memory(|m| m.focused()); + let focused = crate::framework::focused_widget(ctx); let on_row = focused.is_some_and(|f| queue.iter().any(|i| row_id(i.id) == f)); let by_mouse = focused.is_none() && app.ui.focused == crate::dock::PanelKind::RenderQueue; if (on_row || by_mouse) && can_navigate && app.dialog.is_none() && !widgets::any_menu_open(ctx) { diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index 41c2f2ab1..45abbc049 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -547,7 +547,7 @@ fn phase_layers(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, v: let sel_vertices = app.session.state.selected_vertices.clone(); // Pen path in progress ends when the tool changes or its mask is gone; Esc / Enter also end // free transform. - let finish = ui.input(|i| i.key_pressed(egui::Key::Escape) || i.key_pressed(egui::Key::Enter)) && !ctx.egui_wants_keyboard_input(); + let finish = ui.input(|i| i.key_pressed(egui::Key::Escape) || i.key_pressed(egui::Key::Enter)) && !crate::framework::widget_has_keyboard(&ctx); if app.ui.tool != Tool::Pen || finish { ui.data_mut(|d| d.remove::<(LayerId, u64)>(pen_id())); } @@ -884,8 +884,8 @@ fn phase_menu(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, v: &m super::viewer_drop::show(app, ui, &painter, &map, ectx); // Interaction. - let resp = ui.interact(area, egui::Id::unique("viewer-interact"), Sense::click_and_drag()); - crate::framework::host_canvas(ui.ctx(), resp.id); + let resp = ui.interact(area, crate::framework::viewer_id(), Sense::click_and_drag()); + crate::framework::host_canvas(ui.ctx()); let menu_hits_id = egui::Id::unique(("viewer-menu-hits", cid.0)); // A right-click an overlay took (the Cryptomatte eyedropper's) opens the menu too. let forwarded = ui.data_mut(|d| d.remove_temp::(egui::Id::unique(FORWARDED_SECONDARY))); diff --git a/crates/ui-egui/src/panels/viewer_tools.rs b/crates/ui-egui/src/panels/viewer_tools.rs index 1901eb01b..2bf1df122 100644 --- a/crates/ui-egui/src/panels/viewer_tools.rs +++ b/crates/ui-egui/src/panels/viewer_tools.rs @@ -346,7 +346,7 @@ pub(crate) fn shown_region(ctx: &egui::Context, comp: [f32; 2]) -> [f64; 4] { /// Show Snapshot: the stored snapshot is on screen (button or F5 held). pub(crate) fn showing_snapshot(app: &EffectcraftApp, ctx: &egui::Context) -> bool { - let f5 = ctx.input(|i| i.key_down(egui::Key::F5)) && !ctx.egui_wants_keyboard_input(); + let f5 = ctx.input(|i| i.key_down(egui::Key::F5)) && !crate::framework::widget_has_keyboard(ctx); app.session.snapshot.is_some() && (app.session.state.viewer.show_snapshot || f5) } diff --git a/crates/ui-egui/tests/it/ui_viewer.rs b/crates/ui-egui/tests/it/ui_viewer.rs index ef59cf6cf..7071af7d2 100644 --- a/crates/ui-egui/tests/it/ui_viewer.rs +++ b/crates/ui-egui/tests/it/ui_viewer.rs @@ -278,6 +278,45 @@ fn viewer_double_click_on_a_covered_path_of_the_selected_layer_free_transforms_i assert!(h.state().auto.find("viewer.freeTransform.handle.0").is_some(), "the free-transform box is up"); } +/// The Composition viewer as the focused Tab stop leaves the keys to the app, and Escape also makes it +/// let go of the keyboard. +#[test] +fn the_focused_viewer_leaves_the_keys_to_the_app() { + use craft_ui::runtime::focus::{FocusTarget, request}; + let viewer = egui::Id::unique("viewer-interact"); + let mut h = selection_harness(); + let layer = h.state().session.active_comp().unwrap().layers[0].id; + let s = &mut h.state_mut().session; + s.execute("layer.select", json!({"layers": [layer.0]})).unwrap(); + s.execute("mask.new", json!({"layer": layer.0, "vertices": [[10, 10], [70, 10], [70, 70], [10, 70]], "closed": true})).unwrap(); + h.run_steps(40); + let at = screen(&h, [320.0, 150.0]); + h.input_mut().events.push(Event::PointerMoved(at)); + h.step(); + for _ in 0..2 { + for pressed in [true, false] { + h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }); + } + h.step(); + } + h.run_steps(3); + assert!(h.state().auto.find("viewer.freeTransform.handle.0").is_some(), "the free-transform box is up"); + let focus_viewer = |h: &mut Harness<'_, EffectcraftApp>| { + request(&h.ctx, FocusTarget::Id(viewer)); + h.run_steps(2); + assert_eq!(h.ctx.memory(|m| m.focused()), Some(viewer)); + }; + focus_viewer(&mut h); + assert_eq!(effectcraft_ui_egui::native_menu::text_field_window(&h.ctx), None, "the Edit menu's commands are the app's"); + key(&mut h, egui::Key::Escape, egui::Modifiers::NONE); + assert!(h.state().auto.find("viewer.freeTransform.handle.0").is_none(), "Escape ends the free transform"); + assert_eq!(h.ctx.memory(|m| m.focused()), None, "and the viewer lets go of the keyboard"); + focus_viewer(&mut h); + key(&mut h, egui::Key::H, egui::Modifiers::NONE); + assert_eq!(h.state().ui.tool, Tool::Hand, "a tool shortcut runs"); + assert_eq!(h.ctx.memory(|m| m.focused()), Some(viewer)); +} + /// A double-click where a precomp layer covers the selected layer opens the precomp: like a single /// click, it acts on the topmost layer, which its first click selects (#1535). #[test]