changeset 357:4d3a0c4f2430

core: better back key (ESC) management: - back key (which is mapped to esc keycode by SDL2 backend) is now handler with a platform specific method when on root widget (i.e. a default widget is selected, or nothing is selected). Default behaviour is to do nothing, while on Android the app is put to background - CagouWidget now has a default key_input method which go back to default widget.
author Goffi <goffi@goffi.org>
date Sat, 18 Jan 2020 23:12:52 +0100
parents 307c2501d8b2
children 8efed1d47d9f
files cagou/core/cagou_main.py cagou/core/cagou_widget.py cagou/core/platform_/android.py cagou/core/platform_/base.py cagou/core/share_widget.py cagou/plugins/plugin_wid_chat.py cagou/plugins/plugin_wid_widget_selector.py
diffstat 7 files changed, 48 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Fri Jan 17 20:35:57 2020 +0100
+++ b/cagou/core/cagou_main.py	Sat Jan 18 23:12:52 2020 +0100
@@ -351,6 +351,11 @@
             return True
 
         if key == 27:
+            if ((self.host.selected_widget is None
+                 or self.host.selected_widget.__class__ == self.host.default_class)):
+                # we are on root widget, or nothing is selected
+                return local_platform.on_key_back_root()
+
             # we disable [esc] handling, because default action is to quit app
             return True
         elif key == 292:
@@ -444,6 +449,12 @@
             for w in w_list:
                 yield w
 
+    @property
+    def default_class(self):
+        if self.default_wid is None:
+            return None
+        return self.default_wid['main']
+
     @QuickApp.sync.setter
     def sync(self, state):
         QuickApp.sync.fset(self, state)
@@ -753,8 +764,9 @@
             raise exceptions.InternalError("no CagouWidget found when "
                                            "trying to switch widget")
 
+        # selected_widget can be modified in changeWidget, so we need to set it before
+        self.selected_widget = new
         to_change.whwrapper.changeWidget(new)
-        self.selected_widget = new
 
     def _addVisibleWidget(self, widget):
         """declare a widget visible
@@ -854,7 +866,7 @@
         if self.selected_widget is not None:
             return self.selected_widget
         # no widget is selected we check if we have any default widget
-        default_cls = self.default_wid['main']
+        default_cls = self.default_class
         for w in self.visible_widgets:
             if isinstance(w, default_cls):
                 return w
--- a/cagou/core/cagou_widget.py	Fri Jan 17 20:35:57 2020 +0100
+++ b/cagou/core/cagou_widget.py	Sat Jan 18 23:12:52 2020 +0100
@@ -123,6 +123,12 @@
         new_widget = factory(plugin_info, None, iter(G.host.profiles))
         G.host.switchWidget(self, new_widget)
 
+    def key_input(self, window, key, scancode, codepoint, modifier):
+        if key == 27:
+            # we go back to root screen
+            G.host.switchWidget(self)
+            return True
+
     def onHeaderInput(self):
         log.info("header input text entered")
 
--- a/cagou/core/platform_/android.py	Fri Jan 17 20:35:57 2020 +0100
+++ b/cagou/core/platform_/android.py	Sat Jan 18 23:12:52 2020 +0100
@@ -32,7 +32,8 @@
 log = logging.getLogger(__name__)
 
 service = autoclass('org.salutatoi.cagou.ServiceBackend')
-mActivity = autoclass('org.kivy.android.PythonActivity').mActivity
+PythonActivity = autoclass('org.kivy.android.PythonActivity')
+mActivity = PythonActivity.mActivity
 Intent = autoclass('android.content.Intent')
 AndroidString = autoclass('java.lang.String')
 Uri = autoclass('android.net.Uri')
@@ -103,6 +104,15 @@
         self._frontend_status_socket.sendall(STATE_STOPPED)
         self._frontend_status_socket.close()
 
+    def on_key_back_root(self):
+        PythonActivity.moveTaskToBack(True)
+        return True
+
+    def on_key_back_share(self, share_widget):
+        share_widget.close()
+        PythonActivity.moveTaskToBack(True)
+        return True
+
     def getPathFromUri(self, uri):
         cursor = mActivity.getContentResolver().query(uri, None, None, None, None)
         if cursor is None:
--- a/cagou/core/platform_/base.py	Fri Jan 17 20:35:57 2020 +0100
+++ b/cagou/core/platform_/base.py	Sat Jan 18 23:12:52 2020 +0100
@@ -35,8 +35,8 @@
     def init_platform(self):
         # we don't want multi-touch emulation with mouse
 
-        # this option doesn't make sense on Android and cause troubles, so we only activate
-        # it for other platforms (cf. https://github.com/kivy/kivy/issues/6229)
+        # this option doesn't make sense on Android and cause troubles, so we only
+        # activate it for other platforms (cf. https://github.com/kivy/kivy/issues/6229)
         KivyConfig.set('input', 'mouse', 'mouse,disable_multitouch')
 
     def on_app_build(self, wid):
@@ -57,6 +57,15 @@
     def on_stop(self):
         pass
 
+    def on_key_back_root(self):
+        """Back key is called while being on root widget"""
+        return True
+
+    def on_key_back_share(self, share_widget):
+        """Back key is called while being on share widget"""
+        share_widget.close()
+        return True
+
     def open_url(self, url, wid=None):
         """Open an URL in the way appropriate for the platform
 
--- a/cagou/core/share_widget.py	Fri Jan 17 20:35:57 2020 +0100
+++ b/cagou/core/share_widget.py	Sat Jan 18 23:12:52 2020 +0100
@@ -157,5 +157,4 @@
 
     def key_input(self, window, key, scancode, codepoint, modifier):
         if key == 27:
-            self.close()
-            return True
+            return G.local_platform.on_key_back_share(self)
--- a/cagou/plugins/plugin_wid_chat.py	Fri Jan 17 20:35:57 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Sat Jan 18 23:12:52 2020 +0100
@@ -886,16 +886,6 @@
             G.host.switchWidget(
                 self, factory(plugin_info, contact_jid, profiles=[self.profile]))
 
-    @property
-    def profiles(self):
-        return [self.profile]
-
-    def key_input(self, window, key, scancode, codepoint, modifier):
-        if key == 27:
-            # we go back to root screen
-            G.host.switchWidget(self)
-            return True
-
 
 PLUGIN_INFO["factory"] = Chat.factory
 quick_widgets.register(quick_chat.QuickChat, Chat)
--- a/cagou/plugins/plugin_wid_widget_selector.py	Fri Jan 17 20:35:57 2020 +0100
+++ b/cagou/plugins/plugin_wid_widget_selector.py	Sat Jan 18 23:12:52 2020 +0100
@@ -66,6 +66,11 @@
         for i in self.items:
             i.width = width
 
+    def key_input(self, window, key, scancode, codepoint, modifier):
+        # we pass to avoid default CagouWidget which is going back to default widget
+        # (which is this one)
+        pass
+
     @classmethod
     def factory(cls, plugin_info, target, profiles):
         return cls()