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 
8 #include "ftxui/dom/elements.hpp" // for Constraint, WidthOrHeight, EQUAL, GREATER_THAN, LESS_THAN, WIDTH, unpack, Decorator, Element, size
9 #include "ftxui/dom/node.hpp" // for Node, Elements
10 #include "ftxui/dom/requirement.hpp" // for Requirement
11 #include "ftxui/screen/box.hpp" // for Box
12 
13 namespace ftxui {
14 
15 namespace {
16 class Size : public Node {
17  public:
18  Size(Element child, WidthOrHeight direction, Constraint constraint, int value)
19  : Node(unpack(std::move(child))),
20  direction_(direction),
21  constraint_(constraint),
22  value_(value) {}
23 
24  void ComputeRequirement() override {
26  requirement_ = children_[0]->requirement();
27 
28  auto& value = direction_ == WIDTH ? requirement_.min_x : requirement_.min_y;
29 
30  switch (constraint_) {
31  case LESS_THAN:
32  value = std::min(value, value_);
33  break;
34  case EQUAL:
35  value = value_;
36  break;
37  case GREATER_THAN:
38  value = std::max(value, value_);
39  break;
40  }
41 
42  if (direction_ == WIDTH) {
43  requirement_.flex_grow_x = 0;
44  requirement_.flex_shrink_x = 0;
45  } else {
46  requirement_.flex_grow_y = 0;
47  requirement_.flex_shrink_y = 0;
48  }
49  }
50 
51  void SetBox(Box box) override {
52  Node::SetBox(box);
53 
54  if (direction_ == WIDTH) {
55  switch (constraint_) {
56  case LESS_THAN:
57  case EQUAL:
58  box.x_max = std::min(box.x_min + value_ + 1, box.x_max);
59  break;
60  case GREATER_THAN:
61  break;
62  }
63  } else {
64  switch (constraint_) {
65  case LESS_THAN:
66  case EQUAL:
67  box.y_max = std::min(box.y_min + value_ + 1, box.y_max);
68  break;
69  case GREATER_THAN:
70  break;
71  }
72  }
73  children_[0]->SetBox(box);
74  }
75 
76  private:
77  WidthOrHeight direction_;
78  Constraint constraint_;
79  int value_;
80 };
81 } // namespace
82 
83 /// @brief Apply a constraint on the size of an element.
84 /// @param direction Whether the WIDTH of the HEIGHT of the element must be
85 /// constrained.
86 /// @param constraint The type of constaint.
87 /// @param value The value.
88 /// @ingroup dom
89 Decorator size(WidthOrHeight direction, Constraint constraint, int value) {
90  return [=](Element e) {
91  return std::make_shared<Size>(std::move(e), direction, constraint, value);
92  };
93 }
94 
95 } // 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:147
@ WIDTH
Definition: elements.hpp:147
std::function< Element(Element)> Decorator
Definition: elements.hpp:24
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition: size.cpp:89
std::shared_ptr< Node > Element
Definition: elements.hpp:22
Constraint
Definition: elements.hpp:148
@ LESS_THAN
Definition: elements.hpp:148
@ EQUAL
Definition: elements.hpp:148
@ GREATER_THAN
Definition: elements.hpp:148