Skip to content

Commit f65153f

Browse files
committed
Reworked shell data flow
1 parent 6211a11 commit f65153f

6 files changed

Lines changed: 68 additions & 66 deletions

File tree

Applications/Shell/AppLauncherPicker.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ class AppLauncherPicker : public UIComponent {
2525
// Skip invalid app ID - should have been cleaned up on startup
2626
continue;
2727
}
28-
ApplicationEntry* application_entry = application_it->second;
29-
Application_Info* application_info = application_entry->info;
28+
ApplicationEntry& application_entry = application_it->second;
29+
Application_Info* application_info = (application_entry.type == ApplicationType::Native) ?
30+
application_entry.native.info :
31+
&(application_entry.python.info->info);
3032

3133

3234
uint8_t x = added_apps % 8;
@@ -60,18 +62,20 @@ class AppLauncherPicker : public UIComponent {
6062
// Skip invalid app ID - should have been cleaned up on startup
6163
return false;
6264
}
63-
ApplicationEntry* application_entry = application_it->second;
64-
Application_Info* application = application_entry->info;
65+
ApplicationEntry& application_entry = application_it->second;
66+
Application_Info* application = (application_entry.type == ApplicationType::Native) ?
67+
application_entry.native.info :
68+
&(application_entry.python.info->info);
6569

6670

6771
if(keyInfo->state == RELEASED)
6872
{
6973
MLOGD("Shell", "Launching App ID: %X", app_id);
7074
shell->LaunchAnimation(xy, application->color);
7175

72-
if (application_entry->type == ApplicationType::Python) {
76+
if (application_entry.type == ApplicationType::Python) {
7377
// Launch Python app with script path argument
74-
vector<string> args = { application_entry->python.file_path };
78+
vector<string> args = { application_entry.python.info->file_path };
7579
MatrixOS::SYS::ExecuteAPP("203 Systems", "Python", args);
7680
} else {
7781
// Launch native app normally

Applications/Shell/AppLauncherPickerEditMode.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ class AppLauncherPickerEditMode : public UIComponent {
4343
// Skip invalid app ID - should have been cleaned up on startup
4444
continue;
4545
}
46-
ApplicationEntry* application_entry = application_it->second;
47-
Application_Info* application_info = application_entry->info;
46+
ApplicationEntry& application_entry = application_it->second;
47+
Application_Info* application_info = (application_entry.type == ApplicationType::Native) ?
48+
application_entry.native.info :
49+
&(application_entry.python.info->info);
4850

4951
uint8_t x = added_apps % 8;
5052
uint8_t y = added_apps / 8;
@@ -93,8 +95,10 @@ class AppLauncherPickerEditMode : public UIComponent {
9395
// Skip invalid app ID - should have been cleaned up on startup
9496
return false;
9597
}
96-
ApplicationEntry* application_entry = application_it->second;
97-
Application_Info* application = application_entry->info;
98+
ApplicationEntry& application_entry = application_it->second;
99+
Application_Info* application = (application_entry.type == ApplicationType::Native) ?
100+
application_entry.native.info :
101+
&(application_entry.python.info->info);
98102

99103
// Check if an app is already selected
100104
if (shell->selected_app_id != 0 && shell->selected_app_id != app_id) {
@@ -134,8 +138,10 @@ class AppLauncherPickerEditMode : public UIComponent {
134138
// Skip invalid app ID - should have been cleaned up on startup
135139
return false;
136140
}
137-
ApplicationEntry* application_entry = application_it->second;
138-
Application_Info* application = application_entry->info;
141+
ApplicationEntry& application_entry = application_it->second;
142+
Application_Info* application = (application_entry.type == ApplicationType::Native) ?
143+
application_entry.native.info :
144+
&(application_entry.python.info->info);
139145

140146
MatrixOS::UIUtility::TextScroll(application->name, application->color);
141147
return true;

Applications/Shell/PythonAppDiscovery.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,46 +110,43 @@ Color ParseColorArray(JsonArrayConst color_array) {
110110
return Color(r, g, b);
111111
}
112112

113-
vector<DiscoveredPythonApp> ScanPythonApplications() {
114-
vector<DiscoveredPythonApp> discovered_apps;
115-
113+
void ScanPythonApplications(vector<PythonAppInfo>& python_app_infos) {
116114
if(MatrixOS::FileSystem::Available() == false)
117115
{
118-
return discovered_apps;
116+
return;
119117
}
120118

121119
if (!MatrixOS::FileSystem::Exists("rootfs:/MatrixOS/Applications")) {
122120
MLOGW("Shell", "Applications directory not found, creating it");
123121
MatrixOS::FileSystem::MakeDir("rootfs:/MatrixOS");
124122
MatrixOS::FileSystem::MakeDir("rootfs:/MatrixOS/Applications");
125-
return discovered_apps;
123+
return;
126124
}
127125

128-
ScanDirectory("rootfs:/MatrixOS/Applications", discovered_apps);
129-
return discovered_apps;
126+
ScanDirectory("rootfs:/MatrixOS/Applications", python_app_infos);
130127
}
131128

132-
void ScanDirectory(const string& directory_path, vector<DiscoveredPythonApp>& discovered_apps) {
129+
void ScanDirectory(const string& directory_path, vector<PythonAppInfo>& python_app_infos) {
133130
vector<string> entries = MatrixOS::FileSystem::ListDir(directory_path);
134131

135132
for (const string& entry : entries) {
136133
string full_path = directory_path + "/" + entry;
137134

138135
if (entry == "AppInfo.json") {
139136
// Found AppInfo.json, process it
140-
LoadApp(directory_path, full_path, discovered_apps);
137+
LoadApp(directory_path, full_path, python_app_infos);
141138
} else {
142139
// Check if it's a directory by trying to list it
143140
vector<string> sub_entries = MatrixOS::FileSystem::ListDir(full_path);
144141
if (!sub_entries.empty() || MatrixOS::FileSystem::Exists(full_path + "/AppInfo.json")) {
145142
// It's a directory, scan recursively
146-
ScanDirectory(full_path, discovered_apps);
143+
ScanDirectory(full_path, python_app_infos);
147144
}
148145
}
149146
}
150147
}
151148

152-
bool LoadApp(const string& directory_path, const string& json_filepath, vector<DiscoveredPythonApp>& discovered_apps) {
149+
bool LoadApp(const string& directory_path, const string& json_filepath, vector<PythonAppInfo>& python_app_infos) {
153150
// Read JSON file
154151
File file = MatrixOS::FileSystem::Open(json_filepath, "r");
155152
if (!file.Available()) {
@@ -203,11 +200,11 @@ bool LoadApp(const string& directory_path, const string& json_filepath, vector<D
203200
// Full path to Python file
204201
string python_file_path = directory_path + "/" + app_info.appMainFile;
205202

206-
// Add to discovered apps
207-
DiscoveredPythonApp discovered_app;
208-
discovered_app.info = info;
209-
discovered_app.python_file_path = python_file_path;
210-
discovered_apps.push_back(discovered_app);
203+
// Add to python app infos
204+
PythonAppInfo app_data;
205+
app_data.info = info;
206+
app_data.file_path = python_file_path;
207+
python_app_infos.push_back(app_data);
211208

212209
MLOGI("Shell", "Found Python app: %s by %s",
213210
info.name.c_str(), info.author.c_str());

Applications/Shell/PythonAppDiscovery.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ bool ValidateVersionCompatibility(const uint32_t required_version[3]);
2323
bool ValidatePythonFile(const string& directory_path, const string& python_filename);
2424
Color ParseColorArray(JsonArrayConst color_array);
2525

26-
// Structure for discovered Python apps
27-
struct DiscoveredPythonApp {
26+
// Simple struct to store Python app data
27+
struct PythonAppInfo {
2828
Application_Info info;
29-
string python_file_path;
29+
string file_path;
3030
};
3131

3232
// Directory scanning functions
33-
vector<DiscoveredPythonApp> ScanPythonApplications();
34-
void ScanDirectory(const string& directory_path, vector<DiscoveredPythonApp>& discovered_apps);
35-
bool LoadApp(const string& directory_path, const string& json_filepath, vector<DiscoveredPythonApp>& discovered_apps);
33+
void ScanPythonApplications(vector<PythonAppInfo>& python_app_infos);
34+
void ScanDirectory(const string& directory_path, vector<PythonAppInfo>& python_app_infos);
35+
bool LoadApp(const string& directory_path, const string& json_filepath, vector<PythonAppInfo>& python_app_infos);
3636

3737

3838
} // namespace PythonAppDiscovery

Applications/Shell/Shell.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void Shell::Loop() {
4747
void Shell::InitializeFolderSystem() {
4848
// Clear and initialize folders
4949
folders.clear();
50+
python_app_infos.clear();
5051
all_applications.clear();
5152

5253
// Load folder colors
@@ -61,8 +62,7 @@ void Shell::InitializeFolderSystem() {
6162

6263
// First, add all native apps to all_applications
6364
for (const auto& [app_id, app_info] : applications) {
64-
ApplicationEntry* entry = new ApplicationEntry(app_info);
65-
all_applications[app_id] = entry;
65+
all_applications.emplace(app_id, ApplicationEntry(app_info));
6666
}
6767

6868
// Then discover and add Python applications
@@ -103,9 +103,12 @@ void Shell::InitializeFolderSystem() {
103103
folders[folder_id].app_ids.push_back(app_id);
104104
missing_apps_found = true;
105105

106+
Application_Info* info = (app_entry.type == ApplicationType::Native) ?
107+
app_entry.native.info :
108+
&(app_entry.python.info->info);
106109
MLOGD("Shell", "Added app %s-%s to folder %d",
107-
app_entry->info->author.c_str(),
108-
app_entry->info->name.c_str(),
110+
info->author.c_str(),
111+
info->name.c_str(),
109112
folder_id);
110113
}
111114
}
@@ -308,9 +311,12 @@ void Shell::CleanupInvalidApps() {
308311
}
309312
}
310313

311-
uint8_t Shell::GetAppFolder(uint32_t app_id, ApplicationEntry* app_entry) {
314+
uint8_t Shell::GetAppFolder(uint32_t app_id, const ApplicationEntry& app_entry) {
312315
// Check if app is invisible first
313-
if (!app_entry->info->visibility) {
316+
Application_Info* info = (app_entry.type == ApplicationType::Native) ?
317+
app_entry.native.info :
318+
&(app_entry.python.info->info);
319+
if (!info->visibility) {
314320
return FOLDER_INVISIBLE;
315321
}
316322

@@ -329,11 +335,11 @@ uint8_t Shell::GetAppFolder(uint32_t app_id, ApplicationEntry* app_entry) {
329335
void Shell::DiscoverPythonApps() {
330336
MLOGI("Shell", "Starting Python application discovery");
331337

332-
// Get discovered Python apps
333-
vector<PythonAppDiscovery::DiscoveredPythonApp> python_apps = PythonAppDiscovery::ScanPythonApplications();
338+
// Scan Python apps directly into our storage
339+
PythonAppDiscovery::ScanPythonApplications(python_app_infos);
334340

335341
// Add them to shell's application map
336-
for (const auto& py_app : python_apps) {
342+
for (auto& py_app : python_app_infos) {
337343
uint32_t app_id = StringHash(py_app.info.author + "-" + py_app.info.name);
338344

339345
// Check for duplicates
@@ -343,15 +349,14 @@ void Shell::DiscoverPythonApps() {
343349
continue;
344350
}
345351

346-
// Create ApplicationEntry for Python app
347-
ApplicationEntry* entry = new ApplicationEntry(py_app.info, py_app.python_file_path);
348-
all_applications[app_id] = entry;
352+
// Create ApplicationEntry pointing to the stored PythonAppInfo
353+
all_applications.emplace(app_id, ApplicationEntry(&py_app));
349354

350355
MLOGD("Shell", "Registered Python app: %s-%s (ID: %X)",
351356
py_app.info.author.c_str(), py_app.info.name.c_str(), app_id);
352357
}
353358

354-
MLOGI("Shell", "Python application discovery completed: %d apps added", python_apps.size());
359+
MLOGI("Shell", "Python application discovery completed: %d apps added", python_app_infos.size());
355360
}
356361

357362
void Shell::ApplicationLauncher() {

Applications/Shell/Shell.h

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,25 @@ enum class ApplicationType : uint8_t {
1313

1414
struct ApplicationEntry {
1515
ApplicationType type;
16-
Application_Info* info; // Points to either native.info or python.info
1716

1817
union {
1918
struct {
20-
Application_Info* info; // Points to registered global info (not owned)
19+
Application_Info* info; // Points to Application_Info regardless of type
2120
} native;
2221
struct {
23-
Application_Info info; // Owned copy for Python apps
24-
string file_path; // Python script path
22+
PythonAppDiscovery::PythonAppInfo* info; // Points to PythonAppInfo for Python apps
2523
} python;
2624
};
2725

2826
ApplicationEntry(Application_Info* native_app_info)
2927
: type(ApplicationType::Native) {
3028
native.info = native_app_info;
31-
info = native.info;
3229
}
3330

34-
ApplicationEntry(const Application_Info& py_info, const string& file_path)
31+
ApplicationEntry(PythonAppDiscovery::PythonAppInfo* py_app_info)
3532
: type(ApplicationType::Python) {
36-
// Use placement new to construct the union members properly
37-
new (&python.info) Application_Info(py_info);
38-
new (&python.file_path) string(file_path);
39-
info = &python.info;
33+
python.info = py_app_info;
4034
}
41-
42-
~ApplicationEntry() {
43-
if (type == ApplicationType::Python) {
44-
python.info.~Application_Info();
45-
python.file_path.~string();
46-
}
47-
}
4835
};
4936

5037
class Shell : public Application {
@@ -75,8 +62,11 @@ class Shell : public Application {
7562
// Folder definitions (0-5)
7663
std::unordered_map<uint8_t, Folder> folders;
7764

65+
// Storage for Python app infos (to keep Application_Info objects alive)
66+
std::vector<PythonAppDiscovery::PythonAppInfo> python_app_infos;
67+
7868
// Shell's view of all applications (native + Python)
79-
std::unordered_map<uint32_t, ApplicationEntry*> all_applications;
69+
std::unordered_map<uint32_t, ApplicationEntry> all_applications;
8070

8171
uint8_t current_folder = 0; // Start with folder 0
8272
uint32_t selected_app_id = 0; // Currently selected app in edit mode (0 = none)
@@ -87,7 +77,7 @@ class Shell : public Application {
8777
// Folder system functions
8878
void InitializeFolderSystem();
8979
void TestFileSystem();
90-
uint8_t GetAppFolder(uint32_t app_id, ApplicationEntry* app_entry);
80+
uint8_t GetAppFolder(uint32_t app_id, const ApplicationEntry& app_entry);
9181
bool EnableFolder(uint8_t folder_idx, Color color);
9282
void DisableFolder(uint8_t folder_id);
9383
void DeleteFolder(uint8_t folder_id);

0 commit comments

Comments
 (0)