lib: Create a Counter object

With increment() and decrement() functions that can be used to change
the GtkAdjustment's value. These functions return None if the value did
not change.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-07-21 11:43:37 -04:00
parent fc82beca3d
commit b75d43a304
2 changed files with 42 additions and 0 deletions

22
lib/counter.py Normal file
View File

@ -0,0 +1,22 @@
# Copyright 2021 (c) Anna Schumaker.
from gi.repository import Gtk
class Counter(Gtk.Adjustment):
def __init__(self, min, max):
Gtk.Adjustment.__init__(self)
self.configure(value=min, lower=min, upper=max + 1, step_increment=1,
page_increment=1, page_size=1)
def __change_value__(self, n):
value = self.get_value()
self.set_value(value + n)
if self.get_value() == value:
return None
return self.get_value()
def increment(self):
return self.__change_value__(1)
def decrement(self):
return self.__change_value__(-1)

20
lib/test_counter.py Normal file
View File

@ -0,0 +1,20 @@
# Copyright 2021 (c) Anna Schumaker.
from . import counter
from gi.repository import Gtk
import unittest
class TestCounter(unittest.TestCase):
def test_counter(self):
c = counter.Counter(1, 10)
self.assertIsInstance(c, Gtk.Adjustment)
self.assertEqual(c.get_lower(), 1)
self.assertEqual(c.get_upper(), 11)
self.assertEqual(c.get_value(), 1)
for i in [ 2, 3, 4, 5, 6, 7, 8, 9, 10, None ]:
self.assertEqual(c.increment(), i)
for i in [ 9, 8, 7, 6, 5, 4, 3, 2, 1, None ]:
self.assertEqual(c.decrement(), i)