9
9
from . import colors
10
10
from . import sending
11
11
from .exceptions import Error
12
- from .flow import ahk_call , global_ahk_lock
12
+ from .flow import ahk_call , global_ahk_lock , _wait_for
13
13
from .hotkey_context import HotkeyContext
14
14
from .settings import get_settings , optional_ms
15
15
from .unset import UNSET , UnsetType
@@ -280,7 +280,7 @@ def wait(self, title=UNSET, *, class_name=UNSET, id=UNSET, pid=UNSET, exe=UNSET,
280
280
<https://www.autohotkey.com/docs/commands/WinWait.htm>`_
281
281
"""
282
282
self = self ._filter (title , class_name , id , pid , exe , text , match )
283
- return self . _wait ( "WinWait" , timeout )
283
+ return _wait_for ( timeout , self . exist ) or Window ( None )
284
284
285
285
def wait_active (self , title = UNSET , * , class_name = UNSET , id = UNSET , pid = UNSET , exe = UNSET , text = UNSET , match = None ,
286
286
timeout = None ):
@@ -297,7 +297,7 @@ def wait_active(self, title=UNSET, *, class_name=UNSET, id=UNSET, pid=UNSET, exe
297
297
query = self ._query ()
298
298
if query == ("" , "" , "" , "" ):
299
299
self = dc .replace (self , title = "A" )
300
- return self . _wait ( "WinWaitActive" , timeout )
300
+ return _wait_for ( timeout , self . get_active ) or Window ( None )
301
301
302
302
def wait_inactive (self , title = UNSET , * , class_name = UNSET , id = UNSET , pid = UNSET , exe = UNSET , text = UNSET , match = None ,
303
303
timeout = None ) -> bool :
@@ -316,7 +316,7 @@ def wait_inactive(self, title=UNSET, *, class_name=UNSET, id=UNSET, pid=UNSET, e
316
316
<https://www.autohotkey.com/docs/commands/WinWaitActive.htm>`_
317
317
"""
318
318
self = self ._filter (title , class_name , id , pid , exe , text , match )
319
- return self . _wait_close ( "WinWaitNotActive" , timeout )
319
+ return _wait_for ( timeout , lambda : not self . get_active ()) or False
320
320
321
321
def wait_close (self , title = UNSET , * , class_name = UNSET , id = UNSET , pid = UNSET , exe = UNSET , text = UNSET , match = None ,
322
322
timeout = None ):
@@ -335,22 +335,9 @@ def wait_close(self, title=UNSET, *, class_name=UNSET, id=UNSET, pid=UNSET, exe=
335
335
<https://www.autohotkey.com/docs/commands/WinWaitClose.htm>`_
336
336
"""
337
337
self = self ._filter (title , class_name , id , pid , exe , text , match )
338
- return self ._wait_close ("WinWaitClose" , timeout )
339
-
340
- def _wait (self , cmd , timeout ):
341
- win_id = self ._call (cmd , * self ._include (), timeout , * self ._exclude (), set_delay = True )
342
- if not win_id :
343
- return Window (None )
344
- return Window (win_id )
345
-
346
- def _wait_close (self , cmd , timeout ):
347
- # WinWaitClose and WinWaitNotActive don't set the Last Found Window,
348
- # return False if the wait was timed out.
349
- ok = self ._call (cmd , * self ._include (), timeout , * self ._exclude (), set_delay = True )
350
- if ok is None :
351
- # There are no matching windows, and that's what we are waiting for.
352
- return True
353
- return bool (ok )
338
+ # WinWaitClose doesn't set Last Found Window, return False if the wait
339
+ # was timed out.
340
+ return _wait_for (timeout , lambda : not self .exist ()) or False
354
341
355
342
def close_all (self , title = UNSET , * , class_name = UNSET , id = UNSET , pid = UNSET , exe = UNSET , text = UNSET , match = None ,
356
343
timeout = None ):
@@ -468,9 +455,9 @@ def _group_action(self, cmd, timeout=UNSET):
468
455
query_hash_str = str (query_hash ).replace ("-" , "m" ) # AHK doesn't allow "-" in group names
469
456
label = ""
470
457
self ._call ("GroupAdd" , query_hash_str , * self ._include (), label , * self ._exclude ())
471
- self ._call (cmd , f"ahk_group { query_hash_str } " , "" , timeout or "" , set_delay = True )
458
+ self ._call (cmd , f"ahk_group { query_hash_str } " , "" , "" , set_delay = True )
472
459
if timeout is not UNSET :
473
- return not self .exist ( )
460
+ return self .wait_close ( timeout = timeout )
474
461
475
462
def window_context (self , title = UNSET , * , class_name = UNSET , id = UNSET , pid = UNSET , exe = UNSET , text = UNSET , match = None ):
476
463
"""window_context(title: str = UNSET, **criteria) -> ahkpy.HotkeyContext
@@ -1974,11 +1961,14 @@ def wait_status_bar(self, bar_text="", *,
1974
1961
:command: `StatusBarWait
1975
1962
<https://www.autohotkey.com/docs/commands/StatusBarWait.htm>`_
1976
1963
"""
1964
+ # TODO: StatusBarWait is blocking and is not interruptable. However, it
1965
+ # is usually more efficient to use StatusBarWait rather than calling
1966
+ # StatusBarGetText in a loop.
1977
1967
try :
1978
1968
ok = self ._call (
1979
1969
"StatusBarWait" ,
1980
1970
bar_text ,
1981
- timeout if timeout is not None else "" ,
1971
+ timeout ,
1982
1972
part + 1 ,
1983
1973
* self ._include (),
1984
1974
interval * 1000 ,
0 commit comments