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. Todos los derechos reservados.
2// El uso de este código fuente se rige por la licencia MIT que se puede encontrar en
3// el archivo LICENSE.
4
5#include "ftxui/dom/selection.hpp" // for Selection
6#include <algorithm> // for max, min
7#include <string> // for string
8#include <tuple> // for ignore
9
10#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
11
12namespace ftxui {
13
14namespace {
15class Unselectable : public NodeDecorator {
16 public:
18
19 void Select(Selection& ignored) override {
20 std::ignore = ignored;
21 // Sobreescribe el método select para no hacer nada.
22 }
23};
24} // namespace
25
26/// @brief Crea una selección vacía.
27Selection::Selection() = default;
28
29/// @brief Crea una selección.
30/// @param start_x La coordenada x del inicio de la selección.
31/// @param start_y La coordenada y del inicio de la selección.
32/// @param end_x La coordenada x del final de la selección.
33/// @param end_y La coordenada y del final de la selección.
34Selection::Selection(int start_x, int start_y, int end_x, int end_y)
35 : start_x_(start_x),
36 start_y_(start_y),
37 end_x_(end_x),
38 end_y_(end_y),
39 box_{
40 std::min(start_x, end_x),
41 std::max(start_x, end_x),
42 std::min(start_y, end_y),
43 std::max(start_y, end_y),
44 },
45 empty_(false) {}
46
47Selection::Selection(int start_x,
48 int start_y,
49 int end_x,
50 int end_y,
51 Selection* parent)
52 : start_x_(start_x),
53 start_y_(start_y),
54 end_x_(end_x),
55 end_y_(end_y),
56 box_{
57 std::min(start_x, end_x),
58 std::max(start_x, end_x),
59 std::min(start_y, end_y),
60 std::max(start_y, end_y),
61 },
62 parent_(parent),
63 empty_(false) {}
64
65/// @brief Obtiene el cuadro de la selección.
66/// @return El cuadro de la selección.
67const Box& Selection::GetBox() const {
68 return box_;
69}
70
71/// @brief Satura la selección para que esté dentro del cuadro.
72/// Esto es llamado por `hbox` para propagar la selección a sus hijos.
73/// @param box El cuadro en el que saturar la selección.
74/// @return La selección saturada.
76 int start_x = start_x_;
77 int start_y = start_y_;
78 int end_x = end_x_;
79 int end_y = end_y_;
80
81 const bool start_outside = !box.Contain(start_x, start_y);
82 const bool end_outside = !box.Contain(end_x, end_y);
83 const bool properly_ordered =
84 start_y < end_y || (start_y == end_y && start_x <= end_x);
85 if (properly_ordered) {
86 if (start_outside) {
87 start_x = box.x_min;
88 start_y = box.y_min;
89 }
90 if (end_outside) {
91 end_x = box.x_max;
92 end_y = box.y_max;
93 }
94 } else {
95 if (start_outside) {
96 start_x = box.x_max;
97 start_y = box.y_max;
98 }
99 if (end_outside) {
100 end_x = box.x_min;
101 end_y = box.y_min;
102 }
103 }
104 return {
105 start_x, start_y, end_x, end_y, parent_,
106 };
107}
108
109/// @brief Satura la selección para que esté dentro del cuadro.
110/// Esto es llamado por `vbox` para propagar la selección a sus hijos.
111/// @param box El cuadro en el que saturar la selección.
112/// @return La selección saturada.
114 int start_x = start_x_;
115 int start_y = start_y_;
116 int end_x = end_x_;
117 int end_y = end_y_;
118
119 const bool start_outside = !box.Contain(start_x, start_y);
120 const bool end_outside = !box.Contain(end_x, end_y);
121 const bool properly_ordered =
122 start_y < end_y || (start_y == end_y && start_x <= end_x);
123
124 if (properly_ordered) {
125 if (start_outside) {
126 start_x = box.x_min;
127 start_y = box.y_min;
128 }
129 if (end_outside) {
130 end_x = box.x_max;
131 end_y = box.y_max;
132 }
133 } else {
134 if (start_outside) {
135 start_x = box.x_max;
136 start_y = box.y_max;
137 }
138 if (end_outside) {
139 end_x = box.x_min;
140 end_y = box.y_min;
141 }
142 }
143 return {start_x, start_y, end_x, end_y, parent_};
144}
145
146void Selection::AddPart(const std::string& part, int y, int left, int right) {
147 if (parent_ != this) {
148 parent_->AddPart(part, y, left, right);
149 return;
150 }
151 [&] {
152 if (parts_.str().empty()) {
153 parts_ << part;
154 return;
155 }
156
157 if (y_ != y) {
158 parts_ << '\n' << part;
159 return;
160 }
161
162 if (x_ == left + 1) {
163 parts_ << part;
164 return;
165 }
166
167 parts_ << part;
168 }();
169 y_ = y;
170 x_ = right;
171}
172
173} // namespace ftxui
NodeDecorator(Element child)
const Box & GetBox() const
Obtiene el cuadro de la selección.
void AddPart(const std::string &part, int y, int left, int right)
Selection SaturateVertical(Box box)
Satura la selección para que esté dentro del cuadro. Esto es llamado por vbox para propagar la selecc...
Selection()
Crea una selección vacía.
Selection SaturateHorizontal(Box box)
Satura la selección para que esté dentro del cuadro. Esto es llamado por hbox para propagar la selecc...
Representa una selección en una interfaz de usuario de terminal.
Definition selection.hpp:22
bool Contain(int x, int y) const
Definition box.cpp:42
int x_max
Definition box.hpp:18
int y_min
Definition box.hpp:19
int y_max
Definition box.hpp:20
int x_min
Definition box.hpp:17
Box es una estructura que representa un área rectangular en un espacio 2D.
Definition box.hpp:16
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:10
std::uint8_t left
Definition screen.cpp:130
std::uint8_t right
Definition screen.cpp:132