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// 本原始碼的使用受 MIT 授權條款約束,該條款可在 LICENSE 檔案中找到。
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(),
23 }) |
24 xflex;
25 });
26}
27
28int main() {
29 auto screen = ScreenInteractive::FitComponent();
30
31 // -- 選單
32 // ---------------------------------------------------------------------- const std::vector<std::string> menu_entries = {
33 "Menu 1",
34 "Menu 2",
35 "Menu 3",
36 "Menu 4",
37 };
39 auto menu = Menu(&menu_entries, &menu_selected);
40 menu = Wrap("Menu", menu);
41
42 // -- 開關------------------------------------------------------------------
44 std::vector<std::string> toggle_entries = {
45 "Toggle_1",
46 "Toggle_2",
47 };
49 toggle = Wrap("Toggle", toggle);
50
51 // -- 核取方塊 ---------------------------------------------------------------
52 bool checkbox_1_selected = false;
53 bool checkbox_2_selected = false;
54 bool checkbox_3_selected = false;
55 bool checkbox_4_selected = false;
56
57 auto checkboxes = Container::Vertical({
58 Checkbox("checkbox1", &checkbox_1_selected),
59 Checkbox("checkbox2", &checkbox_2_selected),
60 Checkbox("checkbox3", &checkbox_3_selected),
61 Checkbox("checkbox4", &checkbox_4_selected),
62 });
63 checkboxes = Wrap("Checkbox", checkboxes);
64
65 // -- 單選框 ---------------------------------------------------------------
67 std::vector<std::string> radiobox_entries = {
68 "Radiobox 1",
69 "Radiobox 2",
70 "Radiobox 3",
71 "Radiobox 4",
72 };
74 radiobox = Wrap("Radiobox", radiobox);
75
76 // -- 輸入 ------------------------------------------------------------------
77 std::string input_label;
78 auto input = Input(&input_label, "placeholder");
79 input = Wrap("Input", input);
80
81 // -- 按鈕 -----------------------------------------------------------------
82 std::string button_label = "Quit";
83 std::function<void()> on_button_clicked_;
84 auto button = Button(&button_label, screen.ExitLoopClosure());
85 button = Wrap("Button", button);
86
87 // -- 滑桿 -----------------------------------------------------------------
90 int slider_value_3 = 128;
91 auto sliders = Container::Vertical({
92 Slider("R:", &slider_value_1, 0, 256, 1),
93 Slider("G:", &slider_value_2, 0, 256, 1),
94 Slider("B:", &slider_value_3, 0, 256, 1),
95 });
96 sliders = Wrap("Slider", sliders);
97
98 // 大段文字:
100 return vbox({
101 text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. "),
102 text("Sed do eiusmod tempor incididunt ut labore et dolore magna "
103 "aliqua. "),
104 text("Ut enim ad minim veniam, quis nostrud exercitation ullamco "
105 "laboris nisi ut aliquip ex ea commodo consequat. "),
106 text("Duis aute irure dolor in reprehenderit in voluptate velit esse "
107 "cillum dolore eu fugiat nulla pariatur. "),
108 text("Excepteur sint occaecat cupidatat non proident, sunt in culpa "
109 "qui officia deserunt mollit anim id est laborum. "),
110
111 });
112 });
113 lorel_ipsum = Wrap("Lorel Ipsum", lorel_ipsum);
114
115 // -- 佈局
116 // ----------------------------------------------------------------- auto layout = Container::Vertical({
117 menu,
118 toggle,
120 radiobox,
121 input,
122 sliders,
123 button,
125 });
126
127 auto component = Renderer(layout, [&] {
128 return vbox({
129 menu->Render(),
130 separator(),
131 toggle->Render(),
132 separator(),
134 separator(),
135 radiobox->Render(),
136 separator(),
137 input->Render(),
138 separator(),
139 sliders->Render(),
140 separator(),
141 button->Render(),
142 separator(),
144 }) |
145 xflex | size(WIDTH, GREATER_THAN, 40) | border;
146 });
147
148 screen.Loop(component);
149
150 return 0;
151}
Component Wrap(std::string name, Component component)
Definition gallery.cpp:17
bool checkbox_4_selected
Definition gallery.cpp:55
bool checkbox_1_selected
Definition gallery.cpp:52
int menu_selected
Definition gallery.cpp:38
auto button
Definition gallery.cpp:84
auto checkboxes
Definition gallery.cpp:57
int slider_value_3
Definition gallery.cpp:90
auto radiobox
Definition gallery.cpp:73
auto toggle
Definition gallery.cpp:48
std::string button_label
Definition gallery.cpp:82
bool checkbox_3_selected
Definition gallery.cpp:54
std::string input_label
Definition gallery.cpp:77
auto component
Definition gallery.cpp:127
int toggle_selected
Definition gallery.cpp:43
std::vector< std::string > toggle_entries
Definition gallery.cpp:44
bool checkbox_2_selected
Definition gallery.cpp:53
auto input
Definition gallery.cpp:78
auto menu
Definition gallery.cpp:39
int slider_value_2
Definition gallery.cpp:89
int radiobox_selected
Definition gallery.cpp:66
auto lorel_ipsum
Definition gallery.cpp:99
std::vector< std::string > radiobox_entries
Definition gallery.cpp:67
std::function< void()> on_button_clicked_
Definition gallery.cpp:83
int main()
Definition gallery.cpp:28
auto sliders
Definition gallery.cpp:91
int slider_value_1
Definition gallery.cpp:88
static ScreenInteractive FitComponent()
Component Checkbox(CheckboxOption options)
Component Menu(MenuOption options)
文字列表。選定的元素會被聚焦。
Component Toggle(ConstStringListRef entries, int *selected)
元素的水平列表。使用者可以在其中導航。
Component Radiobox(RadioboxOption options)
元素清單,只能選擇一個。
Component Button(ButtonOption options)
繪製一個按鈕。點擊時執行一個函數。
Component Renderer(Component child, std::function< Element()>)
回傳一個新的元件,類似於 |child|,但使用 |render| 作為 Component::Render() 事件。
Component Input(InputOption options={})
用於編輯文字的輸入框。
virtual void Render(Screen &screen)
Element xflex(Element)
在 X 軸上盡可能擴展/在需要時最小化。
Definition flex.cpp:146
Decorator size(WidthOrHeight, Constraint, int value)
限制元素的大小。
Element text(std::wstring text)
顯示一段 Unicode 文字。
Definition text.cpp:160
Element separator()
在兩個元素之間繪製垂直或水平分隔線。
Element border(Element)
在元素周圍繪製邊框。
Element vbox(Elements)
一個垂直一個接一個顯示元素的容器。
Definition vbox.cpp:95
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
Element hbox(Elements)
一個逐一水平顯示元素的容器。
Definition hbox.cpp:94
Component Slider(SliderOption< T > options)
@ GREATER_THAN
Definition elements.hpp:159
std::shared_ptr< ComponentBase > Component