Skip to content

Commit eb85958

Browse files
authored
Unify text input events onto a base class and rename UITextEvent (#2312)
* Unify UIText* events around a base class
1 parent 05b476f commit eb85958

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

arcade/gui/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from arcade.gui.events import UIOnChangeEvent
1616
from arcade.gui.events import UIOnClickEvent
1717
from arcade.gui.events import UIOnUpdateEvent
18-
from arcade.gui.events import UITextEvent
18+
from arcade.gui.events import UITextInputEvent
1919
from arcade.gui.events import UITextMotionEvent
2020
from arcade.gui.events import UITextMotionSelectEvent
2121
from arcade.gui.mixins import UIDraggableMixin
@@ -87,7 +87,7 @@
8787
"UISpace",
8888
"UISpriteWidget",
8989
"UITextArea",
90-
"UITextEvent",
90+
"UITextInputEvent",
9191
"UITextMotionEvent",
9292
"UITextMotionSelectEvent",
9393
"UITextureButton",

arcade/gui/events.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,39 @@ class UIKeyReleaseEvent(UIKeyEvent):
9595

9696
@dataclass
9797
class UITextEvent(UIEvent):
98-
"""Covers all the text cursor event."""
98+
"""Base class for text-related events.
99+
100+
It holds no data of its own.
101+
"""
102+
103+
pass
104+
105+
106+
@dataclass
107+
class UITextInputEvent(UITextEvent):
108+
"""Triggered whenever the user inputs any text.
109+
110+
Usually, this will be after :py:class:`UIKeyPressEvent` and before a
111+
:py:class:`UIKeyReleaseEvent`. It may also occur when:
112+
113+
* the user holds down a key to repeat letters or spaces
114+
* a platform-specific input method, such as pen input on a tablet PC
115+
116+
To learn more, see pyglet's `relevant documentation <https://pyglet.readthedocs.io/en/development/modules/window.html#pyglet.window.Window.on_text>`_.
117+
"""
99118

100119
text: str
101120

102121

103122
@dataclass
104-
class UITextMotionEvent(UIEvent):
123+
class UITextMotionEvent(UITextEvent):
105124
"""Triggered when text cursor moves."""
106125

107126
motion: Any
108127

109128

110129
@dataclass
111-
class UITextMotionSelectEvent(UIEvent):
130+
class UITextMotionSelectEvent(UITextEvent):
112131
"""Triggered when the text cursor moves selecting the text with it."""
113132

114133
selection: Any

arcade/gui/experimental/password_input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
from typing import Optional
44

5-
from arcade.gui import Surface, UIEvent, UIInputText, UITextEvent
5+
from arcade.gui import Surface, UIEvent, UIInputText, UITextInputEvent
66

77

88
class UIPasswordInput(UIInputText):
99
"""A password input field. The text is hidden with asterisks."""
1010

1111
def on_event(self, event: UIEvent) -> Optional[bool]:
12-
if isinstance(event, UITextEvent):
12+
if isinstance(event, UITextInputEvent):
1313
event.text = event.text.replace("\n", "").replace("\r", "") # remove new lines!
1414
return super().on_event(event)
1515

arcade/gui/experimental/typed_text_input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import arcade
66
from arcade.color import BLACK, RED, WHITE
7-
from arcade.gui import UIEvent, UIInputText, UILabel, UITextEvent
7+
from arcade.gui import UIEvent, UIInputText, UILabel, UITextInputEvent
88
from arcade.types import Color, RGBOrA255
99
from arcade.utils import type_name
1010

@@ -150,7 +150,7 @@ def _checked_parse(self, text: str):
150150

151151
def on_event(self, event: UIEvent) -> Optional[bool]:
152152
# print(f"In {type_name(event)}")
153-
if isinstance(event, UITextEvent) and self._active:
153+
if isinstance(event, UITextInputEvent) and self._active:
154154
text = event.text.replace("\r", "").replace("\r", "")
155155
event.text = text
156156

arcade/gui/ui_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
UIMouseReleaseEvent,
2828
UIMouseScrollEvent,
2929
UIOnUpdateEvent,
30-
UITextEvent,
30+
UITextInputEvent,
3131
UITextMotionEvent,
3232
UITextMotionSelectEvent,
3333
)
@@ -402,7 +402,7 @@ def on_key_release(self, symbol: int, modifiers: int):
402402
return self.dispatch_ui_event(UIKeyReleaseEvent(self, symbol, modifiers)) # type: ignore
403403

404404
def on_text(self, text):
405-
return self.dispatch_ui_event(UITextEvent(self, text))
405+
return self.dispatch_ui_event(UITextInputEvent(self, text))
406406

407407
def on_text_motion(self, motion):
408408
return self.dispatch_ui_event(UITextMotionEvent(self, motion))

arcade/gui/widgets/text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
UIMouseEvent,
1515
UIMousePressEvent,
1616
UIMouseScrollEvent,
17-
UITextEvent,
17+
UITextInputEvent,
1818
UITextMotionEvent,
1919
UITextMotionSelectEvent,
2020
)
@@ -449,7 +449,7 @@ def on_event(self, event: UIEvent) -> Optional[bool]:
449449
# If active pass all non press events to caret
450450
if self._active:
451451
# Act on events if active
452-
if isinstance(event, UITextEvent):
452+
if isinstance(event, UITextInputEvent):
453453
self.caret.on_text(event.text)
454454
self.trigger_full_render()
455455
elif isinstance(event, UITextMotionEvent):

tests/unit/gui/test_uimanager_callbacks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
UIMouseScrollEvent,
55
UIMouseMovementEvent,
66
UIKeyPressEvent,
7-
UITextEvent,
7+
UITextInputEvent,
88
UITextMotionEvent,
99
UITextMotionSelectEvent,
1010
)
@@ -100,7 +100,7 @@ def test_on_text_passes_an_event(uimanager):
100100
uimanager.on_text("a")
101101

102102
event = records[-1]
103-
assert isinstance(event, UITextEvent)
103+
assert isinstance(event, UITextInputEvent)
104104
assert event.text == "a"
105105

106106

0 commit comments

Comments
 (0)