From b6d2daa4935128ca8193863d4deaf58fa40b3154 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Thu, 17 Feb 2022 18:11:18 +0100 Subject: [PATCH] feat(gridnav): add lv_gridnav_set_focused fixes #3069 --- src/extra/others/gridnav/lv_gridnav.c | 21 +++++++++++++++++++++ src/extra/others/gridnav/lv_gridnav.h | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/src/extra/others/gridnav/lv_gridnav.c b/src/extra/others/gridnav/lv_gridnav.c index 874e6a982..a60298cfb 100644 --- a/src/extra/others/gridnav/lv_gridnav.c +++ b/src/extra/others/gridnav/lv_gridnav.c @@ -81,6 +81,27 @@ void lv_gridnav_remove(lv_obj_t * obj) lv_obj_remove_event_cb(obj, gridnav_event_cb); } +void lv_gridnav_set_focused(lv_obj_t * cont, lv_obj_t * to_focus, lv_anim_enable_t anim_en) +{ + LV_ASSERT_NULL(to_focus); + lv_gridnav_dsc_t * dsc = lv_obj_get_event_user_data(cont, gridnav_event_cb); + if(dsc == NULL) { + LV_LOG_WARN("`cont` is not a gridnav container"); + return; + } + + if(obj_is_focuable(to_focus) == false) { + LV_LOG_WARN("The object to focus is not focusable"); + return; + } + + lv_obj_clear_state(dsc->focused_obj, LV_STATE_FOCUSED | LV_STATE_FOCUS_KEY); + lv_obj_add_state(to_focus, LV_STATE_FOCUSED | LV_STATE_FOCUS_KEY); + lv_obj_scroll_to_view(to_focus, anim_en); + dsc->focused_obj = to_focus; + +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/extra/others/gridnav/lv_gridnav.h b/src/extra/others/gridnav/lv_gridnav.h index ea81595de..f480ded46 100644 --- a/src/extra/others/gridnav/lv_gridnav.h +++ b/src/extra/others/gridnav/lv_gridnav.h @@ -103,6 +103,14 @@ void lv_gridnav_add(lv_obj_t * obj, lv_gridnav_ctrl_t ctrl); */ void lv_gridnav_remove(lv_obj_t * obj); +/** + * Manually focus an object on gridnav container + * @param cont pointer to a gridnav container + * @param to_focus pointer to an object to focus + * @param anim_en LV_ANIM_ON/OFF + */ +void lv_gridnav_set_focused(lv_obj_t * cont, lv_obj_t * to_focus, lv_anim_enable_t anim_en); + /********************** * MACROS **********************/