File: macro-action-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 (253 lines) | stat: -rw-r--r-- 6,547 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
#include "macro-action-window.hpp"
#include "help-icon.hpp"
#include "layout-helpers.hpp"
#include "platform-funcs.hpp"
#include "selection-helpers.hpp"

namespace advss {

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

#ifdef _WIN32
bool MacroActionWindow::_registered = MacroActionFactory::Register(
	MacroActionWindow::id,
	{MacroActionWindow::Create, MacroActionWindowEdit::Create,
	 "AdvSceneSwitcher.action.window"});
#else
bool MacroActionWindow::_registered = false;
#endif // _WIN32

const static std::map<MacroActionWindow::Action, std::string> actionTypes = {
	{MacroActionWindow::Action::SET_FOCUS_WINDOW,
	 "AdvSceneSwitcher.action.window.type.setFocusWindow"},
	{MacroActionWindow::Action::MAXIMIZE_WINDOW,
	 "AdvSceneSwitcher.action.window.type.maximizeWindow"},
	{MacroActionWindow::Action::MINIMIZE_WINDOW,
	 "AdvSceneSwitcher.action.window.type.minimizeWindow"},
	{MacroActionWindow::Action::CLOSE_WINDOW,
	 "AdvSceneSwitcher.action.window.type.closeWindow"},
};

#ifdef _WIN32
void SetFocusWindow(const std::string &);
void MaximizeWindow(const std::string &);
void MinimizeWindow(const std::string &);
void CloseWindow(const std::string &);
#else
void SetFocusWindow(const std::string &) {}
void MaximizeWindow(const std::string &) {}
void MinimizeWindow(const std::string &) {}
void CloseWindow(const std::string &) {}
#endif // _WIN32

std::optional<std::string> MacroActionWindow::GetMatchingWindow() const
{
	std::vector<std::string> windowList;
	GetWindowList(windowList);

	if (!_regex.Enabled()) {
		if (std::find(windowList.begin(), windowList.end(),
			      std::string(_window)) == windowList.end()) {
			return {};
		}
		return _window;
	}

	for (const auto &window : windowList) {
		if (_regex.Matches(window, _window)) {
			return window;
		}
	}

	return {};
}

bool MacroActionWindow::PerformAction()
{
	auto window = GetMatchingWindow();
	if (!window) {
		return true;
	}

	switch (_action) {
	case Action::SET_FOCUS_WINDOW:
		SetFocusWindow(*window);
		break;
	case Action::MAXIMIZE_WINDOW:
		MaximizeWindow(*window);
		break;
	case Action::MINIMIZE_WINDOW:;
		MinimizeWindow(*window);
		break;
	case Action::CLOSE_WINDOW:
		CloseWindow(*window);
		break;
	default:
		break;
	}

	return true;
}

void MacroActionWindow::LogAction() const
{
	auto window = GetMatchingWindow();
	switch (_action) {
	case Action::SET_FOCUS_WINDOW:
		blog(LOG_INFO, "focus window \"%s\"",
		     window ? window->c_str() : "nothing matched");
		break;
	case Action::MAXIMIZE_WINDOW:
		blog(LOG_INFO, "maximize window \"%s\"",
		     window ? window->c_str() : "nothing matched");
		break;
	case Action::MINIMIZE_WINDOW:
		blog(LOG_INFO, "minimize window \"%s\"",
		     window ? window->c_str() : "nothing matched");
		break;
	case Action::CLOSE_WINDOW:
		blog(LOG_INFO, "close window \"%s\"",
		     window ? window->c_str() : "nothing matched");
		break;
	default:
		break;
	}
}

bool MacroActionWindow::Save(obs_data_t *obj) const
{
	MacroAction::Save(obj);
	obs_data_set_int(obj, "action", static_cast<int>(_action));
	_window.Save(obj, "window");
	_regex.Save(obj, "regex");
	return true;
}

bool MacroActionWindow::Load(obs_data_t *obj)
{
	MacroAction::Load(obj);
	_action = static_cast<Action>(obs_data_get_int(obj, "action"));
	_window.Load(obj, "window");
	_regex.Load(obj, "regex");
	return true;
}

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

std::shared_ptr<MacroAction> MacroActionWindow::Create(Macro *m)
{
	return std::make_shared<MacroActionWindow>(m);
}

std::shared_ptr<MacroAction> MacroActionWindow::Copy() const
{
	return std::make_shared<MacroActionWindow>(*this);
}

void MacroActionWindow::ResolveVariablesToFixedValues()
{
	_window.ResolveVariables();
}

static inline void populateActionSelection(QComboBox *list)
{
	for (const auto &[_, name] : actionTypes) {
		list->addItem(obs_module_text(name.c_str()));
	}
}

MacroActionWindowEdit::MacroActionWindowEdit(
	QWidget *parent, std::shared_ptr<MacroActionWindow> entryData)
	: QWidget(parent),
	  _actions(new QComboBox()),
	  _windows(new WindowSelectionWidget(this)),
	  _regex(new RegexConfigWidget(this)),
	  _infoLayout(new QHBoxLayout())
{
	populateActionSelection(_actions);

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

	auto focusLimitation = new QLabel(obs_module_text(
		"AdvSceneSwitcher.action.window.type.setFocusWindow.limitation"));
	_infoLayout->addWidget(focusLimitation);

	auto focusLimitationDetails = new HelpIcon(obs_module_text(
		"AdvSceneSwitcher.action.window.type.setFocusWindow.limitation.details"));
	_infoLayout->addWidget(focusLimitationDetails);
	_infoLayout->addStretch();

	QWidget::connect(_actions, SIGNAL(currentIndexChanged(int)), this,
			 SLOT(ActionChanged(int)));
	QWidget::connect(_windows, SIGNAL(currentTextChanged(const QString &)),
			 this, SLOT(WindowChanged(const QString &)));
	QWidget::connect(_regex,
			 SIGNAL(RegexConfigChanged(const RegexConfig &)), this,
			 SLOT(RegexChanged(const RegexConfig &)));

	auto entryLayout = new QHBoxLayout;
	PlaceWidgets(obs_module_text("AdvSceneSwitcher.action.window.entry"),
		     entryLayout,
		     {{"{{actions}}", _actions},
		      {"{{windows}}", _windows},
		      {"{{regex}}", _regex}});
	auto layout = new QVBoxLayout();
	layout->addLayout(entryLayout);
	layout->addLayout(_infoLayout);
	setLayout(layout);

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

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

	_actions->setCurrentIndex(static_cast<int>(_entryData->_action));
	_windows->setCurrentText(
		QString::fromStdString(_entryData->_window.UnresolvedValue()));
	_regex->SetRegexConfig(_entryData->_regex);
	SetWidgetVisibility();
}

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

void MacroActionWindowEdit::RegexChanged(const RegexConfig &regex)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_regex = regex;
	adjustSize();
	updateGeometry();
}

void MacroActionWindowEdit::SetWidgetVisibility()
{
	SetLayoutVisible(_infoLayout,
			 _entryData->_action ==
				 MacroActionWindow::Action::SET_FOCUS_WINDOW);
	adjustSize();
	updateGeometry();
}

void MacroActionWindowEdit::ActionChanged(int value)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->_action = static_cast<MacroActionWindow::Action>(value);
	SetWidgetVisibility();
}

} // namespace advss