26class RadioboxBase :
public ComponentBase {
28 RadioboxBase(ConstStringListRef entries,
30 Ref<RadioboxOption> option)
31 : entries_(entries), selected_(selected), option_(std::move(option)) {
32#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
35 if (option_->style_checked ==
"◉ ")
36 option_->style_checked =
"(*)";
37 if (option_->style_unchecked ==
"○ ")
38 option_->style_unchecked =
"( )";
40 hovered_ = *selected_;
47 bool is_menu_focused = Focused();
48 for (
int i = 0; i <
size(); ++i) {
49 bool is_focused = (focused_entry() == i) && is_menu_focused;
50 bool is_selected = (hovered_ == i);
52 auto style = is_selected ? (is_focused ? option_->style_selected_focused
53 : option_->style_selected)
54 : (is_focused ? option_->style_focused
55 : option_->style_normal);
56 auto focus_management = !is_selected ?
nothing
57 : is_menu_focused ?
focus
60 const std::string& symbol =
61 *selected_ == i ? option_->style_checked : option_->style_unchecked;
62 elements.push_back(
hbox(
text(symbol),
text(entries_[i]) | style) |
63 focus_management |
reflect(boxes_[i]));
68 bool OnEvent(Event event)
override {
70 if (!CaptureMouse(event))
74 return OnMouseEvent(event);
77 int old_hovered = hovered_;
83 (hovered_) -= box_.y_max - box_.y_min;
85 (hovered_) += box_.y_max - box_.y_min;
89 (hovered_) =
size() - 1;
91 hovered_ = (hovered_ + 1) %
size();
93 hovered_ = (hovered_ +
size() - 1) %
size();
97 if (hovered_ != old_hovered) {
98 focused_entry() = hovered_;
104 if (event == Event::Character(
' ') || event ==
Event::Return) {
105 *selected_ = hovered_;
107 option_->on_change();
113 bool OnMouseEvent(Event event) {
116 return OnMouseWheel(event);
119 for (
int i = 0; i <
size(); ++i) {
120 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
127 if (*selected_ != i) {
129 option_->on_change();
138 bool OnMouseWheel(Event event) {
139 if (!box_.Contain(event.mouse().x, event.mouse().y))
142 int old_hovered = hovered_;
151 if (hovered_ != old_hovered)
152 option_->on_change();
158 boxes_.resize(
size());
164 bool Focusable() const final {
return entries_.size(); }
165 int& focused_entry() {
return option_->focused_entry(); }
166 int size()
const {
return entries_.size(); }
168 ConstStringListRef entries_;
171 std::vector<Box> boxes_;
173 Ref<RadioboxOption> option_;
An adapter. Reference a list of strings.
An adapter. Own or reference an mutable object.
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Element nothing(Element element)
A decoration doing absolutely nothing.
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
Element hbox(Elements)
A container displaying elements horizontally one by one.
std::vector< Element > Elements
Element text(std::wstring text)
Display a piece of unicode text.
Decorator reflect(Box &box)
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
std::shared_ptr< ComponentBase > Component
Element vbox(Elements)
A container displaying elements vertically one by one.
static const Event TabReverse
static const Event PageUp
static const Event ArrowUp
static const Event ArrowDown
static const Event PageDown
static const Event Return