24class MenuBase :
public ComponentBase {
26 MenuBase(ConstStringListRef entries,
int* selected, Ref<MenuOption> option)
27 : entries_(entries), selected_(selected), option_(option) {}
31 bool is_menu_focused =
Focused();
32 boxes_.resize(entries_.
size());
33 for (
size_t i = 0; i < entries_.
size(); ++i) {
34 bool is_focused = (focused_entry() == int(i)) && is_menu_focused;
35 bool is_selected = (*selected_ == int(i));
37 auto style = is_selected ? (is_focused ? option_->style_selected_focused
38 : option_->style_selected)
39 : (is_focused ? option_->style_focused
40 : option_->style_normal);
41 auto focus_management = !is_selected ?
nothing
42 : is_menu_focused ?
focus
44 auto icon = is_selected ?
"> " :
" ";
45 elements.push_back(
text(icon + entries_[i]) | style | focus_management |
51 bool OnEvent(Event event)
override {
56 return OnMouseEvent(event);
59 int old_selected = *selected_;
65 *selected_ = (*selected_ + 1) % entries_.
size();
67 *selected_ = (*selected_ + entries_.
size() - 1) % entries_.
size();
69 *selected_ = std::max(0, std::min(
int(entries_.
size()) - 1, *selected_));
71 if (*selected_ != old_selected) {
72 focused_entry() = *selected_;
86 bool OnMouseEvent(Event event) {
89 return OnMouseWheel(event);
98 for (
int i = 0; i < int(boxes_.size()); ++i) {
99 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
106 if (*selected_ != i) {
108 option_->on_change();
116 bool OnMouseWheel(Event event) {
117 if (!box_.
Contain(event.mouse().x, event.mouse().y))
119 int old_selected = *selected_;
126 *selected_ = std::max(0, std::min(
int(entries_.
size()) - 1, *selected_));
128 if (*selected_ != old_selected)
129 option_->on_change();
133 bool Focusable() const final {
return entries_.
size(); }
134 int& focused_entry() {
return option_->focused_entry(); }
137 ConstStringListRef entries_;
139 Ref<MenuOption> option_;
141 std::vector<Box> boxes_;
183 : label_(std::move(label)), option_(std::move(option)) {}
187 bool focused = Focused();
189 hovered_ ? (focused ? option_->style_selected_focused
190 : option_->style_selected)
191 : (focused ? option_->style_focused : option_->style_normal);
193 auto label = focused ?
"> " + (*label_)
195 return text(label) | style | focus_management |
reflect(box_);
197 bool Focusable()
const override {
return true; }
198 bool OnEvent(
Event event)
override {
202 hovered_ = box_.Contain(event.
mouse().
x, event.
mouse().
y);
218 bool hovered_ =
false;
221 return Make<Impl>(std::move(label), std::move(option));
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
CapturedMouse CaptureMouse(const Event &event)
Take the CapturedMouse if available. There is only one component of them. It represents a component t...
void TakeFocus()
Configure all the ancestors to give focus to this component.
An adapter. Reference a list of strings.
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
An adapter. Own or reference an mutable object.
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
Element text(std::wstring text)
Display a piece of unicode text.
Component Menu(ConstStringListRef entries, int *selected_, Ref< MenuOption >={})
A list of text. The focused element is selected.
Decorator reflect(Box &box)
Component MenuEntry(ConstStringRef label, Ref< MenuEntryOption >={})
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
std::shared_ptr< ComponentBase > Component
Element vbox(Elements)
A container displaying elements vertically one by one.
bool Contain(int x, int y)
Represent an event. It can be key press event, a terminal resize, or more ...
static const Event TabReverse
static const Event ArrowUp
static const Event ArrowDown
static const Event Return