2022-03-31 08:17:43 +08:00
|
|
|
#include <array> // for array, array<>::value_type
|
|
|
|
#include <memory> // for make_shared, allocator
|
|
|
|
#include <string> // for basic_string, string
|
|
|
|
#include <utility> // for move
|
2021-05-02 02:40:35 +08:00
|
|
|
|
2022-03-14 01:51:46 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for Element, BorderStyle, LIGHT, separator, DOUBLE, EMPTY, HEAVY, separatorCharacter, separatorDouble, separatorEmpty, separatorHSelector, separatorHeavy, separatorLight, separatorStyled, separatorVSelector
|
|
|
|
#include "ftxui/dom/node.hpp" // for Node
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/dom/requirement.hpp" // for Requirement
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
2022-03-14 01:51:46 +08:00
|
|
|
#include "ftxui/screen/color.hpp" // for Color
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
|
2018-09-20 03:52:25 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2019-01-07 00:10:35 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
namespace {
|
|
|
|
using Charset = std::array<std::string, 2>; // NOLINT
|
|
|
|
using Charsets = std::array<Charset, 5>; // NOLINT
|
2022-04-27 20:00:46 +08:00
|
|
|
// NOLINTNEXTLINE
|
|
|
|
const Charsets charsets = {
|
|
|
|
Charset{"│", "─"}, //
|
|
|
|
Charset{"┃", "━"}, //
|
|
|
|
Charset{"║", "═"}, //
|
|
|
|
Charset{"│", "─"}, //
|
|
|
|
Charset{" ", " "}, //
|
2021-09-12 06:36:59 +08:00
|
|
|
};
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
} // namespace
|
|
|
|
|
2018-09-20 03:52:25 +08:00
|
|
|
class Separator : public Node {
|
|
|
|
public:
|
2022-03-31 08:17:43 +08:00
|
|
|
explicit Separator(std::string value) : value_(std::move(value)) {}
|
2021-10-16 05:04:11 +08:00
|
|
|
|
|
|
|
void ComputeRequirement() override {
|
|
|
|
requirement_.min_x = 1;
|
|
|
|
requirement_.min_y = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
|
|
|
for (int y = box_.y_min; y <= box_.y_max; ++y) {
|
|
|
|
for (int x = box_.x_min; x <= box_.x_max; ++x) {
|
2022-01-22 22:38:01 +08:00
|
|
|
Pixel& pixel = screen.PixelAt(x, y);
|
|
|
|
pixel.character = value_;
|
|
|
|
pixel.automerge = true;
|
2021-10-16 05:04:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SeparatorAuto : public Node {
|
|
|
|
public:
|
2022-03-31 08:17:43 +08:00
|
|
|
explicit SeparatorAuto(BorderStyle style) : style_(style) {}
|
2021-09-12 06:36:59 +08:00
|
|
|
|
2018-09-20 03:52:25 +08:00
|
|
|
void ComputeRequirement() override {
|
2020-06-01 22:13:29 +08:00
|
|
|
requirement_.min_x = 1;
|
|
|
|
requirement_.min_y = 1;
|
2018-09-20 03:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool is_column = (box_.x_max == box_.x_min);
|
|
|
|
const bool is_line = (box_.y_min == box_.y_max);
|
2018-09-20 03:52:25 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
const std::string c = charsets[style_][int(is_line && !is_column)];
|
2018-09-20 03:52:25 +08:00
|
|
|
|
2019-01-20 05:06:05 +08:00
|
|
|
for (int y = box_.y_min; y <= box_.y_max; ++y) {
|
|
|
|
for (int x = box_.x_min; x <= box_.x_max; ++x) {
|
2022-01-22 22:38:01 +08:00
|
|
|
Pixel& pixel = screen.PixelAt(x, y);
|
|
|
|
pixel.character = c;
|
|
|
|
pixel.automerge = true;
|
2018-09-20 03:52:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-12 06:36:59 +08:00
|
|
|
|
|
|
|
BorderStyle style_;
|
2018-09-20 03:52:25 +08:00
|
|
|
};
|
|
|
|
|
2021-10-16 05:04:11 +08:00
|
|
|
class SeparatorWithPixel : public SeparatorAuto {
|
2019-01-27 09:33:06 +08:00
|
|
|
public:
|
2022-03-31 08:17:43 +08:00
|
|
|
explicit SeparatorWithPixel(Pixel pixel)
|
|
|
|
: SeparatorAuto(LIGHT), pixel_(std::move(pixel)) {
|
2022-01-22 22:38:01 +08:00
|
|
|
pixel_.automerge = true;
|
|
|
|
}
|
2021-05-17 07:34:53 +08:00
|
|
|
void Render(Screen& screen) override {
|
|
|
|
for (int y = box_.y_min; y <= box_.y_max; ++y) {
|
|
|
|
for (int x = box_.x_min; x <= box_.x_max; ++x) {
|
|
|
|
screen.PixelAt(x, y) = pixel_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 23:18:11 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Pixel pixel_;
|
2019-01-27 09:33:06 +08:00
|
|
|
};
|
|
|
|
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements.
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @see separatorStyled
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 16:36:04 +08:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-16 05:04:11 +08:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 16:36:04 +08:00
|
|
|
/// Element document = vbox({
|
2021-10-16 05:04:11 +08:00
|
|
|
/// text("up"),
|
2021-10-03 16:36:04 +08:00
|
|
|
/// separator(),
|
2021-10-16 05:04:11 +08:00
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-16 05:04:11 +08:00
|
|
|
/// up
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ────
|
2021-10-16 05:04:11 +08:00
|
|
|
/// down
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
2020-05-21 02:36:47 +08:00
|
|
|
Element separator() {
|
2021-10-16 05:04:11 +08:00
|
|
|
return std::make_shared<SeparatorAuto>(LIGHT);
|
2018-09-20 03:52:25 +08:00
|
|
|
}
|
|
|
|
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements.
|
|
|
|
/// @param style the style of the separator.
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @see separatorStyled
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 16:36:04 +08:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-16 05:04:11 +08:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 16:36:04 +08:00
|
|
|
/// Element document = vbox({
|
2021-10-16 05:04:11 +08:00
|
|
|
/// text("up"),
|
|
|
|
/// separatorStyled(DOUBLE),
|
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-16 05:04:11 +08:00
|
|
|
/// up
|
|
|
|
/// ════
|
|
|
|
/// down
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
2021-09-12 06:36:59 +08:00
|
|
|
Element separatorStyled(BorderStyle style) {
|
2021-10-16 05:04:11 +08:00
|
|
|
return std::make_shared<SeparatorAuto>(style);
|
2021-09-12 06:36:59 +08:00
|
|
|
}
|
|
|
|
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements, using the LIGHT style.
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @see separatorStyled
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 16:36:04 +08:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-16 05:04:11 +08:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 16:36:04 +08:00
|
|
|
/// Element document = vbox({
|
2021-10-16 05:04:11 +08:00
|
|
|
/// text("up"),
|
2021-10-03 16:36:04 +08:00
|
|
|
/// separatorLight(),
|
2021-10-16 05:04:11 +08:00
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-16 05:04:11 +08:00
|
|
|
/// up
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ────
|
2021-10-16 05:04:11 +08:00
|
|
|
/// down
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
2021-09-12 06:36:59 +08:00
|
|
|
Element separatorLight() {
|
2021-10-16 05:04:11 +08:00
|
|
|
return std::make_shared<SeparatorAuto>(LIGHT);
|
2021-09-12 06:36:59 +08:00
|
|
|
}
|
2021-10-03 16:36:04 +08:00
|
|
|
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements, using the HEAVY style.
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @see separatorStyled
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 16:36:04 +08:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-16 05:04:11 +08:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 16:36:04 +08:00
|
|
|
/// Element document = vbox({
|
2021-10-16 05:04:11 +08:00
|
|
|
/// text("up"),
|
2021-10-03 16:36:04 +08:00
|
|
|
/// separatorHeavy(),
|
2021-10-16 05:04:11 +08:00
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-16 05:04:11 +08:00
|
|
|
/// up
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ━━━━
|
2021-10-16 05:04:11 +08:00
|
|
|
/// down
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
2021-09-12 06:36:59 +08:00
|
|
|
Element separatorHeavy() {
|
2021-10-16 05:04:11 +08:00
|
|
|
return std::make_shared<SeparatorAuto>(HEAVY);
|
2021-09-12 06:36:59 +08:00
|
|
|
}
|
2021-10-03 16:36:04 +08:00
|
|
|
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements, using the DOUBLE style.
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @see separatorStyled
|
2021-10-16 05:04:11 +08:00
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
2021-10-03 16:36:04 +08:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
2021-10-16 05:04:11 +08:00
|
|
|
/// // Use 'border' as a function...
|
2021-10-03 16:36:04 +08:00
|
|
|
/// Element document = vbox({
|
2021-10-16 05:04:11 +08:00
|
|
|
/// text("up"),
|
2021-10-03 16:36:04 +08:00
|
|
|
/// separatorDouble(),
|
2021-10-16 05:04:11 +08:00
|
|
|
/// text("down"),
|
|
|
|
/// });
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-10-16 05:04:11 +08:00
|
|
|
/// up
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ════
|
2021-10-16 05:04:11 +08:00
|
|
|
/// down
|
2021-10-03 16:36:04 +08:00
|
|
|
/// ```
|
2021-09-12 06:36:59 +08:00
|
|
|
Element separatorDouble() {
|
2021-10-16 05:04:11 +08:00
|
|
|
return std::make_shared<SeparatorAuto>(DOUBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements, using the EMPTY style.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
|
|
|
/// @see separatorStyled
|
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// // Use 'border' as a function...
|
|
|
|
/// Element document = vbox({
|
|
|
|
/// text("up"),
|
|
|
|
/// separator(),
|
|
|
|
/// text("down"),
|
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// up
|
|
|
|
///
|
|
|
|
/// down
|
|
|
|
/// ```
|
|
|
|
Element separatorEmpty() {
|
|
|
|
return std::make_shared<SeparatorAuto>(EMPTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Draw a vertical or horizontal separation in between two other
|
|
|
|
/// elements.
|
|
|
|
/// @param value the character to fill the separator area.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorDouble
|
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorEmpty
|
|
|
|
/// @see separatorRounded
|
|
|
|
/// @see separatorStyled
|
|
|
|
/// @see separatorCharacter
|
|
|
|
///
|
|
|
|
/// Add a visual separation in between two elements.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// // Use 'border' as a function...
|
|
|
|
/// Element document = vbox({
|
|
|
|
/// text("up"),
|
|
|
|
/// separator(),
|
|
|
|
/// text("down"),
|
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// up
|
|
|
|
/// ────
|
|
|
|
/// down
|
|
|
|
/// ```
|
|
|
|
Element separatorCharacter(std::string value) {
|
2022-03-31 08:17:43 +08:00
|
|
|
return std::make_shared<Separator>(std::move(value));
|
2019-01-27 09:33:06 +08:00
|
|
|
}
|
|
|
|
|
2021-10-03 16:36:04 +08:00
|
|
|
/// @brief Draw a separator in between two element filled with a given pixel.
|
|
|
|
/// @ingroup dom
|
|
|
|
/// @see separator
|
|
|
|
/// @see separatorLight
|
|
|
|
/// @see separatorHeavy
|
|
|
|
/// @see separatorDouble
|
|
|
|
/// @see separatorStyled
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Pixel empty;
|
|
|
|
/// Element document = vbox({
|
|
|
|
/// text("Up"),
|
|
|
|
/// separator(empty),
|
|
|
|
/// text("Down"),
|
|
|
|
/// })
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// Up
|
|
|
|
///
|
|
|
|
/// Down
|
|
|
|
/// ```
|
|
|
|
Element separator(Pixel pixel) {
|
2022-03-31 08:17:43 +08:00
|
|
|
return std::make_shared<SeparatorWithPixel>(std::move(pixel));
|
2021-10-03 16:36:04 +08:00
|
|
|
}
|
|
|
|
|
2022-03-14 01:51:46 +08:00
|
|
|
/// @brief Draw an horizontal bar, with the area in between left/right colored
|
|
|
|
/// differently.
|
|
|
|
/// @param left the left limit of the active area.
|
|
|
|
/// @param right the right limit of the active area.
|
|
|
|
/// @param selected_color the color of the selected area.
|
|
|
|
/// @param unselected_color the color of the unselected area.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = separatorHSelector(2,5, Color::White, Color::Blue);
|
|
|
|
/// ```
|
|
|
|
Element separatorHSelector(float left,
|
|
|
|
float right,
|
2022-03-31 08:17:43 +08:00
|
|
|
Color unselected_color,
|
|
|
|
Color selected_color) {
|
2022-03-14 01:51:46 +08:00
|
|
|
class Impl : public Node {
|
|
|
|
public:
|
|
|
|
Impl(float left, float right, Color selected_color, Color unselected_color)
|
|
|
|
: left_(left),
|
|
|
|
right_(right),
|
2022-03-31 08:17:43 +08:00
|
|
|
unselected_color_(unselected_color),
|
|
|
|
selected_color_(selected_color) {}
|
2022-03-14 01:51:46 +08:00
|
|
|
void ComputeRequirement() override {
|
|
|
|
requirement_.min_x = 1;
|
|
|
|
requirement_.min_y = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (box_.y_max < box_.y_min) {
|
2022-03-14 01:51:46 +08:00
|
|
|
return;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
// This are the two location with an empty demi-cell.
|
2022-03-31 08:17:43 +08:00
|
|
|
int demi_cell_left = int(left_ * 2.F - 1.F); // NOLINT
|
|
|
|
int demi_cell_right = int(right_ * 2.F + 2.F); // NOLINT
|
2022-03-14 01:51:46 +08:00
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const int y = box_.y_min;
|
2022-03-14 01:51:46 +08:00
|
|
|
for (int x = box_.x_min; x <= box_.x_max; ++x) {
|
|
|
|
Pixel& pixel = screen.PixelAt(x, y);
|
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const int a = (x - box_.x_min) * 2;
|
|
|
|
const int b = a + 1;
|
|
|
|
const bool a_empty = demi_cell_left == a || demi_cell_right == a;
|
|
|
|
const bool b_empty = demi_cell_left == b || demi_cell_right == b;
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
if (!a_empty && !b_empty) {
|
|
|
|
pixel.character = "─";
|
|
|
|
pixel.automerge = true;
|
|
|
|
} else {
|
2022-03-31 08:17:43 +08:00
|
|
|
pixel.character = a_empty ? "╶" : "╴"; // NOLINT
|
2022-03-14 01:51:46 +08:00
|
|
|
pixel.automerge = false;
|
|
|
|
}
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (demi_cell_left <= a && b <= demi_cell_right) {
|
2022-03-14 01:51:46 +08:00
|
|
|
pixel.foreground_color = selected_color_;
|
2022-03-31 08:17:43 +08:00
|
|
|
} else {
|
2022-03-14 01:51:46 +08:00
|
|
|
pixel.foreground_color = unselected_color_;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float left_;
|
|
|
|
float right_;
|
|
|
|
Color unselected_color_;
|
2022-03-31 08:17:43 +08:00
|
|
|
Color selected_color_;
|
2022-03-14 01:51:46 +08:00
|
|
|
};
|
2022-03-31 08:17:43 +08:00
|
|
|
return std::make_shared<Impl>(left, right, unselected_color, selected_color);
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Draw an vertical bar, with the area in between up/downcolored
|
|
|
|
/// differently.
|
|
|
|
/// @param up the left limit of the active area.
|
|
|
|
/// @param down the right limit of the active area.
|
|
|
|
/// @param selected_color the color of the selected area.
|
|
|
|
/// @param unselected_color the color of the unselected area.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = separatorHSelector(2,5, Color::White, Color::Blue);
|
|
|
|
/// ```
|
|
|
|
Element separatorVSelector(float up,
|
|
|
|
float down,
|
2022-03-31 08:17:43 +08:00
|
|
|
Color unselected_color,
|
|
|
|
Color selected_color) {
|
2022-03-14 01:51:46 +08:00
|
|
|
class Impl : public Node {
|
|
|
|
public:
|
2022-03-31 08:17:43 +08:00
|
|
|
Impl(float up, float down, Color unselected_color, Color selected_color)
|
2022-03-14 01:51:46 +08:00
|
|
|
: up_(up),
|
|
|
|
down_(down),
|
2022-03-31 08:17:43 +08:00
|
|
|
unselected_color_(unselected_color),
|
|
|
|
selected_color_(selected_color) {}
|
2022-03-14 01:51:46 +08:00
|
|
|
void ComputeRequirement() override {
|
|
|
|
requirement_.min_x = 1;
|
|
|
|
requirement_.min_y = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Render(Screen& screen) override {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (box_.x_max < box_.x_min) {
|
2022-03-14 01:51:46 +08:00
|
|
|
return;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
// This are the two location with an empty demi-cell.
|
2022-12-20 01:51:25 +08:00
|
|
|
const int demi_cell_up = int(up_ * 2 - 1);
|
|
|
|
const int demi_cell_down = int(down_ * 2 + 2);
|
2022-03-14 01:51:46 +08:00
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const int x = box_.x_min;
|
2022-03-14 01:51:46 +08:00
|
|
|
for (int y = box_.y_min; y <= box_.y_max; ++y) {
|
|
|
|
Pixel& pixel = screen.PixelAt(x, y);
|
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const int a = (y - box_.y_min) * 2;
|
|
|
|
const int b = a + 1;
|
|
|
|
const bool a_empty = demi_cell_up == a || demi_cell_down == a;
|
|
|
|
const bool b_empty = demi_cell_up == b || demi_cell_down == b;
|
2022-03-14 01:51:46 +08:00
|
|
|
|
|
|
|
if (!a_empty && !b_empty) {
|
|
|
|
pixel.character = "│";
|
|
|
|
pixel.automerge = true;
|
|
|
|
} else {
|
2022-03-31 08:17:43 +08:00
|
|
|
pixel.character = a_empty ? "╷" : "╵"; // NOLINT
|
2022-03-14 01:51:46 +08:00
|
|
|
pixel.automerge = false;
|
|
|
|
}
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (demi_cell_up <= a && b <= demi_cell_down) {
|
2022-03-14 01:51:46 +08:00
|
|
|
pixel.foreground_color = selected_color_;
|
2022-03-31 08:17:43 +08:00
|
|
|
} else {
|
2022-03-14 01:51:46 +08:00
|
|
|
pixel.foreground_color = unselected_color_;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2022-03-14 01:51:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float up_;
|
|
|
|
float down_;
|
|
|
|
Color unselected_color_;
|
2022-03-31 08:17:43 +08:00
|
|
|
Color selected_color_;
|
2022-03-14 01:51:46 +08:00
|
|
|
};
|
2022-03-31 08:17:43 +08:00
|
|
|
return std::make_shared<Impl>(up, down, unselected_color, selected_color);
|
2022-03-14 01:51:46 +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.
|