mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-17 01:52:02 +00:00
trezor/ui/swipe: rework thresholds
This commit is contained in:
parent
7bbfba1ac0
commit
a09e64090c
@ -3,10 +3,6 @@ from micropython import const
|
|||||||
from trezor import io, ui
|
from trezor import io, ui
|
||||||
from . import contains, rotate
|
from . import contains, rotate
|
||||||
|
|
||||||
_SWIPE_DISTANCE_THRESHOLD = const(20) # Min pixels in the primary direction
|
|
||||||
_SWIPE_VELOCITY_THRESHOLD = const(200) # Min pixels per second
|
|
||||||
_SWIPE_RATIO_THRESHOLD = const(30) # Max ratio or directions in %
|
|
||||||
|
|
||||||
SWIPE_UP = const(0x01)
|
SWIPE_UP = const(0x01)
|
||||||
SWIPE_DOWN = const(0x02)
|
SWIPE_DOWN = const(0x02)
|
||||||
SWIPE_LEFT = const(0x04)
|
SWIPE_LEFT = const(0x04)
|
||||||
@ -15,6 +11,8 @@ SWIPE_VERTICAL = const(SWIPE_UP | SWIPE_DOWN)
|
|||||||
SWIPE_HORIZONTAL = const(SWIPE_LEFT | SWIPE_RIGHT)
|
SWIPE_HORIZONTAL = const(SWIPE_LEFT | SWIPE_RIGHT)
|
||||||
SWIPE_ALL = const(SWIPE_VERTICAL | SWIPE_HORIZONTAL)
|
SWIPE_ALL = const(SWIPE_VERTICAL | SWIPE_HORIZONTAL)
|
||||||
|
|
||||||
|
_SWIPE_DISTANCE = const(120)
|
||||||
|
|
||||||
|
|
||||||
def degrees(swipe: int) -> int:
|
def degrees(swipe: int) -> int:
|
||||||
if swipe == SWIPE_UP:
|
if swipe == SWIPE_UP:
|
||||||
@ -29,13 +27,12 @@ def degrees(swipe: int) -> int:
|
|||||||
|
|
||||||
class Swipe(ui.Widget):
|
class Swipe(ui.Widget):
|
||||||
|
|
||||||
def __init__(self, area=None, absolute=False, directions=SWIPE_ALL):
|
def __init__(self, area=None, absolute=False, directions=SWIPE_ALL, treshold=80):
|
||||||
self.area = area or (0, 0, ui.SCREEN, ui.SCREEN)
|
self.area = area or (0, 0, ui.SCREEN, ui.SCREEN)
|
||||||
self.absolute = absolute
|
self.absolute = absolute
|
||||||
self.directions = directions
|
self.directions = directions
|
||||||
|
self.treshold = treshold
|
||||||
self.start_pos = None
|
self.start_pos = None
|
||||||
self.start_time = 0
|
|
||||||
self.light = None
|
|
||||||
self.light_origin = None
|
self.light_origin = None
|
||||||
self.light_target = ui.BACKLIGHT_NONE
|
self.light_target = ui.BACKLIGHT_NONE
|
||||||
|
|
||||||
@ -44,12 +41,9 @@ class Swipe(ui.Widget):
|
|||||||
if not self.absolute:
|
if not self.absolute:
|
||||||
pos = rotate(pos)
|
pos = rotate(pos)
|
||||||
|
|
||||||
temp_time = utime.ticks_ms() / 1000
|
|
||||||
|
|
||||||
if event == io.TOUCH_MOVE and self.start_pos is not None:
|
if event == io.TOUCH_MOVE and self.start_pos is not None:
|
||||||
pdx = pos[0] - self.start_pos[0]
|
pdx = pos[0] - self.start_pos[0]
|
||||||
pdy = pos[1] - self.start_pos[1]
|
pdy = pos[1] - self.start_pos[1]
|
||||||
td = temp_time - self.start_time
|
|
||||||
|
|
||||||
pdxa = abs(pdx)
|
pdxa = abs(pdx)
|
||||||
pdya = abs(pdy)
|
pdya = abs(pdy)
|
||||||
@ -59,51 +53,40 @@ class Swipe(ui.Widget):
|
|||||||
ui.display.backlight(ui.lerpi(
|
ui.display.backlight(ui.lerpi(
|
||||||
self.light_origin,
|
self.light_origin,
|
||||||
self.light_target,
|
self.light_target,
|
||||||
pdxa / 120 if pdxa < 120 else 1))
|
pdxa / _SWIPE_DISTANCE if pdxa < _SWIPE_DISTANCE else 1))
|
||||||
elif pdxa < pdya and self.directions & SWIPE_VERTICAL:
|
elif pdxa < pdya and self.directions & SWIPE_VERTICAL:
|
||||||
# Vertical direction
|
# Vertical direction
|
||||||
if (pdy > 0 and self.directions & SWIPE_DOWN) or (pdy < 0 and self.directions & SWIPE_UP):
|
if (pdy > 0 and self.directions & SWIPE_DOWN) or (pdy < 0 and self.directions & SWIPE_UP):
|
||||||
ui.display.backlight(ui.lerpi(
|
ui.display.backlight(ui.lerpi(
|
||||||
self.light_origin,
|
self.light_origin,
|
||||||
self.light_target,
|
self.light_target,
|
||||||
pdya / 120 if pdya < 120 else 1))
|
pdya / _SWIPE_DISTANCE if pdya < _SWIPE_DISTANCE else 1))
|
||||||
|
|
||||||
elif event == io.TOUCH_START and contains(self.area, pos):
|
elif event == io.TOUCH_START and contains(self.area, pos):
|
||||||
self.start_time = temp_time
|
|
||||||
self.start_pos = pos
|
self.start_pos = pos
|
||||||
self.light_origin = ui.BACKLIGHT_NORMAL
|
self.light_origin = ui.BACKLIGHT_NORMAL
|
||||||
|
|
||||||
elif event == io.TOUCH_END and self.start_pos is not None:
|
elif event == io.TOUCH_END and self.start_pos is not None:
|
||||||
td = temp_time - self.start_time
|
|
||||||
pdx = pos[0] - self.start_pos[0]
|
pdx = pos[0] - self.start_pos[0]
|
||||||
pdy = pos[1] - self.start_pos[1]
|
pdy = pos[1] - self.start_pos[1]
|
||||||
pdxa = abs(pdx)
|
pdxa = abs(pdx)
|
||||||
pdya = abs(pdy)
|
pdya = abs(pdy)
|
||||||
if pdxa > pdya and self.directions & SWIPE_HORIZONTAL:
|
if pdxa > pdya and self.directions & SWIPE_HORIZONTAL:
|
||||||
# Horizontal direction
|
# Horizontal direction
|
||||||
velxa = abs(pdx / td)
|
ratio = pdxa / _SWIPE_DISTANCE if pdxa < _SWIPE_DISTANCE else 1
|
||||||
ratio = int(pdya / pdxa * 100) if pdxa > 0 else 100
|
if ratio * 100 >= self.treshold:
|
||||||
if (velxa >= _SWIPE_VELOCITY_THRESHOLD and
|
|
||||||
pdxa >= _SWIPE_DISTANCE_THRESHOLD and
|
|
||||||
ratio <= _SWIPE_RATIO_THRESHOLD):
|
|
||||||
if pdx > 0 and self.directions & SWIPE_RIGHT:
|
if pdx > 0 and self.directions & SWIPE_RIGHT:
|
||||||
return SWIPE_RIGHT
|
return SWIPE_RIGHT
|
||||||
elif pdx < 0 and self.directions & SWIPE_LEFT:
|
elif pdx < 0 and self.directions & SWIPE_LEFT:
|
||||||
return SWIPE_LEFT
|
return SWIPE_LEFT
|
||||||
elif pdxa < pdya and self.directions & SWIPE_VERTICAL:
|
elif pdxa < pdya and self.directions & SWIPE_VERTICAL:
|
||||||
# Vertical direction
|
# Vertical direction
|
||||||
velya = abs(pdy / td)
|
ratio = pdya / _SWIPE_DISTANCE if pdya < _SWIPE_DISTANCE else 1
|
||||||
ratio = int(pdxa / pdya * 100) if pdya > 0 else 100
|
if ratio * 100 >= self.treshold:
|
||||||
if (velya >= _SWIPE_VELOCITY_THRESHOLD and
|
|
||||||
pdya >= _SWIPE_DISTANCE_THRESHOLD and
|
|
||||||
ratio <= _SWIPE_RATIO_THRESHOLD):
|
|
||||||
if pdy > 0 and self.directions & SWIPE_DOWN:
|
if pdy > 0 and self.directions & SWIPE_DOWN:
|
||||||
self.light = None
|
|
||||||
return SWIPE_DOWN
|
return SWIPE_DOWN
|
||||||
elif pdy < 0 and self.directions & SWIPE_UP:
|
elif pdy < 0 and self.directions & SWIPE_UP:
|
||||||
self.light = None
|
|
||||||
return SWIPE_UP
|
return SWIPE_UP
|
||||||
# No swipe, reset the state
|
# No swipe, reset the state
|
||||||
self.start_pos = None
|
self.start_pos = None
|
||||||
self.start_time = 0
|
|
||||||
ui.display.backlight(self.light_origin)
|
ui.display.backlight(self.light_origin)
|
||||||
|
Loading…
Reference in New Issue
Block a user