[MouseFox logo]
The MouseFox Project
Join The Community Discord Server
[Discord logo]
Edit on GitHub

kvex.widgets.scroll

Home of XScroll.

 1"""Home of `XScroll`."""
 2
 3from .. import kivy as kv
 4from ..behaviors import XThemed
 5from .widget import XWidget
 6
 7
 8class XScroll(XThemed, XWidget, kv.ScrollView):
 9    """ScrollView."""
10
11    reset_scroll_value = kv.NumericProperty(1, allownone=True)
12
13    def __init__(
14        self,
15        view: kv.Widget,
16        scroll_amount: float = 50,
17        **kwargs,
18    ):
19        """Initialize the class.
20
21        Args:
22            view: A widget to put in the scroll view.
23            scroll_amount: Resolution of scroll in pixels.
24        """
25        kwargs = dict(bar_width="5sp") | kwargs
26        super().__init__(**kwargs)
27        self.scroll_amount = scroll_amount
28        self.scroll_type = ["bars"]
29        self.view = view
30        self.add_widget(view)
31        self.bind(size=self._on_size, on_touch_down=self._on_touch_down)
32        self.view.bind(size=self._on_size)
33        self._on_size()
34
35    def on_subtheme(self, subtheme):
36        """Apply colors."""
37        self.bar_color = subtheme.accent2.rgba
38        self.bar_inactive_color = subtheme.accent1.rgba
39
40    def reset_scroll(self, *, scroll: float = 1):
41        """Reset scroll to the top left."""
42        self.scroll_x = self.scroll_y = scroll
43
44    def _on_size(self, *a):
45        self.do_scroll_x = self.view.width > self.width
46        self.do_scroll_y = self.view.height > self.height
47        if self.reset_scroll_value is not None:
48            self.reset_scroll(scroll=self.reset_scroll_value)
49
50    def _on_touch_down(self, w, m):
51        if m.button not in {"scrollup", "scrolldown"}:
52            return False
53        if not self.collide_point(*m.pos):
54            return False
55        return self.do_scroll(up=m.button != "scrollup")
56
57    def do_scroll(self, count: int = 1, /, *, up: bool = False):
58        """Scroll down or up by count times self.scroll_amount."""
59        if not any((self.do_scroll_x, self.do_scroll_y)):
60            return False
61        dir = 1 if up else -1
62        pixels_x, pixels_y = self.convert_distance_to_scroll(
63            self.scroll_amount * count,
64            self.scroll_amount * count,
65        )
66        self.scroll_x = min(1, max(0, self.scroll_x + pixels_x * dir))
67        self.scroll_y = min(1, max(0, self.scroll_y + pixels_y * dir))
68        return True
69
70
71__all__ = (
72    "XScroll",
73)
class XScroll(kvex.behaviors.XThemed, kvex.widgets.widget.XWidget, kivy.uix.scrollview.ScrollView):
 9class XScroll(XThemed, XWidget, kv.ScrollView):
10    """ScrollView."""
11
12    reset_scroll_value = kv.NumericProperty(1, allownone=True)
13
14    def __init__(
15        self,
16        view: kv.Widget,
17        scroll_amount: float = 50,
18        **kwargs,
19    ):
20        """Initialize the class.
21
22        Args:
23            view: A widget to put in the scroll view.
24            scroll_amount: Resolution of scroll in pixels.
25        """
26        kwargs = dict(bar_width="5sp") | kwargs
27        super().__init__(**kwargs)
28        self.scroll_amount = scroll_amount
29        self.scroll_type = ["bars"]
30        self.view = view
31        self.add_widget(view)
32        self.bind(size=self._on_size, on_touch_down=self._on_touch_down)
33        self.view.bind(size=self._on_size)
34        self._on_size()
35
36    def on_subtheme(self, subtheme):
37        """Apply colors."""
38        self.bar_color = subtheme.accent2.rgba
39        self.bar_inactive_color = subtheme.accent1.rgba
40
41    def reset_scroll(self, *, scroll: float = 1):
42        """Reset scroll to the top left."""
43        self.scroll_x = self.scroll_y = scroll
44
45    def _on_size(self, *a):
46        self.do_scroll_x = self.view.width > self.width
47        self.do_scroll_y = self.view.height > self.height
48        if self.reset_scroll_value is not None:
49            self.reset_scroll(scroll=self.reset_scroll_value)
50
51    def _on_touch_down(self, w, m):
52        if m.button not in {"scrollup", "scrolldown"}:
53            return False
54        if not self.collide_point(*m.pos):
55            return False
56        return self.do_scroll(up=m.button != "scrollup")
57
58    def do_scroll(self, count: int = 1, /, *, up: bool = False):
59        """Scroll down or up by count times self.scroll_amount."""
60        if not any((self.do_scroll_x, self.do_scroll_y)):
61            return False
62        dir = 1 if up else -1
63        pixels_x, pixels_y = self.convert_distance_to_scroll(
64            self.scroll_amount * count,
65            self.scroll_amount * count,
66        )
67        self.scroll_x = min(1, max(0, self.scroll_x + pixels_x * dir))
68        self.scroll_y = min(1, max(0, self.scroll_y + pixels_y * dir))
69        return True

ScrollView.

XScroll(view: kivy.uix.widget.Widget, scroll_amount: float = 50, **kwargs)
14    def __init__(
15        self,
16        view: kv.Widget,
17        scroll_amount: float = 50,
18        **kwargs,
19    ):
20        """Initialize the class.
21
22        Args:
23            view: A widget to put in the scroll view.
24            scroll_amount: Resolution of scroll in pixels.
25        """
26        kwargs = dict(bar_width="5sp") | kwargs
27        super().__init__(**kwargs)
28        self.scroll_amount = scroll_amount
29        self.scroll_type = ["bars"]
30        self.view = view
31        self.add_widget(view)
32        self.bind(size=self._on_size, on_touch_down=self._on_touch_down)
33        self.view.bind(size=self._on_size)
34        self._on_size()

Initialize the class.

Arguments:
  • view: A widget to put in the scroll view.
  • scroll_amount: Resolution of scroll in pixels.
reset_scroll_value

NumericProperty(defaultvalue=0, **kw) Property that represents a numeric value.

It only accepts the int or float numeric data type or a string that can be
converted to a number as shown below. For other numeric types use ObjectProperty
or use errorhandler to convert it to an int/float.

It does not support numpy numbers so they must be manually converted to int/float.
E.g. ``widget.num = np.arange(4)[0]`` will raise an exception. Numpy arrays are not
supported at all, even by ObjectProperty because their comparision does not return
a bool. But if you must use a Kivy property, use a ObjectProperty with ``comparator``
set to ``np.array_equal``. E.g.::

    >>> class A(EventDispatcher):
    ...     data = ObjectProperty(comparator=np.array_equal)
    >>> a = A()
    >>> a.bind(data=print)
    >>> a.data = np.arange(2)
    <__main__.A object at 0x000001C839B50208> [0 1]
    >>> a.data = np.arange(3)
    <__main__.A object at 0x000001C839B50208> [0 1 2]

:Parameters:
    `defaultvalue`: int or float, defaults to 0
        Specifies the default value of the property.

>>> wid = Widget()
>>> wid.x = 42
>>> print(wid.x)
42
>>> wid.x = "plop"
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "properties.pyx", line 93, in kivy.properties.Property.__set__
   File "properties.pyx", line 111, in kivy.properties.Property.set
   File "properties.pyx", line 159, in kivy.properties.NumericProperty.check
 ValueError: NumericProperty accept only int/float

*Changed in version 1.4.1:*
NumericProperty can now accept custom text and tuple value to indicate a
type, like "in", "pt", "px", "cm", "mm", in the format: '10pt' or (10,
'pt').
scroll_type

Sets the type of scrolling to use for the content of the scrollview. Available options are: ['content'], ['bars'], ['bars', 'content'].

+---------------------+------------------------------------------------+ | ['content'] | Content is scrolled by dragging or swiping the | | | content directly. | +---------------------+------------------------------------------------+ | ['bars'] | Content is scrolled by dragging or swiping the | | | scroll bars. | +---------------------+------------------------------------------------+ | ['bars', 'content'] | Content is scrolled by either of the above | | | methods. | +---------------------+------------------------------------------------+

New in version 1.8.0.

scroll_type is an ~kivy.properties.OptionProperty and defaults to ['content'].

def on_subtheme(self, subtheme):
36    def on_subtheme(self, subtheme):
37        """Apply colors."""
38        self.bar_color = subtheme.accent2.rgba
39        self.bar_inactive_color = subtheme.accent1.rgba

Apply colors.

def reset_scroll(self, *, scroll: float = 1):
41    def reset_scroll(self, *, scroll: float = 1):
42        """Reset scroll to the top left."""
43        self.scroll_x = self.scroll_y = scroll

Reset scroll to the top left.

def do_scroll(self, count: int = 1, /, *, up: bool = False):
58    def do_scroll(self, count: int = 1, /, *, up: bool = False):
59        """Scroll down or up by count times self.scroll_amount."""
60        if not any((self.do_scroll_x, self.do_scroll_y)):
61            return False
62        dir = 1 if up else -1
63        pixels_x, pixels_y = self.convert_distance_to_scroll(
64            self.scroll_amount * count,
65            self.scroll_amount * count,
66        )
67        self.scroll_x = min(1, max(0, self.scroll_x + pixels_x * dir))
68        self.scroll_y = min(1, max(0, self.scroll_y + pixels_y * dir))
69        return True

Scroll down or up by count times self.scroll_amount.

Inherited Members
kivy.uix.scrollview.ScrollView
scroll_distance
scroll_wheel_distance
scroll_timeout
scroll_x
scroll_y
do_scroll_x
do_scroll_y
always_overscroll
vbar
hbar
bar_color
bar_inactive_color
bar_width
bar_pos_x
bar_pos_y
bar_pos
bar_margin
effect_cls
effect_x
effect_y
viewport_size
smooth_scroll_end
on__viewport
canvas
on_effect_x
on_effect_y
on_effect_cls
to_local
to_parent
simulate_touch_down
on_motion
on_touch_down
on_scroll_start
on_touch_move
on_scroll_move
on_touch_up
on_scroll_stop
scroll_to
convert_distance_to_scroll
update_from_scroll
add_widget
remove_widget
kivy.uix.widget.Widget
proxy_ref
apply_class_lang_rules
collide_point
collide_widget
on_kv_post
clear_widgets
register_for_motion_event
unregister_for_motion_event
export_to_png
export_as_image
get_root_window
get_parent_window
walk
walk_reverse
to_widget
to_window
get_window_matrix
x
y
width
height
pos
size
get_right
set_right
right
get_top
set_top
top
get_center_x
set_center_x
center_x
get_center_y
set_center_y
center_y
center
cls
children
parent
size_hint_x
size_hint_y
size_hint
pos_hint
size_hint_min_x
size_hint_min_y
size_hint_min
size_hint_max_x
size_hint_max_y
size_hint_max
ids
opacity
on_opacity
get_disabled
set_disabled
inc_disabled
dec_disabled
disabled
motion_filter
kivy._event.EventDispatcher
register_event_type
unregister_event_types
unregister_event_type
is_event_type
bind
unbind
fbind
funbind
unbind_uid
get_property_observers
events
dispatch
dispatch_generic
dispatch_children
setter
getter
property
properties
create_property
apply_property