FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_options.hpp
Go to the documentation of this file.
1// Copyright 2021 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#ifndef FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
5#define FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
6
7#include <chrono> // for milliseconds
8#include <ftxui/component/animation.hpp> // for Duration, QuadraticInOut, Function
9#include <ftxui/dom/direction.hpp> // for Direction, Direction::Left, Direction::Right, Direction::Down
10#include <ftxui/dom/elements.hpp> // for Element, separator
11#include <ftxui/util/ref.hpp> // for Ref, ConstRef, StringRef
13#include <functional> // for function
14#include <limits> // for numeric_limits
15#include <string> // for string
16
17#include "ftxui/component/component_base.hpp" // for Component
18#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
19#include "ftxui/util/export.hpp" // for FTXUI_EXPORT
20
21namespace ftxui {
22
23/// @brief arguments pour la transformation depuis |ButtonOption|, |CheckboxOption|,
24/// |RadioboxOption|, |MenuEntryOption|, |MenuOption|.
25struct FTXUI_EXPORT(COMPONENT) EntryState {
26 std::string label; ///< Le libellé à afficher.
27 bool state; ///< L'état du bouton/checkbox/radiobox
28 bool active; ///< Indique si l'entrée est active.
29 bool focused; ///< Indique si l'entrée est celle sur laquelle l'utilisateur a le focus.
30 int index; ///< Index de l'entrée lorsqu'applicable ou -1.
31};
32
33/// @brief Option pour l'effet de soulignement.
34/// @ingroup component
35struct FTXUI_EXPORT(COMPONENT) UnderlineOption {
36 bool enabled = false;
37
38 Color color_active = Color::White;
39 Color color_inactive = Color::GrayDark;
40
41 animation::easing::Function leader_function =
42 animation::easing::QuadraticInOut;
43 animation::easing::Function follower_function =
44 animation::easing::QuadraticInOut;
45
46 animation::Duration leader_duration = std::chrono::milliseconds(250);
47 animation::Duration leader_delay = std::chrono::milliseconds(0);
48 animation::Duration follower_duration = std::chrono::milliseconds(250);
49 animation::Duration follower_delay = std::chrono::milliseconds(0);
50
51 void SetAnimation(animation::Duration d, animation::easing::Function f);
52 void SetAnimationDuration(animation::Duration d);
53 void SetAnimationFunction(animation::easing::Function f);
54 void SetAnimationFunction(animation::easing::Function f_leader,
55 animation::easing::Function f_follower);
56};
57
58/// @brief Option concernant une couleur potentiellement animée.
59/// @ingroup component
60struct FTXUI_EXPORT(COMPONENT) AnimatedColorOption {
61 void Set(
62 Color inactive,
63 Color active,
64 animation::Duration duration = std::chrono::milliseconds(250),
65 animation::easing::Function function = animation::easing::QuadraticInOut);
66
67 bool enabled = false;
68 Color inactive;
69 Color active;
70 animation::Duration duration = std::chrono::milliseconds(250);
71 animation::easing::Function function = animation::easing::QuadraticInOut;
72};
73
74struct FTXUI_EXPORT(COMPONENT) AnimatedColorsOption {
75 AnimatedColorOption background;
76 AnimatedColorOption foreground;
77};
78
79/// @brief Option pour le composant MenuEntry.
80/// @ingroup component
81struct FTXUI_EXPORT(COMPONENT) MenuEntryOption {
82 ConstStringRef label = "MenuEntry";
83 std::function<Element(const EntryState& state)> transform;
84 AnimatedColorsOption animated_colors;
85};
86
87/// @brief Option pour le composant Menu.
88/// @ingroup component
89struct FTXUI_EXPORT(COMPONENT) MenuOption {
90 // Constructeurs standards:
91 static MenuOption Horizontal();
92 static MenuOption HorizontalAnimated();
93 static MenuOption Vertical();
94 static MenuOption VerticalAnimated();
95 static MenuOption Toggle();
96
97 ConstStringListRef entries; ///> La liste des entrées.
98 Ref<int> selected = 0; ///> L'index de l'entrée sélectionnée.
99
100 // Style :
101 UnderlineOption underline;
102 MenuEntryOption entries_option;
103 Direction direction = Direction::Down;
104 std::function<Element()> elements_prefix;
105 std::function<Element()> elements_infix;
106 std::function<Element()> elements_postfix;
107
108 // Observateurs:
109 std::function<void()> on_change; ///> Appelé lorsque l'entrée sélectionnée change.
110 std::function<void()> on_enter; ///> Appelé lorsque l'utilisateur appuie sur Entrée.
111 Ref<int> focused_entry = 0;
112};
113
114/// @brief Option pour le composant AnimatedButton.
115/// @ingroup component
116struct FTXUI_EXPORT(COMPONENT) ButtonOption {
117 // Constructeurs standards:
118 static ButtonOption Ascii();
119 static ButtonOption Simple();
120 static ButtonOption Border();
121 static ButtonOption Animated();
122 static ButtonOption Animated(Color color);
123 static ButtonOption Animated(Color background, Color foreground);
124 static ButtonOption Animated(Color background,
125 Color foreground,
126 Color background_active,
127 Color foreground_active);
128
129 ConstStringRef label = "Button";
130 std::function<void()> on_click = [] {};
131
132 // Style :
133 std::function<Element(const EntryState&)> transform;
134 AnimatedColorsOption animated_colors;
135};
136
137/// @brief Option pour le composant Checkbox.
138/// @ingroup component
139struct FTXUI_EXPORT(COMPONENT) CheckboxOption {
140 // Constructeurs standards:
141 static CheckboxOption Simple();
142
143 ConstStringRef label = "Checkbox";
144
145 Ref<bool> checked = false;
146
147 // Style :
148 std::function<Element(const EntryState&)> transform;
149
150 // Observateur:
151 /// Appelé lorsque l'utilisateur change l'état.
152 std::function<void()> on_change = [] {};
153};
154
155/// @brief Utilisé pour définir le style du composant Input.
156struct FTXUI_EXPORT(COMPONENT) InputState {
158 bool hovered; ///< Indique si l'entrée est survolée par la souris.
159 bool focused; ///< Indique si l'entrée a le focus de l'utilisateur.
160 bool is_placeholder; ///< Indique si l'entrée est vide et affiche le
161 ///< texte de substitution (placeholder).
162};
163
164/// @brief Option pour le composant Input.
165/// @ingroup component
166struct FTXUI_EXPORT(COMPONENT) InputOption {
167 // Un ensemble de styles prédéfinis:
168
169 /// @brief Crée le style d'entrée par défaut:
170 static InputOption Default();
171 /// @brief Un style blanc sur noir avec des marges élevées:
172 static InputOption Spacious();
173
174 /// Le contenu de l'entrée.
175 StringRef content = "";
176
177 /// Le contenu de l'entrée lorsqu'elle est vide.
178 StringRef placeholder = "";
179
180 // Style :
181 std::function<Element(InputState)> transform;
182 Ref<bool> password = false; ///< Obscurcit le contenu de l'entrée en utilisant '*'.
183 Ref<bool> multiline = true; ///< Indique si l'entrée peut être multiligne.
184 Ref<bool> insert = true; ///< Mode d'insertion ou de remplacement des caractères.
185
186 /// Appelé lorsque le contenu change.
187 std::function<void()> on_change = [] {};
188 /// Appelé lorsque l'utilisateur appuie sur Entrée.
189 std::function<void()> on_enter = [] {};
190
191 // La position du curseur:
192 Ref<int> cursor_position = 0;
193};
194
195/// @brief Option pour le composant Radiobox.
196/// @ingroup component
197struct FTXUI_EXPORT(COMPONENT) RadioboxOption {
198 // Constructeurs standards:
199 static RadioboxOption Simple();
200
201 // Contenu:
202 ConstStringListRef entries;
203 Ref<int> selected = 0;
204
205 // Style :
206 std::function<Element(const EntryState&)> transform;
207
208 // Observateurs:
209 /// Appelé lorsque l'entrée sélectionnée change.
210 std::function<void()> on_change = [] {};
211 Ref<int> focused_entry = 0;
212};
213
214struct FTXUI_EXPORT(COMPONENT) ResizableSplitOption {
216 Component back;
217 Ref<Direction> direction = Direction::Left;
218 Ref<int> main_size =
219 (direction() == Direction::Left || direction() == Direction::Right) ? 20
220 : 10;
221 std::function<Element()> separator_func = [] { return ::ftxui::separator(); };
222
223 // Contraintes sur main_size:
224 Ref<int> min = 0;
225 Ref<int> max = std::numeric_limits<int>::max();
226};
227
228// @brief Option pour le composant `Slider`.
229// @ingroup component
230template <typename T>
233 ConstRef<T> min = T(0);
234 ConstRef<T> max = T(100);
235 ConstRef<T> increment = (max() - min()) / 20;
236 Direction direction = Direction::Right;
237 Color color_active = Color::White;
238 Color color_inactive = Color::GrayDark;
239 std::function<void()> on_change; ///> Appelé lorsque `value` est mis à jour.
240};
241
242/// @brief État passé à la fonction de rendu du composant `Window`.
243/// @ingroup component
244struct FTXUI_EXPORT(COMPONENT) WindowRenderState {
245 Element inner; ///< L'élément enveloppé à l'intérieur de cette fenêtre.
246 const std::string& title; ///< Le titre de la fenêtre.
247 bool active = false; ///< Indique si la fenêtre est active.
248 bool drag = false; ///< Indique si la fenêtre est en cours de déplacement.
249 bool resize = false; ///< Indique si la fenêtre est en cours de redimensionnement.
250 bool hover_left = false; ///< Indique si le côté gauche redimensionnable est survolé.
251 bool hover_right = false; ///< Indique si le côté droit redimensionnable est survolé.
252 bool hover_top = false; ///< Indique si le côté supérieur redimensionnable est survolé.
253 bool hover_down = false; ///< Indique si le côté inférieur redimensionnable est survolé.
254};
255
256// @brief Option pour le composant `Window`.
257// @ingroup component
258struct FTXUI_EXPORT(COMPONENT) WindowOptions {
259 Component inner; ///< Le composant enveloppé par cette fenêtre.
260 ConstStringRef title = ""; ///< Le titre affiché par cette fenêtre.
261
262 Ref<int> left = 0; ///< La position du côté gauche de la fenêtre.
263 Ref<int> top = 0; ///< La position du côté supérieur de la fenêtre.
264 Ref<int> width = 20; ///< La largeur de la fenêtre.
265 Ref<int> height = 10; ///< La hauteur de la fenêtre.
266
267 Ref<bool> resize_left = true; ///< Le côté gauche peut-il être redimensionné ?
268 Ref<bool> resize_right = true; ///< Le côté droit peut-il être redimensionné ?
269 Ref<bool> resize_top = true; ///< Le côté supérieur peut-il être redimensionné ?
270 Ref<bool> resize_down = true; ///< Le côté inférieur peut-il être redimensionné ?
271
272 /// Une fonction optionnelle pour personnaliser l'apparence de la fenêtre :
273 std::function<Element(const WindowRenderState&)> render;
274};
275
276/// @brief Option pour le composant Dropdown.
277/// @ingroup component
278/// Un menu déroulant est une case à cocher qui ouvre/ferme une radiobox.
279struct FTXUI_EXPORT(COMPONENT) DropdownOption {
280 /// Indique si le menu déroulant est ouvert ou fermé :
281 Ref<bool> open = false;
282 // Les options pour la case à cocher :
283 CheckboxOption checkbox;
284 // Les options pour la radiobox :
285 RadioboxOption radiobox;
286 // La fonction de transformation :
287 std::function<Element(bool open, Element checkbox, Element radiobox)>
288 transform;
289};
290
291} // namespace ftxui
292
293#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
Un adaptateur. Possède ou référence un objet immuable.
Definition ref.hpp:20
Un adaptateur. Possède ou référence un objet mutable.
Definition ref.hpp:56
Direction
Direction est une énumération qui représente les quatre directions cardinales.
Definition direction.hpp:15
L'espace de noms FTXUI ftxui::
Definition animation.hpp:11
std::shared_ptr< Node > Element
Definition elements.hpp:25
Direction direction
Definition elements.hpp:83
const Element & element
Definition node.hpp:94
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23
std::uint8_t top
Definition screen.cpp:148
std::uint8_t left
Definition screen.cpp:147
std::function< void()> on_change