mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-18 09:08:08 +08:00
88
include/ftxui/dom/elements.hpp
Normal file
88
include/ftxui/dom/elements.hpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#ifndef FTXUI_DOM_ELEMENTS_HPP
|
||||
#define FTXUI_DOM_ELEMENTS_HPP
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/screen/color.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
using Element = std::unique_ptr<Node>;
|
||||
using Elements = std::vector<Element>;
|
||||
using Decorator = std::function<Element(Element)>;
|
||||
using GraphFunction = std::function<std::vector<int>(int,int)>;
|
||||
|
||||
// --- Widget ---
|
||||
Element text(std::wstring text);
|
||||
Element separator();
|
||||
Element separator(Pixel);
|
||||
Element gauge(float ratio);
|
||||
Element border(Element);
|
||||
Decorator borderWith(Pixel);
|
||||
Element window(Element title, Element content);
|
||||
Element spinner(int charset_index, size_t image_index);
|
||||
Elements paragraph(std::wstring text); // Use inside hflow(). Split by space.
|
||||
Element graph(GraphFunction);
|
||||
|
||||
// -- Decorator ---
|
||||
Element bold(Element);
|
||||
Element dim(Element);
|
||||
Element inverted(Element);
|
||||
Element underlined(Element);
|
||||
Element blink(Element);
|
||||
Decorator color(Color);
|
||||
Decorator bgcolor(Color);
|
||||
Element color(Color, Element);
|
||||
Element bgcolor(Color, Element);
|
||||
|
||||
// --- Layout ---
|
||||
// Horizontal, Vertical or stacked set of elements.
|
||||
Element hbox(Elements);
|
||||
Element vbox(Elements);
|
||||
Element dbox(Elements);
|
||||
Element hflow(Elements);
|
||||
|
||||
// -- Flexibility ---
|
||||
// Define how to share the remaining space when not all of it is used inside a
|
||||
// container.
|
||||
Element filler();
|
||||
Element flex(Element);
|
||||
Element notflex(Element);
|
||||
|
||||
// -- Size override;
|
||||
enum Direction { WIDTH, HEIGHT };
|
||||
enum Constraint { LESS_THAN, EQUAL, GREATER_THAN };
|
||||
Decorator size(Direction, Constraint, int value);
|
||||
|
||||
// --- Frame ---
|
||||
// A frame is a scrollable area. The internal area is potentially larger than
|
||||
// the external one. The internal area is scrolled in order to make visible the
|
||||
// focused element.
|
||||
Element frame(Element);
|
||||
Element focus(Element);
|
||||
Element select(Element);
|
||||
|
||||
// --- Util --------------------------------------------------------------------
|
||||
Element hcenter(Element);
|
||||
Element vcenter(Element);
|
||||
Element center(Element);
|
||||
Element align_right(Element);
|
||||
Element nothing(Element element);
|
||||
|
||||
// Pipe elements into decorator togethers.
|
||||
// Examples: text("ftxui") | bold | underlined;
|
||||
Element operator|(Element, Decorator);
|
||||
Elements operator|(Elements, Decorator);
|
||||
Decorator operator|(Decorator, Decorator);
|
||||
|
||||
// Make container able to take any number of children as input.
|
||||
#include "take_any_args.hpp"
|
||||
TAKE_ANY_ARGS(vbox)
|
||||
TAKE_ANY_ARGS(hbox)
|
||||
TAKE_ANY_ARGS(dbox)
|
||||
TAKE_ANY_ARGS(hflow)
|
||||
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_DOM_ELEMENTS_HPP */
|
42
include/ftxui/dom/node.hpp
Normal file
42
include/ftxui/dom/node.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef FTXUI_DOM_NODE_HPP
|
||||
#define FTXUI_DOM_NODE_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "ftxui/dom/requirement.hpp"
|
||||
#include "ftxui/screen/box.hpp"
|
||||
#include "ftxui/screen/screen.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
class Node {
|
||||
public:
|
||||
Node();
|
||||
Node(std::vector<std::unique_ptr<Node>> children);
|
||||
virtual ~Node();
|
||||
|
||||
// Step 1: Compute layout requirement. Tell parent what dimensions this
|
||||
// element wants to be.
|
||||
// Propagated from Children to Parents.
|
||||
virtual void ComputeRequirement();
|
||||
Requirement requirement() { return requirement_; }
|
||||
|
||||
// Step 2: Assign this element its final dimensions.
|
||||
// Propagated from Parents to Children.
|
||||
virtual void SetBox(Box box);
|
||||
|
||||
// Step 3: Draw this element.
|
||||
virtual void Render(Screen& screen);
|
||||
|
||||
std::vector<std::unique_ptr<Node>> children;
|
||||
protected:
|
||||
Requirement requirement_;
|
||||
Box box_;
|
||||
};
|
||||
|
||||
void Render(Screen& screen, Node* node);
|
||||
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_DOM_NODE_HPP */
|
26
include/ftxui/dom/requirement.hpp
Normal file
26
include/ftxui/dom/requirement.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef FTXUI_DOM_REQUIREMENT_HPP
|
||||
#define FTXUI_DOM_REQUIREMENT_HPP
|
||||
|
||||
#include "ftxui/screen/box.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
struct Requirement {
|
||||
// The required size to fully draw the element.
|
||||
struct { int x = 0; int y = 0; } min;
|
||||
|
||||
// How much flexibility is given to the component.
|
||||
struct { int x = 0; int y = 0; } flex;
|
||||
|
||||
// Frame.
|
||||
enum Selection {
|
||||
NORMAL = 0,
|
||||
SELECTED = 1,
|
||||
FOCUSED = 2,
|
||||
} selection = NORMAL;
|
||||
Box selected_box;
|
||||
};
|
||||
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_REQUIREMENT_HPP */
|
30
include/ftxui/dom/take_any_args.hpp
Normal file
30
include/ftxui/dom/take_any_args.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <type_traits>
|
||||
|
||||
template <class T>
|
||||
void Merge(Elements& container, T t) {}
|
||||
|
||||
template <>
|
||||
inline void Merge(Elements& container, Element element) {
|
||||
container.push_back(std::move(element));
|
||||
}
|
||||
|
||||
template <>
|
||||
inline void Merge(Elements& container, Elements elements) {
|
||||
for(auto& element : elements)
|
||||
container.push_back(std::move(element));
|
||||
}
|
||||
|
||||
// Turn a set of arguments into a vector.
|
||||
template <class... Args>
|
||||
Elements unpack(Args... args) {
|
||||
std::vector<Element> vec;
|
||||
(Merge(vec, std::move(args)), ...);
|
||||
return vec;
|
||||
}
|
||||
|
||||
// Make |container| able to take any number of argments.
|
||||
#define TAKE_ANY_ARGS(container) \
|
||||
template <class... Args> \
|
||||
Element container(Args... children) { \
|
||||
return container(unpack(std::forward<Args>(children)...)); \
|
||||
}
|
Reference in New Issue
Block a user