Add option for radiobox

This commit is contained in:
ArthurSonzogni 2021-07-10 10:50:25 +02:00 committed by Arthur Sonzogni
parent 33b3d1c7ab
commit ae6473363d
4 changed files with 34 additions and 24 deletions

View File

@ -36,7 +36,9 @@ Component Input(StringRef content,
Component Menu(const std::vector<std::wstring>* entries, Component Menu(const std::vector<std::wstring>* entries,
int* selected_, int* selected_,
ConstRef<MenuOption> = {}); ConstRef<MenuOption> = {});
Component Radiobox(const std::vector<std::wstring>* entries, int* selected_); Component Radiobox(const std::vector<std::wstring>* entries,
int* selected_,
ConstRef<RadioboxOption> option = {});
Component Toggle(const std::vector<std::wstring>* entries, int* selected); Component Toggle(const std::vector<std::wstring>* entries, int* selected);
template <class T> // T = {int, float, long} template <class T> // T = {int, float, long}
Component Slider(StringRef label, T* value, T min, T max, T increment); Component Slider(StringRef label, T* value, T min, T max, T increment);

View File

@ -36,6 +36,16 @@ struct InputOption {
std::function<void()> on_enter = [] {}; std::function<void()> on_enter = [] {};
}; };
struct RadioboxOption {
std::wstring checked = L"";
std::wstring unchecked = L"";
Decorator focused_style = inverted;
Decorator unfocused_style = nothing;
std::function<void()> on_change = []() {};
};
}; // namespace ftxui }; // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */ #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */

View File

@ -22,20 +22,13 @@ class RadioboxBase : public ComponentBase {
static RadioboxBase* From(Component component); static RadioboxBase* From(Component component);
// Constructor. // Constructor.
RadioboxBase(const std::vector<std::wstring>* entries, int* selected); RadioboxBase(const std::vector<std::wstring>* entries,
int* selected,
ConstRef<RadioboxOption> option);
~RadioboxBase() override = default; ~RadioboxBase() override = default;
int focused = 0; int focused = 0;
std::wstring checked = L"";
std::wstring unchecked = L"";
Decorator focused_style = inverted;
Decorator unfocused_style = nothing;
// State update callback.
std::function<void()> on_change = []() {};
// Component implementation. // Component implementation.
Element Render() override; Element Render() override;
bool OnEvent(Event) override; bool OnEvent(Event) override;
@ -47,6 +40,7 @@ class RadioboxBase : public ComponentBase {
bool OnMouseEvent(Event event); bool OnMouseEvent(Event event);
int cursor_position = 0; int cursor_position = 0;
std::vector<Box> boxes_; std::vector<Box> boxes_;
ConstRef<RadioboxOption> option_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -39,8 +39,10 @@ namespace ftxui {
/// ○ entry 2 /// ○ entry 2
/// ○ entry 3 /// ○ entry 3
/// ``` /// ```
Component Radiobox(const std::vector<std::wstring>* entries, int* selected) { Component Radiobox(const std::vector<std::wstring>* entries,
return Make<RadioboxBase>(entries, selected); int* selected,
ConstRef<RadioboxOption> option) {
return Make<RadioboxBase>(entries, selected, std::move(option));
} }
// static // static
@ -49,15 +51,16 @@ RadioboxBase* RadioboxBase::From(Component component) {
} }
RadioboxBase::RadioboxBase(const std::vector<std::wstring>* entries, RadioboxBase::RadioboxBase(const std::vector<std::wstring>* entries,
int* selected) int* selected,
: entries_(entries), selected_(selected) { ConstRef<RadioboxOption> option)
: entries_(entries), selected_(selected), option_(std::move(option)) {
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK) #if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
// Microsoft terminal do not use fonts able to render properly the default // Microsoft terminal do not use fonts able to render properly the default
// radiobox glyph. // radiobox glyph.
if (checked == L"") if (option_->checked == L"")
checked = L"(*)"; option_->checked = L"(*)";
if (unchecked == L"") if (option_->unchecked == L"")
unchecked = L"( )"; option_->unchecked = L"( )";
#endif #endif
} }
@ -66,13 +69,14 @@ Element RadioboxBase::Render() {
bool is_focused = Focused(); bool is_focused = Focused();
boxes_.resize(entries_->size()); boxes_.resize(entries_->size());
for (size_t i = 0; i < entries_->size(); ++i) { for (size_t i = 0; i < entries_->size(); ++i) {
auto style = auto style = (focused == int(i) && is_focused) ? option_->focused_style
(focused == int(i) && is_focused) ? focused_style : unfocused_style; : option_->unfocused_style;
auto focus_management = (focused != int(i)) ? nothing auto focus_management = (focused != int(i)) ? nothing
: is_focused ? focus : is_focused ? focus
: select; : 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) | elements.push_back(hbox(text(symbol), text(entries_->at(i)) | style) |
focus_management | reflect(boxes_[i])); focus_management | reflect(boxes_[i]));
} }
@ -107,7 +111,7 @@ bool RadioboxBase::OnEvent(Event event) {
if (event == Event::Character(' ') || event == Event::Return) { if (event == Event::Character(' ') || event == Event::Return) {
*selected_ = focused; *selected_ = focused;
on_change(); option_->on_change();
} }
return false; return false;
@ -129,7 +133,7 @@ bool RadioboxBase::OnMouseEvent(Event event) {
TakeFocus(); TakeFocus();
if (*selected_ != i) { if (*selected_ != i) {
*selected_ = i; *selected_ = i;
on_change(); option_->on_change();
} }
return true; return true;
} }