diff --git a/include/ftxui/component/component.hpp b/include/ftxui/component/component.hpp index 8a59d11..f8624cd 100644 --- a/include/ftxui/component/component.hpp +++ b/include/ftxui/component/component.hpp @@ -36,7 +36,9 @@ Component Input(StringRef content, Component Menu(const std::vector* entries, int* selected_, ConstRef = {}); -Component Radiobox(const std::vector* entries, int* selected_); +Component Radiobox(const std::vector* entries, + int* selected_, + ConstRef option = {}); Component Toggle(const std::vector* entries, int* selected); template // T = {int, float, long} Component Slider(StringRef label, T* value, T min, T max, T increment); diff --git a/include/ftxui/component/component_options.hpp b/include/ftxui/component/component_options.hpp index 897fc0b..13f69d2 100644 --- a/include/ftxui/component/component_options.hpp +++ b/include/ftxui/component/component_options.hpp @@ -36,6 +36,16 @@ struct InputOption { std::function on_enter = [] {}; }; +struct RadioboxOption { + std::wstring checked = L"◉ "; + std::wstring unchecked = L"○ "; + + Decorator focused_style = inverted; + Decorator unfocused_style = nothing; + + std::function on_change = []() {}; +}; + }; // namespace ftxui #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */ diff --git a/include/ftxui/component/radiobox.hpp b/include/ftxui/component/radiobox.hpp index 9614f95..897a7fe 100644 --- a/include/ftxui/component/radiobox.hpp +++ b/include/ftxui/component/radiobox.hpp @@ -22,20 +22,13 @@ class RadioboxBase : public ComponentBase { static RadioboxBase* From(Component component); // Constructor. - RadioboxBase(const std::vector* entries, int* selected); + RadioboxBase(const std::vector* entries, + int* selected, + ConstRef option); ~RadioboxBase() override = default; int focused = 0; - std::wstring checked = L"◉ "; - std::wstring unchecked = L"○ "; - - Decorator focused_style = inverted; - Decorator unfocused_style = nothing; - - // State update callback. - std::function on_change = []() {}; - // Component implementation. Element Render() override; bool OnEvent(Event) override; @@ -47,6 +40,7 @@ class RadioboxBase : public ComponentBase { bool OnMouseEvent(Event event); int cursor_position = 0; std::vector boxes_; + ConstRef option_; }; } // namespace ftxui diff --git a/src/ftxui/component/radiobox.cpp b/src/ftxui/component/radiobox.cpp index b721e2d..5288883 100644 --- a/src/ftxui/component/radiobox.cpp +++ b/src/ftxui/component/radiobox.cpp @@ -39,8 +39,10 @@ namespace ftxui { /// ○ entry 2 /// ○ entry 3 /// ``` -Component Radiobox(const std::vector* entries, int* selected) { - return Make(entries, selected); +Component Radiobox(const std::vector* entries, + int* selected, + ConstRef option) { + return Make(entries, selected, std::move(option)); } // static @@ -49,15 +51,16 @@ RadioboxBase* RadioboxBase::From(Component component) { } RadioboxBase::RadioboxBase(const std::vector* entries, - int* selected) - : entries_(entries), selected_(selected) { + int* selected, + ConstRef option) + : entries_(entries), selected_(selected), 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"(*)"; - if (unchecked == L"○ ") - unchecked = L"( )"; + if (option_->checked == L"◉ ") + option_->checked = L"(*)"; + if (option_->unchecked == L"○ ") + option_->unchecked = L"( )"; #endif } @@ -66,13 +69,14 @@ Element RadioboxBase::Render() { bool is_focused = Focused(); boxes_.resize(entries_->size()); for (size_t i = 0; i < entries_->size(); ++i) { - auto style = - (focused == int(i) && is_focused) ? focused_style : unfocused_style; + auto style = (focused == int(i) && is_focused) ? option_->focused_style + : option_->unfocused_style; auto focus_management = (focused != int(i)) ? nothing : is_focused ? focus : select; - const std::wstring& symbol = *selected_ == int(i) ? checked : unchecked; + const std::wstring& symbol = + *selected_ == int(i) ? option_->checked : option_->unchecked; elements.push_back(hbox(text(symbol), text(entries_->at(i)) | style) | focus_management | reflect(boxes_[i])); } @@ -107,7 +111,7 @@ bool RadioboxBase::OnEvent(Event event) { if (event == Event::Character(' ') || event == Event::Return) { *selected_ = focused; - on_change(); + option_->on_change(); } return false; @@ -129,7 +133,7 @@ bool RadioboxBase::OnMouseEvent(Event event) { TakeFocus(); if (*selected_ != i) { *selected_ = i; - on_change(); + option_->on_change(); } return true; }