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// このソースコードの使用は、LICENSEファイルにあるMITライセンスに準拠しています。
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()
ターミナル出力の幅に一致し、描画されるコンポーネントの高さに一致するScreenInteractiveを作成します。
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|をComponentRender()イベントとして使用する新しいコンポーネントを返します。
virtual void Render(Screen &screen)
要素をftxui::Screenに表示します。
Definition node.cpp:59
Decorator bgcolor(Color)
背景色を使用して装飾します。
Element flex(Element)
子要素をコンテナに残されたスペースに比例して拡大させます。
Definition flex.cpp:120
Element bold(Element)
より強調したい要素に、太字フォントを使用します。
Definition bold.cpp:33
Element text(std::wstring text)
ユニコードテキストを表示します。
Definition text.cpp:160
Element filler()
コンテナに残されたスペースに比例して拡大する要素。
Definition flex.cpp:96
Element dim(Element)
強調を抑えたい要素に、明るいフォントを使用します。
Definition dim.cpp:33
Decorator color(Color)
前景色を使用して装飾します。
Element vbox(Elements)
要素を縦に一つずつ表示するコンテナ。
Definition vbox.cpp:94
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:9
std::shared_ptr< Node > Element
Definition elements.hpp:21
Element hbox(Elements)
要素を水平方向に1つずつ表示するコンテナ。
Definition hbox.cpp:93
Element border(Element)
Element borderDouble(Element)
Element separator()
std::shared_ptr< ComponentBase > Component
|ButtonOption|、|CheckboxOption|、|RadioboxOption|、|MenuEntryOption|、|MenuOption|からの変換の引数。