FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
gallery.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// このソースコードの使用は、LICENSEファイルにあるMITライセンスによって管理されています。
3#include <functional> // for function
4#include <memory> // for shared_ptr, allocator, __shared_ptr_access
5#include <string> // for string, basic_string
6#include <vector> // for vector
7
8#include "ftxui/component/captured_mouse.hpp" // for ftxui
9#include "ftxui/component/component.hpp" // for Slider, Checkbox, Vertical, Renderer, Button, Input, Menu, Radiobox, Toggle
10#include "ftxui/component/component_base.hpp" // for ComponentBase
11#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
12#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, xflex, text, WIDTH, hbox, vbox, EQUAL, border, GREATER_THAN
13
14using namespace ftxui;
15
16// コンポーネントを左側にタイトルを付けてきれいに表示します。
17Component Wrap(std::string name, Component component) {
18 return Renderer(component, [name, component] {
19 return hbox({
20 text(name) | size(WIDTH, EQUAL, 8),
21 separator(),
22 component->Render() | xflex,
23 }) |
24 xflex;
25 });
26}
27
28int main() {
29 auto screen = ScreenInteractive::FitComponent();
30
31 // -- メニュー
32 // ----------------------------------------------------------------------
33 const std::vector<std::string> menu_entries = {
34 "Menu 1",
35 "Menu 2",
36 "Menu 3",
37 "Menu 4",
38 };
39 int menu_selected = 0;
40 auto menu = Menu(&menu_entries, &menu_selected);
41 menu = Wrap("Menu", menu);
42
43 // -- トグル------------------------------------------------------------------
44 int toggle_selected = 0;
45 std::vector<std::string> toggle_entries = {
46 "Toggle_1",
47 "Toggle_2",
48 };
49 auto toggle = Toggle(&toggle_entries, &toggle_selected);
50 toggle = Wrap("Toggle", toggle);
51
52 // -- チェックボックス ---------------------------------------------------------------
53 bool checkbox_1_selected = false;
54 bool checkbox_2_selected = false;
55 bool checkbox_3_selected = false;
56 bool checkbox_4_selected = false;
57
58 auto checkboxes = Container::Vertical({
59 Checkbox("checkbox1", &checkbox_1_selected),
60 Checkbox("checkbox2", &checkbox_2_selected),
61 Checkbox("checkbox3", &checkbox_3_selected),
62 Checkbox("checkbox4", &checkbox_4_selected),
63 });
64 checkboxes = Wrap("Checkbox", checkboxes);
65
66 // -- ラジオボックス ---------------------------------------------------------------
67 int radiobox_selected = 0;
68 std::vector<std::string> radiobox_entries = {
69 "Radiobox 1",
70 "Radiobox 2",
71 "Radiobox 3",
72 "Radiobox 4",
73 };
74 auto radiobox = Radiobox(&radiobox_entries, &radiobox_selected);
75 radiobox = Wrap("Radiobox", radiobox);
76
77 // -- 入力 ------------------------------------------------------------------
78 std::string input_label;
79 auto input = Input(&input_label, "placeholder");
80 input = Wrap("Input", input);
81
82 // -- ボタン -----------------------------------------------------------------
83 std::string button_label = "Quit";
84 std::function<void()> on_button_clicked_;
85 auto button = Button(&button_label, screen.ExitLoopClosure());
86 button = Wrap("Button", button);
87
88 // -- スライダー -----------------------------------------------------------------
89 int slider_value_1 = 12;
90 int slider_value_2 = 56;
91 int slider_value_3 = 128;
92 auto sliders = Container::Vertical({
93 Slider("R:", &slider_value_1, 0, 256, 1),
94 Slider("G:", &slider_value_2, 0, 256, 1),
95 Slider("B:", &slider_value_3, 0, 256, 1),
96 });
97 sliders = Wrap("Slider", sliders);
98
99 // 長いテキスト:
100 auto lorel_ipsum = Renderer([] {
101 return vbox({
102 text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. "),
103 text("Sed do eiusmod tempor incididunt ut labore et dolore magna "
104 "aliqua. "),
105 text("Ut enim ad minim veniam, quis nostrud exercitation ullamco "
106 "laboris nisi ut aliquip ex ea commodo consequat. "),
107 text("Duis aute irure dolor in reprehenderit in voluptate velit esse "
108 "cillum dolore eu fugiat nulla pariatur. "),
109 text("Excepteur sint occaecat cupidatat non proident, sunt in culpa "
110 "qui officia deserunt mollit anim id est laborum. "),
111
112 });
113 });
114 lorel_ipsum = Wrap("Lorel Ipsum", lorel_ipsum);
115
116 // -- レイアウト
117 // -----------------------------------------------------------------
118 auto layout = Container::Vertical({
119 menu,
120 toggle,
121 checkboxes,
122 radiobox,
123 input,
124 sliders,
125 button,
126 lorel_ipsum,
127 });
128
129 auto component = Renderer(layout, [&] {
130 return vbox({
131 menu->Render(),
132 separator(),
133 toggle->Render(),
134 separator(),
135 checkboxes->Render(),
136 separator(),
137 radiobox->Render(),
138 separator(),
139 input->Render(),
140 separator(),
141 sliders->Render(),
142 separator(),
143 button->Render(),
144 separator(),
145 lorel_ipsum->Render(),
146 }) |
147 xflex | size(WIDTH, GREATER_THAN, 40) | border;
148 });
149
150 screen.Loop(component);
151
152 return 0;
153}
Component Wrap(std::string name, Component component)
Definition gallery.cpp:17
int main()
Definition gallery.cpp:28
Element Render()
コンポーネントを描画します。 このftxui::ComponentBaseを表すftxui::Screen上に描画されるftxui::Elementを構築します。レンダリングを変更するにはOnRende...
static ScreenInteractive FitComponent()
描画されるコンポーネントの幅と高さに一致するScreenInteractiveを作成します。
Component Menu(MenuOption options)
テキストのリスト。フォーカスされた要素が選択されます。
Component Toggle(ConstStringListRef entries, int *selected)
要素の水平リスト。ユーザーはこれらを操作できます。
Component Radiobox(RadioboxOption options)
1つだけ選択できる要素のリスト。
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked. (ja: ボタンを描画します。クリックされたときに機能を実行します。)
Component Renderer(Component child, std::function< Element()>)
|child|に似ていますが、|render|をComponentRender()イベントとして使用する新しいコンポーネントを返します。
Component Checkbox(CheckboxOption options)
チェック可能な要素を描画します。
virtual void Render(Screen &screen)
要素をftxui::Screenに表示します。
Definition node.cpp:59
Element xflex(Element)
必要に応じてX軸上で拡大・縮小します。
Definition flex.cpp:126
Element text(std::wstring text)
ユニコードテキストを表示します。
Definition text.cpp:160
Element vbox(Elements)
要素を縦に一つずつ表示するコンテナ。
Definition vbox.cpp:94
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
Element hbox(Elements)
要素を水平方向に1つずつ表示するコンテナ。
Definition hbox.cpp:93
Element border(Element)
Component Input(InputOption options={})
Component Slider(SliderOption< T > options)
どの方向にも対応するスライダー。
Element separator()
@ GREATER_THAN
Definition elements.hpp:157
std::shared_ptr< ComponentBase > Component
return size
Definition string.cpp:1516