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#include "ftxui/util/export.hpp"
15
16namespace ftxui {
17
18class Node;
19class Screen;
20
21using Element = std::shared_ptr<Node>;
22using Elements = std::vector<Element>;
23
24/// @brief Node is the base class for all elements in the DOM tree.
25///
26/// It represents a single node in the document object model (DOM) and provides
27/// the basic structure for layout and rendering.
28/// It contains methods for computing layout requirements, setting the box
29/// dimensions, selecting content, rendering to the screen, and checking the
30/// layout status.
31/// It typically contains child elements, which are also instances of Node.
32///
33/// Users are expected to derive from this class to create custom elements.
34///
35/// A list of builtin elements can be found in the `elements.hpp` file.
36///
37/// @ingroup dom
38class FTXUI_EXPORT(DOM) Node {
39 public:
40 Node();
41 explicit Node(Elements children);
42 Node(const Node&) = delete;
43 Node(const Node&&) = delete;
44 Node& operator=(const Node&) = delete;
45 Node& operator=(const Node&&) = delete;
46
47 virtual ~Node();
48
49 // Step 1: Compute layout requirement. Tell parent what dimensions this
50 // element wants to be.
51 // Propagated from Children to Parents.
52 virtual void ComputeRequirement();
53 Requirement requirement() { return requirement_; }
54
55 // Step 2: Assign this element its final dimensions.
56 // Propagated from Parents to Children.
57 virtual void SetBox(Box box);
58
59 // Step 3: (optional) Selection
60 // Propagated from Parents to Children.
61 virtual void Select(Selection& selection);
62
63 // Step 4: Draw this element.
64 virtual void Render(Screen& screen);
65
66 virtual std::string GetSelectedContent(Selection& selection);
67
68 // Layout may not resolve within a single iteration for some elements. This
69 // allows them to request additional iterations. This signal must be
70 // forwarded to children at least once.
71 struct Status {
72 int iteration = 0;
73 bool need_iteration = false;
74 };
75 virtual void Check(Status* status);
76
77 // ABI Reserve:
78 virtual void Reserved1();
79 virtual void Reserved2();
80 virtual void Reserved3();
81 virtual void Reserved4();
82 virtual void Reserved5();
83 virtual void Reserved6();
84 virtual void Reserved7();
85 virtual void Reserved8();
86
87 friend void Render(Screen& screen, Node* node, Selection& selection);
88
89 protected:
90 Elements children_;
91 Requirement requirement_;
92 Box box_;
93};
94
95FTXUI_EXPORT(DOM) void Render(Screen& screen, const Element& element);
96FTXUI_EXPORT(DOM) void Render(Screen& screen, Node* node);
97FTXUI_EXPORT(DOM)
98void Render(Screen& screen, Node* node, Selection& selection);
99FTXUI_EXPORT(DOM)
100std::string GetNodeSelectedContent(Screen& screen,
101 Node* node,
102 Selection& selection);
103
104} // namespace ftxui
105
106#endif // FTXUI_DOM_NODE_HPP
#define FTXUI_EXPORT(component)
Definition export.hpp:24
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
std::shared_ptr< Node > Element
Definition elements.hpp:24
std::vector< Element > Elements
Definition elements.hpp:25