载入中...
搜索中...
未找到
Layout.cpp
浏览该文件的文档.
1#include "ui/Layout.h"
2#include "ui/Theme.h"
3
4#include <imgui.h>
5
6#include <algorithm>
7#include <cfloat>
8
9namespace eve::ui {
10namespace {
11
12float clampV(float v, float lo, float hi) {
13 if (lo > 0.f) v = std::max(v, lo);
14 if (hi > 0.f) v = std::min(v, hi);
15 return v;
16}
17
18float effectiveGrow(const FlexItemSpec &s) {
19 float g = s.flexGrow;
20 if (s.isSpacer && g <= 0.f) g = 1.f;
21 return std::max(0.f, g);
22}
23
24} // namespace
25
26FlexResult flexArrangeSingleLine(bool row, float gap, float availMain, float availCross,
27 FlexAlign containerAlign, FlexJustify justify,
28 const std::vector<FlexItemSpec> &items) {
29 FlexResult res;
30 res.items.resize(items.size());
31 if (availMain < 0.f) availMain = 0.f;
32 if (availCross < 0.f) availCross = 0.f;
33
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);
39
40 float flowTotal = 0.f;
41 int flowCount = 0;
42 float growSum = 0.f;
43
44 for (int i = 0; i < n; ++i) {
45 const FlexItemSpec &s = items[size_t(i)];
46 float m = s.explicitMain > 0.f
47 ? s.explicitMain
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
53 ? s.explicitCross
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)
57 c = row ? m / s.aspectRatio : m * s.aspectRatio;
58 mainSize[size_t(i)] = m;
59 crossSize[size_t(i)] = c;
60
61 if (s.absolute) continue;
62 growSum += effectiveGrow(s);
63 flowTotal += m + s.marginBefore + s.marginAfter;
64 ++flowCount;
65 }
66
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) {
74 const FlexItemSpec &s = items[size_t(i)];
75 if (!s.absolute && s.flexShrink > 0.f)
76 shrinkWeight += s.flexShrink * mainSize[size_t(i)];
77 }
78 if (shrinkWeight > 0.f) {
79 for (int i = 0; i < n; ++i) {
80 const FlexItemSpec &s = items[size_t(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);
86 }
87 }
88 float shrunkTotal = float(gapCount) * gap;
89 for (int i = 0; i < n; ++i) {
90 const FlexItemSpec &s = items[size_t(i)];
91 if (!s.absolute)
92 shrunkTotal += mainSize[size_t(i)] + s.marginBefore + s.marginAfter;
93 }
94 res.overflowMain = std::max(0.f, shrunkTotal - availMain);
95 freeSpace = 0.f;
96 }
97
98 float leading = 0.f;
99 float between = gap;
100 if (growSum > 0.f) {
101 for (int i = 0; i < n; ++i) {
102 const FlexItemSpec &s = items[size_t(i)];
103 if (s.absolute) continue;
104 const float g = effectiveGrow(s);
105 if (g > 0.f) mainSize[size_t(i)] += freeSpace * (g / growSum);
106 }
107 } else if (freeSpace > 0.f && flowCount > 0) {
108 switch (justify) {
110 leading = freeSpace * 0.5f;
111 break;
112 case FlexJustify::End:
113 leading = freeSpace;
114 break;
116 if (gapCount > 0) between = gap + freeSpace / float(gapCount);
117 break;
119 if (flowCount > 0) {
120 const float edge = freeSpace / float(flowCount * 2);
121 leading = edge;
122 between = gap + edge * 2.f;
123 }
124 break;
126 default:
127 break;
128 }
129 }
130
131 float cur = leading;
132 for (int i = 0; i < n; ++i) {
133 const FlexItemSpec &s = items[size_t(i)];
134 if (s.absolute) {
135 mainPos[size_t(i)] =
136 s.anchorMain * availMain + s.posMain - s.anchorMain * mainSize[size_t(i)];
137 continue;
138 }
139 mainPos[size_t(i)] = cur + s.marginBefore;
140 cur += s.marginBefore + mainSize[size_t(i)] + s.marginAfter + between;
141 }
142 const float contentMain = flowCount > 0 ? cur - between : 0.f;
143
144 float contentCross = 0.f;
145 for (int i = 0; i < n; ++i) {
146 const FlexItemSpec &s = items[size_t(i)];
147 if (s.absolute) {
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)] +
152 s.marginCrossAfter);
153 continue;
154 }
155 const float cb = s.marginCrossBefore;
156 const float ca = s.marginCrossAfter;
157 const FlexAlign align =
158 s.alignSelf >= 0 ? FlexAlign(s.alignSelf) : containerAlign;
159 const bool stretch = align == FlexAlign::Stretch;
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;
164 }
165 switch (align) {
167 crossPos[size_t(i)] = (availCross - crossSize[size_t(i)] - cb - ca) * 0.5f + cb;
168 break;
169 case FlexAlign::End:
170 crossPos[size_t(i)] = availCross - crossSize[size_t(i)] - ca;
171 break;
172 case FlexAlign::Start:
174 default:
175 crossPos[size_t(i)] = cb;
176 break;
177 }
178 contentCross = std::max(contentCross,
179 crossPos[size_t(i)] + crossSize[size_t(i)] + ca);
180 }
181
182 for (int i = 0; i < n; ++i) {
183 FlexRect &r = res.items[size_t(i)];
184 if (row) {
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)];
189 } else {
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)];
194 }
195 }
196
197 res.contentW = row ? contentMain : contentCross;
198 res.contentH = row ? contentCross : contentMain;
199 return res;
200}
201
202FlexResult flexArrange(bool row, float gap, float availMain, float availCross,
203 FlexAlign containerAlign, FlexJustify justify,
204 const std::vector<FlexItemSpec> &items, bool wrap, float crossGap) {
205 if (!wrap)
206 return flexArrangeSingleLine(row, gap, availMain, availCross, containerAlign, justify,
207 items);
208
210 result.items.resize(items.size());
211 if (crossGap < 0.f) crossGap = gap;
212 std::vector<size_t> lineIndices;
213 float lineOuterMain = 0.f;
214 float crossOffset = 0.f;
215
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) {
222 line.push_back(items[index]);
223 const auto &s = items[index];
224 float cross = s.explicitCross > 0.f ? s.explicitCross : s.basisCross;
225 float main = s.explicitMain > 0.f
226 ? s.explicitMain
227 : (s.flexBasis >= 0.f ? s.flexBasis : s.basisMain);
228 if (s.aspectRatio > 0.f && s.explicitCross <= 0.f && s.percentCross <= 0.f)
229 cross = row ? main / s.aspectRatio : main * s.aspectRatio;
230 lineCross = std::max(lineCross,
231 cross + s.marginCrossBefore + s.marginCrossAfter);
232 }
233 const FlexResult arranged = flexArrangeSingleLine(
234 row, gap, availMain, lineCross, containerAlign, justify, line);
235 for (size_t i = 0; i < lineIndices.size(); ++i) {
236 FlexRect rect = arranged.items[i];
237 if (row) rect.y += crossOffset;
238 else rect.x += crossOffset;
239 result.items[lineIndices[i]] = rect;
240 }
241 result.overflowMain = std::max(result.overflowMain, arranged.overflowMain);
242 const float lineMain = row ? arranged.contentW : arranged.contentH;
243 result.contentW = row ? std::max(result.contentW, lineMain)
244 : crossOffset + lineCross;
245 result.contentH = row ? crossOffset + lineCross
246 : std::max(result.contentH, lineMain);
247 crossOffset += lineCross + crossGap;
248 lineIndices.clear();
249 lineOuterMain = 0.f;
250 };
251
252 for (size_t i = 0; i < items.size(); ++i) {
253 const FlexItemSpec &s = items[i];
254 if (s.absolute) {
255 const FlexResult absolute = flexArrangeSingleLine(
256 row, 0.f, availMain, availCross, containerAlign, justify, {s});
257 result.items[i] = absolute.items[0];
258 continue;
259 }
260 float main = s.explicitMain > 0.f
261 ? s.explicitMain
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);
270 }
271 flushLine();
272 if (row) result.contentH = std::max(0.f, result.contentH);
273 else result.contentW = std::max(0.f, result.contentW);
274 return result;
275}
276
277GridResult gridArrange(int columns, float columnGap, float rowGap, float availWidth,
278 const std::vector<GridItemSpec> &items) {
280 result.items.resize(items.size());
281 columns = std::max(1, columns);
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;
289 int row = 0;
290 int column = 0;
291 for (size_t i = 0; i < items.size(); ++i) {
292 const int span = std::clamp(items[i].columnSpan, 1, columns);
293 if (column + span > columns) {
294 ++row;
295 column = 0;
296 }
297 if (row >= int(rowHeights.size())) rowHeights.resize(size_t(row + 1), 0.f);
298 slots[i] = {row, column, span};
299 const float width = cellWidth * float(span) + columnGap * float(span - 1) -
300 items[i].marginL - items[i].marginR;
301 float height = items[i].basisH;
302 if (items[i].aspectRatio > 0.f) height = std::max(0.f, width) / items[i].aspectRatio;
303 rowHeights[size_t(row)] =
304 std::max(rowHeights[size_t(row)], height + items[i].marginT + items[i].marginB);
305 column += span;
306 if (column >= columns) {
307 ++row;
308 column = 0;
309 }
310 }
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) {
315 const Slot slot = slots[i];
316 const float outerW = cellWidth * float(slot.span) + columnGap * float(slot.span - 1);
317 FlexRect &rect = result.items[i];
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);
324 }
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);
329 return result;
330}
331
332void measureFlowChildren(UIHost::Tree &tree, int firstChild, float *outW, float *outH) {
333 float rowW = 0.f;
334 float rowH = 0.f;
335 float maxW = 0.f;
336 float totalH = 0.f;
337 bool sameRow = false;
338 const ImGuiStyle &style = ImGui::GetStyle();
339 int index = firstChild;
340 while (index >= 0 && index < int(tree.nodes.size())) {
341 UINode &n = tree.nodes[size_t(index)];
342 const int next = n.nextSibling;
343 if (n.visible) {
344 if (n.type == NodeType::SameLine) {
345 sameRow = true;
346 } else {
347 measureNode(tree, index);
348 const float w = n.measuredW + n.marginL + n.marginR;
349 const float h = n.measuredH + n.marginT + n.marginB;
350 if (sameRow) {
351 rowW += style.ItemSpacing.x + w;
352 } else {
353 if (rowH > 0.f) totalH += style.ItemSpacing.y;
354 totalH += rowH;
355 rowW = w;
356 rowH = 0.f;
357 }
358 rowH = std::max(rowH, h);
359 maxW = std::max(maxW, rowW);
360 sameRow = false;
361 }
362 }
363 index = next;
364 }
365 if (rowH > 0.f) totalH += rowH;
366 if (outW) *outW = maxW;
367 if (outH) *outH = totalH;
368}
369
371 if (index < 0 || index >= int(tree.nodes.size())) return;
372 UINode &n = tree.nodes[size_t(index)];
373 n.measuredW = 0.f;
374 n.measuredH = 0.f;
375 const ImGuiStyle &style = ImGui::GetStyle();
376
377 switch (n.type) {
378 case NodeType::Text: {
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;
383 break;
384 }
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());
388 n.measuredW = t.x;
389 n.measuredH = t.y;
390 break;
391 }
392 case NodeType::Combo:
395 n.measuredH = ImGui::GetFrameHeight();
396 break;
397 }
398 case NodeType::Link: {
399 const ImVec2 t = ImGui::CalcTextSize(n.text.empty() ? "Link" : n.text.c_str());
400 n.measuredW = t.x;
401 n.measuredH = t.y;
402 break;
403 }
404 case NodeType::Button: {
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();
409 break;
410 }
411 case NodeType::Checkbox: {
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();
416 break;
417 }
418 case NodeType::Switch: {
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();
423 break;
424 }
425 case NodeType::Badge: {
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;
429 break;
430 }
432 n.measuredW = 120.f;
433 n.measuredH = n.multiline ? 96.f : ImGui::GetFrameHeight();
434 break;
435 case NodeType::Slider:
436 n.measuredW = 120.f;
437 n.measuredH = ImGui::GetFrameHeight();
438 break;
440 const float swatch = ImGui::GetFrameHeight();
441 const float gap = 4.f;
442 const int cols = 8;
443 int count = 0;
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();
448 if (end > i) ++count;
449 i = end + 1;
450 }
451 }
452 if (count <= 0) count = 16;
453 const int rows = (count + cols - 1) / cols;
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;
457 break;
458 }
460 n.measuredW = 100.f;
461 n.measuredH = ImGui::GetFrameHeight();
462 break;
465 case NodeType::Image:
467 n.measuredW = n.sizeX > 0.f ? n.sizeX : 32.f;
468 n.measuredH = n.sizeY > 0.f ? n.sizeY : 32.f;
469 break;
471 n.measuredW = style.ItemSpacing.x;
472 n.measuredH = 1.f;
473 break;
474 case NodeType::Spacer:
475 n.measuredW = n.sizeX > 0.f ? n.sizeX : 0.f;
476 n.measuredH = n.sizeY > 0.f ? n.sizeY : 0.f;
477 break;
478 case NodeType::Child:
479 n.measuredW = n.sizeX > 0.f ? n.sizeX : 80.f;
480 n.measuredH = n.sizeY > 0.f ? n.sizeY : 120.f;
481 break;
483 n.measuredW = n.sizeX > 0.f ? n.sizeX : 0.f;
484 n.measuredH = n.sizeY > 0.f ? n.sizeY : 120.f;
485 break;
487 n.measuredW = n.sizeX > 0.f ? n.sizeX : 0.f;
488 n.measuredH = n.sizeY > 0.f ? n.sizeY : 240.f;
489 break;
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();
495 if (n.open) {
496 float w = 0.f, h = 0.f;
497 measureFlowChildren(tree, n.firstChild, &w, &h);
498 n.measuredW = std::max(n.measuredW, w);
499 n.measuredH += h;
500 }
501 break;
502 }
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) +
508 break;
509 }
510 case NodeType::MenuItem: {
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();
515 break;
516 }
517 case NodeType::Menu: {
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();
521 break;
522 }
524 case NodeType::StatusBar: {
525 float w = 0.f, h = 0.f;
526 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
527 UINode &child = tree.nodes[size_t(c)];
528 if (!child.visible) continue;
529 measureNode(tree, c);
530 w += child.measuredW;
531 h = std::max(h, child.measuredH);
532 }
533 n.measuredW = w;
534 const float defaultHeight =
537 themeUiScale();
538 n.measuredH = n.sizeY > 0.f ? n.sizeY : defaultHeight;
539 break;
540 }
541 case NodeType::Toolbox: {
542 int count = 0;
543 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
544 measureNode(tree, c);
545 if (tree.nodes[size_t(c)].visible) ++count;
546 }
547 const float cell = n.itemHeight > 0.f
548 ? n.itemHeight
550 const int cols = int(n.value) > 0 ? int(n.value) : std::max(1, std::min(4, count));
551 const int rows = cols > 0 ? (count + cols - 1) / cols : 0;
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;
554 break;
555 }
556 case NodeType::Sidebar: {
557 float w = 0.f, h = 0.f;
558 measureFlowChildren(tree, n.firstChild, &w, &h);
559 const float defaultWidth = globalTheme().layout.sidebarWidth * themeUiScale();
560 n.measuredW = n.sizeX > 0.f ? n.sizeX : std::max(defaultWidth, w);
561 n.measuredH = n.sizeY > 0.f ? n.sizeY : h;
562 break;
563 }
564 case NodeType::SplitPane: {
565 int first = n.firstChild;
566 int second = first >= 0 ? tree.nodes[size_t(first)].nextSibling : -1;
567 if (first >= 0) measureNode(tree, first);
568 if (second >= 0) measureNode(tree, second);
569 if (n.flexDirection == FlexDirection::Row) {
570 n.measuredW = (first >= 0 ? tree.nodes[size_t(first)].measuredW : 0.f) +
572 (second >= 0 ? tree.nodes[size_t(second)].measuredW : 0.f);
573 n.measuredH = std::max(first >= 0 ? tree.nodes[size_t(first)].measuredH : 0.f,
574 second >= 0 ? tree.nodes[size_t(second)].measuredH : 0.f);
575 } else {
576 n.measuredW = std::max(first >= 0 ? tree.nodes[size_t(first)].measuredW : 0.f,
577 second >= 0 ? tree.nodes[size_t(second)].measuredW : 0.f);
578 n.measuredH = (first >= 0 ? tree.nodes[size_t(first)].measuredH : 0.f) +
580 (second >= 0 ? tree.nodes[size_t(second)].measuredH : 0.f);
581 }
582 break;
583 }
584 case NodeType::Tab: {
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();
588 break;
589 }
590 case NodeType::TabBar: {
591 float w = 0.f, h = 0.f;
592 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
593 UINode &child = tree.nodes[size_t(c)];
594 if (!child.visible) continue;
595 measureNode(tree, c);
596 w += child.measuredW;
597 h = std::max(h, child.measuredH);
598 }
599 n.measuredW = w;
600 const float defaultHeight = globalTheme().layout.tabBarHeight * themeUiScale();
601 n.measuredH = n.sizeY > 0.f ? n.sizeY : defaultHeight;
602 break;
603 }
607 case NodeType::AppFrame: {
608 float w = 0.f, h = 0.f;
609 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
610 UINode &child = tree.nodes[size_t(c)];
611 if (!child.visible) continue;
612 measureNode(tree, c);
613 w = std::max(w, child.measuredW);
614 h += child.measuredH;
615 }
616 if (n.type == NodeType::PanelFrame) {
618 }
619 n.measuredW = w + n.paddingL + n.paddingR;
620 n.measuredH = h + n.paddingT + n.paddingB;
621 break;
622 }
623 case NodeType::Group:
624 case NodeType::Card:
626 case NodeType::MenuBar: {
627 float w = 0.f, h = 0.f;
628 measureFlowChildren(tree, n.firstChild, &w, &h);
629 n.measuredW = w + n.paddingL + n.paddingR;
630 n.measuredH = h + n.paddingT + n.paddingB;
631 break;
632 }
633 case NodeType::Grid: {
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) {
637 UINode &child = tree.nodes[size_t(c)];
638 if (!child.visible || child.absolute) continue;
639 measureNode(tree, c);
640 GridItemSpec item;
641 item.basisW = child.measuredW;
642 item.basisH = child.measuredH;
643 item.marginL = child.marginL;
644 item.marginT = child.marginT;
645 item.marginR = child.marginR;
646 item.marginB = child.marginB;
649 items.push_back(item);
650 maxCellWidth = std::max(maxCellWidth,
651 (child.measuredW + child.marginL + child.marginR) /
652 float(std::max(1, child.gridColumnSpan)));
653 }
654 const int columns = std::max(1, n.gridColumns);
655 const float columnGap = n.columnGap >= 0.f
656 ? n.columnGap
657 : (n.gap >= 0.f ? n.gap : style.ItemSpacing.x);
658 const float rowGap = n.rowGap >= 0.f
659 ? n.rowGap
660 : (n.gap >= 0.f ? n.gap : style.ItemSpacing.y);
661 const float naturalWidth = maxCellWidth * float(columns) +
662 columnGap * float(columns - 1);
663 const GridResult arranged =
664 gridArrange(columns, columnGap, rowGap, naturalWidth, items);
665 n.measuredW = arranged.contentW + n.paddingL + n.paddingR;
666 n.measuredH = arranged.contentH + n.paddingT + n.paddingB;
667 break;
668 }
669 case NodeType::Flex: {
670 const bool row = n.flexDirection == FlexDirection::Row;
671 float mainSum = 0.f;
672 float crossMax = 0.f;
673 int count = 0;
674 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
675 UINode &child = tree.nodes[size_t(c)];
676 if (!child.visible) continue;
677 measureNode(tree, c);
678 if (child.absolute) continue; // absolutely placed items don't size the flex
679 const float m = row ? child.measuredW : child.measuredH;
680 const float cm = row ? child.measuredH : child.measuredW;
681 const float mb = row ? child.marginL : child.marginT;
682 const float ma = row ? child.marginR : child.marginB;
683 const float cb = row ? child.marginT : child.marginL;
684 const float ca = row ? child.marginB : child.marginR;
685 mainSum += m + mb + ma;
686 crossMax = std::max(crossMax, cm + cb + ca);
687 ++count;
688 }
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;
696 break;
697 }
698 case NodeType::Window: {
699 float w = 0.f, h = 0.f;
700 measureFlowChildren(tree, n.firstChild, &w, &h);
701 n.measuredW = w + n.paddingL + n.paddingR;
702 n.measuredH = h + n.paddingT + n.paddingB;
703 break;
704 }
706 default:
707 break;
708 }
709
710 if (n.type != NodeType::Child && n.type != NodeType::Window) {
711 if (n.sizeX > 0.f) n.measuredW = n.sizeX;
712 if (n.sizeY > 0.f) n.measuredH = n.sizeY;
713 }
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;
717 }
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;
722 }
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);
727}
728
730 if (tree.root >= 0) measureNode(tree, tree.root);
731}
732
733} // namespace eve::ui
double m[16]
float w
Definition AnimClip.cpp:738
const std::string & s
float gap
int rows
int cols
float aspectRatio
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec3 n
Definition Grass.cpp:63
double r
float v
std::int32_t second
std::int32_t c
std::int32_t first
int h
std::uint32_t height
std::uint32_t width
std::string label
Poco::JSON::Object::Ptr result
float t
std::string slot
unsigned line
unsigned column
size_t end
LocalPageCacheEntry slots[ShadowConfig::kLocalSlots]
Definition Shadow.cpp:159
int count
Cell cell
int columns
uint32_t index
int main(int argc, char **argv)
Definition main.cpp:46
FlexResult flexArrangeSingleLine(bool row, float gap, float availMain, float availCross, FlexAlign containerAlign, FlexJustify justify, const std::vector< FlexItemSpec > &items)
Definition Layout.cpp:26
GridResult gridArrange(int columns, float columnGap, float rowGap, float availWidth, const std::vector< GridItemSpec > &items)
Places source-ordered items into equal-width fixed columns.
Definition Layout.cpp:277
void measureTree(UIHost::Tree &tree)
Measure tree.
Definition Layout.cpp:729
WidgetDesc child(std::string id, std::vector< WidgetDesc > children, float width, float height)
Scrollable child region with an explicit size.
Definition Widget.cpp:826
FlexJustify
Main-axis distribution of free space in a Flex container.
Definition UIHost.h:92
void measureNode(UIHost::Tree &tree, int index)
Measure node.
Definition Layout.cpp:370
Theme & globalTheme()
Global theme.
Definition Theme.cpp:277
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
Definition Widget.cpp:870
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.
Definition Layout.cpp:202
FlexAlign
Cross-axis alignment of Flex children.
Definition UIHost.h:89
void measureFlowChildren(UIHost::Tree &tree, int firstChild, float *outW, float *outH)
Measure flow children.
Definition Layout.cpp:332
float themeUiScale()
Theme ui scale.
Definition Theme.cpp:305
FlexItemSpec public API.
Definition Layout.h:25
FlexRect public API.
Definition Layout.h:56
FlexResult public API.
Definition Layout.h:64
std::vector< FlexRect > items
Definition Layout.h:65
GridItemSpec public API.
Definition Layout.h:82
GridResult public API.
Definition Layout.h:94
std::vector< FlexRect > items
Definition Layout.h:95
float toolbarHeight
Definition Theme.h:17
float toolboxCellSize
Definition Theme.h:20
float searchMinWidth
Definition Theme.h:30
float panelHeaderHeight
Definition Theme.h:33
float sectionSpacingY
Definition Theme.h:29
float statusBarHeight
Definition Theme.h:18
ThemeLayout layout
Definition Theme.h:43
std::vector< UINode > nodes
Definition UIHost.h:330
Retained UI widget node (arena tree). Conceptual counterpart of eve::scene::SceneNode; built declarat...
Definition UIHost.h:151
FlexDirection flexDirection
Definition Widget.h:107