Loading...
Searching...
No Matches
slider.cpp
Go to the documentation of this file.
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <algorithm> // for max, min
#include <ftxui/component/component_options.hpp> // for SliderOption
#include <ftxui/dom/direction.hpp> // for Direction, Direction::Down, Direction::Left, Direction::Right, Direction::Up
#include <string> // for allocator
#include <utility> // for move
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/component.hpp" // for Make, Slider
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowLeft, Event::ArrowRight, Event::ArrowUp
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed, Mouse::Released
#include "ftxui/component/screen_interactive.hpp" // for Component
#include "ftxui/dom/elements.hpp" // for operator|, text, Element, xflex, hbox, color, underlined, reflect, Decorator, dim, vcenter, focus, nothing, select, yflex, gaugeDirection
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
#include "ftxui/util/ref.hpp" // for ConstRef, Ref, ConstStringRef
namespace ftxui {
namespace {
Decorator flexDirection(Direction direction) {
switch (direction) {
case Direction::Up:
case Direction::Down:
return yflex;
case Direction::Left:
case Direction::Right:
return xflex;
}
return xflex; // NOT_REACHED()
}
template <class T>
class SliderBase : public SliderOption<T>, public ComponentBase {
public:
explicit SliderBase(SliderOption<T> options) : SliderOption<T>(options) {}
Element OnRender() override {
auto gauge_color =
Focused() ? color(this->color_active) : color(this->color_inactive);
const float percent =
float(this->value() - this->min()) / float(this->max() - this->min());
return gaugeDirection(percent, this->direction) |
flexDirection(this->direction) | reflect(gauge_box_) | gauge_color;
}
void OnLeft() {
switch (this->direction) {
case Direction::Right:
this->value() -= this->increment();
break;
case Direction::Left:
this->value() += this->increment();
break;
case Direction::Up:
case Direction::Down:
break;
}
}
void OnRight() {
switch (this->direction) {
case Direction::Right:
this->value() += this->increment();
break;
case Direction::Left:
this->value() -= this->increment();
break;
case Direction::Up:
case Direction::Down:
break;
}
}
void OnUp() {
switch (this->direction) {
case Direction::Up:
this->value() -= this->increment();
break;
case Direction::Down:
this->value() += this->increment();
break;
case Direction::Left:
case Direction::Right:
break;
}
}
void OnDown() {
switch (this->direction) {
case Direction::Down:
this->value() += this->increment();
break;
case Direction::Up:
this->value() -= this->increment();
break;
case Direction::Left:
case Direction::Right:
break;
}
}
bool OnEvent(Event event) final {
if (event.is_mouse()) {
return OnMouseEvent(event);
}
T old_value = this->value();
if (event == Event::ArrowLeft || event == Event::Character('h')) {
OnLeft();
}
if (event == Event::ArrowRight || event == Event::Character('l')) {
OnRight();
}
if (event == Event::ArrowUp || event == Event::Character('k')) {
OnDown();
}
if (event == Event::ArrowDown || event == Event::Character('j')) {
OnUp();
}
this->value() = std::max(this->min(), std::min(this->max(), this->value()));
if (old_value != this->value()) {
if (this->on_change) {
this->on_change();
}
return true;
}
return ComponentBase::OnEvent(event);
}
bool OnCapturedMouseEvent(Event event) {
if (event.mouse().motion == Mouse::Released) {
captured_mouse_ = nullptr;
return true;
}
T old_value = this->value();
switch (this->direction) {
case Direction::Right: {
this->value() = this->min() + (event.mouse().x - gauge_box_.x_min) *
(this->max() - this->min()) /
(gauge_box_.x_max - gauge_box_.x_min);
break;
}
case Direction::Left: {
this->value() = this->max() - (event.mouse().x - gauge_box_.x_min) *
(this->max() - this->min()) /
(gauge_box_.x_max - gauge_box_.x_min);
break;
}
case Direction::Down: {
this->value() = this->min() + (event.mouse().y - gauge_box_.y_min) *
(this->max() - this->min()) /
(gauge_box_.y_max - gauge_box_.y_min);
break;
}
case Direction::Up: {
this->value() = this->max() - (event.mouse().y - gauge_box_.y_min) *
(this->max() - this->min()) /
(gauge_box_.y_max - gauge_box_.y_min);
break;
}
}
this->value() = std::max(this->min(), std::min(this->max(), this->value()));
if (old_value != this->value() && this->on_change) {
this->on_change();
}
return true;
}
bool OnMouseEvent(Event event) {
if (captured_mouse_) {
return OnCapturedMouseEvent(event);
}
if (event.mouse().button != Mouse::Left) {
return false;
}
if (event.mouse().motion != Mouse::Pressed) {
return false;
}
if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
return false;
}
captured_mouse_ = CaptureMouse(event);
if (captured_mouse_) {
TakeFocus();
return OnCapturedMouseEvent(event);
}
return false;
}
bool Focusable() const final { return true; }
private:
Box gauge_box_;
CapturedMouse captured_mouse_;
};
class SliderWithLabel : public ComponentBase {
public:
SliderWithLabel(ConstStringRef label, Component inner)
: label_(std::move(label)) {
Add(std::move(inner));
SetActiveChild(ChildAt(0));
}
private:
bool OnEvent(Event event) final {
if (ComponentBase::OnEvent(event)) {
return true;
}
if (!event.is_mouse()) {
return false;
}
mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
if (!mouse_hover_) {
return false;
}
if (!CaptureMouse(event)) {
return false;
}
return true;
}
Element OnRender() override {
auto gauge_color = (Focused() || mouse_hover_) ? color(Color::White)
: color(Color::GrayDark);
auto element = hbox({
text(label_()) | dim | vcenter,
hbox({
text("["),
ComponentBase::Render() | underlined,
text("]"),
}) | xflex,
}) |
gauge_color | xflex | reflect(box_);
element |= focus;
return element;
}
ConstStringRef label_;
Box box_;
bool mouse_hover_ = false;
};
} // namespace
/// @brief An horizontal slider.
/// @param label The name of the slider.
/// @param value The current value of the slider.
/// @param min The minimum value.
/// @param max The maximum value.
/// @param increment The increment when used by the cursor.
/// @ingroup component
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// int value = 50;
/// auto slider = Slider("Value:", &value, 0, 100, 1);
/// screen.Loop(slider);
/// ```
///
/// ### Output
///
/// ```bash
/// Value:[██████████████████████████ ]
/// ```
Component Slider(ConstStringRef label,
Ref<int> value,
ConstRef<int> min,
ConstRef<int> max,
ConstRef<int> increment) {
SliderOption<int> option;
option.value = value;
option.min = min;
option.max = max;
option.increment = increment;
auto slider = Make<SliderBase<int>>(option);
return Make<SliderWithLabel>(std::move(label), slider);
}
Component Slider(ConstStringRef label,
Ref<float> value,
ConstRef<float> min,
ConstRef<float> max,
ConstRef<float> increment) {
SliderOption<float> option;
option.value = value;
option.min = min;
option.max = max;
option.increment = increment;
auto slider = Make<SliderBase<float>>(option);
return Make<SliderWithLabel>(std::move(label), slider);
}
Component Slider(ConstStringRef label,
Ref<long> value,
ConstRef<long> min,
ConstRef<long> max,
ConstRef<long> increment) {
SliderOption<long> option;
option.value = value;
option.min = min;
option.max = max;
option.increment = increment;
auto slider = Make<SliderBase<long>>(option);
return Make<SliderWithLabel>(std::move(label), slider);
}
/// @brief A slider in any direction.
/// @param options The options
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// int value = 50;
/// auto slider = Slider({
/// .value = &value,
/// .min = 0,
/// .max = 100,
/// .increment= 20,
/// });
/// screen.Loop(slider);
/// ```
template <typename T>
Component Slider(SliderOption<T> options) {
return Make<SliderBase<T>>(options);
}
template Component Slider(SliderOption<int8_t>);
template Component Slider(SliderOption<int16_t>);
template Component Slider(SliderOption<int32_t>);
template Component Slider(SliderOption<int64_t>);
template Component Slider(SliderOption<uint8_t>);
template Component Slider(SliderOption<uint16_t>);
template Component Slider(SliderOption<uint32_t>);
template Component Slider(SliderOption<uint64_t>);
template Component Slider(SliderOption<float>);
template Component Slider(SliderOption<double>);
} // namespace ftxui
box.hpp
captured_mouse.hpp
ftxui::Color::White
@ White
Definition color.hpp:67
ftxui::Color::GrayDark
@ GrayDark
Definition color.hpp:60
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:111
ftxui::ComponentBase::OnEvent
virtual bool OnEvent(Event)
Called in response to an event.
Definition component.cpp:161
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
mouse.hpp
ftxui
Definition animation.hpp:10
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 gauge.cpp:169
ftxui::Decorator
std::function< Element(Element)> Decorator
Definition elements.hpp:24
ftxui::Direction
Direction
Definition direction.hpp:8
ftxui::Direction::Down
@ Down
ftxui::Direction::Up
@ Up
ftxui::Direction::Right
@ Right
ftxui::Direction::Left
@ Left
ftxui::CapturedMouse
std::unique_ptr< CapturedMouseInterface > CapturedMouse
Definition captured_mouse.hpp:19
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::yflex
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Definition flex.cpp:135
ftxui::hbox
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
ftxui::underlined
Element underlined(Element)
Make the underlined element to be underlined.
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::Slider
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition slider.cpp:346
ftxui::reflect
Decorator reflect(Box &box)
Definition reflect.cpp:43
ftxui::SliderOption::value
Ref< T > value
Definition component_options.hpp:224
ftxui::dim
Element dim(Element)
Use a light font, for elements with less emphasis.
Definition dim.cpp:33
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 color.cpp:110
ftxui::SliderOption
Definition component_options.hpp:223
ref.hpp
screen_interactive.hpp
ftxui::Event::ArrowUp
static const Event ArrowUp
Definition event.hpp:40
ftxui::Event::ArrowDown
static const Event ArrowDown
Definition event.hpp:41
ftxui::Event::ArrowLeft
static const Event ArrowLeft
Definition event.hpp:38
ftxui::Event::ArrowRight
static const Event ArrowRight
Definition event.hpp:39
ftxui::Mouse::Left
@ Left
Definition mouse.hpp:13
ftxui::Mouse::Pressed
@ Pressed
Definition mouse.hpp:25
ftxui::Mouse::Released
@ Released
Definition mouse.hpp:24