mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-08-06 11:05:42 +08:00
91b7752268
* pressed state for larger sliders
* wifibutton
* fix
* clean up
* some work
* don't nee this now
* stash
* more
* new pressed bigcircle
* black
* interp
* just check position
* clean up and fix slider reset
* fix custom
* no speed
* stash
* even chatter couldn't figure this one out
* makes sense to combine together, less split mentality
* clean that up
* fix lag
* match ui.py prio to eliminate lag on wifiui show event. separately, why is this slow?
* night mode
* delay scroll over
* fix auto scrolling
* stash
* waiting looks disabled
* clean up and don't reset sliders until user goes back
* rm
* fix
* add termsheader back
* fix callbacks
* ctrl alt l
* fix text spacing
* clean up
* stash
* fix style
* i want to go back
* guard on exit
* kinda useless stuff
* Revert "kinda useless stuff"
This reverts commit a4acbac31523408f358c5f68262cb630aa13ad8e.
* Revert "guard on exit"
This reverts commit 63ccfbf64edfbe1a144a441681f5ec78d8021ff7.
* wide
* setup pressed!
* grow animation
* 10s after initial
* slow fast
* start onboarding (terms)
* rm duplicate page
* add qr code
* final grey
* fix visual lag on first start
* clean up dead code
* dont exit from cancel
* revert grey
* clean up, REVIEW ME
* Revert "clean up, REVIEW ME"
This reverts commit c66fa60947c5f922520e7cf58c630b4bbe2d0177.
* reboot slider
* kb fix
* Revert "kb fix"
This reverts commit 883039448e6c37ae1d25d4f75ada6e96b6736358.
* ./ goes to letters
* Revert "./ goes to letters"
This reverts commit 0d97442427edb1a000638863a3f2181204ddc160.
* clean up
* some more clean up
* more
* clean up
* rename block
* reset pending scroll so it can't use stale data in rare sequence
* remove unused assets
* clean up imports
* fix updater
* clean up
* fix double reboot
* demo time - reset to setup on reboot
* let manager restart
* Revert "demo time - reset to setup on reboot"
This reverts commit 9468657e8438a1ce8fcb5266403b7bb3539f131f.
* url... and no grow animation on start button
* one next button
* grow instead of shake wifi button
* 36 pt font size in setup
* touch up onboarding a lil
* Revert "rm cpp bz2 (#37332)"
This reverts commit f4a36f7f74.
* more onboarding and clean up
* clean up
* wow what an amazing future clean up
* back to software select
* fix
* copy
* fix dm confirmation dialog not disabling widget underneath, all fixed with real nav stack in here
* uploading
* lint
* add review terms to device w/ close button
* todo
* remove old Terms vertical scrolling classes
* use new Scroller!
* installer
* tweak to match figma exactly
* revert
* fixup updater
* demo day
* demo day v2
* ... for percent while finishing setup
* demo day v3
* demo day v4
* remove ...
* demo day v6 -- "why does it do that!!"
* demo day v7 -- no flash
* hmm
* demo day v7
* prebuilt
* revert demo day
* scroll after pop animation
* back -> retry
* stash fixes
* damn, need back_callback
* scroll over immediately if already in network setup
* tweaks
* going down is confusing
* more
* Revert "more"
This reverts commit 29ce75b1f81eb40e7527a71d27842d9a66802206.
* Revert "going down is confusing"
This reverts commit 0cd2ae30d4135db1ccba6478429b45e886714e9c.
* dupl
* nl
* sort functions
* more clean up from merge
* move
* more
* dismiss to download (hack)
* Revert "dismiss to download (hack)"
This reverts commit 53c45ed1f63db1f0cebbce0dfab1777c8658f505.
* onboarding work
* set brightness and timeout in root onboarding only
* clean up
* type
* keep 5m for settings preview
* switch back to letters on . or /
* reset first step scroller
* custom software warning goes down network comes up and back cb fix
* clean up
* smaller qr
* ReviewTermsPage just for device as NavWidget
* clean up
* installer: stay on 100%
* reset has internet while in wifiui
* try this
* try this
* see what error we get exactly
see what error we get exactly
* not final solution but see how good
* rm
* copy changes
* reset on disconnect
* for separate pr
* Revert "reset on disconnect"
This reverts commit 552372fa4d497ba7d9de7f2edb730ee63798ffa4.
* revert this, too buggy
* fix for updater
* sort
* fix test
* minor cleanup
* more leaks than this rn
* onboarding clean up
* clean up application
* click delay to small button
* clean up
* reset more state
* fix training guide not cleaning up driverview
* Revert "fix training guide not cleaning up driverview"
This reverts commit cac7c5f436056cc9e747f80905d390790fb83c22.
* simpler fix :(
* nice catch, if you go back to terms it will reset 300s timeout and brightness
* duplicate show
* unused
220 lines
8.2 KiB
Python
220 lines
8.2 KiB
Python
from __future__ import annotations
|
|
|
|
import abc
|
|
import pyray as rl
|
|
from collections.abc import Callable
|
|
from openpilot.system.ui.widgets import Widget
|
|
from openpilot.common.filter_simple import BounceFilter, FirstOrderFilter
|
|
from openpilot.system.ui.lib.application import gui_app, MousePos, MouseEvent
|
|
|
|
SWIPE_AWAY_THRESHOLD = 80 # px to dismiss after releasing
|
|
START_DISMISSING_THRESHOLD = 40 # px to start dismissing while dragging
|
|
BLOCK_SWIPE_AWAY_THRESHOLD = 60 # px horizontal movement to block swipe away
|
|
|
|
NAV_BAR_MARGIN = 6
|
|
NAV_BAR_WIDTH = 205
|
|
NAV_BAR_HEIGHT = 8
|
|
|
|
DISMISS_PUSH_OFFSET = NAV_BAR_MARGIN + NAV_BAR_HEIGHT + 50 # px extra to push down when dismissing
|
|
DISMISS_ANIMATION_RC = 0.2 # slightly slower for non-user triggered dismiss animation
|
|
|
|
|
|
class NavBar(Widget):
|
|
FADE_AFTER_SECONDS = 2.0
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.set_rect(rl.Rectangle(0, 0, NAV_BAR_WIDTH, NAV_BAR_HEIGHT))
|
|
self._alpha = 1.0
|
|
self._alpha_filter = FirstOrderFilter(1.0, 0.1, 1 / gui_app.target_fps)
|
|
self._fade_time = 0.0
|
|
|
|
def set_alpha(self, alpha: float) -> None:
|
|
self._alpha = alpha
|
|
self._fade_time = rl.get_time()
|
|
|
|
def show_event(self):
|
|
super().show_event()
|
|
self._alpha = 1.0
|
|
self._alpha_filter.x = 1.0
|
|
self._fade_time = rl.get_time()
|
|
|
|
def _render(self, _):
|
|
if rl.get_time() - self._fade_time > self.FADE_AFTER_SECONDS:
|
|
self._alpha = 0.0
|
|
alpha = self._alpha_filter.update(self._alpha)
|
|
|
|
# white bar with black border
|
|
rl.draw_rectangle_rounded(self._rect, 1.0, 6, rl.Color(255, 255, 255, int(255 * 0.9 * alpha)))
|
|
rl.draw_rectangle_rounded_lines_ex(self._rect, 1.0, 6, 2, rl.Color(0, 0, 0, int(255 * 0.3 * alpha)))
|
|
|
|
|
|
class NavWidget(Widget, abc.ABC):
|
|
"""
|
|
A full screen widget that supports back navigation by swiping down from the top.
|
|
"""
|
|
BACK_TOUCH_AREA_PERCENTAGE = 0.65
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
# State
|
|
self._drag_start_pos: MousePos | None = None # cleared after certain amount of horizontal movement
|
|
self._dragging_down = False # swiped down enough to trigger dismissing on release
|
|
self._playing_dismiss_animation = False # released and animating away
|
|
self._y_pos_filter = BounceFilter(0.0, 0.1, 1 / gui_app.target_fps, bounce=1)
|
|
|
|
self._back_callback: Callable[[], None] | None = None # persistent callback for user-initiated back navigation
|
|
self._dismiss_callback: Callable[[], None] | None = None # transient callback for programmatic dismiss
|
|
|
|
# TODO: move this state into NavBar
|
|
self._nav_bar = NavBar()
|
|
self._nav_bar_show_time = 0.0
|
|
self._nav_bar_y_filter = FirstOrderFilter(0.0, 0.1, 1 / gui_app.target_fps)
|
|
|
|
def _back_enabled(self) -> bool:
|
|
# Children can override this to block swipe away, like when not at
|
|
# the top of a vertical scroll panel to prevent erroneous swipes
|
|
return True
|
|
|
|
def set_back_callback(self, callback: Callable[[], None]) -> None:
|
|
self._back_callback = callback
|
|
|
|
def _handle_mouse_event(self, mouse_event: MouseEvent) -> None:
|
|
super()._handle_mouse_event(mouse_event)
|
|
|
|
# Don't let touch events change filter state during dismiss animation
|
|
if self._playing_dismiss_animation:
|
|
return
|
|
|
|
if mouse_event.left_pressed:
|
|
# user is able to swipe away if starting near top of screen
|
|
self._y_pos_filter.update_alpha(0.04)
|
|
in_dismiss_area = mouse_event.pos.y < self._rect.height * self.BACK_TOUCH_AREA_PERCENTAGE
|
|
|
|
if in_dismiss_area and self._back_enabled():
|
|
self._drag_start_pos = mouse_event.pos
|
|
|
|
elif mouse_event.left_down:
|
|
if self._drag_start_pos is not None:
|
|
# block swiping away if too much horizontal or upward movement
|
|
# block (lock-in) threshold is higher than start dismissing
|
|
horizontal_movement = abs(mouse_event.pos.x - self._drag_start_pos.x) > BLOCK_SWIPE_AWAY_THRESHOLD
|
|
upward_movement = mouse_event.pos.y - self._drag_start_pos.y < -BLOCK_SWIPE_AWAY_THRESHOLD
|
|
|
|
if not (horizontal_movement or upward_movement):
|
|
# no blocking movement, check if we should start dismissing
|
|
if mouse_event.pos.y - self._drag_start_pos.y > START_DISMISSING_THRESHOLD:
|
|
self._dragging_down = True
|
|
else:
|
|
if not self._dragging_down:
|
|
self._drag_start_pos = None
|
|
|
|
elif mouse_event.left_released:
|
|
# reset rc for either slide up or down animation
|
|
self._y_pos_filter.update_alpha(0.1)
|
|
|
|
# if far enough, trigger back navigation callback
|
|
if self._drag_start_pos is not None:
|
|
if mouse_event.pos.y - self._drag_start_pos.y > SWIPE_AWAY_THRESHOLD:
|
|
self._playing_dismiss_animation = True
|
|
|
|
self._drag_start_pos = None
|
|
self._dragging_down = False
|
|
|
|
def _update_state(self):
|
|
super()._update_state()
|
|
|
|
new_y = 0.0
|
|
|
|
if self._dragging_down:
|
|
self._nav_bar.set_alpha(1.0)
|
|
|
|
# FIXME: disabling this widget on new push_widget still causes this widget to track mouse events without mouse down
|
|
if not self.enabled:
|
|
self._drag_start_pos = None
|
|
|
|
if self._drag_start_pos is not None:
|
|
last_mouse_event = gui_app.last_mouse_event
|
|
# push entire widget as user drags it away
|
|
new_y = max(last_mouse_event.pos.y - self._drag_start_pos.y, 0)
|
|
if new_y < SWIPE_AWAY_THRESHOLD:
|
|
new_y /= 2 # resistance until mouse release would dismiss widget
|
|
|
|
if self._playing_dismiss_animation:
|
|
new_y = self._rect.height + DISMISS_PUSH_OFFSET
|
|
|
|
new_y = round(self._y_pos_filter.update(new_y))
|
|
if abs(new_y) < 1 and self._y_pos_filter.velocity.x == 0.0:
|
|
new_y = self._y_pos_filter.x = 0.0
|
|
|
|
if new_y > self._rect.height + DISMISS_PUSH_OFFSET - 10:
|
|
gui_app.pop_widget()
|
|
|
|
# Only one callback should ever be fired
|
|
if self._dismiss_callback is not None:
|
|
self._dismiss_callback()
|
|
self._dismiss_callback = None
|
|
elif self._back_callback is not None:
|
|
self._back_callback()
|
|
|
|
self._playing_dismiss_animation = False
|
|
self._drag_start_pos = None
|
|
self._dragging_down = False
|
|
|
|
self.set_position(self._rect.x, new_y)
|
|
|
|
def _layout(self):
|
|
# Dim whatever is behind this widget, fading with position (runs after _update_state so position is correct)
|
|
overlay_alpha = int(200 * max(0.0, min(1.0, 1.0 - self._rect.y / self._rect.height))) if self._rect.height > 0 else 0
|
|
rl.draw_rectangle(0, 0, int(self._rect.width), int(self._rect.height), rl.Color(0, 0, 0, overlay_alpha))
|
|
|
|
bounce_height = 20
|
|
rl.draw_rectangle(int(self._rect.x), int(self._rect.y), int(self._rect.width), int(self._rect.height + bounce_height), rl.BLACK)
|
|
|
|
def render(self, rect: rl.Rectangle | None = None) -> bool | int | None:
|
|
ret = super().render(rect)
|
|
|
|
bar_x = self._rect.x + (self._rect.width - self._nav_bar.rect.width) / 2
|
|
nav_bar_delayed = rl.get_time() - self._nav_bar_show_time < 0.4
|
|
# User dragging or dismissing, nav bar follows NavWidget
|
|
if self._drag_start_pos is not None or self._playing_dismiss_animation:
|
|
self._nav_bar_y_filter.x = NAV_BAR_MARGIN + self._y_pos_filter.x
|
|
# Waiting to show
|
|
elif nav_bar_delayed:
|
|
self._nav_bar_y_filter.x = -NAV_BAR_MARGIN - NAV_BAR_HEIGHT
|
|
# Animate back to top
|
|
else:
|
|
self._nav_bar_y_filter.update(NAV_BAR_MARGIN)
|
|
|
|
self._nav_bar.set_position(bar_x, round(self._nav_bar_y_filter.x))
|
|
self._nav_bar.render()
|
|
|
|
return ret
|
|
|
|
@property
|
|
def is_dismissing(self) -> bool:
|
|
return self._dragging_down or self._playing_dismiss_animation
|
|
|
|
def dismiss(self, callback: Callable[[], None] | None = None):
|
|
"""Programmatically trigger the dismiss animation. Calls pop_widget when done, then callback."""
|
|
if not self._playing_dismiss_animation:
|
|
self._playing_dismiss_animation = True
|
|
self._y_pos_filter.update_alpha(DISMISS_ANIMATION_RC)
|
|
self._dismiss_callback = callback
|
|
|
|
def show_event(self):
|
|
super().show_event()
|
|
self._nav_bar.show_event()
|
|
|
|
# Reset state
|
|
self._drag_start_pos = None
|
|
self._dragging_down = False
|
|
self._playing_dismiss_animation = False
|
|
self._dismiss_callback = None
|
|
# Start NavWidget off-screen, no matter how tall it is
|
|
self._y_pos_filter.update_alpha(0.1)
|
|
self._y_pos_filter.x = gui_app.height
|
|
|
|
self._nav_bar_y_filter.x = -NAV_BAR_MARGIN - NAV_BAR_HEIGHT
|
|
self._nav_bar_show_time = rl.get_time()
|