You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
113 lines
4.9 KiB
Python
113 lines
4.9 KiB
Python
import lvgl as lv
|
|
|
|
from ..app.activity import Activity
|
|
from .setting_activity import SettingActivity
|
|
import mpos.ui
|
|
|
|
# Used to list and edit all settings:
|
|
class SettingsActivity(Activity):
|
|
|
|
# Taken from the Intent (initialized in onCreate)
|
|
prefs = None
|
|
settings = ()
|
|
|
|
def onCreate(self):
|
|
extras = self.getIntent().extras or {}
|
|
self.prefs = extras.get("prefs")
|
|
self.settings = extras.get("settings") or ()
|
|
if not self.prefs:
|
|
print("ERROR: SettingsActivity missing 'prefs' in Intent extras")
|
|
if not self.settings:
|
|
print("WARNING: SettingsActivity has no settings to display")
|
|
|
|
print("creating SettingsActivity ui...")
|
|
screen = lv.obj()
|
|
screen.set_style_pad_all(mpos.ui.DisplayMetrics.pct_of_width(2), lv.PART.MAIN)
|
|
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
|
|
screen.set_style_border_width(0, lv.PART.MAIN)
|
|
self.setContentView(screen)
|
|
|
|
def onResume(self, screen):
|
|
# Create settings entries
|
|
screen.clean()
|
|
if not self.prefs:
|
|
print("ERROR: SettingsActivity cannot render without prefs")
|
|
return
|
|
# Get the group for focusable objects
|
|
focusgroup = lv.group_get_default()
|
|
if not focusgroup:
|
|
print("WARNING: could not get default focusgroup")
|
|
|
|
for setting in self.settings:
|
|
# Check if it should be shown:
|
|
should_show_function = setting.get("should_show")
|
|
if should_show_function:
|
|
should_show = should_show_function(setting)
|
|
if should_show is False:
|
|
continue
|
|
# Container for each setting
|
|
setting_cont = lv.obj(screen)
|
|
setting_cont.set_width(lv.pct(100))
|
|
setting_cont.set_height(lv.SIZE_CONTENT)
|
|
setting_cont.set_style_border_width(1, lv.PART.MAIN)
|
|
setting_cont.set_style_pad_all(mpos.ui.DisplayMetrics.pct_of_width(2), lv.PART.MAIN)
|
|
setting_cont.add_flag(lv.obj.FLAG.CLICKABLE)
|
|
setting["cont"] = setting_cont # Store container reference for visibility control
|
|
|
|
# Title label (bold, larger)
|
|
title = lv.label(setting_cont)
|
|
title.set_text(setting["title"])
|
|
title.set_style_text_font(lv.font_montserrat_16, lv.PART.MAIN)
|
|
title.set_pos(0, 0)
|
|
|
|
# Value label (smaller, below title)
|
|
value = lv.label(setting_cont)
|
|
if setting.get("activity_class"):
|
|
placeholder = setting.get("placeholder") or ""
|
|
value_text = placeholder
|
|
elif setting.get("dont_persist"):
|
|
value_text = "(not persisted)"
|
|
else:
|
|
stored_value = self.prefs.get_string(setting["key"])
|
|
if stored_value is None:
|
|
default_value = setting.get("default_value")
|
|
if default_value is not None:
|
|
value_text = f"(defaults to {default_value})"
|
|
else:
|
|
value_text = "(not set)"
|
|
else:
|
|
value_text = stored_value
|
|
value.set_text(value_text)
|
|
value.set_style_text_font(lv.font_montserrat_12, lv.PART.MAIN)
|
|
value.set_style_text_color(lv.color_hex(0x666666), lv.PART.MAIN)
|
|
value.set_pos(0, 20)
|
|
setting["value_label"] = value # Store reference for updating
|
|
setting_cont.add_event_cb(lambda e, s=setting: self.startSettingActivity(s), lv.EVENT.CLICKED, None)
|
|
setting_cont.add_event_cb(lambda e, container=setting_cont: self.focus_container(container),lv.EVENT.FOCUSED,None)
|
|
setting_cont.add_event_cb(lambda e, container=setting_cont: self.defocus_container(container),lv.EVENT.DEFOCUSED,None)
|
|
if focusgroup:
|
|
focusgroup.add_obj(setting_cont)
|
|
|
|
def focus_container(self, container):
|
|
#print(f"container {container} focused, setting border...")
|
|
container.set_style_border_color(lv.theme_get_color_primary(None),lv.PART.MAIN)
|
|
container.set_style_border_width(1, lv.PART.MAIN)
|
|
container.scroll_to_view(True) # scroll to bring it into view
|
|
|
|
def defocus_container(self, container):
|
|
#print(f"container {container} defocused, unsetting border...")
|
|
container.set_style_border_width(0, lv.PART.MAIN)
|
|
|
|
def startSettingActivity(self, setting):
|
|
from ..content.intent import Intent
|
|
activity_class = SettingActivity
|
|
if setting.get("ui") == "activity":
|
|
activity_class = setting.get("activity_class")
|
|
if not activity_class:
|
|
print("ERROR: Setting is defined as 'activity' ui without 'activity_class', aborting...")
|
|
|
|
intent = Intent(activity_class=activity_class)
|
|
intent.putExtra("setting", setting)
|
|
intent.putExtra("prefs", self.prefs)
|
|
self.startActivity(intent)
|