Add ref for sliders. (#457)

This resolves:
https://github.com/ArthurSonzogni/FTXUI/issues/456
This commit is contained in:
Arthur Sonzogni 2022-08-13 16:26:53 +02:00 committed by GitHub
parent f461050759
commit d755356481
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 40 deletions

View File

@ -14,6 +14,7 @@ current (development)
### Component: ### Component:
- Feature: Add the `Modal` component. - Feature: Add the `Modal` component.
- Feature: `Slider` supports taking references for all its arguments.
### Screen ### Screen
- Feature: add `Box::Union(a,b) -> Box` - Feature: add `Box::Union(a,b) -> Box`

View File

@ -67,8 +67,21 @@ Component Radiobox(ConstStringListRef entries,
Ref<RadioboxOption> option = {}); Ref<RadioboxOption> option = {});
Component Toggle(ConstStringListRef entries, int* selected); Component Toggle(ConstStringListRef entries, int* selected);
template <class T> // T = {int, float, long} Component Slider(ConstStringRef label,
Component Slider(ConstStringRef label, T* value, T min, T max, T increment); Ref<int> value,
ConstRef<int> min = 0,
ConstRef<int> max = 100,
ConstRef<int> increment = 5);
Component Slider(ConstStringRef label,
Ref<float> value,
ConstRef<float> min = 0.f,
ConstRef<float> max = 100.f,
ConstRef<float> increment = 5.f);
Component Slider(ConstStringRef label,
Ref<long> value,
ConstRef<long> min = 0l,
ConstRef<long> max = 100l,
ConstRef<long> increment = 5l);
Component ResizableSplitLeft(Component main, Component back, int* main_size); Component ResizableSplitLeft(Component main, Component back, int* main_size);
Component ResizableSplitRight(Component main, Component back, int* main_size); Component ResizableSplitRight(Component main, Component back, int* main_size);

View File

@ -66,6 +66,9 @@ class ConstStringRef {
ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {} ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {}
ConstStringRef(const char* ref) ConstStringRef(const char* ref)
: ConstStringRef(to_wstring(std::string(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_; }
const std::string* operator->() const { const std::string* operator->() const {
return address_ ? address_ : &owned_; return address_ ? address_ : &owned_;

View File

@ -17,19 +17,23 @@ namespace ftxui {
template <class T> template <class T>
class SliderBase : public ComponentBase { class SliderBase : public ComponentBase {
public: public:
SliderBase(ConstStringRef label, T* value, T min, T max, T increment) SliderBase(ConstStringRef label,
Ref<T> value,
ConstRef<T> min,
ConstRef<T> max,
ConstRef<T> increment)
: label_(std::move(label)), : label_(std::move(label)),
value_(value), value_(std::move(value)),
min_(min), min_(std::move(min)),
max_(max), max_(std::move(max)),
increment_(increment) {} increment_(std::move(increment)) {}
Element Render() override { Element Render() override {
auto gauge_color = auto gauge_color =
Focused() ? color(Color::GrayLight) : color(Color::GrayDark); 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({ return hbox({
text(*label_) | dim | vcenter, text(label_()) | dim | vcenter,
hbox({ hbox({
text("["), text("["),
gauge(percent) | underlined | xflex | reflect(gauge_box_), gauge(percent) | underlined | xflex | reflect(gauge_box_),
@ -45,14 +49,14 @@ class SliderBase : public ComponentBase {
} }
if (event == Event::ArrowLeft || event == Event::Character('h')) { if (event == Event::ArrowLeft || event == Event::Character('h')) {
*value_ -= increment_; value_() -= increment_();
*value_ = std::max(*value_, min_); value_() = std::max(value_(), min_());
return true; return true;
} }
if (event == Event::ArrowRight || event == Event::Character('l')) { if (event == Event::ArrowRight || event == Event::Character('l')) {
*value_ += increment_; value_() += increment_();
*value_ = std::min(*value_, max_); value_() = std::min(*value_, max_());
return true; return true;
} }
@ -77,9 +81,9 @@ class SliderBase : public ComponentBase {
} }
if (captured_mouse_) { 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); (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 true;
} }
return false; return false;
@ -89,10 +93,10 @@ class SliderBase : public ComponentBase {
private: private:
ConstStringRef label_; ConstStringRef label_;
T* value_; Ref<T> value_;
T min_; ConstRef<T> min_;
T max_; ConstRef<T> max_;
T increment_ = 1; ConstRef<T> increment_;
Box box_; Box box_;
Box gauge_box_; Box gauge_box_;
CapturedMouse captured_mouse_; CapturedMouse captured_mouse_;
@ -120,28 +124,30 @@ class SliderBase : public ComponentBase {
/// ```bash /// ```bash
/// Value:[██████████████████████████ ] /// Value:[██████████████████████████ ]
/// ``` /// ```
template <class T> Component Slider(ConstStringRef label,
Component Slider(ConstStringRef label, T* value, T min, T max, T increment) { Ref<int> value,
return Make<SliderBase<T>>(std::move(label), value, min, max, increment); ConstRef<int> min,
ConstRef<int> max,
ConstRef<int> increment) {
return Make<SliderBase<int>>(std::move(label), std::move(value), std::move(min),
std::move(max), std::move(increment));
}
Component Slider(ConstStringRef label,
Ref<float> value,
ConstRef<float> min,
ConstRef<float> max,
ConstRef<float> increment) {
return Make<SliderBase<float>>(std::move(label), std::move(value), std::move(min),
std::move(max), std::move(increment));
}
Component Slider(ConstStringRef label,
Ref<long> value,
ConstRef<long> min,
ConstRef<long> max,
ConstRef<long> increment) {
return Make<SliderBase<long>>(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 } // namespace ftxui