You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Add SettingsActivity framework
...so apps can easily add settings screens with just a few lines of code!
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@
|
||||
- Improve robustness with custom exception that does not deinit() the TaskHandler
|
||||
- Improve robustness by removing TaskHandler callback that throws an uncaught exception
|
||||
- Make "Power Off" button on desktop exit completely
|
||||
- Promote SettingActivity from app to framework: now all apps can use it to easily build a setting screen
|
||||
- Create new SettingsActivity and SettingActivity framework so apps can easily add settings screens with just a few lines of code
|
||||
|
||||
0.5.2
|
||||
=====
|
||||
|
||||
@@ -15,6 +15,7 @@ from .display import (
|
||||
from .event import get_event_name, print_event
|
||||
from .util import shutdown, set_foreground_app, get_foreground_app
|
||||
from .setting_activity import SettingActivity
|
||||
from .settings_activity import SettingsActivity
|
||||
|
||||
__all__ = [
|
||||
"setContentView", "back_screen", "remove_and_stop_current_activity", "remove_and_stop_all_activities"
|
||||
@@ -28,5 +29,6 @@ __all__ = [
|
||||
"get_pointer_xy",
|
||||
"get_event_name", "print_event",
|
||||
"shutdown", "set_foreground_app", "get_foreground_app",
|
||||
"SettingActivity"
|
||||
"SettingActivity",
|
||||
"SettingsActivity"
|
||||
]
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import lvgl as lv
|
||||
|
||||
import mpos
|
||||
from mpos.apps import Activity, Intent
|
||||
from .setting_activity import SettingActivity
|
||||
|
||||
# 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.pct_of_display_width(2), 0)
|
||||
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
|
||||
screen.set_style_border_width(0, 0)
|
||||
self.setContentView(screen)
|
||||
|
||||
def onResume(self, screen):
|
||||
wallet_type = self.prefs.get_string("wallet_type") # might have changed in the settings
|
||||
|
||||
# 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, 0)
|
||||
#setting_cont.set_style_border_side(lv.BORDER_SIDE.BOTTOM, 0)
|
||||
setting_cont.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
|
||||
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, 0)
|
||||
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)"))
|
||||
value.set_style_text_font(lv.font_montserrat_12, 0)
|
||||
value.set_style_text_color(lv.color_hex(0x666666), 0)
|
||||
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):
|
||||
intent = Intent(activity_class=SettingActivity)
|
||||
intent.putExtra("prefs", self.prefs)
|
||||
intent.putExtra("setting", setting)
|
||||
self.startActivity(intent)
|
||||
Reference in New Issue
Block a user