From 1b93668df7e710c4fc5c55c169727c8c9689b0ac Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Thu, 29 Apr 2021 09:52:49 +0200 Subject: [PATCH] feat(obj) save self_size in the obejct to save a lot of recalculations --- examples/widgets/meter/lv_example_meter_3.c | 1 - src/core/lv_event.c | 2 +- src/core/lv_event.h | 2 +- src/core/lv_obj.h | 1 + src/core/lv_obj_pos.c | 17 +++++++---------- src/core/lv_obj_pos.h | 2 +- src/core/lv_obj_scroll.c | 16 ++++++++++------ src/draw/lv_draw_img.c | 1 - src/widgets/lv_chart.c | 4 +++- src/widgets/lv_checkbox.c | 6 +++--- src/widgets/lv_dropdown.c | 6 +++++- src/widgets/lv_img.c | 4 ++-- src/widgets/lv_label.c | 6 +++--- src/widgets/lv_line.c | 4 ++-- src/widgets/lv_roller.c | 4 ++-- src/widgets/lv_table.c | 4 ++-- 16 files changed, 43 insertions(+), 37 deletions(-) diff --git a/examples/widgets/meter/lv_example_meter_3.c b/examples/widgets/meter/lv_example_meter_3.c index 77ef2c397..b41058280 100644 --- a/examples/widgets/meter/lv_example_meter_3.c +++ b/examples/widgets/meter/lv_example_meter_3.c @@ -8,7 +8,6 @@ static void set_value(void * indic, int32_t v) lv_meter_set_indicator_end_value(meter, indic, v); } - /** * A clock from a meter */ diff --git a/src/core/lv_event.c b/src/core/lv_event.c index 0637aa2fd..43cdf2b21 100644 --- a/src/core/lv_event.c +++ b/src/core/lv_event.c @@ -285,7 +285,7 @@ static bool event_is_bubbled(lv_event_code_t e) case LV_EVENT_CHILD_CHANGED: case LV_EVENT_SIZE_CHANGED: case LV_EVENT_STYLE_CHANGED: - case LV_EVENT_GET_SELF_SIZE: + case LV_EVENT_REFR_SELF_SIZE: return false; default: return true; diff --git a/src/core/lv_event.h b/src/core/lv_event.h index 916f826dd..efc319d11 100644 --- a/src/core/lv_event.h +++ b/src/core/lv_event.h @@ -76,7 +76,7 @@ typedef enum { LV_EVENT_SIZE_CHANGED, /**< Object coordinates/size have changed*/ LV_EVENT_STYLE_CHANGED, /**< Object's style has changed*/ LV_EVENT_BASE_DIR_CHANGED, /**< The base dir has changed*/ - LV_EVENT_GET_SELF_SIZE, /**< Get the internal size of a widget*/ + LV_EVENT_REFR_SELF_SIZE, /**< Get the internal size of a widget*/ _LV_EVENT_LAST /** Number of default events*/ }lv_event_code_t; diff --git a/src/core/lv_obj.h b/src/core/lv_obj.h index dda91f22a..a133a269f 100644 --- a/src/core/lv_obj.h +++ b/src/core/lv_obj.h @@ -164,6 +164,7 @@ typedef struct _lv_obj_t { void * user_data; #endif lv_area_t coords; + lv_point_t self_size; lv_obj_flag_t flags; lv_state_t state; uint16_t layout_inv :1; diff --git a/src/core/lv_obj_pos.c b/src/core/lv_obj_pos.c index 100a5a3a4..b54acdef8 100644 --- a/src/core/lv_obj_pos.c +++ b/src/core/lv_obj_pos.c @@ -592,23 +592,20 @@ void lv_obj_get_content_coords(const lv_obj_t * obj, lv_area_t * area) lv_coord_t lv_obj_get_self_width(struct _lv_obj_t * obj) { - lv_point_t p = {0, LV_COORD_MIN}; - lv_event_send((lv_obj_t * )obj, LV_EVENT_GET_SELF_SIZE, &p); - return p.x; + return obj->self_size.x; } lv_coord_t lv_obj_get_self_height(struct _lv_obj_t * obj) { - lv_point_t p = {LV_COORD_MIN, 0}; - lv_event_send((lv_obj_t * )obj, LV_EVENT_GET_SELF_SIZE, &p); - return p.y; + return obj->self_size.y; } -bool lv_obj_handle_self_size_chg(struct _lv_obj_t * obj) +bool lv_obj_refresh_self_size(struct _lv_obj_t * obj) { - lv_coord_t w_set = lv_obj_get_style_width(obj, LV_PART_MAIN); - lv_coord_t h_set = lv_obj_get_style_height(obj, LV_PART_MAIN); - if(w_set != LV_SIZE_CONTENT && h_set != LV_SIZE_CONTENT) return false; + lv_obj_update_layout(obj); + obj->self_size.x = 0; + obj->self_size.y = 0; + lv_event_send(obj, LV_EVENT_REFR_SELF_SIZE, &obj->self_size); lv_obj_refr_size(obj); return true; diff --git a/src/core/lv_obj_pos.h b/src/core/lv_obj_pos.h index 46cc71851..09bce23cf 100644 --- a/src/core/lv_obj_pos.h +++ b/src/core/lv_obj_pos.h @@ -293,7 +293,7 @@ lv_coord_t lv_obj_get_self_height(struct _lv_obj_t * obj); * @param obj pointer to an object * @return false: nothing happened; true: refresh happened */ -bool lv_obj_handle_self_size_chg(struct _lv_obj_t * obj); +bool lv_obj_refresh_self_size(struct _lv_obj_t * obj); void lv_obj_refr_pos(struct _lv_obj_t * obj); diff --git a/src/core/lv_obj_scroll.c b/src/core/lv_obj_scroll.c index 553e84c3e..e39637b47 100644 --- a/src/core/lv_obj_scroll.c +++ b/src/core/lv_obj_scroll.c @@ -299,10 +299,12 @@ void lv_obj_scroll_to_x(lv_obj_t * obj, lv_coord_t x, lv_anim_enable_t anim_en) /*Don't let scroll more then naturally possible by the size of the content*/ if(x < 0) x = 0; - lv_coord_t scroll_max = lv_obj_get_scroll_left(obj) + lv_obj_get_scroll_right(obj); - if(scroll_max < 0) scroll_max = 0; + if(x > 0) { + lv_coord_t scroll_max = lv_obj_get_scroll_left(obj) + lv_obj_get_scroll_right(obj); + if(scroll_max < 0) scroll_max = 0; - if(x > scroll_max) x = scroll_max; + if(x > scroll_max) x = scroll_max; + } lv_coord_t scroll_x = lv_obj_get_scroll_x(obj); lv_coord_t diff = -x + scroll_x; @@ -316,9 +318,11 @@ void lv_obj_scroll_to_y(lv_obj_t * obj, lv_coord_t y, lv_anim_enable_t anim_en) /*Don't let scroll more then naturally possible by the size of the content*/ if(y < 0) y = 0; - lv_coord_t scroll_max = lv_obj_get_scroll_top(obj) + lv_obj_get_scroll_bottom(obj); - if(scroll_max < 0) scroll_max = 0; - if(y > scroll_max) y = scroll_max; + if(y > 0) { + lv_coord_t scroll_max = lv_obj_get_scroll_top(obj) + lv_obj_get_scroll_bottom(obj); + if(scroll_max < 0) scroll_max = 0; + if(y > scroll_max) y = scroll_max; + } lv_coord_t scroll_y = lv_obj_get_scroll_y(obj); lv_coord_t diff = -y + scroll_y; diff --git a/src/draw/lv_draw_img.c b/src/draw/lv_draw_img.c index 8209575bd..b590f126e 100644 --- a/src/draw/lv_draw_img.c +++ b/src/draw/lv_draw_img.c @@ -61,7 +61,6 @@ void lv_draw_img_dsc_init(lv_draw_img_dsc_t * dsc) dsc->opa = LV_OPA_COVER; dsc->zoom = LV_IMG_ZOOM_NONE; dsc->antialias = LV_COLOR_DEPTH > 8 ? 1 : 0; - } /** diff --git a/src/widgets/lv_chart.c b/src/widgets/lv_chart.c index dd8d33873..526a5704f 100644 --- a/src/widgets/lv_chart.c +++ b/src/widgets/lv_chart.c @@ -196,6 +196,7 @@ void lv_chart_set_zoom_x(lv_obj_t * obj, uint16_t zoom_x) if(chart->zoom_x == zoom_x) return; chart->zoom_x = zoom_x; + lv_obj_refresh_self_size(obj); lv_obj_invalidate(obj); } @@ -207,6 +208,7 @@ void lv_chart_set_zoom_y(lv_obj_t * obj, uint16_t zoom_y) if(chart->zoom_y == zoom_y) return; chart->zoom_y = zoom_y; + lv_obj_refresh_self_size(obj); lv_obj_invalidate(obj); } @@ -645,7 +647,7 @@ static void lv_chart_event(const lv_obj_class_t * class_p, lv_event_t * e) lv_coord_t * s = lv_event_get_param(e); *s = LV_MAX4(*s, chart->tick[LV_CHART_AXIS_X].draw_size, chart->tick[LV_CHART_AXIS_PRIMARY_Y].draw_size, chart->tick[LV_CHART_AXIS_SECONDARY_Y].draw_size); - } else if(code == LV_EVENT_GET_SELF_SIZE) { + } else if(code == LV_EVENT_REFR_SELF_SIZE) { lv_point_t * p = lv_event_get_param(e); p->x = (lv_obj_get_content_width(obj) * chart->zoom_x) >> 8; p->y = (lv_obj_get_content_height(obj) * chart->zoom_y) >> 8; diff --git a/src/widgets/lv_checkbox.c b/src/widgets/lv_checkbox.c index 58bb0ba7b..40b6e8d73 100644 --- a/src/widgets/lv_checkbox.c +++ b/src/widgets/lv_checkbox.c @@ -82,7 +82,7 @@ void lv_checkbox_set_text(lv_obj_t * obj, const char * txt) cb->static_txt = 0; - lv_obj_handle_self_size_chg(obj); + lv_obj_refresh_self_size(obj); } void lv_checkbox_set_text_static(lv_obj_t * obj, const char * txt) @@ -94,7 +94,7 @@ void lv_checkbox_set_text_static(lv_obj_t * obj, const char * txt) cb->txt = (char*)txt; cb->static_txt = 1; - lv_obj_handle_self_size_chg(obj); + lv_obj_refresh_self_size(obj); } /*===================== @@ -155,7 +155,7 @@ static void lv_checkbox_event(const lv_obj_class_t * class_p, lv_event_t * e) if (code == LV_EVENT_PRESSED || code == LV_EVENT_RELEASED) { lv_obj_invalidate(obj); } - else if (code == LV_EVENT_GET_SELF_SIZE) { + else if (code == LV_EVENT_REFR_SELF_SIZE) { lv_point_t * p = lv_event_get_param(e); lv_checkbox_t * cb = (lv_checkbox_t *)obj; diff --git a/src/widgets/lv_dropdown.c b/src/widgets/lv_dropdown.c index 7b06643b2..2e38090f1 100644 --- a/src/widgets/lv_dropdown.c +++ b/src/widgets/lv_dropdown.c @@ -658,10 +658,14 @@ static void lv_dropdown_event(const lv_obj_class_t * class_p, lv_event_t * e) lv_obj_invalidate(obj); } } + else if(code == LV_EVENT_STYLE_CHANGED) { + lv_obj_refresh_self_size(obj); + } else if(code == LV_EVENT_SIZE_CHANGED) { + lv_obj_refresh_self_size(obj); if(dropdown->list) lv_dropdown_close(obj); } - else if(code == LV_EVENT_GET_SELF_SIZE) { + else if(code == LV_EVENT_REFR_SELF_SIZE) { lv_point_t * p = lv_event_get_param(e); const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); p->y = lv_font_get_line_height(font); diff --git a/src/widgets/lv_img.c b/src/widgets/lv_img.c index ce46906f1..c01082212 100644 --- a/src/widgets/lv_img.c +++ b/src/widgets/lv_img.c @@ -149,7 +149,7 @@ void lv_img_set_src(lv_obj_t * obj, const void * src) img->pivot.x = header.w / 2; img->pivot.y = header.h / 2; - lv_obj_handle_self_size_chg(obj); + lv_obj_refresh_self_size(obj); /*Provide enough room for the rotated corners*/ if(img->angle || img->zoom != LV_IMG_ZOOM_NONE) lv_obj_refresh_ext_draw_size(obj); @@ -471,7 +471,7 @@ static void lv_img_event(const lv_obj_class_t * class_p, lv_event_t * e) info->result = _lv_area_is_point_on(&a, info->point, 0); } } - else if(code == LV_EVENT_GET_SELF_SIZE) { + else if(code == LV_EVENT_REFR_SELF_SIZE) { lv_point_t * p = lv_event_get_param(e);; p->x = img->w; p->y = img->h; diff --git a/src/widgets/lv_label.c b/src/widgets/lv_label.c index a18c57610..76c14f47c 100644 --- a/src/widgets/lv_label.c +++ b/src/widgets/lv_label.c @@ -763,7 +763,7 @@ static void lv_label_event(const lv_obj_class_t * class_p, lv_event_t * e) lv_label_revert_dots(obj); lv_label_refr_text(obj); } - else if(code == LV_EVENT_GET_SELF_SIZE) { + else if(code == LV_EVENT_REFR_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); @@ -905,7 +905,7 @@ static void lv_label_refr_text(lv_obj_t * obj) lv_txt_get_size(&size, label->text, font, letter_space, line_space, max_w, flag); - lv_obj_handle_self_size_chg(obj); + lv_obj_refresh_self_size(obj); /*In scroll mode start an offset animations*/ if(label->long_mode == LV_LABEL_LONG_SCROLL) { @@ -1079,7 +1079,7 @@ static void lv_label_refr_text(lv_obj_t * obj) } } else if(label->long_mode == LV_LABEL_LONG_DOT) { - lv_obj_handle_self_size_chg(obj); + lv_obj_refresh_self_size(obj); if(size.y <= lv_area_get_height(&txt_coords)) { /*No dots are required, the text is short enough*/ label->dot_end = LV_LABEL_DOT_END_INV; } diff --git a/src/widgets/lv_line.c b/src/widgets/lv_line.c index ba0c5f0b8..42f1849bb 100644 --- a/src/widgets/lv_line.c +++ b/src/widgets/lv_line.c @@ -69,7 +69,7 @@ void lv_line_set_points(lv_obj_t * obj, const lv_point_t points[], uint16_t poin line->point_array = points; line->point_num = point_num; - lv_obj_handle_self_size_chg(obj); + lv_obj_refresh_self_size(obj); lv_obj_invalidate(obj); } @@ -138,7 +138,7 @@ static void lv_line_event(const lv_obj_class_t * class_p, lv_event_t * e) lv_coord_t * s = lv_event_get_param(e); if(*s < line_width) *s = line_width; } - else if(code == LV_EVENT_GET_SELF_SIZE) { + else if(code == LV_EVENT_REFR_SELF_SIZE) { lv_line_t * line = (lv_line_t *)obj; lv_point_t * p = lv_event_get_param(e); diff --git a/src/widgets/lv_roller.c b/src/widgets/lv_roller.c index 874aa8785..4fbf24fc9 100644 --- a/src/widgets/lv_roller.c +++ b/src/widgets/lv_roller.c @@ -317,7 +317,7 @@ static void lv_roller_event(const lv_obj_class_t * class_p, lv_event_t * e) lv_obj_t * obj = lv_event_get_target(e); lv_roller_t * roller = (lv_roller_t*)obj; - if(code == LV_EVENT_GET_SELF_SIZE) { + if(code == LV_EVENT_REFR_SELF_SIZE) { lv_point_t * p = lv_event_get_param(e); p->x = get_selected_label_width(obj); } @@ -325,7 +325,7 @@ static void lv_roller_event(const lv_obj_class_t * class_p, lv_event_t * e) lv_obj_t * label = get_label(obj); /*Be sure the label's style is updated before processing the roller*/ if(label) lv_event_send(label, LV_EVENT_STYLE_CHANGED, NULL); - lv_obj_handle_self_size_chg(obj); + lv_obj_refresh_self_size(obj); refr_position(obj, false); } else if(code == LV_EVENT_SIZE_CHANGED) { diff --git a/src/widgets/lv_table.c b/src/widgets/lv_table.c index bf0556b74..c5675fb88 100644 --- a/src/widgets/lv_table.c +++ b/src/widgets/lv_table.c @@ -476,7 +476,7 @@ static void lv_table_event(const lv_obj_class_t * class_p, lv_event_t * e) if(code == LV_EVENT_STYLE_CHANGED) { refr_size(obj, 0); } - else if(code == LV_EVENT_GET_SELF_SIZE) { + else if(code == LV_EVENT_REFR_SELF_SIZE) { lv_point_t * p = lv_event_get_param(e); uint32_t i; lv_coord_t w = 0; @@ -779,7 +779,7 @@ static void refr_size(lv_obj_t * obj, uint32_t strat_row) table->row_h[i] = LV_CLAMP(minh, table->row_h[i], maxh); } - lv_obj_handle_self_size_chg(obj) ; + lv_obj_refresh_self_size(obj) ; } static lv_coord_t get_row_height(lv_obj_t * obj, uint16_t row_id, const lv_font_t * font,