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  // FTXUI implements handlers for Ctrl-C and Ctrl-Z. By default, these handlers
63  // are executed, even if the component catches the event. This avoid users
64  // handling every event to be trapped in the application. However, in some
65  // cases, the application may want to handle these events itself. In this
66  // case, the application can force FTXUI to not handle these events by calling
67  // the following functions with force=true.
68  void ForceHandleCtrlC(bool force);
69  void ForceHandleCtrlZ(bool force);
70 
71  private:
72  void ExitNow();
73 
74  void Install();
75  void Uninstall();
76 
77  void PreMain();
78  void PostMain();
79 
80  bool HasQuitted();
81  void RunOnce(Component component);
82  void RunOnceBlocking(Component component);
83 
84  void HandleTask(Component component, Task& task);
85  void Draw(Component component);
86  void ResetCursorPosition();
87 
88  void Signal(int signal);
89 
90  ScreenInteractive* suspended_screen_ = nullptr;
91  enum class Dimension {
92  FitComponent,
93  Fixed,
94  Fullscreen,
95  TerminalOutput,
96  };
97  Dimension dimension_ = Dimension::Fixed;
98  bool use_alternative_screen_ = false;
100  int dimy,
101  Dimension dimension,
102  bool use_alternative_screen);
103 
104  bool track_mouse_ = true;
105 
106  Sender<Task> task_sender_;
107  Receiver<Task> task_receiver_;
108 
109  std::string set_cursor_position;
110  std::string reset_cursor_position;
111 
112  std::atomic<bool> quit_{false};
113  std::thread event_listener_;
114  std::thread animation_listener_;
115  bool animation_requested_ = false;
116  animation::TimePoint previous_animation_time_;
117 
118  int cursor_x_ = 1;
119  int cursor_y_ = 1;
120 
121  bool mouse_captured = false;
122  bool previous_frame_resized_ = false;
123 
124  bool frame_valid_ = false;
125 
126  bool force_handle_ctrl_c_ = true;
127  bool force_handle_ctrl_z_ = true;
128 
129  // The style of the cursor to restore on exit.
130  int cursor_reset_shape_ = 1;
131 
132  friend class Loop;
133 
134  public:
135  class Private {
136  public:
137  static void Signal(ScreenInteractive& s, int signal) { s.Signal(signal); }
138  };
139  friend Private;
140 };
141 
142 } // namespace ftxui
143 
144 #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.
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.
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:25
std::chrono::time_point< Clock > TimePoint
Definition: animation.hpp:19
std::unique_ptr< CapturedMouseInterface > CapturedMouse
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