FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
screen_interactive.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_SCREEN_INTERACTIVE_HPP
5#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_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>;
25class ScreenInteractivePrivate;
26
27namespace task {
28class TaskRunner;
29}
30
31/// @brief ScreenInteractive is a `Screen` that can handle events, run a main
32/// loop, and manage components.
33///
34/// @ingroup component
35class ScreenInteractive : public Screen {
36 public:
37 // Constructors:
38 static ScreenInteractive FixedSize(int dimx, int dimy);
44
45 // Destructor.
47
48 // Options. Must be called before Loop().
49 void TrackMouse(bool enable = true);
50
51 // Return the currently active screen, nullptr if none.
52 static ScreenInteractive* 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 screen 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 void ResetCursorPosition();
102
103 void Signal(int signal);
104
105 void FetchTerminalEvents();
106
107 void PostAnimationTask();
108
109 ScreenInteractive* suspended_screen_ = nullptr;
110 enum class Dimension {
112 Fixed,
115 };
116 ScreenInteractive(Dimension dimension,
117 int dimx,
118 int dimy,
119 bool use_alternative_screen);
120 const Dimension dimension_;
121 const bool use_alternative_screen_;
122
123 bool track_mouse_ = true;
124
125 std::string set_cursor_position;
126 std::string reset_cursor_position;
127
128 std::atomic<bool> quit_{false};
129 bool animation_requested_ = false;
130 animation::TimePoint previous_animation_time_;
131
132 int cursor_x_ = 1;
133 int cursor_y_ = 1;
134
135 std::uint64_t frame_count_ = 0;
136 bool mouse_captured = false;
137 bool previous_frame_resized_ = false;
138
139 bool frame_valid_ = false;
140
141 bool force_handle_ctrl_c_ = true;
142 bool force_handle_ctrl_z_ = true;
143
144 // The style of the cursor to restore on exit.
145 int cursor_reset_shape_ = 1;
146
147 // Selection 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 private implementation idiom (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 /* end of include guard: FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP */
static void Signal(ScreenInteractive &s, int signal)
static ScreenInteractive TerminalOutput()
void Exit()
Exit the main loop.
static ScreenInteractive FixedSize(int dimx, int dimy)
void PostEvent(Event event)
Add an event to the main loop. It will be executed later, after every other scheduled events.
void Post(Task task)
Add a task to the main loop. It will be executed later, after every other scheduled tasks.
static ScreenInteractive FitComponent()
static ScreenInteractive Fullscreen()
static ScreenInteractive FullscreenPrimaryScreen()
static ScreenInteractive * Active()
Return the currently active screen, or null if none.
CapturedMouse CaptureMouse()
Try to get the unique lock about behing able to capture the mouse.
std::string GetSelection()
Returns the content of the current selection.
static ScreenInteractive FullscreenAlternateScreen()
void TrackMouse(bool enable=true)
Set whether mouse is tracked and events reported. called outside of the main loop....
void SelectionChange(std::function< void()> callback)
void RequestAnimationFrame()
Add a task to draw the screen one more time, until all the animations are done.
Closure ExitLoopClosure()
Return a function to exit the main loop.
void ForceHandleCtrlC(bool force)
Force FTXUI to handle or not handle Ctrl-C, even if the component catches the Event::CtrlC.
void ForceHandleCtrlZ(bool force)
Force FTXUI to handle or not handle Ctrl-Z, even if the component catches the Event::CtrlZ.
Closure WithRestoredIO(Closure)
Decorate a function. It executes the same way, but with the currently active screen terminal hooks te...
Loop is a class that manages the event loop for a component.
Definition loop.hpp:56
ScreenInteractive is a Screen that can handle events, run a main loop, and manage components.
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
int dimy() const
Definition image.hpp:36
int dimx() const
Definition image.hpp:35
A rectangular grid of Pixel.
Definition screen.hpp:26
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