Skip to content

Commit 1841873

Browse files
committed
Refactor window handling in desktop module to improve cleanup and parameter management
1 parent 16af78a commit 1841873

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

shards/modules/desktop/desktop.hpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,31 @@ struct Globals {
1919

2020
template <typename T> class WindowBase {
2121
public:
22-
void cleanup(SHContext* context) {
22+
void cleanup(SHContext *context) {
2323
// reset to default
2424
// force finding it again next run
2525
_window = WindowDefault();
26+
27+
_winName.cleanup();
28+
_winClass.cleanup();
29+
}
30+
31+
void warmup(SHContext *context) {
32+
_winName.warmup(context);
33+
_winClass.warmup(context);
2634
}
2735

2836
static SHTypesInfo inputTypes() { return shards::CoreInfo::NoneType; }
2937

3038
static SHParametersInfo parameters() { return SHParametersInfo(windowParams); }
3139

32-
virtual void setParam(int index, const SHVar &value) {
40+
void setParam(int index, const SHVar &value) {
3341
switch (index) {
3442
case 0:
35-
_winName = SHSTRVIEW(value);
43+
_winName = value;
3644
break;
3745
case 1:
38-
_winClass = SHSTRVIEW(value);
46+
_winClass = value;
3947
break;
4048
default:
4149
break;
@@ -46,9 +54,9 @@ template <typename T> class WindowBase {
4654
auto res = SHVar();
4755
switch (index) {
4856
case 0:
49-
return shards::Var(_winName);
57+
return _winName;
5058
case 1:
51-
return shards::Var(_winClass);
59+
return _winClass;
5260
default:
5361
break;
5462
}
@@ -57,13 +65,13 @@ template <typename T> class WindowBase {
5765

5866
protected:
5967
static inline shards::ParamsInfo windowParams = shards::ParamsInfo(
60-
shards::ParamsInfo::Param("Title", SHCCSTR("The title of the window to look for."), shards::CoreInfo::StringType),
68+
shards::ParamsInfo::Param("Title", SHCCSTR("The title of the window to look for."), shards::CoreInfo::StringOrStringVar),
6169
shards::ParamsInfo::Param("Class", SHCCSTR("An optional and platform dependent window class."),
62-
shards::CoreInfo::StringType));
70+
shards::CoreInfo::StringOrStringVar));
6371

6472
static T WindowDefault();
65-
std::string _winName;
66-
std::string _winClass;
73+
shards::ParamVar _winName;
74+
shards::ParamVar _winClass;
6775
T _window;
6876
};
6977

@@ -261,7 +269,7 @@ struct MousePosBase {
261269

262270
SHVar getParam(int index) { return _window; }
263271

264-
void cleanup(SHContext* context) { _window.cleanup(); }
272+
void cleanup(SHContext *context) { _window.cleanup(); }
265273
void warmup(SHContext *context) { _window.warmup(context); }
266274
};
267275

shards/modules/desktop/desktop.win.cpp

+45-15
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,46 @@ class WindowWindows : public WindowBase<HWND> {
3232
protected:
3333
std::vector<wchar_t> _wTitle;
3434
std::vector<wchar_t> _wClass;
35+
shards::OwnedVar _previousTitle;
36+
shards::OwnedVar _previousClass;
3537

3638
public:
37-
void setParam(int index, const SHVar &value) override {
38-
WindowBase<HWND>::setParam(index, value);
39+
void cleanup(SHContext *context) {
40+
WindowBase::cleanup(context);
41+
_wTitle.clear();
42+
_wClass.clear();
43+
_previousTitle = shards::Var::Empty;
44+
_previousClass = shards::Var::Empty;
45+
}
3946

40-
switch (index) {
41-
case 0:
42-
_wTitle.resize(MultiByteToWideChar(CP_UTF8, 0, _winName.c_str(), -1, 0, 0));
43-
MultiByteToWideChar(CP_UTF8, 0, _winName.c_str(), -1, &_wTitle[0], _wTitle.size());
44-
break;
45-
case 1:
46-
_wClass.resize(MultiByteToWideChar(CP_UTF8, 0, _winClass.c_str(), -1, 0, 0));
47-
MultiByteToWideChar(CP_UTF8, 0, _winClass.c_str(), -1, &_wClass[0], _wClass.size());
48-
break;
49-
default:
50-
break;
47+
void ensureParams() {
48+
auto &name = _winName.get();
49+
auto &class_ = _winClass.get();
50+
bool changed = name != _previousTitle || class_ != _previousClass;
51+
if (changed) {
52+
SHLOG_TRACE("Window title changed, re-evaluating.");
53+
_window = NULL;
54+
55+
_previousTitle = name;
56+
_previousClass = class_;
57+
58+
if (name.valueType == SHType::String) {
59+
_wTitle.resize(MultiByteToWideChar(CP_UTF8, 0, name.payload.stringValue, -1, 0, 0));
60+
MultiByteToWideChar(CP_UTF8, 0, name.payload.stringValue, -1, &_wTitle[0], _wTitle.size());
61+
} else {
62+
_wTitle.clear();
63+
}
64+
65+
if (class_.valueType == SHType::String) {
66+
_wClass.resize(MultiByteToWideChar(CP_UTF8, 0, class_.payload.stringValue, -1, 0, 0));
67+
MultiByteToWideChar(CP_UTF8, 0, class_.payload.stringValue, -1, &_wClass[0], _wClass.size());
68+
} else {
69+
_wClass.clear();
70+
}
71+
72+
if (_wTitle.size() == 0) {
73+
throw ActivationError("Window title must be set.");
74+
}
5175
}
5276
}
5377
};
@@ -57,8 +81,10 @@ class HasWindow : public WindowWindows {
5781
static SHTypesInfo outputTypes() { return CoreInfo::BoolType; }
5882

5983
SHVar activate(SHContext *context, const SHVar &input) {
84+
ensureParams();
85+
6086
if (!_window || !IsWindow(_window)) {
61-
if (_winClass.size() > 0) {
87+
if (_wClass.size() > 0) {
6288
_window = FindWindowW(&_wClass[0], &_wTitle[0]);
6389
} else {
6490
_window = FindWindowW(NULL, &_wTitle[0]);
@@ -74,8 +100,10 @@ class WaitWindow : public WindowWindows {
74100
static SHTypesInfo outputTypes() { return Globals::windowType; }
75101

76102
SHVar activate(SHContext *context, const SHVar &input) {
103+
ensureParams();
104+
77105
while (!_window || !IsWindow(_window)) {
78-
if (_winClass.size() > 0) {
106+
if (_wClass.size() > 0) {
79107
_window = FindWindowW(&_wClass[0], &_wTitle[0]);
80108
} else {
81109
_window = FindWindowW(NULL, &_wTitle[0]);
@@ -1114,6 +1142,7 @@ struct LastInput : public LastInputBase {
11141142

11151143
RUNTIME_SHARD(Desktop, HasWindow);
11161144
RUNTIME_SHARD_cleanup(HasWindow);
1145+
RUNTIME_SHARD_warmup(HasWindow);
11171146
RUNTIME_SHARD_inputTypes(HasWindow);
11181147
RUNTIME_SHARD_outputTypes(HasWindow);
11191148
RUNTIME_SHARD_parameters(HasWindow);
@@ -1124,6 +1153,7 @@ RUNTIME_SHARD_END(HasWindow);
11241153

11251154
RUNTIME_SHARD(Desktop, WaitWindow);
11261155
RUNTIME_SHARD_cleanup(WaitWindow);
1156+
RUNTIME_SHARD_warmup(WaitWindow);
11271157
RUNTIME_SHARD_inputTypes(WaitWindow);
11281158
RUNTIME_SHARD_outputTypes(WaitWindow);
11291159
RUNTIME_SHARD_parameters(WaitWindow);

0 commit comments

Comments
 (0)