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