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