10 void ComputeGrow(std::vector<Element>* elements,
13 for (
Element& element : *elements) {
15 extra_space * element.flex_grow / std::max(flex_grow_sum, 1);
16 extra_space -= added_space;
17 flex_grow_sum -= element.flex_grow;
18 element.size = element.min_size + added_space;
25 void ComputeShrinkEasy(std::vector<Element>* elements,
27 int flex_shrink_sum) {
28 for (
Element& element : *elements) {
29 int added_space = extra_space * element.min_size * element.flex_shrink /
30 std::max(flex_shrink_sum, 1);
31 extra_space -= added_space;
32 flex_shrink_sum -= element.flex_shrink * element.min_size;
33 element.size = element.min_size + added_space;
41 void ComputeShrinkHard(std::vector<Element>* elements,
44 for (
Element& element : *elements) {
45 if (element.flex_shrink) {
50 int added_space = extra_space * element.min_size / std::max(1,
size);
51 extra_space -= added_space;
52 size -= element.min_size;
54 element.size = element.min_size + added_space;
60 void Compute(std::vector<Element>* elements,
int target_size) {
62 int flex_grow_sum = 0;
63 int flex_shrink_sum = 0;
64 int flex_shrink_size = 0;
66 for (
auto& element : *elements) {
67 flex_grow_sum += element.flex_grow;
68 flex_shrink_sum += element.min_size * element.flex_shrink;
69 if (element.flex_shrink)
70 flex_shrink_size += element.min_size;
71 size += element.min_size;
74 int extra_space = target_size -
size;
76 ComputeGrow(elements, extra_space, flex_grow_sum);
77 else if (flex_shrink_size + extra_space >= 0)
78 ComputeShrinkEasy(elements, extra_space, flex_shrink_sum);
80 ComputeShrinkHard(elements, extra_space + flex_shrink_size,
81 size - flex_shrink_size);