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