FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
composition.cpp
Go to the documentation of this file.
1// 版權所有 2021 Arthur Sonzogni. 保留所有權利。
2// 本原始碼的使用受 MIT 授權條款約束,該條款可在 LICENSE 檔案中找到。
3#include <memory> // for allocator, shared_ptr, __shared_ptr_access
4#include <string> // for operator+, to_string
5
6#include "ftxui/component/captured_mouse.hpp" // for ftxui
7#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
8#include "ftxui/component/component_base.hpp" // for ComponentBase
9#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
10#include "ftxui/dom/elements.hpp" // for text, separator, Element, operator|, vbox, border
11
12using namespace ftxui;
13
14// 本範例展示如何將多個元件組合成一個,並保持它們的互動性。
15int main() {
16 auto left_count = 0;
17 auto right_count = 0;
18
19 auto left_buttons = Container::Horizontal({
20 Button("Decrease", [&] { left_count--; }),
21 Button("Increase", [&] { left_count++; }),
22 });
23
24 auto right_buttons = Container::Horizontal({
25 Button("Decrease", [&] { right_count--; }),
26 Button("Increase", [&] { right_count++; }),
27 });
28
29 // Renderer 使用新的渲染函數裝飾其子元件。子元件對事件的反應方式保持不變。
30 auto leftpane = Renderer(left_buttons, [&] {
31 return vbox({
32 text("This is the left control"),
33 separator(),
34 text("Left button count: " + std::to_string(left_count)),
35 left_buttons->Render(),
36 }) |
37 border;
38 });
39
40 auto rightpane = Renderer(right_buttons, [&] {
41 return vbox({
42 text("This is the right control"),
43 separator(),
44 text("Right button count: " + std::to_string(right_count)),
45 right_buttons->Render(),
46 }) |
47 border;
48 });
49
50 // Container 將元件分組在一起。要渲染 Container::Horizontal,
51 // 它會將其子元件並排渲染。它保持它們的互動性,並提供使用箭頭鍵在元件之間導航的邏輯。
52 auto composition = Container::Horizontal({leftpane, rightpane});
53
54 auto screen = ScreenInteractive::FitComponent();
55 screen.Loop(composition);
56 return 0;
57}
58
59// 感謝 Chris Morgan 提供此範例!
int main()
static ScreenInteractive FitComponent()
Component Button(ButtonOption options)
繪製一個按鈕。點擊時執行一個函數。
Component Renderer(Component child, std::function< Element()>)
回傳一個新的元件,類似於 |child|,但使用 |render| 作為 Component::Render() 事件。
virtual void Render(Screen &screen)
Element text(std::wstring text)
顯示一段 Unicode 文字。
Definition text.cpp:160
Element separator()
在兩個元素之間繪製垂直或水平分隔線。
Element vbox(Elements)
一個垂直一個接一個顯示元素的容器。
Definition vbox.cpp:95
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10