diff cagou/plugins/plugin_wid_chat.py @ 276:a0835f0212d8

chat: multi-lines input: - text input is now multi-lines and grows with the text (up to a max) - on touch devices (i.e. on Android), an icon is added to the right to send the message, allowing to use virtual enter key for multi-lines messages
author Goffi <goffi@goffi.org>
date Wed, 20 Mar 2019 09:29:44 +0100
parents 805c4103dac5
children f5302d57fb09
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_chat.py	Wed Mar 20 09:29:44 2019 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Wed Mar 20 09:29:44 2019 +0100
@@ -18,14 +18,17 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+from functools import partial
+import mimetypes
+import sys
 from sat.core import log as logging
-log = logging.getLogger(__name__)
 from sat.core.i18n import _
 from cagou.core.constants import Const as C
 from kivy.uix.boxlayout import BoxLayout
 from kivy.uix.gridlayout import GridLayout
 from kivy.uix.textinput import TextInput
 from kivy.metrics import sp, dp
+from kivy.clock import Clock
 from kivy import properties
 from sat_frontends.quick_frontend import quick_widgets
 from sat_frontends.quick_frontend import quick_chat
@@ -37,9 +40,8 @@
 from kivy.uix.dropdown import DropDown
 from kivy.core.window import Window
 from cagou import G
-from functools import partial
-import mimetypes
 
+log = logging.getLogger(__name__)
 
 PLUGIN_INFO = {
     "name": _(u"chat"),
@@ -123,18 +125,41 @@
             self.delivery.text =  u'\u2714' if status == 'delivered' else u''
 
 
+class SendButton(SymbolButton):
+    message_input_box = properties.ObjectProperty()
+
+
 class MessageInputBox(BoxLayout):
-    pass
+    message_input = properties.ObjectProperty()
+
+    def __init__(self, *args, **kwargs):
+        super(MessageInputBox, self).__init__(*args, **kwargs)
+        Clock.schedule_once(self.post_init, 0)
+
+    def post_init(self, *args):
+        if sys.platform == 'android':
+            self.add_widget(SendButton(message_input_box=self), 0)
+
+    def send_text(self):
+        self.message_input.send_text()
 
 
 class MessageInputWidget(TextInput):
 
-    def _key_down(self, key, repeat=False):
-        displayed_str, internal_str, internal_action, scale = key
-        if internal_action == 'enter':
-            self.dispatch('on_text_validate')
+    def keyboard_on_key_down(self, window, keycode, text, modifiers):
+        # We don't send text when shift is pressed to be able to add line feeds
+        # (i.e. multi-lines messages). We don't send on Android either as the
+        # send button appears on this platform.
+        if (keycode[-1] == "enter"
+            and "shift" not in modifiers
+            and sys.platform != 'android'):
+            self.send_text()
         else:
-            super(MessageInputWidget, self)._key_down(key, repeat)
+            return super(MessageInputWidget, self).keyboard_on_key_down(
+                window, keycode, text, modifiers)
+
+    def send_text(self):
+        self.dispatch('on_text_validate')
 
 
 class MessagesWidget(GridLayout):