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