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. 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 <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
21 namespace ftxui {
22
23 namespace {
24 Decorator flexDirection(Direction direction) {
25 switch (direction) {
26 case Direction::Up:
27 case Direction::Down:
28 return yflex;
29 case Direction::Left:
30 case Direction::Right:
31 return xflex;
32 }
33 return xflex; // NOT_REACHED()
34}
35
36 template <class T>
37 class SliderBase : public SliderOption<T>, public ComponentBase {
38 public:
39 explicit SliderBase(SliderOption<T> options) : SliderOption<T>(options) {}
40
41 Element OnRender() override {
42 auto gauge_color =
43 Focused() ? color(this->color_active) : color(this->color_inactive);
44 const float percent =
45 float(this->value() - this->min()) / float(this->max() - this->min());
46 return gaugeDirection(percent, this->direction) |
47 flexDirection(this->direction) | reflect(gauge_box_) | gauge_color;
48 }
49
50 void OnLeft() {
51 switch (this->direction) {
52 case Direction::Right:
53 this->value() -= this->increment();
54 break;
55 case Direction::Left:
56 this->value() += this->increment();
57 break;
58 case Direction::Up:
59 case Direction::Down:
60 break;
61 }
62 }
63
64 void OnRight() {
65 switch (this->direction) {
66 case Direction::Right:
67 this->value() += this->increment();
68 break;
69 case Direction::Left:
70 this->value() -= this->increment();
71 break;
72 case Direction::Up:
73 case Direction::Down:
74 break;
75 }
76 }
77
78 void OnUp() {
79 switch (this->direction) {
80 case Direction::Up:
81 this->value() -= this->increment();
82 break;
83 case Direction::Down:
84 this->value() += this->increment();
85 break;
86 case Direction::Left:
87 case Direction::Right:
88 break;
89 }
90 }
91
92 void OnDown() {
93 switch (this->direction) {
94 case Direction::Down:
95 this->value() += this->increment();
96 break;
97 case Direction::Up:
98 this->value() -= this->increment();
99 break;
100 case Direction::Left:
101 case Direction::Right:
102 break;
103 }
104 }
105
106 bool OnEvent(Event event) final {
107 if (event.is_mouse()) {
108 return OnMouseEvent(event);
109 }
110
111 T old_value = this->value();
112 if (event == Event::ArrowLeft || event == Event::Character('h')) {
113 OnLeft();
114 }
115 if (event == Event::ArrowRight || event == Event::Character('l')) {
116 OnRight();
117 }
118 if (event == Event::ArrowUp || event == Event::Character('k')) {
119 OnDown();
120 }
121 if (event == Event::ArrowDown || event == Event::Character('j')) {
122 OnUp();
123 }
124
125 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
126 if (old_value != this->value()) {
127 if (this->on_change) {
128 this->on_change();
129 }
130 return true;
131 }
132
133 return ComponentBase::OnEvent(event);
134 }
135
136 bool OnCapturedMouseEvent(Event event) {
137 if (event.mouse().motion == Mouse::Released) {
138 captured_mouse_ = nullptr;
139 return true;
140 }
141
142 T old_value = this->value();
143 switch (this->direction) {
144 case Direction::Right: {
145 this->value() = this->min() + (event.mouse().x - gauge_box_.x_min) *
146 (this->max() - this->min()) /
147 (gauge_box_.x_max - gauge_box_.x_min);
148
149 break;
150 }
151 case Direction::Left: {
152 this->value() = this->max() - (event.mouse().x - gauge_box_.x_min) *
153 (this->max() - this->min()) /
154 (gauge_box_.x_max - gauge_box_.x_min);
155 break;
156 }
157 case Direction::Down: {
158 this->value() = this->min() + (event.mouse().y - gauge_box_.y_min) *
159 (this->max() - this->min()) /
160 (gauge_box_.y_max - gauge_box_.y_min);
161 break;
162 }
163 case Direction::Up: {
164 this->value() = this->max() - (event.mouse().y - gauge_box_.y_min) *
165 (this->max() - this->min()) /
166 (gauge_box_.y_max - gauge_box_.y_min);
167 break;
168 }
169 }
170
171 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
172
173 if (old_value != this->value() && this->on_change) {
174 this->on_change();
175 }
176 return true;
177 }
178
179 bool OnMouseEvent(Event event) {
180 if (captured_mouse_) {
181 return OnCapturedMouseEvent(event);
182 }
183
184 if (event.mouse().button != Mouse::Left) {
185 return false;
186 }
187 if (event.mouse().motion != Mouse::Pressed) {
188 return false;
189 }
190
191 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
192 return false;
193 }
194
195 captured_mouse_ = CaptureMouse(event);
196
197 if (captured_mouse_) {
198 TakeFocus();
199 return OnCapturedMouseEvent(event);
200 }
201
202 return false;
203 }
204
205 bool Focusable() const final { return true; }
206
207 private:
208 Box gauge_box_;
209 CapturedMouse captured_mouse_;
210};
211
212 class SliderWithLabel : public ComponentBase {
213 public:
214 SliderWithLabel(ConstStringRef label, Component inner)
215 : label_(std::move(label)) {
216 Add(std::move(inner));
217 SetActiveChild(ChildAt(0));
218 }
219
220 private:
221 bool OnEvent(Event event) final {
222 if (ComponentBase::OnEvent(event)) {
223 return true;
224 }
225
226 if (!event.is_mouse()) {
227 return false;
228 }
229
230 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
231
232 if (!mouse_hover_) {
233 return false;
234 }
235
236 if (!CaptureMouse(event)) {
237 return false;
238 }
239
240 return true;
241 }
242
243 Element OnRender() override {
244 auto gauge_color = (Focused() || mouse_hover_) ? color(Color::White)
245 : color(Color::GrayDark);
246 auto element = hbox({
247 text(label_()) | dim | vcenter,
248 hbox({
249 text("["),
250 ComponentBase::Render() | underlined,
251 text("]"),
252 }) | xflex,
253 }) |
254 gauge_color | xflex | reflect(box_);
255
256 element |= focus;
257 return element;
258 }
259
260 ConstStringRef label_;
261 Box box_;
262 bool mouse_hover_ = false;
263};
264
265} // namespace
266
267/// @brief An horizontal slider.
268/// @param label The name of the slider.
269/// @param value The current value of the slider.
270/// @param min The minimum value.
271/// @param max The maximum value.
272/// @param increment The increment when used by the cursor.
273/// @ingroup component
274///
275/// ### Example
276///
277/// ```cpp
278/// auto screen = ScreenInteractive::TerminalOutput();
279/// int value = 50;
280/// auto slider = Slider("Value:", &value, 0, 100, 1);
281/// screen.Loop(slider);
282/// ```
283///
284/// ### Output
285///
286/// ```bash
287/// Value:[██████████████████████████ ]
288/// ```
289 Component Slider(ConstStringRef label,
290 Ref<int> value,
291 ConstRef<int> min,
292 ConstRef<int> max,
293 ConstRef<int> increment) {
294 SliderOption<int> option;
295 option.value = value;
296 option.min = min;
297 option.max = max;
298 option.increment = increment;
299 auto slider = Make<SliderBase<int>>(option);
300 return Make<SliderWithLabel>(std::move(label), slider);
301}
302
303 Component Slider(ConstStringRef label,
304 Ref<float> value,
305 ConstRef<float> min,
306 ConstRef<float> max,
307 ConstRef<float> increment) {
308 SliderOption<float> option;
309 option.value = value;
310 option.min = min;
311 option.max = max;
312 option.increment = increment;
313 auto slider = Make<SliderBase<float>>(option);
314 return Make<SliderWithLabel>(std::move(label), slider);
315}
316 Component Slider(ConstStringRef label,
317 Ref<long> value,
318 ConstRef<long> min,
319 ConstRef<long> max,
320 ConstRef<long> increment) {
321 SliderOption<long> option;
322 option.value = value;
323 option.min = min;
324 option.max = max;
325 option.increment = increment;
326 auto slider = Make<SliderBase<long>>(option);
327 return Make<SliderWithLabel>(std::move(label), slider);
328}
329
330/// @brief A slider in any direction.
331/// @param options The options
332/// ### Example
333///
334/// ```cpp
335/// auto screen = ScreenInteractive::TerminalOutput();
336/// int value = 50;
337/// auto slider = Slider({
338/// .value = &value,
339/// .min = 0,
340/// .max = 100,
341/// .increment= 20,
342/// });
343/// screen.Loop(slider);
344/// ```
345 template <typename T>
346 Component Slider(SliderOption<T> options) {
347 return Make<SliderBase<T>>(options);
348}
349
350 template Component Slider(SliderOption<int8_t>);
351 template Component Slider(SliderOption<int16_t>);
352 template Component Slider(SliderOption<int32_t>);
353 template Component Slider(SliderOption<int64_t>);
354
355 template Component Slider(SliderOption<uint8_t>);
356 template Component Slider(SliderOption<uint16_t>);
357 template Component Slider(SliderOption<uint32_t>);
358 template Component Slider(SliderOption<uint64_t>);
359
360 template Component Slider(SliderOption<float>);
361 template Component Slider(SliderOption<double>);
362
363} // namespace ftxui
box.hpp
captured_mouse.hpp
ftxui::ConstRef
An adapter. Own or reference an immutable object.
Definition ref.hpp:17
ftxui::ConstStringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:94
ftxui::Ref
An adapter. Own or reference an mutable object.
Definition ref.hpp:46
color.hpp
component.hpp
component_base.hpp
component_options.hpp
direction.hpp
elements.hpp
event.hpp
ftxui::Mouse::Left
@ Left
Definition mouse.hpp:13
ftxui::Mouse::Pressed
@ Pressed
Definition mouse.hpp:25
ftxui::Mouse::Released
@ Released
Definition mouse.hpp:24
ftxui::ComponentBase::Render
Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxui::Screen representing this ftxui::...
Definition component.cpp:103
ftxui::Event::ArrowUp
static const Event ArrowUp
Definition event.hpp:42
ftxui::Event::ArrowDown
static const Event ArrowDown
Definition event.hpp:43
ftxui::ComponentBase::OnEvent
virtual bool OnEvent(Event)
Called in response to an event.
Definition component.cpp:151
ftxui::Event::ArrowLeft
static const Event ArrowLeft
Definition event.hpp:40
ftxui::Event::ArrowRight
static const Event ArrowRight
Definition event.hpp:41
ftxui::xflex
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition flex.cpp:129
ftxui::gaugeDirection
Element gaugeDirection(float progress, Direction direction)
Draw a high definition progress bar progressing in specified direction.
Definition src/ftxui/dom/gauge.cpp:169
ftxui::Direction
Direction
Direction is an enumeration that represents the four cardinal directions.
Definition direction.hpp:13
ftxui::yflex
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Definition flex.cpp:135
ftxui::underlined
Element underlined(Element)
Underline the given element.
Definition underlined.cpp:33
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::focus
Element focus(Element)
Set the child to be the one focused among its siblings.
Definition frame.cpp:101
ftxui::vcenter
Element vcenter(Element)
Center an element vertically.
Definition composite_decorator.cpp:23
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition dom/color.cpp:110
ftxui::Direction::Down
@ Down
ftxui::Direction::Up
@ Up
ftxui::Direction::Right
@ Right
ftxui::Direction::Left
@ Left
ftxui::Color::White
@ White
Definition color.hpp:69
ftxui::Color::GrayDark
@ GrayDark
Definition color.hpp:62
mouse.hpp
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Decorator
std::function< Element(Element)> Decorator
Definition elements.hpp:24
ftxui::CapturedMouse
std::unique_ptr< CapturedMouseInterface > CapturedMouse
Definition captured_mouse.hpp:20
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::hbox
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
ftxui::Slider
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition src/ftxui/component/slider.cpp:346
ftxui::reflect
Decorator reflect(Box &box)
Definition reflect.cpp:43
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ref.hpp
screen_interactive.hpp
ftxui::SliderOption
Definition component_options.hpp:224
ftxui::SliderOption::min
ConstRef< T > min
Definition component_options.hpp:226
ftxui::SliderOption::increment
ConstRef< T > increment
Definition component_options.hpp:228
ftxui::SliderOption::value
Ref< T > value
Definition component_options.hpp:225
ftxui::SliderOption::max
ConstRef< T > max
Definition component_options.hpp:227