FTXUI/src/ftxui/dom/flex.cpp

81 lines
1.8 KiB
C++
Raw Normal View History

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.
#include "ftxui/dom/elements.hpp"
2020-03-23 05:32:44 +08:00
#include "ftxui/dom/node.hpp"
2018-09-18 14:48:40 +08:00
namespace ftxui {
2018-09-18 14:48:40 +08:00
using FlexFunction = void (*)(Requirement&);
void function_flex_grow(Requirement& r) {
r.flex_grow_x = 1;
r.flex_grow_y = 1;
}
void function_flex_shrink(Requirement& r) {
r.flex_shrink_x = 1;
r.flex_shrink_y = 1;
}
void function_flex(Requirement& r) {
r.flex_grow_x = 1;
r.flex_grow_y = 1;
r.flex_shrink_x = 1;
r.flex_shrink_y = 1;
}
void function_not_flex(Requirement& r) {
r.flex_grow_x = 0;
r.flex_grow_y = 0;
r.flex_shrink_x = 0;
r.flex_shrink_y = 0;
}
2018-09-18 14:48:40 +08:00
class Flex : public Node {
public:
Flex(FlexFunction f) { f_ = f; }
Flex(FlexFunction f, Element child) : Node(unpack(std::move(child))), f_(f) {}
2018-09-18 14:48:40 +08:00
~Flex() override {}
2019-01-27 09:33:06 +08:00
void ComputeRequirement() override {
2020-06-01 22:13:29 +08:00
requirement_.min_x = 0;
requirement_.min_y = 0;
2018-09-20 03:52:25 +08:00
if (!children.empty()) {
children[0]->ComputeRequirement();
requirement_ = children[0]->requirement();
}
f_(requirement_);
2018-09-18 14:48:40 +08:00
}
2018-09-20 03:52:25 +08:00
void SetBox(Box box) override {
if (children.empty())
return;
children[0]->SetBox(box);
}
2018-09-18 14:48:40 +08:00
FlexFunction f_;
2019-01-27 09:33:06 +08:00
};
Element filler() {
return std::make_shared<Flex>(function_flex);
2018-09-18 14:48:40 +08:00
}
Element flex(Element child) {
return std::make_shared<Flex>(function_flex, std::move(child));
}
Element flex_grow(Element child) {
return std::make_shared<Flex>(function_flex_grow, std::move(child));
}
Element flex_shrink(Element child) {
return std::make_shared<Flex>(function_flex_shrink, std::move(child));
2018-09-20 03:52:25 +08:00
}
Element notflex(Element child) {
return std::make_shared<Flex>(function_not_flex, std::move(child));
2019-01-27 09:33:06 +08:00
}
2020-02-12 04:44:55 +08:00
} // namespace ftxui