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/app.hpp" // for Component
14#include "ftxui/component/component.hpp" // for Make, Menu, MenuEntry, Toggle
15#include "ftxui/component/component_base.hpp" // for ComponentBase
16#include "ftxui/component/component_options.hpp" // for MenuOption, MenuEntryOption, UnderlineOption, AnimatedColorOption, AnimatedColorsOption, EntryState
17#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
18#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Released, Mouse::WheelDown, Mouse::WheelUp, Mouse::None
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) {
73 }
74
75 bool IsHorizontal() { return ftxui::IsHorizontal(direction); }
76 void OnChange() {
77 if (on_change) {
78 on_change();
79 }
80 }
81
82 void OnEnter() {
83 if (on_enter) {
84 on_enter();
85 }
86 }
87
88 void Clamp() {
89 if (selected() != selected_previous_) {
90 SelectedTakeFocus();
91 }
92 boxes_.resize(size());
93 selected() = util::clamp(selected(), 0, size() - 1);
94 selected_previous_ = util::clamp(selected_previous_, 0, size() - 1);
95 selected_focus_ = util::clamp(selected_focus_, 0, size() - 1);
96 focused_entry() = util::clamp(focused_entry(), 0, size() - 1);
97 }
98
99 void OnAnimation(animation::Params& params) override {
100 animator_first_.OnAnimation(params);
101 animator_second_.OnAnimation(params);
102 for (auto& animator : animator_background_) {
103 animator.OnAnimation(params);
104 }
105 for (auto& animator : animator_foreground_) {
106 animator.OnAnimation(params);
107 }
108 }
109
110 Element OnRender() override {
111 Clamp();
112 UpdateAnimationTarget();
113
114 Elements elements;
115 const bool is_menu_focused = Focused();
116 if (elements_prefix) {
117 elements.push_back(elements_prefix());
118 }
119 elements.reserve(size());
120 for (int i = 0; i < size(); ++i) {
121 if (i != 0 && elements_infix) {
122 elements.push_back(elements_infix());
123 }
124 const bool is_focused = (focused_entry() == i) && is_menu_focused;
125 const bool is_selected = (selected() == i);
126
127 const EntryState state = {
128 std::string(entries[i]), false, is_selected, is_focused, i,
129 };
130
132 : DefaultOptionTransform) //
133 (state);
134 if (selected_focus_ == i) {
135 element |= focus;
136 }
137 element |= AnimatedColorStyle(i);
138 element |= reflect(boxes_[i]);
139 elements.push_back(element);
140 }
141 if (elements_postfix) {
142 elements.push_back(elements_postfix());
143 }
144
145 if (IsInverted(direction)) {
146 std::reverse(elements.begin(), elements.end());
147 }
148
149 const Element bar =
150 IsHorizontal() ? hbox(std::move(elements)) : vbox(std::move(elements));
151
152 if (!underline.enabled) {
153 return bar | reflect(box_);
154 }
155
156 if (IsHorizontal()) {
157 return vbox({
158 bar | xflex,
159 separatorHSelector(first_, second_, //
162 }) |
163 reflect(box_);
164 } else {
165 return hbox({
166 separatorVSelector(first_, second_, //
169 bar | yflex,
170 }) |
171 reflect(box_);
172 }
173 }
174
175 void SelectedTakeFocus() {
176 selected_previous_ = selected();
177 selected_focus_ = selected();
178 }
179
180 void OnUp() {
181 switch (direction) {
182 case Direction::Up:
183 selected()++;
184 break;
185 case Direction::Down:
186 selected()--;
187 break;
188 case Direction::Left:
189 case Direction::Right:
190 break;
191 }
192 }
193
194 void OnDown() {
195 switch (direction) {
196 case Direction::Up:
197 selected()--;
198 break;
199 case Direction::Down:
200 selected()++;
201 break;
202 case Direction::Left:
203 case Direction::Right:
204 break;
205 }
206 }
207
208 void OnLeft() {
209 switch (direction) {
210 case Direction::Left:
211 selected()++;
212 break;
213 case Direction::Right:
214 selected()--;
215 break;
216 case Direction::Down:
217 case Direction::Up:
218 break;
219 }
220 }
221
222 void OnRight() {
223 switch (direction) {
224 case Direction::Left:
225 selected()--;
226 break;
227 case Direction::Right:
228 selected()++;
229 break;
230 case Direction::Down:
231 case Direction::Up:
232 break;
233 }
234 }
235
236 // NOLINTNEXTLINE(readability-function-cognitive-complexity)
237 bool OnEvent(Event event) override {
238 Clamp();
239 if (!CaptureMouse(event)) {
240 return false;
241 }
242
243 if (event.is_mouse()) {
244 return OnMouseEvent(event);
245 }
246
247 if (Focused()) {
248 const int old_selected = selected();
249 if (event == Event::ArrowUp || event == Event::Character('k')) {
250 OnUp();
251 }
252 if (event == Event::ArrowDown || event == Event::Character('j')) {
253 OnDown();
254 }
255 if (event == Event::ArrowLeft || event == Event::Character('h')) {
256 OnLeft();
257 }
258 if (event == Event::ArrowRight || event == Event::Character('l')) {
259 OnRight();
260 }
261 if (event == Event::PageUp) {
262 selected() -= box_.y_max - box_.y_min;
263 }
264 if (event == Event::PageDown) {
265 selected() += box_.y_max - box_.y_min;
266 }
267 if (event == Event::Home) {
268 selected() = 0;
269 }
270 if (event == Event::End) {
271 selected() = size() - 1;
272 }
273 if (event == Event::Tab && size()) {
274 selected() = (selected() + 1) % size();
275 }
276 if (event == Event::TabReverse && size()) {
277 selected() = (selected() + size() - 1) % size();
278 }
279
280 selected() = util::clamp(selected(), 0, size() - 1);
281
282 if (selected() != old_selected) {
284 SelectedTakeFocus();
285 OnChange();
286 return true;
287 }
288 }
289
290 if (event == Event::Return) {
291 OnEnter();
292 return true;
293 }
294
295 return false;
296 }
297
298 bool OnMouseEvent(Event event) {
299 if (event.mouse().button == Mouse::WheelDown ||
300 event.mouse().button == Mouse::WheelUp) {
301 return OnMouseWheel(event);
302 }
303
304 if (event.mouse().button != Mouse::None &&
305 event.mouse().button != Mouse::Left) {
306 return false;
307 }
308 if (!CaptureMouse(event)) {
309 return false;
310 }
311 for (int i = 0; i < size(); ++i) {
312 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y)) {
313 continue;
314 }
315
316 TakeFocus();
317 focused_entry() = i;
318
319 if (event.mouse().button == Mouse::Left &&
320 event.mouse().motion == Mouse::Pressed) {
321 if (selected() != i) {
322 selected() = i;
323 selected_previous_ = selected();
324 OnChange();
325 }
326 return true;
327 }
328 }
329 return false;
330 }
331
332 bool OnMouseWheel(Event event) {
333 if (!box_.Contain(event.mouse().x, event.mouse().y)) {
334 return false;
335 }
336 const int old_selected = selected();
337
338 if (event.mouse().button == Mouse::WheelUp) {
339 selected()--;
340 }
341 if (event.mouse().button == Mouse::WheelDown) {
342 selected()++;
343 }
344
345 selected() = util::clamp(selected(), 0, size() - 1);
346
347 if (selected() != old_selected) {
348 SelectedTakeFocus();
349 OnChange();
350 }
351 return true;
352 }
353
354 void UpdateAnimationTarget() {
355 UpdateColorTarget();
356 UpdateUnderlineTarget();
357 }
358
359 void UpdateColorTarget() {
360 if (size() != int(animation_background_.size())) {
361 animation_background_.resize(size());
362 animation_foreground_.resize(size());
363 animator_background_.clear();
364 animator_foreground_.clear();
365
366 const int len = size();
367 animator_background_.reserve(len);
368 animator_foreground_.reserve(len);
369 for (int i = 0; i < len; ++i) {
370 animation_background_[i] = 0.F;
371 animation_foreground_[i] = 0.F;
372 animator_background_.emplace_back(&animation_background_[i], 0.F,
373 std::chrono::milliseconds(0),
375 animator_foreground_.emplace_back(&animation_foreground_[i], 0.F,
376 std::chrono::milliseconds(0),
378 }
379 }
380
381 const bool is_menu_focused = Focused();
382 for (int i = 0; i < size(); ++i) {
383 const bool is_focused = (focused_entry() == i) && is_menu_focused;
384 const bool is_selected = (selected() == i);
385 float target = is_selected ? 1.F : is_focused ? 0.5F : 0.F; // NOLINT
386 if (animator_background_[i].to() != target) {
387 animator_background_[i] = animation::Animator(
388 &animation_background_[i], target,
391 animator_foreground_[i] = animation::Animator(
392 &animation_foreground_[i], target,
395 }
396 }
397 }
398
399 Decorator AnimatedColorStyle(int i) {
400 Decorator style = nothing;
402 style = style | color(Color::Interpolate(
403 animation_foreground_[i],
406 }
407
409 style = style | bgcolor(Color::Interpolate(
410 animation_background_[i],
413 }
414 return style;
415 }
416
417 void UpdateUnderlineTarget() {
418 if (!underline.enabled) {
419 return;
420 }
421
422 if (FirstTarget() == animator_first_.to() &&
423 SecondTarget() == animator_second_.to()) {
424 return;
425 }
426
427 if (FirstTarget() >= animator_first_.to()) {
428 animator_first_ = animation::Animator(
429 &first_, FirstTarget(), underline.follower_duration,
431
432 animator_second_ = animation::Animator(
433 &second_, SecondTarget(), underline.leader_duration,
435 } else {
436 animator_first_ = animation::Animator(
437 &first_, FirstTarget(), underline.leader_duration,
439
440 animator_second_ = animation::Animator(
441 &second_, SecondTarget(), underline.follower_duration,
443 }
444 }
445
446 bool Focusable() const final { return entries.size(); }
447 int size() const { return int(entries.size()); }
448 float FirstTarget() {
449 if (boxes_.empty()) {
450 return 0.F;
451 }
452 const int value = IsHorizontal() ? boxes_[selected()].x_min - box_.x_min
453 : boxes_[selected()].y_min - box_.y_min;
454 return float(value);
455 }
456 float SecondTarget() {
457 if (boxes_.empty()) {
458 return 0.F;
459 }
460 const int value = IsHorizontal() ? boxes_[selected()].x_max - box_.x_min
461 : boxes_[selected()].y_max - box_.y_min;
462 return float(value);
463 }
464
465 protected:
466 int selected_previous_ = selected();
467 int selected_focus_ = selected();
468
469 // Mouse click support:
470 std::vector<Box> boxes_;
471 Box box_;
472
473 // Animation support:
474 float first_ = 0.F;
475 float second_ = 0.F;
476 animation::Animator animator_first_ = animation::Animator(&first_, 0.F);
477 animation::Animator animator_second_ = animation::Animator(&second_, 0.F);
478 std::vector<animation::Animator> animator_background_;
479 std::vector<animation::Animator> animator_foreground_;
480 std::vector<float> animation_background_;
481 std::vector<float> animation_foreground_;
482};
483
484/// @brief A list of text. The focused element is selected.
485/// @param option a structure containing all the paramters.
486/// @ingroup component
487///
488/// ### Example
489///
490/// ```cpp
491/// auto screen = App::TerminalOutput();
492/// std::vector<std::string> entries = {
493/// "entry 1",
494/// "entry 2",
495/// "entry 3",
496/// };
497/// int selected = 0;
498/// auto menu = Menu({
499/// .entries = &entries,
500/// .selected = &selected,
501/// });
502/// screen.Loop(menu);
503/// ```
504///
505/// ### Output
506///
507/// ```bash
508/// > entry 1
509/// entry 2
510/// entry 3
511/// ```
512// NOLINTNEXTLINE
514 return Make<MenuBase>(std::move(option));
515}
516
517/// @brief A list of text. The focused element is selected.
518/// @param entries The list of entries in the menu.
519/// @param selected The index of the currently selected element.
520/// @param option Additional optional parameters.
521/// @ingroup component
522///
523/// ### Example
524///
525/// ```cpp
526/// auto screen = App::TerminalOutput();
527/// std::vector<std::string> entries = {
528/// "entry 1",
529/// "entry 2",
530/// "entry 3",
531/// };
532/// int selected = 0;
533/// auto menu = Menu(&entries, &selected);
534/// screen.Loop(menu);
535/// ```
536///
537/// ### Output
538///
539/// ```bash
540/// > entry 1
541/// entry 2
542/// entry 3
543/// ```
544Component Menu(ConstStringListRef entries, int* selected, MenuOption option) {
545 option.entries = std::move(entries);
546 option.selected = selected;
547 return Menu(option);
548}
549
550/// @brief An horizontal list of elements. The user can navigate through them.
551/// @param entries The list of selectable entries to display.
552/// @param selected Reference the selected entry.
553/// See also |Menu|.
554/// @ingroup component
555Component Toggle(ConstStringListRef entries, int* selected) {
556 return Menu(std::move(entries), selected, MenuOption::Toggle());
557}
558
559/// @brief A specific menu entry. They can be put into a Container::Vertical to
560/// form a menu.
561/// @param label The text drawn representing this element.
562/// @param option Additional optional parameters.
563/// @ingroup component
564///
565/// ### Example
566///
567/// ```cpp
568/// auto screen = App::TerminalOutput();
569/// int selected = 0;
570/// auto menu = Container::Vertical({
571/// MenuEntry("entry 1"),
572/// MenuEntry("entry 2"),
573/// MenuEntry("entry 3"),
574/// }, &selected);
575/// screen.Loop(menu);
576/// ```
577///
578/// ### Output
579///
580/// ```bash
581/// > entry 1
582/// entry 2
583/// entry 3
584/// ```
586 option.label = std::move(label);
587 return MenuEntry(std::move(option));
588}
589
590/// @brief A specific menu entry. They can be put into a Container::Vertical to
591/// form a menu.
592/// @param option The parameters.
593/// @ingroup component
594///
595/// ### Example
596///
597/// ```cpp
598/// auto screen = App::TerminalOutput();
599/// int selected = 0;
600/// auto menu = Container::Vertical({
601/// MenuEntry({.label = "entry 1"}),
602/// MenuEntry({.label = "entry 2"}),
603/// MenuEntry({.label = "entry 3"}),
604/// }, &selected);
605/// screen.Loop(menu);
606/// ```
607///
608/// ### Output
609///
610/// ```bash
611/// > entry 1
612/// entry 2
613/// entry 3
614/// ```
616 class Impl : public ComponentBase, public MenuEntryOption {
617 public:
618 explicit Impl(MenuEntryOption option)
619 : MenuEntryOption(std::move(option)) {}
620
621 private:
622 Element OnRender() override {
623 const bool is_focused = Focused();
624 UpdateAnimationTarget();
625
626 const EntryState state{
627 std::string(label()), false, hovered_, is_focused, Index(),
628 };
629
630 Element element = (transform ? transform : DefaultOptionTransform) //
631 (state);
632
633 if (is_focused) {
634 element |= focus;
635 }
636
637 return element | AnimatedColorStyle() | reflect(box_);
638 }
639
640 void UpdateAnimationTarget() {
641 const bool focused = Focused();
642 float target = focused ? 1.F : hovered_ ? 0.5F : 0.F; // NOLINT
643 if (target == animator_background_.to()) {
644 return;
645 }
646 animator_background_ = animation::Animator(
647 &animation_background_, target, animated_colors.background.duration,
648 animated_colors.background.function);
649 animator_foreground_ = animation::Animator(
650 &animation_foreground_, target, animated_colors.foreground.duration,
651 animated_colors.foreground.function);
652 }
653
654 Decorator AnimatedColorStyle() {
655 Decorator style = nothing;
656 if (animated_colors.foreground.enabled) {
657 style = style |
658 color(Color::Interpolate(animation_foreground_,
659 animated_colors.foreground.inactive,
660 animated_colors.foreground.active));
661 }
662
663 if (animated_colors.background.enabled) {
664 style = style |
665 bgcolor(Color::Interpolate(animation_background_,
666 animated_colors.background.inactive,
667 animated_colors.background.active));
668 }
669 return style;
670 }
671
672 bool Focusable() const override { return true; }
673 bool OnEvent(Event event) override {
674 if (!event.is_mouse()) {
675 return false;
676 }
677
678 hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
679
680 if (!hovered_) {
681 return false;
682 }
683
684 if (event.mouse().button == Mouse::Left &&
685 event.mouse().motion == Mouse::Pressed) {
686 TakeFocus();
687 return true;
688 }
689
690 return false;
691 }
692
693 void OnAnimation(animation::Params& params) override {
694 animator_background_.OnAnimation(params);
695 animator_foreground_.OnAnimation(params);
696 }
697
698 Box box_;
699 bool hovered_ = false;
700
701 float animation_background_ = 0.F;
702 float animation_foreground_ = 0.F;
703 animation::Animator animator_background_ =
704 animation::Animator(&animation_background_, 0.F);
705 animation::Animator animator_foreground_ =
706 animation::Animator(&animation_foreground_, 0.F);
707 };
708
709 return Make<Impl>(std::move(option));
710}
711
712} // namespace ftxui
An adapter. Reference a list of strings.
Definition ref.hpp:141
size_t size() const
Definition ref.hpp:195
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:112
static const Event TabReverse
Definition event.hpp:58
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:64
animation::easing::Function function
animation::Duration follower_delay
bool is_mouse() const
Definition event.hpp:111
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:146
ConstStringListRef entries
animation::easing::Function follower_function
static const Event ArrowUp
Definition event.hpp:44
static const Event Tab
Definition event.hpp:57
std::function< Element()> elements_infix
static const Event ArrowDown
Definition event.hpp:45
static const Event End
Definition event.hpp:63
Button button
Definition mouse.hpp:30
static const Event Home
Definition event.hpp:62
std::function< Element()> elements_postfix
AnimatedColorsOption animated_colors
std::function< void()> on_change
static const Event PageDown
Definition event.hpp:65
static const Event Return
Definition event.hpp:55
static const Event ArrowLeft
Definition event.hpp:42
animation::Duration leader_delay
Motion motion
Definition mouse.hpp:33
static const Event ArrowRight
Definition event.hpp:43
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 focus(Element)
Set the child to be the one focused among its siblings.
Definition frame.cpp:101
Element text(std::wstring_view text)
Display a piece of unicode text.
Definition text.cpp:164
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
Definition app.hpp:24
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...