24class ToggleBase :
public ComponentBase {
26 ToggleBase(ConstStringListRef entries,
28 Ref<ToggleOption> option)
29 : entries_(entries), selected_(selected), option_(std::move(option)) {}
35 bool is_toggle_focused = Focused();
36 for (
int i = 0; i <
size(); ++i) {
41 bool is_focused = (focused_entry() == i) && is_toggle_focused;
42 bool is_selected = (*selected_ == 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 {
60 return OnMouseEvent(event);
62 int old_selected = *selected_;
68 *selected_ = (*selected_ + 1) %
size();
70 *selected_ = (*selected_ +
size() - 1) %
size();
74 if (old_selected != *selected_) {
75 focused_entry() = *selected_;
88 bool OnMouseEvent(Event event) {
89 if (!CaptureMouse(event))
91 for (
int i = 0; i <
size(); ++i) {
92 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
100 if (*selected_ != i) {
102 option_->on_change();
111 boxes_.resize(
size());
116 bool Focusable() const final {
return size(); }
117 int& focused_entry() {
return option_->focused_entry(); }
118 int size()
const {
return entries_.size(); }
120 ConstStringListRef entries_;
123 std::vector<Box> boxes_;
124 Ref<ToggleOption> 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)
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)
Element separator(void)
Draw a vertical or horizontal separation in between two other elements.
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
static const Event TabReverse
static const Event Return
static const Event ArrowLeft
static const Event ArrowRight