Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Unified Diff: ui/views/controls/md_slider.cc

Issue 2335513002: Adding ripple effect for clicks on MD slider (Closed)
Patch Set: improve animation in slider and address comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/controls/md_slider.cc
diff --git a/ui/views/controls/md_slider.cc b/ui/views/controls/md_slider.cc
index 1a58eaec55cd9ec966c6a5ea67eb260224616532..87fcc2a7d0609a5c20cdbed98fbc76343bd1fad1 100644
--- a/ui/views/controls/md_slider.cc
+++ b/ui/views/controls/md_slider.cc
@@ -6,6 +6,7 @@
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPaint.h"
+#include "ui/gfx/animation/slide_animation.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
@@ -16,18 +17,24 @@ namespace views {
// Color of slider at the active and the disabled state, respectively.
const SkColor kActiveColor = SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4);
const SkColor kDisabledColor = SkColorSetARGB(0x42, 0x00, 0x00, 0x00);
+const SkColor kHighlightColor = SkColorSetARGB(0x4D, 0x42, 0x85, 0xF4);
varkha 2016/09/23 22:30:25 Can this be a derivative of kActiveColor?
yiyix 2016/09/24 06:24:36 I now try to save the alpha value instead and crea
// The thickness of the slider.
const int kLineThickness = 2;
-// The radius of the thumb of the slider.
-const int kThumbRadius = 6;
+// The radius of the thumb and the highlighted thumb of the slider,
+// respectively.
+const float kThumbRadius = 6.f;
+const float kThumbHighlightRadius = 10.f;
// The stroke of the thumb when the slider is disabled.
const int kThumbStroke = 2;
+// Duration of the thumb highlight growing effect animation.
+const int kSlideHighlightChangeDurationMs = 150;
+
MdSlider::MdSlider(SliderListener* listener)
- : Slider(listener), is_active_(true) {
+ : Slider(listener), is_active_(true), thumb_highlight_radius_(0.f) {
SchedulePaint();
}
@@ -37,17 +44,26 @@ void MdSlider::OnPaint(gfx::Canvas* canvas) {
Slider::OnPaint(canvas);
// Paint the slider.
- const int thumb_size = kThumbRadius * 2;
const gfx::Rect content = GetContentsBounds();
- const int width = content.width() - thumb_size;
+ const int width = content.width() - kThumbRadius * 2;
const int full = GetAnimatingValue() * width;
const int empty = width - full;
const int y = content.height() / 2 - kLineThickness / 2;
+ const int x = content.x() + full + kThumbRadius;
canvas->FillRect(gfx::Rect(content.x(), y, full, kLineThickness),
is_active_ ? kActiveColor : kDisabledColor);
varkha 2016/09/23 22:30:25 nit: you can cache the color and use it below.
yiyix 2016/09/24 06:24:35 Sure. thanks.
- canvas->FillRect(
- gfx::Rect(content.x() + full + thumb_size, y, empty, kLineThickness),
- kDisabledColor);
+ canvas->FillRect(gfx::Rect(x + kThumbRadius, y, empty, kLineThickness),
+ kDisabledColor);
+
+ gfx::Point thumb_center(x, content.height() / 2);
+
+ // Paint the thumb highlight if it exists.
+ if (is_active_ && thumb_highlight_radius_ > kThumbRadius) {
+ SkPaint highlight;
+ highlight.setColor(kHighlightColor);
+ highlight.setFlags(SkPaint::kAntiAlias_Flag);
+ canvas->DrawCircle(thumb_center, thumb_highlight_radius_, highlight);
+ }
// Paint the thumb of the slider.
SkPaint paint;
@@ -59,7 +75,7 @@ void MdSlider::OnPaint(gfx::Canvas* canvas) {
paint.setStyle(SkPaint::kStroke_Style);
}
canvas->DrawCircle(
- gfx::Point(content.x() + full + kThumbRadius, content.height() / 2),
+ thumb_center,
is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint);
}
@@ -75,4 +91,32 @@ void MdSlider::UpdateState(bool control_on) {
int MdSlider::GetThumbWidth() {
return kThumbRadius * 2;
}
+
+void MdSlider::SetHighlighted(bool is_highlighted) {
+ if (!highlight_animation_)
+ highlight_animation_.reset(new gfx::SlideAnimation(this));
+ if (is_highlighted) {
+ highlight_animation_->SetSlideDuration(kSlideHighlightChangeDurationMs);
+ highlight_animation_->Show();
+ } else {
+ highlight_animation_->Hide();
+ }
+}
+
+void MdSlider::AnimationProgressed(const gfx::Animation* animation) {
+ if (animation != highlight_animation_.get()) {
+ Slider::AnimationProgressed(animation);
+ return;
+ }
+ thumb_highlight_radius_ =
+ animation->CurrentValueBetween(kThumbRadius, kThumbHighlightRadius);
+ SchedulePaint();
+}
+
+void MdSlider::AnimationEnded(const gfx::Animation* animation) {
+ if (animation == highlight_animation_.get() &&
+ !highlight_animation_->IsShowing())
tdanderson 2016/09/23 20:55:50 nit: include {} since boolean condition spans > 1
yiyix 2016/09/23 21:39:57 Done.
+ highlight_animation_.reset();
+}
+
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698