FTXUI  3.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
scroll_indicator.cpp
Go to the documentation of this file.
1#include <algorithm> // for max
2#include <memory> // for make_shared, __shared_ptr_access
3#include <string> // for string
4#include <utility> // for move
5#include <vector> // for __alloc_traits<>::value_type
6
7#include "ftxui/dom/elements.hpp" // for Element, vscroll_indicator
8#include "ftxui/dom/node.hpp" // for Node, Elements
9#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
10#include "ftxui/dom/requirement.hpp" // for Requirement
11#include "ftxui/screen/box.hpp" // for Box
12#include "ftxui/screen/screen.hpp" // for Screen, Pixel
13
14namespace ftxui {
15
16/// @brief Add a filter that will invert the foreground and the background
17/// colors.
18/// @ingroup dom
20 class Impl : public NodeDecorator {
21 using NodeDecorator::NodeDecorator;
22
23 void ComputeRequirement() override {
24 NodeDecorator::ComputeRequirement();
25 requirement_ = children_[0]->requirement();
26 requirement_.min_x++;
27 }
28
29 void SetBox(Box box) override {
30 NodeDecorator::SetBox(box);
31 if (box_.x_min > box_.x_max) {
32 box_.x_max--;
33 }
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 int size_inner = box_.y_max - box_.y_min;
43 int size_outter = stencil.y_max - stencil.y_min + 1;
44 if (size_outter >= size_inner) {
45 return;
46 }
47
48 int size = 2 * size_outter * size_outter / size_inner;
49 size = std::max(size, 1);
50
51 int start_y = 2 * stencil.y_min + //
52 2 * (stencil.y_min - box_.y_min) * size_outter / size_inner;
53
54 const int x = stencil.x_max;
55 for (int y = stencil.y_min; y <= stencil.y_max; ++y) {
56 int y_up = 2 * y + 0;
57 int y_down = 2 * y + 1;
58 bool up = (start_y <= y_up) && (y_up <= start_y + size);
59 bool down = (start_y <= y_down) && (y_down <= start_y + size);
60
61 const char* c = up ? (down ? "┃" : "╹") : (down ? "╻" : " "); // NOLINT
62 screen.PixelAt(x, y) = Pixel();
63 screen.PixelAt(x, y).character = c;
64 }
65 };
66 };
67 return std::make_shared<Impl>(std::move(child));
68}
69
70} // namespace ftxui
71
72// Copyright 2021 Arthur Sonzogni. All rights reserved.
73// Use of this source code is governed by the MIT license that can be found in
74// the LICENSE file.
A rectangular grid of Pixel.
Definition screen.hpp:53
Pixel & PixelAt(int x, int y)
Access a Pixel at a given position.
Definition screen.cpp:447
Element vscroll_indicator(Element)
Add a filter that will invert the foreground and the background colors.
std::shared_ptr< Node > Element
Definition elements.hpp:18
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:43
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:85
int x_max
Definition box.hpp:8
int y_min
Definition box.hpp:9
int y_max
Definition box.hpp:10
A unicode character and its associated style.
Definition screen.hpp:16
std::string character
Definition screen.hpp:21