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 アプリの幅。
41 /// @param dimy アプリの高さ。
42 static App FixedSize(int dimx, int dimy);
43
44 /// @brief ターミナルのフルサイズを使用してAppを作成します。これは、
45 /// ターミナルの内容を乱さないよう、代替スクリーンバッファを使用します。
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 /// されます。
80 /// @note これは`App::Loop`を呼び出す前に呼び出す必要があります。
81 void TrackMouse(bool enable = true);
82
83 /// @brief パイプ入力の自動処理を有効または無効にします。
84 /// 有効にすると、FTXUIはパイプ入力を検出し、キーボード入力のために
85 /// /dev/ttyから標準入力をリダイレクトします。これにより、アプリケーションは
86 /// インタラクティブなキーボードイベントを受け取りながら、パイプされた
87 /// データを読み取ることができます。
88 /// @param enable パイプ入力処理を有効にするかどうか。デフォルトはtrueです。
89 /// @note これはLoop()の前に呼び出す必要があります。
90 /// @note この機能はデフォルトで有効になっています。
91 /// @note この機能はPOSIXシステム(Linux/macOS)でのみ利用可能です。
92 void HandlePipedInput(bool enable = true);
93
94 /// @brief 現在アクティブなappを返します。存在しない場合はnullptrを返します。
95 static App* Active();
96
97 // メインループの開始/停止。
98
99 /// @brief メインループを実行します。
100 /// @param component 描画するコンポーネント。
101 void Loop(Component component);
102
103 /// @brief メインループを終了します。
104 void Exit();
105
106 /// @brief メインループを終了する関数を返します。
107 Closure ExitLoopClosure();
108
109 /// @brief 関数をデコレートします。出力された関数は、入力された関数と
110 /// 同様に実行されますが、現在アクティブなappのターミナルフックが
111 /// 一時的にアンインストールされた状態で実行されます。
112 Closure WithRestoredIO(Closure fn);
113
114 /// @brief FTXUIはCtrl-CとCtrl-Zのハンドラを実装しています。デフォルトでは、
115 /// これらのハンドラは、コンポーネントがイベントを捕捉したとしても実行されます。
116 /// これにより、ユーザーがすべてのイベントを処理してアプリケーションに
117 /// 閉じ込められることを回避できます。しかし、場合によってはアプリケーションが
118 /// これらのイベントを自分自身で処理したいことがあります。この場合、
119 /// アプリケーションは以下の関数をforce=trueで呼び出すことで、FTXUIが
120 /// これらのイベントを処理しないように強制できます。
121 void ForceHandleCtrlC(bool force = true);
122
123 /// @brief コンポーネントがEvent::CtrlZを捕捉したとしても、FTXUIにCtrl-Zを
124 /// 処理させる、または処理させないように強制します。
125 void ForceHandleCtrlZ(bool force = true);
126
127 // ループによって実行されるタスクを投稿します。
128
129 /// @brief メインループにタスクを追加します。
130 /// 他のすべてのスケジュール済みタスクの後に、後で実行されます。
131 void Post(Task task);
132
133 /// @brief メインループにイベントを追加します。
134 /// 他のすべてのスケジュール済みイベントの後に、後で実行されます。
135 void PostEvent(Event event);
136
137 /// @brief メインループにタスクを追加します。
138 /// 他のすべてのスケジュール済みタスクの後に、後で実行されます。
139 static void PostEventOrExecute(Closure closure);
140
141 /// @brief すべてのアニメーションが完了するまで、画面をもう一度描画する
142 /// タスクを追加します。
143 void RequestAnimationFrame();
144
145 // 選択 API:
146
147 /// @brief マウスをキャプチャできることについてのユニークロックの取得を
148 /// 試みます。
149 /// @return マウスがまだキャプチャされていない場合はユニークロック、
150 /// そうでない場合はnullを返します。
151 CapturedMouse CaptureMouse();
152
153 /// @brief 現在の選択内容を返します。
154 std::string GetSelection();
155
156 /// @brief 選択が変更されたときに呼び出されるコールバックを設定します。
157 void SelectionChange(std::function<void()> callback);
158
159 // ターミナル情報。
160
161 /// @brief ターミナル名を返します。
162 const std::string& TerminalName() const;
163
164 /// @brief ターミナルのバージョンを返します。
165 int TerminalVersion() const;
166
167 /// @brief ターミナルエミュレータ名を返します。
168 const std::string& TerminalEmulatorName() const;
169
170 /// @brief ターミナルエミュレータのバージョンを返します。
171 const std::string& TerminalEmulatorVersion() const;
172
173 /// @brief ターミナルの能力を返します。
174 const std::vector<int>& TerminalCapabilities() const;
175
176 /// @brief ターミナル能力の名前を返します。
177 std::vector<std::string> TerminalCapabilityNames() const;
178
179 private:
180 void ExitNow();
181 void Install();
182 void Uninstall();
183
184 void PreMain();
185 void PostMain();
186
187 /// @brief メインループが終了したかどうかを返します。
188 bool HasQuitted();
189 void RunOnce(const Component& component);
190 void RunOnceBlocking(Component component);
191
192 void HandleTask(Component component, Task& task);
193 bool HandleSelection(bool handled, Event event);
194 void Draw(Component component);
195 std::string ResetCursorPosition();
196
197 void RequestCursorPosition(bool force = false);
198
199 void TerminalSend(std::string_view);
200 void TerminalFlush();
201
202 void InstallPipedInputHandling();
203 void InstallTerminalInfo();
204
205 void Signal(int signal);
206
207 size_t FetchTerminalEvents();
208
209 void PostAnimationTask();
210
211 struct Internal;
212 explicit App(std::unique_ptr<Internal> internal, int dimx, int dimy);
213
214 std::unique_ptr<Internal> internal_;
215
216 friend class Loop;
217
218 public:
219 class Private {
220 public:
221 static void Signal(App& s, int signal) { s.Signal(signal); }
222 };
223 friend Private;
224};
225
226} // namespace ftxui
227
228#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