better error handling around getMonitorFromString

This commit is contained in:
Vaxry 2023-01-14 20:45:24 +01:00
parent 668d90c700
commit b5b436e01d

View file

@ -1643,6 +1643,10 @@ void CCompositor::swapActiveWorkspaces(CMonitor* pMonitorA, CMonitor* pMonitorB)
CMonitor* CCompositor::getMonitorFromString(const std::string& name) { CMonitor* CCompositor::getMonitorFromString(const std::string& name) {
if (name[0] == '+' || name[0] == '-') { if (name[0] == '+' || name[0] == '-') {
// relative // relative
if (m_vMonitors.size() == 1)
return m_vMonitors.begin()->get();
const auto OFFSET = name[0] == '-' ? name : name.substr(1); const auto OFFSET = name[0] == '-' ? name : name.substr(1);
if (!isNumber(OFFSET)) { if (!isNumber(OFFSET)) {
@ -1671,7 +1675,7 @@ CMonitor* CCompositor::getMonitorFromString(const std::string& name) {
if (currentPlace != std::clamp(currentPlace, 0, (int)m_vMonitors.size())) { if (currentPlace != std::clamp(currentPlace, 0, (int)m_vMonitors.size())) {
Debug::log(WARN, "Error in getMonitorFromString: Vaxry's code sucks."); Debug::log(WARN, "Error in getMonitorFromString: Vaxry's code sucks.");
currentPlace = std::clamp(currentPlace, 0, (int)m_vMonitors.size()); currentPlace = std::clamp(currentPlace, 0, (int)m_vMonitors.size() - 1);
} }
return m_vMonitors[currentPlace].get(); return m_vMonitors[currentPlace].get();