Skip to content

Commit

Permalink
theming: respect user current color scheme (#1592)
Browse files Browse the repository at this point in the history
* theming: respect user current color scheme

* fix example
  • Loading branch information
T-Dynamos authored Feb 18, 2024
1 parent 06e5f0c commit 4b8f456
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 263 deletions.
26 changes: 25 additions & 1 deletion examples/common_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from kivy.utils import hex_colormap

from materialyoucolor.utils.platform_utils import SCHEMES
from kivymd.uix.menu import MDDropdownMenu

KV = """
Expand Down Expand Up @@ -55,7 +56,8 @@ def open_menu(self, menu_button):
menu_items = []
for item, method in {
"Set palette": lambda: self.set_palette(),
"Switch theme style": lambda: self.theme_cls.switch_theme(),
"Switch theme style": lambda: self.switch_theme(),
"Switch scheme type": lambda: self.set_scheme_type(),
"Disabled widgets": lambda: self.disabled_widgets(),
}.items():
menu_items.append(
Expand All @@ -72,6 +74,9 @@ def open_menu(self, menu_button):

def switch_palette(self, selected_palette):
self.theme_cls.primary_palette = selected_palette

def switch_theme(self):
self.theme_cls.switch_theme()

def set_palette(self):
instance_from_menu = self.get_instance_from_menu("Set palette")
Expand All @@ -92,6 +97,25 @@ def set_palette(self):
items=menu_items,
).open()

def set_scheme_type(self):
instance_from_menu = self.get_instance_from_menu("Switch scheme type")

menu_items = []
for scheme_name in SCHEMES.keys():
menu_items.append(
{
"text": scheme_name,
"on_release": lambda x=scheme_name: self.update_scheme_name(x),
}
)
MDDropdownMenu(
caller=instance_from_menu,
items=menu_items,
).open()

def update_scheme_name(self, scheme_name):
self.theme_cls.dynamic_scheme_name = scheme_name

def get_instance_from_menu(self, name_item):
index = 0
rv = self.menu.ids.md_menu
Expand Down
188 changes: 67 additions & 121 deletions examples/dynamic_color_schemes.py
Original file line number Diff line number Diff line change
@@ -1,135 +1,81 @@
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty, ColorProperty
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.utils import hex_colormap
from kivymd.uix.boxlayout import MDBoxLayout

from kivymd.uix.menu import MDDropdownMenu
from kivymd.app import MDApp


KV = """
<ColorCard>
orientation: "vertical"
MDLabel:
text: root.text
color: "grey"
adaptive_height: True
MDCard:
theme_bg_color: "Custom"
md_bg_color: root.bg_color
MDScreen:
MDIconButton:
on_release: app.open_menu(self)
pos_hint: {"top": .98}
x: "12dp"
icon: "menu"
MDRecycleView:
id: card_list
viewclass: "ColorCard"
bar_width: 0
size_hint_y: None
height: root.height - dp(68)
RecycleGridLayout:
cols: 3
spacing: "16dp"
padding: "16dp"
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
"""


class ColorCard(BoxLayout):
text = StringProperty()
bg_color = ColorProperty()


class Example(MDApp):
menu: MDDropdownMenu = None
from kivymd.dynamic_color import DynamicColor
from examples.common_app import CommonApp, KV

Builder.load_string("""
#:import Clipboard kivy.core.clipboard.Clipboard
<DynamicColorInfo>:
name: "primaryColor"
color:[0,0,0,0]
size_hint_y:None
height:dp(130)
orientation:"vertical"
spacing:dp(10)
BoxLayout:
spacing:dp(10)
MDIconButton:
icon:"content-copy"
size_hint_x:None
width:dp(50)
on_release:
Clipboard.copy(root.name)
MDLabel:
text:root.name
adaptive_height:True
MDBoxLayout:
md_bg_color:root.color
radius:dp(10)
<Container>:
ScrollView:
MDBoxLayout:
orientation:"vertical"
id:main_view
adaptive_height:True
spacing:dp(20)
""")

class Container(MDBoxLayout):
pass

class DynamicColorInfo(BoxLayout):
pass

class Example(MDApp, CommonApp):

def build(self):
self.theme_cls.dynamic_color = True
self.theme_cls.path_to_wallpaper = "path_to_some_image.png"
self.theme_cls.on_colors = lambda : Clock.schedule_once(self.refresh)
return Builder.load_string(KV)

def get_instance_from_menu(self, name_item):
index = 0
rv = self.menu.ids.md_menu
opts = rv.layout_manager.view_opts
datas = rv.data[0]

for data in rv.data:
if data["text"] == name_item:
index = rv.data.index(data)
break

instance = rv.view_adapter.get_view(
index, datas, opts[index]["viewclass"]
)

return instance

def open_menu(self, menu_button):
menu_items = []
for item, method in {
"Set palette": lambda: self.set_palette(),
"Switch theme style": lambda: self.theme_switch(),
}.items():
menu_items.append({"text": item, "on_release": method})
self.menu = MDDropdownMenu(
caller=menu_button,
items=menu_items,
)
self.menu.open()

def set_palette(self):
instance_from_menu = self.get_instance_from_menu("Set palette")
available_palettes = [
name_color.capitalize() for name_color in hex_colormap.keys()
]

menu_items = []
for name_palette in available_palettes:
menu_items.append(
{
"text": name_palette,
"on_release": lambda x=name_palette: self.switch_palette(x),
}
)
MDDropdownMenu(
caller=instance_from_menu,
items=menu_items,
).open()

def switch_palette(self, selected_palette):
self.theme_cls.primary_palette = selected_palette
Clock.schedule_once(self.generate_cards, 0.5)

def theme_switch(self) -> None:
self.theme_cls.switch_theme()
Clock.schedule_once(self.generate_cards, 0.5)

def generate_cards(self, *args):
self.root.ids.card_list.data = []
for name_color in self.theme_cls.current_schemes_color_data:
self.root.ids.card_list.data.append(
{
"bg_color": getattr(self.theme_cls, name_color),
"text": name_color,
}
)

def on_start(self):
super().on_start()
Clock.schedule_once(self.generate_cards)

parent_widget = self.root.ids.widget_box.parent.parent
parent_widget.clear_widgets()
self.container = Container()
parent_widget.add_widget(self.container)
self.container.ids.main_view.clear_widgets()

for color in vars(DynamicColor).keys():
if "__" in color:
continue
widget = DynamicColorInfo()
widget.name = color
widget.color = getattr(self.theme_cls, color)
self.container.ids.main_view.add_widget(widget)

Clock.schedule_once(self.refresh)

def refresh(self, *arg):
for widget in self.container.ids.main_view.children:
widget.color = getattr(self.theme_cls, widget.name)

Example().run()
Loading

0 comments on commit 4b8f456

Please sign in to comment.