FTXUI  5.0.0
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#include <vector> // for __alloc_traits<>::value_type
9
10#include "ftxui/dom/elements.hpp" // for Element, vscroll_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 Add a filter that will invert the foreground and the background
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
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) = Pixel();
68 screen.PixelAt(x, y).character = c;
69 }
70 }
71 };
72 return std::make_shared<Impl>(std::move(child));
73}
74
75} // 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
Element vscroll_indicator(Element)
Add a filter that will invert the foreground and the background 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
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition slider.cpp:339
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
A unicode character and its associated style.
Definition screen.hpp:20