Modified layout manager to be a python context manager.

This commit is contained in:
Russell Keith-Magee 2014-04-25 09:32:23 +08:00
parent 2ff75c0958
commit fc08474d3d
3 changed files with 55 additions and 49 deletions

View file

@ -81,7 +81,18 @@ class LayoutManager(SimplexSolver):
self.children[widget] = constraints
def enforce(self, width, height):
def layout(self, width, height):
self.bounding_box.width.value = width
self.bounding_box.height.value = height
return self
def __enter__(self):
# Removing a constraint resets the value. Save the current value
# of the width/height constraint temporarily, and restore after
# removing the constraints.
width = self.bounding_box.width.value
height = self.bounding_box.height.value
self.remove_constraint(self.width_constraint)
self.remove_constraint(self.height_constraint)
@ -94,7 +105,7 @@ class LayoutManager(SimplexSolver):
self.add_constraint(self.width_constraint)
self.add_constraint(self.height_constraint)
def relax(self):
def __exit__(self, type, value, traceback):
self.remove_constraint(self.width_constraint)
self.remove_constraint(self.height_constraint)

View file

@ -28,40 +28,37 @@ class TContainer(Gtk.Fixed):
self.set_allocation(allocation)
# Temporarily enforce a size requirement based on the allocation
self.layout_manager.enforce(allocation.width, allocation.height)
with self.layout_manager.layout(allocation.width, allocation.height):
for widget in self.layout_manager.children:
print widget, widget._bounding_box
if not widget._impl.get_visible():
print "CHILD NOT VISIBLE"
else:
min_width, preferred_width = widget._width_hint
min_height, preferred_height = widget._height_hint
x_pos = widget._bounding_box.x.value
if widget._expand_horizontal:
width = widget._bounding_box.width.value
for widget in self.layout_manager.children:
print widget, widget._bounding_box
if not widget._impl.get_visible():
print "CHILD NOT VISIBLE"
else:
x_pos = x_pos + ((widget._bounding_box.width.value - preferred_width) / 2.0)
width = preferred_width
min_width, preferred_width = widget._width_hint
min_height, preferred_height = widget._height_hint
y_pos = widget._bounding_box.y.value
if widget._expand_vertical:
height = widget._bounding_box.height.value
else:
y_pos = y_pos + ((widget._bounding_box.height.value - preferred_height) / 2.0)
height = preferred_height
x_pos = widget._bounding_box.x.value
if widget._expand_horizontal:
width = widget._bounding_box.width.value
else:
x_pos = x_pos + ((widget._bounding_box.width.value - preferred_width) / 2.0)
width = preferred_width
child_allocation = cairo.RectangleInt()
child_allocation.x = x_pos
child_allocation.y = y_pos
child_allocation.width = width
child_allocation.height = height
y_pos = widget._bounding_box.y.value
if widget._expand_vertical:
height = widget._bounding_box.height.value
else:
y_pos = y_pos + ((widget._bounding_box.height.value - preferred_height) / 2.0)
height = preferred_height
widget._impl.size_allocate(child_allocation)
child_allocation = cairo.RectangleInt()
child_allocation.x = x_pos
child_allocation.y = y_pos
child_allocation.width = width
child_allocation.height = height
# Restore the unbounded allocation
self.layout_manager.relax()
widget._impl.size_allocate(child_allocation)
class Container(Widget):

View file

@ -79,29 +79,27 @@ class Container(Widget):
widget._create(window, x_pos, y_pos, width, height)
def _resize(self, x, y, width, height):
self._layout_manager.enforce(width, height)
for widget in self._layout_manager.children:
min_width, preferred_width = widget._width_hint
min_height, preferred_height = widget._height_hint
with self._layout_manager.layout(width, height):
for widget in self._layout_manager.children:
min_width, preferred_width = widget._width_hint
min_height, preferred_height = widget._height_hint
x_pos = widget._bounding_box.x.value
if widget._expand_horizontal:
width = widget._bounding_box.width.value
else:
x_pos = x_pos + ((widget._bounding_box.width.value - preferred_width) / 2.0)
width = preferred_width
x_pos = widget._bounding_box.x.value
if widget._expand_horizontal:
width = widget._bounding_box.width.value
else:
x_pos = x_pos + ((widget._bounding_box.width.value - preferred_width) / 2.0)
width = preferred_width
y_pos = widget._bounding_box.y.value
if widget._expand_vertical:
height = widget._bounding_box.height.value
else:
y_pos = y_pos + ((widget._bounding_box.height.value - preferred_height) / 2.0)
height = preferred_height
y_pos = widget._bounding_box.y.value
if widget._expand_vertical:
height = widget._bounding_box.height.value
else:
y_pos = y_pos + ((widget._bounding_box.height.value - preferred_height) / 2.0)
height = preferred_height
widget._resize(x_pos, y_pos, width, height)
self._layout_manager.relax()
widget._resize(x_pos, y_pos, width, height)
@property
def _width_hint(self):