allow for centering on box layout

This commit is contained in:
hippoz 2023-05-24 22:59:25 +03:00
parent 8b56dcd3dd
commit f4fcca122b
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
2 changed files with 16 additions and 2 deletions

View file

@ -118,7 +118,11 @@ int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p)
x += gap; x += gap;
} }
current->rect.x = x; current->rect.x = x;
current->rect.y = y; if (box_layout_node->justify_secondary_dimension == UI_BOX_LAYOUT_JUSTIFY_CENTER) {
current->rect.y = margin_top + (maximum_secondary_position - current->rect.h) / 2;
} else {
current->rect.y = y;
}
x += current->rect.w; x += current->rect.w;
} else { } else {
if (current->height_policy == UI_SIZE_POLICY_GROW) { if (current->height_policy == UI_SIZE_POLICY_GROW) {
@ -130,7 +134,11 @@ int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p)
if (current_index) { if (current_index) {
y += gap; y += gap;
} }
current->rect.x = x; if (box_layout_node->justify_secondary_dimension == UI_BOX_LAYOUT_JUSTIFY_CENTER) {
current->rect.x = margin_left + (maximum_secondary_position - current->rect.w) / 2;
} else {
current->rect.x = x;
}
current->rect.y = y; current->rect.y = y;
y += current->rect.h; y += current->rect.h;
} }

View file

@ -3,9 +3,15 @@
#include "node.h" #include "node.h"
enum UIBoxLayoutJustify {
UI_BOX_LAYOUT_JUSTIFY_START,
UI_BOX_LAYOUT_JUSTIFY_CENTER
};
typedef struct UIBoxLayoutNode { typedef struct UIBoxLayoutNode {
UINode node; UINode node;
enum UIDirection direction; enum UIDirection direction;
enum UIBoxLayoutJustify justify_secondary_dimension;
double margin_top, margin_left, margin_bottom, margin_right, gap; double margin_top, margin_left, margin_bottom, margin_right, gap;
} UIBoxLayoutNode; } UIBoxLayoutNode;