[lv_page] Padding ignored when focus

LVGL Version

7.1.0

Description

Hi,
I have a problem with the function lv_page_focus(), when a padding has been configured for the part LV_PAGE_PART_BG.
On the following example (See the current behaviour on the screenshot), when the line 4 is focused, the padding is not used to position the scrollable object. But when the line 5 is focused, this padding is correctly used.

I have created a patch, which seems to work, but I am not sure that I have not forgotten other parameters.
You can find the result on the screenshot.

Code to reproduce

static int pos = 0;
static lv_obj_t * page;
static lv_obj_t * item[6];
static lv_obj_t * label[6];

void create_list(void)
{
    int id;

    /*Create a page*/
    page = lv_page_create(lv_scr_act(), NULL);
    lv_obj_set_size(page, 240, 320);
    lv_obj_align(page, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_page_set_scrl_layout(page, LV_LAYOUT_COLUMN_LEFT);
    lv_obj_set_style_local_pad_bottom(page, LV_PAGE_PART_BG, LV_STATE_DEFAULT, 40);

    for(id=0 ; id<6 ; ++id)
    {
        item[id] = lv_cont_create(page, NULL);
        lv_obj_set_size(item[id], 240, 80);
        lv_obj_set_style_local_bg_opa(item[id], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
        lv_obj_set_style_local_bg_color(item[id], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
        lv_obj_set_style_local_bg_grad_color(item[id], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);

        label[id] = lv_label_create(item[id], NULL);
        lv_obj_set_width(label[id], 240);
        lv_label_set_align(label[id], LV_LABEL_ALIGN_CENTER);
        lv_obj_set_style_local_text_font(label[id], LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &lv_font_montserrat_40);
        lv_obj_set_style_local_text_color(label[id], LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
        lv_label_set_text_fmt(label[id], "Line %d", id);
        lv_obj_align(label[id], item[id], LV_ALIGN_CENTER, 0, 0);
    }

    lv_page_focus(page, item[pos], FALSE);
    lv_obj_set_style_local_text_color(label[pos], LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_color(item[pos], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_set_style_local_bg_grad_color(item[pos], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
}

void select_next_item(void)
{
    lv_obj_set_style_local_text_color(label[pos], LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_set_style_local_bg_color(item[pos], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_grad_color(item[pos], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    ++pos;
    lv_page_focus(page, item[pos], FALSE);
    lv_obj_set_style_local_text_color(label[pos], LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_color(item[pos], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_set_style_local_bg_grad_color(item[pos], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
}

Patch

diff --git a/src/lv_widgets/lv_page.c b/src/lv_widgets/lv_page.c
index 726e404d..7efe2aab 100644
--- a/src/lv_widgets/lv_page.c
+++ b/src/lv_widgets/lv_page.c
@@ -495,14 +495,14 @@ void lv_page_focus(lv_obj_t * page, const lv_obj_t * obj, lv_anim_enable_t anim_
     lv_coord_t scrlable_y = lv_obj_get_y(ext->scrl);
     lv_coord_t page_h     = lv_obj_get_height(page);

-    lv_coord_t top_err = -(scrlable_y + obj_y);
-    lv_coord_t bot_err = scrlable_y + obj_y + obj_h - page_h;
-
     lv_style_int_t bg_top = lv_obj_get_style_pad_top(page, LV_PAGE_PART_BG);
     lv_style_int_t bg_bottom = lv_obj_get_style_pad_bottom(page, LV_PAGE_PART_BG);
     lv_style_int_t scrl_top = lv_obj_get_style_pad_top(ext->scrl, LV_CONT_PART_MAIN);
     lv_style_int_t scrl_bottom = lv_obj_get_style_pad_bottom(ext->scrl, LV_CONT_PART_MAIN);

+    lv_coord_t top_err = -(scrlable_y + (obj_y - scrl_top - bg_top) - scrl_top);
+    lv_coord_t bot_err = scrlable_y + (obj_y + scrl_bottom + bg_bottom) + scrl_bottom + obj_h - page_h;
+
     /*Out of the page on the top*/
     if((obj_h <= page_h && top_err > 0) || (obj_h > page_h && top_err < bot_err)) {
         /*Calculate a new position and let some space above*/
@@ -524,14 +524,14 @@ void lv_page_focus(lv_obj_t * page, const lv_obj_t * obj, lv_anim_enable_t anim_
     lv_coord_t scrlable_x = lv_obj_get_x(ext->scrl);
     lv_coord_t page_w     = lv_obj_get_width(page);

-    lv_coord_t left_err  = -(scrlable_x + obj_x);
-    lv_coord_t right_err = scrlable_x + obj_x + obj_w - page_w;
-
     lv_style_int_t bg_left = lv_obj_get_style_pad_left(page, LV_PAGE_PART_BG);
     lv_style_int_t bg_right = lv_obj_get_style_pad_right(page, LV_PAGE_PART_BG);
     lv_style_int_t scrl_left = lv_obj_get_style_pad_top(ext->scrl, LV_CONT_PART_MAIN);
     lv_style_int_t scrl_right = lv_obj_get_style_pad_bottom(ext->scrl, LV_CONT_PART_MAIN);

+    lv_coord_t left_err  = -(scrlable_x + (obj_x - scrl_left - bg_left) - scrl_left);
+    lv_coord_t right_err = scrlable_x + (obj_x + scrl_right + bg_right) + scrl_right + obj_w - page_w;
+
     /*Out of the page on the left*/
     if((obj_w <= page_w && left_err > 0) || (obj_w > page_w && left_err < right_err)) {
         /*Calculate a new position and let some space on the side*/

Screenshot

page

Could you send a pull request to master with the patch?

The pull request has been sent :

Thank you