FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
composition.cpp
Go to the documentation of this file.
1// Copyright 2021 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 <memory> // for allocator, shared_ptr, __shared_ptr_access
5#include <string> // for operator+, to_string
6
7#include "ftxui/component/app.hpp" // for App
8#include "ftxui/component/captured_mouse.hpp" // for ftxui
9#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
10#include "ftxui/component/component_base.hpp" // for ComponentBase
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 // Renderer 使用新的渲染函數裝飾其子元件。子元件對事件的反應方式保持不變。
32 auto leftpane = Renderer(left_buttons, [&] {
33 return vbox({
34 text("This is the left control"),
35 separator(),
36 text("Left button count: " + std::to_string(left_count)),
37 left_buttons->Render(),
38 }) |
39 border;
40 });
41
42 auto rightpane = Renderer(right_buttons, [&] {
43 return vbox({
44 text("This is the right control"),
45 separator(),
46 text("Right button count: " + std::to_string(right_count)),
47 right_buttons->Render(),
48 }) |
49 border;
50 });
51
52 // Container 將元件組合在一起。要渲染一個 Container::Horizontal,
53 // 它會將其子元件並排渲染。它維持了它們的互動性,並
54 // 提供使用方向鍵在它們之間導覽的邏輯。
55 auto composition = Container::Horizontal({leftpane, rightpane});
56
57 auto screen = App::FitComponent();
58 screen.Loop(composition);
59 return 0;
60}
61
62// 感謝 Chris Morgan 提供此範例!
int main()
Component Button(ConstStringRef label, std::function< void()> on_click, ButtonOption options=ButtonOption::Simple())
繪製一個按鈕。點擊時執行一個函式。
Component Renderer(Component child, std::function< Element()>)
回傳一個新的 Component,類似 |child|,但使用 |render| 作為 Component::Render() 事件。
Element text(std::string_view text)
顯示一段 UTF8 編碼的 Unicode 文字。
Element separator()
在兩個元素之間繪製垂直或水平分隔線。
Element vbox(Elements children)
一個垂直一個接一個顯示元素的容器。
Definition vbox.cpp:96
FTXUI ftxui:: 命名空間
Definition animation.hpp:11