From 25232d92e7d2526dc2504b2fc0455cd2d17c6aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Costa?= Date: Mon, 6 Jan 2025 15:40:23 +0100 Subject: [PATCH] feat(dropdown): add lv_anim_enable_t parameter to lv_dropddown_set_selected (#7310) --- .../flex_layout/lv_demo_flex_layout_flex_loader.c | 4 ++-- demos/stress/lv_demo_stress.c | 2 +- docs/details/widgets/dropdown.rst | 2 +- env_support/pikascript/pika_lv_wegit.c | 6 +++--- src/others/observer/lv_observer.c | 2 +- .../calendar/lv_calendar_header_dropdown.c | 4 ++-- src/widgets/dropdown/lv_dropdown.c | 4 ++-- src/widgets/dropdown/lv_dropdown.h | 5 +++-- tests/src/test_cases/widgets/test_dropdown.c | 15 ++++++++------- 9 files changed, 23 insertions(+), 21 deletions(-) diff --git a/demos/flex_layout/lv_demo_flex_layout_flex_loader.c b/demos/flex_layout/lv_demo_flex_layout_flex_loader.c index 9493b250b..5a4dfc878 100644 --- a/demos/flex_layout/lv_demo_flex_layout_flex_loader.c +++ b/demos/flex_layout/lv_demo_flex_layout_flex_loader.c @@ -33,7 +33,7 @@ for (i = 0; i < ARRAY_SIZE(flex_align_map); i++) { \ if (flex_align == flex_align_map[i]) { \ lv_dropdown_set_selected(ui->ctrl_pad.tab.align.ddlist_align_##item, \ - i); \ + i, LV_ANIM_OFF); \ break; \ } \ } \ @@ -114,7 +114,7 @@ void flex_loader_obj_update(lv_obj_t * obj, view_t * ui) uint32_t i; for(i = 0; i < ARRAY_SIZE(flex_flow_map); i++) { if(flex_flow == flex_flow_map[i]) { - lv_dropdown_set_selected(ui->ctrl_pad.tab.flex.ddlist_flow, i); + lv_dropdown_set_selected(ui->ctrl_pad.tab.flex.ddlist_flow, i, LV_ANIM_OFF); break; } } diff --git a/demos/stress/lv_demo_stress.c b/demos/stress/lv_demo_stress.c index c4b3437de..ef864f932 100644 --- a/demos/stress/lv_demo_stress.c +++ b/demos/stress/lv_demo_stress.c @@ -209,7 +209,7 @@ static void obj_test_task_cb(lv_timer_t * tmr) obj = lv_dropdown_create(main_page); lv_dropdown_set_options(obj, "Zero\nOne\nTwo\nThree\nFour\nFive\nSix\nSeven\nEight"); lv_dropdown_open(obj); - lv_dropdown_set_selected(obj, 2); + lv_dropdown_set_selected(obj, 2, LV_ANIM_ON); auto_delete(obj, LV_DEMO_STRESS_TIME_STEP * 3 + 11); break; diff --git a/docs/details/widgets/dropdown.rst b/docs/details/widgets/dropdown.rst index 9e22874d2..ce7da652b 100644 --- a/docs/details/widgets/dropdown.rst +++ b/docs/details/widgets/dropdown.rst @@ -87,7 +87,7 @@ the options string's contents must remain available for the life of the Drop-Dow List and :cpp:func:`lv_dropdown_add_option` cannot be used. You can select an option programmatically with -:cpp:expr:`lv_dropdown_set_selected(dropdown, id)`, where ``id`` is the index of +:cpp:expr:`lv_dropdown_set_selected(dropdown, id, LV_ANIM_ON/LV_ANIM_OFF)`, where ``id`` is the index of the target option. Get selected option diff --git a/env_support/pikascript/pika_lv_wegit.c b/env_support/pikascript/pika_lv_wegit.c index 122347cf5..df3fd614b 100644 --- a/env_support/pikascript/pika_lv_wegit.c +++ b/env_support/pikascript/pika_lv_wegit.c @@ -264,9 +264,9 @@ void pika_lvgl_dropdown_set_dir(PikaObj *self, int dir){ lv_obj_t* lv_obj = obj_getPtr(self, "lv_obj"); lv_dropdown_set_dir(lv_obj, dir); } -void pika_lvgl_dropdown_set_selected(PikaObj *self, int sel_opt){ +void pika_lvgl_dropdown_set_selected(PikaObj *self, int sel_opt, int anim){ lv_obj_t* lv_obj = obj_getPtr(self, "lv_obj"); - lv_dropdown_set_selected(lv_obj, sel_opt); + lv_dropdown_set_selected(lv_obj, sel_opt, anim); } void pika_lvgl_dropdown_set_selected_highlight(PikaObj *self, int en){ lv_obj_t* lv_obj = obj_getPtr(self, "lv_obj"); @@ -361,4 +361,4 @@ void pika_lvgl_textarea_set_one_line(PikaObj* self, int en) { lv_obj_t* lv_obj = obj_getPtr(self, "lv_obj"); lv_textarea_set_one_line(lv_obj, en); } -#endif \ No newline at end of file +#endif diff --git a/src/others/observer/lv_observer.c b/src/others/observer/lv_observer.c index 911bb76e7..247f22be8 100644 --- a/src/others/observer/lv_observer.c +++ b/src/others/observer/lv_observer.c @@ -757,7 +757,7 @@ static void dropdown_value_changed_event_cb(lv_event_t * e) static void dropdown_value_observer_cb(lv_observer_t * observer, lv_subject_t * subject) { - lv_dropdown_set_selected(observer->target, subject->value.num); + lv_dropdown_set_selected(observer->target, subject->value.num, LV_ANIM_OFF); } #endif /*LV_USE_DROPDOWN*/ diff --git a/src/widgets/calendar/lv_calendar_header_dropdown.c b/src/widgets/calendar/lv_calendar_header_dropdown.c index dfa35632f..92fb90a82 100644 --- a/src/widgets/calendar/lv_calendar_header_dropdown.c +++ b/src/widgets/calendar/lv_calendar_header_dropdown.c @@ -174,10 +174,10 @@ static void value_changed_event_cb(lv_event_t * e) const uint32_t year = (year_p[0] - '0') * 1000 + (year_p[1] - '0') * 100 + (year_p[2] - '0') * 10 + (year_p[3] - '0'); - lv_dropdown_set_selected(year_dd, year - cur_date->year); + lv_dropdown_set_selected(year_dd, year - cur_date->year, LV_ANIM_OFF); lv_obj_t * month_dd = lv_obj_get_child(header, 1); - lv_dropdown_set_selected(month_dd, cur_date->month - 1); + lv_dropdown_set_selected(month_dd, cur_date->month - 1, LV_ANIM_OFF); } #endif /*LV_USE_CALENDAR_HEADER_ARROW*/ diff --git a/src/widgets/dropdown/lv_dropdown.c b/src/widgets/dropdown/lv_dropdown.c index 24c416cdb..3cdd8a629 100644 --- a/src/widgets/dropdown/lv_dropdown.c +++ b/src/widgets/dropdown/lv_dropdown.c @@ -336,7 +336,7 @@ void lv_dropdown_clear_options(lv_obj_t * obj) if(dropdown->list) lv_obj_invalidate(dropdown->list); } -void lv_dropdown_set_selected(lv_obj_t * obj, uint32_t sel_opt) +void lv_dropdown_set_selected(lv_obj_t * obj, uint32_t sel_opt, lv_anim_enable_t anim) { LV_ASSERT_OBJ(obj, MY_CLASS); @@ -347,7 +347,7 @@ void lv_dropdown_set_selected(lv_obj_t * obj, uint32_t sel_opt) dropdown->sel_opt_id_orig = dropdown->sel_opt_id; if(dropdown->list) { - position_to_selected(obj, LV_ANIM_OFF); + position_to_selected(obj, anim); } lv_obj_invalidate(obj); diff --git a/src/widgets/dropdown/lv_dropdown.h b/src/widgets/dropdown/lv_dropdown.h index c9f55be12..67f2b09f1 100644 --- a/src/widgets/dropdown/lv_dropdown.h +++ b/src/widgets/dropdown/lv_dropdown.h @@ -36,8 +36,8 @@ enum { LV_PROPERTY_ID(DROPDOWN, TEXT, LV_PROPERTY_TYPE_TEXT, 0), LV_PROPERTY_ID(DROPDOWN, OPTIONS, LV_PROPERTY_TYPE_TEXT, 1), LV_PROPERTY_ID(DROPDOWN, OPTION_COUNT, LV_PROPERTY_TYPE_INT, 2), - LV_PROPERTY_ID(DROPDOWN, SELECTED, LV_PROPERTY_TYPE_INT, 3), // LV_PROPERTY_ID(DROPDOWN, SELECTED_STR, LV_PROPERTY_TYPE_TEXT, 4), + LV_PROPERTY_ID2(DROPDOWN, SELECTED, LV_PROPERTY_TYPE_INT, LV_PROPERTY_TYPE_BOOL, 3), LV_PROPERTY_ID(DROPDOWN, DIR, LV_PROPERTY_TYPE_INT, 5), LV_PROPERTY_ID(DROPDOWN, SYMBOL, LV_PROPERTY_TYPE_TEXT, 6), LV_PROPERTY_ID(DROPDOWN, SELECTED_HIGHLIGHT, LV_PROPERTY_TYPE_INT, 7), @@ -108,8 +108,9 @@ void lv_dropdown_clear_options(lv_obj_t * obj); * Set the selected option * @param obj pointer to drop-down list object * @param sel_opt id of the selected option (0 ... number of option - 1); + * @param anim LV_ANIM_ON: set the selected option with an animation; LV_ANIM_OFF: set the option immediately */ -void lv_dropdown_set_selected(lv_obj_t * obj, uint32_t sel_opt); +void lv_dropdown_set_selected(lv_obj_t * obj, uint32_t sel_opt, lv_anim_enable_t anim); /** * Set the direction of the a drop-down list diff --git a/tests/src/test_cases/widgets/test_dropdown.c b/tests/src/test_cases/widgets/test_dropdown.c index 6265d2745..2dedafebd 100644 --- a/tests/src/test_cases/widgets/test_dropdown.c +++ b/tests/src/test_cases/widgets/test_dropdown.c @@ -118,7 +118,7 @@ void test_dropdown_set_options(void) void test_dropdown_select(void) { lv_obj_t * dd1 = lv_dropdown_create(lv_screen_active()); - lv_dropdown_set_selected(dd1, 2); + lv_dropdown_set_selected(dd1, 2, LV_ANIM_OFF); TEST_ASSERT_EQUAL(2, lv_dropdown_get_selected(dd1)); @@ -132,7 +132,7 @@ void test_dropdown_select(void) TEST_ASSERT_EQUAL_STRING("Opt", buf); /*Out of range*/ - lv_dropdown_set_selected(dd1, 3); + lv_dropdown_set_selected(dd1, 3, LV_ANIM_OFF); TEST_ASSERT_EQUAL(2, lv_dropdown_get_selected(dd1)); } @@ -324,12 +324,12 @@ void test_dropdown_render_1(void) lv_obj_t * dd1 = lv_dropdown_create(lv_screen_active()); lv_obj_set_pos(dd1, 10, 10); - lv_dropdown_set_selected(dd1, 1); + lv_dropdown_set_selected(dd1, 1, LV_ANIM_OFF); lv_obj_t * dd2 = lv_dropdown_create(lv_screen_active()); lv_obj_set_pos(dd2, 200, 10); lv_obj_set_width(dd2, 200); - lv_dropdown_set_selected(dd2, 2); + lv_dropdown_set_selected(dd2, 2, LV_ANIM_OFF); lv_dropdown_open(dd2); TEST_ASSERT_TRUE(lv_dropdown_get_selected_highlight(dd2)); lv_dropdown_set_selected_highlight(dd2, false); @@ -343,7 +343,7 @@ void test_dropdown_render_1(void) lv_dropdown_set_text(dd3, "A text"); TEST_ASSERT_EQUAL_STRING("A text", lv_dropdown_get_text(dd3)); - lv_dropdown_set_selected(dd3, 2); + lv_dropdown_set_selected(dd3, 2, LV_ANIM_OFF); TEST_ASSERT_EQUAL(LV_DIR_BOTTOM, lv_dropdown_get_dir(dd3)); lv_dropdown_set_dir(dd3, LV_DIR_LEFT); @@ -358,7 +358,7 @@ void test_dropdown_render_1(void) lv_obj_t * list = lv_dropdown_get_list(dd3); lv_obj_set_style_text_line_space(list, 5, 0); lv_obj_set_style_bg_color(list, lv_color_hex3(0xf00), LV_PART_SELECTED | LV_STATE_CHECKED); - lv_dropdown_set_selected(dd3, 3); + lv_dropdown_set_selected(dd3, 3, LV_ANIM_OFF); TEST_ASSERT_EQUAL_SCREENSHOT("widgets/dropdown_1.png"); } @@ -475,7 +475,8 @@ void test_dropdown_properties(void) TEST_ASSERT_EQUAL(2, lv_obj_get_property(obj, LV_PROPERTY_DROPDOWN_OPTION_COUNT).num); prop.id = LV_PROPERTY_DROPDOWN_SELECTED; - prop.num = 1; + prop.arg1.num = 1; + prop.arg2.enable = LV_ANIM_OFF; TEST_ASSERT_TRUE(lv_obj_set_property(obj, &prop) == LV_RESULT_OK); TEST_ASSERT_EQUAL_INT(1, lv_dropdown_get_selected(obj)); TEST_ASSERT_EQUAL_INT(1, lv_obj_get_property(obj, LV_PROPERTY_DROPDOWN_SELECTED).num);