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/animation/slide_animation.h" | 8 #include "ui/gfx/animation/slide_animation.h" |
9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 // static | 24 // static |
25 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton"; | 25 const char FrameCaptionButton::kViewClassName[] = "FrameCaptionButton"; |
26 | 26 |
27 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener, | 27 FrameCaptionButton::FrameCaptionButton(views::ButtonListener* listener, |
28 CaptionButtonIcon icon) | 28 CaptionButtonIcon icon) |
29 : CustomButton(listener), | 29 : CustomButton(listener), |
30 icon_(icon), | 30 icon_(icon), |
| 31 paint_as_active_(false), |
31 last_paint_scale_(1.0f), | 32 last_paint_scale_(1.0f), |
32 normal_image_id_(-1), | 33 icon_image_id_(-1), |
33 hovered_image_id_(-1), | 34 inactive_icon_image_id_(-1), |
34 pressed_image_id_(-1), | 35 hovered_background_image_id_(-1), |
| 36 pressed_background_image_id_(-1), |
35 swap_images_animation_(new gfx::SlideAnimation(this)) { | 37 swap_images_animation_(new gfx::SlideAnimation(this)) { |
36 swap_images_animation_->Reset(1); | 38 swap_images_animation_->Reset(1); |
37 EnableCanvasFlippingForRTLUI(true); | 39 EnableCanvasFlippingForRTLUI(true); |
38 } | 40 } |
39 | 41 |
40 FrameCaptionButton::~FrameCaptionButton() { | 42 FrameCaptionButton::~FrameCaptionButton() { |
41 } | 43 } |
42 | 44 |
43 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, | 45 void FrameCaptionButton::SetImages(CaptionButtonIcon icon, |
44 Animate animate, | 46 Animate animate, |
45 int normal_image_id, | 47 int icon_image_id, |
46 int hovered_image_id, | 48 int inactive_icon_image_id, |
47 int pressed_image_id) { | 49 int hovered_background_image_id, |
| 50 int pressed_background_image_id) { |
48 // The early return is dependant on |animate| because callers use SetImages() | 51 // The early return is dependant on |animate| because callers use SetImages() |
49 // with ANIMATE_NO to progress the crossfade animation to the end. | 52 // with ANIMATE_NO to progress the crossfade animation to the end. |
50 if (icon == icon_ && | 53 if (icon == icon_ && |
51 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && | 54 (animate == ANIMATE_YES || !swap_images_animation_->is_animating()) && |
52 normal_image_id == normal_image_id_ && | 55 icon_image_id == icon_image_id_ && |
53 hovered_image_id == hovered_image_id_ && | 56 inactive_icon_image_id == inactive_icon_image_id_ && |
54 pressed_image_id == pressed_image_id_) { | 57 hovered_background_image_id == hovered_background_image_id_ && |
| 58 pressed_background_image_id == pressed_background_image_id_) { |
55 return; | 59 return; |
56 } | 60 } |
57 | 61 |
58 if (animate == ANIMATE_YES) { | 62 if (animate == ANIMATE_YES) { |
59 gfx::Canvas canvas(size(), last_paint_scale_, false); | 63 gfx::Canvas canvas(size(), last_paint_scale_, false); |
60 OnPaint(&canvas); | 64 OnPaint(&canvas); |
61 crossfade_image_ = gfx::ImageSkia(canvas.ExtractImageRep()); | 65 crossfade_image_ = gfx::ImageSkia(canvas.ExtractImageRep()); |
62 } | 66 } |
63 | 67 |
64 icon_ = icon; | 68 icon_ = icon; |
65 normal_image_id_ = normal_image_id; | 69 icon_image_id_ = icon_image_id; |
66 hovered_image_id_ = hovered_image_id; | 70 inactive_icon_image_id_ = inactive_icon_image_id; |
67 pressed_image_id_ = pressed_image_id; | 71 hovered_background_image_id_ = hovered_background_image_id; |
| 72 pressed_background_image_id_ = pressed_background_image_id; |
68 | 73 |
69 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 74 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
70 normal_image_ = *rb.GetImageSkiaNamed(normal_image_id); | 75 icon_image_ = *rb.GetImageSkiaNamed(icon_image_id); |
71 hovered_image_ = *rb.GetImageSkiaNamed(hovered_image_id); | 76 inactive_icon_image_ = *rb.GetImageSkiaNamed(inactive_icon_image_id); |
72 pressed_image_ = *rb.GetImageSkiaNamed(pressed_image_id); | 77 hovered_background_image_ = *rb.GetImageSkiaNamed( |
| 78 hovered_background_image_id); |
| 79 pressed_background_image_ = *rb.GetImageSkiaNamed( |
| 80 pressed_background_image_id); |
73 | 81 |
74 if (animate == ANIMATE_YES) { | 82 if (animate == ANIMATE_YES) { |
75 swap_images_animation_->Reset(0); | 83 swap_images_animation_->Reset(0); |
76 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); | 84 swap_images_animation_->SetSlideDuration(kSwapImagesAnimationDurationMs); |
77 swap_images_animation_->Show(); | 85 swap_images_animation_->Show(); |
78 } else { | 86 } else { |
79 swap_images_animation_->Reset(1); | 87 swap_images_animation_->Reset(1); |
80 } | 88 } |
81 PreferredSizeChanged(); | 89 PreferredSizeChanged(); |
82 SchedulePaint(); | 90 SchedulePaint(); |
83 } | 91 } |
84 | 92 |
85 bool FrameCaptionButton::IsAnimatingImageSwap() const { | 93 bool FrameCaptionButton::IsAnimatingImageSwap() const { |
86 return swap_images_animation_->is_animating(); | 94 return swap_images_animation_->is_animating(); |
87 } | 95 } |
88 | 96 |
89 gfx::Size FrameCaptionButton::GetPreferredSize() { | 97 gfx::Size FrameCaptionButton::GetPreferredSize() { |
90 return normal_image_.isNull() ? gfx::Size() : normal_image_.size(); | 98 return hovered_background_image_.isNull() ? |
| 99 gfx::Size() : hovered_background_image_.size(); |
91 } | 100 } |
92 | 101 |
93 const char* FrameCaptionButton::GetClassName() const { | 102 const char* FrameCaptionButton::GetClassName() const { |
94 return kViewClassName; | 103 return kViewClassName; |
95 } | 104 } |
96 | 105 |
97 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { | 106 void FrameCaptionButton::OnPaint(gfx::Canvas* canvas) { |
98 // TODO(pkotwicz): Take |CustomButton::hover_animation_| into account once | 107 // TODO(pkotwicz): Take |CustomButton::hover_animation_| into account once |
99 // the button has separate images for the icon and background. | 108 // the button has separate images for the icon and background. |
100 | 109 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 155 } |
147 | 156 |
148 void FrameCaptionButton::StateChanged() { | 157 void FrameCaptionButton::StateChanged() { |
149 if (state_ == STATE_HOVERED || state_ == STATE_PRESSED) | 158 if (state_ == STATE_HOVERED || state_ == STATE_PRESSED) |
150 swap_images_animation_->Reset(1); | 159 swap_images_animation_->Reset(1); |
151 } | 160 } |
152 | 161 |
153 void FrameCaptionButton::PaintWithAnimationEndState( | 162 void FrameCaptionButton::PaintWithAnimationEndState( |
154 gfx::Canvas* canvas, | 163 gfx::Canvas* canvas, |
155 int opacity) { | 164 int opacity) { |
156 gfx::ImageSkia img; | 165 SkPaint paint; |
157 if (state() == STATE_HOVERED) { | 166 paint.setAlpha(opacity); |
158 img = hovered_image_; | 167 if (state() == STATE_HOVERED) |
159 } else if (state() == STATE_PRESSED) { | 168 canvas->DrawImageInt(hovered_background_image_, 0, 0, paint); |
160 img = pressed_image_; | 169 else if (state() == STATE_PRESSED) |
161 } else { | 170 canvas->DrawImageInt(pressed_background_image_, 0, 0, paint); |
162 img = normal_image_; | |
163 } | |
164 | 171 |
165 if (!img.isNull()) { | 172 gfx::ImageSkia icon_image = paint_as_active_ ? |
166 SkPaint paint; | 173 icon_image_ : inactive_icon_image_; |
167 paint.setAlpha(opacity); | 174 canvas->DrawImageInt(icon_image, |
168 canvas->DrawImageInt(img, 0, 0, paint); | 175 (width() - icon_image.width()) / 2, |
169 } | 176 (height() - icon_image.height()) / 2, |
| 177 paint); |
170 } | 178 } |
171 | 179 |
172 } // namespace ash | 180 } // namespace ash |
OLD | NEW |