From d75535648196b5f6a4505205a9853d671e5c8ec6 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Sat, 13 Aug 2022 16:26:53 +0200 Subject: [PATCH] Add ref for sliders. (#457) This resolves: https://github.com/ArthurSonzogni/FTXUI/issues/456 --- CHANGELOG.md | 1 + include/ftxui/component/component.hpp | 17 +++++- include/ftxui/util/ref.hpp | 3 + src/ftxui/component/slider.cpp | 82 ++++++++++++++------------- 4 files changed, 63 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4ce571..94a86ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ current (development) ### Component: - Feature: Add the `Modal` component. +- Feature: `Slider` supports taking references for all its arguments. ### Screen - Feature: add `Box::Union(a,b) -> Box` diff --git a/include/ftxui/component/component.hpp b/include/ftxui/component/component.hpp index 6e04cb0..fb8729d 100644 --- a/include/ftxui/component/component.hpp +++ b/include/ftxui/component/component.hpp @@ -67,8 +67,21 @@ Component Radiobox(ConstStringListRef entries, Ref option = {}); Component Toggle(ConstStringListRef entries, int* selected); -template // T = {int, float, long} -Component Slider(ConstStringRef label, T* value, T min, T max, T increment); +Component Slider(ConstStringRef label, + Ref value, + ConstRef min = 0, + ConstRef max = 100, + ConstRef increment = 5); +Component Slider(ConstStringRef label, + Ref value, + ConstRef min = 0.f, + ConstRef max = 100.f, + ConstRef increment = 5.f); +Component Slider(ConstStringRef label, + Ref value, + ConstRef min = 0l, + ConstRef max = 100l, + ConstRef increment = 5l); Component ResizableSplitLeft(Component main, Component back, int* main_size); Component ResizableSplitRight(Component main, Component back, int* main_size); diff --git a/include/ftxui/util/ref.hpp b/include/ftxui/util/ref.hpp index d7eb64a..4381065 100644 --- a/include/ftxui/util/ref.hpp +++ b/include/ftxui/util/ref.hpp @@ -66,6 +66,9 @@ class ConstStringRef { ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {} ConstStringRef(const char* ref) : ConstStringRef(to_wstring(std::string(ref))) {} + const std::string& operator()() const { + return address_ ? *address_ : owned_; + } const std::string& operator*() const { return address_ ? *address_ : owned_; } const std::string* operator->() const { return address_ ? address_ : &owned_; diff --git a/src/ftxui/component/slider.cpp b/src/ftxui/component/slider.cpp index 1194c63..20aa5e9 100644 --- a/src/ftxui/component/slider.cpp +++ b/src/ftxui/component/slider.cpp @@ -17,19 +17,23 @@ namespace ftxui { template class SliderBase : public ComponentBase { public: - SliderBase(ConstStringRef label, T* value, T min, T max, T increment) + SliderBase(ConstStringRef label, + Ref value, + ConstRef min, + ConstRef max, + ConstRef increment) : label_(std::move(label)), - value_(value), - min_(min), - max_(max), - increment_(increment) {} + value_(std::move(value)), + min_(std::move(min)), + max_(std::move(max)), + increment_(std::move(increment)) {} Element Render() override { auto gauge_color = Focused() ? color(Color::GrayLight) : color(Color::GrayDark); - float percent = float(*value_ - min_) / float(max_ - min_); + float percent = float(value_() - min_()) / float(max_() - min_()); return hbox({ - text(*label_) | dim | vcenter, + text(label_()) | dim | vcenter, hbox({ text("["), gauge(percent) | underlined | xflex | reflect(gauge_box_), @@ -45,14 +49,14 @@ class SliderBase : public ComponentBase { } if (event == Event::ArrowLeft || event == Event::Character('h')) { - *value_ -= increment_; - *value_ = std::max(*value_, min_); + value_() -= increment_(); + value_() = std::max(value_(), min_()); return true; } if (event == Event::ArrowRight || event == Event::Character('l')) { - *value_ += increment_; - *value_ = std::min(*value_, max_); + value_() += increment_(); + value_() = std::min(*value_, max_()); return true; } @@ -77,9 +81,9 @@ class SliderBase : public ComponentBase { } if (captured_mouse_) { - *value_ = min_ + (event.mouse().x - gauge_box_.x_min) * (max_ - min_) / + value_() = min_() + (event.mouse().x - gauge_box_.x_min) * (max_() - min_()) / (gauge_box_.x_max - gauge_box_.x_min); - *value_ = std::max(min_, std::min(max_, *value_)); + value_() = std::max(min_(), std::min(max_(), value_())); return true; } return false; @@ -89,10 +93,10 @@ class SliderBase : public ComponentBase { private: ConstStringRef label_; - T* value_; - T min_; - T max_; - T increment_ = 1; + Ref value_; + ConstRef min_; + ConstRef max_; + ConstRef increment_; Box box_; Box gauge_box_; CapturedMouse captured_mouse_; @@ -120,28 +124,30 @@ class SliderBase : public ComponentBase { /// ```bash /// Value:[██████████████████████████ ] /// ``` -template -Component Slider(ConstStringRef label, T* value, T min, T max, T increment) { - return Make>(std::move(label), value, min, max, increment); +Component Slider(ConstStringRef label, + Ref value, + ConstRef min, + ConstRef max, + ConstRef increment) { + return Make>(std::move(label), std::move(value), std::move(min), + std::move(max), std::move(increment)); +} +Component Slider(ConstStringRef label, + Ref value, + ConstRef min, + ConstRef max, + ConstRef increment) { + return Make>(std::move(label), std::move(value), std::move(min), + std::move(max), std::move(increment)); +} +Component Slider(ConstStringRef label, + Ref value, + ConstRef min, + ConstRef max, + ConstRef increment) { + return Make>(std::move(label), std::move(value), std::move(min), + std::move(max), std::move(increment)); } - -template Component Slider(ConstStringRef label, - int* value, - int min, - int max, - int increment); - -template Component Slider(ConstStringRef label, - float* value, - float min, - float max, - float increment); - -template Component Slider(ConstStringRef label, - long* value, - long min, - long max, - long increment); } // namespace ftxui