Skip to content

Commit 46c8618

Browse files
File Manager: add ActionActivity
1 parent a18518e commit 46c8618

2 files changed

Lines changed: 87 additions & 3 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import lvgl as lv
2+
import os
3+
from mpos import Activity
4+
5+
6+
class ActionActivity(Activity):
7+
8+
def onCreate(self):
9+
self._path = self.getIntent().extras.get("path")
10+
screen = lv.obj()
11+
12+
path_label = lv.label(screen)
13+
path_label.set_text(self._path)
14+
path_label.set_width(lv.pct(90))
15+
path_label.align(lv.ALIGN.TOP_MID, 0, 10)
16+
17+
self._delete_btn = lv.button(screen)
18+
lv.label(self._delete_btn).set_text("Delete")
19+
self._delete_btn.add_event_cb(lambda e: self.delete_cb(), lv.EVENT.CLICKED, None)
20+
self._delete_btn.align(lv.ALIGN.CENTER, 0, -40)
21+
22+
self._rename_btn = lv.button(screen)
23+
lv.label(self._rename_btn).set_text("Rename")
24+
self._rename_btn.add_event_cb(lambda e: self.show_rename_ui(), lv.EVENT.CLICKED, None)
25+
self._rename_btn.align(lv.ALIGN.CENTER, 0, 20)
26+
27+
name_part = self._path.rstrip('/').split('/')[-1]
28+
self._rename_ta = lv.textarea(screen)
29+
self._rename_ta.set_text(name_part)
30+
self._rename_ta.set_width(lv.pct(80))
31+
self._rename_ta.align(lv.ALIGN.CENTER, 0, -20)
32+
self._rename_ta.add_flag(lv.obj.FLAG.HIDDEN)
33+
34+
self._confirm_btn = lv.button(screen)
35+
lv.label(self._confirm_btn).set_text("Confirm")
36+
self._confirm_btn.add_event_cb(lambda e: self.confirm_rename(), lv.EVENT.CLICKED, None)
37+
self._confirm_btn.align(lv.ALIGN.CENTER, 0, 30)
38+
self._confirm_btn.add_flag(lv.obj.FLAG.HIDDEN)
39+
40+
self._cancel_btn = lv.button(screen)
41+
lv.label(self._cancel_btn).set_text("Cancel")
42+
self._cancel_btn.add_event_cb(lambda e: self.cancel_rename(), lv.EVENT.CLICKED, None)
43+
self._cancel_btn.align(lv.ALIGN.CENTER, 0, 80)
44+
self._cancel_btn.add_flag(lv.obj.FLAG.HIDDEN)
45+
46+
self.setContentView(screen)
47+
48+
def delete_cb(self):
49+
try:
50+
os.remove(self._path)
51+
except OSError:
52+
try:
53+
os.rmdir(self._path)
54+
except OSError as e:
55+
print(f"Error deleting {self._path}: {e}")
56+
self.finish()
57+
58+
def show_rename_ui(self):
59+
self._delete_btn.add_flag(lv.obj.FLAG.HIDDEN)
60+
self._rename_btn.add_flag(lv.obj.FLAG.HIDDEN)
61+
self._rename_ta.remove_flag(lv.obj.FLAG.HIDDEN)
62+
self._confirm_btn.remove_flag(lv.obj.FLAG.HIDDEN)
63+
self._cancel_btn.remove_flag(lv.obj.FLAG.HIDDEN)
64+
65+
def confirm_rename(self):
66+
dir_part = '/'.join(self._path.rstrip('/').split('/')[:-1]) or '/'
67+
new_name = self._rename_ta.get_text()
68+
new_path = f"{dir_part}/{new_name}"
69+
try:
70+
os.rename(self._path, new_path)
71+
except OSError as e:
72+
print(f"Error renaming {self._path} to {new_path}: {e}")
73+
self.finish()
74+
75+
def cancel_rename(self):
76+
self._delete_btn.remove_flag(lv.obj.FLAG.HIDDEN)
77+
self._rename_btn.remove_flag(lv.obj.FLAG.HIDDEN)
78+
self._rename_ta.add_flag(lv.obj.FLAG.HIDDEN)
79+
self._confirm_btn.add_flag(lv.obj.FLAG.HIDDEN)
80+
self._cancel_btn.add_flag(lv.obj.FLAG.HIDDEN)

internal_filesystem/apps/com.micropythonos.filemanager/assets/file_manager.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import lvgl as lv
2-
from mpos import Activity, sdcard, ui
2+
from mpos import Activity, Intent, sdcard, ui
3+
from action_activity import ActionActivity
34

45
class FileManager(Activity):
56

@@ -29,7 +30,7 @@ def onResume(self, screen):
2930

3031
def file_explorer_event_cb(self, event):
3132
event_code = event.get_code()
32-
# Ignore:
33+
# Ignore:
3334
# =======
3435
# 2: PRESSING
3536
# 19: HIT_TEST
@@ -48,8 +49,11 @@ def file_explorer_event_cb(self, event):
4849
print(f"file_explorer_event_cb {event_code} with name {name}")
4950
if event_code == lv.EVENT.VALUE_CHANGED:
5051
path = self.file_explorer.explorer_get_current_path()
52+
clean_path = path[2:] if path[1] == ':' else path
5153
file = self.file_explorer.explorer_get_selected_file_name()
52-
print(f"Selected: {path}{file}")
54+
fullpath = f"{clean_path}{file}"
55+
print(f"Selected: {fullpath}")
56+
self.startActivity(Intent(activity_class=ActionActivity).putExtra("path", fullpath))
5357

5458
# Custom log callback to capture FPS
5559
def log_callback(self, level, log_str):

0 commit comments

Comments
 (0)