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:
- Feature: Add the `Modal` component.
- Feature: `Slider` supports taking references for all its arguments.
### Screen
- Feature: add `Box::Union(a,b) -> Box`

View File

@ -67,8 +67,21 @@ Component Radiobox(ConstStringListRef entries,
Ref<RadioboxOption> option = {});
Component Toggle(ConstStringListRef entries, int* selected);
template <class T> // T = {int, float, long}
Component Slider(ConstStringRef label, T* value, T min, T max, T increment);
Component Slider(ConstStringRef label,
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 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 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_;

View File

@ -17,19 +17,23 @@ namespace ftxui {
template <class T>
class SliderBase : public ComponentBase {
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)),
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<T> value_;
ConstRef<T> min_;
ConstRef<T> max_;
ConstRef<T> increment_;
Box box_;
Box gauge_box_;
CapturedMouse captured_mouse_;
@ -120,28 +124,30 @@ class SliderBase : public ComponentBase {
/// ```bash
/// Value:[██████████████████████████ ]
/// ```
template <class T>
Component Slider(ConstStringRef label, T* value, T min, T max, T increment) {
return Make<SliderBase<T>>(std::move(label), value, min, max, increment);
Component Slider(ConstStringRef label,
Ref<int> value,
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