feat(scroll): add lv_obj_update_snap()

This commit is contained in:
Gabor Kiss-Vamosi
2021-02-23 11:30:27 +01:00
parent d55a27887e
commit 1442a0f8bc
4 changed files with 41 additions and 5 deletions

View File

@@ -230,6 +230,12 @@ lv_coord_t lv_indev_scroll_throw_predict(lv_indev_t * indev, lv_dir_t dir)
return sum;
}
void lv_indev_scroll_get_snap_dist(lv_obj_t * obj, lv_point_t * p)
{
p->x = find_snap_point_x(obj, obj->coords.x1, obj->coords.x2, 0);
p->y = find_snap_point_y(obj, obj->coords.y1, obj->coords.y2, 0);
}
/**********************
* STATIC FUNCTIONS
**********************/
@@ -398,6 +404,15 @@ static void init_scroll_limits(lv_indev_proc_t * proc)
if(proc->types.pointer.scroll_area.y2 == 0) proc->types.pointer.scroll_area.y2 = LV_COORD_MAX;
}
/**
* Search for snap point in the `min` - `max` range.
* @param obj the object on which snap point should be found
* @param min ignore snap points smaller then this. (Absolute coordinate)
* @param max ignore snap points greater then this. (Absolute coordinate)
* @param ofs offset to snap points. Useful the get a snap point in an imagined case
* what if children are already moved by this value
* @return the distance of the snap point.
*/
static lv_coord_t find_snap_point_x(const lv_obj_t * obj, lv_coord_t min, lv_coord_t max, lv_coord_t ofs)
{
lv_snap_align_t align = lv_obj_get_scroll_snap_align_x(obj);

View File

@@ -29,24 +29,31 @@ extern "C" {
/**
* Handle scrolling. Called by LVGL during input device processing
* @param proc pointer to an input device's proc field
* @param proc pointer to an input device's proc field
*/
void _lv_indev_scroll_handler(lv_indev_proc_t * proc);
/**
* Handle throwing after scrolling. Called by LVGL during input device processing
* @param proc pointer to an input device's proc field
* @param proc pointer to an input device's proc field
*/
void _lv_indev_scroll_throw_handler(lv_indev_proc_t * proc);
/**
* Predict where would a scroll throw end
* @param indev pointer to an input device
* @param dir `LV_DIR_VER` or `LV_DIR_HOR`
* @return the difference compared to the current position when the throw would be finished
* @param indev pointer to an input device
* @param dir ` LV_DIR_VER` or `LV_DIR_HOR`
* @return the difference compared to the current position when the throw would be finished
*/
lv_coord_t lv_indev_scroll_throw_predict(lv_indev_t * indev, lv_dir_t dir);
/**
* Get the distance of the nearest snap point
* @param obj the object on which snap points should be found
* @param p save the distance of the found snap point there
*/
void lv_indev_scroll_get_snap_dist(lv_obj_t * obj, lv_point_t * p);
/**********************
* MACROS
**********************/

View File

@@ -10,6 +10,7 @@
#include "lv_obj.h"
#include "lv_indev.h"
#include "lv_disp.h"
#include "lv_indev_scroll.h"
/*********************
* DEFINES
@@ -396,6 +397,13 @@ void lv_obj_scroll_to_view_recursive(lv_obj_t * obj, lv_anim_enable_t anim_en)
}
}
void lv_obj_update_snap(lv_obj_t * obj, lv_anim_enable_t anim_en)
{
lv_point_t p;
lv_indev_scroll_get_snap_dist(obj, &p);
lv_obj_scroll_by(obj, p.x, p.y, anim_en);
}
void lv_obj_get_scrollbar_area(lv_obj_t * obj, lv_area_t * hor_area, lv_area_t * ver_area)
{
lv_area_set(hor_area, 0, 0, 0, 0);

View File

@@ -248,6 +248,12 @@ void lv_obj_scroll_to_view(struct _lv_obj_t * obj, lv_anim_enable_t anim_en);
*/
void lv_obj_scroll_to_view_recursive(struct _lv_obj_t * obj, lv_anim_enable_t anim_en);
/**
* Check the children of `obj` and scroll `obj` to fulfill the scroll_snap settings
* @param obj an object whose children needs to checked and snapped
* @param anim_en LV_ANIM_ON/OFF
*/
void lv_obj_update_snap(struct _lv_obj_t * obj, lv_anim_enable_t anim_en);
/**
* Get the area of the scrollbars