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 uint8_t kHighlightColorAlpha = 0x4D; |
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; |
| 53 const SkColor current_thumb_color = |
| 54 is_active_ ? kActiveColor : kDisabledColor; |
46 canvas->FillRect(gfx::Rect(content.x(), y, full, kLineThickness), | 55 canvas->FillRect(gfx::Rect(content.x(), y, full, kLineThickness), |
47 is_active_ ? kActiveColor : kDisabledColor); | 56 current_thumb_color); |
48 canvas->FillRect( | 57 canvas->FillRect(gfx::Rect(x + kThumbRadius, y, empty, kLineThickness), |
49 gfx::Rect(content.x() + full + thumb_size, y, empty, kLineThickness), | 58 kDisabledColor); |
50 kDisabledColor); | 59 |
| 60 gfx::Point thumb_center(x, content.height() / 2); |
| 61 |
| 62 // Paint the thumb highlight if it exists. |
| 63 if (is_active_ && thumb_highlight_radius_ > kThumbRadius) { |
| 64 SkPaint highlight; |
| 65 SkColor kHighlightColor = SkColorSetA(kActiveColor, kHighlightColorAlpha); |
| 66 highlight.setColor(kHighlightColor); |
| 67 highlight.setFlags(SkPaint::kAntiAlias_Flag); |
| 68 canvas->DrawCircle(thumb_center, thumb_highlight_radius_, highlight); |
| 69 } |
51 | 70 |
52 // Paint the thumb of the slider. | 71 // Paint the thumb of the slider. |
53 SkPaint paint; | 72 SkPaint paint; |
54 paint.setColor(is_active_ ? kActiveColor : kDisabledColor); | 73 paint.setColor(current_thumb_color); |
55 paint.setFlags(SkPaint::kAntiAlias_Flag); | 74 paint.setFlags(SkPaint::kAntiAlias_Flag); |
56 | 75 |
57 if (!is_active_) { | 76 if (!is_active_) { |
58 paint.setStrokeWidth(kThumbStroke); | 77 paint.setStrokeWidth(kThumbStroke); |
59 paint.setStyle(SkPaint::kStroke_Style); | 78 paint.setStyle(SkPaint::kStroke_Style); |
60 } | 79 } |
61 canvas->DrawCircle( | 80 canvas->DrawCircle( |
62 gfx::Point(content.x() + full + kThumbRadius, content.height() / 2), | 81 thumb_center, |
63 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint); | 82 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint); |
64 } | 83 } |
65 | 84 |
66 const char* MdSlider::GetClassName() const { | 85 const char* MdSlider::GetClassName() const { |
67 return "MdSlider"; | 86 return "MdSlider"; |
68 } | 87 } |
69 | 88 |
70 void MdSlider::UpdateState(bool control_on) { | 89 void MdSlider::UpdateState(bool control_on) { |
71 is_active_ = control_on; | 90 is_active_ = control_on; |
72 SchedulePaint(); | 91 SchedulePaint(); |
73 } | 92 } |
74 | 93 |
75 int MdSlider::GetThumbWidth() { | 94 int MdSlider::GetThumbWidth() { |
76 return kThumbRadius * 2; | 95 return kThumbRadius * 2; |
77 } | 96 } |
| 97 |
| 98 void MdSlider::SetHighlighted(bool is_highlighted) { |
| 99 if (!highlight_animation_) { |
| 100 if (!is_highlighted) |
| 101 return; |
| 102 |
| 103 highlight_animation_.reset(new gfx::SlideAnimation(this)); |
| 104 highlight_animation_->SetSlideDuration(kSlideHighlightChangeDurationMs); |
| 105 } |
| 106 if (is_highlighted) |
| 107 highlight_animation_->Show(); |
| 108 else |
| 109 highlight_animation_->Hide(); |
| 110 } |
| 111 |
| 112 void MdSlider::AnimationProgressed(const gfx::Animation* animation) { |
| 113 if (animation != highlight_animation_.get()) { |
| 114 Slider::AnimationProgressed(animation); |
| 115 return; |
| 116 } |
| 117 thumb_highlight_radius_ = |
| 118 animation->CurrentValueBetween(kThumbRadius, kThumbHighlightRadius); |
| 119 SchedulePaint(); |
| 120 } |
| 121 |
| 122 void MdSlider::AnimationEnded(const gfx::Animation* animation) { |
| 123 if (animation != highlight_animation_.get()) { |
| 124 Slider::AnimationEnded(animation); |
| 125 return; |
| 126 } |
| 127 if (animation == highlight_animation_.get() && |
| 128 !highlight_animation_->IsShowing()) { |
| 129 highlight_animation_.reset(); |
| 130 } |
| 131 } |
| 132 |
78 } // namespace views | 133 } // namespace views |
OLD | NEW |