FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
hoverable.cpp
Go to the documentation of this file.
1// Copyright 2022 Arthur Sonzogni. All rights reserved.
2// 本原始碼的使用受 MIT 授權條款約束,詳情請參閱 LICENSE 檔案。
3#include <functional> // for function
4#include <utility> // for move
5
6#include "ftxui/component/component.hpp" // for ComponentDecorator, Hoverable, Make
7#include "ftxui/component/component_base.hpp" // for ComponentBase
8#include "ftxui/component/event.hpp" // for Event
9#include "ftxui/component/mouse.hpp" // for Mouse
10#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
11#include "ftxui/dom/elements.hpp" // for operator|, reflect, Element
12#include "ftxui/screen/box.hpp" // for Box
13
14namespace ftxui {
15
16namespace {
17
18void Post(std::function<void()> f) {
19 if (auto* screen = ScreenInteractive::Active()) {
20 screen->Post(std::move(f));
21 return;
22 }
23 f();
24}
25
26} // namespace
27
28/// @brief 包裝一個元件。提供能力以判斷滑鼠是否懸停在其上方。
29/// @param component 被包裝的元件。
30/// @param hover 反映元件是否被懸停的值。
31/// @ingroup component
32///
33/// ### 範例
34///
35/// ```cpp
36/// auto button = Button("exit", screen.ExitLoopClosure());
37/// bool hover = false;
38/// auto button_hover = Hoverable(button, &hover);
39/// ```
40// NOLINTNEXTLINE
42 class Impl : public ComponentBase {
43 public:
44 Impl(Component component, bool* hover)
45 : component_(std::move(component)), hover_(hover) {
46 Add(component_);
47 }
48
49 private:
50 Element OnRender() override {
51 return ComponentBase::OnRender() | reflect(box_);
52 }
53
54 bool OnEvent(Event event) override {
55 if (event.is_mouse()) {
56 *hover_ = box_.Contain(event.mouse().x, event.mouse().y) &&
57 CaptureMouse(event);
58 }
59
60 return ComponentBase::OnEvent(event);
61 }
62
63 Component component_;
64 bool* hover_;
65 Box box_;
66 };
67
68 return Make<Impl>(component, hover);
69}
70
71/// @brief 包裝一個元件。使用回呼函數。
72/// @param component 被包裝的元件。
73/// @param on_enter 進入時的回呼函數
74/// @param on_leave 離開時的回呼函數
75/// @ingroup component
76///
77/// ### 範例
78///
79/// ```cpp
80/// auto button = Button("exit", screen.ExitLoopClosure());
81/// bool hover = false;
82/// auto button_hover = Hoverable(button, &hover);
83/// ```
85 std::function<void()> on_enter,
86 std::function<void()> on_leave) {
87 class Impl : public ComponentBase {
88 public:
90 std::function<void()> on_enter,
91 std::function<void()> on_leave)
92 : component_(std::move(component)),
93 on_enter_(std::move(on_enter)),
94 on_leave_(std::move(on_leave)) {
95 Add(component_);
96 }
97
98 private:
99 Element OnRender() override {
100 return ComponentBase::OnRender() | reflect(box_);
101 }
102
103 bool OnEvent(Event event) override {
104 if (event.is_mouse()) {
105 const bool hover = box_.Contain(event.mouse().x, event.mouse().y) &&
106 CaptureMouse(event);
107 if (hover != hover_) {
108 Post(hover ? on_enter_ : on_leave_);
109 }
110 hover_ = hover;
111 }
112
113 return ComponentBase::OnEvent(event);
114 }
115
116 Component component_;
117 Box box_;
118 bool hover_ = false;
119 std::function<void()> on_enter_;
120 std::function<void()> on_leave_;
121 };
122
123 return Make<Impl>(std::move(component), std::move(on_enter),
124 std::move(on_leave));
125}
126
127/// @brief 包裝一個元件。提供能力以判斷滑鼠是否懸停在其上方。
128/// @param hover 反映元件是否被懸停的值。
129/// @ingroup component
130///
131/// ### 範例
132///
133/// ```cpp
134/// bool hover = false;
135/// auto button = Button("exit", screen.ExitLoopClosure());
136/// button |= Hoverable(&hover);
137/// ```
139 return [hover](Component component) {
140 return Hoverable(std::move(component), hover);
141 };
142}
143
144/// @brief 包裝一個元件。提供能力以判斷滑鼠是否懸停在其上方。
145/// @param on_enter 當滑鼠懸停在元件上時呼叫。
146/// @param on_leave 當滑鼠離開元件時呼叫。
147/// @ingroup component
148///
149/// ### 範例
150///
151/// ```cpp
152/// auto button = Button("exit", screen.ExitLoopClosure());
153/// int on_enter_cnt = 0;
154/// int on_leave_cnt = 0;
155/// button |= Hoverable(
156/// [&]{ on_enter_cnt++; },
157/// [&]{ on_leave_cnt++; }
158/// );
159/// ```
160// NOLINTNEXTLINE
161ComponentDecorator Hoverable(std::function<void()> on_enter,
162 // NOLINTNEXTLINE
163 std::function<void()> on_leave) {
164 return [on_enter, on_leave](Component component) {
165 return Hoverable(std::move(component), on_enter, on_leave);
166 };
167}
168
169/// @brief 包裝一個元件。提供能力以判斷滑鼠是否懸停在其上方。
170/// @param component 被包裝的元件。
171/// @param on_change 當滑鼠進入或離開元件時呼叫。
172/// @ingroup component
173///
174/// ### 範例
175///
176/// ```cpp
177/// auto button = Button("exit", screen.ExitLoopClosure());
178/// bool hovered = false;
179/// auto button_hoverable = Hoverable(button,
180/// [&](bool hover) { hovered = hover;});
181/// ```
182// NOLINTNEXTLINE
183Component Hoverable(Component component, std::function<void(bool)> on_change) {
184 return Hoverable(
185 std::move(component), //
186 [on_change] { on_change(true); }, //
187 [on_change] { on_change(false); } //
188 );
189}
190
191/// @brief 包裝一個元件。提供能力以判斷滑鼠是否懸停在其上方。
192/// @param on_change 當滑鼠進入或離開元件時呼叫。
193/// @ingroup component
194///
195/// ### 範例
196///
197/// ```cpp
198/// auto button = Button("exit", screen.ExitLoopClosure());
199/// bool hovered = false;
200/// button |= Hoverable([&](bool hover) { hovered = hover;});
201/// ```
202// NOLINTNEXTLINE
203ComponentDecorator Hoverable(std::function<void(bool)> on_change) {
204 return [on_change](Component component) {
205 return Hoverable(std::move(component), on_change);
206 };
207}
208
209} // namespace ftxui
auto component
Definition gallery.cpp:127
bool is_mouse() const
Definition event.hpp:107
struct Mouse mouse
Definition event.hpp:142
static ScreenInteractive * Active()
Return the currently active screen, or null if none.
它將自己實作為 ftxui::Element 進行渲染。它透過回應 ftxui::Event 來實現鍵盤導航。
Component Hoverable(Component component, bool *hover)
包裝一個元件。提供能力以判斷滑鼠是否懸停在其上方。
Definition hoverable.cpp:41
代表一個事件。它可以是按鍵事件、終端機大小調整,或更多...
Definition event.hpp:27
Box 是一個表示二維空間中矩形區域的結構。
Definition box.hpp:14
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
return Make< Impl >(option)
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:31
std::shared_ptr< ComponentBase > Component