Description
How to implement styles to be used on generic function that creates buttons based on config structure passed as operant.
What MCU/Processor/Board and compiler are you using?
esp32 , simulator
What LVGL version are you using?
8.3
What do you want to achieve?
I am trying to achive a button generator function. That we give a config struct and returns a button. Function should initialize styles for the button based on config but the styles can not be static cause the function should be used several times in order to create all buttons needed in program.
What have you tried so far?
I have 3 styles one normal button one for focused and one for pressed. In the config i am passing button size, text, font, text color, and other params and flags. The styles initially in the function where created as static and they worked only for one button.the style of the last button is overwriting all the rest. When i removed the static from the styles and let them to be block oriented that i have crash of function.
Code to reproduce
typedef struct {
int32_t width;
int32_t height;
ButtonTask cbTask;
bool checkable;
char* text;
const lv_font_t* textFont;
lv_color_t textColor;
}button_t;
lv_obj_t* str_create_button(button_t btnConf,lv_obj_t* parent){
static lv_style_t style_btn;
lv_style_init(&style_btn);
lv_style_set_width(&style_btn, btnConf.width);
lv_style_set_height(&style_btn, btnConf.height);
lv_style_set_text_font(&style_btn, btnConf.textFont);
lv_style_set_text_color(&style_btn, btnConf.textColor);
lv_style_set_outline_width(&style_btn, 0);
lv_style_set_radius(&style_btn, btnConf.height/2);
lv_style_set_bg_grad_color(&style_btn, lv_color_black());
lv_style_set_bg_grad_dir(&style_btn, LV_GRAD_DIR_VER);
static lv_style_t style_btn_fc;
lv_style_init(&style_btn_fc);
lv_style_set_outline_width(&style_btn_fc, 0);
static lv_style_t style_btn_pr;
lv_style_init(&style_btn_pr);
lv_style_set_width(&style_btn_pr, btnConf.width);
lv_style_set_height(&style_btn_pr, btnConf.height);
lv_style_set_outline_width(&style_btn_pr, 0);
lv_style_set_radius(&style_btn_pr, btnConf.height/2);
lv_style_set_bg_color(&style_btn_pr, lv_palette_main(LV_PALETTE_RED));
lv_style_set_bg_grad_color(&style_btn_pr, lv_palette_darken(LV_PALETTE_RED, 4));
lv_style_set_bg_grad_dir(&style_btn_pr, LV_GRAD_DIR_VER);
lv_style_set_shadow_color(&style_btn_pr, lv_palette_main(LV_PALETTE_RED));
lv_style_set_shadow_width(&style_btn_pr, 2*btnConf.height);
lv_style_set_shadow_opa(&style_btn_pr, LV_OPA_COVER);
static const lv_style_prop_t btn_trans_props[] = {
LV_STYLE_BG_COLOR,
LV_STYLE_SHADOW_COLOR,
LV_STYLE_SHADOW_WIDTH,
LV_STYLE_SHADOW_OPA,
0, /*End marker*/
};
static lv_style_transition_dsc_t btn_trans;
lv_style_transition_dsc_init(&btn_trans, btn_trans_props, lv_anim_path_ease_out, 10, 0,NULL);
lv_style_set_transition(&style_btn_pr, &btn_trans);
lv_style_set_transition(&style_btn, &btn_trans);
lv_obj_t* btn=lv_btn_create(parent);
lv_obj_t* label=lv_label_create(btn);
if(btnConf.checkable){
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
}
lv_label_set_text(label, btnConf.text);
lv_obj_center(label);
lv_obj_add_event_cb(btn, str_button_event_handler, LV_EVENT_PRESSED, (void *)btnConf.cbTask);
lv_obj_add_style(btn, &style_btn, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_add_style(btn, &style_btn_fc, LV_PART_MAIN | LV_STATE_FOCUS_KEY);
lv_obj_add_style(btn, &style_btn_pr, LV_PART_MAIN | LV_STATE_CHECKED);
lv_obj_add_style(btn, &style_btn_pr, LV_PART_MAIN | LV_STATE_PRESSED);
return btn;
}