FTXUI  2.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
9Node::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
21void Node::SetBox(Box box) {
22 box_ = box;
23}
24
25/// @brief Display an element on a ftxui::Screen.
26/// @ingroup dom
27void Node::Render(Screen& screen) {
28 for (auto& child : children_)
29 child->Render(screen);
30}
31
32void 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
40void 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
46void 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.
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:51
void ApplyShader()
Definition screen.cpp:459
int dimy() const
Definition screen.hpp:68
int dimx() const
Definition screen.hpp:67
std::shared_ptr< Node > Element
Definition elements.hpp:18
std::vector< Element > Elements
Definition elements.hpp:19
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