Files
sunnypilot/system/ui/widgets/layouts.py
github-actions[bot] f398269b1a sunnypilot v2026.04.06-4377
version: sunnypilot v2026.001.000 (dev)
date: 2026-04-06T01:24:35
master commit: 8fec761f89
2026-04-06 01:24:35 +00:00

60 lines
1.5 KiB
Python

from enum import IntFlag
from openpilot.system.ui.widgets import Widget
class Alignment(IntFlag):
LEFT = 0
# TODO: implement
# H_CENTER = 2
# RIGHT = 4
TOP = 8
V_CENTER = 16
BOTTOM = 32
class HBoxLayout(Widget):
"""
A Widget that lays out child Widgets horizontally.
"""
def __init__(self, widgets: list[Widget] | None = None, spacing: int = 0,
alignment: Alignment = Alignment.LEFT | Alignment.V_CENTER):
super().__init__()
self._spacing = spacing
self._alignment = alignment
if widgets is not None:
for widget in widgets:
self.add_widget(widget)
@property
def widgets(self) -> list[Widget]:
return self._children
def add_widget(self, widget: Widget) -> None:
self._child(widget)
def _render(self, _):
visible_widgets = [w for w in self._children if w.is_visible]
cur_offset_x = 0
for idx, widget in enumerate(visible_widgets):
spacing = self._spacing if (idx > 0) else 0
x = self._rect.x + cur_offset_x + spacing
cur_offset_x += widget.rect.width + spacing
if self._alignment & Alignment.TOP:
y = self._rect.y
elif self._alignment & Alignment.BOTTOM:
y = self._rect.y + self._rect.height - widget.rect.height
else: # center
y = self._rect.y + (self._rect.height - widget.rect.height) / 2
# Update widget position and render
widget.set_position(x, y)
widget.set_parent_rect(self._rect)
widget.render()