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// 本原始碼的使用受 MIT 授權條款約束,該條款可在 LICENSE 文件中找到。
3#include <array> // for array
4#include <chrono> // for milliseconds
5#include <functional> // for function
6#include <memory> // for __shared_ptr_access, shared_ptr, allocator
7#include <string> // for string, char_traits, operator+, basic_string
8#include <vector> // for vector
9
10#include "ftxui/component/animation.hpp" // for ElasticOut, Linear
11#include "ftxui/component/component.hpp" // for Menu, Horizontal, Renderer, Vertical
12#include "ftxui/component/component_base.hpp" // for ComponentBase
13#include "ftxui/component/component_options.hpp" // for MenuOption, EntryState, MenuEntryOption, AnimatedColorOption, AnimatedColorsOption, UnderlineOption
14#include "ftxui/component/mouse.hpp" // for ftxui
15#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
16#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, text, bgcolor, hbox, bold, color, filler, border, vbox, borderDouble, dim, flex, hcenter
17#include "ftxui/screen/color.hpp" // for Color, Color::Red, Color::Black, Color::Yellow, Color::Blue, Color::Default, Color::White
18
19using namespace ftxui;
20
21Component VMenu1(std::vector<std::string>* entries, int* selected);
22Component VMenu2(std::vector<std::string>* entries, int* selected);
23Component VMenu3(std::vector<std::string>* entries, int* selected);
24Component VMenu4(std::vector<std::string>* entries, int* selected);
25Component VMenu5(std::vector<std::string>* entries, int* selected);
26Component VMenu6(std::vector<std::string>* entries, int* selected);
27Component VMenu7(std::vector<std::string>* entries, int* selected);
28Component VMenu8(std::vector<std::string>* entries, int* selected);
29Component HMenu1(std::vector<std::string>* entries, int* selected);
30Component HMenu2(std::vector<std::string>* entries, int* selected);
31Component HMenu3(std::vector<std::string>* entries, int* selected);
32Component HMenu4(std::vector<std::string>* entries, int* selected);
33Component HMenu5(std::vector<std::string>* entries, int* selected);
34
35int main() {
37
38 std::vector<std::string> entries{
39 "Monkey", "Dog", "Cat", "Bird", "Elephant", "Cat",
40 };
41 std::array<int, 12> selected = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
42
43 auto vmenu_1_ = VMenu1(&entries, &selected[0]);
44 auto vmenu_2_ = VMenu2(&entries, &selected[1]);
45 auto vmenu_3_ = VMenu3(&entries, &selected[2]);
46 auto vmenu_4_ = VMenu4(&entries, &selected[3]);
47 auto vmenu_5_ = VMenu5(&entries, &selected[4]);
48 auto vmenu_6_ = VMenu6(&entries, &selected[5]);
49 auto vmenu_7_ = VMenu7(&entries, &selected[6]);
50 auto vmenu_8_ = VMenu8(&entries, &selected[7]);
51
52 auto hmenu_1_ = HMenu1(&entries, &selected[8]);
53 auto hmenu_2_ = HMenu2(&entries, &selected[9]);
54 auto hmenu_3_ = HMenu3(&entries, &selected[10]);
55 auto hmenu_4_ = HMenu4(&entries, &selected[11]);
56 auto hmenu_5_ = HMenu5(&entries, &selected[12]);
57
58 auto container = Container::Vertical({
59 Container::Horizontal({
60 vmenu_1_,
61 vmenu_2_,
62 vmenu_3_,
63 vmenu_4_,
64 vmenu_5_,
65 vmenu_6_,
66 vmenu_7_,
67 vmenu_8_,
68 }),
69 hmenu_1_,
70 hmenu_2_,
71 hmenu_3_,
72 hmenu_4_,
73 hmenu_5_,
74 });
75
76 auto renderer = Renderer(container, [&] {
77 return //
78 hbox({
79 vbox({
80 hbox({
81 vmenu_1_->Render(),
82 separator(),
83 vmenu_2_->Render(),
84 separator(),
85 vmenu_3_->Render(),
86 separator(),
87 vmenu_4_->Render(),
88 separator(),
89 vmenu_5_->Render(),
90 vmenu_6_->Render(),
91 separator(),
92 vmenu_7_->Render(),
93 separator(),
94 vmenu_8_->Render(),
95 }),
96 separator(),
97 hmenu_1_->Render(),
98 separator(),
99 hmenu_2_->Render(),
100 separator(),
101 hmenu_3_->Render(),
102 separator(),
103 hmenu_4_->Render(),
104 hmenu_5_->Render(),
105 }) | border,
106 filler(),
107 });
108 });
109
110 screen.Loop(renderer);
111}
112
113Component VMenu1(std::vector<std::string>* entries, int* selected) {
114 auto option = MenuOption::Vertical();
115 option.entries_option.transform = [](EntryState state) {
116 state.label = (state.active ? "> " : " ") + state.label;
117 Element e = text(state.label);
118 if (state.focused) {
119 e = e | bgcolor(Color::Blue);
120 }
121 if (state.active) {
122 e = e | bold;
123 }
124 return e;
125 };
126 return Menu(entries, selected, option);
127}
128
129Component VMenu2(std::vector<std::string>* entries, int* selected) {
130 auto option = MenuOption::Vertical();
131 option.entries_option.transform = [](EntryState state) {
132 state.label += (state.active ? " <" : " ");
133 Element e = hbox(filler(), text(state.label));
134 if (state.focused) {
135 e = e | bgcolor(Color::Red);
136 }
137 if (state.active) {
138 e = e | bold;
139 }
140 return e;
141 };
142 return Menu(entries, selected, option);
143}
144
145Component VMenu3(std::vector<std::string>* entries, int* selected) {
146 auto option = MenuOption::Vertical();
147 option.entries_option.transform = [](EntryState state) {
148 Element e = state.active ? text("[" + state.label + "]")
149 : text(" " + state.label + " ");
150 if (state.focused) {
151 e = e | bold;
152 }
153
154 if (state.focused) {
155 e = e | color(Color::Blue);
156 }
157 if (state.active) {
158 e = e | bold;
159 }
160 return e;
161 };
162 return Menu(entries, selected, option);
163}
164
165Component VMenu4(std::vector<std::string>* entries, int* selected) {
166 auto option = MenuOption::Vertical();
167 option.entries_option.transform = [](EntryState state) {
168 if (state.active && state.focused) {
169 return text(state.label) | color(Color::Yellow) | bgcolor(Color::Black) |
170 bold;
171 }
172
173 if (state.active) {
174 return text(state.label) | color(Color::Yellow) | bgcolor(Color::Black);
175 }
176 if (state.focused) {
177 return text(state.label) | color(Color::Black) | bgcolor(Color::Yellow) |
178 bold;
179 }
180 return text(state.label) | color(Color::Black) | bgcolor(Color::Yellow);
181 };
182 return Menu(entries, selected, option);
183}
184
185Component VMenu5(std::vector<std::string>* entries, int* selected) {
186 auto option = MenuOption::Vertical();
187 option.entries_option.transform = [](EntryState state) {
188 auto element = text(state.label);
189 if (state.active && state.focused) {
190 return element | borderDouble;
191 }
192 if (state.active) {
193 return element | border;
194 }
195 if (state.focused) {
196 return element | bold;
197 }
198 return element;
199 };
200 return Menu(entries, selected, option);
201}
202
203Component VMenu6(std::vector<std::string>* entries, int* selected) {
204 auto option = MenuOption::VerticalAnimated();
205 option.underline.color_inactive = Color::Default;
206 option.underline.color_active = Color::Red;
207 option.underline.SetAnimationFunction(animation::easing::Linear);
208 return Menu(entries, selected, option);
209}
210
211Component VMenu7(std::vector<std::string>* entries, int* selected) {
212 auto option = MenuOption::Vertical();
213 option.entries_option.animated_colors.foreground.enabled = true;
214 option.entries_option.animated_colors.background.enabled = true;
215 option.entries_option.animated_colors.background.active = Color::Red;
216 option.entries_option.animated_colors.background.inactive = Color::Black;
217 option.entries_option.animated_colors.foreground.active = Color::White;
218 option.entries_option.animated_colors.foreground.inactive = Color::Red;
219 return Menu(entries, selected, option);
220}
221
222Component VMenu8(std::vector<std::string>* entries, int* selected) {
223 auto option = MenuOption::Vertical();
224 option.entries_option.animated_colors.foreground.Set(
225 Color::Red, Color::White, std::chrono::milliseconds(500));
226 return Menu(entries, selected, option);
227}
228
229Component HMenu1(std::vector<std::string>* entries, int* selected) {
230 return Menu(entries, selected, MenuOption::Horizontal());
231}
232
233Component HMenu2(std::vector<std::string>* entries, int* selected) {
234 return Menu(entries, selected, MenuOption::Toggle());
235}
236
237Component HMenu3(std::vector<std::string>* entries, int* selected) {
238 auto option = MenuOption::Toggle();
239 option.elements_infix = [] { return text(" 🮣🮠 "); };
240
241 return Menu(entries, selected, option);
242}
243
244Component HMenu4(std::vector<std::string>* entries, int* selected) {
245 return Menu(entries, selected, MenuOption::HorizontalAnimated());
246}
247
248Component HMenu5(std::vector<std::string>* entries, int* selected) {
249 auto option = MenuOption::HorizontalAnimated();
250 option.underline.SetAnimation(std::chrono::milliseconds(1500),
251 animation::easing::ElasticOut);
252 option.entries_option.transform = [](EntryState state) {
253 Element e = text(state.label) | hcenter | flex;
254 if (state.active && state.focused) {
255 e = e | bold;
256 }
257 if (!state.focused && !state.active) {
258 e = e | dim;
259 }
260 return e;
261 };
262 option.underline.color_inactive = Color::Default;
263 option.underline.color_active = Color::Red;
264 return Menu(entries, selected, option);
265}
static ScreenInteractive TerminalOutput()
static MenuOption Toggle()
帶有分隔線的水平選單標準選項。 這對於實現一個標籤頁列很有用。
static MenuOption Horizontal()
水平選單的標準選項。 這對於實現一個標籤頁列很有用。
static MenuOption VerticalAnimated()
動畫垂直選單的標準選項。 這對於實現一個可選項目列表很有用。
static MenuOption Vertical()
垂直選單的標準選項。 這對於實現一個可選項目列表很有用。
static MenuOption HorizontalAnimated()
動畫水平選單的標準選項。 這對於實現一個標籤頁列很有用。
Component Menu(MenuOption options)
文字列表。選定的元素會被聚焦。
Component Renderer(Component child, std::function< Element()>)
回傳一個新的元件,類似於 |child|,但使用 |render| 作為 Component::Render() 事件。
virtual void Render(Screen &screen)
Decorator bgcolor(Color)
使用背景顏色進行裝飾。
Element borderDouble(Element)
在元素周圍繪製雙線邊框。
Element flex(Element)
使子元素按比例擴展以佔據容器中剩餘的空間。
Definition flex.cpp:140
Element bold(Element)
使用粗體字型,用於需要更多強調的元素。
Definition bold.cpp:33
Element text(std::wstring text)
顯示一段 Unicode 文字。
Definition text.cpp:160
Element separator()
在兩個元素之間繪製垂直或水平分隔線。
Element filler()
一個元素,它會按比例擴展以佔據容器中剩餘的空間。
Definition flex.cpp:97
Element dim(Element)
使用淺色字體,適用於不那麼強調的元素。
Definition dim.cpp:33
Element border(Element)
在元素周圍繪製邊框。
Decorator color(Color)
使用前景顏色進行裝飾。
Element vbox(Elements)
一個垂直一個接一個顯示元素的容器。
Definition vbox.cpp:95
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)
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element hbox(Elements)
一個逐一水平顯示元素的容器。
Definition hbox.cpp:94
std::shared_ptr< ComponentBase > Component
來自 |ButtonOption|、|CheckboxOption|、 |RadioboxOption|、|MenuEntryOption|、|MenuOption| 的轉換參數。