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 { |
not at google - send to devlin
2012/07/26 02:29:44
strange to pass in the cache then not use it somet
Jeffrey Yasskin
2012/07/26 21:11:47
Are you referring to the fact that if an icon is s
not at google - send to devlin
2012/07/27 03:16:12
Of course. That's not what I meant; the caching sh
Jeffrey Yasskin
2012/07/31 14:40:01
Done, I think.
| |
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 static int Width(const gfx::Image& image) { | |
247 if (image.IsEmpty()) | |
248 return 0; | |
249 return image.ToSkBitmap()->width(); | |
250 } | |
not at google - send to devlin
2012/07/26 02:29:44
anonymous namespaces preferred over static methods
Jeffrey Yasskin
2012/07/26 21:11:47
Sure, done. Is there any practical effect beyond s
not at google - send to devlin
2012/07/27 03:16:12
No idea. Reviewer once made me do it, said somethi
| |
251 | |
221 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, | 252 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, |
222 const gfx::Rect& bounds, | 253 const gfx::Rect& bounds, |
223 int tab_id) { | 254 int tab_id) { |
224 std::string text = GetBadgeText(tab_id); | 255 std::string text = GetBadgeText(tab_id); |
225 if (text.empty()) | 256 if (text.empty()) |
226 return; | 257 return; |
227 | 258 |
228 SkColor text_color = GetBadgeTextColor(tab_id); | 259 SkColor text_color = GetBadgeTextColor(tab_id); |
229 SkColor background_color = GetBadgeBackgroundColor(tab_id); | 260 SkColor background_color = GetBadgeBackgroundColor(tab_id); |
230 | 261 |
(...skipping 10 matching lines...) Expand all Loading... | |
241 text_paint->setColor(text_color); | 272 text_paint->setColor(text_color); |
242 | 273 |
243 // Calculate text width. We clamp it to a max size. | 274 // Calculate text width. We clamp it to a max size. |
244 SkScalar text_width = text_paint->measureText(text.c_str(), text.size()); | 275 SkScalar text_width = text_paint->measureText(text.c_str(), text.size()); |
245 text_width = SkIntToScalar( | 276 text_width = SkIntToScalar( |
246 std::min(kMaxTextWidth, SkScalarFloor(text_width))); | 277 std::min(kMaxTextWidth, SkScalarFloor(text_width))); |
247 | 278 |
248 // Calculate badge size. It is clamped to a min width just because it looks | 279 // Calculate badge size. It is clamped to a min width just because it looks |
249 // silly if it is too skinny. | 280 // silly if it is too skinny. |
250 int badge_width = SkScalarFloor(text_width) + kPadding * 2; | 281 int badge_width = SkScalarFloor(text_width) + kPadding * 2; |
251 int icon_width = GetIcon(tab_id).width(); | 282 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) | 283 // 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. | 284 // 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)) | 285 if (icon_width != 0 && (badge_width % 2 != icon_width % 2)) |
255 badge_width += 1; | 286 badge_width += 1; |
256 badge_width = std::max(kBadgeHeight, badge_width); | 287 badge_width = std::max(kBadgeHeight, badge_width); |
257 | 288 |
258 // Paint the badge background color in the right location. It is usually | 289 // 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. | 290 // right-aligned, but it can also be center-aligned if it is large. |
260 SkRect rect; | 291 SkRect rect; |
261 rect.fBottom = SkIntToScalar(bounds.bottom() - kBottomMargin); | 292 rect.fBottom = SkIntToScalar(bounds.bottom() - kBottomMargin); |
262 rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); | 293 rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); |
263 if (badge_width >= kCenterAlignThreshold) { | 294 if (badge_width >= kCenterAlignThreshold) { |
264 rect.fLeft = SkIntToScalar( | 295 rect.fLeft = SkIntToScalar( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 } | 339 } |
309 | 340 |
310 base::WeakPtr<ExtensionAction::IconAnimation> ExtensionAction::GetIconAnimation( | 341 base::WeakPtr<ExtensionAction::IconAnimation> ExtensionAction::GetIconAnimation( |
311 int tab_id) const { | 342 int tab_id) const { |
312 std::map<int, linked_ptr<IconAnimationWrapper> >::const_iterator it = | 343 std::map<int, linked_ptr<IconAnimationWrapper> >::const_iterator it = |
313 icon_animation_.find(tab_id); | 344 icon_animation_.find(tab_id); |
314 return (it != icon_animation_.end()) ? it->second->animation()->AsWeakPtr() | 345 return (it != icon_animation_.end()) ? it->second->animation()->AsWeakPtr() |
315 : base::WeakPtr<IconAnimation>(); | 346 : base::WeakPtr<IconAnimation>(); |
316 } | 347 } |
317 | 348 |
349 gfx::Image ExtensionAction::ApplyIconAnimation(int tab_id, | |
350 const gfx::Image& orig) const { | |
351 std::map<int, linked_ptr<IconAnimationWrapper> >::const_iterator it = | |
352 icon_animation_.find(tab_id); | |
353 if (it == icon_animation_.end()) | |
354 return orig; | |
355 return gfx::Image(it->second->animation()->Apply(*orig.ToSkBitmap())); | |
356 } | |
357 | |
318 void ExtensionAction::RunIconAnimation(int tab_id) { | 358 void ExtensionAction::RunIconAnimation(int tab_id) { |
319 IconAnimationWrapper* icon_animation = | 359 IconAnimationWrapper* icon_animation = |
320 new IconAnimationWrapper(this, tab_id); | 360 new IconAnimationWrapper(this, tab_id); |
321 icon_animation_[tab_id] = make_linked_ptr(icon_animation); | 361 icon_animation_[tab_id] = make_linked_ptr(icon_animation); |
322 icon_animation->animation()->Start(); | 362 icon_animation->animation()->Start(); |
323 } | 363 } |
OLD | NEW |