FTXUI  5.0.0
C++ functional terminal UI.
scroll_indicator.cpp
Go to the documentation of this file.
1// Copyright 2021 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <algorithm> // for max
5#include <memory> // for make_shared, __shared_ptr_access
6#include <string> // for string
7#include <utility> // for move
8#include <vector> // for __alloc_traits<>::value_type
9
10#include "ftxui/dom/elements.hpp" // for Element, vscroll_indicator, hscroll_indicator
11#include "ftxui/dom/node.hpp" // for Node, Elements
12#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
13#include "ftxui/dom/requirement.hpp" // for Requirement
14#include "ftxui/screen/box.hpp" // for Box
15#include "ftxui/screen/screen.hpp" // for Screen, Pixel
16
17namespace ftxui {
18
19/// @brief Display a vertical scrollbar to the right.
20/// colors.
21/// @ingroup dom
23 class Impl : public NodeDecorator {
25
26 void ComputeRequirement() override {
28 requirement_ = children_[0]->requirement();
29 requirement_.min_x++;
30 }
31
32 void SetBox(Box box) override {
33 box_ = box;
34 box.x_max--;
35 children_[0]->SetBox(box);
36 }
37
38 void Render(Screen& screen) final {
40
41 const Box& stencil = screen.stencil;
42
43 const int size_inner = box_.y_max - box_.y_min;
44 if (size_inner <= 0) {
45 return;
46 }
47 const int size_outter = stencil.y_max - stencil.y_min + 1;
48 if (size_outter >= size_inner) {
49 return;
50 }
51
52 int size = 2 * size_outter * size_outter / size_inner;
53 size = std::max(size, 1);
54
55 const int start_y =
56 2 * stencil.y_min + //
57 2 * (stencil.y_min - box_.y_min) * size_outter / size_inner;
58
59 const int x = stencil.x_max;
60 for (int y = stencil.y_min; y <= stencil.y_max; ++y) {
61 const int y_up = 2 * y + 0;
62 const int y_down = 2 * y + 1;
63 const bool up = (start_y <= y_up) && (y_up <= start_y + size);
64 const bool down = (start_y <= y_down) && (y_down <= start_y + size);
65
66 const char* c = up ? (down ? "┃" : "╹") : (down ? "╻" : " "); // NOLINT
67 screen.PixelAt(x, y).character = c;
68 }
69 }
70 };
71 return std::make_shared<Impl>(std::move(child));
72}
73
74/// @brief Display an horizontal scrollbar to the bottom.
75/// colors.
76/// @ingroup dom
78 class Impl : public NodeDecorator {
80
81 void ComputeRequirement() override {
83 requirement_ = children_[0]->requirement();
84 requirement_.min_y++;
85 }
86
87 void SetBox(Box box) override {
88 box_ = box;
89 box.y_max--;
90 children_[0]->SetBox(box);
91 }
92
93 void Render(Screen& screen) final {
95
96 const Box& stencil = screen.stencil;
97
98 const int size_inner = box_.x_max - box_.x_min;
99 if (size_inner <= 0) {
100 return;
101 }
102 const int size_outter = stencil.x_max - stencil.x_min + 1;
103 if (size_outter >= size_inner) {
104 return;
105 }
106
107 int size = 2 * size_outter * size_outter / size_inner;
108 size = std::max(size, 1);
109
110 const int start_x =
111 2 * stencil.x_min + //
112 2 * (stencil.x_min - box_.x_min) * size_outter / size_inner;
113
114 const int y = stencil.y_max;
115 for (int x = stencil.x_min; x <= stencil.x_max; ++x) {
116 const int x_left = 2 * x + 0;
117 const int x_right = 2 * x + 1;
118 const bool left = (start_x <= x_left) && (x_left <= start_x + size);
119 const bool right = (start_x <= x_right) && (x_right <= start_x + size);
120
121 const char* c =
122 left ? (right ? "─" : "╴") : (right ? "╶" : " "); // NOLINT
123 screen.PixelAt(x, y).character = c;
124 }
125 }
126 };
127 return std::make_shared<Impl>(std::move(child));
128}
129
130} // namespace ftxui
NodeDecorator(Element child)
void ComputeRequirement() override
Compute how much space an elements needs.
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition: node.cpp:32
A rectangular grid of Pixel.
Definition: screen.hpp:63
Pixel & PixelAt(int x, int y)
Access a cell (Pixel) at a given position.
Definition: screen.cpp:470
Element vscroll_indicator(Element)
Display a vertical scrollbar to the right. colors.
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition: size.cpp:90
std::shared_ptr< Node > Element
Definition: elements.hpp:23
Element hscroll_indicator(Element)
Display an horizontal scrollbar to the bottom. colors.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition: node.cpp:47
int x_max
Definition: box.hpp:11
int y_min
Definition: box.hpp:12
int y_max
Definition: box.hpp:13
int x_min
Definition: box.hpp:10
std::string character
Definition: screen.hpp:47