FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/component/slider.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. 保留所有權利。
2// 此原始碼的使用受 MIT 許可證的約束,該許可證可在
3// LICENSE 文件中找到。
4#include <algorithm> // for max, min
5#include <ftxui/component/component_options.hpp> // for SliderOption
6#include <ftxui/dom/direction.hpp> // for Direction, Direction::Down, Direction::Left, Direction::Right, Direction::Up
7#include <string> // for allocator
8#include <utility> // for move
9
10#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
11#include "ftxui/component/component.hpp" // for Make, Slider
12#include "ftxui/component/component_base.hpp" // for ComponentBase
13#include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowLeft, Event::ArrowRight, Event::ArrowUp
14#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed, Mouse::Released
15#include "ftxui/component/screen_interactive.hpp" // for Component
16#include "ftxui/dom/elements.hpp" // for operator|, text, Element, xflex, hbox, color, underlined, reflect, Decorator, dim, vcenter, focus, nothing, select, yflex, gaugeDirection
17#include "ftxui/screen/box.hpp" // for Box
18#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
19#include "ftxui/util/ref.hpp" // for ConstRef, Ref, ConstStringRef
20
21namespace ftxui {
22
23namespace {
24Decorator flexDirection(Direction direction) {
25 switch (direction) {
26 case Direction::Up:
27 case Direction::Down:
28 return yflex;
29 case Direction::Left:
31 return xflex;
32 }
33 return xflex; // NOT_REACHED()
34}
35
36Direction Opposite(Direction d) {
37 switch (d) {
38 case Direction::Up:
39 return Direction::Down;
40 case Direction::Down:
41 return Direction::Up;
42 case Direction::Left:
43 return Direction::Right;
45 return Direction::Left;
46 }
47 return d; // NOT_REACHED()
48}
49
50template <class T>
51class SliderBase : public SliderOption<T>, public ComponentBase {
52 public:
53 explicit SliderBase(SliderOption<T> options) : SliderOption<T>(options) {}
54
55 Element OnRender() override {
56 auto gauge_color =
57 Focused() ? color(this->color_active) : color(this->color_inactive);
58 const float percent =
59 float(this->value() - this->min()) / float(this->max() - this->min());
60 return gaugeDirection(percent, this->direction) |
61 flexDirection(this->direction) | reflect(gauge_box_) | gauge_color;
62 }
63
64 void OnDirection(Direction pressed) {
65 if (pressed == this->direction) {
66 this->value() += this->increment();
67 return;
68 }
69
70 if (pressed == Opposite(this->direction)) {
71 this->value() -= this->increment();
72 return;
73 }
74 }
75
76 bool OnEvent(Event event) final {
77 if (event.is_mouse()) {
78 return OnMouseEvent(event);
79 }
80
81 T old_value = this->value();
82 if (event == Event::ArrowLeft || event == Event::Character('h')) {
83 OnDirection(Direction::Left);
84 }
85 if (event == Event::ArrowRight || event == Event::Character('l')) {
86 OnDirection(Direction::Right);
87 }
88 if (event == Event::ArrowUp || event == Event::Character('k')) {
89 OnDirection(Direction::Up);
90 }
91 if (event == Event::ArrowDown || event == Event::Character('j')) {
92 OnDirection(Direction::Down);
93 }
94
95 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
96 if (old_value != this->value()) {
97 if (this->on_change) {
98 this->on_change();
99 }
100 return true;
101 }
102
103 return ComponentBase::OnEvent(event);
104 }
105
106 bool OnCapturedMouseEvent(Event event) {
107 if (event.mouse().motion == Mouse::Released) {
108 captured_mouse_ = nullptr;
109 return true;
110 }
111
112 T old_value = this->value();
113 switch (this->direction) {
114 case Direction::Right: {
115 this->value() = this->min() + (event.mouse().x - gauge_box_.x_min) *
116 (this->max() - this->min()) /
117 (gauge_box_.x_max - gauge_box_.x_min);
118
119 break;
120 }
121 case Direction::Left: {
122 this->value() = this->max() - (event.mouse().x - gauge_box_.x_min) *
123 (this->max() - this->min()) /
124 (gauge_box_.x_max - gauge_box_.x_min);
125 break;
126 }
127 case Direction::Down: {
128 this->value() = this->min() + (event.mouse().y - gauge_box_.y_min) *
129 (this->max() - this->min()) /
130 (gauge_box_.y_max - gauge_box_.y_min);
131 break;
132 }
133 case Direction::Up: {
134 this->value() = this->max() - (event.mouse().y - gauge_box_.y_min) *
135 (this->max() - this->min()) /
136 (gauge_box_.y_max - gauge_box_.y_min);
137 break;
138 }
139 }
140
141 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
142
143 if (old_value != this->value() && this->on_change) {
144 this->on_change();
145 }
146 return true;
147 }
148
149 bool OnMouseEvent(Event event) {
150 if (captured_mouse_) {
151 return OnCapturedMouseEvent(event);
152 }
153
154 if (event.mouse().button != Mouse::Left) {
155 return false;
156 }
157 if (event.mouse().motion != Mouse::Pressed) {
158 return false;
159 }
160
161 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
162 return false;
163 }
164
165 captured_mouse_ = CaptureMouse(event);
166
167 if (captured_mouse_) {
168 TakeFocus();
169 return OnCapturedMouseEvent(event);
170 }
171
172 return false;
173 }
174
175 bool Focusable() const final { return true; }
176
177 private:
178 Box gauge_box_;
179 CapturedMouse captured_mouse_;
180};
181
182class SliderWithLabel : public ComponentBase {
183 public:
184 SliderWithLabel(ConstStringRef label, Component inner)
185 : label_(std::move(label)) {
186 Add(std::move(inner));
187 SetActiveChild(ChildAt(0));
188 }
189
190 private:
191 bool OnEvent(Event event) final {
192 if (ComponentBase::OnEvent(event)) {
193 return true;
194 }
195
196 if (!event.is_mouse()) {
197 return false;
198 }
199
200 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
201
202 if (!mouse_hover_) {
203 return false;
204 }
205
206 if (!CaptureMouse(event)) {
207 return false;
208 }
209
210 return true;
211 }
212
213 Element OnRender() override {
214 auto gauge_color = (Focused() || mouse_hover_) ? color(Color::White)
216 auto element = hbox({
217 text(label_()) | dim | vcenter,
218 hbox({
219 text("["),
221 text("]"),
222 }) | xflex,
223 }) |
224 gauge_color | xflex | reflect(box_);
225
226 element |= focus;
227 return element;
228 }
229
230 ConstStringRef label_;
231 Box box_;
232 bool mouse_hover_ = false;
233};
234
235} // namespace
236
237/// @brief 水平滑桿。
238/// @param label 滑桿的名稱。
239/// @param value 滑桿的當前值。
240/// @param min 最小值。
241/// @param max 最大值。
242/// @param increment 當遊標使用時的增量。
243/// ### 範例
244///
245/// ```cpp
246/// auto screen = ScreenInteractive::TerminalOutput();
247/// int value = 50;
248/// auto slider = Slider("Value:", &value, 0, 100, 1);
249/// screen.Loop(slider);
250/// ```
251///
252/// ### 輸出
253///
254/// ```bash
255/// Value:[██████████████████████████ ]
256/// ```
258 Ref<int> value,
259 ConstRef<int> min,
260 ConstRef<int> max,
261 ConstRef<int> increment) {
262 SliderOption<int> option;
263 option.value = value;
264 option.min = min;
265 option.max = max;
266 option.increment = increment;
267 auto slider = Make<SliderBase<int>>(option);
268 return Make<SliderWithLabel>(std::move(label), slider);
269}
270
272 Ref<float> value,
273 ConstRef<float> min,
274 ConstRef<float> max,
275 ConstRef<float> increment) {
276 SliderOption<float> option;
277 option.value = value;
278 option.min = min;
279 option.max = max;
280 option.increment = increment;
281 auto slider = Make<SliderBase<float>>(option);
282 return Make<SliderWithLabel>(std::move(label), slider);
283}
285 Ref<long> value,
286 ConstRef<long> min,
287 ConstRef<long> max,
288 ConstRef<long> increment) {
289 SliderOption<long> option;
290 option.value = value;
291 option.min = min;
292 option.max = max;
293 option.increment = increment;
294 auto slider = Make<SliderBase<long>>(option);
295 return Make<SliderWithLabel>(std::move(label), slider);
296}
297
298/// @brief 任何方向的滑桿。
299/// @param options 選項
300/// ### 範例
301/// auto screen = ScreenInteractive::TerminalOutput();
302/// int value = 50;
303/// auto slider = Slider({
304/// .value = &value,
305/// .min = 0,
306/// .max = 100,
307/// .increment= 20,
308/// });
309/// screen.Loop(slider);
310/// ```
311template <typename T>
312Component Slider(SliderOption<T> options) {
313 return Make<SliderBase<T>>(options);
314}
315
316template Component Slider(SliderOption<int8_t>);
317template Component Slider(SliderOption<int16_t>);
318template Component Slider(SliderOption<int32_t>);
319template Component Slider(SliderOption<int64_t>);
320
321template Component Slider(SliderOption<uint8_t>);
322template Component Slider(SliderOption<uint16_t>);
323template Component Slider(SliderOption<uint32_t>);
324template Component Slider(SliderOption<uint64_t>);
325
326template Component Slider(SliderOption<float>);
327template Component Slider(SliderOption<double>);
328
329} // namespace ftxui
一個適配器。擁有或引用一個不可變的物件。
Definition ref.hpp:17
一個適配器。擁有或引用一個常數字串。為方便起見,此類別將多個不可變字串轉換為共享表示。
Definition ref.hpp:92
一個適配器。擁有或引用一個可變的物件。
Definition ref.hpp:46
Element Render()
繪製組件。 建構一個 ftxui::Element,用於在表示此 ftxui::ComponentBase 的 ftxui::Screen 上繪製。請覆寫 OnRender() 以修改渲染。
static const Event ArrowUp
Definition event.hpp:40
static const Event ArrowDown
Definition event.hpp:41
virtual bool OnEvent(Event)
回應事件時呼叫。
static const Event ArrowLeft
Definition event.hpp:38
static const Event ArrowRight
Definition event.hpp:39
Element xflex(Element)
在 X 軸上盡可能擴展/在需要時最小化。
Definition flex.cpp:146
Element gaugeDirection(float progress, Direction direction)
繪製一個指定方向的高解析度進度條。
Direction
Direction 是一個列舉,表示四個主要方向。
Definition direction.hpp:12
Element yflex(Element)
在 Y 軸上盡可能擴展/在需要時最小化。
Definition flex.cpp:152
Element underlined(Element)
為給定元素加上底線。
Element text(std::wstring text)
顯示一段 Unicode 文字。
Definition text.cpp:160
Element focus(Element)
將 child 設置為其同級元素中被聚焦的元素。
Definition frame.cpp:101
Element vcenter(Element)
垂直置中一個元素。
Decorator color(Color)
使用前景顏色進行裝飾。
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::function< Element(Element)> Decorator
Definition elements.hpp:24
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element hbox(Elements)
一個逐一水平顯示元素的容器。
Definition hbox.cpp:94
Component Slider(SliderOption< T > options)
Decorator reflect(Box &box)
Definition reflect.cpp:42
std::shared_ptr< ComponentBase > Component