-
Notifications
You must be signed in to change notification settings - Fork 65
/
window.py
371 lines (305 loc) · 11.6 KB
/
window.py
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
from ahk.script import ScriptEngine
import ast
from ahk.utils import make_logger, escape_sequence_replace
from contextlib import suppress
logger = make_logger(__name__)
class WindowNotFoundError(ValueError):
pass
class Control:
def __init__(self):
raise NotImplementedError
def click(self):
"""
REF: https://www.autohotkey.com/docs/commands/ControlClick.htm
:return:
"""
raise NotImplementedError
def focus(self):
"""
REF: https://www.autohotkey.com/docs/commands/ControlFocus.htm
:return:
"""
raise NotImplementedError
def get(self, key):
"""
REF: https://www.autohotkey.com/docs/commands/ControlGet.htm
:param key:
:return:
"""
raise NotImplementedError
def has_focus(self):
raise NotImplementedError
@property
def position(self):
"""
REF: https://www.autohotkey.com/docs/commands/ControlGetPos.htm
:return:
"""
raise NotImplementedError
@property
def text(self):
"""
REF: https://www.autohotkey.com/docs/commands/ControlGetText.htm
:return:
"""
raise NotImplementedError
@text.setter
def text(self, new_text):
"""
REF: https://www.autohotkey.com/docs/commands/ControlSetText.htm
:param new_text:
:return:
"""
raise NotImplementedError
def move(self):
"""
REF: https://www.autohotkey.com/docs/commands/ControlMove.htm
:return:
"""
raise NotImplementedError
def send(self, raw=False):
"""
REF: https://www.autohotkey.com/docs/commands/ControlSend.htm
:param raw:
:return:
"""
raise NotImplementedError
class Window(object):
_subcommands = {
'id': 'ID',
'process_name': 'ProcessName',
'pid': 'PID',
'process_path': 'ProcessPath',
'process': 'ProcessPath',
'controls': 'ControlList',
'controls_hwnd': 'ControlListHwnd',
'transparency': 'Transparent',
'trans_color': 'TransColor',
'style': 'Style', # This will probably get a property later
'ex_style': 'ExStyle', # This will probably get a property later
}
# add reverse lookups
_subcommands.update({value: value for value in _subcommands.values()})
def __init__(self, engine: ScriptEngine, ahk_id: str, encoding=None):
self.engine = engine # should this be a weakref instead?
self.id = ahk_id
self.encoding = encoding
@classmethod
def from_mouse_position(cls, engine: ScriptEngine, **kwargs):
script = engine.render_template('window/from_mouse.ahk')
ahk_id = engine.run_script(script)
return cls(engine=engine, ahk_id=ahk_id, **kwargs)
@classmethod
def from_pid(cls, engine: ScriptEngine, pid, **kwargs):
script = engine.render_template('window/get.ahk',
subcommand="ID",
title=f'ahk_pid {pid}')
ahk_id = engine.run_script(script)
return cls(engine=engine, ahk_id=ahk_id, **kwargs)
def __getattr__(self, attr):
if attr.lower() in self._subcommands:
return self.get(attr)
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{attr}'")
def get(self, subcommand):
sub = self._subcommands.get(subcommand)
if not sub:
raise ValueError(f'No such subcommand {subcommand}')
script = self._render_template('window/get.ahk',
subcommand=sub,
title=f'ahk_id {self.id}')
return self.engine.run_script(script)
def __repr__(self):
return f'<ahk.window.Window ahk_id={self.id}>'
def win_set(self, subcommand, value):
script = self._render_template('window/win_set.ahk', subcommand=subcommand, value=value)
self.engine.run_script(script)
def _get_pos(self):
script = self._render_template('window/win_position.ahk')
resp = self.engine.run_script(script)
try:
value = ast.literal_eval(resp)
return value
except SyntaxError:
raise WindowNotFoundError('No window found')
@property
def rect(self):
return self._get_pos()
@rect.setter
def rect(self, new_position):
x, y, width, height = new_position
self.move(x=x, y=y, width=width, height=height)
@property
def position(self):
x, y, _, _ = self._get_pos()
return x, y
@position.setter
def position(self, new_position):
x, y = new_position
self.move(x, y)
@property
def width(self):
_, _, width, _ = self._get_pos()
return width
@width.setter
def width(self, new_width):
self.move(width=new_width)
@property
def height(self):
_, _, _, height = self._get_pos()
return height
@height.setter
def height(self, new_height):
self.move(height=new_height)
@property
def active(self):
script = self._render_template('window/win_is_active.ahk')
result = self.engine.run_script(script)
result = bool(ast.literal_eval(result))
return result
def disable(self):
self.win_set('Disable', '')
def enable(self):
self.win_set('Enable', '')
def redraw(self):
self.win_set('Redraw', '')
@property
def title(self):
script = self._render_template('window/win_get_title.ahk')
result = self.engine.run_script(script, decode=False)
if self.encoding:
return result.stdout.decode(encoding=self.encoding)
return result.stdout
@property
def class_name(self):
script = self._render_template('window/win_get_class.ahk')
result = self.engine.run_script(script, decode=False)
if self.encoding:
return result.stdout.decode(encoding=self.encoding)
return result.stdout
@property
def text(self):
script = self._render_template('window/win_get_text.ahk')
result = self.engine.run_script(script, decode=False)
if self.encoding:
return result.stdout.decode(encoding=self.encoding)
return result.stdout
@property
def always_on_top(self):
script = self._render_template('window/win_is_always_on_top.ahk')
resp = self.engine.run_script(script)
return bool(ast.literal_eval(resp))
@always_on_top.setter
def always_on_top(self, value):
if value in ('on', 'On', True, 1):
self.win_set('AlwaysOnTop', 'On')
elif value in ('off', 'Off', False, 0):
self.win_set('AlwaysOnTop', 'Off')
elif value in ('toggle', 'Toggle', -1):
self.win_set('AlwaysOnTop', 'Toggle')
else:
raise ValueError(f'"{value}" not a valid option. Please use On/Off/Toggle/True/False/0/1/-1')
def close(self, seconds_to_wait=''):
script = self._render_template('window/win_close.ahk', seconds_to_wait=seconds_to_wait)
self.engine.run_script(script)
def to_bottom(self):
"""
Send window to bottom (behind other windows)
:return:
"""
self.win_set('Bottom', '')
def to_top(self):
self.win_set('Top', '')
def _render_template(self, *args, **kwargs):
kwargs['win'] = self
return self.engine.render_template(*args, **kwargs)
def activate(self):
script = self._render_template('window/win_activate.ahk')
self.engine.run_script(script)
def move(self, x='', y='', width=None, height=None):
script = self._render_template('window/win_move.ahk', x=x, y=y, width=width, height=height)
self.engine.run_script(script)
def send(self, keys, delay=None, raw=False, blocking=False, escape=False):
"""
Send keystrokes directly to the window.
Uses ControlSend
https://autohotkey.com/docs/commands/Send.htm
"""
if escape:
keys = escape_sequence_replace(keys)
script = self._render_template('window/win_send.ahk', keys=keys, raw=raw, delay=delay, blocking=blocking)
return self.engine.run_script(script, blocking=blocking)
def __eq__(self, other):
if not isinstance(other, Window):
return False
return self.id == other.id
def __hash__(self):
return hash(repr(self))
class WindowMixin(ScriptEngine):
def __init__(self, *args, **kwargs):
self.window_encoding = kwargs.pop('window_encoding', None)
super().__init__(*args, **kwargs)
def win_get(self, title='', text='', exclude_title='', exclude_text='', encoding=None):
encoding = encoding or self.window_encoding
script = self.render_template('window/get.ahk',
subcommand='ID',
title=title,
text=text,
exclude_text=exclude_text,
exclude_title=exclude_title)
ahk_id = self.run_script(script)
return Window(engine=self, ahk_id=ahk_id, encoding=encoding)
def win_set(self, subcommand, *args, blocking=True):
script = self.render_template('window/set.ahk', subcommand=subcommand, *args, blocking=blocking)
self.run_script(script, blocking=blocking)
@property
def active_window(self):
return self.win_get(title='A')
def _all_window_ids(self):
script = self.render_template('window/id_list.ahk')
result = self.run_script(script)
return result.split('\n')[:-1] # last one is always an empty string
def windows(self):
"""
Returns a list of windows
:return:
"""
windowze = []
for ahk_id in self._all_window_ids():
win = Window(engine=self, ahk_id=ahk_id, encoding=self.window_encoding)
windowze.append(win)
return windowze
def find_windows(self, func=None, **kwargs):
if func is None:
exact = kwargs.pop('exact', False)
def func(win):
for attr, expected in kwargs.items():
if exact:
result = getattr(win, attr) == expected
else:
result = expected in getattr(win, attr)
if result is False:
return False
return True
for window in filter(func, self.windows()):
yield window
def find_window(self, func=None, **kwargs):
with suppress(StopIteration):
return next(self.find_windows(func=func, **kwargs))
def find_windows_by_title(self, title, exact=False):
for window in self.find_windows(title=title, exact=exact):
yield window
def find_window_by_title(self, *args, **kwargs):
with suppress(StopIteration):
return next(self.find_windows_by_title(*args, **kwargs))
def find_windows_by_text(self, text, exact=False):
for window in self.find_windows(text=text, exact=exact):
yield window
def find_window_by_text(self, *args, **kwargs):
with suppress(StopIteration):
return next(self.find_windows_by_text(*args, **kwargs))
def find_windows_by_class(self, class_name, exact=False):
for window in self.find_windows(class_name=class_name, exact=exact):
yield window
def find_window_by_class(self, *args, **kwargs):
with suppress(StopIteration):
return next(self.find_windows_by_class(*args, **kwargs))