FTXUI  0.8.1
C++ functional terminal UI.
node.cpp
Go to the documentation of this file.
1 #include <utility>
2 
3 #include "ftxui/dom/node.hpp"
5 
6 namespace ftxui {
7 
9 Node::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
21 void Node::SetBox(Box box) {
22  box_ = box;
23 }
24 
25 /// @brief Display an element on a ftxui::Screen.
26 /// @ingroup dom
27 void Node::Render(Screen& screen) {
28  for (auto& child : children_)
29  child->Render(screen);
30 }
31 
32 /// @brief Display an element on a ftxui::Screen.
33 /// @ingroup dom
34 void Render(Screen& screen, const Element& element) {
35  Render(screen, element.get());
36 }
37 
38 /// @brief Display an element on a ftxui::Screen.
39 /// @ingroup dom
40 void Render(Screen& screen, Node* node) {
41  // Step 1: Find what dimension this elements wants to be.
42  node->ComputeRequirement();
43 
44  Box box;
45  box.x_min = 0;
46  box.y_min = 0;
47  box.x_max = screen.dimx() - 1;
48  box.y_max = screen.dimy() - 1;
49 
50  // Step 2: Assign a dimension to the element.
51  node->SetBox(box);
52  screen.stencil = box;
53 
54  // Step 3: Draw the element.
55  node->Render(screen);
56 
57  // Step 4: Apply shaders
58  screen.ApplyShader();
59 }
60 
61 } // namespace ftxui
62 
63 // Copyright 2020 Arthur Sonzogni. All rights reserved.
64 // Use of this source code is governed by the MIT license that can be found in
65 // the LICENSE file.
ftxui::Node::SetBox
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition: node.cpp:21
ftxui::Box::x_min
int x_min
Definition: box.hpp:7
ftxui::Node::box_
Box box_
Definition: node.hpp:41
ftxui::Box::y_max
int y_max
Definition: box.hpp:10
ftxui::Box
Definition: box.hpp:6
ftxui
Definition: captured_mouse.hpp:6
node.hpp
ftxui::Node::Render
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition: node.cpp:27
ftxui::Node::children_
Elements children_
Definition: node.hpp:39
ftxui::Box::x_max
int x_max
Definition: box.hpp:8
ftxui::Elements
std::vector< Element > Elements
Definition: elements.hpp:16
ftxui::Node::Node
Node()
Definition: node.cpp:8
ftxui::Element
std::shared_ptr< Node > Element
Definition: elements.hpp:15
ftxui::Screen::dimy
int dimy()
Definition: screen.hpp:66
ftxui::Box::y_min
int y_min
Definition: box.hpp:9
ftxui::Screen::stencil
Box stencil
Definition: screen.hpp:75
ftxui::Screen
A rectangular grid of Pixel.
Definition: screen.hpp:49
ftxui::Node::~Node
virtual ~Node()
Definition: node.cpp:10
ftxui::Render
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:34
ftxui::Screen::dimx
int dimx()
Definition: screen.hpp:65
ftxui::Node
Definition: node.hpp:19
ftxui::Node::ComputeRequirement
virtual void ComputeRequirement()
Compute how much space an elements needs.
Definition: node.cpp:14
screen.hpp
ftxui::Screen::ApplyShader
void ApplyShader()
Definition: screen.cpp:226