From a354daa71d03e6f2aa62a429761e62779cf51d4f Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Wed, 3 Feb 2021 20:46:25 +0100 Subject: [PATCH] Fix regex matching for window tab (#113) Regex matches could never match if full-screen or maximized was selected due to only checking for direct matches during these checks. --- src/scene-trigger.cpp | 1 - src/switch-window.cpp | 106 +++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 44 deletions(-) diff --git a/src/scene-trigger.cpp b/src/scene-trigger.cpp index 5b8cd5b3..558f8da9 100644 --- a/src/scene-trigger.cpp +++ b/src/scene-trigger.cpp @@ -569,7 +569,6 @@ void SceneTriggerWidget::TriggerActionChanged(int index) void SceneTriggerWidget::DurationChanged(double dur) { - if (loading || !switchData) { return; } diff --git a/src/switch-window.cpp b/src/switch-window.cpp index 444f03a2..aa830555 100644 --- a/src/switch-window.cpp +++ b/src/switch-window.cpp @@ -1,3 +1,5 @@ +#include + #include "headers/advanced-scene-switcher.hpp" #include "headers/utility.hpp" @@ -169,28 +171,47 @@ void AdvSceneSwitcher::on_ignoreWindows_currentRowChanged(int idx) } } -bool isRunning(std::string &title) +void checkWindowTitleSwitchDirect(WindowSwitch &s, + std::string ¤tWindowTitle, bool &match, + OBSWeakSource &scene, + OBSWeakSource &transition) { - QStringList windows; + bool focus = s.window == currentWindowTitle; + bool fullscreen = (!s.fullscreen || isFullscreen(s.window)); + bool max = (!s.maximized || isMaximized(s.window)); - GetWindowList(windows); - bool equals = windows.contains(QString::fromStdString(title)); - bool matches = (windows.indexOf(QRegularExpression( - QString::fromStdString(title))) != -1); - - return (equals || matches); + if (focus && fullscreen && max) { + match = true; + scene = s.getScene(); + transition = s.transition; + } } -bool isFocused(std::string &title) +void checkWindowTitleSwitchRegex(WindowSwitch &s, + std::string ¤tWindowTitle, + std::vector windowList, + bool &match, OBSWeakSource &scene, + OBSWeakSource &transition) { - std::string current; + for (auto &window : windowList) { + try { + std::regex expr(s.window); + if (!std::regex_match(window, expr)) { + continue; + } + } catch (const std::regex_error &) { + } - GetCurrentWindowTitle(current); - bool equals = (title == current); - bool matches = QString::fromStdString(current).contains( - QRegularExpression(QString::fromStdString(title))); + bool focus = window == currentWindowTitle; + bool fullscreen = (!s.fullscreen || isFullscreen(window)); + bool max = (!s.maximized || isMaximized(window)); - return (equals || matches); + if (focus && fullscreen && max) { + match = true; + scene = s.getScene(); + transition = s.transition; + } + } } void SwitcherData::checkWindowTitleSwitch(bool &match, OBSWeakSource &scene, @@ -200,48 +221,47 @@ void SwitcherData::checkWindowTitleSwitch(bool &match, OBSWeakSource &scene, return; } - std::string title; - bool ignored = false; + std::string currentWindowTitle; + GetCurrentWindowTitle(currentWindowTitle); // Check if current window is ignored - GetCurrentWindowTitle(title); for (auto &window : ignoreWindowsSwitches) { - bool equals = (title == window); - bool matches = QString::fromStdString(title).contains( - QRegularExpression(QString::fromStdString(window))); + bool equals = (currentWindowTitle == window); - if (equals || matches) { - ignored = true; - title = lastTitle; + try { + std::regex expr(window); + bool matches = + std::regex_match(currentWindowTitle, expr); - break; + if (equals || matches) { + currentWindowTitle = lastTitle; + break; + } + } catch (const std::regex_error &) { } } - lastTitle = title; - // Check for match + lastTitle = currentWindowTitle; + + std::vector windowList; + GetWindowList(windowList); + for (WindowSwitch &s : windowSwitches) { if (!s.initialized()) { continue; } - bool fullscreen = (!s.fullscreen || isFullscreen(s.window)); - bool max = (!s.maximized || isMaximized(s.window)); - bool focus = (!s.focus || isFocused(s.window)); - // True if current window is ignored AND switch equals OR matches last window - bool ignore = - (ignored && - (title == s.window || - QString::fromStdString(title).contains( - QRegularExpression( - QString::fromStdString(s.window))))); - - if (isRunning(s.window) && - (fullscreen && (max && (focus || ignore)))) { - match = true; - scene = s.getScene(); - transition = s.transition; + if (std::find(windowList.begin(), windowList.end(), s.window) != + windowList.end()) { + checkWindowTitleSwitchDirect(s, currentWindowTitle, + match, scene, transition); + } else { + checkWindowTitleSwitchRegex(s, currentWindowTitle, + windowList, match, scene, + transition); + } + if (match) { if (verbose) { s.logMatch(); }