Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/md_slider.h" | 5 #include "ui/views/controls/md_slider.h" |
| 6 | 6 |
| 7 #include "third_party/skia/include/core/SkColor.h" | 7 #include "third_party/skia/include/core/SkColor.h" |
| 8 #include "third_party/skia/include/core/SkPaint.h" | 8 #include "third_party/skia/include/core/SkPaint.h" |
| 9 #include "ui/gfx/animation/slide_animation.h" | |
| 9 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/geometry/point.h" | 11 #include "ui/gfx/geometry/point.h" |
| 11 #include "ui/gfx/geometry/rect.h" | 12 #include "ui/gfx/geometry/rect.h" |
| 12 #include "ui/views/controls/slider.h" | 13 #include "ui/views/controls/slider.h" |
| 13 | 14 |
| 14 namespace views { | 15 namespace views { |
| 15 | 16 |
| 16 // Color of slider at the active and the disabled state, respectively. | 17 // Color of slider at the active and the disabled state, respectively. |
| 17 const SkColor kActiveColor = SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4); | 18 const SkColor kActiveColor = SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4); |
| 18 const SkColor kDisabledColor = SkColorSetARGB(0x42, 0x00, 0x00, 0x00); | 19 const SkColor kDisabledColor = SkColorSetARGB(0x42, 0x00, 0x00, 0x00); |
| 20 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
| |
| 19 | 21 |
| 20 // The thickness of the slider. | 22 // The thickness of the slider. |
| 21 const int kLineThickness = 2; | 23 const int kLineThickness = 2; |
| 22 | 24 |
| 23 // The radius of the thumb of the slider. | 25 // The radius of the thumb and the highlighted thumb of the slider, |
| 24 const int kThumbRadius = 6; | 26 // respectively. |
| 27 const float kThumbRadius = 6.f; | |
| 28 const float kThumbHighlightRadius = 10.f; | |
| 25 | 29 |
| 26 // The stroke of the thumb when the slider is disabled. | 30 // The stroke of the thumb when the slider is disabled. |
| 27 const int kThumbStroke = 2; | 31 const int kThumbStroke = 2; |
| 28 | 32 |
| 33 // Duration of the thumb highlight growing effect animation. | |
| 34 const int kSlideHighlightChangeDurationMs = 150; | |
| 35 | |
| 29 MdSlider::MdSlider(SliderListener* listener) | 36 MdSlider::MdSlider(SliderListener* listener) |
| 30 : Slider(listener), is_active_(true) { | 37 : Slider(listener), is_active_(true), thumb_highlight_radius_(0.f) { |
| 31 SchedulePaint(); | 38 SchedulePaint(); |
| 32 } | 39 } |
| 33 | 40 |
| 34 MdSlider::~MdSlider() {} | 41 MdSlider::~MdSlider() {} |
| 35 | 42 |
| 36 void MdSlider::OnPaint(gfx::Canvas* canvas) { | 43 void MdSlider::OnPaint(gfx::Canvas* canvas) { |
| 37 Slider::OnPaint(canvas); | 44 Slider::OnPaint(canvas); |
| 38 | 45 |
| 39 // Paint the slider. | 46 // Paint the slider. |
| 40 const int thumb_size = kThumbRadius * 2; | |
| 41 const gfx::Rect content = GetContentsBounds(); | 47 const gfx::Rect content = GetContentsBounds(); |
| 42 const int width = content.width() - thumb_size; | 48 const int width = content.width() - kThumbRadius * 2; |
| 43 const int full = GetAnimatingValue() * width; | 49 const int full = GetAnimatingValue() * width; |
| 44 const int empty = width - full; | 50 const int empty = width - full; |
| 45 const int y = content.height() / 2 - kLineThickness / 2; | 51 const int y = content.height() / 2 - kLineThickness / 2; |
| 52 const int x = content.x() + full + kThumbRadius; | |
| 46 canvas->FillRect(gfx::Rect(content.x(), y, full, kLineThickness), | 53 canvas->FillRect(gfx::Rect(content.x(), y, full, kLineThickness), |
| 47 is_active_ ? kActiveColor : kDisabledColor); | 54 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.
| |
| 48 canvas->FillRect( | 55 canvas->FillRect(gfx::Rect(x + kThumbRadius, y, empty, kLineThickness), |
| 49 gfx::Rect(content.x() + full + thumb_size, y, empty, kLineThickness), | 56 kDisabledColor); |
| 50 kDisabledColor); | 57 |
| 58 gfx::Point thumb_center(x, content.height() / 2); | |
| 59 | |
| 60 // Paint the thumb highlight if it exists. | |
| 61 if (is_active_ && thumb_highlight_radius_ > kThumbRadius) { | |
| 62 SkPaint highlight; | |
| 63 highlight.setColor(kHighlightColor); | |
| 64 highlight.setFlags(SkPaint::kAntiAlias_Flag); | |
| 65 canvas->DrawCircle(thumb_center, thumb_highlight_radius_, highlight); | |
| 66 } | |
| 51 | 67 |
| 52 // Paint the thumb of the slider. | 68 // Paint the thumb of the slider. |
| 53 SkPaint paint; | 69 SkPaint paint; |
| 54 paint.setColor(is_active_ ? kActiveColor : kDisabledColor); | 70 paint.setColor(is_active_ ? kActiveColor : kDisabledColor); |
| 55 paint.setFlags(SkPaint::kAntiAlias_Flag); | 71 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 56 | 72 |
| 57 if (!is_active_) { | 73 if (!is_active_) { |
| 58 paint.setStrokeWidth(kThumbStroke); | 74 paint.setStrokeWidth(kThumbStroke); |
| 59 paint.setStyle(SkPaint::kStroke_Style); | 75 paint.setStyle(SkPaint::kStroke_Style); |
| 60 } | 76 } |
| 61 canvas->DrawCircle( | 77 canvas->DrawCircle( |
| 62 gfx::Point(content.x() + full + kThumbRadius, content.height() / 2), | 78 thumb_center, |
| 63 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint); | 79 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint); |
| 64 } | 80 } |
| 65 | 81 |
| 66 const char* MdSlider::GetClassName() const { | 82 const char* MdSlider::GetClassName() const { |
| 67 return "MdSlider"; | 83 return "MdSlider"; |
| 68 } | 84 } |
| 69 | 85 |
| 70 void MdSlider::UpdateState(bool control_on) { | 86 void MdSlider::UpdateState(bool control_on) { |
| 71 is_active_ = control_on; | 87 is_active_ = control_on; |
| 72 SchedulePaint(); | 88 SchedulePaint(); |
| 73 } | 89 } |
| 74 | 90 |
| 75 int MdSlider::GetThumbWidth() { | 91 int MdSlider::GetThumbWidth() { |
| 76 return kThumbRadius * 2; | 92 return kThumbRadius * 2; |
| 77 } | 93 } |
| 94 | |
| 95 void MdSlider::SetHighlighted(bool is_highlighted) { | |
| 96 if (!highlight_animation_) | |
| 97 highlight_animation_.reset(new gfx::SlideAnimation(this)); | |
| 98 if (is_highlighted) { | |
| 99 highlight_animation_->SetSlideDuration(kSlideHighlightChangeDurationMs); | |
| 100 highlight_animation_->Show(); | |
| 101 } else { | |
| 102 highlight_animation_->Hide(); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void MdSlider::AnimationProgressed(const gfx::Animation* animation) { | |
| 107 if (animation != highlight_animation_.get()) { | |
| 108 Slider::AnimationProgressed(animation); | |
| 109 return; | |
| 110 } | |
| 111 thumb_highlight_radius_ = | |
| 112 animation->CurrentValueBetween(kThumbRadius, kThumbHighlightRadius); | |
| 113 SchedulePaint(); | |
| 114 } | |
| 115 | |
| 116 void MdSlider::AnimationEnded(const gfx::Animation* animation) { | |
| 117 if (animation == highlight_animation_.get() && | |
| 118 !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.
| |
| 119 highlight_animation_.reset(); | |
| 120 } | |
| 121 | |
| 78 } // namespace views | 122 } // namespace views |
| OLD | NEW |