@@ -32,22 +32,46 @@ class WindowWindows : public WindowBase<HWND> {
32
32
protected:
33
33
std::vector<wchar_t > _wTitle;
34
34
std::vector<wchar_t > _wClass;
35
+ shards::OwnedVar _previousTitle;
36
+ shards::OwnedVar _previousClass;
35
37
36
38
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
+ }
39
46
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
+ }
51
75
}
52
76
}
53
77
};
@@ -57,8 +81,10 @@ class HasWindow : public WindowWindows {
57
81
static SHTypesInfo outputTypes () { return CoreInfo::BoolType; }
58
82
59
83
SHVar activate (SHContext *context, const SHVar &input) {
84
+ ensureParams ();
85
+
60
86
if (!_window || !IsWindow (_window)) {
61
- if (_winClass .size () > 0 ) {
87
+ if (_wClass .size () > 0 ) {
62
88
_window = FindWindowW (&_wClass[0 ], &_wTitle[0 ]);
63
89
} else {
64
90
_window = FindWindowW (NULL , &_wTitle[0 ]);
@@ -74,8 +100,10 @@ class WaitWindow : public WindowWindows {
74
100
static SHTypesInfo outputTypes () { return Globals::windowType; }
75
101
76
102
SHVar activate (SHContext *context, const SHVar &input) {
103
+ ensureParams ();
104
+
77
105
while (!_window || !IsWindow (_window)) {
78
- if (_winClass .size () > 0 ) {
106
+ if (_wClass .size () > 0 ) {
79
107
_window = FindWindowW (&_wClass[0 ], &_wTitle[0 ]);
80
108
} else {
81
109
_window = FindWindowW (NULL , &_wTitle[0 ]);
@@ -1114,6 +1142,7 @@ struct LastInput : public LastInputBase {
1114
1142
1115
1143
RUNTIME_SHARD (Desktop, HasWindow);
1116
1144
RUNTIME_SHARD_cleanup (HasWindow);
1145
+ RUNTIME_SHARD_warmup (HasWindow);
1117
1146
RUNTIME_SHARD_inputTypes (HasWindow);
1118
1147
RUNTIME_SHARD_outputTypes (HasWindow);
1119
1148
RUNTIME_SHARD_parameters (HasWindow);
@@ -1124,6 +1153,7 @@ RUNTIME_SHARD_END(HasWindow);
1124
1153
1125
1154
RUNTIME_SHARD (Desktop, WaitWindow);
1126
1155
RUNTIME_SHARD_cleanup (WaitWindow);
1156
+ RUNTIME_SHARD_warmup (WaitWindow);
1127
1157
RUNTIME_SHARD_inputTypes (WaitWindow);
1128
1158
RUNTIME_SHARD_outputTypes (WaitWindow);
1129
1159
RUNTIME_SHARD_parameters (WaitWindow);
0 commit comments