FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
src/ftxui/component/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
20 namespace ftxui {
21
22 namespace {
23
24 Element 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
35 class 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): 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 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 Draw a button. Execute a function when clicked.
153/// @param option Additional optional parameters.
154/// @ingroup component
155/// @see ButtonBase
156///
157/// ### Example
158///
159/// ```cpp
160/// auto screen = ScreenInteractive::FitComponent();
161/// Component button = Button({
162/// .label = "Click to quit",
163/// .on_click = screen.ExitLoopClosure(),
164/// });
165/// screen.Loop(button)
166/// ```
167///
168/// ### Output
169///
170/// ```bash
171/// ┌─────────────┐
172/// │Click to quit│
173/// └─────────────┘
174/// ```
175 Component Button(ButtonOption option) {
176 return Make<ButtonBase>(std::move(option));
177}
178
179/// @brief Draw a button. Execute a function when clicked.
180/// @param label The label of the button.
181/// @param on_click The action to execute when clicked.
182/// @param option Additional optional parameters.
183/// @ingroup component
184/// @see ButtonBase
185///
186/// ### Example
187///
188/// ```cpp
189/// auto screen = ScreenInteractive::FitComponent();
190/// std::string label = "Click to quit";
191/// Component button = Button(&label, screen.ExitLoopClosure());
192/// screen.Loop(button)
193/// ```
194///
195/// ### Output
196///
197/// ```bash
198/// ┌─────────────┐
199/// │Click to quit│
200/// └─────────────┘
201/// ```
202// NOLINTNEXTLINE
203 Component Button(ConstStringRef label,
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
animation.hpp
box.hpp
ftxui::ConstStringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:94
color.hpp
component.hpp
component_base.hpp
component_options.hpp
elements.hpp
event.hpp
ftxui::Mouse::Left
@ Left
Definition mouse.hpp:13
ftxui::Mouse::Pressed
@ Pressed
Definition mouse.hpp:25
ftxui::ButtonOption::on_click
std::function< void()> on_click
Definition component_options.hpp:127
ftxui::ButtonOption::label
ConstStringRef label
Definition component_options.hpp:126
ftxui::Event::Return
static const Event Return
Definition event.hpp:53
ftxui::Button
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definition src/ftxui/component/button.cpp:175
ftxui::ButtonOption
Option for the AnimatedButton component.
Definition component_options.hpp:113
ftxui::bgcolor
Decorator bgcolor(Color)
Decorate using a background color.
Definition dom/color.cpp:124
ftxui::nothing
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition dom/util.cpp:28
ftxui::bold
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
ftxui::inverted
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::focus
Element focus(Element)
Set the child to be the one focused among its siblings.
Definition frame.cpp:101
ftxui::border
Element border(Element)
Draw a border around the element.
Definition src/ftxui/dom/border.cpp:227
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition dom/color.cpp:110
ftxui::Color::Interpolate
static Color Interpolate(float t, const Color &a, const Color &b)
Definition screen/color.cpp:203
mouse.hpp
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Decorator
std::function< Element(Element)> Decorator
Definition elements.hpp:24
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::reflect
Decorator reflect(Box &box)
Definition reflect.cpp:43
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ref.hpp
screen_interactive.hpp