24class RadioboxBase :
public ComponentBase {
26 RadioboxBase(ConstStringListRef entries,
28 Ref<RadioboxOption> option)
29 : entries_(entries), selected_(selected), option_(std::move(option)) {}
35 const bool is_menu_focused = Focused();
36 for (
int i = 0; i <
size(); ++i) {
37 const bool is_focused = (focused_entry() == i) && is_menu_focused;
38 const bool is_selected = (hovered_ == i);
39 auto focus_management = !is_selected ?
nothing
40 : is_menu_focused ?
focus
42 auto state = EntryState{
49 (option_->transform ? option_->transform
52 elements.push_back(element | focus_management |
reflect(boxes_[i]));
58 bool OnEvent(Event event)
override {
60 if (!CaptureMouse(event)) {
64 if (event.is_mouse()) {
65 return OnMouseEvent(event);
69 const int old_hovered = hovered_;
77 (hovered_) -= box_.y_max - box_.y_min;
80 (hovered_) += box_.y_max - box_.y_min;
86 (hovered_) =
size() - 1;
89 hovered_ = (hovered_ + 1) %
size();
92 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_;
106 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)) {
128 if (*selected_ != i) {
130 option_->on_change();
139 bool OnMouseWheel(Event event) {
140 if (!box_.Contain(event.mouse().x, event.mouse().y)) {
144 const int old_hovered = hovered_;
155 if (hovered_ != old_hovered) {
156 option_->on_change();
163 boxes_.resize(
size());
169 bool Focusable() const final {
return entries_.size(); }
170 int& focused_entry() {
return option_->focused_entry(); }
171 int size()
const {
return int(entries_.size()); }
173 ConstStringListRef entries_;
175 int hovered_ = *selected_;
176 std::vector<Box> boxes_;
178 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
std::vector< Element > Elements
Decorator reflect(Box &box)
void Render(Screen &screen, const Element &element)
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
static RadioboxOption Simple()
Option for standard Radiobox.
std::function< Element(const EntryState &)> transform