FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
menu_entries.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 <functional> // for function
5#include <iostream> // for basic_ostream::operator<<, operator<<, endl, basic_ostream, basic_ostream<>::__ostream_type, cout, ostream
6#include <memory> // for allocator, shared_ptr, __shared_ptr_access
7#include <string> // for char_traits, to_string, operator+, string, basic_string
8
9#include "ftxui/component/app.hpp" // for App
10#include "ftxui/component/captured_mouse.hpp" // for ftxui
11#include "ftxui/component/component.hpp" // for MenuEntry, Renderer, Vertical
12#include "ftxui/component/component_base.hpp" // for ComponentBase
13#include "ftxui/component/component_options.hpp" // for MenuEntryOption
14#include "ftxui/dom/elements.hpp" // for operator|, Element, separator, text, hbox, size, frame, color, vbox, HEIGHT, LESS_THAN, bold, border, inverted
15#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::Cyan, Color::Green, Color::Red, Color::Yellow
16
17using namespace ftxui;
18
19// Définit un style spécial pour certaines entrées de menu.
20MenuEntryOption Colored(ftxui::Color c) {
21 MenuEntryOption option;
22 option.transform = [c](EntryState state) {
23 state.label = (state.active ? "> " : " ") + state.label;
24 Element e = text(state.label) | color(c);
25 if (state.focused) {
26 e = e | inverted;
27 }
28 if (state.active) {
29 e = e | bold;
30 }
31 return e;
32 };
33 return option;
34}
35
36int main() {
37 auto screen = App::TerminalOutput();
38
39 int selected = 0;
40 auto menu = Container::Vertical(
41 {
42 MenuEntry(" 1. improve"),
43 MenuEntry(" 2. tolerant"),
44 MenuEntry(" 3. career"),
45 MenuEntry(" 4. cast"),
46 MenuEntry(" 5. question"),
47
48 Renderer([] { return separator(); }),
49
50 MenuEntry(" 6. rear", Colored(Color::Red)),
51 MenuEntry(" 7. drown", Colored(Color::Yellow)),
52 MenuEntry(" 8. nail", Colored(Color::Green)),
53 MenuEntry(" 9. quit", Colored(Color::Cyan)),
54 MenuEntry("10. decorative", Colored(Color::Blue)),
55
56 Renderer([] { return separator(); }),
57
58 MenuEntry("11. costume"),
59 MenuEntry("12. pick"),
60 MenuEntry("13. oral"),
61 MenuEntry("14. minister"),
62 MenuEntry("15. football"),
63 MenuEntry("16. welcome"),
64 MenuEntry("17. copper"),
65 MenuEntry("18. inhabitant"),
66 MenuEntry("19. fortune"),
67 },
68 &selected);
69
70 // Affiche le menu avec une bordure
71 auto renderer = Renderer(menu, [&] {
72 return vbox({
73 hbox(text("selected = "), text(std::to_string(selected))),
74 separator(),
75 menu->Render() | frame | size(HEIGHT, LESS_THAN, 10),
76 }) |
77 border;
78 });
79
80 screen.Loop(renderer);
81
82 std::cout << "Selected element = " << selected << std::endl;
83}
Component Renderer(Component child, std::function< Element()>)
Retourne un nouveau Component, similaire à |child|, mais utilisant |render| comme événement Component...
Component MenuEntry(ConstStringRef label, MenuEntryOption options={})
Une entrée de menu spécifique. Elles peuvent être placées dans un Container::Vertical pour former un ...
Element text(std::string_view text)
Affiche un morceau de texte unicode encodé en UTF8.
Decorator size(WidthOrHeight direction, Constraint constraint, int value)
Applique une contrainte sur la taille d'un élément.
Element bold(Element child)
Utilise une police en gras, pour les éléments avec plus d'emphase.
Definition bold.cpp:33
Element inverted(Element child)
Ajoute un filtre qui inversera les couleurs de premier plan et d'arrière-plan.
Definition inverted.cpp:33
Element separator()
Dessine une séparation verticale ou horizontale entre deux autres éléments.
Element color(Color color, Element child)
Définit la couleur de premier plan d'un élément.
Definition dom/color.cpp:81
Element vbox(Elements children)
Un conteneur affichant les éléments verticalement un par un.
Definition vbox.cpp:96
MenuEntryOption Colored(ftxui::Color c)
int main()
L'espace de noms FTXUI ftxui::
Definition animation.hpp:11
@ LESS_THAN
Definition elements.hpp:194
std::shared_ptr< Node > Element
Definition elements.hpp:25
Element hbox(Elements children)
Un conteneur affichant les éléments horizontalement un par un.
Definition hbox.cpp:94