2021-05-15 03:43:35 +08:00
|
|
|
#include <string> // for allocator
|
2021-05-10 02:32:27 +08:00
|
|
|
#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::ArrowLeft, Event::ArrowRight
|
|
|
|
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed, Mouse::Released
|
|
|
|
#include "ftxui/component/screen_interactive.hpp" // for Component
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for operator|, text, Element, reflect, xflex, gauge, hbox, underlined, color, dim, vcenter
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
|
|
#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::GrayLight
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/util/ref.hpp" // for StringRef
|
2021-04-29 06:18:58 +08:00
|
|
|
|
|
|
|
namespace ftxui {
|
2021-05-02 00:13:56 +08:00
|
|
|
|
2021-05-02 02:40:35 +08:00
|
|
|
template <class T>
|
2021-05-10 02:32:27 +08:00
|
|
|
class SliderBase : public ComponentBase {
|
2021-04-29 06:18:58 +08:00
|
|
|
public:
|
2022-08-13 22:26:53 +08:00
|
|
|
SliderBase(ConstStringRef label,
|
|
|
|
Ref<T> value,
|
|
|
|
ConstRef<T> min,
|
|
|
|
ConstRef<T> max,
|
|
|
|
ConstRef<T> increment)
|
2022-03-31 08:17:43 +08:00
|
|
|
: label_(std::move(label)),
|
2022-08-21 23:23:13 +08:00
|
|
|
value_(value),
|
|
|
|
min_(min),
|
|
|
|
max_(max),
|
|
|
|
increment_(increment) {}
|
2021-04-29 06:18:58 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
Element Render() override {
|
2021-04-29 06:18:58 +08:00
|
|
|
auto gauge_color =
|
|
|
|
Focused() ? color(Color::GrayLight) : color(Color::GrayDark);
|
2022-08-13 22:26:53 +08:00
|
|
|
float percent = float(value_() - min_()) / float(max_() - min_());
|
2021-04-29 06:18:58 +08:00
|
|
|
return hbox({
|
2022-08-13 22:26:53 +08:00
|
|
|
text(label_()) | dim | vcenter,
|
2021-04-29 06:18:58 +08:00
|
|
|
hbox({
|
2021-08-09 05:25:20 +08:00
|
|
|
text("["),
|
2021-04-29 06:18:58 +08:00
|
|
|
gauge(percent) | underlined | xflex | reflect(gauge_box_),
|
2021-08-09 05:25:20 +08:00
|
|
|
text("]"),
|
2021-04-29 06:18:58 +08:00
|
|
|
}) | xflex,
|
|
|
|
}) |
|
|
|
|
gauge_color | xflex | reflect(box_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnEvent(Event event) final {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (event.is_mouse()) {
|
2021-04-29 06:18:58 +08:00
|
|
|
return OnMouseEvent(event);
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-04-29 06:18:58 +08:00
|
|
|
|
|
|
|
if (event == Event::ArrowLeft || event == Event::Character('h')) {
|
2022-08-13 22:26:53 +08:00
|
|
|
value_() -= increment_();
|
|
|
|
value_() = std::max(value_(), min_());
|
2021-04-29 06:18:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event == Event::ArrowRight || event == Event::Character('l')) {
|
2022-08-13 22:26:53 +08:00
|
|
|
value_() += increment_();
|
|
|
|
value_() = std::min(*value_, max_());
|
2021-04-29 06:18:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
return ComponentBase::OnEvent(event);
|
2021-04-29 06:18:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OnMouseEvent(Event event) {
|
2021-05-02 00:13:56 +08:00
|
|
|
if (captured_mouse_ && event.mouse().motion == Mouse::Released) {
|
|
|
|
captured_mouse_ = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
if (box_.Contain(event.mouse().x, event.mouse().y) && CaptureMouse(event)) {
|
2021-05-02 00:13:56 +08:00
|
|
|
TakeFocus();
|
|
|
|
}
|
|
|
|
|
2021-04-29 06:18:58 +08:00
|
|
|
if (event.mouse().button == Mouse::Left &&
|
2021-05-02 00:13:56 +08:00
|
|
|
event.mouse().motion == Mouse::Pressed &&
|
|
|
|
gauge_box_.Contain(event.mouse().x, event.mouse().y) &&
|
|
|
|
!captured_mouse_) {
|
2021-05-02 02:40:35 +08:00
|
|
|
captured_mouse_ = CaptureMouse(event);
|
2021-05-02 00:13:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (captured_mouse_) {
|
2022-08-21 23:23:13 +08:00
|
|
|
value_() = min_() + (event.mouse().x - gauge_box_.x_min) *
|
|
|
|
(max_() - min_()) /
|
|
|
|
(gauge_box_.x_max - gauge_box_.x_min);
|
2022-08-13 22:26:53 +08:00
|
|
|
value_() = std::max(min_(), std::min(max_(), value_()));
|
2021-05-02 00:13:56 +08:00
|
|
|
return true;
|
2021-04-29 06:18:58 +08:00
|
|
|
}
|
2021-05-02 00:13:56 +08:00
|
|
|
return false;
|
2021-04-29 06:18:58 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 02:32:33 +08:00
|
|
|
bool Focusable() const final { return true; }
|
2021-08-06 04:40:40 +08:00
|
|
|
|
2021-04-29 06:18:58 +08:00
|
|
|
private:
|
2021-08-09 05:25:20 +08:00
|
|
|
ConstStringRef label_;
|
2022-08-13 22:26:53 +08:00
|
|
|
Ref<T> value_;
|
|
|
|
ConstRef<T> min_;
|
|
|
|
ConstRef<T> max_;
|
|
|
|
ConstRef<T> increment_;
|
2021-04-29 06:18:58 +08:00
|
|
|
Box box_;
|
|
|
|
Box gauge_box_;
|
2021-05-02 00:13:56 +08:00
|
|
|
CapturedMouse captured_mouse_;
|
2021-04-29 06:18:58 +08:00
|
|
|
};
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
/// @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;
|
2021-08-09 05:25:20 +08:00
|
|
|
/// auto slider = Slider("Value:", &value, 0, 100, 1);
|
2021-05-10 02:32:27 +08:00
|
|
|
/// screen.Loop(slider);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// Value:[██████████████████████████ ]
|
|
|
|
/// ```
|
2022-08-13 22:26:53 +08:00
|
|
|
Component Slider(ConstStringRef label,
|
|
|
|
Ref<int> value,
|
|
|
|
ConstRef<int> min,
|
|
|
|
ConstRef<int> max,
|
|
|
|
ConstRef<int> increment) {
|
2022-08-21 23:23:13 +08:00
|
|
|
return Make<SliderBase<int>>(std::move(label), value, min, max, increment);
|
2022-08-13 22:26:53 +08:00
|
|
|
}
|
|
|
|
Component Slider(ConstStringRef label,
|
|
|
|
Ref<float> value,
|
|
|
|
ConstRef<float> min,
|
|
|
|
ConstRef<float> max,
|
|
|
|
ConstRef<float> increment) {
|
2022-08-21 23:23:13 +08:00
|
|
|
return Make<SliderBase<float>>(std::move(label), value, min, max, increment);
|
2022-08-13 22:26:53 +08:00
|
|
|
}
|
|
|
|
Component Slider(ConstStringRef label,
|
|
|
|
Ref<long> value,
|
|
|
|
ConstRef<long> min,
|
|
|
|
ConstRef<long> max,
|
|
|
|
ConstRef<long> increment) {
|
2022-08-21 23:23:13 +08:00
|
|
|
return Make<SliderBase<long>>(std::move(label), value, min, max, increment);
|
2021-04-29 06:18:58 +08:00
|
|
|
}
|
2021-06-03 03:11:23 +08:00
|
|
|
|
2021-04-29 06:18:58 +08:00
|
|
|
} // namespace ftxui
|
2021-05-02 02:40:35 +08:00
|
|
|
|
|
|
|
// 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.
|