FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
flex.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. 保留所有權利。
2// 本原始碼的使用受 MIT 授權條款約束,詳情請參閱
3// LICENSE 檔案。
4#include <memory> // for make_shared, __shared_ptr_access
5#include <utility> // for move
6
7#include "ftxui/dom/elements.hpp" // for Element, unpack, filler, flex, flex_grow, flex_shrink, notflex, xflex, xflex_grow, xflex_shrink, yflex, yflex_grow, yflex_shrink
8#include "ftxui/dom/node.hpp" // for Elements, Node
9#include "ftxui/dom/requirement.hpp" // for Requirement
10#include "ftxui/screen/box.hpp" // for Box
11
12namespace ftxui {
13
14namespace {
15
16using FlexFunction = void (*)(Requirement&);
17
18void function_flex_grow(Requirement& r) {
19 r.flex_grow_x = 1;
20 r.flex_grow_y = 1;
21}
22
23void function_xflex_grow(Requirement& r) {
24 r.flex_grow_x = 1;
25}
26
27void function_yflex_grow(Requirement& r) {
28 r.flex_grow_y = 1;
29}
30
31void function_flex_shrink(Requirement& r) {
32 r.flex_shrink_x = 1;
33 r.flex_shrink_y = 1;
34}
35
36void function_xflex_shrink(Requirement& r) {
37 r.flex_shrink_x = 1;
38}
39
40void function_yflex_shrink(Requirement& r) {
41 r.flex_shrink_y = 1;
42}
43
44void function_flex(Requirement& r) {
45 r.flex_grow_x = 1;
46 r.flex_grow_y = 1;
47 r.flex_shrink_x = 1;
48 r.flex_shrink_y = 1;
49}
50
51void function_xflex(Requirement& r) {
52 r.flex_grow_x = 1;
53 r.flex_shrink_x = 1;
54}
55
56void function_yflex(Requirement& r) {
57 r.flex_grow_y = 1;
58 r.flex_shrink_y = 1;
59}
60
61void function_not_flex(Requirement& r) {
62 r.flex_grow_x = 0;
63 r.flex_grow_y = 0;
64 r.flex_shrink_x = 0;
65 r.flex_shrink_y = 0;
66}
67
68class Flex : public Node {
69 public:
70 explicit Flex(FlexFunction f) : f_(f) {}
71 Flex(FlexFunction f, Element child) : Node(unpack(std::move(child))), f_(f) {}
72 void ComputeRequirement() override {
73 requirement_.min_x = 0;
74 requirement_.min_y = 0;
75 if (!children_.empty()) {
76 children_[0]->ComputeRequirement();
77 requirement_ = children_[0]->requirement();
78 }
79 f_(requirement_);
80 }
81
82 void SetBox(Box box) override {
83 Node::SetBox(box);
84 if (children_.empty()) {
85 return;
86 }
87 children_[0]->SetBox(box);
88 }
89
90 FlexFunction f_;
91};
92
93} // namespace
94
95/// @brief 一個元素,它會按比例擴展以佔據容器中剩餘的空間。
96/// @ingroup dom
98 return std::make_shared<Flex>(function_flex);
99}
100
101/// @brief 使子元素按比例擴展以佔據容器中剩餘的空間。
102
103/// @ingroup dom
104
105///
106
107/// #### 範例:
108
109///
110
111/// ~~~cpp
112
113/// hbox({
114
115/// text("left") | border ,
116
117/// text("middle") | border | flex,
118
119/// text("right") | border,
120
121/// });
122
123/// ~~~
124
125///
126
127/// #### 輸出:
128
129///
130
131/// ~~~bash
132
133/// ┌────┐┌─────────────────────────────────────────────────────────┐┌─────┐
134
135/// │left││middle ││right│
136
137/// └────┘└─────────────────────────────────────────────────────────┘└─────┘
138
139/// ~~~
141 return std::make_shared<Flex>(function_flex, std::move(child));
142}
143
144/// @brief 在 X 軸上盡可能擴展/在需要時最小化。
145/// @ingroup dom
147 return std::make_shared<Flex>(function_xflex, std::move(child));
148}
149
150/// @brief 在 Y 軸上盡可能擴展/在需要時最小化。
151/// @ingroup dom
153 return std::make_shared<Flex>(function_yflex, std::move(child));
154}
155
156/// @brief 盡可能擴展。
157/// @ingroup dom
159 return std::make_shared<Flex>(function_flex_grow, std::move(child));
160}
161
162/// @brief 在 X 軸上盡可能擴展。
163/// @ingroup dom
165 return std::make_shared<Flex>(function_xflex_grow, std::move(child));
166}
167
168/// @brief 在 Y 軸上盡可能擴展。
169/// @ingroup dom
171 return std::make_shared<Flex>(function_yflex_grow, std::move(child));
172}
173
174/// @brief 在需要時最小化。
175/// @ingroup dom
177 return std::make_shared<Flex>(function_flex_shrink, std::move(child));
178}
179
180/// @brief 在 X 軸上在需要時最小化。
181/// @ingroup dom
183 return std::make_shared<Flex>(function_xflex_shrink, std::move(child));
184}
185
186/// @brief 在 Y 軸上在需要時最小化。
187/// @ingroup dom
189 return std::make_shared<Flex>(function_yflex_shrink, std::move(child));
190}
191
192/// @brief 使元素不可伸縮。
193/// @ingroup dom
195 return std::make_shared<Flex>(function_not_flex, std::move(child));
196}
197
198} // namespace ftxui
FlexFunction f_
Definition flex.cpp:90
virtual void SetBox(Box box)
為元素分配繪圖位置和尺寸。
Definition node.cpp:40
Element xflex(Element)
在 X 軸上盡可能擴展/在需要時最小化。
Definition flex.cpp:146
Element xflex_grow(Element)
在 X 軸上盡可能擴展。
Definition flex.cpp:164
Element flex(Element)
使子元素按比例擴展以佔據容器中剩餘的空間。
Definition flex.cpp:140
Element yflex(Element)
在 Y 軸上盡可能擴展/在需要時最小化。
Definition flex.cpp:152
Element flex_shrink(Element)
在需要時最小化。
Definition flex.cpp:176
Element yflex_grow(Element)
在 Y 軸上盡可能擴展。
Definition flex.cpp:170
Element flex_grow(Element)
盡可能擴展。
Definition flex.cpp:158
Element notflex(Element)
使元素不可伸縮。
Definition flex.cpp:194
Element xflex_shrink(Element)
在 X 軸上在需要時最小化。
Definition flex.cpp:182
Element filler()
一個元素,它會按比例擴展以佔據容器中剩餘的空間。
Definition flex.cpp:97
Element yflex_shrink(Element)
在 Y 軸上在需要時最小化。
Definition flex.cpp:188
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22