FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
screen_interactive.hpp
Go to the documentation of this file.
1// 版權所有 2020 Arthur Sonzogni。保留所有權利。
2// 本原始碼的使用受 MIT 授權條款約束,該條款可在 LICENSE 檔案中找到。
3#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
4#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
5
6#include <atomic> // for atomic
7#include <functional> // for function
8#include <memory> // for shared_ptr
9#include <string> // for string
10
11#include "ftxui/component/animation.hpp" // for TimePoint
12#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
13#include "ftxui/component/event.hpp" // for Event
14#include "ftxui/component/task.hpp" // for Task, Closure
15#include "ftxui/dom/selection.hpp" // for SelectionOption
16#include "ftxui/screen/screen.hpp" // for Screen
17
18namespace ftxui {
19class ComponentBase;
20class Loop;
21struct Event;
22
23using Component = std::shared_ptr<ComponentBase>;
24class ScreenInteractivePrivate;
25
26namespace task {
27class TaskRunner;
28}
29
30/// @brief ScreenInteractive 是一個可以處理事件、執行主迴圈並管理組件的 `Screen`。
31///
32/// @ingroup component
33class ScreenInteractive : public Screen {
34 public:
35 // 建構函式:
36 static ScreenInteractive FixedSize(int dimx, int dimy);
42
43 // 解構函式。
45
46 // 選項。必須在 Loop() 之前呼叫。
47 void TrackMouse(bool enable = true);
48 void HandlePipedInput(bool enable = true);
49
50 // 返回當前活動的螢幕,如果沒有則返回 nullptr。
51 static ScreenInteractive* Active();
52
53 // 啟動/停止主迴圈。
54 void Loop(Component);
55 void Exit();
57
58 // 發布要由迴圈執行的任務。
59 void Post(Task task);
60 void PostEvent(Event event);
62
64
65 // 裝飾一個函式。輸出函式的執行方式與輸入函式類似,但會暫時卸載當前活動螢幕的終端機掛鉤。
67
68 // FTXUI 實作了 Ctrl-C 和 Ctrl-Z 的處理程式。預設情況下,即使組件捕捉到事件,這些處理程式也會執行。這避免了使用者處理每個事件時被應用程式困住。然而,在某些情況下,應用程式可能希望自行處理這些事件。在這種情況下,應用程式可以透過呼叫以下函式並將 force=true 傳遞來強制 FTXUI 不處理這些事件。
69 void ForceHandleCtrlC(bool force);
70 void ForceHandleCtrlZ(bool force);
71
72 // 選取 API。
73 std::string GetSelection();
74 void SelectionChange(std::function<void()> callback);
75
76 private:
77 void ExitNow();
78
79 void Install();
80 void Uninstall();
81
82 void PreMain();
83 void PostMain();
84
85 bool HasQuitted();
86 void RunOnce(Component component);
87 void RunOnceBlocking(Component component);
88
89 void HandleTask(Component component, Task& task);
90 bool HandleSelection(bool handled, Event event);
91 void RefreshSelection();
92 void Draw(Component component);
93 void ResetCursorPosition();
94
95 void InstallPipedInputHandling();
96
97 void Signal(int signal);
98
99 void FetchTerminalEvents();
100
101 void PostAnimationTask();
102
103 ScreenInteractive* suspended_screen_ = nullptr;
104 enum class Dimension {
106 Fixed,
109 };
111 int dimx,
112 int dimy,
113 bool use_alternative_screen);
114
115 const Dimension dimension_;
116 const bool use_alternative_screen_;
117
118 bool track_mouse_ = true;
119
120 std::string set_cursor_position;
121 std::string reset_cursor_position;
122
123 std::atomic<bool> quit_{false};
124 bool animation_requested_ = false;
125 animation::TimePoint previous_animation_time_;
126
127 int cursor_x_ = 1;
128 int cursor_y_ = 1;
129
130 std::uint64_t frame_count_ = 0;
131 bool mouse_captured = false;
132 bool previous_frame_resized_ = false;
133
134 bool frame_valid_ = false;
135
136 bool force_handle_ctrl_c_ = true;
137 bool force_handle_ctrl_z_ = true;
138
139 // 管道輸入處理狀態(僅限 POSIX)
140 bool handle_piped_input_ = true;
141 // /dev/tty 的檔案描述符,用於管道輸入處理。
142 int tty_fd_ = -1;
143
144 // 退出時要恢復的游標樣式。
145 int cursor_reset_shape_ = 1;
146
147 // 選取 API:
148 CapturedMouse selection_pending_;
149 struct SelectionData {
150 int start_x = -1;
151 int start_y = -1;
152 int end_x = -2;
153 int end_y = -2;
154 bool empty = true;
155 bool operator==(const SelectionData& other) const;
156 bool operator!=(const SelectionData& other) const;
157 };
158 SelectionData selection_data_;
159 SelectionData selection_data_previous_;
160 std::unique_ptr<Selection> selection_;
161 std::function<void()> selection_on_change_;
162
163 // PIMPL 私有實作慣用語 (Pimpl)。
164 struct Internal;
165 std::unique_ptr<Internal> internal_;
166
167 friend class Loop;
168
169 Component component_;
170
171 public:
172 class Private {
173 public:
174 static void Signal(ScreenInteractive& s, int signal) { s.Signal(signal); }
175 };
176 friend Private;
177};
178
179} // namespace ftxui
180
181#endif /* 包含防護結束: FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP */
Loop 是一個用於管理元件事件循環的類別。
Definition loop.hpp:56
static void Signal(ScreenInteractive &s, int signal)
auto component
Definition gallery.cpp:127
screen Loop(component)
static ScreenInteractive TerminalOutput()
void HandlePipedInput(bool enable=true)
Enable or disable automatic piped input handling. When enabled, FTXUI will detect piped input and red...
void Exit()
Exit the main loop.
static ScreenInteractive FixedSize(int dimx, int dimy)
void PostEvent(Event event)
Add an event to the main loop. It will be executed later, after every other scheduled events.
void Post(Task task)
Add a task to the main loop. It will be executed later, after every other scheduled tasks.
static ScreenInteractive FitComponent()
static ScreenInteractive Fullscreen()
static ScreenInteractive FullscreenPrimaryScreen()
static ScreenInteractive * Active()
Return the currently active screen, or null if none.
CapturedMouse CaptureMouse()
Try to get the unique lock about behing able to capture the mouse.
std::string GetSelection()
Returns the content of the current selection.
static ScreenInteractive FullscreenAlternateScreen()
void TrackMouse(bool enable=true)
Set whether mouse is tracked and events reported. called outside of the main loop....
void SelectionChange(std::function< void()> callback)
void RequestAnimationFrame()
Add a task to draw the screen one more time, until all the animations are done.
Closure ExitLoopClosure()
Return a function to exit the main loop.
void ForceHandleCtrlC(bool force)
Force FTXUI to handle or not handle Ctrl-C, even if the component catches the Event::CtrlC.
void ForceHandleCtrlZ(bool force)
Force FTXUI to handle or not handle Ctrl-Z, even if the component catches the Event::CtrlZ.
Closure WithRestoredIO(Closure)
Decorate a function. It executes the same way, but with the currently active screen terminal hooks te...
ScreenInteractive 是一個可以處理事件、執行主迴圈並管理組件的 Screen。
代表一個事件。它可以是按鍵事件、終端機大小調整,或更多...
Definition event.hpp:27
int dimy() const
Definition image.hpp:36
int dimx() const
Definition image.hpp:35
像素的矩形網格。
Definition screen.hpp:26
FTXUI 的 ftxui::Dimension:: 命名空間
std::chrono::time_point< Clock > TimePoint
Definition animation.hpp:24
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
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