FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
modal_dialog.cpp
浏览该文件的文档.
1// 版权所有 2022 Arthur Sonzogni。保留所有权利。
2// 本源代码受 MIT 许可协议的约束,该协议可在 LICENSE 文件中找到。
3#include <ftxui/component/component_options.hpp> // for ButtonOption
4#include <ftxui/component/mouse.hpp> // for ftxui
5#include <functional> // for function
6#include <memory> // for allocator, shared_ptr
7
8#include "ftxui/component/component.hpp" // for Button, operator|=, Renderer, Vertical, Modal
9#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive, Component
10#include "ftxui/dom/elements.hpp" // for operator|, separator, text, size, Element, vbox, border, GREATER_THAN, WIDTH, center, HEIGHT
11
12using namespace ftxui;
13
15
16// 主组件的定义。细节不重要。
17Component MainComponent(std::function<void()> show_modal,
18 std::function<void()> exit) {
19 auto component = Container::Vertical({
20 Button("Show modal", show_modal, button_style),
21 Button("Quit", exit, button_style),
22 });
23 // 优化两个按钮的渲染方式:
24 component |= Renderer([&](Element inner) {
25 return vbox({
26 text("Main component"),
27 separator(),
28 inner,
29 }) //
30 | size(WIDTH, GREATER_THAN, 15) //
31 | size(HEIGHT, GREATER_THAN, 15) //
32 | border //
33 | center; //
34 });
35 return component;
36}
37
38// 模态组件的定义。细节不重要。
39Component ModalComponent(std::function<void()> do_nothing,
40 std::function<void()> hide_modal) {
41 auto component = Container::Vertical({
42 Button("Do nothing", do_nothing, button_style),
43 Button("Quit modal", hide_modal, button_style),
44 });
45 // Polish how the two buttons are rendered:
46 component |= Renderer([&](Element inner) {
47 return vbox({
48 text("Modal component "),
49 separator(),
50 inner,
51 }) //
52 | size(WIDTH, GREATER_THAN, 30) //
53 | border; //
54 });
55 return component;
56}
57
58int main(int argc, const char* argv[]) {
60
61 // 应用程序状态:
62 bool modal_shown = false;
63
64 // 修改状态的一些操作:
65 auto show_modal = [&] { modal_shown = true; };
66 auto hide_modal = [&] { modal_shown = false; };
67 auto exit = screen.ExitLoopClosure();
68 auto do_nothing = [&] {};
69
70 // 实例化主组件和模态组件:
71 auto main_component = MainComponent(show_modal, exit);
72 auto modal_component = ModalComponent(do_nothing, hide_modal);
73
74 // 使用 `Modal` 函数将主组件和模态窗口一起使用。
75 // |modal_shown| 布尔值控制模态窗口是否显示。
76 main_component |= Modal(modal_component, &modal_shown);
77
78 screen.Loop(main_component);
79 return 0;
80}
static ButtonOption Animated()
创建一个使用动画颜色的ButtonOption。
static ScreenInteractive TerminalOutput()
Component Button(ButtonOption options)
绘制一个按钮。点击时执行一个函数。
Component Modal(Component main, Component modal, const bool *show_modal)
Component Renderer(Component child, std::function< Element()>)
返回一个新组件,类似于 |child|,但使用 |render| 作为 Component::Render() 事件。
Decorator size(WidthOrHeight, Constraint, int value)
对元素大小应用约束。
Element center(Element)
水平并垂直居中一个元素。
Element text(std::wstring text)
显示一段Unicode文本。
Element separator()
在两个其他元素之间绘制垂直或水平分隔线。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
auto button_style
Component ModalComponent(std::function< void()> do_nothing, std::function< void()> hide_modal)
Component MainComponent(std::function< void()> show_modal, std::function< void()> exit)
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< Node > Element
Element border(Element)
std::shared_ptr< ComponentBase > Component