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
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): 考慮將任務發佈到主循環,而不是立即調用它。
103 on_click(); // May delete this.
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 繪製一個按鈕。點擊時執行一個函數。
152/// @param option 額外的可選參數。
153/// @ingroup component
154/// @see ButtonBase
155///
156/// ### 範例
157///
158/// ```cpp
159/// auto screen = ScreenInteractive::FitComponent();
160/// Component button = Button({
161/// .label = "Click to quit",
162/// .on_click = screen.ExitLoopClosure(),
163/// });
164/// screen.Loop(button)
165/// ```
166///
167/// ### 輸出
168///
169/// ```bash
170/// ┌─────────────┐
171/// │Click to quit│
172/// └─────────────┘
173/// ```
175 return Make<ButtonBase>(std::move(option));
176}
177
178/// @brief 繪製一個按鈕。點擊時執行一個函數。
179/// @param label 按鈕的標籤。
180/// @param on_click 點擊時要執行的動作。
181/// @param option 額外的可選參數。
182/// @ingroup component
183/// @see ButtonBase
184///
185/// ### 範例
186///
187/// ```cpp
188/// auto screen = ScreenInteractive::FitComponent();
189/// std::string label = "Click to quit";
190/// Component button = Button(&label, screen.ExitLoopClosure());
191/// screen.Loop(button)
192/// ```
193///
194/// ### 輸出
195///
196/// ```bash
197/// ┌─────────────┐
198/// │Click to quit│
199/// └─────────────┘
200/// ```
201// NOLINTNEXTLINE
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
一個適配器。擁有或引用一個常數字串。為方便起見,此類別將多個不可變字串轉換為共享表示。
Definition ref.hpp:92
std::function< void()> on_click
static const Event Return
Definition event.hpp:51
Component Button(ButtonOption options)
繪製一個按鈕。點擊時執行一個函數。
AnimatedButton 元件的選項。
Decorator bgcolor(Color)
使用背景顏色進行裝飾。
Element nothing(Element element)
一個什麼都不做的裝飾器。
Definition dom/util.cpp:28
Element bold(Element)
使用粗體字型,用於需要更多強調的元素。
Definition bold.cpp:33
Element inverted(Element)
添加一個濾鏡,它將反轉前景和背景 顏色。
Definition inverted.cpp:34
Element text(std::wstring text)
顯示一段 Unicode 文字。
Definition text.cpp:160
Element focus(Element)
將 child 設置為其同級元素中被聚焦的元素。
Definition frame.cpp:101
Element border(Element)
在元素周圍繪製邊框。
Decorator color(Color)
使用前景顏色進行裝飾。
static Color Interpolate(float t, const Color &a, const Color &b)
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:26
std::shared_ptr< Node > Element
Definition elements.hpp:22
Decorator reflect(Box &box)
Definition reflect.cpp:42
std::shared_ptr< ComponentBase > Component