Skip to content

Commit

Permalink
fix(scroll) keep the scroll position on object deleted
Browse files Browse the repository at this point in the history
kisvegabor committed Jun 9, 2021
1 parent 769c4a3 commit 52edbb4
Showing 4 changed files with 48 additions and 36 deletions.
30 changes: 1 addition & 29 deletions src/core/lv_obj_pos.c
Original file line number Diff line number Diff line change
@@ -83,7 +83,6 @@ void lv_obj_set_y(lv_obj_t * obj, lv_coord_t y)

bool lv_obj_refr_size(lv_obj_t * obj)
{

LV_ASSERT_OBJ(obj, MY_CLASS);

/*If the width or height is set by a layout do not modify them*/
@@ -187,34 +186,7 @@ bool lv_obj_refr_size(lv_obj_t * obj)
/*Invalidate the new area*/
lv_obj_invalidate(obj);

/*Be sure the bottom side is not remains scrolled in*/
/*With snapping the content can't be scrolled in*/
if(lv_obj_get_scroll_snap_y(obj) == LV_SCROLL_SNAP_NONE) {
lv_coord_t st = lv_obj_get_scroll_top(obj);
lv_coord_t sb = lv_obj_get_scroll_bottom(obj);
if(sb < 0 && st > 0) {
sb = LV_MIN(st, -sb);
lv_obj_scroll_by(obj, 0, sb, LV_ANIM_OFF);
}
}

if(lv_obj_get_scroll_snap_x(obj) == LV_SCROLL_SNAP_NONE) {
lv_coord_t sl = lv_obj_get_scroll_left(obj);
lv_coord_t sr = lv_obj_get_scroll_right(obj);
if(lv_obj_get_style_base_dir(obj, LV_PART_MAIN) != LV_BASE_DIR_RTL) {
/*Be sure the left side is not remains scrolled in*/
if(sr < 0 && sl > 0) {
sr = LV_MIN(sl, -sr);
lv_obj_scroll_by(obj, sr, 0, LV_ANIM_OFF);
}
} else {
/*Be sure the right side is not remains scrolled in*/
if(sl < 0 && sr > 0) {
sr = LV_MIN(sr, -sl);
lv_obj_scroll_by(obj, sl, 0, LV_ANIM_OFF);
}
}
}
lv_obj_readjust_scroll(obj, LV_ANIM_OFF);

/*If the object was out of the parent invalidate the new scrollbar area too.
*If it wasn't out of the parent but out now, also invalidate the srollbars*/
39 changes: 38 additions & 1 deletion src/core/lv_obj_scroll.c
Original file line number Diff line number Diff line change
@@ -256,7 +256,6 @@ void lv_obj_scroll_by(lv_obj_t * obj, lv_coord_t x, lv_coord_t y, lv_anim_enable
lv_anim_set_ready_cb(&a, scroll_anim_ready_cb);

if(x) {

uint32_t t = lv_anim_speed_to_time((lv_disp_get_hor_res(d) * 2) >> 2, 0, x);
if(t < SCROLL_ANIM_TIME_MIN) t = SCROLL_ANIM_TIME_MIN;
if(t > SCROLL_ANIM_TIME_MAX) t = SCROLL_ANIM_TIME_MAX;
@@ -290,6 +289,9 @@ void lv_obj_scroll_by(lv_obj_t * obj, lv_coord_t x, lv_coord_t y, lv_anim_enable
}
} else {
/*Remove pending animations*/
lv_res_t res;
res = lv_event_send(obj, LV_EVENT_SCROLL_END, NULL);
if(res != LV_RES_OK) return;
lv_anim_del(obj, scroll_y_anim);
lv_anim_del(obj, scroll_x_anim);
scroll_by_raw(obj, x, y);
@@ -539,6 +541,38 @@ void lv_obj_scrollbar_invalidate(lv_obj_t * obj)
if(lv_area_get_size(&ver_area) > 0) lv_obj_invalidate_area(obj, &ver_area);
}

void lv_obj_readjust_scroll(lv_obj_t * obj, lv_anim_enable_t anim_en)
{
/*Be sure the bottom side is not remains scrolled in*/
/*With snapping the content can't be scrolled in*/
if(lv_obj_get_scroll_snap_y(obj) == LV_SCROLL_SNAP_NONE) {
lv_coord_t st = lv_obj_get_scroll_top(obj);
lv_coord_t sb = lv_obj_get_scroll_bottom(obj);
if(sb < 0 && st > 0) {
sb = LV_MIN(st, -sb);
lv_obj_scroll_by(obj, 0, sb, anim_en);
}
}

if(lv_obj_get_scroll_snap_x(obj) == LV_SCROLL_SNAP_NONE) {
lv_coord_t sl = lv_obj_get_scroll_left(obj);
lv_coord_t sr = lv_obj_get_scroll_right(obj);
if(lv_obj_get_style_base_dir(obj, LV_PART_MAIN) != LV_BASE_DIR_RTL) {
/*Be sure the left side is not remains scrolled in*/
if(sr < 0 && sl > 0) {
sr = LV_MIN(sl, -sr);
lv_obj_scroll_by(obj, sr, 0, anim_en);
}
} else {
/*Be sure the right side is not remains scrolled in*/
if(sl < 0 && sr > 0) {
sr = LV_MIN(sr, -sl);
lv_obj_scroll_by(obj, sl, 0, anim_en);
}
}
}
}

/**********************
* STATIC FUNCTIONS
**********************/
@@ -666,6 +700,9 @@ static void scroll_area_into_view(const lv_area_t * area, lv_obj_t * child, lv_p
}

/*Remove any pending scroll animations.*/
lv_res_t res;
res = lv_event_send(parent, LV_EVENT_SCROLL_END, NULL);
if(res != LV_RES_OK) return;
lv_anim_del(parent, scroll_x_anim);
lv_anim_del(parent, scroll_y_anim);

7 changes: 7 additions & 0 deletions src/core/lv_obj_scroll.h
Original file line number Diff line number Diff line change
@@ -266,6 +266,13 @@ void lv_obj_get_scrollbar_area(struct _lv_obj_t * obj, lv_area_t * hor, lv_area_
*/
void lv_obj_scrollbar_invalidate(struct _lv_obj_t * obj);

/**
* Checked if the content is scrolled "in" and adjusts it to a normal position.
* @param obj pointer to an object
* @param anim_en LV_ANIM_ON/OFF
*/
void lv_obj_readjust_scroll(struct _lv_obj_t * obj, lv_anim_enable_t anim_en);

/**********************
* MACROS
**********************/
8 changes: 2 additions & 6 deletions src/core/lv_obj_tree.c
Original file line number Diff line number Diff line change
@@ -67,12 +67,8 @@ void lv_obj_del(lv_obj_t * obj)

/*Call the ancestor's event handler to the parent to notify it about the child delete*/
if(par) {
/*Just to remove scroll animations if any*/
lv_obj_scroll_to(par, 0, 0, LV_ANIM_OFF);
if(par->spec_attr) {
par->spec_attr->scroll.x = 0;
par->spec_attr->scroll.y = 0;
}
lv_obj_readjust_scroll(par, LV_ANIM_OFF);
lv_obj_scrollbar_invalidate(par);
lv_event_send(par, LV_EVENT_CHILD_CHANGED, NULL);
}

0 comments on commit 52edbb4

Please sign in to comment.