FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
menu_style.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 <array> // for array
5#include <chrono> // for milliseconds
6#include <functional> // for function
7#include <memory> // for __shared_ptr_access, shared_ptr, allocator
8#include <string> // for string, char_traits, operator+, basic_string
9#include <vector> // for vector
10
11#include "ftxui/component/animation.hpp" // for ElasticOut, Linear
12#include "ftxui/component/component.hpp" // for Menu, Horizontal, Renderer, Vertical
13#include "ftxui/component/component_base.hpp" // for ComponentBase
14#include "ftxui/component/component_options.hpp" // for MenuOption, EntryState, MenuEntryOption, AnimatedColorOption, AnimatedColorsOption, UnderlineOption
15#include "ftxui/component/mouse.hpp" // for ftxui
16#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
17#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, text, bgcolor, hbox, bold, color, filler, border, vbox, borderDouble, dim, flex, hcenter
18#include "ftxui/screen/color.hpp" // for Color, Color::Red, Color::Black, Color::Yellow, Color::Blue, Color::Default, Color::White
19
20using namespace ftxui;
21
22Component VMenu1(std::vector<std::string>* entries, int* selected);
23Component VMenu2(std::vector<std::string>* entries, int* selected);
24Component VMenu3(std::vector<std::string>* entries, int* selected);
25Component VMenu4(std::vector<std::string>* entries, int* selected);
26Component VMenu5(std::vector<std::string>* entries, int* selected);
27Component VMenu6(std::vector<std::string>* entries, int* selected);
28Component VMenu7(std::vector<std::string>* entries, int* selected);
29Component VMenu8(std::vector<std::string>* entries, int* selected);
30Component HMenu1(std::vector<std::string>* entries, int* selected);
31Component HMenu2(std::vector<std::string>* entries, int* selected);
32Component HMenu3(std::vector<std::string>* entries, int* selected);
33Component HMenu4(std::vector<std::string>* entries, int* selected);
34Component HMenu5(std::vector<std::string>* entries, int* selected);
35
36int main() {
38
39 std::vector<std::string> entries{
40 "Monkey", "Dog", "Cat", "Bird", "Elephant", "Cat",
41 };
42 std::array<int, 12> selected = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
43
44 auto vmenu_1_ = VMenu1(&entries, &selected[0]);
45 auto vmenu_2_ = VMenu2(&entries, &selected[1]);
46 auto vmenu_3_ = VMenu3(&entries, &selected[2]);
47 auto vmenu_4_ = VMenu4(&entries, &selected[3]);
48 auto vmenu_5_ = VMenu5(&entries, &selected[4]);
49 auto vmenu_6_ = VMenu6(&entries, &selected[5]);
50 auto vmenu_7_ = VMenu7(&entries, &selected[6]);
51 auto vmenu_8_ = VMenu8(&entries, &selected[7]);
52
53 auto hmenu_1_ = HMenu1(&entries, &selected[8]);
54 auto hmenu_2_ = HMenu2(&entries, &selected[9]);
55 auto hmenu_3_ = HMenu3(&entries, &selected[10]);
56 auto hmenu_4_ = HMenu4(&entries, &selected[11]);
57 auto hmenu_5_ = HMenu5(&entries, &selected[12]);
58
59 auto container = Container::Vertical({
61 vmenu_1_,
62 vmenu_2_,
63 vmenu_3_,
64 vmenu_4_,
65 vmenu_5_,
66 vmenu_6_,
67 vmenu_7_,
68 vmenu_8_,
69 }),
70 hmenu_1_,
71 hmenu_2_,
72 hmenu_3_,
73 hmenu_4_,
74 hmenu_5_,
75 });
76
77 auto renderer = Renderer(container, [&] {
78 return //
79 hbox({
80 vbox({
81 hbox({
82 vmenu_1_->Render(),
83 separator(),
84 vmenu_2_->Render(),
85 separator(),
86 vmenu_3_->Render(),
87 separator(),
88 vmenu_4_->Render(),
89 separator(),
90 vmenu_5_->Render(),
91 vmenu_6_->Render(),
92 separator(),
93 vmenu_7_->Render(),
94 separator(),
95 vmenu_8_->Render(),
96 }),
97 separator(),
98 hmenu_1_->Render(),
99 separator(),
100 hmenu_2_->Render(),
101 separator(),
102 hmenu_3_->Render(),
103 separator(),
104 hmenu_4_->Render(),
105 hmenu_5_->Render(),
106 }) | border,
107 filler(),
108 });
109 });
110
111 screen.Loop(renderer);
112}
113
114Component VMenu1(std::vector<std::string>* entries, int* selected) {
115 auto option = MenuOption::Vertical();
116 option.entries_option.transform = [](EntryState state) {
117 state.label = (state.active ? "> " : " ") + state.label;
118 Element e = text(state.label);
119 if (state.focused)
120 e = e | bgcolor(Color::Blue);
121 if (state.active)
122 e = e | bold;
123 return e;
124 };
125 return Menu(entries, selected, option);
126}
127
128Component VMenu2(std::vector<std::string>* entries, int* selected) {
129 auto option = MenuOption::Vertical();
130 option.entries_option.transform = [](EntryState state) {
131 state.label += (state.active ? " <" : " ");
132 Element e = hbox(filler(), text(state.label));
133 if (state.focused)
134 e = e | bgcolor(Color::Red);
135 if (state.active)
136 e = e | bold;
137 return e;
138 };
139 return Menu(entries, selected, option);
140}
141
142Component VMenu3(std::vector<std::string>* entries, int* selected) {
143 auto option = MenuOption::Vertical();
144 option.entries_option.transform = [](EntryState state) {
145 Element e = state.active ? text("[" + state.label + "]")
146 : text(" " + state.label + " ");
147 if (state.focused)
148 e = e | bold;
149
150 if (state.focused)
151 e = e | color(Color::Blue);
152 if (state.active)
153 e = e | bold;
154 return e;
155 };
156 return Menu(entries, selected, option);
157}
158
159Component VMenu4(std::vector<std::string>* entries, int* selected) {
160 auto option = MenuOption::Vertical();
161 option.entries_option.transform = [](EntryState state) {
162 if (state.active && state.focused) {
163 return text(state.label) | color(Color::Yellow) | bgcolor(Color::Black) |
164 bold;
165 }
166
167 if (state.active) {
168 return text(state.label) | color(Color::Yellow) | bgcolor(Color::Black);
169 }
170 if (state.focused) {
171 return text(state.label) | color(Color::Black) | bgcolor(Color::Yellow) |
172 bold;
173 }
174 return text(state.label) | color(Color::Black) | bgcolor(Color::Yellow);
175 };
176 return Menu(entries, selected, option);
177}
178
179Component VMenu5(std::vector<std::string>* entries, int* selected) {
180 auto option = MenuOption::Vertical();
181 option.entries_option.transform = [](EntryState state) {
182 auto element = text(state.label);
183 if (state.active && state.focused) {
184 return element | borderDouble;
185 }
186 if (state.active) {
187 return element | border;
188 }
189 if (state.focused) {
190 return element | bold;
191 }
192 return element;
193 };
194 return Menu(entries, selected, option);
195}
196
197Component VMenu6(std::vector<std::string>* entries, int* selected) {
198 auto option = MenuOption::VerticalAnimated();
199 option.underline.color_inactive = Color::Default;
200 option.underline.color_active = Color::Red;
201 option.underline.SetAnimationFunction(animation::easing::Linear);
202 return Menu(entries, selected, option);
203}
204
205Component VMenu7(std::vector<std::string>* entries, int* selected) {
206 auto option = MenuOption::Vertical();
207 option.entries_option.animated_colors.foreground.enabled = true;
208 option.entries_option.animated_colors.background.enabled = true;
209 option.entries_option.animated_colors.background.active = Color::Red;
210 option.entries_option.animated_colors.background.inactive = Color::Black;
211 option.entries_option.animated_colors.foreground.active = Color::White;
212 option.entries_option.animated_colors.foreground.inactive = Color::Red;
213 return Menu(entries, selected, option);
214}
215
216Component VMenu8(std::vector<std::string>* entries, int* selected) {
217 auto option = MenuOption::Vertical();
218 option.entries_option.animated_colors.foreground.Set(
219 Color::Red, Color::White, std::chrono::milliseconds(500));
220 return Menu(entries, selected, option);
221}
222
223Component HMenu1(std::vector<std::string>* entries, int* selected) {
224 return Menu(entries, selected, MenuOption::Horizontal());
225}
226
227Component HMenu2(std::vector<std::string>* entries, int* selected) {
228 return Menu(entries, selected, MenuOption::Toggle());
229}
230
231Component HMenu3(std::vector<std::string>* entries, int* selected) {
232 auto option = MenuOption::Toggle();
233 option.elements_infix = [] { return text(" 🮣🮠 "); };
234
235 return Menu(entries, selected, option);
236}
237
238Component HMenu4(std::vector<std::string>* entries, int* selected) {
239 return Menu(entries, selected, MenuOption::HorizontalAnimated());
240}
241
242Component HMenu5(std::vector<std::string>* entries, int* selected) {
243 auto option = MenuOption::HorizontalAnimated();
244 option.underline.SetAnimation(std::chrono::milliseconds(1500),
245 animation::easing::ElasticOut);
246 option.entries_option.transform = [](EntryState state) {
247 Element e = text(state.label) | hcenter | flex;
248 if (state.active && state.focused)
249 e = e | bold;
250 if (!state.focused && !state.active)
251 e = e | dim;
252 return e;
253 };
254 option.underline.color_inactive = Color::Default;
255 option.underline.color_active = Color::Red;
256 return Menu(entries, selected, option);
257}
static ScreenInteractive TerminalOutput()
static MenuOption Toggle()
Standard options for a horizontal menu with some separator. This can be useful to implement a tab bar...
static MenuOption Horizontal()
Standard options for a horizontal menu. This can be useful to implement a tab bar.
static MenuOption VerticalAnimated()
Standard options for an animated vertical menu. This can be useful to implement a list of selectable ...
static MenuOption Vertical()
Standard options for a vertical menu. This can be useful to implement a list of selectable items.
static MenuOption HorizontalAnimated()
Standard options for an animated horizontal menu. This can be useful to implement a tab bar.
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:59
Decorator bgcolor(Color)
Decorate using a background color.
Element borderDouble(Element)
Draw a double border around the element.
Element flex(Element)
Make a child element to expand proportionally to the space left in a container.
Definition flex.cpp:123
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Element filler()
An element that will take expand proportionally to the space left in a container.
Definition flex.cpp:98
Element dim(Element)
Use a light font, for elements with less emphasis.
Definition dim.cpp:33
Element border(Element)
Draw a border around the element.
Decorator color(Color)
Decorate using a foreground color.
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
Component HMenu5(std::vector< std::string > *entries, int *selected)
Component HMenu4(std::vector< std::string > *entries, int *selected)
Component VMenu5(std::vector< std::string > *entries, int *selected)
Component HMenu1(std::vector< std::string > *entries, int *selected)
Component HMenu2(std::vector< std::string > *entries, int *selected)
Component VMenu2(std::vector< std::string > *entries, int *selected)
Component HMenu3(std::vector< std::string > *entries, int *selected)
Component VMenu1(std::vector< std::string > *entries, int *selected)
int main()
Component VMenu3(std::vector< std::string > *entries, int *selected)
Component VMenu6(std::vector< std::string > *entries, int *selected)
Component VMenu7(std::vector< std::string > *entries, int *selected)
Component VMenu8(std::vector< std::string > *entries, int *selected)
Component VMenu4(std::vector< std::string > *entries, int *selected)
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
std::shared_ptr< ComponentBase > Component
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...