From f4fcca122bbceded903ca26218add39f14a13fa8 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Wed, 24 May 2023 22:59:25 +0300 Subject: [PATCH] allow for centering on box layout --- src/box-layout-node.c | 12 ++++++++++-- src/box-layout-node.h | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/box-layout-node.c b/src/box-layout-node.c index 7b6dbc9..513c736 100644 --- a/src/box-layout-node.c +++ b/src/box-layout-node.c @@ -118,7 +118,11 @@ int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p) x += gap; } 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; } else { 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) { 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; y += current->rect.h; } diff --git a/src/box-layout-node.h b/src/box-layout-node.h index 078f409..a8fc793 100644 --- a/src/box-layout-node.h +++ b/src/box-layout-node.h @@ -3,9 +3,15 @@ #include "node.h" +enum UIBoxLayoutJustify { + UI_BOX_LAYOUT_JUSTIFY_START, + UI_BOX_LAYOUT_JUSTIFY_CENTER +}; + typedef struct UIBoxLayoutNode { UINode node; enum UIDirection direction; + enum UIBoxLayoutJustify justify_secondary_dimension; double margin_top, margin_left, margin_bottom, margin_right, gap; } UIBoxLayoutNode;