changeset 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 1f88e7781fd0
children f5302d57fb09
files cagou/core/cagou_main.py cagou/plugins/plugin_wid_chat.kv cagou/plugins/plugin_wid_chat.py
diffstat 3 files changed, 49 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Wed Mar 20 09:29:44 2019 +0100
+++ b/cagou/core/cagou_main.py	Wed Mar 20 09:29:44 2019 +0100
@@ -491,7 +491,7 @@
         self.postInit()
 
     def postInit(self, dummy=None):
-        # FIXME: resize seem to bug on android, so we use below_target for now
+        # FIXME: resize doesn't work with SDL2 on android, so we use below_target for now
         self.app.root_window.softinput_mode = "below_target"
         profile_manager = self.app._profile_manager
         del self.app._profile_manager
--- a/cagou/plugins/plugin_wid_chat.kv	Wed Mar 20 09:29:44 2019 +0100
+++ b/cagou/plugins/plugin_wid_chat.kv	Wed Mar 20 09:29:44 2019 +0100
@@ -113,13 +113,15 @@
             id: messages_widget
     MessageInputBox:
         size_hint: 1, None
-        height: dp(40)
+        height: self.minimum_height
         spacing: dp(10)
         padding: [app.MARGIN_LEFT, 0, app.MARGIN_RIGHT, dp(10)]
         message_input: message_input
         MessageInputWidget:
             id: message_input
-            size_hint: 1, 1
+            size_hint: 1, None
+            height: min(self.minimum_height, dp(250))
+            multiline: True
             hint_text: _(u"Enter your message here")
             on_text_validate: root.onSend(args[0])
         SymbolButton
@@ -131,6 +133,16 @@
             font_size: dp(25)
             on_release: TransferMenu(callback=root.onTransferOK).show(self)
 
+
+<SendButton>:
+    # SendButton is only shown on touch devices
+    symbol: "forward"
+    size_hint: None, 1
+    width: dp(30)
+    font_size: dp(25)
+    on_release: self.message_input_box.send_text()
+
+
 <EncryptionMainButton>:
     size_hint: None, 1
     width: dp(30)
--- 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):