diff --git a/src/widgets/label/lv_label.c b/src/widgets/label/lv_label.c index 496120007..3d8fa8336 100644 --- a/src/widgets/label/lv_label.c +++ b/src/widgets/label/lv_label.c @@ -763,24 +763,27 @@ static void lv_label_event(const lv_obj_class_t * class_p, lv_event_t * e) lv_label_refr_text(obj); } else if(code == LV_EVENT_GET_SELF_SIZE) { - lv_point_t size; lv_label_t * label = (lv_label_t *)obj; - const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); - lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_MAIN); - lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN); - lv_text_flag_t flag = LV_TEXT_FLAG_NONE; - if(label->recolor != 0) flag |= LV_TEXT_FLAG_RECOLOR; - if(label->expand != 0) flag |= LV_TEXT_FLAG_EXPAND; - lv_coord_t w = lv_obj_get_content_width(obj); - if(lv_obj_get_style_width(obj, LV_PART_MAIN) == LV_SIZE_CONTENT && !obj->w_layout) w = LV_COORD_MAX; - else w = lv_obj_get_content_width(obj); + if(label->invalid_size_cache) { + const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); + lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_MAIN); + lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN); + lv_text_flag_t flag = LV_TEXT_FLAG_NONE; + if(label->recolor != 0) flag |= LV_TEXT_FLAG_RECOLOR; + if(label->expand != 0) flag |= LV_TEXT_FLAG_EXPAND; - lv_txt_get_size(&size, label->text, font, letter_space, line_space, w, flag); + lv_coord_t w = lv_obj_get_content_width(obj); + if(lv_obj_get_style_width(obj, LV_PART_MAIN) == LV_SIZE_CONTENT && !obj->w_layout) w = LV_COORD_MAX; + else w = lv_obj_get_content_width(obj); + + lv_txt_get_size(&label->size_cache, label->text, font, letter_space, line_space, w, flag); + label->invalid_size_cache = false; + } lv_point_t * self_size = lv_event_get_param(e); - self_size->x = LV_MAX(self_size->x, size.x); - self_size->y = LV_MAX(self_size->y, size.y); + self_size->x = LV_MAX(self_size->x, label->size_cache.x); + self_size->y = LV_MAX(self_size->y, label->size_cache.y); } else if(code == LV_EVENT_DRAW_MAIN) { draw_main(e); @@ -899,6 +902,7 @@ static void lv_label_refr_text(lv_obj_t * obj) #if LV_LABEL_LONG_TXT_HINT label->hint.line_start = -1; /*The hint is invalid if the text changes*/ #endif + label->invalid_size_cache = true; lv_area_t txt_coords; lv_obj_get_content_coords(obj, &txt_coords); diff --git a/src/widgets/label/lv_label.h b/src/widgets/label/lv_label.h index cf3f0643b..db820a10b 100644 --- a/src/widgets/label/lv_label.h +++ b/src/widgets/label/lv_label.h @@ -68,12 +68,14 @@ typedef struct { uint32_t sel_end; #endif + lv_point_t size_cache; /*Text size cache*/ lv_point_t offset; /*Text draw position offset*/ lv_label_long_mode_t long_mode : 3; /*Determine what to do with the long texts*/ uint8_t static_txt : 1; /*Flag to indicate the text is static*/ uint8_t recolor : 1; /*Enable in-line letter re-coloring*/ uint8_t expand : 1; /*Ignore real width (used by the library with LV_LABEL_LONG_SCROLL)*/ - uint8_t dot_tmp_alloc : 1; /*1: dot is allocated, 0: dot directly holds up to 4 chars*/ + uint8_t dot_tmp_alloc : 1; /*1: dot is allocated, 0: dot directly holds up to 4 chars*/ + uint8_t invalid_size_cache : 1; /*1: Recalculate size and update cache*/ } lv_label_t; extern const lv_obj_class_t lv_label_class;