Skip to content
Snippets Groups Projects
Commit a4681f0a authored by dos's avatar dos Committed by ave
Browse files

py,st3m: ScrollController: Improve safety checks around target position

Previously, target position could become an illegal value after calling
various methods until the next think call, so apps couldn't trust that
the received target position will be correct.

Make target position handling more robust and do boundary checks right
away whenever the value changes.
parent 20b61746
Branches
Tags
No related merge requests found
Pipeline #8091 skipped
...@@ -49,18 +49,25 @@ class ScrollController(st3m.Responder): ...@@ -49,18 +49,25 @@ class ScrollController(st3m.Responder):
if count < 0: if count < 0:
count = 0 count = 0
self._nitems = count self._nitems = count
if self._target_position >= self._nitems:
def set_position(self, position: int) -> None: self._target_position = self._nitems - 1
"""
Immediately set a position without animating the transition.
"""
self._target_position = self._current_position = position
def scroll_to(self, position: int) -> None: def scroll_to(self, position: int) -> None:
""" """
Scroll to specified position. Scroll to specified position.
""" """
self._target_position = position self._target_position = position
if self._target_position < 0:
self._target_position = 0
if self._target_position >= self._nitems:
self._target_position = self._nitems - 1
def set_position(self, position: int) -> None:
"""
Immediately set a position without animating the transition.
"""
self.scroll_to(position)
self._current_position = self._target_position
def scroll_left(self) -> None: def scroll_left(self) -> None:
""" """
...@@ -69,6 +76,8 @@ class ScrollController(st3m.Responder): ...@@ -69,6 +76,8 @@ class ScrollController(st3m.Responder):
""" """
self._target_position -= 1 self._target_position -= 1
self._velocity = -10 self._velocity = -10
if self._target_position < 0:
self._target_position = 0
def scroll_right(self) -> None: def scroll_right(self) -> None:
""" """
...@@ -77,6 +86,8 @@ class ScrollController(st3m.Responder): ...@@ -77,6 +86,8 @@ class ScrollController(st3m.Responder):
""" """
self._target_position += 1 self._target_position += 1
self._velocity = 10 self._velocity = 10
if self._target_position >= self._nitems:
self._target_position = self._nitems - 1
def think(self, ins: InputState, delta_ms: int) -> None: def think(self, ins: InputState, delta_ms: int) -> None:
if self._nitems == 0: if self._nitems == 0:
...@@ -84,10 +95,6 @@ class ScrollController(st3m.Responder): ...@@ -84,10 +95,6 @@ class ScrollController(st3m.Responder):
self._current_position = 0 self._current_position = 0
self._velocity = 0 self._velocity = 0
return return
if self._target_position < 0:
self._target_position = 0
if self._target_position >= self._nitems:
self._target_position = self._nitems - 1
self._physics_step(min(delta_ms, 100) / 1000.0) self._physics_step(min(delta_ms, 100) / 1000.0)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment