fix text node extent invalidation

This commit is contained in:
hippoz 2023-05-24 23:00:17 +03:00
parent f4fcca122b
commit ae80e1776f
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
4 changed files with 17 additions and 15 deletions

View file

@ -183,7 +183,7 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void *
case UPDATE_COUNT: { case UPDATE_COUNT: {
state->count = d; state->count = d;
snprintf(state->counter_text, sizeof(state->counter_text), "%d", state->count); snprintf(state->counter_text, sizeof(state->counter_text), "%d", state->count);
state->counter_text_node->pending_text = state->counter_text; text_node_set_text(state->counter_text_node, state->counter_text);
node_request_relayout((UINode*)state->counter_text_node); node_request_relayout((UINode*)state->counter_text_node);
break; break;
} }

View file

@ -26,10 +26,6 @@ int text_input_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
UITextInputNode *n = (UITextInputNode *)node; UITextInputNode *n = (UITextInputNode *)node;
switch (ev) { switch (ev) {
case UI_EVENT_ATTACHED: {
node_notify_subscribe(node->parent, node, (enum UIEvent[]){ UI_EVENT_KEY_DOWN, UI_EVENT_PRESSED, UI_EVENT_UNPRESSED, 0 });
break;
}
case UI_EVENT_KEY_DOWN: { case UI_EVENT_KEY_DOWN: {
xkb_keycode_t keycode = d; xkb_keycode_t keycode = d;
struct xkb_state *xkb_state = (struct xkb_state*)p; struct xkb_state *xkb_state = (struct xkb_state*)p;
@ -87,7 +83,7 @@ int text_input_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
} }
if (n->text_node) { if (n->text_node) {
n->text_node->pending_text = n->text.data; text_node_set_text(n->text_node, n->text.data);
n->text_node->caret_index = n->text_cursor_index; n->text_node->caret_index = n->text_cursor_index;
n->text_node->wrap = true; n->text_node->wrap = true;
node_request_relayout(&n->text_node->node); node_request_relayout(&n->text_node->node);

View file

@ -12,7 +12,7 @@ UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA col
UITextNode *n = malloc(sizeof(UITextNode)); UITextNode *n = malloc(sizeof(UITextNode));
node_init(&n->node); node_init(&n->node);
n->node.flags = UI_NODE_COMPONENT; n->node.flags = UI_NODE_COMPONENT;
n->pending_text = text; n->_pending_text = text;
n->desc = desc; n->desc = desc;
n->layout = NULL; n->layout = NULL;
n->color = color; n->color = color;
@ -25,6 +25,12 @@ UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA col
return n; return n;
} }
void text_node_set_text(UITextNode *text_node, char *text)
{
text_node->_pending_text = text;
text_node->node.cached_computed_extents = false;
}
int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p) int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
{ {
(void)d; (void)d;
@ -36,11 +42,11 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
UITextNode *n = (UITextNode*)node; UITextNode *n = (UITextNode*)node;
if (n->pending_text) { if (n->_pending_text) {
if (n->layout) { if (n->layout) {
pango_layout_set_text(n->layout, n->pending_text, -1); pango_layout_set_text(n->layout, n->_pending_text, -1);
pango_cairo_update_layout(node->drw, n->layout); pango_cairo_update_layout(node->drw, n->layout);
n->pending_text = NULL; n->_pending_text = NULL;
} else { } else {
PangoLayout *layout = pango_cairo_create_layout(n->node.drw); PangoLayout *layout = pango_cairo_create_layout(n->node.drw);
pango_layout_set_font_description(layout, n->desc); pango_layout_set_font_description(layout, n->desc);
@ -49,11 +55,10 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
} else { } else {
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
} }
pango_layout_set_text(layout, n->pending_text, -1); pango_layout_set_text(layout, n->_pending_text, -1);
n->layout = layout; n->layout = layout;
n->pending_text = NULL; n->_pending_text = NULL;
} }
node->parent->cached_computed_extents = false;
} }
switch (ev) { switch (ev) {
@ -118,7 +123,7 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
g_object_unref(n->layout); g_object_unref(n->layout);
} }
n->layout = NULL; n->layout = NULL;
n->pending_text = NULL; n->_pending_text = NULL;
break; break;
} }
default: { default: {

View file

@ -11,7 +11,7 @@ typedef struct UITextNode {
UINode node; UINode node;
PangoFontDescription *desc; PangoFontDescription *desc;
PangoLayout *layout; PangoLayout *layout;
char *pending_text; char *_pending_text;
UIRGBA color; UIRGBA color;
ssize_t caret_index; ssize_t caret_index;
UINode *caret_node; UINode *caret_node;
@ -19,6 +19,7 @@ typedef struct UITextNode {
} UITextNode; } UITextNode;
int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p); int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p);
void text_node_set_text(UITextNode *text_node, char *text);
UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA color, char *text); UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA color, char *text);
#endif // _UI__TEXT_NODE_H #endif // _UI__TEXT_NODE_H