Skip to content

Commit

Permalink
keybinds: unify changeworkspace dispatcher (#3250)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
memchr committed Sep 11, 2023
1 parent 5a6d0e9 commit df51c45
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/helpers/Workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void CWorkspace::rememberPrevWorkspace(const CWorkspace* prev) {
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");
return;
}
Expand Down
85 changes: 26 additions & 59 deletions src/managers/KeybindManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,11 @@ void CKeybindManager::changeworkspace(std::string args) {
// 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 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 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) {
// Do nothing if there's no previous workspace, otherwise switch to it.
Expand All @@ -830,57 +830,20 @@ void CKeybindManager::changeworkspace(std::string args) {
return;
}

g_pInputManager->unconstrainMouse();
g_pInputManager->m_bEmptyFocusCursorSet = false;

if (workspaceToChangeTo == PCURRENTWORKSPACE->m_iID) {
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, ""};
const bool BISWORKSPACECURRENT = workspaceToChangeTo == PCURRENTWORKSPACE->m_iID;

if (BISWORKSPACECURRENT && (!(*PBACKANDFORTH || EXPLICITPREVIOUS) || PCURRENTWORKSPACE->m_sPrevWorkspace.iID == -1))
return;
}

auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(workspaceToChangeTo);
g_pInputManager->unconstrainMouse();
g_pInputManager->m_bEmptyFocusCursorSet = false;

auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo);
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);
g_pInputManager->simulateMouseMovement();
return;
Expand All @@ -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);

if (!PMONITORWORKSPACEOWNER)
return;

g_pCompositor->setActiveMonitor(PMONITORWORKSPACEOWNER);

PMONITORWORKSPACEOWNER->changeWorkspace(pWorkspaceToChangeTo);
PMONITORWORKSPACEOWNER->changeWorkspace(pWorkspaceToChangeTo, false, true);

if (PMONITOR != PMONITORWORKSPACEOWNER) {
if (PMONITOR != PMONITORWORKSPACEOWNER)
g_pCompositor->warpCursorTo(PMONITORWORKSPACEOWNER->middle());

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);
}
if (BISWORKSPACECURRENT) {
if (*PALLOWWORKSPACECYCLES)
pWorkspaceToChangeTo->rememberPrevWorkspace(PCURRENTWORKSPACE);
else if (!EXPLICITPREVIOUS)
pWorkspaceToChangeTo->rememberPrevWorkspace(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();
}
Expand Down Expand Up @@ -975,7 +942,7 @@ void CKeybindManager::moveActiveToWorkspace(std::string args) {
g_pCompositor->warpCursorTo(PWINDOW->middle());

if (*PALLOWWORKSPACECYCLES)
pWorkspace->m_sPrevWorkspace = {POLDWS->m_iID, POLDWS->m_szName};
pWorkspace->rememberPrevWorkspace(POLDWS);
}

void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) {
Expand Down

0 comments on commit df51c45

Please sign in to comment.