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/button/toggle_button.h" | 5 #include "ui/views/controls/button/toggle_button.h" |
| 6 | 6 |
| 7 #include "third_party/skia/include/core/SkDrawLooper.h" | |
| 8 #include "third_party/skia/include/core/SkPaint.h" | |
| 7 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
| 8 #include "ui/gfx/color_utils.h" | 10 #include "ui/gfx/color_utils.h" |
| 9 #include "ui/views/border.h" | 11 #include "ui/views/border.h" |
| 10 | 12 |
| 11 namespace views { | 13 namespace views { |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 // Constants are measured in dip. | 17 // Constants are measured in dip. |
| 16 const int kTrackHeight = 14; | 18 const int kTrackHeight = 14; |
| 17 const int kTrackWidth = 36; | 19 const int kTrackWidth = 36; |
| 18 // Margins from edge of track to edge of view. | 20 // Margins from edge of track to edge of view. |
| 19 const int kTrackVerticalMargin = 4; | 21 const int kTrackVerticalMargin = 6; |
| 20 const int kTrackHorizontalMargin = 1; | 22 const int kTrackHorizontalMargin = 2; |
| 21 // Margin from edge of thumb to closest three edges of view. | 23 // Margin from edge of thumb to closest edge of view. Note that the thumb |
| 22 const int kThumbMargin = 1; | 24 // margins must be sufficiently large to allow space for the shadow. |
| 25 const int kThumbHorizontalMargin = 2; | |
| 26 // Margin from top/bottom edge of thumb to top/bottom edge of view. | |
| 27 const int kThumbVerticalMargin = 3; | |
| 23 | 28 |
| 24 } // namespace | 29 } // namespace |
| 25 | 30 |
| 26 ToggleButton::ToggleButton(ButtonListener* listener) | 31 ToggleButton::ToggleButton(ButtonListener* listener) |
| 27 : CustomButton(listener), is_on_(false), slide_animation_(this) { | 32 : CustomButton(listener), is_on_(false), slide_animation_(this) { |
| 28 slide_animation_.SetSlideDuration(80 /* ms */); | 33 slide_animation_.SetSlideDuration(80 /* ms */); |
| 29 slide_animation_.SetTweenType(gfx::Tween::LINEAR); | 34 slide_animation_.SetTweenType(gfx::Tween::LINEAR); |
| 30 SetBorder(Border::CreateEmptyBorder( | 35 SetBorder(Border::CreateEmptyBorder( |
| 31 gfx::Insets(kTrackVerticalMargin, kTrackHorizontalMargin))); | 36 gfx::Insets(kTrackVerticalMargin, kTrackHorizontalMargin))); |
| 32 } | 37 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 50 gfx::Rect rect(0, 0, kTrackWidth, kTrackHeight); | 55 gfx::Rect rect(0, 0, kTrackWidth, kTrackHeight); |
| 51 if (border()) | 56 if (border()) |
| 52 rect.Inset(-border()->GetInsets()); | 57 rect.Inset(-border()->GetInsets()); |
| 53 return rect.size(); | 58 return rect.size(); |
| 54 } | 59 } |
| 55 | 60 |
| 56 void ToggleButton::OnPaint(gfx::Canvas* canvas) { | 61 void ToggleButton::OnPaint(gfx::Canvas* canvas) { |
| 57 SkAlpha blend = | 62 SkAlpha blend = |
| 58 static_cast<SkAlpha>(SK_AlphaOPAQUE * slide_animation_.GetCurrentValue()); | 63 static_cast<SkAlpha>(SK_AlphaOPAQUE * slide_animation_.GetCurrentValue()); |
| 59 | 64 |
| 65 // Track. | |
| 60 gfx::RectF track_rect(GetContentsBounds()); | 66 gfx::RectF track_rect(GetContentsBounds()); |
| 61 SkPaint paint; | 67 SkPaint track_paint; |
| 62 paint.setAntiAlias(true); | 68 track_paint.setAntiAlias(true); |
| 63 | |
| 64 // Track. | |
| 65 const SkColor track_on_color = | 69 const SkColor track_on_color = |
| 66 SkColorSetA(GetNativeTheme()->GetSystemColor( | 70 SkColorSetA(GetNativeTheme()->GetSystemColor( |
| 67 ui::NativeTheme::kColorId_CallToActionColor), | 71 ui::NativeTheme::kColorId_CallToActionColor), |
| 68 0xFF / 2); | 72 0xFF / 2); |
| 69 // TODO(estade): get this color from the theme. | 73 // TODO(estade): get this color from the theme. |
| 70 const SkColor track_off_color = SkColorSetA(SK_ColorBLACK, 0x61); | 74 const SkColor track_off_color = SkColorSetA(SK_ColorBLACK, 0x61); |
| 71 paint.setColor( | 75 track_paint.setColor( |
| 72 color_utils::AlphaBlend(track_on_color, track_off_color, blend)); | 76 color_utils::AlphaBlend(track_on_color, track_off_color, blend)); |
| 73 canvas->DrawRoundRect(track_rect, track_rect.height() / 2, paint); | 77 canvas->DrawRoundRect(track_rect, track_rect.height() / 2, track_paint); |
| 74 | 78 |
| 75 // Thumb. | 79 // Thumb. |
| 76 const SkColor thumb_on_color = GetNativeTheme()->GetSystemColor( | |
| 77 ui::NativeTheme::kColorId_CallToActionColor); | |
| 78 // TODO(estade): get this color from the theme. | |
| 79 const SkColor thumb_off_color = SkColorSetRGB(0xFA, 0xFA, 0xFA); | |
| 80 paint.setColor( | |
| 81 color_utils::AlphaBlend(thumb_on_color, thumb_off_color, blend)); | |
| 82 gfx::Rect thumb_bounds = GetLocalBounds(); | 80 gfx::Rect thumb_bounds = GetLocalBounds(); |
| 83 thumb_bounds.Inset(gfx::Insets(kThumbMargin)); | 81 thumb_bounds.Inset(gfx::Insets(kThumbVerticalMargin, kThumbHorizontalMargin)); |
| 84 thumb_bounds.set_x(thumb_bounds.x() + | 82 thumb_bounds.set_x(thumb_bounds.x() + |
| 85 slide_animation_.GetCurrentValue() * | 83 slide_animation_.GetCurrentValue() * |
| 86 (thumb_bounds.width() - thumb_bounds.height())); | 84 (thumb_bounds.width() - thumb_bounds.height())); |
| 87 thumb_bounds.set_width(thumb_bounds.height()); | 85 thumb_bounds.set_width(thumb_bounds.height()); |
| 88 thumb_bounds.set_x(GetMirroredXForRect(thumb_bounds)); | 86 thumb_bounds.set_x(GetMirroredXForRect(thumb_bounds)); |
| 87 SkPaint thumb_paint; | |
| 88 std::vector<gfx::ShadowValue> shadows; | |
| 89 shadows.emplace(shadows.end(), gfx::Vector2d(0, 1), 5.f, | |
|
sadrul
2016/07/22 03:35:05
emplace_back instead?
Evan Stade
2016/07/22 14:03:28
done, thanks
| |
| 90 SkColorSetA(SK_ColorBLACK, 0x99)); | |
| 91 thumb_paint.setLooper(gfx::CreateShadowDrawLooperCorrectBlur(shadows)); | |
| 92 thumb_paint.setStyle(SkPaint::kStrokeAndFill_Style); | |
| 93 thumb_paint.setAntiAlias(true); | |
| 94 const SkColor thumb_on_color = GetNativeTheme()->GetSystemColor( | |
| 95 ui::NativeTheme::kColorId_CallToActionColor); | |
| 96 // TODO(estade): get this color from the theme. | |
| 97 const SkColor thumb_off_color = SkColorSetRGB(0xFA, 0xFA, 0xFA); | |
| 98 thumb_paint.setColor( | |
| 99 color_utils::AlphaBlend(thumb_on_color, thumb_off_color, blend)); | |
| 89 canvas->DrawCircle(gfx::RectF(thumb_bounds).CenterPoint(), | 100 canvas->DrawCircle(gfx::RectF(thumb_bounds).CenterPoint(), |
| 90 thumb_bounds.height() / 2.f, paint); | 101 thumb_bounds.height() / 2.f, thumb_paint); |
| 91 // TODO(estade): add a shadow to the thumb. | |
| 92 } | 102 } |
| 93 | 103 |
| 94 void ToggleButton::NotifyClick(const ui::Event& event) { | 104 void ToggleButton::NotifyClick(const ui::Event& event) { |
| 95 SetIsOn(!is_on(), true); | 105 SetIsOn(!is_on(), true); |
| 96 CustomButton::NotifyClick(event); | 106 CustomButton::NotifyClick(event); |
| 97 } | 107 } |
| 98 | 108 |
| 99 void ToggleButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 109 void ToggleButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 100 SchedulePaint(); | 110 SchedulePaint(); |
| 101 } | 111 } |
| 102 | 112 |
| 103 void ToggleButton::AnimationProgressed(const gfx::Animation* animation) { | 113 void ToggleButton::AnimationProgressed(const gfx::Animation* animation) { |
| 104 if (animation == &slide_animation_) | 114 if (animation == &slide_animation_) |
| 105 SchedulePaint(); | 115 SchedulePaint(); |
| 106 else | 116 else |
| 107 CustomButton::AnimationProgressed(animation); | 117 CustomButton::AnimationProgressed(animation); |
| 108 } | 118 } |
| 109 | 119 |
| 110 } // namespace views | 120 } // namespace views |
| OLD | NEW |