From 07c7235b7245375802c767472d62b776458c221d Mon Sep 17 00:00:00 2001 From: Tom Benham <38216488+tomben13@users.noreply.github.com> Date: Mon, 4 Mar 2024 18:05:20 +0100 Subject: [PATCH] keybinds: Better handling of workspace_back_and_forth (#4952) * Removed redundant boolean condition * Better handling of workspace_back_and_forth when using focusworkspaceoncurrentmonitor dispatcher * Fixed config acquisition --------- Co-authored-by: Tom Benham --- src/Compositor.cpp | 5 ++++- src/managers/KeybindManager.cpp | 35 ++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index bb80f3ef..1f7f6182 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -2042,6 +2042,9 @@ void CCompositor::swapActiveWorkspaces(CMonitor* pMonitorA, CMonitor* pMonitorB) pMonitorA->activeWorkspace = PWORKSPACEB->m_iID; pMonitorB->activeWorkspace = PWORKSPACEA->m_iID; + PWORKSPACEA->rememberPrevWorkspace(PWORKSPACEB); + PWORKSPACEB->rememberPrevWorkspace(PWORKSPACEA); + g_pLayoutManager->getCurrentLayout()->recalculateMonitor(pMonitorA->ID); g_pLayoutManager->getCurrentLayout()->recalculateMonitor(pMonitorB->ID); @@ -2150,7 +2153,7 @@ void CCompositor::moveWorkspaceToMonitor(CWorkspace* pWorkspace, CMonitor* pMoni // fix old mon int nextWorkspaceOnMonitorID = -1; - if (!SWITCHINGISACTIVE || !POLDMON) + if (!SWITCHINGISACTIVE) nextWorkspaceOnMonitorID = pWorkspace->m_iID; else { for (auto& w : m_vWorkspaces) { diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 9e12750b..47a4b119 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -1546,9 +1546,9 @@ void CKeybindManager::moveWorkspaceToMonitor(std::string args) { void CKeybindManager::focusWorkspaceOnCurrentMonitor(std::string args) { std::string workspaceName; - const int WORKSPACEID = getWorkspaceIDFromString(args, workspaceName); + int workspaceID = getWorkspaceIDFromString(args, workspaceName); - if (WORKSPACEID == WORKSPACE_INVALID) { + if (workspaceID == WORKSPACE_INVALID) { Debug::log(ERR, "focusWorkspaceOnCurrentMonitor invalid workspace!"); return; } @@ -1560,30 +1560,43 @@ void CKeybindManager::focusWorkspaceOnCurrentMonitor(std::string args) { return; } - auto PWORKSPACE = g_pCompositor->getWorkspaceByID(WORKSPACEID); + auto pWorkspace = g_pCompositor->getWorkspaceByID(workspaceID); - if (!PWORKSPACE) { - PWORKSPACE = g_pCompositor->createNewWorkspace(WORKSPACEID, PCURRMONITOR->ID); + if (!pWorkspace) { + pWorkspace = g_pCompositor->createNewWorkspace(workspaceID, PCURRMONITOR->ID); // we can skip the moving, since it's already on the current monitor - changeworkspace(PWORKSPACE->getConfigName()); + changeworkspace(pWorkspace->getConfigName()); return; } - if (PWORKSPACE->m_iMonitorID != PCURRMONITOR->ID) { - const auto POLDMONITOR = g_pCompositor->getMonitorFromID(PWORKSPACE->m_iMonitorID); + static auto PBACKANDFORTH = CConfigValue("binds:workspace_back_and_forth"); + + if (*PBACKANDFORTH && PCURRMONITOR->activeWorkspace == workspaceID && pWorkspace->m_sPrevWorkspace.iID != -1) { + const int PREVWORKSPACEID = pWorkspace->m_sPrevWorkspace.iID; + const auto PREVWORKSPACENAME = pWorkspace->m_sPrevWorkspace.name; + // Workspace to focus is previous workspace + pWorkspace = g_pCompositor->getWorkspaceByID(PREVWORKSPACEID); + if (!pWorkspace) + pWorkspace = g_pCompositor->createNewWorkspace(PREVWORKSPACEID, PCURRMONITOR->ID, PREVWORKSPACENAME); + + workspaceID = pWorkspace->m_iID; + } + + if (pWorkspace->m_iMonitorID != PCURRMONITOR->ID) { + const auto POLDMONITOR = g_pCompositor->getMonitorFromID(pWorkspace->m_iMonitorID); if (!POLDMONITOR) { // wat Debug::log(ERR, "focusWorkspaceOnCurrentMonitor old monitor doesn't exist!"); return; } - if (POLDMONITOR->activeWorkspace == WORKSPACEID) { + if (POLDMONITOR->activeWorkspace == workspaceID) { g_pCompositor->swapActiveWorkspaces(POLDMONITOR, PCURRMONITOR); return; } else { - g_pCompositor->moveWorkspaceToMonitor(PWORKSPACE, PCURRMONITOR, true); + g_pCompositor->moveWorkspaceToMonitor(pWorkspace, PCURRMONITOR, true); } } - changeworkspace(PWORKSPACE->getConfigName()); + changeworkspace(pWorkspace->getConfigName()); } void CKeybindManager::toggleSpecialWorkspace(std::string args) {