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 ~ExtensionActionIconFactoryBridge() {} |
Robert Sesek
2012/08/30 19:08:21
Should be virtual (yikes that it wasn't before!)
tbarzic
2012/08/30 20:48:40
Done.
| |
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 ExtensionActionIconFactory* icon_factory() { return &icon_factory_; } | |
Robert Sesek
2012/08/30 19:08:21
const method
tbarzic
2012/08/30 20:48:40
the method is not really const (since it returns n
| |
79 | |
89 private: | 80 private: |
90 // Weak. Owns us. | 81 // Weak. Owns us. |
91 BrowserActionButton* owner_; | 82 BrowserActionButton* owner_; |
92 | 83 |
93 // Loads the button's icons for us on the file thread. | 84 // The object that browser action will use to create icon for us. |
94 ImageLoadingTracker tracker_; | 85 // It may load icon asynchronously (in which case initial icon returned by |
86 // the action will be blank), so we have to observe it for icon's updates. | |
87 ExtensionActionIconFactory icon_factory_; | |
95 | 88 |
96 // The browser action whose images we're loading. | 89 // The browser action whose images we're loading. |
97 ExtensionAction* const browser_action_; | 90 ExtensionAction* const browser_action_; |
98 | 91 |
99 // Used for registering to receive notifications and automatic clean up. | 92 // Used for registering to receive notifications and automatic clean up. |
100 content::NotificationRegistrar registrar_; | 93 content::NotificationRegistrar registrar_; |
101 | 94 |
102 DISALLOW_COPY_AND_ASSIGN(ExtensionImageTrackerBridge); | 95 DISALLOW_COPY_AND_ASSIGN(ExtensionActionIconFactoryBridge); |
103 }; | 96 }; |
104 | 97 |
105 @interface BrowserActionCell (Internals) | 98 @interface BrowserActionCell (Internals) |
106 - (void)drawBadgeWithinFrame:(NSRect)frame; | 99 - (void)drawBadgeWithinFrame:(NSRect)frame; |
107 @end | 100 @end |
108 | 101 |
109 @interface BrowserActionButton (Private) | 102 @interface BrowserActionButton (Private) |
110 - (void)endDrag; | 103 - (void)endDrag; |
111 @end | 104 @end |
112 | 105 |
(...skipping 29 matching lines...) Expand all Loading... | |
142 [self setButtonType:NSMomentaryChangeButton]; | 135 [self setButtonType:NSMomentaryChangeButton]; |
143 [self setShowsBorderOnlyWhileMouseInside:YES]; | 136 [self setShowsBorderOnlyWhileMouseInside:YES]; |
144 | 137 |
145 [self setMenu:[[[ExtensionActionContextMenu alloc] | 138 [self setMenu:[[[ExtensionActionContextMenu alloc] |
146 initWithExtension:extension | 139 initWithExtension:extension |
147 browser:browser | 140 browser:browser |
148 extensionAction:extension->browser_action()] autorelease]]; | 141 extensionAction:extension->browser_action()] autorelease]]; |
149 | 142 |
150 tabId_ = tabId; | 143 tabId_ = tabId; |
151 extension_ = extension; | 144 extension_ = extension; |
152 imageLoadingBridge_.reset(new ExtensionImageTrackerBridge(self, extension)); | 145 iconFactoryBridge_.reset( |
146 new ExtensionActionIconFactoryBridge(self, extension)); | |
153 | 147 |
154 moveAnimation_.reset([[NSViewAnimation alloc] init]); | 148 moveAnimation_.reset([[NSViewAnimation alloc] init]); |
155 [moveAnimation_ gtm_setDuration:kAnimationDuration | 149 [moveAnimation_ gtm_setDuration:kAnimationDuration |
156 eventMask:NSLeftMouseUpMask]; | 150 eventMask:NSLeftMouseUpMask]; |
157 [moveAnimation_ setAnimationBlockingMode:NSAnimationNonblocking]; | 151 [moveAnimation_ setAnimationBlockingMode:NSAnimationNonblocking]; |
158 | 152 |
159 [self updateState]; | 153 [self updateState]; |
160 } | 154 } |
161 | 155 |
162 return self; | 156 return self; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 if (tabId_ < 0) | 236 if (tabId_ < 0) |
243 return; | 237 return; |
244 | 238 |
245 std::string tooltip = extension_->browser_action()->GetTitle(tabId_); | 239 std::string tooltip = extension_->browser_action()->GetTitle(tabId_); |
246 if (tooltip.empty()) { | 240 if (tooltip.empty()) { |
247 [self setToolTip:nil]; | 241 [self setToolTip:nil]; |
248 } else { | 242 } else { |
249 [self setToolTip:base::SysUTF8ToNSString(tooltip)]; | 243 [self setToolTip:base::SysUTF8ToNSString(tooltip)]; |
250 } | 244 } |
251 | 245 |
252 gfx::Image image = extension_->browser_action()->GetIcon(tabId_); | 246 gfx::Image image = |
247 extension_->browser_action()->GetIcon(tabId_, | |
248 iconFactoryBridge_->icon_factory()); | |
253 if (!image.IsEmpty()) | 249 if (!image.IsEmpty()) |
254 [self setImage:image.ToNSImage()]; | 250 [self setImage:image.ToNSImage()]; |
255 | 251 |
256 [[self cell] setTabId:tabId_]; | 252 [[self cell] setTabId:tabId_]; |
257 | 253 |
258 bool enabled = extension_->browser_action()->GetIsVisible(tabId_); | 254 bool enabled = extension_->browser_action()->GetIsVisible(tabId_); |
259 [self setEnabled:enabled ? YES : NO]; | 255 [self setEnabled:enabled ? YES : NO]; |
260 | 256 |
261 [self setNeedsDisplay:YES]; | 257 [self setNeedsDisplay:YES]; |
262 } | 258 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 } | 303 } |
308 | 304 |
309 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 305 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
310 gfx::ScopedNSGraphicsContextSaveGState scopedGState; | 306 gfx::ScopedNSGraphicsContextSaveGState scopedGState; |
311 [super drawInteriorWithFrame:cellFrame inView:controlView]; | 307 [super drawInteriorWithFrame:cellFrame inView:controlView]; |
312 cellFrame.origin.y += kBrowserActionBadgeOriginYOffset; | 308 cellFrame.origin.y += kBrowserActionBadgeOriginYOffset; |
313 [self drawBadgeWithinFrame:cellFrame]; | 309 [self drawBadgeWithinFrame:cellFrame]; |
314 } | 310 } |
315 | 311 |
316 @end | 312 @end |
OLD | NEW |