28 const std::vector<FlexItemSpec> &items) {
30 res.
items.resize(items.size());
31 if (availMain < 0.f) availMain = 0.f;
32 if (availCross < 0.f) availCross = 0.f;
34 const int n = int(items.size());
35 std::vector<float> mainSize(
size_t(
n), 0.f);
36 std::vector<float> crossSize(
size_t(
n), 0.f);
37 std::vector<float> mainPos(
size_t(
n), 0.f);
38 std::vector<float> crossPos(
size_t(
n), 0.f);
40 float flowTotal = 0.f;
44 for (
int i = 0; i <
n; ++i) {
48 : (
s.percentMain > 0.f
49 ?
s.percentMain * availMain
50 : (
s.flexBasis >= 0.f ?
s.flexBasis :
s.basisMain));
51 m = clampV(
m,
s.minMain,
s.maxMain);
52 float c =
s.explicitCross > 0.f
54 : (
s.percentCross > 0.f ?
s.percentCross * availCross :
s.basisCross);
55 c = clampV(
c,
s.minCross,
s.maxCross);
56 if (
s.aspectRatio > 0.f &&
s.explicitCross <= 0.f &&
s.percentCross <= 0.f)
58 mainSize[size_t(i)] =
m;
59 crossSize[size_t(i)] =
c;
61 if (
s.absolute)
continue;
62 growSum += effectiveGrow(
s);
63 flowTotal +=
m +
s.marginBefore +
s.marginAfter;
67 const int gapCount = std::max(0, flowCount - 1);
68 float total = flowTotal + float(gapCount) *
gap;
69 float freeSpace = availMain - total;
70 if (freeSpace < 0.f) {
71 const float deficit = -freeSpace;
72 float shrinkWeight = 0.f;
73 for (
int i = 0; i <
n; ++i) {
75 if (!
s.absolute &&
s.flexShrink > 0.f)
78 if (shrinkWeight > 0.f) {
79 for (
int i = 0; i <
n; ++i) {
81 if (
s.absolute ||
s.flexShrink <= 0.f)
continue;
82 const float share = deficit *
83 (
s.flexShrink * mainSize[size_t(i)] / shrinkWeight);
84 mainSize[size_t(i)] = clampV(std::max(0.f, mainSize[
size_t(i)] - share),
85 s.minMain,
s.maxMain);
88 float shrunkTotal = float(gapCount) *
gap;
89 for (
int i = 0; i <
n; ++i) {
92 shrunkTotal += mainSize[size_t(i)] +
s.
marginBefore +
s.marginAfter;
94 res.
overflowMain = std::max(0.f, shrunkTotal - availMain);
101 for (
int i = 0; i <
n; ++i) {
103 if (
s.absolute)
continue;
104 const float g = effectiveGrow(
s);
105 if (
g > 0.f) mainSize[size_t(i)] += freeSpace * (
g / growSum);
107 }
else if (freeSpace > 0.f && flowCount > 0) {
110 leading = freeSpace * 0.5f;
116 if (gapCount > 0) between =
gap + freeSpace / float(gapCount);
120 const float edge = freeSpace / float(flowCount * 2);
132 for (
int i = 0; i <
n; ++i) {
136 s.
anchorMain * availMain +
s.posMain -
s.anchorMain * mainSize[size_t(i)];
139 mainPos[size_t(i)] = cur +
s.marginBefore;
140 cur +=
s.marginBefore + mainSize[size_t(i)] +
s.marginAfter + between;
142 const float contentMain = flowCount > 0 ? cur - between : 0.f;
144 float contentCross = 0.f;
145 for (
int i = 0; i <
n; ++i) {
148 crossPos[size_t(i)] =
149 s.
anchorCross * availCross +
s.posCross -
s.anchorCross * crossSize[size_t(i)];
150 contentCross = std::max(contentCross,
151 crossPos[
size_t(i)] + crossSize[
size_t(i)] +
155 const float cb =
s.marginCrossBefore;
156 const float ca =
s.marginCrossAfter;
158 s.alignSelf >= 0 ?
FlexAlign(
s.alignSelf) : containerAlign;
160 if (stretch &&
s.explicitCross <= 0.f &&
s.percentCross <= 0.f) {
161 float c = availCross - cb - ca;
162 if (
c < 0.f)
c = 0.f;
163 crossSize[size_t(i)] =
c;
167 crossPos[size_t(i)] = (availCross - crossSize[size_t(i)] - cb - ca) * 0.5f + cb;
170 crossPos[size_t(i)] = availCross - crossSize[size_t(i)] - ca;
175 crossPos[size_t(i)] = cb;
178 contentCross = std::max(contentCross,
179 crossPos[
size_t(i)] + crossSize[
size_t(i)] + ca);
182 for (
int i = 0; i <
n; ++i) {
185 r.x = mainPos[size_t(i)];
186 r.y = crossPos[size_t(i)];
187 r.w = mainSize[size_t(i)];
188 r.h = crossSize[size_t(i)];
190 r.x = crossPos[size_t(i)];
191 r.y = mainPos[size_t(i)];
192 r.w = crossSize[size_t(i)];
193 r.h = mainSize[size_t(i)];
204 const std::vector<FlexItemSpec> &items,
bool wrap,
float crossGap) {
211 if (crossGap < 0.f) crossGap =
gap;
212 std::vector<size_t> lineIndices;
213 float lineOuterMain = 0.f;
214 float crossOffset = 0.f;
216 auto flushLine = [&]() {
217 if (lineIndices.empty())
return;
218 std::vector<FlexItemSpec>
line;
219 line.reserve(lineIndices.size());
220 float lineCross = 0.f;
221 for (
size_t index : lineIndices) {
223 const auto &
s = items[
index];
224 float cross =
s.explicitCross > 0.f ?
s.explicitCross :
s.basisCross;
225 float main =
s.explicitMain > 0.f
227 : (
s.flexBasis >= 0.f ?
s.flexBasis :
s.basisMain);
228 if (
s.aspectRatio > 0.f &&
s.explicitCross <= 0.f &&
s.percentCross <= 0.f)
230 lineCross = std::max(lineCross,
231 cross +
s.marginCrossBefore +
s.marginCrossAfter);
234 row,
gap, availMain, lineCross, containerAlign, justify,
line);
235 for (
size_t i = 0; i < lineIndices.size(); ++i) {
237 if (
row) rect.
y += crossOffset;
238 else rect.
x += crossOffset;
239 result.items[lineIndices[i]] = rect;
244 : crossOffset + lineCross;
245 result.contentH =
row ? crossOffset + lineCross
246 : std::max(
result.contentH, lineMain);
247 crossOffset += lineCross + crossGap;
252 for (
size_t i = 0; i < items.size(); ++i) {
256 row, 0.f, availMain, availCross, containerAlign, justify, {
s});
260 float main =
s.explicitMain > 0.f
262 : (
s.percentMain > 0.f
263 ?
s.percentMain * availMain
264 : (
s.flexBasis >= 0.f ?
s.flexBasis :
s.basisMain));
265 main = clampV(
main,
s.minMain,
s.maxMain) +
s.marginBefore +
s.marginAfter;
266 const float next = lineOuterMain + (lineIndices.empty() ? 0.f :
gap) +
main;
267 if (!lineIndices.empty() && next > availMain) flushLine();
268 lineOuterMain += (lineIndices.empty() ? 0.f :
gap) +
main;
269 lineIndices.push_back(i);
278 const std::vector<GridItemSpec> &items) {
282 columnGap = std::max(0.f, columnGap);
283 rowGap = std::max(0.f, rowGap);
284 const float gaps = float(
columns - 1) * columnGap;
285 const float cellWidth = std::max(0.f, (availWidth - gaps) /
float(
columns));
286 struct Slot {
int row = 0;
int column = 0;
int span = 1; };
287 std::vector<Slot>
slots(items.size());
288 std::vector<float> rowHeights;
291 for (
size_t i = 0; i < items.size(); ++i) {
292 const int span = std::clamp(items[i].columnSpan, 1,
columns);
297 if (
row >=
int(rowHeights.size())) rowHeights.resize(
size_t(
row + 1), 0.f);
299 const float width = cellWidth * float(span) + columnGap * float(span - 1) -
300 items[i].
marginL - items[i].marginR;
301 float height = items[i].basisH;
303 rowHeights[size_t(
row)] =
304 std::max(rowHeights[
size_t(
row)],
height + items[i].marginT + items[i].marginB);
311 std::vector<float> rowY(rowHeights.size(), 0.f);
312 for (
size_t r = 1;
r < rowHeights.size(); ++
r)
313 rowY[
r] = rowY[
r - 1] + rowHeights[
r - 1] + rowGap;
314 for (
size_t i = 0; i < items.size(); ++i) {
316 const float outerW = cellWidth * float(
slot.span) + columnGap * float(
slot.span - 1);
318 rect.
x = float(
slot.column) * (cellWidth + columnGap) + items[i].marginL;
319 rect.
y = rowY[size_t(
slot.row)] + items[i].marginT;
320 rect.
w = std::max(0.f, outerW - items[i].marginL - items[i].marginR);
321 rect.
h = items[i].aspectRatio > 0.f
322 ? rect.
w / items[i].aspectRatio
323 : std::max(0.f, items[i].basisH);
325 result.contentW = std::max(0.f, availWidth);
326 if (!rowHeights.empty())
327 result.contentH = rowY.back() + rowHeights.back();
328 result.overflowX = std::max(0.f, gaps - availWidth);
371 if (index < 0 || index >=
int(tree.
nodes.size()))
return;
375 const ImGuiStyle &style = ImGui::GetStyle();
379 if (
n.fontSize > 0.f) {
380 const ImVec2 textSize = ImGui::GetFont()->CalcTextSizeA(
n.fontSize, FLT_MAX,
n.wrapWidth,
n.text.c_str());
381 n.measuredW = textSize.x;
382 n.measuredH = textSize.y;
385 const ImVec2
t =
n.wrapWidth > 0.f
386 ? ImGui::CalcTextSize(
n.text.c_str(),
nullptr,
false,
n.wrapWidth)
387 : ImGui::CalcTextSize(
n.text.c_str());
395 n.measuredH = ImGui::GetFrameHeight();
399 const ImVec2
t = ImGui::CalcTextSize(
n.text.empty() ?
"Link" :
n.text.c_str());
405 const char *
label =
n.text.empty() ?
"Button" :
n.text.c_str();
406 const ImVec2
t = ImGui::CalcTextSize(
label);
407 n.measuredW =
t.x + style.FramePadding.x * 2.f;
408 n.measuredH = ImGui::GetFrameHeight();
412 const char *
label =
n.text.empty() ?
"Check" :
n.text.c_str();
413 const ImVec2
t = ImGui::CalcTextSize(
label);
414 n.measuredW = ImGui::GetFrameHeight() + style.ItemInnerSpacing.x +
t.x;
415 n.measuredH = ImGui::GetFrameHeight();
419 const ImVec2
t = ImGui::CalcTextSize(
n.text.c_str());
420 const float trackW = ImGui::GetFrameHeight() * 1.7f;
421 n.measuredW = trackW + (
n.text.empty() ? 0.f : style.ItemInnerSpacing.x +
t.x);
422 n.measuredH = ImGui::GetFrameHeight();
426 const ImVec2
t = ImGui::CalcTextSize(
n.text.c_str());
427 n.measuredW =
t.x + style.FramePadding.x * 2.f;
428 n.measuredH =
t.y + 6.f;
433 n.measuredH =
n.multiline ? 96.f : ImGui::GetFrameHeight();
437 n.measuredH = ImGui::GetFrameHeight();
440 const float swatch = ImGui::GetFrameHeight();
441 const float gap = 4.f;
444 if (!
n.valueText.empty()) {
445 for (
size_t i = 0; i <
n.valueText.size();) {
446 size_t end =
n.valueText.find_first_of(
";\n", i);
447 if (
end == std::string::npos)
end =
n.valueText.size();
454 n.measuredW =
n.sizeX > 0.f ?
n.sizeX : 220.f;
455 n.measuredH = ImGui::GetFrameHeight() + style.ItemSpacing.y +
456 float(
rows) * swatch + float(std::max(0,
rows - 1)) *
gap;
461 n.measuredH = ImGui::GetFrameHeight();
467 n.measuredW =
n.sizeX > 0.f ?
n.sizeX : 32.f;
468 n.measuredH =
n.sizeY > 0.f ?
n.sizeY : 32.f;
471 n.measuredW = style.ItemSpacing.x;
475 n.measuredW =
n.sizeX > 0.f ?
n.sizeX : 0.f;
476 n.measuredH =
n.sizeY > 0.f ?
n.sizeY : 0.f;
479 n.measuredW =
n.sizeX > 0.f ?
n.sizeX : 80.f;
480 n.measuredH =
n.sizeY > 0.f ?
n.sizeY : 120.f;
483 n.measuredW =
n.sizeX > 0.f ?
n.sizeX : 0.f;
484 n.measuredH =
n.sizeY > 0.f ?
n.sizeY : 120.f;
487 n.measuredW =
n.sizeX > 0.f ?
n.sizeX : 0.f;
488 n.measuredH =
n.sizeY > 0.f ?
n.sizeY : 240.f;
491 const char *
label =
n.text.empty() ?
"Section" :
n.text.c_str();
492 const ImVec2
t = ImGui::CalcTextSize(
label);
493 n.measuredW =
t.x + style.FramePadding.x * 2.f + 18.f;
494 n.measuredH = ImGui::GetFrameHeight();
496 float w = 0.f,
h = 0.f;
498 n.measuredW = std::max(
n.measuredW,
w);
504 const ImVec2
t = ImGui::CalcTextSize(
n.text.c_str());
505 n.measuredW =
t.x + 10.f;
506 n.measuredH = std::max(ImGui::GetFrameHeight(),
t.y + style.ItemSpacing.y) +
511 const ImVec2
label = ImGui::CalcTextSize(
n.text.c_str());
512 const ImVec2 shortcut = ImGui::CalcTextSize(
n.valueText.c_str());
513 n.measuredW =
label.x + shortcut.x + style.ItemSpacing.x * 4.f;
514 n.measuredH = ImGui::GetFrameHeight();
518 const ImVec2
t = ImGui::CalcTextSize(
n.text.c_str());
519 n.measuredW =
t.x + style.FramePadding.x * 2.f;
520 n.measuredH = ImGui::GetFrameHeight();
525 float w = 0.f,
h = 0.f;
526 for (
int c =
n.firstChild;
c >= 0;
c = tree.
nodes[size_t(
c)].nextSibling) {
531 h = std::max(
h,
child.measuredH);
534 const float defaultHeight =
538 n.measuredH =
n.sizeY > 0.f ?
n.sizeY : defaultHeight;
543 for (
int c =
n.firstChild;
c >= 0;
c = tree.
nodes[size_t(
c)].nextSibling) {
547 const float cell =
n.itemHeight > 0.f
550 const int cols = int(
n.value) > 0 ? int(
n.value) : std::max(1, std::min(4,
count));
552 n.measuredW = float(
cols) *
cell + float(std::max(0,
cols - 1)) * style.ItemSpacing.x;
553 n.measuredH = float(
rows) *
cell + float(std::max(0,
rows - 1)) * style.ItemSpacing.y;
557 float w = 0.f,
h = 0.f;
560 n.measuredW =
n.sizeX > 0.f ?
n.sizeX : std::max(defaultWidth,
w);
561 n.measuredH =
n.sizeY > 0.f ?
n.sizeY :
h;
573 n.measuredH = std::max(
first >= 0 ? tree.
nodes[
size_t(
first)].measuredH : 0.f,
576 n.measuredW = std::max(
first >= 0 ? tree.
nodes[
size_t(
first)].measuredW : 0.f,
585 const ImVec2
t = ImGui::CalcTextSize(
n.text.c_str());
586 n.measuredW =
t.x + style.FramePadding.x * 4.f;
587 n.measuredH = ImGui::GetFrameHeight();
591 float w = 0.f,
h = 0.f;
592 for (
int c =
n.firstChild;
c >= 0;
c = tree.
nodes[size_t(
c)].nextSibling) {
597 h = std::max(
h,
child.measuredH);
601 n.measuredH =
n.sizeY > 0.f ?
n.sizeY : defaultHeight;
608 float w = 0.f,
h = 0.f;
609 for (
int c =
n.firstChild;
c >= 0;
c = tree.
nodes[size_t(
c)].nextSibling) {
613 w = std::max(
w,
child.measuredW);
619 n.measuredW =
w +
n.paddingL +
n.paddingR;
620 n.measuredH =
h +
n.paddingT +
n.paddingB;
627 float w = 0.f,
h = 0.f;
629 n.measuredW =
w +
n.paddingL +
n.paddingR;
630 n.measuredH =
h +
n.paddingT +
n.paddingB;
634 std::vector<GridItemSpec> items;
635 float maxCellWidth = 0.f;
636 for (
int c =
n.firstChild;
c >= 0;
c = tree.
nodes[size_t(
c)].nextSibling) {
649 items.push_back(item);
650 maxCellWidth = std::max(maxCellWidth,
654 const int columns = std::max(1,
n.gridColumns);
655 const float columnGap =
n.columnGap >= 0.f
657 : (
n.gap >= 0.f ?
n.gap : style.ItemSpacing.x);
658 const float rowGap =
n.rowGap >= 0.f
660 : (
n.gap >= 0.f ?
n.gap : style.ItemSpacing.y);
661 const float naturalWidth = maxCellWidth * float(
columns) +
662 columnGap * float(
columns - 1);
665 n.measuredW = arranged.
contentW +
n.paddingL +
n.paddingR;
666 n.measuredH = arranged.
contentH +
n.paddingT +
n.paddingB;
672 float crossMax = 0.f;
674 for (
int c =
n.firstChild;
c >= 0;
c = tree.
nodes[size_t(
c)].nextSibling) {
685 mainSum +=
m + mb + ma;
686 crossMax = std::max(crossMax, cm + cb + ca);
689 const float gap =
n.gap >= 0.f ?
n.gap : (
row ? style.ItemSpacing.x : style.ItemSpacing.y);
690 const float padMain =
row ?
n.
paddingL +
n.paddingR :
n.paddingT +
n.paddingB;
691 const float padCross =
row ?
n.
paddingT +
n.paddingB :
n.paddingL +
n.paddingR;
692 const float mainSize = mainSum + float(std::max(0,
count - 1)) *
gap + padMain;
693 const float crossSize = crossMax + padCross;
694 n.measuredW =
row ? mainSize : crossSize;
695 n.measuredH =
row ? crossSize : mainSize;
699 float w = 0.f,
h = 0.f;
701 n.measuredW =
w +
n.paddingL +
n.paddingR;
702 n.measuredH =
h +
n.paddingT +
n.paddingB;
711 if (
n.sizeX > 0.f)
n.measuredW =
n.sizeX;
712 if (
n.sizeY > 0.f)
n.measuredH =
n.sizeY;
714 if (
n.aspectRatio > 0.f) {
715 if (
n.sizeX > 0.f &&
n.sizeY <= 0.f)
n.measuredH =
n.measuredW /
n.aspectRatio;
716 else if (
n.sizeY > 0.f &&
n.sizeX <= 0.f)
n.measuredW =
n.measuredH *
n.aspectRatio;
718 if (!
n.helpText.empty()) {
719 const ImVec2 help = ImGui::CalcTextSize(
n.helpText.c_str());
720 n.measuredW = std::max(
n.measuredW, help.x);
721 n.measuredH += style.ItemSpacing.y + help.y;
723 if (
n.minSizeX > 0.f)
n.measuredW = std::max(
n.measuredW,
n.minSizeX);
724 if (
n.minSizeY > 0.f)
n.measuredH = std::max(
n.measuredH,
n.minSizeY);
725 if (
n.maxSizeX > 0.f)
n.measuredW = std::min(
n.measuredW,
n.maxSizeX);
726 if (
n.maxSizeY > 0.f)
n.measuredH = std::min(
n.measuredH,
n.maxSizeY);
int main(int argc, char **argv)
FlexResult flexArrangeSingleLine(bool row, float gap, float availMain, float availCross, FlexAlign containerAlign, FlexJustify justify, const std::vector< FlexItemSpec > &items)
GridResult gridArrange(int columns, float columnGap, float rowGap, float availWidth, const std::vector< GridItemSpec > &items)
Places source-ordered items into equal-width fixed columns.
WidgetDesc child(std::string id, std::vector< WidgetDesc > children, float width, float height)
Scrollable child region with an explicit size.
FlexResult flexArrange(bool row, float gap, float availMain, float availCross, FlexAlign containerAlign, FlexJustify justify, const std::vector< FlexItemSpec > &items, bool wrap, float crossGap)
Flex arrange.