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 <ftxui/component/receiver.hpp> // for Receiver, Sender
9#include <functional> // for function
10#include <memory> // for shared_ptr
11#include <string> // for string
12#include <thread> // for thread
13#include <variant> // for variant
14
15#include "ftxui/component/animation.hpp" // for TimePoint
16#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
17#include "ftxui/component/event.hpp" // for Event
18#include "ftxui/component/task.hpp" // for Task, Closure
19#include "ftxui/dom/selection.hpp" // for SelectionOption
20#include "ftxui/screen/screen.hpp" // for Screen
21
22namespace ftxui {
23class ComponentBase;
24class Loop;
25struct Event;
26
27using Component = std::shared_ptr<ComponentBase>;
28class ScreenInteractivePrivate;
29
30/// @brief ScreenInteractive is a `Screen` that can handle events, run a main
31/// loop, and manage components.
32///
33/// @ingroup component
34class ScreenInteractive : public Screen {
35 public:
36 // Constructors:
37 static ScreenInteractive FixedSize(int dimx, int dimy);
43
44 // Options. Must be called before Loop().
45 void TrackMouse(bool enable = true);
46
47 // Return the currently active screen, nullptr if none.
48 static ScreenInteractive* Active();
49
50 // Start/Stop the main loop.
51 void Loop(Component);
52 void Exit();
54
55 // Post tasks to be executed by the loop.
56 void Post(Task task);
57 void PostEvent(Event event);
59
61
62 // Decorate a function. The outputted one will execute similarly to the
63 // inputted one, but with the currently active screen terminal hooks
64 // temporarily uninstalled.
66
67 // FTXUI implements handlers for Ctrl-C and Ctrl-Z. By default, these handlers
68 // are executed, even if the component catches the event. This avoid users
69 // handling every event to be trapped in the application. However, in some
70 // cases, the application may want to handle these events itself. In this
71 // case, the application can force FTXUI to not handle these events by calling
72 // the following functions with force=true.
73 void ForceHandleCtrlC(bool force);
74 void ForceHandleCtrlZ(bool force);
75
76 // Selection API.
77 std::string GetSelection();
78 void SelectionChange(std::function<void()> callback);
79
80 private:
81 void ExitNow();
82
83 void Install();
84 void Uninstall();
85
86 void PreMain();
87 void PostMain();
88
89 bool HasQuitted();
90 void RunOnce(Component component);
91 void RunOnceBlocking(Component component);
92
93 void HandleTask(Component component, Task& task);
94 bool HandleSelection(bool handled, Event event);
95 void RefreshSelection();
96 void Draw(Component component);
97 void ResetCursorPosition();
98
99 void Signal(int signal);
100
101 ScreenInteractive* suspended_screen_ = nullptr;
102 enum class Dimension {
104 Fixed,
107 };
108 Dimension dimension_ = Dimension::Fixed;
109 bool use_alternative_screen_ = false;
111 int dimy,
112 Dimension dimension,
113 bool use_alternative_screen);
114
115 bool track_mouse_ = true;
116
117 Sender<Task> task_sender_;
118 Receiver<Task> task_receiver_;
119
120 std::string set_cursor_position;
121 std::string reset_cursor_position;
122
123 std::atomic<bool> quit_{false};
124 std::thread event_listener_;
125 std::thread animation_listener_;
126 bool animation_requested_ = false;
127 animation::TimePoint previous_animation_time_;
128
129 int cursor_x_ = 1;
130 int cursor_y_ = 1;
131
132 bool mouse_captured = false;
133 bool previous_frame_resized_ = false;
134
135 bool frame_valid_ = false;
136
137 bool force_handle_ctrl_c_ = true;
138 bool force_handle_ctrl_z_ = true;
139
140 // The style of the cursor to restore on exit.
141 int cursor_reset_shape_ = 1;
142
143 // Selection API:
144 CapturedMouse selection_pending_;
145 struct SelectionData {
146 int start_x = -1;
147 int start_y = -1;
148 int end_x = -2;
149 int end_y = -2;
150 bool empty = true;
151 bool operator==(const SelectionData& other) const;
152 bool operator!=(const SelectionData& other) const;
153 };
154 SelectionData selection_data_;
155 SelectionData selection_data_previous_;
156 std::unique_ptr<Selection> selection_;
157 std::function<void()> selection_on_change_;
158
159 friend class Loop;
160
161 public:
162 class Private {
163 public:
164 static void Signal(ScreenInteractive& s, int signal) { s.Signal(signal); }
165 };
166 friend Private;
167};
168
169} // namespace ftxui
170
171#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:33
int dimx() const
Definition image.hpp:32
A rectangular grid of Pixel.
Definition screen.hpp:27
std::chrono::time_point< Clock > TimePoint
Definition animation.hpp:29
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::unique_ptr< ReceiverImpl< T > > Receiver
Definition receiver.hpp:46
std::unique_ptr< SenderImpl< T > > Sender
Definition receiver.hpp:45
std::variant< Event, Closure, AnimationTask > Task
Definition task.hpp:14
std::function< void()> Closure
Definition task.hpp:13
std::shared_ptr< ComponentBase > Component