Refactor app loading and window management (#609)
This commit is contained in:
committed by
GitHub
parent
dc3f6104b8
commit
37c507544b
@@ -1,114 +1,168 @@
|
||||
#include <Tactility/app/App.h>
|
||||
#include <Tactility/app/AppContext.h>
|
||||
#include <Tactility/app/AppManifest.h>
|
||||
#include <Tactility/app/alertdialog/AlertDialog.h>
|
||||
#include <Tactility/lvgl/Style.h>
|
||||
#include <Tactility/StringUtils.h>
|
||||
#include <Tactility/file/File.h>
|
||||
#include <app/event.h>
|
||||
#include <app/manager.h>
|
||||
#include <app/manifest.h>
|
||||
#include <app/install.h>
|
||||
|
||||
#include <tactility/check.h>
|
||||
#include <format>
|
||||
|
||||
#include <lvgl/lvgl.h>
|
||||
#include <lvgl/widgets/toolbar.h>
|
||||
|
||||
#include <format>
|
||||
#include <lvgl_window_manager/window_manager.h>
|
||||
|
||||
#include <Tactility/StringUtils.h>
|
||||
#include <Tactility/app/alertdialog/AlertDialog.h>
|
||||
#include <Tactility/file/File.h>
|
||||
#include <Tactility/lvgl/Style.h>
|
||||
|
||||
#include <tactility/log.h>
|
||||
|
||||
constexpr auto* TAG = "AppDetails";
|
||||
|
||||
namespace tt::app::appdetails {
|
||||
|
||||
extern const AppManifest manifest;
|
||||
extern const ::AppManifest manifest;
|
||||
|
||||
void start(const std::string& appId) {
|
||||
auto bundle = std::make_shared<Bundle>();
|
||||
bundle->putString("appId", appId);
|
||||
app::start(manifest.appId, bundle);
|
||||
namespace {
|
||||
|
||||
struct Context {
|
||||
uint32_t appInstanceId;
|
||||
std::string targetAppId;
|
||||
// findAppManifestById() returns the old-model registry's AppManifest type - AppDetails
|
||||
// shows details for apps in that registry regardless of which system they run under.
|
||||
AppManifest targetManifest = { };
|
||||
uint32_t pendingUninstallDialogId = 0;
|
||||
};
|
||||
|
||||
|
||||
void onPressUninstall(lv_event_t* event) {
|
||||
auto* ctx = static_cast<Context*>(lv_event_get_user_data(event));
|
||||
std::vector<std::string> choices = { "Yes", "No" };
|
||||
ctx->pendingUninstallDialogId = alertdialog::start(
|
||||
ctx->appInstanceId,
|
||||
"Confirmation",
|
||||
std::format("Uninstall {}?", ctx->targetManifest.name),
|
||||
choices
|
||||
);
|
||||
}
|
||||
|
||||
class AppDetailsApp : public App {
|
||||
void onBackPressed(lv_event_t* event) {
|
||||
auto* ctx = static_cast<Context*>(lv_event_get_user_data(event));
|
||||
// Async, non-blocking - must NOT call app_manager_stop() directly here: that bound-waits
|
||||
// (thread_join) for this app's own thread to finish, which needs the LVGL lock
|
||||
// (window_manager_remove()) - but this callback runs ON the LVGL task, which would
|
||||
// deadlock against itself.
|
||||
AppEvent closeEvent { .type = APP_EVENT_CLOSE, .timestamp = 0, .result = {} };
|
||||
app_event_emit(ctx->appInstanceId, &closeEvent);
|
||||
}
|
||||
|
||||
std::shared_ptr<AppManifest> manifest;
|
||||
void createWidgets(lv_obj_t* parent, void* userData) {
|
||||
auto* ctx = static_cast<Context*>(userData);
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_style_pad_row(parent, 0, LV_STATE_DEFAULT);
|
||||
|
||||
static void onPressUninstall(lv_event_t* event) {
|
||||
auto* self = static_cast<AppDetailsApp*>(lv_event_get_user_data(event));
|
||||
std::vector<std::string> choices = {
|
||||
"Yes",
|
||||
"No"
|
||||
};
|
||||
alertdialog::start("Confirmation", std::format("Uninstall {}?", self->manifest->appName), choices);
|
||||
}
|
||||
auto title = std::format("{} details", ctx->targetManifest.name);
|
||||
auto* toolbar = lvgl_toolbar_create(parent, title.c_str());
|
||||
// The global toolbar nav callback only knows how to stop old-model apps.
|
||||
lvgl_toolbar_set_nav_action(toolbar, LV_SYMBOL_CLOSE, onBackPressed, ctx);
|
||||
|
||||
public:
|
||||
auto* wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_width(wrapper, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(wrapper, 1);
|
||||
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_style_border_width(wrapper, 0, LV_STATE_DEFAULT);
|
||||
lvgl::obj_set_style_bg_invisible(wrapper);
|
||||
|
||||
void onCreate(AppContext& app) override {
|
||||
const auto parameters = app.getParameters();
|
||||
check(parameters != nullptr, "Parameters missing");
|
||||
auto app_id = parameters->getString("appId");
|
||||
manifest = findAppManifestById(app_id);
|
||||
assert(manifest != nullptr);
|
||||
}
|
||||
auto identifier = std::format("Identifier: {}", ctx->targetManifest.id);
|
||||
auto* identifier_label = lv_label_create(wrapper);
|
||||
lv_label_set_text(identifier_label, identifier.c_str());
|
||||
|
||||
void onShow(AppContext& app, lv_obj_t* parent) override {
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_style_pad_row(parent, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto title = std::format("{} details", manifest->appName);
|
||||
lvgl_toolbar_create(parent, title.c_str());
|
||||
|
||||
auto* wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_width(wrapper, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(wrapper, 1);
|
||||
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_style_border_width(wrapper, 0, LV_STATE_DEFAULT);
|
||||
lvgl::obj_set_style_bg_invisible(wrapper);
|
||||
|
||||
auto identifier = std::format("Identifier: {}", manifest->appId);
|
||||
auto* identifier_label = lv_label_create(wrapper);
|
||||
lv_label_set_text(identifier_label, identifier.c_str());
|
||||
|
||||
auto* location_label = lv_label_create(wrapper);
|
||||
std::string location;
|
||||
if (manifest->appLocation.isInternal()) {
|
||||
location = "internal";
|
||||
} else {
|
||||
if (!string::getPathParent(manifest->appLocation.getPath(), location)) {
|
||||
location = "external";
|
||||
}
|
||||
auto* location_label = lv_label_create(wrapper);
|
||||
std::string location;
|
||||
bool is_internal = ctx->targetManifest.location.type == APP_LOCATION_MEMORY;
|
||||
bool is_external = ctx->targetManifest.location.type == APP_LOCATION_PATH;
|
||||
if (is_internal) {
|
||||
location = "internal";
|
||||
} else if (is_external) {
|
||||
if (!string::getPathParent(static_cast<const char*>(ctx->targetManifest.location.location), location)) {
|
||||
location = "external";
|
||||
}
|
||||
std::string location_label_text = std::format("Location: {}", location);
|
||||
lv_label_set_text(location_label, location_label_text.c_str());
|
||||
} else {
|
||||
LOG_E(TAG, "Unknown app location type %d", ctx->targetManifest.location.type);
|
||||
return;
|
||||
}
|
||||
std::string location_label_text = std::format("Location: {}", location);
|
||||
lv_label_set_text(location_label, location_label_text.c_str());
|
||||
|
||||
if (manifest->appLocation.isExternal()) {
|
||||
auto* uninstall_button = lv_button_create(wrapper);
|
||||
lv_obj_set_width(uninstall_button, LV_PCT(100));
|
||||
lv_obj_add_event_cb(uninstall_button, onPressUninstall, LV_EVENT_SHORT_CLICKED, this);
|
||||
auto* uninstall_label = lv_label_create(uninstall_button);
|
||||
lv_obj_align(uninstall_label, LV_ALIGN_CENTER, 0, 0);
|
||||
lv_label_set_text(uninstall_label, "Uninstall");
|
||||
if (is_external) {
|
||||
auto* uninstall_button = lv_button_create(wrapper);
|
||||
lv_obj_set_width(uninstall_button, LV_PCT(100));
|
||||
lv_obj_add_event_cb(uninstall_button, onPressUninstall, LV_EVENT_SHORT_CLICKED, ctx);
|
||||
auto* uninstall_label = lv_label_create(uninstall_button);
|
||||
lv_obj_align(uninstall_label, LV_ALIGN_CENTER, 0, 0);
|
||||
lv_label_set_text(uninstall_label, "Uninstall");
|
||||
}
|
||||
}
|
||||
|
||||
int32_t appMain(uint32_t appInstanceId, int argc, char* argv[]) {
|
||||
|
||||
Context ctx {};
|
||||
ctx.appInstanceId = appInstanceId;
|
||||
ctx.targetAppId = (argc > 0) ? argv[0] : std::string();
|
||||
ctx.targetManifest = *app_manager_find_manifest(ctx.targetAppId.c_str());
|
||||
|
||||
AppEventSubscription sub {};
|
||||
sub.app_instance_id = appInstanceId;
|
||||
app_event_subscribe(&sub);
|
||||
|
||||
WindowId window = window_manager_create(appInstanceId, createWidgets, &ctx);
|
||||
|
||||
bool shouldClose = false;
|
||||
while (!shouldClose) {
|
||||
AppEvent event {};
|
||||
if (app_event_await(&sub, &event, portMAX_DELAY) != ERROR_NONE) {
|
||||
break;
|
||||
}
|
||||
switch (event.type) {
|
||||
case APP_EVENT_CLOSE:
|
||||
app_manager_finish(appInstanceId);
|
||||
shouldClose = true;
|
||||
break;
|
||||
case APP_EVENT_RESULT:
|
||||
if (event.result.launch_id == ctx.pendingUninstallDialogId) {
|
||||
if (event.result.result == 0) { // 0 = Yes
|
||||
app_uninstall(ctx.targetManifest.id);
|
||||
app_manager_finish(appInstanceId);
|
||||
shouldClose = true;
|
||||
}
|
||||
app_manager_stop(event.result.launch_id);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void onResult(AppContext& appContext, LaunchId launchId, Result result, std::unique_ptr<Bundle> bundle) override {
|
||||
if (result != Result::Ok || bundle == nullptr) {
|
||||
return;
|
||||
}
|
||||
window_manager_remove(window);
|
||||
app_event_unsubscribe(&sub);
|
||||
|
||||
if (alertdialog::getResultIndex(*bundle) != 0) { // 0 = Yes
|
||||
return;
|
||||
}
|
||||
|
||||
uninstall(manifest->appId);
|
||||
|
||||
// Stop app
|
||||
stop();
|
||||
}
|
||||
};
|
||||
|
||||
extern const AppManifest manifest = {
|
||||
.appId = "AppDetails",
|
||||
.appName = "App Details",
|
||||
.appCategory = Category::System,
|
||||
.appFlags = AppManifest::Flags::Hidden,
|
||||
.createApp = create<AppDetailsApp>
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void start(const std::string& appId) {
|
||||
const char* argv[] = { appId.c_str() };
|
||||
uint32_t instanceId = 0;
|
||||
app_manager_start_with_parameters(manifest.id, 1, argv, &instanceId);
|
||||
}
|
||||
|
||||
extern const ::AppManifest manifest = {
|
||||
.id = "AppDetails",
|
||||
.name = "App Details",
|
||||
.category = APP_CATEGORY_SYSTEM,
|
||||
.location = { APP_LOCATION_MEMORY, reinterpret_cast<void*>(appMain) },
|
||||
.flags = APP_MANIFEST_FLAG_HIDDEN,
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user