Refactor app launching (#174)
- Refactor the way apps work: Instead of a C interface, they are now C++ classes. The main reasoning is that attaching data to an app was cumbersome. Having different implementations for different kinds of apps was cumbersome too. (3 or 4 layers of manifest nesting for the TactilityC project) - External apps are still written in C, but they get a createData/destroyData in their manifest, so: - External apps now have their own manifest. - All functions in the original AppManifest are removed and replaced by a single `createApp` function - External apps now automatically register (each app individually!) when they run the first time. As a side-effect they become visible in the `AppList` app! - Adapted all apps for the new interface. - Adapted all internal logic for these changes (Gui, ViewPort, Loader, AppContext, AppInstance, etc.) - Rewrote parts of Loader to use std::shared_ptr to make the code much safer. - Added a refcount check for the `AppInstance` and `App` at the end of their lifecycle. Show warning if refcount is too high.
This commit is contained in:
committed by
GitHub
parent
2bbd44a8b5
commit
c3bcf93698
@@ -13,119 +13,104 @@ namespace tt::app::timedatesettings {
|
||||
|
||||
extern const AppManifest manifest;
|
||||
|
||||
struct Data {
|
||||
class TimeDateSettingsApp : public App {
|
||||
|
||||
private:
|
||||
|
||||
Mutex mutex = Mutex(Mutex::Type::Recursive);
|
||||
lv_obj_t* regionLabelWidget = nullptr;
|
||||
};
|
||||
|
||||
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
|
||||
std::shared_ptr<Data> _Nullable optData() {
|
||||
app::AppContext* app = service::loader::getCurrentApp();
|
||||
if (app->getManifest().id == manifest.id) {
|
||||
return std::static_pointer_cast<Data>(app->getData());
|
||||
} else {
|
||||
return nullptr;
|
||||
static void onConfigureTimeZonePressed(TT_UNUSED lv_event_t* event) {
|
||||
timezone::start();
|
||||
}
|
||||
}
|
||||
|
||||
static void onConfigureTimeZonePressed(TT_UNUSED lv_event_t* event) {
|
||||
timezone::start();
|
||||
}
|
||||
|
||||
static void onTimeFormatChanged(lv_event_t* event) {
|
||||
auto* widget = lv_event_get_target_obj(event);
|
||||
bool show_24 = lv_obj_has_state(widget, LV_STATE_CHECKED);
|
||||
time::setTimeFormat24Hour(show_24);
|
||||
}
|
||||
|
||||
static void onShow(AppContext& app, lv_obj_t* parent) {
|
||||
auto data = std::static_pointer_cast<Data>(app.getData());
|
||||
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
|
||||
lvgl::toolbar_create(parent, app);
|
||||
|
||||
auto* main_wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_flex_flow(main_wrapper, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_width(main_wrapper, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(main_wrapper, 1);
|
||||
|
||||
auto* region_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_width(region_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(region_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(region_wrapper, 0, 0);
|
||||
lv_obj_set_style_border_width(region_wrapper, 0, 0);
|
||||
|
||||
auto* region_prefix_label = lv_label_create(region_wrapper);
|
||||
lv_label_set_text(region_prefix_label, "Region: ");
|
||||
lv_obj_align(region_prefix_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
|
||||
auto* region_label = lv_label_create(region_wrapper);
|
||||
std::string timeZoneName = time::getTimeZoneName();
|
||||
if (timeZoneName.empty()) {
|
||||
timeZoneName = "not set";
|
||||
static void onTimeFormatChanged(lv_event_t* event) {
|
||||
auto* widget = lv_event_get_target_obj(event);
|
||||
bool show_24 = lv_obj_has_state(widget, LV_STATE_CHECKED);
|
||||
time::setTimeFormat24Hour(show_24);
|
||||
}
|
||||
data->regionLabelWidget = region_label;
|
||||
lv_label_set_text(region_label, timeZoneName.c_str());
|
||||
// TODO: Find out why Y offset is needed
|
||||
lv_obj_align_to(region_label, region_prefix_label, LV_ALIGN_OUT_RIGHT_MID, 0, 8);
|
||||
|
||||
auto* region_button = lv_button_create(region_wrapper);
|
||||
lv_obj_align(region_button, LV_ALIGN_TOP_RIGHT, 0, 0);
|
||||
auto* region_button_image = lv_image_create(region_button);
|
||||
lv_obj_add_event_cb(region_button, onConfigureTimeZonePressed, LV_EVENT_SHORT_CLICKED, nullptr);
|
||||
lv_image_set_src(region_button_image, LV_SYMBOL_SETTINGS);
|
||||
public:
|
||||
|
||||
auto* time_format_wrapper= lv_obj_create(main_wrapper);
|
||||
lv_obj_set_width(time_format_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(time_format_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(time_format_wrapper, 0, 0);
|
||||
lv_obj_set_style_border_width(time_format_wrapper, 0, 0);
|
||||
void onShow(AppContext& app, lv_obj_t* parent) override {
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
|
||||
auto* time_24h_label = lv_label_create(time_format_wrapper);
|
||||
lv_label_set_text(time_24h_label, "24-hour clock");
|
||||
lv_obj_align(time_24h_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
lvgl::toolbar_create(parent, app);
|
||||
|
||||
auto* time_24h_switch = lv_switch_create(time_format_wrapper);
|
||||
lv_obj_align(time_24h_switch, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(time_24h_switch, onTimeFormatChanged, LV_EVENT_VALUE_CHANGED, nullptr);
|
||||
if (time::isTimeFormat24Hour()) {
|
||||
lv_obj_add_state(time_24h_switch, LV_STATE_CHECKED);
|
||||
} else {
|
||||
lv_obj_remove_state(time_24h_switch, LV_STATE_CHECKED);
|
||||
auto* main_wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_flex_flow(main_wrapper, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_width(main_wrapper, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(main_wrapper, 1);
|
||||
|
||||
auto* region_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_width(region_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(region_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(region_wrapper, 0, 0);
|
||||
lv_obj_set_style_border_width(region_wrapper, 0, 0);
|
||||
|
||||
auto* region_prefix_label = lv_label_create(region_wrapper);
|
||||
lv_label_set_text(region_prefix_label, "Region: ");
|
||||
lv_obj_align(region_prefix_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
|
||||
auto* region_label = lv_label_create(region_wrapper);
|
||||
std::string timeZoneName = time::getTimeZoneName();
|
||||
if (timeZoneName.empty()) {
|
||||
timeZoneName = "not set";
|
||||
}
|
||||
regionLabelWidget = region_label;
|
||||
lv_label_set_text(region_label, timeZoneName.c_str());
|
||||
// TODO: Find out why Y offset is needed
|
||||
lv_obj_align_to(region_label, region_prefix_label, LV_ALIGN_OUT_RIGHT_MID, 0, 8);
|
||||
|
||||
auto* region_button = lv_button_create(region_wrapper);
|
||||
lv_obj_align(region_button, LV_ALIGN_TOP_RIGHT, 0, 0);
|
||||
auto* region_button_image = lv_image_create(region_button);
|
||||
lv_obj_add_event_cb(region_button, onConfigureTimeZonePressed, LV_EVENT_SHORT_CLICKED, nullptr);
|
||||
lv_image_set_src(region_button_image, LV_SYMBOL_SETTINGS);
|
||||
|
||||
auto* time_format_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_width(time_format_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(time_format_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(time_format_wrapper, 0, 0);
|
||||
lv_obj_set_style_border_width(time_format_wrapper, 0, 0);
|
||||
|
||||
auto* time_24h_label = lv_label_create(time_format_wrapper);
|
||||
lv_label_set_text(time_24h_label, "24-hour clock");
|
||||
lv_obj_align(time_24h_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
|
||||
auto* time_24h_switch = lv_switch_create(time_format_wrapper);
|
||||
lv_obj_align(time_24h_switch, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(time_24h_switch, onTimeFormatChanged, LV_EVENT_VALUE_CHANGED, nullptr);
|
||||
if (time::isTimeFormat24Hour()) {
|
||||
lv_obj_add_state(time_24h_switch, LV_STATE_CHECKED);
|
||||
} else {
|
||||
lv_obj_remove_state(time_24h_switch, LV_STATE_CHECKED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto data = std::make_shared<Data>();
|
||||
app.setData(data);
|
||||
}
|
||||
void onResult(AppContext& app, Result result, std::unique_ptr<Bundle> bundle) override {
|
||||
if (result == Result::Ok) {
|
||||
auto name = timezone::getResultName(*bundle);
|
||||
auto code = timezone::getResultCode(*bundle);
|
||||
TT_LOG_I(TAG, "Result name=%s code=%s", name.c_str(), code.c_str());
|
||||
time::setTimeZone(name, code);
|
||||
|
||||
static void onResult(AppContext& app, Result result, const Bundle& bundle) {
|
||||
if (result == ResultOk) {
|
||||
auto data = std::static_pointer_cast<Data>(app.getData());
|
||||
auto name = timezone::getResultName(bundle);
|
||||
auto code = timezone::getResultCode(bundle);
|
||||
TT_LOG_I(TAG, "Result name=%s code=%s", name.c_str(), code.c_str());
|
||||
time::setTimeZone(name, code);
|
||||
|
||||
if (!name.empty()) {
|
||||
if (lvgl::lock(100 / portTICK_PERIOD_MS)) {
|
||||
lv_label_set_text(data->regionLabelWidget, name.c_str());
|
||||
lvgl::unlock();
|
||||
if (!name.empty()) {
|
||||
if (lvgl::lock(100 / portTICK_PERIOD_MS)) {
|
||||
lv_label_set_text(regionLabelWidget, name.c_str());
|
||||
lvgl::unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
extern const AppManifest manifest = {
|
||||
.id = "TimeDateSettings",
|
||||
.name = "Time & Date",
|
||||
.icon = TT_ASSETS_APP_ICON_TIME_DATE_SETTINGS,
|
||||
.type = TypeSettings,
|
||||
.onStart = onStart,
|
||||
.onShow = onShow,
|
||||
.onResult = onResult
|
||||
.type = Type::Settings,
|
||||
.createApp = create<TimeDateSettingsApp>
|
||||
};
|
||||
|
||||
void start() {
|
||||
|
||||
Reference in New Issue
Block a user