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 #include <iostream> |
bruthig
2016/09/22 21:05:24
nit: Remove
yiyix
2016/09/23 20:35:36
Done.
| |
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 |
15 namespace gfx { | |
16 class SlideAnimation; | |
bruthig
2016/09/22 21:05:24
The forward declaration should be in the .h.
yiyix
2016/09/23 20:35:37
sorry, i must have seen so many definition in the
| |
17 } | |
18 | |
14 namespace views { | 19 namespace views { |
15 | 20 |
16 // Color of slider at the active and the disabled state, respectively. | 21 // Color of slider at the active and the disabled state, respectively. |
17 const SkColor kActiveColor = SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4); | 22 const SkColor kActiveColor = SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4); |
18 const SkColor kDisabledColor = SkColorSetARGB(0x42, 0x00, 0x00, 0x00); | 23 const SkColor kDisabledColor = SkColorSetARGB(0x42, 0x00, 0x00, 0x00); |
24 const SkColor kHighlightColor = SkColorSetARGB(0x4D, 0x42, 0x85, 0xF4); | |
19 | 25 |
20 // The thickness of the slider. | 26 // The thickness of the slider. |
21 const int kLineThickness = 2; | 27 const int kLineThickness = 2; |
22 | 28 |
23 // The radius of the thumb of the slider. | 29 // The radius of the thumb and the highlighted thumb of the slider, |
24 const int kThumbRadius = 6; | 30 // respectively. |
31 const float kThumbRadius = 6.0; | |
bruthig
2016/09/22 21:05:24
This is currently assigning a double literal, i.e.
yiyix
2016/09/23 20:35:37
is there any difference between these two casting?
bruthig
2016/09/23 22:24:39
Nope, but I believe 6.f is the preferred way by ow
| |
32 const float kThumbHighlightRadius = 10.0; | |
25 | 33 |
26 // The stroke of the thumb when the slider is disabled. | 34 // The stroke of the thumb when the slider is disabled. |
27 const int kThumbStroke = 2; | 35 const int kThumbStroke = 2; |
28 | 36 |
37 // Duration of the thumb highlight growing effect animation. | |
38 const int kSlideValueChangeDurationMS = 150; | |
39 | |
29 MdSlider::MdSlider(SliderListener* listener) | 40 MdSlider::MdSlider(SliderListener* listener) |
30 : Slider(listener), is_active_(true) { | 41 : Slider(listener), is_active_(true), animating_thumb_value_(0.f) { |
31 SchedulePaint(); | 42 SchedulePaint(); |
32 } | 43 } |
33 | 44 |
34 MdSlider::~MdSlider() {} | 45 MdSlider::~MdSlider() {} |
35 | 46 |
36 void MdSlider::OnPaint(gfx::Canvas* canvas) { | 47 void MdSlider::OnPaint(gfx::Canvas* canvas) { |
37 Slider::OnPaint(canvas); | 48 Slider::OnPaint(canvas); |
38 | 49 |
39 // Paint the slider. | 50 // Paint the slider. |
40 const int thumb_size = kThumbRadius * 2; | |
41 const gfx::Rect content = GetContentsBounds(); | 51 const gfx::Rect content = GetContentsBounds(); |
42 const int width = content.width() - thumb_size; | 52 const int width = content.width() - kThumbRadius * 2; |
43 const int full = GetAnimatingValue() * width; | 53 const int full = GetAnimatingValue() * width; |
44 const int empty = width - full; | 54 const int empty = width - full; |
45 const int y = content.height() / 2 - kLineThickness / 2; | 55 const int y = content.height() / 2 - kLineThickness / 2; |
56 const int x = content.x() + full + kThumbRadius; | |
46 canvas->FillRect(gfx::Rect(content.x(), y, full, kLineThickness), | 57 canvas->FillRect(gfx::Rect(content.x(), y, full, kLineThickness), |
47 is_active_ ? kActiveColor : kDisabledColor); | 58 is_active_ ? kActiveColor : kDisabledColor); |
48 canvas->FillRect( | 59 canvas->FillRect(gfx::Rect(x + kThumbRadius, y, empty, kLineThickness), |
49 gfx::Rect(content.x() + full + thumb_size, y, empty, kLineThickness), | 60 kDisabledColor); |
50 kDisabledColor); | 61 |
62 gfx::Point center(x, content.height() / 2); | |
bruthig
2016/09/22 21:05:24
nit: |thumb_center| would be more descriptive than
yiyix
2016/09/23 20:35:36
Done.
| |
63 | |
64 // Paint the thumb highlight if it exists. | |
65 if (is_active_ && animating_thumb_value_ > kThumbRadius) { | |
66 SkPaint highlight; | |
67 highlight.setColor(kHighlightColor); | |
68 highlight.setFlags(SkPaint::kAntiAlias_Flag); | |
69 float highlight_radius = | |
70 highlight_animation_.get() && highlight_animation_->is_animating() | |
71 ? animating_thumb_value_ | |
72 : kThumbHighlightRadius; | |
73 canvas->DrawCircle(center, highlight_radius, highlight); | |
bruthig
2016/09/22 21:05:24
You should just paint using the |animating_thumb_v
yiyix
2016/09/23 20:35:36
Make sense, it always reflects the correct value n
| |
74 } | |
51 | 75 |
52 // Paint the thumb of the slider. | 76 // Paint the thumb of the slider. |
53 SkPaint paint; | 77 SkPaint paint; |
54 paint.setColor(is_active_ ? kActiveColor : kDisabledColor); | 78 paint.setColor(is_active_ ? kActiveColor : kDisabledColor); |
55 paint.setFlags(SkPaint::kAntiAlias_Flag); | 79 paint.setFlags(SkPaint::kAntiAlias_Flag); |
56 | 80 |
57 if (!is_active_) { | 81 if (!is_active_) { |
58 paint.setStrokeWidth(kThumbStroke); | 82 paint.setStrokeWidth(kThumbStroke); |
59 paint.setStyle(SkPaint::kStroke_Style); | 83 paint.setStyle(SkPaint::kStroke_Style); |
60 } | 84 } |
61 canvas->DrawCircle( | 85 canvas->DrawCircle( |
62 gfx::Point(content.x() + full + kThumbRadius, content.height() / 2), | 86 center, is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), |
63 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint); | 87 paint); |
64 } | 88 } |
65 | 89 |
66 const char* MdSlider::GetClassName() const { | 90 const char* MdSlider::GetClassName() const { |
67 return "MdSlider"; | 91 return "MdSlider"; |
68 } | 92 } |
69 | 93 |
70 void MdSlider::UpdateState(bool control_on) { | 94 void MdSlider::UpdateState(bool control_on) { |
71 is_active_ = control_on; | 95 is_active_ = control_on; |
72 SchedulePaint(); | 96 SchedulePaint(); |
73 } | 97 } |
74 | 98 |
75 int MdSlider::GetThumbWidth() { | 99 int MdSlider::GetThumbWidth() { |
76 return kThumbRadius * 2; | 100 return kThumbRadius * 2; |
77 } | 101 } |
102 | |
103 void MdSlider::SetHighlighted(bool focus) { | |
104 if (focus) { | |
105 highlight_animation_.reset(new gfx::SlideAnimation(this)); | |
bruthig
2016/09/22 21:05:24
By always creating a new |highlight_animation_| wh
yiyix
2016/09/23 20:35:36
Done.
| |
106 highlight_animation_->SetSlideDuration(kSlideValueChangeDurationMS); | |
107 highlight_animation_->Show(); | |
108 } else { | |
109 highlight_animation_->Hide(); | |
bruthig
2016/09/22 21:05:24
You should probably be more robust here and ensure
yiyix
2016/09/23 20:35:36
good catch!
| |
110 } | |
111 SchedulePaint(); | |
bruthig
2016/09/22 21:05:24
You shouldn't need to call SchedulePaint() here.
yiyix
2016/09/23 20:35:36
Done.
| |
112 } | |
113 | |
114 void MdSlider::AnimationProgressed(const gfx::Animation* animation) { | |
115 Slider::AnimationProgressed(animation); | |
116 if (animation == highlight_animation_.get()) { | |
117 float old_animating_thumb_value = animating_thumb_value_; | |
118 animating_thumb_value_ = | |
119 animation->CurrentValueBetween(kThumbRadius, kThumbHighlightRadius); | |
120 if (old_animating_thumb_value != animating_thumb_value_) | |
121 SchedulePaint(); | |
122 } | |
123 } | |
124 | |
78 } // namespace views | 125 } // namespace views |
OLD | NEW |