Source code for acg.custom_widgets.main_menu

"""Implements :class:`MainMenu`."""


import os

import toolz
from kivy.lang import Builder
from kivy.properties import DictProperty, ListProperty, ObjectProperty, StringProperty
from kivy.uix.screenmanager import Screen
from kivy.uix.stacklayout import StackLayout
from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import MDList, OneLineIconListItem
from kivymd.uix.menu import MDDropdownMenu

from .behaviors import CheckBehavior, ChildrenFromDataBehavior


[docs]class DrawerItem(CheckBehavior, OneLineIconListItem): """List item that changes color based on :attr:`current_state`."""
[docs] icon = StringProperty()
""":class:`~kivy.properties.StringProperty`: name of icon."""
[docs] name = StringProperty("test")
""":class:`~kivy.properties.StringProperty`: name of the screen."""
[docs] kv_file_name = StringProperty("")
""":class:`~kivy.properties.StringProperty`: name of the screens kv-file."""
[docs]class DrawerList(ThemableBehavior, ChildrenFromDataBehavior, MDList): """ List containing :class:`DrawerItem`. It has one active element at all times whose screen_name attribute is saved in :attr:`current`. """ check_one = True child_class_name = "DrawerItem"
[docs] current = StringProperty("")
""":class:`~kivy.properties.StringProperty` of currently active :attr:`DrawerItem.screen_name`.""" nav_drawer = ObjectProperty()
[docs] def on_child_release(self, instance): """Set states of child's according to :attr:`check_one`. Gets called if a child dispatches `on_release`-event. """ if self.check_one: for child in self.children: child.current_state = child == instance else: instance.current_state = not instance.current_state self.current = instance.name self.nav_drawer.set_state("close")
[docs] def on_current(self, *_): """Update state of :attr:`current` by calling :meth:´on_child_release´.""" current_child = self.get_child(self.current) if not current_child.current_state: self.on_child_release(current_child)
[docs]class KvScreen(Screen): """ Screen that automatically adds content of kv-file at :attr:`path` as child. If :attr:`path` does not exist, create file. """
[docs] kv_path = StringProperty("screen_default.kv")
""":class:`~kivy.properties.StringProperty` defaults to ``"screen_default.kv"`` """ def __init__(self, **kwargs): super().__init__(**kwargs) # TODO: TEST WEATHER THIS HAS A USE!! # self.size_hint = None, 1 # self.width = Window.width # Window.bind(width=self.setter("width")) if not os.path.exists(self.kv_path): self._create_content_file() self._load_content() def _load_content(self): self.add_widget(Builder.load_file(self.kv_path)) def _create_content_file(self): with open(self.kv_path, "w") as file: file.write(f'MDLabel:\n\ttext:"{self.name}"')