Skip to content

Commit

Permalink
feat(table): scroll to the selected cell with key navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
kisvegabor committed Nov 16, 2022
1 parent 340a1cb commit 39d03a8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/widgets/lv_table.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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

1 comment on commit 39d03a8

@kisvegabor
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please open an issue, and add a code snippet to reproduce the issue.

Please sign in to comment.