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
14#include "ftxui/component/animation.hpp" // for TimePoint
15#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
16#include "ftxui/component/event.hpp" // for Event
17#include "ftxui/component/task.hpp" // for Task, Closure
18#include "ftxui/dom/selection.hpp" // for SelectionOption
19#include "ftxui/screen/screen.hpp" // for Screen
20
21 namespace ftxui {
22 class ComponentBase;
23 class Loop;
24 struct Event;
25
26 using Component = std::shared_ptr<ComponentBase>;
27 class ScreenInteractivePrivate;
28
29/// @brief ScreenInteractive is a `Screen` that can handle events, run a main
30/// loop, and manage components.
31///
32/// @ingroup component
33 class ScreenInteractive : public Screen {
34 public:
35 // Constructors:
36 static ScreenInteractive FixedSize(int dimx, int dimy);
37 static ScreenInteractive Fullscreen();
38 static ScreenInteractive FullscreenPrimaryScreen();
39 static ScreenInteractive FullscreenAlternateScreen();
40 static ScreenInteractive FitComponent();
41 static ScreenInteractive TerminalOutput();
42
43 // Options. Must be called before Loop().
44 void TrackMouse(bool enable = true);
45
46 // Return the currently active screen, nullptr if none.
47 static ScreenInteractive* Active();
48
49 // Start/Stop the main loop.
50 void Loop(Component);
51 void Exit();
52 Closure ExitLoopClosure();
53
54 // Post tasks to be executed by the loop.
55 void Post(Task task);
56 void PostEvent(Event event);
57 void RequestAnimationFrame();
58
59 CapturedMouse CaptureMouse();
60
61 // Decorate a function. The outputted one will execute similarly to the
62 // inputted one, but with the currently active screen terminal hooks
63 // temporarily uninstalled.
64 Closure WithRestoredIO(Closure);
65
66 // FTXUI implements handlers for Ctrl-C and Ctrl-Z. By default, these handlers
67 // are executed, even if the component catches the event. This avoid users
68 // handling every event to be trapped in the application. However, in some
69 // cases, the application may want to handle these events itself. In this
70 // case, the application can force FTXUI to not handle these events by calling
71 // the following functions with force=true.
72 void ForceHandleCtrlC(bool force);
73 void ForceHandleCtrlZ(bool force);
74
75 // Selection API.
76 std::string GetSelection();
77 void SelectionChange(std::function<void()> callback);
78
79 private:
80 void ExitNow();
81
82 void Install();
83 void Uninstall();
84
85 void PreMain();
86 void PostMain();
87
88 bool HasQuitted();
89 void RunOnce(Component component);
90 void RunOnceBlocking(Component component);
91
92 void HandleTask(Component component, Task& task);
93 bool HandleSelection(bool handled, Event event);
94 void RefreshSelection();
95 void Draw(Component component);
96 void ResetCursorPosition();
97
98 void Signal(int signal);
99
100 ScreenInteractive* suspended_screen_ = nullptr;
101 enum class Dimension {
102 FitComponent,
103 Fixed,
104 Fullscreen,
105 TerminalOutput,
106 };
107 ScreenInteractive(Dimension dimension,
108 int dimx,
109 int dimy,
110 bool use_alternative_screen);
111 const Dimension dimension_;
112 const bool use_alternative_screen_;
113
114 bool track_mouse_ = true;
115
116 Sender<Task> task_sender_;
117 Receiver<Task> task_receiver_;
118
119 std::string set_cursor_position;
120 std::string reset_cursor_position;
121
122 std::atomic<bool> quit_{false};
123 std::thread event_listener_;
124 std::thread animation_listener_;
125 bool animation_requested_ = false;
126 animation::TimePoint previous_animation_time_;
127
128 int cursor_x_ = 1;
129 int cursor_y_ = 1;
130
131 std::uint64_t frame_count_ = 0;
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 */
animation.hpp
captured_mouse.hpp
ftxui::ScreenInteractive::Private
Definition screen_interactive.hpp:162
ftxui::ScreenInteractive::Private::Signal
static void Signal(ScreenInteractive &s, int signal)
Definition screen_interactive.hpp:164
event.hpp
ftxui::ScreenInteractive::TerminalOutput
static ScreenInteractive TerminalOutput()
Definition screen_interactive.cpp:410
ftxui::ScreenInteractive::Exit
void Exit()
Exit the main loop.
Definition screen_interactive.cpp:1033
ftxui::ScreenInteractive::FixedSize
static ScreenInteractive FixedSize(int dimx, int dimy)
Definition screen_interactive.cpp:363
ftxui::ScreenInteractive::PostEvent
void PostEvent(Event event)
Add an event to the main loop. It will be executed later, after every other scheduled events.
Definition screen_interactive.cpp:466
ftxui::ScreenInteractive::Post
void Post(Task task)
Add a task to the main loop. It will be executed later, after every other scheduled tasks.
Definition screen_interactive.cpp:454
ftxui::ScreenInteractive::FitComponent
static ScreenInteractive FitComponent()
Definition screen_interactive.cpp:423
ftxui::ScreenInteractive::Fullscreen
static ScreenInteractive Fullscreen()
Definition screen_interactive.cpp:376
ftxui::ScreenInteractive::Private
friend Private
Definition screen_interactive.hpp:166
ftxui::ScreenInteractive::FullscreenPrimaryScreen
static ScreenInteractive FullscreenPrimaryScreen()
Definition screen_interactive.cpp:384
ftxui::ScreenInteractive::Loop
friend class Loop
Definition screen_interactive.hpp:159
ftxui::ScreenInteractive::Active
static ScreenInteractive * Active()
Return the currently active screen, or null if none.
Definition screen_interactive.cpp:596
ftxui::ScreenInteractive::CaptureMouse
CapturedMouse CaptureMouse()
Try to get the unique lock about behing able to capture the mouse.
Definition screen_interactive.cpp:487
ftxui::ScreenInteractive::GetSelection
std::string GetSelection()
Returns the content of the current selection.
Definition screen_interactive.cpp:583
ftxui::ScreenInteractive::FullscreenAlternateScreen
static ScreenInteractive FullscreenAlternateScreen()
Definition screen_interactive.cpp:397
ftxui::ScreenInteractive::TrackMouse
void TrackMouse(bool enable=true)
Set whether mouse is tracked and events reported. called outside of the main loop....
Definition screen_interactive.cpp:448
ftxui::ScreenInteractive::SelectionChange
void SelectionChange(std::function< void()> callback)
Definition screen_interactive.cpp:590
ftxui::ScreenInteractive::RequestAnimationFrame
void RequestAnimationFrame()
Add a task to draw the screen one more time, until all the animations are done.
Definition screen_interactive.cpp:472
ftxui::ScreenInteractive::ExitLoopClosure
Closure ExitLoopClosure()
Return a function to exit the main loop.
Definition screen_interactive.cpp:1028
ftxui::ScreenInteractive::ForceHandleCtrlC
void ForceHandleCtrlC(bool force)
Force FTXUI to handle or not handle Ctrl-C, even if the component catches the Event::CtrlC.
Definition screen_interactive.cpp:572
ftxui::ScreenInteractive::ForceHandleCtrlZ
void ForceHandleCtrlZ(bool force)
Force FTXUI to handle or not handle Ctrl-Z, even if the component catches the Event::CtrlZ.
Definition screen_interactive.cpp:578
ftxui::ScreenInteractive::WithRestoredIO
Closure WithRestoredIO(Closure)
Decorate a function. It executes the same way, but with the currently active screen terminal hooks te...
Definition screen_interactive.cpp:562
ftxui::Loop
Loop is a class that manages the event loop for a component.
Definition loop.hpp:56
ftxui::ScreenInteractive
ScreenInteractive is a Screen that can handle events, run a main loop, and manage components.
Definition screen_interactive.hpp:33
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
ftxui::Image::dimy
int dimy() const
Definition image.hpp:33
ftxui::Image::dimx
int dimx() const
Definition image.hpp:32
ftxui::Screen
A rectangular grid of Pixel.
Definition screen.hpp:27
ftxui::animation::TimePoint
std::chrono::time_point< Clock > TimePoint
Definition animation.hpp:29
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::CapturedMouse
std::unique_ptr< CapturedMouseInterface > CapturedMouse
Definition captured_mouse.hpp:20
ftxui::Receiver
std::unique_ptr< ReceiverImpl< T > > Receiver
Definition receiver.hpp:46
ftxui::Sender
std::unique_ptr< SenderImpl< T > > Sender
Definition receiver.hpp:45
ftxui::Task
std::variant< Event, Closure, AnimationTask > Task
Definition task.hpp:14
ftxui::Closure
std::function< void()> Closure
Definition task.hpp:13
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
receiver.hpp
screen.hpp
selection.hpp
task.hpp