From 6d7de203308221e786c88751d01b45c3afa6efe4 Mon Sep 17 00:00:00 2001 From: Tarek Ismail Date: Wed, 18 Dec 2024 19:31:46 +0200 Subject: [PATCH] Refactor `MinimalWindowManager::advise_new_window` according to feedback Mainly, reduced the calls to `Impl::prevent_focus_stealing` and `Impl::ApplicationSelector::advise_new_window` to one call. And renamed things to be easier to understand. --- src/miral/minimal_window_manager.cpp | 29 +++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/miral/minimal_window_manager.cpp b/src/miral/minimal_window_manager.cpp index 48355b8a7a..dbebccf478 100644 --- a/src/miral/minimal_window_manager.cpp +++ b/src/miral/minimal_window_manager.cpp @@ -104,7 +104,12 @@ struct miral::MinimalWindowManager::Impl void apply_resize_by(Displacement movement); - bool prevent_focus_stealing(WindowInfo const& info); + // Returns true if the window should NOT be in focus + // Relevant for focus stealing prevention where windows are focused on + // launch in specific circumstances. + bool should_prevent_focus(WindowInfo const& info); + + bool advise_new_window(WindowInfo const& info); MirInputEventModifier const pointer_drag_modifier; FocusStealing const focus_stealing; @@ -147,7 +152,7 @@ auto miral::MinimalWindowManager::place_new_window( void miral::MinimalWindowManager::handle_window_ready(WindowInfo& window_info) { - if (!self->prevent_focus_stealing(window_info) && window_info.can_be_active()) + if (!self->should_prevent_focus(window_info) && window_info.can_be_active()) { tools.select_active_window(window_info.window()); } @@ -297,16 +302,14 @@ auto miral::MinimalWindowManager::confirm_inherited_move(WindowInfo const& windo void miral::MinimalWindowManager::advise_new_window(miral::WindowInfo const& window_info) { + auto should_receive_focus = self->advise_new_window(window_info); + // If the window is prevented from stealing focus, swap the old focused // window (now in the back) with the new window in the front. // // If it's a legitimate window, it'll be focused and raised via - // xdg-activation. - auto in_background = self->prevent_focus_stealing(window_info); - - self->application_selector.advise_new_window(window_info, in_background); - - if (in_background) + // xdg-activation later on. + if (!should_receive_focus) tools.swap_tree_order(tools.active_window(), window_info.window()); } @@ -638,7 +641,7 @@ void miral::MinimalWindowManager::Impl::apply_resize_by(Displacement movement) } } -bool miral::MinimalWindowManager::Impl::prevent_focus_stealing(miral::WindowInfo const& info) +bool miral::MinimalWindowManager::Impl::should_prevent_focus(miral::WindowInfo const& info) { auto const without_parent = !info.parent(); auto const decoration_surface = info.type() == mir_window_type_decoration; @@ -646,3 +649,11 @@ bool miral::MinimalWindowManager::Impl::prevent_focus_stealing(miral::WindowInfo (without_parent || decoration_surface) && info.depth_layer() == mir_depth_layer_application && tools.active_window().application() != info.window().application(); } + +bool miral::MinimalWindowManager::Impl::advise_new_window(WindowInfo const& info) +{ + auto should_receive_focus = !should_prevent_focus(info); + application_selector.advise_new_window(info, should_receive_focus); + + return should_receive_focus; +}