From 04f0efadc33fd3ac9fb0ed176dbc00dde9906f8e Mon Sep 17 00:00:00 2001 From: Charles Taylor Date: Sun, 21 Aug 2022 20:21:21 +1000 Subject: add switching to previous workspace --- src/managers/KeybindManager.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/managers/KeybindManager.cpp') diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 9939993a..543066c1 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -454,11 +454,31 @@ void CKeybindManager::changeworkspace(std::string args) { int workspaceToChangeTo = 0; std::string workspaceName = ""; + // Flag needed so that the previous workspace is not recorded when switching + // to a previous workspace. + bool isSwitchingToPrevious = false; + if (args.find("[internal]") == 0) { workspaceToChangeTo = std::stoi(args.substr(10)); const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(workspaceToChangeTo); if (PWORKSPACE) workspaceName = PWORKSPACE->m_szName; + } else if (args.find("previous") == 0) { + const auto P_MONITOR = g_pCompositor->getMonitorFromCursor(); + const auto P_CURRENT_WORKSPACE = g_pCompositor->getWorkspaceByID(P_MONITOR->activeWorkspace); + + // Do nothing if there's no previous workspace, otherwise switch to it. + if (P_CURRENT_WORKSPACE->m_iPrevWorkspaceID == -1) { + Debug::log(LOG, "No previous workspace to change to"); + return; + } + else { + workspaceToChangeTo = P_CURRENT_WORKSPACE->m_iPrevWorkspaceID; + isSwitchingToPrevious = true; + + // TODO: Add support for cycles + P_CURRENT_WORKSPACE->m_iPrevWorkspaceID = -1; + } } else { workspaceToChangeTo = getWorkspaceIDFromString(args, workspaceName); } @@ -477,6 +497,10 @@ void CKeybindManager::changeworkspace(std::string args) { const auto PWORKSPACETOCHANGETO = g_pCompositor->getWorkspaceByID(workspaceToChangeTo); + if (!isSwitchingToPrevious) + // Remember previous workspace. + PWORKSPACETOCHANGETO->m_iPrevWorkspaceID = g_pCompositor->getMonitorFromCursor()->activeWorkspace; + if (workspaceToChangeTo == SPECIAL_WORKSPACE_ID) PWORKSPACETOCHANGETO->m_iMonitorID = PMONITOR->ID; @@ -541,6 +565,10 @@ void CKeybindManager::changeworkspace(std::string args) { const auto PWORKSPACE = g_pCompositor->m_vWorkspaces.emplace_back(std::make_unique(PMONITOR->ID, workspaceName, workspaceToChangeTo == SPECIAL_WORKSPACE_ID)).get(); + if (!isSwitchingToPrevious) + // Remember previous workspace. + PWORKSPACE->m_iPrevWorkspaceID = OLDWORKSPACE; + // start anim on new workspace PWORKSPACE->startAnim(true, ANIMTOLEFT); -- cgit v1.2.3 From d6ff7e40cfde6a8484b50911358032b4c53c721c Mon Sep 17 00:00:00 2001 From: Charles Taylor Date: Sun, 21 Aug 2022 20:40:06 +1000 Subject: add general:workspace_back_and_forth option --- src/managers/KeybindManager.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/managers/KeybindManager.cpp') diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 543066c1..45af02a0 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -464,8 +464,8 @@ void CKeybindManager::changeworkspace(std::string args) { if (PWORKSPACE) workspaceName = PWORKSPACE->m_szName; } else if (args.find("previous") == 0) { - const auto P_MONITOR = g_pCompositor->getMonitorFromCursor(); - const auto P_CURRENT_WORKSPACE = g_pCompositor->getWorkspaceByID(P_MONITOR->activeWorkspace); + const auto P_CURRENT_WORKSPACE = g_pCompositor->getWorkspaceByID( + g_pCompositor->getMonitorFromCursor()->activeWorkspace); // Do nothing if there's no previous workspace, otherwise switch to it. if (P_CURRENT_WORKSPACE->m_iPrevWorkspaceID == -1) { @@ -488,6 +488,21 @@ void CKeybindManager::changeworkspace(std::string args) { return; } + // Workspace_back_and_forth being enabled means that an attempt to switch to + // the current workspace will instead switch to the previous. + if (g_pConfigManager->getConfigValuePtr("general:workspace_back_and_forth")->intValue == 1 + && g_pCompositor->getMonitorFromCursor()->activeWorkspace == workspaceToChangeTo) { + + const auto P_CURRENT_WORKSPACE = g_pCompositor->getWorkspaceByID( + g_pCompositor->getMonitorFromCursor()->activeWorkspace); + + workspaceToChangeTo = P_CURRENT_WORKSPACE->m_iPrevWorkspaceID; + isSwitchingToPrevious = true; + + // TODO: Add support for cycles + P_CURRENT_WORKSPACE->m_iPrevWorkspaceID = -1; + } + // remove constraints g_pInputManager->unconstrainMouse(); -- cgit v1.2.3 From 9ee42836d5e2c707f7cf203628a0ae530d59c5cd Mon Sep 17 00:00:00 2001 From: Charles Taylor Date: Sun, 21 Aug 2022 20:47:56 +1000 Subject: add general:allow_workspace_cycles option --- src/managers/KeybindManager.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/managers/KeybindManager.cpp') diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 45af02a0..ab22d98f 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -476,8 +476,10 @@ void CKeybindManager::changeworkspace(std::string args) { workspaceToChangeTo = P_CURRENT_WORKSPACE->m_iPrevWorkspaceID; isSwitchingToPrevious = true; - // TODO: Add support for cycles - P_CURRENT_WORKSPACE->m_iPrevWorkspaceID = -1; + // If the previous workspace ID isn't reset, cycles can form when continually going + // to the previous workspace again and again. + if (!g_pConfigManager->getConfigValuePtr("general:allow_workspace_cycles")->intValue) + P_CURRENT_WORKSPACE->m_iPrevWorkspaceID = -1; } } else { workspaceToChangeTo = getWorkspaceIDFromString(args, workspaceName); @@ -499,8 +501,10 @@ void CKeybindManager::changeworkspace(std::string args) { workspaceToChangeTo = P_CURRENT_WORKSPACE->m_iPrevWorkspaceID; isSwitchingToPrevious = true; - // TODO: Add support for cycles - P_CURRENT_WORKSPACE->m_iPrevWorkspaceID = -1; + // If the previous workspace ID isn't reset, cycles can form when continually going + // to the previous workspace again and again. + if (!g_pConfigManager->getConfigValuePtr("general:allow_workspace_cycles")->intValue) + P_CURRENT_WORKSPACE->m_iPrevWorkspaceID = -1; } // remove constraints -- cgit v1.2.3 From 3c8c605541d6b3e046951ba8710dfb2e9452a3de Mon Sep 17 00:00:00 2001 From: Charles Taylor Date: Sun, 21 Aug 2022 21:58:46 +1000 Subject: fix style conflicts and config + monitor retrieval --- src/managers/KeybindManager.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'src/managers/KeybindManager.cpp') diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index ab22d98f..374525a2 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -464,22 +464,23 @@ void CKeybindManager::changeworkspace(std::string args) { if (PWORKSPACE) workspaceName = PWORKSPACE->m_szName; } else if (args.find("previous") == 0) { - const auto P_CURRENT_WORKSPACE = g_pCompositor->getWorkspaceByID( + const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID( g_pCompositor->getMonitorFromCursor()->activeWorkspace); // Do nothing if there's no previous workspace, otherwise switch to it. - if (P_CURRENT_WORKSPACE->m_iPrevWorkspaceID == -1) { + if (PCURRENTWORKSPACE->m_iPrevWorkspaceID == -1) { Debug::log(LOG, "No previous workspace to change to"); return; } else { - workspaceToChangeTo = P_CURRENT_WORKSPACE->m_iPrevWorkspaceID; + workspaceToChangeTo = PCURRENTWORKSPACE->m_iPrevWorkspaceID; isSwitchingToPrevious = true; // If the previous workspace ID isn't reset, cycles can form when continually going // to the previous workspace again and again. - if (!g_pConfigManager->getConfigValuePtr("general:allow_workspace_cycles")->intValue) - P_CURRENT_WORKSPACE->m_iPrevWorkspaceID = -1; + static auto *const PALLOWWORKSPACECYCLES = &g_pConfigManager->getConfigValuePtr("binds:allow_workspace_cycles")->intValue; + if (!*PALLOWWORKSPACECYCLES) + PCURRENTWORKSPACE->m_iPrevWorkspaceID = -1; } } else { workspaceToChangeTo = getWorkspaceIDFromString(args, workspaceName); @@ -492,19 +493,20 @@ void CKeybindManager::changeworkspace(std::string args) { // Workspace_back_and_forth being enabled means that an attempt to switch to // the current workspace will instead switch to the previous. - if (g_pConfigManager->getConfigValuePtr("general:workspace_back_and_forth")->intValue == 1 - && g_pCompositor->getMonitorFromCursor()->activeWorkspace == workspaceToChangeTo) { + static auto *const PBACKANDFORTH = &g_pConfigManager->getConfigValuePtr("binds:workspace_back_and_forth")->intValue; + if (*PBACKANDFORTH && g_pCompositor->m_pLastMonitor->activeWorkspace == workspaceToChangeTo) { - const auto P_CURRENT_WORKSPACE = g_pCompositor->getWorkspaceByID( - g_pCompositor->getMonitorFromCursor()->activeWorkspace); + const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID( + g_pCompositor->m_pLastMonitor->activeWorkspace); - workspaceToChangeTo = P_CURRENT_WORKSPACE->m_iPrevWorkspaceID; + workspaceToChangeTo = PCURRENTWORKSPACE->m_iPrevWorkspaceID; isSwitchingToPrevious = true; // If the previous workspace ID isn't reset, cycles can form when continually going // to the previous workspace again and again. - if (!g_pConfigManager->getConfigValuePtr("general:allow_workspace_cycles")->intValue) - P_CURRENT_WORKSPACE->m_iPrevWorkspaceID = -1; + static auto *const PALLOWWORKSPACECYCLES = &g_pConfigManager->getConfigValuePtr("binds:allow_workspace_cycles")->intValue; + if (!*PALLOWWORKSPACECYCLES) + PCURRENTWORKSPACE->m_iPrevWorkspaceID = -1; } // remove constraints @@ -518,7 +520,7 @@ void CKeybindManager::changeworkspace(std::string args) { if (!isSwitchingToPrevious) // Remember previous workspace. - PWORKSPACETOCHANGETO->m_iPrevWorkspaceID = g_pCompositor->getMonitorFromCursor()->activeWorkspace; + PWORKSPACETOCHANGETO->m_iPrevWorkspaceID = g_pCompositor->m_pLastMonitor->activeWorkspace; if (workspaceToChangeTo == SPECIAL_WORKSPACE_ID) PWORKSPACETOCHANGETO->m_iMonitorID = PMONITOR->ID; -- cgit v1.2.3 From 6ec932d11fcc490f3d3f8c544af8fa1a5c8ef4f7 Mon Sep 17 00:00:00 2001 From: Charles Taylor Date: Sun, 21 Aug 2022 22:05:35 +1000 Subject: fix bug which allowed a switch to workspace ID -1. This only happened for the workspace_back_and_forth setting, since it was missing a check. --- src/managers/KeybindManager.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/managers/KeybindManager.cpp') diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 374525a2..ddf65b42 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -493,11 +493,12 @@ void CKeybindManager::changeworkspace(std::string args) { // Workspace_back_and_forth being enabled means that an attempt to switch to // the current workspace will instead switch to the previous. + const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID( + g_pCompositor->m_pLastMonitor->activeWorkspace); static auto *const PBACKANDFORTH = &g_pConfigManager->getConfigValuePtr("binds:workspace_back_and_forth")->intValue; - if (*PBACKANDFORTH && g_pCompositor->m_pLastMonitor->activeWorkspace == workspaceToChangeTo) { - - const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID( - g_pCompositor->m_pLastMonitor->activeWorkspace); + if (*PBACKANDFORTH + && PCURRENTWORKSPACE->m_iID == workspaceToChangeTo + && PCURRENTWORKSPACE->m_iPrevWorkspaceID != -1) { workspaceToChangeTo = PCURRENTWORKSPACE->m_iPrevWorkspaceID; isSwitchingToPrevious = true; -- cgit v1.2.3 From 9dbdd66da417eb2316387f3af078349c50bc2929 Mon Sep 17 00:00:00 2001 From: Charles Taylor Date: Sun, 21 Aug 2022 22:11:40 +1000 Subject: fix retrieval of monitor --- src/managers/KeybindManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/managers/KeybindManager.cpp') diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index ddf65b42..3d821d14 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -465,7 +465,7 @@ void CKeybindManager::changeworkspace(std::string args) { workspaceName = PWORKSPACE->m_szName; } else if (args.find("previous") == 0) { const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID( - g_pCompositor->getMonitorFromCursor()->activeWorkspace); + g_pCompositor->m_pLastMonitor->activeWorkspace); // Do nothing if there's no previous workspace, otherwise switch to it. if (PCURRENTWORKSPACE->m_iPrevWorkspaceID == -1) { -- cgit v1.2.3