24 Element DefaultTransform(EntryState params) {
35 class ButtonBase :
public ComponentBase,
public ButtonOption {
37 explicit ButtonBase(ButtonOption option) : ButtonOption(std::move(option)) {}
41 const bool active = Active();
42 const bool focused = Focused();
43 const bool focused_or_hover = focused || mouse_hover_;
45 float target = focused_or_hover ? 1.f : 0.f;
46 if (target != animator_background_.to()) {
47 SetAnimationTarget(target);
51 const EntryState state{
52 *label,
false, active, focused_or_hover, Index(),
55 auto element = (transform ? transform : DefaultTransform)
57 return element | AnimatedColorStyle() | focus_management |
reflect(box_);
62 if (animated_colors.background.enabled) {
65 animated_colors.background.inactive,
66 animated_colors.background.active));
68 if (animated_colors.foreground.enabled) {
71 animated_colors.foreground.inactive,
72 animated_colors.foreground.active));
77 void SetAnimationTarget(
float target) {
78 if (animated_colors.foreground.enabled) {
79 animator_foreground_ = animation::Animator(
80 &animation_foreground_, target, animated_colors.foreground.duration,
81 animated_colors.foreground.function);
83 if (animated_colors.background.enabled) {
84 animator_background_ = animation::Animator(
85 &animation_background_, target, animated_colors.background.duration,
86 animated_colors.background.function);
90 void OnAnimation(animation::Params& p)
override {
91 animator_background_.OnAnimation(p);
92 animator_foreground_.OnAnimation(p);
96 animation_background_ = 0.5F;
97 animation_foreground_ = 0.5F;
98 SetAnimationTarget(1.F);
105 bool OnEvent(Event event)
override {
106 if (event.is_mouse()) {
107 return OnMouseEvent(event);
117 bool OnMouseEvent(Event event) {
119 box_.Contain(event.mouse().x, event.mouse().y) && CaptureMouse(event);
135 bool Focusable() const final {
return true; }
138 bool mouse_hover_ =
false;
140 ButtonOption option_;
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_);
175 return Make<ButtonBase>(std::move(option));
203 std::function<
void()> on_click,
205 option.
label = std::move(label);
206 option.
on_click = std::move(on_click);
207 return Make<ButtonBase>(std::move(option));
static Color Interpolate(float t, const Color &a, const Color &b)
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Decorator bgcolor(Color)
Decorate using a background color.
std::function< Element(Element)> Decorator
Element nothing(Element element)
A decoration doing absolutely nothing.
std::shared_ptr< Node > Element
std::shared_ptr< ComponentBase > Component
Element bold(Element)
Use a bold font, for elements with more emphasis.
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Element text(std::wstring text)
Display a piece of unicode text.
Element select(Element)
Set the child to be the one selected among its siblings.
Element focus(Element)
Set the child to be the one in focus globally.
Decorator reflect(Box &box)
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Element border(Element)
Draw a border around the element.
Decorator color(Color)
Decorate using a foreground color.
static const Event Return