FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
dom/util.cpp
Go to the documentation of this file.
1// 版權所有 2020 Arthur Sonzogni. 保留所有權利。
2// 此原始碼受 MIT 授權條款約束,詳情請參閱
3// LICENSE 文件。
4#include <algorithm> // for min
5#include <functional> // for function
6#include <memory> // for __shared_ptr_access, make_unique
7#include <utility> // for move
8
9#include "ftxui/dom/elements.hpp" // for Element, Decorator, Elements, operator|, Fit, emptyElement, nothing, operator|=
10#include "ftxui/dom/node.hpp" // for Node, Node::Status
11#include "ftxui/dom/requirement.hpp" // for Requirement
12#include "ftxui/screen/box.hpp" // for Box
13#include "ftxui/screen/screen.hpp" // for Full
14#include "ftxui/screen/terminal.hpp" // for Dimensions
15
16namespace ftxui {
17
18namespace {
19Decorator compose(Decorator a, Decorator b) {
20 return [a = std::move(a), b = std::move(b)](Element element) {
21 return b(a(std::move(element)));
22 };
23}
24} // namespace
25
26/// @brief 一個什麼都不做的裝飾器。
27/// @ingroup dom
29 return element;
30}
31
32/// @brief 將兩個裝飾器組合為一個。
33/// @ingroup dom
34///
35/// ### 範例
36///
37/// ```cpp
38/// auto decorator = bold | blink;
39/// ```
41 return compose(std::move(a), //
42 std::move(b));
43}
44
45/// @brief 從一組元素中,對每個元素應用一個裝飾器。
46/// @return 一組被裝飾的元素。
47/// @ingroup dom
48Elements operator|(Elements elements, Decorator decorator) { // NOLINT
49 Elements output;
50 output.reserve(elements.size());
51 for (auto& it : elements) {
52 output.push_back(std::move(it) | decorator);
53 }
54 return output;
55}
56
57/// @brief 從一個元素中,應用一個裝飾器。
58/// @return 被裝飾的元素。
59/// @ingroup dom
60///
61/// ### 範例
62///
63/// 兩者是等效的:
64/// ```cpp
65/// bold(text("Hello"));
66/// ```
67/// ```cpp
68/// text("Hello") | bold;
69/// ```
70Element operator|(Element element, Decorator decorator) { // NOLINT
71 return decorator(std::move(element));
72}
73
74/// @brief 對元素應用一個裝飾器。
75/// @return 被裝飾的元素。
76/// @ingroup dom
77///
78/// ### 範例
79///
80/// 兩者是等效的:
81/// ```cpp
82/// auto element = text("Hello");
83/// element |= bold;
84/// ```
86 e = e | std::move(d);
87 return e;
88}
89
90/// 適合給定元素的最小尺寸。
91/// @see Fixed
92/// @see Full
93Dimensions Dimension::Fit(Element& e, bool extend_beyond_screen) {
94 const Dimensions fullsize = Dimension::Full();
95 Box box;
96 box.x_min = 0;
97 box.y_min = 0;
98 box.x_max = fullsize.dimx;
99 box.y_max = fullsize.dimy;
100
101 Node::Status status;
102 e->Check(&status);
103 const int max_iteration = 20;
104 while (status.need_iteration && status.iteration < max_iteration) {
105 e->ComputeRequirement();
106
107 // 不要給元素超過它所需的空間:
108 box.x_max = std::min(box.x_max, e->requirement().min_x);
109 box.y_max = e->requirement().min_y;
110 if (!extend_beyond_screen) {
111 box.y_max = std::min(box.y_max, fullsize.dimy);
112 }
113
114 e->SetBox(box);
115 status.need_iteration = false;
116 status.iteration++;
117 e->Check(&status);
118
119 if (!status.need_iteration) {
120 break;
121 }
122 // 增加框的大小直到它適應...
123 box.x_max = std::min(e->requirement().min_x, fullsize.dimx);
124 box.y_max = e->requirement().min_y;
125
126 // ... 但不要超出螢幕尺寸:
127 if (!extend_beyond_screen) {
128 box.y_max = std::min(box.y_max, fullsize.dimy);
129 }
130 }
131
132 return {
133 box.x_max,
134 box.y_max,
135 };
136}
137
138/// 一個 0x0 大小,不繪製任何東西的元素。
139/// @ingroup dom
141 class Impl : public Node {
142 void ComputeRequirement() override {
143 requirement_.min_x = 0;
144 requirement_.min_y = 0;
145 }
146 };
147 return std::make_unique<Impl>();
148}
149
150} // namespace ftxui
節點是DOM樹中所有元素的基底類別。
Definition node.hpp:34
Element nothing(Element element)
一個什麼都不做的裝飾器。
Definition dom/util.cpp:28
Element emptyElement()
Definition dom/util.cpp:140
Dimensions Fit(Element &, bool extend_beyond_screen=false)
Dimensions Full()
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::function< Element(Element)> Decorator
Definition elements.hpp:24
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::vector< Element > Elements
Definition elements.hpp:23
Component operator|(Component component, ComponentDecorator decorator)
Component & operator|=(Component &component, ComponentDecorator decorator)