OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/common/extensions/extension_action.h" | 5 #include "chrome/common/extensions/extension_action.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "chrome/common/badge_util.h" | 10 #include "chrome/common/badge_util.h" |
11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| 12 #include "grit/theme_resources.h" |
12 #include "grit/ui_resources.h" | 13 #include "grit/ui_resources.h" |
13 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
14 #include "third_party/skia/include/core/SkCanvas.h" | 15 #include "third_party/skia/include/core/SkCanvas.h" |
15 #include "third_party/skia/include/core/SkDevice.h" | 16 #include "third_party/skia/include/core/SkDevice.h" |
16 #include "third_party/skia/include/core/SkPaint.h" | 17 #include "third_party/skia/include/core/SkPaint.h" |
17 #include "third_party/skia/include/effects/SkGradientShader.h" | 18 #include "third_party/skia/include/effects/SkGradientShader.h" |
18 #include "ui/base/animation/animation_delegate.h" | 19 #include "ui/base/animation/animation_delegate.h" |
19 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
20 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
21 #include "ui/gfx/rect.h" | 22 #include "ui/gfx/rect.h" |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 | 185 |
185 bool ExtensionAction::HasPopup(int tab_id) const { | 186 bool ExtensionAction::HasPopup(int tab_id) const { |
186 return !GetPopupUrl(tab_id).is_empty(); | 187 return !GetPopupUrl(tab_id).is_empty(); |
187 } | 188 } |
188 | 189 |
189 GURL ExtensionAction::GetPopupUrl(int tab_id) const { | 190 GURL ExtensionAction::GetPopupUrl(int tab_id) const { |
190 return GetValue(&popup_url_, tab_id); | 191 return GetValue(&popup_url_, tab_id); |
191 } | 192 } |
192 | 193 |
193 void ExtensionAction::SetIcon(int tab_id, const SkBitmap& bitmap) { | 194 void ExtensionAction::SetIcon(int tab_id, const SkBitmap& bitmap) { |
194 SetValue(&icon_, tab_id, bitmap); | 195 SetValue(&icon_, tab_id, gfx::Image(bitmap)); |
195 } | 196 } |
196 | 197 |
197 SkBitmap ExtensionAction::GetIcon(int tab_id) const { | 198 gfx::Image ExtensionAction::GetIcon(int tab_id, |
198 return GetValue(&icon_, tab_id); | 199 const PathToIconCache& cache) const { |
| 200 // Check if a specific icon is set for this tab. |
| 201 gfx::Image icon = GetValue(&icon_, tab_id); |
| 202 if (icon.IsEmpty()) { |
| 203 // Need to find an icon from a path. |
| 204 const std::string* path = NULL; |
| 205 // Check if one of the elements of icon_path() was selected. |
| 206 int icon_index = GetIconIndex(tab_id); |
| 207 if (icon_index >= 0) { |
| 208 path = &icon_paths()->at(icon_index); |
| 209 } else { |
| 210 // Otherwise, use the default icon. |
| 211 path = &default_icon_path(); |
| 212 } |
| 213 |
| 214 PathToIconCache::const_iterator cached_icon = cache.find(*path); |
| 215 if (cached_icon != cache.end()) { |
| 216 icon = cached_icon->second; |
| 217 } else { |
| 218 icon = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 219 IDR_EXTENSIONS_FAVICON); |
| 220 } |
| 221 } |
| 222 |
| 223 return ApplyIconAnimation(tab_id, icon); |
199 } | 224 } |
200 | 225 |
201 void ExtensionAction::SetIconIndex(int tab_id, int index) { | 226 void ExtensionAction::SetIconIndex(int tab_id, int index) { |
202 if (static_cast<size_t>(index) >= icon_paths_.size()) { | 227 if (static_cast<size_t>(index) >= icon_paths_.size()) { |
203 NOTREACHED(); | 228 NOTREACHED(); |
204 return; | 229 return; |
205 } | 230 } |
206 SetValue(&icon_index_, tab_id, index); | 231 SetValue(&icon_index_, tab_id, index); |
207 } | 232 } |
208 | 233 |
209 void ExtensionAction::ClearAllValuesForTab(int tab_id) { | 234 void ExtensionAction::ClearAllValuesForTab(int tab_id) { |
210 popup_url_.erase(tab_id); | 235 popup_url_.erase(tab_id); |
211 title_.erase(tab_id); | 236 title_.erase(tab_id); |
212 icon_.erase(tab_id); | 237 icon_.erase(tab_id); |
213 icon_index_.erase(tab_id); | 238 icon_index_.erase(tab_id); |
214 badge_text_.erase(tab_id); | 239 badge_text_.erase(tab_id); |
215 badge_text_color_.erase(tab_id); | 240 badge_text_color_.erase(tab_id); |
216 badge_background_color_.erase(tab_id); | 241 badge_background_color_.erase(tab_id); |
217 visible_.erase(tab_id); | 242 visible_.erase(tab_id); |
218 icon_animation_.erase(tab_id); | 243 icon_animation_.erase(tab_id); |
219 } | 244 } |
220 | 245 |
| 246 namespace { |
| 247 int Width(const gfx::Image& image) { |
| 248 if (image.IsEmpty()) |
| 249 return 0; |
| 250 return image.ToSkBitmap()->width(); |
| 251 } |
| 252 } // namespace |
| 253 |
221 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, | 254 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, |
222 const gfx::Rect& bounds, | 255 const gfx::Rect& bounds, |
223 int tab_id) { | 256 int tab_id) { |
224 std::string text = GetBadgeText(tab_id); | 257 std::string text = GetBadgeText(tab_id); |
225 if (text.empty()) | 258 if (text.empty()) |
226 return; | 259 return; |
227 | 260 |
228 SkColor text_color = GetBadgeTextColor(tab_id); | 261 SkColor text_color = GetBadgeTextColor(tab_id); |
229 SkColor background_color = GetBadgeBackgroundColor(tab_id); | 262 SkColor background_color = GetBadgeBackgroundColor(tab_id); |
230 | 263 |
(...skipping 10 matching lines...) Expand all Loading... |
241 text_paint->setColor(text_color); | 274 text_paint->setColor(text_color); |
242 | 275 |
243 // Calculate text width. We clamp it to a max size. | 276 // Calculate text width. We clamp it to a max size. |
244 SkScalar text_width = text_paint->measureText(text.c_str(), text.size()); | 277 SkScalar text_width = text_paint->measureText(text.c_str(), text.size()); |
245 text_width = SkIntToScalar( | 278 text_width = SkIntToScalar( |
246 std::min(kMaxTextWidth, SkScalarFloor(text_width))); | 279 std::min(kMaxTextWidth, SkScalarFloor(text_width))); |
247 | 280 |
248 // Calculate badge size. It is clamped to a min width just because it looks | 281 // Calculate badge size. It is clamped to a min width just because it looks |
249 // silly if it is too skinny. | 282 // silly if it is too skinny. |
250 int badge_width = SkScalarFloor(text_width) + kPadding * 2; | 283 int badge_width = SkScalarFloor(text_width) + kPadding * 2; |
251 int icon_width = GetIcon(tab_id).width(); | 284 int icon_width = Width(GetValue(&icon_, tab_id)); |
252 // Force the pixel width of badge to be either odd (if the icon width is odd) | 285 // Force the pixel width of badge to be either odd (if the icon width is odd) |
253 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. | 286 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. |
254 if (icon_width != 0 && (badge_width % 2 != GetIcon(tab_id).width() % 2)) | 287 if (icon_width != 0 && (badge_width % 2 != icon_width % 2)) |
255 badge_width += 1; | 288 badge_width += 1; |
256 badge_width = std::max(kBadgeHeight, badge_width); | 289 badge_width = std::max(kBadgeHeight, badge_width); |
257 | 290 |
258 // Paint the badge background color in the right location. It is usually | 291 // Paint the badge background color in the right location. It is usually |
259 // right-aligned, but it can also be center-aligned if it is large. | 292 // right-aligned, but it can also be center-aligned if it is large. |
260 SkRect rect; | 293 SkRect rect; |
261 rect.fBottom = SkIntToScalar(bounds.bottom() - kBottomMargin); | 294 rect.fBottom = SkIntToScalar(bounds.bottom() - kBottomMargin); |
262 rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); | 295 rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); |
263 if (badge_width >= kCenterAlignThreshold) { | 296 if (badge_width >= kCenterAlignThreshold) { |
264 rect.fLeft = SkIntToScalar( | 297 rect.fLeft = SkIntToScalar( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 } | 341 } |
309 | 342 |
310 base::WeakPtr<ExtensionAction::IconAnimation> ExtensionAction::GetIconAnimation( | 343 base::WeakPtr<ExtensionAction::IconAnimation> ExtensionAction::GetIconAnimation( |
311 int tab_id) const { | 344 int tab_id) const { |
312 std::map<int, linked_ptr<IconAnimationWrapper> >::const_iterator it = | 345 std::map<int, linked_ptr<IconAnimationWrapper> >::const_iterator it = |
313 icon_animation_.find(tab_id); | 346 icon_animation_.find(tab_id); |
314 return (it != icon_animation_.end()) ? it->second->animation()->AsWeakPtr() | 347 return (it != icon_animation_.end()) ? it->second->animation()->AsWeakPtr() |
315 : base::WeakPtr<IconAnimation>(); | 348 : base::WeakPtr<IconAnimation>(); |
316 } | 349 } |
317 | 350 |
| 351 gfx::Image ExtensionAction::ApplyIconAnimation(int tab_id, |
| 352 const gfx::Image& orig) const { |
| 353 std::map<int, linked_ptr<IconAnimationWrapper> >::const_iterator it = |
| 354 icon_animation_.find(tab_id); |
| 355 if (it == icon_animation_.end()) |
| 356 return orig; |
| 357 return gfx::Image(it->second->animation()->Apply(*orig.ToSkBitmap())); |
| 358 } |
| 359 |
318 void ExtensionAction::RunIconAnimation(int tab_id) { | 360 void ExtensionAction::RunIconAnimation(int tab_id) { |
319 IconAnimationWrapper* icon_animation = | 361 IconAnimationWrapper* icon_animation = |
320 new IconAnimationWrapper(this, tab_id); | 362 new IconAnimationWrapper(this, tab_id); |
321 icon_animation_[tab_id] = make_linked_ptr(icon_animation); | 363 icon_animation_[tab_id] = make_linked_ptr(icon_animation); |
322 icon_animation->animation()->Start(); | 364 icon_animation->animation()->Start(); |
323 } | 365 } |
OLD | NEW |