1
0
Fork 0
mirror of https://git.lynn.is/Gwen/python-layout.git synced 2024-01-13 01:31:55 +01:00
python-layout/pillow_layout/flex.py
2023-02-07 23:56:17 +01:00

64 lines
2.6 KiB
Python

from . import Layout, FlexDirection, FlexWrap, FlexJustify, FlexAlignItems, FlexAlignContent
from .internal.flexlayouter import FlexLayouter
from .internal.helpers import min_with_none, max_with_none
class Flex(Layout):
def __init__(
self,
contents=None,
direction=FlexDirection.ROW,
wrap=FlexWrap.NO_WRAP,
justify=FlexJustify.START,
align_items=FlexAlignItems.START,
align_content=FlexAlignContent.START,
gap=0,
**kwargs
):
super().__init__(**kwargs)
if contents is None:
contents = []
self.direction = direction
self.wrap = wrap
self.justify = justify
self.align_items = align_items
self.align_content = align_content
self.gap = gap
self.contents = contents
self.flex_layouter = FlexLayouter(contents, gap, direction, wrap, justify, align_content, align_items)
def children(self):
return self.contents
def get_min_inner_width(self, max_height=None):
max_width = min_with_none(self.max_width, 0)
width, _ = self.flex_layouter.get_dimensions(self.min_width, self.min_height, max_width,
min_with_none(self.max_height, max_height))
return width
def get_min_inner_height(self, max_width=None):
max_height = min_with_none(self.min_height, 0)
_, height = self.flex_layouter.get_dimensions(self.min_width, self.min_height,
min_with_none(self.max_width, max_width), max_height)
return height
def get_ideal_inner_dimensions(self, min_width=None, min_height=None, available_width=None, available_height=None):
min_width = max_with_none(min_width, self.min_width)
min_height = max_with_none(min_height, self.min_height)
available_width = min_with_none(available_width, self.max_width, self.width)
available_height = min_with_none(available_height, self.max_height, self.height)
return self.flex_layouter.get_dimensions(min_width, min_height, available_width, available_height)
def render_content(self, rect):
image = self.make_canvas()
x1, y1, x2, y2 = rect
width = x2 - x1 + 1
height = y2 - y1 + 1
contents = self.flex_layouter.arrange(self.min_width, self.min_height, width, height)
for (cx1, cy1, cx2, cy2), content in contents:
content_image = content.render((cx1 + x1, cy1 + y1, cx2 + x1, cy2 + y1))
image.alpha_composite(content_image)
return image