FTXUI  0.11.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
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
12namespace ftxui {
13
14class 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
86Decorator 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.
Elements children_
Definition node.hpp:39
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:21
Requirement requirement_
Definition node.hpp:40
virtual void ComputeRequirement()
Compute how much space an elements needs.
Definition node.cpp:14
Dimensions Size()
Definition terminal.cpp:30
std::function< Element(Element)> Decorator
Definition elements.hpp:17
@ WIDTH
Definition elements.hpp:94
std::shared_ptr< Node > Element
Definition elements.hpp:15
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:86
@ LESS_THAN
Definition elements.hpp:95
@ EQUAL
Definition elements.hpp:95
@ GREATER_THAN
Definition elements.hpp:95