FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
src/ftxui/dom/selection.cpp
浏览该文件的文档.
1// Copyright 2024 Arthur Sonzogni. All rights reserved.
2// 本源代码的使用受 MIT 许可协议的约束,该协议可在 LICENSE 文件中找到。
3
4#include "ftxui/dom/selection.hpp" // for Selection
5#include <algorithm> // for max, min
6#include <string> // for string
7#include <tuple> // for ignore
8
9#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
10
11namespace ftxui {
12
13namespace {
14class Unselectable : public NodeDecorator {
15 public:
17
18 void Select(Selection& ignored) override {
19 std::ignore = ignored;
20 // 覆盖 select 方法,使其不执行任何操作。
21 }
22};
23} // namespace
24
25/// @brief 创建一个空选择。
26Selection::Selection() = default;
27
28/// @brief 创建一个选择。
29/// @param start_x 选择起始的x坐标。
30/// @param start_y 选择起始的y坐标。
31/// @param end_x 选择结束的x坐标。
32/// @param end_y 选择结束的y坐标。
33Selection::Selection(int start_x, int start_y, int end_x, int end_y)
34 : start_x_(start_x),
35 start_y_(start_y),
36 end_x_(end_x),
37 end_y_(end_y),
38 box_{
39 std::min(start_x, end_x),
40 std::max(start_x, end_x),
41 std::min(start_y, end_y),
42 std::max(start_y, end_y),
43 },
44 empty_(false) {}
45
46Selection::Selection(int start_x,
47 int start_y,
48 int end_x,
49 int end_y,
50 Selection* parent)
51 : start_x_(start_x),
52 start_y_(start_y),
53 end_x_(end_x),
54 end_y_(end_y),
55 box_{
56 std::min(start_x, end_x),
57 std::max(start_x, end_x),
58 std::min(start_y, end_y),
59 std::max(start_y, end_y),
60 },
61 parent_(parent),
62 empty_(false) {}
63
64/// @brief 获取选择框。
65/// @return 选择框。
66const Box& Selection::GetBox() const {
67 return box_;
68}
69
70/// @brief 将选择饱和到框内。
71/// 这由 `hbox` 调用,以将选择传播到其子级。
72/// @param box 用于饱和选择的框。
73/// @return 饱和后的选择。
75 int start_x = start_x_;
76 int start_y = start_y_;
77 int end_x = end_x_;
78 int end_y = end_y_;
79
80 const bool start_outside = !box.Contain(start_x, start_y);
81 const bool end_outside = !box.Contain(end_x, end_y);
82 const bool properly_ordered =
83 start_y < end_y || (start_y == end_y && start_x <= end_x);
84 if (properly_ordered) {
85 if (start_outside) {
86 start_x = box.x_min;
87 start_y = box.y_min;
88 }
89 if (end_outside) {
90 end_x = box.x_max;
91 end_y = box.y_max;
92 }
93 } else {
94 if (start_outside) {
95 start_x = box.x_max;
96 start_y = box.y_max;
97 }
98 if (end_outside) {
99 end_x = box.x_min;
100 end_y = box.y_min;
101 }
102 }
103 return {
104 start_x, start_y, end_x, end_y, parent_,
105 };
106}
107
108/// @brief 将选择饱和到框内。
109/// 这由 `vbox` 调用,以将选择传播到其子级。
110/// @param box 用于饱和选择的框。
111/// @return 饱和后的选择。
113 int start_x = start_x_;
114 int start_y = start_y_;
115 int end_x = end_x_;
116 int end_y = end_y_;
117
118 const bool start_outside = !box.Contain(start_x, start_y);
119 const bool end_outside = !box.Contain(end_x, end_y);
120 const bool properly_ordered =
121 start_y < end_y || (start_y == end_y && start_x <= end_x);
122
123 if (properly_ordered) {
124 if (start_outside) {
125 start_x = box.x_min;
126 start_y = box.y_min;
127 }
128 if (end_outside) {
129 end_x = box.x_max;
130 end_y = box.y_max;
131 }
132 } else {
133 if (start_outside) {
134 start_x = box.x_max;
135 start_y = box.y_max;
136 }
137 if (end_outside) {
138 end_x = box.x_min;
139 end_y = box.y_min;
140 }
141 }
142 return {start_x, start_y, end_x, end_y, parent_};
143}
144
145void Selection::AddPart(const std::string& part, int y, int left, int right) {
146 if (parent_ != this) {
147 parent_->AddPart(part, y, left, right);
148 return;
149 }
150 [&] {
151 if (parts_.str().empty()) {
152 parts_ << part;
153 return;
154 }
155
156 if (y_ != y) {
157 parts_ << '\n' << part;
158 return;
159 }
160
161 if (x_ == left + 1) {
162 parts_ << part;
163 return;
164 }
165
166 parts_ << part;
167 }();
168 y_ = y;
169 x_ = right;
170}
171
172} // namespace ftxui
const Box & GetBox() const
获取选择框。
void AddPart(const std::string &part, int y, int left, int right)
Selection SaturateVertical(Box box)
将选择饱和到框内。 这由 vbox 调用,以将选择传播到其子级。
Selection()
创建一个空选择。
Selection SaturateHorizontal(Box box)
将选择饱和到框内。 这由 hbox 调用,以将选择传播到其子级。
代表终端用户界面中的选择。
bool Contain(int x, int y) const
定义 box.cpp:42
Box是一个表示2D空间中矩形区域的结构体。
定义 box.hpp:15
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::uint8_t left
std::uint8_t right