mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-20 18:48:08 +08:00
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:
@@ -1 +1,31 @@
|
||||
State => Components => Document => Text.
|
||||
#
|
||||
* Level 0: terminal output.
|
||||
* Level 1: ftxui::Screen
|
||||
* Level 2: ftxui::dom::Node
|
||||
* Level 3: ftxui::component::Component
|
||||
|
||||
## Level 0: terminal output.
|
||||
The terminal you know, you can append text on it. It is represented by
|
||||
std::cout.
|
||||
|
||||
## Level 1: ftxui::Screen
|
||||
A rectangular grid of characters.
|
||||
Use Terminal::ToString() to append its content into the console.
|
||||
|
||||
## Level 2: ftxui::dom::Node
|
||||
A hierarchical set of element.
|
||||
They handle layout and Render themself on the screen.
|
||||
See ftxui/dom/elements.hpp
|
||||
|
||||
You can make implement your own.
|
||||
|
||||
## Level 3: ftxui::component::Component
|
||||
A hierarchical set of component. A component render itself by producing
|
||||
ftxui::dom::Node in Component::Render().
|
||||
|
||||
Some component can handle events:
|
||||
* keyboard
|
||||
* mouse
|
||||
* terminal event
|
||||
|
||||
You can make implement your own.
|
||||
|
34
ftxui/include/ftxui/component/Event.hpp
Normal file
34
ftxui/include/ftxui/component/Event.hpp
Normal 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 */
|
44
ftxui/include/ftxui/component/component.hpp
Normal file
44
ftxui/include/ftxui/component/component.hpp
Normal 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 */
|
25
ftxui/include/ftxui/component/component_direction.hpp
Normal file
25
ftxui/include/ftxui/component/component_direction.hpp
Normal 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_ */
|
20
ftxui/include/ftxui/component/component_horizontal.hpp
Normal file
20
ftxui/include/ftxui/component/component_horizontal.hpp
Normal 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_ */
|
20
ftxui/include/ftxui/component/component_vertical.hpp
Normal file
20
ftxui/include/ftxui/component/component_vertical.hpp
Normal 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_ */
|
34
ftxui/include/ftxui/component/delegate.hpp
Normal file
34
ftxui/include/ftxui/component/delegate.hpp
Normal 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 */
|
31
ftxui/include/ftxui/component/menu.hpp
Normal file
31
ftxui/include/ftxui/component/menu.hpp
Normal 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 */
|
31
ftxui/include/ftxui/component/toggle.hpp
Normal file
31
ftxui/include/ftxui/component/toggle.hpp
Normal 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_ */
|
@@ -1,23 +0,0 @@
|
||||
#ifndef FTXUI_STATE_HPP
|
||||
#define FTXUI_STATE_HPP
|
||||
|
||||
#include "ftxui/core/requirement.hpp"
|
||||
#include "ftxui/core/document.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
class Component {
|
||||
public:
|
||||
virtual Document Render() = 0;
|
||||
|
||||
// Requirement -------------------------------------------------------------
|
||||
virtual void ComputeRequirement();
|
||||
Requirement requirement() { return requirement_; }
|
||||
|
||||
private:
|
||||
Requirement requirement_;
|
||||
};
|
||||
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_STATE_HPP */
|
@@ -1,12 +0,0 @@
|
||||
#ifndef FTXUI_DOCUMENT_HPP
|
||||
#define FTXUI_DOCUMENT_HPP
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
class Document {
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif /* end of include guard: FTXUI_DOCUMENT_HPP */
|
@@ -1,33 +0,0 @@
|
||||
#ifndef FTXUI_CORE_SCREEN
|
||||
#define FTXUI_CORE_SCREEN
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
class Node;
|
||||
}
|
||||
|
||||
class Screen {
|
||||
public:
|
||||
Screen(size_t dimx, size_t dimy);
|
||||
wchar_t& at(size_t x, size_t y);
|
||||
std::string ToString();
|
||||
|
||||
size_t dimx() { return dimx_;}
|
||||
size_t dimy() { return dimy_;}
|
||||
|
||||
static Screen WholeTerminal();
|
||||
static Screen TerminalOutput(std::unique_ptr<dom::Node>& element);
|
||||
|
||||
private:
|
||||
size_t dimx_;
|
||||
size_t dimy_;
|
||||
std::vector<std::wstring> lines_;
|
||||
};
|
||||
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_CORE_SCREEN */
|
@@ -1,16 +1,14 @@
|
||||
#ifndef FTXUI_CORE_DOM_ELEMENTS_HPP
|
||||
#define FTXUI_CORE_DOM_ELEMENTS_HPP
|
||||
#ifndef FTXUI_DOM_ELEMENTS_HPP
|
||||
#define FTXUI_DOM_ELEMENTS_HPP
|
||||
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include <initializer_list>
|
||||
#include "ftxui/dom/node.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
using Element = std::unique_ptr<Node>;
|
||||
using Child = std::unique_ptr<Node>;
|
||||
using Children = std::vector<std::unique_ptr<Node>>;
|
||||
|
||||
using Children = std::vector<Child>;
|
||||
|
||||
// --- Layout ----
|
||||
Element vbox(Children);
|
||||
@@ -22,7 +20,13 @@ Element text(std::wstring text);
|
||||
Element separator();
|
||||
Element gauge(float ratio);
|
||||
Element frame(Child);
|
||||
Element frame(std::wstring title, Child);
|
||||
Element frame(Child title, Child content);
|
||||
|
||||
// -- Decorator (Style) ---
|
||||
Element bold(Element);
|
||||
Element dim(Element);
|
||||
Element inverted(Element);
|
||||
Element underlined(Element);
|
||||
|
||||
// --- Decorator ---
|
||||
Element hcenter(Element);
|
||||
@@ -31,23 +35,23 @@ Element center(Element);
|
||||
Element flex(Element);
|
||||
|
||||
template <class... Args>
|
||||
std::vector<Element> unpack(Args... args) {
|
||||
std::vector<Element> vec;
|
||||
Children unpack(Args... args) {
|
||||
Children vec;
|
||||
(vec.push_back(std::forward<Args>(args)), ...);
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <class... Args>
|
||||
std::unique_ptr<Node> vbox(Args... children) {
|
||||
Element vbox(Args... children) {
|
||||
return vbox(unpack(std::forward<Args>(children)...));
|
||||
}
|
||||
|
||||
template <class... Args>
|
||||
std::unique_ptr<Node> hbox(Args... children) {
|
||||
Element hbox(Args... children) {
|
||||
return hbox(unpack(std::forward<Args>(children)...));
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_CORE_DOM_ELEMENTS_HPP */
|
||||
#endif /* end of include guard: FTXUI_DOM_ELEMENTS_HPP */
|
@@ -1,12 +1,12 @@
|
||||
#ifndef CORE_DOM_NODE_HPP
|
||||
#define CORE_DOM_NODE_HPP
|
||||
#ifndef DOM_NODE_HPP
|
||||
#define DOM_NODE_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "ftxui/core/requirement.hpp"
|
||||
#include "ftxui/core/screen.hpp"
|
||||
#include "ftxui/core/box.hpp"
|
||||
#include "ftxui/requirement.hpp"
|
||||
#include "ftxui/screen.hpp"
|
||||
#include "ftxui/box.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
@@ -41,4 +41,4 @@ void Render(Screen& screen, Node* node);
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: CORE_DOM_NODE_HPP */
|
||||
#endif /* end of include guard: DOM_NODE_HPP */
|
@@ -1,5 +1,5 @@
|
||||
#ifndef FTXUI_LAYOUT_REQUIREMENT_HPP
|
||||
#define FTXUI_LAYOUT_REQUIREMENT_HPP
|
||||
#ifndef FTXUI_REQUIREMENT_HPP
|
||||
#define FTXUI_REQUIREMENT_HPP
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
@@ -25,4 +25,4 @@ struct Requirement {
|
||||
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_LAYOUT_REQUIREMENT_HPP */
|
||||
#endif /* end of include guard: FTXUI_REQUIREMENT_HPP */
|
55
ftxui/include/ftxui/screen.hpp
Normal file
55
ftxui/include/ftxui/screen.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef FTXUI_SCREEN
|
||||
#define FTXUI_SCREEN
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
class Node;
|
||||
}
|
||||
|
||||
struct Pixel {
|
||||
wchar_t character = U' ';
|
||||
bool bold = false;
|
||||
bool inverted = false;
|
||||
bool underlined = false;
|
||||
bool dim = false;
|
||||
};
|
||||
|
||||
class Screen {
|
||||
public:
|
||||
// Constructor.
|
||||
Screen(size_t dimx, size_t dimy);
|
||||
|
||||
// Constructor using the terminal.
|
||||
static Screen TerminalFullscreen();
|
||||
static Screen TerminalOutput(std::unique_ptr<dom::Node>& element);
|
||||
|
||||
// dom::Node write into the screen using Screen::at.
|
||||
wchar_t& at(size_t x, size_t y);
|
||||
Pixel& PixelAt(size_t x, size_t y);
|
||||
|
||||
// Convert the screen into a printable string in the terminal.
|
||||
std::string ToString();
|
||||
|
||||
// Get screen dimensions.
|
||||
size_t dimx() { return dimx_;}
|
||||
size_t dimy() { return dimy_;}
|
||||
|
||||
// Move the terminal cursor n-lines up with n = dimy().
|
||||
std::string ResetPosition();
|
||||
|
||||
// Fill with space.
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
size_t dimx_;
|
||||
size_t dimy_;
|
||||
std::vector<std::vector<Pixel>> pixels_;
|
||||
};
|
||||
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_SCREEN */
|
34
ftxui/include/ftxui/screen_interactive.hpp
Normal file
34
ftxui/include/ftxui/screen_interactive.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef FTXUI_SCREEN_INTERACTIVE
|
||||
#define FTXUI_SCREEN_INTERACTIVE
|
||||
|
||||
#include "ftxui/screen.hpp"
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
namespace component {
|
||||
class Delegate;
|
||||
class Component;
|
||||
} // namespace component
|
||||
|
||||
class ScreenInteractive : public Screen {
|
||||
public:
|
||||
ScreenInteractive(size_t dimx, size_t dimy);
|
||||
~ScreenInteractive();
|
||||
component::Delegate* delegate();
|
||||
void Loop();
|
||||
std::function<void()> ExitLoopClosure();
|
||||
|
||||
private:
|
||||
class Delegate;
|
||||
std::unique_ptr<Delegate> delegate_;
|
||||
|
||||
void Clear();
|
||||
void Draw();
|
||||
bool quit_ = false;
|
||||
};
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_SCREEN_INTERACTIVE */
|
Reference in New Issue
Block a user