Add support for nxxm.

[nxxm](https://nxxm.github.io)
This commit is contained in:
ArthurSonzogni
2019-02-02 01:59:48 +01:00
parent 2eddd0fa17
commit ef0de8d873
72 changed files with 309 additions and 165 deletions

View File

@@ -0,0 +1,39 @@
#ifndef FTXUI_COMPONENT_CHECKBOX_HPP
#define FTXUI_COMPONENT_CHECKBOX_HPP
#include "ftxui/component/component.hpp"
#include <functional>
namespace ftxui {
class CheckBox : public Component {
public:
// Constructor.
CheckBox() = default;
~CheckBox() override = default;
bool state = false;
std::wstring label = L"label";
//std::wstring checked = L"[X] ";
//std::wstring unchecked = L"[ ] ";
std::wstring checked = L"";
std::wstring unchecked = L"";
Decorator focused_style = inverted;
Decorator unfocused_style = nothing;
// State update callback.
std::function<void()> on_change = [](){};
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
private:
int cursor_position = 0;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_CHECKBOX_HPP */

View File

@@ -0,0 +1,56 @@
#ifndef FTXUI_COMPONENT_COMPONENT_HPP
#define FTXUI_COMPONENT_COMPONENT_HPP
#include "ftxui/component/event.hpp"
#include "ftxui/dom/elements.hpp"
namespace ftxui {
class Delegate;
class Focus;
class Component {
public:
// Constructor/Destructor.
Component() = default;
virtual ~Component();
// Component hierarchy.
Component* Parent() { return parent_; }
void Add(Component* children);
// Renders the component.
virtual Element Render();
// Handles an event.
// By default, reduce on children with a lazy OR.
//
// Returns whether the event was handled or not.
virtual bool OnEvent(Event);
// Focus management ----------------------------------------------------------
//
// If this component contains children, this indicates which one is active,
// nullptr if none is active.
//
// We say an element has the focus if the chain of ActiveChild() from the
// root component contains this object.
virtual Component* ActiveChild();
// Whether this is the active child of its parent.
bool Active();
// Whether all the ancestors are active.
bool Focused();
private:
Component* parent_ = nullptr;
void Detach();
void Attach(Component* parent);
protected:
std::vector<Component*> children_;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HPP */

View File

@@ -0,0 +1,45 @@
#ifndef FTXUI_COMPONENT_CONTAINER_HPP
#define FTXUI_COMPONENT_CONTAINER_HPP
#include "ftxui/component/component.hpp"
namespace ftxui {
// A component where focus and events are automatically handled for you.
// List of container:
//
// Please use HorizontalContainer or VerticalContainer.
class Container : public Component {
public:
static Container Vertical();
static Container Horizontal();
static Container Tab(int* selector);
~Container() override = default;
// Component override.
bool OnEvent(Event event) override;
Element Render() override;
Component* ActiveChild() override;
protected:
// Handlers
using EventHandler = bool (Container::*)(Event);
bool VerticalEvent(Event event);
bool HorizontalEvent(Event event);
bool TabEvent(Event event) { return false; }
EventHandler event_handler_;
using RenderHandler = Element (Container::*)();
Element VerticalRender();
Element HorizontalRender();
Element TabRender();
RenderHandler render_handler_;
int selected_ = 0;
int* selector_ = &selected_;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_CONTAINER_HPP */

View File

@@ -0,0 +1,41 @@
#ifndef FTXUI_COMPONENT_EVENT_HPP
#define FTXUI_COMPONENT_EVENT_HPP
#include <vector>
#include <array>
namespace ftxui {
struct Event{
public:
// --- Character ---
static Event Character(int);
// --- Arrow ---
static Event ArrowLeft;
static Event ArrowRight;
static Event ArrowUp;
static Event ArrowDown;
// --- Other ---
static Event Backspace;
static Event Delete;
static Event Return;
static Event Escape;
static Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
// --- Custom ---
static Event Custom;
bool operator==(const Event& other) { return values == other.values; }
// Internal representation.
std::array<int, 5> values = {0, 0, 0, 0, 0};
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_EVENT_HPP */

View File

@@ -0,0 +1,33 @@
#ifndef FTXUI_COMPONENT_INPUT_H_
#define FTXUI_COMPONENT_INPUT_H_
#include "ftxui/component/component.hpp"
#include <functional>
namespace ftxui {
class Input : public Component {
public:
// Constructor.
Input() = default;
~Input() override = default;
// State.
std::wstring content;
std::wstring placeholder;
// State update callback.
std::function<void()> on_change = [](){};
std::function<void()> on_enter = [](){};
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
private:
int cursor_position = 0;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_INPUT_H_ */

View File

@@ -0,0 +1,35 @@
#ifndef FTXUI_COMPONENT_MENU
#define FTXUI_COMPONENT_MENU
#include "ftxui/component/component.hpp"
#include "ftxui/dom/elements.hpp"
#include <functional>
namespace ftxui {
class Menu : public Component {
public:
// Constructor.
Menu() = default;
~Menu() override = default;
// State.
std::vector<std::wstring> entries = {};
int selected = 0;
Decorator focused_style = inverted;
Decorator selected_style = bold;
Decorator normal_style = nothing;
// State update callback.
std::function<void()> on_change = [](){};
std::function<void()> on_enter = [](){};
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
};
} // namespace ftxui::Component
#endif /* end of include guard: FTXUI_COMPONENT_MENU */

View File

@@ -0,0 +1,38 @@
#ifndef FTXUI_COMPONENT_RADIOBOX_HPP
#define FTXUI_COMPONENT_RADIOBOX_HPP
#include "ftxui/component/component.hpp"
#include <functional>
namespace ftxui {
class RadioBox : public Component {
public:
// Constructor.
RadioBox() = default;
~RadioBox() override = default;
int selected = 0;
int focused = 0;
std::vector<std::wstring> entries;
std::wstring checked = L"";
std::wstring unchecked = L"";
Decorator focused_style = inverted;
Decorator unfocused_style = nothing;
// State update callback.
std::function<void()> on_change = [](){};
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
private:
int cursor_position = 0;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_RADIOBOX_HPP */

View File

@@ -0,0 +1,51 @@
#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <queue>
#include <atomic>
#include "ftxui/component/event.hpp"
#include "ftxui/screen/screen.hpp"
namespace ftxui {
class Component;
class ScreenInteractive : public Screen {
public:
static ScreenInteractive FixedSize(int dimx, int dimy);
static ScreenInteractive Fullscreen();
static ScreenInteractive FitComponent();
static ScreenInteractive TerminalOutput();
~ScreenInteractive();
void Loop(Component*);
std::function<void()> ExitLoopClosure();
void PostEvent(Event event);
private:
void Draw(Component* component);
void EventLoop(Component* component);
enum class Dimension {
FitComponent,
Fixed,
Fullscreen,
TerminalOutput,
};
Dimension dimension_ = Dimension::Fixed;
ScreenInteractive(int dimx, int dimy, Dimension dimension);
std::condition_variable events_queue_wait;
std::mutex events_queue_mutex;
std::queue<Event> events_queue;
std::atomic<bool> quit_ = false;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP */

View File

@@ -0,0 +1,33 @@
#ifndef FTXUI_COMPONENT_TOGGLE_H_
#define FTXUI_COMPONENT_TOGGLE_H_
#include "ftxui/component/component.hpp"
#include <functional>
#include <string>
namespace ftxui {
class Toggle : public Component {
public:
// Constructor.
~Toggle() override = default;
// State.
int selected = 0;
std::vector<std::wstring> entries = {L"On", L"Off"};
Decorator focused_style = inverted;
Decorator selected_style = bold;
Decorator normal_style = dim;
// Callback.
std::function<void()> on_change = [](){};
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
};
} // namespace ftxui::Component
#endif /* end of include guard: FTXUI_COMPONENT_TOGGLE_H_ */