Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ash/frame/caption_buttons/frame_caption_button.h" | 5 #include "ash/frame/caption_buttons/frame_caption_button.h" |
| 6 | 6 |
| 7 #include "ui/base/resource/resource_bundle.h" | 7 #include "ui/base/resource/resource_bundle.h" |
| 8 #include "ui/gfx/animation/slide_animation.h" | 8 #include "ui/gfx/animation/slide_animation.h" |
| 9 #include "ui/gfx/animation/throb_animation.h" | 9 #include "ui/gfx/animation/throb_animation.h" |
| 10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 11 | 11 |
| 12 namespace ash { | 12 namespace ash { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // The duration of the crossfade animation when swapping the button's images. | 16 // The duration of the crossfade animation when swapping the button's images. |
| 17 const int kSwapImagesAnimationDurationMs = 200; | 17 const int kSwapImagesAnimationDurationMs = 200; |
| 18 | 18 |
| 19 // The duration of the fade out animation of the old icon during a crossfade | 19 // The duration of the fade out animation of the old icon during a crossfade |
| 20 // animation as a ratio of |kSwapImagesAnimationDurationMs|. | 20 // animation as a ratio of |kSwapImagesAnimationDurationMs|. |
| 21 const float kFadeOutRatio = 0.5f; | 21 const float kFadeOutRatio = 0.5f; |
| 22 | 22 |
| 23 // The alpha to draw inactive icons with. | 23 // The alpha to draw inactive icons with. |
| 24 const float kInactiveIconAlpha = 0.2f; | 24 const float kInactiveIconAlpha = 0.2f; |
| 25 | 25 |
| 26 // The background colors used for the button hovered and pressed states. | |
| 27 // TODO(tdanderson|estade): Request these colors from ThemeProvider. | |
| 28 const SkColor kBackgroundHoveredColor = SkColorSetARGB(0x14, 0x0, 0x0, 0x0); | |
|
Peter Kasting
2015/12/17 20:56:08
Nit: Use SkColorSetA(SK_ColorBLACK, 0x14)
tdanderson
2015/12/18 17:42:53
Done.
| |
| 29 const SkColor kBackgroundPressedColor = SkColorSetARGB(0x24, 0x0, 0x0, 0x0); | |
|
tdanderson
2015/12/17 18:00:54
Evan, are we in a position now where I can move th
| |
| 30 | |
| 26 } // namespace | 31 } // namespace |
| 27 | 32 |
| 28 // static | 33 // static |
| 29 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton"; | 34 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton"; |
| 30 | 35 |
| 31 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener, | 36 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener, |
| 32 CaptionButtonIcon icon) | 37 CaptionButtonIcon icon) |
| 33 : CustomButton(listener), | 38 : CustomButton(listener), |
| 34 icon_(icon), | 39 icon_(icon), |
| 40 size_(gfx::Size()), | |
| 35 paint_as_active_(false), | 41 paint_as_active_(false), |
| 36 alpha_(255), | 42 alpha_(255), |
| 37 icon_image_id_(-1), | 43 icon_image_id_(-1), |
| 38 hovered_background_image_id_(-1), | |
| 39 pressed_background_image_id_(-1), | |
| 40 swap_images_animation_(new gfx::SlideAnimation(this)) { | 44 swap_images_animation_(new gfx::SlideAnimation(this)) { |
| 41 swap_images_animation_->Reset(1); | 45 swap_images_animation_->Reset(1); |
| 42 | 46 |
| 43 // Do not flip the gfx::Canvas passed to the OnPaint() method. The snap left | 47 // Do not flip the gfx::Canvas passed to the OnPaint() method. The snap left |
| 44 // and snap right button icons should not be flipped. The other icons are | 48 // and snap right button icons should not be flipped. The other icons are |
| 45 // horizontally symmetrical. | 49 // horizontally symmetrical. |
| 46 } | 50 } |
| 47 | 51 |
| 48 FrameCaptionButton::~FrameCaptionButton() { | 52 FrameCaptionButton::~FrameCaptionButton() { |
| 49 } | 53 } |
| 50 | 54 |
| 51 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, | 55 void FrameCaptionButton::SetImage(CaptionButtonIcon icon, |
| 52 Animate animate, | 56 Animate animate, |
| 53 int icon_image_id, | 57 int icon_image_id) { |
| 54 int hovered_background_image_id, | 58 // The early return is dependant on |animate| because callers use SetImage() |
| 55 int pressed_background_image_id) { | |
| 56 // The early return is dependant on |animate| because callers use SetImages() | |
| 57 // with ANIMATE_NO to progress the crossfade animation to the end. | 59 // with ANIMATE_NO to progress the crossfade animation to the end. |
| 58 if (icon == icon_ && | 60 if (icon == icon_ && |
| 59 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && | 61 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && |
| 60 icon_image_id == icon_image_id_ && | 62 icon_image_id == icon_image_id_) { |
| 61 hovered_background_image_id == hovered_background_image_id_ && | |
| 62 pressed_background_image_id == pressed_background_image_id_) { | |
| 63 return; | 63 return; |
| 64 } | 64 } |
| 65 | 65 |
| 66 if (animate == ANIMATE_YES) | 66 if (animate == ANIMATE_YES) |
| 67 crossfade_icon_image_ = icon_image_; | 67 crossfade_icon_image_ = icon_image_; |
| 68 | 68 |
| 69 icon_ = icon; | 69 icon_ = icon; |
| 70 icon_image_id_ = icon_image_id; | 70 icon_image_id_ = icon_image_id; |
| 71 hovered_background_image_id_ = hovered_background_image_id; | |
| 72 pressed_background_image_id_ = pressed_background_image_id; | |
| 73 | 71 |
| 74 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 72 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 75 icon_image_ = *rb.GetImageSkiaNamed(icon_image_id); | 73 icon_image_ = *rb.GetImageSkiaNamed(icon_image_id); |
| 76 hovered_background_image_ = *rb.GetImageSkiaNamed( | |
| 77 hovered_background_image_id); | |
| 78 pressed_background_image_ = *rb.GetImageSkiaNamed( | |
| 79 pressed_background_image_id); | |
| 80 | 74 |
| 81 if (animate == ANIMATE_YES) { | 75 if (animate == ANIMATE_YES) { |
| 82 swap_images_animation_->Reset(0); | 76 swap_images_animation_->Reset(0); |
| 83 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); | 77 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); |
| 84 swap_images_animation_->Show(); | 78 swap_images_animation_->Show(); |
| 85 } else { | 79 } else { |
| 86 swap_images_animation_->Reset(1); | 80 swap_images_animation_->Reset(1); |
| 87 } | 81 } |
| 88 PreferredSizeChanged(); | 82 PreferredSizeChanged(); |
| 89 SchedulePaint(); | 83 SchedulePaint(); |
| 90 } | 84 } |
| 91 | 85 |
| 92 bool FrameCaptionButton::IsAnimatingImageSwap() const { | 86 bool FrameCaptionButton::IsAnimatingImageSwap() const { |
| 93 return swap_images_animation_->is_animating(); | 87 return swap_images_animation_->is_animating(); |
| 94 } | 88 } |
| 95 | 89 |
| 96 void FrameCaptionButton::SetAlpha(int alpha) { | 90 void FrameCaptionButton::SetAlpha(int alpha) { |
| 97 if (alpha_ != alpha) { | 91 if (alpha_ != alpha) { |
| 98 alpha_ = alpha; | 92 alpha_ = alpha; |
| 99 SchedulePaint(); | 93 SchedulePaint(); |
| 100 } | 94 } |
| 101 } | 95 } |
| 102 | 96 |
| 103 gfx::Size FrameCaptionButton::GetPreferredSize() const { | 97 gfx::Size FrameCaptionButton::GetPreferredSize() const { |
| 104 return hovered_background_image_.isNull() ? | 98 return size_; |
| 105 gfx::Size() : hovered_background_image_.size(); | |
| 106 } | 99 } |
| 107 | 100 |
| 108 const char* FrameCaptionButton::GetClassName() const { | 101 const char* FrameCaptionButton::GetClassName() const { |
| 109 return kViewClassName; | 102 return kViewClassName; |
| 110 } | 103 } |
| 111 | 104 |
| 112 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { | 105 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { |
| 113 if (hover_animation_->is_animating() || state() == STATE_HOVERED) { | 106 if (hover_animation_->is_animating() || state() == STATE_HOVERED) { |
| 114 int hovered_background_alpha = hover_animation_->is_animating() ? | 107 SkColor color = kBackgroundHoveredColor; |
| 115 hover_animation_->CurrentValueBetween(0, 255) : 255; | 108 int hovered_background_alpha = |
| 116 SkPaint paint; | 109 hover_animation_->is_animating() |
| 117 paint.setAlpha(hovered_background_alpha); | 110 ? hover_animation_->CurrentValueBetween(0, SkColorGetA(color)) |
| 118 canvas->DrawImageInt(hovered_background_image_, 0, 0, paint); | 111 : SkColorGetA(color); |
| 112 color = SkColorSetA(color, hovered_background_alpha); | |
|
Evan Stade
2015/12/17 21:52:37
hmm, it seems like you have two distinct values he
tdanderson
2015/12/18 17:42:53
Done.
Peter Kasting
2015/12/18 18:59:15
No, he's suggesting changing your implementation.
Evan Stade
2015/12/18 19:12:04
Assuming I'm understanding how this all works (and
Peter Kasting
2015/12/18 19:36:38
Oh. I assumed the motivation was that it's possib
| |
| 113 canvas->DrawColor(color); | |
| 119 } else if (state() == STATE_PRESSED) { | 114 } else if (state() == STATE_PRESSED) { |
| 120 canvas->DrawImageInt(pressed_background_image_, 0, 0); | 115 canvas->DrawColor(kBackgroundPressedColor); |
| 121 } | 116 } |
| 122 | 117 |
| 123 int icon_alpha = swap_images_animation_->CurrentValueBetween(0, 255); | 118 int icon_alpha = swap_images_animation_->CurrentValueBetween(0, 255); |
| 124 int crossfade_icon_alpha = 0; | 119 int crossfade_icon_alpha = 0; |
| 125 if (icon_alpha < static_cast<int>(kFadeOutRatio * 255)) | 120 if (icon_alpha < static_cast<int>(kFadeOutRatio * 255)) |
| 126 crossfade_icon_alpha = static_cast<int>(255 - icon_alpha / kFadeOutRatio); | 121 crossfade_icon_alpha = static_cast<int>(255 - icon_alpha / kFadeOutRatio); |
| 127 | 122 |
| 128 if (crossfade_icon_alpha > 0 && !crossfade_icon_image_.isNull()) { | 123 if (crossfade_icon_alpha > 0 && !crossfade_icon_image_.isNull()) { |
| 129 gfx::Canvas icon_canvas(icon_image_.size(), canvas->image_scale(), false); | 124 gfx::Canvas icon_canvas(icon_image_.size(), canvas->image_scale(), false); |
| 130 SkPaint paint; | 125 SkPaint paint; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 | 179 |
| 185 SkPaint paint; | 180 SkPaint paint; |
| 186 paint.setAlpha(alpha); | 181 paint.setAlpha(alpha); |
| 187 canvas->DrawImageInt(to_center, | 182 canvas->DrawImageInt(to_center, |
| 188 (width() - to_center.width()) / 2, | 183 (width() - to_center.width()) / 2, |
| 189 (height() - to_center.height()) / 2, | 184 (height() - to_center.height()) / 2, |
| 190 paint); | 185 paint); |
| 191 } | 186 } |
| 192 | 187 |
| 193 } // namespace ash | 188 } // namespace ash |
| OLD | NEW |