Feature: hscroll_indicator (#753)

This is the symetrical of `vscroll_indicator`.

Requested by @ibrahimnasson.

Fixed:https://github.com/ArthurSonzogni/FTXUI/issues/752
This commit is contained in:
Arthur Sonzogni
2023-09-26 23:08:42 +02:00
committed by GitHub
parent 20d4be286b
commit c24a274292
16 changed files with 185 additions and 22 deletions

View File

@@ -7,7 +7,7 @@
#include <utility> // for move
#include <vector> // for __alloc_traits<>::value_type
#include "ftxui/dom/elements.hpp" // for Element, vscroll_indicator
#include "ftxui/dom/elements.hpp" // for Element, vscroll_indicator, hscroll_indicator
#include "ftxui/dom/node.hpp" // for Node, Elements
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
#include "ftxui/dom/requirement.hpp" // for Requirement
@@ -16,7 +16,7 @@
namespace ftxui {
/// @brief Add a filter that will invert the foreground and the background
/// @brief Display a vertical scrollbar to the right.
/// colors.
/// @ingroup dom
Element vscroll_indicator(Element child) {
@@ -72,4 +72,61 @@ Element vscroll_indicator(Element child) {
return std::make_shared<Impl>(std::move(child));
}
/// @brief Display an horizontal scrollbar to the bottom.
/// colors.
/// @ingroup dom
Element hscroll_indicator(Element child) {
class Impl : public NodeDecorator {
using NodeDecorator::NodeDecorator;
void ComputeRequirement() override {
NodeDecorator::ComputeRequirement();
requirement_ = children_[0]->requirement();
requirement_.min_y++;
}
void SetBox(Box box) override {
box_ = box;
box.y_max--;
children_[0]->SetBox(box);
}
void Render(Screen& screen) final {
NodeDecorator::Render(screen);
const Box& stencil = screen.stencil;
const int size_inner = box_.x_max - box_.x_min;
if (size_inner <= 0) {
return;
}
const int size_outter = stencil.x_max - stencil.x_min + 1;
if (size_outter >= size_inner) {
return;
}
int size = 2 * size_outter * size_outter / size_inner;
size = std::max(size, 1);
const int start_x =
2 * stencil.x_min + //
2 * (stencil.x_min - box_.x_min) * size_outter / size_inner;
const int y = stencil.y_max;
for (int x = stencil.x_min; x <= stencil.x_max; ++x) {
const int x_left = 2 * x + 0;
const int x_right = 2 * x + 1;
const bool left = (start_x <= x_left) && (x_left <= start_x + size);
const bool right = (start_x <= x_right) && (x_right <= start_x + size);
const char* c =
left ? (right ? "" : "") : (right ? "" : " "); // NOLINT
screen.PixelAt(x, y) = Pixel();
screen.PixelAt(x, y).character = c;
}
}
};
return std::make_shared<Impl>(std::move(child));
}
} // namespace ftxui