[Telemetry] Remove smoothness's reliance on auto issuing marker feature of smoothness...
[chromium-blink-merge.git] / ash / frame / caption_buttons / frame_caption_button.cc
blob2b166436a2498ea7ec13bca7ef6266ad55753127
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.
5 #include "ash/frame/caption_buttons/frame_caption_button.h"
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"
12 namespace ash {
14 namespace {
16 // The duration of the crossfade animation when swapping the button's images.
17 const int kSwapImagesAnimationDurationMs = 200;
19 // The duration of the fade out animation of the old icon during a crossfade
20 // animation as a ratio of |kSwapImagesAnimationDurationMs|.
21 const float kFadeOutRatio = 0.5f;
23 // The alpha to draw inactive icons with.
24 const float kInactiveIconAlpha = 0.2f;
26 } // namespace
28 // static
29 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton";
31 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener,
32 CaptionButtonIcon icon)
33 : CustomButton(listener),
34 icon_(icon),
35 paint_as_active_(false),
36 alpha_(255),
37 icon_image_id_(-1),
38 hovered_background_image_id_(-1),
39 pressed_background_image_id_(-1),
40 swap_images_animation_(new gfx::SlideAnimation(this)) {
41 swap_images_animation_->Reset(1);
43 // 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
45 // horizontally symmetrical.
48 FrameCaptionButton::~FrameCaptionButton() {
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;
66 if (animate == ANIMATE_YES)
67 crossfade_icon_image_ = icon_image_;
69 icon_ = icon;
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;
74 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
75 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);
81 if (animate == ANIMATE_YES) {
82 swap_images_animation_->Reset(0);
83 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs);
84 swap_images_animation_->Show();
85 } else {
86 swap_images_animation_->Reset(1);
88 PreferredSizeChanged();
89 SchedulePaint();
92 bool FrameCaptionButton::IsAnimatingImageSwap() const {
93 return swap_images_animation_->is_animating();
96 void FrameCaptionButton::SetAlpha(int alpha) {
97 if (alpha_ != alpha) {
98 alpha_ = alpha;
99 SchedulePaint();
103 gfx::Size FrameCaptionButton::GetPreferredSize() const {
104 return hovered_background_image_.isNull() ?
105 gfx::Size() : hovered_background_image_.size();
108 const char* FrameCaptionButton::GetClassName() const {
109 return kViewClassName;
112 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) {
113 if (hover_animation_->is_animating() || state() == STATE_HOVERED) {
114 int hovered_background_alpha = hover_animation_->is_animating() ?
115 hover_animation_->CurrentValueBetween(0, 255) : 255;
116 SkPaint paint;
117 paint.setAlpha(hovered_background_alpha);
118 canvas->DrawImageInt(hovered_background_image_, 0, 0, paint);
119 } else if (state() == STATE_PRESSED) {
120 canvas->DrawImageInt(pressed_background_image_, 0, 0);
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);
128 if (crossfade_icon_alpha > 0 && !crossfade_icon_image_.isNull()) {
129 gfx::Canvas icon_canvas(icon_image_.size(), canvas->image_scale(), false);
130 SkPaint paint;
131 paint.setAlpha(icon_alpha);
132 icon_canvas.DrawImageInt(icon_image_, 0, 0, paint);
134 paint.setAlpha(crossfade_icon_alpha);
135 paint.setXfermodeMode(SkXfermode::kPlus_Mode);
136 icon_canvas.DrawImageInt(crossfade_icon_image_, 0, 0, paint);
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);
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);
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();
167 CustomButton::OnGestureEvent(event);
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;
182 alpha *= inactive_alpha;
185 SkPaint paint;
186 paint.setAlpha(alpha);
187 canvas->DrawImageInt(to_center,
188 (width() - to_center.width()) / 2,
189 (height() - to_center.height()) / 2,
190 paint);
193 } // namespace ash