2023-08-19 19:56:36 +08:00
|
|
|
// 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.
|
2019-01-07 00:10:35 +08:00
|
|
|
#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
|
|
|
|
#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2021-05-15 04:00:49 +08:00
|
|
|
#include <atomic> // for atomic
|
2022-05-08 14:44:38 +08:00
|
|
|
#include <ftxui/component/receiver.hpp> // for Receiver, Sender
|
2021-05-15 04:00:49 +08:00
|
|
|
#include <functional> // for function
|
2022-05-08 14:44:38 +08:00
|
|
|
#include <memory> // for shared_ptr
|
2021-05-15 04:00:49 +08:00
|
|
|
#include <string> // for string
|
2021-09-17 02:45:26 +08:00
|
|
|
#include <thread> // for thread
|
2022-02-13 18:11:34 +08:00
|
|
|
#include <variant> // for variant
|
2019-01-27 09:33:06 +08:00
|
|
|
|
2022-03-14 01:51:46 +08:00
|
|
|
#include "ftxui/component/animation.hpp" // for TimePoint
|
2021-05-02 02:40:35 +08:00
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
2021-07-17 18:02:08 +08:00
|
|
|
#include "ftxui/component/event.hpp" // for Event
|
2022-10-19 03:29:27 +08:00
|
|
|
#include "ftxui/component/task.hpp" // for Task, Closure
|
2021-07-17 18:02:08 +08:00
|
|
|
#include "ftxui/screen/screen.hpp" // for Screen
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2021-05-10 02:32:27 +08:00
|
|
|
class ComponentBase;
|
2022-10-19 03:29:27 +08:00
|
|
|
class Loop;
|
2021-07-17 18:02:08 +08:00
|
|
|
struct Event;
|
2021-05-10 02:32:27 +08:00
|
|
|
|
|
|
|
using Component = std::shared_ptr<ComponentBase>;
|
2022-02-13 18:11:34 +08:00
|
|
|
class ScreenInteractivePrivate;
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
class ScreenInteractive : public Screen {
|
2020-03-24 04:26:00 +08:00
|
|
|
public:
|
2022-03-14 01:51:46 +08:00
|
|
|
// Constructors:
|
2020-03-24 04:26:00 +08:00
|
|
|
static ScreenInteractive FixedSize(int dimx, int dimy);
|
|
|
|
static ScreenInteractive Fullscreen();
|
2023-11-12 00:57:07 +08:00
|
|
|
static ScreenInteractive FullscreenPrimaryScreen();
|
|
|
|
static ScreenInteractive FullscreenAlternateScreen();
|
2020-03-24 04:26:00 +08:00
|
|
|
static ScreenInteractive FitComponent();
|
|
|
|
static ScreenInteractive TerminalOutput();
|
|
|
|
|
2023-08-19 16:57:50 +08:00
|
|
|
// Options. Must be called before Loop().
|
|
|
|
void TrackMouse(bool enable = true);
|
|
|
|
|
2022-03-14 01:51:46 +08:00
|
|
|
// Return the currently active screen, nullptr if none.
|
|
|
|
static ScreenInteractive* Active();
|
|
|
|
|
2022-10-19 03:29:27 +08:00
|
|
|
// Start/Stop the main loop.
|
2021-05-10 02:32:27 +08:00
|
|
|
void Loop(Component);
|
2022-10-19 03:29:27 +08:00
|
|
|
void Exit();
|
2022-02-13 18:11:34 +08:00
|
|
|
Closure ExitLoopClosure();
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2022-10-19 03:29:27 +08:00
|
|
|
// Post tasks to be executed by the loop.
|
2022-02-13 18:11:34 +08:00
|
|
|
void Post(Task task);
|
2020-03-24 04:26:00 +08:00
|
|
|
void PostEvent(Event event);
|
2022-03-14 01:51:46 +08:00
|
|
|
void RequestAnimationFrame();
|
2022-02-13 18:11:34 +08:00
|
|
|
|
2021-05-02 00:13:56 +08:00
|
|
|
CapturedMouse CaptureMouse();
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2022-01-19 20:38:39 +08:00
|
|
|
// Decorate a function. The outputted one will execute similarly to the
|
|
|
|
// inputted one, but with the currently active screen terminal hooks
|
|
|
|
// temporarily uninstalled.
|
2022-02-13 18:11:34 +08:00
|
|
|
Closure WithRestoredIO(Closure);
|
2022-01-19 20:38:39 +08:00
|
|
|
|
2024-04-28 21:17:54 +08:00
|
|
|
// 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);
|
|
|
|
|
2024-08-31 21:39:42 +08:00
|
|
|
std::string GetSelection();
|
2024-08-27 18:40:48 +08:00
|
|
|
|
2020-03-24 04:26:00 +08:00
|
|
|
private:
|
2022-12-02 05:56:35 +08:00
|
|
|
void ExitNow();
|
|
|
|
|
2021-09-01 23:47:48 +08:00
|
|
|
void Install();
|
|
|
|
void Uninstall();
|
2022-01-19 20:38:39 +08:00
|
|
|
|
2022-10-19 03:29:27 +08:00
|
|
|
void PreMain();
|
|
|
|
void PostMain();
|
2021-09-01 23:47:48 +08:00
|
|
|
|
2022-10-19 03:29:27 +08:00
|
|
|
bool HasQuitted();
|
|
|
|
void RunOnce(Component component);
|
|
|
|
void RunOnceBlocking(Component component);
|
|
|
|
|
|
|
|
void HandleTask(Component component, Task& task);
|
2024-08-31 21:39:42 +08:00
|
|
|
bool HandleSelection(Event event);
|
|
|
|
void RefreshSelection();
|
2021-05-10 02:32:27 +08:00
|
|
|
void Draw(Component component);
|
2022-12-02 05:56:35 +08:00
|
|
|
void ResetCursorPosition();
|
2022-10-19 03:29:27 +08:00
|
|
|
|
2022-12-02 05:56:35 +08:00
|
|
|
void Signal(int signal);
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2022-02-13 18:11:34 +08:00
|
|
|
ScreenInteractive* suspended_screen_ = nullptr;
|
2020-03-24 04:26:00 +08:00
|
|
|
enum class Dimension {
|
|
|
|
FitComponent,
|
|
|
|
Fixed,
|
|
|
|
Fullscreen,
|
|
|
|
TerminalOutput,
|
|
|
|
};
|
|
|
|
Dimension dimension_ = Dimension::Fixed;
|
2020-05-03 02:39:56 +08:00
|
|
|
bool use_alternative_screen_ = false;
|
|
|
|
ScreenInteractive(int dimx,
|
|
|
|
int dimy,
|
|
|
|
Dimension dimension,
|
|
|
|
bool use_alternative_screen);
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2023-08-19 19:56:36 +08:00
|
|
|
bool track_mouse_ = true;
|
2023-08-19 16:57:50 +08:00
|
|
|
|
2022-02-13 18:11:34 +08:00
|
|
|
Sender<Task> task_sender_;
|
|
|
|
Receiver<Task> task_receiver_;
|
2020-03-24 04:26:00 +08:00
|
|
|
|
|
|
|
std::string set_cursor_position;
|
|
|
|
std::string reset_cursor_position;
|
2020-03-25 06:26:55 +08:00
|
|
|
|
2024-05-26 21:28:05 +08:00
|
|
|
std::atomic<bool> quit_{false};
|
2021-09-01 23:47:48 +08:00
|
|
|
std::thread event_listener_;
|
2022-03-14 01:51:46 +08:00
|
|
|
std::thread animation_listener_;
|
2022-03-26 14:55:52 +08:00
|
|
|
bool animation_requested_ = false;
|
2022-10-19 03:29:27 +08:00
|
|
|
animation::TimePoint previous_animation_time_;
|
2021-04-25 00:16:13 +08:00
|
|
|
|
2021-07-04 23:38:31 +08:00
|
|
|
int cursor_x_ = 1;
|
|
|
|
int cursor_y_ = 1;
|
2021-05-02 00:13:56 +08:00
|
|
|
|
|
|
|
bool mouse_captured = false;
|
2022-01-12 06:06:36 +08:00
|
|
|
bool previous_frame_resized_ = false;
|
2022-02-13 18:11:34 +08:00
|
|
|
|
2022-10-19 03:29:27 +08:00
|
|
|
bool frame_valid_ = false;
|
|
|
|
|
2024-04-28 21:17:54 +08:00
|
|
|
bool force_handle_ctrl_c_ = true;
|
|
|
|
bool force_handle_ctrl_z_ = true;
|
|
|
|
|
2023-12-17 17:24:33 +08:00
|
|
|
// The style of the cursor to restore on exit.
|
|
|
|
int cursor_reset_shape_ = 1;
|
|
|
|
|
2022-10-19 03:29:27 +08:00
|
|
|
friend class Loop;
|
|
|
|
|
2022-02-13 18:11:34 +08:00
|
|
|
public:
|
|
|
|
class Private {
|
|
|
|
public:
|
2022-12-02 05:56:35 +08:00
|
|
|
static void Signal(ScreenInteractive& s, int signal) { s.Signal(signal); }
|
2022-02-13 18:11:34 +08:00
|
|
|
};
|
|
|
|
friend Private;
|
2018-10-10 01:06:03 +08:00
|
|
|
};
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-07 00:10:35 +08:00
|
|
|
#endif /* end of include guard: FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP */
|