1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

feat(msgbox) add a content area for custom content (#2561)

* added lv_dialog

* fixed include path

* formatted code

* make dialog backdrop themeable

* added missing backdrop init

* moved features in lv_dialog to lv_msgbox

* simplified msgbox style
This commit is contained in:
Mariotaku 2021-09-21 03:49:36 +09:00 committed by GitHub
parent bb6829796b
commit 258a9ae03b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 13 deletions

View File

@ -126,7 +126,7 @@ typedef struct {
#endif
#if LV_USE_MSGBOX
lv_style_t msgbox_bg, msgbox_btn_bg;
lv_style_t msgbox_bg, msgbox_btn_bg, msgbox_backdrop_bg;
#endif
#if LV_USE_KEYBOARD
@ -513,6 +513,10 @@ static void style_init(void)
style_init_reset(&styles->msgbox_bg);
lv_style_set_max_width(&styles->msgbox_bg, lv_pct(100));
style_init_reset(&styles->msgbox_backdrop_bg);
lv_style_set_bg_color(&styles->msgbox_backdrop_bg, lv_palette_main(LV_PALETTE_GREY));
lv_style_set_bg_opa(&styles->msgbox_backdrop_bg, LV_OPA_50);
#endif
#if LV_USE_KEYBOARD
style_init_reset(&styles->keyboard_btn_bg);
@ -957,6 +961,9 @@ static void theme_apply(lv_theme_t * th, lv_obj_t * obj)
lv_obj_add_style(obj, &styles->msgbox_bg, 0);
return;
}
else if(lv_obj_check_type(obj, &lv_msgbox_backdrop_class)) {
lv_obj_add_style(obj, &styles->msgbox_backdrop_bg, 0);
}
#endif
#if LV_USE_SPINBOX
else if(lv_obj_check_type(obj, &lv_spinbox_class)) {

View File

@ -27,7 +27,26 @@ static void msgbox_close_click_event_cb(lv_event_t * e);
/**********************
* STATIC VARIABLES
**********************/
const lv_obj_class_t lv_msgbox_class = {.base_class = &lv_obj_class, .instance_size = sizeof(lv_msgbox_t)};
const lv_obj_class_t lv_msgbox_class = {
.base_class = &lv_obj_class,
.width_def = LV_DPI_DEF * 2,
.height_def = LV_SIZE_CONTENT,
.instance_size = sizeof(lv_msgbox_t)
};
const lv_obj_class_t lv_msgbox_content_class = {
.base_class = &lv_obj_class,
.width_def = LV_PCT(100),
.height_def = LV_SIZE_CONTENT,
.instance_size = sizeof(lv_obj_t)
};
const lv_obj_class_t lv_msgbox_backdrop_class = {
.base_class = &lv_obj_class,
.width_def = LV_PCT(100),
.height_def = LV_PCT(100),
.instance_size = sizeof(lv_obj_t)
};
/**********************
* MACROS
@ -44,11 +63,10 @@ lv_obj_t * lv_msgbox_create(lv_obj_t * parent, const char * title, const char *
bool auto_parent = false;
if(parent == NULL) {
auto_parent = true;
parent = lv_obj_create(lv_layer_top());
lv_obj_remove_style_all(parent);
parent = lv_obj_class_create_obj(&lv_msgbox_backdrop_class, lv_layer_top());
LV_ASSERT_MALLOC(parent);
lv_obj_class_init_obj(parent);
lv_obj_clear_flag(parent, LV_OBJ_FLAG_IGNORE_LAYOUT);
lv_obj_set_style_bg_color(parent, lv_palette_main(LV_PALETTE_GREY), 0);
lv_obj_set_style_bg_opa(parent, LV_OPA_50, 0);
lv_obj_set_size(parent, LV_PCT(100), LV_PCT(100));
}
@ -60,9 +78,7 @@ lv_obj_t * lv_msgbox_create(lv_obj_t * parent, const char * title, const char *
if(auto_parent) lv_obj_add_flag(obj, LV_MSGBOX_FLAG_AUTO_PARENT);
lv_obj_set_size(obj, LV_DPI_DEF * 2, LV_SIZE_CONTENT);
lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW_WRAP);
lv_obj_set_flex_align(obj, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
bool has_title = title && strlen(title) > 0;
@ -87,10 +103,15 @@ lv_obj_t * lv_msgbox_create(lv_obj_t * parent, const char * title, const char *
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
}
mbox->text = lv_label_create(obj);
mbox->content = lv_obj_class_create_obj(&lv_msgbox_content_class, obj);
bool has_txt = txt && strlen(txt) > 0;
if (has_txt) {
mbox->text = lv_label_create(mbox->content);
lv_label_set_text(mbox->text, txt);
lv_label_set_long_mode(mbox->text, LV_LABEL_LONG_WRAP);
lv_obj_set_width(mbox->text, lv_pct(100));
}
if(btn_txts) {
mbox->btns = lv_btnmatrix_create(obj);
@ -130,6 +151,12 @@ lv_obj_t * lv_msgbox_get_text(lv_obj_t * obj)
return mbox->text;
}
lv_obj_t * lv_msgbox_get_content(lv_obj_t * obj)
{
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
return mbox->content;
}
lv_obj_t * lv_msgbox_get_btns(lv_obj_t * obj)
{
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
@ -154,6 +181,11 @@ void lv_msgbox_close(lv_obj_t * mbox)
else lv_obj_del(mbox);
}
void lv_msgbox_close_async(lv_obj_t * dialog)
{
if(lv_obj_has_flag(dialog, LV_MSGBOX_FLAG_AUTO_PARENT)) lv_obj_del_async(lv_obj_get_parent(dialog));
else lv_obj_del_async(dialog);
}
/**********************
* STATIC FUNCTIONS

View File

@ -38,18 +38,21 @@ typedef struct {
lv_obj_t obj;
lv_obj_t * title;
lv_obj_t * close_btn;
lv_obj_t * content;
lv_obj_t * text;
lv_obj_t * btns;
} lv_msgbox_t;
extern const lv_obj_class_t lv_msgbox_class;
extern const lv_obj_class_t lv_msgbox_backdrop_class;
/**********************
* GLOBAL PROTOTYPES
**********************/
/**
* Create a message box objects
* Create a message box object
* @param parent pointer to parent or NULL to create a full screen modal message box
* @param title the title of the message box
* @param txt the text of the message box
@ -66,6 +69,8 @@ lv_obj_t * lv_msgbox_get_close_btn(lv_obj_t * obj);
lv_obj_t * lv_msgbox_get_text(lv_obj_t * obj);
lv_obj_t * lv_msgbox_get_content(lv_obj_t * obj);
lv_obj_t * lv_msgbox_get_btns(lv_obj_t * obj);
/**
@ -79,6 +84,8 @@ const char * lv_msgbox_get_active_btn_text(lv_obj_t * mbox);
void lv_msgbox_close(lv_obj_t * mbox);
void lv_msgbox_close_async(lv_obj_t * mbox);
/**********************
* MACROS
**********************/