-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
theming: respect user current color scheme (#1592)
* theming: respect user current color scheme * fix example
- Loading branch information
Showing
5 changed files
with
162 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.