Skip to content

Commit

Permalink
fix(imgbtn) add lv_imgbtn_set_state
Browse files Browse the repository at this point in the history
The normal lv_obj_add/clear_state couldn't refresh the image button properly.
  • Loading branch information
kisvegabor committed Sep 13, 2021
1 parent 953d9fb commit 26e15fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/widgets/extra/imgbtn.md
Expand Up @@ -34,6 +34,11 @@ The possible states are:
If you set sources only in `LV_IMGBTN_STATE_RELEASED`, these sources will be used in other states too.
If you set e.g. `LV_IMGBTN_STATE_PRESSED` they will be used in pressed state instead of the released images.


### States
Instead of the regular `lv_obj_add/clear_state()` functions the `lv_imgbtn_set_state(imgbtn, LV_IMGBTN_STATE_...)` functions should be used to manually set a state.


## Events
- `LV_EVENT_VALUE_CHANGED` Sent when the button is toggled.

Expand Down
17 changes: 17 additions & 0 deletions src/extra/widgets/imgbtn/lv_imgbtn.c
Expand Up @@ -90,6 +90,23 @@ void lv_imgbtn_set_src(lv_obj_t * obj, lv_imgbtn_state_t state, const void * src
refr_img(obj);
}

void lv_imgbtn_set_state(lv_obj_t * obj, lv_imgbtn_state_t state)
{
LV_ASSERT_OBJ(obj, MY_CLASS);

lv_state_t obj_state = LV_STATE_DEFAULT;
if(state == LV_IMGBTN_STATE_PRESSED || state == LV_IMGBTN_STATE_CHECKED_PRESSED) obj_state |= LV_STATE_PRESSED;
if(state == LV_IMGBTN_STATE_DISABLED || state == LV_IMGBTN_STATE_CHECKED_DISABLED) obj_state |= LV_STATE_DISABLED;
if(state == LV_IMGBTN_STATE_CHECKED_DISABLED || state == LV_IMGBTN_STATE_CHECKED_PRESSED || state == LV_IMGBTN_STATE_CHECKED_RELEASED) {
obj_state |= LV_STATE_CHECKED;
}

lv_obj_clear_state(obj, LV_STATE_CHECKED | LV_STATE_PRESSED | LV_STATE_DISABLED);
lv_obj_add_state(obj, obj_state);

refr_img(obj);
}

/*=====================
* Getter functions
*====================*/
Expand Down
7 changes: 7 additions & 0 deletions src/extra/widgets/imgbtn/lv_imgbtn.h
Expand Up @@ -78,6 +78,13 @@ void lv_imgbtn_set_src(lv_obj_t * imgbtn, lv_imgbtn_state_t state, const void *
const void * src_right);


/**
* Use this function instead of `lv_obj_add/clear_state` to set a state manually
* @param imgbtn pointer to an image button object
* @param state the new state
*/
void lv_imgbtn_set_state(lv_obj_t * imgbtn, lv_imgbtn_state_t state);

/*=====================
* Getter functions
*====================*/
Expand Down

0 comments on commit 26e15fa

Please sign in to comment.