| 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/common/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 set_animate_on_state_change(true); | |
| 48 swap_images_animation_->Reset(1); | |
| 49 | |
| 50 // Do not flip the gfx::Canvas passed to the OnPaint() method. The snap left | |
| 51 // and snap right button icons should not be flipped. The other icons are | |
| 52 // horizontally symmetrical. | |
| 53 } | |
| 54 | |
| 55 FrameCaptionButton::~FrameCaptionButton() {} | |
| 56 | |
| 57 void FrameCaptionButton::SetImage(CaptionButtonIcon icon, | |
| 58 Animate animate, | |
| 59 const gfx::VectorIcon& icon_definition) { | |
| 60 gfx::ImageSkia new_icon_image = gfx::CreateVectorIcon( | |
| 61 icon_definition, | |
| 62 use_light_images_ ? SK_ColorWHITE : gfx::kChromeIconGrey); | |
| 63 | |
| 64 // The early return is dependent on |animate| because callers use SetImage() | |
| 65 // with ANIMATE_NO to progress the crossfade animation to the end. | |
| 66 if (icon == icon_ && | |
| 67 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && | |
| 68 new_icon_image.BackedBySameObjectAs(icon_image_)) { | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 if (animate == ANIMATE_YES) | |
| 73 crossfade_icon_image_ = icon_image_; | |
| 74 | |
| 75 icon_ = icon; | |
| 76 icon_definition_ = &icon_definition; | |
| 77 icon_image_ = new_icon_image; | |
| 78 | |
| 79 if (animate == ANIMATE_YES) { | |
| 80 swap_images_animation_->Reset(0); | |
| 81 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); | |
| 82 swap_images_animation_->Show(); | |
| 83 } else { | |
| 84 swap_images_animation_->Reset(1); | |
| 85 } | |
| 86 PreferredSizeChanged(); | |
| 87 SchedulePaint(); | |
| 88 } | |
| 89 | |
| 90 bool FrameCaptionButton::IsAnimatingImageSwap() const { | |
| 91 return swap_images_animation_->is_animating(); | |
| 92 } | |
| 93 | |
| 94 void FrameCaptionButton::SetAlpha(int alpha) { | |
| 95 if (alpha_ != alpha) { | |
| 96 alpha_ = alpha; | |
| 97 SchedulePaint(); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 gfx::Size FrameCaptionButton::GetPreferredSize() const { | |
| 102 return size_; | |
| 103 } | |
| 104 | |
| 105 const char* FrameCaptionButton::GetClassName() const { | |
| 106 return kViewClassName; | |
| 107 } | |
| 108 | |
| 109 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { | |
| 110 SkAlpha bg_alpha = SK_AlphaTRANSPARENT; | |
| 111 if (hover_animation().is_animating()) | |
| 112 bg_alpha = hover_animation().CurrentValueBetween(0, kHoveredAlpha); | |
| 113 else if (state() == STATE_HOVERED) | |
| 114 bg_alpha = kHoveredAlpha; | |
| 115 else if (state() == STATE_PRESSED) | |
| 116 bg_alpha = kPressedAlpha; | |
| 117 | |
| 118 if (bg_alpha != SK_AlphaTRANSPARENT) { | |
| 119 canvas->DrawColor(SkColorSetA( | |
| 120 use_light_images_ ? SK_ColorWHITE : SK_ColorBLACK, bg_alpha)); | |
| 121 } | |
| 122 | |
| 123 int icon_alpha = swap_images_animation_->CurrentValueBetween(0, 255); | |
| 124 int crossfade_icon_alpha = 0; | |
| 125 if (icon_alpha < static_cast<int>(kFadeOutRatio * 255)) | |
| 126 crossfade_icon_alpha = static_cast<int>(255 - icon_alpha / kFadeOutRatio); | |
| 127 | |
| 128 if (crossfade_icon_alpha > 0 && !crossfade_icon_image_.isNull()) { | |
| 129 gfx::Canvas icon_canvas(icon_image_.size(), canvas->image_scale(), false); | |
| 130 cc::PaintFlags flags; | |
| 131 flags.setAlpha(icon_alpha); | |
| 132 icon_canvas.DrawImageInt(icon_image_, 0, 0, flags); | |
| 133 | |
| 134 flags.setAlpha(crossfade_icon_alpha); | |
| 135 flags.setBlendMode(SkBlendMode::kPlus); | |
| 136 icon_canvas.DrawImageInt(crossfade_icon_image_, 0, 0, flags); | |
| 137 | |
| 138 PaintCentered(canvas, gfx::ImageSkia(icon_canvas.ExtractImageRep()), | |
| 139 alpha_); | |
| 140 } else { | |
| 141 if (!swap_images_animation_->is_animating()) | |
| 142 icon_alpha = alpha_; | |
| 143 PaintCentered(canvas, icon_image_, icon_alpha); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 void FrameCaptionButton::OnGestureEvent(ui::GestureEvent* event) { | |
| 148 // CustomButton does not become pressed when the user drags off and then back | |
| 149 // onto the button. Make FrameCaptionButton pressed in this case because this | |
| 150 // behavior is more consistent with AlternateFrameSizeButton. | |
| 151 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN || | |
| 152 event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { | |
| 153 if (HitTestPoint(event->location())) { | |
| 154 SetState(STATE_PRESSED); | |
| 155 RequestFocus(); | |
| 156 event->StopPropagation(); | |
| 157 } else { | |
| 158 SetState(STATE_NORMAL); | |
| 159 } | |
| 160 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { | |
| 161 if (HitTestPoint(event->location())) { | |
| 162 SetState(STATE_HOVERED); | |
| 163 NotifyClick(*event); | |
| 164 event->StopPropagation(); | |
| 165 } | |
| 166 } | |
| 167 CustomButton::OnGestureEvent(event); | |
| 168 } | |
| 169 | |
| 170 void FrameCaptionButton::PaintCentered(gfx::Canvas* canvas, | |
| 171 const gfx::ImageSkia& to_center, | |
| 172 int alpha) { | |
| 173 if (!paint_as_active_) { | |
| 174 // Paint icons as active when they are hovered over or pressed. | |
| 175 double inactive_alpha = kInactiveIconAlpha; | |
| 176 if (hover_animation().is_animating()) { | |
| 177 inactive_alpha = | |
| 178 hover_animation().CurrentValueBetween(inactive_alpha, 1.0f); | |
| 179 } else if (state() == STATE_PRESSED || state() == STATE_HOVERED) { | |
| 180 inactive_alpha = 1.0f; | |
| 181 } | |
| 182 alpha *= inactive_alpha; | |
| 183 } | |
| 184 | |
| 185 cc::PaintFlags flags; | |
| 186 flags.setAlpha(alpha); | |
| 187 canvas->DrawImageInt(to_center, (width() - to_center.width()) / 2, | |
| 188 (height() - to_center.height()) / 2, flags); | |
| 189 } | |
| 190 | |
| 191 } // namespace ash | |
| OLD | NEW |