FTXUI  0.11.1
C++ functional terminal UI.
menu.cpp
Go to the documentation of this file.
1 #include <stddef.h> // for size_t
2 #include <algorithm> // for max, min
3 #include <functional> // for function
4 #include <memory> // for shared_ptr, allocator_traits<>::value_type
5 #include <string> // for operator+, string
6 #include <utility> // for move
7 #include <vector> // for vector, __alloc_traits<>::value_type
8 
9 #include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
10 #include "ftxui/component/component.hpp" // for Make, Menu
11 #include "ftxui/component/component_base.hpp" // for ComponentBase
12 #include "ftxui/component/component_options.hpp" // for MenuOption
13 #include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowUp, Event::Return, Event::Tab, Event::TabReverse
14 #include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Released
15 #include "ftxui/component/screen_interactive.hpp" // for Component
16 #include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, text, vbox, Elements, focus, nothing, select
17 #include "ftxui/screen/box.hpp" // for Box
18 #include "ftxui/util/ref.hpp" // for Ref
19 
20 namespace ftxui {
21 
22 /// @brief A list of items. The user can navigate through them.
23 /// @ingroup component
24 class MenuBase : public ComponentBase {
25  public:
26  MenuBase(ConstStringListRef entries, int* selected, Ref<MenuOption> option)
27  : entries_(entries), selected_(selected), option_(option) {}
28 
29  Element Render() override {
30  Elements elements;
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));
36 
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
43  : select;
44  auto icon = is_selected ? "> " : " ";
45  elements.push_back(text(icon + entries_[i]) | style | focus_management |
46  reflect(boxes_[i]));
47  }
48  return vbox(std::move(elements)) | reflect(box_);
49  }
50 
51  bool OnEvent(Event event) override {
52  if (!CaptureMouse(event))
53  return false;
54 
55  if (event.is_mouse())
56  return OnMouseEvent(event);
57 
58  if (Focused()) {
59  int old_selected = *selected_;
60  if (event == Event::ArrowUp || event == Event::Character('k'))
61  (*selected_)--;
62  if (event == Event::ArrowDown || event == Event::Character('j'))
63  (*selected_)++;
64  if (event == Event::PageUp)
65  (*selected_) -= box_.y_max - box_.y_min;
66  if (event == Event::PageDown)
67  (*selected_) += box_.y_max - box_.y_min;
68  if (event == Event::Home)
69  (*selected_) = 0;
70  if (event == Event::End)
71  (*selected_) = entries_.size() - 1;
72  if (event == Event::Tab && entries_.size())
73  *selected_ = (*selected_ + 1) % entries_.size();
74  if (event == Event::TabReverse && entries_.size())
75  *selected_ = (*selected_ + entries_.size() - 1) % entries_.size();
76 
77  *selected_ = std::max(0, std::min(int(entries_.size()) - 1, *selected_));
78 
79  if (*selected_ != old_selected) {
80  focused_entry() = *selected_;
81  option_->on_change();
82  return true;
83  }
84  }
85 
86  if (event == Event::Return) {
87  option_->on_enter();
88  return true;
89  }
90 
91  return false;
92  }
93 
94  bool OnMouseEvent(Event event) {
95  if (event.mouse().button == Mouse::WheelDown ||
96  event.mouse().button == Mouse::WheelUp) {
97  return OnMouseWheel(event);
98  }
99 
100  if (event.mouse().button != Mouse::None &&
101  event.mouse().button != Mouse::Left) {
102  return false;
103  }
104  if (!CaptureMouse(event))
105  return false;
106  for (int i = 0; i < int(boxes_.size()); ++i) {
107  if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
108  continue;
109 
110  TakeFocus();
111  focused_entry() = i;
112  if (event.mouse().button == Mouse::Left &&
113  event.mouse().motion == Mouse::Released) {
114  if (*selected_ != i) {
115  *selected_ = i;
116  option_->on_change();
117  }
118  return true;
119  }
120  }
121  return false;
122  }
123 
124  bool OnMouseWheel(Event event) {
125  if (!box_.Contain(event.mouse().x, event.mouse().y))
126  return false;
127  int old_selected = *selected_;
128 
129  if (event.mouse().button == Mouse::WheelUp)
130  (*selected_)--;
131  if (event.mouse().button == Mouse::WheelDown)
132  (*selected_)++;
133 
134  *selected_ = std::max(0, std::min(int(entries_.size()) - 1, *selected_));
135 
136  if (*selected_ != old_selected)
137  option_->on_change();
138  return true;
139  }
140 
141  bool Focusable() const final { return entries_.size(); }
142  int& focused_entry() { return option_->focused_entry(); }
143 
144  protected:
145  ConstStringListRef entries_;
146  int* selected_ = 0;
147  Ref<MenuOption> option_;
148 
149  std::vector<Box> boxes_;
150  Box box_;
151 };
152 
153 /// @brief A list of text. The focused element is selected.
154 /// @param entries The list of entries in the menu.
155 /// @param selected The index of the currently selected element.
156 /// @param option Additional optional parameters.
157 /// @ingroup component
158 /// @see MenuBase
159 ///
160 /// ### Example
161 ///
162 /// ```cpp
163 /// auto screen = ScreenInteractive::TerminalOutput();
164 /// std::vector<std::string> entries = {
165 /// "entry 1",
166 /// "entry 2",
167 /// "entry 3",
168 /// };
169 /// int selected = 0;
170 /// auto menu = Menu(&entries, &selected);
171 /// screen.Loop(menu);
172 /// ```
173 ///
174 /// ### Output
175 ///
176 /// ```bash
177 /// > entry 1
178 /// entry 2
179 /// entry 3
180 /// ```
182  int* selected,
183  Ref<MenuOption> option) {
184  return Make<MenuBase>(entries, selected, std::move(option));
185 }
186 
188  class Impl : public ComponentBase {
189  public:
190  Impl(ConstStringRef label, Ref<MenuEntryOption> option)
191  : label_(std::move(label)), option_(std::move(option)) {}
192 
193  private:
194  Element Render() override {
195  bool focused = Focused();
196  auto style =
197  hovered_ ? (focused ? option_->style_selected_focused
198  : option_->style_selected)
199  : (focused ? option_->style_focused : option_->style_normal);
200  auto focus_management = focused ? select : nothing;
201  auto label = focused ? "> " + (*label_) //
202  : " " + (*label_);
203  return text(label) | style | focus_management | reflect(box_);
204  }
205  bool Focusable() const override { return true; }
206  bool OnEvent(Event event) override {
207  if (!event.is_mouse())
208  return false;
209 
210  hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
211 
212  if (!hovered_)
213  return false;
214 
215  if (event.mouse().button == Mouse::Left &&
216  event.mouse().motion == Mouse::Released) {
217  TakeFocus();
218  return true;
219  }
220 
221  return false;
222  }
223  ConstStringRef label_;
224  Ref<MenuEntryOption> option_;
225  Box box_;
226  bool hovered_ = false;
227  };
228 
229  return Make<Impl>(std::move(label), std::move(option));
230 }
231 
232 } // namespace ftxui
233 
234 // Copyright 2020 Arthur Sonzogni. All rights reserved.
235 // Use of this source code is governed by the MIT license that can be found in
236 // the LICENSE file.
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....
Definition: component.cpp:132
CapturedMouse CaptureMouse(const Event &event)
Take the CapturedMouse if available. There is only one component of them. It represents a component t...
Definition: component.cpp:166
void TakeFocus()
Configure all the ancestors to give focus to this component.
Definition: component.cpp:154
An adapter. Reference a list of strings.
Definition: ref.hpp:94
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition: ref.hpp:76
An adapter. Own or reference an mutable object.
Definition: ref.hpp:27
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition: util.cpp:26
std::shared_ptr< Node > Element
Definition: elements.hpp:16
std::shared_ptr< ComponentBase > Component
Element focus(Element)
Definition: frame.cpp:79
Element text(std::wstring text)
Display a piece of unicode text.
Definition: text.cpp:106
Component Menu(ConstStringListRef entries, int *selected_, Ref< MenuOption >={})
A list of text. The focused element is selected.
Definition: menu.cpp:181
std::vector< Element > Elements
Definition: elements.hpp:17
Decorator reflect(Box &box)
Definition: reflect.cpp:39
Component MenuEntry(ConstStringRef label, Ref< MenuEntryOption >={})
Definition: menu.cpp:187
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:40
Element select(Element)
Definition: frame.cpp:38
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition: vbox.cpp:77
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:25
static const Event TabReverse
Definition: event.hpp:46
static const Event PageUp
Definition: event.hpp:52
bool is_mouse() const
Definition: event.hpp:62
static Event Character(std::string)
Definition: event.cpp:10
static const Event ArrowUp
Definition: event.hpp:37
static const Event Tab
Definition: event.hpp:45
static const Event ArrowDown
Definition: event.hpp:38
static const Event End
Definition: event.hpp:50
static const Event Home
Definition: event.hpp:49
struct Mouse & mouse()
Definition: event.hpp:63
static const Event PageDown
Definition: event.hpp:53
static const Event Return
Definition: event.hpp:43
Button button
Definition: mouse.hpp:24
Motion motion
Definition: mouse.hpp:27