diff --git a/docs/changes.md b/docs/changes.md index 20232230d..0a6aa27bf 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,7 +1,10 @@ # 最新动态 +2022/04/28 + * 完善 combo\_box 在非加载时设置options后,更新显示文本。 + 2022/04/27 - * 修复combo_box_ex初始化时出现文本全选的问题(感谢雨欣提供补丁)。 + * 修复combo\_box\_ex初始化时出现文本全选的问题(感谢雨欣提供补丁)。 2022/04/26 * 完善 stream serial 的文档(感谢智明提供补丁)。 diff --git a/src/widgets/combo_box.c b/src/widgets/combo_box.c index a4090f200..3317586ed 100644 --- a/src/widgets/combo_box.c +++ b/src/widgets/combo_box.c @@ -240,7 +240,12 @@ ret_t combo_box_parse_options(widget_t* widget, const char* str) { } ret_t combo_box_set_options(widget_t* widget, const char* options) { - return combo_box_parse_options(widget, options); + combo_box_t* combo_box = COMBO_BOX(widget); + ret_t ret = combo_box_parse_options(widget, options); + if (!widget->loading) { + ret = combo_box_sync_index_to_value(widget, 0, FALSE); + } + return ret; } static ret_t combo_box_text_to_index(widget_t* widget, const char* text) { @@ -276,7 +281,7 @@ static ret_t combo_box_set_prop(widget_t* widget, const char* name, const value_ combo_box_set_localize_options(widget, value_bool(v)); return RET_OK; } else if (tk_str_eq(name, WIDGET_PROP_OPTIONS)) { - combo_box_parse_options(widget, value_str(v)); + combo_box_set_options(widget, value_str(v)); return RET_OK; } else if (tk_str_eq(name, WIDGET_PROP_ITEM_HEIGHT)) { combo_box_set_item_height(widget, value_uint32(v)); diff --git a/tests/combo_box_test.cc b/tests/combo_box_test.cc index 0af130b41..4da864628 100644 --- a/tests/combo_box_test.cc +++ b/tests/combo_box_test.cc @@ -323,3 +323,18 @@ TEST(ComboBox, remove_option) { widget_destroy(w); } + +TEST(ComboBox, set_options) { + char text[100]; + widget_t* w = combo_box_create(NULL, 10, 20, 30, 40); + + combo_box_set_options(w, "1:red;2:green;3:blue"); + widget_get_text_utf8(w, text, sizeof(text)-1); + ASSERT_STREQ(text, "red"); + + combo_box_set_options(w, "aaa;bbb;ccc"); + widget_get_text_utf8(w, text, sizeof(text)-1); + ASSERT_STREQ(text, "aaa"); + + widget_destroy(w); +}