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/// @ingroup component
34class App : public Screen {
35 public:
36 // Constructors:
37 static App FixedSize(int dimx, int dimy);
38 static App Fullscreen();
41 static App FitComponent();
42 static App TerminalOutput();
43
44 // Destructor.
45 ~App() override;
46
47 // Options. Must be called before Loop().
48 void TrackMouse(bool enable = true);
49 void HandlePipedInput(bool enable = true);
50
51 // Return the currently active app, nullptr if none.
52 static App* Active();
53
54 // Start/Stop the main loop.
55 void Loop(Component);
56 void Exit();
58
59 // Post tasks to be executed by the loop.
60 void Post(Task task);
61 void PostEvent(Event event);
63
65
66 // Decorate a function. The outputted one will execute similarly to the
67 // inputted one, but with the currently active app terminal hooks
68 // temporarily uninstalled.
70
71 // FTXUI implements handlers for Ctrl-C and Ctrl-Z. By default, these handlers
72 // are executed, even if the component catches the event. This avoid users
73 // handling every event to be trapped in the application. However, in some
74 // cases, the application may want to handle these events itself. In this
75 // case, the application can force FTXUI to not handle these events by calling
76 // the following functions with force=true.
77 void ForceHandleCtrlC(bool force);
78 void ForceHandleCtrlZ(bool force);
79
80 // Selection API.
81 std::string GetSelection();
82 void SelectionChange(std::function<void()> callback);
83
84 private:
85 void ExitNow();
86
87 void Install();
88 void Uninstall();
89
90 void PreMain();
91 void PostMain();
92
93 bool HasQuitted();
94 void RunOnce(Component component);
95 void RunOnceBlocking(Component component);
96
97 void HandleTask(Component component, Task& task);
98 bool HandleSelection(bool handled, Event event);
99 void RefreshSelection();
100 void Draw(Component component);
101 std::string ResetCursorPosition();
102
103 void TerminalSend(std::string_view);
104 void TerminalFlush();
105
106 void InstallPipedInputHandling();
107
108 void Signal(int signal);
109
110 void FetchTerminalEvents();
111
112 void PostAnimationTask();
113
114 App* suspended_screen_ = nullptr;
115 enum class Dimension {
117 Fixed,
120 };
121 App(Dimension dimension, int dimx, int dimy, bool use_alternative_screen);
122
123 const Dimension dimension_;
124 const bool use_alternative_screen_;
125
126 bool track_mouse_ = true;
127
128 std::string set_cursor_position_;
129 std::string reset_cursor_position_;
130
131 std::atomic<bool> quit_{false};
132 bool animation_requested_ = false;
133 animation::TimePoint previous_animation_time_;
134
135 int cursor_x_ = 1;
136 int cursor_y_ = 1;
137
138 std::uint64_t frame_count_ = 0;
139 bool mouse_captured = false;
140 bool previous_frame_resized_ = false;
141
142 bool frame_valid_ = false;
143
144 bool force_handle_ctrl_c_ = true;
145 bool force_handle_ctrl_z_ = true;
146
147 // Piped input handling state (POSIX only)
148 bool handle_piped_input_ = true;
149 // File descriptor for /dev/tty, used for piped input handling.
150 int tty_fd_ = -1;
151
152 // The style of the cursor to restore on exit.
153 int cursor_reset_shape_ = 1;
154
155 // Selection API:
156 CapturedMouse selection_pending_;
157 struct SelectionData {
158 int start_x = -1;
159 int start_y = -1;
160 int end_x = -2;
161 int end_y = -2;
162 bool empty = true;
163 bool operator==(const SelectionData& other) const;
164 bool operator!=(const SelectionData& other) const;
165 };
166 SelectionData selection_data_;
167 SelectionData selection_data_previous_;
168 std::unique_ptr<Selection> selection_;
169 std::function<void()> selection_on_change_;
170
171 // PIMPL private implementation idiom (Pimpl).
172 struct Internal;
173 std::unique_ptr<Internal> internal_;
174
175 friend class Loop;
176
177 Component component_;
178
179 public:
180 class Private {
181 public:
182 static void Signal(App& s, int signal) { s.Signal(signal); }
183 };
184 friend Private;
185};
186
187} // namespace ftxui
188
189#endif /* end of include guard: FTXUI_COMPONENT_APP_HPP */
static void Signal(App &s, int signal)
Definition app.hpp:182
void HandlePipedInput(bool enable=true)
Enable or disable automatic piped input handling. When enabled, FTXUI will detect piped input and red...
Definition app.cpp:377
void Exit()
Exit the main loop.
Definition app.cpp:1041
void PostEvent(Event event)
Add an event to the main loop. It will be executed later, after every other scheduled events.
Definition app.cpp:391
static App FitComponent()
Definition app.cpp:340
~App() override
static App * Active()
Return the currently active screen, or null if none.
Definition app.cpp:522
void Post(Task task)
Add a task to the main loop. It will be executed later, after every other scheduled tasks.
Definition app.cpp:383
friend Private
Definition app.hpp:184
static App FullscreenPrimaryScreen()
Definition app.cpp:299
static App Fullscreen()
Definition app.cpp:291
friend class Loop
Definition app.hpp:175
static App TerminalOutput()
Definition app.cpp:325
static App FullscreenAlternateScreen()
Definition app.cpp:312
CapturedMouse CaptureMouse()
Try to get the unique lock about behing able to capture the mouse.
Definition app.cpp:412
static App FixedSize(int dimx, int dimy)
Definition app.cpp:278
std::string GetSelection()
Returns the content of the current selection.
Definition app.cpp:509
void TrackMouse(bool enable=true)
Set whether mouse is tracked and events reported. called outside of the main loop....
Definition app.cpp:365
void SelectionChange(std::function< void()> callback)
Definition app.cpp:516
void RequestAnimationFrame()
Add a task to draw the screen one more time, until all the animations are done.
Definition app.cpp:397
Closure ExitLoopClosure()
Return a function to exit the main loop.
Definition app.cpp:1036
void ForceHandleCtrlC(bool force)
Force FTXUI to handle or not handle Ctrl-C, even if the component catches the Event::CtrlC.
Definition app.cpp:498
void ForceHandleCtrlZ(bool force)
Force FTXUI to handle or not handle Ctrl-Z, even if the component catches the Event::CtrlZ.
Definition app.cpp:504
Closure WithRestoredIO(Closure)
Decorate a function. It executes the same way, but with the currently active screen terminal hooks te...
Definition app.cpp:488
App is a Screen that can handle events, run a main loop, and manage components.
Definition app.hpp:34
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:40
int dimx() const
Definition surface.hpp:39
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