Support SIGTSTP and task posting. (#331)

- Add support for SIGTSTP:
https://github.com/ArthurSonzogni/FTXUI/issues/330
This

- Add support for task posting.
This allows folks to defer function execution, and execute it directly
below the main loop. The task are executed in a FIFO order.
This commit is contained in:
Arthur Sonzogni
2022-02-13 11:11:34 +01:00
committed by GitHub
parent 62747a49b6
commit 9c4218c2a8
14 changed files with 165 additions and 94 deletions

View File

@@ -2,7 +2,8 @@
#define FTXUI_COMPONENT_EVENT_HPP
#include <ftxui/component/mouse.hpp> // for Mouse
#include <string> // for string, operator==
#include <functional>
#include <string> // for string, operator==
#include <vector>
namespace ftxui {

View File

@@ -7,9 +7,11 @@
#include <memory> // for shared_ptr
#include <string> // for string
#include <thread> // for thread
#include <variant> // for variant
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/event.hpp" // for Event
#include "ftxui/component/task.hpp" // for Closure, Task
#include "ftxui/screen/screen.hpp" // for Screen
namespace ftxui {
@@ -17,37 +19,38 @@ class ComponentBase;
struct Event;
using Component = std::shared_ptr<ComponentBase>;
class ScreenInteractivePrivate;
class ScreenInteractive : public Screen {
public:
using Callback = std::function<void()>;
static ScreenInteractive FixedSize(int dimx, int dimy);
static ScreenInteractive Fullscreen();
static ScreenInteractive FitComponent();
static ScreenInteractive TerminalOutput();
void Loop(Component);
Callback ExitLoopClosure();
Closure ExitLoopClosure();
void Post(Task task);
void PostEvent(Event event);
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.
Callback WithRestoredIO(Callback);
Closure WithRestoredIO(Closure);
private:
void Install();
void Uninstall();
void Main(Component component);
ScreenInteractive* suspended_screen_ = nullptr;
void Draw(Component component);
void EventLoop(Component component);
void SigStop();
ScreenInteractive* suspended_screen_ = nullptr;
enum class Dimension {
FitComponent,
Fixed,
@@ -61,8 +64,8 @@ class ScreenInteractive : public Screen {
Dimension dimension,
bool use_alternative_screen);
Sender<Event> event_sender_;
Receiver<Event> event_receiver_;
Sender<Task> task_sender_;
Receiver<Task> task_receiver_;
std::string set_cursor_position;
std::string reset_cursor_position;
@@ -75,6 +78,13 @@ class ScreenInteractive : public Screen {
bool mouse_captured = false;
bool previous_frame_resized_ = false;
public:
class Private {
public:
static void SigStop(ScreenInteractive& s) { return s.SigStop(); }
};
friend Private;
};
} // namespace ftxui

View File

@@ -0,0 +1,12 @@
#include <functional>
#include <variant>
#include "ftxui/component/event.hpp"
namespace ftxui {
using Closure = std::function<void()>;
using Task = std::variant<Event, Closure>;
} // namespace ftxui
// Copyright 2022 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.