1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00
lvgl/examples/others/observer/lv_example_observer_6.c
zylalx1 47ec2784d8
chore: update some code and docs to use v9 API (#5876)
Co-authored-by: Neo Xu <neo.xu1990@gmail.com>
2024-04-10 15:17:44 +08:00

191 lines
7.1 KiB
C

#include "../../lv_examples.h"
#if LV_USE_OBSERVER && LV_USE_SLIDER && LV_USE_LABEL && LV_BUILD_EXAMPLES
typedef enum {
THEME_MODE_LIGHT,
THEME_MODE_DARK,
} theme_mode_t;
static lv_obj_t * my_panel_create(lv_obj_t * parent);
static lv_obj_t * my_button_create(lv_obj_t * parent, const char * text, lv_event_cb_t event_cb);
static void switch_theme_event_cb(lv_event_t * e);
static lv_subject_t theme_subject;
/**
* Change between light and dark mode
*/
void lv_example_observer_6(void)
{
lv_subject_init_int(&theme_subject, THEME_MODE_DARK);
lv_obj_t * panel1 = my_panel_create(lv_screen_active());
lv_obj_set_flex_flow(panel1, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(panel1, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_size(panel1, lv_pct(90), lv_pct(90));
lv_obj_center(panel1);
my_button_create(panel1, "Button 1", switch_theme_event_cb);
my_button_create(panel1, "Button 2", switch_theme_event_cb);
my_button_create(panel1, "Button 3", switch_theme_event_cb);
my_button_create(panel1, "Button 4", switch_theme_event_cb);
my_button_create(panel1, "Button 5", switch_theme_event_cb);
my_button_create(panel1, "Button 6", switch_theme_event_cb);
my_button_create(panel1, "Button 7", switch_theme_event_cb);
my_button_create(panel1, "Button 8", switch_theme_event_cb);
my_button_create(panel1, "Button 9", switch_theme_event_cb);
my_button_create(panel1, "Button 10", switch_theme_event_cb);
}
static void switch_theme_event_cb(lv_event_t * e)
{
LV_UNUSED(e);
if(lv_subject_get_int(&theme_subject) == THEME_MODE_LIGHT) lv_subject_set_int(&theme_subject, THEME_MODE_DARK);
else lv_subject_set_int(&theme_subject, THEME_MODE_LIGHT);
}
/*-----------------------------------------
* my_panel.c
*
* It would be a separate file with its own
* local types, data, and functions
*------------------------------------------*/
typedef struct {
lv_style_t style_main;
lv_style_t style_scrollbar;
} my_panel_styles_t;
static void my_panel_style_observer_cb(lv_observer_t * observer, lv_subject_t * subject)
{
LV_UNUSED(subject);
LV_UNUSED(observer);
theme_mode_t m = lv_subject_get_int(&theme_subject);
my_panel_styles_t * styles = lv_observer_get_target(observer);
if(m == THEME_MODE_LIGHT) {
lv_style_set_bg_color(&styles->style_main, lv_color_hex3(0xfff));
lv_style_set_shadow_color(&styles->style_main, lv_color_hex3(0x888));
lv_style_set_text_color(&styles->style_main, lv_color_hex3(0x222));
lv_style_set_bg_color(&styles->style_scrollbar, lv_color_hex3(0x888));
}
if(m == THEME_MODE_DARK) {
lv_style_set_bg_color(&styles->style_main, lv_color_hex(0x040038));
lv_style_set_shadow_color(&styles->style_main, lv_color_hex3(0xaaa));
lv_style_set_text_color(&styles->style_main, lv_color_hex3(0xeee));
lv_style_set_bg_color(&styles->style_scrollbar, lv_color_hex3(0xaaa));
}
lv_obj_report_style_change(&styles->style_main);
lv_obj_report_style_change(&styles->style_scrollbar);
}
static lv_obj_t * my_panel_create(lv_obj_t * parent)
{
static bool inited = false;
static my_panel_styles_t styles;
if(!inited) {
inited = true;
lv_style_init(&styles.style_main);
lv_style_set_radius(&styles.style_main, 12);
lv_style_set_bg_opa(&styles.style_main, LV_OPA_COVER);
lv_style_set_shadow_width(&styles.style_main, 24);
lv_style_set_shadow_offset_x(&styles.style_main, 4);
lv_style_set_shadow_offset_y(&styles.style_main, 6);
lv_style_set_pad_all(&styles.style_main, 12);
lv_style_set_pad_gap(&styles.style_main, 16);
lv_style_init(&styles.style_scrollbar);
lv_style_set_width(&styles.style_scrollbar, 4);
lv_style_set_radius(&styles.style_scrollbar, 2);
lv_style_set_pad_right(&styles.style_scrollbar, 8);
lv_style_set_pad_ver(&styles.style_scrollbar, 8);
lv_style_set_bg_opa(&styles.style_scrollbar, LV_OPA_50);
lv_subject_add_observer_with_target(&theme_subject, my_panel_style_observer_cb, &styles, NULL);
}
lv_obj_t * panel = lv_obj_create(parent);
lv_obj_remove_style_all(panel);
lv_obj_add_style(panel, &styles.style_main, 0);
lv_obj_add_style(panel, &styles.style_scrollbar, LV_PART_SCROLLBAR);
return panel;
}
/*-----------------------------------------
* my_button.c
*
* It would be a separate file with its own
* local types, data, and functions
*------------------------------------------*/
typedef struct {
lv_style_t style_main;
lv_style_t style_pressed;
} my_button_styles_t;
static void my_button_style_observer_cb(lv_observer_t * observer, lv_subject_t * subject)
{
LV_UNUSED(subject);
LV_UNUSED(observer);
theme_mode_t m = lv_subject_get_int(&theme_subject);
my_button_styles_t * styles = lv_observer_get_target(observer);
if(m == THEME_MODE_LIGHT) {
lv_style_set_bg_color(&styles->style_main, lv_color_hex(0x3379de));
lv_style_set_bg_grad_color(&styles->style_main, lv_color_hex(0xd249a5));
lv_style_set_shadow_color(&styles->style_main, lv_color_hex(0x3379de));
lv_style_set_text_color(&styles->style_main, lv_color_hex3(0xfff));
lv_style_set_color_filter_opa(&styles->style_pressed, LV_OPA_70);
}
if(m == THEME_MODE_DARK) {
lv_style_set_bg_color(&styles->style_main, lv_color_hex(0xde1382));
lv_style_set_bg_grad_color(&styles->style_main, lv_color_hex(0x4b0c72));
lv_style_set_shadow_color(&styles->style_main, lv_color_hex(0x4b0c72));
lv_style_set_text_color(&styles->style_main, lv_color_hex3(0xfff));
lv_style_set_color_filter_opa(&styles->style_pressed, LV_OPA_30);
}
lv_obj_report_style_change(&styles->style_main);
lv_obj_report_style_change(&styles->style_pressed);
}
static lv_obj_t * my_button_create(lv_obj_t * parent, const char * text, lv_event_cb_t event_cb)
{
static bool inited = false;
static my_button_styles_t styles;
if(!inited) {
inited = true;
lv_style_init(&styles.style_main);
lv_style_set_radius(&styles.style_main, LV_RADIUS_CIRCLE);
lv_style_set_bg_opa(&styles.style_main, LV_OPA_COVER);
lv_style_set_bg_grad_dir(&styles.style_main, LV_GRAD_DIR_HOR);
lv_style_set_shadow_width(&styles.style_main, 24);
lv_style_set_shadow_offset_y(&styles.style_main, 6);
lv_style_set_pad_hor(&styles.style_main, 32);
lv_style_set_pad_ver(&styles.style_main, 12);
lv_style_init(&styles.style_pressed);
lv_style_set_color_filter_dsc(&styles.style_pressed, &lv_color_filter_shade);
lv_subject_add_observer_with_target(&theme_subject, my_button_style_observer_cb, &styles, NULL);
}
lv_obj_t * btn = lv_button_create(parent);
lv_obj_remove_style_all(btn);
lv_obj_add_style(btn, &styles.style_main, 0);
lv_obj_add_style(btn, &styles.style_pressed, LV_STATE_PRESSED);
lv_obj_add_event_cb(btn, event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_t * label = lv_label_create(btn);
lv_label_set_text(label, text);
return btn;
}
#endif