Files
MicroPythonOS/internal_filesystem/lib/mpos/ui/settings_activity.py
T
2026-01-26 12:30:50 +01:00

90 lines
3.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 the Intent:
prefs = None
settings = None
def onCreate(self):
self.prefs = self.getIntent().extras.get("prefs")
self.settings = self.getIntent().extras.get("settings")
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()
# 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)
value.set_text(self.prefs.get_string(setting["key"], "(not set)" if not setting.get("dont_persist") else "(not persisted)"))
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)