FTXUI  0.11.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
box_helper.cpp
Go to the documentation of this file.
2
3#include <algorithm> // for max
4
5namespace ftxui {
6namespace box_helper {
7
8namespace {
9// Called when the size allowed is greater than the requested size. This
10// distributes the extra spaces toward the flexible elements, in relative
11// proportions.
12void ComputeGrow(std::vector<Element>* elements,
13 int extra_space,
14 int flex_grow_sum) {
15 for (Element& element : *elements) {
16 int added_space =
17 extra_space * element.flex_grow / std::max(flex_grow_sum, 1);
18 extra_space -= added_space;
19 flex_grow_sum -= element.flex_grow;
20 element.size = element.min_size + added_space;
21 }
22}
23
24// Called when the size allowed is lower than the requested size, and the
25// shrinkable element can absorbe the (negative) extra_space. This distribute
26// the extra_space toward those.
27void ComputeShrinkEasy(std::vector<Element>* elements,
28 int extra_space,
29 int flex_shrink_sum) {
30 for (Element& element : *elements) {
31 int added_space = extra_space * element.min_size * element.flex_shrink /
32 std::max(flex_shrink_sum, 1);
33 extra_space -= added_space;
34 flex_shrink_sum -= element.flex_shrink * element.min_size;
35 element.size = element.min_size + added_space;
36 }
37}
38
39// Called when the size allowed is lower than the requested size, and the
40// shrinkable element can not absorbe the (negative) extra_space. This assign
41// zero to shrinkable elements and distribute the remaining (negative)
42// extra_space toward the other non shrinkable elements.
43void ComputeShrinkHard(std::vector<Element>* elements,
44 int extra_space,
45 int size) {
46 for (Element& element : *elements) {
47 if (element.flex_shrink) {
48 element.size = 0;
49 continue;
50 }
51
52 int added_space = extra_space * element.min_size / std::max(1, size);
53 extra_space -= added_space;
54 size -= element.min_size;
55
56 element.size = element.min_size + added_space;
57 }
58}
59
60} // namespace
61
62void Compute(std::vector<Element>* elements, int target_size) {
63 int size = 0;
64 int flex_grow_sum = 0;
65 int flex_shrink_sum = 0;
66 int flex_shrink_size = 0;
67
68 for (auto& element : *elements) {
69 flex_grow_sum += element.flex_grow;
70 flex_shrink_sum += element.min_size * element.flex_shrink;
71 if (element.flex_shrink)
72 flex_shrink_size += element.min_size;
73 size += element.min_size;
74 }
75
76 int extra_space = target_size - size;
77 if (extra_space >= 0)
78 ComputeGrow(elements, extra_space, flex_grow_sum);
79 else if (flex_shrink_size + extra_space >= 0)
80 ComputeShrinkEasy(elements, extra_space, flex_shrink_sum);
81 else
82 ComputeShrinkHard(elements, extra_space + flex_shrink_size,
83 size - flex_shrink_size);
84}
85
86} // namespace box_helper
87} // namespace ftxui
88
89// Copyright 2021 Arthur Sonzogni. All rights reserved.
90// Use of this source code is governed by the MIT license that can be found in
91// the LICENSE file.
void Compute(std::vector< Element > *elements, int target_size)
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:86