FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
gallery.cpp
浏览该文件的文档.
1// 版权所有 2020 Arthur Sonzogni。保留所有权利。
2// 本源代码的使用受 MIT 许可证的约束,该许可证可在
3// LICENSE 文件中找到。
4#include <functional> // for function
5#include <memory> // for shared_ptr, allocator, __shared_ptr_access
6#include <string> // for string, basic_string
7#include <vector> // for vector
8
9#include "ftxui/component/captured_mouse.hpp" // for ftxui
10#include "ftxui/component/component.hpp" // for Slider, Checkbox, Vertical, Renderer, Button, Input, Menu, Radiobox, Toggle
11#include "ftxui/component/component_base.hpp" // for ComponentBase
12#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
13#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, xflex, text, WIDTH, hbox, vbox, EQUAL, border, GREATER_THAN
14
15using namespace ftxui;
16
17// 在左侧带标题的情况下漂亮地显示组件。
18Component Wrap(std::string name, Component component) {
19 return Renderer(component, [name, component] {
20 return hbox({
21 text(name) | size(WIDTH, EQUAL, 8),
22 separator(),
23 component->Render() | xflex,
24 }) |
25 xflex;
26 });
27}
28
29int main() {
30 auto screen = ScreenInteractive::FitComponent();
31
32 // -- 菜单
33 // ----------------------------------------------------------------------
34 const std::vector<std::string> menu_entries = {
35 "Menu 1",
36 "Menu 2",
37 "Menu 3",
38 "Menu 4",
39 };
40 int menu_selected = 0;
41 auto menu = Menu(&menu_entries, &menu_selected);
42 menu = Wrap("Menu", menu);
43
44 // -- 切换------------------------------------------------------------------
45 int toggle_selected = 0;
46 std::vector<std::string> toggle_entries = {
47 "Toggle_1",
48 "Toggle_2",
49 };
50 auto toggle = Toggle(&toggle_entries, &toggle_selected);
51 toggle = Wrap("Toggle", toggle);
52
53 // -- 复选框 ---------------------------------------------------------------
54 bool checkbox_1_selected = false;
55 bool checkbox_2_selected = false;
56 bool checkbox_3_selected = false;
57 bool checkbox_4_selected = false;
58
59 auto checkboxes = Container::Vertical({
60 Checkbox("checkbox1", &checkbox_1_selected),
61 Checkbox("checkbox2", &checkbox_2_selected),
62 Checkbox("checkbox3", &checkbox_3_selected),
63 Checkbox("checkbox4", &checkbox_4_selected),
64 });
65 checkboxes = Wrap("Checkbox", checkboxes);
66
67 // -- 单选框 ---------------------------------------------------------------
68 int radiobox_selected = 0;
69 std::vector<std::string> radiobox_entries = {
70 "Radiobox 1",
71 "Radiobox 2",
72 "Radiobox 3",
73 "Radiobox 4",
74 };
75 auto radiobox = Radiobox(&radiobox_entries, &radiobox_selected);
76 radiobox = Wrap("Radiobox", radiobox);
77
78 // -- 输入 ------------------------------------------------------------------
79 std::string input_label;
80 auto input = Input(&input_label, "placeholder");
81 input = Wrap("Input", input);
82
83 // -- 按钮 -----------------------------------------------------------------
84 std::string button_label = "Quit";
85 std::function<void()> on_button_clicked_;
86 auto button = Button(&button_label, screen.ExitLoopClosure());
87 button = Wrap("Button", button);
88
89 // -- 滑块 -----------------------------------------------------------------
90 int slider_value_1 = 12;
91 int slider_value_2 = 56;
92 int slider_value_3 = 128;
93 auto sliders = Container::Vertical({
94 Slider("R:", &slider_value_1, 0, 256, 1),
95 Slider("G:", &slider_value_2, 0, 256, 1),
96 Slider("B:", &slider_value_3, 0, 256, 1),
97 });
98 sliders = Wrap("Slider", sliders);
99
100 // 大文本:
101 auto lorel_ipsum = Renderer([] {
102 return vbox({
103 text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. "),
104 text("Sed do eiusmod tempor incididunt ut labore et dolore magna "
105 "aliqua. "),
106 text("Ut enim ad minim veniam, quis nostrud exercitation ullamco "
107 "laboris nisi ut aliquip ex ea commodo consequat. "),
108 text("Duis aute irure dolor in reprehenderit in voluptate velit esse "
109 "cillum dolore eu fugiat nulla pariatur. "),
110 text("Excepteur sint occaecat cupidatat non proident, sunt in culpa "
111 "qui officia deserunt mollit anim id est laborum. "),
112
113 });
114 });
115 lorel_ipsum = Wrap("Lorel Ipsum", lorel_ipsum);
116
117 // -- 布局
118 // -----------------------------------------------------------------
119 auto layout = Container::Vertical({
120 menu,
121 toggle,
122 checkboxes,
123 radiobox,
124 input,
125 sliders,
126 button,
127 lorel_ipsum,
128 });
129
130 auto component = Renderer(layout, [&] {
131 return vbox({
132 menu->Render(),
133 separator(),
134 toggle->Render(),
135 separator(),
136 checkboxes->Render(),
137 separator(),
138 radiobox->Render(),
139 separator(),
140 input->Render(),
141 separator(),
142 sliders->Render(),
143 separator(),
144 button->Render(),
145 separator(),
146 lorel_ipsum->Render(),
147 }) |
148 xflex | size(WIDTH, GREATER_THAN, 40) | border;
149 });
150
151 screen.Loop(component);
152
153 return 0;
154}
Component Wrap(std::string name, Component component)
int main()
Element Render()
绘制组件。 构建一个 ftxui::Element,用于在表示此 ftxui::ComponentBase 的 ftxui::Screen 上绘制。 请覆盖 OnRender() 以修改渲染。
static ScreenInteractive FitComponent()
创建一个 ScreenInteractive,其宽度和高度与正在绘制的组件匹配。
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={})
用于编辑文本的输入框。
Component Checkbox(CheckboxOption options)
绘制可勾选元素。
virtual void Render(Screen &screen)
在 ftxui::Screen 上显示元素。
Element xflex(Element)
在 X 轴上尽可能地扩展/收缩。
Decorator size(WidthOrHeight, Constraint, int value)
对元素大小应用约束。
Element text(std::wstring text)
显示一段Unicode文本。
Element separator()
在两个其他元素之间绘制垂直或水平分隔线。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
Element hbox(Elements)
一个按水平顺序逐一显示元素的容器。
Element border(Element)
Component Slider(SliderOption< T > options)
任意方向的滑块。
std::shared_ptr< ComponentBase > Component