24class ToggleBase :
public ComponentBase {
26 ToggleBase(ConstStringListRef entries,
28 Ref<ToggleOption> option)
29 : entries_(entries), selected_(selected), option_(std::move(option)) {}
34 bool is_toggle_focused = Focused();
35 boxes_.resize(entries_.size());
36 for (
size_t i = 0; i < entries_.size(); ++i) {
41 bool is_focused = (focused_entry() == int(i)) && is_toggle_focused;
42 bool is_selected = (*selected_ == int(i));
44 auto style = is_selected ? (is_focused ? option_->style_selected_focused
45 : option_->style_selected)
46 : (is_focused ? option_->style_focused
47 : option_->style_normal);
48 auto focus_management = !is_selected ?
nothing
49 : is_toggle_focused ?
focus
51 children.push_back(
text(entries_[i]) | style | focus_management |
54 return hbox(std::move(children));
57 bool OnEvent(Event event)
override {
59 return OnMouseEvent(event);
61 int old_selected = *selected_;
67 *selected_ = (*selected_ + 1) % entries_.size();
69 *selected_ = (*selected_ + entries_.size() - 1) % entries_.size();
71 *selected_ = std::max(0, std::min(
int(entries_.size()) - 1, *selected_));
73 if (old_selected != *selected_) {
74 focused_entry() = *selected_;
87 bool OnMouseEvent(Event event) {
88 if (!CaptureMouse(event))
90 for (
int i = 0; i < int(boxes_.size()); ++i) {
91 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
99 if (*selected_ != i) {
101 option_->on_change();
109 bool Focusable() const final {
return entries_.size(); }
110 int& focused_entry() {
return option_->focused_entry(); }
112 ConstStringListRef entries_;
115 std::vector<Box> boxes_;
116 Ref<ToggleOption> option_;
An adapter. Reference a list of strings.
An adapter. Own or reference an mutable object.
Element nothing(Element element)
A decoration doing absolutely nothing.
Component Toggle(ConstStringListRef entries, int *selected, Ref< ToggleOption > option={})
An horizontal list of elements. The user can navigate through them.
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.
std::shared_ptr< ComponentBase > Component
static const Event TabReverse
static const Event Return
static const Event ArrowLeft
static const Event ArrowRight