From 39d03a80f45847a1977cfe9cc6a509b1613d0aca Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Wed, 16 Nov 2022 11:32:00 +0100 Subject: [PATCH] feat(table): scroll to the selected cell with key navigation Fixes https://forum.lvgl.io/t/grid-navigation-with-automatic-scrolling-for-table/10285 --- src/widgets/lv_table.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/widgets/lv_table.c b/src/widgets/lv_table.c index 5ff65ab23..8adba1310 100644 --- a/src/widgets/lv_table.c +++ b/src/widgets/lv_table.c @@ -42,6 +42,7 @@ static lv_res_t get_pressed_cell(lv_obj_t * obj, uint16_t * row, uint16_t * col) static size_t get_cell_txt_len(const char * txt); static void copy_cell_txt(char * dst, const char * txt); static void get_cell_area(lv_obj_t * obj, uint16_t row, uint16_t col, lv_area_t * area); +static void scroll_to_selected_cell(lv_obj_t * obj); static inline bool is_cell_empty(void * cell) { @@ -516,6 +517,7 @@ static void lv_table_event(const lv_obj_class_t * class_p, lv_event_t * e) if(col == LV_TABLE_CELL_NONE || row == LV_TABLE_CELL_NONE) { table->col_act = 0; table->row_act = 0; + scroll_to_selected_cell(obj); lv_obj_invalidate(obj); return; } @@ -560,6 +562,8 @@ static void lv_table_event(const lv_obj_class_t * class_p, lv_event_t * e) table->row_act = row; lv_obj_invalidate(obj); + scroll_to_selected_cell(obj); + res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL); if(res != LV_RES_OK) return; } @@ -1004,4 +1008,26 @@ static void get_cell_area(lv_obj_t * obj, uint16_t row, uint16_t col, lv_area_t } + +static void scroll_to_selected_cell(lv_obj_t * obj) +{ + lv_table_t * table = (lv_table_t *)obj; + + lv_area_t a; + get_cell_area(obj, table->row_act, table->col_act, &a); + if(a.x1 < 0) { + lv_obj_scroll_by_bounded(obj, -a.x1, 0, LV_ANIM_ON); + } + else if(a.x2 > lv_obj_get_width(obj)) { + lv_obj_scroll_by_bounded(obj, lv_obj_get_width(obj) - a.x2, 0, LV_ANIM_ON); + } + + if(a.y1 < 0) { + lv_obj_scroll_by_bounded(obj, 0, -a.y1, LV_ANIM_ON); + } + else if(a.y2 > lv_obj_get_height(obj)) { + lv_obj_scroll_by_bounded(obj, 0, lv_obj_get_height(obj) - a.y2 , LV_ANIM_ON); + } + +} #endif