This commit is contained in:
ArthurSonzogni 2024-10-29 08:01:19 +01:00
parent 27388d456f
commit a23879a273
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C
4 changed files with 128 additions and 98 deletions

View File

@ -39,6 +39,8 @@ current (development)
component in its parent. See #932 component in its parent. See #932
- Feature: Add `EntryState::index`. This allows to get the index of a menu entry. - Feature: Add `EntryState::index`. This allows to get the index of a menu entry.
See #932 See #932
- Feature: Add `SliderOption::on_change`. This allows to set a callback when the
slider value changes. See #938.
### Dom ### Dom
- Feature: Add `hscroll_indicator`. It display an horizontal indicator - Feature: Add `hscroll_indicator`. It display an horizontal indicator

View File

@ -222,13 +222,13 @@ struct ResizableSplitOption {
template <typename T> template <typename T>
struct SliderOption { struct SliderOption {
Ref<T> value; Ref<T> value;
std::function<void(T)> callback = nullptr;
ConstRef<T> min = T(0); ConstRef<T> min = T(0);
ConstRef<T> max = T(100); ConstRef<T> max = T(100);
ConstRef<T> increment = (max() - min()) / 20; ConstRef<T> increment = (max() - min()) / 20;
Direction direction = Direction::Right; Direction direction = Direction::Right;
Color color_active = Color::White; Color color_active = Color::White;
Color color_inactive = Color::GrayDark; Color color_inactive = Color::GrayDark;
std::function<void()> on_change; ///> Called when `value` is updated.
}; };
// Parameter pack used by `WindowOptions::render`. // Parameter pack used by `WindowOptions::render`.

View File

@ -35,76 +35,72 @@ Decorator flexDirection(Direction direction) {
} }
template <class T> template <class T>
class SliderBase : public ComponentBase { class SliderBase : public SliderOption<T>, public ComponentBase {
public: public:
explicit SliderBase(SliderOption<T> options) explicit SliderBase(SliderOption<T> options) : SliderOption<T>(options) {}
: callback_(options.callback), min_(options.min), max_(options.max),
increment_(options.increment), options_(options) {
SetValue(options.value());
}
Element Render() override { Element Render() override {
auto gauge_color = Focused() ? color(options_.color_active) auto gauge_color =
: color(options_.color_inactive); Focused() ? color(this->color_active) : color(this->color_inactive);
const float percent = float(value_() - min_()) / float(max_() - min_()); const float percent =
return gaugeDirection(percent, options_.direction) | float(this->value() - this->min()) / float(this->max() - this->min());
flexDirection(options_.direction) | reflect(gauge_box_) | return gaugeDirection(percent, this->direction) |
gauge_color; flexDirection(this->direction) | reflect(gauge_box_) | gauge_color;
} }
void OnLeft() { void OnLeft() {
switch (options_.direction) { switch (this->direction) {
case Direction::Right: case Direction::Right:
SetValue(value_() - increment_()); this->value() -= this->increment();
break; break;
case Direction::Left: case Direction::Left:
SetValue(value_() + increment_()); this->value() += this->increment();
break; break;
case Direction::Up: case Direction::Up:
case Direction::Down: case Direction::Down:
break; break;
} }
} }
void OnRight() { void OnRight() {
switch (options_.direction) { switch (this->direction) {
case Direction::Right: case Direction::Right:
SetValue(value_() + increment_()); this->value() += this->increment();
break; break;
case Direction::Left: case Direction::Left:
SetValue(value_() - increment_()); this->value() -= this->increment();
break; break;
case Direction::Up: case Direction::Up:
case Direction::Down: case Direction::Down:
break; break;
} }
} }
void OnUp() { void OnUp() {
switch (options_.direction) { switch (this->direction) {
case Direction::Up: case Direction::Up:
SetValue(value_() - increment_()); this->value() -= this->increment();
break; break;
case Direction::Down: case Direction::Down:
SetValue(value_() + increment_()); this->value() += this->increment();
break; break;
case Direction::Left: case Direction::Left:
case Direction::Right: case Direction::Right:
break; break;
} }
} }
void OnDown() { void OnDown() {
switch (options_.direction) { switch (this->direction) {
case Direction::Down: case Direction::Down:
SetValue(value_() - increment_()); this->value() += this->increment();
break; break;
case Direction::Up: case Direction::Up:
SetValue(value_() + increment_()); this->value() -= this->increment();
break; break;
case Direction::Left: case Direction::Left:
case Direction::Right: case Direction::Right:
break; break;
} }
} }
@ -113,7 +109,7 @@ public:
return OnMouseEvent(event); return OnMouseEvent(event);
} }
T old_value = value_(); T old_value = this->value();
if (event == Event::ArrowLeft || event == Event::Character('h')) { if (event == Event::ArrowLeft || event == Event::Character('h')) {
OnLeft(); OnLeft();
} }
@ -127,8 +123,11 @@ public:
OnUp(); OnUp();
} }
SetValue(util::clamp(value_(), min_(), max_())); this->value() = std::max(this->min(), std::min(this->max(), this->value()));
if (old_value != value_()) { if (old_value != this->value()) {
if (this->on_change) {
this->on_change();
}
return true; return true;
} }
@ -142,35 +141,45 @@ public:
return true; return true;
} }
switch (options_.direction) { T old_value = this->value();
case Direction::Right: { switch (this->direction) {
SetValue(min_() + (event.mouse().x - gauge_box_.x_min) * case Direction::Right: {
(max_() - min_()) / this->value() =
(gauge_box_.x_max - gauge_box_.x_min)); this->min() + (event.mouse().x - gauge_box_.x_min) *
(this->max() - this->min()) /
(gauge_box_.x_max - gauge_box_.x_min);
break; break;
} }
case Direction::Left: { case Direction::Left: {
SetValue(max_() - (event.mouse().x - gauge_box_.x_min) * this->value() =
(max_() - min_()) / this->max() - (event.mouse().x - gauge_box_.x_min) *
(gauge_box_.x_max - gauge_box_.x_min)); (this->max() - this->min()) /
break; (gauge_box_.x_max - gauge_box_.x_min);
} break;
case Direction::Down: { }
SetValue(min_() + (event.mouse().y - gauge_box_.y_min) * case Direction::Down: {
(max_() - min_()) / this->value() =
(gauge_box_.y_max - gauge_box_.y_min)); this->min() + (event.mouse().y - gauge_box_.y_min) *
break; (this->max() - this->min()) /
} (gauge_box_.y_max - gauge_box_.y_min);
case Direction::Up: { break;
SetValue(max_() - (event.mouse().y - gauge_box_.y_min) * }
(max_() - min_()) / case Direction::Up: {
(gauge_box_.y_max - gauge_box_.y_min)); this->value() =
break; this->max() - (event.mouse().y - gauge_box_.y_min) *
} (this->max() - this->min()) /
(gauge_box_.y_max - gauge_box_.y_min);
break;
}
} }
SetValue(std::max(min_(), std::min(max_(), value_()))); 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; return true;
} }
@ -197,25 +206,11 @@ public:
bool Focusable() const final { return true; } bool Focusable() const final { return true; }
void SetValue(Ref<T> val) { private:
value_() = util::clamp(val(), min_(), max_());
if (callback_ != nullptr) {
callback_(value_());
}
}
private:
std::function<void(T)> callback_;
Ref<T> value_;
ConstRef<T> min_;
ConstRef<T> max_;
ConstRef<T> increment_;
SliderOption<T> options_;
Box gauge_box_; Box gauge_box_;
CapturedMouse captured_mouse_; CapturedMouse captured_mouse_;
}; };
class SliderWithLabel : public ComponentBase { class SliderWithLabel : public ComponentBase {
public: public:
SliderWithLabel(ConstStringRef label, Component inner) SliderWithLabel(ConstStringRef label, Component inner)

View File

@ -45,6 +45,7 @@ Event MouseReleased(int x, int y) {
} // namespace } // namespace
TEST(SliderTest, Right) { TEST(SliderTest, Right) {
int updated = 0;
int value = 50; int value = 50;
auto slider = Slider<int>({ auto slider = Slider<int>({
.value = &value, .value = &value,
@ -52,23 +53,31 @@ TEST(SliderTest, Right) {
.max = 100, .max = 100,
.increment = 10, .increment = 10,
.direction = Direction::Right, .direction = Direction::Right,
.on_change = [&]() { updated++; },
}); });
Screen screen(11, 1); Screen screen(11, 1);
Render(screen, slider->Render()); Render(screen, slider->Render());
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 0);
EXPECT_TRUE(slider->OnEvent(MousePressed(3, 0))); EXPECT_TRUE(slider->OnEvent(MousePressed(3, 0)));
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 0);
EXPECT_TRUE(slider->OnEvent(MousePressed(9, 0))); EXPECT_TRUE(slider->OnEvent(MousePressed(9, 0)));
EXPECT_EQ(value, 90); EXPECT_EQ(value, 90);
EXPECT_EQ(updated, 1);
EXPECT_TRUE(slider->OnEvent(MousePressed(9, 2))); EXPECT_TRUE(slider->OnEvent(MousePressed(9, 2)));
EXPECT_EQ(value, 90); EXPECT_EQ(value, 90);
EXPECT_EQ(updated, 1);
EXPECT_TRUE(slider->OnEvent(MousePressed(5, 2))); EXPECT_TRUE(slider->OnEvent(MousePressed(5, 2)));
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 2);
EXPECT_TRUE(slider->OnEvent(MouseReleased(5, 2))); EXPECT_TRUE(slider->OnEvent(MouseReleased(5, 2)));
EXPECT_FALSE(slider->OnEvent(MousePressed(5, 2))); EXPECT_FALSE(slider->OnEvent(MousePressed(5, 2)));
EXPECT_EQ(value, 50);
} }
TEST(SliderTest, Left) { TEST(SliderTest, Left) {
int updated = 0;
int value = 50; int value = 50;
auto slider = Slider<int>({ auto slider = Slider<int>({
.value = &value, .value = &value,
@ -76,23 +85,31 @@ TEST(SliderTest, Left) {
.max = 100, .max = 100,
.increment = 10, .increment = 10,
.direction = Direction::Left, .direction = Direction::Left,
.on_change = [&]() { updated++; },
}); });
Screen screen(11, 1); Screen screen(11, 1);
Render(screen, slider->Render()); Render(screen, slider->Render());
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 0);
EXPECT_TRUE(slider->OnEvent(MousePressed(3, 0))); EXPECT_TRUE(slider->OnEvent(MousePressed(3, 0)));
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 0);
EXPECT_TRUE(slider->OnEvent(MousePressed(9, 0))); EXPECT_TRUE(slider->OnEvent(MousePressed(9, 0)));
EXPECT_EQ(value, 10); EXPECT_EQ(value, 10);
EXPECT_EQ(updated, 1);
EXPECT_TRUE(slider->OnEvent(MousePressed(9, 2))); EXPECT_TRUE(slider->OnEvent(MousePressed(9, 2)));
EXPECT_EQ(value, 10); EXPECT_EQ(value, 10);
EXPECT_EQ(updated, 1);
EXPECT_TRUE(slider->OnEvent(MousePressed(5, 2))); EXPECT_TRUE(slider->OnEvent(MousePressed(5, 2)));
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 2);
EXPECT_TRUE(slider->OnEvent(MouseReleased(5, 2))); EXPECT_TRUE(slider->OnEvent(MouseReleased(5, 2)));
EXPECT_FALSE(slider->OnEvent(MousePressed(5, 2))); EXPECT_FALSE(slider->OnEvent(MousePressed(5, 2)));
EXPECT_EQ(value, 50);
} }
TEST(SliderTest, Down) { TEST(SliderTest, Down) {
int updated = 0;
int value = 50; int value = 50;
auto slider = Slider<int>({ auto slider = Slider<int>({
.value = &value, .value = &value,
@ -100,23 +117,32 @@ TEST(SliderTest, Down) {
.max = 100, .max = 100,
.increment = 10, .increment = 10,
.direction = Direction::Down, .direction = Direction::Down,
.on_change = [&]() { updated++; },
}); });
Screen screen(1, 11); Screen screen(1, 11);
Render(screen, slider->Render()); Render(screen, slider->Render());
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 0);
EXPECT_TRUE(slider->OnEvent(MousePressed(0, 3))); EXPECT_TRUE(slider->OnEvent(MousePressed(0, 3)));
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 0);
EXPECT_TRUE(slider->OnEvent(MousePressed(0, 9))); EXPECT_TRUE(slider->OnEvent(MousePressed(0, 9)));
EXPECT_EQ(value, 90); EXPECT_EQ(value, 90);
EXPECT_EQ(updated, 1);
EXPECT_TRUE(slider->OnEvent(MousePressed(2, 9))); EXPECT_TRUE(slider->OnEvent(MousePressed(2, 9)));
EXPECT_EQ(value, 90); EXPECT_EQ(value, 90);
EXPECT_EQ(updated, 1);
EXPECT_TRUE(slider->OnEvent(MousePressed(2, 5))); EXPECT_TRUE(slider->OnEvent(MousePressed(2, 5)));
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 2);
EXPECT_TRUE(slider->OnEvent(MouseReleased(2, 5))); EXPECT_TRUE(slider->OnEvent(MouseReleased(2, 5)));
EXPECT_FALSE(slider->OnEvent(MousePressed(2, 5))); EXPECT_FALSE(slider->OnEvent(MousePressed(2, 5)));
EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 2);
} }
TEST(SliderTest, Up) { TEST(SliderTest, Up) {
int updated = 0;
int value = 50; int value = 50;
auto slider = Slider<int>({ auto slider = Slider<int>({
.value = &value, .value = &value,
@ -124,20 +150,27 @@ TEST(SliderTest, Up) {
.max = 100, .max = 100,
.increment = 10, .increment = 10,
.direction = Direction::Up, .direction = Direction::Up,
.on_change = [&]() { updated++; },
}); });
Screen screen(1, 11); Screen screen(1, 11);
Render(screen, slider->Render()); Render(screen, slider->Render());
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 0);
EXPECT_TRUE(slider->OnEvent(MousePressed(0, 3))); EXPECT_TRUE(slider->OnEvent(MousePressed(0, 3)));
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 0);
EXPECT_TRUE(slider->OnEvent(MousePressed(0, 9))); EXPECT_TRUE(slider->OnEvent(MousePressed(0, 9)));
EXPECT_EQ(value, 10); EXPECT_EQ(value, 10);
EXPECT_EQ(updated, 1);
EXPECT_TRUE(slider->OnEvent(MousePressed(2, 9))); EXPECT_TRUE(slider->OnEvent(MousePressed(2, 9)));
EXPECT_EQ(value, 10); EXPECT_EQ(value, 10);
EXPECT_EQ(updated, 1);
EXPECT_TRUE(slider->OnEvent(MousePressed(2, 5))); EXPECT_TRUE(slider->OnEvent(MousePressed(2, 5)));
EXPECT_EQ(value, 50); EXPECT_EQ(value, 50);
EXPECT_EQ(updated, 2);
EXPECT_TRUE(slider->OnEvent(MouseReleased(2, 5))); EXPECT_TRUE(slider->OnEvent(MouseReleased(2, 5)));
EXPECT_FALSE(slider->OnEvent(MousePressed(2, 5))); EXPECT_FALSE(slider->OnEvent(MousePressed(2, 5)));
EXPECT_EQ(value, 50);
} }
TEST(SliderTest, Focus) { TEST(SliderTest, Focus) {