Loading...
Searching...
No Matches
size.cpp
Go to the documentation of this file.
// 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 <algorithm> // for min, max
#include <memory> // for make_shared, __shared_ptr_access
#include <utility> // for move
#include "ftxui/dom/elements.hpp" // for Constraint, WidthOrHeight, EQUAL, GREATER_THAN, LESS_THAN, WIDTH, unpack, Decorator, Element, size
#include "ftxui/dom/node.hpp" // for Node, Elements
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
namespace ftxui {
namespace {
class Size : public Node {
public:
Size(Element child, WidthOrHeight direction, Constraint constraint, int value)
: Node(unpack(std::move(child))),
direction_(direction),
constraint_(constraint),
value_(std::max(0, value)) {}
void ComputeRequirement() override {
Node::ComputeRequirement();
requirement_ = children_[0]->requirement();
auto& value = direction_ == WIDTH ? requirement_.min_x : requirement_.min_y;
switch (constraint_) {
case LESS_THAN:
value = std::min(value, value_);
break;
case EQUAL:
value = value_;
break;
case GREATER_THAN:
value = std::max(value, value_);
break;
}
if (direction_ == WIDTH) {
requirement_.flex_grow_x = 0;
requirement_.flex_shrink_x = 0;
} else {
requirement_.flex_grow_y = 0;
requirement_.flex_shrink_y = 0;
}
}
void SetBox(Box box) override {
Node::SetBox(box);
if (direction_ == WIDTH) {
switch (constraint_) {
case LESS_THAN:
case EQUAL:
box.x_max = std::min(box.x_min + value_ + 1, box.x_max);
break;
case GREATER_THAN:
break;
}
} else {
switch (constraint_) {
case LESS_THAN:
case EQUAL:
box.y_max = std::min(box.y_min + value_ + 1, box.y_max);
break;
case GREATER_THAN:
break;
}
}
children_[0]->SetBox(box);
}
private:
WidthOrHeight direction_;
Constraint constraint_;
int value_;
};
} // namespace
/// @brief Apply a constraint on the size of an element.
/// @param direction Whether the WIDTH of the HEIGHT of the element must be
/// constrained.
/// @param constraint The type of constaint.
/// @param value The value.
/// @ingroup dom
Decorator size(WidthOrHeight direction, Constraint constraint, int value) {
return [=](Element e) {
return std::make_shared<Size>(std::move(e), direction, constraint, value);
};
}
} // namespace ftxui
box.hpp
ftxui::Node
Definition node.hpp:23
ftxui::Node::SetBox
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:43
ftxui::Node::ComputeRequirement
virtual void ComputeRequirement()
Compute how much space an elements needs.
Definition node.cpp:21
elements.hpp
ftxui::Terminal::Size
Dimensions Size()
Get the terminal size.
Definition terminal.cpp:94
ftxui
Definition animation.hpp:10
ftxui::WidthOrHeight
WidthOrHeight
Definition elements.hpp:153
ftxui::WIDTH
@ WIDTH
Definition elements.hpp:153
ftxui::Decorator
std::function< Element(Element)> Decorator
Definition elements.hpp:24
ftxui::size
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:89
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::Constraint
Constraint
Definition elements.hpp:154
ftxui::LESS_THAN
@ LESS_THAN
Definition elements.hpp:154
ftxui::EQUAL
@ EQUAL
Definition elements.hpp:154
ftxui::GREATER_THAN
@ GREATER_THAN
Definition elements.hpp:154
node.hpp
requirement.hpp