FTXUI  0.11.1
C++ functional terminal UI.
node.cpp
Go to the documentation of this file.
1 #include <utility> // for move
2 
3 #include "ftxui/dom/node.hpp"
4 #include "ftxui/screen/screen.hpp" // for Screen
5 
6 namespace ftxui {
7 
9 Node::Node(Elements children) : children_(std::move(children)) {}
11 
12 /// @brief Compute how much space an elements needs.
13 /// @ingroup dom
15  for (auto& child : children_)
16  child->ComputeRequirement();
17 }
18 
19 /// @brief Assign a position and a dimension to an element for drawing.
20 /// @ingroup dom
21 void Node::SetBox(Box box) {
22  box_ = box;
23 }
24 
25 /// @brief Display an element on a ftxui::Screen.
26 /// @ingroup dom
27 void Node::Render(Screen& screen) {
28  for (auto& child : children_)
29  child->Render(screen);
30 }
31 
32 void Node::Check(Status* status) {
33  for (auto& child : children_)
34  child->Check(status);
35  status->need_iteration |= (status->iteration == 0);
36 }
37 
38 /// @brief Display an element on a ftxui::Screen.
39 /// @ingroup dom
40 void Render(Screen& screen, const Element& element) {
41  Render(screen, element.get());
42 }
43 
44 /// @brief Display an element on a ftxui::Screen.
45 /// @ingroup dom
46 void Render(Screen& screen, Node* node) {
47  Box box;
48  box.x_min = 0;
49  box.y_min = 0;
50  box.x_max = screen.dimx() - 1;
51  box.y_max = screen.dimy() - 1;
52 
53  Node::Status status;
54  node->Check(&status);
55  while (status.need_iteration && status.iteration < 20) {
56  // Step 1: Find what dimension this elements wants to be.
57  node->ComputeRequirement();
58 
59  // Step 2: Assign a dimension to the element.
60  node->SetBox(box);
61 
62  // Check if the element needs another iteration of the layout algorithm.
63  status.need_iteration = false;
64  status.iteration++;
65  node->Check(&status);
66  }
67 
68  // Step 3: Draw the element.
69  screen.stencil = box;
70  node->Render(screen);
71 
72  // Step 4: Apply shaders
73  screen.ApplyShader();
74 }
75 
76 } // namespace ftxui
77 
78 // Copyright 2020 Arthur Sonzogni. All rights reserved.
79 // Use of this source code is governed by the MIT license that can be found in
80 // the LICENSE file.
Node()
Definition: node.cpp:8
bool need_iteration
Definition: node.hpp:43
Elements children_
Definition: node.hpp:48
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition: node.cpp:21
virtual void ComputeRequirement()
Compute how much space an elements needs.
Definition: node.cpp:14
virtual void Check(Status *status)
Definition: node.cpp:32
virtual ~Node()
Definition: node.cpp:10
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition: node.cpp:27
Box box_
Definition: node.hpp:50
A rectangular grid of Pixel.
Definition: screen.hpp:49
void ApplyShader()
Definition: screen.cpp:455
int dimy()
Definition: screen.hpp:66
int dimx()
Definition: screen.hpp:65
std::shared_ptr< Node > Element
Definition: elements.hpp:16
std::vector< Element > Elements
Definition: elements.hpp:17
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:40
int x_max
Definition: box.hpp:8
int y_min
Definition: box.hpp:9
int y_max
Definition: box.hpp:10
int x_min
Definition: box.hpp:7