test-kivy-app/kivy_venv/lib/python3.11/site-packages/kivymd/uix/snackbar/snackbar.py

449 lines
12 KiB
Python
Raw Normal View History

2024-09-15 12:12:16 +00:00
"""
Components/Snackbar
===================
.. seealso::
`Material Design spec, Snackbars <https://m3.material.io/components/snackbar/overview>`_
.. rubric:: Snackbars provide brief messages about app processes at the bottom
of the screen.
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snackbar.png
:align: center
2024-09-15 17:57:02 +00:00
- Snackbars shouldnt interrupt the users experience
- Usually appear at the bottom of the UI
- Can disappear on their own or remain on screen until the user takes action
2024-09-15 12:12:16 +00:00
Usage
-----
.. code-block:: python
MDSnackbar(
2024-09-15 17:57:02 +00:00
MDSnackbarText(
text="Text",
2024-09-15 12:12:16 +00:00
),
2024-09-15 17:57:02 +00:00
y=dp(24),
pos_hint={"center_x": 0.5},
size_hint_x=0.5,
2024-09-15 12:12:16 +00:00
).open()
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-anatomy-detail.png
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
1. Container
2. Supporting text
3. Action (optional)
4. Icon (optional close affordance)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Anatomy
-------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-anatomy.png
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Configurations
==============
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
1. Single line
--------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-single-line.png
2024-09-15 12:12:16 +00:00
:align: center
.. code-block:: python
MDSnackbar(
2024-09-15 17:57:02 +00:00
MDSnackbarText(
text="Single-line snackbar",
2024-09-15 12:12:16 +00:00
),
2024-09-15 17:57:02 +00:00
y=dp(24),
pos_hint={"center_x": 0.5},
2024-09-15 12:12:16 +00:00
size_hint_x=0.5,
).open()
2024-09-15 17:57:02 +00:00
2. Single-line snackbar with action
-----------------------------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-single-line-with-action.png
:align: center
2024-09-15 12:12:16 +00:00
.. code-block:: python
MDSnackbar(
2024-09-15 17:57:02 +00:00
MDSnackbarSupportingText(
text="Single-line snackbar with action",
2024-09-15 12:12:16 +00:00
),
2024-09-15 17:57:02 +00:00
MDSnackbarButtonContainer(
MDSnackbarActionButton(
MDSnackbarActionButtonText(
text="Action button"
),
),
pos_hint={"center_y": 0.5}
2024-09-15 12:12:16 +00:00
),
y=dp(24),
2024-09-15 17:57:02 +00:00
orientation="horizontal",
2024-09-15 12:12:16 +00:00
pos_hint={"center_x": 0.5},
size_hint_x=0.5,
).open()
2024-09-15 17:57:02 +00:00
3. Single-line snackbar with action and close buttons
-----------------------------------------------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-single-line-with-action-and-close-buttons.png
:align: center
2024-09-15 12:12:16 +00:00
.. code-block:: python
MDSnackbar(
2024-09-15 17:57:02 +00:00
MDSnackbarSupportingText(
text="Single-line snackbar with action and close buttons",
2024-09-15 12:12:16 +00:00
),
2024-09-15 17:57:02 +00:00
MDSnackbarButtonContainer(
MDSnackbarActionButton(
MDSnackbarActionButtonText(
text="Action button"
),
),
MDSnackbarCloseButton(
icon="close",
),
pos_hint={"center_y": 0.5}
2024-09-15 12:12:16 +00:00
),
y=dp(24),
2024-09-15 17:57:02 +00:00
orientation="horizontal",
2024-09-15 12:12:16 +00:00
pos_hint={"center_x": 0.5},
size_hint_x=0.5,
).open()
2024-09-15 17:57:02 +00:00
4. Two-line snackbar with action and close buttons
--------------------------------------------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-two-line-with-action-and-close-buttons.png
:align: center
2024-09-15 12:12:16 +00:00
.. code-block:: python
MDSnackbar(
2024-09-15 17:57:02 +00:00
MDSnackbarText(
text="Single-line snackbar",
2024-09-15 12:12:16 +00:00
),
2024-09-15 17:57:02 +00:00
MDSnackbarSupportingText(
text="with action and close buttons",
2024-09-15 12:12:16 +00:00
),
2024-09-15 17:57:02 +00:00
MDSnackbarButtonContainer(
MDSnackbarActionButton(
MDSnackbarActionButtonText(
text="Action button"
),
),
MDSnackbarCloseButton(
icon="close",
),
pos_hint={"center_y": 0.5}
2024-09-15 12:12:16 +00:00
),
y=dp(24),
2024-09-15 17:57:02 +00:00
orientation="horizontal",
2024-09-15 12:12:16 +00:00
pos_hint={"center_x": 0.5},
size_hint_x=0.5,
).open()
2024-09-15 17:57:02 +00:00
5. Two-line snackbar with action and close buttons at the bottom
----------------------------------------------------------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-two-line-with-action-and-close-buttons-bottom.png
:align: center
2024-09-15 12:12:16 +00:00
.. code-block:: python
2024-09-15 17:57:02 +00:00
MDSnackbar(
MDSnackbarText(
text="Single-line snackbar with action",
),
MDSnackbarSupportingText(
text="and close buttons at the bottom",
padding=[0, 0, 0, dp(56)],
),
MDSnackbarButtonContainer(
Widget(),
2024-09-15 12:12:16 +00:00
MDSnackbarActionButton(
2024-09-15 17:57:02 +00:00
MDSnackbarActionButtonText(
text="Action button"
),
2024-09-15 12:12:16 +00:00
),
MDSnackbarCloseButton(
icon="close",
),
2024-09-15 17:57:02 +00:00
),
y=dp(124),
pos_hint={"center_x": 0.5},
size_hint_x=0.5,
padding=[0, 0, "8dp", "8dp"],
).open()
2024-09-15 12:12:16 +00:00
API break
=========
1.1.1 version
-------------
.. code-block:: python
snackbar = Snackbar(
text="First string",
snackbar_x="10dp",
snackbar_y="24dp",
)
snackbar.size_hint_x = (
Window.width - (snackbar.snackbar_x * 2)
) / Window.width
snackbar.buttons = [
MDFlatButton(
text="Done",
theme_text_color="Custom",
text_color="#8E353C",
on_release=snackbar.dismiss,
),
]
snackbar.open()
1.2.0 version
-------------
.. code-block:: python
MDSnackbar(
MDLabel(
text="First string",
),
MDSnackbarActionButton(
text="Done",
theme_text_color="Custom",
text_color="#8E353C",
),
y=dp(24),
pos_hint={"center_x": 0.5},
size_hint_x=0.5,
md_bg_color="#E8D8D7",
).open()
2024-09-15 17:57:02 +00:00
2.0.0 version
-------------
.. code-block:: python
MDSnackbar(
MDSnackbarSupportingText(
text="Single-line snackbar with action",
),
MDSnackbarButtonContainer(
MDSnackbarActionButton(
MDSnackbarActionButtonText(
text="Action button"
),
),
pos_hint={"center_y": 0.5}
),
y=dp(24),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.5,
background_color=self.theme_cls.onPrimaryContainerColor,
).open()
2024-09-15 12:12:16 +00:00
"""
__all__ = (
"MDSnackbar",
2024-09-15 17:57:02 +00:00
"MDSnackbarText",
"MDSnackbarSupportingText",
"MDSnackbarButtonContainer",
2024-09-15 12:12:16 +00:00
"MDSnackbarActionButton",
2024-09-15 17:57:02 +00:00
"MDSnackbarActionButtonText",
2024-09-15 12:12:16 +00:00
"MDSnackbarCloseButton",
)
import os
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.lang import Builder
2024-09-15 17:57:02 +00:00
from kivy.metrics import dp
2024-09-15 12:12:16 +00:00
from kivy.properties import (
BooleanProperty,
ListProperty,
NumericProperty,
2024-09-15 17:57:02 +00:00
ColorProperty,
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
2024-09-15 12:12:16 +00:00
from kivymd import uix_path
2024-09-15 17:57:02 +00:00
from kivymd.uix.behaviors import MotionShackBehavior, DeclarativeBehavior
from kivymd.uix.button import MDButton, MDIconButton, MDButtonText
2024-09-15 12:12:16 +00:00
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
with open(
os.path.join(uix_path, "snackbar", "snackbar.kv"), encoding="utf-8"
) as kv_file:
Builder.load_string(kv_file.read())
2024-09-15 17:57:02 +00:00
class MDSnackbarButtonContainer(DeclarativeBehavior, BoxLayout):
"""
The class implements a container for placing snackbar buttons.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
For more information, see in the
:class:`~kivymd.uix.behaviors.declarative_behavior.DeclarativeBehavior` and
:class:`~kivy.uix.boxlayout.BoxLayout` classes documentation.
"""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def add_widget(self, widget, *args, **kwargs):
def set_container_width(w):
self.parent.width += w.width
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
if isinstance(
widget, (MDSnackbarActionButton, MDSnackbarCloseButton, Widget)
):
Clock.schedule_once(lambda x: set_container_width(widget), 0.2)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
return super().add_widget(widget)
2024-09-15 12:12:16 +00:00
class MDSnackbarCloseButton(MDIconButton):
"""
Snackbar closed button class.
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.button.button.MDIconButton` class documentation.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
class MDSnackbarActionButtonText(MDButtonText):
"""
The class implements the text for the :class:`~MDSnackbarActionButton`
class.
.. versionchanged:: 2.2.0
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
For more information, see in the
:class:`~kivymd.uix.button.button.MDButtonText` class documentation.
"""
class MDSnackbarActionButton(MDButton):
2024-09-15 12:12:16 +00:00
"""
Snackbar action button class.
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.button.button.MDButton` class documentation.
2024-09-15 12:12:16 +00:00
"""
class MDSnackbar(MotionShackBehavior, MDCard):
"""
Snackbar class.
.. versionchanged:: 1.2.0
Rename `BaseSnackbar` to `MDSnackbar` class.
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.behaviors.motion_behavior.MotionShackBehavior` and
:class:`~kivymd.uix.card.card.MDCard` and
2024-09-15 12:12:16 +00:00
class documentation.
:Events:
:attr:`on_open`
2024-09-15 17:57:02 +00:00
Fired when a snackbar opened.
2024-09-15 12:12:16 +00:00
:attr:`on_dismiss`
2024-09-15 17:57:02 +00:00
Fired when a snackbar closes.
2024-09-15 12:12:16 +00:00
"""
duration = NumericProperty(3)
"""
The amount of time that the snackbar will stay on screen for.
:attr:`duration` is a :class:`~kivy.properties.NumericProperty`
and defaults to `3`.
"""
auto_dismiss = BooleanProperty(True)
"""
Whether to use automatic closing of the snackbar or not.
:attr:`auto_dismiss` is a :class:`~kivy.properties.BooleanProperty`
and defaults to `True`.
"""
2024-09-15 17:57:02 +00:00
radius = ListProperty([dp(4), dp(4), dp(4), dp(4)])
2024-09-15 12:12:16 +00:00
"""
Snackbar radius.
:attr:`radius` is a :class:`~kivy.properties.ListProperty`
2024-09-15 17:57:02 +00:00
and defaults to `[dp(4), dp(4), dp(4), dp(4)]`
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
background_color = ColorProperty(None)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
The background color in (r, g, b, a) or string format of the snackbar.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`background_color` is a :class:`~kivy.properties.ColorProperty`
2024-09-15 12:12:16 +00:00
and defaults to `None`.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.register_event_type("on_open")
self.register_event_type("on_dismiss")
self.opacity = 0
def dismiss(self, *args) -> None:
"""Dismiss the snackbar."""
super().on_dismiss()
def open(self) -> None:
"""Show the snackbar."""
2024-09-15 17:57:02 +00:00
Window.add_widget(self)
2024-09-15 12:12:16 +00:00
super().on_open()
def add_widget(self, widget, *args, **kwargs):
2024-09-15 17:57:02 +00:00
if isinstance(widget, (MDSnackbarText, MDSnackbarSupportingText)):
2024-09-15 12:12:16 +00:00
self.ids.label_container.add_widget(widget)
2024-09-15 17:57:02 +00:00
elif isinstance(widget, MDSnackbarButtonContainer):
self.ids.button_container.size_hint_x = (
1 if self.orientation == "vertical" else None
)
self.ids.button_container.add_widget(widget)
else:
2024-09-15 12:12:16 +00:00
return super().add_widget(widget)
def on_open(self, *args) -> None:
2024-09-15 17:57:02 +00:00
"""Fired when a snackbar opened."""
2024-09-15 12:12:16 +00:00
def on_dismiss(self, *args) -> None:
2024-09-15 17:57:02 +00:00
"""Fired when a snackbar closed."""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class MDSnackbarText(MDLabel):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
The class implements the text.
For more information, see in the
:class:`~kivymd.uix.label.label.MDLabel` class documentation.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
class MDSnackbarSupportingText(MDLabel):
"""
The class implements the supporting text.
For more information, see in the
:class:`~kivymd.uix.label.label.MDLabel` class documentation.
"""