From b1a3ab5493877595545a09b5d63da2fa79720be7 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sun, 6 Aug 2023 21:00:17 +0200 Subject: [PATCH] Fix window condition ignoring title matching if regex is disabled If the only enabled option was window title matching and regular expressions were not used any window title would match regardless if it existed or not --- src/macro-core/macro-condition-window.cpp | 49 ++++++++++++++++------- src/macro-core/macro-condition-window.hpp | 9 +++-- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/macro-core/macro-condition-window.cpp b/src/macro-core/macro-condition-window.cpp index 3342b4c4..f144040a 100644 --- a/src/macro-core/macro-condition-window.cpp +++ b/src/macro-core/macro-condition-window.cpp @@ -40,7 +40,8 @@ static bool windowContainsText(const std::string &window, return text == matchText; } -bool MacroConditionWindow::WindowMatches(const std::string &window) +bool MacroConditionWindow::WindowMatchesRequirements( + const std::string &window) const { const bool focusCheckOK = (!_focus || window == switcher->currentTitle); if (!focusCheckOK) { @@ -60,26 +61,52 @@ bool MacroConditionWindow::WindowMatches(const std::string &window) return false; } - if (_checkText) { - auto text = GetTextInWindow(window); - SetVariableValue(text.value_or("")); - } - return true; } +bool MacroConditionWindow::WindowMatches( + const std::vector &windowList) +{ + bool match = !_checkTitle || + std::find(windowList.begin(), windowList.end(), + std::string(_window)) != windowList.end(); + match = match && WindowMatchesRequirements(_window); + SetVariableValueBasedOnMatch(_window); + return match; +} + bool MacroConditionWindow::WindowRegexMatches( const std::vector &windowList) { + // No need to test if checking for window title is required as if the + // user has disabled window title matching the option will always be + // enabled in the backend and use the regular expression ".*". + for (const auto &window : windowList) { if (matchRegex(_windowRegex, window, _window) && - WindowMatches(window)) { + WindowMatchesRequirements(window)) { + SetVariableValueBasedOnMatch(window); return true; } } + SetVariableValueBasedOnMatch(""); return false; } +void MacroConditionWindow::SetVariableValueBasedOnMatch( + const std::string &matchWindow) +{ + if (!IsReferencedInVars()) { + return; + } + if (_checkText) { + auto text = GetTextInWindow(matchWindow); + SetVariableValue(text.value_or("")); + } else { + SetVariableValue(switcher->currentTitle); + } +} + static bool foregroundWindowChanged() { return switcher->currentTitle != switcher->lastTitle; @@ -87,19 +114,13 @@ static bool foregroundWindowChanged() bool MacroConditionWindow::CheckCondition() { - SetVariableValue(""); - if (!_checkText) { - SetVariableValue(switcher->currentTitle); - } - std::vector windowList; GetWindowList(windowList); - bool match = false; if (_windowRegex.Enabled()) { match = WindowRegexMatches(windowList); } else { - match = WindowMatches(_window); + match = WindowMatches(windowList); } match = match && (!_windowFocusChanged || foregroundWindowChanged()); return match; diff --git a/src/macro-core/macro-condition-window.hpp b/src/macro-core/macro-condition-window.hpp index 2eed1a9b..19e4d665 100644 --- a/src/macro-core/macro-condition-window.hpp +++ b/src/macro-core/macro-condition-window.hpp @@ -21,10 +21,6 @@ public: return std::make_shared(m); } -private: - bool WindowMatches(const std::string &window); - bool WindowRegexMatches(const std::vector &windowList); - public: StringVariable _window; RegexConfig _windowRegex; @@ -40,6 +36,11 @@ public: RegexConfig _textRegex = RegexConfig::PartialMatchRegexConfig(); private: + bool WindowMatchesRequirements(const std::string &window) const; + bool WindowMatches(const std::vector &windowList); + bool WindowRegexMatches(const std::vector &windowList); + void SetVariableValueBasedOnMatch(const std::string &matchWindow); + static bool _registered; static const std::string id; };