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/canvas.h" | 9 #include "ui/gfx/canvas.h" |
10 #include "ui/gfx/geometry/point.h" | 10 #include "ui/gfx/geometry/point.h" |
11 #include "ui/gfx/geometry/rect.h" | 11 #include "ui/gfx/geometry/rect.h" |
12 #include "ui/views/controls/slider.h" | 12 #include "ui/views/controls/slider.h" |
13 | 13 |
14 namespace views { | 14 namespace views { |
15 | 15 |
16 // Color of slider at the active and the disabled state, respectively. | 16 // Color of slider at the active and the disabled state, respectively. |
17 const SkColor kActiveColor = SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4); | 17 const SkColor kActiveColor = SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4); |
18 const SkColor kDisabledColor = SkColorSetARGB(0x42, 0x00, 0x00, 0x00); | 18 const SkColor kDisabledColor = SkColorSetARGB(0x42, 0x00, 0x00, 0x00); |
19 const SkColor kRippleColor = SkColorSetARGB(0x4D, 0x42, 0x85, 0xF4); | |
bruthig
2016/09/12 17:16:48
I'd recommend using a more generic, and hopefully
yiyix
2016/09/20 20:25:46
Done.
| |
19 | 20 |
20 // The thickness of the slider. | 21 // The thickness of the slider. |
21 const int kLineThickness = 2; | 22 const int kLineThickness = 2; |
22 | 23 |
23 // The radius of the thumb of the slider. | 24 // The radius of the thumb of the slider. |
24 const int kThumbRadius = 6; | 25 const int kThumbRadius = 6; |
25 | 26 |
26 // The stroke of the thumb when the slider is disabled. | 27 // The stroke of the thumb when the slider is disabled. |
27 const int kThumbStroke = 2; | 28 const int kThumbStroke = 2; |
28 | 29 |
30 // The radius and stroke of the thumb's ripple. | |
31 const int kThumbRippleRadius = 10; | |
32 const int kThumbRippleStroke = 4; | |
33 | |
29 MdSlider::MdSlider(SliderListener* listener) | 34 MdSlider::MdSlider(SliderListener* listener) |
30 : Slider(listener), is_active_(true) { | 35 : Slider(listener), is_active_(true), is_pressed_(false) { |
31 SchedulePaint(); | 36 SchedulePaint(); |
32 } | 37 } |
33 | 38 |
34 MdSlider::~MdSlider() {} | 39 MdSlider::~MdSlider() {} |
35 | 40 |
41 bool MdSlider::OnMousePressed(const ui::MouseEvent& event) { | |
42 is_pressed_ = true; | |
bruthig
2016/09/12 17:16:48
If you replace |is_pressed_| with an int |thumb_ri
yiyix
2016/09/20 20:25:46
Thanks, I think so..
| |
43 SchedulePaint(); | |
44 return Slider::OnMousePressed(event); | |
45 } | |
46 | |
47 void MdSlider::OnMouseReleased(const ui::MouseEvent& event) { | |
48 is_pressed_ = false; | |
49 Slider::OnMouseReleased(event); | |
50 SchedulePaint(); | |
51 } | |
52 | |
36 void MdSlider::OnPaint(gfx::Canvas* canvas) { | 53 void MdSlider::OnPaint(gfx::Canvas* canvas) { |
37 Slider::OnPaint(canvas); | 54 Slider::OnPaint(canvas); |
38 | 55 |
39 // Paint the slider. | 56 // Paint the slider. |
40 const int thumb_size = kThumbRadius * 2; | 57 const int thumb_size = kThumbRadius * 2; |
41 const gfx::Rect content = GetContentsBounds(); | 58 const gfx::Rect content = GetContentsBounds(); |
42 const int width = content.width() - thumb_size; | 59 const int width = content.width() - thumb_size; |
43 const int full = GetAnimatingValue() * width; | 60 const int full = GetAnimatingValue() * width; |
44 const int empty = width - full; | 61 const int empty = width - full; |
45 const int y = content.height() / 2 - kLineThickness / 2; | 62 const int y = content.height() / 2 - kLineThickness / 2; |
46 canvas->FillRect(gfx::Rect(content.x(), y, full, kLineThickness), | 63 canvas->FillRect(gfx::Rect(content.x(), y, full, kLineThickness), |
47 is_active_ ? kActiveColor : kDisabledColor); | 64 is_active_ ? kActiveColor : kDisabledColor); |
48 canvas->FillRect( | 65 canvas->FillRect( |
49 gfx::Rect(content.x() + full + thumb_size, y, empty, kLineThickness), | 66 gfx::Rect(content.x() + full + thumb_size, y, empty, kLineThickness), |
50 kDisabledColor); | 67 kDisabledColor); |
51 | 68 |
52 // Paint the thumb of the slider. | 69 // Paint the thumb of the slider. |
53 SkPaint paint; | 70 SkPaint paint; |
54 paint.setColor(is_active_ ? kActiveColor : kDisabledColor); | 71 paint.setColor(is_active_ ? kActiveColor : kDisabledColor); |
55 paint.setFlags(SkPaint::kAntiAlias_Flag); | 72 paint.setFlags(SkPaint::kAntiAlias_Flag); |
56 | 73 |
57 if (!is_active_) { | 74 if (!is_active_) { |
58 paint.setStrokeWidth(kThumbStroke); | 75 paint.setStrokeWidth(kThumbStroke); |
59 paint.setStyle(SkPaint::kStroke_Style); | 76 paint.setStyle(SkPaint::kStroke_Style); |
60 } | 77 } |
61 canvas->DrawCircle( | 78 canvas->DrawCircle( |
62 gfx::Point(content.x() + full + kThumbRadius, content.height() / 2), | 79 gfx::Point(content.x() + full + kThumbRadius, content.height() / 2), |
63 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint); | 80 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), paint); |
81 | |
82 if (is_pressed_ && is_active_) { | |
83 SkPaint ripple; | |
84 ripple.setColor(kRippleColor); | |
85 ripple.setStrokeWidth(kThumbRippleStroke); | |
86 ripple.setStyle(SkPaint::kStroke_Style); | |
87 canvas->DrawCircle( | |
88 gfx::Point(content.x() + full + kThumbRadius, content.height() / 2), | |
89 (kThumbRippleRadius - kThumbRippleStroke / 2), ripple); | |
90 } | |
64 } | 91 } |
65 | 92 |
66 const char* MdSlider::GetClassName() const { | 93 const char* MdSlider::GetClassName() const { |
67 return "MdSlider"; | 94 return "MdSlider"; |
68 } | 95 } |
69 | 96 |
70 void MdSlider::UpdateState(bool control_on) { | 97 void MdSlider::UpdateState(bool control_on) { |
71 is_active_ = control_on; | 98 is_active_ = control_on; |
72 SchedulePaint(); | 99 SchedulePaint(); |
73 } | 100 } |
74 | 101 |
75 int MdSlider::GetThumbWidth() { | 102 int MdSlider::GetThumbWidth() { |
76 return kThumbRadius * 2; | 103 return kThumbRadius * 2; |
77 } | 104 } |
105 | |
78 } // namespace views | 106 } // namespace views |
OLD | NEW |