keybinds: unify changeworkspace dispatcher (#3250)

* refactor: changeworkspace dispatcher

* refactor: remove redundant focusWindow calls

* refactor(changeworkspace): warp cursor to middle of last focused window

* refactor: use rememberPrevWorkspace

* Fix: `CWorkspace::rememberPreWorkspace` condition is illogical
This commit is contained in:
memchr 2023-09-11 14:14:43 +00:00 committed by GitHub
parent 5a6d0e9963
commit df51c45d7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 60 deletions

View File

@ -152,7 +152,7 @@ void CWorkspace::rememberPrevWorkspace(const CWorkspace* prev) {
return; return;
} }
if (prev->m_sPrevWorkspace.iID == m_sPrevWorkspace.iID) { if (prev->m_iID == m_iID) {
Debug::log(LOG, "Tried to set prev workspace to the same as current one"); Debug::log(LOG, "Tried to set prev workspace to the same as current one");
return; return;
} }

View File

@ -801,11 +801,11 @@ void CKeybindManager::changeworkspace(std::string args) {
// the current workspace will instead switch to the previous. // the current workspace will instead switch to the previous.
static auto* const PBACKANDFORTH = &g_pConfigManager->getConfigValuePtr("binds:workspace_back_and_forth")->intValue; static auto* const PBACKANDFORTH = &g_pConfigManager->getConfigValuePtr("binds:workspace_back_and_forth")->intValue;
static auto* const PALLOWWORKSPACECYCLES = &g_pConfigManager->getConfigValuePtr("binds:allow_workspace_cycles")->intValue; static auto* const PALLOWWORKSPACECYCLES = &g_pConfigManager->getConfigValuePtr("binds:allow_workspace_cycles")->intValue;
static auto* const PFOLLOWMOUSE = &g_pConfigManager->getConfigValuePtr("input:follow_mouse")->intValue;
const auto PMONITOR = g_pCompositor->m_pLastMonitor; const auto PMONITOR = g_pCompositor->m_pLastMonitor;
const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace); const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace);
const bool EXPLICITPREVIOUS = args.find("previous") == 0;
const bool EXPLICITPREVIOUS = args.find("previous") == 0;
if (args.find("previous") == 0) { if (args.find("previous") == 0) {
// Do nothing if there's no previous workspace, otherwise switch to it. // Do nothing if there's no previous workspace, otherwise switch to it.
@ -830,57 +830,20 @@ void CKeybindManager::changeworkspace(std::string args) {
return; return;
} }
const bool BISWORKSPACECURRENT = workspaceToChangeTo == PCURRENTWORKSPACE->m_iID;
if (BISWORKSPACECURRENT && (!(*PBACKANDFORTH || EXPLICITPREVIOUS) || PCURRENTWORKSPACE->m_sPrevWorkspace.iID == -1))
return;
g_pInputManager->unconstrainMouse(); g_pInputManager->unconstrainMouse();
g_pInputManager->m_bEmptyFocusCursorSet = false; g_pInputManager->m_bEmptyFocusCursorSet = false;
if (workspaceToChangeTo == PCURRENTWORKSPACE->m_iID) { auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo);
if ((!*PBACKANDFORTH && !EXPLICITPREVIOUS) || PCURRENTWORKSPACE->m_sPrevWorkspace.iID == -1)
return;
auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(PCURRENTWORKSPACE->m_sPrevWorkspace.iID);
g_pInputManager->releaseAllMouseButtons();
if (pWorkspaceToChangeTo) {
const auto PMONITORWORKSPACEOWNER = PMONITOR->ID == pWorkspaceToChangeTo->m_iMonitorID ? PMONITOR : g_pCompositor->getMonitorFromID(pWorkspaceToChangeTo->m_iMonitorID);
if (!PMONITORWORKSPACEOWNER)
return;
g_pCompositor->setActiveMonitor(PMONITORWORKSPACEOWNER);
const auto PREVWSDATA = pWorkspaceToChangeTo->m_sPrevWorkspace;
PMONITORWORKSPACEOWNER->changeWorkspace(pWorkspaceToChangeTo);
if (PMONITOR != PMONITORWORKSPACEOWNER) {
g_pCompositor->warpCursorTo(PMONITORWORKSPACEOWNER->middle());
g_pCompositor->setActiveMonitor(PMONITORWORKSPACEOWNER);
if (const auto PLASTWINDOW = pWorkspaceToChangeTo->getLastFocusedWindow(); PLASTWINDOW)
g_pCompositor->focusWindow(PLASTWINDOW);
else if (const auto PFIRSTWINDOW = g_pCompositor->getFirstWindowOnWorkspace(pWorkspaceToChangeTo->m_iID); PFIRSTWINDOW)
g_pCompositor->focusWindow(PFIRSTWINDOW);
else
g_pCompositor->focusWindow(nullptr);
}
} else {
pWorkspaceToChangeTo = g_pCompositor->createNewWorkspace(PCURRENTWORKSPACE->m_sPrevWorkspace.iID, PMONITOR->ID, PCURRENTWORKSPACE->m_sPrevWorkspace.name);
PMONITOR->changeWorkspace(pWorkspaceToChangeTo);
}
if (*PALLOWWORKSPACECYCLES)
pWorkspaceToChangeTo->m_sPrevWorkspace = {PCURRENTWORKSPACE->m_iID, PCURRENTWORKSPACE->m_szName};
else if (!EXPLICITPREVIOUS)
pWorkspaceToChangeTo->m_sPrevWorkspace = {-1, ""};
return;
}
auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(workspaceToChangeTo);
if (!pWorkspaceToChangeTo) if (!pWorkspaceToChangeTo)
pWorkspaceToChangeTo = g_pCompositor->createNewWorkspace(workspaceToChangeTo, PMONITOR->ID, workspaceName); pWorkspaceToChangeTo = g_pCompositor->createNewWorkspace(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo, PMONITOR->ID,
BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.name : workspaceName);
if (pWorkspaceToChangeTo->m_bIsSpecialWorkspace) { if (!BISWORKSPACECURRENT && pWorkspaceToChangeTo->m_bIsSpecialWorkspace) {
PMONITOR->setSpecialWorkspace(pWorkspaceToChangeTo); PMONITOR->setSpecialWorkspace(pWorkspaceToChangeTo);
g_pInputManager->simulateMouseMovement(); g_pInputManager->simulateMouseMovement();
return; return;
@ -890,22 +853,26 @@ void CKeybindManager::changeworkspace(std::string args) {
const auto PMONITORWORKSPACEOWNER = PMONITOR->ID == pWorkspaceToChangeTo->m_iMonitorID ? PMONITOR : g_pCompositor->getMonitorFromID(pWorkspaceToChangeTo->m_iMonitorID); const auto PMONITORWORKSPACEOWNER = PMONITOR->ID == pWorkspaceToChangeTo->m_iMonitorID ? PMONITOR : g_pCompositor->getMonitorFromID(pWorkspaceToChangeTo->m_iMonitorID);
if (!PMONITORWORKSPACEOWNER)
return;
g_pCompositor->setActiveMonitor(PMONITORWORKSPACEOWNER); g_pCompositor->setActiveMonitor(PMONITORWORKSPACEOWNER);
PMONITORWORKSPACEOWNER->changeWorkspace(pWorkspaceToChangeTo); PMONITORWORKSPACEOWNER->changeWorkspace(pWorkspaceToChangeTo, false, true);
if (PMONITOR != PMONITORWORKSPACEOWNER) { if (PMONITOR != PMONITORWORKSPACEOWNER)
g_pCompositor->warpCursorTo(PMONITORWORKSPACEOWNER->middle()); g_pCompositor->warpCursorTo(PMONITORWORKSPACEOWNER->middle());
if (const auto PLASTWINDOW = pWorkspaceToChangeTo->getLastFocusedWindow(); PLASTWINDOW) if (BISWORKSPACECURRENT) {
g_pCompositor->focusWindow(PLASTWINDOW); if (*PALLOWWORKSPACECYCLES)
else if (const auto PFIRSTWINDOW = g_pCompositor->getFirstWindowOnWorkspace(pWorkspaceToChangeTo->m_iID); PFIRSTWINDOW) pWorkspaceToChangeTo->rememberPrevWorkspace(PCURRENTWORKSPACE);
g_pCompositor->focusWindow(PFIRSTWINDOW); else if (!EXPLICITPREVIOUS)
else pWorkspaceToChangeTo->rememberPrevWorkspace(nullptr);
g_pCompositor->focusWindow(nullptr); } else
} pWorkspaceToChangeTo->rememberPrevWorkspace(PCURRENTWORKSPACE);
pWorkspaceToChangeTo->m_sPrevWorkspace = {PCURRENTWORKSPACE->m_iID, PCURRENTWORKSPACE->m_szName}; if (auto PLASTWINDOW = pWorkspaceToChangeTo->getLastFocusedWindow(); PLASTWINDOW && *PFOLLOWMOUSE == 1)
g_pCompositor->warpCursorTo(PLASTWINDOW->middle());
g_pInputManager->simulateMouseMovement(); g_pInputManager->simulateMouseMovement();
} }
@ -975,7 +942,7 @@ void CKeybindManager::moveActiveToWorkspace(std::string args) {
g_pCompositor->warpCursorTo(PWINDOW->middle()); g_pCompositor->warpCursorTo(PWINDOW->middle());
if (*PALLOWWORKSPACECYCLES) if (*PALLOWWORKSPACECYCLES)
pWorkspace->m_sPrevWorkspace = {POLDWS->m_iID, POLDWS->m_szName}; pWorkspace->rememberPrevWorkspace(POLDWS);
} }
void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) { void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) {