26Element DefaultTransform(EntryState params) {
37class ButtonBase :
public ComponentBase,
public ButtonOption {
39 explicit ButtonBase(ButtonOption option) : ButtonOption(std::move(option)) {}
43 const bool active = Active();
44 const bool focused = Focused();
45 const bool focused_or_hover = focused || mouse_hover_;
47 float target = focused_or_hover ? 1.f : 0.f;
48 if (target != animator_background_.to()) {
49 SetAnimationTarget(target);
53 const EntryState state = {
60 auto element = (transform ? transform : DefaultTransform)
62 return element | AnimatedColorStyle() | focus_management |
reflect(box_);
67 if (animated_colors.background.enabled) {
70 animated_colors.background.inactive,
71 animated_colors.background.active));
73 if (animated_colors.foreground.enabled) {
76 animated_colors.foreground.inactive,
77 animated_colors.foreground.active));
82 void SetAnimationTarget(
float target) {
83 if (animated_colors.foreground.enabled) {
84 animator_foreground_ = animation::Animator(
85 &animation_foreground_, target, animated_colors.foreground.duration,
86 animated_colors.foreground.function);
88 if (animated_colors.background.enabled) {
89 animator_background_ = animation::Animator(
90 &animation_background_, target, animated_colors.background.duration,
91 animated_colors.background.function);
95 void OnAnimation(animation::Params& p)
override {
96 animator_background_.OnAnimation(p);
97 animator_foreground_.OnAnimation(p);
102 animation_background_ = 0.5F;
103 animation_foreground_ = 0.5F;
104 SetAnimationTarget(1.F);
107 bool OnEvent(Event event)
override {
108 if (event.is_mouse()) {
109 return OnMouseEvent(event);
119 bool OnMouseEvent(Event event) {
121 box_.Contain(event.mouse().x, event.mouse().y) && CaptureMouse(event);
137 bool Focusable() const final {
return true; }
140 bool mouse_hover_ =
false;
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_);
205 std::function<
void()> on_click,
207 option.
label = label;
208 option.
on_click = std::move(on_click);
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< T > Make(Args &&... args)
std::shared_ptr< Node > Element
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.
std::shared_ptr< ComponentBase > Component
Decorator color(Color)
Decorate using a foreground color.
static const Event Return