2020-04-20 03:00:37 +08:00
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
|
2020-03-23 05:32:44 +08:00
|
|
|
#include <algorithm>
|
2020-03-27 08:42:46 +08:00
|
|
|
#include <iostream>
|
2020-03-23 05:32:44 +08:00
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/dom/elements.hpp"
|
2019-06-23 23:47:33 +08:00
|
|
|
#include "ftxui/dom/node.hpp"
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
class VBox : public Node {
|
|
|
|
public:
|
2019-01-13 01:24:46 +08:00
|
|
|
VBox(Elements children) : Node(std::move(children)) {}
|
2018-09-18 14:48:40 +08:00
|
|
|
~VBox() {}
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
void ComputeRequirement() override {
|
2020-06-01 22:13:29 +08:00
|
|
|
requirement_.min_x = 0;
|
|
|
|
requirement_.min_y = 0;
|
2020-06-02 05:40:32 +08:00
|
|
|
requirement_.flex_grow_x = 0;
|
|
|
|
requirement_.flex_grow_y = 0;
|
|
|
|
requirement_.flex_shrink_x = 0;
|
|
|
|
requirement_.flex_shrink_y = 0;
|
2018-09-18 14:48:40 +08:00
|
|
|
for (auto& child : children) {
|
|
|
|
child->ComputeRequirement();
|
2019-01-20 05:06:05 +08:00
|
|
|
if (requirement_.selection < child->requirement().selection) {
|
|
|
|
requirement_.selection = child->requirement().selection;
|
|
|
|
requirement_.selected_box = child->requirement().selected_box;
|
2020-06-01 22:13:29 +08:00
|
|
|
requirement_.selected_box.y_min += requirement_.min_y;
|
|
|
|
requirement_.selected_box.y_max += requirement_.min_y;
|
2019-01-20 05:06:05 +08:00
|
|
|
}
|
2020-06-01 22:13:29 +08:00
|
|
|
requirement_.min_y += child->requirement().min_y;
|
|
|
|
requirement_.min_x =
|
|
|
|
std::max(requirement_.min_x, child->requirement().min_x);
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
void SetBox(Box box) override {
|
2018-09-18 14:48:40 +08:00
|
|
|
Node::SetBox(box);
|
|
|
|
|
2019-01-20 05:06:05 +08:00
|
|
|
int space = box.y_max - box.y_min + 1;
|
2020-06-01 22:13:29 +08:00
|
|
|
int extra_space = space - requirement_.min_y;
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
int size = 0;
|
|
|
|
int flex_grow_sum = 0;
|
|
|
|
int flex_shrink_sum = 0;
|
|
|
|
int flex_shrink_size = 0;
|
|
|
|
for (auto& child : children) {
|
|
|
|
const Requirement& r = child->requirement();
|
|
|
|
flex_grow_sum += r.flex_grow_y;
|
|
|
|
flex_shrink_sum += r.min_y * r.flex_shrink_y;
|
|
|
|
if (r.flex_shrink_y) {
|
|
|
|
flex_shrink_size += r.min_y;
|
|
|
|
}
|
|
|
|
size += r.min_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extra_space >= 0)
|
|
|
|
SetBoxGrow(box, extra_space, flex_grow_sum);
|
|
|
|
else if (flex_shrink_size + extra_space >= 0)
|
|
|
|
SetBoxShrinkEasy(box, extra_space, flex_shrink_sum);
|
|
|
|
else
|
|
|
|
SetBoxShrinkHard(box, extra_space + flex_shrink_size,
|
|
|
|
size - flex_shrink_size);
|
|
|
|
}
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
void SetBoxGrow(Box box, int extra_space, int flex_grow_sum) {
|
2019-01-20 05:06:05 +08:00
|
|
|
int y = box.y_min;
|
2018-09-18 14:48:40 +08:00
|
|
|
for (auto& child : children) {
|
|
|
|
Box child_box = box;
|
2020-06-02 05:40:32 +08:00
|
|
|
const Requirement& r = child->requirement();
|
|
|
|
|
|
|
|
int added_space =
|
|
|
|
extra_space * r.flex_grow_y / std::max(flex_grow_sum, 1);
|
|
|
|
extra_space -= added_space;
|
|
|
|
flex_grow_sum -= r.flex_grow_y;
|
|
|
|
|
2019-01-20 05:06:05 +08:00
|
|
|
child_box.y_min = y;
|
2020-06-02 05:40:32 +08:00
|
|
|
child_box.y_max = y + r.min_y + added_space - 1;
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
child->SetBox(child_box);
|
|
|
|
y = child_box.y_max + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetBoxShrinkEasy(Box box, int extra_space, int flex_shrink_sum) {
|
|
|
|
int y = box.y_min;
|
|
|
|
for (auto& child : children) {
|
|
|
|
Box child_box = box;
|
|
|
|
const Requirement& r = child->requirement();
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
int added_space = extra_space * r.min_y * r.flex_shrink_y /
|
|
|
|
std::max(flex_shrink_sum, 1);
|
|
|
|
extra_space -= added_space;
|
|
|
|
flex_shrink_sum -= r.flex_shrink_y * r.min_y;
|
|
|
|
|
|
|
|
child_box.y_min = y;
|
|
|
|
child_box.y_max = y + r.min_y + added_space - 1;
|
|
|
|
|
|
|
|
child->SetBox(child_box);
|
|
|
|
y = child_box.y_max + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetBoxShrinkHard(Box box, int extra_space, int size) {
|
|
|
|
int y = box.y_min;
|
|
|
|
for (auto& child : children) {
|
|
|
|
Box child_box = box;
|
|
|
|
const Requirement& r = child->requirement();
|
|
|
|
|
|
|
|
if (r.flex_shrink_y) {
|
|
|
|
child_box.y_min = y;
|
|
|
|
child_box.y_max = y - 1;
|
|
|
|
child->SetBox(child_box);
|
|
|
|
continue;
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
2020-06-02 05:40:32 +08:00
|
|
|
|
|
|
|
int added_space = extra_space * r.min_y / std::max(1, size);
|
|
|
|
extra_space -= added_space;
|
|
|
|
size -= r.min_y;
|
|
|
|
|
|
|
|
child_box.y_min = y;
|
|
|
|
child_box.y_max = y + r.min_y + added_space - 1;
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
child->SetBox(child_box);
|
2019-01-20 05:06:05 +08:00
|
|
|
y = child_box.y_max + 1;
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
/// @brief A container displaying elements vertically one by one.
|
|
|
|
/// @param children The elements in the container
|
|
|
|
/// @return The container.
|
|
|
|
///
|
|
|
|
/// #### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// vbox({
|
|
|
|
/// text(L"Up"),
|
|
|
|
/// text(L"Down"),
|
|
|
|
/// });
|
|
|
|
/// ```
|
2020-05-21 02:36:47 +08:00
|
|
|
Element vbox(Elements children) {
|
|
|
|
return std::make_shared<VBox>(std::move(children));
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
|
2020-02-12 04:44:55 +08:00
|
|
|
} // namespace ftxui
|