FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
composition.cpp
浏览该文件的文档.
1// 版权所有 2021 Arthur Sonzogni. 保留所有权利。
2// 本源代码的使用受 MIT 许可证的约束,该许可证可在以下位置找到:
3// LICENSE 文件。
4#include <memory> // for allocator, shared_ptr, __shared_ptr_access
5#include <string> // for operator+, to_string
6
7#include "ftxui/component/captured_mouse.hpp" // for ftxui
8#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
9#include "ftxui/component/component_base.hpp" // for ComponentBase
10#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
11#include "ftxui/dom/elements.hpp" // for text, separator, Element, operator|, vbox, border
12
13using namespace ftxui;
14
15// 一个示例,展示如何将多个组件组合成一个并保持它们的
16// 交互性。
17int main() {
18 auto left_count = 0;
19 auto right_count = 0;
20
21 auto left_buttons = Container::Horizontal({
22 Button("Decrease", [&] { left_count--; }),
23 Button("Increase", [&] { left_count++; }),
24 });
25
26 auto right_buttons = Container::Horizontal({
27 Button("Decrease", [&] { right_count--; }),
28 Button("Increase", [&] { right_count++; }),
29 });
30
31 // 渲染器用新的渲染函数装饰其子组件。子组件对事件的反应方式
32 // 保持不变。
33 auto leftpane = Renderer(left_buttons, [&] {
34 return vbox({
35 text("This is the left control"),
36 separator(),
37 text("Left button count: " + std::to_string(left_count)),
38 left_buttons->Render(),
39 }) |
40 border;
41 });
42
43 auto rightpane = Renderer(right_buttons, [&] {
44 return vbox({
45 text("This is the right control"),
46 separator(),
47 text("Right button count: " + std::to_string(right_count)),
48 right_buttons->Render(),
49 }) |
50 border;
51 });
52
53 // 容器将组件组合在一起。要渲染一个 Container::Horizontal,
54 // 它并排渲染其子组件。它保持它们的交互性并
55 // 提供使用箭头键从一个导航到另一个的逻辑。
56 auto composition = Container::Horizontal({leftpane, rightpane});
57
58 auto screen = ScreenInteractive::FitComponent();
59 screen.Loop(composition);
60 return 0;
61}
62
63// 感谢 Chris Morgan 提供此示例!
int main()
static ScreenInteractive FitComponent()
创建一个 ScreenInteractive,其宽度和高度与正在绘制的组件匹配。
Component Button(ButtonOption options)
绘制一个按钮。点击时执行一个函数。
Component Renderer(Component child, std::function< Element()>)
返回一个新组件,类似于 |child|,但使用 |render| 作为 Component::Render() 事件。
virtual void Render(Screen &screen)
在 ftxui::Screen 上显示元素。
Element text(std::wstring text)
显示一段Unicode文本。
Element separator()
在两个其他元素之间绘制垂直或水平分隔线。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase