24class MenuBase :
public ComponentBase {
26 MenuBase(ConstStringListRef entries,
int* selected, Ref<MenuOption> option)
27 : entries_(entries), selected_(selected), option_(option) {}
32 bool is_menu_focused =
Focused();
33 for (
int i = 0; i < size(); ++i) {
34 bool is_focused = (focused_entry() == i) && is_menu_focused;
35 bool is_selected = (*selected_ == 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 {
57 return OnMouseEvent(event);
60 int old_selected = *selected_;
72 (*selected_) = size() - 1;
74 *selected_ = (*selected_ + 1) % size();
76 *selected_ = (*selected_ + size() - 1) % size();
78 *selected_ =
util::clamp(*selected_, 0, size() - 1);
80 if (*selected_ != old_selected) {
81 focused_entry() = *selected_;
95 bool OnMouseEvent(Event event) {
98 return OnMouseWheel(event);
107 for (
int i = 0; i < size(); ++i) {
108 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
115 if (*selected_ != i) {
117 option_->on_change();
125 bool OnMouseWheel(Event event) {
126 if (!box_.
Contain(event.mouse().x, event.mouse().y))
128 int old_selected = *selected_;
135 *selected_ =
util::clamp(*selected_, 0, size() - 1);
137 if (*selected_ != old_selected)
138 option_->on_change();
143 boxes_.resize(size());
144 *selected_ =
util::clamp(*selected_, 0, size() - 1);
145 focused_entry() =
util::clamp(focused_entry(), 0, size() - 1);
148 bool Focusable() const final {
return entries_.
size(); }
149 int& focused_entry() {
return option_->focused_entry(); }
150 int size()
const {
return entries_.
size(); }
153 ConstStringListRef entries_;
155 Ref<MenuOption> option_;
157 std::vector<Box> boxes_;
199 : label_(std::move(label)), option_(std::move(option)) {}
203 bool focused = Focused();
205 hovered_ ? (focused ? option_->style_selected_focused
206 : option_->style_selected)
207 : (focused ? option_->style_focused : option_->style_normal);
209 auto label = focused ?
"> " + (*label_)
211 return text(label) | style | focus_management |
reflect(box_);
213 bool Focusable()
const override {
return true; }
214 bool OnEvent(
Event event)
override {
218 hovered_ = box_.Contain(event.
mouse().
x, event.
mouse().
y);
234 bool hovered_ =
false;
237 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.
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
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 PageUp
static const Event ArrowUp
static const Event ArrowDown
static const Event PageDown
static const Event Return