2021-05-15 04:00:49 +08:00
|
|
|
#include <algorithm> // for max
|
|
|
|
#include <iterator> // for begin, end
|
2021-08-09 05:25:20 +08:00
|
|
|
#include <memory> // for allocator, make_shared, __shared_ptr_access
|
|
|
|
#include <string> // for basic_string, string
|
2021-05-15 04:00:49 +08:00
|
|
|
#include <utility> // for move
|
|
|
|
#include <vector> // for vector, __alloc_traits<>::value_type
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2021-05-02 02:40:35 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for unpack, Element, Decorator, Elements, border, borderWith, window
|
|
|
|
#include "ftxui/dom/node.hpp" // for Node
|
|
|
|
#include "ftxui/dom/requirement.hpp" // for Requirement
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
|
|
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
|
2019-01-20 05:06:05 +08:00
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
static std::string simple_border_charset[] = {"╭", "╮", "╰", "╯", "─",
|
|
|
|
"│", "┬", "┴", "┤", "├"};
|
2021-08-08 04:30:55 +08:00
|
|
|
|
|
|
|
// For reference, here is the charset for normal border:
|
2021-08-09 05:25:20 +08:00
|
|
|
// {"┌", "┐", "└", "┘", "─", "│", "┬", "┴", "┤", "├"};
|
2021-08-08 04:30:55 +08:00
|
|
|
// TODO(arthursonzogni): Consider adding options to choose the kind of borders
|
|
|
|
// to use.
|
2019-01-20 05:06:05 +08:00
|
|
|
|
|
|
|
class Border : public Node {
|
|
|
|
public:
|
2019-01-27 09:33:06 +08:00
|
|
|
Border(Elements children)
|
|
|
|
: Node(std::move(children)),
|
|
|
|
charset(std::begin(simple_border_charset),
|
|
|
|
std::end(simple_border_charset)) {}
|
|
|
|
Border(Elements children, Pixel pixel)
|
|
|
|
: Node(std::move(children)), charset_pixel(10, pixel) {}
|
2019-01-20 05:06:05 +08:00
|
|
|
|
2020-03-23 05:32:44 +08:00
|
|
|
std::vector<Pixel> charset_pixel;
|
2021-08-09 05:25:20 +08:00
|
|
|
std::vector<std::string> charset;
|
2019-01-27 09:33:06 +08:00
|
|
|
|
2019-01-20 05:06:05 +08:00
|
|
|
void ComputeRequirement() override {
|
|
|
|
Node::ComputeRequirement();
|
2021-05-16 23:18:11 +08:00
|
|
|
requirement_ = children_[0]->requirement();
|
2020-06-01 22:13:29 +08:00
|
|
|
requirement_.min_x += 2;
|
|
|
|
requirement_.min_y += 2;
|
2021-05-16 23:18:11 +08:00
|
|
|
if (children_.size() == 2) {
|
2020-06-01 22:13:29 +08:00
|
|
|
requirement_.min_x =
|
2021-05-16 23:18:11 +08:00
|
|
|
std::max(requirement_.min_x, children_[1]->requirement().min_x + 2);
|
2019-01-20 05:06:05 +08:00
|
|
|
}
|
|
|
|
requirement_.selected_box.x_min++;
|
|
|
|
requirement_.selected_box.x_max++;
|
|
|
|
requirement_.selected_box.y_min++;
|
|
|
|
requirement_.selected_box.y_max++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetBox(Box box) override {
|
|
|
|
Node::SetBox(box);
|
2021-05-16 23:18:11 +08:00
|
|
|
if (children_.size() == 2) {
|
2019-01-20 05:06:05 +08:00
|
|
|
Box title_box;
|
|
|
|
title_box.x_min = box.x_min + 1;
|
|
|
|
title_box.x_max = box.x_max - 1;
|
|
|
|
title_box.y_min = box.y_min;
|
|
|
|
title_box.y_max = box.y_min;
|
2021-05-16 23:18:11 +08:00
|
|
|
children_[1]->SetBox(title_box);
|
2019-01-20 05:06:05 +08:00
|
|
|
}
|
|
|
|
box.x_min++;
|
|
|
|
box.x_max--;
|
|
|
|
box.y_min++;
|
|
|
|
box.y_max--;
|
2021-05-16 23:18:11 +08:00
|
|
|
children_[0]->SetBox(box);
|
2019-01-20 05:06:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
|
|
|
// Draw content.
|
2021-05-16 23:18:11 +08:00
|
|
|
children_[0]->Render(screen);
|
2019-01-20 05:06:05 +08:00
|
|
|
|
|
|
|
// Draw the border.
|
|
|
|
if (box_.x_min >= box_.x_max || box_.y_min >= box_.y_max)
|
|
|
|
return;
|
|
|
|
|
2019-01-27 09:33:06 +08:00
|
|
|
if (!charset.empty())
|
|
|
|
RenderPixel(screen);
|
|
|
|
else
|
|
|
|
RenderChar(screen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPixel(Screen& screen) {
|
2019-01-20 05:06:05 +08:00
|
|
|
screen.at(box_.x_min, box_.y_min) = charset[0];
|
|
|
|
screen.at(box_.x_max, box_.y_min) = charset[1];
|
|
|
|
screen.at(box_.x_min, box_.y_max) = charset[2];
|
|
|
|
screen.at(box_.x_max, box_.y_max) = charset[3];
|
2020-03-23 05:32:44 +08:00
|
|
|
for (float x = box_.x_min + 1; x < box_.x_max; ++x) {
|
2019-01-20 05:06:05 +08:00
|
|
|
screen.at(x, box_.y_min) = charset[4];
|
|
|
|
screen.at(x, box_.y_max) = charset[4];
|
|
|
|
}
|
2020-03-23 05:32:44 +08:00
|
|
|
for (float y = box_.y_min + 1; y < box_.y_max; ++y) {
|
2019-01-20 05:06:05 +08:00
|
|
|
screen.at(box_.x_min, y) = charset[5];
|
2020-03-23 05:32:44 +08:00
|
|
|
screen.at(box_.x_max, y) = charset[5];
|
2019-01-20 05:06:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw title.
|
2021-05-16 23:18:11 +08:00
|
|
|
if (children_.size() == 2)
|
|
|
|
children_[1]->Render(screen);
|
2019-01-20 05:06:05 +08:00
|
|
|
}
|
2019-01-27 09:33:06 +08:00
|
|
|
|
|
|
|
void RenderChar(Screen& screen) {
|
|
|
|
screen.PixelAt(box_.x_min, box_.y_min) = charset_pixel[0];
|
|
|
|
screen.PixelAt(box_.x_max, box_.y_min) = charset_pixel[1];
|
|
|
|
screen.PixelAt(box_.x_min, box_.y_max) = charset_pixel[2];
|
|
|
|
screen.PixelAt(box_.x_max, box_.y_max) = charset_pixel[3];
|
2020-03-23 05:32:44 +08:00
|
|
|
for (float x = box_.x_min + 1; x < box_.x_max; ++x) {
|
2019-01-27 09:33:06 +08:00
|
|
|
screen.PixelAt(x, box_.y_min) = charset_pixel[4];
|
|
|
|
screen.PixelAt(x, box_.y_max) = charset_pixel[4];
|
|
|
|
}
|
2020-03-23 05:32:44 +08:00
|
|
|
for (float y = box_.y_min + 1; y < box_.y_max; ++y) {
|
2019-01-27 09:33:06 +08:00
|
|
|
screen.PixelAt(box_.x_min, y) = charset_pixel[5];
|
2020-03-23 05:32:44 +08:00
|
|
|
screen.PixelAt(box_.x_max, y) = charset_pixel[5];
|
2019-01-27 09:33:06 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-20 05:06:05 +08:00
|
|
|
};
|
2020-05-25 07:34:13 +08:00
|
|
|
/// @brief Draw a border around the element.
|
|
|
|
/// @ingroup dom
|
|
|
|
///
|
|
|
|
/// Add a border around an element
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// // Use 'border' as a function...
|
2021-08-09 05:25:20 +08:00
|
|
|
/// Element document = border(text("The element"));
|
2020-05-25 07:34:13 +08:00
|
|
|
///
|
|
|
|
/// // ...Or as a 'pipe'.
|
2021-08-09 05:25:20 +08:00
|
|
|
/// Element document = text("The element") | border;
|
2020-05-25 07:34:13 +08:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// ┌───────────┐
|
|
|
|
/// │The element│
|
|
|
|
/// └───────────┘
|
|
|
|
/// ```
|
2020-05-21 02:36:47 +08:00
|
|
|
Element border(Element child) {
|
|
|
|
return std::make_shared<Border>(unpack(std::move(child)));
|
2019-01-20 05:06:05 +08:00
|
|
|
}
|
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
/// @brief Draw window with a title and a border around the element.
|
|
|
|
/// @param title The title of the window.
|
|
|
|
/// @param content The element to be wrapped.
|
|
|
|
/// @ingroup dom
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @see border
|
2020-05-25 07:34:13 +08:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-08-09 05:25:20 +08:00
|
|
|
/// Element document = window(text("Title"),
|
|
|
|
/// text("content")
|
2020-05-25 07:34:13 +08:00
|
|
|
/// );
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// ┌Title──┐
|
|
|
|
/// │content│
|
|
|
|
/// └───────┘
|
|
|
|
/// ```
|
2020-05-21 02:36:47 +08:00
|
|
|
Element window(Element title, Element content) {
|
|
|
|
return std::make_shared<Border>(unpack(std::move(content), std::move(title)));
|
2019-01-20 05:06:05 +08:00
|
|
|
}
|
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
/// @brief Same as border but with a constant Pixel around the element.
|
|
|
|
/// @ingroup dom
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @see border
|
2019-01-27 09:33:06 +08:00
|
|
|
Decorator borderWith(Pixel pixel) {
|
|
|
|
return [pixel](Element child) {
|
2020-05-21 02:36:47 +08:00
|
|
|
return std::make_shared<Border>(unpack(std::move(child)), pixel);
|
2019-01-20 05:06:05 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-12 04:44:55 +08:00
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +08:00
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|