Refactor app loading and window management (#609)
This commit is contained in:
committed by
GitHub
parent
dc3f6104b8
commit
37c507544b
@@ -3,16 +3,23 @@
|
||||
#include <Tactility/Tactility.h>
|
||||
|
||||
#include <Tactility/settings/KeyboardSettings.h>
|
||||
#include <Tactility/lvgl/Toolbar.h>
|
||||
|
||||
#include <lvgl/icons/shared.h>
|
||||
#include <app/event.h>
|
||||
#include <app/manager.h>
|
||||
#include <app/manifest.h>
|
||||
|
||||
#include <lvgl_window_manager/window_manager.h>
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/backlight.h>
|
||||
|
||||
#include <lvgl.h>
|
||||
#include <lvgl/widgets/toolbar.h>
|
||||
|
||||
namespace tt::app::keyboardsettings {
|
||||
|
||||
extern const ::AppManifest manifest;
|
||||
|
||||
constexpr auto* TAG = "KeyboardSettings";
|
||||
|
||||
// Shared timeout values: 15s, 30s, 1m, 2m, 5m, Never (0)
|
||||
@@ -35,157 +42,209 @@ static void applyKeyboardBacklight(bool enabled, uint8_t brightness) {
|
||||
}
|
||||
}
|
||||
|
||||
class KeyboardSettingsApp final : public App {
|
||||
namespace {
|
||||
|
||||
struct Context {
|
||||
uint32_t appInstanceId;
|
||||
settings::keyboard::KeyboardSettings kbSettings;
|
||||
bool updated = false;
|
||||
lv_obj_t* switchBacklight = nullptr;
|
||||
lv_obj_t* sliderBrightness = nullptr;
|
||||
lv_obj_t* switchTimeoutEnable = nullptr;
|
||||
lv_obj_t* timeoutDropdown = nullptr;
|
||||
|
||||
static void onBacklightSwitch(lv_event_t* e) {
|
||||
auto* app = static_cast<KeyboardSettingsApp*>(lv_event_get_user_data(e));
|
||||
bool enabled = lv_obj_has_state(app->switchBacklight, LV_STATE_CHECKED);
|
||||
app->kbSettings.backlightEnabled = enabled;
|
||||
app->updated = true;
|
||||
if (app->sliderBrightness) {
|
||||
if (enabled) lv_obj_clear_state(app->sliderBrightness, LV_STATE_DISABLED);
|
||||
else lv_obj_add_state(app->sliderBrightness, LV_STATE_DISABLED);
|
||||
}
|
||||
applyKeyboardBacklight(enabled, app->kbSettings.backlightBrightness);
|
||||
}
|
||||
|
||||
static void onBrightnessChanged(lv_event_t* e) {
|
||||
auto* app = static_cast<KeyboardSettingsApp*>(lv_event_get_user_data(e));
|
||||
int32_t v = lv_slider_get_value(app->sliderBrightness);
|
||||
app->kbSettings.backlightBrightness = static_cast<uint8_t>(v);
|
||||
app->updated = true;
|
||||
if (app->kbSettings.backlightEnabled) {
|
||||
applyKeyboardBacklight(true, app->kbSettings.backlightBrightness);
|
||||
}
|
||||
}
|
||||
|
||||
static void onTimeoutEnableSwitch(lv_event_t* e) {
|
||||
auto* app = static_cast<KeyboardSettingsApp*>(lv_event_get_user_data(e));
|
||||
bool enabled = lv_obj_has_state(app->switchTimeoutEnable, LV_STATE_CHECKED);
|
||||
app->kbSettings.backlightTimeoutEnabled = enabled;
|
||||
app->updated = true;
|
||||
if (app->timeoutDropdown) {
|
||||
if (enabled) {
|
||||
lv_obj_clear_state(app->timeoutDropdown, LV_STATE_DISABLED);
|
||||
} else {
|
||||
lv_obj_add_state(app->timeoutDropdown, LV_STATE_DISABLED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void onTimeoutChanged(lv_event_t* event) {
|
||||
auto* app = static_cast<KeyboardSettingsApp*>(lv_event_get_user_data(event));
|
||||
auto* dropdown = static_cast<lv_obj_t*>(lv_event_get_target(event));
|
||||
uint32_t idx = lv_dropdown_get_selected(dropdown);
|
||||
if (idx < (sizeof(TIMEOUT_VALUES_MS) / sizeof(TIMEOUT_VALUES_MS[0]))) {
|
||||
app->kbSettings.backlightTimeoutMs = TIMEOUT_VALUES_MS[idx];
|
||||
app->updated = true;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void onShow(AppContext& app, lv_obj_t* parent) override {
|
||||
kbSettings = settings::keyboard::loadOrGetDefault();
|
||||
updated = false;
|
||||
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_style_pad_row(parent, 0, LV_STATE_DEFAULT);
|
||||
|
||||
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);
|
||||
|
||||
// Keyboard backlight toggle
|
||||
auto* bl_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_size(bl_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(bl_wrapper, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_width(bl_wrapper, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto* bl_label = lv_label_create(bl_wrapper);
|
||||
lv_label_set_text(bl_label, "Keyboard backlight");
|
||||
lv_obj_align(bl_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
switchBacklight = lv_switch_create(bl_wrapper);
|
||||
if (kbSettings.backlightEnabled) lv_obj_add_state(switchBacklight, LV_STATE_CHECKED);
|
||||
lv_obj_align(switchBacklight, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(switchBacklight, onBacklightSwitch, LV_EVENT_VALUE_CHANGED, this);
|
||||
|
||||
// Brightness slider
|
||||
auto* br_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_size(br_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(br_wrapper, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_width(br_wrapper, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto* br_label = lv_label_create(br_wrapper);
|
||||
lv_label_set_text(br_label, "Brightness");
|
||||
lv_obj_align(br_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
sliderBrightness = lv_slider_create(br_wrapper);
|
||||
lv_obj_set_width(sliderBrightness, LV_PCT(50));
|
||||
lv_obj_align(sliderBrightness, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_slider_set_range(sliderBrightness, 0, 255);
|
||||
lv_slider_set_value(sliderBrightness, kbSettings.backlightBrightness, LV_ANIM_OFF);
|
||||
if (!kbSettings.backlightEnabled) lv_obj_add_state(sliderBrightness, LV_STATE_DISABLED);
|
||||
lv_obj_add_event_cb(sliderBrightness, onBrightnessChanged, LV_EVENT_VALUE_CHANGED, this);
|
||||
|
||||
// Backlight timeout enable
|
||||
auto* to_enable_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_size(to_enable_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(to_enable_wrapper, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_width(to_enable_wrapper, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto* to_enable_label = lv_label_create(to_enable_wrapper);
|
||||
lv_label_set_text(to_enable_label, "Auto backlight off");
|
||||
lv_obj_align(to_enable_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
switchTimeoutEnable = lv_switch_create(to_enable_wrapper);
|
||||
if (kbSettings.backlightTimeoutEnabled) lv_obj_add_state(switchTimeoutEnable, LV_STATE_CHECKED);
|
||||
lv_obj_align(switchTimeoutEnable, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(switchTimeoutEnable, onTimeoutEnableSwitch, LV_EVENT_VALUE_CHANGED, this);
|
||||
|
||||
auto* timeout_select_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_size(timeout_select_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(timeout_select_wrapper, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_width(timeout_select_wrapper, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto* timeout_value_label = lv_label_create(timeout_select_wrapper);
|
||||
lv_label_set_text(timeout_value_label, "Timeout");
|
||||
lv_obj_align(timeout_value_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
|
||||
// Backlight timeout value (seconds)
|
||||
timeoutDropdown = lv_dropdown_create(timeout_select_wrapper);
|
||||
lv_dropdown_set_options(timeoutDropdown, "15 seconds\n30 seconds\n1 minute\n2 minutes\n5 minutes\nNever");
|
||||
lv_obj_align(timeoutDropdown, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(timeoutDropdown, onTimeoutChanged, LV_EVENT_VALUE_CHANGED, this);
|
||||
// Initialize dropdown selection from settings
|
||||
lv_dropdown_set_selected(timeoutDropdown, timeoutMsToIndex(kbSettings.backlightTimeoutMs));
|
||||
if (!kbSettings.backlightTimeoutEnabled) {
|
||||
lv_obj_add_state(timeoutDropdown, LV_STATE_DISABLED);
|
||||
}
|
||||
}
|
||||
|
||||
void onHide(AppContext& app) override {
|
||||
if (updated) {
|
||||
const auto copy = kbSettings;
|
||||
getMainDispatcher().dispatch([copy]{ settings::keyboard::save(copy); });
|
||||
updated = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
extern const AppManifest manifest = {
|
||||
.appId = "KeyboardSettings",
|
||||
.appName = "Keyboard",
|
||||
.appIcon = LVGL_ICON_SHARED_KEYBOARD_ALT,
|
||||
.appCategory = Category::Settings,
|
||||
.createApp = create<KeyboardSettingsApp>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void onBacklightSwitch(lv_event_t* e) {
|
||||
auto* ctx = static_cast<Context*>(lv_event_get_user_data(e));
|
||||
bool enabled = lv_obj_has_state(ctx->switchBacklight, LV_STATE_CHECKED);
|
||||
ctx->kbSettings.backlightEnabled = enabled;
|
||||
ctx->updated = true;
|
||||
if (ctx->sliderBrightness) {
|
||||
if (enabled) lv_obj_clear_state(ctx->sliderBrightness, LV_STATE_DISABLED);
|
||||
else lv_obj_add_state(ctx->sliderBrightness, LV_STATE_DISABLED);
|
||||
}
|
||||
applyKeyboardBacklight(enabled, ctx->kbSettings.backlightBrightness);
|
||||
}
|
||||
|
||||
void onBrightnessChanged(lv_event_t* e) {
|
||||
auto* ctx = static_cast<Context*>(lv_event_get_user_data(e));
|
||||
int32_t v = lv_slider_get_value(ctx->sliderBrightness);
|
||||
ctx->kbSettings.backlightBrightness = static_cast<uint8_t>(v);
|
||||
ctx->updated = true;
|
||||
if (ctx->kbSettings.backlightEnabled) {
|
||||
applyKeyboardBacklight(true, ctx->kbSettings.backlightBrightness);
|
||||
}
|
||||
}
|
||||
|
||||
void onTimeoutEnableSwitch(lv_event_t* e) {
|
||||
auto* ctx = static_cast<Context*>(lv_event_get_user_data(e));
|
||||
bool enabled = lv_obj_has_state(ctx->switchTimeoutEnable, LV_STATE_CHECKED);
|
||||
ctx->kbSettings.backlightTimeoutEnabled = enabled;
|
||||
ctx->updated = true;
|
||||
if (ctx->timeoutDropdown) {
|
||||
if (enabled) {
|
||||
lv_obj_clear_state(ctx->timeoutDropdown, LV_STATE_DISABLED);
|
||||
} else {
|
||||
lv_obj_add_state(ctx->timeoutDropdown, LV_STATE_DISABLED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void onTimeoutChanged(lv_event_t* event) {
|
||||
auto* ctx = static_cast<Context*>(lv_event_get_user_data(event));
|
||||
auto* dropdown = static_cast<lv_obj_t*>(lv_event_get_target(event));
|
||||
uint32_t idx = lv_dropdown_get_selected(dropdown);
|
||||
if (idx < (sizeof(TIMEOUT_VALUES_MS) / sizeof(TIMEOUT_VALUES_MS[0]))) {
|
||||
ctx->kbSettings.backlightTimeoutMs = TIMEOUT_VALUES_MS[idx];
|
||||
ctx->updated = true;
|
||||
}
|
||||
}
|
||||
|
||||
void createWidgets(lv_obj_t* parent, void* userData) {
|
||||
auto* ctx = static_cast<Context*>(userData);
|
||||
|
||||
ctx->kbSettings = settings::keyboard::loadOrGetDefault();
|
||||
ctx->updated = false;
|
||||
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_style_pad_row(parent, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto* toolbar = lvgl_toolbar_create(parent, "Keyboard");
|
||||
// The global toolbar nav callback only knows how to stop old-model apps.
|
||||
lvgl_toolbar_set_nav_action(toolbar, LV_SYMBOL_CLOSE, onBackPressed, ctx);
|
||||
|
||||
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);
|
||||
|
||||
// Keyboard backlight toggle
|
||||
auto* bl_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_size(bl_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(bl_wrapper, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_width(bl_wrapper, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto* bl_label = lv_label_create(bl_wrapper);
|
||||
lv_label_set_text(bl_label, "Keyboard backlight");
|
||||
lv_obj_align(bl_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
ctx->switchBacklight = lv_switch_create(bl_wrapper);
|
||||
if (ctx->kbSettings.backlightEnabled) lv_obj_add_state(ctx->switchBacklight, LV_STATE_CHECKED);
|
||||
lv_obj_align(ctx->switchBacklight, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(ctx->switchBacklight, onBacklightSwitch, LV_EVENT_VALUE_CHANGED, ctx);
|
||||
|
||||
// Brightness slider
|
||||
auto* br_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_size(br_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(br_wrapper, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_width(br_wrapper, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto* br_label = lv_label_create(br_wrapper);
|
||||
lv_label_set_text(br_label, "Brightness");
|
||||
lv_obj_align(br_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
ctx->sliderBrightness = lv_slider_create(br_wrapper);
|
||||
lv_obj_set_width(ctx->sliderBrightness, LV_PCT(50));
|
||||
lv_obj_align(ctx->sliderBrightness, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_slider_set_range(ctx->sliderBrightness, 0, 255);
|
||||
lv_slider_set_value(ctx->sliderBrightness, ctx->kbSettings.backlightBrightness, LV_ANIM_OFF);
|
||||
if (!ctx->kbSettings.backlightEnabled) lv_obj_add_state(ctx->sliderBrightness, LV_STATE_DISABLED);
|
||||
lv_obj_add_event_cb(ctx->sliderBrightness, onBrightnessChanged, LV_EVENT_VALUE_CHANGED, ctx);
|
||||
|
||||
// Backlight timeout enable
|
||||
auto* to_enable_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_size(to_enable_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(to_enable_wrapper, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_width(to_enable_wrapper, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto* to_enable_label = lv_label_create(to_enable_wrapper);
|
||||
lv_label_set_text(to_enable_label, "Auto backlight off");
|
||||
lv_obj_align(to_enable_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
ctx->switchTimeoutEnable = lv_switch_create(to_enable_wrapper);
|
||||
if (ctx->kbSettings.backlightTimeoutEnabled) lv_obj_add_state(ctx->switchTimeoutEnable, LV_STATE_CHECKED);
|
||||
lv_obj_align(ctx->switchTimeoutEnable, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(ctx->switchTimeoutEnable, onTimeoutEnableSwitch, LV_EVENT_VALUE_CHANGED, ctx);
|
||||
|
||||
auto* timeout_select_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_size(timeout_select_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(timeout_select_wrapper, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_width(timeout_select_wrapper, 0, LV_STATE_DEFAULT);
|
||||
|
||||
auto* timeout_value_label = lv_label_create(timeout_select_wrapper);
|
||||
lv_label_set_text(timeout_value_label, "Timeout");
|
||||
lv_obj_align(timeout_value_label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
|
||||
// Backlight timeout value (seconds)
|
||||
ctx->timeoutDropdown = lv_dropdown_create(timeout_select_wrapper);
|
||||
lv_dropdown_set_options(ctx->timeoutDropdown, "15 seconds\n30 seconds\n1 minute\n2 minutes\n5 minutes\nNever");
|
||||
lv_obj_align(ctx->timeoutDropdown, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(ctx->timeoutDropdown, onTimeoutChanged, LV_EVENT_VALUE_CHANGED, ctx);
|
||||
// Initialize dropdown selection from settings
|
||||
lv_dropdown_set_selected(ctx->timeoutDropdown, timeoutMsToIndex(ctx->kbSettings.backlightTimeoutMs));
|
||||
if (!ctx->kbSettings.backlightTimeoutEnabled) {
|
||||
lv_obj_add_state(ctx->timeoutDropdown, LV_STATE_DISABLED);
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors the old onHide() behaviour: persist the settings (regardless of whether the app is
|
||||
// giving up its thread for a save/resume cycle, or closing for good) whenever they changed.
|
||||
void persistIfUpdated(Context& ctx) {
|
||||
if (ctx.updated) {
|
||||
const auto copy = ctx.kbSettings;
|
||||
getMainDispatcher().dispatch([copy]{ settings::keyboard::save(copy); });
|
||||
ctx.updated = false;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t appMain(uint32_t appInstanceId, int argc, char* argv[]) {
|
||||
Context ctx {};
|
||||
ctx.appInstanceId = appInstanceId;
|
||||
|
||||
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:
|
||||
persistIfUpdated(ctx);
|
||||
app_manager_finish(appInstanceId);
|
||||
shouldClose = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
window_manager_remove(window);
|
||||
app_event_unsubscribe(&sub);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern const ::AppManifest manifest = {
|
||||
.id = "KeyboardSettings",
|
||||
.name = "Keyboard",
|
||||
.category = APP_CATEGORY_SETTINGS,
|
||||
.location = { APP_LOCATION_MEMORY, reinterpret_cast<void*>(appMain) }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user