comparison cagou/core/widgets_handler.py @ 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 9835cafbd909
comparison
equal deleted inserted replaced
170:f4522b7c3318 171:27b4ceb977c7
386 class WidgetsHandlerLayout(Layout): 386 class WidgetsHandlerLayout(Layout):
387 count = 0 387 count = 0
388 388
389 def __init__(self, **kwargs): 389 def __init__(self, **kwargs):
390 super(WidgetsHandlerLayout, self).__init__(**kwargs) 390 super(WidgetsHandlerLayout, self).__init__(**kwargs)
391 self._layout_size = None # size used for the last layout
391 fbind = self.fbind 392 fbind = self.fbind
392 update = self._trigger_layout 393 update = self._trigger_layout
393 fbind('children', update) 394 fbind('children', update)
394 fbind('parent', update) 395 fbind('parent', update)
395 fbind('size', update) 396 fbind('size', self.adjust_prop)
396 fbind('pos', update) 397 fbind('pos', update)
397 398
398 @property 399 @property
399 def default_widget(self): 400 def default_widget(self):
400 return G.host.default_wid['factory'](G.host.default_wid, None, None) 401 return G.host.default_wid['factory'](G.host.default_wid, None, None)
401 402
403 def adjust_prop(self, handler, new_size):
404 """Adjust children proportion
405
406 useful when this widget is resized (e.g. when going to fullscreen)
407 """
408 if len(self.children) > 1:
409 old_width, old_height = self._layout_size
410 if not old_width or not old_height:
411 # we don't want division by zero
412 return self._trigger_layout(handler, new_size)
413 width_factor = float(self.width) / old_width
414 height_factor = float(self.height) / old_height
415 for child in self.children:
416 child.width *= width_factor
417 child.height *= height_factor
418 child.x *= width_factor
419 child.y *= height_factor
420 self._trigger_layout(handler, new_size)
421
402 def do_layout(self, *args): 422 def do_layout(self, *args):
423 self._layout_size = self.size[:]
403 for child in self.children: 424 for child in self.children:
404 # XXX: left must be calculated before right and bottom before top 425 # XXX: left must be calculated before right and bottom before top
405 # because they are the pos, and are used to caculate size (right and top) 426 # because they are the pos, and are used to caculate size (right and top)
406 # left 427 # left
407 left = child._left_wid 428 left = child._left_wid