-
Notifications
You must be signed in to change notification settings - Fork 1
/
scroller.py
179 lines (142 loc) · 5.88 KB
/
scroller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from weakref import ref
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.stencilview import StencilLayout
from kivy.effects.dampedscroll import DampedScrollEffect
from kivy.properties import AliasProperty, ListProperty, NumericProperty, ObjectProperty, OptionProperty
class ScrollerEffect(DampedScrollEffect):
min_velocity = NumericProperty(10)
_parent = ObjectProperty(None)
max = NumericProperty(0)
def _get_target_widget(self):
if self._parent:
return self._parent._viewport()
target_widget = AliasProperty(_get_target_widget, None)
def _get_min(self):
if self.target_widget:
return -(self.target_widget.size[1] - self._parent.height)
else:
return 0
min = AliasProperty(_get_min, None)
def on_scroll(self, instance, value):
vp = instance.target_widget
if vp:
parent = instance._parent
sh = vp.height - parent.height
if sh >= 1:
sy = value/float(sh)
parent.scroll_y = -sy
if ((not instance.is_manual) and ((abs(instance.velocity) <= instance.min_velocity) or (not value))):
parent.mode = 'normal'
def cancel(self):
self.is_manual = False
self.velocity = 0
self._parent.mode = 'normal'
class Scroller(StencilLayout):
scroll_distance = NumericProperty('10dp')
scroll_y = NumericProperty(1.0)
bar_color = ListProperty([0.7, 0.7, 0.7, 0.9])
bar_width = NumericProperty('2dp')
bar_margin = NumericProperty(0)
_viewport = ObjectProperty(lambda : None)
bar_alpha = NumericProperty(1.0)
mode = OptionProperty('normal', options=('down', 'normal', 'scrolling'))
def _get_vbar(self):
# must return (y, height) in %
# calculate the viewport size / Scroller size %
ret = (0, 1.)
if self._viewport():
vh = self._viewport().height
h = self.height
if vh > h:
ph = max(0.01, h / float(vh))
sy = min(1.0, max(0.0, self.scroll_y))
py = (1. - ph) * sy
ret = (py, ph)
return ret
vbar = AliasProperty(_get_vbar, None, bind=('scroll_y', '_viewport'))
def __init__(self, **kwargs):
self.effect_y = ScrollerEffect(_parent=self.proxy_ref, round_value=False)
super(Scroller, self).__init__(**kwargs)
self.bind(scroll_y=self._trigger_layout)
def do_layout(self, *args):
if 1 not in self.size:
vp = self._viewport()
if vp:
x, y = self.pos
w, h = self.size
vp.w, vp.x = w, x
if vp.height > self.height:
sh = vp.height - h
vp.y = y - self.scroll_y * sh
else:
vp.y = (y + h) - vp.height
def on_height(self, instance, *args):
self.effect_y.value = self.effect_y.min * self.scroll_y
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
touch.grab(self)
self.effect_y.start(touch.y)
if self.mode == 'normal':
self.mode = 'down'
return super(Scroller, self).on_touch_down(touch)
elif self.mode == 'scrolling':
return True
def on_touch_move(self, touch):
if touch.grab_current is self:
if self.mode == 'down':
ret = super(Scroller, self).on_touch_move(touch)
if ret:
touch.ungrab(self)
self.effect_y.cancel()
return True
elif ((abs(touch.dy) > self.scroll_distance) and (self._viewport().height > self.height)):
self.mode = 'scrolling'
l = len(touch.grab_list)
if l > 1:
NoneType = type(None)
for i, j in enumerate(touch.grab_list[:]):
item = j()
if type(item) not in (NoneType, Scroller):
touch.ungrab(item)
item.cancel()
touch.grab_list.insert(i, lambda : None)
del item, j, NoneType
if self.mode == 'scrolling':
self.effect_y.update(touch.y)
return True
def on_touch_up(self, touch):
if touch.grab_current is self:
touch.ungrab(self)
if self.mode == 'down':
self.effect_y.cancel()
elif self.mode == 'scrolling':
self.effect_y.stop(touch.y)
self.effect_y.on_scroll(self.effect_y, self.effect_y.scroll)
return True
return super(Scroller, self).on_touch_up(touch)
def add_widget(self, widget, index=0):
if self._viewport():
raise Exception('Scroller accept only one widget')
super(Scroller, self).add_widget(widget, index)
widget.unbind(pos=self._trigger_layout,
pos_hint=self._trigger_layout)
widget.bind(height=self.on_height)
self._viewport = ref(widget)
del widget
self._trigger_layout()
def remove_widget(self, widget):
super(Scroller, self).remove_widget(widget)
if widget is self._viewport():
self._viewport = lambda : None
def on_scroll_y(self, instance, value):
self.parent.scroll_y = (1 - min(1, max(value, 0)))
Builder.load_string("""
<Scroller>:
canvas.after:
Color:
rgba: self.bar_color[:3] + [self.bar_color[3] * self.bar_alpha]
Rectangle:
pos: self.right - self.bar_width - self.bar_margin, self.y + self.height * self.vbar[0]
size: self.bar_width, self.height * self.vbar[1]
""")