From 3103119b7cdda67e1d43588ad07a80465626b3c2 Mon Sep 17 00:00:00 2001 From: Chris Naporlee <55722668+chrisn731@users.noreply.github.com> Date: Sat, 28 Mar 2026 11:30:25 -0400 Subject: input: allow focus to switch to most recently used window on closed (#13769) --- hyprtester/src/tests/main/layout.cpp | 38 +++++++++++++++++++++++++ src/config/supplementary/ConfigDescriptions.hpp | 8 +++--- src/layout/algorithm/Algorithm.cpp | 4 ++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/hyprtester/src/tests/main/layout.cpp b/hyprtester/src/tests/main/layout.cpp index 186d7034..5be29a6f 100644 --- a/hyprtester/src/tests/main/layout.cpp +++ b/hyprtester/src/tests/main/layout.cpp @@ -103,6 +103,43 @@ static void testPosPreserve() { Tests::killAllWindows(); } +static bool testFocusMRUAfterClose() { + NLog::log("{}Testing focus after close (MRU order)", Colors::GREEN); + + OK(getFromSocket("/reload")); + OK(getFromSocket("/keyword dwindle:default_split_ratio 1.25")); + OK(getFromSocket("/keyword input:focus_on_close 2")); + + EXPECT(!!Tests::spawnKitty("kitty_A"), true); + EXPECT(!!Tests::spawnKitty("kitty_B"), true); + EXPECT(!!Tests::spawnKitty("kitty_C"), true); + + OK(getFromSocket("/dispatch focuswindow class:kitty_A")); + OK(getFromSocket("/dispatch focuswindow class:kitty_B")); + OK(getFromSocket("/dispatch focuswindow class:kitty_C")); + + OK(getFromSocket("/dispatch killactive")); + Tests::waitUntilWindowsN(2); + + { + auto str = getFromSocket("/activewindow"); + EXPECT(str.contains("class: kitty_B"), true); + } + + OK(getFromSocket("/dispatch killactive")); + Tests::waitUntilWindowsN(1); + + { + auto str = getFromSocket("/activewindow"); + EXPECT(str.contains("class: kitty_A"), true); + } + + NLog::log("{}Killing all windows", Colors::YELLOW); + Tests::killAllWindows(); + EXPECT(Tests::windowCount(), 0); + return true; +} + static bool test() { NLog::log("{}Testing layout generic", Colors::GREEN); @@ -115,6 +152,7 @@ static bool test() { testCrashOnGeomUpdate(); testPosPreserve(); + testFocusMRUAfterClose(); // clean up NLog::log("Cleaning up", Colors::YELLOW); diff --git a/src/config/supplementary/ConfigDescriptions.hpp b/src/config/supplementary/ConfigDescriptions.hpp index 6a287d43..7d752e5a 100644 --- a/src/config/supplementary/ConfigDescriptions.hpp +++ b/src/config/supplementary/ConfigDescriptions.hpp @@ -655,9 +655,9 @@ namespace Config::Supplementary { .value = "input:focus_on_close", .description = "Controls the window focus behavior when a window is closed. When set to 0, focus will shift to the next window candidate. When set to 1, focus will shift " - "to the window under the cursor.", + "to the window under the cursor. When set to 2, focus will shift to the most recently used/active window.", .type = CONFIG_OPTION_CHOICE, - .data = SConfigOptionDescription::SChoiceData{0, "next,cursor"}, + .data = SConfigOptionDescription::SChoiceData{0, "next,cursor,mru"}, }, SConfigOptionDescription{ .value = "input:mouse_refocus", @@ -697,8 +697,8 @@ namespace Config::Supplementary { }, /* - * input:touchpad: - */ + * input:touchpad: + */ SConfigOptionDescription{ .value = "input:touchpad:disable_while_typing", diff --git a/src/layout/algorithm/Algorithm.cpp b/src/layout/algorithm/Algorithm.cpp index b83a03a3..b9e94524 100644 --- a/src/layout/algorithm/Algorithm.cpp +++ b/src/layout/algorithm/Algorithm.cpp @@ -237,7 +237,9 @@ const UP& CAlgorithm::floatingAlgo() const { } SP CAlgorithm::getNextCandidate(SP old) { - if (old->floating()) { + static auto FOCUSONCLOSE = CConfigValue("input:focus_on_close"); + + if (old->floating() || *FOCUSONCLOSE == 2) { // use window history to determine best target for (const auto& w : Desktop::History::windowTracker()->fullHistory() | std::views::reverse) { if (!w->m_workspace || w->m_workspace->m_space != m_space || !w->layoutTarget() || !w->layoutTarget()->space()) -- cgit v1.2.3