laatikoita/layout.py

87 lines
2.4 KiB
Python

#TODO: Proper documentation and error handling
def heighten(box, above, below, fill=None):
if fill is None:
return box.heighten(above, below)
else:
return box.heighten(above, below, fill)
def widen(box, before, after, fill=None):
if fill is None:
return box.widen(before, after)
else:
return box.widen(before, after, fill)
def align_top(box, height, fill=None):
padding = height - getattr(box, 'box', box).height
return heighten(box, 0, padding, fill)
def align_bottom(box, height, fill=None):
padding = height - getattr(box, 'box', box).height
return heighten(box, padding, 0, fill)
def center_vertically(box, height, fill=None):
padding = height - getattr(box, 'box', box).height
above = padding // 2
below = padding - above
return heighten(box, above, below, fill)
def align_beginning(box, width, fill=None):
padding = width - getattr(box, 'box', box).width
return widen(box, 0, padding, fill)
def align_end(box, width, fill=None):
padding = width - getattr(box, 'box', box).width
return widen(box, padding, 0, fill)
def center_horizontally(box, width, fill=None):
padding = width - getattr(box, 'box', box).width
before = padding // 2
after = padding - before
return widen(box, before, after, fill)
def pad_bottom(box, height):
padding = height - box.box.height
return box.vstretch(0, padding)
def pad_top(box, height):
padding = height - box.box.height
return box.vstretch(padding, 0)
def pad_vertically(box, height):
padding = height - box.box.height
above = padding // 2
below = padding - above
return box.vstretch(above, below)
def pad_after(box, width):
padding = width - box.box.width
return box.hstretch(0, padding)
def pad_before(box, width):
padding = width - box.box.width
return box.hstretch(padding, 0)
def pad_horizontally(box, width):
padding = width - box.box.width
before = padding // 2
after = padding - before
return box.hstretch(before, after)
def lay(boxes, heighten=align_top):
boxes=list(boxes)
max_height = max(getattr(i, 'box', i).height for i in boxes)
x, *rest = boxes
x = heighten(box=x, height=max_height)
for box in rest:
x = x.beside(heighten(box=box, height=max_height))
return x
def stack(boxes, widen=align_beginning):
boxes=list(boxes)
max_width = max(getattr(i, 'box', i).width for i in boxes)
x, *rest = boxes
x = widen(box=x, width=max_width)
for box in rest:
x = x.above(widen(box=box, width=max_width))
return x