more ty, part 2 (#38379)

This commit is contained in:
Adeeb Shihadeh
2026-07-19 11:02:00 -07:00
committed by GitHub
parent 19ecc37de8
commit f0d93eb32d
57 changed files with 194 additions and 165 deletions
+19 -10
View File
@@ -3,16 +3,25 @@ from __future__ import annotations
import abc
import pyray as rl
from enum import IntEnum
from typing import TypeVar
from typing import Protocol, TypeVar
from collections.abc import Callable
from openpilot.system.ui.lib.application import gui_app, MousePos, MAX_TOUCH_SLOTS, MouseEvent
try:
from openpilot.selfdrive.ui.ui_state import device
except ImportError:
class Device:
awake = True
device = Device()
class DeviceLike(Protocol):
awake: bool
def _get_device() -> DeviceLike:
try:
from openpilot.selfdrive.ui.ui_state import device
return device
except ImportError:
class Device:
awake = True
return Device()
device = _get_device()
W = TypeVar('W', bound='Widget')
@@ -185,16 +194,16 @@ class Widget(abc.ABC):
"""Optionally update the widget's non-layout state. This is called before rendering."""
@abc.abstractmethod
def _render(self, rect: rl.Rectangle) -> bool | int | None:
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_press(self, mouse_pos: MousePos) -> None:
def _handle_mouse_press(self, mouse_pos: MousePos, /) -> None:
"""Optionally handle mouse press events."""
def _handle_mouse_release(self, mouse_pos: MousePos) -> None:
def _handle_mouse_release(self, mouse_pos: MousePos, /) -> None:
"""Optionally handle mouse release events."""
if self._click_delay is not None:
self._click_release_time = rl.get_time() + self._click_delay
+1 -2
View File
@@ -1,7 +1,6 @@
import math
from enum import IntEnum
from collections.abc import Callable
from itertools import zip_longest
from typing import Union
import pyray as rl
@@ -210,7 +209,7 @@ class Label(Widget):
icon_x = self._rect.x + (self._rect.width - self._icon.width) / 2
rl.draw_texture_v(self._icon, rl.Vector2(icon_x, icon_y), rl.WHITE)
for text, text_size, emojis in zip_longest(self._text_wrapped, self._text_size, self._emojis, fillvalue=[]):
for text, text_size, emojis in zip(self._text_wrapped, self._text_size, self._emojis, strict=True):
line_pos = rl.Vector2(text_pos.x, text_pos.y)
if self._text_alignment == rl.GuiTextAlignment.TEXT_ALIGN_LEFT:
line_pos.x += self._text_padding
+1 -1
View File
@@ -78,7 +78,7 @@ class NavWidget(Widget, abc.ABC):
# the top of a vertical scroll panel to prevent erroneous swipes
return True
def set_back_callback(self, callback: Callable[[], None]) -> None:
def set_back_callback(self, callback: Callable[[], None] | None) -> None:
self._back_callback = callback
def set_shown_callback(self, callback: Callable[[], None] | None) -> None:
+7 -12
View File
@@ -15,16 +15,6 @@ from openpilot.system.ui.widgets.label import gui_label
from openpilot.system.ui.widgets.scroller_tici import Scroller
from openpilot.system.ui.widgets.list_view import ButtonAction, ListItem, MultipleButtonAction, ToggleAction, button_item, text_item
# These are only used for AdvancedNetworkSettings, standalone apps just need WifiManagerUI
try:
from openpilot.common.params import Params
from openpilot.selfdrive.ui.ui_state import ui_state
from openpilot.selfdrive.ui.lib.prime_state import PrimeType
except Exception:
Params = None
ui_state = None
PrimeType = None
NM_DEVICE_STATE_NEED_AUTH = 60
MIN_PASSWORD_LENGTH = 8
MAX_PASSWORD_LENGTH = 64
@@ -105,11 +95,16 @@ class NetworkUI(Widget):
class AdvancedNetworkSettings(Widget):
def __init__(self, wifi_manager: WifiManager):
assert Params is not None
# AdvancedNetworkSettings needs the full openpilot environment, standalone apps just use WifiManagerUI
from openpilot.common.params import Params
from openpilot.selfdrive.ui.ui_state import ui_state
from openpilot.selfdrive.ui.lib.prime_state import PrimeType
super().__init__()
self._wifi_manager = wifi_manager
self._wifi_manager.add_callbacks(networks_updated=self._on_network_updated)
self._params = Params()
self._prime_state = ui_state.prime_state
self._cell_prime_types = (PrimeType.NONE, PrimeType.LITE)
self._keyboard = Keyboard(max_text_size=MAX_PASSWORD_LENGTH, min_text_size=MIN_PASSWORD_LENGTH, show_password_toggle=True)
@@ -254,7 +249,7 @@ class AdvancedNetworkSettings(Widget):
self._wifi_manager.process_callbacks()
# If not using prime SIM, show GSM settings and enable IPv4 forwarding
show_cell_settings = ui_state.prime_state.get_type() in (PrimeType.NONE, PrimeType.LITE)
show_cell_settings = self._prime_state.get_type() in self._cell_prime_types
self._wifi_manager.set_ipv4_forward(show_cell_settings)
self._roaming_btn.set_visible(show_cell_settings)
self._apn_btn.set_visible(show_cell_settings)
+6 -6
View File
@@ -1,6 +1,6 @@
import pyray as rl
import numpy as np
from collections.abc import Callable
from collections.abc import Callable, Sequence
from openpilot.common.filter_simple import FirstOrderFilter, BounceFilter
from openpilot.common.swaglog import cloudlog
@@ -40,7 +40,7 @@ class ScrollIndicator(Widget):
self._content_size = content_size
self._viewport = viewport
def _render(self, _):
def _render(self, _, /):
# scale indicator width based on content size
indicator_w = float(np.interp(self._content_size, [1000, 3000], [300, 100]))
@@ -69,7 +69,7 @@ class ScrollIndicator(Widget):
class _Scroller(Widget):
"""Should use wrapper below to reduce boilerplate"""
def __init__(self, items: list[Widget], horizontal: bool = True, snap_items: bool = False, spacing: int = ITEM_SPACING,
def __init__(self, items: Sequence[Widget], horizontal: bool = True, snap_items: bool = False, spacing: int = ITEM_SPACING,
pad: int = ITEM_SPACING, scroll_indicator: bool = True, edge_shadows: bool = True):
super().__init__()
self._items: list[Widget] = []
@@ -150,7 +150,7 @@ class _Scroller(Widget):
and not self.moving_items and (original_touch_valid_callback() if
original_touch_valid_callback else True))
def add_widgets(self, items: list[Widget]) -> None:
def add_widgets(self, items: Sequence[Widget]) -> None:
for item in items:
self.add_widget(item)
@@ -332,7 +332,7 @@ class _Scroller(Widget):
else:
item.render()
def _render(self, _):
def _render(self, _, /):
rl.begin_scissor_mode(int(self._rect.x), int(self._rect.y),
int(self._rect.width), int(self._rect.height))
@@ -397,7 +397,7 @@ class Scroller(Widget):
# pass down enabled to child widget for nav stack
self._scroller.set_enabled(lambda: self.enabled)
def _render(self, _):
def _render(self, _, /):
self._scroller.render(self._rect)
+2 -1
View File
@@ -1,4 +1,5 @@
import pyray as rl
from collections.abc import Sequence
from openpilot.system.ui.lib.scroll_panel import GuiScrollPanel
from openpilot.system.ui.widgets import Widget
@@ -23,7 +24,7 @@ class LineSeparator(Widget):
class Scroller(Widget):
def __init__(self, items: list[Widget], spacing: int = ITEM_SPACING, line_separator: bool = False, pad_end: bool = True):
def __init__(self, items: Sequence[Widget], spacing: int = ITEM_SPACING, line_separator: bool = False, pad_end: bool = True):
super().__init__()
self._items: list[Widget] = []
self._spacing = spacing