mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
fix(ui): refresh status styles only for matched selectors
- skip style refreshes when no :hover/:active/:focus selector matches - detect self and descendant status selectors separately before refresh - preserve API text content across CSS content overrides and restore it on exit - cover unmatched, matched and text-content status changes in integration tests
This commit is contained in:
@@ -29,18 +29,25 @@ 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_request_refresh_style(w);
|
||||
if (w->state < UI_WIDGET_STATE_READY ||
|
||||
w->state == UI_WIDGET_STATE_DELETED) {
|
||||
ui_widget_request_refresh_style(w);
|
||||
return 1;
|
||||
}
|
||||
if (w->extra && w->extra->rules.ignore_status_change) {
|
||||
return 0;
|
||||
}
|
||||
/* A status on this widget can also affect descendant selectors.
|
||||
* Refresh the subtree first; only refresh the widget itself when a
|
||||
* matching self selector exists. */
|
||||
if (ui_widget_get_children_style_changes(w, 1, name) > 0) {
|
||||
ui_widget_refresh_children_by_status(w);
|
||||
return 1;
|
||||
}
|
||||
if (ui_widget_get_self_style_changes(w, 1, name) > 0) {
|
||||
ui_widget_request_refresh_style(w);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -112,8 +112,9 @@ static void ui_widget_update_style_dependencies(ui_widget_t *w,
|
||||
}
|
||||
}
|
||||
|
||||
size_t ui_widget_get_children_style_changes(ui_widget_t *w, int type,
|
||||
const char *name)
|
||||
static size_t ui_widget_get_style_changes(ui_widget_t *w, int type,
|
||||
const char *name, size_t first_group,
|
||||
size_t last_group)
|
||||
{
|
||||
css_selector_t *s;
|
||||
list_t selector_names;
|
||||
@@ -168,7 +169,7 @@ size_t ui_widget_get_children_style_changes(ui_widget_t *w, int type,
|
||||
if (i == n) {
|
||||
continue;
|
||||
}
|
||||
for (i = 1; i < css_get_groups_length(); ++i) {
|
||||
for (i = first_group; i < last_group; ++i) {
|
||||
count += css_query_selector_from_group(
|
||||
(int)i, selector_name, s, NULL);
|
||||
}
|
||||
@@ -182,6 +183,19 @@ size_t ui_widget_get_children_style_changes(ui_widget_t *w, int type,
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t ui_widget_get_children_style_changes(ui_widget_t *w, int type,
|
||||
const char *name)
|
||||
{
|
||||
return ui_widget_get_style_changes(w, type, name, 1,
|
||||
css_get_groups_length());
|
||||
}
|
||||
|
||||
size_t ui_widget_get_self_style_changes(ui_widget_t *w, int type,
|
||||
const char *name)
|
||||
{
|
||||
return ui_widget_get_style_changes(w, type, name, 0, 1);
|
||||
}
|
||||
|
||||
void ui_widget_update_children_style(ui_widget_t *w)
|
||||
{
|
||||
list_node_t *node;
|
||||
|
||||
@@ -100,6 +100,9 @@ LIBUI_INLINE bool ui_widget_has_flex_column_direction(ui_widget_t *w)
|
||||
size_t ui_widget_get_children_style_changes(ui_widget_t *w, int type,
|
||||
const char *name);
|
||||
|
||||
size_t ui_widget_get_self_style_changes(ui_widget_t *w, int type,
|
||||
const char *name);
|
||||
|
||||
void ui_widget_resolve_background(ui_widget_t *w);
|
||||
|
||||
void ui_widget_destroy_style(ui_widget_t *w);
|
||||
|
||||
+55
-25
@@ -26,6 +26,7 @@ typedef struct ui_text_task {
|
||||
|
||||
typedef struct ui_text {
|
||||
wchar_t *content;
|
||||
wchar_t *base_content;
|
||||
bool trimming;
|
||||
ui_widget_t *widget;
|
||||
pd_text_t *layer;
|
||||
@@ -52,6 +53,36 @@ static bool ui_text_content_equal(const wchar_t *a, const wchar_t *b)
|
||||
return wcscmp(a, b) == 0;
|
||||
}
|
||||
|
||||
static int ui_text_apply_content_w(ui_widget_t *w, const wchar_t *text)
|
||||
{
|
||||
ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype);
|
||||
wchar_t *content, *newtext;
|
||||
|
||||
content = y_wcsdup(text ? text : L"");
|
||||
if (!content) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
newtext = y_wcsdup(content);
|
||||
if (!newtext) {
|
||||
free(content);
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (txt->trimming) {
|
||||
y_wcstrim(newtext, content, NULL);
|
||||
}
|
||||
if (txt->content) {
|
||||
free(txt->content);
|
||||
}
|
||||
txt->content = content;
|
||||
if (txt->task.content) {
|
||||
free(txt->task.content);
|
||||
}
|
||||
txt->task.update_content = true;
|
||||
txt->task.content = newtext;
|
||||
ui_widget_request_update(w);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool parse_boolean(const char *str)
|
||||
{
|
||||
if (strcmp(str, "on") == 0 || strcmp(str, "true") == 0 ||
|
||||
@@ -69,7 +100,7 @@ static void ui_text_on_parse_attr(ui_widget_t *w, const char *name,
|
||||
if (strcmp(name, "trimming") == 0) {
|
||||
if (parse_boolean(value) != txt->trimming) {
|
||||
txt->trimming = !txt->trimming;
|
||||
ui_text_set_content_w(w, txt->content);
|
||||
ui_text_apply_content_w(w, txt->content);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -150,7 +181,8 @@ static void ui_text_on_update_style(ui_widget_t *w)
|
||||
style.white_space != CSS_WHITE_SPACE_NOWRAP);
|
||||
pd_text_set_word_break(txt->layer, style.word_break);
|
||||
if (content_changed) {
|
||||
ui_text_set_content_w(w, style.content);
|
||||
ui_text_apply_content_w(
|
||||
w, style.content ? style.content : txt->base_content);
|
||||
}
|
||||
pd_text_set_style(txt->layer, &text_style);
|
||||
pd_text_set_underline(
|
||||
@@ -206,6 +238,7 @@ static void ui_text_on_init(ui_widget_t *w)
|
||||
txt->task.update_content = false;
|
||||
txt->task.content = NULL;
|
||||
txt->content = NULL;
|
||||
txt->base_content = NULL;
|
||||
txt->trimming = true;
|
||||
txt->layer = pd_text_create();
|
||||
pd_text_set_autowrap(txt->layer, true);
|
||||
@@ -226,6 +259,7 @@ static void ui_text_on_destroy(ui_widget_t *w)
|
||||
ui_text_style_destroy(&txt->style);
|
||||
pd_text_destroy(txt->layer);
|
||||
free(txt->content);
|
||||
free(txt->base_content);
|
||||
if (txt->task.content) {
|
||||
free(txt->task.content);
|
||||
txt->task.content = NULL;
|
||||
@@ -412,35 +446,25 @@ static void ui_text_on_paint(ui_widget_t *w, pd_context_t *paint,
|
||||
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 *content, *newtext = NULL;
|
||||
wchar_t *base_content = NULL;
|
||||
int ret = 0;
|
||||
|
||||
if (text) {
|
||||
/* Snapshot the source before releasing the previous buffer.
|
||||
* Callers may pass txt->content itself, so reading text after
|
||||
* the free below would be a use-after-free. */
|
||||
content = y_wcsdup(text);
|
||||
if (!content) {
|
||||
/* Snapshot the source before releasing the previous buffer. */
|
||||
base_content = y_wcsdup(text);
|
||||
if (!base_content) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
newtext = y_wcsdup(content);
|
||||
if (!newtext) {
|
||||
free(content);
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (txt->content) {
|
||||
free(txt->content);
|
||||
}
|
||||
txt->content = content;
|
||||
if (txt->trimming) {
|
||||
y_wcstrim(newtext, content, NULL);
|
||||
}
|
||||
if (!txt->style.content) {
|
||||
ret = ui_text_apply_content_w(w, text);
|
||||
if (ret != 0) {
|
||||
free(base_content);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
if (txt->task.content) {
|
||||
free(txt->task.content);
|
||||
}
|
||||
txt->task.update_content = true;
|
||||
txt->task.content = newtext;
|
||||
ui_widget_request_update(w);
|
||||
free(txt->base_content);
|
||||
txt->base_content = base_content;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -461,6 +485,12 @@ size_t ui_text_get_content_w(ui_widget_t *w, wchar_t *buf, size_t size)
|
||||
{
|
||||
ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype);
|
||||
|
||||
if (!txt->content) {
|
||||
if (buf && size > 0) {
|
||||
buf[0] = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (buf && size > 0) {
|
||||
wcsncpy(buf, txt->content, size);
|
||||
buf[size - 1] = 0;
|
||||
|
||||
+10
@@ -63,6 +63,16 @@ home-column {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
/* A hover-only layout change with no geometric effect: `position` forces the
|
||||
* status refresh through the reflow path while the final box stays stable. */
|
||||
.test-home-card-body:hover {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.test-home-empty button:hover {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.test-home-body-block {
|
||||
height: 200px;
|
||||
background: #f7f7f7;
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
/*
|
||||
* tests/integration/test_widget_status.c
|
||||
*
|
||||
* Copyright (c) 2026, Liu Chao <hello@lcui.dev> All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
* This file is part of LCUI, distributed under the MIT License found
|
||||
* in the LICENSE.TXT file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Status changes should only request a style refresh when a matching
|
||||
* :hover / :active / :focus rule exists. A matched visual rule must not
|
||||
* reflow the widget or its parent, while a matched `content` rule on a Text
|
||||
* widget must update the text and reflow the affected subtree.
|
||||
*/
|
||||
|
||||
#include <LCUI.h>
|
||||
#include <ctest-custom.h>
|
||||
#include <wchar.h>
|
||||
|
||||
static const char *widget_status_css = "\
|
||||
.status-host {\
|
||||
display: block;\
|
||||
width: 200px;\
|
||||
height: 100px;\
|
||||
}\
|
||||
.status-plain {\
|
||||
display: block;\
|
||||
width: 80px;\
|
||||
height: 20px;\
|
||||
background-color: #101010;\
|
||||
}\
|
||||
.status-target {\
|
||||
display: block;\
|
||||
width: 80px;\
|
||||
height: 20px;\
|
||||
background-color: #111111;\
|
||||
}\
|
||||
.status-target:hover {\
|
||||
background-color: #222222;\
|
||||
}\
|
||||
.status-target:active {\
|
||||
background-color: #333333;\
|
||||
}\
|
||||
.status-target:focus {\
|
||||
background-color: #444444;\
|
||||
}\
|
||||
.status-text-parent {\
|
||||
display: block;\
|
||||
width: 120px;\
|
||||
height: auto;\
|
||||
}\
|
||||
.status-text {\
|
||||
display: block;\
|
||||
width: 80px;\
|
||||
white-space: normal;\
|
||||
font-size: 14px;\
|
||||
line-height: 20px;\
|
||||
}\
|
||||
.status-text:hover {\
|
||||
content: \"A much longer hover label that wraps\";\
|
||||
}\
|
||||
";
|
||||
|
||||
static void settle(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 4; ++i) {
|
||||
ui_update();
|
||||
}
|
||||
}
|
||||
|
||||
static ui_widget_t *create_status_widget(const char *host_class,
|
||||
const char *target_class,
|
||||
ui_widget_t **parent_out)
|
||||
{
|
||||
ui_widget_t *parent = ui_create_widget(NULL);
|
||||
ui_widget_t *target = ui_create_widget(NULL);
|
||||
|
||||
ui_widget_add_class(parent, host_class);
|
||||
ui_widget_add_class(target, target_class);
|
||||
ui_widget_append(parent, target);
|
||||
ui_widget_append(ui_root(), parent);
|
||||
if (parent_out) {
|
||||
*parent_out = parent;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
static void destroy_status_widget(ui_widget_t *w)
|
||||
{
|
||||
ui_widget_destroy(w->parent);
|
||||
ui_clear_trash();
|
||||
}
|
||||
|
||||
static void test_unmatched_status_does_not_refresh_style(void)
|
||||
{
|
||||
ui_widget_t *parent = NULL;
|
||||
ui_widget_t *target =
|
||||
create_status_widget("status-host", "status-plain", &parent);
|
||||
const char *statuses[] = { "hover", "active", "focus" };
|
||||
int i;
|
||||
|
||||
settle();
|
||||
for (i = 0; i < 3; ++i) {
|
||||
ui_widget_add_status(target, statuses[i]);
|
||||
ctest_equal_bool("unmatched status should not request a target "
|
||||
"style refresh",
|
||||
target->update.should_refresh_style, false);
|
||||
ctest_equal_bool(
|
||||
"unmatched status should not request a target update",
|
||||
target->update.should_update_self, false);
|
||||
ctest_equal_bool(
|
||||
"unmatched status should not request a parent reflow",
|
||||
parent->update.should_reflow, false);
|
||||
settle();
|
||||
|
||||
ui_widget_remove_status(target, statuses[i]);
|
||||
ctest_equal_bool("removing an unmatched status should not "
|
||||
"refresh the target",
|
||||
target->update.should_refresh_style, false);
|
||||
ctest_equal_bool(
|
||||
"removing an unmatched status should not request a parent "
|
||||
"reflow",
|
||||
parent->update.should_reflow, false);
|
||||
settle();
|
||||
}
|
||||
|
||||
destroy_status_widget(target);
|
||||
}
|
||||
|
||||
static void test_matched_status_refreshes_without_reflow(void)
|
||||
{
|
||||
ui_widget_t *parent = NULL;
|
||||
ui_widget_t *target =
|
||||
create_status_widget("status-host", "status-target", &parent);
|
||||
const struct {
|
||||
const char *name;
|
||||
css_color_value_t color;
|
||||
} statuses[] = {
|
||||
{ "hover", css_color(255, 0x22, 0x22, 0x22) },
|
||||
{ "active", css_color(255, 0x33, 0x33, 0x33) },
|
||||
{ "focus", css_color(255, 0x44, 0x44, 0x44) },
|
||||
};
|
||||
float width;
|
||||
float height;
|
||||
float parent_height;
|
||||
int i;
|
||||
|
||||
settle();
|
||||
width = target->border_box.width;
|
||||
height = target->border_box.height;
|
||||
parent_height = parent->border_box.height;
|
||||
for (i = 0; i < 3; ++i) {
|
||||
ui_widget_add_status(target, statuses[i].name);
|
||||
ctest_equal_bool(
|
||||
"matched status should request a target style refresh",
|
||||
target->update.should_refresh_style, true);
|
||||
ctest_equal_bool(
|
||||
"matched status should not request a target reflow",
|
||||
target->update.should_reflow, false);
|
||||
ctest_equal_bool(
|
||||
"matched status should not request a parent reflow",
|
||||
parent->update.should_reflow, false);
|
||||
settle();
|
||||
ctest_equal_int("matched status should apply its style",
|
||||
target->computed_style.background_color,
|
||||
statuses[i].color);
|
||||
ctest_equal_float("matched status should keep the target width",
|
||||
target->border_box.width, width);
|
||||
ctest_equal_float(
|
||||
"matched status should keep the target height",
|
||||
target->border_box.height, height);
|
||||
ctest_equal_float(
|
||||
"matched status should keep the parent height",
|
||||
parent->border_box.height, parent_height);
|
||||
|
||||
ui_widget_remove_status(target, statuses[i].name);
|
||||
ctest_equal_bool(
|
||||
"removing a matched status should request a style refresh",
|
||||
target->update.should_refresh_style, true);
|
||||
ctest_equal_bool(
|
||||
"removing a matched status should not request a target "
|
||||
"reflow",
|
||||
target->update.should_reflow, false);
|
||||
ctest_equal_bool(
|
||||
"removing a matched status should not request a parent "
|
||||
"reflow",
|
||||
parent->update.should_reflow, false);
|
||||
settle();
|
||||
ctest_equal_int("removing a matched status should restore the "
|
||||
"base style",
|
||||
target->computed_style.background_color,
|
||||
css_color(255, 0x11, 0x11, 0x11));
|
||||
ctest_equal_float("removing a matched status should keep the "
|
||||
"target width",
|
||||
target->border_box.width, width);
|
||||
ctest_equal_float("removing a matched status should keep the "
|
||||
"parent height",
|
||||
parent->border_box.height, parent_height);
|
||||
}
|
||||
|
||||
destroy_status_widget(target);
|
||||
}
|
||||
|
||||
static void test_status_content_change_reflows_text(void)
|
||||
{
|
||||
ui_widget_t *parent = NULL;
|
||||
ui_widget_t *text;
|
||||
wchar_t content[128];
|
||||
float text_height;
|
||||
float parent_height;
|
||||
|
||||
parent = ui_create_widget(NULL);
|
||||
text = ui_create_widget("text");
|
||||
ui_widget_add_class(parent, "status-text-parent");
|
||||
ui_widget_add_class(text, "status-text");
|
||||
ui_text_set_content(text, "Short");
|
||||
ui_widget_append(parent, text);
|
||||
ui_widget_append(ui_root(), parent);
|
||||
settle();
|
||||
|
||||
text_height = text->border_box.height;
|
||||
parent_height = parent->border_box.height;
|
||||
ui_widget_add_status(text, "hover");
|
||||
ctest_equal_bool("content status should request a text style refresh",
|
||||
text->update.should_refresh_style, true);
|
||||
settle();
|
||||
|
||||
ui_text_get_content_w(text, content, 128);
|
||||
ctest_equal_bool(
|
||||
"content status should update the text",
|
||||
wcscmp(content, L"A much longer hover label that wraps") == 0,
|
||||
true);
|
||||
ctest_equal_bool("content status should grow the text height",
|
||||
text->border_box.height > text_height, true);
|
||||
ctest_equal_bool("content status should reflow the parent height",
|
||||
parent->border_box.height > parent_height, true);
|
||||
|
||||
ui_widget_remove_status(text, "hover");
|
||||
settle();
|
||||
ui_text_get_content_w(text, content, 128);
|
||||
ctest_equal_bool("removing the content status should restore the text",
|
||||
wcscmp(content, L"Short") == 0, true);
|
||||
ctest_equal_float("removing the content status should restore the text "
|
||||
"height",
|
||||
text->border_box.height, text_height);
|
||||
ctest_equal_float("removing the content status should restore the "
|
||||
"parent height",
|
||||
parent->border_box.height, parent_height);
|
||||
|
||||
destroy_status_widget(text);
|
||||
}
|
||||
|
||||
void test_widget_status(void)
|
||||
{
|
||||
lcui_init();
|
||||
ui_load_css_string(widget_status_css, __FILE__);
|
||||
ui_widget_resize(ui_root(), 400, 300);
|
||||
|
||||
ctest_describe("widget status unmatched",
|
||||
test_unmatched_status_does_not_refresh_style);
|
||||
ctest_describe("widget status matched",
|
||||
test_matched_status_refreshes_without_reflow);
|
||||
ctest_describe("widget status content",
|
||||
test_status_content_change_reflows_text);
|
||||
|
||||
lcui_destroy();
|
||||
}
|
||||
@@ -25,6 +25,7 @@ extern void test_text_decoration(void);
|
||||
extern void test_textinput(void);
|
||||
extern void test_font_face(void);
|
||||
extern void test_widget_opacity(void);
|
||||
extern void test_widget_status(void);
|
||||
extern void test_block_layout(void);
|
||||
extern void test_flex_layout(void);
|
||||
extern void test_flex_layout_percentage_wrap(void);
|
||||
@@ -86,6 +87,7 @@ static const ctest_suite_t suites[] = {
|
||||
{ "textinput", test_textinput },
|
||||
{ "font face", test_font_face },
|
||||
{ "widget opacity", test_widget_opacity },
|
||||
{ "widget status", test_widget_status },
|
||||
{ "block layout", test_block_layout },
|
||||
{ "flex layout", test_flex_layout },
|
||||
{ "flex layout percentage wrap", test_flex_layout_percentage_wrap },
|
||||
|
||||
Reference in New Issue
Block a user