Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Docking: a modal opened from a floating panel window focuses the main window

aiongg3b5d85d66427
diff --git a/crates/ui-egui/src/panel_docking.rs b/crates/ui-egui/src/panel_docking.rs
index 52dd988ff..d9af2c480 100644
--- a/crates/ui-egui/src/panel_docking.rs
+++ b/crates/ui-egui/src/panel_docking.rs
@@ -1043,6 +1043,15 @@ pub(crate) fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
let raise = app.ui.docking_raise.take();
let mut blocked = Vec::new();
let modal = modal_open(app);
+ // A modal opened from a floating panel's window (a double-click, a shortcut typed there)
+ // leaves the OS keyboard focus on that window, which takes no input while the modal is open.
+ let was_open_id = egui::Id::new("modal-was-open");
+ let was_open = ui.ctx().data(|d| d.get_temp::<bool>(was_open_id)).unwrap_or(false);
+ ui.ctx().data_mut(|d| d.insert_temp(was_open_id, modal));
+ let main_focused = ui.ctx().input_for(egui::ViewportId::ROOT, |i| i.viewport().focused);
+ if focus_main_for_modal(was_open, modal, main_focused) {
+ ui.ctx().send_viewport_cmd_to(egui::ViewportId::ROOT, egui::ViewportCommand::Focus);
+ }
if modal || egui::Popup::is_any_open(ui.ctx()) {
blocked.push(ui.ctx().content_rect());
} else if crate::story_editor::visible(app) {
@@ -1107,6 +1116,12 @@ pub(crate) fn modal_open(app: &DesignApp) -> bool {
app.ui.dialog.is_some() || app.ui.about || app.ui.palette.is_some()
}
+/// Whether to focus the main window: on the frame a modal opens (`was_open` false, `open`
+/// true) while the main window doesn't have the keyboard.
+pub(crate) fn focus_main_for_modal(was_open: bool, open: bool, main_focused: Option<bool>) -> bool {
+ open && !was_open && main_focused != Some(true)
+}
+
/// What the control channel reports: the layout in use (craft-ui's serialized `Layout`),
/// whether it is the workspace's default, the panel (or `canvas`) last clicked or selected, the
/// panels closed with a remembered place, the panel whose flyout is open, and where each
diff --git a/crates/ui-egui/src/panel_docking_tests.rs b/crates/ui-egui/src/panel_docking_tests.rs
index b3a1ee11b..8bdbb57b7 100644
--- a/crates/ui-egui/src/panel_docking_tests.rs
+++ b/crates/ui-egui/src/panel_docking_tests.rs
@@ -1275,3 +1275,15 @@ fn a_document_dropped_on_a_floating_panel_window_opens() {
open_window_drops(&mut app, &[files]);
assert_eq!(app.session.documents().len(), 1, "the dropped document opens");
}
+
+/// A modal that opens while a floating panel's own window has the keyboard brings the main
+/// window to the front, so the modal gets the keys; once open, or with the main window already
+/// focused, nothing is sent.
+#[test]
+fn a_modal_opening_from_a_floating_window_focuses_the_main_window() {
+ assert!(focus_main_for_modal(false, true, Some(false)));
+ assert!(focus_main_for_modal(false, true, None));
+ assert!(!focus_main_for_modal(false, true, Some(true)));
+ assert!(!focus_main_for_modal(true, true, Some(false)), "only on the frame it opens");
+ assert!(!focus_main_for_modal(false, false, Some(false)));
+}