#ifndef UI_VIEWS_CONTROLS_SLIDER_H_
#define UI_VIEWS_CONTROLS_SLIDER_H_
#include <memory>
#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/animation/animation_delegate.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/views/view.h"
#include "ui/views/views_export.h"
namespace views {
namespace test {
class SliderTestApi;
}
class Slider;
enum class SliderChangeReason {
kByUser,
kByApi,
};
class VIEWS_EXPORT SliderListener {
public:
virtual void SliderValueChanged(Slider* sender,
float value,
float old_value,
SliderChangeReason reason) = 0;
virtual void SliderDragStarted(Slider* sender) {}
virtual void SliderDragEnded(Slider* sender) {}
protected:
virtual ~SliderListener() = default;
};
class VIEWS_EXPORT Slider : public View, public gfx::AnimationDelegate {
public:
METADATA_HEADER(Slider);
explicit Slider(SliderListener* listener = nullptr);
Slider(const Slider&) = delete;
Slider& operator=(const Slider&) = delete;
~Slider() override;
float GetValue() const;
void SetValue(float value);
float GetValueIndicatorRadius() const;
void SetValueIndicatorRadius(float radius);
bool GetEnableAccessibilityEvents() const;
void SetEnableAccessibilityEvents(bool enabled);
enum class RenderingStyle {
kDefaultStyle,
kMinimalStyle,
};
void SetRenderingStyle(RenderingStyle style);
RenderingStyle style() const { return style_; }
void SetAllowedValues(const base::flat_set<float>* allowed_values);
const base::flat_set<float>& allowed_values() const {
return allowed_values_;
}
static constexpr float kThumbRadius = 4.f;
protected:
float GetAnimatingValue() const;
void SetHighlighted(bool is_highlighted);
void AnimationProgressed(const gfx::Animation* animation) override;
void AnimationEnded(const gfx::Animation* animation) override;
void OnPaint(gfx::Canvas* canvas) override;
private:
friend class test::SliderTestApi;
void SetValueInternal(float value, SliderChangeReason reason);
void PrepareForMove(const int new_x);
void MoveButtonTo(const gfx::Point& point);
void OnSliderDragStarted();
void OnSliderDragEnded();
gfx::Size CalculatePreferredSize() const override;
bool OnMousePressed(const ui::MouseEvent& event) override;
bool OnMouseDragged(const ui::MouseEvent& event) override;
void OnMouseReleased(const ui::MouseEvent& event) override;
bool OnKeyPressed(const ui::KeyEvent& event) override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
bool HandleAccessibleAction(const ui::AXActionData& action_data) override;
void OnFocus() override;
void OnBlur() override;
void VisibilityChanged(View* starting_from, bool is_visible) override;
void AddedToWidget() override;
void OnGestureEvent(ui::GestureEvent* event) override;
void set_listener(SliderListener* listener) { listener_ = listener; }
void NotifyPendingAccessibilityValueChanged();
virtual SkColor GetThumbColor() const;
virtual SkColor GetTroughColor() const;
int GetSliderExtraPadding() const;
raw_ptr<SliderListener, DanglingUntriaged> listener_;
std::unique_ptr<gfx::SlideAnimation> move_animation_;
base::flat_set<float> allowed_values_;
float value_ = 0.f;
float keyboard_increment_ = 0.1f;
float initial_animating_value_ = 0.f;
bool value_is_valid_ = false;
bool accessibility_events_enabled_ = true;
int initial_button_offset_ = 0;
float value_indicator_radius_ = kThumbRadius;
RenderingStyle style_ = RenderingStyle::kDefaultStyle;
float thumb_highlight_radius_ = 0.f;
gfx::SlideAnimation highlight_animation_{this};
bool pending_accessibility_value_change_ = false;
};
}
#endif