fd54925d16ca — Alain Leufroy 2 years ago
main packer widget: preserve focus and maximalization while changing split orientation

Mostly, when the user reduces the window size, `gh` changes
automatically the split orientation. In this case, `gh` should
preserve the focused pane and maximalization.

To do so, We build the columns/pile widgets once and apply
focus/maximalization to both everytime.
1 files changed, 24 insertions(+), 10 deletions(-)

M lairucrem/widgets/mainwidget.py
M lairucrem/widgets/mainwidget.py +24 -10
@@ 176,7 176,10 @@ class packer(delegate_to_widget_mixin('_
         self._orientation = config.DEFAULT_MAIN_VIEW
         self._widgets = widgets
         self._original_widget = None
+        self._column_widget = None
+        self._pile_widget = None
         self._previous_size = None
+        self._initialize_containers()
         if self._orientation:
             self._previous_size = ANY
             self._update_container()

          
@@ 189,17 192,29 @@ class packer(delegate_to_widget_mixin('_
         self._previous_size = size
         return super(packer, self).render(size, focus)
 
+    def _initialize_containers(self):
+        self._column_widget = urwid.Columns(self._widgets)
+        apply_mixin(self._column_widget.contents, maximizable_columns_contents_mixin)
+        self._pile_widget = urwid.Pile(self._widgets)
+        apply_mixin(self._pile_widget.contents, maximizable_pile_contents_mixin)
+
     def _update_container(self):
         """update the main container depending on the given size"""
         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)
+            self._original_widget = self._column_widget
         elif self._orientation == config.VERTICAL:
-            self._original_widget = urwid.Pile(self._widgets)
-            apply_mixin(self._original_widget.contents, maximizable_pile_contents_mixin)
+            self._original_widget = self._pile_widget
         self._invalidate()
 
+    def _set_focus_position(self, num):
+        self._column_widget.focus_position = num
+        self._pile_widget.focus_position = num
+
+    def _toggle_miximize(self):
+        self._column_widget.contents.toggle_maximize()
+        self._pile_widget.contents.toggle_maximize()
+
     def _guess_orientation(self, size):
         cols = size[0]
         self._orientation = config.HORIZONTAL if cols > 160 else config.VERTICAL

          
@@ 208,10 223,9 @@ class packer(delegate_to_widget_mixin('_
         """Process pressed key"""
         key = self._keypress_next_packed(size, key)
         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()
+            self._toggle_miximize()
         if command == config.CMD_SPLIT_HORIZONTALLY:
             self._orientation = config.VERTICAL
             self._update_container()

          
@@ 221,14 235,14 @@ class packer(delegate_to_widget_mixin('_
             self._update_container()
             return
         if command == CURSOR_LEFT:
-            widget.focus_position = max(0, widget.focus_position - 1)
+            self._set_focus_position(max(0, self._original_widget.focus_position - 1))
             return
         if command == CURSOR_RIGHT:
-            widget.focus_position = min(len(self._widgets) - 1,
-                                        widget.focus_position + 1)
+            self._set_focus_position(
+                min(len(self._widgets) - 1, self._original_widget.focus_position + 1))
             return
         if command in (config.CMD_NEXT_SELECTABLE, config.CMD_PREV_SELECTABLE):
-            widget.focus_position = self._get_next_widget_position()
+            self._set_focus_position(self._get_next_widget_position())
             return
         return key