Skip to content

Commit

Permalink
Merge pull request #501 from ganimtron-10/textbox_hook
Browse files Browse the repository at this point in the history
Creating an `off_focus` hook in `TextBox2D`
  • Loading branch information
skoudoro committed Jun 27, 2022
2 parents cc2dd01 + af1e522 commit 541e7c1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion fury/data/files/test_ui_textbox.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"CharEvent": 36, "MouseMoveEvent": 400, "KeyPressEvent": 36, "KeyReleaseEvent": 36, "LeftButtonPressEvent": 1, "LeftButtonReleaseEvent": 1, "RightButtonPressEvent": 0, "RightButtonReleaseEvent": 0, "MiddleButtonPressEvent": 0, "MiddleButtonReleaseEvent": 0}
{"CharEvent": 75, "MouseMoveEvent": 1, "KeyPressEvent": 77, "KeyReleaseEvent": 75, "LeftButtonPressEvent": 2, "LeftButtonReleaseEvent": 2, "RightButtonPressEvent": 0, "RightButtonReleaseEvent": 0, "MiddleButtonPressEvent": 0, "MiddleButtonReleaseEvent": 0}
Binary file modified fury/data/files/test_ui_textbox.log.gz
Binary file not shown.
15 changes: 9 additions & 6 deletions fury/ui/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def __init__(self, width, height, text="Enter Text", position=(100, 10),
self.caret_pos = 0
self.init = True

self.off_focus = lambda ui: None

def _setup(self):
"""Setup this UI component.
Expand Down Expand Up @@ -186,11 +188,12 @@ def handle_character(self, key, key_char):
----------
character : str
"""
if key_char != '' and key_char in printable:
self.add_character(key_char)
elif key.lower() == "return":
if key.lower() == "return":
self.render_text(False)
self.off_focus(self)
return True
elif key_char != '' and key_char in printable:
self.add_character(key_char)
if key.lower() == "backspace":
self.remove_character()
elif key.lower() == "left":
Expand Down Expand Up @@ -1740,9 +1743,9 @@ def _setup(self):
# Option's button
self.button_icons = []
self.button_icons.append(('unchecked',
read_viz_icons(fname="stop2.png")))
read_viz_icons(fname="stop2.png")))
self.button_icons.append(('checked',
read_viz_icons(fname="checkmark.png")))
read_viz_icons(fname="checkmark.png")))
self.button = Button2D(icon_fnames=self.button_icons,
size=self.button_size)

Expand Down Expand Up @@ -2396,7 +2399,7 @@ def _setup(self):
scroll_bar_height = self.nb_slots * (size[1] - 2 * self.margin) \
/ len(self.values)
self.scroll_bar = Rectangle2D(size=(int(size[0]/20),
scroll_bar_height))
scroll_bar_height))
if len(self.values) <= self.nb_slots:
self.scroll_bar.set_visibility(False)
self.panel.add_element(
Expand Down
11 changes: 11 additions & 0 deletions fury/ui/tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def test_ui_textbox(recording=False):
another_textbox_test = ui.TextBox2D(height=3, width=10, text="Enter Text")
another_textbox_test.set_message("Enter Text")

# Checking whether textbox went out of focus
is_off_focused = [False]

def _off_focus(textbox):
is_off_focused[0] = True

# Set up a callback when textbox went out of focus
textbox_test.off_focus = _off_focus

# Assign the counter callback to every possible event.
event_counter = EventCounter()
event_counter.monitor(textbox_test)
Expand All @@ -50,6 +59,8 @@ def test_ui_textbox(recording=False):
expected = EventCounter.load(expected_events_counts_filename)
event_counter.check_counts(expected)

npt.assert_equal(is_off_focused[0], True)


def test_ui_line_slider_2d_horizontal_bottom(recording=False):
filename = "test_ui_line_slider_2d_horizontal_bottom"
Expand Down

0 comments on commit 541e7c1

Please sign in to comment.