diff --git a/internal_filesystem/lib/mpos/ui/appearance_manager.py b/internal_filesystem/lib/mpos/ui/appearance_manager.py index 93fbdf09..8e9bc62b 100644 --- a/internal_filesystem/lib/mpos/ui/appearance_manager.py +++ b/internal_filesystem/lib/mpos/ui/appearance_manager.py @@ -155,6 +155,26 @@ class AppearanceManager: print(f"[AppearanceManager] Light mode set to: {is_light}") + @classmethod + def set_theme(cls, prefs): + """ + Set the theme from preferences and reinitialize LVGL theme. + + This is a convenience method that loads theme settings from SharedPreferences + and applies them. It's equivalent to calling init() with the preferences. + + Args: + prefs: SharedPreferences object containing theme settings + + Example: + from mpos import AppearanceManager + import mpos.config + + prefs = mpos.config.SharedPreferences("theme_settings") + AppearanceManager.set_theme(prefs) + """ + cls.init(prefs) + # ========== Theme Colors ========== @classmethod diff --git a/internal_filesystem/lib/mpos/ui/theme.py b/internal_filesystem/lib/mpos/ui/theme.py deleted file mode 100644 index 9074eacf..00000000 --- a/internal_filesystem/lib/mpos/ui/theme.py +++ /dev/null @@ -1,85 +0,0 @@ -import lvgl as lv -import mpos.config - -# Global style for keyboard button fix -_keyboard_button_fix_style = None -_is_light_mode = True - -def get_keyboard_button_fix_style(): - """ - Get the keyboard button fix style for light mode. - - The LVGL default theme applies bg_color_white to keyboard buttons, - which makes them white-on-white (invisible) in light mode. - This function returns a custom style to override that. - - Returns: - lv.style_t: Style to apply to keyboard buttons, or None if not needed - """ - global _keyboard_button_fix_style, _is_light_mode - - # Only return style in light mode - if not _is_light_mode: - return None - - # Create style if it doesn't exist - if _keyboard_button_fix_style is None: - _keyboard_button_fix_style = lv.style_t() - _keyboard_button_fix_style.init() - - # Set button background to light gray (matches LVGL's intended design) - # This provides contrast against white background - # Using palette_lighten gives us the same gray as used in the theme - gray_color = lv.palette_lighten(lv.PALETTE.GREY, 2) - _keyboard_button_fix_style.set_bg_color(gray_color) - _keyboard_button_fix_style.set_bg_opa(lv.OPA.COVER) - - return _keyboard_button_fix_style - -# On ESP32, the keyboard buttons in light mode have no color, just white, -# which makes them hard to see on the white background. Probably a bug in the -# underlying LVGL or MicroPython or lvgl_micropython. -def fix_keyboard_button_style(keyboard): - """ - Apply keyboard button visibility fix to a keyboard instance. - - Call this function after creating a keyboard to ensure buttons - are visible in light mode. - - Args: - keyboard: The lv.keyboard instance to fix - """ - style = get_keyboard_button_fix_style() - if style: - keyboard.add_style(style, lv.PART.ITEMS) - print(f"Applied keyboard button fix for light mode to keyboard instance") - -def set_theme(prefs): - global _is_light_mode - - # Load and set theme: - theme_light_dark = prefs.get_string("theme_light_dark", "light") # default to a light theme - theme_dark_bool = ( theme_light_dark == "dark" ) - _is_light_mode = not theme_dark_bool # Track for keyboard button fix - - primary_color = lv.theme_get_color_primary(None) - color_string = prefs.get_string("theme_primary_color") - if color_string: - try: - color_string = color_string.replace("0x", "").replace("#", "").strip().lower() - color_int = int(color_string, 16) - print(f"Setting primary color: {color_int}") - primary_color = lv.color_hex(color_int) - except Exception as e: - print(f"Converting color setting '{color_string}' to lv_color_hex() got exception: {e}") - - lv.theme_default_init(mpos.ui.main_display._disp_drv, primary_color, lv.color_hex(0xFBDC05), theme_dark_bool, lv.font_montserrat_12) - #mpos.ui.main_display.set_theme(theme) # not needed, default theme is applied immediately - - # Recreate keyboard button fix style if mode changed - global _keyboard_button_fix_style - _keyboard_button_fix_style = None # Force recreation with new theme colors - -def is_light_mode(): - global _is_light_mode - return _is_light_mode \ No newline at end of file diff --git a/tests/test_graphical_custom_keyboard.py b/tests/test_graphical_custom_keyboard.py index 94a81f0b..872d2439 100644 --- a/tests/test_graphical_custom_keyboard.py +++ b/tests/test_graphical_custom_keyboard.py @@ -13,7 +13,7 @@ import unittest import lvgl as lv import sys import os -from mpos import MposKeyboard, wait_for_render, capture_screenshot +from mpos import MposKeyboard, wait_for_render, capture_screenshot, AppearanceManager class TestGraphicalMposKeyboard(unittest.TestCase): @@ -200,12 +200,11 @@ class TestGraphicalMposKeyboard(unittest.TestCase): # Set light mode (should already be default) import mpos.config - import mpos.ui.theme prefs = mpos.config.SharedPreferences("theme_settings") editor = prefs.edit() editor.put_string("theme_light_dark", "light") editor.commit() - mpos.ui.theme.set_theme(prefs) + AppearanceManager.set_theme(prefs) wait_for_render(10) # Create keyboard diff --git a/tests/test_graphical_keyboard_styling.py b/tests/test_graphical_keyboard_styling.py index b840bd9a..54d8f0ed 100644 --- a/tests/test_graphical_keyboard_styling.py +++ b/tests/test_graphical_keyboard_styling.py @@ -25,6 +25,7 @@ import os from mpos import ( wait_for_render, capture_screenshot, + AppearanceManager, ) @@ -62,7 +63,7 @@ class TestKeyboardStyling(unittest.TestCase): editor.commit() # Reapply original theme - mpos.ui.theme.set_theme(prefs) + AppearanceManager.set_theme(prefs) print("=== Test cleanup complete ===\n") @@ -90,7 +91,7 @@ class TestKeyboardStyling(unittest.TestCase): keyboard.set_style_min_height(160, 0) # Apply the keyboard button fix - mpos.ui.theme.fix_keyboard_button_style(keyboard) + AppearanceManager.apply_keyboard_fix(keyboard) # Load the screen and wait for rendering lv.screen_load(screen) @@ -228,7 +229,7 @@ class TestKeyboardStyling(unittest.TestCase): editor.commit() # Apply theme - mpos.ui.theme.set_theme(prefs) + AppearanceManager.set_theme(prefs) wait_for_render(iterations=10) # Create test keyboard @@ -282,7 +283,7 @@ class TestKeyboardStyling(unittest.TestCase): editor.commit() # Apply theme - mpos.ui.theme.set_theme(prefs) + AppearanceManager.set_theme(prefs) wait_for_render(iterations=10) # Create test keyboard @@ -335,7 +336,7 @@ class TestKeyboardStyling(unittest.TestCase): editor.commit() # Apply theme - mpos.ui.theme.set_theme(prefs) + AppearanceManager.set_theme(prefs) wait_for_render(iterations=10) # Create test keyboard