FTXUI  0.11.1
C++ functional terminal UI.
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 
14 namespace 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 {
22 
23  void ComputeRequirement() override {
25  requirement_ = children_[0]->requirement();
26  requirement_.min_x++;
27  }
28 
29  void SetBox(Box box) override {
30  Node::SetBox(box);
31  if (box_.x_min > box_.x_max)
32  box_.x_max--;
33  children_[0]->SetBox(box);
34  }
35 
36  void Render(Screen& screen) final {
37  Node::Render(screen);
38 
39  const Box& stencil = screen.stencil;
40 
41  int size_inner = box_.y_max - box_.y_min;
42  int size_outter = stencil.y_max - stencil.y_min;
43  if (size_outter >= size_inner)
44  return;
45 
46  int start_y = 2 * stencil.y_min + 2 * float(stencil.y_min - box_.y_min) *
47  (size_outter - 1) / size_inner;
48  int size = 2 * float(size_outter) * (size_outter - 1) / size_inner + 2;
49  size = std::max(size, 1);
50 
51  const int x = stencil.x_max;
52  for (int y = stencil.y_min; y <= stencil.y_max; ++y) {
53  bool up = (2 * y + -1 >= start_y) && (2 * y - 1 <= start_y + size);
54  bool down = (2 * y - 0 >= start_y) && (2 * y - 0 <= start_y + size);
55 
56  const char* c = up ? (down ? "┃" : "╹") : (down ? "╻" : " ");
57  screen.PixelAt(x, y).character = c;
58  screen.PixelAt(x, y).inverted = true;
59  }
60  };
61  };
62  return std::make_shared<Impl>(std::move(child));
63 }
64 
65 } // namespace ftxui
66 
67 // Copyright 2021 Arthur Sonzogni. All rights reserved.
68 // Use of this source code is governed by the MIT license that can be found in
69 // the LICENSE file.
NodeDecorator(Element child)
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition: node.cpp:21
virtual void ComputeRequirement()
Compute how much space an elements needs.
Definition: node.cpp:14
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition: node.cpp:27
A rectangular grid of Pixel.
Definition: screen.hpp:49
Pixel & PixelAt(int x, int y)
Access a Pixel at a given position.
Definition: screen.cpp:407
Element vscroll_indicator(Element)
Add a filter that will invert the foreground and the background colors.
std::shared_ptr< Node > Element
Definition: elements.hpp:16
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:40
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition: size.cpp:86
int x_max
Definition: box.hpp:8
int y_min
Definition: box.hpp:9
int y_max
Definition: box.hpp:10
bool inverted
Definition: screen.hpp:29
std::string character
Definition: screen.hpp:19