FTXUI  4.1.1
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 box_ = box;
31 box.x_max--;
32 children_[0]->SetBox(box);
33 }
34
35 void Render(Screen& screen) final {
36 NodeDecorator::Render(screen);
37
38 const Box& stencil = screen.stencil;
39
40 const int size_inner = box_.y_max - box_.y_min;
41 if (size_inner <= 0) {
42 return;
43 }
44 const int size_outter = stencil.y_max - stencil.y_min + 1;
45 if (size_outter >= size_inner) {
46 return;
47 }
48
49 int size = 2 * size_outter * size_outter / size_inner;
50 size = std::max(size, 1);
51
52 const int start_y =
53 2 * stencil.y_min + //
54 2 * (stencil.y_min - box_.y_min) * size_outter / size_inner;
55
56 const int x = stencil.x_max;
57 for (int y = stencil.y_min; y <= stencil.y_max; ++y) {
58 const int y_up = 2 * y + 0;
59 const int y_down = 2 * y + 1;
60 const bool up = (start_y <= y_up) && (y_up <= start_y + size);
61 const bool down = (start_y <= y_down) && (y_down <= start_y + size);
62
63 const char* c = up ? (down ? "┃" : "╹") : (down ? "╻" : " "); // NOLINT
64 screen.PixelAt(x, y) = Pixel();
65 screen.PixelAt(x, y).character = c;
66 }
67 };
68 };
69 return std::make_shared<Impl>(std::move(child));
70}
71
72} // namespace ftxui
73
74// Copyright 2021 Arthur Sonzogni. All rights reserved.
75// Use of this source code is governed by the MIT license that can be found in
76// the LICENSE file.
A rectangular grid of Pixel.
Definition screen.hpp:57
Pixel & PixelAt(int x, int y)
Access a Pixel at a given position.
Definition screen.cpp:470
Element vscroll_indicator(Element)
Add a filter that will invert the foreground and the background colors.
std::shared_ptr< Node > Element
Definition elements.hpp:19
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:44
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