changeset 171:27b4ceb977c7

widgets handler: keep proportion on resize
author Goffi <goffi@goffi.org>
date Sun, 29 Apr 2018 13:10:14 +0200
parents f4522b7c3318
children 7103655647aa
files cagou/core/widgets_handler.py
diffstat 1 files changed, 22 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/widgets_handler.py	Sun Apr 29 12:23:52 2018 +0200
+++ b/cagou/core/widgets_handler.py	Sun Apr 29 13:10:14 2018 +0200
@@ -388,18 +388,39 @@
 
     def __init__(self, **kwargs):
         super(WidgetsHandlerLayout, self).__init__(**kwargs)
+        self._layout_size = None  # size used for the last layout
         fbind = self.fbind
         update = self._trigger_layout
         fbind('children', update)
         fbind('parent', update)
-        fbind('size', update)
+        fbind('size', self.adjust_prop)
         fbind('pos', update)
 
     @property
     def default_widget(self):
         return G.host.default_wid['factory'](G.host.default_wid, None, None)
 
+    def adjust_prop(self, handler, new_size):
+        """Adjust children proportion
+
+        useful when this widget is resized (e.g. when going to fullscreen)
+        """
+        if len(self.children) > 1:
+            old_width, old_height = self._layout_size
+            if not old_width or not old_height:
+                # we don't want division by zero
+                return self._trigger_layout(handler, new_size)
+            width_factor = float(self.width) / old_width
+            height_factor = float(self.height) / old_height
+            for child in self.children:
+                child.width *= width_factor
+                child.height *= height_factor
+                child.x *= width_factor
+                child.y *= height_factor
+        self._trigger_layout(handler, new_size)
+
     def do_layout(self, *args):
+        self._layout_size = self.size[:]
         for child in self.children:
             # XXX: left must be calculated before right and bottom before top
             #      because they are the pos, and are used to caculate size (right and top)