FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
app.hpp
Go to the documentation of this file.
1// Copyright 2020 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#ifndef FTXUI_COMPONENT_APP_HPP
5#define FTXUI_COMPONENT_APP_HPP
6
7#include <atomic> // for atomic
8#include <chrono> // for steady_clock, time_point
9#include <functional> // for function
10#include <memory> // for shared_ptr, unique_ptr
11#include <string> // for string, basic_string, allocator
12#include <vector> // for vector
13
14#include "ftxui/component/animation.hpp" // for TimePoint
16#include "ftxui/component/task.hpp" // for Task, Closure
17#include "ftxui/screen/screen.hpp" // for Screen
18#include "ftxui/screen/terminal.hpp" // for Dimensions
19#include "ftxui/util/export.hpp"
20
21namespace ftxui {
22class ComponentBase;
23using Component = std::shared_ptr<ComponentBase>;
24struct Event;
25class Selection;
26class TaskRunner;
27
28/// @brief App 是一個管理應用程式生命週期的類別。
29/// 它負責初始化終端機、執行主迴圈,
30/// 並在結束時清理。
31///
32/// @note 這個類別先前名為 ScreenInteractive。
33///
34/// @ingroup component
35class FTXUI_EXPORT(COMPONENT) App : public Screen {
36 public:
37 // 建構子:
38
39 /// @brief 建立一個固定大小的 App。
40 /// @param dimx App 的寬度。
41 /// @param dimy App 的高度。
42 static App FixedSize(int dimx, int dimy);
43
44 /// @brief 建立一個佔滿整個終端機大小的 App。這會使用
45 /// 替代畫面緩衝區(alternate screen buffer),以避免弄亂終端機的內容。
46 /// @note 這與 `App::FullscreenAlternateScreen()` 相同
47 static App Fullscreen();
48
49 /// @brief 建立一個佔滿整個終端機大小的 App。使用的是主要畫面
50 /// 緩衝區。這代表如果終端機被調整大小,先前的
51 /// 內容可能會與終端機內容混雜在一起。
52 static App FullscreenPrimaryScreen();
53
54 /// @brief 建立一個佔滿整個終端機大小的 App。這會使用
55 /// 替代畫面緩衝區,以避免破壞終端機的內容。
56 static App FullscreenAlternateScreen();
57
58 /// @brief 建立一個寬度和高度與正在繪製的元件相符的 App。
59 static App FitComponent();
60
61 /// @brief 建立一個寬度符合終端機輸出寬度,
62 /// 且高度符合正在繪製的元件的 App。
63 static App TerminalOutput();
64
65 // 解構函式。
66 ~App() override;
67
68 App(App&&) noexcept;
69 App& operator=(App&&) noexcept;
70 App(const App&) = delete;
71 App& operator=(const App&) = delete;
72
73 // 選項。必須在 Loop() 之前呼叫。
74
75 /// @brief 設定是否追蹤滑鼠並回報事件。
76 /// @param enable 是否啟用滑鼠事件追蹤。
77 /// @note 滑鼠追蹤預設為啟用。
78 /// @note 滑鼠追蹤僅在支援它的終端機上受支援。
79 /// @note 必須在呼叫 `App::Loop` 之前呼叫此函式。
80 void TrackMouse(bool enable = true);
81
82 /// @brief 啟用或停用自動管道輸入處理。
83 /// 啟用時,FTXUI 會偵測管道輸入,並將標準輸入從
84 /// /dev/tty 重新導向以取得鍵盤輸入,讓應用程式在讀取管道資料的
85 /// 同時仍能接收互動式鍵盤事件。
86 /// @param enable 是否啟用管道輸入處理。預設為 true。
87 /// @note 必須在 Loop() 之前呼叫。
88 /// @note 此功能預設為啟用。
89 /// @note 此功能僅在 POSIX 系統(Linux/macOS)上可用。
90 void HandlePipedInput(bool enable = true);
91
92 /// @brief 回傳目前作用中的 app,若無則回傳 nullptr。
93 static App* Active();
94
95 // 開始/停止主迴圈。
96
97 /// @brief 執行主迴圈。
98 /// @param component 要繪製的元件。
99 void Loop(Component component);
100
101 /// @brief 結束主迴圈。
102 void Exit();
103
104 /// @brief 回傳一個用來結束主迴圈的函式。
105 Closure ExitLoopClosure();
106
107 /// @brief 裝飾一個函式。輸出的函式執行方式會與輸入的函式類似,
108 /// 但目前作用中的 app 終端機掛勾會暫時被卸除。
109 Closure WithRestoredIO(Closure fn);
110
111 /// @brief FTXUI 實作了 Ctrl-C 和 Ctrl-Z 的處理器。預設情況下,
112 /// 即使元件捕捉到該事件,這些處理器仍會被執行。這避免使用者
113 /// 必須處理每個事件才能跳脫應用程式。然而,在某些情況下,
114 /// 應用程式可能想要自行處理這些事件。在這種情況下,
115 /// 應用程式可以透過呼叫下列函式並傳入 force=true,
116 /// 強制 FTXUI 不處理這些事件。
117 void ForceHandleCtrlC(bool force = true);
118
119 /// @brief 強制 FTXUI 處理或不處理 Ctrl-Z,即使元件
120 /// 捕捉到了 Event::CtrlZ。
121 void ForceHandleCtrlZ(bool force = true);
122
123 // 將任務發布給迴圈執行。
124
125 /// @brief 新增一個任務到主迴圈。
126 /// 它會在稍後、所有其他已排程任務之後執行。
127 void Post(Task task);
128
129 /// @brief 新增一個事件到主迴圈。
130 /// 它會在稍後、所有其他已排程事件之後執行。
131 void PostEvent(Event event);
132
133 /// @brief 新增一個任務到主迴圈。
134 /// 它會在稍後、所有其他已排程任務之後執行。
135 static void PostEventOrExecute(Closure closure);
136
137 /// @brief 新增一個任務,在所有動畫完成之前,
138 /// 多繪製畫面一次。
139 void RequestAnimationFrame();
140
141 // 選取 API:
142
143 /// @brief 嘗試取得能夠捕捉滑鼠的唯一鎖。
144 /// @return 若滑鼠尚未被捕捉,回傳一個唯一鎖,否則回傳
145 /// null。
146 CapturedMouse CaptureMouse();
147
148 /// @brief 回傳目前選取內容。
149 std::string GetSelection();
150
151 /// @brief 設定一個當選取內容改變時會被呼叫的回呼函式。
152 void SelectionChange(std::function<void()> callback);
153
154 // 終端機資訊。
155
156 /// @brief 回傳終端機名稱。
157 const std::string& TerminalName() const;
158
159 /// @brief 回傳終端機版本。
160 int TerminalVersion() const;
161
162 /// @brief 回傳終端機模擬器名稱。
163 const std::string& TerminalEmulatorName() const;
164
165 /// @brief 回傳終端機模擬器版本。
166 const std::string& TerminalEmulatorVersion() const;
167
168 /// @brief 回傳終端機能力。
169 const std::vector<int>& TerminalCapabilities() const;
170
171 /// @brief 回傳終端機能力的名稱。
172 std::vector<std::string> TerminalCapabilityNames() const;
173
174 private:
175 void ExitNow();
176 void Install();
177 void Uninstall();
178
179 void PreMain();
180 void PostMain();
181
182 /// @brief 回傳主迴圈是否已結束。
183 bool HasQuitted();
184 void RunOnce(const Component& component);
185 void RunOnceBlocking(Component component);
186
187 void HandleTask(Component component, Task& task);
188 bool HandleSelection(bool handled, Event event);
189 void Draw(Component component);
190 std::string ResetCursorPosition();
191
192 void RequestCursorPosition(bool force = false);
193
194 void TerminalSend(std::string_view);
195 void TerminalFlush();
196
197 void InstallPipedInputHandling();
198 void InstallTerminalInfo();
199
200 void Signal(int signal);
201
202 size_t FetchTerminalEvents();
203
204 void PostAnimationTask();
205
206 struct Internal;
207 explicit App(std::unique_ptr<Internal> internal, int dimx, int dimy);
208
209 std::unique_ptr<Internal> internal_;
210
211 friend class Loop;
212
213 public:
214 class Private {
215 public:
216 static void Signal(App& s, int signal) { s.Signal(signal); }
217 };
218 friend Private;
219};
220
221} // namespace ftxui
222
223#endif /* end of include guard: FTXUI_COMPONENT_APP_HPP */
FTXUI ftxui:: 命名空間
Definition animation.hpp:11
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::variant< Event, Closure, AnimationTask > Task
Definition task.hpp:14
std::function< void()> Closure
Definition task.hpp:13
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23