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 <utility> // for move
8
9#include "ftxui/component/app.hpp" // for Component
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/dom/elements.hpp" // for operator|, text, Element, xflex, hbox, color, underlined, reflect, Decorator, dim, vcenter, focus, nothing, select, yflex, gaugeDirection
16#include "ftxui/screen/box.hpp" // for Box
17#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
18#include "ftxui/util/ref.hpp" // for ConstRef, Ref, ConstStringRef
19
20namespace ftxui {
21
22namespace {
23Decorator flexDirection(Direction direction) {
24 switch (direction) {
25 case Direction::Up:
26 case Direction::Down:
27 return yflex;
28 case Direction::Left:
30 return xflex;
31 }
32 return xflex; // NOT_REACHED()
33}
34
35Direction Opposite(Direction d) {
36 switch (d) {
37 case Direction::Up:
38 return Direction::Down;
39 case Direction::Down:
40 return Direction::Up;
41 case Direction::Left:
42 return Direction::Right;
44 return Direction::Left;
45 }
46 return d; // NOT_REACHED()
47}
48
49template <class T>
50class SliderBase : public SliderOption<T>, public ComponentBase {
51 public:
52 explicit SliderBase(SliderOption<T> options)
53 : SliderOption<T>(std::move(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 App::PostEventOrExecute(this->on_change);
98 return true;
99 }
100
101 return ComponentBase::OnEvent(event);
102 }
103
104 bool OnCapturedMouseEvent(Event event) {
105 if (event.mouse().motion == Mouse::Released) {
106 captured_mouse_ = nullptr;
107 return true;
108 }
109
110 T old_value = this->value();
111 switch (this->direction) {
112 case Direction::Right: {
113 this->value() = this->min() + (event.mouse().x - gauge_box_.x_min) *
114 (this->max() - this->min()) /
115 (gauge_box_.x_max - gauge_box_.x_min);
116
117 break;
118 }
119 case Direction::Left: {
120 this->value() = this->max() - (event.mouse().x - gauge_box_.x_min) *
121 (this->max() - this->min()) /
122 (gauge_box_.x_max - gauge_box_.x_min);
123 break;
124 }
125 case Direction::Down: {
126 this->value() = this->min() + (event.mouse().y - gauge_box_.y_min) *
127 (this->max() - this->min()) /
128 (gauge_box_.y_max - gauge_box_.y_min);
129 break;
130 }
131 case Direction::Up: {
132 this->value() = this->max() - (event.mouse().y - gauge_box_.y_min) *
133 (this->max() - this->min()) /
134 (gauge_box_.y_max - gauge_box_.y_min);
135 break;
136 }
137 }
138
139 this->value() = std::max(this->min(), std::min(this->max(), this->value()));
140
141 if (old_value != this->value()) {
142 App::PostEventOrExecute(this->on_change);
143 }
144 return true;
145 }
146
147 bool OnMouseEvent(Event event) {
148 if (captured_mouse_) {
149 return OnCapturedMouseEvent(event);
150 }
151
152 if (event.mouse().button != Mouse::Left) {
153 return false;
154 }
155 if (event.mouse().motion != Mouse::Pressed) {
156 return false;
157 }
158
159 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
160 return false;
161 }
162
163 captured_mouse_ = CaptureMouse(event);
164
165 if (captured_mouse_) {
166 TakeFocus();
167 return OnCapturedMouseEvent(event);
168 }
169
170 return false;
171 }
172
173 bool Focusable() const final { return true; }
174
175 private:
176 Box gauge_box_;
177 CapturedMouse captured_mouse_;
178};
179
180class SliderWithLabel : public ComponentBase {
181 public:
182 SliderWithLabel(ConstStringRef label, Component inner)
183 : label_(std::move(label)) {
184 Add(std::move(inner));
185 SetActiveChild(ChildAt(0));
186 }
187
188 private:
189 bool OnEvent(Event event) final {
190 if (ComponentBase::OnEvent(event)) {
191 return true;
192 }
193
194 if (!event.is_mouse()) {
195 return false;
196 }
197
198 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
199
200 if (!mouse_hover_) {
201 return false;
202 }
203
204 if (!CaptureMouse(event)) {
205 return false;
206 }
207
208 return true;
209 }
210
211 Element OnRender() override {
212 auto gauge_color = (Focused() || mouse_hover_) ? color(Color::White)
213 : color(Color::GrayDark);
214 auto element = hbox({
215 text(label_()) | dim | vcenter,
216 hbox({
217 text("["),
218 ComponentBase::Render() | underlined,
219 text("]"),
220 }) | xflex,
221 }) |
222 gauge_color | xflex | reflect(box_);
223
224 element |= focus;
225 return element;
226 }
227
228 ConstStringRef label_;
229 Box box_;
230 bool mouse_hover_ = false;
231};
232
233} // namespace
234
235/// @brief An horizontal slider.
236/// @param label The name of the slider.
237/// @param value The current value of the slider.
238/// @param min The minimum value.
239/// @param max The maximum value.
240/// @param increment The increment when used by the cursor.
241/// @ingroup component
242///
243/// ### Example
244///
245/// ```cpp
246/// auto screen = App::TerminalOutput();
247/// int value = 50;
248/// auto slider = Slider("Value:", &value, 0, 100, 1);
249/// screen.Loop(slider);
250/// ```
251///
252/// ### Output
253///
254/// ```bash
255/// Value:[██████████████████████████ ]
256/// ```
257Component Slider(ConstStringRef label,
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
271Component Slider(ConstStringRef label,
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}
284Component Slider(ConstStringRef label,
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 A slider in any direction.
299/// @param options The options
300/// ### Example
301///
302/// ```cpp
303/// auto screen = App::TerminalOutput();
304/// int value = 50;
305/// auto slider = Slider({
306/// .value = &value,
307/// .min = 0,
308/// .max = 100,
309/// .increment= 20,
310/// });
311/// screen.Loop(slider);
312/// ```
313template <typename T>
315 return Make<SliderBase<T>>(std::move(options));
316}
317
318template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<int8_t>);
319template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<int16_t>);
320template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<int32_t>);
321template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<int64_t>);
322
323template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<uint8_t>);
324template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<uint16_t>);
325template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<uint32_t>);
326template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<uint64_t>);
327
328template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<float>);
329template FTXUI_EXPORT(COMPONENT) Component Slider(SliderOption<double>);
330
331} // namespace ftxui
An adapter. Own or reference an immutable object.
Definition ref.hpp:20
An adapter. Own or reference an mutable object.
Definition ref.hpp:56
#define FTXUI_EXPORT(component)
Definition export.hpp:24
Element text(std::string_view text)
Display a piece of UTF8 encoded unicode text.
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition flex.cpp:129
Element gaugeDirection(float progress, Direction direction)
Draw a high definition progress bar progressing in specified direction.
Direction
Direction is an enumeration that represents the four cardinal directions.
Definition direction.hpp:13
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Definition flex.cpp:135
Element underlined(Element child)
Underline the given element.
Element focus(Element child)
Set the child to be the one focused among its siblings.
Definition frame.cpp:101
Element vcenter(Element child)
Center an element vertically.
Element color(Color color, Element child)
Set the foreground color of an element.
Definition dom/color.cpp:81
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:28
std::shared_ptr< Node > Element
Definition elements.hpp:24
Direction direction
Definition elements.hpp:82
Element hbox(Elements children)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
std::function< Element(Element)> Decorator
Definition elements.hpp:26
Component Slider(SliderOption< T > options)
A slider in any direction.
int y
Definition elements.hpp:126
const Element & element
Definition node.hpp:95
Decorator reflect(Box &box)
Definition reflect.cpp:43
int value
Definition elements.hpp:178
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23