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/screen/screen.hpp" // for Screen
20
21namespace ftxui {
22class ComponentBase;
23class Loop;
24struct Event;
25
26using Component = std::shared_ptr<ComponentBase>;
28
29class ScreenInteractive : public Screen {
30 public:
31 // Constructors:
32 static ScreenInteractive FixedSize(int dimx, int dimy);
36
37 // Options. Must be called before Loop().
38 void TrackMouse(bool enable = true);
39
40 // Return the currently active screen, nullptr if none.
41 static ScreenInteractive* Active();
42
43 // Start/Stop the main loop.
44 void Loop(Component);
45 void Exit();
47
48 // Post tasks to be executed by the loop.
49 void Post(Task task);
50 void PostEvent(Event event);
52
54
55 // Decorate a function. The outputted one will execute similarly to the
56 // inputted one, but with the currently active screen terminal hooks
57 // temporarily uninstalled.
59
60 private:
61 void ExitNow();
62
63 void Install();
64 void Uninstall();
65
66 void PreMain();
67 void PostMain();
68
69 bool HasQuitted();
70 void RunOnce(Component component);
71 void RunOnceBlocking(Component component);
72
73 void HandleTask(Component component, Task& task);
74 void Draw(Component component);
75 void ResetCursorPosition();
76
77 void Signal(int signal);
78
79 ScreenInteractive* suspended_screen_ = nullptr;
80 enum class Dimension {
82 Fixed,
85 };
86 Dimension dimension_ = Dimension::Fixed;
87 bool use_alternative_screen_ = false;
89 int dimy,
90 Dimension dimension,
92
93 bool track_mouse_ = true;
94
95 Sender<Task> task_sender_;
96 Receiver<Task> task_receiver_;
97
98 std::string set_cursor_position;
99 std::string reset_cursor_position;
100
101 std::atomic<bool> quit_ = false;
102 std::thread event_listener_;
103 std::thread animation_listener_;
104 bool animation_requested_ = false;
105 animation::TimePoint previous_animation_time_;
106
107 int cursor_x_ = 1;
108 int cursor_y_ = 1;
109
110 bool mouse_captured = false;
111 bool previous_frame_resized_ = false;
112
113 bool frame_valid_ = false;
114
115 friend class Loop;
116
117 public:
118 class Private {
119 public:
120 static void Signal(ScreenInteractive& s, int signal) { s.Signal(signal); }
121 };
122 friend Private;
123};
124
125} // namespace ftxui
126
127#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 * 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.
void TrackMouse(bool enable=true)
Set whether mouse is tracked and events reported. called outside of the main loop....
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.
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:63
int dimy() const
Definition screen.hpp:85
int dimx() const
Definition screen.hpp:84
std::chrono::time_point< Clock > TimePoint
Definition animation.hpp:23
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< ComponentBase > Component
std::unique_ptr< ReceiverImpl< T > > Receiver
Definition receiver.hpp:48
std::unique_ptr< SenderImpl< T > > Sender
Definition receiver.hpp:47
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition slider.cpp:339
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:29