Skip to content

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

Docking: a modal focuses the main window only when a floating panel window has the keyboard

aiongg3ed45a7e3fb7
diff --git a/crates/ui-egui/src/panel_docking.rs b/crates/ui-egui/src/panel_docking.rs
index d9af2c480..a0a3f8854 100644
--- a/crates/ui-egui/src/panel_docking.rs
+++ b/crates/ui-egui/src/panel_docking.rs
@@ -1048,8 +1048,9 @@ pub(crate) fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
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) {
+ let floats = native_viewports(app, ui.ctx());
+ let float_focused = ui.ctx().input(|i| floats.iter().any(|v| i.raw.viewports.get(v).and_then(|v| v.focused) == Some(true)));
+ if focus_main_for_modal(was_open, modal, float_focused) {
ui.ctx().send_viewport_cmd_to(egui::ViewportId::ROOT, egui::ViewportCommand::Focus);
}
if modal || egui::Popup::is_any_open(ui.ctx()) {
@@ -1117,9 +1118,10 @@ pub(crate) fn modal_open(app: &DesignApp) -> bool {
}
/// 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)
+/// true) while one of the floating panels' own windows has the keyboard. With the app in the
+/// background nothing is focused, so a modal that opens on its own doesn't pull the app forward.
+pub(crate) fn focus_main_for_modal(was_open: bool, open: bool, float_focused: bool) -> bool {
+ open && !was_open && float_focused
}
/// What the control channel reports: the layout in use (craft-ui's serialized `Layout`),
diff --git a/crates/ui-egui/src/panel_docking_tests.rs b/crates/ui-egui/src/panel_docking_tests.rs
index 8bdbb57b7..3b4f863cc 100644
--- a/crates/ui-egui/src/panel_docking_tests.rs
+++ b/crates/ui-egui/src/panel_docking_tests.rs
@@ -1277,13 +1277,12 @@ fn a_document_dropped_on_a_floating_panel_window_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.
+/// window to the front, so the modal gets the keys. Once open, with the main window focused or
+/// with the app in the background (no float 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)));
+ assert!(focus_main_for_modal(false, true, true));
+ assert!(!focus_main_for_modal(false, true, false), "main window focused, or the app in the background");
+ assert!(!focus_main_for_modal(true, true, true), "only on the frame it opens");
+ assert!(!focus_main_for_modal(false, false, true));
}