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 <string> // for string
7#include <utility> // for move
8
9#include "ftxui/component/animation.hpp" // for Animator, Params (ptr only)
10#include "ftxui/component/app.hpp" // for Component
11#include "ftxui/component/component.hpp" // for Make, Button
12#include "ftxui/component/component_base.hpp" // for ComponentBase
13#include "ftxui/component/component_options.hpp" // for ButtonOption, AnimatedColorOption, AnimatedColorsOption, EntryState
14#include "ftxui/component/event.hpp" // for Event, Event::Return
15#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
16#include "ftxui/dom/elements.hpp" // for operator|, Decorator, Element, operator|=, bgcolor, color, reflect, text, bold, border, inverted, nothing
17#include "ftxui/screen/box.hpp" // for Box
18#include "ftxui/screen/color.hpp" // for Color
19#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
20
21namespace ftxui {
22
23namespace {
24
25Element DefaultTransform(EntryState params) { // NOLINT
26 auto element = text(params.label) | border;
27 if (params.active) {
28 element |= bold;
29 }
30 if (params.focused) {
32 }
33 return element;
34}
35
36class ButtonBase : public ComponentBase, public ButtonOption {
37 public:
38 explicit ButtonBase(ButtonOption option) : ButtonOption(std::move(option)) {}
39
40 // Component implementation:
41 Element OnRender() override {
42 const bool active = Active();
43 const bool focused = Focused();
44 const bool focused_or_hover = focused || mouse_hover_;
45
46 float target = focused_or_hover ? 1.f : 0.f; // NOLINT
47 if (target != animator_background_.to()) {
48 SetAnimationTarget(target);
49 }
50
51 const EntryState state{
52 std::string(*label), false, active, focused_or_hover, Index(),
53 };
54
55 auto element = (transform ? transform : DefaultTransform) //
56 (state);
57 element |= AnimatedColorStyle();
58 element |= focus;
59 element |= reflect(box_);
60 return element;
61 }
62
63 Decorator AnimatedColorStyle() {
64 Decorator style = nothing;
65 if (animated_colors.background.enabled) {
66 style = style |
67 bgcolor(Color::Interpolate(animation_foreground_, //
68 animated_colors.background.inactive,
69 animated_colors.background.active));
70 }
71 if (animated_colors.foreground.enabled) {
72 style =
73 style | color(Color::Interpolate(animation_foreground_, //
74 animated_colors.foreground.inactive,
75 animated_colors.foreground.active));
76 }
77 return style;
78 }
79
80 void SetAnimationTarget(float target) {
81 if (animated_colors.foreground.enabled) {
82 animator_foreground_ = animation::Animator(
83 &animation_foreground_, target, animated_colors.foreground.duration,
84 animated_colors.foreground.function);
85 }
86 if (animated_colors.background.enabled) {
87 animator_background_ = animation::Animator(
88 &animation_background_, target, animated_colors.background.duration,
89 animated_colors.background.function);
90 }
91 }
92
93 void OnAnimation(animation::Params& p) override {
94 animator_background_.OnAnimation(p);
95 animator_foreground_.OnAnimation(p);
96 }
97
98 void OnClick() {
99 animation_background_ = 0.5F; // NOLINT
100 animation_foreground_ = 0.5F; // NOLINT
101 SetAnimationTarget(1.F); // NOLINT
102
103 App::PostEventOrExecute(on_click);
104 }
105
106 bool OnEvent(Event event) override {
107 if (event.is_mouse()) {
108 return OnMouseEvent(event);
109 }
110
111 if (event == Event::Return) {
112 OnClick(); // May delete this.
113 return true;
114 }
115 return false;
116 }
117
118 bool OnMouseEvent(Event event) {
119 mouse_hover_ =
120 box_.Contain(event.mouse().x, event.mouse().y) && CaptureMouse(event);
121
122 if (!mouse_hover_) {
123 return false;
124 }
125
126 if (event.mouse().button == Mouse::Left &&
127 event.mouse().motion == Mouse::Pressed) {
128 TakeFocus();
129 OnClick(); // May delete this.
130 return true;
131 }
132
133 return false;
134 }
135
136 bool Focusable() const final { return true; }
137
138 private:
139 bool mouse_hover_ = false;
140 Box box_;
141 float animation_background_ = 0;
142 float animation_foreground_ = 0;
143 animation::Animator animator_background_ =
144 animation::Animator(&animation_background_);
145 animation::Animator animator_foreground_ =
146 animation::Animator(&animation_foreground_);
147};
148
149} // namespace
150
151/// @brief Draw a button. Execute a function when clicked.
152/// @param option Additional optional parameters.
153/// @ingroup component
154/// @see ButtonBase
155///
156/// ### Example
157///
158/// ```cpp
159/// auto screen = App::FitComponent();
160/// Component button = Button({
161/// .label = "Click to quit",
162/// .on_click = screen.ExitLoopClosure(),
163/// });
164/// screen.Loop(button)
165/// ```
166///
167/// ### Output
168///
169/// ```bash
170/// ┌─────────────┐
171/// │Click to quit│
172/// └─────────────┘
173/// ```
174Component Button(ButtonOption option) {
175 return Make<ButtonBase>(std::move(option));
176}
177
178/// @brief Draw a button. Execute a function when clicked.
179/// @param label The label of the button.
180/// @param on_click The action to execute when clicked.
181/// @param option Additional optional parameters.
182/// @ingroup component
183/// @see ButtonBase
184///
185/// ### Example
186///
187/// ```cpp
188/// auto screen = App::FitComponent();
189/// std::string label = "Click to quit";
190/// Component button = Button(&label, screen.ExitLoopClosure());
191/// screen.Loop(button)
192/// ```
193///
194/// ### Output
195///
196/// ```bash
197/// ┌─────────────┐
198/// │Click to quit│
199/// └─────────────┘
200/// ```
201// NOLINTNEXTLINE
202Component Button(ConstStringRef label,
203 std::function<void()> on_click,
204 ButtonOption option) {
205 option.label = std::move(label);
206 option.on_click = std::move(on_click);
207 return Make<ButtonBase>(std::move(option));
208}
209
210} // namespace ftxui
Component Button(ConstStringRef label, std::function< void()> on_click, ButtonOption options=ButtonOption::Simple())
Draw a button. Execute a function when clicked.
Element text(std::string_view text)
Display a piece of UTF8 encoded unicode text.
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition dom/util.cpp:28
Element bold(Element child)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
Element inverted(Element child)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
Element focus(Element child)
Set the child to be the one focused among its siblings.
Definition frame.cpp:101
Element bgcolor(Color color, Element child)
Set the background color of an element.
Definition dom/color.cpp:96
Element color(Color color, Element child)
Set the foreground color of an element.
Definition dom/color.cpp:81
Element border(Element child)
Draw a border around the element.
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:28
std::shared_ptr< Node > Element
Definition elements.hpp:24
std::function< Element(Element)> Decorator
Definition elements.hpp:26
const Element & element
Definition node.hpp:95
Decorator reflect(Box &box)
Definition reflect.cpp:43
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23