在应用page widgets时遇到了一个坑:
在page上创建了container子对象,然后编写container子对象的event响应,在响应函数中需要获取其父对象的资源,然后发现它父对象变成了它爷爷了。一路debug终于找到问题,在page的LV_SIGNAL_CHILD_CHG 信号中,将子对象移到了可滚动的对象上(lv_obj_set_parent(tmp, ext->scrl);),就这样儿子变孙子了。虽然能正常使用,虽然很巧妙,就是感觉怪怪的。
/*Automatically move children to the scrollable object*/
else if(sign == LV_SIGNAL_CHILD_CHG) {
lv_obj_t * child;
if(ext->scrl == NULL) return LV_RES_OK;
lv_fit_t fit_left = lv_page_get_scrl_fit_left(page);
lv_fit_t fit_right = lv_page_get_scrl_fit_right(page);
lv_fit_t fit_top = lv_page_get_scrl_fit_top(page);
lv_bidi_dir_t base_dir = lv_obj_get_base_dir(page);
lv_style_int_t scrl_left = lv_obj_get_style_pad_left(ext->scrl, LV_CONT_PART_MAIN);
lv_style_int_t scrl_right = lv_obj_get_style_pad_right(ext->scrl, LV_CONT_PART_MAIN);
lv_style_int_t scrl_top = lv_obj_get_style_pad_top(ext->scrl, LV_CONT_PART_MAIN);
child = lv_obj_get_child(page, NULL);
while(child != NULL) {
if(lv_obj_is_protected(child, LV_PROTECT_PARENT) == false) {
lv_obj_t * tmp = child;
child = lv_obj_get_child(page, child); /*Get the next child before move this*/
/* Reposition the child to take padding into account
* It's required to keep new the object on the same coordinate if FIT is enabled.*/
if((tmp->coords.x1 == page->coords.x1) &&
(fit_left == LV_FIT_TIGHT || fit_left == LV_FIT_MAX) &&
base_dir != LV_BIDI_DIR_RTL) {
tmp->coords.x1 += scrl_left;
tmp->coords.x2 += scrl_left;
}
else if((tmp->coords.x2 == page->coords.x2) &&
(fit_right == LV_FIT_TIGHT || fit_right == LV_FIT_MAX)
&& base_dir == LV_BIDI_DIR_RTL) {
tmp->coords.x1 -= scrl_right;
tmp->coords.x2 -= scrl_right;
}
if((tmp->coords.y1 == page->coords.y1) && (fit_top == LV_FIT_TIGHT || fit_top == LV_FIT_MAX)) {
tmp->coords.y1 += scrl_top;
tmp->coords.y2 += scrl_top;
}
lv_obj_set_parent(tmp, ext->scrl);
}
else {
child = lv_obj_get_child(page, child);
}
}
}