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