refactor(ui/slider): improve track/thumb initialization and validation

Rename track/thumb accessors from set_track/set_thumb to get_track/get_thumb.
Add private init methods and a constant THUMB_WIDTH.
Add validation to set_width with log and assert.
Modify set_height to assume non-null m_thumb.
This commit is contained in:
2026-07-14 21:19:56 +08:00
parent a6fbbe8e1e
commit 4653ae9f06
2 changed files with 27 additions and 13 deletions

View File

@@ -10,15 +10,13 @@ public:
Slider& set_slider(float* value, const float& min, const float& max);
Image& set_track();
Image& set_thumb();
Image& get_track();
Image& get_thumb();
float width() const override;
float height() const override;
Slider& set_scale(float scale);
Slider& set_width(float width);
Slider& set_height(float h);
@@ -26,6 +24,11 @@ public:
bool handle_mouse_button_event(const MouseButtonEvent& e) override;
private:
static constexpr float THUMB_WIDTH = 5.0f;
void init_track();
void init_thumb();
void update_value(float mouse_x);
void on_update(float) override;

View File

@@ -6,7 +6,10 @@
#include <algorithm>
namespace Cubed {
Slider::Slider(Widget* parent) : Widget(parent) {}
Slider::Slider(Widget* parent) : Widget(parent) {
init_thumb();
init_track();
}
Slider& Slider::set_slider(float* value, const float& min, const float& max) {
m_value = value;
@@ -15,20 +18,27 @@ Slider& Slider::set_slider(float* value, const float& min, const float& max) {
return *this;
}
Image& Slider::set_track() {
Image& Slider::get_track() { return *m_track; }
Image& Slider::get_thumb() { return *m_thumb; }
void Slider::init_track() {
m_track = std::make_unique<Image>(this);
m_track->set_fill(true);
m_track->set_anchor(Anchor::TOP_LEFT);
return *m_track;
}
Image& Slider::set_thumb() {
void Slider::init_thumb() {
m_thumb = std::make_unique<Image>(this);
m_thumb->set_height(height());
m_thumb->set_width(10.0f);
m_thumb->set_width(THUMB_WIDTH);
m_thumb->set_anchor(Anchor::TOP_LEFT);
m_thumb->set_scale(m_scale);
return *m_thumb;
}
float Slider::width() const { return m_width * m_scale; }
@@ -43,6 +53,8 @@ Slider& Slider::set_scale(float scale) {
Slider& Slider::set_width(float width) {
if (!m_thumb || width < m_thumb->width()) {
Logger::error("Width is too small set failed!");
ASSERT(false);
return *this;
}
m_width = width;
@@ -51,9 +63,8 @@ Slider& Slider::set_width(float width) {
Slider& Slider::set_height(float h) {
m_height = h;
if (m_thumb) {
ASSERT_MSG(m_thumb, "Thumb is nullptr !");
m_thumb->set_height(height());
}
return *this;
}