FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
node.cpp
Go to the documentation of this file.
1#include <iostream>
2// Copyright 2020 Arthur Sonzogni. All rights reserved.
3// Use of this source code is governed by the MIT license that can be found in
4// the LICENSE file.
5#include <ftxui/screen/box.hpp> // for Box
6#include <utility> // for move
7
8#include "ftxui/dom/node.hpp"
9#include "ftxui/screen/screen.hpp" // for Screen
10
11namespace ftxui {
12
13Node::Node() = default;
15Node::~Node() = default;
16
17/// @brief Compute how much space an elements needs.
18/// @ingroup dom
20 for (auto& child : children_) {
21 child->ComputeRequirement();
22 }
23}
24
25/// @brief Assign a position and a dimension to an element for drawing.
26/// @ingroup dom
28 box_ = box;
29}
30
31/// @brief Compute the selection of an element.
32/// @ingroup dom
33void Node::Select(Selection& selection) {
34 // If this Node box_ doesn't intersect with the selection, then no selection.
35 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
36 return;
37 }
38
39 // By default we defer the selection to the children.
40 for (auto& child : children_) {
41 child->Select(selection);
42 }
43}
44
45/// @brief Display an element on a ftxui::Screen.
46/// @ingroup dom
48 for (auto& child : children_) {
49 child->Render(screen);
50 }
51}
52
54 for (auto& child : children_) {
55 child->Check(status);
56 }
57 status->need_iteration |= (status->iteration == 0);
58}
59
60std::string Node::GetSelectedContent(Selection& selection) {
61 std::string content;
62
63 for (auto& child : children_) {
64 content += child->GetSelectedContent(selection);
65 }
66
67 return content;
68}
69
70/// @brief Display an element on a ftxui::Screen.
71/// @ingroup dom
72void Render(Screen& screen, const Element& element) {
73 Selection selection;
74 Render(screen, element.get(), selection);
75}
76
77/// @brief Display an element on a ftxui::Screen.
78/// @ingroup dom
80 Selection selection;
81 Render(screen, node, selection);
82}
83
84void Render(Screen& screen, Node* node, Selection& selection) {
85 Box box;
86 box.x_min = 0;
87 box.y_min = 0;
88 box.x_max = screen.dimx() - 1;
89 box.y_max = screen.dimy() - 1;
90
92 node->Check(&status);
93 const int max_iterations = 20;
94 while (status.need_iteration && status.iteration < max_iterations) {
95 // Step 1: Find what dimension this elements wants to be.
96 node->ComputeRequirement();
97
98 // Step 2: Assign a dimension to the element.
99 node->SetBox(box);
100
101 // Check if the element needs another iteration of the layout algorithm.
102 status.need_iteration = false;
103 status.iteration++;
104 node->Check(&status);
105 }
106
107 // Step 3: Selection
108 if (!selection.IsEmpty()) {
109 node->Select(selection);
110 }
111
112 // Step 4: Draw the element.
113 screen.stencil = box;
114 node->Render(screen);
115
116 // Step 5: Apply shaders
117 screen.ApplyShader();
118}
119
121 Node* node,
122 Selection& selection) {
123 Box box;
124 box.x_min = 0;
125 box.y_min = 0;
126 box.x_max = screen.dimx() - 1;
127 box.y_max = screen.dimy() - 1;
128
130 node->Check(&status);
131 const int max_iterations = 20;
132 while (status.need_iteration && status.iteration < max_iterations) {
133 // Step 1: Find what dimension this elements wants to be.
134 node->ComputeRequirement();
135
136 // Step 2: Assign a dimension to the element.
137 node->SetBox(box);
138
139 // Check if the element needs another iteration of the layout algorithm.
140 status.need_iteration = false;
141 status.iteration++;
142 node->Check(&status);
143 }
144
145 // Step 3: Selection
146 node->Select(selection);
147
148 // Step 4: get the selected content.
149 return node->GetSelectedContent(selection);
150}
151
152} // namespace ftxui
virtual void Select(Selection &selection)
Compute the selection of an element.
Definition node.cpp:33
Elements children_
Definition node.hpp:63
virtual std::string GetSelectedContent(Selection &selection)
Definition node.cpp:60
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:27
virtual void ComputeRequirement()
Compute how much space an elements needs.
Definition node.cpp:19
virtual void Check(Status *status)
Definition node.cpp:53
virtual ~Node()
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:47
Box box_
Definition node.hpp:65
A rectangular grid of Pixel.
Definition screen.hpp:27
Represent a selection in the terminal.
Definition selection.hpp:17
const Box & GetBox() const
Get the box of the selection.
Definition selection.cpp:65
bool IsEmpty() const
Definition selection.hpp:26
std::string GetNodeSelectedContent(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:120
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::vector< Element > Elements
Definition elements.hpp:23
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:72
static auto Intersection(Box a, Box b) -> Box
Definition box.cpp:12
int x_min
Definition box.hpp:10