14void ComputeGrow(std::vector<Element>* elements,
17 for (
Element& element : *elements) {
18 const int added_space =
19 extra_space * element.flex_grow / std::max(flex_grow_sum, 1);
20 extra_space -= added_space;
21 flex_grow_sum -= element.flex_grow;
22 element.size = element.min_size + added_space;
29void ComputeShrinkEasy(std::vector<Element>* elements,
31 int flex_shrink_sum) {
32 for (
Element& element : *elements) {
33 const int added_space = extra_space * element.min_size *
34 element.flex_shrink / std::max(flex_shrink_sum, 1);
35 extra_space -= added_space;
36 flex_shrink_sum -= element.flex_shrink * element.min_size;
37 element.size = element.min_size + added_space;
45void ComputeShrinkHard(std::vector<Element>* elements,
48 for (
Element& element : *elements) {
49 if (element.flex_shrink != 0) {
54 const int added_space = extra_space * element.min_size / std::max(1,
size);
55 extra_space -= added_space;
56 size -= element.min_size;
58 element.size = element.min_size + added_space;
64void Compute(std::vector<Element>* elements,
int target_size) {
66 int flex_grow_sum = 0;
67 int flex_shrink_sum = 0;
68 int flex_shrink_size = 0;
70 for (
auto& element : *elements) {
71 flex_grow_sum += element.flex_grow;
72 flex_shrink_sum += element.min_size * element.flex_shrink;
73 if (element.flex_shrink != 0) {
74 flex_shrink_size += element.min_size;
76 size += element.min_size;
79 const int extra_space = target_size -
size;
80 if (extra_space >= 0) {
81 ComputeGrow(elements, extra_space, flex_grow_sum);
82 }
else if (flex_shrink_size + extra_space >= 0) {
83 ComputeShrinkEasy(elements, extra_space, flex_shrink_sum);
86 ComputeShrinkHard(elements, extra_space + flex_shrink_size,
87 size - flex_shrink_size);
void Compute(std::vector< Element > *elements, int target_size)
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.