| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/views/controls/md_slider.h" | |
| 6 | |
| 7 #include "cc/paint/paint_flags.h" | |
| 8 #include "third_party/skia/include/core/SkColor.h" | |
| 9 #include "third_party/skia/include/core/SkPaint.h" | |
| 10 #include "ui/gfx/animation/slide_animation.h" | |
| 11 #include "ui/gfx/canvas.h" | |
| 12 #include "ui/gfx/geometry/point.h" | |
| 13 #include "ui/gfx/geometry/rect.h" | |
| 14 #include "ui/views/controls/slider.h" | |
| 15 | |
| 16 namespace views { | |
| 17 | |
| 18 // Color of slider at the active and the disabled state, respectively. | |
| 19 const SkColor kActiveColor = SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4); | |
| 20 const SkColor kDisabledColor = SkColorSetARGB(0xFF, 0xBD, 0xBD, 0xBD); | |
| 21 const uint8_t kHighlightColorAlpha = 0x4D; | |
| 22 | |
| 23 // The thickness of the slider. | |
| 24 const int kLineThickness = 2; | |
| 25 | |
| 26 // The radius used to draw rounded slider ends. | |
| 27 const float kSliderRoundedRadius = 2.f; | |
| 28 | |
| 29 // The radius of the thumb and the highlighted thumb of the slider, | |
| 30 // respectively. | |
| 31 const float kThumbRadius = 6.f; | |
| 32 const float kThumbHighlightRadius = 10.f; | |
| 33 | |
| 34 // The stroke of the thumb when the slider is disabled. | |
| 35 const int kThumbStroke = 2; | |
| 36 | |
| 37 // Duration of the thumb highlight growing effect animation. | |
| 38 const int kSlideHighlightChangeDurationMs = 150; | |
| 39 | |
| 40 MdSlider::MdSlider(SliderListener* listener) | |
| 41 : Slider(listener), is_active_(true), thumb_highlight_radius_(0.f) { | |
| 42 SchedulePaint(); | |
| 43 } | |
| 44 | |
| 45 MdSlider::~MdSlider() {} | |
| 46 | |
| 47 void MdSlider::OnPaint(gfx::Canvas* canvas) { | |
| 48 Slider::OnPaint(canvas); | |
| 49 | |
| 50 // Paint the slider. | |
| 51 const gfx::Rect content = GetContentsBounds(); | |
| 52 const int width = content.width() - kThumbRadius * 2; | |
| 53 const int full = GetAnimatingValue() * width; | |
| 54 const int empty = width - full; | |
| 55 const int y = content.height() / 2 - kLineThickness / 2; | |
| 56 const int x = content.x() + full + kThumbRadius; | |
| 57 const SkColor current_thumb_color = | |
| 58 is_active_ ? kActiveColor : kDisabledColor; | |
| 59 | |
| 60 // Extra space used to hide slider ends behind the thumb. | |
| 61 const int extra_padding = 1; | |
| 62 | |
| 63 cc::PaintFlags slider_flags; | |
| 64 slider_flags.setAntiAlias(true); | |
| 65 slider_flags.setColor(current_thumb_color); | |
| 66 canvas->DrawRoundRect( | |
| 67 gfx::Rect(content.x(), y, full + extra_padding, kLineThickness), | |
| 68 kSliderRoundedRadius, slider_flags); | |
| 69 slider_flags.setColor(kDisabledColor); | |
| 70 canvas->DrawRoundRect(gfx::Rect(x + kThumbRadius - extra_padding, y, | |
| 71 empty + extra_padding, kLineThickness), | |
| 72 kSliderRoundedRadius, slider_flags); | |
| 73 | |
| 74 gfx::Point thumb_center(x, content.height() / 2); | |
| 75 | |
| 76 // Paint the thumb highlight if it exists. | |
| 77 const int thumb_highlight_radius = | |
| 78 HasFocus() ? kThumbHighlightRadius : thumb_highlight_radius_; | |
| 79 if (is_active_ && thumb_highlight_radius > kThumbRadius) { | |
| 80 cc::PaintFlags highlight; | |
| 81 SkColor kHighlightColor = SkColorSetA(kActiveColor, kHighlightColorAlpha); | |
| 82 highlight.setColor(kHighlightColor); | |
| 83 highlight.setFlags(cc::PaintFlags::kAntiAlias_Flag); | |
| 84 canvas->DrawCircle(thumb_center, thumb_highlight_radius, highlight); | |
| 85 } | |
| 86 | |
| 87 // Paint the thumb of the slider. | |
| 88 cc::PaintFlags flags; | |
| 89 flags.setColor(current_thumb_color); | |
| 90 flags.setFlags(cc::PaintFlags::kAntiAlias_Flag); | |
| 91 | |
| 92 if (!is_active_) { | |
| 93 flags.setStrokeWidth(kThumbStroke); | |
| 94 flags.setStyle(cc::PaintFlags::kStroke_Style); | |
| 95 } | |
| 96 canvas->DrawCircle( | |
| 97 thumb_center, | |
| 98 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), flags); | |
| 99 } | |
| 100 | |
| 101 const char* MdSlider::GetClassName() const { | |
| 102 return "MdSlider"; | |
| 103 } | |
| 104 | |
| 105 void MdSlider::UpdateState(bool control_on) { | |
| 106 is_active_ = control_on; | |
| 107 SchedulePaint(); | |
| 108 } | |
| 109 | |
| 110 int MdSlider::GetThumbWidth() { | |
| 111 return kThumbRadius * 2; | |
| 112 } | |
| 113 | |
| 114 void MdSlider::SetHighlighted(bool is_highlighted) { | |
| 115 if (!highlight_animation_) { | |
| 116 if (!is_highlighted) | |
| 117 return; | |
| 118 | |
| 119 highlight_animation_.reset(new gfx::SlideAnimation(this)); | |
| 120 highlight_animation_->SetSlideDuration(kSlideHighlightChangeDurationMs); | |
| 121 } | |
| 122 if (is_highlighted) | |
| 123 highlight_animation_->Show(); | |
| 124 else | |
| 125 highlight_animation_->Hide(); | |
| 126 } | |
| 127 | |
| 128 void MdSlider::AnimationProgressed(const gfx::Animation* animation) { | |
| 129 if (animation != highlight_animation_.get()) { | |
| 130 Slider::AnimationProgressed(animation); | |
| 131 return; | |
| 132 } | |
| 133 thumb_highlight_radius_ = | |
| 134 animation->CurrentValueBetween(kThumbRadius, kThumbHighlightRadius); | |
| 135 SchedulePaint(); | |
| 136 } | |
| 137 | |
| 138 void MdSlider::AnimationEnded(const gfx::Animation* animation) { | |
| 139 if (animation != highlight_animation_.get()) { | |
| 140 Slider::AnimationEnded(animation); | |
| 141 return; | |
| 142 } | |
| 143 if (animation == highlight_animation_.get() && | |
| 144 !highlight_animation_->IsShowing()) { | |
| 145 highlight_animation_.reset(); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 } // namespace views | |
| OLD | NEW |