Add setPreconditions()

The idea is to avoid repeated calls to the same functions which lead to
the same outcome, but just calling them once in the precondition checks.

For now move repeated calls of GetCurrentWindowTitle() and ignoreWindow
checks to this function.
This commit is contained in:
WarmUpTill 2021-06-18 20:09:40 +02:00 committed by WarmUpTill
parent decee4647b
commit bdd9d12257
7 changed files with 33 additions and 50 deletions

View File

@ -1,6 +1,7 @@
#include <QMainWindow>
#include <QAction>
#include <QFileDialog>
#include <regex>
#include <obs-module.h>
#include <obs-frontend-api.h>
@ -199,6 +200,7 @@ void SwitcherData::Thread()
if (checkPause()) {
continue;
}
setPreconditions();
match = checkForMatch(scene, transition, linger,
setPrevSceneAfterLinger, macroMatch);
if (stop) {
@ -252,6 +254,31 @@ void SwitcherData::Thread()
blog(LOG_INFO, "stopped");
}
void SwitcherData::setPreconditions()
{
// Window title
lastTitle = currentTitle;
std::string title;
GetCurrentWindowTitle(title);
for (auto &window : ignoreWindowsSwitches) {
bool equals = (title == window);
bool matches = false;
if (!equals) {
try {
std::regex expr(window);
matches = std::regex_match(title, expr);
} catch (const std::regex_error &) {
}
}
if (equals || matches) {
title = lastTitle;
break;
}
}
currentTitle = title;
}
bool SwitcherData::checkForMatch(OBSWeakSource &scene,
OBSWeakSource &transition, int &linger,
bool &setPrevSceneAfterLinger,

View File

@ -98,6 +98,7 @@ struct SwitcherData {
std::deque<WindowSwitch> windowSwitches;
std::vector<std::string> ignoreIdleWindows;
std::string lastTitle;
std::string currentTitle;
std::deque<ScreenRegionSwitch> screenRegionSwitches;
@ -202,6 +203,7 @@ struct SwitcherData {
void writeSceneInfoToFile();
void writeToStatusFile(const QString &msg);
void setPreconditions();
bool checkForMatch(OBSWeakSource &scene, OBSWeakSource &transition,
int &linger, bool &setPreviousSceneAsMatch,
bool &macroMatch);

View File

@ -14,12 +14,8 @@ bool MacroConditionProcess::_registered = MacroConditionFactory::Register(
bool MacroConditionProcess::CheckCondition()
{
std::string title;
QStringList runningProcesses;
QString proc = QString::fromStdString(_process);
GetCurrentWindowTitle(title);
GetProcessList(runningProcesses);
bool equals = runningProcesses.contains(proc);

View File

@ -84,29 +84,11 @@ bool SwitcherData::checkExeSwitch(OBSWeakSource &scene,
return false;
}
std::string title;
std::string title = switcher->currentTitle;
QStringList runningProcesses;
bool ignored = false;
bool match = false;
// Check if current window is ignored
GetCurrentWindowTitle(title);
for (auto &window : ignoreWindowsSwitches) {
// True if ignored switch equals title
bool equals = (title == window);
// True if ignored switch matches title
bool matches = QString::fromStdString(title).contains(
QRegularExpression(QString::fromStdString(window)));
if (equals || matches) {
ignored = true;
title = lastTitle;
break;
}
}
lastTitle = title;
// Check for match
GetProcessList(runningProcesses);
for (ExecutableSwitch &s : executableSwitches) {

View File

@ -13,9 +13,8 @@ bool SwitcherData::checkIdleSwitch(OBSWeakSource &scene,
return false;
}
std::string title;
std::string title = switcher->currentTitle;
bool ignoreIdle = false;
GetCurrentWindowTitle(title);
bool match = false;
for (std::string &window : ignoreIdleWindows) {

View File

@ -171,9 +171,7 @@ bool checkPauseWindow(std::string &currentTitle, std::string &title,
bool SwitcherData::checkPause()
{
bool pauseAll = false;
std::string title;
GetCurrentWindowTitle(title);
std::string title = switcher->currentTitle;
resetPause();

View File

@ -221,29 +221,8 @@ bool SwitcherData::checkWindowTitleSwitch(OBSWeakSource &scene,
return false;
}
std::string currentWindowTitle;
GetCurrentWindowTitle(currentWindowTitle);
std::string currentWindowTitle = switcher->currentTitle;
bool match = false;
// Check if current window is ignored
for (auto &window : ignoreWindowsSwitches) {
bool equals = (currentWindowTitle == window);
try {
std::regex expr(window);
bool matches =
std::regex_match(currentWindowTitle, expr);
if (equals || matches) {
currentWindowTitle = lastTitle;
break;
}
} catch (const std::regex_error &) {
}
}
lastTitle = currentWindowTitle;
std::vector<std::string> windowList;
GetWindowList(windowList);