FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
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;
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
27 box_ = box;
28}
29
30/// @brief Display an element on a ftxui::Screen.
31/// @ingroup dom
33 for (auto& child : children_) {
34 child->Render(screen);
35 }
36}
37
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
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
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
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:25
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:47
int x_min
Definition box.hpp:10