Updated kivymd

This commit is contained in:
Mark Qvist 2022-10-02 17:16:59 +02:00
parent c6df8b851b
commit abcf173cc8
160 changed files with 11617 additions and 6545 deletions

View file

@ -1,8 +1,9 @@
#:import images_path kivymd.images_path
#:import Thumb kivymd.uix.selectioncontrol.Thumb
#:import get_color_from_hex kivy.utils.get_color_from_hex
#:import colors kivymd.color_definitions.colors
<HintBoxContainer@MDCard+FakeRectangularElevationBehavior>
<HintBoxContainer@MDCard>
<MDSlider>
@ -10,9 +11,32 @@
Clear
Color:
rgba:
self._track_color_disabled if self.disabled \
else (self._track_color_active if self.active \
else self._track_color_normal)
( \
self.track_color_disabled \
if self.track_color_disabled else \
self.theme_cls.disabled_hint_text_color) \
if self.disabled else \
( \
( \
self.track_color_active \
if self.track_color_active else \
( \
get_color_from_hex(colors["Gray"]["400"]) \
if app.theme_cls.theme_style == "Light" else \
(1, 1, 1, .3) \
) \
) \
if self.active else \
( \
self.track_color_inactive \
if self.track_color_inactive else \
( \
self.theme_cls.disabled_hint_text_color \
if app.theme_cls.theme_style == "Light" else \
get_color_from_hex(colors["Gray"]["800"]) \
) \
) \
)
Rectangle:
size:
(self.width - self.padding * 2 - self._offset[0], dp(4)) if \
@ -26,10 +50,16 @@
# If 0 draw circle
Color:
rgba:
(0, 0, 0, 0) if not self._is_off \
else (self._track_color_disabled if self.disabled \
else (self._track_color_active \
if self.active else self._track_color_normal))
(0, 0, 0, 0) if not self._is_off else \
( \
self.track_color_disabled \
if self.disabled and self.track_color_disabled else \
( \
self.theme_cls.disabled_hint_text_color \
if app.theme_cls.theme_style == "Light" else \
get_color_from_hex(colors["Gray"]["800"]) \
) \
)
Line:
width: 2
circle:
@ -41,55 +71,86 @@
Color:
rgba:
(0, 0, 0, 0) if self._is_off \
else (self.color if not self.disabled \
else self._track_color_disabled)
else \
( \
( \
self.color if self.color else \
app.theme_cls.primary_color \
) \
if not self.disabled else \
( \
self.track_color_disabled \
if self.track_color_disabled else \
( \
(0, 0, 0, .26) \
if app.theme_cls.theme_style == "Light" else (1, 1, 1, .3) \
) \
) \
)
Rectangle:
size:
((self.width - self.padding * 2) * self.value_normalized, sp(4)) \
if root.orientation == "horizontal" \
else \
if root.orientation == "horizontal" else \
(sp(4), (self.height - self.padding * 2) * self.value_normalized)
pos:
(self.x + self.padding, self.center_y - dp(4)) \
if self.orientation == "horizontal" \
else (self.center_x - dp(4), self.y + self.padding)
if self.orientation == "horizontal" else \
(self.center_x - dp(4), self.y + self.padding)
Thumb:
id: thumb
size_hint: None, None
size:
(dp(12), dp(12)) \
if root.disabled \
else \
((dp(24), dp(24)) \
if root.active \
else \
(dp(12), dp(12)) if root.disabled else ((dp(24), dp(24)) \
if root.active else \
(dp(16), dp(16)))
pos:
(root.value_pos[0] - dp(8), root.center_y - thumb.height / 2 - dp(2)) \
if root.orientation == "horizontal" \
else (root.center_x - thumb.width / 2 - dp(2), \
root.value_pos[1] - dp(8))
color:
(0, 0, 0, 0) if root._is_off else (root._track_color_disabled \
if root.disabled else root.color)
elevation: 0 if root._is_off else (4 if root.active else 2)
md_bg_color:
(0, 0, 0, 0) if root._is_off else \
( \
( \
root.thumb_color_disabled \
if root.thumb_color_disabled else \
get_color_from_hex(colors["Gray"]["800"]) \
) \
if root.disabled else \
( \
(root.thumb_color_active \
if root.thumb_color_active else \
root.theme_cls.primary_color \
) \
if root.active else \
( \
root.thumb_color_inactive \
if root.thumb_color_inactive else \
root.theme_cls.primary_color \
) \
) \
)
elevation: 0 if root._is_off else (3 if root.active else 1)
HintBoxContainer:
id: hint_box
size_hint: None, None
md_bg_color: root.hint_bg_color
elevation: 0
md_bg_color: root.hint_bg_color if root.hint_bg_color else [0, 0, 0, 0]
elevation: 1.5
opacity: 1 if root.active else 0
radius: root.hint_radius
padding: "6dp", "6dp", "6dp", "8dp"
shadow_color:
([0, 0, 0, 0.6] if root.hint_bg_color else [0, 0, 0, 0]) \
if root.active else \
[0, 0, 0, 0]
size:
lbl_value.width + self.padding[0] * 2, \
lbl_value.height + self.padding[0]
pos:
(root.value_pos[0] - dp(9), root.center_y - hint_box.height / 2 + dp(30)) \
if root.orientation == "horizontal" \
else \
if root.orientation == "horizontal" else \
(root.center_x - hint_box.width / 2 + dp(30), root.value_pos[1] - dp(8))
MDLabel:
@ -101,16 +162,8 @@
adaptive_size: True
pos_hint: {"center_x": .5, "center_y": .5}
text_color:
( \
root.color \
if root.active \
else (0, 0, 0, 0) \
) \
if not root.hint_text_color \
else \
root.hint_text_color
app.theme_cls.primary_color \
if not root.hint_text_color else root.hint_text_color
text:
str(root.value) \
if isinstance(root.step, float) \
else \
str(int(root.value))
if isinstance(root.step, float) else str(int(root.value))

View file

@ -10,70 +10,13 @@ Components/Slider
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slider.png
:align: center
With value hint
---------------
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen
MDSlider:
min: 0
max: 100
value: 40
'''
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
Test().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slider-1.gif
:align: center
Without value hint
------------------
.. code-block:: kv
MDSlider:
min: 0
max: 100
value: 40
hint: False
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slider-2.gif
:align: center
Without custom color
--------------------
.. code-block:: kv
MDSlider:
min: 0
max: 100
value: 40
hint: False
color: app.theme_cls.accent_color
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slider-3.png
:align: center
"""
__all__ = ("MDSlider",)
import os
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import (
@ -83,10 +26,8 @@ from kivy.properties import (
VariableListProperty,
)
from kivy.uix.slider import Slider
from kivy.utils import get_color_from_hex
from kivymd import uix_path
from kivymd.color_definitions import colors
from kivymd.theming import ThemableBehavior
with open(
@ -96,6 +37,11 @@ with open(
class MDSlider(ThemableBehavior, Slider):
"""
Class for creating a Slider widget. See in the
:class:`~kivy.uix.slider.Slider` class documentation.
"""
active = BooleanProperty(False)
"""
If the slider is clicked.
@ -104,17 +50,50 @@ class MDSlider(ThemableBehavior, Slider):
and defaults to `False`.
"""
color = ColorProperty(None)
"""
Color slider.
.. code-block:: kv
MDSlider
color: "red"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-color.png
:align: center
:attr:`color` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""
hint = BooleanProperty(True)
"""
If True, then the current value is displayed above the slider.
.. code-block:: kv
MDSlider
hint: True
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-hint.png
:align: center
:attr:`hint` is an :class:`~kivy.properties.BooleanProperty`
and defaults to `True`.
"""
hint_bg_color = ColorProperty([0, 0, 0, 0])
hint_bg_color = ColorProperty(None)
"""
Hint rectangle color in ``rgba`` format.
Hint rectangle color in (r.g.b.a) format.
.. code-block:: kv
MDSlider
hint: True
hint_bg_color: "red"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-hint-bg-color.png
:align: center
:attr:`hint_bg_color` is an :class:`~kivy.properties.ColorProperty`
and defaults to `[0, 0, 0, 0]`.
@ -122,7 +101,17 @@ class MDSlider(ThemableBehavior, Slider):
hint_text_color = ColorProperty(None)
"""
Hint text color in ``rgba`` format.
Hint text color in (r.g.b.a) format.
.. code-block:: kv
MDSlider
hint: True
hint_bg_color: "red"
hint_text_color: "white"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-hint-text-color.png
:align: center
:attr:`hint_text_color` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
@ -132,10 +121,132 @@ class MDSlider(ThemableBehavior, Slider):
"""
Hint radius.
.. code-block:: kv
MDSlider
hint: True
hint_bg_color: "red"
hint_text_color: "white"
hint_radius: [6, 0, 6, 0]
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-hint-radius.png
:align: center
:attr:`hint_radius` is an :class:`~kivy.properties.VariableListProperty`
and defaults to `[dp(4), dp(4), dp(4), dp(4)]`.
"""
thumb_color_active = ColorProperty(None)
"""
The color of the thumb when the slider is active.
.. versionadded:: 1.0.0
.. code-block:: kv
MDSlider
thumb_color_active: "red"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-thumb-color-active.png
:align: center
:attr:`thumb_color_active` is an :class:`~kivy.properties.ColorProperty`
and default to `None`.
"""
thumb_color_inactive = ColorProperty(None)
"""
The color of the thumb when the slider is inactive.
.. versionadded:: 1.0.0
.. code-block:: kv
MDSlider
thumb_color_inactive: "red"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-thumb-color-inactive.png
:align: center
:attr:`thumb_color_inactive` is an :class:`~kivy.properties.ColorProperty`
and default to `None`.
"""
thumb_color_disabled = ColorProperty(None)
"""
The color of the thumb when the slider is in the disabled state.
.. versionadded:: 1.0.0
.. code-block:: kv
MDSlider
value: 55
disabled: True
thumb_color_disabled: "red"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-thumb-color-disabled.png
:align: center
:attr:`thumb_color_disabled` is an :class:`~kivy.properties.ColorProperty`
and default to `None`.
"""
track_color_active = ColorProperty(None)
"""
The color of the track when the slider is active.
.. versionadded:: 1.0.0
.. code-block:: kv
MDSlider
track_color_active: "red"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-track-color-active.png
:align: center
:attr:`track_color_active` is an :class:`~kivy.properties.ColorProperty`
and default to `None`.
"""
track_color_inactive = ColorProperty(None)
"""
The color of the track when the slider is inactive.
.. versionadded:: 1.0.0
.. code-block:: kv
MDSlider
track_color_inactive: "red"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-track-color-inactive.png
:align: center
:attr:`track_color_inactive` is an :class:`~kivy.properties.ColorProperty`
and default to `None`.
"""
track_color_disabled = ColorProperty(None)
"""
The color of the track when the slider is in the disabled state.
.. versionadded:: 1.0.0
.. code-block:: kv
MDSlider
disabled: True
track_color_disabled: "red"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-track-color-disabled.png
:align: center
:attr:`track_color_disabled` is an :class:`~kivy.properties.ColorProperty`
and default to `None`.
"""
show_off = BooleanProperty(True)
"""
Show the `'off'` ring when set to minimum value.
@ -144,40 +255,29 @@ class MDSlider(ThemableBehavior, Slider):
and defaults to `True`.
"""
color = ColorProperty([0, 0, 0, 0])
"""
Color slider in ``rgba`` format.
:attr:`color` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""
_track_color_active = ColorProperty([0, 0, 0, 0])
_track_color_normal = ColorProperty([0, 0, 0, 0])
_track_color_disabled = ColorProperty([0, 0, 0, 0])
_thumb_pos = ListProperty([0, 0])
_thumb_color_disabled = ColorProperty(
get_color_from_hex(colors["Gray"]["400"])
)
# Internal state of ring
# Internal state of ring.
_is_off = BooleanProperty(False)
# Internal adjustment to reposition sliders for ring
# Internal adjustment to reposition sliders for ring.
_offset = ListProperty((0, 0))
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.theme_cls.bind(
theme_style=self._set_colors,
primary_color=self._set_colors,
primary_palette=self._set_colors,
)
self._set_colors()
Clock.schedule_once(self.set_thumb_icon)
def on_hint(self, instance, value):
if not value:
self.remove_widget(self.ids.hint_box)
def set_thumb_icon(self, *args) -> None:
self.ids.thumb.ids.icon.icon = "blank"
def on_value_normalized(self, *args):
def on_hint(self, instance, value) -> None:
def on_hint(*args):
if not value:
self.remove_widget(self.ids.hint_box)
# Schedule using for declarative style.
# Otherwise get AttributeError exception.
Clock.schedule_once(on_hint)
def on_value_normalized(self, *args) -> None:
"""
When the ``value == min`` set it to `'off'` state and make slider
a ring.
@ -185,13 +285,13 @@ class MDSlider(ThemableBehavior, Slider):
self._update_is_off()
def on_show_off(self, *args):
def on_show_off(self, *args) -> None:
self._update_is_off()
def on__is_off(self, *args):
def on__is_off(self, *args) -> None:
self._update_offset()
def on_active(self, *args):
def on_active(self, *args) -> None:
self._update_offset()
def on_touch_down(self, touch):
@ -213,26 +313,3 @@ class MDSlider(ThemableBehavior, Slider):
def _update_is_off(self):
self._is_off = self.show_off and (self.value_normalized == 0)
def _set_colors(self, *args):
if self.theme_cls.theme_style == "Dark":
self._track_color_normal = get_color_from_hex("FFFFFF")
self._track_color_normal[3] = 0.3
self._track_color_active = self._track_color_normal
self._track_color_disabled = self._track_color_normal
if self.color == [0, 0, 0, 0]:
self.color = get_color_from_hex(
colors[self.theme_cls.primary_palette]["200"]
)
self.thumb_color_disabled = get_color_from_hex(
colors["Gray"]["800"]
)
else:
self._track_color_normal = get_color_from_hex("000000")
self._track_color_normal[3] = 0.26
self._track_color_active = get_color_from_hex("000000")
self._track_color_active[3] = 0.38
self._track_color_disabled = get_color_from_hex("000000")
self._track_color_disabled[3] = 0.26
if self.color == [0, 0, 0, 0]:
self.color = self.theme_cls.primary_color