[core/decorators] Simplify and test scrolling

This commit is contained in:
Tobias Witek 2020-02-29 14:05:02 +01:00
parent b99ac07ef0
commit cb3482ae27
2 changed files with 21 additions and 14 deletions

View file

@ -11,26 +11,27 @@ def scrollable(func):
if width < 0 or len(text) <= width: if width < 0 or len(text) <= width:
return text return text
start = widget.get('scrolling.start', 0)
bounce = util.format.asbool(module.parameter('scrolling.bounce', True)) bounce = util.format.asbool(module.parameter('scrolling.bounce', True))
scroll_speed = util.format.asint(module.parameter('scrolling.speed', 1)) scroll_speed = util.format.asint(module.parameter('scrolling.speed', 1))
start = widget.get('scrolling.start', 0)
if start + width > len(text):
if bounce:
widget.set('scrolling.direction', 'left')
start -= scroll_speed*2
else:
start = 0
elif start < 0:
if bounce:
widget.set('scrolling.direction', 'right')
direction = widget.get('scrolling.direction', 'right') direction = widget.get('scrolling.direction', 'right')
if direction == 'left': if direction == 'left':
scroll_speed = -scroll_speed scroll_speed = -scroll_speed
widget.set('scrolling.start', start + scroll_speed) if start + scroll_speed <= 0: # bounce back
text = text[start:width+start] widget.set('scrolling.direction', 'right')
return text next_start = start + scroll_speed
if next_start + width > len(text):
if not bounce:
next_start = 0
else:
next_start = start - scroll_speed
widget.set('scrolling.direction', 'left')
widget.set('scrolling.start', next_start)
return text[start:start+width]
return wrapper return wrapper
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -44,6 +44,10 @@ class config(unittest.TestCase):
self.assertEqual('cd', self.module.get(self.widget)) self.assertEqual('cd', self.module.get(self.widget))
self.assertEqual('bc', self.module.get(self.widget)) self.assertEqual('bc', self.module.get(self.widget))
self.assertEqual('ab', self.module.get(self.widget)) self.assertEqual('ab', self.module.get(self.widget))
self.assertEqual('bc', self.module.get(self.widget))
self.assertEqual('cd', self.module.get(self.widget))
self.assertEqual('bc', self.module.get(self.widget))
self.assertEqual('ab', self.module.get(self.widget))
def test_nobounce(self): def test_nobounce(self):
self.module.set('scrolling.bounce', False) self.module.set('scrolling.bounce', False)
@ -53,5 +57,7 @@ class config(unittest.TestCase):
self.assertEqual('bc', self.module.get(self.widget)) self.assertEqual('bc', self.module.get(self.widget))
self.assertEqual('cd', self.module.get(self.widget)) self.assertEqual('cd', self.module.get(self.widget))
self.assertEqual('ab', self.module.get(self.widget)) self.assertEqual('ab', self.module.get(self.widget))
self.assertEqual('bc', self.module.get(self.widget))
self.assertEqual('cd', self.module.get(self.widget))
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4