Repository: commaai/openpilot
File: selfdrive/ui/mici/onroad/driver_camera_dialog.py (Python)

Code before:
EventName = log.OnroadEvent.EventName

EVENT_TO_INT = EventName.schema.enumerants


class DriverCameraView(CameraView):
  def _calc_frame_matrix(self, rect: rl.Rectangle):
    base = super()._calc_frame_matrix(rect)
    driver_view_ratio = 1.5
    base[0, 0] *= driver_view_ratio
    base[1, 1] *= driver_view_ratio
    return base


class DriverCameraDialog(NavWidget):
  def __init__(self, no_escape=False):
    super().__init__()
    self._camera_view = DriverCameraView("camerad", VisionStreamType.VISION_STREAM_DRIVER)
    self.driver_state_renderer = DriverStateRenderer(lines=True)
    self.driver_state_renderer.set_rect(rl.Rectangle(0, 0, 200, 200))
    self.driver_state_renderer.load_icons()
    self._pm = messaging.PubMaster(['selfdriveState'])
    if not no_escape:
      # TODO: this can grow unbounded, should be given some thought
      device.add_interactive_timeout_callback(self.stop_dmonitoringmodeld)
    self.set_back_callback(self.stop_dmonitoringmodeld)
    self.set_back_enabled(not no_escape)

    # Load eye icons
    self._eye_fill_texture = None
    self._eye_orange_texture = None
    self._eye_size = 74
    self._glasses_texture = None
    self._glasses_size = 171

    self._load_eye_textures()

  def stop_dmonitoringmodeld(self):
    ui_state.params.put_bool("IsDriverViewEnabled", False)
    gui_app.set_modal_overlay(None)

  def show_event(self):
    super().show_event()
    ui_state.params.put_bool("IsDriverViewEnabled", True)
    self._publish_alert_sound(None)
    device.reset_interactive_timeout(300)
    ui_state.params.remove("DriverTooDistracted")

  def hide_event(self):
    super().hide_event()
    device.reset_interactive_timeout()

Review comment (suggestion):
no circular references to self. this allows the widget's ref count to drop to 0 and deletes PubMaster and frees CameraView in `__del__`

Code after:
EventName = log.OnroadEvent.EventName

EVENT_TO_INT = EventName.schema.enumerants


class DriverCameraView(CameraView):
  def _calc_frame_matrix(self, rect: rl.Rectangle):
    base = super()._calc_frame_matrix(rect)
    driver_view_ratio = 1.5
    base[0, 0] *= driver_view_ratio
    base[1, 1] *= driver_view_ratio
    return base


class DriverCameraDialog(NavWidget):
  def __init__(self, no_escape=False):
    super().__init__()
    self._camera_view = DriverCameraView("camerad", VisionStreamType.VISION_STREAM_DRIVER)
    self.driver_state_renderer = DriverStateRenderer(lines=True)
    self.driver_state_renderer.set_rect(rl.Rectangle(0, 0, 200, 200))
    self.driver_state_renderer.load_icons()
    self._pm = messaging.PubMaster(['selfdriveState'])
    if not no_escape:
      # TODO: this can grow unbounded, should be given some thought
      device.add_interactive_timeout_callback(lambda: gui_app.set_modal_overlay(None))
    self.set_back_callback(lambda: gui_app.set_modal_overlay(None))
    self.set_back_enabled(not no_escape)

    # Load eye icons
    self._eye_fill_texture = None
    self._eye_orange_texture = None
    self._eye_size = 74
    self._glasses_texture = None
    self._glasses_size = 171

    self._load_eye_textures()

  def show_event(self):
    super().show_event()
    ui_state.params.put_bool("IsDriverViewEnabled", True)
    self._publish_alert_sound(None)
    device.reset_interactive_timeout(300)
    ui_state.params.remove("DriverTooDistracted")

  def hide_event(self):
    super().hide_event()
    ui_state.params.put_bool("IsDriverViewEnabled", False)
    device.reset_interactive_timeout()

  def _handle_mouse_release(self, _):
    ui_state.params.remove("DriverTooDistracted")