Textinput - Separate deletion and insertion / action keys handling (#5482)

* refactored textinput::_key_down

* PEP8 fix: removed blank lines

* PEP8 fix: long if statement now on 2 lines

* fixed single-line enter issue
This commit is contained in:
Boyan Hristov 2017-11-19 22:47:02 +01:00 committed by Peter Badida
parent e64d24d33b
commit efe213d7f5
1 changed files with 18 additions and 20 deletions

View File

@ -2247,9 +2247,24 @@ class TextInput(FocusBehavior, Widget):
def _key_down(self, key, repeat=False):
displayed_str, internal_str, internal_action, scale = key
if internal_action is None:
if self._selection:
# handle deletion
if (self._selection and
internal_action in (None, 'del', 'backspace', 'enter')):
if internal_action != 'enter' or self.multiline:
self.delete_selection()
elif internal_action == 'del':
# Move cursor one char to the right. If that was successful,
# do a backspace (effectively deleting char right of cursor)
cursor = self.cursor
self.do_cursor_movement('cursor_right')
if cursor != self.cursor:
self.do_backspace(mode='del')
elif internal_action == 'backspace':
self.do_backspace()
# handle action keys and text insertion
if internal_action is None:
self.insert_text(displayed_str)
elif internal_action in ('shift', 'shift_L', 'shift_R'):
if not self._selection:
@ -2274,22 +2289,8 @@ class TextInput(FocusBehavior, Widget):
self._update_selection()
else:
self.cancel_selection()
elif self._selection and internal_action in ('del', 'backspace'):
self.delete_selection()
elif internal_action == 'del':
# Move cursor one char to the right. If that was successful,
# do a backspace (effectively deleting char right of cursor)
cursor = self.cursor
self.do_cursor_movement('cursor_right')
if cursor != self.cursor:
self.do_backspace(mode='del')
elif internal_action == 'backspace':
self.do_backspace()
elif internal_action == 'enter':
if self.multiline and self._selection:
self.delete_selection()
self.insert_text(u'\n')
elif self.multiline:
if self.multiline:
self.insert_text(u'\n')
else:
self.dispatch('on_text_validate')
@ -2297,9 +2298,6 @@ class TextInput(FocusBehavior, Widget):
self.focus = False
elif internal_action == 'escape':
self.focus = False
if internal_action != 'escape':
# self._recalc_size()
pass
def _key_up(self, key, repeat=False):
displayed_str, internal_str, internal_action, scale = key