Skip to content

Commit bbecdfd

Browse files
committed
improve combo_box
1 parent fd7e4a8 commit bbecdfd

4 files changed

Lines changed: 37 additions & 1 deletion

File tree

docs/changes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# 最新动态
22

3+
2026/9/8
4+
* 修复combo_box设置options属性会导致mvvm把错误的值设置到模型的问题(感谢福明提供补丁)
5+
36
2026/9/7
47
* 修正combo_box在没有options时获取value返回值不正确的问题(感谢福明提供补丁)
58

src/widgets/combo_box.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ static ret_t combo_box_on_destroy(widget_t* widget) {
9090
idle_remove(combo_box->init_popup_button_idle_id);
9191
}
9292

93+
if (combo_box->index_to_value_idle_id != TK_INVALID_ID) {
94+
idle_remove(combo_box->index_to_value_idle_id);
95+
}
96+
9397
str_reset(&(combo_box->text));
9498
combo_box_reset_options(widget);
9599
TKMEM_FREE(combo_box->open_window);
@@ -311,14 +315,38 @@ ret_t combo_box_parse_options(widget_t* widget, const char* str) {
311315
return RET_OK;
312316
}
313317

318+
static ret_t combo_box_on_idle_sync_index_to_value(const idle_info_t* idle) {
319+
widget_t* widget = NULL;
320+
combo_box_t* combo_box = NULL;
321+
return_value_if_fail(idle != NULL, RET_BAD_PARAMS);
322+
323+
widget = WIDGET(idle->ctx);
324+
combo_box = COMBO_BOX(widget);
325+
ENSURE(combo_box);
326+
327+
combo_box_sync_index_to_value(widget, combo_box->selected_index, FALSE);
328+
combo_box->index_to_value_idle_id = TK_INVALID_ID;
329+
330+
return RET_OK;
331+
}
332+
333+
static ret_t combo_box_idle_sync_index_to_value(widget_t* widget) {
334+
combo_box_t* combo_box = COMBO_BOX(widget);
335+
ENSURE(combo_box);
336+
if (!combo_box->index_to_value_idle_id) {
337+
combo_box->index_to_value_idle_id = idle_add(combo_box_on_idle_sync_index_to_value, widget);
338+
}
339+
return RET_OK;
340+
}
341+
314342
ret_t combo_box_set_options(widget_t* widget, const char* options) {
315343
ret_t ret = RET_FAIL;
316344
combo_box_t* combo_box = COMBO_BOX(widget);
317345
return_value_if_fail(combo_box != NULL, RET_BAD_PARAMS);
318346

319347
ret = combo_box_parse_options(widget, options);
320348
if (!widget->loading) {
321-
ret = combo_box_sync_index_to_value(widget, combo_box->selected_index, FALSE);
349+
combo_box_idle_sync_index_to_value(widget);
322350
}
323351

324352
return ret;

src/widgets/combo_box.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ typedef struct _combo_box_t {
232232
bool_t is_button_click;
233233
event_func_t on_item_click;
234234
void* on_item_click_ctx;
235+
uint32_t index_to_value_idle_id;
235236
uint32_t init_popup_button_idle_id;
236237
} combo_box_t;
237238

tests/combo_box_test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,14 @@ TEST(ComboBox, set_options) {
447447
widget_t* w = combo_box_create(NULL, 10, 20, 30, 40);
448448

449449
combo_box_set_options(w, "1:red;2:green;3:blue");
450+
idle_dispatch();
451+
450452
widget_get_text_utf8(w, text, sizeof(text) - 1);
451453
ASSERT_STREQ(text, "red");
452454

453455
combo_box_set_options(w, "aaa;bbb;ccc");
456+
idle_dispatch();
457+
454458
widget_get_text_utf8(w, text, sizeof(text) - 1);
455459
ASSERT_STREQ(text, "aaa");
456460

0 commit comments

Comments
 (0)