[core/decorators] Simplify and test scrolling
This commit is contained in:
parent
b99ac07ef0
commit
cb3482ae27
2 changed files with 21 additions and 14 deletions
|
@ -11,26 +11,27 @@ def scrollable(func):
|
|||
if width < 0 or len(text) <= width:
|
||||
return text
|
||||
|
||||
start = widget.get('scrolling.start', 0)
|
||||
bounce = util.format.asbool(module.parameter('scrolling.bounce', True))
|
||||
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')
|
||||
|
||||
if direction == 'left':
|
||||
scroll_speed = -scroll_speed
|
||||
widget.set('scrolling.start', start + scroll_speed)
|
||||
text = text[start:width+start]
|
||||
if start + scroll_speed <= 0: # bounce back
|
||||
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
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -44,6 +44,10 @@ class config(unittest.TestCase):
|
|||
self.assertEqual('cd', self.module.get(self.widget))
|
||||
self.assertEqual('bc', 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):
|
||||
self.module.set('scrolling.bounce', False)
|
||||
|
@ -53,5 +57,7 @@ class config(unittest.TestCase):
|
|||
self.assertEqual('bc', self.module.get(self.widget))
|
||||
self.assertEqual('cd', 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
|
||||
|
|
Loading…
Reference in a new issue