diff --git a/src/miral/basic_window_manager.cpp b/src/miral/basic_window_manager.cpp index c4490d0e38..4aca8c3952 100644 --- a/src/miral/basic_window_manager.cpp +++ b/src/miral/basic_window_manager.cpp @@ -17,8 +17,10 @@ #include "basic_window_manager.h" #include "display_configuration_listeners.h" +#include "mir_toolkit/common.h" #include "miral/window_manager_tools.h" +#include #include #include #include @@ -291,25 +293,38 @@ void miral::BasicWindowManager::refocus( if (can_activate_window_for_session_in_workspace(application, workspaces_containing_window)) return; + auto const is_hidden = [this](auto const& w) + { + auto const& info = info_for(w); + return info.state() != mir_window_state_hidden || info.state() == mir_window_state_minimized; + }; + // Try to activate to recently active window of any application in a shared workspace { miral::Window new_focus; - mru_active_windows.enumerate([&](miral::Window& window) + auto workspaces_containing_window_mut = workspaces_containing_window; + // Need to sort only once + std::sort(workspaces_containing_window_mut.begin(), workspaces_containing_window_mut.end()); + + mru_active_windows.enumerate([&](miral::Window& other_window) { - // select_active_window() calls set_focus_to() which updates mru_active_windows and changes window - auto const w = window; + if(is_hidden(other_window)) return true; - for (auto const& workspace : workspaces_containing(w)) - { - for (auto const& ww : workspaces_containing_window) - { - if (ww == workspace) - { - return !(new_focus = select_active_window(w)); - } - } - } + auto workspaces_containing_other_window = workspaces_containing(other_window); + std::sort(workspaces_containing_other_window.begin(), workspaces_containing_other_window.end()); + + auto intersection = std::vector>(); + + std::set_intersection( + workspaces_containing_window_mut.begin(), + workspaces_containing_window_mut.end(), + workspaces_containing_other_window.begin(), + workspaces_containing_other_window.end(), + std::back_inserter(intersection)); + + if(!intersection.empty()) + return !(new_focus = select_active_window(other_window)); return true; }); @@ -328,14 +343,12 @@ void miral::BasicWindowManager::refocus( { // select_active_window() calls set_focus_to() which updates mru_active_windows and changes window auto const w = window; + if(is_hidden(w)) return true; return !(new_focus = select_active_window(w)); }); if (new_focus) return; } - - // Fallback to cycling through applications - focus_next_application(); } void miral::BasicWindowManager::erase(miral::WindowInfo const& info) @@ -2993,4 +3006,4 @@ void miral::BasicWindowManager::move_cursor_to(mir::geometry::PointF point) sink->handle_input(std::move(event)); }); }); -} \ No newline at end of file +}