2021-05-15 04:00:49 +08:00
|
|
|
|
#include <memory> // for make_shared, __shared_ptr_access
|
|
|
|
|
#include <utility> // for move
|
2021-12-12 00:58:25 +08:00
|
|
|
|
#include <vector> // for __alloc_traits<>::value_type
|
2021-05-10 02:32:27 +08:00
|
|
|
|
|
|
|
|
|
#include "ftxui/dom/elements.hpp" // for Element, unpack, filler, flex, flex_grow, flex_shrink, notflex, xflex, xflex_grow, xflex_shrink, yflex, yflex_grow, yflex_shrink
|
2021-12-12 00:58:25 +08:00
|
|
|
|
#include "ftxui/dom/node.hpp" // for Elements, Node
|
2021-05-10 02:32:27 +08:00
|
|
|
|
#include "ftxui/dom/requirement.hpp" // for Requirement
|
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
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
|
|
|
|
|
2020-08-09 20:53:56 +08:00
|
|
|
|
namespace {
|
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
|
using FlexFunction = void (*)(Requirement&);
|
|
|
|
|
|
|
|
|
|
void function_flex_grow(Requirement& r) {
|
|
|
|
|
r.flex_grow_x = 1;
|
|
|
|
|
r.flex_grow_y = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 06:27:39 +08:00
|
|
|
|
void function_xflex_grow(Requirement& r) {
|
|
|
|
|
r.flex_grow_x = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void function_yflex_grow(Requirement& r) {
|
|
|
|
|
r.flex_grow_y = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
|
void function_flex_shrink(Requirement& r) {
|
|
|
|
|
r.flex_shrink_x = 1;
|
|
|
|
|
r.flex_shrink_y = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 06:27:39 +08:00
|
|
|
|
void function_xflex_shrink(Requirement& r) {
|
|
|
|
|
r.flex_shrink_x = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void function_yflex_shrink(Requirement& r) {
|
|
|
|
|
r.flex_shrink_y = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 06:27:39 +08:00
|
|
|
|
void function_xflex(Requirement& r) {
|
|
|
|
|
r.flex_grow_x = 1;
|
|
|
|
|
r.flex_shrink_x = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void function_yflex(Requirement& r) {
|
|
|
|
|
r.flex_grow_y = 1;
|
|
|
|
|
r.flex_shrink_y = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 20:53:56 +08:00
|
|
|
|
} // namespace
|
|
|
|
|
|
2018-09-18 14:48:40 +08:00
|
|
|
|
class Flex : public Node {
|
|
|
|
|
public:
|
2022-03-31 08:17:43 +08:00
|
|
|
|
explicit Flex(FlexFunction f) : f_(f) {}
|
2020-06-02 05:40:32 +08:00
|
|
|
|
Flex(FlexFunction f, Element child) : Node(unpack(std::move(child))), f_(f) {}
|
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;
|
2021-05-16 23:18:11 +08:00
|
|
|
|
if (!children_.empty()) {
|
|
|
|
|
children_[0]->ComputeRequirement();
|
|
|
|
|
requirement_ = children_[0]->requirement();
|
2018-09-20 03:52:25 +08:00
|
|
|
|
}
|
2020-06-02 05:40:32 +08:00
|
|
|
|
f_(requirement_);
|
2018-09-18 14:48:40 +08:00
|
|
|
|
}
|
2018-09-20 03:52:25 +08:00
|
|
|
|
|
|
|
|
|
void SetBox(Box box) override {
|
2022-03-31 08:17:43 +08:00
|
|
|
|
if (children_.empty()) {
|
2018-09-20 03:52:25 +08:00
|
|
|
|
return;
|
2022-03-31 08:17:43 +08:00
|
|
|
|
}
|
2021-05-16 23:18:11 +08:00
|
|
|
|
children_[0]->SetBox(box);
|
2018-09-20 03:52:25 +08:00
|
|
|
|
}
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
|
FlexFunction f_;
|
2019-01-27 09:33:06 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
|
/// @brief An element that will take expand proportionnally to the space left in
|
|
|
|
|
/// a container.
|
|
|
|
|
/// @ingroup dom
|
2020-05-21 02:36:47 +08:00
|
|
|
|
Element filler() {
|
2020-06-02 05:40:32 +08:00
|
|
|
|
return std::make_shared<Flex>(function_flex);
|
2018-09-18 14:48:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
|
/// @brief Make a child element to expand proportionnally to the space left in a
|
|
|
|
|
/// container.
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @ingroup dom
|
2020-05-25 07:34:13 +08:00
|
|
|
|
///
|
|
|
|
|
/// #### Examples:
|
|
|
|
|
///
|
|
|
|
|
/// ~~~cpp
|
|
|
|
|
/// hbox({
|
2021-08-09 05:25:20 +08:00
|
|
|
|
/// text("left") | border ,
|
|
|
|
|
/// text("middle") | border | flex,
|
|
|
|
|
/// text("right") | border,
|
2020-05-25 07:34:13 +08:00
|
|
|
|
/// });
|
|
|
|
|
/// ~~~
|
|
|
|
|
///
|
|
|
|
|
/// #### Output:
|
|
|
|
|
///
|
|
|
|
|
/// ~~~bash
|
|
|
|
|
/// ┌────┐┌─────────────────────────────────────────────────────────┐┌─────┐
|
|
|
|
|
/// │left││middle ││right│
|
|
|
|
|
/// └────┘└─────────────────────────────────────────────────────────┘└─────┘
|
|
|
|
|
/// ~~~
|
2020-05-21 02:36:47 +08:00
|
|
|
|
Element flex(Element child) {
|
2020-06-02 05:40:32 +08:00
|
|
|
|
return std::make_shared<Flex>(function_flex, std::move(child));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @brief Expand/Minimize if possible/needed on the X axis.
|
|
|
|
|
/// @ingroup dom
|
2020-07-17 06:27:39 +08:00
|
|
|
|
Element xflex(Element child) {
|
|
|
|
|
return std::make_shared<Flex>(function_xflex, std::move(child));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @brief Expand/Minimize if possible/needed on the Y axis.
|
|
|
|
|
/// @ingroup dom
|
2020-07-17 06:27:39 +08:00
|
|
|
|
Element yflex(Element child) {
|
|
|
|
|
return std::make_shared<Flex>(function_yflex, std::move(child));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @brief Expand if possible.
|
|
|
|
|
/// @ingroup dom
|
2020-06-02 05:40:32 +08:00
|
|
|
|
Element flex_grow(Element child) {
|
|
|
|
|
return std::make_shared<Flex>(function_flex_grow, std::move(child));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @brief Expand if possible on the X axis.
|
|
|
|
|
/// @ingroup dom
|
2020-07-17 06:27:39 +08:00
|
|
|
|
Element xflex_grow(Element child) {
|
|
|
|
|
return std::make_shared<Flex>(function_xflex_grow, std::move(child));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @brief Expand if possible on the Y axis.
|
|
|
|
|
/// @ingroup dom
|
2020-07-17 06:27:39 +08:00
|
|
|
|
Element yflex_grow(Element child) {
|
|
|
|
|
return std::make_shared<Flex>(function_yflex_grow, std::move(child));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @brief Minimize if needed.
|
|
|
|
|
/// @ingroup dom
|
2020-06-02 05:40:32 +08:00
|
|
|
|
Element flex_shrink(Element child) {
|
|
|
|
|
return std::make_shared<Flex>(function_flex_shrink, std::move(child));
|
2018-09-20 03:52:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @brief Minimize if needed on the X axis.
|
|
|
|
|
/// @ingroup dom
|
2020-07-17 06:27:39 +08:00
|
|
|
|
Element xflex_shrink(Element child) {
|
|
|
|
|
return std::make_shared<Flex>(function_xflex_shrink, std::move(child));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @brief Minimize if needed on the Y axis.
|
|
|
|
|
/// @ingroup dom
|
2020-07-17 06:27:39 +08:00
|
|
|
|
Element yflex_shrink(Element child) {
|
|
|
|
|
return std::make_shared<Flex>(function_yflex_shrink, std::move(child));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
|
/// @brief Make the element not flexible.
|
|
|
|
|
/// @ingroup dom
|
2020-05-21 02:36:47 +08:00
|
|
|
|
Element notflex(Element child) {
|
2020-06-02 05:40:32 +08:00
|
|
|
|
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
|
2020-08-16 06:24:18 +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.
|