Files
ESP32-S3-Touch-LCD-5B/src/modules/ui_kit.cpp
T

179 lines
6.8 KiB
C++

/*
* UI kit implementation - see ui_kit.h
*/
#include "ui_kit.h"
namespace UiKit {
lv_obj_t* screen() {
lv_obj_t* s = lv_obj_create(NULL);
lv_obj_set_style_bg_color(s, UI_COL_BG, 0);
lv_obj_set_style_bg_opa(s, LV_OPA_COVER, 0);
lv_obj_clear_flag(s, LV_OBJ_FLAG_SCROLLABLE);
return s;
}
void header(lv_obj_t* scr, const char* title, lv_event_cb_t back_cb) {
lv_obj_t* t = label(scr, title, &lv_font_montserrat_30, UI_COL_TEXT);
lv_obj_align(t, LV_ALIGN_TOP_LEFT, 24, 18);
// -176: the StatusOverlay Wi-Fi chip owns the last 150 px of the top-right corner
lv_obj_t* back = button(scr, LV_SYMBOL_LEFT " Driveline", 200, 56, BTN_SECONDARY, back_cb, NULL);
lv_obj_align(back, LV_ALIGN_TOP_RIGHT, -176, 10);
}
lv_obj_t* label(lv_obj_t* parent, const char* text, const lv_font_t* font, lv_color_t color) {
lv_obj_t* l = lv_label_create(parent);
lv_label_set_text(l, text);
lv_obj_set_style_text_font(l, font, 0);
lv_obj_set_style_text_color(l, color, 0);
return l;
}
static void styleButton(lv_obj_t* b, btn_style_t style) {
lv_color_t bg = UI_COL_PANEL2, fg = UI_COL_TEXT;
switch (style) {
case BTN_PRIMARY: bg = UI_COL_ACCENT; fg = lv_color_hex(0x0B1620); break;
case BTN_SECONDARY: bg = UI_COL_PANEL2; fg = UI_COL_TEXT; break;
case BTN_DANGER: bg = UI_COL_WARN; fg = UI_COL_TEXT; break;
case BTN_SUCCESS: bg = UI_COL_OK; fg = lv_color_hex(0x0B1620); break;
}
lv_obj_set_style_bg_color(b, bg, 0);
lv_obj_set_style_bg_opa(b, LV_OPA_COVER, 0);
lv_obj_set_style_text_color(b, fg, 0);
lv_obj_set_style_radius(b, 10, 0);
lv_obj_set_style_shadow_width(b, 0, 0);
lv_obj_set_style_bg_color(b, lv_color_hex(0x3A4450), LV_STATE_DISABLED);
lv_obj_set_style_text_color(b, UI_COL_MUTED, LV_STATE_DISABLED);
}
lv_obj_t* button(lv_obj_t* parent, const char* text, lv_coord_t w, lv_coord_t h,
btn_style_t style, lv_event_cb_t cb, void* user) {
lv_obj_t* b = lv_btn_create(parent);
lv_obj_set_size(b, w, h);
styleButton(b, style);
if (cb) lv_obj_add_event_cb(b, cb, LV_EVENT_CLICKED, user);
lv_obj_t* l = lv_label_create(b);
lv_label_set_text(l, text);
lv_obj_set_style_text_font(l, &lv_font_montserrat_20, 0);
lv_obj_center(l);
return b;
}
void setButtonText(lv_obj_t* btn, const char* text) {
setText(lv_obj_get_child(btn, 0), text);
}
lv_obj_t* toggle(lv_obj_t* parent, lv_event_cb_t cb, void* user) {
lv_obj_t* sw = lv_switch_create(parent);
lv_obj_set_size(sw, 92, 48);
lv_obj_set_style_bg_color(sw, UI_COL_PANEL2, 0);
lv_obj_set_style_bg_color(sw, UI_COL_ACCENT, (lv_style_selector_t)LV_PART_INDICATOR | (lv_style_selector_t)LV_STATE_CHECKED);
lv_obj_set_style_anim_time(sw, 0, 0); // no knob slide animation: one redraw per flip
if (cb) lv_obj_add_event_cb(sw, cb, LV_EVENT_VALUE_CHANGED, user);
return sw;
}
void setChecked(lv_obj_t* obj, bool on) {
if (obj == NULL || lv_obj_has_state(obj, LV_STATE_CHECKED) == on) return;
if (on) lv_obj_add_state(obj, LV_STATE_CHECKED);
else lv_obj_clear_state(obj, LV_STATE_CHECKED);
}
lv_obj_t* box(lv_obj_t* parent, lv_flex_flow_t flow) {
lv_obj_t* b = lv_obj_create(parent);
lv_obj_remove_style_all(b);
lv_obj_set_flex_flow(b, flow);
lv_obj_clear_flag(b, LV_OBJ_FLAG_SCROLLABLE);
return b;
}
lv_obj_t* card(lv_obj_t* parent) {
lv_obj_t* c = lv_obj_create(parent);
lv_obj_set_style_bg_color(c, UI_COL_PANEL, 0);
lv_obj_set_style_border_width(c, 0, 0);
lv_obj_set_style_radius(c, 12, 0);
lv_obj_set_style_pad_all(c, 16, 0);
lv_obj_set_style_shadow_width(c, 0, 0);
lv_obj_clear_flag(c, LV_OBJ_FLAG_SCROLLABLE);
return c;
}
void setText(lv_obj_t* lbl, const char* text) {
if (lbl == NULL) return;
const char* cur = lv_label_get_text(lbl);
if (cur && strcmp(cur, text) == 0) return;
lv_label_set_text(lbl, text);
}
void setTextColor(lv_obj_t* lbl, lv_color_t c) {
if (lbl == NULL) return;
if (lv_obj_get_style_text_color(lbl, LV_PART_MAIN).full == c.full) return;
lv_obj_set_style_text_color(lbl, c, 0);
}
void setVisible(lv_obj_t* obj, bool visible) {
if (obj == NULL || lv_obj_has_flag(obj, LV_OBJ_FLAG_HIDDEN) == !visible) return;
if (visible) lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);
else lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
}
// ---- confirm dialog --------------------------------------------------------
static const char* g_btn_map[3] = {"Cancel", "OK", ""};
static char g_ok_text[32];
static void (*g_confirm_cb)(bool, void*) = NULL;
static void* g_confirm_user = NULL;
static void onConfirm(lv_event_t* e) {
lv_obj_t* mb = lv_event_get_current_target(e);
uint16_t id = lv_msgbox_get_active_btn(mb);
if (id == LV_BTNMATRIX_BTN_NONE) return;
lv_msgbox_close_async(mb);
void (*cb)(bool, void*) = g_confirm_cb;
g_confirm_cb = NULL;
if (cb) cb(id == 1, g_confirm_user);
}
void confirm(const char* title, const char* text, const char* ok_text,
void (*cb)(bool ok, void* user), void* user) {
snprintf(g_ok_text, sizeof(g_ok_text), "%s", ok_text);
g_btn_map[1] = g_ok_text;
g_confirm_cb = cb;
g_confirm_user = user;
lv_obj_t* mb = lv_msgbox_create(NULL, title, text, g_btn_map, false);
lv_obj_set_width(mb, 640);
lv_obj_set_style_bg_color(mb, UI_COL_PANEL, 0);
lv_obj_set_style_border_width(mb, 0, 0);
lv_obj_set_style_shadow_width(mb, 0, 0);
lv_obj_set_style_radius(mb, 14, 0);
lv_obj_set_style_pad_all(mb, 24, 0);
lv_obj_set_style_pad_row(mb, 16, 0);
lv_obj_set_style_text_color(mb, UI_COL_TEXT, 0);
lv_obj_set_style_text_font(lv_msgbox_get_title(mb), &lv_font_montserrat_24, 0);
lv_obj_set_style_text_font(lv_msgbox_get_text(mb), &lv_font_montserrat_20, 0);
lv_obj_t* btns = lv_msgbox_get_btns(mb);
lv_obj_set_size(btns, LV_PCT(100), 64);
lv_obj_set_style_text_font(btns, &lv_font_montserrat_20, 0);
lv_obj_set_style_pad_column(btns, 16, 0);
lv_obj_set_style_bg_color(btns, UI_COL_PANEL2, LV_PART_ITEMS);
lv_obj_set_style_text_color(btns, UI_COL_TEXT, LV_PART_ITEMS);
lv_obj_set_style_radius(btns, 10, LV_PART_ITEMS);
lv_obj_set_style_shadow_width(btns, 0, LV_PART_ITEMS);
lv_btnmatrix_set_btn_ctrl(btns, 1, LV_BTNMATRIX_CTRL_CHECKED); // highlight the action
lv_obj_set_style_bg_color(btns, UI_COL_ACCENT, (lv_style_selector_t)LV_PART_ITEMS | (lv_style_selector_t)LV_STATE_CHECKED);
lv_obj_set_style_text_color(btns, lv_color_hex(0x0B1620), (lv_style_selector_t)LV_PART_ITEMS | (lv_style_selector_t)LV_STATE_CHECKED);
lv_obj_t* backdrop = lv_obj_get_parent(mb);
lv_obj_set_style_bg_color(backdrop, lv_color_black(), 0);
lv_obj_set_style_bg_opa(backdrop, LV_OPA_70, 0);
lv_obj_center(mb);
lv_obj_add_event_cb(mb, onConfirm, LV_EVENT_VALUE_CHANGED, NULL);
}
} // namespace UiKit