From fbf0303b2d92f53f5aeca5181112e2f72a642f1e Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Mon, 30 Jul 2018 07:42:36 +0200 Subject: [PATCH] lv_list: add lv_list_set/get_btn_selected() --- lv_objx/lv_list.c | 151 ++++++++++++++++++++++++---------------------- lv_objx/lv_list.h | 25 ++++++++ 2 files changed, 103 insertions(+), 73 deletions(-) diff --git a/lv_objx/lv_list.c b/lv_objx/lv_list.c index 8ab000725..49123ed7c 100644 --- a/lv_objx/lv_list.c +++ b/lv_objx/lv_list.c @@ -91,6 +91,7 @@ lv_obj_t * lv_list_create(lv_obj_t * par, const lv_obj_t * copy) ext->styles_btn[LV_BTN_STATE_TGL_PR] = &lv_style_btn_tgl_pr; ext->styles_btn[LV_BTN_STATE_INA] = &lv_style_btn_ina; ext->anim_time = LV_LIST_FOCUS_TIME; + ext->selected_btn = NULL; lv_obj_set_signal_func(new_list, lv_list_signal); @@ -228,6 +229,36 @@ lv_obj_t * lv_list_add(lv_obj_t * list, const void * img_src, const char * txt, * Setter functions *====================*/ +#if USE_LV_GROUP + +/** + * Make a button selected + * @param list pointer to a list object + * @param btn pointer to a button to select + */ +void lv_list_set_btn_selected(lv_obj_t * list, lv_obj_t * btn) +{ + lv_list_ext_t * ext = lv_obj_get_ext_attr(list); + + if(ext->selected_btn) { + lv_btn_state_t s = lv_btn_get_state(ext->selected_btn); + if(s == LV_BTN_STATE_PR) lv_btn_set_state(ext->selected_btn, LV_BTN_STATE_REL); + else if(s == LV_BTN_STATE_TGL_PR) lv_btn_set_state(ext->selected_btn, LV_BTN_STATE_TGL_REL); + } + + ext->selected_btn = btn; + + if(ext->selected_btn) { + lv_btn_state_t s = lv_btn_get_state(ext->selected_btn); + if(s == LV_BTN_STATE_REL) lv_btn_set_state(ext->selected_btn, LV_BTN_STATE_PR); + else if(s == LV_BTN_STATE_TGL_REL) lv_btn_set_state(ext->selected_btn, LV_BTN_STATE_TGL_PR); + + lv_page_focus(list, ext->selected_btn, ext->anim_time); + } +} + +#endif + /** * Set scroll animation duration on 'list_up()' 'list_down()' 'list_focus()' * @param list pointer to a list object @@ -359,6 +390,20 @@ lv_obj_t * lv_list_get_btn_img(const lv_obj_t * btn) #endif } +#if USE_LV_GROUP +/** + * Get the currently selected button + * @param list pointer to a list object + * @return pointer to the selected button + */ +lv_obj_t * lv_list_get_btn_selected(const lv_obj_t * list) +{ + lv_list_ext_t * ext = lv_obj_get_ext_attr(list); + return ext->selected_btn; +} + +#endif + /** * Get scroll animation duration * @param list pointer to a list object @@ -531,6 +576,8 @@ static lv_res_t lv_list_signal(lv_obj_t * list, lv_signal_t sign, void * param) res = ancestor_page_signal(list, sign, param); if(res != LV_RES_OK) return res; + lv_list_ext_t * ext = lv_obj_get_ext_attr(list); + if(sign == LV_SIGNAL_CORD_CHG) { /*Be sure the width of the buttons are correct*/ lv_coord_t w = lv_obj_get_width(list); @@ -541,85 +588,44 @@ static lv_res_t lv_list_signal(lv_obj_t * list, lv_signal_t sign, void * param) /*Because of the possible change of horizontal and vertical padding refresh buttons width */ refr_btn_width(list); } else if(sign == LV_SIGNAL_FOCUS) { -#if USE_LV_BTN /*Mark the last clicked button (if any) as selected because it triggered the focus*/ if(last_clicked_btn) { - lv_btn_set_state(last_clicked_btn, LV_BTN_STATE_PR); + lv_list_set_btn_selected(list, last_clicked_btn); } else { /*Get the first button and mark it as selected*/ - lv_obj_t * btn = get_prev_btn(list, NULL); - if(btn) lv_btn_set_state(btn, LV_BTN_STATE_PR); - + lv_list_set_btn_selected(list, get_prev_btn(list, NULL)); } -#else - /*Get the first button and mark it as selected*/ - lv_obj_t * btn = get_prev_btn(list, NULL); - if(btn) lv_btn_set_state(btn, LV_BTN_STATE_PR); -#endif - } else if(sign == LV_SIGNAL_DEFOCUS) { - /*Get the 'pressed' button*/ - lv_obj_t * btn = NULL; - btn = get_next_btn(list, btn); - while(btn != NULL) { - if(lv_btn_get_state(btn) == LV_BTN_STATE_PR) break; - btn = get_next_btn(list, btn); - } - - if(btn != NULL) { - lv_btn_set_state(btn, LV_BTN_STATE_REL); - } - last_clicked_btn = NULL; /*button click will set if comes before focus*/ + /*De-select the selected btn*/ + lv_list_set_btn_selected(list, NULL); + last_clicked_btn = NULL; /*button click will be set if click happens before focus*/ + ext->selected_btn = NULL; } else if(sign == LV_SIGNAL_GET_EDITABLE) { bool * editable = (bool *)param; *editable = true; } else if(sign == LV_SIGNAL_CONTROLL) { char c = *((char *)param); if(c == LV_GROUP_KEY_RIGHT || c == LV_GROUP_KEY_DOWN) { - /*Get the last pressed button*/ - lv_obj_t * btn = NULL; - lv_list_ext_t * ext = lv_obj_get_ext_attr(list); - btn = get_prev_btn(list, btn); - while(btn != NULL) { - if(lv_btn_get_state(btn) == LV_BTN_STATE_PR) break; - btn = get_prev_btn(list, btn); - } - - /*If there is a valid "pressed" button the make the previous "pressed"*/ - if(btn != NULL) { - lv_obj_t * btn_prev = get_prev_btn(list, btn); - if(btn_prev != NULL) { - lv_btn_set_state(btn, LV_BTN_STATE_REL); - lv_btn_set_state(btn_prev, LV_BTN_STATE_PR); - lv_page_focus(list, btn_prev, ext->anim_time); - } - } - } else if(c == LV_GROUP_KEY_LEFT || c == LV_GROUP_KEY_UP) { - /*Get the last pressed button*/ - lv_obj_t * btn = NULL; - lv_list_ext_t * ext = lv_obj_get_ext_attr(list); - btn = get_next_btn(list, btn); - while(btn != NULL) { - if(lv_btn_get_state(btn) == LV_BTN_STATE_PR) break; - btn = get_next_btn(list, btn); - } - - /*If there is a valid "pressed" button the make the next "pressed"*/ - if(btn != NULL) { - lv_obj_t * btn_next = get_next_btn(list, btn); - if(btn_next != NULL) { - lv_btn_set_state(btn, LV_BTN_STATE_REL); - lv_btn_set_state(btn_next, LV_BTN_STATE_PR); - lv_page_focus(list, btn_next, ext->anim_time); - } - } - /*If there is no "pressed" button the make the first "pressed"*/ + /*If there is a valid selected button the make the previous selected*/ + if(ext->selected_btn) { + lv_obj_t * btn_prev = get_prev_btn(list, ext->selected_btn); + if(btn_prev) lv_list_set_btn_selected(list, btn_prev); + } + /*If there is no selected button the make the first selected*/ else { - btn = get_next_btn(list, NULL); - if(btn) { /*If there are no buttons on the list hen there is no first button*/ - lv_btn_set_state(btn, LV_BTN_STATE_PR); - lv_page_focus(list, btn, ext->anim_time); - } + lv_obj_t * btn = get_next_btn(list, NULL); + if(btn) lv_list_set_btn_selected(list, get_next_btn(list, NULL)); /*If there are no buttons on the list then there is no first button*/ + } + } else if(c == LV_GROUP_KEY_LEFT || c == LV_GROUP_KEY_UP) { + /*If there is a valid selected button the make the next selected*/ + if(ext->selected_btn != NULL) { + lv_obj_t * btn_next = get_next_btn(list, ext->selected_btn); + if(btn_next) lv_list_set_btn_selected(list, btn_next); + } + /*If there is no selected button the make the first selected*/ + else { + lv_obj_t * btn = get_next_btn(list, NULL); + if(btn) lv_list_set_btn_selected(list, get_next_btn(list, NULL)); } } else if(c == LV_GROUP_KEY_ENTER) { /*Get the 'pressed' button*/ @@ -668,7 +674,7 @@ static lv_res_t lv_list_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * para lv_obj_t * list = lv_obj_get_parent(lv_obj_get_parent(btn)); lv_group_t * g = lv_obj_get_group(list); if(lv_group_get_focused(g) == list && lv_indev_is_dragging(lv_indev_get_act()) == false) { - /* Is the list is focused then be sure only button being released + /* Is the list is focused then be sure only the button being released * has a pressed state to indicate the selected state on the list*/ lv_obj_t * btn_i = get_next_btn(list, NULL); while(btn_i) { @@ -678,13 +684,12 @@ static lv_res_t lv_list_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * para btn_i = get_next_btn(list, btn_i); } - /*Make the released button "selected" by setting pressed state for it*/ - lv_btn_state_t s = lv_btn_get_state(btn); - if(s == LV_BTN_STATE_REL) lv_btn_set_state(btn, LV_BTN_STATE_PR); - else if(s == LV_BTN_STATE_TGL_REL) lv_btn_set_state(btn, LV_BTN_STATE_TGL_PR); - } - /* If `click_focus == 1` then LV_SIGNAL_FOCUS need to know which button triggered the focus + } + /*Make the released button "selected"*/ + lv_list_set_btn_selected(list, btn); + + /* If `click_focus == 1` then LV_SIGNAL_FOCUS need to know which button triggered the focus * to mark it as selected (pressed state)*/ last_clicked_btn = btn; diff --git a/lv_objx/lv_list.h b/lv_objx/lv_list.h index 1acd9df06..c3a52082f 100644 --- a/lv_objx/lv_list.h +++ b/lv_objx/lv_list.h @@ -56,6 +56,9 @@ typedef struct uint16_t anim_time; /*Scroll animation time*/ lv_style_t *styles_btn[LV_BTN_STATE_NUM]; /*Styles of the list element buttons*/ lv_style_t *style_img; /*Style of the list element images on buttons*/ +#if USE_LV_GROUP + lv_obj_t * selected_btn; +#endif } lv_list_ext_t; typedef enum { @@ -104,6 +107,15 @@ lv_obj_t * lv_list_add(lv_obj_t * list, const void * img_src, const char * txt, /*===================== * Setter functions *====================*/ +#if USE_LV_GROUP + +/** + * Make a button selected + * @param list pointer to a list object + * @param btn pointer to a button to select + */ +void lv_list_set_btn_selected(lv_obj_t * list, lv_obj_t * btn); +#endif /** * Set scroll animation duration on 'list_up()' 'list_down()' 'list_focus()' @@ -154,6 +166,18 @@ lv_obj_t * lv_list_get_btn_label(const lv_obj_t * btn); */ lv_obj_t * lv_list_get_btn_img(const lv_obj_t * btn); + + +#if USE_LV_GROUP +/** + * Get the currently selected button + * @param list pointer to a list object + * @return pointer to the selected button + */ +lv_obj_t * lv_list_get_btn_selected(const lv_obj_t * list); +#endif + + /** * Get scroll animation duration * @param list pointer to a list object @@ -161,6 +185,7 @@ lv_obj_t * lv_list_get_btn_img(const lv_obj_t * btn); */ uint16_t lv_list_get_anim_time(const lv_obj_t *list); + /** * Get the scroll bar mode of a list * @param list pointer to a list object