Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1418)

Side by Side Diff: chrome/browser/ui/cocoa/extensions/browser_action_button.mm

Issue 10806058: Move icon fallbacks into ExtensionAction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move the icon cache inside ExtensionAction. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" 5 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 } // anonymous namespace 43 } // anonymous namespace
44 44
45 // A helper class to bridge the asynchronous Skia bitmap loading mechanism to 45 // A helper class to bridge the asynchronous Skia bitmap loading mechanism to
46 // the extension's button. 46 // the extension's button.
47 class ExtensionImageTrackerBridge : public content::NotificationObserver, 47 class ExtensionImageTrackerBridge : public content::NotificationObserver,
48 public ImageLoadingTracker::Observer { 48 public ImageLoadingTracker::Observer {
49 public: 49 public:
50 ExtensionImageTrackerBridge(BrowserActionButton* owner, 50 ExtensionImageTrackerBridge(BrowserActionButton* owner,
51 const Extension* extension) 51 const Extension* extension)
52 : owner_(owner), 52 : owner_(owner),
53 tracker_(this) { 53 tracker_(this),
54 browser_action_(extension->browser_action()) {
54 // The Browser Action API does not allow the default icon path to be 55 // The Browser Action API does not allow the default icon path to be
55 // changed at runtime, so we can load this now and cache it. 56 // changed at runtime, so we can load this now and cache it.
56 std::string path = extension->browser_action()->default_icon_path(); 57 std::string path = extension->browser_action()->default_icon_path();
57 if (!path.empty()) { 58 if (!path.empty()) {
58 tracker_.LoadImage(extension, extension->GetResource(path), 59 tracker_.LoadImage(extension, extension->GetResource(path),
59 gfx::Size(Extension::kBrowserActionIconMaxSize, 60 gfx::Size(Extension::kBrowserActionIconMaxSize,
60 Extension::kBrowserActionIconMaxSize), 61 Extension::kBrowserActionIconMaxSize),
61 ImageLoadingTracker::DONT_CACHE); 62 ImageLoadingTracker::DONT_CACHE);
62 } else {
63 // Set the icon to be the default extensions icon.
64 SkBitmap bm = *ResourceBundle::GetSharedInstance().GetBitmapNamed(
65 IDR_EXTENSIONS_FAVICON);
66 [owner_ setDefaultIcon:gfx::SkBitmapToNSImage(bm)];
67 [owner_ updateState];
68 } 63 }
69 registrar_.Add( 64 registrar_.Add(
70 this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, 65 this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
71 content::Source<ExtensionAction>(extension->browser_action())); 66 content::Source<ExtensionAction>(browser_action_));
72 } 67 }
73 68
74 ~ExtensionImageTrackerBridge() {} 69 ~ExtensionImageTrackerBridge() {}
75 70
76 // ImageLoadingTracker::Observer implementation. 71 // ImageLoadingTracker::Observer implementation.
77 void OnImageLoaded(const gfx::Image& image, 72 void OnImageLoaded(const gfx::Image& image,
78 const std::string& extension_id, 73 const std::string& extension_id,
79 int index) OVERRIDE { 74 int index) OVERRIDE {
80 if (!image.IsEmpty()) 75 browser_action_->CacheIcon(browser_action_->default_icon_path(), image);
81 [owner_ setDefaultIcon:image.ToNSImage()];
82 [owner_ updateState]; 76 [owner_ updateState];
83 } 77 }
84 78
85 // Overridden from content::NotificationObserver. 79 // Overridden from content::NotificationObserver.
86 void Observe(int type, 80 void Observe(int type,
87 const content::NotificationSource& source, 81 const content::NotificationSource& source,
88 const content::NotificationDetails& details) { 82 const content::NotificationDetails& details) {
89 if (type == chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED) 83 if (type == chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED)
90 [owner_ updateState]; 84 [owner_ updateState];
91 else 85 else
92 NOTREACHED(); 86 NOTREACHED();
93 } 87 }
94 88
95 private: 89 private:
96 // Weak. Owns us. 90 // Weak. Owns us.
97 BrowserActionButton* owner_; 91 BrowserActionButton* owner_;
98 92
99 // Loads the button's icons for us on the file thread. 93 // Loads the button's icons for us on the file thread.
100 ImageLoadingTracker tracker_; 94 ImageLoadingTracker tracker_;
101 95
96 // The browser action whose images we're loading.
97 ExtensionAction* const browser_action_;
98
102 // Used for registering to receive notifications and automatic clean up. 99 // Used for registering to receive notifications and automatic clean up.
103 content::NotificationRegistrar registrar_; 100 content::NotificationRegistrar registrar_;
104 101
105 DISALLOW_COPY_AND_ASSIGN(ExtensionImageTrackerBridge); 102 DISALLOW_COPY_AND_ASSIGN(ExtensionImageTrackerBridge);
106 }; 103 };
107 104
108 @interface BrowserActionCell (Internals) 105 @interface BrowserActionCell (Internals)
109 - (void)drawBadgeWithinFrame:(NSRect)frame; 106 - (void)drawBadgeWithinFrame:(NSRect)frame;
110 @end 107 @end
111 108
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 self, NSViewAnimationTargetKey, 231 self, NSViewAnimationTargetKey,
235 [NSValue valueWithRect:[self frame]], NSViewAnimationStartFrameKey, 232 [NSValue valueWithRect:[self frame]], NSViewAnimationStartFrameKey,
236 [NSValue valueWithRect:frameRect], NSViewAnimationEndFrameKey, 233 [NSValue valueWithRect:frameRect], NSViewAnimationEndFrameKey,
237 nil]; 234 nil];
238 [moveAnimation_ setViewAnimations: 235 [moveAnimation_ setViewAnimations:
239 [NSArray arrayWithObject:animationDictionary]]; 236 [NSArray arrayWithObject:animationDictionary]];
240 [moveAnimation_ startAnimation]; 237 [moveAnimation_ startAnimation];
241 } 238 }
242 } 239 }
243 240
244 - (void)setDefaultIcon:(NSImage*)image {
245 defaultIcon_.reset([image retain]);
246 }
247
248 - (void)setTabSpecificIcon:(NSImage*)image {
249 tabSpecificIcon_.reset([image retain]);
250 }
251
252 - (void)updateState { 241 - (void)updateState {
253 if (tabId_ < 0) 242 if (tabId_ < 0)
254 return; 243 return;
255 244
256 std::string tooltip = extension_->browser_action()->GetTitle(tabId_); 245 std::string tooltip = extension_->browser_action()->GetTitle(tabId_);
257 if (tooltip.empty()) { 246 if (tooltip.empty()) {
258 [self setToolTip:nil]; 247 [self setToolTip:nil];
259 } else { 248 } else {
260 [self setToolTip:base::SysUTF8ToNSString(tooltip)]; 249 [self setToolTip:base::SysUTF8ToNSString(tooltip)];
261 } 250 }
262 251
263 SkBitmap image = extension_->browser_action()->GetIcon(tabId_); 252 gfx::Image image = extension_->browser_action()->GetIcon(tabId_);
264 if (!image.isNull()) { 253 if (!image.IsEmpty())
265 [self setTabSpecificIcon:gfx::SkBitmapToNSImage(image)]; 254 [self setImage:image.ToNSImage()];
266 [self setImage:tabSpecificIcon_];
267 } else if (defaultIcon_) {
268 [self setImage:defaultIcon_];
269 }
270 255
271 [[self cell] setTabId:tabId_]; 256 [[self cell] setTabId:tabId_];
272 257
273 bool enabled = extension_->browser_action()->GetIsVisible(tabId_); 258 bool enabled = extension_->browser_action()->GetIsVisible(tabId_);
274 [self setEnabled:enabled ? YES : NO]; 259 [self setEnabled:enabled ? YES : NO];
275 260
276 [self setNeedsDisplay:YES]; 261 [self setNeedsDisplay:YES];
277 } 262 }
278 263
279 - (BOOL)isAnimating { 264 - (BOOL)isAnimating {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 } 307 }
323 308
324 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { 309 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
325 gfx::ScopedNSGraphicsContextSaveGState scopedGState; 310 gfx::ScopedNSGraphicsContextSaveGState scopedGState;
326 [super drawInteriorWithFrame:cellFrame inView:controlView]; 311 [super drawInteriorWithFrame:cellFrame inView:controlView];
327 cellFrame.origin.y += kBrowserActionBadgeOriginYOffset; 312 cellFrame.origin.y += kBrowserActionBadgeOriginYOffset;
328 [self drawBadgeWithinFrame:cellFrame]; 313 [self drawBadgeWithinFrame:cellFrame];
329 } 314 }
330 315
331 @end 316 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698