FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
node.hpp
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#ifndef FTXUI_DOM_NODE_HPP
5#define FTXUI_DOM_NODE_HPP
6
7#include <memory> // for shared_ptr
8#include <vector> // for vector
9
10#include "ftxui/dom/requirement.hpp" // for Requirement
11#include "ftxui/dom/selection.hpp" // for Selection
12#include "ftxui/screen/box.hpp" // for Box
14
15namespace ftxui {
16
17class Node;
18class Screen;
19
20using Element = std::shared_ptr<Node>;
21using Elements = std::vector<Element>;
22
23/// @brief Node is the base class for all elements in the DOM tree.
24///
25/// It represents a single node in the document object model (DOM) and provides
26/// the basic structure for layout and rendering.
27/// It contains methods for computing layout requirements, setting the box
28/// dimensions, selecting content, rendering to the screen, and checking the
29/// layout status.
30/// It typically contains child elements, which are also instances of Node.
31///
32/// Users are expected to derive from this class to create custom elements.
33///
34/// A list of builtin elements can be found in the `elements.hpp` file.
35///
36/// @ingroup dom
37class Node {
38 public:
40 explicit Node(Elements children);
41 Node(const Node&) = delete;
42 Node(const Node&&) = delete;
43 Node& operator=(const Node&) = delete;
44 Node& operator=(const Node&&) = delete;
45
46 virtual ~Node();
47
48 // Step 1: Compute layout requirement. Tell parent what dimensions this
49 // element wants to be.
50 // Propagated from Children to Parents.
51 virtual void ComputeRequirement();
53
54 // Step 2: Assign this element its final dimensions.
55 // Propagated from Parents to Children.
56 virtual void SetBox(Box box);
57
58 // Step 3: (optional) Selection
59 // Propagated from Parents to Children.
60 virtual void Select(Selection& selection);
61
62 // Step 4: Draw this element.
63 virtual void Render(Screen& screen);
64
65 virtual std::string GetSelectedContent(Selection& selection);
66
67 // Layout may not resolve within a single iteration for some elements. This
68 // allows them to request additionnal iterations. This signal must be
69 // forwarded to children at least once.
70 struct Status {
71 int iteration = 0;
72 bool need_iteration = false;
73 };
74 virtual void Check(Status* status);
75
76 friend void Render(Screen& screen, Node* node, Selection& selection);
77
78 protected:
82};
83
84void Render(Screen& screen, const Element& element);
85void Render(Screen& screen, Node* node);
86void Render(Screen& screen, Node* node, Selection& selection);
87std::string GetNodeSelectedContent(Screen& screen,
88 Node* node,
89 Selection& selection);
90
91} // namespace ftxui
92
93#endif // FTXUI_DOM_NODE_HPP
virtual void Select(Selection &selection)
Compute the selection of an element.
Definition node.cpp:46
Elements children_
Definition node.hpp:79
virtual std::string GetSelectedContent(Selection &selection)
Definition node.cpp:72
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:41
Requirement requirement_
Definition node.hpp:80
Requirement requirement()
Definition node.hpp:52
virtual void ComputeRequirement()
Compute how much space an element needs.
Definition node.cpp:20
virtual void Check(Status *status)
Definition node.cpp:65
virtual ~Node()
Node & operator=(const Node &)=delete
Node(const Node &)=delete
Box box_
Definition node.hpp:81
friend void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:96
Node & operator=(const Node &&)=delete
Node(const Node &&)=delete
Node is the base class for all elements in the DOM tree.
Definition node.hpp:37
Represents a selection in a terminal user interface.
Definition selection.hpp:22
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
Requirement is a structure that defines the layout requirements for a Node in the terminal user inter...
A rectangular grid of Pixel.
Definition screen.hpp:27
Box is a structure that represents a rectangular area in a 2D space.
Definition box.hpp:16
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::string GetNodeSelectedContent(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:168
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::vector< Element > Elements
Definition elements.hpp:23