From fb84ca75df7c95708e7137a6a67b8fcddba01667 Mon Sep 17 00:00:00 2001 From: Liu Date: Sun, 12 Nov 2023 22:45:17 +0800 Subject: [PATCH] feat(ui): improve widget update process and related data structures --- lib/ui-widgets/src/scrollbar.c | 8 +- lib/ui-widgets/src/text.c | 71 ++-- lib/ui-widgets/src/textinput.c | 138 ++++---- lib/ui/include/ui/base.h | 56 ++-- lib/ui/include/ui/style.h | 2 - lib/ui/include/ui/types.h | 541 ++++++++++++++---------------- lib/ui/src/ui_diff.c | 19 +- lib/ui/src/ui_renderer.c | 33 +- lib/ui/src/ui_tree.c | 32 +- lib/ui/src/ui_updater.c | 328 ++++++------------ lib/ui/src/ui_widget.c | 8 +- lib/ui/src/ui_widget_classes.c | 4 +- lib/ui/src/ui_widget_hash.c | 3 - lib/ui/src/ui_widget_helper.c | 4 +- lib/ui/src/ui_widget_observer.c | 2 +- lib/ui/src/ui_widget_observer.h | 2 +- lib/ui/src/ui_widget_prototype.c | 12 +- lib/ui/src/ui_widget_status.c | 4 +- lib/ui/src/ui_widget_style.c | 25 +- tests/cases/test_widget_opacity.c | 4 +- 20 files changed, 583 insertions(+), 713 deletions(-) diff --git a/lib/ui-widgets/src/scrollbar.c b/lib/ui-widgets/src/scrollbar.c index ddb75c7d..a5fb46c1 100644 --- a/lib/ui-widgets/src/scrollbar.c +++ b/lib/ui-widgets/src/scrollbar.c @@ -281,7 +281,7 @@ static void ui_scrollbar_thumb_on_mousemove(ui_widget_t *thumb, ui_event_t *e, ui_widget_emit_event(target, e, &layer_pos); } scrollbar->pos = layer_pos; - ui_widget_update_style(target); + ui_widget_request_update_style(target); ui_widget_move(thumb, x, y); } @@ -373,7 +373,7 @@ static void ui_scrollbar_update_size(ui_widget_t *w) CSS_UNIT_PERCENT); } ui_scrollbar_set_position(w, scrollbar->pos); - ui_widget_update_style(thumb); + ui_widget_request_update_style(thumb); if (n < 100.f) { ui_widget_show(w); if (scrollbar->direction == UI_SCROLLBAR_HORIZONTAL) { @@ -687,8 +687,8 @@ float ui_scrollbar_set_position(ui_widget_t *w, float pos) ui_widget_emit_event(content, e, &new_pos); } scrollbar->pos = pos; - ui_widget_update_style(thumb); - ui_widget_update_style(content); + ui_widget_request_update_style(thumb); + ui_widget_request_update_style(content); return pos; } diff --git a/lib/ui-widgets/src/text.c b/lib/ui-widgets/src/text.c index a7c6738f..053a391c 100644 --- a/lib/ui-widgets/src/text.c +++ b/lib/ui-widgets/src/text.c @@ -91,15 +91,13 @@ static void ui_text_on_parse_text(ui_widget_t *w, const char *text) ui_text_set_content(w, text); } -static void ui_text_on_update(ui_widget_t *w) +static void ui_text_on_update_content(ui_widget_t *w) { - float scale = ui_metrics.scale; - - ui_rect_t rect; - ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); - list_t rects; list_node_t *node; + ui_rect_t rect; + ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); + float scale = ui_metrics.scale; list_create(&rects); pd_text_update(txt->layer, &rects); @@ -108,7 +106,7 @@ static void ui_text_on_update(ui_widget_t *w) ui_widget_mark_dirty_rect(w, &rect, UI_BOX_TYPE_CONTENT_BOX); } pd_rects_clear(&rects); - ui_widget_add_task(w, UI_TASK_REFLOW); + ui_widget_request_reflow(w); } static void ui_text_on_update_style(ui_widget_t *w) @@ -116,6 +114,7 @@ static void ui_text_on_update_style(ui_widget_t *w) ui_text_style_t style; pd_text_style_t text_style; ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); + bool content_changed; ui_text_style_init(&style); ui_compute_text_style(&style, &w->computed_style); @@ -123,6 +122,7 @@ static void ui_text_on_update_style(ui_widget_t *w) ui_text_style_destroy(&style); return; } + content_changed = (!txt->style.content != !style.content); convert_font_style_to_text_style(&style, &text_style); pd_text_set_align(txt->layer, style.text_align); pd_text_set_line_height(txt->layer, style.line_height); @@ -132,16 +132,35 @@ static void ui_text_on_update_style(ui_widget_t *w) style.word_break == CSS_WORD_BREAK_BREAK_ALL ? PD_WORD_BREAK_BREAK_ALL : PD_WORD_BREAK_NORMAL); - pd_text_set_style(txt->layer, &text_style); - if (style.content) { + if (content_changed) { ui_text_set_content_w(w, style.content); - } else if (txt->style.content) { - ui_text_set_content_w(w, NULL); } + pd_text_set_style(txt->layer, &text_style); ui_text_style_destroy(&txt->style); pd_text_style_destroy(&text_style); + txt->task.update_content = true; txt->style = style; - ui_text_on_update(w); +} + +static void ui_text_on_update(ui_widget_t *w, ui_task_type_t task) +{ + ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); + + switch (task) { + case UI_TASK_UPDATE_STYLE: + ui_text_on_update_style(w); + break; + case UI_TASK_AFTER_UPDATE: + if (txt->task.update_content) { + pd_text_write(txt->layer, txt->task.content, NULL); + ui_text_on_update_content(w); + free(txt->task.content); + txt->task.content = NULL; + txt->task.update_content = FALSE; + } + default: + break; + } } static void ui_text_on_init(ui_widget_t *w) @@ -226,7 +245,6 @@ static void ui_text_on_resize(ui_widget_t *w, float width, float height) ui_rect_t rect; ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); - ; list_t rects; list_node_t *node; @@ -274,6 +292,9 @@ int ui_text_set_content_w(ui_widget_t *w, const wchar_t *text) ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); wchar_t *newtext = wcsdup2(text); + if (ui_widget_has_class(w, "fui-icon-regular")) { + _DEBUG_MSG("set content\n"); + } if (!newtext) { return -ENOMEM; } @@ -303,7 +324,7 @@ int ui_text_set_content_w(ui_widget_t *w, const wchar_t *text) } txt->task.update_content = TRUE; txt->task.content = newtext; - ui_widget_add_task(w, UI_TASK_USER); + ui_widget_request_update(w); return 0; } @@ -327,7 +348,7 @@ void ui_text_set_multiline(ui_widget_t *w, bool enable) ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); pd_text_set_multiline(txt->layer, enable); - ui_widget_add_task(w, UI_TASK_USER); + ui_widget_request_update(w); } static void text_on_font_face_load(ui_widget_t *w, ui_event_t *e, void *arg) @@ -338,26 +359,11 @@ static void text_on_font_face_load(ui_widget_t *w, ui_event_t *e, void *arg) for (list_each(node, &ui_text.list)) { txt = node->data; if (txt->widget->state != UI_WIDGET_STATE_DELETED) { - ui_widget_refresh_style(txt->widget); + ui_widget_request_refresh_style(txt->widget); } } } -static void ui_text_on_run_rask(ui_widget_t *w, int task) -{ - ui_text_t *txt; - - txt = ui_widget_get_data(w, ui_text.prototype); - if (txt->task.update_content) { - pd_text_write(txt->layer, txt->task.content, NULL); - ui_text_on_update(w); - free(txt->task.content); - txt->task.content = NULL; - txt->task.update_content = FALSE; - ui_widget_add_task(w->parent, UI_TASK_REFLOW); - } -} - void ui_register_text(void) { ui_text.prototype = ui_create_widget_prototype("text", NULL); @@ -366,10 +372,9 @@ void ui_register_text(void) ui_text.prototype->destroy = ui_text_on_destroy; ui_text.prototype->autosize = ui_text_on_auto_size; ui_text.prototype->resize = ui_text_on_resize; - ui_text.prototype->update = ui_text_on_update_style; + ui_text.prototype->update = ui_text_on_update; ui_text.prototype->settext = ui_text_on_parse_text; ui_text.prototype->setattr = ui_text_on_parse_attr; - ui_text.prototype->runtask = ui_text_on_run_rask; list_create(&ui_text.list); ui_on_event("font_face_load", text_on_font_face_load, NULL); } diff --git a/lib/ui-widgets/src/textinput.c b/lib/ui-widgets/src/textinput.c index 4a76b701..bb5047d7 100644 --- a/lib/ui-widgets/src/textinput.c +++ b/lib/ui-widgets/src/textinput.c @@ -55,7 +55,7 @@ typedef struct ui_textinput_t { ui_widget_t *caret; /**< 文本插入符 */ bool is_read_only; /**< 是否只读 */ bool is_multiline_mode; /**< 是否为多行模式 */ - bool is_placeholder_shown; /**< 是否已经显示占位符 */ + bool placeholder_visible; /**< 是否已经显示占位符 */ wchar_t *allow_input_char; /**< 允许输入的字符 */ wchar_t password_char; /**< 屏蔽符的副本 */ size_t text_block_size; /**< 块大小 */ @@ -129,7 +129,7 @@ static void ui_textinput_update_caret(ui_widget_t *widget) float scale = ui_metrics.scale; float x, y, caret_x = 0, caret_y = 0; - if (!edit->is_placeholder_shown) { + if (!edit->placeholder_visible) { pd_pos_t pos; if (pd_text_get_insert_pixel_position(edit->layer, &pos) != 0) { return; @@ -170,7 +170,7 @@ static void ui_textinput_update_caret(ui_widget_t *widget) offset_y = y_iround((y - caret_y) * scale); if (pd_text_set_offset(edit->layer, offset_x, offset_y)) { edit->tasks[TASK_UPDATE] = TRUE; - ui_widget_add_task(widget, UI_TASK_USER); + ui_widget_request_update(widget); } x += widget->computed_style.padding_left; y += widget->computed_style.padding_top; @@ -186,7 +186,7 @@ static void ui_textinput_update_caret(ui_widget_t *widget) void ui_textinput_move_caret(ui_widget_t *widget, int row, int col) { ui_textinput_t *edit = ui_widget_get_data(widget, ui_textinput_proto); - if (edit->is_placeholder_shown) { + if (edit->placeholder_visible) { row = col = 0; } pd_text_set_insert_position(edit->layer, row, col); @@ -276,13 +276,14 @@ static int ui_textinput_add_textblock(ui_widget_t *widget, const wchar_t *wtext, /* 添加文本块至缓冲区 */ list_append(&edit->text_blocks, block); } - edit->tasks[TASK_SET_TEXT] = TRUE; - ui_widget_add_task(widget, UI_TASK_USER); + edit->tasks[TASK_SET_TEXT] = true; + ui_widget_request_update(widget); return 0; } /** 更新文本框内的字体位图 */ -static void TextEdit_ProcTextBlock(ui_widget_t *widget, textblock_t *txtblk) +static void textinput_process_textblock(ui_widget_t *widget, + textblock_t *txtblk) { list_t *tags; ui_textinput_t *edit; @@ -327,7 +328,7 @@ static void TextEdit_ProcTextBlock(ui_widget_t *widget, textblock_t *txtblk) } /** 更新文本框的文本图层 */ -static void TextEdit_UpdateTextLayer(ui_widget_t *w) +static void textinput_update_textlayer(ui_widget_t *w) { float scale; list_t rects; @@ -355,48 +356,44 @@ static void TextEdit_UpdateTextLayer(ui_widget_t *w) pd_rects_clear(&rects); } -static void ui_textinput_on_task(ui_widget_t *widget, int task) +static void ui_textinput_update(ui_widget_t *w) { + ui_textinput_t *edit = ui_widget_get_data(w, ui_textinput_proto); + bool placeholder_visible = edit->layer_source->length == 0; + + if (placeholder_visible) { + edit->layer = edit->layer_placeholder; + } else if (edit->password_char) { + edit->layer = edit->layer_mask; + } else { + edit->layer = edit->layer_source; + } + textinput_update_textlayer(w); + if (edit->placeholder_visible != placeholder_visible) { + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_PADDING_BOX); + } + edit->placeholder_visible = placeholder_visible; + ui_textinput_update_caret(w); +} + +static void ui_textinput_update_text(ui_widget_t *widget) +{ + list_t blocks; + list_node_t *node; + ui_event_t ev; ui_textinput_t *edit = ui_widget_get_data(widget, ui_textinput_proto); - if (edit->tasks[TASK_SET_TEXT]) { - list_t blocks; - list_node_t *node; - ui_event_t ev; - - list_create(&blocks); - thread_mutex_lock(&edit->mutex); - list_concat(&blocks, &edit->text_blocks); - thread_mutex_unlock(&edit->mutex); - for (list_each(node, &blocks)) { - TextEdit_ProcTextBlock(widget, node->data); - } - list_destroy(&blocks, - (list_item_destructor_t)textblock_destroy); - ui_event_init(&ev, "change"); - ui_widget_emit_event(widget, ev, NULL); - edit->tasks[TASK_SET_TEXT] = FALSE; - edit->tasks[TASK_UPDATE] = TRUE; - } - if (edit->tasks[TASK_UPDATE]) { - bool is_shown; - is_shown = edit->layer_source->length == 0; - if (is_shown) { - edit->layer = edit->layer_placeholder; - } else if (edit->password_char) { - edit->layer = edit->layer_mask; - } else { - edit->layer = edit->layer_source; - } - TextEdit_UpdateTextLayer(widget); - if (edit->is_placeholder_shown != is_shown) { - ui_widget_mark_dirty_rect(widget, NULL, - UI_BOX_TYPE_PADDING_BOX); - } - edit->is_placeholder_shown = is_shown; - edit->tasks[TASK_UPDATE] = FALSE; - ui_textinput_update_caret(widget); + list_create(&blocks); + thread_mutex_lock(&edit->mutex); + list_concat(&blocks, &edit->text_blocks); + thread_mutex_unlock(&edit->mutex); + for (list_each(node, &blocks)) { + textinput_process_textblock(widget, node->data); } + list_destroy(&blocks, + (list_item_destructor_t)textblock_destroy); + ui_event_init(&ev, "change"); + ui_widget_emit_event(widget, ev, NULL); } static void ui_textinput_on_resize(ui_widget_t *w, float width, float height) @@ -511,7 +508,7 @@ void ui_textinput_clear_text(ui_widget_t *widget) pd_text_empty(edit->layer_source); pd_style_tags_clear(&edit->text_tags); edit->tasks[TASK_UPDATE] = TRUE; - ui_widget_add_task(widget, UI_TASK_USER); + ui_widget_request_update(widget); thread_mutex_unlock(&edit->mutex); ui_widget_mark_dirty_rect(widget, NULL, UI_BOX_TYPE_PADDING_BOX); } @@ -559,8 +556,8 @@ void ui_textinput_set_password_char(ui_widget_t *w, wchar_t ch) ui_textinput_t *edit = ui_widget_get_data(w, ui_textinput_proto); edit->password_char = ch; - edit->tasks[TASK_UPDATE] = TRUE; - ui_widget_add_task(w, UI_TASK_USER); + edit->tasks[TASK_UPDATE] = true; + ui_widget_request_update(w); pd_text_empty(edit->layer_mask); if (!edit->password_char) { edit->layer = edit->layer_source; @@ -593,7 +590,7 @@ int ui_textinput_set_placeholder_w(ui_widget_t *w, const wchar_t *wstr) thread_mutex_lock(&edit->mutex); pd_text_empty(edit->layer_placeholder); thread_mutex_unlock(&edit->mutex); - if (edit->is_placeholder_shown) { + if (edit->placeholder_visible) { ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_PADDING_BOX); } return ui_textinput_add_textblock(w, wstr, TEXTBLOCK_ACTION_INSERT, @@ -663,8 +660,8 @@ static void ui_textinput_on_press_backspace_key(ui_widget_t *widget, int n_ch) pd_text_backspace(edit->layer_mask, n_ch); } ui_textcaret_refresh(edit->caret); - edit->tasks[TASK_UPDATE] = TRUE; - ui_widget_add_task(widget, UI_TASK_USER); + edit->tasks[TASK_UPDATE] = true; + ui_widget_request_update(widget); thread_mutex_unlock(&edit->mutex); ui_event_init(&ev, "change"); ui_widget_emit_event(widget, ev, NULL); @@ -682,8 +679,8 @@ static void ui_textinput_on_press_delete_key(ui_widget_t *widget, int n_ch) pd_text_delete(edit->layer_mask, n_ch); } ui_textcaret_refresh(edit->caret); - edit->tasks[TASK_UPDATE] = TRUE; - ui_widget_add_task(widget, UI_TASK_USER); + edit->tasks[TASK_UPDATE] = true; + ui_widget_request_update(widget); thread_mutex_unlock(&edit->mutex); ui_event_init(&ev, "change"); ui_widget_emit_event(widget, ev, NULL); @@ -840,7 +837,7 @@ static void ui_textinput_on_mousemove(ui_widget_t *w, ui_event_t *e, void *arg) float offset_x, offset_y; ui_textinput_t *edit = ui_widget_get_data(w, ui_textinput_proto); - if (edit->is_placeholder_shown) { + if (edit->placeholder_visible) { ui_textinput_update_caret(w); return; } @@ -895,7 +892,7 @@ static void ui_textinput_on_init(ui_widget_t *w) edit->is_read_only = FALSE; edit->password_char = 0; edit->allow_input_char = NULL; - edit->is_placeholder_shown = FALSE; + edit->placeholder_visible = FALSE; edit->is_multiline_mode = FALSE; edit->layer_mask = pd_text_create(); edit->layer_source = pd_text_create(); @@ -992,8 +989,30 @@ static void ui_textinput_on_update_style(ui_widget_t *w) ui_text_style_destroy(&edit->style); pd_text_style_destroy(&text_style); edit->style = style; - edit->tasks[TASK_UPDATE] = TRUE; - ui_widget_add_task(w, UI_TASK_USER); + edit->tasks[TASK_UPDATE] = true; +} + +static void ui_textinput_on_update(ui_widget_t *w, ui_task_type_t task) +{ + ui_textinput_t *edit = ui_widget_get_data(w, ui_textinput_proto); + + switch (task) { + case UI_TASK_UPDATE_STYLE: + ui_textinput_on_update_style(w); + break; + case UI_TASK_AFTER_UPDATE: + if (edit->tasks[TASK_SET_TEXT]) { + ui_textinput_update_text(w); + edit->tasks[TASK_SET_TEXT] = false; + edit->tasks[TASK_UPDATE] = true; + } + if (edit->tasks[TASK_UPDATE]) { + ui_textinput_update(w); + edit->tasks[TASK_UPDATE] = false; + } + default: + break; + } } void ui_register_textinput(void) @@ -1006,7 +1025,6 @@ void ui_register_textinput(void) ui_textinput_proto->setattr = ui_textinput_on_set_attr; ui_textinput_proto->autosize = ui_textinput_on_auto_size; ui_textinput_proto->resize = ui_textinput_on_resize; - ui_textinput_proto->runtask = ui_textinput_on_task; - ui_textinput_proto->update = ui_textinput_on_update_style; + ui_textinput_proto->update = ui_textinput_on_update; ui_load_css_string(ui_textinput_css, __FILE__); } diff --git a/lib/ui/include/ui/base.h b/lib/ui/include/ui/base.h index 4ca7c424..6ee8943b 100644 --- a/lib/ui/include/ui/base.h +++ b/lib/ui/include/ui/base.h @@ -18,7 +18,7 @@ LIBUI_PUBLIC void ui_widget_destroy(ui_widget_t *w); LIBUI_PUBLIC void ui_widget_set_title(ui_widget_t *w, const wchar_t *title); LIBUI_PUBLIC void ui_widget_set_text(ui_widget_t *w, const char *text); LIBUI_PUBLIC void ui_widget_get_offset(ui_widget_t *w, ui_widget_t *parent, - float *offset_x, float *offset_y); + float *offset_x, float *offset_y); LIBUI_PUBLIC bool ui_widget_in_viewport(ui_widget_t *w); // Root @@ -35,20 +35,20 @@ LIBUI_PUBLIC int ui_widget_set_id(ui_widget_t *w, const char *idstr); LIBUI_PUBLIC ui_widget_extra_data_t *ui_create_extra_data(ui_widget_t *widget); -LIBUI_INLINE ui_widget_extra_data_t *ui_widget_use_extra_data(ui_widget_t *widget) +LIBUI_INLINE ui_widget_extra_data_t *ui_widget_use_extra_data( + ui_widget_t *widget) { - return widget->extra ? widget->extra : ui_create_extra_data(widget); + return widget->extra ? widget->extra : ui_create_extra_data(widget); } // Attributes LIBUI_PUBLIC int ui_widget_set_attr_ex(ui_widget_t *w, const char *name, - void *value, int value_type, - void (*value_destructor)(void *)); + void *value, int value_type, + void (*value_destructor)(void *)); LIBUI_PUBLIC int ui_widget_set_attr(ui_widget_t *w, const char *name, - const char *value); -LIBUI_PUBLIC const char *ui_widget_get_attr(ui_widget_t *w, - const char *name); + const char *value); +LIBUI_PUBLIC const char *ui_widget_get_attr(ui_widget_t *w, const char *name); // Classes @@ -61,7 +61,7 @@ LIBUI_PUBLIC int ui_widget_remove_class(ui_widget_t *w, const char *class_name); LIBUI_PUBLIC int ui_widget_add_status(ui_widget_t *w, const char *status_name); LIBUI_PUBLIC bool ui_widget_has_status(ui_widget_t *w, const char *status_name); LIBUI_PUBLIC int ui_widget_remove_status(ui_widget_t *w, - const char *status_name); + const char *status_name); LIBUI_PUBLIC void ui_widget_update_status(ui_widget_t *widget); LIBUI_PUBLIC void ui_widget_set_disabled(ui_widget_t *w, bool disabled); @@ -77,8 +77,7 @@ LIBUI_PUBLIC ui_widget_t *ui_widget_prev(ui_widget_t *w); LIBUI_PUBLIC ui_widget_t *ui_widget_next(ui_widget_t *w); LIBUI_PUBLIC ui_widget_t *ui_widget_get_child(ui_widget_t *w, size_t index); LIBUI_PUBLIC size_t ui_widget_each(ui_widget_t *w, - ui_widget_callback_t callback, - void *arg); + ui_widget_callback_t callback, void *arg); LIBUI_PUBLIC ui_widget_t *ui_widget_at(ui_widget_t *widget, int ix, int iy); LIBUI_PUBLIC void ui_print_tree(ui_widget_t *w); @@ -90,7 +89,7 @@ LIBUI_PUBLIC void ui_widget_resize(ui_widget_t *w, float width, float height); LIBUI_PUBLIC void ui_widget_show(ui_widget_t *w); LIBUI_PUBLIC void ui_widget_hide(ui_widget_t *w); LIBUI_PUBLIC ui_widget_t *ui_widget_get_closest(ui_widget_t *w, - const char *type); + const char *type); LIBUI_PUBLIC dict_t *ui_widget_collect_references(ui_widget_t *w); // Layout @@ -101,7 +100,7 @@ LIBUI_PUBLIC dict_t *ui_widget_collect_references(ui_widget_t *w); * @param rule 父级组件所使用的布局规则 */ LIBUI_PUBLIC void ui_widget_prepare_reflow(ui_widget_t *w, - ui_layout_rule_t rule); + ui_layout_rule_t rule); LIBUI_PUBLIC void ui_widget_reset_size(ui_widget_t *w); @@ -113,7 +112,7 @@ LIBUI_PUBLIC void ui_widget_reflow(ui_widget_t *w); LIBUI_PUBLIC void ui_widget_expose_dirty_rect(ui_widget_t *w); LIBUI_PUBLIC bool ui_widget_mark_dirty_rect(ui_widget_t *w, ui_rect_t *in_rect, - ui_box_type_t box_type); + ui_box_type_t box_type); LIBUI_PUBLIC size_t ui_widget_get_dirty_rects(ui_widget_t *w, list_t *rects); LIBUI_PUBLIC size_t ui_widget_render(ui_widget_t *w, pd_context_t *paint); @@ -124,19 +123,32 @@ LIBUI_PUBLIC size_t ui_update(void); LIBUI_PUBLIC void ui_refresh_style(void); LIBUI_PUBLIC void ui_widget_set_rules(ui_widget_t *w, - const ui_widget_rules_t *rules); -LIBUI_PUBLIC void ui_widget_add_task_for_children(ui_widget_t *widget, - ui_task_type_t task); -LIBUI_PUBLIC void ui_widget_add_task(ui_widget_t *widget, int task); + const ui_widget_rules_t *rules); +LIBUI_PUBLIC void ui_widget_request_refresh_children(ui_widget_t *widget); +LIBUI_PUBLIC void ui_widget_request_update(ui_widget_t *w); -LIBUI_INLINE void ui_widget_refresh_style(ui_widget_t *w) +LIBUI_INLINE void ui_widget_request_reflow(ui_widget_t *w) { - ui_widget_add_task(w, UI_TASK_REFRESH_STYLE); + w->update.should_reflow = true; + ui_widget_request_update(w); } -LIBUI_INLINE void ui_widget_update_style(ui_widget_t *w) +LIBUI_INLINE void ui_widget_request_refresh_style(ui_widget_t *w) { - ui_widget_add_task(w, UI_TASK_UPDATE_STYLE); + w->update.should_refresh_style = true; + ui_widget_request_update(w); +} + +LIBUI_INLINE void ui_widget_request_update_style(ui_widget_t *w) +{ + w->update.should_update_style = true; + ui_widget_request_update(w); +} + +LIBUI_INLINE bool ui_widget_has_update(ui_widget_t *w) +{ + return w->update.should_update_self || w->update.should_refresh_style || + w->update.should_update_style; } LIBUI_END_DECLS diff --git a/lib/ui/include/ui/style.h b/lib/ui/include/ui/style.h index 65404a5d..74517e65 100644 --- a/lib/ui/include/ui/style.h +++ b/lib/ui/include/ui/style.h @@ -16,7 +16,6 @@ LIBUI_PUBLIC int ui_widget_unset_style(ui_widget_t *w, int key); LIBUI_PUBLIC css_selector_node_t *ui_widget_create_selector_node( ui_widget_t *w); LIBUI_PUBLIC css_selector_t *ui_widget_create_selector(ui_widget_t *w); -LIBUI_PUBLIC void ui_widget_print_stylesheet(ui_widget_t *w); LIBUI_PUBLIC void ui_widget_update_children_style(ui_widget_t *w); LIBUI_PUBLIC void ui_widget_refresh_children_style(ui_widget_t *w); LIBUI_PUBLIC int ui_widget_set_style_string(ui_widget_t *w, @@ -33,7 +32,6 @@ LIBUI_PUBLIC void ui_widget_set_style_numeric_value(ui_widget_t *w, int key, css_numeric_value_t value); LIBUI_PUBLIC void ui_widget_compute_style(ui_widget_t *w); LIBUI_PUBLIC void ui_widget_force_update_style(ui_widget_t *w); -LIBUI_PUBLIC void ui_widget_force_refresh_style(ui_widget_t *w); LIBUI_END_DECLS diff --git a/lib/ui/include/ui/types.h b/lib/ui/include/ui/types.h index 5a887656..6953cf32 100644 --- a/lib/ui/include/ui/types.h +++ b/lib/ui/include/ui/types.h @@ -7,249 +7,236 @@ #include typedef struct ui_rect_t { - float x, y, width, height; + float x, y, width, height; } ui_rect_t; typedef struct ui_area_t { - float left, top, right, bottom; + float left, top, right, bottom; } ui_area_t; typedef css_metrics_t ui_metrics_t; typedef enum ui_density_Level_t { - UI_DENSITY_LEVEL_SMALL, - UI_DENSITY_LEVEL_NORMAL, - UI_DENSITY_LEVEL_LARGE, - UI_DENSITY_LEVEL_BIG + UI_DENSITY_LEVEL_SMALL, + UI_DENSITY_LEVEL_NORMAL, + UI_DENSITY_LEVEL_LARGE, + UI_DENSITY_LEVEL_BIG } ui_density_Level_t; typedef enum ui_sizing_rule_t { - UI_SIZING_RULE_NONE, - UI_SIZING_RULE_FIXED, - UI_SIZING_RULE_FILL, - UI_SIZING_RULE_PERCENT, - UI_SIZING_RULE_FIT_CONTENT + UI_SIZING_RULE_NONE, + UI_SIZING_RULE_FIXED, + UI_SIZING_RULE_FILL, + UI_SIZING_RULE_PERCENT, + UI_SIZING_RULE_FIT_CONTENT } ui_sizing_rule_t; typedef enum ui_layout_rule_t { - UI_LAYOUT_RULE_AUTO, - UI_LAYOUT_RULE_MAX_CONTENT, - UI_LAYOUT_RULE_FIXED_WIDTH = 0b11, - UI_LAYOUT_RULE_FIXED_HEIGHT = 0b101, - UI_LAYOUT_RULE_FIXED = 0b111 + UI_LAYOUT_RULE_AUTO, + UI_LAYOUT_RULE_MAX_CONTENT, + UI_LAYOUT_RULE_FIXED_WIDTH = 0b11, + UI_LAYOUT_RULE_FIXED_HEIGHT = 0b101, + UI_LAYOUT_RULE_FIXED = 0b111 } ui_layout_rule_t; typedef enum ui_widget_state_t { - UI_WIDGET_STATE_CREATED = 0, - UI_WIDGET_STATE_UPDATED, - UI_WIDGET_STATE_LAYOUTED, - UI_WIDGET_STATE_READY, - UI_WIDGET_STATE_NORMAL, - UI_WIDGET_STATE_DELETED, + UI_WIDGET_STATE_CREATED = 0, + UI_WIDGET_STATE_UPDATED, + UI_WIDGET_STATE_LAYOUTED, + UI_WIDGET_STATE_READY, + UI_WIDGET_STATE_NORMAL, + UI_WIDGET_STATE_DELETED, } ui_widget_state_t; typedef enum ui_box_type_t { - UI_BOX_TYPE_CONTENT_BOX, - UI_BOX_TYPE_PADDING_BOX, - UI_BOX_TYPE_BORDER_BOX, - UI_BOX_TYPE_GRAPH_BOX + UI_BOX_TYPE_CONTENT_BOX, + UI_BOX_TYPE_PADDING_BOX, + UI_BOX_TYPE_BORDER_BOX, + UI_BOX_TYPE_GRAPH_BOX } ui_box_type_t; typedef enum ui_dirty_rect_type_t { - UI_DIRTY_RECT_TYPE_NONE, - UI_DIRTY_RECT_TYPE_CUSTOM, - UI_DIRTY_RECT_TYPE_FULL, + UI_DIRTY_RECT_TYPE_NONE, + UI_DIRTY_RECT_TYPE_CUSTOM, + UI_DIRTY_RECT_TYPE_FULL, } ui_dirty_rect_type_t; typedef struct ui_border_style_t_ { - struct { - int style; - float width; - pd_color_t color; - } top, right, bottom, left; - float top_left_radius; - float top_right_radius; - float bottom_left_radius; - float bottom_right_radius; + struct { + int style; + float width; + pd_color_t color; + } top, right, bottom, left; + float top_left_radius; + float top_right_radius; + float bottom_left_radius; + float bottom_right_radius; } ui_border_style_t; typedef struct ui_widget_attribute_t_ { - char *name; - struct { - int type; - void (*destructor)(void *); - union { - char *string; - void *data; - }; - } value; + char *name; + struct { + int type; + void (*destructor)(void *); + union { + char *string; + void *data; + }; + } value; } ui_widget_attribute_t; typedef struct ui_widget_actual_style_t_ { - float x, y; - pd_rect_t canvas_box; - pd_rect_t border_box; - pd_rect_t padding_box; - pd_rect_t content_box; + float x, y; + pd_rect_t canvas_box; + pd_rect_t border_box; + pd_rect_t padding_box; + pd_rect_t content_box; } ui_widget_actual_style_t; typedef enum ui_task_type_t { - UI_TASK_REFRESH_STYLE, - UI_TASK_UPDATE_STYLE, - UI_TASK_REFLOW, - UI_TASK_USER, - UI_TASK_TOTAL_NUM + UI_TASK_BEFORE_UPDATE, + UI_TASK_REFRESH_STYLE, + UI_TASK_UPDATE_STYLE, + UI_TASK_AFTER_UPDATE, + UI_TASK_REFLOW, } ui_task_type_t; typedef struct ui_event_listener_t ui_event_listener_t; typedef struct ui_widget_t ui_widget_t; typedef list_t ui_widget_listeners_t; -typedef void (*ui_widget_callback_t)(ui_widget_t *, void*); +typedef void (*ui_widget_callback_t)(ui_widget_t *, void *); typedef void (*ui_widget_function_t)(ui_widget_t *); -typedef void (*ui_widget_task_handler_t)(ui_widget_t *, int); -typedef void (*ui_widget_size_getter_t)(ui_widget_t *, float *, float *, - ui_layout_rule_t); -typedef void (*ui_widget_size_setter_t)(ui_widget_t *, float, float); -typedef void (*ui_widget_attr_setter_t)(ui_widget_t *, const char *, - const char *); -typedef void (*ui_widget_text_setter_t)(ui_widget_t *, const char *); -typedef void (*ui_widget_painter_t)(ui_widget_t *, pd_context_t *, - ui_widget_actual_style_t *); + typedef struct ui_widget_prototype_t ui_widget_prototype_t; typedef struct ui_widget_rules_t { - /** - * Suspend update if the current widget is not visible or is - * completely covered by other widgets - */ - bool only_on_visible; + /** + * Suspend update if the current widget is not visible or is + * completely covered by other widgets + */ + bool only_on_visible; - /** - * First update the children in the visible area - * If your widget has a lot of children and you want to see the - * children who are currently seeing the priority update, we recommend - * enabling this rule. - */ - bool first_update_visible_children; + /** + * First update the children in the visible area + * If your widget has a lot of children and you want to see the + * children who are currently seeing the priority update, we recommend + * enabling this rule. + */ + bool first_update_visible_children; - /** - * Cache the stylesheets of children to improve the query speed of - * the stylesheet. - * If this rule is enabled, we recommend that you manually call - * ui_widget_generate_hash() to generate a hash value for the children - * of the widget. - */ - bool cache_children_style; + /** Refresh the style of all child widgets if the status has changed */ + bool ignore_status_change; - /** Refresh the style of all child widgets if the status has changed */ - bool ignore_status_change; + /** Refresh the style of all child widgets if the classes has changed */ + bool ignore_classes_change; - /** Refresh the style of all child widgets if the classes has changed */ - bool ignore_classes_change; + /** + * Maximum number of children updated at each update + * values: + * 0 - Update all children at once + * -1 - Automatically calculates the appropriate maximum number + * N - Custom maximum number + */ + int max_update_children_count; - /** - * Maximum number of children updated at each update - * values: - * 0 - Update all children at once - * -1 - Automatically calculates the appropriate maximum number - * N - Custom maximum number - */ - int max_update_children_count; + /** Limit the number of children rendered */ + unsigned max_render_children_count; - /** Limit the number of children rendered */ - unsigned max_render_children_count; - - /** A callback function on update progress */ - void (*on_update_progress)(ui_widget_t *, size_t); + /** A callback function on update progress */ + void (*on_update_progress)(ui_widget_t *, size_t); } ui_widget_rules_t; typedef struct ui_widget_update_t { - /** Should update for self? */ - bool for_self; - - /** Should update for children? */ - bool for_children; - - /** States of tasks */ - bool states[UI_TASK_TOTAL_NUM]; + bool should_update_self : 1; + bool should_refresh_style : 1; + bool should_update_style : 1; + bool should_update_children : 1; + bool should_reflow : 1; + ui_rect_t border_box_backup; + ui_rect_t canvas_box_backup; } ui_widget_update_t; +typedef struct ui_widget_rendering_t { + ui_rect_t dirty_rect; + ui_dirty_rect_type_t dirty_rect_type; + bool has_child_dirty_rect; +} ui_widget_rendering_t; + typedef struct ui_profile_t { - long time; - size_t update_count; - size_t refresh_count; - size_t layout_count; - size_t user_task_count; - size_t destroy_count; - size_t destroy_time; + long time; + size_t update_count; + size_t refresh_count; + size_t layout_count; + size_t user_task_count; + size_t destroy_count; + size_t destroy_time; } ui_profile_t; struct ui_widget_prototype_t { - char *name; - ui_widget_function_t init; - ui_widget_function_t refresh; - ui_widget_function_t destroy; - ui_widget_function_t update; - ui_widget_task_handler_t runtask; - ui_widget_attr_setter_t setattr; - ui_widget_text_setter_t settext; - ui_widget_size_getter_t autosize; - ui_widget_size_setter_t resize; - ui_widget_painter_t paint; - ui_widget_prototype_t *proto; + char *name; + ui_widget_function_t init; + ui_widget_function_t destroy; + void (*update)(ui_widget_t *, ui_task_type_t); + void (*setattr)(ui_widget_t *, const char *, const char *); + void (*settext)(ui_widget_t *, const char *); + void (*autosize)(ui_widget_t *, float *, float *, ui_layout_rule_t); + void (*resize)(ui_widget_t *, float, float); + void (*paint)(ui_widget_t *, pd_context_t *, + ui_widget_actual_style_t *); + ui_widget_prototype_t *proto; }; typedef struct ui_widget_data_entry_t { - void *data; - ui_widget_prototype_t *proto; + void *data; + ui_widget_prototype_t *proto; } ui_widget_data_entry_t; typedef struct ui_widget_data_t { - unsigned length; - ui_widget_data_entry_t *list; + unsigned length; + ui_widget_data_entry_t *list; } ui_widget_data_t; // Event begin typedef enum ui_event_type_t { - UI_EVENT_NONE, - UI_EVENT_LINK, - UI_EVENT_UNLINK, - UI_EVENT_READY, - UI_EVENT_DESTROY, - UI_EVENT_FOCUS, - UI_EVENT_BLUR, - UI_EVENT_AFTERLAYOUT, - UI_EVENT_KEYDOWN, - UI_EVENT_KEYUP, - UI_EVENT_KEYPRESS, - UI_EVENT_TEXTINPUT, + UI_EVENT_NONE, + UI_EVENT_LINK, + UI_EVENT_UNLINK, + UI_EVENT_READY, + UI_EVENT_DESTROY, + UI_EVENT_FOCUS, + UI_EVENT_BLUR, + UI_EVENT_AFTERLAYOUT, + UI_EVENT_KEYDOWN, + UI_EVENT_KEYUP, + UI_EVENT_KEYPRESS, + UI_EVENT_TEXTINPUT, - UI_EVENT_MOUSEOVER, - UI_EVENT_MOUSEMOVE, - UI_EVENT_MOUSEOUT, - UI_EVENT_MOUSEDOWN, - UI_EVENT_MOUSEUP, - UI_EVENT_WHEEL, - UI_EVENT_CLICK, - UI_EVENT_DBLCLICK, - UI_EVENT_TOUCH, - UI_EVENT_TOUCHDOWN, - UI_EVENT_TOUCHUP, - UI_EVENT_TOUCHMOVE, - UI_EVENT_PASTE, + UI_EVENT_MOUSEOVER, + UI_EVENT_MOUSEMOVE, + UI_EVENT_MOUSEOUT, + UI_EVENT_MOUSEDOWN, + UI_EVENT_MOUSEUP, + UI_EVENT_WHEEL, + UI_EVENT_CLICK, + UI_EVENT_DBLCLICK, + UI_EVENT_TOUCH, + UI_EVENT_TOUCHDOWN, + UI_EVENT_TOUCHUP, + UI_EVENT_TOUCHMOVE, + UI_EVENT_PASTE, - UI_EVENT_FONT_FACE_LOAD, - UI_EVENT_USER + UI_EVENT_FONT_FACE_LOAD, + UI_EVENT_USER } ui_event_type_t; typedef enum ui_mouse_button_code_t { - UI_MOUSE_BUTTON_MAIN, - UI_MOUSE_BUTTON_AUXILIARY, - UI_MOUSE_BUTTON_SECNODARY, - UI_MOUSE_BUTTON_FORTH_BUTTON, - UI_MOUSE_BUTTON_FIFTH_BUTTON + UI_MOUSE_BUTTON_MAIN, + UI_MOUSE_BUTTON_AUXILIARY, + UI_MOUSE_BUTTON_SECNODARY, + UI_MOUSE_BUTTON_FORTH_BUTTON, + UI_MOUSE_BUTTON_FIFTH_BUTTON } ui_mouse_button_code_t; #define UI_MOUSE_BUTTON_LEFT UI_MOUSE_BUTTON_MAIN @@ -261,59 +248,59 @@ typedef enum ui_mouse_button_code_t { * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent */ typedef struct ui_keyboard_event_t { - int code; - bool alt_key; - bool ctrl_key; - bool shift_key; - bool meta_key; - bool is_composing; + int code; + bool alt_key; + bool ctrl_key; + bool shift_key; + bool meta_key; + bool is_composing; } ui_keyboard_event_t; typedef struct ui_touch_point_t { - float x; - float y; - int id; - int state; - bool is_primary; + float x; + float y; + int id; + int state; + bool is_primary; } ui_touch_point_t; typedef struct ui_touch_event_t { - unsigned n_points; - ui_touch_point_t *points; + unsigned n_points; + ui_touch_point_t *points; } ui_touch_event_t; typedef struct ui_paint_event_t { - pd_rect_t rect; + pd_rect_t rect; } ui_paint_event_t; typedef struct ui_textinput_event_t { - wchar_t *text; - size_t length; + wchar_t *text; + size_t length; } ui_textinput_event_t; /** * @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent */ typedef struct ui_mouse_event_t { - float x; - float y; - int button; + float x; + float y; + int button; } ui_mouse_event_t; typedef enum ui_wheel_delta_mode_t { - UI_WHEEL_DELTA_PIXEL = 0, - UI_WHEEL_DELTA_LINE, - UI_WHEEL_DELTA_PAGE + UI_WHEEL_DELTA_PIXEL = 0, + UI_WHEEL_DELTA_LINE, + UI_WHEEL_DELTA_PAGE } ui_wheel_delta_mode_t; /** * @see https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent */ typedef struct ui_wheel_event_t { - int delta_x; - int delta_y; - int delta_z; - int delta_mode; + int delta_x; + int delta_y; + int delta_z; + int delta_mode; } ui_wheel_event_t; typedef struct ui_event_t ui_event_t; @@ -321,113 +308,107 @@ typedef void (*ui_event_handler_t)(ui_widget_t *, ui_event_t *, void *); typedef void (*ui_event_arg_destructor_t)(void *); struct ui_event_t { - uint32_t type; /**< 事件类型标识号 */ - void *data; /**< 附加数据 */ - ui_widget_t *target; /**< 触发事件的部件 */ - bool cancel_bubble; /**< 是否取消事件冒泡 */ - union { - ui_mouse_event_t mouse; - ui_wheel_event_t wheel; - ui_keyboard_event_t key; - ui_touch_event_t touch; - ui_textinput_event_t text; - }; + uint32_t type; /**< 事件类型标识号 */ + void *data; /**< 附加数据 */ + ui_widget_t *target; /**< 触发事件的部件 */ + bool cancel_bubble; /**< 是否取消事件冒泡 */ + union { + ui_mouse_event_t mouse; + ui_wheel_event_t wheel; + ui_keyboard_event_t key; + ui_touch_event_t touch; + ui_textinput_event_t text; + }; }; typedef struct ui_widget_extra_data_t { - ui_widget_listeners_t listeners; - list_t observer_connections; - ui_widget_rules_t rules; - size_t default_max_update_count; - size_t update_progress; - dict_t *style_cache; + ui_widget_listeners_t listeners; + list_t observer_connections; + ui_widget_rules_t rules; + size_t default_max_update_count; + size_t update_progress; } ui_widget_extra_data_t; struct ui_widget_t { - unsigned hash; - ui_widget_state_t state; + unsigned hash; + ui_widget_state_t state; - char *id; - char *type; - int tab_index; - strlist_t classes; - strlist_t status; - wchar_t *title; - dict_t *attributes; - bool disabled; - bool event_blocked; + char *id; + char *type; + int tab_index; + strlist_t classes; + strlist_t status; + wchar_t *title; + dict_t *attributes; + bool disabled; + bool event_blocked; - /** - * Coordinates calculated by the layout system - * The position of the rectangular boxes is calculated based on it - */ - float layout_x, layout_y; + /** + * Coordinates calculated by the layout system + * The position of the rectangular boxes is calculated based on it + */ + float layout_x, layout_y; - /** - * A box’s “ideal” size in a given axis when given infinite available - * space. See more: https://drafts.csswg.org/css-sizing-3/#max-content - */ - float max_content_width, max_content_height; + /** + * A box’s “ideal” size in a given axis when given infinite available + * space. See more: https://drafts.csswg.org/css-sizing-3/#max-content + */ + float max_content_width, max_content_height; - /** See more: - * https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model */ - ui_rect_t content_box; - ui_rect_t padding_box; - ui_rect_t border_box; - ui_rect_t canvas_box; - ui_rect_t outer_box; + /** See more: + * https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model */ + ui_rect_t content_box; + ui_rect_t padding_box; + ui_rect_t border_box; + ui_rect_t canvas_box; + ui_rect_t outer_box; - css_style_decl_t *custom_style; - const css_style_decl_t *matched_style; - css_computed_style_t specified_style; - css_computed_style_t computed_style; + css_style_decl_t *custom_style; + const css_style_decl_t *matched_style; + css_computed_style_t specified_style; + css_computed_style_t computed_style; - /** Some data bound to the prototype */ - ui_widget_data_t data; - ui_widget_extra_data_t *extra; + /** Some data bound to the prototype */ + ui_widget_data_t data; + ui_widget_extra_data_t *extra; - /** - * Prototype chain - * It is used to implement the inheritance of widgets, - * Just like prototype chain in JavaScript - */ - const ui_widget_prototype_t *proto; + /** + * Prototype chain + * It is used to implement the inheritance of widgets, + * Just like prototype chain in JavaScript + */ + const ui_widget_prototype_t *proto; - ui_widget_update_t update; + ui_widget_update_t update; + ui_widget_rendering_t rendering; - ui_rect_t dirty_rect; - ui_dirty_rect_type_t dirty_rect_type; - bool has_child_dirty_rect; - ui_rect_t border_box_backup; - ui_rect_t canvas_box_backup; + /** Parent widget */ + ui_widget_t *parent; - /** Parent widget */ - ui_widget_t *parent; + /** List of child widgets */ + list_t children; - /** List of child widgets */ - list_t children; + /** + * List of child widgets in descending order by z-index + * @see + *https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context + **/ + list_t stacking_context; - /** - * List of child widgets in descending order by z-index - * @see - *https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context - **/ - list_t stacking_context; + /** + * Position in the parent->children + * this == list_get(&this->parent->children, this.index) + */ + size_t index; - /** - * Position in the parent->children - * this == list_get(&this->parent->children, this.index) - */ - size_t index; + /** + * Node in the parent->children + * &this->node == list_get_node(&this->parent->children, this.index) + */ + list_node_t node; - /** - * Node in the parent->children - * &this->node == list_get_node(&this->parent->children, this.index) - */ - list_node_t node; - - /** Node in the parent->children_show */ - list_node_t node_show; + /** Node in the parent->children_show */ + list_node_t node_show; }; #endif diff --git a/lib/ui/src/ui_diff.c b/lib/ui/src/ui_diff.c index a7de1a57..f9be1505 100644 --- a/lib/ui/src/ui_diff.c +++ b/lib/ui/src/ui_diff.c @@ -27,13 +27,14 @@ void ui_style_diff_init(ui_style_diff_t *diff, ui_widget_t *w) if (!ui_widget_is_visible(w->parent)) { return; } - if (w->parent->dirty_rect_type == UI_DIRTY_RECT_TYPE_FULL) { - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; + if (w->parent->rendering.dirty_rect_type == + UI_DIRTY_RECT_TYPE_FULL) { + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; return; } } if (w->state < UI_WIDGET_STATE_LAYOUTED) { - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; } diff->should_add_dirty_rect = true; } @@ -58,7 +59,7 @@ void ui_style_diff_end(ui_style_diff_t *diff, ui_widget_t *w) IS_PROP_TYPE_CHANGED(justify_content) || IS_PROP_TYPE_CHANGED(align_content) || IS_PROP_TYPE_CHANGED(align_items)))) { - ui_widget_add_task(w, UI_TASK_REFLOW); + ui_widget_request_reflow(w); } if (w->parent && ui_widget_in_layout_flow(w)) { if (layout_changed || IS_LENGTH_PROP_CHANGED(margin_left) || @@ -69,7 +70,7 @@ void ui_style_diff_end(ui_style_diff_t *diff, ui_widget_t *w) (IS_PROP_TYPE_CHANGED(flex_grow) || IS_PROP_TYPE_CHANGED(flex_shrink) || IS_PROP_TYPE_CHANGED(flex_basis)))) { - ui_widget_add_task(w->parent, UI_TASK_REFLOW); + ui_widget_request_reflow(w->parent); } } if (ui_widget_is_visible(w) != diff->visible) { @@ -78,12 +79,12 @@ void ui_style_diff_end(ui_style_diff_t *diff, ui_widget_t *w) record = ui_mutation_record_create( w, UI_MUTATION_RECORD_TYPE_PROPERTIES); record->property_name = strdup2("visible"); - ui_widget_add_mutation_recrod(w, record); + ui_widget_add_mutation_record(w, record); ui_mutation_record_destroy(record); } if (diff->should_add_dirty_rect) { - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; - w->dirty_rect = diff->canvas_box; + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; + w->rendering.dirty_rect = diff->canvas_box; ui_widget_expose_dirty_rect(w); } return; @@ -120,7 +121,7 @@ void ui_style_diff_end(ui_style_diff_t *diff, ui_widget_t *w) IS_PROP_VALUE_CHANGED(background_position_y) || IS_PROP_VALUE_CHANGED(background_width) || IS_PROP_VALUE_CHANGED(background_height)) { - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; ui_widget_expose_dirty_rect(w); } } diff --git a/lib/ui/src/ui_renderer.c b/lib/ui/src/ui_renderer.c index af3340fb..ae974930 100644 --- a/lib/ui/src/ui_renderer.c +++ b/lib/ui/src/ui_renderer.c @@ -95,7 +95,7 @@ static bool ui_widget_has_round_border(ui_widget_t *w) void ui_widget_expose_dirty_rect(ui_widget_t *w) { while (w->parent) { - w->parent->has_child_dirty_rect = true; + w->parent->rendering.has_child_dirty_rect = true; w = w->parent; } } @@ -109,7 +109,7 @@ bool ui_widget_mark_dirty_rect(ui_widget_t *w, ui_rect_t *in_rect, return false; } if (in_rect) { - if (w->dirty_rect_type == UI_DIRTY_RECT_TYPE_FULL) { + if (w->rendering.dirty_rect_type == UI_DIRTY_RECT_TYPE_FULL) { return false; } rect = *in_rect; @@ -138,14 +138,14 @@ bool ui_widget_mark_dirty_rect(ui_widget_t *w, ui_rect_t *in_rect, rect.y += w->content_box.y - w->canvas_box.y; break; } - if (w->dirty_rect_type > UI_DIRTY_RECT_TYPE_NONE) { - ui_rect_merge(&w->dirty_rect, &rect, &w->dirty_rect); + if (w->rendering.dirty_rect_type > UI_DIRTY_RECT_TYPE_NONE) { + ui_rect_merge(&w->rendering.dirty_rect, &rect, &w->rendering.dirty_rect); } else { - w->dirty_rect = rect; + w->rendering.dirty_rect = rect; } - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_CUSTOM; + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_CUSTOM; } else { - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; } ui_widget_expose_dirty_rect(w); return true; @@ -159,15 +159,16 @@ static void ui_widget_collect_dirty_rect(ui_widget_t *w, list_t *rects, float x, list_node_t *node; do { - if (w->parent && - w->parent->dirty_rect_type == UI_DIRTY_RECT_TYPE_FULL) { - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; + if (w->parent && w->parent->rendering.dirty_rect_type == + UI_DIRTY_RECT_TYPE_FULL) { + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; break; } - if (w->dirty_rect_type == UI_DIRTY_RECT_TYPE_FULL) { + if (w->rendering.dirty_rect_type == UI_DIRTY_RECT_TYPE_FULL) { rect = w->canvas_box; - } else if (w->dirty_rect_type == UI_DIRTY_RECT_TYPE_CUSTOM) { - rect = w->dirty_rect; + } else if (w->rendering.dirty_rect_type == + UI_DIRTY_RECT_TYPE_CUSTOM) { + rect = w->rendering.dirty_rect; rect.x += w->canvas_box.x; rect.y += w->canvas_box.y; } else { @@ -182,7 +183,7 @@ static void ui_widget_collect_dirty_rect(ui_widget_t *w, list_t *rects, float x, list_append(rects, actual_rect); } } while (0); - if (w->has_child_dirty_rect) { + if (w->rendering.has_child_dirty_rect) { visible_area.x -= x; visible_area.y -= y; ui_rect_overlap(&visible_area, &w->padding_box, &visible_area); @@ -194,8 +195,8 @@ static void ui_widget_collect_dirty_rect(ui_widget_t *w, list_t *rects, float x, y + w->padding_box.y, visible_area); } } - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_NONE; - w->has_child_dirty_rect = false; + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_NONE; + w->rendering.has_child_dirty_rect = false; } size_t ui_widget_get_dirty_rects(ui_widget_t *w, list_t *rects) diff --git a/lib/ui/src/ui_tree.c b/lib/ui/src/ui_tree.c index 04b5b2c5..5a579325 100644 --- a/lib/ui/src/ui_tree.c +++ b/lib/ui/src/ui_tree.c @@ -26,19 +26,19 @@ int ui_widget_append(ui_widget_t* parent, ui_widget_t* widget) widget->parent = parent; widget->state = UI_WIDGET_STATE_CREATED; widget->index = parent->children.length; - widget->parent->update.for_children = true; + widget->parent->update.should_update_children = true; list_append_node(&parent->children, &widget->node); ev.cancel_bubble = true; ev.type = UI_EVENT_LINK; - ui_widget_refresh_style(widget); + ui_widget_request_refresh_style(widget); ui_widget_refresh_children_style(widget); ui_widget_emit_event(widget, ev, NULL); ui_widget_update_status(widget); - ui_widget_add_task(parent, UI_TASK_REFLOW); + ui_widget_request_reflow(parent); if (ui_widget_has_observer(parent, TYPE_CHILD_LIST)) { record = ui_mutation_record_create(widget, TYPE_CHILD_LIST); list_append(&record->added_widgets, widget); - ui_widget_add_mutation_recrod(parent, record); + ui_widget_add_mutation_record(parent, record); ui_mutation_record_destroy(record); } return 0; @@ -62,7 +62,7 @@ int ui_widget_prepend(ui_widget_t* parent, ui_widget_t* widget) widget->index = 0; widget->parent = parent; widget->state = UI_WIDGET_STATE_CREATED; - widget->parent->update.for_children = true; + widget->parent->update.should_update_children = true; node = &widget->node; list_insert_node(&parent->children, 0, node); /** 修改它后面的部件的 index 值 */ @@ -75,13 +75,13 @@ int ui_widget_prepend(ui_widget_t* parent, ui_widget_t* widget) ev.cancel_bubble = true; ev.type = UI_EVENT_LINK; ui_widget_emit_event(widget, ev, NULL); - ui_widget_add_task_for_children(widget, UI_TASK_REFRESH_STYLE); + ui_widget_request_refresh_children(widget); ui_widget_update_status(widget); - ui_widget_add_task(parent, UI_TASK_REFLOW); + ui_widget_request_reflow(parent); if (ui_widget_has_observer(parent, TYPE_CHILD_LIST)) { record = ui_mutation_record_create(widget, TYPE_CHILD_LIST); list_append(&record->added_widgets, widget); - ui_widget_add_mutation_recrod(parent, record); + ui_widget_add_mutation_record(parent, record); ui_mutation_record_destroy(record); } return 0; @@ -104,7 +104,7 @@ int ui_widget_unwrap(ui_widget_t* w) for (list_each(node, &w->children)) { list_append(&record->removed_widgets, node->data); } - ui_widget_add_mutation_recrod(w, record); + ui_widget_add_mutation_record(w, record); ui_mutation_record_destroy(record); } if (ui_widget_has_observer(w->parent, TYPE_CHILD_LIST)) { @@ -112,7 +112,7 @@ int ui_widget_unwrap(ui_widget_t* w) for (list_each(node, &w->children)) { list_append(&record->added_widgets, node->data); } - ui_widget_add_mutation_recrod(w->parent, record); + ui_widget_add_mutation_record(w->parent, record); ui_mutation_record_destroy(record); } children = &w->parent->children; @@ -139,7 +139,7 @@ int ui_widget_unwrap(ui_widget_t* w) child->parent = w->parent; ev.type = UI_EVENT_LINK; ui_widget_emit_event(child, ev, NULL); - ui_widget_add_task_for_children(child, UI_TASK_REFRESH_STYLE); + ui_widget_request_refresh_children(child); node = prev; --len; } @@ -192,11 +192,11 @@ int ui_widget_unlink(ui_widget_t* w) ui_widget_emit_event(w, ev, NULL); list_unlink(&w->parent->children, node); list_unlink(&w->parent->stacking_context, &w->node_show); - ui_widget_add_task(w->parent, UI_TASK_REFLOW); + ui_widget_request_reflow(w->parent); if (ui_widget_has_observer(w->parent, TYPE_CHILD_LIST)) { record = ui_mutation_record_create(w->parent, TYPE_CHILD_LIST); list_append(&record->removed_widgets, w->parent); - ui_widget_add_mutation_recrod(w->parent, record); + ui_widget_add_mutation_record(w->parent, record); ui_mutation_record_destroy(record); } w->parent = NULL; @@ -216,7 +216,7 @@ void ui_widget_empty(ui_widget_t* w) for (list_each(node, &w->children)) { list_append(&record->removed_widgets, node->data); } - ui_widget_add_mutation_recrod(w, record); + ui_widget_add_mutation_record(w, record); ui_mutation_record_destroy(record); } while ((node = list_get_first_node(&w->children)) != NULL) { @@ -227,7 +227,7 @@ void ui_widget_empty(ui_widget_t* w) } list_destroy_without_node(&w->stacking_context, NULL); ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_GRAPH_BOX); - ui_widget_refresh_style(w); + ui_widget_request_refresh_style(w); } void ui_widget_remove(ui_widget_t* w) @@ -248,7 +248,7 @@ void ui_widget_remove(ui_widget_t* w) node = node->next; } if (w->computed_style.type_bits.position != CSS_POSITION_ABSOLUTE) { - ui_widget_add_task(w->parent, UI_TASK_REFLOW); + ui_widget_request_reflow(w->parent); } ui_widget_mark_dirty_rect(w->parent, &w->canvas_box, UI_BOX_TYPE_CONTENT_BOX); diff --git a/lib/ui/src/ui_updater.c b/lib/ui/src/ui_updater.c index 33339302..d8bf2b50 100644 --- a/lib/ui/src/ui_updater.c +++ b/lib/ui/src/ui_updater.c @@ -18,28 +18,16 @@ #include "ui_widget_observer.h" #include "ui_widget.h" -// TODO: 考虑将 style_cache 移动到 ui_updater 内 - -typedef struct ui_updater_profile_t ui_updater_profile_t; -struct ui_updater_profile_t { - unsigned style_hash; +static struct ui_updater_t { /** - * dict_t + * dict_t */ dict_t* style_cache; - ui_updater_profile_t* parent; -}; - -static struct ui_updater_t { dict_type_t style_cache_dict; ui_metrics_t metrics; bool refresh_all; - ui_widget_function_t handlers[UI_TASK_TOTAL_NUM]; } ui_updater; -static size_t ui_widget_update_with_context(ui_widget_t* w, - ui_updater_profile_t* ctx); - static uint64_t ui_style_dict_hash(const void* key) { return (*(unsigned int*)key); @@ -68,58 +56,35 @@ static void ui_style_dict_val_free(void* privdata, void* val) css_style_decl_destroy(val); } -static void ui_widget_on_refresh_style(ui_widget_t* w) -{ - // TODO:是否有必要将样式计算功能分为 refresh 和 update? - ui_widget_force_refresh_style(w); -} - -static void ui_widget_on_update_style(ui_widget_t* w) -{ - ui_widget_force_update_style(w); -} - -void ui_widget_add_task_for_children(ui_widget_t* widget, ui_task_type_t task) +void ui_widget_request_refresh_children(ui_widget_t* widget) { ui_widget_t* child; list_node_t* node; - widget->update.for_children = true; + widget->update.should_update_children = true; for (list_each(node, &widget->children)) { child = node->data; - ui_widget_add_task(child, task); - ui_widget_add_task_for_children(child, task); + child->update.should_refresh_style = true; + ui_widget_request_refresh_children(child); } } -void ui_widget_add_task(ui_widget_t* widget, int task) +void ui_widget_request_update(ui_widget_t* w) { - if (widget->state == UI_WIDGET_STATE_DELETED) { - return; - } - DEBUG_MSG("[%lu] %s, %d\n", widget->index, widget->type, task); - widget->update.for_self = true; - widget->update.states[task] = true; - widget = widget->parent; - /* 向没有标记的父级部件添加标记 */ - while (widget && !widget->update.for_children) { - widget->update.for_children = true; - widget = widget->parent; + w->update.should_update_self = true; + for (w = w->parent; w; w = w->parent) { + w->update.should_update_children = true; } } void ui_widget_set_rules(ui_widget_t* w, const ui_widget_rules_t* rules) { ui_widget_use_extra_data(w); - if (w->extra->style_cache) { - dict_destroy(w->extra->style_cache); - } if (rules) { w->extra->rules = *rules; } else { memset(&w->extra->rules, 0, sizeof(ui_widget_rules_t)); } - w->extra->style_cache = NULL; w->extra->update_progress = 0; w->extra->default_max_update_count = 2048; } @@ -164,9 +129,6 @@ void ui_widget_update_stacking_context(ui_widget_t* w) } } -#define set_task_handler(ID, HANDLER) \ - ui_updater.handlers[UI_TASK_##ID] = HANDLER - void ui_init_updater(void) { dict_type_t* dt = &ui_updater.style_cache_dict; @@ -177,57 +139,25 @@ void ui_init_updater(void) dt->hash_function = ui_style_dict_hash; dt->key_destructor = ui_style_dict_string_key_free; dt->val_destructor = ui_style_dict_val_free; - set_task_handler(UPDATE_STYLE, ui_widget_on_update_style); - set_task_handler(REFRESH_STYLE, ui_widget_on_refresh_style); - set_task_handler(REFLOW, NULL); ui_updater.refresh_all = true; } -static ui_updater_profile_t* ui_widget_begin_update(ui_widget_t* w, - ui_updater_profile_t* ctx) +static void ui_widget_match_style(ui_widget_t* w) { - unsigned hash; css_selector_t* selector; css_style_decl_t* style; const css_style_decl_t* matched_style; - ui_updater_profile_t* self_ctx; - ui_updater_profile_t* parent_ctx; - self_ctx = malloc(sizeof(ui_updater_profile_t)); - if (!self_ctx) { - return NULL; - } - self_ctx->parent = ctx; - self_ctx->style_cache = NULL; - for (parent_ctx = ctx; parent_ctx; parent_ctx = parent_ctx->parent) { - if (parent_ctx->style_cache) { - self_ctx->style_cache = parent_ctx->style_cache; - self_ctx->style_hash = parent_ctx->style_hash; - break; - } - } - if (w->hash && w->update.states[UI_TASK_REFRESH_STYLE]) { + if (w->hash && w->update.should_refresh_style) { ui_widget_generate_self_hash(w); } - if (!self_ctx->style_cache && w->extra && - w->extra->rules.cache_children_style) { - if (!w->extra->style_cache) { - w->extra->style_cache = - dict_create(&ui_updater.style_cache_dict, NULL); - } - ui_widget_generate_self_hash(w); - self_ctx->style_hash = w->hash; - self_ctx->style_cache = w->extra->style_cache; - } matched_style = w->matched_style; - if (self_ctx->style_cache && w->hash) { - hash = self_ctx->style_hash; - hash = ((hash << 5) + hash) + w->hash; - style = dict_fetch_value(self_ctx->style_cache, &hash); + if (w->hash) { + style = dict_fetch_value(ui_updater.style_cache, &w->hash); if (!style) { selector = ui_widget_create_selector(w); style = css_select_style_with_cache(selector); - dict_add(self_ctx->style_cache, &hash, style); + dict_add(ui_updater.style_cache, &w->hash, style); css_selector_destroy(selector); } w->matched_style = style; @@ -236,21 +166,9 @@ static ui_updater_profile_t* ui_widget_begin_update(ui_widget_t* w, w->matched_style = css_select_style_with_cache(selector); css_selector_destroy(selector); } - if (w->matched_style != matched_style) { - ui_widget_add_task(w, UI_TASK_REFRESH_STYLE); - } - return self_ctx; } -static void ui_widget_end_update(ui_updater_profile_t* ctx) -{ - ctx->style_cache = NULL; - ctx->parent = NULL; - free(ctx); -} - -static size_t ui_widget_update_visible_children(ui_widget_t* w, - ui_updater_profile_t* ctx) +static size_t ui_widget_update_visible_children(ui_widget_t* w) { size_t total = 0, count; bool found = false; @@ -296,9 +214,9 @@ static size_t ui_widget_update_visible_children(ui_widget_t* w, continue; } found = true; - count = ui_widget_update_with_context(child, ctx); - if (child->update.for_self || child->update.for_children) { - w->update.for_children = true; + count = ui_widget_update(child); + if (ui_widget_has_update(child)) { + w->update.should_update_children = true; } total += count; node = next; @@ -306,8 +224,7 @@ static size_t ui_widget_update_visible_children(ui_widget_t* w, return total; } -static size_t ui_widget_update_children(ui_widget_t* w, - ui_updater_profile_t* ctx) +static size_t ui_widget_update_children(ui_widget_t* w) { clock_t msec = 0; ui_widget_t* child; @@ -315,9 +232,6 @@ static size_t ui_widget_update_children(ui_widget_t* w, list_node_t *node, *next; size_t total = 0, update_count = 0, count; - if (!w->update.for_children) { - return 0; - } node = w->children.head.next; rules = w->extra ? &w->extra->rules : NULL; if (rules) { @@ -331,21 +245,21 @@ static size_t ui_widget_update_children(ui_widget_t* w, } DEBUG_MSG("%s %s: is visible\n", w->type, w->id); if (rules->first_update_visible_children) { - total += ui_widget_update_visible_children(w, ctx); + total += ui_widget_update_visible_children(w); DEBUG_MSG("first update visible children " "count: %zu\n", total); } - if (!w->update.for_children) { + if (!w->update.should_update_children) { return 0; } } while (node) { child = node->data; next = node->next; - count = ui_widget_update_with_context(child, ctx); - if (child->update.for_self || child->update.for_children) { - w->update.for_children = true; + count = ui_widget_update(child); + if (ui_widget_has_update(child)) { + w->update.should_update_children = true; } total += count; node = next; @@ -364,14 +278,14 @@ static size_t ui_widget_update_children(ui_widget_t* w, if (rules->max_update_children_count > 0) { if (update_count >= (size_t)rules->max_update_children_count) { - w->update.for_children = true; + w->update.should_update_children = true; break; } } if (update_count < w->extra->default_max_update_count) { continue; } - w->update.for_children = true; + w->update.should_update_children = true; msec = (clock() - msec); if (msec < 1) { w->extra->default_max_update_count += 128; @@ -385,7 +299,7 @@ static size_t ui_widget_update_children(ui_widget_t* w, break; } if (rules) { - if (!w->update.for_children) { + if (!w->update.should_update_children) { w->extra->update_progress = w->stacking_context.length; } if (rules->on_update_progress) { @@ -395,78 +309,48 @@ static size_t ui_widget_update_children(ui_widget_t* w, return total; } -static void ui_widget_update_self(ui_widget_t* w, ui_updater_profile_t* ctx) -{ - int i; - bool* states; - ui_style_diff_t style_diff = { 0 }; - - if (!w->update.for_self) { - return; - } - ui_style_diff_init(&style_diff, w); - if (!ui_updater.refresh_all) { - ui_style_diff_begin(&style_diff, w); - } - states = w->update.states; - for (i = 0; i < UI_TASK_REFLOW; ++i) { - if (states[i]) { - if (w->proto && w->proto->runtask) { - w->proto->runtask(w, i); - } - if (ui_updater.handlers[i]) { - ui_updater.handlers[i](w); - } - } - } - if (states[UI_TASK_USER] && w->proto && w->proto->runtask) { - w->proto->runtask(w, UI_TASK_USER); - } - ui_widget_add_state(w, UI_WIDGET_STATE_UPDATED); - ui_style_diff_end(&style_diff, w); -} - -static size_t ui_widget_update_with_context(ui_widget_t* w, - ui_updater_profile_t* ctx) +size_t ui_widget_update(ui_widget_t* w) { size_t count = 0; - ui_updater_profile_t* self_ctx; + ui_style_diff_t style_diff = { 0 }; if (ui_updater.refresh_all) { - w->update.for_self = true; - w->update.for_children = true; - w->update.states[UI_TASK_REFRESH_STYLE] = true; - w->update.states[UI_TASK_REFLOW] = true; - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; + w->update.should_update_children = true; + w->update.should_refresh_style = true; + w->update.should_reflow = true; + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; } - if (!w->update.for_self && !w->update.for_children) { - return 0; - } - self_ctx = ui_widget_begin_update(w, ctx); - ui_widget_update_self(w, self_ctx); -#ifdef UI_DEBUG_ENABLED - { - UI_WIDGET_STR(w, str); - UI_DEBUG_MSG("%s: start update children", str); - ui_debug_msg_indent++; - } -#endif - count += ui_widget_update_children(w, self_ctx); - -#ifdef UI_DEBUG_ENABLED - { - ui_debug_msg_indent--; - UI_WIDGET_STR(w, str); - UI_DEBUG_MSG("%s: end update children", str); - } -#endif - if (w->update.states[UI_TASK_REFLOW]) { -#ifdef UI_DEBUG_ENABLED - { - UI_WIDGET_STR(w, str); - UI_DEBUG_MSG("%s: start initiative reflow", str); + if (ui_widget_has_update(w)) { + if (w->proto && w->proto->update) { + w->proto->update(w, UI_TASK_BEFORE_UPDATE); } -#endif + if (w->update.should_refresh_style) { + ui_widget_match_style(w); + w->update.should_update_style = true; + if (w->proto && w->proto->update) { + w->proto->update(w, UI_TASK_REFRESH_STYLE); + } + } + if (w->update.should_update_style) { + ui_style_diff_init(&style_diff, w); + if (!ui_updater.refresh_all) { + ui_style_diff_begin(&style_diff, w); + } + ui_widget_force_update_style(w); + ui_style_diff_end(&style_diff, w); + if (w->proto && w->proto->update) { + w->proto->update(w, UI_TASK_UPDATE_STYLE); + } + } + ui_widget_add_state(w, UI_WIDGET_STATE_UPDATED); + if (w->proto && w->proto->update) { + w->proto->update(w, UI_TASK_AFTER_UPDATE); + } + } + if (w->update.should_update_children) { + count += ui_widget_update_children(w); + } + if (w->update.should_reflow) { ui_widget_reset_size(w); ui_widget_reflow(w); w->max_content_width = w->content_box.width; @@ -474,95 +358,81 @@ static size_t ui_widget_update_with_context(ui_widget_t* w, if (!w->parent || !ui_widget_in_layout_flow(w)) { ui_widget_update_box_position(w); } - w->update.states[UI_TASK_REFLOW] = false; -#ifdef UI_DEBUG_ENABLED - { - UI_WIDGET_STR(w, str); - UI_DEBUG_MSG("%s: end initiative reflow", str); - } -#endif } - ui_widget_end_update(self_ctx); ui_widget_update_stacking_context(w); return count; } -size_t ui_widget_update(ui_widget_t* w) -{ - size_t count; - ui_updater_profile_t* ctx; - - ctx = ui_widget_begin_update(w, NULL); - count = ui_widget_update_with_context(w, ctx); - ui_widget_end_update(ctx); - return count; -} - void ui_refresh_style(void) { ui_updater.refresh_all = true; } -void ui_mark_dirty_rects(ui_widget_t* w) +static void ui_process_mutations(ui_widget_t* w) { ui_mutation_record_t* record; ui_mutation_record_type_t type = UI_MUTATION_RECORD_TYPE_PROPERTIES; if (w->parent) { - if (w->parent->dirty_rect_type != UI_DIRTY_RECT_TYPE_FULL && - (!ui_rect_is_equal(&w->border_box, &w->border_box_backup) || + if (w->parent->rendering.dirty_rect_type != + UI_DIRTY_RECT_TYPE_FULL && + (!ui_rect_is_equal(&w->border_box, + &w->update.border_box_backup) || !ui_rect_is_equal(&w->canvas_box, - &w->canvas_box_backup))) { - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; - ui_widget_expose_dirty_rect(w); + &w->update.canvas_box_backup))) { + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; + ui_widget_expose_dirty_rect(w); ui_widget_mark_dirty_rect(w->parent, - &w->canvas_box_backup, + &w->update.canvas_box_backup, UI_BOX_TYPE_PADDING_BOX); } - } else if (w->dirty_rect_type != UI_DIRTY_RECT_TYPE_FULL && - (!ui_rect_is_equal(&w->border_box, &w->border_box_backup) || - !ui_rect_is_equal(&w->canvas_box, &w->canvas_box_backup))) { - w->dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; + } else if (w->rendering.dirty_rect_type != UI_DIRTY_RECT_TYPE_FULL && + (!ui_rect_is_equal(&w->border_box, + &w->update.border_box_backup) || + !ui_rect_is_equal(&w->canvas_box, + &w->update.canvas_box_backup))) { + w->rendering.dirty_rect_type = UI_DIRTY_RECT_TYPE_FULL; ui_widget_expose_dirty_rect(w); } - if (w->update.for_children || w->update.states[UI_TASK_REFLOW]) { - ui_widget_each(w, (ui_widget_callback_t)ui_mark_dirty_rects, + if (w->update.should_update_children || w->update.should_reflow) { + ui_widget_each(w, (ui_widget_callback_t)ui_process_mutations, NULL); } - w->update.for_self = false; - w->update.for_children = false; - w->update.states[UI_TASK_REFRESH_STYLE] = false; - w->update.states[UI_TASK_UPDATE_STYLE] = false; - w->update.states[UI_TASK_REFLOW] = false; + w->update.should_update_self = false; + w->update.should_update_style = false; + w->update.should_refresh_style = false; + w->update.should_update_children = false; + w->update.should_reflow = false; ui_widget_add_state(w, UI_WIDGET_STATE_LAYOUTED); if (ui_widget_has_observer(w, type)) { - if (w->border_box_backup.x != w->border_box.x) { + if (w->update.border_box_backup.x != w->border_box.x) { record = ui_mutation_record_create(w, type); record->property_name = strdup2("x"); - ui_widget_add_mutation_recrod(w, record); + ui_widget_add_mutation_record(w, record); ui_mutation_record_destroy(record); } - if (w->border_box_backup.y != w->border_box.y) { + if (w->update.border_box_backup.y != w->border_box.y) { record = ui_mutation_record_create(w, type); record->property_name = strdup2("y"); - ui_widget_add_mutation_recrod(w, record); + ui_widget_add_mutation_record(w, record); ui_mutation_record_destroy(record); } - if (w->border_box_backup.width != w->border_box.width) { + if (w->update.border_box_backup.width != w->border_box.width) { record = ui_mutation_record_create(w, type); record->property_name = strdup2("width"); - ui_widget_add_mutation_recrod(w, record); + ui_widget_add_mutation_record(w, record); ui_mutation_record_destroy(record); } - if (w->border_box_backup.height != w->border_box.height) { + if (w->update.border_box_backup.height != + w->border_box.height) { record = ui_mutation_record_create(w, type); record->property_name = strdup2("height"); - ui_widget_add_mutation_recrod(w, record); + ui_widget_add_mutation_record(w, record); ui_mutation_record_destroy(record); } } - w->canvas_box_backup = w->canvas_box; - w->border_box_backup = w->border_box; + w->update.canvas_box_backup = w->canvas_box; + w->update.border_box_backup = w->border_box; } size_t ui_update(void) @@ -570,6 +440,8 @@ size_t ui_update(void) size_t count; ui_widget_t* root; + ui_updater.style_cache = + dict_create(&ui_updater.style_cache_dict, NULL); if (memcmp(&ui_metrics, &ui_updater.metrics, sizeof(ui_metrics_t))) { ui_updater.refresh_all = true; } @@ -580,7 +452,7 @@ size_t ui_update(void) count = ui_widget_update(root); ui_updater.metrics = ui_metrics; ui_updater.refresh_all = false; - ui_mark_dirty_rects(root); + ui_process_mutations(root); ui_process_mutation_observers(); ui_trash_clear(); return count; diff --git a/lib/ui/src/ui_widget.c b/lib/ui/src/ui_widget.c index cdc244b1..b6757870 100644 --- a/lib/ui/src/ui_widget.c +++ b/lib/ui/src/ui_widget.c @@ -32,7 +32,7 @@ static void ui_widget_init(ui_widget_t* w) void ui_widget_destroy(ui_widget_t* w) { if (w->parent) { - ui_widget_add_task(w->parent, UI_TASK_REFLOW); + ui_widget_request_reflow(w->parent); ui_widget_unlink(w); } ui_widget_destroy_listeners(w); @@ -76,7 +76,7 @@ ui_widget_t* ui_create_widget(const char* type) widget->type = strdup2(type); } widget->proto->init(widget); - ui_widget_add_task(widget, UI_TASK_REFRESH_STYLE); + ui_widget_request_refresh_style(widget); return widget; } @@ -88,7 +88,7 @@ ui_widget_t* ui_create_widget_with_prototype(const ui_widget_prototype_t* proto) widget->proto = proto; widget->type = widget->proto->name; widget->proto->init(widget); - ui_widget_add_task(widget, UI_TASK_REFRESH_STYLE); + ui_widget_request_refresh_style(widget); return widget; } @@ -129,7 +129,7 @@ void ui_widget_set_title(ui_widget_t* w, const wchar_t* title) record = ui_mutation_record_create( w, UI_MUTATION_RECORD_TYPE_PROPERTIES); record->property_name = strdup2("title"); - ui_widget_add_mutation_recrod(w, record); + ui_widget_add_mutation_record(w, record); ui_mutation_record_destroy(record); } } diff --git a/lib/ui/src/ui_widget_classes.c b/lib/ui/src/ui_widget_classes.c index 7af858b1..c4c6775f 100644 --- a/lib/ui/src/ui_widget_classes.c +++ b/lib/ui/src/ui_widget_classes.c @@ -9,7 +9,7 @@ static void ui_widget_refresh_children_by_classes(ui_widget_t* w) if (w->extra && w->extra->rules.ignore_classes_change) { return; } - ui_widget_add_task(w, UI_TASK_REFRESH_STYLE); + ui_widget_request_refresh_style(w); for (list_each(node, &w->children)) { ui_widget_refresh_children_by_classes(node->data); } @@ -17,7 +17,7 @@ static void ui_widget_refresh_children_by_classes(ui_widget_t* w) static int ui_widget_handle_classes_change(ui_widget_t* w, const char *name) { - ui_widget_refresh_style(w); + ui_widget_request_refresh_style(w); if (w->extra && w->extra->rules.ignore_classes_change) { return 0; } diff --git a/lib/ui/src/ui_widget_hash.c b/lib/ui/src/ui_widget_hash.c index 87a848fe..82fd734a 100644 --- a/lib/ui/src/ui_widget_hash.c +++ b/lib/ui/src/ui_widget_hash.c @@ -32,9 +32,6 @@ void ui_widget_generate_self_hash(ui_widget_t* widget) hash = strhash(hash, w->status[i]); } } - if (w->extra && w->extra->rules.cache_children_style) { - break; - } } widget->hash = hash; } diff --git a/lib/ui/src/ui_widget_helper.c b/lib/ui/src/ui_widget_helper.c index f73893c2..9d871172 100644 --- a/lib/ui/src/ui_widget_helper.c +++ b/lib/ui/src/ui_widget_helper.c @@ -33,14 +33,14 @@ void ui_widget_show(ui_widget_t *w) v.array_value[0].type == CSS_KEYWORD_VALUE && v.array_value[0].keyword_value == CSS_KEYWORD_NONE) { ui_widget_unset_style(w, css_prop_display); - ui_widget_update_style(w); + ui_widget_request_update_style(w); } } void ui_widget_hide(ui_widget_t *w) { ui_widget_set_style_keyword_value(w, css_prop_display, CSS_KEYWORD_NONE); - ui_widget_update_style(w); + ui_widget_request_update_style(w); } ui_widget_t *ui_widget_get_closest(ui_widget_t *w, const char *type) diff --git a/lib/ui/src/ui_widget_observer.c b/lib/ui/src/ui_widget_observer.c index db382d7a..a9840355 100644 --- a/lib/ui/src/ui_widget_observer.c +++ b/lib/ui/src/ui_widget_observer.c @@ -43,7 +43,7 @@ bool ui_widget_has_observer(ui_widget_t *widget, ui_mutation_record_type_t type) return false; } -int ui_widget_add_mutation_recrod(ui_widget_t *widget, +int ui_widget_add_mutation_record(ui_widget_t *widget, ui_mutation_record_t *record) { int count = 0; diff --git a/lib/ui/src/ui_widget_observer.h b/lib/ui/src/ui_widget_observer.h index 25a7300f..9146e3ec 100644 --- a/lib/ui/src/ui_widget_observer.h +++ b/lib/ui/src/ui_widget_observer.h @@ -1,5 +1,5 @@ bool ui_widget_has_observer(ui_widget_t *widget, ui_mutation_record_type_t type); -int ui_widget_add_mutation_recrod(ui_widget_t *widget, +int ui_widget_add_mutation_record(ui_widget_t *widget, ui_mutation_record_t *record); diff --git a/lib/ui/src/ui_widget_prototype.c b/lib/ui/src/ui_widget_prototype.c index 40ada573..d26a572d 100644 --- a/lib/ui/src/ui_widget_prototype.c +++ b/lib/ui/src/ui_widget_prototype.c @@ -10,6 +10,10 @@ static void widget_default_method(ui_widget_t *w) { } +static void widget_default_update(ui_widget_t *w, ui_task_type_t task) +{ +} + static void widget_default_attr_setter(ui_widget_t *w, const char *name, const char *value) { @@ -29,10 +33,6 @@ static void widget_default_size_setter(ui_widget_t *w, float width, { } -static void widget_default_task_handler(ui_widget_t *w, int task) -{ -} - static void widget_default_painter(ui_widget_t *w, pd_context_t *paint, ui_widget_actual_style_t *style) { @@ -41,10 +41,8 @@ static void widget_default_painter(ui_widget_t *w, pd_context_t *paint, static ui_widget_prototype_t ui_default_widget_prototype = { .name = NULL, .init = widget_default_method, - .refresh = widget_default_method, .destroy = widget_default_method, - .update = widget_default_method, - .runtask = widget_default_task_handler, + .update = widget_default_update, .setattr = widget_default_attr_setter, .settext = widget_default_text_setter, .autosize = widget_default_size_getter, diff --git a/lib/ui/src/ui_widget_status.c b/lib/ui/src/ui_widget_status.c index e95a08dc..08eb853c 100644 --- a/lib/ui/src/ui_widget_status.c +++ b/lib/ui/src/ui_widget_status.c @@ -9,7 +9,7 @@ static void ui_widget_refresh_children_by_status(ui_widget_t* w) if (w->extra && w->extra->rules.ignore_status_change) { return; } - ui_widget_add_task(w, UI_TASK_REFRESH_STYLE); + ui_widget_request_refresh_style(w); for (list_each(node, &w->children)) { ui_widget_refresh_children_by_status(node->data); } @@ -17,7 +17,7 @@ static void ui_widget_refresh_children_by_status(ui_widget_t* w) static int ui_wdiget_handle_status_change(ui_widget_t* w, const char *name) { - ui_widget_refresh_style(w); + ui_widget_request_refresh_style(w); if (w->state < UI_WIDGET_STATE_READY || w->state == UI_WIDGET_STATE_DELETED) { return 1; } diff --git a/lib/ui/src/ui_widget_style.c b/lib/ui/src/ui_widget_style.c index 34b1dce0..5f090e2e 100644 --- a/lib/ui/src/ui_widget_style.c +++ b/lib/ui/src/ui_widget_style.c @@ -133,9 +133,9 @@ size_t ui_widget_get_children_style_changes(ui_widget_t *w, int type, void ui_widget_update_children_style(ui_widget_t *w) { list_node_t *node; - w->update.for_children = true; + w->update.should_update_children = true; for (list_each(node, &w->children)) { - ui_widget_update_style(node->data); + ui_widget_request_update_style(node->data); ui_widget_update_children_style(node->data); } } @@ -143,9 +143,9 @@ void ui_widget_update_children_style(ui_widget_t *w) void ui_widget_refresh_children_style(ui_widget_t *w) { list_node_t *node; - w->update.for_children = true; + w->update.should_update_children = true; for (list_each(node, &w->children)) { - ui_widget_refresh_style(node->data); + ui_widget_request_refresh_style(node->data); ui_widget_refresh_children_style(node->data); } } @@ -173,7 +173,7 @@ void ui_widget_set_style(ui_widget_t *w, int key, w->custom_style = css_style_decl_create(); css_style_decl_add(w->custom_style, key, value); } - ui_widget_update_style(w); + ui_widget_request_update_style(w); } int ui_widget_unset_style(ui_widget_t *w, int key) @@ -181,7 +181,7 @@ int ui_widget_unset_style(ui_widget_t *w, int key) if (!w->custom_style) { return -1; } - ui_widget_update_style(w); + ui_widget_request_update_style(w); return css_style_decl_remove(w->custom_style, key); } @@ -374,19 +374,6 @@ void ui_widget_force_update_style(ui_widget_t *w) ui_widget_compute_style(w); ui_widget_update_box_size(w); ui_widget_update_box_position(w); - if (w->proto && w->proto->update) { - /* 扩展部分的样式交给该部件自己处理 */ - w->proto->update(w); - } -} - -void ui_widget_force_refresh_style(ui_widget_t *w) -{ - ui_widget_force_update_style(w); - /* 刷新该部件的相关数据 */ - if (w->proto && w->proto->refresh) { - w->proto->refresh(w); - } } void ui_widget_destroy_style(ui_widget_t *w) diff --git a/tests/cases/test_widget_opacity.c b/tests/cases/test_widget_opacity.c index cec09888..651efb47 100644 --- a/tests/cases/test_widget_opacity.c +++ b/tests/cases/test_widget_opacity.c @@ -56,8 +56,8 @@ static void check_widget_opactiy(void) ui_widget_set_style_string(self.parent, "opacity", "0.8"); ui_widget_resize(self.parent, 512, 256); - ui_widget_refresh_style(self.child); - ui_widget_refresh_style(self.parent); + ui_widget_request_refresh_style(self.child); + ui_widget_request_refresh_style(self.parent); ui_widget_update(self.child); ui_widget_update(self.parent); ui_widget_render(self.parent, &paint);