changeset 1186:9cc858acae91

primitivus: added a FOCUS_EXTRA key (default: "ctrl f") which can be used to focus a main part: - ctrl f + 1 (or m) to focus menu - ctrl f + 2 (or b) to focus body - ctrl f + 3 (or n) to focus notifications bar - ctrl f + 4 (or e) to focus edit bar
author Goffi <goffi@goffi.org>
date Mon, 08 Sep 2014 19:19:32 +0200
parents 6184779544c7
children eb1144a22e20
files frontends/src/primitivus/keys.py frontends/src/primitivus/primitivus
diffstat 2 files changed, 43 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/keys.py	Mon Sep 08 18:32:36 2014 +0200
+++ b/frontends/src/primitivus/keys.py	Mon Sep 08 19:19:32 2014 +0200
@@ -55,6 +55,9 @@
 
         #card game
         ("card_game", "CARD_SELECT"): ' ',
+
+        #focus
+        ("focus", "FOCUS_EXTRA"): "ctrl f",
         })
 
 
--- a/frontends/src/primitivus/primitivus	Mon Sep 08 18:32:36 2014 +0200
+++ b/frontends/src/primitivus/primitivus	Mon Sep 08 19:19:32 2014 +0200
@@ -189,6 +189,7 @@
         self._notif_bar = notif_bar
         self._edit_bar = edit_bar
         self._hidden = {'notif_bar'}
+        self._focus_extra = False
         super(PrimitivusTopWidget, self).__init__([('pack', self._menu), self._body, ('pack', self._edit_bar)])
         for position in self.positions:
             setattr(self,
@@ -198,6 +199,44 @@
                    )
         self.focus_position = len(self.contents)-1
 
+    def getVisiblePositions(self, keep=None):
+        """Return positions that are not hidden in the right order
+
+        @param keep: if not None, this position will be keep in the right order, even if it's hidden
+                    (can be useful to find its index)
+        @return (list): list of visible positions
+        """
+        return [pos for pos in self.positions if (keep and pos == keep) or pos not in self._hidden]
+
+    def keypress(self, size, key):
+        """Manage FOCUS keys that focus directly a main part (one of self.positions)
+
+        To avoid key conflicts, a combinaison must be made with FOCUS_EXTRA then an other key
+        """
+        if key == a_key['FOCUS_EXTRA']:
+            self._focus_extra = True
+            return
+        if self._focus_extra:
+            self._focus_extra = False
+            if key in ('m', '1'):
+                focus = 'menu'
+            elif key in ('b', '2'):
+                focus = 'body'
+            elif key in ('n', '3'):
+                focus = 'notif_bar'
+            elif key in ('e', '4'):
+                focus = 'edit_bar'
+            else:
+                return super(PrimitivusTopWidget, self).keypress(size, key)
+
+            if focus in self._hidden:
+                return
+
+            self.focus_position = self.getVisiblePositions().index(focus)
+            return
+
+        return super(PrimitivusTopWidget, self).keypress(size, key)
+
     def widgetGet(self,  position):
         if not position in self.positions:
             raise ValueError("Unknown position {}".format(position))
@@ -212,9 +251,8 @@
         if not position in self.can_hide:
             raise ValueError("Can't switch position {}".format(position))
         hide = not position in self._hidden
-        visible_positions = [pos for pos in self.positions if pos == position or pos not in self._hidden] # we need to keep position to find its index
         widget = self.widgetGet(position)
-        idx = visible_positions.index(position)
+        idx = self.getVisiblePositions(position).index(position)
         if hide:
             del self.contents[idx]
             self._hidden.add(position)