mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-02-19 00:43:54 +08:00
ui: update layout rects on change (#35545)
* update_layout_rects * check prev * about it * need this since touch can change :( * looks nicer * Revert "looks nicer" This reverts commit 8f36c92675db66695f22f93a01682426db9c05e8.
This commit is contained in:
@@ -67,8 +67,6 @@ class HomeLayout(Widget):
|
||||
self.current_state = state
|
||||
|
||||
def _render(self, rect: rl.Rectangle):
|
||||
self._update_layout_rects(rect)
|
||||
|
||||
current_time = time.time()
|
||||
if current_time - self.last_refresh >= REFRESH_INTERVAL:
|
||||
self._refresh()
|
||||
@@ -85,16 +83,16 @@ class HomeLayout(Widget):
|
||||
elif self.current_state == HomeLayoutState.ALERTS:
|
||||
self._render_alerts_view()
|
||||
|
||||
def _update_layout_rects(self, rect: rl.Rectangle):
|
||||
def _update_layout_rects(self):
|
||||
self.header_rect = rl.Rectangle(
|
||||
rect.x + CONTENT_MARGIN, rect.y + CONTENT_MARGIN, rect.width - 2 * CONTENT_MARGIN, HEADER_HEIGHT
|
||||
self._rect.x + CONTENT_MARGIN, self._rect.y + CONTENT_MARGIN, self._rect.width - 2 * CONTENT_MARGIN, HEADER_HEIGHT
|
||||
)
|
||||
|
||||
content_y = rect.y + CONTENT_MARGIN + HEADER_HEIGHT + SPACING
|
||||
content_height = rect.height - CONTENT_MARGIN - HEADER_HEIGHT - SPACING - CONTENT_MARGIN
|
||||
content_y = self._rect.y + CONTENT_MARGIN + HEADER_HEIGHT + SPACING
|
||||
content_height = self._rect.height - CONTENT_MARGIN - HEADER_HEIGHT - SPACING - CONTENT_MARGIN
|
||||
|
||||
self.content_rect = rl.Rectangle(
|
||||
rect.x + CONTENT_MARGIN, content_y, rect.width - 2 * CONTENT_MARGIN, content_height
|
||||
self._rect.x + CONTENT_MARGIN, content_y, self._rect.width - 2 * CONTENT_MARGIN, content_height
|
||||
)
|
||||
|
||||
left_width = self.content_rect.width - RIGHT_COLUMN_WIDTH - SPACING
|
||||
|
||||
@@ -30,8 +30,7 @@ class MainLayout(Widget):
|
||||
# Set callbacks
|
||||
self._setup_callbacks()
|
||||
|
||||
def _render(self, rect):
|
||||
self._update_layout_rects(rect)
|
||||
def _render(self, _):
|
||||
self._handle_onroad_transition()
|
||||
self._render_main_content()
|
||||
|
||||
@@ -42,11 +41,11 @@ class MainLayout(Widget):
|
||||
self._layouts[MainState.SETTINGS].set_callbacks(on_close=self._set_mode_for_state)
|
||||
self._layouts[MainState.ONROAD].set_callbacks(on_click=self._on_onroad_clicked)
|
||||
|
||||
def _update_layout_rects(self, rect):
|
||||
self._sidebar_rect = rl.Rectangle(rect.x, rect.y, SIDEBAR_WIDTH, rect.height)
|
||||
def _update_layout_rects(self):
|
||||
self._sidebar_rect = rl.Rectangle(self._rect.x, self._rect.y, SIDEBAR_WIDTH, self._rect.height)
|
||||
|
||||
x_offset = SIDEBAR_WIDTH if self._sidebar.is_visible else 0
|
||||
self._content_rect = rl.Rectangle(rect.y + x_offset, rect.y, rect.width - x_offset, rect.height)
|
||||
self._content_rect = rl.Rectangle(self._rect.y + x_offset, self._rect.y, self._rect.width - x_offset, self._rect.height)
|
||||
|
||||
def _handle_onroad_transition(self):
|
||||
if ui_state.started != self._prev_onroad:
|
||||
|
||||
@@ -272,12 +272,13 @@ class ListView(Widget):
|
||||
self.scroll_panel = GuiScrollPanel()
|
||||
self._font = gui_app.font(FontWeight.NORMAL)
|
||||
self._hovered_item = -1
|
||||
self._total_height = 0
|
||||
|
||||
def _render(self, rect: rl.Rectangle):
|
||||
total_height = self._update_item_rects(rect)
|
||||
self._update_layout_rects()
|
||||
|
||||
# Update layout and handle scrolling
|
||||
content_rect = rl.Rectangle(rect.x, rect.y, rect.width, total_height)
|
||||
content_rect = rl.Rectangle(rect.x, rect.y, rect.width, self._total_height)
|
||||
scroll_offset = self.scroll_panel.handle_scroll(rect, content_rect)
|
||||
|
||||
# Handle mouse interaction
|
||||
@@ -317,18 +318,18 @@ class ListView(Widget):
|
||||
return i
|
||||
return None
|
||||
|
||||
def _update_item_rects(self, container_rect: rl.Rectangle) -> float:
|
||||
def _update_layout_rects(self):
|
||||
current_y = 0.0
|
||||
for item in self._items:
|
||||
if not item.is_visible:
|
||||
item.rect = rl.Rectangle(container_rect.x, container_rect.y + current_y, container_rect.width, 0)
|
||||
item.rect = rl.Rectangle(self._rect.x, self._rect.y + current_y, self._rect.width, 0)
|
||||
continue
|
||||
|
||||
content_width = item.get_content_width(int(container_rect.width - ITEM_PADDING * 2))
|
||||
content_width = item.get_content_width(int(self._rect.width - ITEM_PADDING * 2))
|
||||
item_height = item.get_item_height(self._font, content_width)
|
||||
item.rect = rl.Rectangle(container_rect.x, container_rect.y + current_y, container_rect.width, item_height)
|
||||
item.rect = rl.Rectangle(self._rect.x, self._rect.y + current_y, self._rect.width, item_height)
|
||||
current_y += item_height
|
||||
return current_y # total height of all items
|
||||
self._total_height = current_y # total height of all items
|
||||
|
||||
def _render_item(self, item: ListItem, y: int):
|
||||
content_x = item.rect.x + ITEM_PADDING
|
||||
|
||||
@@ -24,7 +24,11 @@ class Widget(abc.ABC):
|
||||
self._is_visible = visible
|
||||
|
||||
def set_rect(self, rect: rl.Rectangle) -> None:
|
||||
prev_rect = self._rect
|
||||
self._rect = rect
|
||||
if (rect.x != prev_rect.x or rect.y != prev_rect.y or
|
||||
rect.width != prev_rect.width or rect.height != prev_rect.height):
|
||||
self._update_layout_rects()
|
||||
|
||||
def render(self, rect: rl.Rectangle = None) -> bool | int | None:
|
||||
if rect is not None:
|
||||
@@ -52,6 +56,9 @@ class Widget(abc.ABC):
|
||||
def _render(self, rect: rl.Rectangle) -> bool | int | None:
|
||||
"""Render the widget within the given rectangle."""
|
||||
|
||||
def _update_layout_rects(self) -> None:
|
||||
"""Optionally update any layout rects on Widget rect change."""
|
||||
|
||||
def _handle_mouse_release(self, mouse_pos: rl.Vector2) -> bool:
|
||||
"""Handle mouse release events, if applicable."""
|
||||
"""Optionally handle mouse release events."""
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user