changeset 403:ec6f7581b453

browser_side: microblogs edition: <shift> + <enter> makes a new line, <esc> finishes the edition
author souliane <souliane@mailoo.org>
date Fri, 14 Mar 2014 21:00:13 +0100
parents d7e78cb78dfc
children c393e7dc9ae6
files browser_side/base_panels.py
diffstat 1 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/base_panels.py	Thu Mar 13 16:05:35 2014 +0100
+++ b/browser_side/base_panels.py	Fri Mar 14 21:00:13 2014 +0100
@@ -29,7 +29,7 @@
 from pyjamas.ui.StackPanel import StackPanel
 from pyjamas.ui.TextArea import TextArea
 from pyjamas.ui.Event import BUTTON_LEFT, BUTTON_MIDDLE, BUTTON_RIGHT
-from pyjamas.ui.KeyboardListener import KEY_ENTER, KEY_ESCAPE, KeyboardHandler
+from pyjamas.ui.KeyboardListener import KEY_ENTER, KEY_ESCAPE, KEY_SHIFT, KeyboardHandler
 from pyjamas.ui.FocusListener import FocusHandler
 from pyjamas.ui.ClickListener import ClickHandler
 from pyjamas import DOM
@@ -456,6 +456,7 @@
         self.__enhance_display = enhance_display
         self.__listen_focus = single_line if listen_focus is None else listen_focus
         self.__listen_click = listen_click
+        self.__shift_down = False
         if self.__listen_focus:
             FocusHandler.__init__(self)
         if self.__listen_click:
@@ -504,16 +505,20 @@
     def onKeyDown(self, sender, keycode, modifiers):
         for listener in self.edit_listeners:
             listener(self.textarea, keycode)
-
-    def onKeyPress(self, sender, keycode, modifiers):
         if not self.__single_line:
             return
-        # XXX: it seems that pyjamas never catches the escape key
+        if keycode == KEY_SHIFT or self.__shift_down:  # allow input a new line with <shift> + <enter>
+            self.__shift_down = True
+            return
         if keycode in (KEY_ENTER, KEY_ESCAPE):  # finish the edition
             self.textarea.setFocus(False)
             if not self.__listen_focus:
                 self.edit(False)
 
+    def onKeyUp(self, sender, keycode, modifiers):
+        if keycode == KEY_SHIFT:
+            self.__shift_down = False
+
     def onLostFocus(self, sender):
         """Finish the edition when focus is lost"""
         if self.__listen_focus:
@@ -577,4 +582,6 @@
         SimpleTextEditor.edit(self, edit, abort, sync)
 
     def setFocus(self, focus):
+        if focus:
+            self.textarea.setCursorPos(len(self.textarea.getText()))
         self.textarea.setFocus(focus)