Skip to content

Commit

Permalink
theming: respect user current color scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Dynamos committed Jan 23, 2024
1 parent c709657 commit f4730c5
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 90 deletions.
86 changes: 62 additions & 24 deletions kivymd/theming.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
from kivymd.dynamic_color import DynamicColor
from kivymd.font_definitions import theme_font_styles
from kivymd.material_resources import DEVICE_IOS
from kivymd.utils.get_wallpaper import get_wallpaper
from kivymd.utils.wallpaper import (
get_wallpaper,
get_pregenerated_color_and_scheme,
)

from PIL import Image

Expand All @@ -54,6 +57,12 @@
MaterialDynamicColors,
)
from materialyoucolor.scheme.scheme_tonal_spot import SchemeTonalSpot
from materialyoucolor.scheme.scheme_expressive import SchemeExpressive
from materialyoucolor.scheme.scheme_fruit_salad import SchemeFruitSalad
from materialyoucolor.scheme.scheme_monochrome import SchemeMonochrome
from materialyoucolor.scheme.scheme_rainbow import SchemeRainbow
from materialyoucolor.scheme.scheme_vibrant import SchemeVibrant
from materialyoucolor.scheme.scheme_neutral import SchemeNeutral
from materialyoucolor.hct import Hct
from materialyoucolor.quantize import QuantizeCelebi
from materialyoucolor.score.score import Score
Expand Down Expand Up @@ -150,7 +159,8 @@ def build(self):

dynamic_color_quality = NumericProperty(1 if platform == "android" else 10)
"""
The quality of the generated color scheme from the system wallpaper.
The quality of the generated color scheme from the system wallpaper.
It is equal to or higher than `1`, with `1` representing the maximum quality.
.. warning::
Expand Down Expand Up @@ -466,6 +476,8 @@ def on_start(*args):
and defaults to `'Light'`.
"""

_android_api_scheme = {}

def _get_theme_style(self, opposite: bool) -> str:
if opposite:
return "Light" if self.theme_style == "Dark" else "Dark"
Expand Down Expand Up @@ -645,24 +657,46 @@ def set_colors(self, *args) -> None:
else:
self._set_palette_color()
else:
path_to_wallpaper = get_wallpaper(
App.get_running_app().user_data_dir, self.path_to_wallpaper
)
if path_to_wallpaper:
size_wallpaper = os.path.getsize(path_to_wallpaper)
if size_wallpaper != self._size_current_wallpaper:
self._size_current_wallpaper = os.path.getsize(
path_to_wallpaper
)
self._set_dynamic_color(path_to_wallpaper)
self._android_api_scheme = {"color": None}

# First check android default theme
if platform == "android":
self._android_api_scheme = get_pregenerated_color_and_scheme()

if self._android_api_scheme["color"] is None:
path_to_wallpaper = get_wallpaper(
App.get_running_app().user_data_dir, self.path_to_wallpaper
)
if path_to_wallpaper:
size_wallpaper = os.path.getsize(path_to_wallpaper)
if size_wallpaper != self._size_current_wallpaper:
self._size_current_wallpaper = os.path.getsize(
path_to_wallpaper
)
self._set_dynamic_color(path_to_wallpaper)
else:
Logger.info(
"KivyMD: "
f"Color scheme generated. The color scheme of these "
f"wallpapers has already been generated. Skip it."
)
else:
Logger.info(
"KivyMD: "
f"Color scheme generation. The color scheme of these "
f"wallpapers has already been generated. Skip it."
)
self._set_palette_color()
else:
self._set_palette_color()
# No color generation needed
self._set_application_scheme(
color=self._android_api_scheme["color"],
scheme={
"TONAL_SPOT": SchemeTonalSpot,
"SPRITZ": SchemeNeutral,
"VIBRANT": SchemeVibrant,
"EXPRESSIVE": SchemeExpressive,
"FRUIT_SALAD": SchemeFruitSalad,
"RAINBOW": SchemeRainbow,
"MONOCHROME": SchemeMonochrome,
}[self._android_api_scheme["parsed_scheme"]],
is_android_generated=True,
)

def update_theme_colors(self, *args) -> None:
"""Fired when the `theme_style` value changes."""
Expand Down Expand Up @@ -713,7 +747,11 @@ def _set_dynamic_color(self, path_to_wallpaper: str) -> None:
self._set_application_scheme(color=selected[0])

def _set_application_scheme(
self, default_color: str = None, color: int = None
self,
default_color: str = None,
color: int = None,
scheme=SchemeTonalSpot,
is_android_generated=False,
) -> None:
# Default blue of Google.
start_time = default_timer()
Expand All @@ -725,7 +763,7 @@ def _set_application_scheme(
)
color = argb_from_rgb(*[c * 255 for c in color[:-1]])

scheme = SchemeTonalSpot(
scheme = scheme(
Hct.from_int(color), # the color of current theme in int form
False if self.theme_style == "Light" else True, # dark mode
0.0, # contrast level
Expand All @@ -739,11 +777,11 @@ def _set_application_scheme(

self.disabledTextColor = self._get_disabled_hint_text_color()
end_time = default_timer()

Logger.info(
"KivyMD: "
f"Color scheme generation. Get a color scheme from an installed "
f"palette - {end_time - start_time} sec."
"KivyMD: Color scheme generated. Get a color scheme using "
+ ("an installed palette" if not is_android_generated else "android api")
+ f"- {end_time - start_time} sec."
)

def _set_palette_color(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion kivymd/uix/textfield/textfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ def set_text(*args):
self.text and not self.focus
):
self.on_focus(instance, False)

# set_text()
Clock.schedule_once(set_text)

def on_focus(self, instance, focus: bool) -> None:
Expand Down
65 changes: 0 additions & 65 deletions kivymd/utils/get_wallpaper.py

This file was deleted.

Loading

0 comments on commit f4730c5

Please sign in to comment.