File: macro-condition-window.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (470 lines) | stat: -rw-r--r-- 13,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "macro-condition-window.hpp"
#include "layout-helpers.hpp"
#include "plugin-state-helpers.hpp"
#include "platform-funcs.hpp"

#include <regex>

namespace advss {

const std::string MacroConditionWindow::id = "window";

bool MacroConditionWindow::_registered = MacroConditionFactory::Register(
	MacroConditionWindow::id,
	{MacroConditionWindow::Create, MacroConditionWindowEdit::Create,
	 "AdvSceneSwitcher.condition.window"});

static bool windowContainsText(const std::string &window,
			       const std::string &matchText,
			       const RegexConfig &regex)
{
	auto text = GetTextInWindow(window);
	if (!text.has_value()) {
		return false;
	}

	if (regex.Enabled()) {
		return regex.Matches(*text, matchText);
	}
	return text == matchText;
}

void MacroConditionWindow::SetCheckText(bool value)
{
#ifdef _WIN32
	_checkText = value;
#else
	(void)value;
	_checkText = false;
#endif
	SetupTempVars();
}

bool MacroConditionWindow::GetCheckText()
{
	return _checkText;
}

bool MacroConditionWindow::WindowMatchesRequirements(
	const std::string &window) const
{
	const bool focusCheckOK =
		(!_focus || window == ForegroundWindowTitle());
	if (!focusCheckOK) {
		return false;
	}
	const bool fullscreenCheckOK = (!_fullscreen || IsFullscreen(window));
	if (!fullscreenCheckOK) {
		return false;
	}
	const bool maxCheckOK = (!_maximized || IsMaximized(window));
	if (!maxCheckOK) {
		return false;
	}
	const bool textCheckOK =
		(!_checkText || windowContainsText(window, _text, _textRegex));
	if (!textCheckOK) {
		return false;
	}

	return true;
}

bool MacroConditionWindow::WindowMatches(
	const std::vector<std::string> &windowList)
{
	bool match = !_checkTitle ||
		     std::find(windowList.begin(), windowList.end(),
			       std::string(_window)) != windowList.end();
	match = match && WindowMatchesRequirements(_window);
	SetVariableValueBasedOnMatch(_window);
	return match;
}

#ifdef _WIN32
std::string GetWindowClassByWindowTitle(const std::string &window);
#endif

bool MacroConditionWindow::WindowRegexMatches(
	const std::vector<std::string> &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 (_windowRegex.Matches(window, _window) &&
		    WindowMatchesRequirements(window)) {
			SetVariableValueBasedOnMatch(window);
			return true;
		}
	}
	SetVariableValueBasedOnMatch("");
	return false;
}

void MacroConditionWindow::SetVariableValueBasedOnMatch(
	const std::string &matchWindow)
{
	SetTempVarValue("window", matchWindow);
#ifdef _WIN32
	SetTempVarValue("windowClass",
			GetWindowClassByWindowTitle(matchWindow));
	if (_checkText) {
		const auto text = GetTextInWindow(matchWindow);
		if (text) {
			SetTempVarValue("windowText", *text);
		}
	}
#endif
	if (!IsReferencedInVars()) {
		return;
	}
	if (_checkText) {
		const auto text = GetTextInWindow(matchWindow);
		SetVariableValue(text.value_or(""));
	} else {
		SetVariableValue(ForegroundWindowTitle());
	}
}

static bool foregroundWindowChanged()
{
	return ForegroundWindowTitle() != PreviousForegroundWindowTitle();
}

bool MacroConditionWindow::CheckCondition()
{
	std::vector<std::string> windowList;
	GetWindowList(windowList);
	bool match = false;
	if (_windowRegex.Enabled()) {
		match = WindowRegexMatches(windowList);
	} else {
		match = WindowMatches(windowList);
	}
	match = match && (!_windowFocusChanged || foregroundWindowChanged());
	return match;
}

bool MacroConditionWindow::Save(obs_data_t *obj) const
{
	MacroCondition::Save(obj);
	obs_data_set_bool(obj, "checkTitle", _checkTitle);
	_window.Save(obj, "window");
	_windowRegex.Save(obj, "windowRegexConfig");
	obs_data_set_bool(obj, "fullscreen", _fullscreen);
	obs_data_set_bool(obj, "maximized", _maximized);
	obs_data_set_bool(obj, "focus", _focus);
	obs_data_set_bool(obj, "windowFocusChanged", _windowFocusChanged);
	obs_data_set_bool(obj, "checkWindowText", _checkText);
	_text.Save(obj, "text");
	_textRegex.Save(obj, "textRegexConfig");
	obs_data_set_int(obj, "version", 1);
	return true;
}

bool MacroConditionWindow::Load(obs_data_t *obj)
{
	MacroCondition::Load(obj);
	if (!obs_data_has_user_value(obj, "version")) {
		_checkTitle = true;
		_windowRegex.CreateBackwardsCompatibleRegex(true, true);
	} else {
		_checkTitle = obs_data_get_bool(obj, "checkTitle");
		_windowRegex.Load(obj, "windowRegexConfig");
	}
	_window.Load(obj, "window");
	_fullscreen = obs_data_get_bool(obj, "fullscreen");
	_maximized = obs_data_get_bool(obj, "maximized");
	_focus = obs_data_get_bool(obj, "focus");
	_windowFocusChanged = obs_data_get_bool(obj, "windowFocusChanged");
#ifdef _WIN32
	_checkText = obs_data_get_bool(obj, "checkWindowText");
#else
	_checkText = false;
#endif
	_text.Load(obj, "text");
	_textRegex.Load(obj, "textRegexConfig");
	return true;
}

std::string MacroConditionWindow::GetShortDesc() const
{
	return _window;
}

void MacroConditionWindow::SetupTempVars()
{
	MacroCondition::SetupTempVars();
	AddTempvar(
		"window",
		obs_module_text("AdvSceneSwitcher.tempVar.window.window"),
		obs_module_text(
			"AdvSceneSwitcher.tempVar.window.window.description"));
#ifdef _WIN32
	AddTempvar(
		"windowClass",
		obs_module_text("AdvSceneSwitcher.tempVar.window.windowClass"),
		obs_module_text(
			"AdvSceneSwitcher.tempVar.window.windowClass.description"));

	if (!_checkText) {
		return;
	}

	AddTempvar(
		"windowText",
		obs_module_text("AdvSceneSwitcher.tempVar.window.windowText"),
		obs_module_text(
			"AdvSceneSwitcher.tempVar.window.windowText.description"));
#endif
}

MacroConditionWindowEdit::MacroConditionWindowEdit(
	QWidget *parent, std::shared_ptr<MacroConditionWindow> entryData)
	: QWidget(parent),
	  _windowSelection(new WindowSelectionWidget(this)),
	  _windowRegex(new RegexConfigWidget(this)),
	  _checkTitle(new QCheckBox()),
	  _fullscreen(new QCheckBox()),
	  _maximized(new QCheckBox()),
	  _focused(new QCheckBox()),
	  _windowFocusChanged(new QCheckBox()),
	  _checkText(new QCheckBox()),
	  _text(new VariableTextEdit(this)),
	  _textRegex(new RegexConfigWidget(this)),
	  _focusWindow(new QLabel()),
	  _currentFocusLayout(new QHBoxLayout())
{
	_checkText->setToolTip(obs_module_text(
		"AdvSceneSwitcher.condition.window.entry.text.note"));
	_text->setToolTip(obs_module_text(
		"AdvSceneSwitcher.condition.window.entry.text.note"));

	_windowSelection->setToolTip(
		obs_module_text("AdvSceneSwitcher.tooltip.availableVariables"));

	QWidget::connect(_windowSelection,
			 SIGNAL(currentTextChanged(const QString &)), this,
			 SLOT(WindowChanged(const QString &)));
	QWidget::connect(_windowRegex,
			 SIGNAL(RegexConfigChanged(const RegexConfig &)), this,
			 SLOT(WindowRegexChanged(const RegexConfig &)));
	QWidget::connect(_checkTitle, SIGNAL(stateChanged(int)), this,
			 SLOT(CheckTitleChanged(int)));
	QWidget::connect(_fullscreen, SIGNAL(stateChanged(int)), this,
			 SLOT(FullscreenChanged(int)));
	QWidget::connect(_maximized, SIGNAL(stateChanged(int)), this,
			 SLOT(MaximizedChanged(int)));
	QWidget::connect(_focused, SIGNAL(stateChanged(int)), this,
			 SLOT(FocusedChanged(int)));
	QWidget::connect(_windowFocusChanged, SIGNAL(stateChanged(int)), this,
			 SLOT(WindowFocusChanged(int)));
	QWidget::connect(_checkText, SIGNAL(stateChanged(int)), this,
			 SLOT(CheckTextChanged(int)));
	QWidget::connect(_text, SIGNAL(textChanged()), this,
			 SLOT(WindowTextChanged()));
	QWidget::connect(_textRegex,
			 SIGNAL(RegexConfigChanged(const RegexConfig &)), this,
			 SLOT(TextRegexChanged(const RegexConfig &)));
	QWidget::connect(&_timer, SIGNAL(timeout()), this,
			 SLOT(UpdateFocusWindow()));

	const std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
		{"{{windows}}", _windowSelection},
		{"{{windowRegex}}", _windowRegex},
		{"{{checkTitle}}", _checkTitle},
		{"{{fullscreen}}", _fullscreen},
		{"{{maximized}}", _maximized},
		{"{{focused}}", _focused},
		{"{{windowFocusChanged}}", _windowFocusChanged},
		{"{{focusWindow}}", _focusWindow},
		{"{{checkText}}", _checkText},
		{"{{windowText}}", _text},
		{"{{textRegex}}", _textRegex},
	};

	auto titleLayout = new QHBoxLayout;
	titleLayout->setContentsMargins(0, 0, 0, 0);
	PlaceWidgets(obs_module_text(
			     "AdvSceneSwitcher.condition.window.entry.window"),
		     titleLayout, widgetPlaceholders);
	auto fullscreenLayout = new QHBoxLayout;
	fullscreenLayout->setContentsMargins(0, 0, 0, 0);
	PlaceWidgets(
		obs_module_text(
			"AdvSceneSwitcher.condition.window.entry.fullscreen"),
		fullscreenLayout, widgetPlaceholders);
	auto maximizedLayout = new QHBoxLayout;
	maximizedLayout->setContentsMargins(0, 0, 0, 0);
	PlaceWidgets(
		obs_module_text(
			"AdvSceneSwitcher.condition.window.entry.maximized"),
		maximizedLayout, widgetPlaceholders);
	auto focusedLayout = new QHBoxLayout;
	focusedLayout->setContentsMargins(0, 0, 0, 0);
	PlaceWidgets(obs_module_text(
			     "AdvSceneSwitcher.condition.window.entry.focused"),
		     focusedLayout, widgetPlaceholders);
	auto focusChangedLayout = new QHBoxLayout;
	focusChangedLayout->setContentsMargins(0, 0, 0, 0);
	PlaceWidgets(
		obs_module_text(
			"AdvSceneSwitcher.condition.window.entry.focusedChange"),
		focusChangedLayout, widgetPlaceholders);
	auto textLayout = new QHBoxLayout;
	textLayout->setContentsMargins(0, 0, 0, 0);
	PlaceWidgets(
		obs_module_text("AdvSceneSwitcher.condition.window.entry.text"),
		textLayout, widgetPlaceholders);
	_text->setSizePolicy(QSizePolicy::MinimumExpanding,
			     QSizePolicy::Preferred);
	textLayout->setStretchFactor(_text, 10);
	_currentFocusLayout->setContentsMargins(0, 0, 0, 0);
	PlaceWidgets(
		obs_module_text(
			"AdvSceneSwitcher.condition.window.entry.currentFocus"),
		_currentFocusLayout, widgetPlaceholders);

	auto mainLayout = new QVBoxLayout;
	mainLayout->addLayout(titleLayout);
	mainLayout->addLayout(fullscreenLayout);
	mainLayout->addLayout(maximizedLayout);
	mainLayout->addLayout(focusedLayout);
	mainLayout->addLayout(focusChangedLayout);
	mainLayout->addLayout(textLayout);
#ifndef _WIN32
	SetLayoutVisible(textLayout, false);
#endif
	mainLayout->addLayout(_currentFocusLayout);
	setLayout(mainLayout);

	_entryData = entryData;
	UpdateEntryData();
	_loading = false;

	_timer.start(1000);
}

void MacroConditionWindowEdit::WindowChanged(const QString &text)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_window = text.toStdString();
	emit HeaderInfoChanged(
		QString::fromStdString(_entryData->GetShortDesc()));
}

void MacroConditionWindowEdit::WindowRegexChanged(const RegexConfig &conf)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_windowRegex = conf;
	adjustSize();
	updateGeometry();
}

void MacroConditionWindowEdit::CheckTitleChanged(int state)
{
	GUARD_LOADING_AND_LOCK();
	const QSignalBlocker b1(_windowSelection);
	const QSignalBlocker b2(_windowRegex);
	if (!state) {
		_entryData->_window = ".*";
		_entryData->_windowRegex.SetEnabled(true);
		_windowSelection->setCurrentText(".*");
		_windowRegex->EnableChanged(true);
	}
	_entryData->_checkTitle = state;
	SetWidgetVisibility();
}

void MacroConditionWindowEdit::CheckTextChanged(int state)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->SetCheckText(state);
	SetWidgetVisibility();
}

void MacroConditionWindowEdit::WindowTextChanged()
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_text = _text->toPlainText().toStdString();
	adjustSize();
	updateGeometry();
}

void MacroConditionWindowEdit::TextRegexChanged(const RegexConfig &conf)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_textRegex = conf;
	adjustSize();
	updateGeometry();
}

void MacroConditionWindowEdit::FullscreenChanged(int state)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_fullscreen = state;
}

void MacroConditionWindowEdit::MaximizedChanged(int state)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_maximized = state;
}

void MacroConditionWindowEdit::FocusedChanged(int state)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_focus = state;
	SetWidgetVisibility();
}

void MacroConditionWindowEdit::WindowFocusChanged(int state)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_windowFocusChanged = state;
	SetWidgetVisibility();
}

void MacroConditionWindowEdit::UpdateFocusWindow()
{
	_focusWindow->setText(QString::fromStdString(ForegroundWindowTitle()));
}

void MacroConditionWindowEdit::SetWidgetVisibility()
{
	if (!_entryData) {
		return;
	}
	SetLayoutVisible(_currentFocusLayout,
			 _entryData->_focus || _entryData->_windowFocusChanged);
	_windowSelection->setVisible(_entryData->_checkTitle);
	_windowRegex->setVisible(_entryData->_checkTitle);
	_textRegex->setVisible(_entryData->GetCheckText());
	_text->setVisible(_entryData->GetCheckText());
	adjustSize();
	updateGeometry();
}

void MacroConditionWindowEdit::UpdateEntryData()
{
	if (!_entryData) {
		return;
	}

	_windowSelection->setCurrentText(
		_entryData->_window.UnresolvedValue().c_str());
	_windowRegex->SetRegexConfig(_entryData->_windowRegex);
	_checkTitle->setChecked(_entryData->_checkTitle);
	_fullscreen->setChecked(_entryData->_fullscreen);
	_maximized->setChecked(_entryData->_maximized);
	_focused->setChecked(_entryData->_focus);
	_windowFocusChanged->setChecked(_entryData->_windowFocusChanged);
	_checkText->setChecked(_entryData->GetCheckText());
	_text->setPlainText(_entryData->_text);
	_textRegex->SetRegexConfig(_entryData->_textRegex);
	SetWidgetVisibility();
}

} // namespace advss