From 2daabfa0e981d8d2e33c068c9cc84ecb3cf2daa0 Mon Sep 17 00:00:00 2001 From: Julian Schuler <31921487+julianschuler@users.noreply.github.com> Date: Sat, 17 Dec 2022 23:37:44 +0100 Subject: [PATCH] Add 'exact' option for 'splitratio' (#1245) * Simplify getPlusMinusKeywordResult() * Add an 'exact' option for 'splitratio' --- src/helpers/MiscFunctions.cpp | 105 ++++++++++++-------------------- src/layout/DwindleLayout.cpp | 5 +- src/layout/DwindleLayout.hpp | 4 +- src/layout/IHyprLayout.hpp | 4 +- src/layout/MasterLayout.cpp | 5 +- src/layout/MasterLayout.hpp | 2 +- src/managers/KeybindManager.cpp | 10 ++- 7 files changed, 57 insertions(+), 78 deletions(-) diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 67b0efcb..710ff351 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -30,38 +30,38 @@ #endif static const float transforms[][9] = {{ - 1.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - },{ - 0.0f, 1.0f, 0.0f, - -1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - },{ - -1.0f, 0.0f, 0.0f, - 0.0f, -1.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - },{ - 0.0f, -1.0f, 0.0f, - 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - },{ - -1.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - },{ - 0.0f, 1.0f, 0.0f, - 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - },{ - 1.0f, 0.0f, 0.0f, - 0.0f, -1.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - },{ - 0.0f, -1.0f, 0.0f, - -1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - }, + 1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, + },{ + 0.0f, 1.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, + },{ + -1.0f, 0.0f, 0.0f, + 0.0f, -1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, + },{ + 0.0f, -1.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, + },{ + -1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, + },{ + 0.0f, 1.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, + },{ + 1.0f, 0.0f, 0.0f, + 0.0f, -1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, + },{ + 0.0f, -1.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, + }, }; std::string absolutePath(const std::string& rawpath, const std::string& currentPath) { @@ -162,41 +162,12 @@ std::string removeBeginEndSpacesTabs(std::string str) { } float getPlusMinusKeywordResult(std::string source, float relative) { - float result = INT_MAX; - - if (source[0] == '+') { - try { - if (source.contains('.')) - result = relative + std::stof(source.substr(1)); - else - result = relative + std::stoi(source.substr(1)); - } catch (...) { - Debug::log(ERR, "Invalid arg \"%s\" in getPlusMinusKeywordResult!", source.c_str()); - return INT_MAX; - } - } else if (source[0] == '-') { - try { - if (source.contains('.')) - result = relative - std::stof(source.substr(1)); - else - result = relative - std::stoi(source.substr(1)); - } catch (...) { - Debug::log(ERR, "Invalid arg \"%s\" in getPlusMinusKeywordResult!", source.c_str()); - return INT_MAX; - } - } else { - try { - if (source.contains('.')) - result = stof(source); - else - result = stoi(source); - } catch (...) { - Debug::log(ERR, "Invalid arg \"%s\" in getPlusMinusKeywordResult!", source.c_str()); - return INT_MAX; - } + try { + return relative + stof(source); + } catch (...) { + Debug::log(ERR, "Invalid arg \"%s\" in getPlusMinusKeywordResult!", source.c_str()); + return INT_MAX; } - - return result; } bool isNumber(const std::string& str, bool allowfloat) { @@ -489,4 +460,4 @@ int64_t configStringToInt(const std::string& VALUE) { return 0; } return stol(VALUE); -} \ No newline at end of file +} diff --git a/src/layout/DwindleLayout.cpp b/src/layout/DwindleLayout.cpp index 87f1fe33..c903a441 100644 --- a/src/layout/DwindleLayout.cpp +++ b/src/layout/DwindleLayout.cpp @@ -1166,7 +1166,7 @@ void CHyprDwindleLayout::switchWindows(CWindow* pWindow, CWindow* pWindow2) { g_pHyprRenderer->damageWindow(pWindow2); } -void CHyprDwindleLayout::alterSplitRatioBy(CWindow* pWindow, float ratio) { +void CHyprDwindleLayout::alterSplitRatio(CWindow* pWindow, float ratio, bool exact) { // window should be valid, insallah const auto PNODE = getNodeFromWindow(pWindow); @@ -1174,7 +1174,8 @@ void CHyprDwindleLayout::alterSplitRatioBy(CWindow* pWindow, float ratio) { if (!PNODE || !PNODE->pParent || (PNODE->isGroupMember() && PNODE->getGroupMemberCount() == g_pCompositor->getWindowsOnWorkspace(PNODE->workspaceID))) return; - PNODE->pParent->splitRatio = std::clamp(PNODE->pParent->splitRatio + ratio, 0.1f, 1.9f); + float newRatio = exact ? ratio : PNODE->pParent->splitRatio + ratio; + PNODE->pParent->splitRatio = std::clamp(newRatio, 0.1f, 1.9f); PNODE->pParent->recalcSizePosRecursive(); } diff --git a/src/layout/DwindleLayout.hpp b/src/layout/DwindleLayout.hpp index 2c6f7229..e6a98915 100644 --- a/src/layout/DwindleLayout.hpp +++ b/src/layout/DwindleLayout.hpp @@ -60,7 +60,7 @@ class CHyprDwindleLayout : public IHyprLayout { virtual std::any layoutMessage(SLayoutMessageHeader, std::string); virtual SWindowRenderLayoutHints requestRenderHints(CWindow*); virtual void switchWindows(CWindow*, CWindow*); - virtual void alterSplitRatioBy(CWindow*, float); + virtual void alterSplitRatio(CWindow*, float, bool); virtual std::string getLayoutName(); virtual void onEnable(); @@ -81,4 +81,4 @@ class CHyprDwindleLayout : public IHyprLayout { std::deque getGroupMembers(CWindow*); friend struct SDwindleNodeData; -}; \ No newline at end of file +}; diff --git a/src/layout/IHyprLayout.hpp b/src/layout/IHyprLayout.hpp index a58783e4..1be26d2b 100644 --- a/src/layout/IHyprLayout.hpp +++ b/src/layout/IHyprLayout.hpp @@ -115,10 +115,10 @@ interface IHyprLayout { virtual void switchWindows(CWindow*, CWindow*) = 0; /* - Called when the user requests to change the splitratio by X + Called when the user requests to change the splitratio by or to X on a window */ - virtual void alterSplitRatioBy(CWindow*, float) = 0; + virtual void alterSplitRatio(CWindow*, float, bool exact = false) = 0; /* Called when something wants the current layout's name diff --git a/src/layout/MasterLayout.cpp b/src/layout/MasterLayout.cpp index 5f38c871..0237bca9 100644 --- a/src/layout/MasterLayout.cpp +++ b/src/layout/MasterLayout.cpp @@ -604,7 +604,7 @@ void CHyprMasterLayout::switchWindows(CWindow* pWindow, CWindow* pWindow2) { prepareNewFocus(pWindow2, inheritFullscreen); } -void CHyprMasterLayout::alterSplitRatioBy(CWindow* pWindow, float ratio) { +void CHyprMasterLayout::alterSplitRatio(CWindow* pWindow, float ratio, bool exact) { // window should be valid, insallah const auto PNODE = getNodeFromWindow(pWindow); @@ -614,7 +614,8 @@ void CHyprMasterLayout::alterSplitRatioBy(CWindow* pWindow, float ratio) { const auto PMASTER = getMasterNodeOnWorkspace(pWindow->m_iWorkspaceID); - PMASTER->percMaster = std::clamp(PMASTER->percMaster + ratio, 0.05f, 0.95f); + float newRatio = exact ? ratio : PMASTER->percMaster + ratio; + PMASTER->percMaster = std::clamp(newRatio, 0.05f, 0.95f); recalculateMonitor(pWindow->m_iMonitorID); } diff --git a/src/layout/MasterLayout.hpp b/src/layout/MasterLayout.hpp index 1b637fde..746c500e 100644 --- a/src/layout/MasterLayout.hpp +++ b/src/layout/MasterLayout.hpp @@ -56,7 +56,7 @@ class CHyprMasterLayout : public IHyprLayout { virtual std::any layoutMessage(SLayoutMessageHeader, std::string); virtual SWindowRenderLayoutHints requestRenderHints(CWindow*); virtual void switchWindows(CWindow*, CWindow*); - virtual void alterSplitRatioBy(CWindow*, float); + virtual void alterSplitRatio(CWindow*, float, bool); virtual std::string getLayoutName(); virtual void onEnable(); diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 77db9eca..fffdaa4b 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -1152,6 +1152,7 @@ void CKeybindManager::toggleSplit(std::string args) { void CKeybindManager::alterSplitRatio(std::string args) { float splitratio = 0; + bool exact = false; if (args == "+" || args == "-") { Debug::log(LOG, "alterSplitRatio: using LEGACY +/-, consider switching to the Hyprland syntax."); @@ -1159,7 +1160,12 @@ void CKeybindManager::alterSplitRatio(std::string args) { } if (splitratio == 0) { - splitratio = getPlusMinusKeywordResult(args, 0); + if (args.find("exact") == 0) { + exact = true; + splitratio = getPlusMinusKeywordResult(args.substr(5), 0); + } else { + splitratio = getPlusMinusKeywordResult(args, 0); + } } if (splitratio == INT_MAX) { @@ -1172,7 +1178,7 @@ void CKeybindManager::alterSplitRatio(std::string args) { if (!PLASTWINDOW) return; - g_pLayoutManager->getCurrentLayout()->alterSplitRatioBy(PLASTWINDOW, splitratio); + g_pLayoutManager->getCurrentLayout()->alterSplitRatio(PLASTWINDOW, splitratio, exact); } void CKeybindManager::focusMonitor(std::string arg) {