| 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/button/toggle_button.h" | 5 #include "ui/views/controls/button/toggle_button.h" |
| 6 | 6 |
| 7 #include "third_party/skia/include/core/SkDrawLooper.h" | 7 #include "third_party/skia/include/core/SkDrawLooper.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/color_palette.h" | 10 #include "ui/gfx/color_palette.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 void ToggleButton::OnPaint(gfx::Canvas* canvas) { | 69 void ToggleButton::OnPaint(gfx::Canvas* canvas) { |
| 70 SkAlpha blend = | 70 SkAlpha blend = |
| 71 static_cast<SkAlpha>(SK_AlphaOPAQUE * slide_animation_.GetCurrentValue()); | 71 static_cast<SkAlpha>(SK_AlphaOPAQUE * slide_animation_.GetCurrentValue()); |
| 72 | 72 |
| 73 // Track. | 73 // Track. |
| 74 gfx::RectF track_rect(GetContentsBounds()); | 74 gfx::RectF track_rect(GetContentsBounds()); |
| 75 SkPaint track_paint; | 75 SkPaint track_paint; |
| 76 track_paint.setAntiAlias(true); | 76 track_paint.setAntiAlias(true); |
| 77 const SkColor track_on_color = | 77 const SkColor track_on_color = |
| 78 SkColorSetA(GetNativeTheme()->GetSystemColor( | 78 SkColorSetA(GetNativeTheme()->GetSystemColor( |
| 79 ui::NativeTheme::kColorId_CallToActionColor), | 79 ui::NativeTheme::kColorId_ProminentButtonColor), |
| 80 0xFF / 2); | 80 0xFF / 2); |
| 81 track_paint.setColor( | 81 track_paint.setColor( |
| 82 color_utils::AlphaBlend(track_on_color, kTrackOffColor, blend)); | 82 color_utils::AlphaBlend(track_on_color, kTrackOffColor, blend)); |
| 83 canvas->DrawRoundRect(track_rect, track_rect.height() / 2, track_paint); | 83 canvas->DrawRoundRect(track_rect, track_rect.height() / 2, track_paint); |
| 84 | 84 |
| 85 // Thumb. | 85 // Thumb. |
| 86 gfx::Rect thumb_bounds = GetThumbBounds(); | 86 gfx::Rect thumb_bounds = GetThumbBounds(); |
| 87 SkPaint thumb_paint; | 87 SkPaint thumb_paint; |
| 88 std::vector<gfx::ShadowValue> shadows; | 88 std::vector<gfx::ShadowValue> shadows; |
| 89 shadows.emplace_back(gfx::Vector2d(0, 1), 4.f, | 89 shadows.emplace_back(gfx::Vector2d(0, 1), 4.f, |
| 90 SkColorSetA(SK_ColorBLACK, 0x99)); | 90 SkColorSetA(SK_ColorBLACK, 0x99)); |
| 91 thumb_paint.setLooper(gfx::CreateShadowDrawLooperCorrectBlur(shadows)); | 91 thumb_paint.setLooper(gfx::CreateShadowDrawLooperCorrectBlur(shadows)); |
| 92 thumb_paint.setStyle(SkPaint::kFill_Style); | 92 thumb_paint.setStyle(SkPaint::kFill_Style); |
| 93 thumb_paint.setAntiAlias(true); | 93 thumb_paint.setAntiAlias(true); |
| 94 const SkColor thumb_on_color = GetNativeTheme()->GetSystemColor( | 94 const SkColor thumb_on_color = GetNativeTheme()->GetSystemColor( |
| 95 ui::NativeTheme::kColorId_CallToActionColor); | 95 ui::NativeTheme::kColorId_ProminentButtonColor); |
| 96 // TODO(estade): get this color from the theme? | 96 // TODO(estade): get this color from the theme? |
| 97 const SkColor thumb_off_color = SK_ColorWHITE; | 97 const SkColor thumb_off_color = SK_ColorWHITE; |
| 98 thumb_paint.setColor( | 98 thumb_paint.setColor( |
| 99 color_utils::AlphaBlend(thumb_on_color, thumb_off_color, blend)); | 99 color_utils::AlphaBlend(thumb_on_color, thumb_off_color, blend)); |
| 100 canvas->DrawCircle(gfx::RectF(thumb_bounds).CenterPoint(), | 100 canvas->DrawCircle(gfx::RectF(thumb_bounds).CenterPoint(), |
| 101 thumb_bounds.height() / 2.f, thumb_paint); | 101 thumb_bounds.height() / 2.f, thumb_paint); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void ToggleButton::NotifyClick(const ui::Event& event) { | 104 void ToggleButton::NotifyClick(const ui::Event& event) { |
| 105 SetIsOn(!is_on(), true); | 105 SetIsOn(!is_on(), true); |
| 106 CustomButton::NotifyClick(event); | 106 CustomButton::NotifyClick(event); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void ToggleButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 109 void ToggleButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 110 SchedulePaint(); | 110 SchedulePaint(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 std::unique_ptr<InkDropRipple> ToggleButton::CreateInkDropRipple() const { | 113 std::unique_ptr<InkDropRipple> ToggleButton::CreateInkDropRipple() const { |
| 114 return CreateDefaultInkDropRipple(GetThumbBounds().CenterPoint()); | 114 return CreateDefaultInkDropRipple(GetThumbBounds().CenterPoint()); |
| 115 } | 115 } |
| 116 | 116 |
| 117 SkColor ToggleButton::GetInkDropBaseColor() const { | 117 SkColor ToggleButton::GetInkDropBaseColor() const { |
| 118 return is_on() | 118 return is_on() |
| 119 ? GetNativeTheme()->GetSystemColor( | 119 ? GetNativeTheme()->GetSystemColor( |
| 120 ui::NativeTheme::kColorId_CallToActionColor) | 120 ui::NativeTheme::kColorId_ProminentButtonColor) |
| 121 : kTrackOffColor; | 121 : kTrackOffColor; |
| 122 } | 122 } |
| 123 | 123 |
| 124 bool ToggleButton::ShouldShowInkDropHighlight() const { | 124 bool ToggleButton::ShouldShowInkDropHighlight() const { |
| 125 return false; | 125 return false; |
| 126 } | 126 } |
| 127 | 127 |
| 128 void ToggleButton::AnimationProgressed(const gfx::Animation* animation) { | 128 void ToggleButton::AnimationProgressed(const gfx::Animation* animation) { |
| 129 if (animation == &slide_animation_) | 129 if (animation == &slide_animation_) |
| 130 SchedulePaint(); | 130 SchedulePaint(); |
| 131 else | 131 else |
| 132 CustomButton::AnimationProgressed(animation); | 132 CustomButton::AnimationProgressed(animation); |
| 133 } | 133 } |
| 134 | 134 |
| 135 gfx::Rect ToggleButton::GetThumbBounds() const { | 135 gfx::Rect ToggleButton::GetThumbBounds() const { |
| 136 gfx::Rect thumb_bounds = GetLocalBounds(); | 136 gfx::Rect thumb_bounds = GetLocalBounds(); |
| 137 thumb_bounds.Inset(gfx::Insets(kThumbVerticalMargin, kThumbHorizontalMargin)); | 137 thumb_bounds.Inset(gfx::Insets(kThumbVerticalMargin, kThumbHorizontalMargin)); |
| 138 thumb_bounds.set_x(thumb_bounds.x() + | 138 thumb_bounds.set_x(thumb_bounds.x() + |
| 139 slide_animation_.GetCurrentValue() * | 139 slide_animation_.GetCurrentValue() * |
| 140 (thumb_bounds.width() - thumb_bounds.height())); | 140 (thumb_bounds.width() - thumb_bounds.height())); |
| 141 // The thumb is a circle, so the width should match the height. | 141 // The thumb is a circle, so the width should match the height. |
| 142 thumb_bounds.set_width(thumb_bounds.height()); | 142 thumb_bounds.set_width(thumb_bounds.height()); |
| 143 thumb_bounds.set_x(GetMirroredXForRect(thumb_bounds)); | 143 thumb_bounds.set_x(GetMirroredXForRect(thumb_bounds)); |
| 144 return thumb_bounds; | 144 return thumb_bounds; |
| 145 } | 145 } |
| 146 | 146 |
| 147 } // namespace views | 147 } // namespace views |
| OLD | NEW |