FTXUI  6.0.2
C++ functional terminal UI.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
button.cpp
Go to the documentation of this file.
1// Copyright 2020 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
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()) {
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() {
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): Consider posting the task to the main loop, instead
103 // of invoking it immediately.
104 on_click(); // May delete this.
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(); // May delete this.
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(); // May delete this.
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 ButtonOption option_;
143 float animation_background_ = 0;
144 float animation_foreground_ = 0;
145 animation::Animator animator_background_ =
146 animation::Animator(&animation_background_);
147 animation::Animator animator_foreground_ =
148 animation::Animator(&animation_foreground_);
149};
150
151} // namespace
152
153/// @brief Draw a button. Execute a function when clicked.
154/// @param option Additional optional parameters.
155/// @ingroup component
156/// @see ButtonBase
157///
158/// ### Example
159///
160/// ```cpp
161/// auto screen = ScreenInteractive::FitComponent();
162/// Component button = Button({
163/// .label = "Click to quit",
164/// .on_click = screen.ExitLoopClosure(),
165/// });
166/// screen.Loop(button)
167/// ```
168///
169/// ### Output
170///
171/// ```bash
172/// ┌─────────────┐
173/// │Click to quit│
174/// └─────────────┘
175/// ```
179
180/// @brief Draw a button. Execute a function when clicked.
181/// @param label The label of the button.
182/// @param on_click The action to execute when clicked.
183/// @param option Additional optional parameters.
184/// @ingroup component
185/// @see ButtonBase
186///
187/// ### Example
188///
189/// ```cpp
190/// auto screen = ScreenInteractive::FitComponent();
191/// std::string label = "Click to quit";
192/// Component button = Button(&label, screen.ExitLoopClosure());
193/// screen.Loop(button)
194/// ```
195///
196/// ### Output
197///
198/// ```bash
199/// ┌─────────────┐
200/// │Click to quit│
201/// └─────────────┘
202/// ```
203// NOLINTNEXTLINE
205 std::function<void()> on_click,
207 option.label = std::move(label);
208 option.on_click = std::move(on_click);
209 return Make<ButtonBase>(std::move(option));
210}
211
212} // namespace ftxui
static Color Interpolate(float t, const Color &a, const Color &b)
Definition color.cpp:212
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:94
Decorator bgcolor(Color)
Decorate using a background color.
Definition color.cpp:124
std::function< Element(Element)> Decorator
Definition elements.hpp:24
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition util.cpp:28
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< ComponentBase > Component
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definition button.cpp:176
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
Element focus(Element)
Set the child to be the one focused among its siblings.
Definition frame.cpp:101
Decorator reflect(Box &box)
Definition reflect.cpp:43
Element border(Element)
Draw a border around the element.
Definition border.cpp:227
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:110
Option for the AnimatedButton component.
static const Event Return
Definition event.hpp:51