internal: make getPlusMinusKeywordResult return optional

This commit is contained in:
Vaxry 2023-11-12 13:40:02 +00:00
parent 69e314207d
commit 65efde32c9
3 changed files with 32 additions and 25 deletions

View file

@ -204,12 +204,12 @@ std::string removeBeginEndSpacesTabs(std::string str) {
return str; return str;
} }
float getPlusMinusKeywordResult(std::string source, float relative) { std::optional<float> getPlusMinusKeywordResult(std::string source, float relative) {
try { try {
return relative + stof(source); return relative + stof(source);
} catch (...) { } catch (...) {
Debug::log(ERR, "Invalid arg \"{}\" in getPlusMinusKeywordResult!", source); Debug::log(ERR, "Invalid arg \"{}\" in getPlusMinusKeywordResult!", source);
return INT_MAX; return {};
} }
} }
@ -300,7 +300,13 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) {
Debug::log(ERR, "Relative monitor workspace on monitor null!"); Debug::log(ERR, "Relative monitor workspace on monitor null!");
return WORKSPACE_INVALID; return WORKSPACE_INVALID;
} }
result = (int)getPlusMinusKeywordResult(in.substr(1), 0);
const auto PLUSMINUSRESULT = getPlusMinusKeywordResult(in.substr(1), 0);
if (!PLUSMINUSRESULT.has_value())
return WORKSPACE_INVALID;
result = (int)PLUSMINUSRESULT.value();
int remains = (int)result; int remains = (int)result;
@ -436,7 +442,12 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) {
} }
// monitor relative // monitor relative
result = (int)getPlusMinusKeywordResult(in.substr(1), 0); const auto PLUSMINUSRESULT = getPlusMinusKeywordResult(in.substr(1), 0);
if (!PLUSMINUSRESULT.has_value())
return WORKSPACE_INVALID;
result = (int)PLUSMINUSRESULT.value();
// result now has +/- what we should move on mon // result now has +/- what we should move on mon
int remains = (int)result; int remains = (int)result;
@ -477,9 +488,13 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) {
outName = g_pCompositor->getWorkspaceByID(validWSes[currentItem])->m_szName; outName = g_pCompositor->getWorkspaceByID(validWSes[currentItem])->m_szName;
} else { } else {
if (in[0] == '+' || in[0] == '-') { if (in[0] == '+' || in[0] == '-') {
if (g_pCompositor->m_pLastMonitor) if (g_pCompositor->m_pLastMonitor) {
result = std::max((int)getPlusMinusKeywordResult(in, g_pCompositor->m_pLastMonitor->activeWorkspace), 1); const auto PLUSMINUSRESULT = getPlusMinusKeywordResult(in, g_pCompositor->m_pLastMonitor->activeWorkspace);
else { if (!PLUSMINUSRESULT.has_value())
return WORKSPACE_INVALID;
result = std::max((int)PLUSMINUSRESULT.value(), 1);
} else {
Debug::log(ERR, "Relative workspace on no mon!"); Debug::log(ERR, "Relative workspace on no mon!");
return WORKSPACE_INVALID; return WORKSPACE_INVALID;
} }

View file

@ -27,7 +27,7 @@ void logSystemInfo();
std::string execAndGet(const char*); std::string execAndGet(const char*);
int64_t getPPIDof(int64_t pid); int64_t getPPIDof(int64_t pid);
int64_t configStringToInt(const std::string&); int64_t configStringToInt(const std::string&);
float getPlusMinusKeywordResult(std::string in, float relative); std::optional<float> getPlusMinusKeywordResult(std::string in, float relative);
void matrixProjection(float mat[9], int w, int h, wl_output_transform tr); void matrixProjection(float mat[9], int w, int h, wl_output_transform tr);
double normalizeAngleRad(double ang); double normalizeAngleRad(double ang);
std::string replaceInString(std::string subject, const std::string& search, const std::string& replace); std::string replaceInString(std::string subject, const std::string& search, const std::string& replace);

View file

@ -1216,24 +1216,16 @@ void CKeybindManager::toggleSplit(std::string args) {
} }
void CKeybindManager::alterSplitRatio(std::string args) { void CKeybindManager::alterSplitRatio(std::string args) {
float splitratio = 0; std::optional<float> splitResult;
bool exact = false; bool exact = false;
if (args == "+" || args == "-") {
Debug::log(LOG, "alterSplitRatio: using LEGACY +/-, consider switching to the Hyprland syntax.");
splitratio = (args == "+" ? 0.05f : -0.05f);
}
if (splitratio == 0) {
if (args.starts_with("exact")) { if (args.starts_with("exact")) {
exact = true; exact = true;
splitratio = getPlusMinusKeywordResult(args.substr(5), 0); splitResult = getPlusMinusKeywordResult(args.substr(5), 0);
} else { } else
splitratio = getPlusMinusKeywordResult(args, 0); splitResult = getPlusMinusKeywordResult(args, 0);
}
}
if (splitratio == WORKSPACE_INVALID) { if (!splitResult.has_value()) {
Debug::log(ERR, "Splitratio invalid in alterSplitRatio!"); Debug::log(ERR, "Splitratio invalid in alterSplitRatio!");
return; return;
} }
@ -1243,7 +1235,7 @@ void CKeybindManager::alterSplitRatio(std::string args) {
if (!PLASTWINDOW) if (!PLASTWINDOW)
return; return;
g_pLayoutManager->getCurrentLayout()->alterSplitRatio(PLASTWINDOW, splitratio, exact); g_pLayoutManager->getCurrentLayout()->alterSplitRatio(PLASTWINDOW, splitResult.value(), exact);
} }
void CKeybindManager::focusMonitor(std::string arg) { void CKeybindManager::focusMonitor(std::string arg) {