From 2b7daf061f1456ff8638e353a9f40cbf425c51c3 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Wed, 7 Jul 2021 22:37:50 +0200 Subject: [PATCH] Add options for checkbox. --- include/ftxui/component/checkbox.hpp | 15 +++------- include/ftxui/component/component.hpp | 4 ++- include/ftxui/component/component_options.hpp | 13 +++++++++ src/ftxui/component/checkbox.cpp | 28 +++++++++++-------- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/include/ftxui/component/checkbox.hpp b/include/ftxui/component/checkbox.hpp index d16cef33..a43114ca 100644 --- a/include/ftxui/component/checkbox.hpp +++ b/include/ftxui/component/checkbox.hpp @@ -22,18 +22,11 @@ class CheckboxBase : public ComponentBase { static CheckboxBase* From(Component component); // Constructor. - CheckboxBase(ConstStringRef label, bool* state); + CheckboxBase(ConstStringRef label, + bool* state, + ConstRef option = {}); ~CheckboxBase() override = default; - std::wstring checked = L"▣ "; /// Prefix for a "checked" state. - std::wstring unchecked = L"☐ "; /// Prefix for a "unchecked" state. - - Decorator focused_style = inverted; /// Decorator used when focused. - Decorator unfocused_style = nothing; /// Decorator used when unfocused. - - /// Called when the user change the state of the CheckboxBase. - std::function on_change = []() {}; - // Component implementation. Element Render() override; bool OnEvent(Event) override; @@ -43,8 +36,8 @@ class CheckboxBase : public ComponentBase { ConstStringRef label_; bool* const state_; - int cursor_position = 0; Box box_; + ConstRef option_; }; } // namespace ftxui diff --git a/include/ftxui/component/component.hpp b/include/ftxui/component/component.hpp index 03f9985f..c80e6708 100644 --- a/include/ftxui/component/component.hpp +++ b/include/ftxui/component/component.hpp @@ -27,7 +27,9 @@ std::shared_ptr Make(Args&&... args) { Component Button(ConstStringRef label, std::function on_click, ConstRef = {}); -Component Checkbox(ConstStringRef label, bool* checked); +Component Checkbox(ConstStringRef label, + bool* checked, + ConstRef option = {}); Component Input(StringRef content, ConstStringRef placeholder); Component Menu(const std::vector* entries, int* selected_, diff --git a/include/ftxui/component/component_options.hpp b/include/ftxui/component/component_options.hpp index 85792ad6..beb4f652 100644 --- a/include/ftxui/component/component_options.hpp +++ b/include/ftxui/component/component_options.hpp @@ -1,6 +1,8 @@ #ifndef FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP #define FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP +#include + namespace ftxui { struct MenuOption { @@ -18,6 +20,17 @@ struct ButtonOption { bool border = true; }; +struct CheckboxOption { + std::wstring checked = L"▣ "; /// Prefix for a "checked" state. + std::wstring unchecked = L"☐ "; /// Prefix for a "unchecked" state. + + Decorator focused_style = inverted; /// Decorator used when focused. + Decorator unfocused_style = nothing; /// Decorator used when unfocused. + + /// Called when the user change the state. + std::function on_change = []() {}; +}; + }; // namespace ftxui #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */ diff --git a/src/ftxui/component/checkbox.cpp b/src/ftxui/component/checkbox.cpp index 8d24d435..a1461c75 100644 --- a/src/ftxui/component/checkbox.cpp +++ b/src/ftxui/component/checkbox.cpp @@ -30,8 +30,10 @@ namespace ftxui { /// ```bash /// ☐ Make a sandwitch /// ``` -Component Checkbox(ConstStringRef label, bool* checked) { - return Make(label, checked); +Component Checkbox(ConstStringRef label, + bool* checked, + ConstRef option) { + return Make(label, checked, std::move(option)); } // static @@ -39,23 +41,25 @@ CheckboxBase* CheckboxBase::From(Component component) { return static_cast(component.get()); } -CheckboxBase::CheckboxBase(ConstStringRef label, bool* state) - : label_(label), state_(state) { +CheckboxBase::CheckboxBase(ConstStringRef label, + bool* state, + ConstRef option) + : label_(label), state_(state), option_(std::move(option)) { #if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK) // Microsoft terminal do not use fonts able to render properly the default // radiobox glyph. - if (checked == L"▣ ") - checked = L"[X]"; - if (unchecked == L"☐ ") - unchecked = L"[ ]"; + if (option->checked == L"▣ ") + option->checked = L"[X]"; + if (option->unchecked == L"☐ ") + option->unchecked = L"[ ]"; #endif } Element CheckboxBase::Render() { bool is_focused = Focused(); - auto style = is_focused ? focused_style : unfocused_style; + auto style = is_focused ? option_->focused_style : option_->unfocused_style; auto focus_management = is_focused ? focus : *state_ ? select : nothing; - return hbox(text(*state_ ? checked : unchecked), + return hbox(text(*state_ ? option_->checked : option_->unchecked), text(*label_) | style | focus_management) | reflect(box_); } @@ -66,7 +70,7 @@ bool CheckboxBase::OnEvent(Event event) { if (event == Event::Character(' ') || event == Event::Return) { *state_ = !*state_; - on_change(); + option_->on_change(); return true; } return false; @@ -83,7 +87,7 @@ bool CheckboxBase::OnMouseEvent(Event event) { if (event.mouse().button == Mouse::Left && event.mouse().motion == Mouse::Pressed) { *state_ = !*state_; - on_change(); + option_->on_change(); return true; }