f6f8c4ef49e8 — Alain Leufroy 3 years ago
widget: allow users to maximize the focused pane

Just enlarge the focused pane.

Note: users can maximize the patch pane, and browse the tree with
      `alt+{up,down}`.
2 files changed, 50 insertions(+), 1 deletions(-)

M lairucrem/config.py
M lairucrem/widgets/mainwidget.py
M lairucrem/config.py +4 -0
@@ 362,6 362,7 @@ CMD_SCROLL_LEFT = 'scroll left'
 CMD_SCROLL_RIGHT = 'scroll right'
 CMD_SPLIT_HORIZONTALLY = 'split horizontally'
 CMD_SPLIT_VERTICALLY = 'split vertically'
+CMD_MAXIMALIZE = 'maximalize'
 CURSOR_UP_NEXT_PACKED = 'up next packed'
 CURSOR_DOWN_NEXT_PACKED = 'down next packed'
 CURSOR_PAGE_UP_NEXT_PACKED = 'page up next packed'

          
@@ 401,6 402,7 @@ def keybindings_default():
 
     command_map['-'] = CMD_SPLIT_HORIZONTALLY
     command_map['|'] = CMD_SPLIT_VERTICALLY
+    command_map['='] = CMD_MAXIMALIZE
 
     command_map['ctrl f'] = CMD_FILTER
     command_map['/'] = CMD_SEARCH

          
@@ 452,6 454,7 @@ def keybindings_vim():
 
     command_map['ctrl s'] = CMD_SPLIT_HORIZONTALLY
     command_map['ctrl v'] = CMD_SPLIT_VERTICALLY
+    command_map['ctrl t'] = CMD_MAXIMALIZE
 
     command_map['ctrl f'] = CMD_FILTER
     command_map['/'] = CMD_SEARCH

          
@@ 501,6 504,7 @@ def keybindings_emacs():
 
     command_map['meta 3'] = CMD_SPLIT_VERTICALLY
     command_map['meta 2'] = CMD_SPLIT_HORIZONTALLY
+    command_map['meta 1'] = CMD_MAXIMALIZE
 
     command_map['ctrl f'] = CMD_FILTER
     command_map['/'] = CMD_SEARCH

          
M lairucrem/widgets/mainwidget.py +46 -1
@@ 10,9 10,10 @@ from __future__ import absolute_import, 
 import urwid
 from urwid.canvas import CompositeCanvas
 from urwid.command_map import CURSOR_LEFT, CURSOR_RIGHT
-from urwid.widget import delegate_to_widget_mixin
+from urwid.widget import GIVEN, PACK, WEIGHT, delegate_to_widget_mixin
 
 from .. import config
+from ..utils import apply_mixin
 from .utils import ANY, get_original_widget
 
 

          
@@ 123,6 124,46 @@ class thehelp(urwid.Pile):
         ])
 
 
+class _maximizable_contenxt_base:
+
+    _maximized = False
+
+    @property
+    def _focused_options(self):
+        raise NotImplementedError
+
+    @property
+    def _unfocused_options(self):
+        raise NotImplementedError
+
+    def toggle_maximize(self):
+        self._maximized = not(self._maximized)
+        self._modified()
+
+    def __iter__(self):
+        if self._maximized:
+            for i, (widget, options) in enumerate(super().__iter__()):
+                if i == self._focus:
+                    yield (widget, self._focused_options)
+                else:
+                    yield (widget, self._unfocused_options)
+            return
+        for v in super().__iter__():
+            yield v
+
+
+class maximizable_columns_contents_mixin(_maximizable_contenxt_base):
+
+    _focused_options = (WEIGHT, 1, False)
+    _unfocused_options = (GIVEN, 0, False)
+
+
+class maximizable_pile_contents_mixin(_maximizable_contenxt_base):
+
+    _focused_options = (WEIGHT, 1)
+    _unfocused_options = (GIVEN, 0)
+
+
 class packer(delegate_to_widget_mixin('_original_widget')):
     """Pack the given widgets horizontally (Columns)or vertically (Pile)
     depending on the screen size.

          
@@ 153,8 194,10 @@ class packer(delegate_to_widget_mixin('_
         assert self._orientation
         if self._orientation == config.HORIZONTAL:
             self._original_widget = urwid.Columns(self._widgets)
+            apply_mixin(self._original_widget.contents, maximizable_columns_contents_mixin)
         elif self._orientation == config.VERTICAL:
             self._original_widget = urwid.Pile(self._widgets)
+            apply_mixin(self._original_widget.contents, maximizable_pile_contents_mixin)
         self._invalidate()
 
     def _guess_orientation(self, size):

          
@@ 167,6 210,8 @@ class packer(delegate_to_widget_mixin('_
         key = super(packer, self).keypress(size, key)
         widget = self._original_widget
         command = self._command_map[key]
+        if command == config.CMD_MAXIMALIZE:
+            self._original_widget.contents.toggle_maximize()
         if command == config.CMD_SPLIT_HORIZONTALLY:
             self._orientation = config.VERTICAL
             self._update_container()