Skip to content

Commit

Permalink
improve widget_sync_state_to_children
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Feb 28, 2025
1 parent 0e85a30 commit 7bebd81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
3 changes: 3 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 最新动态

2025/02/28
* 完善 sync state to children(感谢兆坤提供补丁)

2025/02/27
* 增加event_source_fd对象支持fd为-1和设置fd的情况(感谢智明提供补丁)

Expand Down
54 changes: 26 additions & 28 deletions src/base/widget.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,21 +901,32 @@ static ret_t widget_sync_children_state_on_visit(widget_sync_children_state_on_v
return RET_OK;
}

static ret_t widget_sync_state_to_children(widget_t* widget, const char* state_for_style) {
static inline void widget_sync_state_to_children_impl(widget_t* widget,
const char* state_for_style) {
widget_sync_children_state_on_visit_ctx_t ctx = {
.widget = widget,
.state_for_style = state_for_style,
};
return widget_foreach(widget, (tk_visit_t)widget_sync_children_state_on_visit, &ctx);
widget_foreach(widget, (tk_visit_t)widget_sync_children_state_on_visit, &ctx);
}

static inline void widget_sync_state_to_children(widget_t* widget, const char* state_for_style) {
if (widget->last_state_for_style == NULL) {
widget->last_state_for_style = (const char*)(widget->state);
}
if (!tk_str_eq(state_for_style, widget->last_state_for_style)) {
widget_sync_state_to_children_impl(widget, state_for_style);
}
widget->last_state_for_style = state_for_style;
}

ret_t widget_set_state(widget_t* widget, const char* state) {
return_value_if_fail(widget != NULL && state != NULL, RET_BAD_PARAMS);

if (RET_OK == widget_set_state_impl(widget, state)) {
if (widget->sync_state_to_children) {
widget_sync_state_to_children(
widget, widget_get_prop_str(widget, WIDGET_PROP_STATE_FOR_STYLE, widget->state));
/* 在获取 state_for_style 属性后,会调用 widget_sync_state_to_children 函数更新 */
widget_get_prop_str(widget, WIDGET_PROP_STATE_FOR_STYLE, widget->state);
}
}

Expand All @@ -928,8 +939,8 @@ ret_t widget_set_sync_state_to_children(widget_t* widget, bool_t sync_state_to_c
widget->sync_state_to_children = sync_state_to_children;

if (widget->sync_state_to_children) {
return widget_sync_state_to_children(
widget, widget_get_prop_str(widget, WIDGET_PROP_STATE_FOR_STYLE, widget->state));
/* 在获取 state_for_style 属性后,会调用 widget_sync_state_to_children 函数更新 */
widget_get_prop_str(widget, WIDGET_PROP_STATE_FOR_STYLE, widget->state);
}

return RET_OK;
Expand All @@ -943,10 +954,12 @@ ret_t widget_set_state_from_parent_sync(widget_t* widget, bool_t state_from_pare
return RET_OK;
}

static const char* widget_get_state_for_style_impl(widget_t* widget, bool_t active,
bool_t checked) {
const char* widget_get_state_for_style(widget_t* widget, bool_t active, bool_t checked) {
const char* state = WIDGET_STATE_NORMAL;
widget_t* iter = widget;
const char* state = (const char*)(widget->state);
return_value_if_fail(widget != NULL && widget->vt != NULL, state);

state = (const char*)(widget->state);

while (iter != NULL) {
if (!iter->enable) {
Expand Down Expand Up @@ -994,25 +1007,6 @@ static const char* widget_get_state_for_style_impl(widget_t* widget, bool_t acti
return state;
}

const char* widget_get_state_for_style(widget_t* widget, bool_t active, bool_t checked) {
const char* ret = WIDGET_STATE_NORMAL;
return_value_if_fail(widget != NULL, ret);

if (widget->last_state_for_style == NULL) {
widget->last_state_for_style = (const char*)(widget->state);
}

ret = widget_get_state_for_style_impl(widget, active, checked);

if (widget->sync_state_to_children && !tk_str_eq(ret, widget->last_state_for_style)) {
widget_sync_state_to_children(widget, ret);
}

widget->last_state_for_style = ret;

return ret;
}

ret_t widget_set_opacity(widget_t* widget, uint8_t opacity) {
return_value_if_fail(widget != NULL && widget->vt != NULL, RET_BAD_PARAMS);

Expand Down Expand Up @@ -2507,6 +2501,10 @@ ret_t widget_get_prop(widget_t* widget, const char* name, value_t* v) {
}
}

if (widget->sync_state_to_children && tk_str_eq(name, WIDGET_PROP_STATE_FOR_STYLE)) {
widget_sync_state_to_children(widget, value_str(v));
}

return ret;
}

Expand Down

0 comments on commit 7bebd81

Please sign in to comment.