diff cagou/core/cagou_main.py @ 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
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