FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
screen_interactive.hpp
浏览该文件的文档.
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 */
static void Signal(ScreenInteractive &s, int signal)
static ScreenInteractive TerminalOutput()
void HandlePipedInput(bool enable=true)
启用或禁用自动管道输入处理。 启用后,FTXUI 将检测管道输入并将 stdin 从 /dev/tty 重定向 以进行键盘输入,从而允许应用程序读取管道数据,同时仍 接收交互式键盘事件。
static ScreenInteractive FixedSize(int dimx, int dimy)
void PostEvent(Event event)
向主循环添加一个事件。 它将在所有其他计划事件之后执行。
void Post(Task task)
向主循环添加一个任务。 它将在所有其他计划任务之后执行。
static ScreenInteractive FitComponent()
创建一个 ScreenInteractive,其宽度和高度与正在绘制的组件匹配。
static ScreenInteractive Fullscreen()
static ScreenInteractive FullscreenPrimaryScreen()
static ScreenInteractive * Active()
返回当前活动屏幕,如果没有则返回空。
CapturedMouse CaptureMouse()
尝试获取能够捕获鼠标的唯一锁。
std::string GetSelection()
返回当前选择的内容
static ScreenInteractive FullscreenAlternateScreen()
void TrackMouse(bool enable=true)
设置是否跟踪鼠标并报告事件。 在主循环之外调用。例如 ScreenInteractive::Loop(...)。
void SelectionChange(std::function< void()> callback)
void RequestAnimationFrame()
添加一个任务以再次绘制屏幕,直到所有动画完成。
Closure ExitLoopClosure()
返回一个退出主循环的函数。
void ForceHandleCtrlC(bool force)
强制 FTXUI 处理或不处理 Ctrl-C,即使组件 捕获了 Event::CtrlC。
void ForceHandleCtrlZ(bool force)
强制 FTXUI 处理或不处理 Ctrl-Z,即使组件 捕获了 Event::CtrlZ。
Closure WithRestoredIO(Closure)
装饰一个函数。它以相同的方式执行,但在执行期间, 当前活动屏幕终端的钩子会被暂时卸载。
Loop 是一个管理组件事件循环的类。
ScreenInteractive 是一个可以处理事件、运行主循环和管理组件的 Screen。
代表一个事件。它可以是按键事件、终端大小调整等等...
int dimy() const
int dimx() const
像素的矩形网格。
FTXUI ftxui::Dimension:: 命名空间
std::chrono::time_point< Clock > TimePoint
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::variant< Event, Closure, AnimationTask > Task
std::function< void()> Closure
std::shared_ptr< ComponentBase > Component
screen Loop(renderer)