ui: refactor GuiApplication.render into smaller helper methods (#36569)

refactor render into smaller helper method
pull/36543/merge
Dean Lee 2 days ago committed by GitHub
parent 3099f4f12d
commit ed42cfe699
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 85
      system/ui/lib/application.py

@ -381,28 +381,9 @@ class GuiApplication:
rl.clear_background(rl.BLACK) rl.clear_background(rl.BLACK)
# Handle modal overlay rendering and input processing # Handle modal overlay rendering and input processing
if self._modal_overlay.overlay: if self._handle_modal_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:
raise Exception
# Send show event to Widget
if not self._modal_overlay_shown and hasattr(self._modal_overlay.overlay, 'show_event'):
self._modal_overlay.overlay.show_event()
self._modal_overlay_shown = True
if result >= 0:
# Clear the overlay and execute the callback
original_modal = self._modal_overlay
self._modal_overlay = ModalOverlay()
if original_modal.callback is not None:
original_modal.callback(result)
yield False yield False
else: else:
self._modal_overlay_shown = False
yield True yield True
if self._render_texture: if self._render_texture:
@ -417,24 +398,7 @@ class GuiApplication:
rl.draw_fps(10, 10) rl.draw_fps(10, 10)
if self._show_touches: if self._show_touches:
current_time = time.monotonic() self._draw_touch_points()
for mouse_event in self._mouse_events:
if mouse_event.left_pressed:
self._mouse_history.clear()
self._mouse_history.append(MousePosWithTime(mouse_event.pos.x * self._scale, mouse_event.pos.y * self._scale, current_time))
# Remove old touch points that exceed the timeout
while self._mouse_history and (current_time - self._mouse_history[0].t) > TOUCH_HISTORY_TIMEOUT:
self._mouse_history.popleft()
if self._mouse_history:
mouse_pos = self._mouse_history[-1]
rl.draw_circle(int(mouse_pos.x), int(mouse_pos.y), 15, rl.RED)
for idx, mouse_pos in enumerate(self._mouse_history):
perc = idx / len(self._mouse_history)
color = rl.Color(min(int(255 * (1.5 - perc)), 255), int(min(255 * (perc + 0.5), 255)), 50, 255)
rl.draw_circle(int(mouse_pos.x), int(mouse_pos.y), 5, color)
rl.end_drawing() rl.end_drawing()
self._monitor_fps() self._monitor_fps()
@ -456,6 +420,31 @@ class GuiApplication:
def height(self): def height(self):
return self._height return self._height
def _handle_modal_overlay(self) -> bool:
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:
raise Exception
# Send show event to Widget
if not self._modal_overlay_shown and hasattr(self._modal_overlay.overlay, 'show_event'):
self._modal_overlay.overlay.show_event()
self._modal_overlay_shown = True
if result >= 0:
# Clear the overlay and execute the callback
original_modal = self._modal_overlay
self._modal_overlay = ModalOverlay()
if original_modal.callback is not None:
original_modal.callback(result)
return True
else:
self._modal_overlay_shown = False
return False
def _load_fonts(self): def _load_fonts(self):
for font_weight_file in FontWeight: for font_weight_file in FontWeight:
with as_file(FONT_DIR) as fspath: with as_file(FONT_DIR) as fspath:
@ -539,6 +528,26 @@ class GuiApplication:
cloudlog.error(f"FPS dropped critically below {fps}. Shutting down UI.") cloudlog.error(f"FPS dropped critically below {fps}. Shutting down UI.")
os._exit(1) os._exit(1)
def _draw_touch_points(self):
current_time = time.monotonic()
for mouse_event in self._mouse_events:
if mouse_event.left_pressed:
self._mouse_history.clear()
self._mouse_history.append(MousePosWithTime(mouse_event.pos.x * self._scale, mouse_event.pos.y * self._scale, current_time))
# Remove old touch points that exceed the timeout
while self._mouse_history and (current_time - self._mouse_history[0].t) > TOUCH_HISTORY_TIMEOUT:
self._mouse_history.popleft()
if self._mouse_history:
mouse_pos = self._mouse_history[-1]
rl.draw_circle(int(mouse_pos.x), int(mouse_pos.y), 15, rl.RED)
for idx, mouse_pos in enumerate(self._mouse_history):
perc = idx / len(self._mouse_history)
color = rl.Color(min(int(255 * (1.5 - perc)), 255), int(min(255 * (perc + 0.5), 255)), 50, 255)
rl.draw_circle(int(mouse_pos.x), int(mouse_pos.y), 5, color)
def _output_render_profile(self): def _output_render_profile(self):
import io import io
import pstats import pstats

Loading…
Cancel
Save