1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00

feat(layout) add user_data to layout callbacks

It was required for compatibility with the MicroPython binding.
This commit is contained in:
Gabor Kiss-Vamosi 2021-04-19 09:44:03 +02:00
parent 14bc4e9f8f
commit a9ed244bb1
5 changed files with 67 additions and 23 deletions

View File

@ -306,13 +306,16 @@ void lv_obj_update_layout(const lv_obj_t * obj)
mutex = false;
}
uint32_t lv_layout_register(lv_layout_update_cb_t cb)
uint32_t lv_layout_register(lv_layout_update_cb_t cb, void * user_data)
{
layout_cnt++;
LV_GC_ROOT(_lv_layout_list) = lv_mem_realloc(LV_GC_ROOT(_lv_layout_list), layout_cnt * sizeof(lv_layout_update_cb_t));
LV_GC_ROOT(_lv_layout_list) = lv_mem_realloc(LV_GC_ROOT(_lv_layout_list), layout_cnt * sizeof(lv_layout_dsc_t));
LV_ASSERT_MALLOC(LV_GC_ROOT(_lv_layout_list));
LV_GC_ROOT(_lv_layout_list)[layout_cnt - 1] = cb;
LV_GC_ROOT(_lv_layout_list)[layout_cnt - 1].cb = cb;
#if LV_USE_USER_DATA
LV_GC_ROOT(_lv_layout_list)[layout_cnt - 1].user_data = user_data;
#endif
return layout_cnt; /*No -1 to skip 0th index*/
}
@ -605,22 +608,37 @@ void lv_obj_refr_pos(lv_obj_t * obj)
if(lv_obj_is_layout_positioned(obj)) return;
lv_obj_t * parent = lv_obj_get_parent(obj);
lv_coord_t x = lv_obj_get_style_x(obj, LV_PART_MAIN) + lv_obj_get_style_transform_x(obj, LV_PART_MAIN);
lv_coord_t y = lv_obj_get_style_y(obj, LV_PART_MAIN) + lv_obj_get_style_transform_y(obj, LV_PART_MAIN);
lv_coord_t x = lv_obj_get_style_x(obj, LV_PART_MAIN);
lv_coord_t y = lv_obj_get_style_y(obj, LV_PART_MAIN);
if(parent == NULL) {
lv_obj_move_to(obj, x, y);
return;
}
/*Handle percentage value*/
lv_coord_t pw = lv_obj_get_width_fit(parent);
lv_coord_t ph = lv_obj_get_height_fit(parent);
if(LV_COORD_IS_PCT(x)) x = (pw * LV_COORD_GET_PCT(x)) / 100;
if(LV_COORD_IS_PCT(y)) y = (ph * LV_COORD_GET_PCT(y)) / 100;
/*Handle percentage value of translate*/
lv_coord_t tr_x = lv_obj_get_style_transform_x(obj, LV_PART_MAIN);
lv_coord_t tr_y = lv_obj_get_style_transform_y(obj, LV_PART_MAIN);
lv_coord_t w = lv_obj_get_width(obj);
lv_coord_t h = lv_obj_get_height(obj);
if(LV_COORD_IS_PCT(tr_x)) tr_x = (w * LV_COORD_GET_PCT(tr_x)) / 100;
if(LV_COORD_IS_PCT(tr_y)) tr_y = (h * LV_COORD_GET_PCT(tr_y)) / 100;
/*Use the translation*/
x += tr_x;
y += tr_y;
lv_align_t align = lv_obj_get_style_align(obj, LV_PART_MAIN);
if(align == LV_ALIGN_TOP_LEFT) {
lv_obj_move_to(obj, x, y);
}
else {
lv_coord_t pw = lv_obj_get_width_fit(parent);
lv_coord_t ph = lv_obj_get_height_fit(parent);
lv_coord_t w = lv_obj_get_width(obj);
lv_coord_t h = lv_obj_get_height(obj);
switch(align) {
case LV_ALIGN_TOP_MID:
@ -950,7 +968,8 @@ static void layout_update_core(lv_obj_t * obj)
if(lv_obj_get_child_cnt(obj) > 0) {
uint32_t layout_id = lv_obj_get_style_layout(obj, LV_PART_MAIN);
if(layout_id > 0 && layout_id <= layout_cnt) {
LV_GC_ROOT(_lv_layout_list)[layout_id -1](obj);
void * user_data = LV_GC_ROOT(_lv_layout_list)[layout_id -1].user_data;
LV_GC_ROOT(_lv_layout_list)[layout_id -1].cb(obj, user_data);
}
}
}

View File

@ -23,7 +23,13 @@ extern "C" {
* TYPEDEFS
**********************/
struct _lv_obj_t;
typedef void (*lv_layout_update_cb_t)(struct _lv_obj_t *);
typedef void (*lv_layout_update_cb_t)(struct _lv_obj_t *, void * user_data);
typedef struct {
lv_layout_update_cb_t cb;
#if LV_USE_USER_DATA
void * user_data;
#endif
}lv_layout_dsc_t;
/**********************
* GLOBAL PROTOTYPES
@ -133,9 +139,10 @@ void lv_obj_update_layout(const struct _lv_obj_t * obj);
/**
* Regsiter a new layout
* @param cb the layout update callback
* @param user_data custom data that will be passed to `cb`
* @return the ID of the new layout
*/
uint32_t lv_layout_register(lv_layout_update_cb_t cb);
uint32_t lv_layout_register(lv_layout_update_cb_t cb, void * user_data);
/**
* Align an object to an other object.

View File

@ -53,7 +53,7 @@ typedef struct {
/**********************
* STATIC PROTOTYPES
**********************/
static void flex_update(lv_obj_t * cont);
static void flex_update(lv_obj_t * cont, void * user_data);
static int32_t find_track_end(lv_obj_t * cont, flex_t * f, int32_t item_start_id, lv_coord_t item_gap, lv_coord_t max_main_size, track_t * t);
static void children_repos(lv_obj_t * cont, flex_t * f, int32_t item_first_id, int32_t item_last_id, lv_coord_t abs_x, lv_coord_t abs_y, lv_coord_t max_main_size, lv_coord_t item_gap, track_t * t);
static void place_content(lv_flex_place_t place, lv_coord_t max_size, lv_coord_t content_size, lv_coord_t item_cnt, lv_coord_t * start_pos, lv_coord_t * gap);
@ -88,7 +88,7 @@ lv_style_prop_t LV_STYLE_FLEX_GROW;
void lv_flex_init(void)
{
LV_LAYOUT_FLEX = lv_layout_register(flex_update);
LV_LAYOUT_FLEX = lv_layout_register(flex_update, NULL);
LV_STYLE_FLEX_FLOW = lv_style_register_prop();
LV_STYLE_FLEX_MAIN_PLACE = lv_style_register_prop() | LV_STYLE_PROP_LAYOUT_REFR;
@ -119,9 +119,10 @@ void lv_obj_set_flex_grow(struct _lv_obj_t * obj, uint8_t grow)
* STATIC FUNCTIONS
**********************/
static void flex_update(lv_obj_t * cont)
static void flex_update(lv_obj_t * cont, void * user_data)
{
LV_LOG_INFO("update %p container", cont);
LV_UNUSED(user_data);
flex_t f;
lv_flex_flow_t flow = lv_obj_get_style_flex_flow(cont, LV_PART_MAIN);
@ -399,8 +400,16 @@ static void children_repos(lv_obj_t * cont, flex_t * f, int32_t item_first_id, i
if(f->row && rtl) main_pos -= area_get_main_size(&item->coords);
lv_coord_t diff_x = abs_x - item->coords.x1 + lv_obj_get_style_transform_x(item, 0);
lv_coord_t diff_y = abs_y - item->coords.y1 + lv_obj_get_style_transform_y(item, 0);
/*Handle percentage value of translate*/
lv_coord_t tr_x = lv_obj_get_style_transform_x(item, LV_PART_MAIN);
lv_coord_t tr_y = lv_obj_get_style_transform_y(item, LV_PART_MAIN);
lv_coord_t w = lv_obj_get_width(item);
lv_coord_t h = lv_obj_get_height(item);
if(LV_COORD_IS_PCT(tr_x)) tr_x = (w * LV_COORD_GET_PCT(tr_x)) / 100;
if(LV_COORD_IS_PCT(tr_y)) tr_y = (h * LV_COORD_GET_PCT(tr_y)) / 100;
lv_coord_t diff_x = abs_x - item->coords.x1 + tr_x;
lv_coord_t diff_y = abs_y - item->coords.y1 + tr_y;
diff_x += f->row ? main_pos : cross_pos;
diff_y += f->row ? cross_pos : main_pos;

View File

@ -48,7 +48,7 @@ typedef struct {
/**********************
* STATIC PROTOTYPES
**********************/
static void grid_update(lv_obj_t * cont);
static void grid_update(lv_obj_t * cont, void * user_data);
static void calc(struct _lv_obj_t * obj, _lv_grid_calc_t * calc);
static void calc_free(_lv_grid_calc_t * calc);
static void calc_cols(lv_obj_t * cont, _lv_grid_calc_t * c);
@ -98,7 +98,7 @@ lv_style_prop_t LV_STYLE_GRID_CELL_ROW_PLACE;
void lv_grid_init(void)
{
LV_LAYOUT_GRID = lv_layout_register(grid_update);
LV_LAYOUT_GRID = lv_layout_register(grid_update, NULL);
LV_STYLE_GRID_COLUMN_DSC_ARRAY = lv_style_register_prop() | LV_STYLE_PROP_LAYOUT_REFR;
LV_STYLE_GRID_ROW_DSC_ARRAY = lv_style_register_prop() | LV_STYLE_PROP_LAYOUT_REFR;
@ -146,9 +146,10 @@ void lv_obj_set_grid_cell(lv_obj_t * obj, lv_grid_place_t hor_place, uint8_t col
* STATIC FUNCTIONS
**********************/
static void grid_update(lv_obj_t * cont)
static void grid_update(lv_obj_t * cont, void * user_data)
{
LV_LOG_INFO("update %p container", cont);
LV_UNUSED(user_data);
const lv_coord_t * col_templ = get_col_dsc(cont);
const lv_coord_t * row_templ = get_row_dsc(cont);
@ -445,8 +446,16 @@ static void item_repos(lv_obj_t * item, _lv_grid_calc_t * c, item_repos_hint_t *
}
x += lv_obj_get_style_transform_x(item, LV_PART_MAIN);
y += lv_obj_get_style_transform_y(item, LV_PART_MAIN);
/*Handle percentage value of translate*/
lv_coord_t tr_x = lv_obj_get_style_transform_x(item, LV_PART_MAIN);
lv_coord_t tr_y = lv_obj_get_style_transform_y(item, LV_PART_MAIN);
lv_coord_t w = lv_obj_get_width(item);
lv_coord_t h = lv_obj_get_height(item);
if(LV_COORD_IS_PCT(tr_x)) tr_x = (w * LV_COORD_GET_PCT(tr_x)) / 100;
if(LV_COORD_IS_PCT(tr_y)) tr_y = (h * LV_COORD_GET_PCT(tr_y)) / 100;
x += tr_x;
y += tr_y;
lv_coord_t diff_x = hint->grid_abs.x + x - item->coords.x1;
lv_coord_t diff_y = hint->grid_abs.y + y - item->coords.y1;

View File

@ -48,7 +48,7 @@ extern "C" {
LV_DISPATCH(f, lv_ll_t, _lv_group_ll) \
LV_DISPATCH(f, lv_ll_t, _lv_img_decoder_ll) \
LV_DISPATCH(f, lv_ll_t, _lv_obj_style_trans_ll) \
LV_DISPATCH(f, lv_layout_update_cb_t *, _lv_layout_list) \
LV_DISPATCH(f, lv_layout_dsc_t *, _lv_layout_list) \
LV_DISPATCH_COND(f, lv_img_cache_entry_t*, _lv_img_cache_array, LV_IMG_CACHE_DEF, 1) \
LV_DISPATCH_COND(f, lv_img_cache_entry_t, _lv_img_cache_single, LV_IMG_CACHE_DEF, 0) \
LV_DISPATCH(f, lv_timer_t*, _lv_timer_act) \