FTXUI  5.0.0
C++ functional terminal UI.
focus.cpp
Go to the documentation of this file.
1// Copyright 2020 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 <memory> // for make_shared
5#include <utility> // for move
6
7#include "ftxui/dom/elements.hpp" // for Decorator, Element, focusPosition, focusPositionRelative
8#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
9#include "ftxui/dom/requirement.hpp" // for Requirement, Requirement::NORMAL, Requirement::Selection
10#include "ftxui/screen/box.hpp" // for Box
11
12namespace ftxui {
13
14/// @brief Used inside a `frame`, this force the view to be scrolled toward a
15/// a given position. The position is expressed in proportion of the requested
16/// size.
17///
18/// For instance:
19/// - (0, 0) means that the view is scrolled toward the upper left.
20/// - (1, 0) means that the view is scrolled toward the upper right.
21/// - (0, 1) means that the view is scrolled toward the bottom left.
22/// @ingroup dom
23///
24/// ### Example
25///
26/// ```cpp
27/// Element document = huge_document()
28/// | focusPositionRelative(0.f, 1.f)
29/// | frame;
30/// ```
32 class Impl : public NodeDecorator {
33 public:
34 Impl(Element child, float x, float y)
35 : NodeDecorator(std::move(child)), x_(x), y_(y) {}
36
37 void ComputeRequirement() override {
39 requirement_.selection = Requirement::Selection::NORMAL;
40
41 Box& box = requirement_.selected_box;
42 box.x_min = int(float(requirement_.min_x) * x_);
43 box.y_min = int(float(requirement_.min_y) * y_);
44 box.x_max = int(float(requirement_.min_x) * x_);
45 box.y_max = int(float(requirement_.min_y) * y_);
46 }
47
48 private:
49 const float x_;
50 const float y_;
51 };
52
53 return [x, y](Element child) {
54 return std::make_shared<Impl>(std::move(child), x, y);
55 };
56}
57
58/// @brief Used inside a `frame`, this force the view to be scrolled toward a
59/// a given position. The position is expressed in the numbers of cells.
60///
61/// @ingroup dom
62///
63/// ### Example
64///
65/// ```cpp
66/// Element document = huge_document()
67/// | focusPosition(10, 10)
68/// | frame;
69/// ```
70Decorator focusPosition(int x, int y) {
71 class Impl : public NodeDecorator {
72 public:
73 Impl(Element child, int x, int y)
74 : NodeDecorator(std::move(child)), x_(x), y_(y) {}
75
76 void ComputeRequirement() override {
78 requirement_.selection = Requirement::Selection::NORMAL;
79
80 Box& box = requirement_.selected_box;
81 box.x_min = x_;
82 box.y_min = y_;
83 box.x_max = x_;
84 box.y_max = y_;
85 }
86
87 private:
88 const int x_;
89 const int y_;
90 };
91
92 return [x, y](Element child) {
93 return std::make_shared<Impl>(std::move(child), x, y);
94 };
95}
96
97} // namespace ftxui
void ComputeRequirement() override
Compute how much space an elements needs.
Decorator focusPositionRelative(float x, float y)
Used inside a frame, this force the view to be scrolled toward a a given position....
Definition: focus.cpp:31
std::function< Element(Element)> Decorator
Definition: elements.hpp:25
std::shared_ptr< Node > Element
Definition: elements.hpp:23
Decorator focusPosition(int x, int y)
Used inside a frame, this force the view to be scrolled toward a a given position....
Definition: focus.cpp:70
int x_max
Definition: box.hpp:11
int y_min
Definition: box.hpp:12
int y_max
Definition: box.hpp:13
int x_min
Definition: box.hpp:10