FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
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
9#include "ftxui/dom/elements.hpp" // for Element, vscroll_indicator, hscroll_indicator
10#include "ftxui/dom/node.hpp" // for Node, Elements
11#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
12#include "ftxui/dom/requirement.hpp" // for Requirement
13#include "ftxui/screen/box.hpp" // for Box
14#include "ftxui/screen/screen.hpp" // for Screen, Pixel
15
16 namespace ftxui {
17
18/// @brief Display a vertical scrollbar on the right.
19/// Colors follow the content.
20/// @ingroup dom
21 Element vscroll_indicator(Element child) {
22 class Impl : public NodeDecorator {
23 using NodeDecorator::NodeDecorator;
24
25 void ComputeRequirement() override {
26 NodeDecorator::ComputeRequirement();
27 requirement_ = children_[0]->requirement();
28 requirement_.min_x++;
29 }
30
31 void SetBox(Box box) override {
32 box_ = box;
33 box.x_max--;
34 children_[0]->SetBox(box);
35 }
36
37 void Render(Screen& screen) final {
38 NodeDecorator::Render(screen);
39
40 const Box& stencil = screen.stencil;
41
42 const int size_inner = box_.y_max - box_.y_min;
43 if (size_inner <= 0) {
44 return;
45 }
46 const int size_outter = stencil.y_max - stencil.y_min + 1;
47 if (size_outter >= size_inner) {
48 return;
49 }
50
51 int size = 2 * size_outter * size_outter / size_inner;
52 size = std::max(size, 1);
53
54 const int start_y =
55 2 * stencil.y_min + //
56 2 * (stencil.y_min - box_.y_min) * size_outter / size_inner;
57
58 const int x = stencil.x_max;
59 for (int y = stencil.y_min; y <= stencil.y_max; ++y) {
60 const int y_up = 2 * y + 0;
61 const int y_down = 2 * y + 1;
62 const bool up = (start_y <= y_up) && (y_up <= start_y + size);
63 const bool down = (start_y <= y_down) && (y_down <= start_y + size);
64
65 const char* c = up ? (down ? "┃" : "╹") : (down ? "╻" : " "); // NOLINT
66 screen.PixelAt(x, y).character = c;
67 }
68 }
69 };
70 return std::make_shared<Impl>(std::move(child));
71}
72
73/// @brief Display a horizontal scrollbar at the bottom.
74/// Colors follow the content.
75/// @ingroup dom
76 Element hscroll_indicator(Element child) {
77 class Impl : public NodeDecorator {
78 using NodeDecorator::NodeDecorator;
79
80 void ComputeRequirement() override {
81 NodeDecorator::ComputeRequirement();
82 requirement_ = children_[0]->requirement();
83 requirement_.min_y++;
84 }
85
86 void SetBox(Box box) override {
87 box_ = box;
88 box.y_max--;
89 children_[0]->SetBox(box);
90 }
91
92 void Render(Screen& screen) final {
93 NodeDecorator::Render(screen);
94
95 const Box& stencil = screen.stencil;
96
97 const int size_inner = box_.x_max - box_.x_min;
98 if (size_inner <= 0) {
99 return;
100 }
101 const int size_outter = stencil.x_max - stencil.x_min + 1;
102 if (size_outter >= size_inner) {
103 return;
104 }
105
106 int size = 2 * size_outter * size_outter / size_inner;
107 size = std::max(size, 1);
108
109 const int start_x =
110 2 * stencil.x_min + //
111 2 * (stencil.x_min - box_.x_min) * size_outter / size_inner;
112
113 const int y = stencil.y_max;
114 for (int x = stencil.x_min; x <= stencil.x_max; ++x) {
115 const int x_left = 2 * x + 0;
116 const int x_right = 2 * x + 1;
117 const bool left = (start_x <= x_left) && (x_left <= start_x + size);
118 const bool right = (start_x <= x_right) && (x_right <= start_x + size);
119
120 const char* c =
121 left ? (right ? "─" : "╴") : (right ? "╶" : " "); // NOLINT
122 screen.PixelAt(x, y).character = c;
123 }
124 }
125 };
126 return std::make_shared<Impl>(std::move(child));
127}
128
129} // namespace ftxui
box.hpp
ftxui::NodeDecorator
Definition node_decorator.hpp:16
elements.hpp
ftxui::vscroll_indicator
Element vscroll_indicator(Element)
Display a vertical scrollbar on the right. Colors follow the content.
Definition scroll_indicator.cpp:21
ftxui::hscroll_indicator
Element hscroll_indicator(Element)
Display a horizontal scrollbar at the bottom. Colors follow the content.
Definition scroll_indicator.cpp:76
ftxui::Pixel::character
std::string character
Definition pixel.hpp:45
ftxui::Image::PixelAt
Pixel & PixelAt(int x, int y)
Access a cell (Pixel) at a given position.
Definition image.cpp:43
ftxui::Box::x_max
int x_max
Definition box.hpp:18
ftxui::Box::y_min
int y_min
Definition box.hpp:19
ftxui::Image::stencil
Box stencil
Definition image.hpp:38
ftxui::Box::y_max
int y_max
Definition box.hpp:20
ftxui::Box::x_min
int x_min
Definition box.hpp:17
ftxui::Screen
A rectangular grid of Pixel.
Definition screen.hpp:27
ftxui::Box
Box is a structure that represents a rectangular area in a 2D space.
Definition box.hpp:16
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
node.hpp
node_decorator.hpp
requirement.hpp
left
std::uint8_t left
Definition screen.cpp:129
down
std::uint8_t down
Definition screen.cpp:132
right
std::uint8_t right
Definition screen.cpp:131
screen.hpp