FTXUI 6.1.9
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 <functional> // for function
9#include <memory> // for shared_ptr
10#include <string> // for string
11
12#include "ftxui/component/animation.hpp" // for TimePoint
13#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
14#include "ftxui/component/event.hpp" // for Event
15#include "ftxui/component/task.hpp" // for Task, Closure
16#include "ftxui/dom/selection.hpp" // for SelectionOption
17#include "ftxui/screen/screen.hpp" // for Screen
18
19namespace ftxui {
20class ComponentBase;
21class Loop;
22struct Event;
23
24using Component = std::shared_ptr<ComponentBase>;
25
26namespace task {
27class TaskRunner;
28}
29
30/// @brief App is a `Screen` that can handle events, run a main
31/// loop, and manage components.
32///
33/// @note This class was previously named ScreenInteractive.
34///
35/// @ingroup component
36class App : public Screen {
37 public:
38 // Constructors:
39 static App FixedSize(int dimx, int dimy);
40 static App Fullscreen();
43 static App FitComponent();
44 static App TerminalOutput();
45
46 // Destructor.
47 ~App() override;
48
49 // Options. Must be called before Loop().
50 void TrackMouse(bool enable = true);
51 void HandlePipedInput(bool enable = true);
52
53 // Return the currently active app, nullptr if none.
54 static App* Active();
55
56 // Start/Stop the main loop.
57 void Loop(Component);
58 void Exit();
60
61 // Post tasks to be executed by the loop.
62 void Post(Task task);
63 void PostEvent(Event event);
65
67
68 // Decorate a function. The outputted one will execute similarly to the
69 // inputted one, but with the currently active app terminal hooks
70 // temporarily uninstalled.
72
73 // FTXUI implements handlers for Ctrl-C and Ctrl-Z. By default, these handlers
74 // are executed, even if the component catches the event. This avoid users
75 // handling every event to be trapped in the application. However, in some
76 // cases, the application may want to handle these events itself. In this
77 // case, the application can force FTXUI to not handle these events by calling
78 // the following functions with force=true.
79 void ForceHandleCtrlC(bool force);
80 void ForceHandleCtrlZ(bool force);
81
82 // Selection API.
83 std::string GetSelection();
84 void SelectionChange(std::function<void()> callback);
85
86 // Terminal info.
87 const std::string& TerminalName() const;
88 int TerminalVersion() const;
89 const std::string& TerminalEmulatorName() const;
90 const std::string& TerminalEmulatorVersion() const;
91 const std::vector<int>& TerminalCapabilities() const;
92 std::vector<std::string> TerminalCapabilityNames() const;
93
94 private:
95 void ExitNow();
96
97 void Install();
98 void Uninstall();
99
100 void PreMain();
101 void PostMain();
102
103 bool HasQuitted();
104 friend class ThrottledRequest;
105 void RunOnce(const Component& component);
106 void RunOnceBlocking(Component component);
107
108 void HandleTask(Component component, Task& task);
109 bool HandleSelection(bool handled, Event event);
110 void RefreshSelection();
111 void Draw(Component component);
112 std::string ResetCursorPosition();
113
114 void RequestCursorPosition(bool force = false);
115
116 void TerminalSend(std::string_view);
117 void TerminalFlush();
118
119 void InstallPipedInputHandling();
120 void InstallTerminalInfo();
121
122 void Signal(int signal);
123
124 size_t FetchTerminalEvents();
125
126 void PostAnimationTask();
127
128 App* suspended_screen_ = nullptr;
129 enum class Dimension {
131 Fixed,
134 };
135 App(Dimension dimension, int dimx, int dimy, bool use_alternative_screen);
136
137 const Dimension dimension_;
138 const bool use_alternative_screen_;
139
140 bool track_mouse_ = true;
141
142 std::string set_cursor_position_;
143 std::string reset_cursor_position_;
144
145 std::atomic<bool> quit_{false};
146 bool installed_ = false;
147 bool animation_requested_ = false;
148 animation::TimePoint previous_animation_time_;
149
150 int cursor_x_ = 1;
151 int cursor_y_ = 1;
152
153 std::uint64_t frame_count_ = 0;
154 bool mouse_captured = false;
155 bool previous_frame_resized_ = false;
156
157 bool frame_valid_ = false;
158
159 bool force_handle_ctrl_c_ = true;
160 bool force_handle_ctrl_z_ = true;
161
162 // Piped input handling state (POSIX only)
163 bool handle_piped_input_ = true;
164 bool is_stdin_a_tty_ = false;
165 bool is_stdout_a_tty_ = false;
166 // File descriptor for /dev/tty, used for piped input handling.
167 int tty_fd_ = -1;
168
169 std::string terminal_name_ = "unknown";
170 int terminal_version_ = 0;
171
172 std::string terminal_emulator_name_ = "unknown";
173 std::string terminal_emulator_version_ = "unknown";
174
175 std::vector<int> terminal_capabilities_;
176
177 // Selection API:
178 CapturedMouse selection_pending_;
179 struct SelectionData {
180 int start_x = -1;
181 int start_y = -1;
182 int end_x = -2;
183 int end_y = -2;
184 bool empty = true;
185 bool operator==(const SelectionData& other) const;
186 bool operator!=(const SelectionData& other) const;
187 };
188 SelectionData selection_data_;
189 SelectionData selection_data_previous_;
190 std::unique_ptr<Selection> selection_;
191 std::function<void()> selection_on_change_;
192
193 // PIMPL private implementation idiom (Pimpl).
194 struct Internal;
195 std::unique_ptr<Internal> internal_;
196
197 friend class Loop;
198
199 Component component_;
200
201 public:
202 class Private {
203 public:
204 static void Signal(App& s, int signal) { s.Signal(signal); }
205 };
206 friend Private;
207};
208
209} // namespace ftxui
210
211#endif /* end of include guard: FTXUI_COMPONENT_APP_HPP */
static void Signal(App &s, int signal)
Definition app.hpp:204
void HandlePipedInput(bool enable=true)
Enable or disable automatic piped input handling. When enabled, FTXUI will detect piped input and red...
Definition app.cpp:461
void Exit()
Exit the main loop.
Definition app.cpp:1323
void PostEvent(Event event)
Add an event to the main loop. It will be executed later, after every other scheduled events.
Definition app.cpp:483
static App FitComponent()
Definition app.cpp:424
~App() override
static App * Active()
Return the currently active screen, or null if none.
Definition app.cpp:616
void Post(Task task)
Add a task to the main loop. It will be executed later, after every other scheduled tasks.
Definition app.cpp:467
int TerminalVersion() const
Return the terminal version.
Definition app.cpp:816
std::vector< std::string > TerminalCapabilityNames() const
Return the names of the terminal capabilities.
Definition app.cpp:805
friend Private
Definition app.hpp:206
static App FullscreenPrimaryScreen()
Definition app.cpp:383
static App Fullscreen()
Definition app.cpp:375
friend class Loop
Definition app.hpp:197
const std::string & TerminalName() const
Return the terminal name.
Definition app.cpp:811
static App TerminalOutput()
Definition app.cpp:409
static App FullscreenAlternateScreen()
Definition app.cpp:396
CapturedMouse CaptureMouse()
Try to get the unique lock about being able to capture the mouse.
Definition app.cpp:505
const std::string & TerminalEmulatorVersion() const
Return the terminal emulator version.
Definition app.cpp:826
static App FixedSize(int dimx, int dimy)
Definition app.cpp:362
std::string GetSelection()
Returns the content of the current selection.
Definition app.cpp:603
const std::vector< int > & TerminalCapabilities() const
Return the terminal capabilities.
Definition app.cpp:831
void TrackMouse(bool enable=true)
Set whether mouse is tracked and events reported. called outside of the main loop....
Definition app.cpp:449
void SelectionChange(std::function< void()> callback)
Definition app.cpp:610
const std::string & TerminalEmulatorName() const
Return the terminal emulator name.
Definition app.cpp:821
void RequestAnimationFrame()
Add a task to draw the screen one more time, until all the animations are done.
Definition app.cpp:490
Closure ExitLoopClosure()
Return a function to exit the main loop.
Definition app.cpp:1318
void ForceHandleCtrlC(bool force)
Force FTXUI to handle or not handle Ctrl-C, even if the component catches the Event::CtrlC.
Definition app.cpp:592
void ForceHandleCtrlZ(bool force)
Force FTXUI to handle or not handle Ctrl-Z, even if the component catches the Event::CtrlZ.
Definition app.cpp:598
Closure WithRestoredIO(Closure)
Decorate a function. It executes the same way, but with the currently active screen terminal hooks te...
Definition app.cpp:582
App is a Screen that can handle events, run a main loop, and manage components.
Definition app.hpp:36
Loop is a class that manages the event loop for a component.
Definition loop.hpp:56
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:32
int dimy() const
Definition surface.hpp:43
int dimx() const
Definition surface.hpp:42
A rectangular grid of Cell.
Definition screen.hpp:26
The FTXUI ftxui::Dimension:: namespace.
std::chrono::time_point< Clock > TimePoint
Definition animation.hpp:29
The FTXUI ftxui:: namespace.
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
Definition app.hpp:24