Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/techui_builder/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ def _create_widgets(
def layout_widgets(self, widgets: list[EmbeddedDisplay | ActionButton]):
group_spacing: int = 30
max_group_height: int = 800
spacing_x: int = 20
spacing_y: int = 30
spacing_x: int = 5
spacing_y: int = 5
# Group tiles by size
groups: dict[tuple[int, int], list[EmbeddedDisplay | ActionButton]] = (
defaultdict(list)
Expand All @@ -303,26 +303,31 @@ def layout_widgets(self, widgets: list[EmbeddedDisplay | ActionButton]):
groups[key].append(widget)

# Sort groups by width (optional)
sorted_widgets: list[EmbeddedDisplay | ActionButton] = []
sorted_groups = sorted(groups.items(), key=lambda g: g[0][0], reverse=True)
layedout_widgets: list[EmbeddedDisplay | ActionButton] = []
current_x: int = 0
current_y: int = 0
column_width: int = 0
column_levels: list[list[EmbeddedDisplay | ActionButton]] = []

for (h, w), group in sorted_groups:
for (h, w), group in groups.items():
for widget in group:
placed = False
for level in column_levels:
level_y, _ = self._get_widget_position(level[0])
_, widget_width = self._get_widget_dimensions(widget)
level_height = max(self._get_widget_dimensions(t)[0] for t in level)
level_width = (
sum(
(self._get_widget_dimensions(t))[1] + spacing_x
for t in level
)
- spacing_x
) # Find the width of the row

_, widget_width = self._get_widget_dimensions(widget)

if level_height > h or level_height < h:
continue

if (
level_y + h <= max_group_height
and level_width + widget_width <= column_width
Expand All @@ -348,9 +353,9 @@ def layout_widgets(self, widgets: list[EmbeddedDisplay | ActionButton]):
column_levels.append([widget])
current_y += h + spacing_y
column_width = max(column_width, w)
sorted_widgets.append(widget)
layedout_widgets.append(widget)

return sorted_widgets
return layedout_widgets

def build_widgets(self, screen_name: str, screen_entities: list[Entity]):
# Empty widget buffer
Expand Down
16 changes: 8 additions & 8 deletions tests/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,14 @@ def test_generator_create_widgets_related(
"index, x, y",
[
(0, 0, 0),
(1, 0, 150),
(2, 0, 300),
(3, 0, 450),
(4, 0, 600),
(5, 235, 0),
(6, 235, 150),
(7, 355, 150),
(8, 235, 220),
(1, 0, 125),
(2, 0, 250),
(3, 0, 375),
(4, 0, 500),
(5, 0, 625),
(6, 0, 750),
(7, 105, 750),
(8, 235, 0),
],
)
def test_generator_layout_widgets(generator, index, x, y):
Expand Down