Loading...
Searching...
No Matches
screen_interactive.hpp
Go to the documentation of this file.
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#include <atomic> // for atomic
#include <ftxui/component/receiver.hpp> // for Receiver, Sender
#include <functional> // for function
#include <memory> // for shared_ptr
#include <string> // for string
#include <thread> // for thread
#include <variant> // for variant
#include "ftxui/component/animation.hpp" // for TimePoint
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/event.hpp" // for Event
#include "ftxui/component/task.hpp" // for Task, Closure
#include "ftxui/dom/selection.hpp" // for SelectionOption
#include "ftxui/screen/screen.hpp" // for Screen
namespace ftxui {
class ComponentBase;
class Loop;
struct Event;
using Component = std::shared_ptr<ComponentBase>;
class ScreenInteractivePrivate;
class ScreenInteractive : public Screen {
public:
// Constructors:
static ScreenInteractive FixedSize(int dimx, int dimy);
static ScreenInteractive Fullscreen();
static ScreenInteractive FullscreenPrimaryScreen();
static ScreenInteractive FullscreenAlternateScreen();
static ScreenInteractive FitComponent();
static ScreenInteractive TerminalOutput();
// Options. Must be called before Loop().
void TrackMouse(bool enable = true);
// Return the currently active screen, nullptr if none.
static ScreenInteractive* Active();
// Start/Stop the main loop.
void Loop(Component);
void Exit();
Closure ExitLoopClosure();
// Post tasks to be executed by the loop.
void Post(Task task);
void PostEvent(Event event);
void RequestAnimationFrame();
CapturedMouse CaptureMouse();
// Decorate a function. The outputted one will execute similarly to the
// inputted one, but with the currently active screen terminal hooks
// temporarily uninstalled.
Closure WithRestoredIO(Closure);
// FTXUI implements handlers for Ctrl-C and Ctrl-Z. By default, these handlers
// are executed, even if the component catches the event. This avoid users
// handling every event to be trapped in the application. However, in some
// cases, the application may want to handle these events itself. In this
// case, the application can force FTXUI to not handle these events by calling
// the following functions with force=true.
void ForceHandleCtrlC(bool force);
void ForceHandleCtrlZ(bool force);
// Selection API.
std::string GetSelection();
void SelectionChange(std::function<void()> callback);
private:
void ExitNow();
void Install();
void Uninstall();
void PreMain();
void PostMain();
bool HasQuitted();
void RunOnce(Component component);
void RunOnceBlocking(Component component);
void HandleTask(Component component, Task& task);
bool HandleSelection(bool handled, Event event);
void RefreshSelection();
void Draw(Component component);
void ResetCursorPosition();
void Signal(int signal);
ScreenInteractive* suspended_screen_ = nullptr;
enum class Dimension {
FitComponent,
Fixed,
Fullscreen,
TerminalOutput,
};
Dimension dimension_ = Dimension::Fixed;
bool use_alternative_screen_ = false;
ScreenInteractive(int dimx,
int dimy,
Dimension dimension,
bool use_alternative_screen);
bool track_mouse_ = true;
Sender<Task> task_sender_;
Receiver<Task> task_receiver_;
std::string set_cursor_position;
std::string reset_cursor_position;
std::atomic<bool> quit_{false};
std::thread event_listener_;
std::thread animation_listener_;
bool animation_requested_ = false;
animation::TimePoint previous_animation_time_;
int cursor_x_ = 1;
int cursor_y_ = 1;
bool mouse_captured = false;
bool previous_frame_resized_ = false;
bool frame_valid_ = false;
bool force_handle_ctrl_c_ = true;
bool force_handle_ctrl_z_ = true;
// The style of the cursor to restore on exit.
int cursor_reset_shape_ = 1;
// Selection API:
CapturedMouse selection_pending_;
struct SelectionData {
int start_x = -1;
int start_y = -1;
int end_x = -2;
int end_y = -2;
bool empty = true;
bool operator==(const SelectionData& other) const;
bool operator!=(const SelectionData& other) const;
};
SelectionData selection_data_;
SelectionData selection_data_previous_;
std::unique_ptr<Selection> selection_;
std::function<void()> selection_on_change_;
friend class Loop;
public:
class Private {
public:
static void Signal(ScreenInteractive& s, int signal) { s.Signal(signal); }
};
friend Private;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP */
animation.hpp
captured_mouse.hpp
ftxui::ComponentBase
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Definition component_base.hpp:30
ftxui::Image::dimy
int dimy() const
Definition image.hpp:33
ftxui::Image::dimx
int dimx() const
Definition image.hpp:32
ftxui::Loop
Definition loop.hpp:17
ftxui::ScreenInteractive::Private
Definition screen_interactive.hpp:158
ftxui::ScreenInteractive::Private::Signal
static void Signal(ScreenInteractive &s, int signal)
Definition screen_interactive.hpp:160
ftxui::ScreenInteractive::TerminalOutput
static ScreenInteractive TerminalOutput()
Definition screen_interactive.cpp:406
ftxui::ScreenInteractive::Exit
void Exit()
Exit the main loop.
Definition screen_interactive.cpp:1032
ftxui::ScreenInteractive::FixedSize
static ScreenInteractive FixedSize(int dimx, int dimy)
Definition screen_interactive.cpp:360
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:461
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:448
ftxui::ScreenInteractive::FitComponent
static ScreenInteractive FitComponent()
Definition screen_interactive.cpp:416
ftxui::ScreenInteractive::Fullscreen
static ScreenInteractive Fullscreen()
Definition screen_interactive.cpp:374
ftxui::ScreenInteractive::FullscreenPrimaryScreen
static ScreenInteractive FullscreenPrimaryScreen()
Definition screen_interactive.cpp:383
ftxui::ScreenInteractive::Loop
friend class Loop
Definition screen_interactive.hpp:155
ftxui::ScreenInteractive::Active
static ScreenInteractive * Active()
Return the currently active screen, or null if none.
Definition screen_interactive.cpp:594
ftxui::ScreenInteractive::CaptureMouse
CapturedMouse CaptureMouse()
Try to get the unique lock about behing able to capture the mouse.
Definition screen_interactive.cpp:483
ftxui::ScreenInteractive::GetSelection
std::string GetSelection()
Returns the content of the current selection.
Definition screen_interactive.cpp:581
ftxui::ScreenInteractive::FullscreenAlternateScreen
static ScreenInteractive FullscreenAlternateScreen()
Definition screen_interactive.cpp:396
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:441
ftxui::ScreenInteractive::SelectionChange
void SelectionChange(std::function< void()> callback)
Definition screen_interactive.cpp:588
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:467
ftxui::ScreenInteractive::ExitLoopClosure
Closure ExitLoopClosure()
Return a function to exit the main loop.
Definition screen_interactive.cpp:1026
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:570
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:576
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:560
ftxui::Screen::Screen
Screen(int dimx, int dimy)
Definition screen.cpp:398
event.hpp
ftxui::animation::TimePoint
std::chrono::time_point< Clock > TimePoint
Definition animation.hpp:19
ftxui
Definition animation.hpp:10
ftxui::CapturedMouse
std::unique_ptr< CapturedMouseInterface > CapturedMouse
Definition captured_mouse.hpp:19
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
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:27
task.hpp