OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/wm/caption_buttons/frame_caption_button.h" | 5 #include "ash/wm/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/canvas.h" | 8 #include "ui/gfx/canvas.h" |
9 | 9 |
10 namespace ash { | 10 namespace ash { |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 // The duration of the crossfade animation when swapping the button's images. | 14 // The duration of the crossfade animation when swapping the button's images. |
15 const int kSwapImagesAnimationDurationMs = 200; | 15 const int kSwapImagesAnimationDurationMs = 200; |
16 | 16 |
17 // The duration of the fade out animation of the old icon during a crossfade | 17 // The duration of the fade out animation of the old icon during a crossfade |
18 // animation as a ratio of |kSwapImagesAnimationDurationMs|. | 18 // animation as a ratio of |kSwapImagesAnimationDurationMs|. |
19 const float kFadeOutRatio = 0.5f; | 19 const float kFadeOutRatio = 0.5f; |
20 | 20 |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 // static | 23 // static |
24 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton"; | 24 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton"; |
25 | 25 |
26 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener, | 26 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener, |
27 CaptionButtonIcon icon) | 27 CaptionButtonIcon icon) |
28 : CustomButton(listener), | 28 : CustomButton(listener), |
29 icon_(icon), | 29 icon_(icon), |
| 30 paint_as_active_(false), |
30 last_paint_scale_(1.0f), | 31 last_paint_scale_(1.0f), |
31 normal_image_id_(-1), | 32 icon_image_id_(-1), |
32 hovered_image_id_(-1), | 33 inactive_icon_image_id_(-1), |
33 pressed_image_id_(-1), | 34 hovered_background_image_id_(-1), |
| 35 pressed_background_image_id_(-1), |
34 swap_images_animation_(new gfx::SlideAnimation(this)) { | 36 swap_images_animation_(new gfx::SlideAnimation(this)) { |
35 swap_images_animation_->Reset(1); | 37 swap_images_animation_->Reset(1); |
36 EnableCanvasFlippingForRTLUI(true); | 38 EnableCanvasFlippingForRTLUI(true); |
37 } | 39 } |
38 | 40 |
39 FrameCaptionButton::~FrameCaptionButton() { | 41 FrameCaptionButton::~FrameCaptionButton() { |
40 } | 42 } |
41 | 43 |
42 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, | 44 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, |
43 Animate animate, | 45 Animate animate, |
44 int normal_image_id, | 46 int icon_image_id, |
45 int hovered_image_id, | 47 int inactive_icon_image_id, |
46 int pressed_image_id) { | 48 int hovered_background_image_id, |
| 49 int pressed_background_image_id) { |
47 if (icon == icon_ && | 50 if (icon == icon_ && |
48 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && | 51 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && |
49 normal_image_id == normal_image_id_ && | 52 icon_image_id == icon_image_id_ && |
50 hovered_image_id == hovered_image_id_ && | 53 inactive_icon_image_id == inactive_icon_image_id_ && |
51 pressed_image_id == pressed_image_id_) { | 54 hovered_background_image_id == hovered_background_image_id_ && |
| 55 pressed_background_image_id == pressed_background_image_id_) { |
52 return; | 56 return; |
53 } | 57 } |
54 | 58 |
55 if (animate == ANIMATE_YES) { | 59 if (animate == ANIMATE_YES) { |
56 gfx::Canvas canvas(size(), last_paint_scale_, false); | 60 gfx::Canvas canvas(size(), last_paint_scale_, false); |
57 OnPaint(&canvas); | 61 OnPaint(&canvas); |
58 crossfade_image_ = gfx::ImageSkia(canvas.ExtractImageRep()); | 62 crossfade_image_ = gfx::ImageSkia(canvas.ExtractImageRep()); |
59 } | 63 } |
60 | 64 |
61 icon_ = icon; | 65 icon_ = icon; |
62 normal_image_id_ = normal_image_id; | 66 icon_image_id_ = icon_image_id; |
63 hovered_image_id_ = hovered_image_id; | 67 inactive_icon_image_id_ = inactive_icon_image_id; |
64 pressed_image_id_ = pressed_image_id; | 68 hovered_background_image_id_ = hovered_background_image_id; |
| 69 pressed_background_image_id_ = pressed_background_image_id; |
65 | 70 |
66 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 71 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
67 normal_image_ = *rb.GetImageSkiaNamed(normal_image_id); | 72 icon_image_ = *rb.GetImageSkiaNamed(icon_image_id); |
68 hovered_image_ = *rb.GetImageSkiaNamed(hovered_image_id); | 73 inactive_icon_image_ = *rb.GetImageSkiaNamed(inactive_icon_image_id); |
69 pressed_image_ = *rb.GetImageSkiaNamed(pressed_image_id); | 74 hovered_background_image_ = *rb.GetImageSkiaNamed( |
| 75 hovered_background_image_id); |
| 76 pressed_background_image_ = *rb.GetImageSkiaNamed( |
| 77 pressed_background_image_id); |
70 | 78 |
71 if (animate == ANIMATE_YES) { | 79 if (animate == ANIMATE_YES) { |
72 swap_images_animation_->Reset(0); | 80 swap_images_animation_->Reset(0); |
73 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); | 81 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); |
74 swap_images_animation_->Show(); | 82 swap_images_animation_->Show(); |
75 } else { | 83 } else { |
76 swap_images_animation_->Reset(1); | 84 swap_images_animation_->Reset(1); |
77 } | 85 } |
78 PreferredSizeChanged(); | 86 PreferredSizeChanged(); |
79 SchedulePaint(); | 87 SchedulePaint(); |
80 } | 88 } |
81 | 89 |
82 gfx::Size FrameCaptionButton::GetPreferredSize() { | 90 gfx::Size FrameCaptionButton::GetPreferredSize() { |
83 return normal_image_.isNull() ? gfx::Size() : normal_image_.size(); | 91 return hovered_background_image_.isNull() ? |
| 92 gfx::Size() : hovered_background_image_.size(); |
84 } | 93 } |
85 | 94 |
86 const char* FrameCaptionButton::GetClassName() const { | 95 const char* FrameCaptionButton::GetClassName() const { |
87 return kViewClassName; | 96 return kViewClassName; |
88 } | 97 } |
89 | 98 |
90 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { | 99 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { |
91 // TODO(pkotwicz): Take |CustomButton::hover_animation_| into account once | 100 // TODO(pkotwicz): Take |CustomButton::hover_animation_| into account once |
92 // the button has separate images for the icon and background. | 101 // the button has separate images for the icon and background. |
93 | 102 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 } | 145 } |
137 } | 146 } |
138 CustomButton::OnGestureEvent(event); | 147 CustomButton::OnGestureEvent(event); |
139 } | 148 } |
140 | 149 |
141 void FrameCaptionButton::StateChanged() { | 150 void FrameCaptionButton::StateChanged() { |
142 if (state_ == STATE_HOVERED || state_ == STATE_PRESSED) | 151 if (state_ == STATE_HOVERED || state_ == STATE_PRESSED) |
143 swap_images_animation_->Reset(1); | 152 swap_images_animation_->Reset(1); |
144 } | 153 } |
145 | 154 |
146 void FrameCaptionButton::PaintWithAnimationEndState( | 155 void FrameCaptionButton::PaintWithAnimationEndState(gfx::Canvas* canvas, |
147 gfx::Canvas* canvas, | 156 int opacity) { |
148 int opacity) { | 157 SkPaint paint; |
149 gfx::ImageSkia img; | 158 paint.setAlpha(opacity); |
150 if (state() == STATE_HOVERED) { | |
151 img = hovered_image_; | |
152 } else if (state() == STATE_PRESSED) { | |
153 img = pressed_image_; | |
154 } else { | |
155 img = normal_image_; | |
156 } | |
157 | 159 |
158 if (!img.isNull()) { | 160 if (state() == STATE_HOVERED) |
159 SkPaint paint; | 161 canvas->DrawImageInt(hovered_background_image_, 0, 0, paint); |
160 paint.setAlpha(opacity); | 162 else if (state() == STATE_PRESSED) |
161 canvas->DrawImageInt(img, 0, 0, paint); | 163 canvas->DrawImageInt(pressed_background_image_, 0, 0, paint); |
162 } | 164 |
| 165 paint.setAlpha(opacity); |
| 166 gfx::ImageSkia icon_image = paint_as_active_ ? |
| 167 icon_image_ : inactive_icon_image_; |
| 168 canvas->DrawImageInt(icon_image, |
| 169 (width() - icon_image.width()) / 2, |
| 170 (height() - icon_image.height()) / 2, |
| 171 paint); |
163 } | 172 } |
164 | 173 |
165 } // namespace ash | 174 } // namespace ash |
OLD | NEW |