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