Compare commits
No commits in common. "1e2e11cd2b0abca503dffc31a5ca084278410207" and "dc83e09f43c3793603220681520e0c8f90a17878" have entirely different histories.
1e2e11cd2b
...
dc83e09f43
9 changed files with 85 additions and 80 deletions
4
Makefile
4
Makefile
|
@ -13,10 +13,10 @@ OBJS=$(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS))
|
|||
DEPS=$(OBJS:%.o=%.d)
|
||||
|
||||
|
||||
all: CFLAGS+=-fsanitize=address -Og -ggdb
|
||||
all: CFLAGS+=-Og -ggdb -DNDEBUG
|
||||
all: $(BUILD) $(BIN)
|
||||
|
||||
release: CFLAGS+=-O2 -flto=auto -DNDEBUG -ggdb
|
||||
release: CFLAGS+=-O2 -flto=auto -DNDEBUG
|
||||
release: clean $(BUILD) $(BIN)
|
||||
|
||||
$(BIN): $(OBJS)
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "box-layout-node.h"
|
||||
#include "node.h"
|
||||
#include "rect.h"
|
||||
|
||||
int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p)
|
||||
{
|
||||
|
@ -29,33 +27,38 @@ int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p)
|
|||
int growing_widgets = 0;
|
||||
int current_index = 0;
|
||||
|
||||
if (ev == UI_EVENT_RELAYOUT || ev == UI_EVENT_GET_EXTENTS) {
|
||||
if (ev == UI_EVENT_RELAYOUT || ev == UI_EVENT_GET_HEIGHT || ev == UI_EVENT_GET_WIDTH) {
|
||||
for (int i = 0; i < node->nodes_count; i++) {
|
||||
UINode *current = node->nodes[i];
|
||||
if (current->flags & UI_NODE_COMPONENT) continue;
|
||||
|
||||
UIRect current_rect = node_get_computed_rect(current);
|
||||
|
||||
current->rect.w = current_rect.w;
|
||||
current->rect.h = current_rect.h;
|
||||
|
||||
double w = current->rect.w;
|
||||
double h = current->rect.h;
|
||||
if (current->width_policy == UI_SIZE_POLICY_DYNAMIC) {
|
||||
node_dispatch(current, UI_EVENT_GET_WIDTH, 0, &w);
|
||||
current->rect.w = w;
|
||||
}
|
||||
if (current->height_policy == UI_SIZE_POLICY_DYNAMIC) {
|
||||
node_dispatch(current, UI_EVENT_GET_HEIGHT, 0, &h);
|
||||
current->rect.h = h;
|
||||
}
|
||||
if (is_horizontal) {
|
||||
if (current_rect.h > maximum_secondary_position) {
|
||||
maximum_secondary_position = current_rect.h;
|
||||
if (h > maximum_secondary_position) {
|
||||
maximum_secondary_position = h;
|
||||
}
|
||||
if (current->width_policy == UI_SIZE_POLICY_GROW) {
|
||||
growing_widgets++;
|
||||
} else {
|
||||
size += current_rect.w;
|
||||
size += w;
|
||||
}
|
||||
} else {
|
||||
if (current_rect.w > maximum_secondary_position) {
|
||||
maximum_secondary_position = current_rect.w;
|
||||
if (w > maximum_secondary_position) {
|
||||
maximum_secondary_position = w;
|
||||
}
|
||||
if (current->height_policy == UI_SIZE_POLICY_GROW) {
|
||||
growing_widgets++;
|
||||
} else {
|
||||
size += current_rect.h;
|
||||
size += h;
|
||||
}
|
||||
}
|
||||
if (current_index) {
|
||||
|
@ -67,14 +70,18 @@ int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p)
|
|||
maximum_secondary_position += is_horizontal ? margin_top + margin_bottom : margin_left + margin_right;
|
||||
size += is_vertical ? margin_top + margin_bottom : margin_left + margin_right;
|
||||
|
||||
if (ev == UI_EVENT_GET_EXTENTS) {
|
||||
((UIRect*)p)->w = is_horizontal ? size : maximum_secondary_position;
|
||||
((UIRect*)p)->h = is_vertical ? size : maximum_secondary_position;
|
||||
return UI_NODE_COMPUTED_EXTENTS_OK;
|
||||
if (ev == UI_EVENT_GET_WIDTH) {
|
||||
*(double*)p = is_horizontal ? size : maximum_secondary_position;
|
||||
return 1;
|
||||
}
|
||||
if (ev == UI_EVENT_GET_HEIGHT) {
|
||||
*(double*)p = is_vertical ? size : maximum_secondary_position;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// we can distribute growing layout children only if we have a fixed sized policy in the layout primary direction
|
||||
bool is_fixed_for_primary_direction = (is_horizontal && node->width_policy != UI_SIZE_POLICY_COMPUTED) || (is_vertical && node->height_policy != UI_SIZE_POLICY_COMPUTED);
|
||||
bool is_fixed_for_primary_direction = (is_horizontal && node->width_policy != UI_SIZE_POLICY_DYNAMIC) || (is_vertical && node->height_policy != UI_SIZE_POLICY_DYNAMIC);
|
||||
double primary_direction = is_horizontal ? node->rect.w - margin_left - margin_right : node->rect.h - margin_top - margin_bottom;
|
||||
bool distributes_growing_widgets = growing_widgets && is_fixed_for_primary_direction;
|
||||
double size_per_growing_widget = 0;
|
||||
|
@ -88,7 +95,7 @@ int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p)
|
|||
}
|
||||
}
|
||||
|
||||
bool is_fixed_for_secondary_direction = (is_horizontal && node->height_policy != UI_SIZE_POLICY_COMPUTED) || (is_vertical && node->width_policy != UI_SIZE_POLICY_COMPUTED);
|
||||
bool is_fixed_for_secondary_direction = (is_horizontal && node->height_policy != UI_SIZE_POLICY_DYNAMIC) || (is_vertical && node->width_policy != UI_SIZE_POLICY_DYNAMIC);
|
||||
double secondary_direction = is_horizontal ? node->rect.h - margin_top - margin_bottom : node->rect.w - margin_left - margin_right;
|
||||
// if our layout secondary direction is fixed, we set the maximum known secondary position to it
|
||||
if (is_fixed_for_secondary_direction) {
|
||||
|
|
|
@ -125,18 +125,20 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void *
|
|||
UIScrollableNode *scroll = scrollable_new(scroll_container, NULL);
|
||||
UINode *buttons = node_new(scroll_container, "buttons");
|
||||
buttons->width_policy = UI_SIZE_POLICY_GROW;
|
||||
buttons->height_policy = UI_SIZE_POLICY_COMPUTED;
|
||||
buttons->height_policy = UI_SIZE_POLICY_DYNAMIC;
|
||||
scroll->target = buttons;
|
||||
|
||||
box_layout_new(buttons, UI_DIRECTION_VERTICAL);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
UINode *button = node_new(buttons, "button");
|
||||
state_background_node_new(button, UIPurple600, UIPurple700, UIPurple800, 6.0);
|
||||
text_node_new(button, state->font, UINeutral50, "button");
|
||||
dispatcher_new(button, UI_EVENT_UNPRESSED, INCREMENT_COUNT, state, app_handle);
|
||||
button->width_policy = UI_SIZE_POLICY_GROW;
|
||||
}
|
||||
|
||||
printf("%d\n", buttons->last_component_index);
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
25
src/node.c
25
src/node.c
|
@ -6,24 +6,6 @@
|
|||
typedef struct UIWindow UIWindow;
|
||||
int window_invalidate_node(UIWindow *window, UINode *node);
|
||||
|
||||
UIRect node_get_computed_rect(UINode *node) {
|
||||
UIRect rect = node->rect;
|
||||
UIRect extents = node->rect;
|
||||
|
||||
if (!node->cached_computed_extents && (node->width_policy == UI_SIZE_POLICY_COMPUTED || node->height_policy == UI_SIZE_POLICY_COMPUTED)) {
|
||||
if (node_dispatch(node, UI_EVENT_GET_EXTENTS, 0, &extents) == UI_NODE_COMPUTED_EXTENTS_CACHED) {
|
||||
// Whoever returned UI_NODE_COMPUTED_EXTENTS_CACHED has the responsibility to set
|
||||
// cached_computed_extents back to false when the cache is no longer valid.
|
||||
node->cached_computed_extents = true;
|
||||
}
|
||||
}
|
||||
|
||||
rect.w = node->width_policy == UI_SIZE_POLICY_COMPUTED ? extents.w : rect.w;
|
||||
rect.h = node->height_policy == UI_SIZE_POLICY_COMPUTED ? extents.h : rect.h;
|
||||
|
||||
return rect;
|
||||
}
|
||||
|
||||
int node_send(UINode *node, enum UIEvent ev, size_t d, void *p)
|
||||
{
|
||||
if (node->flags & UI_NODE_DISABLED) {
|
||||
|
@ -54,7 +36,7 @@ int node_dispatch(UINode *node, enum UIEvent ev, size_t d, void *p)
|
|||
}
|
||||
if (node->nodes[i]->flags & UI_NODE_COMPONENT) {
|
||||
component_index = i;
|
||||
result = node_send(node->nodes[i], ev, d, p);
|
||||
result = node_dispatch(node->nodes[i], ev, d, p);
|
||||
if (result) break;
|
||||
}
|
||||
}
|
||||
|
@ -96,12 +78,11 @@ void node_init(UINode *node)
|
|||
node->nodes_count = 0;
|
||||
node->nodes_capacity = 0;
|
||||
node->flags = 0;
|
||||
node->width_policy = UI_SIZE_POLICY_COMPUTED;
|
||||
node->height_policy = UI_SIZE_POLICY_COMPUTED;
|
||||
node->width_policy = UI_SIZE_POLICY_DYNAMIC;
|
||||
node->height_policy = UI_SIZE_POLICY_DYNAMIC;
|
||||
node->handle = NULL;
|
||||
node->handle_proto = NULL;
|
||||
node->last_component_index = UI_NODE_LAST_COMPONENT_INDEX_NONE;
|
||||
node->cached_computed_extents = false;
|
||||
}
|
||||
|
||||
void node_free(UINode *node)
|
||||
|
|
|
@ -6,12 +6,11 @@
|
|||
#include "rect.h"
|
||||
#include <cairo.h>
|
||||
|
||||
#define UI_NODE_COMPUTED_EXTENTS_OK (1)
|
||||
#define UI_NODE_COMPUTED_EXTENTS_CACHED (2)
|
||||
enum UIEvent {
|
||||
UI_EVENT_REPAINT,
|
||||
UI_EVENT_RELAYOUT,
|
||||
UI_EVENT_GET_EXTENTS,
|
||||
UI_EVENT_GET_WIDTH,
|
||||
UI_EVENT_GET_HEIGHT,
|
||||
UI_EVENT_HOVERED,
|
||||
UI_EVENT_UNHOVERED,
|
||||
UI_EVENT_PRESSED,
|
||||
|
@ -27,7 +26,7 @@ enum UIEvent {
|
|||
};
|
||||
|
||||
enum UISizePolicy {
|
||||
UI_SIZE_POLICY_COMPUTED,
|
||||
UI_SIZE_POLICY_DYNAMIC,
|
||||
UI_SIZE_POLICY_GROW,
|
||||
UI_SIZE_POLICY_FIXED,
|
||||
UI_SIZE_POLICY_STATIC
|
||||
|
@ -57,13 +56,11 @@ typedef struct UINode {
|
|||
enum UISizePolicy width_policy;
|
||||
enum UISizePolicy height_policy;
|
||||
int last_component_index;
|
||||
bool cached_computed_extents;
|
||||
|
||||
int (*handle)(struct UINode *node, enum UIEvent ev, size_t d, void *p);
|
||||
int (*handle_proto)(struct UINode *node, enum UIEvent ev, size_t d, void *p);
|
||||
} UINode;
|
||||
|
||||
UIRect node_get_computed_rect(UINode *node);
|
||||
int node_send(UINode *node, enum UIEvent ev, size_t d, void *p);
|
||||
int node_dispatch(UINode *node, enum UIEvent ev, size_t d, void *p);
|
||||
UINode *node_by_point(UINode *root, double x, double y);
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
#include "scrollable-node.h"
|
||||
#include "node.h"
|
||||
#include "rect.h"
|
||||
#include "window.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -44,16 +42,26 @@ int scrollable_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
|
|||
if (n->y_scroll > scroll_end) {
|
||||
n->y_scroll = scroll_end;
|
||||
}
|
||||
target->rect.x = n->x_scroll ? -n->x_scroll : 0;
|
||||
target->rect.y = n->y_scroll ? -n->y_scroll : 0;
|
||||
window_invalidate_node(node->window, node);
|
||||
node_request_relayout(n->node.parent);
|
||||
break;
|
||||
}
|
||||
case UI_EVENT_RELAYOUT: {
|
||||
UIRect rect = {0};
|
||||
node_dispatch(target, UI_EVENT_GET_EXTENTS, 0, &rect);
|
||||
target->rect.w = target->width_policy == UI_SIZE_POLICY_GROW ? node->parent->rect.w : rect.w;
|
||||
target->rect.h = target->height_policy == UI_SIZE_POLICY_GROW ? node->parent->rect.h : rect.h;
|
||||
double w = target->rect.w;
|
||||
double h = target->rect.h;
|
||||
if (target->width_policy == UI_SIZE_POLICY_DYNAMIC) {
|
||||
node_dispatch(target, UI_EVENT_GET_WIDTH, 0, &w);
|
||||
target->rect.w = w;
|
||||
} else if (target->width_policy == UI_SIZE_POLICY_GROW) {
|
||||
target->rect.w = node->parent->rect.w;
|
||||
}
|
||||
if (target->height_policy == UI_SIZE_POLICY_DYNAMIC) {
|
||||
node_dispatch(target, UI_EVENT_GET_HEIGHT, 0, &h);
|
||||
target->rect.h = h;
|
||||
} else if (target->height_policy == UI_SIZE_POLICY_GROW) {
|
||||
target->rect.h = node->parent->rect.h;
|
||||
}
|
||||
target->rect.x = n->x_scroll ? -n->x_scroll : 0;
|
||||
target->rect.y = n->y_scroll ? -n->y_scroll : 0;
|
||||
node_dispatch(target, UI_EVENT_RELAYOUT, 0, NULL);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
#include "node.h"
|
||||
#include "pango/pango-layout.h"
|
||||
#include "pango/pango-types.h"
|
||||
#include "rect.h"
|
||||
#include "text-node.h"
|
||||
|
||||
UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA color, char *text)
|
||||
|
@ -18,6 +17,9 @@ UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA col
|
|||
n->color = color;
|
||||
n->caret_index = -1;
|
||||
n->caret_node = NULL;
|
||||
n->computed_dimensions_invalid = true;
|
||||
n->computed_text_width = 0;
|
||||
n->computed_text_height = 0;
|
||||
n->node.text = "text";
|
||||
n->node.handle_proto = text_node_handle;
|
||||
node_attach(parent, (UINode*)n);
|
||||
|
@ -48,7 +50,7 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
|
|||
n->layout = layout;
|
||||
n->pending_text = NULL;
|
||||
}
|
||||
node->parent->cached_computed_extents = false;
|
||||
n->computed_dimensions_invalid = true;
|
||||
}
|
||||
|
||||
switch (ev) {
|
||||
|
@ -61,21 +63,28 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
|
|||
cairo_fill(n->node.drw);
|
||||
break;
|
||||
}
|
||||
case UI_EVENT_GET_EXTENTS: {
|
||||
if (!n->layout) {
|
||||
break;
|
||||
}
|
||||
case UI_EVENT_GET_WIDTH: /* through */
|
||||
case UI_EVENT_GET_HEIGHT: {
|
||||
if (n->computed_dimensions_invalid) {
|
||||
int w, h = 0;
|
||||
pango_layout_get_size(n->layout, &w, &h);
|
||||
|
||||
((UIRect*)p)->w = pango_units_to_double(w);
|
||||
((UIRect*)p)->h = pango_units_to_double(h);
|
||||
return UI_NODE_COMPUTED_EXTENTS_CACHED;
|
||||
n->computed_text_width = pango_units_to_double(w);
|
||||
n->computed_text_height = pango_units_to_double(h);
|
||||
n->computed_dimensions_invalid = false;
|
||||
}
|
||||
if (ev == UI_EVENT_GET_WIDTH) {
|
||||
*(double*)p = n->computed_text_width;
|
||||
}
|
||||
if (ev == UI_EVENT_GET_HEIGHT) {
|
||||
*(double*)p = n->computed_text_height;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case UI_EVENT_RELAYOUT: {
|
||||
if (!n->layout) {
|
||||
break;
|
||||
}
|
||||
pango_cairo_update_layout(n->node.drw, n->layout);
|
||||
if (n->caret_index >= 0) {
|
||||
if (!n->caret_node) {
|
||||
n->caret_node = node_new(&n->node, "caret");
|
||||
|
|
|
@ -15,6 +15,9 @@ typedef struct UITextNode {
|
|||
UIRGBA color;
|
||||
ssize_t caret_index;
|
||||
UINode *caret_node;
|
||||
double computed_text_width;
|
||||
double computed_text_height;
|
||||
bool computed_dimensions_invalid;
|
||||
} UITextNode;
|
||||
|
||||
int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p);
|
||||
|
|
|
@ -428,7 +428,7 @@ static void window_process_xcb_event(UIWindow *window, xcb_generic_event_t *even
|
|||
node_dispatch(window->root, UI_EVENT_RELAYOUT, 0, NULL);
|
||||
window_invalidate_node(window, window->root);
|
||||
#ifdef _UI_DEBUG
|
||||
//node_dump(window->root, 0);
|
||||
node_dump(window->root, 0);
|
||||
#endif
|
||||
}
|
||||
end_clock();
|
||||
|
@ -497,7 +497,6 @@ static void window_process_xcb_event(UIWindow *window, xcb_generic_event_t *even
|
|||
}
|
||||
}
|
||||
|
||||
if (!(ui_event == UI_EVENT_SCROLL && !state)) {
|
||||
UINode *n = node;
|
||||
while (n) {
|
||||
if (node_dispatch(n, ui_event, state, &delta) > 0) {
|
||||
|
@ -505,7 +504,6 @@ static void window_process_xcb_event(UIWindow *window, xcb_generic_event_t *even
|
|||
}
|
||||
n = n->parent;
|
||||
}
|
||||
}
|
||||
|
||||
if (ui_event == UI_EVENT_BUTTON_LEFT_UPDATE) {
|
||||
if (state) {
|
||||
|
|
Loading…
Reference in a new issue