mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 02:13:41 +08:00
Show upstream offroad alerts and update prompts on the StarPilot home
The StarPilot home replaced upstream's home without its offroad alert check, so alerts such as Offroad_ExcessiveActuation (which blocks engagement until acknowledged) and update prompts never appeared. Large UI: the StarPilot home now runs upstream's 10 s alert refresh and draws upstream's UPDATE / ALERTS header pills. While an alert or update panel is open, upstream's home renders instead, with its Close, Acknowledge, Snooze and Update buttons; Close returns to the StarPilot home. Upstream HomeLayout moves its refresh into _update_state and its pills into _render_notifs so both homes share them unchanged. Compact UI: the StarPilot home draws upstream's alerts pill, and a tap in upstream's alerts zone scrolls to the alerts page (settings when no alert is active).
This commit is contained in:
@@ -89,11 +89,6 @@ class HomeLayout(Widget):
|
||||
self.current_state = state
|
||||
|
||||
def _render(self, rect: rl.Rectangle):
|
||||
current_time = time.monotonic()
|
||||
if current_time - self.last_refresh >= REFRESH_INTERVAL:
|
||||
self._refresh()
|
||||
self.last_refresh = current_time
|
||||
|
||||
self._render_header()
|
||||
|
||||
# Render content based on current state
|
||||
@@ -105,6 +100,11 @@ class HomeLayout(Widget):
|
||||
self._render_alerts_view()
|
||||
|
||||
def _update_state(self):
|
||||
current_time = time.monotonic()
|
||||
if current_time - self.last_refresh >= REFRESH_INTERVAL:
|
||||
self._refresh()
|
||||
self.last_refresh = current_time
|
||||
|
||||
self.header_rect = rl.Rectangle(
|
||||
self._rect.x + CONTENT_MARGIN, self._rect.y + CONTENT_MARGIN, self._rect.width - 2 * CONTENT_MARGIN, HEADER_HEIGHT
|
||||
)
|
||||
@@ -140,13 +140,20 @@ class HomeLayout(Widget):
|
||||
self._set_state(HomeLayoutState.ALERTS)
|
||||
|
||||
def _render_header(self):
|
||||
version_text_width = self.header_rect.width - self._render_notifs()
|
||||
version_rect = rl.Rectangle(self.header_rect.x + self.header_rect.width - version_text_width, self.header_rect.y,
|
||||
version_text_width, self.header_rect.height)
|
||||
gui_label(version_rect, self._version_text, 48, rl.WHITE, alignment=TextAlignment.RIGHT)
|
||||
|
||||
def _render_notifs(self) -> float:
|
||||
"""Draw the UPDATE / ALERTS header pills and return the header width they use."""
|
||||
font = gui_app.font(FontWeight.MEDIUM)
|
||||
|
||||
version_text_width = self.header_rect.width
|
||||
used_width = 0.0
|
||||
|
||||
# Update notification button
|
||||
if self.update_available:
|
||||
version_text_width -= self.update_notif_rect.width
|
||||
used_width += self.update_notif_rect.width
|
||||
|
||||
# Highlight if currently viewing updates
|
||||
highlight_color = rl.Color(75, 95, 255, 255) if self.current_state == HomeLayoutState.UPDATE else rl.Color(54, 77, 239, 255)
|
||||
@@ -160,7 +167,7 @@ class HomeLayout(Widget):
|
||||
|
||||
# Alert notification button
|
||||
if self.alert_count > 0:
|
||||
version_text_width -= self.alert_notif_rect.width
|
||||
used_width += self.alert_notif_rect.width
|
||||
|
||||
# Highlight if currently viewing alerts
|
||||
highlight_color = rl.Color(255, 70, 70, 255) if self.current_state == HomeLayoutState.ALERTS else rl.Color(226, 44, 44, 255)
|
||||
@@ -172,13 +179,10 @@ class HomeLayout(Widget):
|
||||
text_y = self.alert_notif_rect.y + (self.alert_notif_rect.height - text_size.y) // 2
|
||||
rl.draw_text_ex(font, alert_text, rl.Vector2(int(text_x), int(text_y)), HEAD_BUTTON_FONT_SIZE, 0, rl.WHITE)
|
||||
|
||||
# Version text (right aligned)
|
||||
# Gap before the right-aligned version text
|
||||
if self.update_available or self.alert_count > 0:
|
||||
version_text_width -= SPACING * 1.5
|
||||
|
||||
version_rect = rl.Rectangle(self.header_rect.x + self.header_rect.width - version_text_width, self.header_rect.y,
|
||||
version_text_width, self.header_rect.height)
|
||||
gui_label(version_rect, self._version_text, 48, rl.WHITE, alignment=TextAlignment.RIGHT)
|
||||
used_width += SPACING * 1.5
|
||||
return used_width
|
||||
|
||||
def _render_home_content(self):
|
||||
self._render_left_column()
|
||||
|
||||
@@ -475,9 +475,11 @@ def test_star_large_routes_shell_and_uses_confirmed_offroad(main, probe):
|
||||
namespace = vars(main.native).copy()
|
||||
namespace['Destination'] = NS(NETWORK='network')
|
||||
namespace['ShellMode'] = NS(HOME='home', SETTINGS='settings', ONROAD='onroad')
|
||||
namespace['HomeLayoutState'] = NS(HOME='home')
|
||||
star = layout_classes(ROOT.parent / 'starpilot/ui/runtime_app.py', {'StarMainLayout'}, namespace)
|
||||
main.layout.__class__ = star.StarMainLayout
|
||||
layout = main.layout
|
||||
layout._home_layout.current_state = 'home'
|
||||
layout.star, layout.page = Mock(), Mock()
|
||||
layout.star.selected = 'developer'
|
||||
layout._large_destination = None
|
||||
|
||||
@@ -16,6 +16,7 @@ class HomeMode(StrEnum):
|
||||
|
||||
class HomeActionKind(StrEnum):
|
||||
OPEN_SETTINGS = "open_settings"
|
||||
OPEN_ALERTS = "open_alerts"
|
||||
OPEN_TOGGLES = "open_toggles"
|
||||
OPEN_PAIRING = "open_pairing"
|
||||
SET_EXPERIMENTAL = "set_experimental"
|
||||
@@ -135,7 +136,8 @@ class HomeInput:
|
||||
return
|
||||
if self.profile == Profile.COMPACT:
|
||||
if 0 <= x < 536 and 0 <= y < 240:
|
||||
self.emit(HomeAction(HomeActionKind.OPEN_SETTINGS))
|
||||
# Right edge is upstream's alerts pill zone (mici ALERTS_ZONE_WIDTH); without alerts it opens settings
|
||||
self.emit(HomeAction(HomeActionKind.OPEN_ALERTS if x >= 536 - 180 else HomeActionKind.OPEN_SETTINGS))
|
||||
elif px < 300 and 50 <= x <= 250 and 35 <= y <= 152:
|
||||
self.emit(HomeAction(HomeActionKind.OPEN_SETTINGS))
|
||||
elif px < 300 and state.recording_audio and 170 <= x <= 245 and 245 <= y <= 285:
|
||||
|
||||
@@ -21,6 +21,8 @@ from openpilot.starpilot.ui.raylib import rl
|
||||
import openpilot.cereal.messaging as messaging
|
||||
from opendbc.car.structs import car as car_schema
|
||||
|
||||
from openpilot.selfdrive.ui.layouts.home import HomeLayoutState
|
||||
from openpilot.selfdrive.ui.mici.layouts.home import HOME_PADDING
|
||||
from openpilot.selfdrive.ui.layouts.main import MainLayout, MainState
|
||||
from openpilot.selfdrive.ui.mici.layouts.main import MiciMainLayout as MiciMainLayout
|
||||
from openpilot.starpilot.ui.runtime_compact import StarMiciMainLayout as StarMiciMainLayout
|
||||
@@ -267,6 +269,7 @@ class StarShellSession:
|
||||
self._on_settings: Any = None
|
||||
self._on_home: Any = None
|
||||
self._on_pairing: Any = None
|
||||
self._on_alerts: Any = None
|
||||
self._on_compact_destination: Any = None
|
||||
self._on_destination_change: Any = None
|
||||
self._request_owner: Any = None
|
||||
@@ -653,10 +656,11 @@ class StarShellSession:
|
||||
|
||||
|
||||
def set_navigation(self, *, on_settings: Any, on_home: Any, on_pairing: Any = None, on_compact_destination: Any = None,
|
||||
on_destination_change: Any = None) -> None:
|
||||
on_destination_change: Any = None, on_alerts: Any = None) -> None:
|
||||
self._on_settings = on_settings
|
||||
self._on_home = on_home
|
||||
self._on_pairing = on_pairing
|
||||
self._on_alerts = on_alerts
|
||||
self._on_compact_destination = on_compact_destination
|
||||
self._on_destination_change = on_destination_change
|
||||
|
||||
@@ -1548,7 +1552,9 @@ class StarShellSession:
|
||||
self._unavailable("experimental shortcut in this context")
|
||||
return
|
||||
if request.source == "home":
|
||||
if action.kind == HomeActionKind.OPEN_SETTINGS and self._on_settings is not None:
|
||||
if action.kind == HomeActionKind.OPEN_ALERTS and self._on_alerts is not None and self._on_alerts():
|
||||
pass # on_alerts returns False when no alert is active, so the tap falls through to settings
|
||||
elif action.kind in (HomeActionKind.OPEN_SETTINGS, HomeActionKind.OPEN_ALERTS) and self._on_settings is not None:
|
||||
self.selected = Destination.STAR
|
||||
self._on_settings()
|
||||
elif action.kind == HomeActionKind.OPEN_TOGGLES and self.profile == Profile.LARGE:
|
||||
@@ -1830,16 +1836,22 @@ class StarShellSession:
|
||||
|
||||
|
||||
class StarShellPage(Widget):
|
||||
def __init__(self, session: StarShellSession, mode: ShellMode, *, on_background_tap: Any = None):
|
||||
def __init__(self, session: StarShellSession, mode: ShellMode, *, on_background_tap: Any = None,
|
||||
alerts_pill: Widget | None = None):
|
||||
super().__init__()
|
||||
self.session, self.mode = session, mode
|
||||
self.on_background_tap = on_background_tap
|
||||
self.alerts_pill = alerts_pill
|
||||
self._press_pos: tuple[float, float] | None = None
|
||||
self._dragged = False
|
||||
self._touch_time: float | None = None
|
||||
|
||||
def _render(self, rect: rl.Rectangle) -> None:
|
||||
self.session.render(self.mode, rect, self._parent_rect)
|
||||
if self.alerts_pill is not None: # bottom right, where upstream's compact home draws it
|
||||
self.alerts_pill.set_position(rect.x + rect.width - self.alerts_pill.rect.width - HOME_PADDING,
|
||||
rect.y + rect.height - self.alerts_pill.rect.height)
|
||||
self.alerts_pill.render()
|
||||
if self.mode == ShellMode.ONROAD and self.session.profile == Profile.COMPACT:
|
||||
bookmark = self.session.camera_owner._bookmark_icon
|
||||
bookmark.set_touch_valid_callback(lambda: self._touch_valid() and abs(self.rect.x) < 1)
|
||||
@@ -2082,8 +2094,11 @@ class StarMainLayout(MainLayout):
|
||||
software_request=SoftwareRequest, download_label=DownloadLabel)
|
||||
|
||||
def _render_main_content(self) -> None:
|
||||
if self._current_mode == MainState.ARABESQUE or self._current_mode == MainState.HOME and ui_state.is_body:
|
||||
super()._render_main_content()
|
||||
on_home = self._current_mode == MainState.HOME
|
||||
if self._current_mode == MainState.ARABESQUE or on_home and (
|
||||
ui_state.is_body or self._home_layout.current_state != HomeLayoutState.HOME):
|
||||
self._update_layout_rects() # onroad may have left a sidebar-less content rect
|
||||
super()._render_main_content() # body home, or upstream's offroad alerts / update screen
|
||||
return
|
||||
rect = self._rect
|
||||
if self._current_mode == MainState.ONROAD:
|
||||
@@ -2097,6 +2112,13 @@ class StarMainLayout(MainLayout):
|
||||
MainState.ONROAD: ShellMode.ONROAD}[self._current_mode]
|
||||
self.page.mode = mode
|
||||
self.page.render(rect)
|
||||
if on_home:
|
||||
# StarPilot draws its own home: keep upstream's alert refresh, header pills and pill taps
|
||||
home = self._home_layout
|
||||
home.set_rect(rl.Rectangle(rect.x + SIDEBAR_WIDTH, rect.y, rect.width - SIDEBAR_WIDTH, rect.height))
|
||||
home._update_state()
|
||||
home._render_notifs()
|
||||
home._process_mouse_events()
|
||||
|
||||
def _on_background_tap(self) -> None:
|
||||
if self._current_mode == MainState.ONROAD:
|
||||
|
||||
@@ -20,7 +20,8 @@ class StarMiciMainLayout(MiciMainLayout):
|
||||
except Exception:
|
||||
self._native_onroad.close()
|
||||
raise
|
||||
self._home_layout = runtime.StarShellPage(self.star, runtime.ShellMode.HOME)
|
||||
# Upstream's alerts pill is already wired to the alerts page; keep it on the StarPilot home
|
||||
self._home_layout = runtime.StarShellPage(self.star, runtime.ShellMode.HOME, alerts_pill=self._home_layout._alerts_pill)
|
||||
self._car_onroad_layout = runtime.StarShellPage(self.star, runtime.ShellMode.ONROAD,
|
||||
on_background_tap=lambda: self._scroll_to(self._home_layout))
|
||||
self._settings_layout = runtime.StarCompactSettings(self.star)
|
||||
@@ -32,7 +33,8 @@ class StarMiciMainLayout(MiciMainLayout):
|
||||
page.set_rect(runtime.rl.Rectangle(0, 0, runtime.gui_app.width, runtime.gui_app.height))
|
||||
self.star.set_navigation(on_settings=lambda: runtime.gui_app.push_widget(self._settings_layout),
|
||||
on_home=lambda: self._settings_layout.dismiss(),
|
||||
on_compact_destination=self._open_compact_destination)
|
||||
on_compact_destination=self._open_compact_destination,
|
||||
on_alerts=self._open_alerts)
|
||||
self._scroller.set_scrolling_enabled(lambda: not self._native_onroad.is_swiping_left())
|
||||
self._on_body_changed()
|
||||
self.star._native_favorite_actions = self._native_favorites
|
||||
@@ -54,6 +56,12 @@ class StarMiciMainLayout(MiciMainLayout):
|
||||
finally:
|
||||
self.star.camera_paint = True
|
||||
|
||||
def _open_alerts(self) -> bool:
|
||||
if self._alerts_layout.active_alerts() == 0:
|
||||
return False
|
||||
self._scroll_to(self._alerts_layout)
|
||||
return True
|
||||
|
||||
def _native_favorites(self):
|
||||
from openpilot.starpilot.ui import runtime_app as runtime
|
||||
def panel():
|
||||
|
||||
@@ -197,7 +197,7 @@ class PreviewRouter:
|
||||
self.toggle_requests: list[ToggleRequest] = []
|
||||
|
||||
def home_action(self, action: HomeAction) -> None:
|
||||
if action.kind == HomeActionKind.OPEN_SETTINGS:
|
||||
if action.kind in (HomeActionKind.OPEN_SETTINGS, HomeActionKind.OPEN_ALERTS): # the preview has no offroad alerts
|
||||
self.in_settings = True
|
||||
self.selected = Destination.STAR
|
||||
else:
|
||||
|
||||
@@ -214,3 +214,22 @@ def test_actual_compact_force_card_hitbox_dispatches_captured_owner_request(life
|
||||
inputs.move(230, 120, state)
|
||||
inputs.release(230, 120, state)
|
||||
session._drive_change.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("active_alerts", [0, 1])
|
||||
def test_compact_alerts_tap_opens_alerts_page_or_falls_back_to_settings(lifecycle, active_alerts):
|
||||
from openpilot.starpilot.ui.home_state import HomeAction, HomeActionKind
|
||||
from openpilot.starpilot.ui.shell import ShellRequest
|
||||
_, session, _, _ = lifecycle
|
||||
layout = runtime_app.StarMiciMainLayout.__new__(runtime_app.StarMiciMainLayout)
|
||||
layout._alerts_layout = Mock(**{"active_alerts.return_value": active_alerts})
|
||||
layout._scroll_to = Mock()
|
||||
on_settings = Mock()
|
||||
session.set_navigation(on_settings=on_settings, on_home=Mock(), on_alerts=layout._open_alerts)
|
||||
session._emit(ShellRequest("home", HomeAction(HomeActionKind.OPEN_ALERTS)))
|
||||
if active_alerts:
|
||||
layout._scroll_to.assert_called_once_with(layout._alerts_layout)
|
||||
on_settings.assert_not_called()
|
||||
else:
|
||||
layout._scroll_to.assert_not_called()
|
||||
on_settings.assert_called_once()
|
||||
|
||||
@@ -18,6 +18,12 @@ class TestHomeActions(unittest.TestCase):
|
||||
self.assertEqual(self.actions, [HomeAction(HomeActionKind.OPEN_SETTINGS)])
|
||||
self.assertFalse(self.state.experimental_enabled)
|
||||
|
||||
def test_compact_right_edge_is_upstream_alerts_pill_zone(self):
|
||||
handler = HomeInput(Profile.COMPACT, self.actions.append)
|
||||
handler.press(500, 200, 10)
|
||||
handler.release(500, 200, 10.5, self.state)
|
||||
self.assertEqual(self.actions, [HomeAction(HomeActionKind.OPEN_ALERTS)])
|
||||
|
||||
def test_long_press_requests_one_change_and_consumes_release(self):
|
||||
for enabled in (False, True):
|
||||
with self.subTest(enabled=enabled):
|
||||
|
||||
@@ -29,7 +29,8 @@ def large_layout():
|
||||
layout._prev_onroad = True
|
||||
settings = Mock()
|
||||
settings._panels = {panel: NS(instance=Mock()) for panel in PanelType}
|
||||
layout._layouts = {runtime_app.MainState.HOME: Mock(), runtime_app.MainState.SETTINGS: settings,
|
||||
layout._home_layout = Mock(current_state=runtime_app.HomeLayoutState.HOME)
|
||||
layout._layouts = {runtime_app.MainState.HOME: layout._home_layout, runtime_app.MainState.SETTINGS: settings,
|
||||
runtime_app.MainState.ONROAD: Mock()}
|
||||
layout._sidebar.set_callbacks(on_settings=layout._on_settings_clicked, on_flag=layout._on_bookmark_clicked)
|
||||
layout._sidebar._render = Mock()
|
||||
|
||||
@@ -52,9 +52,12 @@ class TestOptInLayouts(unittest.TestCase):
|
||||
settings = Mock()
|
||||
settings._panels = {panel: NS(instance=Mock()) for panel in PanelType}
|
||||
settings._panels[PanelType.TOGGLES].instance._scroller._items = []
|
||||
layout._layouts = {runtime_app.MainState.HOME: Mock(), runtime_app.MainState.SETTINGS: settings,
|
||||
layout._home_layout = Mock(current_state=runtime_app.HomeLayoutState.HOME)
|
||||
layout._layouts = {runtime_app.MainState.HOME: layout._home_layout, runtime_app.MainState.SETTINGS: settings,
|
||||
runtime_app.MainState.ONROAD: camera}
|
||||
layout._current_mode = runtime_app.MainState.HOME
|
||||
layout._rect = runtime_app.rl.Rectangle(0, 0, 2160, 1080)
|
||||
layout._sidebar = NS(is_visible=True)
|
||||
|
||||
with patch.object(runtime_app, "validate_runtime_fonts"), patch.object(runtime_app, "validate_runtime_assets"), \
|
||||
patch.object(runtime_app.MainLayout, "__init__", parent_init), \
|
||||
@@ -71,7 +74,6 @@ class TestOptInLayouts(unittest.TestCase):
|
||||
layout._set_current_layout(runtime_app.MainState.HOME)
|
||||
layout._render_main_content()
|
||||
body_render.assert_called_once()
|
||||
layout._rect = Mock()
|
||||
session.snapshot.return_value = NS(home=NS(paired=False))
|
||||
with patch.object(runtime_app.ui_state, "is_body", False), patch.object(layout.page, "render") as custom_render:
|
||||
layout._render_main_content()
|
||||
@@ -86,6 +88,44 @@ class TestOptInLayouts(unittest.TestCase):
|
||||
session.close.assert_called_once()
|
||||
camera.close.assert_called_once()
|
||||
|
||||
def test_large_home_shows_upstream_offroad_alert_screen_only_while_it_is_open(self):
|
||||
layout = runtime_app.StarMainLayout.__new__(runtime_app.StarMainLayout)
|
||||
layout._current_mode = runtime_app.MainState.HOME
|
||||
layout._rect = runtime_app.rl.Rectangle(0, 0, 2160, 1080)
|
||||
layout._sidebar = NS(is_visible=True)
|
||||
layout.page = Mock()
|
||||
layout._home_layout = home = Mock(current_state=runtime_app.HomeLayoutState.HOME)
|
||||
with patch.object(runtime_app.ui_state, "is_body", False), \
|
||||
patch.object(runtime_app.MainLayout, "_render_main_content") as upstream_render:
|
||||
layout._render_main_content()
|
||||
upstream_render.assert_not_called()
|
||||
layout.page.render.assert_called_once_with(layout._rect)
|
||||
# Upstream's alert refresh, header pills and pill taps run over the StarPilot home, in its header row.
|
||||
rect = home.set_rect.call_args.args[0]
|
||||
self.assertEqual((rect.x, rect.width), (300, 1860))
|
||||
home._update_state.assert_called_once()
|
||||
home._render_notifs.assert_called_once()
|
||||
home._process_mouse_events.assert_called_once()
|
||||
|
||||
home.current_state = runtime_app.HomeLayoutState.ALERTS # a new offroad alert or a pill tap
|
||||
layout._render_main_content()
|
||||
upstream_render.assert_called_once()
|
||||
layout.page.render.assert_called_once()
|
||||
|
||||
def test_upstream_home_refreshes_offroad_alerts_before_drawing(self):
|
||||
from openpilot.selfdrive.ui.layouts.home import HomeLayout
|
||||
home = HomeLayout.__new__(HomeLayout)
|
||||
home._rect = runtime_app.rl.Rectangle(300, 0, 1860, 1080)
|
||||
home.last_refresh = 0
|
||||
home.update_available = False
|
||||
home.update_notif_rect = runtime_app.rl.Rectangle(0, 0, 200, 70)
|
||||
home.alert_notif_rect = runtime_app.rl.Rectangle(0, 0, 220, 70)
|
||||
# StarMainLayout only calls _update_state, so the alert refresh must live there, not in _render.
|
||||
with patch.object(HomeLayout, "_refresh") as refresh:
|
||||
home._update_state()
|
||||
refresh.assert_called_once()
|
||||
self.assertEqual(home.alert_notif_rect.x, 340)
|
||||
|
||||
def test_compact_constructor_replaces_scroller_pages_and_keeps_camera_owner(self):
|
||||
session = self._session()
|
||||
camera = Mock()
|
||||
|
||||
Reference in New Issue
Block a user