| 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. |
| 24 const float kInactiveIconAlpha = 0.2f; |
| 25 |
| 23 } // namespace | 26 } // namespace |
| 24 | 27 |
| 25 // static | 28 // static |
| 26 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton"; | 29 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton"; |
| 27 | 30 |
| 28 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener, | 31 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener, |
| 29 CaptionButtonIcon icon) | 32 CaptionButtonIcon icon) |
| 30 : CustomButton(listener), | 33 : CustomButton(listener), |
| 31 icon_(icon), | 34 icon_(icon), |
| 32 paint_as_active_(false), | 35 paint_as_active_(false), |
| 33 alpha_(255), | 36 alpha_(255), |
| 34 icon_image_id_(-1), | 37 icon_image_id_(-1), |
| 35 inactive_icon_image_id_(-1), | |
| 36 hovered_background_image_id_(-1), | 38 hovered_background_image_id_(-1), |
| 37 pressed_background_image_id_(-1), | 39 pressed_background_image_id_(-1), |
| 38 swap_images_animation_(new gfx::SlideAnimation(this)) { | 40 swap_images_animation_(new gfx::SlideAnimation(this)) { |
| 39 swap_images_animation_->Reset(1); | 41 swap_images_animation_->Reset(1); |
| 40 | 42 |
| 41 // Do not flip the gfx::Canvas passed to the OnPaint() method. The snap left | 43 // Do not flip the gfx::Canvas passed to the OnPaint() method. The snap left |
| 42 // and snap right button icons should not be flipped. The other icons are | 44 // and snap right button icons should not be flipped. The other icons are |
| 43 // horizontally symmetrical. | 45 // horizontally symmetrical. |
| 44 } | 46 } |
| 45 | 47 |
| 46 FrameCaptionButton::~FrameCaptionButton() { | 48 FrameCaptionButton::~FrameCaptionButton() { |
| 47 } | 49 } |
| 48 | 50 |
| 49 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, | 51 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, |
| 50 Animate animate, | 52 Animate animate, |
| 51 int icon_image_id, | 53 int icon_image_id, |
| 52 int inactive_icon_image_id, | |
| 53 int hovered_background_image_id, | 54 int hovered_background_image_id, |
| 54 int pressed_background_image_id) { | 55 int pressed_background_image_id) { |
| 55 // The early return is dependant on |animate| because callers use SetImages() | 56 // The early return is dependant on |animate| because callers use SetImages() |
| 56 // with ANIMATE_NO to progress the crossfade animation to the end. | 57 // with ANIMATE_NO to progress the crossfade animation to the end. |
| 57 if (icon == icon_ && | 58 if (icon == icon_ && |
| 58 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && | 59 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && |
| 59 icon_image_id == icon_image_id_ && | 60 icon_image_id == icon_image_id_ && |
| 60 inactive_icon_image_id == inactive_icon_image_id_ && | |
| 61 hovered_background_image_id == hovered_background_image_id_ && | 61 hovered_background_image_id == hovered_background_image_id_ && |
| 62 pressed_background_image_id == pressed_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_ = GetIconImageToPaint(); | 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 inactive_icon_image_id_ = inactive_icon_image_id; | |
| 72 hovered_background_image_id_ = hovered_background_image_id; | 71 hovered_background_image_id_ = hovered_background_image_id; |
| 73 pressed_background_image_id_ = pressed_background_image_id; | 72 pressed_background_image_id_ = pressed_background_image_id; |
| 74 | 73 |
| 75 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 74 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 76 icon_image_ = *rb.GetImageSkiaNamed(icon_image_id); | 75 icon_image_ = *rb.GetImageSkiaNamed(icon_image_id); |
| 77 inactive_icon_image_ = *rb.GetImageSkiaNamed(inactive_icon_image_id); | |
| 78 hovered_background_image_ = *rb.GetImageSkiaNamed( | 76 hovered_background_image_ = *rb.GetImageSkiaNamed( |
| 79 hovered_background_image_id); | 77 hovered_background_image_id); |
| 80 pressed_background_image_ = *rb.GetImageSkiaNamed( | 78 pressed_background_image_ = *rb.GetImageSkiaNamed( |
| 81 pressed_background_image_id); | 79 pressed_background_image_id); |
| 82 | 80 |
| 83 if (animate == ANIMATE_YES) { | 81 if (animate == ANIMATE_YES) { |
| 84 swap_images_animation_->Reset(0); | 82 swap_images_animation_->Reset(0); |
| 85 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); | 83 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); |
| 86 swap_images_animation_->Show(); | 84 swap_images_animation_->Show(); |
| 87 } else { | 85 } else { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 canvas->DrawImageInt(hovered_background_image_, 0, 0, paint); | 118 canvas->DrawImageInt(hovered_background_image_, 0, 0, paint); |
| 121 } else if (state() == STATE_PRESSED) { | 119 } else if (state() == STATE_PRESSED) { |
| 122 canvas->DrawImageInt(pressed_background_image_, 0, 0); | 120 canvas->DrawImageInt(pressed_background_image_, 0, 0); |
| 123 } | 121 } |
| 124 | 122 |
| 125 int icon_alpha = swap_images_animation_->CurrentValueBetween(0, 255); | 123 int icon_alpha = swap_images_animation_->CurrentValueBetween(0, 255); |
| 126 int crossfade_icon_alpha = 0; | 124 int crossfade_icon_alpha = 0; |
| 127 if (icon_alpha < static_cast<int>(kFadeOutRatio * 255)) | 125 if (icon_alpha < static_cast<int>(kFadeOutRatio * 255)) |
| 128 crossfade_icon_alpha = static_cast<int>(255 - icon_alpha / kFadeOutRatio); | 126 crossfade_icon_alpha = static_cast<int>(255 - icon_alpha / kFadeOutRatio); |
| 129 | 127 |
| 130 gfx::ImageSkia icon_image = GetIconImageToPaint(); | |
| 131 if (crossfade_icon_alpha > 0 && !crossfade_icon_image_.isNull()) { | 128 if (crossfade_icon_alpha > 0 && !crossfade_icon_image_.isNull()) { |
| 132 gfx::Canvas icon_canvas(icon_image.size(), canvas->image_scale(), false); | 129 gfx::Canvas icon_canvas(icon_image_.size(), canvas->image_scale(), false); |
| 133 SkPaint paint; | 130 SkPaint paint; |
| 134 paint.setAlpha(icon_alpha); | 131 paint.setAlpha(icon_alpha); |
| 135 icon_canvas.DrawImageInt(icon_image, 0, 0, paint); | 132 icon_canvas.DrawImageInt(icon_image_, 0, 0, paint); |
| 136 | 133 |
| 137 paint.setAlpha(crossfade_icon_alpha); | 134 paint.setAlpha(crossfade_icon_alpha); |
| 138 paint.setXfermodeMode(SkXfermode::kPlus_Mode); | 135 paint.setXfermodeMode(SkXfermode::kPlus_Mode); |
| 139 icon_canvas.DrawImageInt(crossfade_icon_image_, 0, 0, paint); | 136 icon_canvas.DrawImageInt(crossfade_icon_image_, 0, 0, paint); |
| 140 | 137 |
| 141 PaintCentered(canvas, gfx::ImageSkia(icon_canvas.ExtractImageRep()), | 138 PaintCentered(canvas, gfx::ImageSkia(icon_canvas.ExtractImageRep()), |
| 142 alpha_); | 139 alpha_); |
| 143 } else { | 140 } else { |
| 144 if (!swap_images_animation_->is_animating()) | 141 if (!swap_images_animation_->is_animating()) |
| 145 icon_alpha = alpha_; | 142 icon_alpha = alpha_; |
| 146 PaintCentered(canvas, icon_image, icon_alpha); | 143 PaintCentered(canvas, icon_image_, icon_alpha); |
| 147 } | 144 } |
| 148 } | 145 } |
| 149 | 146 |
| 150 void FrameCaptionButton::OnGestureEvent(ui::GestureEvent* event) { | 147 void FrameCaptionButton::OnGestureEvent(ui::GestureEvent* event) { |
| 151 // CustomButton does not become pressed when the user drags off and then back | 148 // CustomButton does not become pressed when the user drags off and then back |
| 152 // onto the button. Make FrameCaptionButton pressed in this case because this | 149 // onto the button. Make FrameCaptionButton pressed in this case because this |
| 153 // behavior is more consistent with AlternateFrameSizeButton. | 150 // behavior is more consistent with AlternateFrameSizeButton. |
| 154 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN || | 151 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN || |
| 155 event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { | 152 event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { |
| 156 if (HitTestPoint(event->location())) { | 153 if (HitTestPoint(event->location())) { |
| 157 SetState(STATE_PRESSED); | 154 SetState(STATE_PRESSED); |
| 158 RequestFocus(); | 155 RequestFocus(); |
| 159 event->StopPropagation(); | 156 event->StopPropagation(); |
| 160 } else { | 157 } else { |
| 161 SetState(STATE_NORMAL); | 158 SetState(STATE_NORMAL); |
| 162 } | 159 } |
| 163 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { | 160 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { |
| 164 if (HitTestPoint(event->location())) { | 161 if (HitTestPoint(event->location())) { |
| 165 SetState(STATE_HOVERED); | 162 SetState(STATE_HOVERED); |
| 166 NotifyClick(*event); | 163 NotifyClick(*event); |
| 167 event->StopPropagation(); | 164 event->StopPropagation(); |
| 168 } | 165 } |
| 169 } | 166 } |
| 170 CustomButton::OnGestureEvent(event); | 167 CustomButton::OnGestureEvent(event); |
| 171 } | 168 } |
| 172 | 169 |
| 173 const gfx::ImageSkia& FrameCaptionButton::GetIconImageToPaint() const { | |
| 174 return paint_as_active_ ? icon_image_ : inactive_icon_image_; | |
| 175 } | |
| 176 | |
| 177 void FrameCaptionButton::PaintCentered(gfx::Canvas* canvas, | 170 void FrameCaptionButton::PaintCentered(gfx::Canvas* canvas, |
| 178 const gfx::ImageSkia& to_center, | 171 const gfx::ImageSkia& to_center, |
| 179 int alpha) { | 172 int alpha) { |
| 173 if (!paint_as_active_) |
| 174 alpha *= kInactiveIconAlpha; |
| 175 |
| 180 SkPaint paint; | 176 SkPaint paint; |
| 181 paint.setAlpha(alpha); | 177 paint.setAlpha(alpha); |
| 182 canvas->DrawImageInt(to_center, | 178 canvas->DrawImageInt(to_center, |
| 183 (width() - to_center.width()) / 2, | 179 (width() - to_center.width()) / 2, |
| 184 (height() - to_center.height()) / 2, | 180 (height() - to_center.height()) / 2, |
| 185 paint); | 181 paint); |
| 186 } | 182 } |
| 187 | 183 |
| 188 } // namespace ash | 184 } // namespace ash |
| OLD | NEW |