FTXUI  5.0.0
C++ functional terminal UI.
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 
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 class ScreenInteractive : public Screen {
30  public:
31  // Constructors:
32  static ScreenInteractive FixedSize(int dimx, int dimy);
38 
39  // Options. Must be called before Loop().
40  void TrackMouse(bool enable = true);
41 
42  // Return the currently active screen, nullptr if none.
43  static ScreenInteractive* Active();
44 
45  // Start/Stop the main loop.
46  void Loop(Component);
47  void Exit();
49 
50  // Post tasks to be executed by the loop.
51  void Post(Task task);
52  void PostEvent(Event event);
53  void RequestAnimationFrame();
54 
56 
57  // Decorate a function. The outputted one will execute similarly to the
58  // inputted one, but with the currently active screen terminal hooks
59  // temporarily uninstalled.
61 
62  private:
63  void ExitNow();
64 
65  void Install();
66  void Uninstall();
67 
68  void PreMain();
69  void PostMain();
70 
71  bool HasQuitted();
72  void RunOnce(Component component);
73  void RunOnceBlocking(Component component);
74 
75  void HandleTask(Component component, Task& task);
76  void Draw(Component component);
77  void ResetCursorPosition();
78 
79  void Signal(int signal);
80 
81  ScreenInteractive* suspended_screen_ = nullptr;
82  enum class Dimension {
83  FitComponent,
84  Fixed,
85  Fullscreen,
86  TerminalOutput,
87  };
88  Dimension dimension_ = Dimension::Fixed;
89  bool use_alternative_screen_ = false;
91  int dimy,
92  Dimension dimension,
93  bool use_alternative_screen);
94 
95  bool track_mouse_ = true;
96 
97  Sender<Task> task_sender_;
98  Receiver<Task> task_receiver_;
99 
100  std::string set_cursor_position;
101  std::string reset_cursor_position;
102 
103  std::atomic<bool> quit_ = false;
104  std::thread event_listener_;
105  std::thread animation_listener_;
106  bool animation_requested_ = false;
107  animation::TimePoint previous_animation_time_;
108 
109  int cursor_x_ = 1;
110  int cursor_y_ = 1;
111 
112  bool mouse_captured = false;
113  bool previous_frame_resized_ = false;
114 
115  bool frame_valid_ = false;
116 
117  // The style of the cursor to restore on exit.
118  int cursor_reset_shape_ = 1;
119 
120  friend class Loop;
121 
122  public:
123  class Private {
124  public:
125  static void Signal(ScreenInteractive& s, int signal) { s.Signal(signal); }
126  };
127  friend Private;
128 };
129 
130 } // namespace ftxui
131 
132 #endif /* end of include guard: FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP */
int dimy() const
Definition: image.hpp:35
int dimx() const
Definition: image.hpp:34
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.
static ScreenInteractive FullscreenAlternateScreen()
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:27
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
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