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 }
122 if (state.active) {
123 e = e | bold;
124 }
125 return e;
126 };
127 return Menu(entries, selected, option);
128}
129
130Component VMenu2(std::vector<std::string>* entries, int* selected) {
131 auto option = MenuOption::Vertical();
132 option.entries_option.transform = [](EntryState state) {
133 state.label += (state.active ? " <" : " ");
134 Element e = hbox(filler(), text(state.label));
135 if (state.focused) {
136 e = e | bgcolor(Color::Red);
137 }
138 if (state.active) {
139 e = e | bold;
140 }
141 return e;
142 };
143 return Menu(entries, selected, option);
144}
145
146Component VMenu3(std::vector<std::string>* entries, int* selected) {
147 auto option = MenuOption::Vertical();
148 option.entries_option.transform = [](EntryState state) {
149 Element e = state.active ? text("[" + state.label + "]")
150 : text(" " + state.label + " ");
151 if (state.focused) {
152 e = e | bold;
153 }
154
155 if (state.focused) {
156 e = e | color(Color::Blue);
157 }
158 if (state.active) {
159 e = e | bold;
160 }
161 return e;
162 };
163 return Menu(entries, selected, option);
164}
165
166Component VMenu4(std::vector<std::string>* entries, int* selected) {
167 auto option = MenuOption::Vertical();
168 option.entries_option.transform = [](EntryState state) {
169 if (state.active && state.focused) {
170 return text(state.label) | color(Color::Yellow) | bgcolor(Color::Black) |
171 bold;
172 }
173
174 if (state.active) {
175 return text(state.label) | color(Color::Yellow) | bgcolor(Color::Black);
176 }
177 if (state.focused) {
178 return text(state.label) | color(Color::Black) | bgcolor(Color::Yellow) |
179 bold;
180 }
181 return text(state.label) | color(Color::Black) | bgcolor(Color::Yellow);
182 };
183 return Menu(entries, selected, option);
184}
185
186Component VMenu5(std::vector<std::string>* entries, int* selected) {
187 auto option = MenuOption::Vertical();
188 option.entries_option.transform = [](EntryState state) {
189 auto element = text(state.label);
190 if (state.active && state.focused) {
191 return element | borderDouble;
192 }
193 if (state.active) {
194 return element | border;
195 }
196 if (state.focused) {
197 return element | bold;
198 }
199 return element;
200 };
201 return Menu(entries, selected, option);
202}
203
204Component VMenu6(std::vector<std::string>* entries, int* selected) {
205 auto option = MenuOption::VerticalAnimated();
206 option.underline.color_inactive = Color::Default;
207 option.underline.color_active = Color::Red;
208 option.underline.SetAnimationFunction(animation::easing::Linear);
209 return Menu(entries, selected, option);
210}
211
212Component VMenu7(std::vector<std::string>* entries, int* selected) {
213 auto option = MenuOption::Vertical();
214 option.entries_option.animated_colors.foreground.enabled = true;
215 option.entries_option.animated_colors.background.enabled = true;
216 option.entries_option.animated_colors.background.active = Color::Red;
217 option.entries_option.animated_colors.background.inactive = Color::Black;
218 option.entries_option.animated_colors.foreground.active = Color::White;
219 option.entries_option.animated_colors.foreground.inactive = Color::Red;
220 return Menu(entries, selected, option);
221}
222
223Component VMenu8(std::vector<std::string>* entries, int* selected) {
224 auto option = MenuOption::Vertical();
225 option.entries_option.animated_colors.foreground.Set(
226 Color::Red, Color::White, std::chrono::milliseconds(500));
227 return Menu(entries, selected, option);
228}
229
230Component HMenu1(std::vector<std::string>* entries, int* selected) {
231 return Menu(entries, selected, MenuOption::Horizontal());
232}
233
234Component HMenu2(std::vector<std::string>* entries, int* selected) {
235 return Menu(entries, selected, MenuOption::Toggle());
236}
237
238Component HMenu3(std::vector<std::string>* entries, int* selected) {
239 auto option = MenuOption::Toggle();
240 option.elements_infix = [] { return text(" 🮣🮠 "); };
241
242 return Menu(entries, selected, option);
243}
244
245Component HMenu4(std::vector<std::string>* entries, int* selected) {
246 return Menu(entries, selected, MenuOption::HorizontalAnimated());
247}
248
249Component HMenu5(std::vector<std::string>* entries, int* selected) {
250 auto option = MenuOption::HorizontalAnimated();
251 option.underline.SetAnimation(std::chrono::milliseconds(1500),
252 animation::easing::ElasticOut);
253 option.entries_option.transform = [](EntryState state) {
254 Element e = text(state.label) | hcenter | flex;
255 if (state.active && state.focused) {
256 e = e | bold;
257 }
258 if (!state.focused && !state.active) {
259 e = e | dim;
260 }
261 return e;
262 };
263 option.underline.color_inactive = Color::Default;
264 option.underline.color_active = Color::Red;
265 return Menu(entries, selected, option);
266}
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|,...