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 <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 is a class that manages the application lifecycle.
29/// It is responsible for initializing the terminal, running the main loop,
30/// and cleaning up on exit.
31///
32/// @note This class was previously named ScreenInteractive.
33///
34/// @ingroup component
35class FTXUI_EXPORT(COMPONENT) App : public Screen {
36 public:
37 // Constructors:
38
39 /// @brief Create an App with a fixed size.
40 /// @param dimx The width of the app.
41 /// @param dimy The height of the app.
42 static App FixedSize(int dimx, int dimy);
43
44 /// @brief Create an App taking the full terminal size. This is using the
45 /// alternate screen buffer to avoid messing with the terminal content.
46 /// @note This is the same as `App::FullscreenAlternateScreen()`
47 static App Fullscreen();
48
49 /// @brief Create an App taking the full terminal size. The primary screen
50 /// buffer is being used. It means if the terminal is resized, the previous
51 /// content might mess up with the terminal content.
52 static App FullscreenPrimaryScreen();
53
54 /// @brief Create an App taking the full terminal size. This is using the
55 /// alternate screen buffer to avoid messing with the terminal content.
56 static App FullscreenAlternateScreen();
57
58 /// @brief Create an App whose width and height match the component being
59 /// drawn.
60 static App FitComponent();
61
62 /// @brief Create an App whose width match the terminal output width and
63 /// the height matches the component being drawn.
64 static App TerminalOutput();
65
66 // Destructor.
67 ~App() override;
68
69 App(App&&) noexcept;
70 App& operator=(App&&) noexcept;
71 App(const App&) = delete;
72 App& operator=(const App&) = delete;
73
74 // Options. Must be called before Loop().
75
76 /// @brief Set whether mouse is tracked and events reported.
77 /// @param enable Whether to enable mouse event tracking.
78 /// @note Mouse tracking is enabled by default.
79 /// @note Mouse tracking is only supported on terminals that supports it.
80 /// @note This must be called before calling `App::Loop`.
81 void TrackMouse(bool enable = true);
82
83 /// @brief Enable or disable automatic piped input handling.
84 /// When enabled, FTXUI will detect piped input and redirect stdin from
85 /// /dev/tty for keyboard input, allowing applications to read piped data
86 /// while still receiving interactive keyboard events.
87 /// @param enable Whether to enable piped input handling. Default is true.
88 /// @note This must be called before Loop().
89 /// @note This feature is enabled by default.
90 /// @note This feature is only available on POSIX systems (Linux/macOS).
91 void HandlePipedInput(bool enable = true);
92
93 /// @brief Return the currently active app, nullptr if none.
94 static App* Active();
95
96 // Start/Stop the main loop.
97
98 /// @brief Execute the main loop.
99 /// @param component The component to draw.
100 void Loop(Component component);
101
102 /// @brief Exit the main loop.
103 void Exit();
104
105 /// @brief Return a function to exit the main loop.
106 Closure ExitLoopClosure();
107
108 /// @brief Decorate a function. The outputted one will execute similarly to
109 /// the inputted one, but with the currently active app terminal hooks
110 /// temporarily uninstalled.
111 Closure WithRestoredIO(Closure fn);
112
113 /// @brief FTXUI implements handlers for Ctrl-C and Ctrl-Z. By default, these
114 /// handlers are executed, even if the component catches the event. This avoid
115 /// users handling every event to be trapped in the application. However, in
116 /// some cases, the application may want to handle these events itself. In
117 /// this case, the application can force FTXUI to not handle these events by
118 /// calling the following functions with force=true.
119 void ForceHandleCtrlC(bool force = true);
120
121 /// @brief Force FTXUI to handle or not handle Ctrl-Z, even if the component
122 /// catches the Event::CtrlZ.
123 void ForceHandleCtrlZ(bool force = true);
124
125 // Post tasks to be executed by the loop.
126
127 /// @brief Add a task to the main loop.
128 /// It will be executed later, after every other scheduled tasks.
129 void Post(Task task);
130
131 /// @brief Add an event to the main loop.
132 /// It will be executed later, after every other scheduled events.
133 void PostEvent(Event event);
134
135 /// @brief Add a task to the main loop.
136 /// It will be executed later, after every other scheduled tasks.
137 static void PostEventOrExecute(Closure closure);
138
139 /// @brief Add a task to draw the screen one more time, until all the
140 /// animations are done.
141 void RequestAnimationFrame();
142
143 // Selection API:
144
145 /// @brief Try to get the unique lock about being able to capture the mouse.
146 /// @return A unique lock if the mouse is not already captured, otherwise a
147 /// null.
148 CapturedMouse CaptureMouse();
149
150 /// @brief Returns the content of the current selection.
151 std::string GetSelection();
152
153 /// @brief Set a callback that will be called when the selection changes.
154 void SelectionChange(std::function<void()> callback);
155
156 // Terminal info.
157
158 /// @brief Return the terminal name.
159 const std::string& TerminalName() const;
160
161 /// @brief Return the terminal version.
162 int TerminalVersion() const;
163
164 /// @brief Return the terminal emulator name.
165 const std::string& TerminalEmulatorName() const;
166
167 /// @brief Return the terminal emulator version.
168 const std::string& TerminalEmulatorVersion() const;
169
170 /// @brief Return the terminal capabilities.
171 const std::vector<int>& TerminalCapabilities() const;
172
173 /// @brief Return the names of the terminal capabilities.
174 std::vector<std::string> TerminalCapabilityNames() const;
175
176 private:
177 void ExitNow();
178 void Install();
179 void Uninstall();
180
181 void PreMain();
182 void PostMain();
183
184 /// @brief Return whether the main loop has been quit.
185 bool HasQuitted();
186 void RunOnce(const Component& component);
187 void RunOnceBlocking(Component component);
188
189 void HandleTask(Component component, Task& task);
190 bool HandleSelection(bool handled, Event event);
191 void Draw(Component component);
192 std::string ResetCursorPosition();
193
194 void RequestCursorPosition(bool force = false);
195
196 void TerminalSend(std::string_view);
197 void TerminalFlush();
198
199 void InstallPipedInputHandling();
200 void InstallTerminalInfo();
201
202 void Signal(int signal);
203
204 size_t FetchTerminalEvents();
205
206 void PostAnimationTask();
207
208 struct Internal;
209 explicit App(std::unique_ptr<Internal> internal, int dimx, int dimy);
210
211 std::unique_ptr<Internal> internal_;
212
213 friend class Loop;
214
215 public:
216 class Private {
217 public:
218 static void Signal(App& s, int signal) { s.Signal(signal); }
219 };
220 friend Private;
221};
222
223} // namespace ftxui
224
225#endif /* end of include guard: FTXUI_COMPONENT_APP_HPP */
The FTXUI ftxui:: namespace.
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