FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/component/menu.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <algorithm> // for max, fill_n, reverse
5#include <chrono> // for milliseconds
6#include <ftxui/dom/direction.hpp> // for Direction, Direction::Down, Direction::Left, Direction::Right, Direction::Up
7#include <functional> // for function
8#include <string> // for operator+, string
9#include <utility> // for move
10#include <vector> // for vector, __alloc_traits<>::value_type
11
12#include "ftxui/component/animation.hpp" // for Animator, Linear
13#include "ftxui/component/component.hpp" // for Make, Menu, MenuEntry, Toggle
14#include "ftxui/component/component_base.hpp" // for ComponentBase
15#include "ftxui/component/component_options.hpp" // for MenuOption, MenuEntryOption, UnderlineOption, AnimatedColorOption, AnimatedColorsOption, EntryState
16#include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowLeft, Event::ArrowRight, Event::ArrowUp, Event::End, Event::Home, Event::PageDown, Event::PageUp, Event::Return, Event::Tab, Event::TabReverse
17#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Released, Mouse::WheelDown, Mouse::WheelUp, Mouse::None
18#include "ftxui/component/screen_interactive.hpp" // for Component
19#include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, Decorator, nothing, Elements, bgcolor, color, hbox, separatorHSelector, separatorVSelector, vbox, xflex, yflex, text, bold, focus, inverted, select
20#include "ftxui/screen/box.hpp" // for Box
21#include "ftxui/screen/color.hpp" // for Color
22#include "ftxui/screen/util.hpp" // for clamp
23#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef, ConstStringRef
24
25namespace ftxui {
26
27namespace {
28
29Element DefaultOptionTransform(const EntryState& state) {
30 std::string label = (state.active ? "> " : " ") + state.label; // NOLINT
31 Element e = text(std::move(label));
32 if (state.focused) {
33 e = e | inverted;
34 }
35 if (state.active) {
36 e = e | bold;
37 }
38 return e;
39}
40
41bool IsInverted(Direction direction) {
42 switch (direction) {
43 case Direction::Up:
44 case Direction::Left:
45 return true;
46 case Direction::Down:
48 return false;
49 }
50 return false; // NOT_REACHED()
51}
52
53bool IsHorizontal(Direction direction) {
54 switch (direction) {
55 case Direction::Left:
57 return true;
58 case Direction::Down:
59 case Direction::Up:
60 return false;
61 }
62 return false; // NOT_REACHED()
63}
64
65} // namespace
66
67/// @brief A list of items. The user can navigate through them.
68/// @ingroup component
69class MenuBase : public ComponentBase, public MenuOption {
70 public:
71 explicit MenuBase(const MenuOption& option) : MenuOption(option) {}
72
73 bool IsHorizontal() { return ftxui::IsHorizontal(direction); }
74 void OnChange() {
75 if (on_change) {
76 on_change();
77 }
78 }
79
80 void OnEnter() {
81 if (on_enter) {
82 on_enter();
83 }
84 }
85
86 void Clamp() {
87 if (selected() != selected_previous_) {
88 SelectedTakeFocus();
89 }
90 boxes_.resize(size());
91 selected() = util::clamp(selected(), 0, size() - 1);
92 selected_previous_ = util::clamp(selected_previous_, 0, size() - 1);
93 selected_focus_ = util::clamp(selected_focus_, 0, size() - 1);
94 focused_entry() = util::clamp(focused_entry(), 0, size() - 1);
95 }
96
97 void OnAnimation(animation::Params& params) override {
98 animator_first_.OnAnimation(params);
99 animator_second_.OnAnimation(params);
100 for (auto& animator : animator_background_) {
101 animator.OnAnimation(params);
102 }
103 for (auto& animator : animator_foreground_) {
104 animator.OnAnimation(params);
105 }
106 }
107
108 Element OnRender() override {
109 Clamp();
110 UpdateAnimationTarget();
111
112 Elements elements;
113 const bool is_menu_focused = Focused();
114 if (elements_prefix) {
115 elements.push_back(elements_prefix());
116 }
117 elements.reserve(size());
118 for (int i = 0; i < size(); ++i) {
119 if (i != 0 && elements_infix) {
120 elements.push_back(elements_infix());
121 }
122 const bool is_focused = (focused_entry() == i) && is_menu_focused;
123 const bool is_selected = (selected() == i);
124
125 const EntryState state = {
126 entries[i], false, is_selected, is_focused, i,
127 };
128
130 : DefaultOptionTransform) //
131 (state);
132 if (selected_focus_ == i) {
133 element |= focus;
134 }
135 element |= AnimatedColorStyle(i);
136 element |= reflect(boxes_[i]);
137 elements.push_back(element);
138 }
139 if (elements_postfix) {
140 elements.push_back(elements_postfix());
141 }
142
143 if (IsInverted(direction)) {
144 std::reverse(elements.begin(), elements.end());
145 }
146
147 const Element bar =
148 IsHorizontal() ? hbox(std::move(elements)) : vbox(std::move(elements));
149
150 if (!underline.enabled) {
151 return bar | reflect(box_);
152 }
153
154 if (IsHorizontal()) {
155 return vbox({
156 bar | xflex,
157 separatorHSelector(first_, second_, //
160 }) |
161 reflect(box_);
162 } else {
163 return hbox({
164 separatorVSelector(first_, second_, //
167 bar | yflex,
168 }) |
169 reflect(box_);
170 }
171 }
172
173 void SelectedTakeFocus() {
174 selected_previous_ = selected();
175 selected_focus_ = selected();
176 }
177
178 void OnUp() {
179 switch (direction) {
180 case Direction::Up:
181 selected()++;
182 break;
183 case Direction::Down:
184 selected()--;
185 break;
186 case Direction::Left:
187 case Direction::Right:
188 break;
189 }
190 }
191
192 void OnDown() {
193 switch (direction) {
194 case Direction::Up:
195 selected()--;
196 break;
197 case Direction::Down:
198 selected()++;
199 break;
200 case Direction::Left:
201 case Direction::Right:
202 break;
203 }
204 }
205
206 void OnLeft() {
207 switch (direction) {
208 case Direction::Left:
209 selected()++;
210 break;
211 case Direction::Right:
212 selected()--;
213 break;
214 case Direction::Down:
215 case Direction::Up:
216 break;
217 }
218 }
219
220 void OnRight() {
221 switch (direction) {
222 case Direction::Left:
223 selected()--;
224 break;
225 case Direction::Right:
226 selected()++;
227 break;
228 case Direction::Down:
229 case Direction::Up:
230 break;
231 }
232 }
233
234 // NOLINTNEXTLINE(readability-function-cognitive-complexity)
235 bool OnEvent(Event event) override {
236 Clamp();
237 if (!CaptureMouse(event)) {
238 return false;
239 }
240
241 if (event.is_mouse()) {
242 return OnMouseEvent(event);
243 }
244
245 if (Focused()) {
246 const int old_selected = selected();
247 if (event == Event::ArrowUp || event == Event::Character('k')) {
248 OnUp();
249 }
250 if (event == Event::ArrowDown || event == Event::Character('j')) {
251 OnDown();
252 }
253 if (event == Event::ArrowLeft || event == Event::Character('h')) {
254 OnLeft();
255 }
256 if (event == Event::ArrowRight || event == Event::Character('l')) {
257 OnRight();
258 }
259 if (event == Event::PageUp) {
260 selected() -= box_.y_max - box_.y_min;
261 }
262 if (event == Event::PageDown) {
263 selected() += box_.y_max - box_.y_min;
264 }
265 if (event == Event::Home) {
266 selected() = 0;
267 }
268 if (event == Event::End) {
269 selected() = size() - 1;
270 }
271 if (event == Event::Tab && size()) {
272 selected() = (selected() + 1) % size();
273 }
274 if (event == Event::TabReverse && size()) {
275 selected() = (selected() + size() - 1) % size();
276 }
277
278 selected() = util::clamp(selected(), 0, size() - 1);
279
280 if (selected() != old_selected) {
282 SelectedTakeFocus();
283 OnChange();
284 return true;
285 }
286 }
287
288 if (event == Event::Return) {
289 OnEnter();
290 return true;
291 }
292
293 return false;
294 }
295
296 bool OnMouseEvent(Event event) {
297 if (event.mouse().button == Mouse::WheelDown ||
298 event.mouse().button == Mouse::WheelUp) {
299 return OnMouseWheel(event);
300 }
301
302 if (event.mouse().button != Mouse::None &&
303 event.mouse().button != Mouse::Left) {
304 return false;
305 }
306 if (!CaptureMouse(event)) {
307 return false;
308 }
309 for (int i = 0; i < size(); ++i) {
310 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y)) {
311 continue;
312 }
313
314 TakeFocus();
315 focused_entry() = i;
316
317 if (event.mouse().button == Mouse::Left &&
318 event.mouse().motion == Mouse::Pressed) {
319 if (selected() != i) {
320 selected() = i;
321 selected_previous_ = selected();
322 OnChange();
323 }
324 return true;
325 }
326 }
327 return false;
328 }
329
330 bool OnMouseWheel(Event event) {
331 if (!box_.Contain(event.mouse().x, event.mouse().y)) {
332 return false;
333 }
334 const int old_selected = selected();
335
336 if (event.mouse().button == Mouse::WheelUp) {
337 selected()--;
338 }
339 if (event.mouse().button == Mouse::WheelDown) {
340 selected()++;
341 }
342
343 selected() = util::clamp(selected(), 0, size() - 1);
344
345 if (selected() != old_selected) {
346 SelectedTakeFocus();
347 OnChange();
348 }
349 return true;
350 }
351
352 void UpdateAnimationTarget() {
353 UpdateColorTarget();
354 UpdateUnderlineTarget();
355 }
356
357 void UpdateColorTarget() {
358 if (size() != int(animation_background_.size())) {
359 animation_background_.resize(size());
360 animation_foreground_.resize(size());
361 animator_background_.clear();
362 animator_foreground_.clear();
363
364 const int len = size();
365 animator_background_.reserve(len);
366 animator_foreground_.reserve(len);
367 for (int i = 0; i < len; ++i) {
368 animation_background_[i] = 0.F;
369 animation_foreground_[i] = 0.F;
370 animator_background_.emplace_back(&animation_background_[i], 0.F,
371 std::chrono::milliseconds(0),
373 animator_foreground_.emplace_back(&animation_foreground_[i], 0.F,
374 std::chrono::milliseconds(0),
376 }
377 }
378
379 const bool is_menu_focused = Focused();
380 for (int i = 0; i < size(); ++i) {
381 const bool is_focused = (focused_entry() == i) && is_menu_focused;
382 const bool is_selected = (selected() == i);
383 float target = is_selected ? 1.F : is_focused ? 0.5F : 0.F; // NOLINT
384 if (animator_background_[i].to() != target) {
385 animator_background_[i] = animation::Animator(
386 &animation_background_[i], target,
389 animator_foreground_[i] = animation::Animator(
390 &animation_foreground_[i], target,
393 }
394 }
395 }
396
397 Decorator AnimatedColorStyle(int i) {
398 Decorator style = nothing;
400 style = style | color(Color::Interpolate(
401 animation_foreground_[i],
404 }
405
407 style = style | bgcolor(Color::Interpolate(
408 animation_background_[i],
411 }
412 return style;
413 }
414
415 void UpdateUnderlineTarget() {
416 if (!underline.enabled) {
417 return;
418 }
419
420 if (FirstTarget() == animator_first_.to() &&
421 SecondTarget() == animator_second_.to()) {
422 return;
423 }
424
425 if (FirstTarget() >= animator_first_.to()) {
426 animator_first_ = animation::Animator(
427 &first_, FirstTarget(), underline.follower_duration,
429
430 animator_second_ = animation::Animator(
431 &second_, SecondTarget(), underline.leader_duration,
433 } else {
434 animator_first_ = animation::Animator(
435 &first_, FirstTarget(), underline.leader_duration,
437
438 animator_second_ = animation::Animator(
439 &second_, SecondTarget(), underline.follower_duration,
441 }
442 }
443
444 bool Focusable() const final { return entries.size(); }
445 int size() const { return int(entries.size()); }
446 float FirstTarget() {
447 if (boxes_.empty()) {
448 return 0.F;
449 }
450 const int value = IsHorizontal() ? boxes_[selected()].x_min - box_.x_min
451 : boxes_[selected()].y_min - box_.y_min;
452 return float(value);
453 }
454 float SecondTarget() {
455 if (boxes_.empty()) {
456 return 0.F;
457 }
458 const int value = IsHorizontal() ? boxes_[selected()].x_max - box_.x_min
459 : boxes_[selected()].y_max - box_.y_min;
460 return float(value);
461 }
462
463 protected:
464 int selected_previous_ = selected();
465 int selected_focus_ = selected();
466
467 // Mouse click support:
468 std::vector<Box> boxes_;
469 Box box_;
470
471 // Animation support:
472 float first_ = 0.F;
473 float second_ = 0.F;
474 animation::Animator animator_first_ = animation::Animator(&first_, 0.F);
475 animation::Animator animator_second_ = animation::Animator(&second_, 0.F);
476 std::vector<animation::Animator> animator_background_;
477 std::vector<animation::Animator> animator_foreground_;
478 std::vector<float> animation_background_;
479 std::vector<float> animation_foreground_;
480};
481
482/// @brief A list of text. The focused element is selected.
483/// @param option a structure containing all the paramters.
484/// @ingroup component
485///
486/// ### Example
487///
488/// ```cpp
489/// auto screen = ScreenInteractive::TerminalOutput();
490/// std::vector<std::string> entries = {
491/// "entry 1",
492/// "entry 2",
493/// "entry 3",
494/// };
495/// int selected = 0;
496/// auto menu = Menu({
497/// .entries = &entries,
498/// .selected = &selected,
499/// });
500/// screen.Loop(menu);
501/// ```
502///
503/// ### Output
504///
505/// ```bash
506/// > entry 1
507/// entry 2
508/// entry 3
509/// ```
510// NOLINTNEXTLINE
512 return Make<MenuBase>(std::move(option));
513}
514
515/// @brief A list of text. The focused element is selected.
516/// @param entries The list of entries in the menu.
517/// @param selected The index of the currently selected element.
518/// @param option Additional optional parameters.
519/// @ingroup component
520///
521/// ### Example
522///
523/// ```cpp
524/// auto screen = ScreenInteractive::TerminalOutput();
525/// std::vector<std::string> entries = {
526/// "entry 1",
527/// "entry 2",
528/// "entry 3",
529/// };
530/// int selected = 0;
531/// auto menu = Menu(&entries, &selected);
532/// screen.Loop(menu);
533/// ```
534///
535/// ### Output
536///
537/// ```bash
538/// > entry 1
539/// entry 2
540/// entry 3
541/// ```
542Component Menu(ConstStringListRef entries, int* selected, MenuOption option) {
543 option.entries = std::move(entries);
544 option.selected = selected;
545 return Menu(option);
546}
547
548/// @brief An horizontal list of elements. The user can navigate through them.
549/// @param entries The list of selectable entries to display.
550/// @param selected Reference the selected entry.
551/// See also |Menu|.
552/// @ingroup component
553Component Toggle(ConstStringListRef entries, int* selected) {
554 return Menu(std::move(entries), selected, MenuOption::Toggle());
555}
556
557/// @brief A specific menu entry. They can be put into a Container::Vertical to
558/// form a menu.
559/// @param label The text drawn representing this element.
560/// @param option Additional optional parameters.
561/// @ingroup component
562///
563/// ### Example
564///
565/// ```cpp
566/// auto screen = ScreenInteractive::TerminalOutput();
567/// int selected = 0;
568/// auto menu = Container::Vertical({
569/// MenuEntry("entry 1"),
570/// MenuEntry("entry 2"),
571/// MenuEntry("entry 3"),
572/// }, &selected);
573/// screen.Loop(menu);
574/// ```
575///
576/// ### Output
577///
578/// ```bash
579/// > entry 1
580/// entry 2
581/// entry 3
582/// ```
584 option.label = std::move(label);
585 return MenuEntry(std::move(option));
586}
587
588/// @brief A specific menu entry. They can be put into a Container::Vertical to
589/// form a menu.
590/// @param option The parameters.
591/// @ingroup component
592///
593/// ### Example
594///
595/// ```cpp
596/// auto screen = ScreenInteractive::TerminalOutput();
597/// int selected = 0;
598/// auto menu = Container::Vertical({
599/// MenuEntry({.label = "entry 1"}),
600/// MenuEntry({.label = "entry 2"}),
601/// MenuEntry({.label = "entry 3"}),
602/// }, &selected);
603/// screen.Loop(menu);
604/// ```
605///
606/// ### Output
607///
608/// ```bash
609/// > entry 1
610/// entry 2
611/// entry 3
612/// ```
614 class Impl : public ComponentBase, public MenuEntryOption {
615 public:
616 explicit Impl(MenuEntryOption option)
617 : MenuEntryOption(std::move(option)) {}
618
619 private:
620 Element OnRender() override {
621 const bool is_focused = Focused();
622 UpdateAnimationTarget();
623
624 const EntryState state{
625 std::string(label()), false, hovered_, is_focused, Index(),
626 };
627
628 Element element = (transform ? transform : DefaultOptionTransform) //
629 (state);
630
631 if (is_focused) {
632 element |= focus;
633 }
634
635 return element | AnimatedColorStyle() | reflect(box_);
636 }
637
638 void UpdateAnimationTarget() {
639 const bool focused = Focused();
640 float target = focused ? 1.F : hovered_ ? 0.5F : 0.F; // NOLINT
641 if (target == animator_background_.to()) {
642 return;
643 }
644 animator_background_ = animation::Animator(
645 &animation_background_, target, animated_colors.background.duration,
646 animated_colors.background.function);
647 animator_foreground_ = animation::Animator(
648 &animation_foreground_, target, animated_colors.foreground.duration,
649 animated_colors.foreground.function);
650 }
651
652 Decorator AnimatedColorStyle() {
653 Decorator style = nothing;
654 if (animated_colors.foreground.enabled) {
655 style = style |
656 color(Color::Interpolate(animation_foreground_,
657 animated_colors.foreground.inactive,
658 animated_colors.foreground.active));
659 }
660
661 if (animated_colors.background.enabled) {
662 style = style |
663 bgcolor(Color::Interpolate(animation_background_,
664 animated_colors.background.inactive,
665 animated_colors.background.active));
666 }
667 return style;
668 }
669
670 bool Focusable() const override { return true; }
671 bool OnEvent(Event event) override {
672 if (!event.is_mouse()) {
673 return false;
674 }
675
676 hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
677
678 if (!hovered_) {
679 return false;
680 }
681
682 if (event.mouse().button == Mouse::Left &&
683 event.mouse().motion == Mouse::Pressed) {
684 TakeFocus();
685 return true;
686 }
687
688 return false;
689 }
690
691 void OnAnimation(animation::Params& params) override {
692 animator_background_.OnAnimation(params);
693 animator_foreground_.OnAnimation(params);
694 }
695
696 Box box_;
697 bool hovered_ = false;
698
699 float animation_background_ = 0.F;
700 float animation_foreground_ = 0.F;
701 animation::Animator animator_background_ =
702 animation::Animator(&animation_background_, 0.F);
703 animation::Animator animator_foreground_ =
704 animation::Animator(&animation_foreground_, 0.F);
705 };
706
707 return Make<Impl>(std::move(option));
708}
709
710} // namespace ftxui
An adapter. Reference a list of strings.
Definition ref.hpp:117
size_t size() const
Definition ref.hpp:178
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:95
static const Event TabReverse
Definition event.hpp:57
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
std::function< Element()> elements_prefix
static MenuOption Toggle()
Standard options for a horizontal menu with some separator. This can be useful to implement a tab bar...
animation::Duration follower_duration
CapturedMouse CaptureMouse(const Event &event)
Take the CapturedMouse if available. There is only one component of them. It represents a component t...
animation::easing::Function leader_function
MenuEntryOption entries_option
static const Event PageUp
Definition event.hpp:63
animation::easing::Function function
animation::Duration follower_delay
bool is_mouse() const
Definition event.hpp:110
void TakeFocus()
Configure all the ancestors to give focus to this component.
std::function< void()> on_enter
UnderlineOption underline
std::function< Element(const EntryState &state)> transform
animation::Duration leader_duration
struct Mouse mouse
Definition event.hpp:145
ConstStringListRef entries
animation::easing::Function follower_function
static const Event ArrowUp
Definition event.hpp:43
static const Event Tab
Definition event.hpp:56
std::function< Element()> elements_infix
static const Event ArrowDown
Definition event.hpp:44
static const Event End
Definition event.hpp:62
Button button
Definition mouse.hpp:30
static const Event Home
Definition event.hpp:61
std::function< Element()> elements_postfix
AnimatedColorsOption animated_colors
std::function< void()> on_change
static const Event PageDown
Definition event.hpp:64
static const Event Return
Definition event.hpp:54
static const Event ArrowLeft
Definition event.hpp:41
animation::Duration leader_delay
Motion motion
Definition mouse.hpp:33
static const Event ArrowRight
Definition event.hpp:42
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Component MenuEntry(MenuEntryOption options)
A specific menu entry. They can be put into a Container::Vertical to form a menu.
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:30
Option for the MenuEntry component.
Option for the Menu component.
Decorator bgcolor(Color)
Decorate using a background color.
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition flex.cpp:129
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition dom/util.cpp:28
Direction
Direction is an enumeration that represents the four cardinal directions.
Definition direction.hpp:13
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Definition flex.cpp:135
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:164
Element focus(Element)
Set the child to be the one focused among its siblings.
Definition frame.cpp:101
Decorator color(Color)
Decorate using a foreground color.
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
bool Contain(int x, int y) const
Definition box.cpp:42
int y_min
Definition box.hpp:19
int y_max
Definition box.hpp:20
int x_min
Definition box.hpp:17
static Color Interpolate(float t, const Color &a, const Color &b)
Box is a structure that represents a rectangular area in a 2D space.
Definition box.hpp:16
float Linear(float p)
Modeled after the line y = x.
Definition animation.cpp:29
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition util.hpp:11
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::function< Element(Element)> Decorator
Definition elements.hpp:25
Element separatorVSelector(float up, float down, Color unselected_color, Color selected_color)
Draw an vertical bar, with the area in between up/downcolored differently.
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:27
std::shared_ptr< Node > Element
Definition elements.hpp:23
Element separatorHSelector(float left, float right, Color unselected_color, Color selected_color)
Draw a horizontal bar, with the area in between left/right colored differently.
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
std::vector< Element > Elements
Definition elements.hpp:24
Decorator reflect(Box &box)
Definition reflect.cpp:43
std::shared_ptr< ComponentBase > Component
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...