FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
flexbox.cpp
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#include <algorithm> // for min, max
5#include <cstddef> // for size_t
6#include <memory> // for __shared_ptr_access, shared_ptr, allocator_traits<>::value_type, make_shared
7#include <tuple> // for ignore
8#include <utility> // for move, swap
9#include <vector> // for vector
10
11#include "ftxui/dom/elements.hpp" // for Element, Elements, flexbox, hflow, vflow
12#include "ftxui/dom/flexbox_config.hpp" // for FlexboxConfig, FlexboxConfig::Direction, FlexboxConfig::Direction::Column, FlexboxConfig::AlignContent, FlexboxConfig::Direction::ColumnInversed, FlexboxConfig::Direction::Row, FlexboxConfig::JustifyContent, FlexboxConfig::Wrap, FlexboxConfig::AlignContent::FlexStart, FlexboxConfig::Direction::RowInversed, FlexboxConfig::JustifyContent::FlexStart, FlexboxConfig::Wrap::Wrap
13#include "ftxui/dom/flexbox_helper.hpp" // for Block, Global, Compute
14#include "ftxui/dom/node.hpp" // for Node, Elements, Node::Status
15#include "ftxui/dom/requirement.hpp" // for Requirement
16#include "ftxui/dom/selection.hpp" // for Selection
17#include "ftxui/screen/box.hpp" // for Box
18
19namespace ftxui {
20
21namespace {
22void Normalize(FlexboxConfig::Direction& direction) {
23 switch (direction) {
24 case FlexboxConfig::Direction::Row:
25 case FlexboxConfig::Direction::RowInversed: {
26 direction = FlexboxConfig::Direction::Row;
27 } break;
28 case FlexboxConfig::Direction::Column:
29 case FlexboxConfig::Direction::ColumnInversed: {
30 direction = FlexboxConfig::Direction::Column;
31 } break;
32 }
33}
34
35void Normalize(FlexboxConfig::AlignContent& align_content) {
36 align_content = FlexboxConfig::AlignContent::FlexStart;
37}
38
39void Normalize(FlexboxConfig::JustifyContent& justify_content) {
40 justify_content = FlexboxConfig::JustifyContent::FlexStart;
41}
42
43void Normalize(FlexboxConfig::Wrap& wrap) {
44 wrap = FlexboxConfig::Wrap::Wrap;
45}
46
47FlexboxConfig Normalize(FlexboxConfig config) {
48 Normalize(config.direction);
49 Normalize(config.wrap);
50 Normalize(config.justify_content);
51 Normalize(config.align_content);
52 return config;
53}
54
55class Flexbox : public Node {
56 public:
57 Flexbox(Elements children, FlexboxConfig config)
58 : Node(std::move(children)),
59 config_(config),
60 config_normalized_(Normalize(config)) {
61 requirement_.flex_grow_x = 1;
62 requirement_.flex_grow_y = 0;
63
64 if (IsColumnOriented()) {
65 std::swap(requirement_.flex_grow_x, requirement_.flex_grow_y);
66 }
67 }
68
69 bool IsColumnOriented() const {
70 return config_.direction == FlexboxConfig::Direction::Column ||
71 config_.direction == FlexboxConfig::Direction::ColumnInversed;
72 }
73
74 void Layout(flexbox_helper::Global& global,
75 bool compute_requirement = false) {
76 global.blocks.reserve(children_.size());
77 for (auto& child : children_) {
78 flexbox_helper::Block block;
79 block.min_size_x = child->requirement().min_x;
80 block.min_size_y = child->requirement().min_y;
81 if (!compute_requirement) {
82 block.flex_grow_x = child->requirement().flex_grow_x;
83 block.flex_grow_y = child->requirement().flex_grow_y;
84 block.flex_shrink_x = child->requirement().flex_shrink_x;
85 block.flex_shrink_y = child->requirement().flex_shrink_y;
86 }
87 global.blocks.push_back(block);
88 }
89
91 }
92
93 void ComputeRequirement() override {
94 requirement_ = Requirement{};
95 for (auto& child : children_) {
96 child->ComputeRequirement();
97 }
98 global_ = flexbox_helper::Global();
100 if (IsColumnOriented()) {
101 global_.size_x = 100000; // NOLINT
102 global_.size_y = asked_;
103 } else {
104 global_.size_x = asked_;
105 global_.size_y = 100000; // NOLINT
106 }
107 Layout(global_, true);
108
109 if (global_.blocks.empty()) {
110 return;
111 }
112
113 // すべてのブロックの和集合を計算する:
114 Box box;
115 box.x_min = global_.blocks[0].x;
116 box.y_min = global_.blocks[0].y;
117 box.x_max = global_.blocks[0].x + global_.blocks[0].dim_x;
118 box.y_max = global_.blocks[0].y + global_.blocks[0].dim_y;
119 for (auto& b : global_.blocks) {
120 box.x_min = std::min(box.x_min, b.x);
121 box.y_min = std::min(box.y_min, b.y);
122 box.x_max = std::max(box.x_max, b.x + b.dim_x);
123 box.y_max = std::max(box.y_max, b.y + b.dim_y);
124 }
125 requirement_.min_x = box.x_max - box.x_min;
126 requirement_.min_y = box.y_max - box.y_min;
127
128 // 選択範囲を見つける:
129 for (size_t i = 0; i < children_.size(); ++i) {
130 if (requirement_.focused.Prefer(children_[i]->requirement().focused)) {
131 requirement_.focused = children_[i]->requirement().focused;
132 // |focused.box|をこのコンポーネント内の位置に応じてシフトする:
133 auto& b = global_.blocks[i];
134 requirement_.focused.box.Shift(b.x, b.y);
135 requirement_.focused.box =
136 Box::Intersection(requirement_.focused.box, box);
137 }
138 }
139 }
140
141 void SetBox(Box box) override {
142 Node::SetBox(box);
143
144 const int asked_previous = asked_;
145 asked_ = std::min(asked_, IsColumnOriented() ? box.y_max - box.y_min + 1
146 : box.x_max - box.x_min + 1);
147 need_iteration_ = (asked_ != asked_previous);
148
149 flexbox_helper::Global global;
150 global.config = config_;
151 global.size_x = box.x_max - box.x_min + 1;
152 global.size_y = box.y_max - box.y_min + 1;
153 Layout(global);
154
155 for (size_t i = 0; i < children_.size(); ++i) {
156 auto& child = children_[i];
157 auto& b = global.blocks[i];
158
159 Box children_box;
160 children_box.x_min = box.x_min + b.x;
161 children_box.y_min = box.y_min + b.y;
162 children_box.x_max = box.x_min + b.x + b.dim_x - 1;
163 children_box.y_max = box.y_min + b.y + b.dim_y - 1;
164
165 const Box intersection = Box::Intersection(children_box, box);
166 child->SetBox(intersection);
167
168 need_iteration_ |= (intersection != children_box);
169 }
170 }
171
172 void Select(Selection& selection) override {
173 // このNode box_が選択範囲と交差しない場合、選択は行われない。
174 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
175 return;
176 }
177
178 Selection selection_lines = IsColumnOriented()
179 ? selection.SaturateVertical(box_)
180 : selection.SaturateHorizontal(box_);
181
182 size_t i = 0;
183 for (auto& line : global_.lines) {
184 Box box;
185 box.x_min = box_.x_min + line.x;
186 box.x_max = box_.x_min + line.x + line.dim_x - 1;
187 box.y_min = box_.y_min + line.y;
188 box.y_max = box_.y_min + line.y + line.dim_y - 1;
189
190 // 行ボックスが選択範囲と交差しない場合、選択なし。
191 if (Box::Intersection(selection.GetBox(), box).IsEmpty()) {
192 i += line.blocks.size();
193 continue;
194 }
195
196 Selection selection_line = IsColumnOriented()
197 ? selection_lines.SaturateHorizontal(box)
198 : selection_lines.SaturateVertical(box);
199
200 for (auto& block : line.blocks) {
201 std::ignore = block;
202 children_[i]->Select(selection_line);
203 i++;
204 }
205 }
206 }
207
208 void Check(Status* status) override {
209 for (auto& child : children_) {
210 child->Check(status);
211 }
212
213 if (status->iteration == 0) {
214 asked_ = 6000; // NOLINT
215 need_iteration_ = true;
216 }
217
218 status->need_iteration |= need_iteration_;
219 }
220
221 int asked_ = 6000; // NOLINT
222 bool need_iteration_ = true;
223 const FlexboxConfig config_;
224 const FlexboxConfig config_normalized_;
225 flexbox_helper::Global global_;
226};
227
228} // namespace
229
230/// @brief 行/列に要素を表示し、一杯になると次の列/行に折り返すことができるコンテナ。
231/// @param children コンテナ内の要素
232/// @param config オプション
233/// @return コンテナ。
234///
235/// #### 例
236///
237/// ```cpp
238/// flexbox({
239/// text("element 1"),
240/// text("element 2"),
241/// text("element 3"),
242/// }, FlexboxConfig()
243/// .Set(FlexboxConfig::Direction::Column)
244/// .Set(FlexboxConfig::Wrap::WrapInversed)
245/// .SetGapMainAxis(1)
246/// .SetGapCrossAxis(1)
247/// )
248/// ```
249Element flexbox(Elements children, FlexboxConfig config) {
250 return std::make_shared<Flexbox>(std::move(children), config);
251}
252
253/// @brief 要素を左から右へ行で表示するコンテナ。一杯になると、下の新しい行から開始します。
254/// @param children コンテナ内の要素
255/// @return コンテナ。
256///
257/// #### 例
258///
259/// ```cpp
260/// hflow({
261/// text("element 1"),
262/// text("element 2"),
263/// text("element 3"),
264/// });
265/// ```
267 return flexbox(std::move(children), FlexboxConfig());
268}
269
270/// @brief 要素を上から下へ行で表示するコンテナ。一杯になると、右側の新しい列から開始します。
271/// @param children コンテナ内の要素
272/// @return コンテナ。
273///
274/// #### 例
275///
276/// ```cpp
277/// vflow({
278/// text("element 1"),
279/// text("element 2"),
280/// text("element 3"),
281/// });
282/// ```
284 return flexbox(std::move(children),
285 FlexboxConfig().Set(FlexboxConfig::Direction::Column));
286}
287
288} // namespace ftxui
int asked_
Definition flexbox.cpp:221
bool need_iteration_
Definition flexbox.cpp:222
const FlexboxConfig config_
Definition flexbox.cpp:223
const FlexboxConfig config_normalized_
Definition flexbox.cpp:224
flexbox_helper::Global global_
Definition flexbox.cpp:225
void Compute(Global &global)
FTXUI ftxui::名前空間
Definition animation.hpp:11
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
行/列に要素を表示し、一杯になると次の列/行に折り返すことができるコンテナ。
Definition flexbox.cpp:249
std::shared_ptr< Node > Element
Definition elements.hpp:25
FTXUI_EXPORT(DOM) Element gridbox(std Element hflow(Elements)
要素を左から右へ行で表示するコンテナ。一杯になると、下の新しい行から開始します。
Definition flexbox.cpp:266
Direction direction
Definition elements.hpp:80
std::vector< Element > Elements
Definition elements.hpp:26
Element vflow(Elements)
要素を上から下へ行で表示するコンテナ。一杯になると、右側の新しい列から開始します。
Definition flexbox.cpp:283