OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "browser_actions_controller.h" | 5 #import "browser_actions_controller.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
10 #include "chrome/browser/browser.h" | 10 #include "chrome/browser/browser.h" |
11 #include "chrome/browser/cocoa/extensions/browser_action_button.h" | 11 #include "chrome/browser/cocoa/extensions/browser_action_button.h" |
12 #include "chrome/browser/cocoa/extensions/browser_actions_container_view.h" | 12 #include "chrome/browser/cocoa/extensions/browser_actions_container_view.h" |
13 #include "chrome/browser/cocoa/extensions/extension_popup_controller.h" | 13 #include "chrome/browser/cocoa/extensions/extension_popup_controller.h" |
14 #include "chrome/browser/extensions/extension_browser_event_router.h" | 14 #include "chrome/browser/extensions/extension_browser_event_router.h" |
15 #include "chrome/browser/extensions/extension_toolbar_model.h" | 15 #include "chrome/browser/extensions/extension_toolbar_model.h" |
16 #include "chrome/browser/extensions/extensions_service.h" | 16 #include "chrome/browser/extensions/extensions_service.h" |
17 #include "chrome/browser/profile.h" | 17 #include "chrome/browser/profile.h" |
18 #include "chrome/browser/tab_contents/tab_contents.h" | 18 #include "chrome/browser/tab_contents/tab_contents.h" |
19 #include "chrome/common/notification_observer.h" | 19 #include "chrome/common/notification_observer.h" |
20 #include "chrome/common/notification_registrar.h" | 20 #include "chrome/common/notification_registrar.h" |
21 | 21 |
22 // The padding between browser action buttons. | 22 // The padding between browser action buttons. |
23 extern const CGFloat kBrowserActionButtonPadding = 3; | 23 extern const CGFloat kBrowserActionButtonPadding = 3; |
24 | 24 |
| 25 namespace { |
| 26 const CGFloat kContainerPadding = 2.0; |
| 27 const CGFloat kGrippyXOffset = 8.0; |
| 28 } // namespace |
| 29 |
25 NSString* const kBrowserActionsChangedNotification = @"BrowserActionsChanged"; | 30 NSString* const kBrowserActionsChangedNotification = @"BrowserActionsChanged"; |
26 | 31 |
27 @interface BrowserActionsController(Private) | 32 @interface BrowserActionsController(Private) |
28 - (void)createActionButtonForExtension:(Extension*)extension | 33 - (void)createActionButtonForExtension:(Extension*)extension |
29 withIndex:(int)index; | 34 withIndex:(int)index; |
30 - (void)removeActionButtonForExtension:(Extension*)extension; | 35 - (void)removeActionButtonForExtension:(Extension*)extension; |
31 - (void)repositionActionButtons; | 36 - (void)repositionActionButtons; |
32 - (int)currentTabId; | 37 - (int)currentTabId; |
33 - (bool)shouldDisplayBrowserAction:(Extension*)extension; | 38 - (bool)shouldDisplayBrowserAction:(Extension*)extension; |
34 @end | 39 @end |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 BrowserActionsController* owner_; | 80 BrowserActionsController* owner_; |
76 | 81 |
77 // Used for registering to receive notifications and automatic clean up. | 82 // Used for registering to receive notifications and automatic clean up. |
78 NotificationRegistrar registrar_; | 83 NotificationRegistrar registrar_; |
79 | 84 |
80 DISALLOW_COPY_AND_ASSIGN(ExtensionsServiceObserverBridge); | 85 DISALLOW_COPY_AND_ASSIGN(ExtensionsServiceObserverBridge); |
81 }; | 86 }; |
82 | 87 |
83 @implementation BrowserActionsController | 88 @implementation BrowserActionsController |
84 | 89 |
| 90 @synthesize containerView = containerView_; |
| 91 |
85 - (id)initWithBrowser:(Browser*)browser | 92 - (id)initWithBrowser:(Browser*)browser |
86 containerView:(BrowserActionsContainerView*)container { | 93 containerView:(BrowserActionsContainerView*)container { |
87 DCHECK(browser && container); | 94 DCHECK(browser && container); |
88 | 95 |
89 if ((self = [super init])) { | 96 if ((self = [super init])) { |
90 browser_ = browser; | 97 browser_ = browser; |
91 profile_ = browser->profile(); | 98 profile_ = browser->profile(); |
92 | 99 |
93 observer_.reset(new ExtensionsServiceObserverBridge(self, profile_)); | 100 observer_.reset(new ExtensionsServiceObserverBridge(self, profile_)); |
94 ExtensionsService* extensionsService = profile_->GetExtensionsService(); | 101 ExtensionsService* extensionsService = profile_->GetExtensionsService(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 if (!toolbarModel_) | 135 if (!toolbarModel_) |
129 return; | 136 return; |
130 | 137 |
131 int i = 0; | 138 int i = 0; |
132 for (ExtensionList::iterator iter = toolbarModel_->begin(); | 139 for (ExtensionList::iterator iter = toolbarModel_->begin(); |
133 iter != toolbarModel_->end(); ++iter) { | 140 iter != toolbarModel_->end(); ++iter) { |
134 [self createActionButtonForExtension:*iter withIndex:i++]; | 141 [self createActionButtonForExtension:*iter withIndex:i++]; |
135 } | 142 } |
136 } | 143 } |
137 | 144 |
| 145 - (CGFloat)idealContainerWidth { |
| 146 NSUInteger buttonCount = [self visibleButtonCount]; |
| 147 if (buttonCount == 0) |
| 148 return 0.0; |
| 149 |
| 150 return kGrippyXOffset + kContainerPadding + (buttonCount * |
| 151 (kBrowserActionWidth + kBrowserActionButtonPadding)); |
| 152 } |
| 153 |
138 - (void)createActionButtonForExtension:(Extension*)extension | 154 - (void)createActionButtonForExtension:(Extension*)extension |
139 withIndex:(int)index { | 155 withIndex:(int)index { |
140 if (!extension->browser_action()) | 156 if (!extension->browser_action()) |
141 return; | 157 return; |
142 | 158 |
143 if (![self shouldDisplayBrowserAction:extension]) | 159 if (![self shouldDisplayBrowserAction:extension]) |
144 return; | 160 return; |
145 | 161 |
146 // Show the container if it's the first button. Otherwise it will be shown | 162 // Show the container if it's the first button. Otherwise it will be shown |
147 // already. | 163 // already. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 } else { | 201 } else { |
186 [self repositionActionButtons]; | 202 [self repositionActionButtons]; |
187 } | 203 } |
188 [[NSNotificationCenter defaultCenter] | 204 [[NSNotificationCenter defaultCenter] |
189 postNotificationName:kBrowserActionsChangedNotification object:self]; | 205 postNotificationName:kBrowserActionsChangedNotification object:self]; |
190 [containerView_ setNeedsDisplay:YES]; | 206 [containerView_ setNeedsDisplay:YES]; |
191 } | 207 } |
192 | 208 |
193 - (void)repositionActionButtons { | 209 - (void)repositionActionButtons { |
194 for (NSUInteger i = 0; i < [buttonOrder_ count]; ++i) { | 210 for (NSUInteger i = 0; i < [buttonOrder_ count]; ++i) { |
195 CGFloat xOffset = i * (kBrowserActionWidth + kBrowserActionButtonPadding); | 211 CGFloat xOffset = kGrippyXOffset + |
| 212 (i * (kBrowserActionWidth + kBrowserActionButtonPadding)); |
196 BrowserActionButton* button = [buttonOrder_ objectAtIndex:i]; | 213 BrowserActionButton* button = [buttonOrder_ objectAtIndex:i]; |
197 NSRect buttonFrame = [button frame]; | 214 NSRect buttonFrame = [button frame]; |
198 buttonFrame.origin.x = xOffset; | 215 buttonFrame.origin.x = xOffset; |
199 [button setFrame:buttonFrame]; | 216 [button setFrame:buttonFrame]; |
200 } | 217 } |
201 } | 218 } |
202 | 219 |
203 - (int)buttonCount { | 220 - (NSUInteger)buttonCount { |
204 return [buttons_ count]; | 221 return [buttons_ count]; |
205 } | 222 } |
206 | 223 |
207 - (int)visibleButtonCount { | 224 - (NSUInteger)visibleButtonCount { |
208 int count = 0; | 225 int count = 0; |
209 for (BrowserActionButton* button in [buttons_ allValues]) { | 226 for (BrowserActionButton* button in [buttons_ allValues]) { |
210 if (![button isHidden]) | 227 if (![button isHidden]) |
211 ++count; | 228 ++count; |
212 } | 229 } |
213 return count; | 230 return count; |
214 } | 231 } |
215 | 232 |
216 - (void)browserActionClicked:(BrowserActionButton*)sender { | 233 - (void)browserActionClicked:(BrowserActionButton*)sender { |
217 int tabId = [self currentTabId]; | 234 int tabId = [self currentTabId]; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 return [buttonOrder_ objectAtIndex:(NSUInteger)index]; | 287 return [buttonOrder_ objectAtIndex:(NSUInteger)index]; |
271 } | 288 } |
272 | 289 |
273 - (bool)shouldDisplayBrowserAction:(Extension*)extension { | 290 - (bool)shouldDisplayBrowserAction:(Extension*)extension { |
274 return (!profile_->IsOffTheRecord() || | 291 return (!profile_->IsOffTheRecord() || |
275 profile_->GetExtensionsService()-> | 292 profile_->GetExtensionsService()-> |
276 IsIncognitoEnabled(extension->id())); | 293 IsIncognitoEnabled(extension->id())); |
277 } | 294 } |
278 | 295 |
279 @end | 296 @end |
OLD | NEW |