FTXUI  5.0.0
C++ functional terminal UI.
size.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <algorithm> // for min, max
5#include <memory> // for make_shared, __shared_ptr_access
6#include <utility> // for move
7#include <vector> // for __alloc_traits<>::value_type
8
9#include "ftxui/dom/elements.hpp" // for Constraint, WidthOrHeight, EQUAL, GREATER_THAN, LESS_THAN, WIDTH, unpack, Decorator, Element, size
10#include "ftxui/dom/node.hpp" // for Node, Elements
11#include "ftxui/dom/requirement.hpp" // for Requirement
12#include "ftxui/screen/box.hpp" // for Box
13
14namespace ftxui {
15
16namespace {
17class Size : public Node {
18 public:
19 Size(Element child, WidthOrHeight direction, Constraint constraint, int value)
20 : Node(unpack(std::move(child))),
21 direction_(direction),
22 constraint_(constraint),
23 value_(value) {}
24
25 void ComputeRequirement() override {
27 requirement_ = children_[0]->requirement();
28
29 auto& value = direction_ == WIDTH ? requirement_.min_x : requirement_.min_y;
30
31 switch (constraint_) {
32 case LESS_THAN:
33 value = std::min(value, value_);
34 break;
35 case EQUAL:
36 value = value_;
37 break;
38 case GREATER_THAN:
39 value = std::max(value, value_);
40 break;
41 }
42
43 if (direction_ == WIDTH) {
44 requirement_.flex_grow_x = 0;
45 requirement_.flex_shrink_x = 0;
46 } else {
47 requirement_.flex_grow_y = 0;
48 requirement_.flex_shrink_y = 0;
49 }
50 }
51
52 void SetBox(Box box) override {
53 Node::SetBox(box);
54
55 if (direction_ == WIDTH) {
56 switch (constraint_) {
57 case LESS_THAN:
58 case EQUAL:
59 box.x_max = std::min(box.x_min + value_ + 1, box.x_max);
60 break;
61 case GREATER_THAN:
62 break;
63 }
64 } else {
65 switch (constraint_) {
66 case LESS_THAN:
67 case EQUAL:
68 box.y_max = std::min(box.y_min + value_ + 1, box.y_max);
69 break;
70 case GREATER_THAN:
71 break;
72 }
73 }
74 children_[0]->SetBox(box);
75 }
76
77 private:
78 WidthOrHeight direction_;
79 Constraint constraint_;
80 int value_;
81};
82} // namespace
83
84/// @brief Apply a constraint on the size of an element.
85/// @param direction Whether the WIDTH of the HEIGHT of the element must be
86/// constrained.
87/// @param constraint The type of constaint.
88/// @param value The value.
89/// @ingroup dom
90Decorator size(WidthOrHeight direction, Constraint constraint, int value) {
91 return [=](Element e) {
92 return std::make_shared<Size>(std::move(e), direction, constraint, value);
93 };
94}
95
96} // namespace ftxui
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition: node.cpp:26
virtual void ComputeRequirement()
Compute how much space an elements needs.
Definition: node.cpp:18
Dimensions Size()
Get the terminal size.
Definition: terminal.cpp:94
WidthOrHeight
Definition: elements.hpp:148
@ WIDTH
Definition: elements.hpp:148
std::function< Element(Element)> Decorator
Definition: elements.hpp:25
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition: size.cpp:90
std::shared_ptr< Node > Element
Definition: elements.hpp:23
Constraint
Definition: elements.hpp:149
@ LESS_THAN
Definition: elements.hpp:149
@ EQUAL
Definition: elements.hpp:149
@ GREATER_THAN
Definition: elements.hpp:149