FTXUI 6.1.9
C++ functional terminal UI.
Chargement...
Recherche...
Aucune correspondance
src/ftxui/dom/selection.cpp
Aller à la documentation de ce fichier.
1// Copyright 2024 Arthur Sonzogni. Tous droits réservés.
2// L'utilisation de ce code source est régie par la licence MIT qui peut être trouvée
3// dans le fichier 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 // Écrase la méthode de sélection pour ne rien faire.
22 }
23};
24} // namespace
25
26/// @brief Crée une sélection vide.
27Selection::Selection() = default;
28
29/// @brief Crée une sélection.
30/// @param start_x La coordonnée x du début de la sélection.
31/// @param start_y La coordonnée y du début de la sélection.
32/// @param end_x La coordonnée x de la fin de la sélection.
33/// @param end_y La coordonnée y de la fin de la sélection.
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 Récupère la boîte de sélection.
66/// @return La boîte de sélection.
67const Box& Selection::GetBox() const {
68 return box_;
69}
70
71/// @brief Sature la sélection pour qu'elle soit à l'intérieur de la boîte.
72/// Ceci est appelé par `hbox` pour propager la sélection à ses enfants.
73/// @param box La boîte dans laquelle saturer la sélection.
74/// @return La sélection saturée.
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 Sature la sélection pour qu'elle soit à l'intérieur de la boîte.
110/// Ceci est appelé par `vbox` pour propager la sélection à ses enfants.
111/// @param box La boîte dans laquelle saturer la sélection.
112/// @return La sélection saturée.
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
Récupère la boîte de sélection.
void AddPart(const std::string &part, int y, int left, int right)
Selection SaturateVertical(Box box)
Sature la sélection pour qu'elle soit à l'intérieur de la boîte. Ceci est appelé par vbox pour propag...
Selection()
Crée une sélection vide.
Selection SaturateHorizontal(Box box)
Sature la sélection pour qu'elle soit à l'intérieur de la boîte. Ceci est appelé par hbox pour propag...
Représente une sélection dans une interface utilisateur 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 est une structure qui représente une zone rectangulaire dans un espace 2D.
Definition box.hpp:16
L'espace de noms FTXUI ftxui::
Definition animation.hpp:10