| 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 #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" |
| 11 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
| 12 #include "chrome/browser/extensions/image_loading_tracker.h" | 12 #include "chrome/browser/extensions/extension_action_icon_factory.h" |
| 13 #include "chrome/browser/ui/cocoa/extensions/extension_action_context_menu.h" | 13 #include "chrome/browser/ui/cocoa/extensions/extension_action_context_menu.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_action.h" | 16 #include "chrome/common/extensions/extension_action.h" |
| 17 #include "chrome/common/extensions/extension_resource.h" | 17 #include "chrome/common/extensions/extension_resource.h" |
| 18 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 20 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
| 21 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
| 22 #include "skia/ext/skia_utils_mac.h" | 22 #include "skia/ext/skia_utils_mac.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 33 NSString* const kBrowserActionButtonDraggingNotification = | 33 NSString* const kBrowserActionButtonDraggingNotification = |
| 34 @"BrowserActionButtonDraggingNotification"; | 34 @"BrowserActionButtonDraggingNotification"; |
| 35 NSString* const kBrowserActionButtonDragEndNotification = | 35 NSString* const kBrowserActionButtonDragEndNotification = |
| 36 @"BrowserActionButtonDragEndNotification"; | 36 @"BrowserActionButtonDragEndNotification"; |
| 37 | 37 |
| 38 const CGFloat kBrowserActionBadgeOriginYOffset = 5; | 38 const CGFloat kBrowserActionBadgeOriginYOffset = 5; |
| 39 const CGFloat kAnimationDuration = 0.2; | 39 const CGFloat kAnimationDuration = 0.2; |
| 40 | 40 |
| 41 // A helper class to bridge the asynchronous Skia bitmap loading mechanism to | 41 // A helper class to bridge the asynchronous Skia bitmap loading mechanism to |
| 42 // the extension's button. | 42 // the extension's button. |
| 43 class ExtensionImageTrackerBridge : public content::NotificationObserver, | 43 class ExtensionActionIconFactoryBridge |
| 44 public ImageLoadingTracker::Observer { | 44 : public content::NotificationObserver, |
| 45 public ExtensionActionIconFactory::Observer { |
| 45 public: | 46 public: |
| 46 ExtensionImageTrackerBridge(BrowserActionButton* owner, | 47 ExtensionActionIconFactoryBridge(BrowserActionButton* owner, |
| 47 const Extension* extension) | 48 const Extension* extension) |
| 48 : owner_(owner), | 49 : owner_(owner), |
| 49 tracker_(this), | 50 icon_factory_(extension, extension->browser_action(), this), |
| 50 browser_action_(extension->browser_action()) { | 51 browser_action_(extension->browser_action()) { |
| 51 // The Browser Action API does not allow the default icon path to be | |
| 52 // changed at runtime, so we can load this now and cache it. | |
| 53 std::string path = extension->browser_action()->default_icon_path(); | |
| 54 if (!path.empty()) { | |
| 55 tracker_.LoadImage(extension, extension->GetResource(path), | |
| 56 gfx::Size(Extension::kBrowserActionIconMaxSize, | |
| 57 Extension::kBrowserActionIconMaxSize), | |
| 58 ImageLoadingTracker::DONT_CACHE); | |
| 59 } | |
| 60 registrar_.Add( | 52 registrar_.Add( |
| 61 this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 53 this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
| 62 content::Source<ExtensionAction>(browser_action_)); | 54 content::Source<ExtensionAction>(browser_action_)); |
| 63 } | 55 } |
| 64 | 56 |
| 65 ~ExtensionImageTrackerBridge() {} | 57 virtual ~ExtensionActionIconFactoryBridge() {} |
| 66 | 58 |
| 67 // ImageLoadingTracker::Observer implementation. | 59 // ImageLoadingTracker::Observer implementation. |
| 68 void OnImageLoaded(const gfx::Image& image, | 60 void OnIconUpdated() OVERRIDE { |
| 69 const std::string& extension_id, | |
| 70 int index) OVERRIDE { | |
| 71 browser_action_->CacheIcon(image); | |
| 72 [owner_ updateState]; | 61 [owner_ updateState]; |
| 73 } | 62 } |
| 74 | 63 |
| 75 // Overridden from content::NotificationObserver. | 64 // Overridden from content::NotificationObserver. |
| 76 void Observe(int type, | 65 void Observe(int type, |
| 77 const content::NotificationSource& source, | 66 const content::NotificationSource& source, |
| 78 const content::NotificationDetails& details) { | 67 const content::NotificationDetails& details) { |
| 79 if (type == chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED) | 68 if (type == chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED) |
| 80 [owner_ updateState]; | 69 [owner_ updateState]; |
| 81 else | 70 else |
| 82 NOTREACHED(); | 71 NOTREACHED(); |
| 83 } | 72 } |
| 84 | 73 |
| 74 gfx::Image GetIcon(int tabId) { |
| 75 return icon_factory_.GetIcon(tabId); |
| 76 } |
| 77 |
| 85 private: | 78 private: |
| 86 // Weak. Owns us. | 79 // Weak. Owns us. |
| 87 BrowserActionButton* owner_; | 80 BrowserActionButton* owner_; |
| 88 | 81 |
| 89 // Loads the button's icons for us on the file thread. | 82 // The object that will be used to get the browser action icon for us. |
| 90 ImageLoadingTracker tracker_; | 83 // It may load the icon asynchronously (in which case the initial icon |
| 84 // returned by the factory will be transparent), so we have to observe it for |
| 85 // updates to the icon. |
| 86 ExtensionActionIconFactory icon_factory_; |
| 91 | 87 |
| 92 // The browser action whose images we're loading. | 88 // The browser action whose images we're loading. |
| 93 ExtensionAction* const browser_action_; | 89 ExtensionAction* const browser_action_; |
| 94 | 90 |
| 95 // Used for registering to receive notifications and automatic clean up. | 91 // Used for registering to receive notifications and automatic clean up. |
| 96 content::NotificationRegistrar registrar_; | 92 content::NotificationRegistrar registrar_; |
| 97 | 93 |
| 98 DISALLOW_COPY_AND_ASSIGN(ExtensionImageTrackerBridge); | 94 DISALLOW_COPY_AND_ASSIGN(ExtensionActionIconFactoryBridge); |
| 99 }; | 95 }; |
| 100 | 96 |
| 101 @interface BrowserActionCell (Internals) | 97 @interface BrowserActionCell (Internals) |
| 102 - (void)drawBadgeWithinFrame:(NSRect)frame; | 98 - (void)drawBadgeWithinFrame:(NSRect)frame; |
| 103 @end | 99 @end |
| 104 | 100 |
| 105 @interface BrowserActionButton (Private) | 101 @interface BrowserActionButton (Private) |
| 106 - (void)endDrag; | 102 - (void)endDrag; |
| 107 @end | 103 @end |
| 108 | 104 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 [self setButtonType:NSMomentaryChangeButton]; | 140 [self setButtonType:NSMomentaryChangeButton]; |
| 145 [self setShowsBorderOnlyWhileMouseInside:YES]; | 141 [self setShowsBorderOnlyWhileMouseInside:YES]; |
| 146 | 142 |
| 147 [self setMenu:[[[ExtensionActionContextMenu alloc] | 143 [self setMenu:[[[ExtensionActionContextMenu alloc] |
| 148 initWithExtension:extension | 144 initWithExtension:extension |
| 149 browser:browser | 145 browser:browser |
| 150 extensionAction:extension->browser_action()] autorelease]]; | 146 extensionAction:extension->browser_action()] autorelease]]; |
| 151 | 147 |
| 152 tabId_ = tabId; | 148 tabId_ = tabId; |
| 153 extension_ = extension; | 149 extension_ = extension; |
| 154 imageLoadingBridge_.reset(new ExtensionImageTrackerBridge(self, extension)); | 150 iconFactoryBridge_.reset( |
| 151 new ExtensionActionIconFactoryBridge(self, extension)); |
| 155 | 152 |
| 156 moveAnimation_.reset([[NSViewAnimation alloc] init]); | 153 moveAnimation_.reset([[NSViewAnimation alloc] init]); |
| 157 [moveAnimation_ gtm_setDuration:kAnimationDuration | 154 [moveAnimation_ gtm_setDuration:kAnimationDuration |
| 158 eventMask:NSLeftMouseUpMask]; | 155 eventMask:NSLeftMouseUpMask]; |
| 159 [moveAnimation_ setAnimationBlockingMode:NSAnimationNonblocking]; | 156 [moveAnimation_ setAnimationBlockingMode:NSAnimationNonblocking]; |
| 160 | 157 |
| 161 [self updateState]; | 158 [self updateState]; |
| 162 } | 159 } |
| 163 | 160 |
| 164 return self; | 161 return self; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 if (tabId_ < 0) | 240 if (tabId_ < 0) |
| 244 return; | 241 return; |
| 245 | 242 |
| 246 std::string tooltip = extension_->browser_action()->GetTitle(tabId_); | 243 std::string tooltip = extension_->browser_action()->GetTitle(tabId_); |
| 247 if (tooltip.empty()) { | 244 if (tooltip.empty()) { |
| 248 [self setToolTip:nil]; | 245 [self setToolTip:nil]; |
| 249 } else { | 246 } else { |
| 250 [self setToolTip:base::SysUTF8ToNSString(tooltip)]; | 247 [self setToolTip:base::SysUTF8ToNSString(tooltip)]; |
| 251 } | 248 } |
| 252 | 249 |
| 253 gfx::Image image = extension_->browser_action()->GetIcon(tabId_); | 250 gfx::Image image = iconFactoryBridge_->GetIcon(tabId_); |
| 251 |
| 254 if (!image.IsEmpty()) | 252 if (!image.IsEmpty()) |
| 255 [self setImage:image.ToNSImage()]; | 253 [self setImage:image.ToNSImage()]; |
| 256 | 254 |
| 257 [[self cell] setTabId:tabId_]; | 255 [[self cell] setTabId:tabId_]; |
| 258 | 256 |
| 259 bool enabled = extension_->browser_action()->GetIsVisible(tabId_); | 257 bool enabled = extension_->browser_action()->GetIsVisible(tabId_); |
| 260 [self setEnabled:enabled ? YES : NO]; | 258 [self setEnabled:enabled ? YES : NO]; |
| 261 | 259 |
| 262 [self setNeedsDisplay:YES]; | 260 [self setNeedsDisplay:YES]; |
| 263 } | 261 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 operation:NSCompositeSourceOver | 319 operation:NSCompositeSourceOver |
| 322 fraction:1.0 | 320 fraction:1.0 |
| 323 respectFlipped:YES | 321 respectFlipped:YES |
| 324 hints:nil]; | 322 hints:nil]; |
| 325 | 323 |
| 326 cellFrame.origin.y += kBrowserActionBadgeOriginYOffset; | 324 cellFrame.origin.y += kBrowserActionBadgeOriginYOffset; |
| 327 [self drawBadgeWithinFrame:cellFrame]; | 325 [self drawBadgeWithinFrame:cellFrame]; |
| 328 } | 326 } |
| 329 | 327 |
| 330 @end | 328 @end |
| OLD | NEW |