ui: add ModalOverlay system for unified modal dialog management (#35478)

add ModalOverlay
This commit is contained in:
Dean Lee
2025-06-07 04:18:07 +08:00
committed by GitHub
parent 75434b10b9
commit 6767bfce44
6 changed files with 70 additions and 47 deletions
+27 -1
View File
@@ -2,6 +2,8 @@ import atexit
import os
import time
import pyray as rl
from collections.abc import Callable
from dataclasses import dataclass
from enum import IntEnum
from importlib.resources import as_file, files
from openpilot.common.swaglog import cloudlog
@@ -36,6 +38,11 @@ class FontWeight(IntEnum):
BLACK = 8
@dataclass
class ModalOverlay:
overlay: object = None
callback: Callable | None = None
class GuiApplication:
def __init__(self, width: int, height: int):
self._fonts: dict[FontWeight, rl.Font] = {}
@@ -50,6 +57,8 @@ class GuiApplication:
self._last_fps_log_time: float = time.monotonic()
self._window_close_requested = False
self._trace_log_callback = None
self._modal_overlay = ModalOverlay()
def request_close(self):
self._window_close_requested = True
@@ -79,6 +88,9 @@ class GuiApplication:
self._set_styles()
self._load_fonts()
def set_modal_overlay(self, overlay, callback: Callable | None = None):
self._modal_overlay = ModalOverlay(overlay=overlay, callback=callback)
def texture(self, asset_path: str, width: int, height: int, alpha_premultiply=False, keep_aspect_ratio=True):
cache_key = f"{asset_path}_{width}_{height}_{alpha_premultiply}{keep_aspect_ratio}"
if cache_key in self._textures:
@@ -148,7 +160,21 @@ class GuiApplication:
rl.begin_drawing()
rl.clear_background(rl.BLACK)
yield
# Handle modal overlay rendering and input processing
if self._modal_overlay.overlay:
if hasattr(self._modal_overlay.overlay, 'render'):
result = self._modal_overlay.overlay.render(rl.Rectangle(0, 0, self.width, self.height))
elif callable(self._modal_overlay.overlay):
result = self._modal_overlay.overlay()
else:
assert(0)
if result >= 0 and self._modal_overlay.callback is not None:
# Execute callback with the result and clear the overlay
self._modal_overlay.callback(result)
self._modal_overlay = ModalOverlay()
else:
yield
if self._render_texture:
rl.end_texture_mode()