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