Refactor component containers.

This commit is contained in:
Arthur Sonzogni
2019-01-12 18:24:46 +01:00
parent 21644eea6b
commit dba019139b
41 changed files with 293 additions and 423 deletions

View File

@@ -13,50 +13,43 @@ class Focus;
class Component {
public:
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;
};
// Constructor/Destructor.
Component(Delegate* delegate);
Component() = default;
virtual ~Component();
// Render the component.
// Component hierarchy.
Component* Parent() { return parent_; }
void Add(Component* children);
// Renders the component.
virtual Element Render();
// Handle an event. By default, it calls this function on each children.
virtual bool OnEvent(Event even);
// Handles an event.
// By default, reduce on children with a lazy OR.
//
// Returns whether the event was handled or not.
virtual bool OnEvent(Event);
// 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
// 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* GetActiveChild() { return nullptr; }
bool Active(); // True is this component is an active child.
bool Focused(); // True if all the ancestors are active childs.
virtual Component* ActiveChild();
// Whether this is the active child of its parent.
bool Active();
// Whether all the ancestors are active.
bool Focused();
Component* Parent();
Component* PreviousSibling();
Component* NextSibling();
private:
Delegate* delegate_;
Component* parent_ = nullptr;
void Detach();
void Attach(Component* parent);
protected:
std::vector<Component*> children_;
};
} // namespace ftxui

View File

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

View File

@@ -1,18 +0,0 @@
#ifndef FTXUI_COMPONENT_COMPONENT_HORIZONTAL_H_
#define FTXUI_COMPONENT_COMPONENT_HORIZONTAL_H_
#include "ftxui/component/component_direction.hpp"
namespace ftxui {
// 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*);
bool HandleDirection(Event) override;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HORIZONTAL_H_ */

View File

@@ -1,18 +0,0 @@
#ifndef FTXUI_COMPONENT_COMPONENT_VERTICAL_H_
#define FTXUI_COMPONENT_COMPONENT_VERTICAL_H_
#include "ftxui/component/component_direction.hpp"
namespace ftxui {
// 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*);
bool HandleDirection(Event) override;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_VERTICAL_H_ */

View File

@@ -0,0 +1,36 @@
#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();
~Container() override = default;
// Component override.
bool OnEvent(Event event) override;
Component* ActiveChild() override;
protected:
// Handlers
using Handler = bool (Container::*)(Event);
Handler handler_;
bool Vertical(Event event);
bool Horizontal(Event event);
size_t selected_ = 0;
Container(Handler);
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_CONTAINER_HPP */

View File

@@ -9,8 +9,8 @@ namespace ftxui {
class Input : public Component {
public:
// Constructor.
Input(Delegate*);
~Input() override;
Input() = default;
~Input() override = default;
// State.
std::wstring content;

View File

@@ -10,7 +10,8 @@ namespace ftxui {
class Menu : public Component {
public:
// Constructor.
Menu(Delegate*);
Menu() = default;
~Menu() override = default;
// State.
std::vector<std::wstring> entries = {};

View File

@@ -1,31 +1,25 @@
#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#include "ftxui/component/component.hpp"
#include "ftxui/screen/screen.hpp"
#include <functional>
#include <memory>
namespace ftxui {
class Component;
class ScreenInteractive : public ftxui::Screen {
class ScreenInteractive : public Screen {
public:
static ScreenInteractive FixedSize(size_t dimx, size_t dimy);
static ScreenInteractive Fullscreen();
static ScreenInteractive TerminalOutput();
~ScreenInteractive();
void Loop();
void Loop(Component*);
std::function<void()> ExitLoopClosure();
Component::Delegate* delegate();
private:
class Delegate;
std::unique_ptr<Delegate> delegate_;
void PrepareDraw();
void Draw(Component* component);
bool quit_ = false;
enum class Dimension {
@@ -34,7 +28,6 @@ class ScreenInteractive : public ftxui::Screen {
Fullscreen,
};
Dimension dimension_ = Dimension::Fixed;
ScreenInteractive(size_t dimx, size_t dimy, Dimension dimension);
};

View File

@@ -10,7 +10,7 @@ namespace ftxui {
class Toggle : public Component {
public:
// Constructor.
Toggle(Delegate*);
~Toggle() override = default;
// State.
size_t activated = 0;

View File

@@ -9,15 +9,13 @@
namespace ftxui {
using Element = std::unique_ptr<Node>;
using Elements = std::vector<Element>;
using Decorator = std::function<Element(Element)>;
using Child = std::unique_ptr<Node>;
using Children = std::vector<Child>;
using Color = ftxui::Color;
// --- Layout ----
Element vbox(Children);
Element hbox(Children);
Element dbox(Children);
Element vbox(Elements);
Element hbox(Elements);
Element dbox(Elements);
// -- Flexibility --
Element filler();
@@ -29,7 +27,7 @@ Element text(std::wstring text);
Element separator();
Element gauge(float ratio);
Element frame(Element);
Element window(Child title, Child content);
Element window(Element title, Element content);
Element spinner(int charset_index, size_t image_index);
// -- Decorator ---

View File

@@ -2,7 +2,7 @@
// Turn a set of arguments into a vector.
template <class... Args>
Children unpack(Args... args) {
Elements unpack(Args... args) {
using T = std::common_type_t<Args...>;
std::vector<T> vec;
(vec.push_back(std::forward<Args>(args)), ...);