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. Todos los derechos reservados.
2// El uso de este código fuente se rige por la licencia MIT que se puede encontrar en
3// el archivo LICENSE.
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 es una `Screen` que puede manejar eventos, ejecutar un
32/// bucle principal y administrar componentes.
33///
34/// @ingroup component
35class ScreenInteractive : public Screen {
36 public:
37 // Constructores:
38 static ScreenInteractive FixedSize(int dimx, int dimy);
44
45 // Destructor.
47
48 // Opciones. Debe ser llamado antes de Loop().
49 void TrackMouse(bool enable = true);
50 void HandlePipedInput(bool enable = true);
51
52 // Devuelve la pantalla activa actualmente, nullptr si no hay ninguna.
53 static ScreenInteractive* Active();
54
55 // Iniciar/Detener el bucle principal.
56 void Loop(Component);
57 void Exit();
59
60 // Publica tareas para ser ejecutadas por el bucle.
61 void Post(Task task);
62 void PostEvent(Event event);
64
66
67 // Decora una función. La función resultante se ejecutará de manera similar a la
68 // función de entrada, pero con los hooks de terminal de la pantalla activa
69 // temporalmente desinstalados.
71
72 // FTXUI implementa manejadores para Ctrl-C y Ctrl-Z. Por defecto, estos
73 // manejadores se ejecutan, incluso si el componente captura el evento. Esto
74 // evita que los usuarios que manejan cada evento queden atrapados en la
75 // aplicación. Sin embargo, en algunos casos, la aplicación puede querer
76 // manejar estos eventos por sí misma. En este caso, la aplicación puede
77 // forzar a FTXUI a no manejar estos eventos llamando a las siguientes
78 // funciones con force=true.
79 void ForceHandleCtrlC(bool force);
80 void ForceHandleCtrlZ(bool force);
81
82 // API de selección.
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 void RunOnce(Component component);
97 void RunOnceBlocking(Component component);
98
99 void HandleTask(Component component, Task& task);
100 bool HandleSelection(bool handled, Event event);
101 void RefreshSelection();
102 void Draw(Component component);
103 void ResetCursorPosition();
104
105 void InstallPipedInputHandling();
106
107 void Signal(int signal);
108
109 void FetchTerminalEvents();
110
111 void PostAnimationTask();
112
113 ScreenInteractive* suspended_screen_ = nullptr;
114 enum class Dimension {
116 Fixed,
119 };
121 int dimx,
122 int dimy,
123 bool use_alternative_screen);
124
125 const Dimension dimension_;
126 const bool use_alternative_screen_;
127
128 bool track_mouse_ = true;
129
130 std::string set_cursor_position;
131 std::string reset_cursor_position;
132
133 std::atomic<bool> quit_{false};
134 bool animation_requested_ = false;
135 animation::TimePoint previous_animation_time_;
136
137 int cursor_x_ = 1;
138 int cursor_y_ = 1;
139
140 std::uint64_t frame_count_ = 0;
141 bool mouse_captured = false;
142 bool previous_frame_resized_ = false;
143
144 bool frame_valid_ = false;
145
146 bool force_handle_ctrl_c_ = true;
147 bool force_handle_ctrl_z_ = true;
148
149 // Estado del manejo de entrada por tubería (solo POSIX)
150 bool handle_piped_input_ = true;
151 // Descriptor de archivo para /dev/tty, usado para el manejo de entrada por tubería.
152 int tty_fd_ = -1;
153
154 // El estilo del cursor a restaurar al salir.
155 int cursor_reset_shape_ = 1;
156
157 // API de selección:
158 CapturedMouse selection_pending_;
159 struct SelectionData {
160 int start_x = -1;
161 int start_y = -1;
162 int end_x = -2;
163 int end_y = -2;
164 bool empty = true;
165 bool operator==(const SelectionData& other) const;
166 bool operator!=(const SelectionData& other) const;
167 };
168 SelectionData selection_data_;
169 SelectionData selection_data_previous_;
170 std::unique_ptr<Selection> selection_;
171 std::function<void()> selection_on_change_;
172
173 // Idioma de implementación privada PIMPL (Pimpl).
174 struct Internal;
175 std::unique_ptr<Internal> internal_;
176
177 friend class Loop;
178
179 Component component_;
180
181 public:
182 class Private {
183 public:
184 static void Signal(ScreenInteractive& s, int signal) { s.Signal(signal); }
185 };
186 friend Private;
187};
188
189} // namespace ftxui
190
191#endif /* end of include guard: FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP */
static void Signal(ScreenInteractive &s, int signal)
screen Loop(composition)
static ScreenInteractive TerminalOutput()
void HandlePipedInput(bool enable=true)
Habilita o deshabilita el manejo automático de entrada por tubería. Cuando está habilitado,...
void Exit()
Sale del bucle principal.
static ScreenInteractive FixedSize(int dimx, int dimy)
void PostEvent(Event event)
Añade un evento al bucle principal. Se ejecutará más tarde, después de todos los demás eventos progra...
void Post(Task task)
Añade una tarea al bucle principal. Se ejecutará más tarde, después de todas las demás tareas program...
static ScreenInteractive FitComponent()
static ScreenInteractive Fullscreen()
static ScreenInteractive FullscreenPrimaryScreen()
static ScreenInteractive * Active()
Devuelve la pantalla actualmente activa, o nulo si no hay ninguna.
CapturedMouse CaptureMouse()
Intenta obtener el bloqueo único para poder capturar el ratón.
std::string GetSelection()
Devuelve el contenido de la selección actual.
static ScreenInteractive FullscreenAlternateScreen()
void TrackMouse(bool enable=true)
Establece si el ratón es rastreado y se informan los eventos. se llama fuera del bucle principal....
void SelectionChange(std::function< void()> callback)
void RequestAnimationFrame()
Añade una tarea para dibujar la pantalla una vez más, hasta que todas las animaciones hayan terminado...
Closure ExitLoopClosure()
Devuelve una función para salir del bucle principal.
void ForceHandleCtrlC(bool force)
Fuerza a FTXUI a manejar o no Ctrl-C, incluso si el componente captura el Event::CtrlC.
void ForceHandleCtrlZ(bool force)
Fuerza a FTXUI a manejar o no Ctrl-Z, incluso si el componente captura el Event::CtrlZ.
Closure WithRestoredIO(Closure)
Decora una función. Se ejecuta de la misma manera, pero con los hooks del terminal de la pantalla act...
Loop es una clase que gestiona el bucle de eventos de un componente.
Definition loop.hpp:56
ScreenInteractive es una Screen que puede manejar eventos, ejecutar un bucle principal y administrar ...
Representa un evento. Puede ser un evento de pulsación de tecla, un redimensionamiento de terminal,...
Definition event.hpp:29
int dimy() const
Definition image.hpp:36
int dimx() const
Definition image.hpp:35
Una cuadrícula rectangular de píxeles.
Definition screen.hpp:26
El espacio de nombres ftxui::Dimension:: de FTXUI.
std::chrono::time_point< Clock > TimePoint
Definition animation.hpp:28
El espacio de nombres ftxui:: de FTXUI.
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