15 void ComputeGrow(std::vector<Element>* elements,
18 for (
Element& element : *elements) {
19 const int added_space =
20 extra_space * element.flex_grow / std::max(flex_grow_sum, 1);
21 extra_space -= added_space;
22 flex_grow_sum -= element.flex_grow;
23 element.size = element.min_size + added_space;
30 void ComputeShrinkEasy(std::vector<Element>* elements,
32 int flex_shrink_sum) {
33 for (
Element& element : *elements) {
34 const int added_space = extra_space * element.min_size *
35 element.flex_shrink / std::max(flex_shrink_sum, 1);
36 extra_space -= added_space;
37 flex_shrink_sum -= element.flex_shrink * element.min_size;
38 element.size = element.min_size + added_space;
46 void ComputeShrinkHard(std::vector<Element>* elements,
49 for (
Element& element : *elements) {
50 if (element.flex_shrink != 0) {
55 const int added_space = extra_space * element.min_size / std::max(1,
size);
56 extra_space -= added_space;
57 size -= element.min_size;
59 element.size = element.min_size + added_space;
65 void Compute(std::vector<Element>* elements,
int target_size) {
67 int flex_grow_sum = 0;
68 int flex_shrink_sum = 0;
69 int flex_shrink_size = 0;
71 for (
auto& element : *elements) {
72 flex_grow_sum += element.flex_grow;
73 flex_shrink_sum += element.min_size * element.flex_shrink;
74 if (element.flex_shrink != 0) {
75 flex_shrink_size += element.min_size;
77 size += element.min_size;
80 const int extra_space = target_size -
size;
81 if (extra_space >= 0) {
82 ComputeGrow(elements, extra_space, flex_grow_sum);
83 }
else if (flex_shrink_size + extra_space >= 0) {
84 ComputeShrinkEasy(elements, extra_space, flex_shrink_sum);
87 ComputeShrinkHard(elements, extra_space + flex_shrink_size,
88 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.