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
20 using namespace ftxui;
21
22 Component VMenu1(std::vector<std::string>* entries, int* selected);
23 Component VMenu2(std::vector<std::string>* entries, int* selected);
24 Component VMenu3(std::vector<std::string>* entries, int* selected);
25 Component VMenu4(std::vector<std::string>* entries, int* selected);
26 Component VMenu5(std::vector<std::string>* entries, int* selected);
27 Component VMenu6(std::vector<std::string>* entries, int* selected);
28 Component VMenu7(std::vector<std::string>* entries, int* selected);
29 Component VMenu8(std::vector<std::string>* entries, int* selected);
30 Component HMenu1(std::vector<std::string>* entries, int* selected);
31 Component HMenu2(std::vector<std::string>* entries, int* selected);
32 Component HMenu3(std::vector<std::string>* entries, int* selected);
33 Component HMenu4(std::vector<std::string>* entries, int* selected);
34 Component HMenu5(std::vector<std::string>* entries, int* selected);
35
36 int main() {
37 auto screen = ScreenInteractive::TerminalOutput();
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({
60 Container::Horizontal({
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
114 Component 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
128 Component 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
142 Component 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
159 Component 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
179 Component 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
197 Component 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
205 Component 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
216 Component 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
223 Component HMenu1(std::vector<std::string>* entries, int* selected) {
224 return Menu(entries, selected, MenuOption::Horizontal());
225}
226
227 Component HMenu2(std::vector<std::string>* entries, int* selected) {
228 return Menu(entries, selected, MenuOption::Toggle());
229}
230
231 Component 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
238 Component HMenu4(std::vector<std::string>* entries, int* selected) {
239 return Menu(entries, selected, MenuOption::HorizontalAnimated());
240}
241
242 Component 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}
animation.hpp
color.hpp
component.hpp
component_base.hpp
component_options.hpp
elements.hpp
ftxui::ScreenInteractive::TerminalOutput
static ScreenInteractive TerminalOutput()
Definition screen_interactive.cpp:410
ftxui::MenuOption::Toggle
static MenuOption Toggle()
Standard options for a horizontal menu with some separator. This can be useful to implement a tab bar...
Definition component_options.cpp:145
ftxui::MenuOption::Horizontal
static MenuOption Horizontal()
Standard options for a horizontal menu. This can be useful to implement a tab bar.
Definition component_options.cpp:69
ftxui::MenuOption::VerticalAnimated
static MenuOption VerticalAnimated()
Standard options for an animated vertical menu. This can be useful to implement a list of selectable ...
Definition component_options.cpp:123
ftxui::MenuOption::Vertical
static MenuOption Vertical()
Standard options for a vertical menu. This can be useful to implement a list of selectable items.
Definition component_options.cpp:102
ftxui::MenuOption::HorizontalAnimated
static MenuOption HorizontalAnimated()
Standard options for an animated horizontal menu. This can be useful to implement a tab bar.
Definition component_options.cpp:93
ftxui::Container::Horizontal
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Definition container.cpp:360
ftxui::Menu
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Definition src/ftxui/component/menu.cpp:512
ftxui::Renderer
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition src/ftxui/component/renderer.cpp:61
ftxui::Container::Vertical
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Definition container.cpp:317
ftxui::Node::Render
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:59
ftxui::bgcolor
Decorator bgcolor(Color)
Decorate using a background color.
Definition dom/color.cpp:124
ftxui::borderDouble
Element borderDouble(Element)
Draw a double border around the element.
Definition src/ftxui/dom/border.cpp:405
ftxui::flex
Element flex(Element)
Make a child element to expand proportionally to the space left in a container.
Definition flex.cpp:123
ftxui::bold
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::separator
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Definition src/ftxui/dom/separator.cpp:134
ftxui::filler
Element filler()
An element that will take expand proportionally to the space left in a container.
Definition flex.cpp:98
ftxui::dim
Element dim(Element)
Use a light font, for elements with less emphasis.
Definition dim.cpp:33
ftxui::border
Element border(Element)
Draw a border around the element.
Definition src/ftxui/dom/border.cpp:227
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition dom/color.cpp:110
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
ftxui::Color::Default
@ Default
Definition color.hpp:50
ftxui::Color::White
@ White
Definition color.hpp:69
ftxui::Color::Yellow
@ Yellow
Definition color.hpp:57
ftxui::Color::Black
@ Black
Definition color.hpp:54
ftxui::Color::Red
@ Red
Definition color.hpp:55
ftxui::Color::Blue
@ Blue
Definition color.hpp:58
HMenu5
Component HMenu5(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:242
HMenu4
Component HMenu4(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:238
VMenu5
Component VMenu5(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:179
HMenu1
Component HMenu1(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:223
HMenu2
Component HMenu2(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:227
VMenu2
Component VMenu2(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:128
HMenu3
Component HMenu3(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:231
VMenu1
Component VMenu1(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:114
main
int main()
Definition menu_style.cpp:36
VMenu3
Component VMenu3(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:142
VMenu6
Component VMenu6(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:197
VMenu7
Component VMenu7(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:205
VMenu8
Component VMenu8(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:216
VMenu4
Component VMenu4(std::vector< std::string > *entries, int *selected)
Definition menu_style.cpp:159
mouse.hpp
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::hbox
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
screen_interactive.hpp
ftxui::EntryState
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...
Definition component_options.hpp:22