2
# -*- coding: utf-8 -*-
6
__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
7
__licenses__ = ["LICENSE.LGPL"]
8
__description__ = "Scroll area widget"
14
from container import Container
15
from image import Image
17
class ScrollArea(Container):
19
A scroll area offers a scrollable container area which can be horizontal,
20
vertical, or both. It supports both standard and touch based scrolling.
24
def __init__(self, area_height=1.0):
25
""" Initialize scroll area """
27
super(ScrollArea, self).__init__()
29
self.area_height = area_height
31
self.set_reactive(True)
32
self.connect('scroll-event', self.on_scroll_event)
35
self._init_animations()
37
self._update_style(self.style)
39
def _init_elements(self):
40
""" Initializes graphical elements """
42
self._background_image = Image()
43
self.add(self._background_image)
45
self._scroll_image = Image()
46
self.add(self._scroll_image)
47
self._scroll_image.natural_height = self.area_height
49
def _init_animations(self):
53
def _update_style(self, props=None):
56
super(ScrollArea, self)._update_style(props)
58
for key, value in props:
59
if key == 'background-image':
60
self.background_image = value
61
self._background_image.set_source(self.background_image)
62
self._background_image._update_layout()
63
elif key == 'scroll-image':
64
self.scroll_image = value
65
self._scroll_image.set_source(self.scroll_image)
66
self._scroll_image._update_layout()
68
def _update_layout(self):
69
""" Updates layout """
71
super(ScrollArea, self)._update_layout()
73
self._background_image._update_layout()
74
self._scroll_image._update_layout()
76
def _update_layout(self):
77
""" Updates layout """
79
super(ScrollArea, self)._update_layout()
81
for child in self.get_children():
82
child._update_layout()
88
self._scroll_image.get_y() - self._scroll_image.get_height() * 0.2
89
if not self._timeline or not self._timeline.is_playing():
90
if target_y < self.get_parent().get_height() - self._scroll_image.get_height():
91
self._timeline = clutter.Timeline(fps=60, duration=200)
92
alpha = clutter.Alpha(self._timeline, clutter.sine_inc_func)
93
self._behaviour = clutter.BehaviourPath(alpha)
94
self._behaviour.append_knots(
95
(0, self.get_parent().get_height() - self._scroll_image.get_height()),
97
(0, self.get_parent().get_height() - self._scroll_image.get_height())
100
self._timeline = clutter.Timeline(fps=60, duration=100)
101
alpha = clutter.Alpha(self._timeline, clutter.smoothstep_inc_func)
102
self._behaviour = clutter.BehaviourPath(alpha)
103
self._behaviour.append_knots(
104
(0, self._scroll_image.get_y()),
107
self._behaviour.apply(self._scroll_image)
108
self._timeline.start()
111
def _scroll_down(self):
115
self._scroll_image.get_y() + self._scroll_image.get_height() * 0.2
117
if not self._timeline or not self._timeline.is_playing():
119
self._timeline = clutter.Timeline(fps=60, duration=200)
120
alpha = clutter.Alpha(self._timeline, clutter.sine_inc_func)
121
self._behaviour = clutter.BehaviourPath(alpha)
122
self._behaviour.append_knots(
128
self._timeline = clutter.Timeline(fps=60, duration=100)
129
alpha = clutter.Alpha(self._timeline, clutter.smoothstep_inc_func)
130
self._behaviour = clutter.BehaviourPath(alpha)
131
self._behaviour.append_knots(
132
(0, self._scroll_image.get_y()),
135
self._behaviour.apply(self._scroll_image)
136
self._timeline.start()
138
def pack(self, widget):
139
""" Packs a widget in the scroll area """
141
self._scroll_image.add(widget)
143
self._update_layout()
145
def unpack(self, widget):
146
""" Unpacks a widget in the scroll widget """
148
self._scroll_image.remove(widget)
150
self._update_layout()
152
def on_scroll_event(self, widget, event):
155
if event.direction == clutter.SCROLL_UP:
157
elif event.direction == clutter.SCROLL_DOWN:
159
elif event.direction == clutter.SCROLL_LEFT:
161
elif event.direction == clutter.SCROLL_RIGHT: