You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					98 lines
				
				3.1 KiB
			
		
		
			
		
	
	
					98 lines
				
				3.1 KiB
			| 
											5 months ago
										 | import abc
 | ||
|  | import pyray as rl
 | ||
|  | from enum import IntEnum
 | ||
| 
											5 months ago
										 | from collections.abc import Callable
 | ||
| 
											4 months ago
										 | from openpilot.system.ui.lib.application import gui_app, MousePos
 | ||
| 
											5 months ago
										 | 
 | ||
|  | 
 | ||
|  | class DialogResult(IntEnum):
 | ||
|  |   CANCEL = 0
 | ||
|  |   CONFIRM = 1
 | ||
|  |   NO_ACTION = -1
 | ||
|  | 
 | ||
|  | 
 | ||
|  | class Widget(abc.ABC):
 | ||
|  |   def __init__(self):
 | ||
| 
											5 months ago
										 |     self._rect: rl.Rectangle = rl.Rectangle(0, 0, 0, 0)
 | ||
| 
											4 months ago
										 |     self._parent_rect: rl.Rectangle = rl.Rectangle(0, 0, 0, 0)
 | ||
| 
											5 months ago
										 |     self._is_pressed = False
 | ||
| 
											5 months ago
										 |     self._is_visible: bool | Callable[[], bool] = True
 | ||
| 
											4 months ago
										 |     self._touch_valid_callback: Callable[[], bool] | None = None
 | ||
|  | 
 | ||
|  |   def set_touch_valid_callback(self, touch_callback: Callable[[], bool]) -> None:
 | ||
|  |     """Set a callback to determine if the widget can be clicked."""
 | ||
|  |     self._touch_valid_callback = touch_callback
 | ||
|  | 
 | ||
|  |   def _touch_valid(self) -> bool:
 | ||
|  |     """Check if the widget can be touched."""
 | ||
|  |     return self._touch_valid_callback() if self._touch_valid_callback else True
 | ||
| 
											5 months ago
										 | 
 | ||
|  |   @property
 | ||
|  |   def is_visible(self) -> bool:
 | ||
|  |     return self._is_visible() if callable(self._is_visible) else self._is_visible
 | ||
|  | 
 | ||
| 
											4 months ago
										 |   @property
 | ||
|  |   def rect(self) -> rl.Rectangle:
 | ||
|  |     return self._rect
 | ||
|  | 
 | ||
| 
											5 months ago
										 |   def set_visible(self, visible: bool | Callable[[], bool]) -> None:
 | ||
|  |     self._is_visible = visible
 | ||
| 
											5 months ago
										 | 
 | ||
| 
											5 months ago
										 |   def set_rect(self, rect: rl.Rectangle) -> None:
 | ||
| 
											4 months ago
										 |     changed = (self._rect.x != rect.x or self._rect.y != rect.y or
 | ||
|  |                self._rect.width != rect.width or self._rect.height != rect.height)
 | ||
| 
											5 months ago
										 |     self._rect = rect
 | ||
| 
											4 months ago
										 |     if changed:
 | ||
|  |       self._update_layout_rects()
 | ||
|  | 
 | ||
|  |   def set_parent_rect(self, parent_rect: rl.Rectangle) -> None:
 | ||
|  |     """Can be used like size hint in QT"""
 | ||
|  |     self._parent_rect = parent_rect
 | ||
|  | 
 | ||
|  |   def set_position(self, x: float, y: float) -> None:
 | ||
|  |     changed = (self._rect.x != x or self._rect.y != y)
 | ||
|  |     self._rect.x, self._rect.y = x, y
 | ||
|  |     if changed:
 | ||
| 
											5 months ago
										 |       self._update_layout_rects()
 | ||
| 
											5 months ago
										 | 
 | ||
|  |   def render(self, rect: rl.Rectangle = None) -> bool | int | None:
 | ||
|  |     if rect is not None:
 | ||
|  |       self.set_rect(rect)
 | ||
|  | 
 | ||
| 
											5 months ago
										 |     self._update_state()
 | ||
|  | 
 | ||
| 
											5 months ago
										 |     if not self.is_visible:
 | ||
|  |       return None
 | ||
|  | 
 | ||
| 
											5 months ago
										 |     ret = self._render(self._rect)
 | ||
| 
											5 months ago
										 | 
 | ||
|  |     # Keep track of whether mouse down started within the widget's rectangle
 | ||
| 
											4 months ago
										 |     for mouse_event in gui_app.mouse_events:
 | ||
|  |       if mouse_event.left_pressed and self._touch_valid():
 | ||
|  |         if rl.check_collision_point_rec(mouse_event.pos, self._rect):
 | ||
|  |           self._is_pressed = True
 | ||
| 
											5 months ago
										 | 
 | ||
| 
											4 months ago
										 |       elif not self._touch_valid():
 | ||
|  |         self._is_pressed = False
 | ||
| 
											4 months ago
										 | 
 | ||
| 
											4 months ago
										 |       elif mouse_event.left_released:
 | ||
|  |         if self._is_pressed and rl.check_collision_point_rec(mouse_event.pos, self._rect):
 | ||
|  |           self._handle_mouse_release(mouse_event.pos)
 | ||
|  |         self._is_pressed = False
 | ||
| 
											5 months ago
										 | 
 | ||
|  |     return ret
 | ||
|  | 
 | ||
|  |   @abc.abstractmethod
 | ||
|  |   def _render(self, rect: rl.Rectangle) -> bool | int | None:
 | ||
|  |     """Render the widget within the given rectangle."""
 | ||
|  | 
 | ||
| 
											5 months ago
										 |   def _update_state(self):
 | ||
|  |     """Optionally update the widget's non-layout state. This is called before rendering."""
 | ||
|  | 
 | ||
| 
											5 months ago
										 |   def _update_layout_rects(self) -> None:
 | ||
|  |     """Optionally update any layout rects on Widget rect change."""
 | ||
|  | 
 | ||
| 
											4 months ago
										 |   def _handle_mouse_release(self, mouse_pos: MousePos) -> bool:
 | ||
| 
											5 months ago
										 |     """Optionally handle mouse release events."""
 | ||
| 
											5 months ago
										 |     return False
 |