| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "ash/mus/frame/caption_buttons/frame_caption_button.h" | |
| 6 | |
| 7 #include "ui/base/resource/resource_bundle.h" | |
| 8 #include "ui/gfx/animation/slide_animation.h" | |
| 9 #include "ui/gfx/animation/throb_animation.h" | |
| 10 #include "ui/gfx/canvas.h" | |
| 11 | |
| 12 namespace ash { | |
| 13 namespace mus { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // The duration of the crossfade animation when swapping the button's images. | |
| 18 const int kSwapImagesAnimationDurationMs = 200; | |
| 19 | |
| 20 // The duration of the fade out animation of the old icon during a crossfade | |
| 21 // animation as a ratio of |kSwapImagesAnimationDurationMs|. | |
| 22 const float kFadeOutRatio = 0.5f; | |
| 23 | |
| 24 // The alpha to draw inactive icons with. | |
| 25 const float kInactiveIconAlpha = 0.2f; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 // static | |
| 30 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton"; | |
| 31 | |
| 32 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener, | |
| 33 CaptionButtonIcon icon) | |
| 34 : CustomButton(listener), | |
| 35 icon_(icon), | |
| 36 paint_as_active_(false), | |
| 37 alpha_(255), | |
| 38 icon_image_id_(-1), | |
| 39 hovered_background_image_id_(-1), | |
| 40 pressed_background_image_id_(-1), | |
| 41 swap_images_animation_(new gfx::SlideAnimation(this)) { | |
| 42 swap_images_animation_->Reset(1); | |
| 43 | |
| 44 // Do not flip the gfx::Canvas passed to the OnPaint() method. The snap left | |
| 45 // and snap right button icons should not be flipped. The other icons are | |
| 46 // horizontally symmetrical. | |
| 47 } | |
| 48 | |
| 49 FrameCaptionButton::~FrameCaptionButton() {} | |
| 50 | |
| 51 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, | |
| 52 Animate animate, | |
| 53 int icon_image_id, | |
| 54 int hovered_background_image_id, | |
| 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. | |
| 58 if (icon == icon_ && | |
| 59 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && | |
| 60 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; | |
| 64 } | |
| 65 | |
| 66 if (animate == ANIMATE_YES) | |
| 67 crossfade_icon_image_ = icon_image_; | |
| 68 | |
| 69 icon_ = icon; | |
| 70 icon_image_id_ = icon_image_id; | |
| 71 // TODO(sky): it doesn't seem like these are used. | |
| 72 hovered_background_image_id_ = hovered_background_image_id; | |
| 73 pressed_background_image_id_ = pressed_background_image_id; | |
| 74 | |
| 75 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 76 icon_image_ = *rb.GetImageSkiaNamed(icon_image_id); | |
| 77 hovered_background_image_ = | |
| 78 *rb.GetImageSkiaNamed(hovered_background_image_id); | |
| 79 pressed_background_image_ = | |
| 80 *rb.GetImageSkiaNamed(pressed_background_image_id); | |
| 81 | |
| 82 if (animate == ANIMATE_YES) { | |
| 83 swap_images_animation_->Reset(0); | |
| 84 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); | |
| 85 swap_images_animation_->Show(); | |
| 86 } else { | |
| 87 swap_images_animation_->Reset(1); | |
| 88 } | |
| 89 PreferredSizeChanged(); | |
| 90 SchedulePaint(); | |
| 91 } | |
| 92 | |
| 93 bool FrameCaptionButton::IsAnimatingImageSwap() const { | |
| 94 return swap_images_animation_->is_animating(); | |
| 95 } | |
| 96 | |
| 97 void FrameCaptionButton::SetAlpha(int alpha) { | |
| 98 if (alpha_ != alpha) { | |
| 99 alpha_ = alpha; | |
| 100 SchedulePaint(); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 gfx::Size FrameCaptionButton::GetPreferredSize() const { | |
| 105 return hovered_background_image_.isNull() ? gfx::Size() | |
| 106 : hovered_background_image_.size(); | |
| 107 } | |
| 108 | |
| 109 const char* FrameCaptionButton::GetClassName() const { | |
| 110 return kViewClassName; | |
| 111 } | |
| 112 | |
| 113 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { | |
| 114 if (hover_animation().is_animating() || state() == STATE_HOVERED) { | |
| 115 int hovered_background_alpha = | |
| 116 hover_animation().CurrentValueBetween(0, 255); | |
| 117 SkPaint paint; | |
| 118 paint.setAlpha(hovered_background_alpha); | |
| 119 canvas->DrawImageInt(hovered_background_image_, 0, 0, paint); | |
| 120 } else if (state() == STATE_PRESSED) { | |
| 121 canvas->DrawImageInt(pressed_background_image_, 0, 0); | |
| 122 } | |
| 123 | |
| 124 int icon_alpha = swap_images_animation_->CurrentValueBetween(0, 255); | |
| 125 int crossfade_icon_alpha = 0; | |
| 126 if (icon_alpha < static_cast<int>(kFadeOutRatio * 255)) | |
| 127 crossfade_icon_alpha = static_cast<int>(255 - icon_alpha / kFadeOutRatio); | |
| 128 | |
| 129 if (crossfade_icon_alpha > 0 && !crossfade_icon_image_.isNull()) { | |
| 130 gfx::Canvas icon_canvas(icon_image_.size(), canvas->image_scale(), false); | |
| 131 SkPaint paint; | |
| 132 paint.setAlpha(icon_alpha); | |
| 133 icon_canvas.DrawImageInt(icon_image_, 0, 0, paint); | |
| 134 | |
| 135 paint.setAlpha(crossfade_icon_alpha); | |
| 136 paint.setXfermodeMode(SkXfermode::kPlus_Mode); | |
| 137 icon_canvas.DrawImageInt(crossfade_icon_image_, 0, 0, paint); | |
| 138 | |
| 139 PaintCentered(canvas, gfx::ImageSkia(icon_canvas.ExtractImageRep()), | |
| 140 alpha_); | |
| 141 } else { | |
| 142 if (!swap_images_animation_->is_animating()) | |
| 143 icon_alpha = alpha_; | |
| 144 PaintCentered(canvas, icon_image_, icon_alpha); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 void FrameCaptionButton::OnGestureEvent(ui::GestureEvent* event) { | |
| 149 // CustomButton does not become pressed when the user drags off and then back | |
| 150 // onto the button. Make FrameCaptionButton pressed in this case because this | |
| 151 // behavior is more consistent with AlternateFrameSizeButton. | |
| 152 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN || | |
| 153 event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { | |
| 154 if (HitTestPoint(event->location())) { | |
| 155 SetState(STATE_PRESSED); | |
| 156 RequestFocus(); | |
| 157 event->StopPropagation(); | |
| 158 } else { | |
| 159 SetState(STATE_NORMAL); | |
| 160 } | |
| 161 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { | |
| 162 if (HitTestPoint(event->location())) { | |
| 163 SetState(STATE_HOVERED); | |
| 164 NotifyClick(*event); | |
| 165 event->StopPropagation(); | |
| 166 } | |
| 167 } | |
| 168 CustomButton::OnGestureEvent(event); | |
| 169 } | |
| 170 | |
| 171 void FrameCaptionButton::PaintCentered(gfx::Canvas* canvas, | |
| 172 const gfx::ImageSkia& to_center, | |
| 173 int alpha) { | |
| 174 if (!paint_as_active_) { | |
| 175 // Paint icons as active when they are hovered over or pressed. | |
| 176 double inactive_alpha = kInactiveIconAlpha; | |
| 177 if (hover_animation().is_animating()) { | |
| 178 inactive_alpha = | |
| 179 hover_animation().CurrentValueBetween(inactive_alpha, 1.0f); | |
| 180 } else if (state() == STATE_PRESSED || state() == STATE_HOVERED) { | |
| 181 inactive_alpha = 1.0f; | |
| 182 } | |
| 183 alpha *= inactive_alpha; | |
| 184 } | |
| 185 | |
| 186 SkPaint paint; | |
| 187 paint.setAlpha(alpha); | |
| 188 canvas->DrawImageInt(to_center, (width() - to_center.width()) / 2, | |
| 189 (height() - to_center.height()) / 2, paint); | |
| 190 } | |
| 191 | |
| 192 } // namespace mus | |
| 193 } // namespace ash | |
| OLD | NEW |