From 7b88656e2567493417189c4b688b23acb22160c9 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Tue, 18 May 2021 17:49:53 +0200 Subject: [PATCH] Add option to have button without border. (#101) --- examples/component/button.cpp | 4 ++-- include/ftxui/component/button.hpp | 3 ++- include/ftxui/component/component.hpp | 4 +++- src/ftxui/component/button.cpp | 15 ++++++++++----- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/examples/component/button.cpp b/examples/component/button.cpp index 5bc78521..5d4f11e5 100644 --- a/examples/component/button.cpp +++ b/examples/component/button.cpp @@ -14,8 +14,8 @@ int main(int argc, const char* argv[]) { // The tree of components. This defines how to navigate using the keyboard. auto buttons = Container::Horizontal({ - Button("Decrease", [&] { value--; }), - Button("Increase", [&] { value++; }), + Button("[Decrease]", [&] { value--; }, false), + Button("[Increase]", [&] { value++; }, false), }); // Modify the way to render them on screen: diff --git a/include/ftxui/component/button.hpp b/include/ftxui/component/button.hpp index 86388236..2257fba5 100644 --- a/include/ftxui/component/button.hpp +++ b/include/ftxui/component/button.hpp @@ -21,7 +21,7 @@ class ButtonBase : public ComponentBase { static ButtonBase* From(Component); // Constructor. - ButtonBase(ConstStringRef label, std::function on_click); + ButtonBase(ConstStringRef label, std::function on_click, bool border); ~ButtonBase() override = default; // Component implementation. @@ -31,6 +31,7 @@ class ButtonBase : public ComponentBase { private: ConstStringRef label_; std::function on_click_; + bool border_; Box box_; }; diff --git a/include/ftxui/component/component.hpp b/include/ftxui/component/component.hpp index eaaaf117..e77aff26 100644 --- a/include/ftxui/component/component.hpp +++ b/include/ftxui/component/component.hpp @@ -22,7 +22,9 @@ std::shared_ptr Make(Args&&... args) { return std::make_shared(args...); } -Component Button(ConstStringRef label, std::function on_click); +Component Button(ConstStringRef label, + std::function on_click, + bool border = true); Component Checkbox(ConstStringRef label, bool* checked); Component Input(StringRef content, ConstStringRef placeholder); Component Menu(const std::vector* entries, int* selected_); diff --git a/src/ftxui/component/button.cpp b/src/ftxui/component/button.cpp index 5ed0ce09..71cf8d94 100644 --- a/src/ftxui/component/button.cpp +++ b/src/ftxui/component/button.cpp @@ -31,8 +31,10 @@ namespace ftxui { /// │Click to quit│ /// └─────────────┘ /// ``` -Component Button(ConstStringRef label, std::function on_click) { - return Make(label, on_click); +Component Button(ConstStringRef label, + std::function on_click, + bool border) { + return Make(label, on_click, border); } // static @@ -40,12 +42,15 @@ ButtonBase* ButtonBase::From(Component component) { return static_cast(component.get()); } -ButtonBase::ButtonBase(ConstStringRef label, std::function on_click) - : label_(label), on_click_(on_click) {} +ButtonBase::ButtonBase(ConstStringRef label, + std::function on_click, + bool border) + : label_(label), on_click_(on_click), border_(border) {} Element ButtonBase::Render() { auto style = Focused() ? inverted : nothing; - return text(*label_) | border | style | reflect(box_); + auto my_border = border_ ? border : nothing; + return text(*label_) | my_border | style | reflect(box_); } bool ButtonBase::OnEvent(Event event) {