From f64e34eb781ee7306510b86673689cc1044f9148 Mon Sep 17 00:00:00 2001 From: Sven Speckmaier Date: Mon, 25 Sep 2023 00:39:21 +0200 Subject: [PATCH] add movefocusgroup command which moves focus through groups before continuing to the next window --- src/managers/KeybindManager.cpp | 73 ++++++++++++++----- src/managers/KeybindManager.hpp | 2 + .../directions/GroupActiveDirection.cpp | 21 ++++++ .../directions/GroupActiveDirection.hpp | 18 +++++ 4 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 src/managers/directions/GroupActiveDirection.cpp create mode 100644 src/managers/directions/GroupActiveDirection.hpp diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 1a7ebafc..ddfcfa1b 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -1,4 +1,5 @@ #include "KeybindManager.hpp" +#include "./directions/GroupActiveDirection.hpp" #include "../render/decorations/CHyprGroupBarDecoration.hpp" #include @@ -28,6 +29,7 @@ CKeybindManager::CKeybindManager() { m_mDispatchers["movetoworkspace"] = moveActiveToWorkspace; m_mDispatchers["movetoworkspacesilent"] = moveActiveToWorkspaceSilent; m_mDispatchers["pseudo"] = toggleActivePseudo; + m_mDispatchers["movegroupfocus"] = moveGroupFocusTo; m_mDispatchers["movefocus"] = moveFocusTo; m_mDispatchers["movewindow"] = moveActiveTo; m_mDispatchers["swapwindow"] = swapActive; @@ -257,6 +259,37 @@ void CKeybindManager::switchToWindow(CWindow* PWINDOWTOCHANGETO) { } }; +CWindow *CKeybindManager::groupActiveInDirection(std::string args) { + const auto PWINDOW = g_pCompositor->m_pLastWindow; + + if (!PWINDOW) + return nullptr; + + if (!PWINDOW->m_sGroupData.pNextWindow) + return nullptr; + + if (PWINDOW->m_sGroupData.pNextWindow == PWINDOW) + return nullptr; + + if (isNumber(args, false)) { + // index starts from '1'; '0' means last window + const int INDEX = std::stoi(args); + if (INDEX > PWINDOW->getGroupSize()) + return nullptr; + if (INDEX == 0) + return PWINDOW->getGroupTail(); + + return PWINDOW->getGroupWindowByIndex(INDEX - 1); + } + + GroupActiveDirection direction(std::move(args)); + if (direction.isBackwards()) { + return PWINDOW->getGroupPrevious(); + } + + return PWINDOW->m_sGroupData.pNextWindow; +} + bool CKeybindManager::onKeyEvent(wlr_keyboard_key_event* e, SKeyboard* pKeyboard) { if (!g_pCompositor->m_bSessionActive || g_pCompositor->m_bUnsafeState) { m_dPressedKeycodes.clear(); @@ -995,6 +1028,25 @@ void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) { g_pInputManager->refocus(); } +void CKeybindManager::moveGroupFocusTo(std::string args) { + const auto PWINDOW = g_pCompositor->m_pLastWindow; + + GroupActiveDirection direction = GroupActiveDirection::fromFocusDirection(args); + CWindow *pNextWindow = groupActiveInDirection(direction.asString()); + + if( + pNextWindow == nullptr + || pNextWindow == PWINDOW + || (direction.isForward() && pNextWindow == PWINDOW->getGroupHead()) + || (direction.isBackwards() && pNextWindow == PWINDOW->getGroupTail()) + ) { + moveFocusTo(args); + return; + } + + PWINDOW->setGroupCurrent(pNextWindow); +} + void CKeybindManager::moveFocusTo(std::string args) { char arg = args[0]; @@ -1163,7 +1215,8 @@ void CKeybindManager::toggleGroup(std::string args) { void CKeybindManager::changeGroupActive(std::string args) { const auto PWINDOW = g_pCompositor->m_pLastWindow; - if (!PWINDOW) + CWindow *pNextWindow = groupActiveInDirection(std::move(args)); + if(!pNextWindow) return; if (!PWINDOW->m_sGroupData.pNextWindow) @@ -1172,23 +1225,7 @@ void CKeybindManager::changeGroupActive(std::string args) { if (PWINDOW->m_sGroupData.pNextWindow == PWINDOW) return; - if (isNumber(args, false)) { - // index starts from '1'; '0' means last window - const int INDEX = std::stoi(args); - if (INDEX > PWINDOW->getGroupSize()) - return; - if (INDEX == 0) - PWINDOW->setGroupCurrent(PWINDOW->getGroupTail()); - else - PWINDOW->setGroupCurrent(PWINDOW->getGroupWindowByIndex(INDEX - 1)); - return; - } - - if (args != "b" && args != "prev") { - PWINDOW->setGroupCurrent(PWINDOW->m_sGroupData.pNextWindow); - } else { - PWINDOW->setGroupCurrent(PWINDOW->getGroupPrevious()); - } + PWINDOW->setGroupCurrent(pNextWindow); } void CKeybindManager::toggleSplit(std::string args) { diff --git a/src/managers/KeybindManager.hpp b/src/managers/KeybindManager.hpp index 9c63249e..009cb08f 100644 --- a/src/managers/KeybindManager.hpp +++ b/src/managers/KeybindManager.hpp @@ -94,6 +94,7 @@ class CKeybindManager { static void moveWindowOutOfGroup(CWindow* pWindow, const std::string& dir = ""); static void moveWindowIntoGroup(CWindow* pWindow, CWindow* pWindowInDirection); static void switchToWindow(CWindow* PWINDOWTOCHANGETO); + static CWindow* groupActiveInDirection(std::string args); // -------------- Dispatchers -------------- // static void killActive(std::string); @@ -107,6 +108,7 @@ class CKeybindManager { static void fakeFullscreenActive(std::string); static void moveActiveToWorkspace(std::string); static void moveActiveToWorkspaceSilent(std::string); + static void moveGroupFocusTo(std::string); static void moveFocusTo(std::string); static void focusUrgentOrLast(std::string); static void focusCurrentOrLast(std::string); diff --git a/src/managers/directions/GroupActiveDirection.cpp b/src/managers/directions/GroupActiveDirection.cpp new file mode 100644 index 00000000..84cf4158 --- /dev/null +++ b/src/managers/directions/GroupActiveDirection.cpp @@ -0,0 +1,21 @@ +#include "GroupActiveDirection.hpp" + +GroupActiveDirection::GroupActiveDirection(std::string direction) : direction(direction) {} + +GroupActiveDirection GroupActiveDirection::fromFocusDirection(std::string direction) { + if(direction == "l") { + return GroupActiveDirection("b"); + } + + return GroupActiveDirection("f"); +} + +bool GroupActiveDirection::isForward() { + return !isBackwards(); +} +bool GroupActiveDirection::isBackwards() { + return direction == "b" || direction == "prev"; +} +std::string GroupActiveDirection::asString() { + return direction; +} diff --git a/src/managers/directions/GroupActiveDirection.hpp b/src/managers/directions/GroupActiveDirection.hpp new file mode 100644 index 00000000..779e5f5a --- /dev/null +++ b/src/managers/directions/GroupActiveDirection.hpp @@ -0,0 +1,18 @@ +#ifndef HYPRLAND_GROUPACTIVEDIRECTION_HPP +#define HYPRLAND_GROUPACTIVEDIRECTION_HPP + +#include + +class GroupActiveDirection { + std::string direction; + + public: + explicit GroupActiveDirection(std::string); + static GroupActiveDirection fromFocusDirection(std::string); + + bool isForward(); + bool isBackwards(); + std::string asString(); +}; + +#endif //HYPRLAND_GROUPACTIVEDIRECTION_HPP