From 359100ca7361307610a9b9938eedf647777dd388 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Wed, 7 Jul 2021 22:23:07 +0200 Subject: [PATCH] Add option for Button. --- examples/component/button.cpp | 6 ++++-- include/ftxui/component/button.hpp | 6 ++++-- include/ftxui/component/component.hpp | 2 +- include/ftxui/component/component_options.hpp | 6 +++++- src/ftxui/component/button.cpp | 10 +++++----- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/examples/component/button.cpp b/examples/component/button.cpp index c632caa2..675c9126 100644 --- a/examples/component/button.cpp +++ b/examples/component/button.cpp @@ -13,11 +13,13 @@ int main(int argc, const char* argv[]) { int value = 50; // The tree of components. This defines how to navigate using the keyboard. + auto button_option = ButtonOption(); + button_option.border = false; auto buttons = Container::Horizontal({ Button( - "[Decrease]", [&] { value--; }, false), + "[Decrease]", [&] { value--; }, &button_option), Button( - "[Increase]", [&] { value++; }, false), + "[Increase]", [&] { value++; }, &button_option), }); // Modify the way to render them on screen: diff --git a/include/ftxui/component/button.hpp b/include/ftxui/component/button.hpp index 2257fba5..5623c66b 100644 --- a/include/ftxui/component/button.hpp +++ b/include/ftxui/component/button.hpp @@ -21,7 +21,9 @@ class ButtonBase : public ComponentBase { static ButtonBase* From(Component); // Constructor. - ButtonBase(ConstStringRef label, std::function on_click, bool border); + ButtonBase(ConstStringRef label, + std::function on_click, + ConstRef option); ~ButtonBase() override = default; // Component implementation. @@ -31,8 +33,8 @@ class ButtonBase : public ComponentBase { private: ConstStringRef label_; std::function on_click_; - bool border_; Box box_; + ConstRef option_; }; } // namespace ftxui diff --git a/include/ftxui/component/component.hpp b/include/ftxui/component/component.hpp index 573b2adc..03f9985f 100644 --- a/include/ftxui/component/component.hpp +++ b/include/ftxui/component/component.hpp @@ -26,7 +26,7 @@ std::shared_ptr Make(Args&&... args) { Component Button(ConstStringRef label, std::function on_click, - bool border = true); + ConstRef = {}); Component Checkbox(ConstStringRef label, bool* checked); Component Input(StringRef content, ConstStringRef placeholder); Component Menu(const std::vector* entries, diff --git a/include/ftxui/component/component_options.hpp b/include/ftxui/component/component_options.hpp index 9bdb329f..85792ad6 100644 --- a/include/ftxui/component/component_options.hpp +++ b/include/ftxui/component/component_options.hpp @@ -14,6 +14,10 @@ struct MenuOption { std::function on_enter = []() {}; }; -}; // namespace ftxui +struct ButtonOption { + bool border = true; +}; + +}; // namespace ftxui #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */ diff --git a/src/ftxui/component/button.cpp b/src/ftxui/component/button.cpp index 71cf8d94..7f445175 100644 --- a/src/ftxui/component/button.cpp +++ b/src/ftxui/component/button.cpp @@ -33,8 +33,8 @@ namespace ftxui { /// ``` Component Button(ConstStringRef label, std::function on_click, - bool border) { - return Make(label, on_click, border); + ConstRef option) { + return Make(label, std::move(on_click), std::move(option)); } // static @@ -44,12 +44,12 @@ ButtonBase* ButtonBase::From(Component component) { ButtonBase::ButtonBase(ConstStringRef label, std::function on_click, - bool border) - : label_(label), on_click_(on_click), border_(border) {} + ConstRef option) + : label_(label), on_click_(on_click), option_(std::move(option)) {} Element ButtonBase::Render() { auto style = Focused() ? inverted : nothing; - auto my_border = border_ ? border : nothing; + auto my_border = option_->border ? border : nothing; return text(*label_) | my_border | style | reflect(box_); }