Skip to content

Commit

Permalink
Fix single frame GUI increment (#1254)
Browse files Browse the repository at this point in the history
* Fix frame increment for single frame videos

* Add test and lint
  • Loading branch information
roomrys authored Apr 4, 2023
1 parent b305eda commit 11a71ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
21 changes: 12 additions & 9 deletions sleap/gui/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"""

import inspect
from typing import Any, Callable, List, Union
from typing import Any, Callable, List, Union, Optional


GSVarType = str
Expand Down Expand Up @@ -83,28 +83,31 @@ def toggle(self, key: GSVarType, default: bool = False):
"""Toggle boolean value for specified key."""
self[key] = not self.get(key, default=default)

def increment(self, key: GSVarType, step: int = 1, mod: int = 1, default: int = 0):
def increment(
self, key: GSVarType, step: int = 1, mod: Optional[int] = None, default: int = 0
):
"""Increment numeric value for specified key.
Args:
key: The key.
step: What to add to current value.
mod: Wrap value (i.e., apply modulus) if not 1.
mod: Wrap value (i.e., apply modulus) if not None.
default: Set value to this if there's no current value for key.
Returns:
None.
"""
if key not in self._state_vars:
self[key] = default
else:
new_value = self.get(key) + step
return

new_value = self.get(key) + step

# take modulo of value if mod arg is not 1
if mod != 1:
new_value %= mod
# Wrap the value if it's out of bounds.
if mod is not None:
new_value %= mod

self[key] = new_value
self[key] = new_value

def increment_in_list(
self, key: GSVarType, value_list: list, reverse: bool = False
Expand Down
7 changes: 6 additions & 1 deletion tests/gui/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ def set_y_from_val_param_callback(x):
assert times_x_changed == 4
assert state["x"] == 2

# Test incrementing value with modulus of 1
state.increment("x", mod=1)
assert times_x_changed == 5
assert state["x"] == 0

# test emitting callbacks without changing value
state.emit("x")
assert times_x_changed == 5
assert times_x_changed == 6


def test_gui_state_bool():
Expand Down

0 comments on commit 11a71ff

Please sign in to comment.