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