OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "mash/wm/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 mash { |
| 13 namespace wm { |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 // The duration of the crossfade animation when swapping the button's images. | 17 // The duration of the crossfade animation when swapping the button's images. |
17 const int kSwapImagesAnimationDurationMs = 200; | 18 const int kSwapImagesAnimationDurationMs = 200; |
18 | 19 |
19 // The duration of the fade out animation of the old icon during a crossfade | 20 // The duration of the fade out animation of the old icon during a crossfade |
20 // animation as a ratio of |kSwapImagesAnimationDurationMs|. | 21 // animation as a ratio of |kSwapImagesAnimationDurationMs|. |
21 const float kFadeOutRatio = 0.5f; | 22 const float kFadeOutRatio = 0.5f; |
22 | 23 |
(...skipping 15 matching lines...) Expand all Loading... |
38 hovered_background_image_id_(-1), | 39 hovered_background_image_id_(-1), |
39 pressed_background_image_id_(-1), | 40 pressed_background_image_id_(-1), |
40 swap_images_animation_(new gfx::SlideAnimation(this)) { | 41 swap_images_animation_(new gfx::SlideAnimation(this)) { |
41 swap_images_animation_->Reset(1); | 42 swap_images_animation_->Reset(1); |
42 | 43 |
43 // Do not flip the gfx::Canvas passed to the OnPaint() method. The snap left | 44 // 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 // and snap right button icons should not be flipped. The other icons are |
45 // horizontally symmetrical. | 46 // horizontally symmetrical. |
46 } | 47 } |
47 | 48 |
48 FrameCaptionButton::~FrameCaptionButton() { | 49 FrameCaptionButton::~FrameCaptionButton() {} |
49 } | |
50 | 50 |
51 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, | 51 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, |
52 Animate animate, | 52 Animate animate, |
53 int icon_image_id, | 53 int icon_image_id, |
54 int hovered_background_image_id, | 54 int hovered_background_image_id, |
55 int pressed_background_image_id) { | 55 int pressed_background_image_id) { |
56 // The early return is dependant on |animate| because callers use SetImages() | 56 // The early return is dependant on |animate| because callers use SetImages() |
57 // with ANIMATE_NO to progress the crossfade animation to the end. | 57 // with ANIMATE_NO to progress the crossfade animation to the end. |
58 if (icon == icon_ && | 58 if (icon == icon_ && |
59 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && | 59 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && |
60 icon_image_id == icon_image_id_ && | 60 icon_image_id == 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_ = icon_image_; | 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 // TODO(sky): it doesn't seem like these are used. |
71 hovered_background_image_id_ = hovered_background_image_id; | 72 hovered_background_image_id_ = hovered_background_image_id; |
72 pressed_background_image_id_ = pressed_background_image_id; | 73 pressed_background_image_id_ = pressed_background_image_id; |
73 | 74 |
74 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 75 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
75 icon_image_ = *rb.GetImageSkiaNamed(icon_image_id); | 76 icon_image_ = *rb.GetImageSkiaNamed(icon_image_id); |
76 hovered_background_image_ = *rb.GetImageSkiaNamed( | 77 hovered_background_image_ = |
77 hovered_background_image_id); | 78 *rb.GetImageSkiaNamed(hovered_background_image_id); |
78 pressed_background_image_ = *rb.GetImageSkiaNamed( | 79 pressed_background_image_ = |
79 pressed_background_image_id); | 80 *rb.GetImageSkiaNamed(pressed_background_image_id); |
80 | 81 |
81 if (animate == ANIMATE_YES) { | 82 if (animate == ANIMATE_YES) { |
82 swap_images_animation_->Reset(0); | 83 swap_images_animation_->Reset(0); |
83 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); | 84 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); |
84 swap_images_animation_->Show(); | 85 swap_images_animation_->Show(); |
85 } else { | 86 } else { |
86 swap_images_animation_->Reset(1); | 87 swap_images_animation_->Reset(1); |
87 } | 88 } |
88 PreferredSizeChanged(); | 89 PreferredSizeChanged(); |
89 SchedulePaint(); | 90 SchedulePaint(); |
90 } | 91 } |
91 | 92 |
92 bool FrameCaptionButton::IsAnimatingImageSwap() const { | 93 bool FrameCaptionButton::IsAnimatingImageSwap() const { |
93 return swap_images_animation_->is_animating(); | 94 return swap_images_animation_->is_animating(); |
94 } | 95 } |
95 | 96 |
96 void FrameCaptionButton::SetAlpha(int alpha) { | 97 void FrameCaptionButton::SetAlpha(int alpha) { |
97 if (alpha_ != alpha) { | 98 if (alpha_ != alpha) { |
98 alpha_ = alpha; | 99 alpha_ = alpha; |
99 SchedulePaint(); | 100 SchedulePaint(); |
100 } | 101 } |
101 } | 102 } |
102 | 103 |
103 gfx::Size FrameCaptionButton::GetPreferredSize() const { | 104 gfx::Size FrameCaptionButton::GetPreferredSize() const { |
104 return hovered_background_image_.isNull() ? | 105 return hovered_background_image_.isNull() ? gfx::Size() |
105 gfx::Size() : hovered_background_image_.size(); | 106 : hovered_background_image_.size(); |
106 } | 107 } |
107 | 108 |
108 const char* FrameCaptionButton::GetClassName() const { | 109 const char* FrameCaptionButton::GetClassName() const { |
109 return kViewClassName; | 110 return kViewClassName; |
110 } | 111 } |
111 | 112 |
112 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { | 113 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { |
113 if (hover_animation_->is_animating() || state() == STATE_HOVERED) { | 114 if (hover_animation_->is_animating() || state() == STATE_HOVERED) { |
114 int hovered_background_alpha = hover_animation_->is_animating() ? | 115 int hovered_background_alpha = |
115 hover_animation_->CurrentValueBetween(0, 255) : 255; | 116 hover_animation_->is_animating() |
| 117 ? hover_animation_->CurrentValueBetween(0, 255) |
| 118 : 255; |
116 SkPaint paint; | 119 SkPaint paint; |
117 paint.setAlpha(hovered_background_alpha); | 120 paint.setAlpha(hovered_background_alpha); |
118 canvas->DrawImageInt(hovered_background_image_, 0, 0, paint); | 121 canvas->DrawImageInt(hovered_background_image_, 0, 0, paint); |
119 } else if (state() == STATE_PRESSED) { | 122 } else if (state() == STATE_PRESSED) { |
120 canvas->DrawImageInt(pressed_background_image_, 0, 0); | 123 canvas->DrawImageInt(pressed_background_image_, 0, 0); |
121 } | 124 } |
122 | 125 |
123 int icon_alpha = swap_images_animation_->CurrentValueBetween(0, 255); | 126 int icon_alpha = swap_images_animation_->CurrentValueBetween(0, 255); |
124 int crossfade_icon_alpha = 0; | 127 int crossfade_icon_alpha = 0; |
125 if (icon_alpha < static_cast<int>(kFadeOutRatio * 255)) | 128 if (icon_alpha < static_cast<int>(kFadeOutRatio * 255)) |
126 crossfade_icon_alpha = static_cast<int>(255 - icon_alpha / kFadeOutRatio); | 129 crossfade_icon_alpha = static_cast<int>(255 - icon_alpha / kFadeOutRatio); |
127 | 130 |
128 if (crossfade_icon_alpha > 0 && !crossfade_icon_image_.isNull()) { | 131 if (crossfade_icon_alpha > 0 && !crossfade_icon_image_.isNull()) { |
129 gfx::Canvas icon_canvas(icon_image_.size(), canvas->image_scale(), false); | 132 gfx::Canvas icon_canvas(icon_image_.size(), canvas->image_scale(), false); |
130 SkPaint paint; | 133 SkPaint paint; |
131 paint.setAlpha(icon_alpha); | 134 paint.setAlpha(icon_alpha); |
132 icon_canvas.DrawImageInt(icon_image_, 0, 0, paint); | 135 icon_canvas.DrawImageInt(icon_image_, 0, 0, paint); |
133 | 136 |
134 paint.setAlpha(crossfade_icon_alpha); | 137 paint.setAlpha(crossfade_icon_alpha); |
135 paint.setXfermodeMode(SkXfermode::kPlus_Mode); | 138 paint.setXfermodeMode(SkXfermode::kPlus_Mode); |
136 icon_canvas.DrawImageInt(crossfade_icon_image_, 0, 0, paint); | 139 icon_canvas.DrawImageInt(crossfade_icon_image_, 0, 0, paint); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 inactive_alpha = | 180 inactive_alpha = |
178 hover_animation_->CurrentValueBetween(inactive_alpha, 1.0f); | 181 hover_animation_->CurrentValueBetween(inactive_alpha, 1.0f); |
179 } else if (state() == STATE_PRESSED || state() == STATE_HOVERED) { | 182 } else if (state() == STATE_PRESSED || state() == STATE_HOVERED) { |
180 inactive_alpha = 1.0f; | 183 inactive_alpha = 1.0f; |
181 } | 184 } |
182 alpha *= inactive_alpha; | 185 alpha *= inactive_alpha; |
183 } | 186 } |
184 | 187 |
185 SkPaint paint; | 188 SkPaint paint; |
186 paint.setAlpha(alpha); | 189 paint.setAlpha(alpha); |
187 canvas->DrawImageInt(to_center, | 190 canvas->DrawImageInt(to_center, (width() - to_center.width()) / 2, |
188 (width() - to_center.width()) / 2, | 191 (height() - to_center.height()) / 2, paint); |
189 (height() - to_center.height()) / 2, | |
190 paint); | |
191 } | 192 } |
192 | 193 |
193 } // namespace ash | 194 } // namespace wm |
| 195 } // namespace mash |
OLD | NEW |