Skip to content

Commit

Permalink
chore(example) minor improvements on lv_example_list_2
Browse files Browse the repository at this point in the history
  • Loading branch information
kisvegabor committed Aug 18, 2021
1 parent 493ace3 commit bb6d6b7
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions examples/widgets/list/lv_example_list_2.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "../../lv_examples.h"
#if LV_USE_LIST && LV_BUILD_EXAMPLES

static lv_obj_t* list1;
static lv_obj_t* list2;
static lv_obj_t* buttonColumn;

static lv_obj_t* currentButton = NULL;

Expand All @@ -12,7 +12,7 @@ static void event_handler(lv_event_t* e)
lv_obj_t* obj = lv_event_get_target(e);
if (code == LV_EVENT_CLICKED)
{
LV_LOG_USER("Clicked: %s", lv_list_get_btn_text(list2, obj));
LV_LOG_USER("Clicked: %s", lv_list_get_btn_text(list1, obj));

if (currentButton == obj)
{
Expand Down Expand Up @@ -42,68 +42,67 @@ static void event_handler(lv_event_t* e)
static void event_handler_mu(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
// lv_obj_t* obj = lv_event_get_target(e);
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
if (currentButton == NULL) return;
lv_obj_t* parent = lv_obj_get_parent(currentButton);
uint_fast32_t i = lv_obj_get_child_id(currentButton);
if (i > 0)
{
if (i > 0) {
lv_obj_swap(parent->spec_attr->children[i], parent->spec_attr->children[i - 1]);
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
}

static void event_handler_dn(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
// lv_obj_t* obj = lv_event_get_target(e);
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
if (currentButton == NULL) return;
lv_obj_t* parent = lv_obj_get_parent(currentButton);
uint_fast32_t i = lv_obj_get_child_id(currentButton);
if (i < lv_obj_get_child_cnt(parent) - 1)
{
if (i < lv_obj_get_child_cnt(parent) - 1) {
lv_obj_swap(parent->spec_attr->children[i], parent->spec_attr->children[i + 1]);
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
}


void lv_example_list_2(void)
{
/*Create a list*/
list2 = lv_list_create(lv_scr_act());
lv_obj_set_size(list2, lv_obj_get_width(lv_scr_act()) - 90, lv_obj_get_height(lv_scr_act()) - 10);
lv_obj_set_align(list2, LV_ALIGN_TOP_LEFT);
lv_obj_set_pos(list2, 5, 5);
lv_obj_set_flex_flow(list2, LV_FLEX_FLOW_COLUMN);
list1 = lv_list_create(lv_scr_act());
lv_obj_set_size(list1, lv_pct(70), lv_pct(100));
lv_obj_set_style_pad_row(list1, 5, 0);

/*Add buttons to the list*/
lv_obj_t* btn;
int i;
for (i = 0; i < 30; i++) {
btn = lv_btn_create(list1);
lv_obj_set_width(btn, lv_pct(50));
lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);

for (int i = 0; i < 10; i++)
{
char szBuf[100];
lv_snprintf(szBuf, sizeof(szBuf), " Item %d ", i);
btn = lv_btn_create(list2);
lv_obj_t* lab = lv_label_create(btn);
lv_label_set_text(lab, szBuf);
lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
lv_label_set_text_fmt(lab, "Item %d", i);
}

buttonColumn = lv_obj_create(lv_scr_act());
lv_obj_set_size(buttonColumn, 80, lv_obj_get_height(list2));
lv_obj_set_align(buttonColumn, LV_ALIGN_TOP_RIGHT);
lv_obj_set_pos(buttonColumn, -5, 5);
lv_obj_set_flex_flow(buttonColumn, LV_FLEX_FLOW_COLUMN);
/*Select the first button by default*/
currentButton = lv_obj_get_child(list1, 0);
lv_obj_add_state(currentButton, LV_STATE_CHECKED);

/*Create a second list with up and down buttons*/
list2 = lv_list_create(lv_scr_act());
lv_obj_set_size(list2, lv_pct(30), lv_pct(100));
lv_obj_align(list2, LV_ALIGN_TOP_RIGHT, 0, 0);
lv_obj_set_flex_flow(list2, LV_FLEX_FLOW_COLUMN);

btn = lv_list_add_btn(buttonColumn, LV_SYMBOL_UP, NULL);
btn = lv_list_add_btn(list2, LV_SYMBOL_UP, "Up");
lv_obj_add_event_cb(btn, event_handler_mu, LV_EVENT_ALL, NULL);
btn = lv_list_add_btn(buttonColumn, LV_SYMBOL_DOWN, NULL);

btn = lv_list_add_btn(list2, LV_SYMBOL_DOWN, "Down");
lv_obj_add_event_cb(btn, event_handler_dn, LV_EVENT_ALL, NULL);
}

#endif
#endif

0 comments on commit bb6d6b7

Please sign in to comment.