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 private:
87 void ExitNow();
88
89 void Install();
90 void Uninstall();
91
92 void PreMain();
93 void PostMain();
94
95 bool HasQuitted();
96 friend class ThrottledRequest;
97 void RunOnce(Component component);
98 void RunOnceBlocking(Component component);
99
100 void HandleTask(Component component, Task& task);
101 bool HandleSelection(bool handled, Event event);
102 void RefreshSelection();
103 void Draw(Component component);
104 std::string ResetCursorPosition();
105
106 void RequestCursorPosition(bool force = false);
107 void RequestCursorShape();
108
109 void TerminalSend(std::string_view);
110 void TerminalFlush();
111
112 void InstallPipedInputHandling();
113
114 void Signal(int signal);
115
116 size_t FetchTerminalEvents();
117
118 void PostAnimationTask();
119
120 App* suspended_screen_ = nullptr;
121 enum class Dimension {
123 Fixed,
126 };
127 App(Dimension dimension, int dimx, int dimy, bool use_alternative_screen);
128
129 const Dimension dimension_;
130 const bool use_alternative_screen_;
131
132 bool track_mouse_ = true;
133
134 std::string set_cursor_position_;
135 std::string reset_cursor_position_;
136
137 std::atomic<bool> quit_{false};
138 bool installed_ = false;
139 bool animation_requested_ = false;
140 animation::TimePoint previous_animation_time_;
141
142 int cursor_x_ = 1;
143 int cursor_y_ = 1;
144
145 std::uint64_t frame_count_ = 0;
146 bool mouse_captured = false;
147 bool previous_frame_resized_ = false;
148
149 bool frame_valid_ = false;
150
151 bool force_handle_ctrl_c_ = true;
152 bool force_handle_ctrl_z_ = true;
153
154 // Piped input handling state (POSIX only)
155 bool handle_piped_input_ = true;
156 bool is_a_tty_ = false;
157 // File descriptor for /dev/tty, used for piped input handling.
158 int tty_fd_ = -1;
159
160 // The style of the cursor to restore on exit.
161 int cursor_reset_shape_ = 1;
162
163 // Selection API:
164 CapturedMouse selection_pending_;
165 struct SelectionData {
166 int start_x = -1;
167 int start_y = -1;
168 int end_x = -2;
169 int end_y = -2;
170 bool empty = true;
171 bool operator==(const SelectionData& other) const;
172 bool operator!=(const SelectionData& other) const;
173 };
174 SelectionData selection_data_;
175 SelectionData selection_data_previous_;
176 std::unique_ptr<Selection> selection_;
177 std::function<void()> selection_on_change_;
178
179 // PIMPL private implementation idiom (Pimpl).
180 struct Internal;
181 std::unique_ptr<Internal> internal_;
182
183 friend class Loop;
184
185 Component component_;
186
187 public:
188 class Private {
189 public:
190 static void Signal(App& s, int signal) { s.Signal(signal); }
191 };
192 friend Private;
193};
194
195} // namespace ftxui
196
197#endif /* end of include guard: FTXUI_COMPONENT_APP_HPP */
static void Signal(App &s, int signal)
Definition app.hpp:190
void HandlePipedInput(bool enable=true)
Enable or disable automatic piped input handling. When enabled, FTXUI will detect piped input and red...
Definition app.cpp:463
void Exit()
Exit the main loop.
Definition app.cpp:1160
void PostEvent(Event event)
Add an event to the main loop. It will be executed later, after every other scheduled events.
Definition app.cpp:477
static App FitComponent()
Definition app.cpp:426
~App() override
static App * Active()
Return the currently active screen, or null if none.
Definition app.cpp:609
void Post(Task task)
Add a task to the main loop. It will be executed later, after every other scheduled tasks.
Definition app.cpp:469
friend Private
Definition app.hpp:192
static App FullscreenPrimaryScreen()
Definition app.cpp:385
static App Fullscreen()
Definition app.cpp:377
friend class Loop
Definition app.hpp:183
static App TerminalOutput()
Definition app.cpp:411
static App FullscreenAlternateScreen()
Definition app.cpp:398
CapturedMouse CaptureMouse()
Try to get the unique lock about behing able to capture the mouse.
Definition app.cpp:498
static App FixedSize(int dimx, int dimy)
Definition app.cpp:364
std::string GetSelection()
Returns the content of the current selection.
Definition app.cpp:596
void TrackMouse(bool enable=true)
Set whether mouse is tracked and events reported. called outside of the main loop....
Definition app.cpp:451
void SelectionChange(std::function< void()> callback)
Definition app.cpp:603
void RequestAnimationFrame()
Add a task to draw the screen one more time, until all the animations are done.
Definition app.cpp:483
Closure ExitLoopClosure()
Return a function to exit the main loop.
Definition app.cpp:1155
void ForceHandleCtrlC(bool force)
Force FTXUI to handle or not handle Ctrl-C, even if the component catches the Event::CtrlC.
Definition app.cpp:585
void ForceHandleCtrlZ(bool force)
Force FTXUI to handle or not handle Ctrl-Z, even if the component catches the Event::CtrlZ.
Definition app.cpp:591
Closure WithRestoredIO(Closure)
Decorate a function. It executes the same way, but with the currently active screen terminal hooks te...
Definition app.cpp:575
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:30
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