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