Skip to content

Commit

Permalink
Move static attributes from GlobalConfig to controller params
Browse files Browse the repository at this point in the history
Update key-value pairs in the params dictionary for a generic
controller. We can now set click timeout and other settings
for each separate controller instance.

Localize delay_before_keys to display controllers moving it
away from the region class as it is still relate to key typing
configuration.

rename toggle_delay to mouse_toggle_delay
  • Loading branch information
laracroft37 committed Dec 5, 2024
1 parent 8ebbd7f commit 0e823d0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 96 deletions.
9 changes: 4 additions & 5 deletions docs/examples/custom_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,16 @@ def mouse_click(self, button=None, count=1, modifiers=None):
See base method for details.
"""
toggle_timeout = GlobalConfig.toggle_delay
click_timeout = GlobalConfig.click_delay
button = self._mousemap.LEFT_BUTTON if button is None else button
if modifiers != None:
self.keys_toggle(modifiers, True)
for _ in range(count):
self._backend_obj.mouse_button(button)
# BUG: QEMU's monitor doesn't handle click events sent too fast,
# so we sleep a bit between mouse up and mouse down
time.sleep(toggle_timeout)
time.sleep(self.params["control"]["mouse_toggle_delay"])
self._backend_obj.mouse_button(button)
time.sleep(click_timeout)
time.sleep(self.params["control"]["after_click_delay"])
if modifiers != None:
self.keys_toggle(modifiers, False)

Expand Down Expand Up @@ -314,6 +312,7 @@ def keys_type(self, text, modifiers=None):
See base method for details.
"""
time.sleep(self.params["control"]["delay_before_keys"])
if modifiers != None:
self.keys_toggle(modifiers, True)

Expand Down Expand Up @@ -350,7 +349,7 @@ def keys_type(self, text, modifiers=None):
elif special_chars.has_key(char) and GlobalConfig.preprocess_special_chars:
char = "shift-%s" % special_chars[char]
self._backend_obj.sendkey(char, hold_time=1)
time.sleep(GlobalConfig.delay_between_keys)
time.sleep(self.params["control"]["delay_between_keys"])

if modifiers != None:
self.keys_toggle(modifiers, False)
68 changes: 0 additions & 68 deletions guibot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ class GlobalConfig(type):
"""

# operational parameters shared between all instances
_toggle_delay = 0.05
_click_delay = 0.1
_drag_delay = 0.5
_drop_delay = 0.5
_keys_delay = 0.2
_type_delay = 0.1
_rescan_speed_on_find = 0.2
_wait_for_animations = False
_smooth_mouse_drag = True
Expand All @@ -80,38 +76,6 @@ class GlobalConfig(type):
_deep_learn_backend = "pytorch"
_hybrid_match_backend = "template"

def toggle_delay(cls, value: float = None) -> float | None:
"""
Get or set property attribute.
:param value: time interval between mouse down and up in a click
:returns: current value if no argument was passed otherwise None
"""
if value is None:
return cls._toggle_delay
else:
cls._toggle_delay = value
return None

#: time interval between mouse down and up in a click
toggle_delay = property(fget=toggle_delay, fset=toggle_delay)

def click_delay(cls, value: float = None) -> float | None:
"""
Get or set property attribute.
:param value: time interval after a click (in a double or n-click)
:returns: current value if no argument was passed otherwise None
"""
if value is None:
return cls._click_delay
else:
cls._click_delay = value
return None

#: time interval after a click (in a double or n-click)
click_delay = property(fget=click_delay, fset=click_delay)

def delay_after_drag(cls, value: float = None) -> float | None:
"""
Get or set property attribute.
Expand Down Expand Up @@ -144,38 +108,6 @@ def delay_before_drop(cls, value: float = None) -> float | None:
#: timeout before drop operation
delay_before_drop = property(fget=delay_before_drop, fset=delay_before_drop)

def delay_before_keys(cls, value: float = None) -> float | None:
"""
Get or set property attribute.
:param value: timeout before key press operation
:returns: current value if no argument was passed otherwise None
"""
if value is None:
return cls._keys_delay
else:
cls._keys_delay = value
return None

#: timeout before key press operation
delay_before_keys = property(fget=delay_before_keys, fset=delay_before_keys)

def delay_between_keys(cls, value: float = None) -> float | None:
"""
Get or set property attribute.
:param value: time interval between two consecutively typed keys
:returns: current value if no argument was passed otherwise None
"""
if value is None:
return cls._type_delay
else:
cls._type_delay = value
return None

#: time interval between two consecutively typed keys
delay_between_keys = property(fget=delay_between_keys, fset=delay_between_keys)

def rescan_speed_on_find(cls, value: float = None) -> float | None:
"""
Get or set property attribute.
Expand Down
41 changes: 22 additions & 19 deletions guibot/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def __configure_backend(
log.log(9, "Setting backend for %s to %s", category, backend)
self.params[category] = {}
self.params[category]["backend"] = backend
self.params[category]["mouse_toggle_delay"] = 0.05
self.params[category]["after_click_delay"] = 0.1
self.params[category]["delay_between_keys"] = 0.1
self.params[category]["delay_before_keys"] = 0.2
log.log(9, "%s %s\n", category, self.params[category])

def configure_backend(
Expand Down Expand Up @@ -344,6 +348,7 @@ def keys_press(self, keys: list[str] | str) -> None:
:param keys: characters or special keys depending on the backend
(see :py:class:`inputmap.Key` for extensive list)
"""
time.sleep(self.params["control"]["delay_before_keys"])
# BUG: pressing multiple times the same key does not work?
self.keys_toggle(keys, True)
self.keys_toggle(keys, False)
Expand Down Expand Up @@ -523,17 +528,15 @@ def mouse_click(
See base method for details.
"""
toggle_timeout = GlobalConfig.toggle_delay
click_timeout = GlobalConfig.click_delay
button = self._mousemap.LEFT_BUTTON if button is None else button
if modifiers is not None:
self.keys_toggle(modifiers, True)
for _ in range(count):
self._backend_obj.mouse.click(button)
# BUG: the mouse button of autopy is pressed down forever (on LEFT)
time.sleep(toggle_timeout)
time.sleep(self.params["control"]["mouse_toggle_delay"])

Check warning on line 537 in guibot/controller.py

View check run for this annotation

Codecov / codecov/patch

guibot/controller.py#L537

Added line #L537 was not covered by tests
self.mouse_up(button)
time.sleep(click_timeout)
time.sleep(self.params["control"]["after_click_delay"])

Check warning on line 539 in guibot/controller.py

View check run for this annotation

Codecov / codecov/patch

guibot/controller.py#L539

Added line #L539 was not covered by tests
if modifiers is not None:
self.keys_toggle(modifiers, False)

Expand Down Expand Up @@ -576,13 +579,14 @@ def keys_type(self, text: list[str] | str, modifiers: list[str] = None) -> None:
See base method for details.
"""
time.sleep(self.params["control"]["delay_before_keys"])

Check warning on line 582 in guibot/controller.py

View check run for this annotation

Codecov / codecov/patch

guibot/controller.py#L582

Added line #L582 was not covered by tests
if modifiers is not None:
self.keys_toggle(modifiers, True)

for part in text:
for char in str(part):
self._backend_obj.key.tap(char, [])
time.sleep(GlobalConfig.delay_between_keys)
time.sleep(self.params["control"]["delay_between_keys"])

Check warning on line 589 in guibot/controller.py

View check run for this annotation

Codecov / codecov/patch

guibot/controller.py#L589

Added line #L589 was not covered by tests
# alternative option:
# autopy.key.type_string(text)

Expand Down Expand Up @@ -748,18 +752,16 @@ def mouse_click(
See base method for details.
"""
toggle_timeout = GlobalConfig.toggle_delay
click_timeout = GlobalConfig.click_delay
button = self._mousemap.LEFT_BUTTON if button is None else button
if modifiers is not None:
self.keys_toggle(modifiers, True)
for _ in range(count):
# BUG: the xdotool click is too fast and non-configurable with timeout
# self._backend_obj.run("click", str(button))
self.mouse_down(button)
time.sleep(toggle_timeout)
time.sleep(self.params["control"]["mouse_toggle_delay"])
self.mouse_up(button)
time.sleep(click_timeout)
time.sleep(self.params["control"]["after_click_delay"])
if modifiers is not None:
self.keys_toggle(modifiers, False)

Expand Down Expand Up @@ -805,6 +807,7 @@ def keys_type(self, text: list[str] | str, modifiers: list[str] = None) -> None:
See base method for details.
"""
time.sleep(self.params["control"]["delay_before_keys"])
if modifiers is not None:
self.keys_toggle(modifiers, True)

Expand Down Expand Up @@ -968,8 +971,6 @@ def mouse_click(
See base method for details.
"""
toggle_timeout = GlobalConfig.toggle_delay
click_timeout = GlobalConfig.click_delay
button = self._mousemap.LEFT_BUTTON if button is None else button
if modifiers is not None:
self.keys_toggle(modifiers, True)
Expand All @@ -978,9 +979,9 @@ def mouse_click(
# sent too fast, so we sleep between mouse up and down and avoid mousePress
# self._backend_obj.mousePress(button)
self.mouse_down(button)
time.sleep(toggle_timeout)
time.sleep(self.params["control"]["mouse_toggle_delay"])
self.mouse_up(button)
time.sleep(click_timeout)
time.sleep(self.params["control"]["after_click_delay"])
if modifiers is not None:
self.keys_toggle(modifiers, False)

Expand Down Expand Up @@ -1032,6 +1033,7 @@ def keys_type(self, text: list[str] | str, modifiers: list[str] = None) -> None:
See base method for details.
"""
time.sleep(self.params["control"]["delay_before_keys"])
if modifiers is not None:
self.keys_toggle(modifiers, True)

Expand All @@ -1045,7 +1047,7 @@ def keys_type(self, text: list[str] | str, modifiers: list[str] = None) -> None:
char = "space"
elif char == "\n":
char = "return"
time.sleep(GlobalConfig.delay_between_keys)
time.sleep(self.params["control"]["delay_between_keys"])
self._backend_obj.keyPress(char)

if modifiers is not None:
Expand Down Expand Up @@ -1182,8 +1184,6 @@ def mouse_click(
See base method for details.
"""
toggle_timeout = GlobalConfig.toggle_delay
click_timeout = GlobalConfig.click_delay
button = self._mousemap.LEFT_BUTTON if button is None else button
if modifiers is not None:
self.keys_toggle(modifiers, True)
Expand All @@ -1192,9 +1192,9 @@ def mouse_click(
# control the toggle speed
# self._backend_obj.click(clicks=count, interval=click_timeout, button=button)
self._backend_obj.mouseDown(button=button)
time.sleep(toggle_timeout)
time.sleep(self.params["control"]["mouse_toggle_delay"])
self._backend_obj.mouseUp(button=button)
time.sleep(click_timeout)
time.sleep(self.params["control"]["after_click_delay"])
if modifiers is not None:
self.keys_toggle(modifiers, False)

Expand Down Expand Up @@ -1253,11 +1253,14 @@ def keys_type(self, text: list[str] | str, modifiers: list[str] = None) -> None:
See base method for details.
"""
time.sleep(self.params["control"]["delay_before_keys"])
if modifiers is not None:
self.keys_toggle(modifiers, True)

for part in text:
self._backend_obj.typewrite(part, interval=GlobalConfig.delay_between_keys)
self._backend_obj.typewrite(
part, interval=self.params["control"]["delay_between_keys"]
)

if modifiers is not None:
self.keys_toggle(modifiers, False)
4 changes: 0 additions & 4 deletions guibot/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,6 @@ def press_keys(self, keys: str | list[str]) -> "Region":
self.press_keys(['a', 'b', 3])
"""
keys_list = self._parse_keys(keys)
time.sleep(GlobalConfig.delay_before_keys)
self.dc_backend.keys_press(keys_list)
return self

Expand All @@ -1050,7 +1049,6 @@ def press_at(
"""
keys_list = self._parse_keys(keys, target_or_location)
match = self.click(target_or_location)
time.sleep(GlobalConfig.delay_before_keys)
self.dc_backend.keys_press(keys_list)
return match

Expand Down Expand Up @@ -1172,7 +1170,6 @@ def type_text(self, text: list[str] | str, modifiers: list[str] = None) -> "Regi
typing special keys.
"""
text_list = self._parse_text(text)
time.sleep(GlobalConfig.delay_before_keys)
if modifiers is not None:
if isinstance(modifiers, str):
modifiers = [modifiers]
Expand All @@ -1198,7 +1195,6 @@ def type_at(
match = None
if target_or_location is not None:
match = self.click(target_or_location)
time.sleep(GlobalConfig.delay_before_keys)
if modifiers is not None:
if isinstance(modifiers, str):
modifiers = [modifiers]
Expand Down

0 comments on commit 0e823d0

Please sign in to comment.