Skip to content

Commit 6c8e145

Browse files
launcher.py: check and refresh onResume()
1 parent bca6f56 commit 6c8e145

1 file changed

Lines changed: 38 additions & 26 deletions

File tree

  • internal_filesystem/builtin/apps/com.micropythonos.launcher/assets

internal_filesystem/builtin/apps/com.micropythonos.launcher/assets/launcher.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
class Launcher(mpos.apps.Activity):
1919

20+
seen_base_names = set()
21+
2022
def onCreate(self):
2123
print("launcher.py onCreate()")
2224
main_screen = lv.obj()
@@ -29,30 +31,18 @@ def onCreate(self):
2931
main_screen.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
3032
self.setContentView(main_screen)
3133

34+
def onResume(self, screen):
35+
redraw = False
36+
app_list = []
37+
# Check and collect subdirectories from existing directories
38+
apps_dir = "apps"
39+
apps_dir_builtin = "builtin/apps"
3240
# Grid parameters
3341
icon_size = 64 # Adjust based on your display
3442
label_height = 24
3543
iconcont_width = icon_size + label_height
3644
iconcont_height = icon_size + label_height
37-
38-
import time
39-
start = time.ticks_ms()
40-
41-
def load_icon(icon_path):
42-
with open(icon_path, 'rb') as f:
43-
image_data = f.read()
44-
image_dsc = lv.image_dsc_t({
45-
'data_size': len(image_data),
46-
'data': image_data
47-
})
48-
return image_dsc
49-
50-
# Check and collect subdirectories from existing directories
51-
apps_dir = "apps"
52-
apps_dir_builtin = "builtin/apps"
53-
seen_base_names = set()
54-
app_list = []
55-
45+
5646
# Check and collect unique subdirectories
5747
for dir_path in [apps_dir, apps_dir_builtin]:
5848
try:
@@ -62,25 +52,37 @@ def load_icon(icon_path):
6252
#print(f"full_path: {full_path}")
6353
if uos.stat(full_path)[0] & 0x4000: # Check if it's a directory
6454
base_name = d
65-
if base_name not in seen_base_names: # Avoid duplicates
66-
seen_base_names.add(base_name)
67-
#print(f"seen_base_names: {seen_base_names}")
55+
if base_name not in self.seen_base_names: # Avoid duplicates
56+
self.seen_base_names.add(base_name)
57+
#print(f"seen_base_names: {self.seen_base_names}")
6858
app = mpos.apps.parse_manifest(f"{full_path}/META-INF/MANIFEST.JSON")
6959
if app.category != "launcher": # Skip launchers
7060
main_launcher = mpos.apps.find_main_launcher_activity(app)
7161
if main_launcher:
62+
redraw = True
7263
app_list.append((app.name, full_path))
7364
except OSError:
7465
pass
7566

67+
if not redraw:
68+
print("Launcher doesn't need redraw, done.")
69+
return
70+
else:
71+
print("Launcher redrawing...")
72+
73+
import time
74+
start = time.ticks_ms()
75+
76+
screen.clean()
77+
7678
# Sort apps alphabetically by app.name
7779
app_list.sort(key=lambda x: x[0].lower()) # Case-insensitive sorting
7880

7981
# Create UI for each app
8082
for app_name, app_dir_fullpath in app_list:
8183
print(f"Adding app {app_name} from {app_dir_fullpath}")
8284
# Create container for each app (icon + label)
83-
app_cont = lv.obj(main_screen)
85+
app_cont = lv.obj(screen)
8486
app_cont.set_size(iconcont_width, iconcont_height)
8587
app_cont.set_style_border_width(0, 0)
8688
app_cont.set_style_pad_all(0, 0)
@@ -89,12 +91,12 @@ def load_icon(icon_path):
8991
icon_path = f"{app_dir_fullpath}/res/mipmap-mdpi/icon_64x64.png"
9092
image = lv.image(app_cont)
9193
try:
92-
image.set_src(load_icon(icon_path))
94+
image.set_src(Launcher.load_icon(icon_path))
9395
except Exception as e:
9496
print(f"Error loading icon {icon_path}: {e} - loading default icon")
9597
icon_path = "builtin/res/mipmap-mdpi/default_icon_64x64.png"
9698
try:
97-
image.set_src(load_icon(icon_path))
99+
image.set_src(Launcher.load_icon(icon_path))
98100
except Exception as e:
99101
print(f"Error loading default icon {icon_path}: {e} - using symbol")
100102
image.set_src(lv.SYMBOL.STOP)
@@ -109,4 +111,14 @@ def load_icon(icon_path):
109111
app_cont.add_event_cb(lambda e, path=app_dir_fullpath: mpos.apps.start_app(path), lv.EVENT.CLICKED, None)
110112

111113
end = time.ticks_ms()
112-
print(f"Displaying all icons took: {end-start}ms")
114+
print(f"Redraw icons took: {end-start}ms")
115+
116+
@staticmethod
117+
def load_icon(icon_path):
118+
with open(icon_path, 'rb') as f:
119+
image_data = f.read()
120+
image_dsc = lv.image_dsc_t({
121+
'data_size': len(image_data),
122+
'data': image_data
123+
})
124+
return image_dsc

0 commit comments

Comments
 (0)