-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow_tweak.py
executable file
·526 lines (403 loc) · 18.3 KB
/
window_tweak.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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# """
# Tools for managing window size and position.
# """
# WIP - here are some quirks that need work:
#
# - 'win snap 200 percent' moves window up a bit when it should stay centered. may be a side-effect related to
# talon resize() API behavior, which will not increase height beyond 1625 for some reason...perhaps related to
# the height of the largest of my 3 screens (which is height 1600).
#
# - here's a weird one: I have vscode maximized on my left hand screen and say 'win size one thousand by one thousand',
# first it resizes, as expected, but then jumps to my primary Screen to the right.
from typing import Optional, Tuple
import queue
import logging
import time
from talon import ui, Module, Context, actions, imgui, settings, app, ctrl
# globals
from .compass_control import CompassControl, Direction, compass_direction, NonDualDirection, non_dual_direction
# # turn debug messages on and off
testing: bool = False
win_compass_control = None
compass_control = None
ctx_stop = None
# talon stuff
mod = Module()
TAG_NAME = 'window_tweak_running'
tag = mod.tag(TAG_NAME, desc="Enable stop command during continuous window move/resize.")
# context used to enable/disable window_tweak_running tag
ctx = Context()
setting_move_frequency = mod.setting(
"win_move_frequency",
type=str,
default="40ms",
desc="The update frequency used when moving a window continuously",
)
setting_resize_frequency = mod.setting(
"win_resize_frequency",
type=str,
default="40ms",
desc="The update frequency used when resizing a window continuously",
)
setting_move_rate = mod.setting(
"win_continuous_move_rate",
type=float,
default=4.5,
desc="The target speed, in cm/sec, for continuous move operations",
)
setting_resize_rate = mod.setting(
"win_continuous_resize_rate",
type=float,
default=4.0,
desc="The target speed, in cm/sec, for continuous resize operations",
)
mod.setting(
"win_hide_move_gui",
type=int,
default=0,
desc="When enabled, the 'Move/Resize Window' GUI will not be shown for continuous move operations.",
)
mod.setting(
"win_hide_resize_gui",
type=int,
default=0,
desc="When enabled, the 'Move/Resize Window' GUI will not be shown for continuous resize operations.",
)
mod.setting(
"win_set_queue_timeout",
type=float,
default=0.2,
desc="How long to wait (in seconds) for talon to signal completion of window move/resize requests.",
)
mod.setting(
"win_set_retries",
type=int,
default=1,
desc="How many times to retry a timed out talon window move/resize request.",
)
setting_verbose_warnings = mod.setting(
"win_verbose_warnings",
type=bool,
default=False,
# window move and resize requests are not guaranteed
desc="Whether to generate warnings for anomalous events.",
)
@imgui.open(y=0)
def win_stop_gui(gui: imgui.GUI) -> None:
gui.text(f"Say 'window stop' or click below.")
gui.line()
if gui.button("Stop moving/resizing"):
actions.user.win_stop()
@imgui.open(x=2100, y=40)
# @imgui.open(x=4000,y=244)
def _win_show_gui(gui: imgui.GUI) -> None:
w = ui.active_window()
gui.text(f"== Window ==")
gui.text(f"Id: {w.id}")
gui.spacer()
x = w.rect.x
y = w.rect.y
width = w.rect.width
height = w.rect.height
gui.text(f"Top Left: {x, y}")
gui.text(f"Top Right: {x + width, y}")
gui.text(f"Bottom Left: {x, y + height}")
gui.text(f"Bottom Right: {x + width, y + height}")
gui.text(f"Center: {round(w.rect.center.x), round(w.rect.center.y)}")
gui.spacer()
gui.text(f"Width: {round(width)}")
gui.text(f"Height: {round(height)}")
gui.line()
gui.text(f"== Mouse ==")
(mouse_x, mouse_y) = ctrl.mouse_pos()
gui.text(f"Position: ({mouse_x:.0f}, {mouse_y:.0f})")
gui.line()
screen = w.screen
gui.text(f"== Screen ==")
gui.spacer()
#gui.text(f"Name: {screen.name}")
# gui.text(f"DPI: {screen.dpi}")
# gui.text(f"DPI_x: {screen.dpi_x}")
# gui.text(f"DPI_y: {screen.dpi_y}")
#gui.text(f"Scale: {screen.scale}")
#gui.spacer()
x = screen.visible_rect.x
y = screen.visible_rect.y
width = screen.visible_rect.width
height = screen.visible_rect.height
gui.text(f"__Visible Rectangle__")
gui.text(f"Top Left: {round(x), round(y)}")
gui.text(f"Top Right: {round(x + width), round(y)}")
gui.text(f"Bottom Left: {round(x), round(y + height)}")
gui.text(f"Bottom Right: {round(x + width), round(y + height)}")
gui.text(f"Center: {round(screen.visible_rect.center.x), round(screen.visible_rect.center.y)}")
gui.spacer()
gui.text(f"Width: {round(width)}")
gui.text(f"Height: {round(height)}")
gui.spacer()
x = screen.rect.x
y = screen.rect.y
width = screen.rect.width
height = screen.rect.height
gui.text(f"__Physical Rectangle__")
gui.text(f"Top Left: {round(x), round(y)}")
gui.text(f"Top Right: {round(x + width), round(y)}")
gui.text(f"Bottom Left: {round(x), round(y + height)}")
gui.text(f"Bottom Right: {round(x + width), round(y + height)}")
gui.text(f"Center: {round(screen.rect.center.x), round(screen.rect.center.y)}")
gui.spacer()
gui.text(f"Width: {round(width)}")
gui.text(f"Height: {round(height)}")
gui.line()
gui.text(f"Say 'window traits hide' to close this window.")
gui.line()
if gui.button("Close"):
_win_show_gui.hide()
class WinCompassControl:
@classmethod
def win_set_rect(cls, old_rect: ui.Rect, rect_id: int, rect_in: ui.Rect) -> Tuple[bool, ui.Rect]:
"""Callback invoked by CompassControl engine for updating the window rect using talon API"""
start_time = time.time_ns()
if not rect_in:
raise ValueError('rect_in is None')
max_retries = retries = settings.get('user.win_set_retries')
queue_timeout = settings.get('user.win_set_queue_timeout')
# rect update code adapted from https://talonvoice.slack.com/archives/C9MHQ4AGP/p1635971780355900
q = queue.Queue()
def on_move(event_win: ui.Window) -> None:
if event_win == w and w.rect != old_rect:
q.put(1)
if testing:
print(f'_win_set_rect: win position changed')
#
def on_resize(event_win: ui.Window) -> None:
if event_win == w and w.rect != old_rect:
q.put(1)
if testing:
print(f'_win_set_rect: win size changed')
# get window handle
windows = ui.windows()
for w in windows:
if w.id == rect_id:
break
else:
if settings.get('user.win_verbose_warnings') != 0:
logging.warning(f'_win_set_rect: invalid window id "{rect_id}"')
return False, w.rect
if testing:
print(f'_win_set_rect: starting...{old_rect=}, {rect_in=}, {w.rect=}')
result = False, old_rect
while retries >= 0:
event_count = 0
if (rect_in.x, rect_in.y) != (w.rect.x, w.rect.y):
# print(f'_win_set_rect: register win_move')
ui.register('win_move', on_move)
event_count += 1
if (rect_in.width, rect_in.height) != (w.rect.width, w.rect.height):
# print(f'_win_set_rect: register win_resize')
ui.register('win_resize', on_resize)
event_count += 1
if event_count == 0:
# sometimes the queue get below times out, yet by the time we loop around here
# for a retry, the set operation has completed successfully. then, the checks above
# fall through to this block. so, the result we return is based on whether this is
# our first time through the loop or not.
success = retries < max_retries
# no real work to do
result = success, rect_in
if testing:
print('_win_set_rect: nothing to do, window already matches given rect.')
break
# do it to it
start_time_rect = time.time_ns()
w.rect = rect_in.copy()
try:
# for testing
#raise queue.Empty()
#raise Exception('just testing')
q.get(timeout=queue_timeout)
if event_count == 2:
q.get(timeout=queue_timeout)
except queue.Empty:
if testing:
print('_win_set_rect: timed out waiting for window update.')
if retries > 0:
if testing:
print('_win_set_rect: retrying after time out...')
retries -= 1
continue
else:
if testing:
print('_win_set_rect: no more retries, failed')
# no more retries
break
else:
if testing:
print(f'_win_set_rect: before: {old_rect}')
print(f'_win_set_rect: requested: {rect_in}')
print(f'_win_set_rect: after: {w.rect}')
position_matches_request = (rect_in.x, rect_in.y) == (w.rect.x, w.rect.y)
size_matches_request = (rect_in.width, rect_in.height) == (w.rect.width, w.rect.height)
if not position_matches_request or not size_matches_request:
if False and app.platform == 'linux':
if testing:
print('_win_set_rect: linux - timed out waiting for window update.')
if retries > 0:
if testing:
print('_win_set_rect: linux - retrying after time out...')
retries -= 1
continue
else:
if testing:
print('_win_set_rect: linux - no more retries, failed')
# no more retries
break
else:
# need to pass rect_id and old_rect here so they can be saved for 'win revert' usage
raise compass_control.RectUpdateError(rect_id=rect_id, initial=old_rect, requested=rect_in, actual=w.rect)
else:
result = True, w.rect
# done with retry loop
break
finally:
ui.unregister('win_move', on_move)
ui.unregister('win_resize', on_resize)
elapsed_time_ms = (time.time_ns() - start_time) / 1e6
if testing:
print(f'_win_set_rect: done ({elapsed_time_ms} ms)')
return result
def win_stop(self) -> None:
"""Callback invoked by CompassControl engine after stopping a continuous operation"""
win_stop_gui.hide()
def on_ready():
"""Callback invoked by Talon, where we populate our global objects"""
global win_compass_control, compass_control, ctx_stop
# if testing:
# print(f"on_ready: {settings.get('user.win_continuous_move_rate')=}")
win_compass_control= WinCompassControl()
compass_control_settings = {
'_continuous_move_frequency_str': setting_move_frequency,
'_continuous_resize_frequency_str': setting_resize_frequency,
'_continuous_move_rate': setting_move_rate,
'_continuous_resize_rate': setting_resize_rate,
'_verbose_warnings': setting_verbose_warnings
}
compass_control= CompassControl(
TAG_NAME,
win_compass_control.win_set_rect,
win_compass_control.win_stop,
compass_control_settings,
testing
)
# context containing the stop command, enabled only when a continuous move/resize is running
ctx_stop = Context()
ctx_stop.matches = fr"""
tag: user.{TAG_NAME}
"""
@ctx_stop.action_class("user")
class WindowTweakActions:
"""
# Commands for controlling continuous window move/resize operations
"""
def win_stop() -> None:
"Stops current window move/resize operation"
compass_control.continuous_stop()
app.register("ready", on_ready)
@mod.action_class
class Actions:
def win_show_info() -> None:
"Shows information about current window position and size"
_win_show_gui.show()
def win_hide_info() -> None:
"Hides the window information window"
_win_show_gui.hide()
def win_stop() -> None:
"Module action declaration for stopping current window move/resize operation"
compass_control.continuous_stop()
def win_move(direction: Optional[Direction] = None) -> None:
"Move window in small increments in the given direction, until stopped"
if not direction:
direction = compass_direction(['center'])
w = ui.active_window()
compass_control.mover.continuous_init(w.rect, w.id, w.screen.visible_rect, w.screen.dpi_x, w.screen.dpi_y, direction)
if settings.get('user.win_hide_move_gui') == 0:
win_stop_gui.show()
def win_move_absolute(x: float, y: float, region: Optional[Direction] = None) -> None:
"Move window to given absolute position, centered on the point indicated by the given region"
w = ui.active_window()
compass_control.mover.move_absolute(w.rect, w.id, x, y, region)
def win_move_to_pointer(region: Optional[NonDualDirection] = non_dual_direction(['north', 'west'])):
"Move window to pointer position, centered on the point indicated by the given region"
w = ui.active_window()
compass_control.mover.resize_to_pointer(w.rect, w.id, w.screen.visible_rect, region)
def win_stretch(direction: Optional[Direction] = None) -> None:
"Stretch window in small increments until stopped, optionally in the given direction"
if not direction:
direction = compass_direction(['center'])
w = ui.active_window()
compass_control.sizer.continuous_init(w.rect, w.id, w.screen.visible_rect, 1, w.screen.dpi_x, w.screen.dpi_y, direction)
if settings.get('user.win_hide_resize_gui') == 0:
win_stop_gui.show()
def win_shrink(direction: Optional[Direction] = None) -> None:
"Shrink window in small increments until stopped, optionally in the given direction"
w = ui.active_window()
if not direction:
direction = compass_direction(['center'])
compass_control.sizer.continuous_init(w.rect, w.id, w.screen.visible_rect, -1, w.screen.dpi_x, w.screen.dpi_y, direction)
if settings.get('user.win_hide_resize_gui') == 0:
win_stop_gui.show()
def win_resize_absolute(target_width: float, target_height: float, region: Optional[Direction] = None) -> None:
"Size window to given absolute dimensions, optionally by stretching/shrinking in the direction indicated by the given region"
if not region:
region = compass_direction(['center'])
w = ui.active_window()
compass_control.sizer.resize_absolute(w.rect, w.id, target_width, target_height, region)
def win_resize_to_pointer(nd_direction: NonDualDirection) -> None:
"Stretch or shrink window to pointer position, centered on the point indicated by the given region"
w = ui.active_window()
compass_control.sizer.resize_to_pointer(w.rect, w.id, w.screen.visible_rect, nd_direction)
def win_move_pixels(distance: int, direction: Optional[Direction] = None) -> None:
"Move window some number of pixels"
if not direction:
direction = compass_direction(['center'])
w = ui.active_window()
delta_width, delta_height = compass_control.get_component_dimensions(w.rect, w.id, w.screen.visible_rect, distance, direction, 'move')
return compass_control.mover.move_pixels_relative(w.rect, w.id, w.screen.visible_rect, delta_width, delta_height, direction)
def win_move_percent(percent: float, direction: Optional[Direction] = None) -> None:
"Move window some percentage of the current size"
if not direction:
direction = compass_direction(['center'])
w = ui.active_window()
delta_width, delta_height = compass_control.get_component_dimensions_by_percent(w.rect, w.id, w.screen.visible_rect, percent, direction, 'move')
return compass_control.mover.move_pixels_relative(w.rect, w.id, w.screen.visible_rect, delta_width, delta_height, direction)
def win_resize_pixels(distance: int, direction: Optional[Direction] = None) -> None:
"Change window size by pixels"
w = ui.active_window()
if not direction:
direction = compass_direction(['center'])
delta_width, delta_height = compass_control.get_component_dimensions(w.rect, w.id, w.screen.visible_rect, distance, direction, 'resize')
if testing:
print(f'win_resize_pixels: {delta_width=}, {delta_height=}')
compass_control.sizer.resize_pixels_relative(w.rect, w.id, w.screen.visible_rect, delta_width, delta_height, direction)
def win_resize_percent(percent: float, direction: Optional[Direction] = None) -> None:
"Change window size by a percentage of current size"
if not direction:
direction = compass_direction(['center'])
w = ui.active_window()
delta_width, delta_height = compass_control.get_component_dimensions_by_percent(w.rect, w.id, w.screen.visible_rect, percent, direction, 'resize')
if testing:
print(f'win_resize_percent: {delta_width=}, {delta_height=}')
compass_control.sizer.resize_pixels_relative(w.rect, w.id, w.screen.visible_rect, delta_width, delta_height, direction)
def win_snap_percent(percent: int) -> None:
"Center window and change size to given percentage of parent screen (in each direction)"
direction = compass_direction(['center'])
w = ui.active_window()
compass_control.snap(w.rect, w.id, w.screen.visible_rect, percent, direction)
def win_revert() -> None:
"Restore current window's last remembered size and position"
w = ui.active_window()
compass_control.revert(w.rect, w.id)
def win_test_bresenham(num: int) -> None:
"Test modified bresenham algo"
if num == 1:
compass_control.mover.test_bresenham()