feat(scroll): user-defined scrollbar length using LV_STYLE_LENGTH (#7306)

Co-authored-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com>
This commit is contained in:
Michele Perla
2024-11-29 02:47:46 +01:00
committed by GitHub
parent 2093644d71
commit b05382e613
3 changed files with 26 additions and 4 deletions

View File

@@ -74,9 +74,27 @@ can be set directly on the :cpp:enumerator:`LV_PART_SCROLLBAR` part of a Widget,
on the Widget's LV_PART_MAIN part, or that of any of its parents, to make a scrollbar on the Widget's LV_PART_MAIN part, or that of any of its parents, to make a scrollbar
inherit the base direction. inherit the base direction.
``pad_left/right/top/bottom`` sets the spacing around the scrollbars and ``pad_left/right/top/bottom`` sets the spacing around the scrollbars,
``width`` sets the scrollbar's width. ``width`` sets the scrollbar's width and ``length`` sets the scrollbar's length:
If `length` is not set or left at `0` the scrollbar's length will be set automatically according to the length of the content.
.. code-block:: c
static lv_style_t style_scrollbar;
lv_style_init(&style_scrollbar);
lv_style_set_pad_left(&style_scrollbar, 2);
lv_style_set_pad_right(&style_scrollbar, 2);
lv_style_set_pad_top(&style_scrollbar, 2);
lv_style_set_pad_bottom(&style_scrollbar, 2);
lv_style_set_width(&style_scrollbar, 10);
lv_style_set_length(&style_scrollbar, 50);
...
lv_obj_add_style(widget, &style_scrollbar, LV_PART_SCROLLBAR);
The minimum length of the scrollbar is fixed to 10, while its maximum length is limited by the
Widget's height or width, depending on whether the scrollbar is vertical or horizontal. Any length value
set outside of these limits will automatically result in a length fixed to either limit.
.. _scroll_events: .. _scroll_events:

View File

@@ -39,6 +39,7 @@ void lv_example_scroll_4(void)
static lv_style_t style; static lv_style_t style;
lv_style_init(&style); lv_style_init(&style);
lv_style_set_width(&style, 4); /*Width of the scrollbar*/ lv_style_set_width(&style, 4); /*Width of the scrollbar*/
lv_style_set_length(&style, 20); /*Length of the scrollbar*/
lv_style_set_pad_right(&style, 5); /*Space from the parallel side*/ lv_style_set_pad_right(&style, 5); /*Space from the parallel side*/
lv_style_set_pad_top(&style, 5); /*Space from the perpendicular side*/ lv_style_set_pad_top(&style, 5); /*Space from the perpendicular side*/

View File

@@ -504,6 +504,7 @@ void lv_obj_get_scrollbar_area(lv_obj_t * obj, lv_area_t * hor_area, lv_area_t *
int32_t left_space = lv_obj_get_style_pad_left(obj, LV_PART_SCROLLBAR); int32_t left_space = lv_obj_get_style_pad_left(obj, LV_PART_SCROLLBAR);
int32_t right_space = lv_obj_get_style_pad_right(obj, LV_PART_SCROLLBAR); int32_t right_space = lv_obj_get_style_pad_right(obj, LV_PART_SCROLLBAR);
int32_t thickness = lv_obj_get_style_width(obj, LV_PART_SCROLLBAR); int32_t thickness = lv_obj_get_style_width(obj, LV_PART_SCROLLBAR);
int32_t length = lv_obj_get_style_length(obj, LV_PART_SCROLLBAR);
int32_t obj_h = lv_obj_get_height(obj); int32_t obj_h = lv_obj_get_height(obj);
int32_t obj_w = lv_obj_get_width(obj); int32_t obj_w = lv_obj_get_width(obj);
@@ -533,7 +534,8 @@ void lv_obj_get_scrollbar_area(lv_obj_t * obj, lv_area_t * hor_area, lv_area_t *
} }
int32_t sb_h = ((obj_h - top_space - bottom_space - hor_req_space) * obj_h) / content_h; int32_t sb_h = ((obj_h - top_space - bottom_space - hor_req_space) * obj_h) / content_h;
sb_h = LV_MAX(sb_h, SCROLLBAR_MIN_SIZE); sb_h = LV_MAX(length > 0 ? length : sb_h, SCROLLBAR_MIN_SIZE); /*Style-defined size, calculated size, or minimum size*/
sb_h = LV_MIN(sb_h, obj_h); /*Limit scrollbar length to parent height*/
rem = (obj_h - top_space - bottom_space - hor_req_space) - rem = (obj_h - top_space - bottom_space - hor_req_space) -
sb_h; /*Remaining size from the scrollbar track that is not the scrollbar itself*/ sb_h; /*Remaining size from the scrollbar track that is not the scrollbar itself*/
int32_t scroll_h = content_h - obj_h; /*The size of the content which can be really scrolled*/ int32_t scroll_h = content_h - obj_h; /*The size of the content which can be really scrolled*/
@@ -571,7 +573,8 @@ void lv_obj_get_scrollbar_area(lv_obj_t * obj, lv_area_t * hor_area, lv_area_t *
hor_area->x2 = obj->coords.x2; hor_area->x2 = obj->coords.x2;
int32_t sb_w = ((obj_w - left_space - right_space - ver_reg_space) * obj_w) / content_w; int32_t sb_w = ((obj_w - left_space - right_space - ver_reg_space) * obj_w) / content_w;
sb_w = LV_MAX(sb_w, SCROLLBAR_MIN_SIZE); sb_w = LV_MAX(length > 0 ? length : sb_w, SCROLLBAR_MIN_SIZE); /*Style-defined size, calculated size, or minimum size*/
sb_w = LV_MIN(sb_w, obj_w); /*Limit scrollbar length to parent width*/
rem = (obj_w - left_space - right_space - ver_reg_space) - rem = (obj_w - left_space - right_space - ver_reg_space) -
sb_w; /*Remaining size from the scrollbar track that is not the scrollbar itself*/ sb_w; /*Remaining size from the scrollbar track that is not the scrollbar itself*/
int32_t scroll_w = content_w - obj_w; /*The size of the content which can be really scrolled*/ int32_t scroll_w = content_w - obj_w; /*The size of the content which can be really scrolled*/