What do you want to achieve?
I just want to switch the built-in themes.
Others
- SquareLine Studio version:
1.5.1 - Operating system:
Linux - Target hardware:
ESP32-S3
What have you tried so far?
I have carefully studied the example from the documentation
but didn’t find anything similar on how to switch the built-in themes.
I tried to apply the method from the example and came to the conclusion that it greatly complicates the code.
In an existing project, I need to change the properties of all the elements.
This is especially true for checkboxes, drop-down lists and etc.
How to apply the properties of embedded themes when switching via
ui_theme_set(0); for light theme or
ui_theme_set(1); for dark theme
in a simple way?
The macro #define LV_THEME_DEFAULT_DARK 0
in the settings file lv_conf.h
in the LVGL
library is used only once in the lv_display_create()
function.
#if LV_USE_THEME_DEFAULT
if(lv_theme_default_is_inited() == false) {
disp->theme = lv_theme_default_init(disp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED),
LV_THEME_DEFAULT_DARK, LV_FONT_DEFAULT);
}
else {
disp->theme = lv_theme_default_get();
}
#elif LV_USE_THEME_SIMPLE
if(lv_theme_simple_is_inited() == false) {
disp->theme = lv_theme_simple_init(disp);
}
else {
disp->theme = lv_theme_simple_get();
}
#endif
Why not let the user change the theme, colors and font at runtime at the initialization stage, eg this?
lv_display_t * lv_display_create(int32_t hor_res, int32_t ver_res, bool dark_theme, lv_palette_t primary, lv_palette_t secondary, const lv_font_t *font_default)
{
...
#if LV_USE_THEME_DEFAULT
if(lv_theme_default_is_inited() == false) {
disp->theme = lv_theme_default_init(disp, lv_palette_main(primary), lv_palette_main(secondary),
dark_theme, font_default);
}
else {
disp->theme = lv_theme_default_get();
}
#elif LV_USE_THEME_SIMPLE
if(lv_theme_simple_is_inited() == false) {
disp->theme = lv_theme_simple_init(disp);
}
else {
disp->theme = lv_theme_simple_get();
}
#endif
....
}
// User Code
lv_font_t *my_font = lv_binfont_create("L:/fonts/main14.bin");
static lv_disp_t *disp;
disp = lv_display_create(TFT_WIDTH, TFT_HEIGHT, true, LV_PALETTE_BLUE, LV_PALETTE_RED, my_font);
lv_display_set_buffers(disp, buf1, buf2, _BUF_SIZE * sizeof(lv_color_t), LV_DISPLAY_RENDER_MODE_PARTIAL);
lv_display_set_flush_cb(disp, my_disp_flush);
Or, in order not to complicate the code and make edits in the entire documentation, just cut this code fragment
#if LV_USE_THEME_DEFAULT
if(lv_theme_default_is_inited() == false) {
disp->theme = lv_theme_default_init(disp, lv_palette_main(primary), lv_palette_main(secondary),
dark_theme, font_default);
}
else {
disp->theme = lv_theme_default_get();
}
#elif LV_USE_THEME_SIMPLE
if(lv_theme_simple_is_inited() == false) {
disp->theme = lv_theme_simple_init(disp);
}
else {
disp->theme = lv_theme_simple_get();
}
#endif
from the functionv_display_create()
and insert the display driver initialization examples in the code user
// User Code
lv_font_t *my_font = lv_binfont_create("L:/fonts/main14.bin");
static lv_disp_t *disp;
disp = lv_display_create(TFT_WIDTH, TFT_HEIGHT);
#if LV_USE_THEME_DEFAULT
if(lv_theme_default_is_inited() == false) {
disp->theme = lv_theme_default_init(disp, llv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED),
true, my_font); // or false for light thene
}
else {
disp->theme = lv_theme_default_get();
}
#elif LV_USE_THEME_SIMPLE
if(lv_theme_simple_is_inited() == false) {
disp->theme = lv_theme_simple_init(disp);
}
else {
disp->theme = lv_theme_simple_get();
}
#endif
lv_display_set_buffers(disp, buf1, buf2, _BUF_SIZE * sizeof(lv_color_t), LV_DISPLAY_RENDER_MODE_PARTIAL);
lv_display_set_flush_cb(disp, my_disp_flush);
then in project the theme will not have to be initialized again
void ui_init(void)
{
lv_disp_t * dispp = lv_display_get_default();
lv_theme_t * theme = lv_theme_default_get();
lv_disp_set_theme(dispp, theme);
ui_main_screen_init();
ui____initial_actions0 = lv_obj_create(NULL);
lv_obj_add_event_cb(ui____initial_actions0, ui_event____initial_actions0, LV_EVENT_ALL, NULL);
lv_disp_load_scr(ui____initial_actions0);
lv_disp_load_scr(ui_main);
}
and the default font may be custom rather than built-in from the start.
And why is it still necessary to edit the project file manually in order to change the default theme and change the primary and secondary colors?
"theme_dark": true,
"theme_color1": 15,
"theme_color2": 0,