FTXUI 6.1.9
C++ functional terminal UI.
Chargement...
Recherche...
Aucune correspondance
src/ftxui/component/button.cpp
Aller à la documentation de ce fichier.
1// Copyright 2020 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 dans
3// le fichier LICENSE.
4
5#include <functional> // for function
6#include <utility> // for move
7
8#include "ftxui/component/animation.hpp" // for Animator, Params (ptr only)
9#include "ftxui/component/component.hpp" // for Make, Button
10#include "ftxui/component/component_base.hpp" // for ComponentBase
11#include "ftxui/component/component_options.hpp" // for ButtonOption, AnimatedColorOption, AnimatedColorsOption, EntryState
12#include "ftxui/component/event.hpp" // for Event, Event::Return
13#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
14#include "ftxui/component/screen_interactive.hpp" // for Component
15#include "ftxui/dom/elements.hpp" // for operator|, Decorator, Element, operator|=, bgcolor, color, reflect, text, bold, border, inverted, nothing
16#include "ftxui/screen/box.hpp" // for Box
17#include "ftxui/screen/color.hpp" // for Color
18#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
19
20namespace ftxui {
21
22namespace {
23
24Element DefaultTransform(EntryState params) { // NOLINT
25 auto element = text(params.label) | border;
26 if (params.active) {
27 element |= bold;
28 }
29 if (params.focused) {
30 element |= inverted;
31 }
32 return element;
33}
34
35class ButtonBase : public ComponentBase, public ButtonOption {
36 public:
37 explicit ButtonBase(ButtonOption option) : ButtonOption(std::move(option)) {}
38
39 // Component implementation:
40 Element OnRender() override {
41 const bool active = Active();
42 const bool focused = Focused();
43 const bool focused_or_hover = focused || mouse_hover_;
44
45 float target = focused_or_hover ? 1.f : 0.f; // NOLINT
46 if (target != animator_background_.to()) {
47 SetAnimationTarget(target);
48 }
49
50 const EntryState state{
51 *label, false, active, focused_or_hover, Index(),
52 };
53
54 auto element = (transform ? transform : DefaultTransform) //
55 (state);
56 element |= AnimatedColorStyle();
57 element |= focus;
58 element |= reflect(box_);
59 return element;
60 }
61
62 Decorator AnimatedColorStyle() {
63 Decorator style = nothing;
64 if (animated_colors.background.enabled) {
65 style = style |
66 bgcolor(Color::Interpolate(animation_foreground_, //
67 animated_colors.background.inactive,
68 animated_colors.background.active));
69 }
70 if (animated_colors.foreground.enabled) {
71 style =
72 style | color(Color::Interpolate(animation_foreground_, //
73 animated_colors.foreground.inactive,
74 animated_colors.foreground.active));
75 }
76 return style;
77 }
78
79 void SetAnimationTarget(float target) {
80 if (animated_colors.foreground.enabled) {
81 animator_foreground_ = animation::Animator(
82 &animation_foreground_, target, animated_colors.foreground.duration,
83 animated_colors.foreground.function);
84 }
85 if (animated_colors.background.enabled) {
86 animator_background_ = animation::Animator(
87 &animation_background_, target, animated_colors.background.duration,
88 animated_colors.background.function);
89 }
90 }
91
92 void OnAnimation(animation::Params& p) override {
93 animator_background_.OnAnimation(p);
94 animator_foreground_.OnAnimation(p);
95 }
96
97 void OnClick() {
98 animation_background_ = 0.5F; // NOLINT
99 animation_foreground_ = 0.5F; // NOLINT
100 SetAnimationTarget(1.F); // NOLINT
101
102 // TODO(arthursonzogni): Envisager de poster la tâche à la boucle principale,
103 // au lieu de l'invoquer immédiatement.
104 on_click(); // Peut être supprimé.
105 }
106
107 bool OnEvent(Event event) override {
108 if (event.is_mouse()) {
109 return OnMouseEvent(event);
110 }
111
112 if (event == Event::Return) {
113 OnClick(); // Peut être supprimé.
114 return true;
115 }
116 return false;
117 }
118
119 bool OnMouseEvent(Event event) {
120 mouse_hover_ =
121 box_.Contain(event.mouse().x, event.mouse().y) && CaptureMouse(event);
122
123 if (!mouse_hover_) {
124 return false;
125 }
126
127 if (event.mouse().button == Mouse::Left &&
128 event.mouse().motion == Mouse::Pressed) {
129 TakeFocus();
130 OnClick(); // Peut être supprimé.
131 return true;
132 }
133
134 return false;
135 }
136
137 bool Focusable() const final { return true; }
138
139 private:
140 bool mouse_hover_ = false;
141 Box box_;
142 float animation_background_ = 0;
143 float animation_foreground_ = 0;
144 animation::Animator animator_background_ =
145 animation::Animator(&animation_background_);
146 animation::Animator animator_foreground_ =
147 animation::Animator(&animation_foreground_);
148};
149
150} // namespace
151
152/// @brief Dessine un bouton. Exécute une fonction lors d'un clic.
153/// @param option Paramètres optionnels supplémentaires.
154/// @ingroup component
155/// @see ButtonBase
156///
157/// ### Exemple
158///
159/// ```cpp
160/// auto screen = ScreenInteractive::FitComponent();
161/// Component button = Button({
162/// .label = "Cliquer pour quitter",
163/// .on_click = screen.ExitLoopClosure(),
164/// });
165/// screen.Loop(button)
166/// ```
167///
168/// ### Sortie
169///
170/// ```bash
171/// ┌─────────────┐
172/// │Cliquer pour quitter│
173/// └─────────────┘
174/// ```
176 return Make<ButtonBase>(std::move(option));
177}
178
179/// @brief Dessine un bouton. Exécute une fonction lors d'un clic.
180/// @param label L'étiquette du bouton.
181/// @param on_click L'action à exécuter lors d'un clic.
182/// @param option Paramètres optionnels supplémentaires.
183/// @ingroup component
184/// @see ButtonBase
185///
186/// ### Exemple
187///
188/// ```cpp
189/// auto screen = ScreenInteractive::FitComponent();
190/// std::string label = "Cliquer pour quitter";
191/// Component button = Button(&label, screen.ExitLoopClosure());
192/// screen.Loop(button)
193/// ```
194///
195/// ### Sortie
196///
197/// ```bash
198/// ┌─────────────┐
199/// │Cliquer pour quitter│
200/// └─────────────┘
201/// ```
202// NOLINTNEXTLINE
204 std::function<void()> on_click,
205 ButtonOption option) {
206 option.label = std::move(label);
207 option.on_click = std::move(on_click);
208 return Make<ButtonBase>(std::move(option));
209}
210
211} // namespace ftxui
Un adaptateur. Possède ou référence une chaîne constante. Par commodité, cette classe convertit plusi...
Definition ref.hpp:94
std::function< void()> on_click
static const Event Return
Definition event.hpp:52
Component Button(ButtonOption options)
Dessine un bouton. Exécute une fonction lors d'un clic.
Option pour le composant AnimatedButton.
Decorator bgcolor(Color)
Décore en utilisant une couleur d'arrière-plan.
Element nothing(Element element)
Une décoration qui ne fait absolument rien.
Definition dom/util.cpp:28
Element bold(Element)
Utilise une police en gras, pour les éléments avec plus d'emphase.
Definition bold.cpp:33
Element inverted(Element)
Ajoute un filtre qui inversera les couleurs de premier plan et d'arrière-plan.
Definition inverted.cpp:33
Element text(std::wstring text)
Affiche un morceau de texte unicode.
Definition text.cpp:160
Element focus(Element)
Définit l'élément child comme étant celui qui est focalisé parmi ses frères.
Definition frame.cpp:101
Element border(Element)
Dessine une bordure autour de l'élément.
Decorator color(Color)
Décore en utilisant une couleur de premier plan.
static Color Interpolate(float t, const Color &a, const Color &b)
L'espace de noms FTXUI ftxui::
Definition animation.hpp:10
std::function< Element(Element)> Decorator
Definition elements.hpp:24
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:27
std::shared_ptr< Node > Element
Definition elements.hpp:22
Decorator reflect(Box &box)
Definition reflect.cpp:43
std::shared_ptr< ComponentBase > Component