Implement a lot of new features.

This commit deserve to be cut into at least 8 sub commit. Sorry, I
acknowledge this is bad... Here are the new features:

 * dom decorator: bold, dim, underlined, inverted.
 * component mechanism
 * components
   * menu
   * toogle
This commit is contained in:
Arthur Sonzogni
2018-10-09 19:06:03 +02:00
parent dd92b89611
commit 711b71688e
63 changed files with 1590 additions and 260 deletions

View File

@@ -0,0 +1,34 @@
#ifndef FTXUI_COMPONENT_EVENT
#define FTXUI_COMPONENT_EVENT
namespace ftxui {
namespace component {
struct Event{
// --- Character ---
static Event Character(char);
// --- Arrow ---
static Event Arrow_Left;
static Event Arrow_Right;
static Event Arrow_Up;
static Event Arrow_Down;
// --- Other ---
static Event Backspace;
static Event Delete;
static Event Escape;
static Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
// Internal representation.
int values [3];
Event(int values[3]) : values(values);
};
} // namespace component
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_EVENT */

View File

@@ -0,0 +1,44 @@
#ifndef FTXUI_COMPONENT_COMPONENT_HPP
#define FTXUI_COMPONENT_COMPONENT_HPP
#include "ftxui/dom/elements.hpp"
#include "ftxui/component/delegate.hpp"
namespace ftxui {
namespace component {
class Delegate;
class Focus;
class Component {
public:
// Constructor/Destructor.
Component(Delegate* delegate);
virtual ~Component();
// Render the component.
virtual dom::Element Render();
// Handle an event. By default, it calls this function on each children.
virtual bool Event(int key);
// If this component contains children, this indicates which one is active. It
// can be none of them.
// We say an element has the focus if the chain of GetActiveChild() from the
// root component contains this object.
virtual Component* GetActiveChild() { return nullptr; }
bool Active(); // True is this component is an active child.
bool Focused(); // True if all the ancestors are active childs.
Component* Parent();
Component* PreviousSibling();
Component* NextSibling();
private:
Delegate* delegate_;
};
} // namespace component
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HPP */

View File

@@ -0,0 +1,25 @@
#ifndef FTXUI_COMPONENT_COMPONENT_DIRECTION_H_
#define FTXUI_COMPONENT_COMPONENT_DIRECTION_H_
#include "ftxui/component/component.hpp"
namespace ftxui {
namespace component {
// A component where focus and events are automatically handled for you.
class ComponentDirection : public Component {
public:
ComponentDirection(Delegate* delegate);
bool Event(int key) override;
Component* GetActiveChild() override;
protected:
void Focus(Component* child);
virtual bool HandleDirection(int key) = 0;
Component* active_child_;
};
} // namespace component
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_DIRECTION_H_ */

View File

@@ -0,0 +1,20 @@
#ifndef FTXUI_COMPONENT_COMPONENT_HORIZONTAL_H_
#define FTXUI_COMPONENT_COMPONENT_HORIZONTAL_H_
#include "ftxui/component/component_direction.hpp"
namespace ftxui {
namespace component {
// A component where focus and events are automatically handled for you.
// It assumes its children are put in the horizontal direction.
class ComponentHorizontal : public ComponentDirection {
public:
ComponentHorizontal(Delegate* delegate);
bool HandleDirection(int key) override;
};
} // namespace component
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HORIZONTAL_H_ */

View File

@@ -0,0 +1,20 @@
#ifndef FTXUI_COMPONENT_COMPONENT_VERTICAL_H_
#define FTXUI_COMPONENT_COMPONENT_VERTICAL_H_
#include "ftxui/component/component_direction.hpp"
namespace ftxui {
namespace component {
// A component where focus and events are automatically handled for you.
// It assumes its children are put in the vertical direction.
class ComponentVertical : public ComponentDirection {
public:
ComponentVertical(Delegate* delegate);
bool HandleDirection(int key) override;
};
} // namespace component
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_VERTICAL_H_ */

View File

@@ -0,0 +1,34 @@
#ifndef FTXUI_COMPONENT_DELEGATE_HPP
#define FTXUI_COMPONENT_DELEGATE_HPP
#include "ftxui/dom/elements.hpp"
namespace ftxui {
namespace component {
class Component;
class Delegate {
public:
Delegate() {}
virtual ~Delegate() {}
// A Delegate shadows a component.
virtual void Register(Component* component) = 0;
virtual Component* component() = 0;
// Create new children.
virtual Delegate* NewChild() = 0;
virtual std::vector<Delegate*> children() = 0;
// Navigate in the tree.
virtual Delegate* PreviousSibling() = 0;
virtual Delegate* NextSibling() = 0;
virtual Delegate* Parent() = 0;
virtual Delegate* Root() = 0;
};
} // namespace component
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_DELEGATE_HPP */

View File

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

View File

@@ -0,0 +1,31 @@
#ifndef FTXUI_COMPONENT_TOGGLE_H_
#define FTXUI_COMPONENT_TOGGLE_H_
#include "ftxui/component/component.hpp"
#include <functional>
namespace ftxui {
namespace component {
class Toggle : public Component {
public:
// Constructor.
Toggle(Delegate*);
// State.
bool activated = true;
std::wstring on = L"On";
std::wstring off = L"Off";
// Callback.
std::function<void()> on_change = [](){};
// Component implementation.
dom::Element Render() override;
bool Event(int key) override;
};
} // namespace component
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_TOGGLE_H_ */