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:
parent
fc82beca3d
commit
b75d43a304
|
@ -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)
|
|
@ -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)
|
Loading…
Reference in New Issue