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

770 lines
22 KiB
Python
Raw Normal View History

2024-09-15 12:12:16 +00:00
"""
Components/NavigationRail
=========================
.. versionadded:: 1.0.0
.. seealso::
2024-09-15 17:57:02 +00:00
`Material Design spec, Navigation rail <https://m3.material.io/components/navigation-rail/overview>`_
2024-09-15 12:12:16 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/navigation-rail.png
:align: center
2024-09-15 17:57:02 +00:00
.. rubric:: Navigation rails let people switch between UI views on mid-sized
devices.
- Can contain 3-7 destinations plus an optional FAB
- Always put the rail in the same place, even on different screens of an app
Example
-------
2024-09-15 12:12:16 +00:00
.. tabs::
.. tab:: Declarative KV style
.. code-block:: python
from kivy.lang import Builder
2024-09-15 17:57:02 +00:00
from kivy.properties import StringProperty
2024-09-15 12:12:16 +00:00
from kivymd.app import MDApp
2024-09-15 17:57:02 +00:00
from kivymd.uix.navigationrail import MDNavigationRailItem
2024-09-15 12:12:16 +00:00
KV = '''
2024-09-15 17:57:02 +00:00
<CommonNavigationRailItem>
MDNavigationRailItemIcon:
icon: root.icon
MDNavigationRailItemLabel:
text: root.text
2024-09-15 12:12:16 +00:00
MDBoxLayout:
MDNavigationRail:
2024-09-15 17:57:02 +00:00
type: "selected"
MDNavigationRailMenuButton:
icon: "menu"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailFabButton:
icon: "home"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
CommonNavigationRailItem:
icon: "folder-outline"
text: "Files"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
CommonNavigationRailItem:
icon: "bookmark-outline"
text: "Bookmark"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
CommonNavigationRailItem:
icon: "library-outline"
text: "Library"
2024-09-15 12:12:16 +00:00
MDScreen:
2024-09-15 17:57:02 +00:00
md_bg_color: self.theme_cls.secondaryContainerColor
2024-09-15 12:12:16 +00:00
'''
2024-09-15 17:57:02 +00:00
class CommonNavigationRailItem(MDNavigationRailItem):
text = StringProperty()
icon = StringProperty()
2024-09-15 12:12:16 +00:00
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
Example().run()
.. tab:: Declarative python style
.. code-block:: python
2024-09-15 17:57:02 +00:00
from kivy.clock import Clock
from kivy.properties import StringProperty
2024-09-15 12:12:16 +00:00
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
2024-09-15 17:57:02 +00:00
from kivymd.uix.navigationrail import (
MDNavigationRailItem,
MDNavigationRail,
MDNavigationRailMenuButton,
MDNavigationRailFabButton,
MDNavigationRailItemIcon,
MDNavigationRailItemLabel,
)
from kivymd.uix.screen import MDScreen
class CommonNavigationRailItem(MDNavigationRailItem):
text = StringProperty()
icon = StringProperty()
def on_icon(self, instance, value):
def on_icon(*ars):
self.add_widget(MDNavigationRailItemIcon(icon=value))
Clock.schedule_once(on_icon)
def on_text(self, instance, value):
def on_text(*ars):
self.add_widget(MDNavigationRailItemLabel(text=value))
Clock.schedule_once(on_text)
2024-09-15 12:12:16 +00:00
class Example(MDApp):
def build(self):
2024-09-15 17:57:02 +00:00
return MDBoxLayout(
MDNavigationRail(
MDNavigationRailMenuButton(
icon="menu",
),
MDNavigationRailFabButton(
icon="home",
),
CommonNavigationRailItem(
icon="bookmark-outline",
text="Files",
),
CommonNavigationRailItem(
icon="folder-outline",
text="Bookmark",
),
CommonNavigationRailItem(
icon="library-outline",
text="Library",
),
type="selected",
),
MDScreen(
md_bg_color=self.theme_cls.secondaryContainerColor,
),
2024-09-15 12:12:16 +00:00
)
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/navigation-rail-usage.png
:align: center
Anatomy
-------
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRail:
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
# Optional.
MDNavigationRailMenuButton:
icon: "menu"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
# Optional.
MDNavigationRailFabButton:
icon: "home"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItem
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItemIcon:
icon: icon
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItemLabel:
text: text
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
[...]
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/navigation-rail-anatomy.png
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Anatomy item
------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItem
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItemIcon:
icon: icon
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItemLabel:
text: text
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/navigation-rail-anatomy-item.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
Rail types
----------
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/navigation-rail-type.png
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
1. Selected
2. Unselected
3. Labeled
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Selected
--------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRail:
type: "selected" # default
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/navigation-rail-type-selected.gif
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Unselected
----------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRail:
type: "unselected"
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/navigation-rail-type-unselected.gif
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Labeled
-------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRail:
type: "labeled"
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/navigation-rail-type-labeled.gif
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Rail anchored
-------------
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/navigation-rail-anchored.png
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
1. Top
2. Center
3. Bottom
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Top
---
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRail:
anchor: "top"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Center
------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRail:
anchor: "center" # default
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Bottom
------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRail:
anchor: "bottom"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
API break
=========
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
1.2.0 version
-------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRail:
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailMenuButton:
icon: "menu"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailFabButton:
icon: "home"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItem:
icon: icon
text: text
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
[...]
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
2.2.0 version
-------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRail:
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailMenuButton:
icon: "menu"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailFabButton:
icon: "home"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItem
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItemIcon:
icon: icon
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDNavigationRailItemLabel:
text: text
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
[...]
2024-09-15 12:12:16 +00:00
"""
__all__ = (
"MDNavigationRail",
"MDNavigationRailItem",
2024-09-15 17:57:02 +00:00
"MDNavigationRailItemIcon",
"MDNavigationRailItemLabel",
2024-09-15 12:12:16 +00:00
"MDNavigationRailFabButton",
"MDNavigationRailMenuButton",
)
import os
from kivy.animation import Animation
from kivy.clock import Clock
2024-09-15 17:57:02 +00:00
from kivy.graphics import (
StencilPush,
RoundedRectangle,
StencilUse,
Color,
Ellipse,
StencilUnUse,
StencilPop,
)
2024-09-15 12:12:16 +00:00
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import (
BooleanProperty,
ColorProperty,
NumericProperty,
OptionProperty,
VariableListProperty,
2024-09-15 17:57:02 +00:00
ObjectProperty,
2024-09-15 12:12:16 +00:00
)
from kivy.uix.behaviors import ButtonBehavior
2024-09-15 17:57:02 +00:00
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
2024-09-15 12:12:16 +00:00
from kivymd import uix_path
2024-09-15 17:57:02 +00:00
from kivymd.theming import ThemableBehavior
from kivymd.uix.behaviors import (
ScaleBehavior,
DeclarativeBehavior,
BackgroundColorBehavior,
RectangularRippleBehavior,
)
from kivymd.uix.behaviors.focus_behavior import FocusBehavior
from kivymd.uix.button import MDFabButton, MDIconButton
from kivymd.uix.label import MDIcon, MDLabel
2024-09-15 12:12:16 +00:00
with open(
os.path.join(uix_path, "navigationrail", "navigationrail.kv"),
encoding="utf-8",
) as kv_file:
Builder.load_string(kv_file.read())
2024-09-15 17:57:02 +00:00
class MDNavigationRailFabButton(MDFabButton):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Implements a floating action button (FAB).
2024-09-15 12:12:16 +00:00
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.button.button.MDFabButton`
class documentation.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
md_bg_color_disabled = 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 switch when
the widget is disabled.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`md_bg_color_disabled` is a :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
2024-09-15 12:12:16 +00:00
"""
class MDNavigationRailMenuButton(MDIconButton):
"""
Implements a menu button.
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
md_bg_color_disabled = 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 switch when
the widget is disabled.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`md_bg_color_disabled` is a :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
class MDNavigationRailItemIcon(RectangularRippleBehavior, MDIcon):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Implements an icon for the :class:`~MDNavigationRailItem` class.
2024-09-15 12:12:16 +00:00
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.behaviors.ripple_behavior.RectangularRippleBehavior` and
:class:`~kivymd.uix.label.label.MDIcon`
2024-09-15 12:12:16 +00:00
classes documentation.
2024-09-15 17:57:02 +00:00
.. versionchanged:: 2.0.0
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
active_indicator_color = ColorProperty(None)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Background color of the active indicator in (r, g, b, a) or string format.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`active_indicator_color` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
_alpha = NumericProperty(0)
_active = BooleanProperty(False)
_navigation_rail = ObjectProperty()
_navigation_item = ObjectProperty()
_layer_color = ColorProperty([0, 0, 0, 0])
_selected_region_width = NumericProperty(dp(0))
def anim_complete(self, *args):
super().anim_complete()
self._navigation_rail.set_active_item(self._navigation_item)
def lay_canvas_instructions(self) -> None:
if not self.ripple_effect:
return
canvas_rectangle = self.canvas.before.get_group(
"navigation-rail-rounded-rectangle"
)[0]
with self.canvas.after if self.ripple_canvas_after else self.canvas.before:
if hasattr(self, "radius"):
self.radius = [
canvas_rectangle.radius[0][0],
]
self._round_rad = self.radius
StencilPush(group="rectangular_ripple_behavior")
RoundedRectangle(
pos=canvas_rectangle.pos,
size=canvas_rectangle.size,
radius=self._round_rad,
group="rectangular_ripple_behavior",
)
StencilUse(group="rectangular_ripple_behavior")
self.col_instruction = Color(
rgba=self.ripple_color, group="rectangular_ripple_behavior"
)
self.ellipse = Ellipse(
size=(self._ripple_rad, self._ripple_rad),
pos=(
self.ripple_pos[0] - self._ripple_rad / 2.0,
self.ripple_pos[1] - self._ripple_rad / 2.0,
),
group="rectangular_ripple_behavior",
)
StencilUnUse(group="rectangular_ripple_behavior")
RoundedRectangle(
pos=self.pos,
size=self.size,
radius=self._round_rad,
group="rectangular_ripple_behavior",
)
StencilPop(group="rectangular_ripple_behavior")
self.bind(ripple_color=self._set_color, _ripple_rad=self._set_ellipse)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class MDNavigationRailItemLabel(ScaleBehavior, MDLabel):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Implements an label for the :class:`~MDNavigationRailItem` class.
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.scale_behavior.ScaleBehavior` and
:class:`~kivymd.uix.label.label.MDLabel`
classes documentation.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionchanged:: 2.0.0
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
scale_value_y = NumericProperty(0)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Y-axis value.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`scale_value_y` is an :class:`~kivy.properties.NumericProperty`
and defaults to `0`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
_active = BooleanProperty(False)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def on__active(self, instance, value) -> None:
"""Fired when the :attr:`_active` value changes."""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
self.text_color = (
self.theme_cls.onSurfaceColor
if value
else self.theme_cls.onSurfaceVariantColor
)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class MDNavigationRailItem(
DeclarativeBehavior,
ButtonBehavior,
ThemableBehavior,
FocusBehavior,
BoxLayout,
):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Implements a menu item with an icon and text.
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.behaviors.ButtonBehavior` and
:class:`~kivymd.theming.ThemableBehavior` and
:class:`~kivymd.uix.behaviors.focus_behavior.FocusBehavior`
:class:`~kivy.uix.boxlayout.BoxLayout`
classes documentation.
2024-09-15 12:12:16 +00:00
"""
active = BooleanProperty(False)
"""
Is the element active.
:attr:`active` is an :class:`~kivy.properties.BooleanProperty`
and defaults to `False`.
"""
2024-09-15 17:57:02 +00:00
radius = VariableListProperty(0, length=4)
"""
Item radius.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionchanged:: 2.0.0
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`radius` is an :class:`~kivy.properties.VariableListProperty`
and defaults to `[0, 0, 0, 0]`.
"""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
_navigation_rail = ObjectProperty()
_icon_item = ObjectProperty()
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def on_active(self, instance, value) -> None:
"""Fired when the :attr:`active` value changes."""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
if value:
Clock.schedule_once(self.on_leave)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def on_enter(self, *args) -> None:
"""Fired when mouse enter the bbox of the widget."""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
if not self.active:
# FIXME: Move layer creation to
# kivymd/uix/behaviors/state_layer_behavior.py module.
self._icon_item._layer_color = self.theme_cls.onSurfaceColor[
:-1
] + [0.12]
Animation(
_selected_region_width=self._icon_item.width + dp(32),
d=0.2,
).start(self._icon_item)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def on_leave(self, *args) -> None:
"""Fired when the mouse goes outside the widget border."""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
self._icon_item._layer_color = self.theme_cls.transparentColor
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def add_widget(self, widget, *args, **kwargs):
if isinstance(widget, MDNavigationRailItemLabel):
Clock.schedule_once(lambda x: self._check_type_rail(widget))
elif isinstance(widget, MDNavigationRailItemIcon):
widget._navigation_rail = self._navigation_rail
widget._navigation_item = self
self._icon_item = widget
return super().add_widget(widget)
def _check_type_rail(self, instance: MDNavigationRailItemLabel):
if self._navigation_rail.type == "labeled":
instance.scale_value_y = 1
class MDNavigationRail(
DeclarativeBehavior,
ThemableBehavior,
BackgroundColorBehavior,
RelativeLayout,
):
2024-09-15 12:12:16 +00:00
"""
Navigation rail class.
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.behaviors.declarative_behavior.DeclarativeBehavior` and
:class:`~kivymd.theming.ThemableBehavior` and
:class:`~kivymd.uix.behaviors.backgroundcolor_behavior.BackgroundColorBehavior` and
:class:`~kivy.uix.relativelayout.RelativeLayout`
classes documentation.
2024-09-15 12:12:16 +00:00
"""
radius = VariableListProperty(0, length=4)
"""
Rail radius.
:attr:`radius` is an :class:`~kivy.properties.VariableListProperty`
and defaults to `[0, 0, 0, 0]`.
"""
2024-09-15 17:57:02 +00:00
anchor = OptionProperty("center", options=["top", "bottom", "center"])
2024-09-15 12:12:16 +00:00
"""
The position of the panel with menu items.
Available options are: `'top'`, `'bottom'`, `'center'`.
:attr:`anchor` is an :class:`~kivy.properties.OptionProperty`
and defaults to `'top'`.
"""
type = OptionProperty(
2024-09-15 17:57:02 +00:00
"selected", options=["labeled", "selected", "unselected"]
2024-09-15 12:12:16 +00:00
)
"""
Type of switching menu items.
Available options are: `'labeled'`, `'selected'`, `'unselected'`.
:attr:`type` is an :class:`~kivy.properties.OptionProperty`
and defaults to `'labeled'`.
"""
2024-09-15 17:57:02 +00:00
fab_button: MDNavigationRailFabButton = ObjectProperty()
menu_button: MDNavigationRailFabButton = ObjectProperty()
2024-09-15 12:12:16 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2024-09-15 17:57:02 +00:00
Clock.schedule_once(self._check_anchor)
def on_size(self, *args) -> None:
"""Fired when the application screen size changes."""
Clock.schedule_once(self._set_pos_menu_fab_buttons)
Clock.schedule_once(self._check_anchor)
2024-09-15 12:12:16 +00:00
def get_items(self) -> list:
2024-09-15 17:57:02 +00:00
"""Returns a list of :class:`~MDNavigationRailItem` objects."""
2024-09-15 12:12:16 +00:00
return self.ids.box_items.children
2024-09-15 17:57:02 +00:00
def set_active_item(self, item: MDNavigationRailItem) -> None:
"""Sets the active menu list item."""
for widget in self.ids.box_items.children:
if item is widget:
widget.active = not widget.active
for widget_item in item.children:
if isinstance(widget_item, MDNavigationRailItemLabel):
widget_item._active = widget.active
if self.type == "selected":
Animation(
scale_value_y=1 if widget.active else 0,
height=widget_item.texture_size[1]
if widget.active
else 0,
d=0.2,
).start(widget_item)
if isinstance(widget_item, MDNavigationRailItemIcon):
widget_item._active = widget.active
widget_item._alpha = 1 if widget.active else 0
widget_item._selected_region_width = 0
Animation(
_selected_region_width=widget_item.width + dp(32)
if widget.active
else 0,
d=0.2,
).start(widget_item)
2024-09-15 12:12:16 +00:00
else:
2024-09-15 17:57:02 +00:00
widget.active = False
for widget_item in widget.children:
widget_item._active = widget.active
if isinstance(widget_item, MDNavigationRailItemLabel):
if self.type == "selected":
Animation(scale_value_y=0, height=0, d=0.2).start(
widget_item
)
if isinstance(widget_item, MDNavigationRailItemIcon):
Animation(
_selected_region_width=0,
_alpha=0,
d=0.2,
).start(widget_item)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def add_widget(self, widget, *args, **kwargs):
if isinstance(widget, MDNavigationRailFabButton):
self.fab_button = widget
super().add_widget(widget)
elif isinstance(widget, MDNavigationRailMenuButton):
self.menu_button = widget
super().add_widget(widget)
elif isinstance(widget, MDNavigationRailItem):
self.ids.box_items.add_widget(widget)
widget._navigation_rail = self
widget._navigation_item = widget
widget.bind(on_release=self.set_active_item)
else:
return super().add_widget(widget)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def _set_pos_menu_fab_buttons(self, *args):
def set_pos_menu_fab_buttons(*args):
if self.fab_button and not self.menu_button:
self.fab_button.y = self.height - (
self.fab_button.height + dp(48)
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
elif self.menu_button and not self.fab_button:
self.menu_button.y = self.height - (
self.menu_button.height + dp(38)
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
elif self.fab_button and self.menu_button:
self.menu_button.y = self.height - (
self.menu_button.height + dp(38)
)
self.fab_button.y = self.height - (
self.fab_button.height + dp(48) + dp(48)
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
Clock.schedule_once(set_pos_menu_fab_buttons)
def _check_anchor(self, *args):
def set_top_pos(*args):
anchor_button = None
if (
self.fab_button
and not self.menu_button
or self.fab_button
and self.menu_button
):
anchor_button = self.fab_button
elif self.menu_button and not self.fab_button:
anchor_button = self.menu_button
self.ids.box_items.y = (
anchor_button.y
- (len(self.ids.box_items.children) * dp(56))
- dp(56)
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
if self.anchor == "center":
self.ids.box_items.pos_hint = {"center_y": 0.5}
elif self.anchor == "top":
Clock.schedule_once(set_top_pos)
elif self.anchor == "bottom":
self.ids.box_items.y = dp(56)