FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/dom/selection.cpp
Go to the documentation of this file.
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
NodeDecorator(Element child)
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 呼叫,用於將選取範圍傳播到其子元素。
表示終端機使用者介面中的選取範圍。
Definition selection.hpp:21
bool Contain(int x, int y) const
Definition box.cpp:42
int x_max
Definition box.hpp:16
int y_min
Definition box.hpp:17
int y_max
Definition box.hpp:18
int x_min
Definition box.hpp:15
Box 是一個表示二維空間中矩形區域的結構。
Definition box.hpp:14
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::uint8_t left
Definition screen.cpp:130
std::uint8_t right
Definition screen.cpp:132