FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/dom/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 在 `frame` 內部使用,這會強制視圖滾動到給定位置。該位置以請求大小的比例表示。
15///
16/// 例如:
17/// - (0, 0) 表示視圖滾動到左上方。
18/// - (1, 0) 表示視圖滾動到右上方。
19/// - (0, 1) 表示視圖滾動到左下方。
20/// @ingroup dom
21///
22/// ### 範例
23///
24/// ```cpp
25/// Element document = huge_document()
26/// | focusPositionRelative(0.f, 1.f)
27/// | frame;
28/// ```
30 class Impl : public NodeDecorator {
31 public:
32 Impl(Element child, float x, float y)
33 : NodeDecorator(std::move(child)), x_(x), y_(y) {}
34
35 void ComputeRequirement() override {
36 NodeDecorator::ComputeRequirement();
37 requirement_.focused.enabled = true;
38 requirement_.focused.node = this;
39 requirement_.focused.box.x_min = int(float(requirement_.min_x) * x_);
40 requirement_.focused.box.y_min = int(float(requirement_.min_y) * y_);
41 requirement_.focused.box.x_max = int(float(requirement_.min_x) * x_);
42 requirement_.focused.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 在 `frame` 內部使用,這會強制視圖滾動到給定位置。該位置以單元格數表示。
56///
57/// @ingroup dom
58///
59/// ### 範例
60///
61/// ```cpp
62/// Element document = huge_document()
63/// | focusPosition(10, 10)
64/// | frame;
65/// ```
66Decorator focusPosition(int x, int y) {
67 class Impl : public NodeDecorator {
68 public:
69 Impl(Element child, int x, int y)
70 : NodeDecorator(std::move(child)), x_(x), y_(y) {}
71
72 void ComputeRequirement() override {
73 NodeDecorator::ComputeRequirement();
74 requirement_.focused.enabled = false;
75
76 Box& box = requirement_.focused.box;
77 box.x_min = x_;
78 box.y_min = y_;
79 box.x_max = x_;
80 box.y_max = y_;
81 }
82
83 private:
84 const int x_;
85 const int y_;
86 };
87
88 return [x, y](Element child) {
89 return std::make_shared<Impl>(std::move(child), x, y);
90 };
91}
92
93} // namespace ftxui
Decorator focusPositionRelative(float x, float y)
在 frame 內部使用,這會強制視圖滾動到給定位置。該位置以請求大小的比例表示。
Decorator focusPosition(int x, int y)
在 frame 內部使用,這會強制視圖滾動到給定位置。該位置以單元格數表示。
int x_max
Definition box.hpp:16
int y_min
Definition box.hpp:17
int y_max
Definition box.hpp:18
int x_min
Definition box.hpp:15
Box 是一個表示二維空間中矩形區域的結構。
Definition box.hpp:14
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::function< Element(Element)> Decorator
Definition elements.hpp:24
std::shared_ptr< Node > Element
Definition elements.hpp:22