FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
modal_dialog.cpp
Go to the documentation of this file.
1// Copyright 2022 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <ftxui/component/component_options.hpp> // for ButtonOption
5#include <ftxui/component/mouse.hpp> // for ftxui
6#include <functional> // for function
7#include <memory> // for allocator, shared_ptr
8
9#include "ftxui/component/app.hpp" // for App, Component
10#include "ftxui/component/component.hpp" // for Button, operator|=, Renderer, Vertical, Modal
11#include "ftxui/dom/elements.hpp" // for operator|, separator, text, size, Element, vbox, border, GREATER_THAN, WIDTH, center, HEIGHT
12
13using namespace ftxui;
14
15auto button_style = ButtonOption::Animated();
16
17// 主要元件的定義。細節並不重要。
18Component MainComponent(std::function<void()> show_modal,
19 std::function<void()> exit) {
20 auto component = Container::Vertical({
21 Button("Show modal", show_modal, button_style),
22 Button("Quit", exit, button_style),
23 });
24 // 潤飾這兩個按鈕的渲染方式:
25 component |= Renderer([&](Element inner) {
26 return vbox({
27 text("Main component"),
28 separator(),
29 inner,
30 }) //
31 | size(WIDTH, GREATER_THAN, 15) //
32 | size(HEIGHT, GREATER_THAN, 15) //
33 | border //
34 | center; //
35 });
36 return component;
37}
38
39// 對話框元件的定義。細節並不重要。
40Component ModalComponent(std::function<void()> do_nothing,
41 std::function<void()> hide_modal) {
42 auto component = Container::Vertical({
43 Button("Do nothing", do_nothing, button_style),
44 Button("Quit modal", hide_modal, button_style),
45 });
46 // 潤飾這兩個按鈕的渲染方式:
47 component |= Renderer([&](Element inner) {
48 return vbox({
49 text("Modal component "),
50 separator(),
51 inner,
52 }) //
53 | size(WIDTH, GREATER_THAN, 30) //
54 | border; //
55 });
56 return component;
57}
58
59int main(int argc, const char* argv[]) {
60 auto screen = App::TerminalOutput();
61
62 // 應用程式的狀態:
63 bool modal_shown = false;
64
65 // 一些會修改狀態的動作:
66 auto show_modal = [&] { modal_shown = true; };
67 auto hide_modal = [&] { modal_shown = false; };
68 auto exit = screen.ExitLoopClosure();
69 auto do_nothing = [&] {};
70
71 // 實例化主要元件與對話框元件:
72 auto main_component = MainComponent(show_modal, exit);
73 auto modal_component = ModalComponent(do_nothing, hide_modal);
74
75 // 使用 `Modal` 函數將主元件及其模態視窗一起使用。
76 // `modal_shown` 布林值控制模態是否顯示。
77 main_component |= Modal(modal_component, &modal_shown);
78
79 screen.Loop(main_component);
80 return 0;
81}
Component Button(ConstStringRef label, std::function< void()> on_click, ButtonOption options=ButtonOption::Simple())
繪製一個按鈕。點擊時執行一個函式。
FTXUI_EXPORT(COMPONENT) ComponentDecorator Maybe(std Component Modal(Component main, Component modal, const bool *show_modal)
Definition modal.cpp:18
Component Renderer(Component child, std::function< Element()>)
回傳一個新的 Component,類似 |child|,但使用 |render| 作為 Component::Render() 事件。
Element text(std::string_view text)
顯示一段 UTF8 編碼的 Unicode 文字。
Decorator size(WidthOrHeight direction, Constraint constraint, int value)
對元素的大小套用限制。
Element center(Element child)
水平與垂直置中一個元素。
Element separator()
在兩個元素之間繪製垂直或水平分隔線。
Element border(Element child)
在元素周圍繪製邊框。
Element vbox(Elements children)
一個垂直一個接一個顯示元素的容器。
Definition vbox.cpp:96
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)
FTXUI ftxui:: 命名空間
Definition animation.hpp:11
@ GREATER_THAN
Definition elements.hpp:191
std::shared_ptr< Node > Element
Definition elements.hpp:25
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23