Reworked the common screen header to add scrolling when the title is too long and to be able to update it's title after is has been created

This commit is contained in:
anschrammh 2023-03-24 14:55:10 +01:00
parent 5d3b5b5285
commit 86be55b404
2 changed files with 60 additions and 5 deletions

View File

@ -1,7 +1,37 @@
#include "lvgl.h"
/***
* It is needed to have a reference on two header_titles because when
* switching from one screen using a header to an other screen which is also using one
* the header of the new screen will be created BEFORE the header of the previous screen
* is deleted ...
*/
/* A reference to the current and previous header title */
static lv_obj_t *header_title[2] = {NULL, NULL};
/* Pointer to the header_title to use */
lv_obj_t **header_title_p = &header_title[0];
static void cleanup_event_cb(lv_event_t * e)
{
lv_obj_t **header_title_deleted_p = e->user_data;
*header_title_deleted_p = NULL;
LV_LOG_USER("header_title cleanup");
}
void common_screen_header_component(lv_obj_t *parent, const char * title, lv_coord_t height)
{
if(header_title[0] == NULL)
header_title_p = &header_title[0];
else if(header_title[1] == NULL)
header_title_p = &header_title[1];
else
{
LV_LOG_ERROR("no free header_title");
return;
}
lv_obj_t *header = lv_obj_create(parent);
lv_obj_set_style_bg_color(header, lv_color_make(129, 141,181), LV_PART_MAIN);
lv_obj_set_size(header, lv_disp_get_hor_res(NULL), height);
@ -9,9 +39,20 @@ void common_screen_header_component(lv_obj_t *parent, const char * title, lv_coo
lv_obj_set_style_border_width(header, 0, LV_PART_MAIN);
lv_obj_clear_flag(header, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_t *header_title = lv_label_create(header);
lv_label_set_text_static(header_title, title);
lv_obj_set_style_text_color(header_title, lv_color_white(), LV_PART_MAIN);
lv_obj_set_style_text_font(header_title, &lv_font_montserrat_30, LV_PART_MAIN);
lv_obj_set_align(header_title, LV_ALIGN_CENTER);
*header_title_p = lv_label_create(header);
lv_label_set_text_static(*header_title_p, title);
lv_obj_set_style_text_align(*header_title_p, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
lv_obj_set_style_text_color(*header_title_p, lv_color_white(), LV_PART_MAIN);
lv_obj_set_style_text_font(*header_title_p, &lv_font_montserrat_30, LV_PART_MAIN);
lv_obj_set_width(*header_title_p, 150);
lv_obj_set_align(*header_title_p, LV_ALIGN_CENTER);
lv_label_set_long_mode(*header_title_p, LV_LABEL_LONG_SCROLL_CIRCULAR);
/* Once the current header_title is destroyed, mark it as NULL to prevent any crashes when trying to change it's title */
lv_obj_add_event_cb(*header_title_p, &(cleanup_event_cb), LV_EVENT_DELETE, header_title_p);
}
void common_screen_header_update_title(const char * title)
{
if(!*header_title_p)return;
lv_label_set_text_static(*header_title_p, title);
}

View File

@ -3,6 +3,20 @@
#include "lvgl.h"
/**
* @brief Adds a screen header with the defined title to the screen passed as parent with the provided height.
*
* @param parent the screen to which the header will be added
* @param title a string used as the header's title
* @param height the height in pixel of the header
*/
void common_screen_header_component(lv_obj_t *parent, const char * title, lv_coord_t height);
/**
* @brief Updates the current title of displayed screen header
*
* @param title a string used as the new header's title
*/
void common_screen_header_update_title(const char * title);
#endif //COMMON_SCREEN_COMPONENTS_H