FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
node.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// このソースコードの使用は、LICENSE ファイルにある MIT ライセンスによって管理されます。
3// the LICENSE file.
4#include <ftxui/screen/box.hpp> // for Box
5#include <string>
6#include <utility> // for move
7
8#include <cstddef>
9#include "ftxui/dom/node.hpp"
10#include "ftxui/dom/selection.hpp" // for Selection
11#include "ftxui/screen/screen.hpp" // for Screen
12
13namespace ftxui {
14
15Node::Node() = default;
16Node::Node(Elements children) : children_(std::move(children)) {}
17Node::~Node() = default;
18
19/// @brief 要素が必要とするスペースを計算します。
21 if (children_.empty()) {
22 return;
23 }
24 for (auto& child : children_) {
25 child->ComputeRequirement();
26 }
27
28 // By default, the requirement is the one of the first child.
29 requirement_ = children_[0]->requirement();
30
31 // Propagate the focused requirement.
32 for (size_t i = 1; i < children_.size(); ++i) {
34 children_[i]->requirement().focused.enabled) {
35 requirement_.focused = children_[i]->requirement().focused;
36 }
37 }
38}
39
40/// @brief 描画のために要素に位置と次元を割り当てます。
41void Node::SetBox(Box box) {
42 box_ = box;
43}
44
45/// @brief 要素の選択を計算します。
46void Node::Select(Selection& selection) {
47 // このノードのbox_が選択範囲と交差しない場合、選択は行われません。
48 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
49 return;
50 }
51
52 // By default we defer the selection to the children.
53 for (auto& child : children_) {
54 child->Select(selection);
55 }
56}
57
58/// @brief 要素をftxui::Screenに表示します。
59void Node::Render(Screen& screen) {
60 for (auto& child : children_) {
61 child->Render(screen);
62 }
63}
64
65void Node::Check(Status* status) {
66 for (auto& child : children_) {
67 child->Check(status);
68 }
69 status->need_iteration |= (status->iteration == 0);
70}
71
72std::string Node::GetSelectedContent(Selection& selection) {
73 std::string content;
74
75 for (auto& child : children_) {
76 content += child->GetSelectedContent(selection);
77 }
78
79 return content;
80}
81
82/// @brief 要素をftxui::Screenに表示します。
83/// @ingroup dom
84void Render(Screen& screen, const Element& element) {
85 Selection selection;
86 Render(screen, element.get(), selection);
87}
88
89/// @brief 要素をftxui::Screenに表示します。
90/// @ingroup dom
91void Render(Screen& screen, Node* node) {
92 Selection selection;
93 Render(screen, node, selection);
94}
95
96void Render(Screen& screen, Node* node, Selection& selection) {
97 Box box;
98 box.x_min = 0;
99 box.y_min = 0;
100 box.x_max = screen.dimx() - 1;
101 box.y_max = screen.dimy() - 1;
102
103 Node::Status status;
104 node->Check(&status);
105 const int max_iterations = 20;
106 while (status.need_iteration && status.iteration < max_iterations) {
107 // ステップ1: この要素がどの次元になるかを検索します。
108 node->ComputeRequirement();
109
110 // ステップ2: 要素に次元を割り当てます。
111 node->SetBox(box);
112
113 // 要素がレイアウトアルゴリズムの別のイテレーションを必要とするかどうかを確認します。
114 status.need_iteration = false;
115 status.iteration++;
116 node->Check(&status);
117 }
118
119 // ステップ3: 選択
120 if (!selection.IsEmpty()) {
121 node->Select(selection);
122 }
123
124 if (node->requirement().focused.enabled
125#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
126 // カーソルを正しい位置に設定すると、CJK (中国語、日本語、韓国語など) 文字を使用するユーザーは、
127 // [入力メソッドエディタ] が正しい場所に表示されるのを確認できます。 [issue] を参照してください。
128 //
129 // [input method editor]:
130 // https://en.wikipedia.org/wiki/Input_method
131 //
132 // [issue]:
133 // https://github.com/ArthurSonzogni/FTXUI/issues/2#issuecomment-505282355
134 //
135 // 残念ながら、Microsoft Terminal はカーソルを適切に非表示にすることを処理しません。
136 // 代わりに、カーソルの下の文字が非表示になり、これは大きな問題です。
137 // その結果、カーソルを正しい位置に設定することはできません。
138 // 右下隅に表示されます。
139 // 参照:
140 // https://github.com/microsoft/terminal/issues/1203
141 // https://github.com/microsoft/terminal/issues/3093
142 &&
144#endif
145 ) {
150 });
151 } else {
153 screen.dimx() - 1,
154 screen.dimy() - 1,
156 });
157 }
158
159 // ステップ4: 要素を描画します。
160 screen.stencil = box;
161 node->Render(screen);
162
163 // ステップ5: シェーダーを適用します。
164 screen.ApplyShader();
165}
166
167std::string GetNodeSelectedContent(Screen& screen,
168 Node* node,
169 Selection& selection) {
170 Box box;
171 box.x_min = 0;
172 box.y_min = 0;
173 box.x_max = screen.dimx() - 1;
174 box.y_max = screen.dimy() - 1;
175
176 Node::Status status;
177 node->Check(&status);
178 const int max_iterations = 20;
179 while (status.need_iteration && status.iteration < max_iterations) {
180 // ステップ1: この要素がどの次元になるかを検索します。
181 node->ComputeRequirement();
182
183 // ステップ2: 要素に次元を割り当てます。
184 node->SetBox(box);
185
186 // 要素がレイアウトアルゴリズムの別のイテレーションを必要とするかどうかを確認します。
187 status.need_iteration = false;
188 status.iteration++;
189 node->Check(&status);
190 }
191
192 // ステップ3: 選択
193 node->Select(selection);
194
195 // ステップ4: 選択されたコンテンツを取得します。
196 return node->GetSelectedContent(selection);
197}
198
199} // namespace ftxui
const Box & GetBox() const
選択範囲のボックスを取得します。
virtual void Select(Selection &selection)
要素の選択を計算します。
Definition node.cpp:46
Elements children_
Definition node.hpp:76
virtual std::string GetSelectedContent(Selection &selection)
Definition node.cpp:72
virtual void SetBox(Box box)
描画のために要素に位置と次元を割り当てます。
Definition node.cpp:41
Requirement requirement_
Definition node.hpp:77
Requirement requirement()
Definition node.hpp:50
virtual void ComputeRequirement()
要素が必要とするスペースを計算します。
Definition node.cpp:20
virtual void Check(Status *status)
Definition node.cpp:65
virtual ~Node()
virtual void Render(Screen &screen)
要素をftxui::Screenに表示します。
Definition node.cpp:59
bool IsEmpty() const
Definition selection.hpp:29
Box box_
Definition node.hpp:78
friend void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:96
NodeはDOMツリー内のすべての要素の基底クラスです。
Definition node.hpp:36
ターミナルユーザーインターフェースにおける選択範囲を表します。
Definition selection.hpp:20
void Render(Screen &screen, const Element &element)
要素をftxui::Screenに表示します。
Definition node.cpp:84
void ApplyShader()
Definition screen.cpp:499
int dimy() const
Definition image.hpp:36
void SetCursor(Cursor cursor)
Definition screen.hpp:65
int x_max
Definition box.hpp:16
int y_min
Definition box.hpp:17
static auto Intersection(Box a, Box b) -> Box
Definition box.cpp:10
Box stencil
Definition image.hpp:41
int y_max
Definition box.hpp:18
int x_min
Definition box.hpp:15
int dimx() const
Definition image.hpp:35
ピクセルの長方形グリッド。
Definition screen.hpp:25
Boxは、2D空間における矩形領域を表す構造体です。
Definition box.hpp:14
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
std::string GetNodeSelectedContent(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:167
std::shared_ptr< Node > Element
Definition elements.hpp:21
std::vector< Element > Elements
Definition elements.hpp:22
Screen::Cursor::Shape cursor_shape