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); | |
| 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 of the slider. |
| 24 const int kThumbRadius = 6; | 26 const float kThumbRadius = 6.0; |
| 25 | 27 |
| 26 // The stroke of the thumb when the slider is disabled. | 28 // The stroke of the thumb when the slider is disabled. |
| 27 const int kThumbStroke = 2; | 29 const int kThumbStroke = 2; |
| 28 | 30 |
| 31 // The radius and stroke of the thumb's ripple. | |
| 32 const float kThumbHighlightStroke = 4.0; | |
| 33 | |
| 34 // Duration of the animation. | |
|
bruthig
2016/09/21 00:27:45
Be more specific here. i.e. What 'animation'?
yiyix
2016/09/21 15:20:52
Sorry, I was playing with slide animation and line
| |
| 35 const int kSlideValueChangeDurationMS = 150; | |
| 36 | |
| 29 MdSlider::MdSlider(SliderListener* listener) | 37 MdSlider::MdSlider(SliderListener* listener) |
| 30 : Slider(listener), is_active_(true) { | 38 : Slider(listener), is_active_(true), animating_thumb_value_(0.f) { |
| 31 SchedulePaint(); | 39 SchedulePaint(); |
| 32 } | 40 } |
| 33 | 41 |
| 34 MdSlider::~MdSlider() {} | 42 MdSlider::~MdSlider() {} |
| 35 | 43 |
| 36 void MdSlider::OnPaint(gfx::Canvas* canvas) { | 44 void MdSlider::OnPaint(gfx::Canvas* canvas) { |
| 37 Slider::OnPaint(canvas); | 45 Slider::OnPaint(canvas); |
| 38 | 46 |
| 39 // Paint the slider. | 47 // Paint the slider. |
| 40 const int thumb_size = kThumbRadius * 2; | 48 const int thumb_size = kThumbRadius * 2; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 54 paint.setColor(is_active_ ? kActiveColor : kDisabledColor); | 62 paint.setColor(is_active_ ? kActiveColor : kDisabledColor); |
| 55 paint.setFlags(SkPaint::kAntiAlias_Flag); | 63 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 56 | 64 |
| 57 if (!is_active_) { | 65 if (!is_active_) { |
| 58 paint.setStrokeWidth(kThumbStroke); | 66 paint.setStrokeWidth(kThumbStroke); |
| 59 paint.setStyle(SkPaint::kStroke_Style); | 67 paint.setStyle(SkPaint::kStroke_Style); |
| 60 } | 68 } |
| 61 canvas->DrawCircle( | 69 canvas->DrawCircle( |
| 62 gfx::Point(content.x() + full + kThumbRadius, content.height() / 2), | 70 gfx::Point(content.x() + full + kThumbRadius, content.height() / 2), |
| 63 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint); | 71 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint); |
| 72 | |
| 73 if (is_active_ && value_ > 0) { | |
|
bruthig
2016/09/21 00:27:46
Is it an intentional decision to paint the highlig
yiyix
2016/09/21 15:20:52
The highlight and the thumb are not over each othe
| |
| 74 SkPaint highlight; | |
| 75 highlight.setColor(kHighlightColor); | |
| 76 highlight.setStyle(SkPaint::kStroke_Style); | |
| 77 highlight.setFlags(SkPaint::kAntiAlias_Flag); | |
| 78 float val = | |
|
bruthig
2016/09/21 00:27:45
Using |highlight_radius| variable name would be mo
yiyix
2016/09/21 15:20:52
Done.
| |
| 79 highlight_animation_.get() && highlight_animation_->is_animating() | |
|
bruthig
2016/09/21 00:27:45
It would be less confusing if the painting of the
yiyix
2016/09/21 15:20:52
I don't really get it, the highlight is dependent
| |
| 80 ? animating_thumb_value_ | |
| 81 : kThumbHighlightStroke; | |
| 82 | |
| 83 highlight.setStrokeWidth(val); | |
| 84 canvas->DrawCircle( | |
| 85 gfx::Point(content.x() + full + kThumbRadius, content.height() / 2), | |
|
bruthig
2016/09/21 00:27:45
Consider computing the thumb center point once and
yiyix
2016/09/21 15:20:52
good point.
| |
| 86 (kThumbRadius + val / 2), highlight); | |
| 87 } | |
| 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::SetFocus(bool focus) { | |
| 104 if (focus) { | |
| 105 value_ = kThumbHighlightStroke; | |
| 106 animating_thumb_value_ = 0; | |
| 107 highlight_animation_.reset(new gfx::SlideAnimation(this)); | |
| 108 highlight_animation_->SetSlideDuration(kSlideValueChangeDurationMS); | |
| 109 highlight_animation_->Show(); | |
| 110 } else { | |
| 111 value_ = 0; | |
|
bruthig
2016/09/21 00:27:45
I think it would be more visually pleasing if the
yiyix
2016/09/21 15:20:52
The animation was not really specified in the desi
| |
| 112 } | |
| 113 SchedulePaint(); | |
| 114 } | |
| 115 | |
| 116 void MdSlider::AnimationProgressed(const gfx::Animation* animation) { | |
| 117 Slider::AnimationProgressed(animation); | |
| 118 animating_thumb_value_ = | |
|
bruthig
2016/09/21 00:27:45
This should only update |animating_thumb_value_| i
yiyix
2016/09/21 15:20:52
Done.
| |
| 119 animation->CurrentValueBetween(animating_thumb_value_, value_); | |
| 120 } | |
| 121 | |
| 78 } // namespace views | 122 } // namespace views |
| OLD | NEW |