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

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

Issue 670463004: Make a platform-independent ToolbarActionViewController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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
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_actions_controller.h" 5 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <string> 8 #include <string>
9 9
10 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
11 #include "chrome/browser/chrome_notification_types.h" 11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/extension_action.h" 12 #include "chrome/browser/extensions/extension_action.h"
13 #include "chrome/browser/extensions/extension_action_manager.h" 13 #include "chrome/browser/extensions/extension_action_manager.h"
14 #include "chrome/browser/extensions/extension_toolbar_model.h" 14 #include "chrome/browser/extensions/extension_toolbar_model.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/sessions/session_tab_helper.h"
17 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_window.h" 17 #include "chrome/browser/ui/browser_window.h"
19 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" 18 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h"
20 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h" 19 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h"
20 #import "chrome/browser/ui/cocoa/extensions/extension_action_context_menu_contro ller.h"
21 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h" 21 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
22 #import "chrome/browser/ui/cocoa/image_button_cell.h" 22 #import "chrome/browser/ui/cocoa/image_button_cell.h"
23 #import "chrome/browser/ui/cocoa/menu_button.h" 23 #import "chrome/browser/ui/cocoa/menu_button.h"
24 #include "chrome/browser/ui/extensions/extension_action_view_controller.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h" 25 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/common/extensions/api/extension_action/action_info.h"
26 #include "content/public/browser/notification_details.h" 26 #include "content/public/browser/notification_details.h"
27 #include "content/public/browser/notification_observer.h" 27 #include "content/public/browser/notification_observer.h"
28 #include "content/public/browser/notification_registrar.h" 28 #include "content/public/browser/notification_registrar.h"
29 #include "content/public/browser/notification_source.h" 29 #include "content/public/browser/notification_source.h"
30 #include "extensions/browser/extension_registry.h"
31 #include "grit/theme_resources.h" 30 #include "grit/theme_resources.h"
32 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h " 31 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h "
33 32
34 using extensions::Extension; 33 using extensions::Extension;
35 using extensions::ExtensionList; 34 using extensions::ExtensionList;
36 35
37 NSString* const kBrowserActionVisibilityChangedNotification = 36 NSString* const kBrowserActionVisibilityChangedNotification =
38 @"BrowserActionVisibilityChangedNotification"; 37 @"BrowserActionVisibilityChangedNotification";
39 38
40 namespace { 39 namespace {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 // Useful in the case of a Browser Action being added/removed from the middle of 82 // Useful in the case of a Browser Action being added/removed from the middle of
84 // the container, this method repositions each button according to the current 83 // the container, this method repositions each button according to the current
85 // toolbar model. 84 // toolbar model.
86 - (void)positionActionButtonsAndAnimate:(BOOL)animate; 85 - (void)positionActionButtonsAndAnimate:(BOOL)animate;
87 86
88 // During container resizing, buttons become more transparent as they are pushed 87 // During container resizing, buttons become more transparent as they are pushed
89 // off the screen. This method updates each button's opacity determined by the 88 // off the screen. This method updates each button's opacity determined by the
90 // position of the button. 89 // position of the button.
91 - (void)updateButtonOpacity; 90 - (void)updateButtonOpacity;
92 91
93 // Returns the existing button with the given extension backing it; nil if it 92 // Returns the existing button associated with the given id; nil if it cannot be
94 // cannot be found or the extension's ID is invalid. 93 // found.
95 - (BrowserActionButton*)buttonForExtension:(const Extension*)extension; 94 - (BrowserActionButton*)buttonForId:(const std::string&)id;
96 95
97 // Returns the preferred width of the container given the number of visible 96 // Returns the preferred width of the container given the number of visible
98 // buttons |buttonCount|. 97 // buttons |buttonCount|.
99 - (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount; 98 - (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount;
100 99
101 // Returns the number of buttons that can fit in the container according to its 100 // Returns the number of buttons that can fit in the container according to its
102 // current size. 101 // current size.
103 - (NSUInteger)containerButtonCapacity; 102 - (NSUInteger)containerButtonCapacity;
104 103
105 // Notification handlers for events registered by the class. 104 // Notification handlers for events registered by the class.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 // Shows or hides the chevron, animating as specified by |animate|. 154 // Shows or hides the chevron, animating as specified by |animate|.
156 - (void)setChevronHidden:(BOOL)hidden 155 - (void)setChevronHidden:(BOOL)hidden
157 inFrame:(NSRect)frame 156 inFrame:(NSRect)frame
158 animate:(BOOL)animate; 157 animate:(BOOL)animate;
159 158
160 // Handles when a menu item within the chevron overflow menu is selected. 159 // Handles when a menu item within the chevron overflow menu is selected.
161 - (void)chevronItemSelected:(id)menuItem; 160 - (void)chevronItemSelected:(id)menuItem;
162 161
163 // Updates the container's grippy cursor based on the number of hidden buttons. 162 // Updates the container's grippy cursor based on the number of hidden buttons.
164 - (void)updateGrippyCursors; 163 - (void)updateGrippyCursors;
165
166 // Returns the ID of the currently selected tab or -1 if none exists.
167 - (int)currentTabId;
168 @end 164 @end
169 165
170 // A helper class to proxy extension notifications to the view controller's 166 // A helper class to proxy extension notifications to the view controller's
171 // appropriate methods. 167 // appropriate methods.
172 class ExtensionServiceObserverBridge 168 class ExtensionServiceObserverBridge
173 : public content::NotificationObserver, 169 : public content::NotificationObserver,
174 public extensions::ExtensionToolbarModel::Observer { 170 public extensions::ExtensionToolbarModel::Observer {
175 public: 171 public:
176 ExtensionServiceObserverBridge(BrowserActionsController* owner, 172 ExtensionServiceObserverBridge(BrowserActionsController* owner,
177 Browser* browser) 173 Browser* browser)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 } 216 }
221 217
222 void ToolbarExtensionRemoved(const Extension* extension) override { 218 void ToolbarExtensionRemoved(const Extension* extension) override {
223 [owner_ removeActionButtonForExtension:extension]; 219 [owner_ removeActionButtonForExtension:extension];
224 [owner_ resizeContainerAndAnimate:NO]; 220 [owner_ resizeContainerAndAnimate:NO];
225 } 221 }
226 222
227 void ToolbarExtensionMoved(const Extension* extension, int index) override {} 223 void ToolbarExtensionMoved(const Extension* extension, int index) override {}
228 224
229 void ToolbarExtensionUpdated(const Extension* extension) override { 225 void ToolbarExtensionUpdated(const Extension* extension) override {
230 BrowserActionButton* button = [owner_ buttonForExtension:extension]; 226 BrowserActionButton* button = [owner_ buttonForId:extension->id()];
231 if (button) 227 if (button)
232 [button updateState]; 228 [button updateState];
233 } 229 }
234 230
235 bool ShowExtensionActionPopup(const Extension* extension, 231 bool ShowExtensionActionPopup(const Extension* extension,
236 bool grant_active_tab) override { 232 bool grant_active_tab) override {
237 // Do not override other popups and only show in active window. 233 // Do not override other popups and only show in active window.
238 ExtensionPopupController* popup = [ExtensionPopupController popup]; 234 ExtensionPopupController* popup = [ExtensionPopupController popup];
239 if (popup || !browser_->window()->IsActive()) 235 if (popup || !browser_->window()->IsActive())
240 return false; 236 return false;
241 237
242 BrowserActionButton* button = [owner_ buttonForExtension:extension]; 238 BrowserActionButton* button = [owner_ buttonForId:extension->id()];
243 return button && [owner_ browserActionClicked:button 239 return button && [button viewController]->ExecuteAction(grant_active_tab);
244 shouldGrant:grant_active_tab];
245 } 240 }
246 241
247 void ToolbarVisibleCountChanged() override {} 242 void ToolbarVisibleCountChanged() override {}
248 243
249 void ToolbarHighlightModeChanged(bool is_highlighting) override {} 244 void ToolbarHighlightModeChanged(bool is_highlighting) override {}
250 245
251 Browser* GetBrowser() override { return browser_; } 246 Browser* GetBrowser() override { return browser_; }
252 247
253 private: 248 private:
254 // The object we need to inform when we get a notification. Weak. Owns us. 249 // The object we need to inform when we get a notification. Weak. Owns us.
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 326
332 - (void)dealloc { 327 - (void)dealloc {
333 if (toolbarModel_) 328 if (toolbarModel_)
334 toolbarModel_->RemoveObserver(observer_.get()); 329 toolbarModel_->RemoveObserver(observer_.get());
335 330
336 [[NSNotificationCenter defaultCenter] removeObserver:self]; 331 [[NSNotificationCenter defaultCenter] removeObserver:self];
337 [super dealloc]; 332 [super dealloc];
338 } 333 }
339 334
340 - (void)update { 335 - (void)update {
341 for (BrowserActionButton* button in [buttons_ allValues]) { 336 for (BrowserActionButton* button in [buttons_ allValues])
342 [button setTabId:[self currentTabId]];
343 [button updateState]; 337 [button updateState];
344 }
345 } 338 }
346 339
347 - (NSUInteger)buttonCount { 340 - (NSUInteger)buttonCount {
348 return [buttons_ count]; 341 return [buttons_ count];
349 } 342 }
350 343
351 - (NSUInteger)visibleButtonCount { 344 - (NSUInteger)visibleButtonCount {
352 return [self buttonCount] - [hiddenButtons_ count]; 345 return [self buttonCount] - [hiddenButtons_ count];
353 } 346 }
354 347
355 - (void)resizeContainerAndAnimate:(BOOL)animate { 348 - (void)resizeContainerAndAnimate:(BOOL)animate {
356 int iconCount = toolbarModel_->GetVisibleIconCount(); 349 int iconCount = toolbarModel_->GetVisibleIconCount();
357 if (iconCount < 0) // If no buttons are hidden. 350 if (iconCount < 0) // If no buttons are hidden.
358 iconCount = [self buttonCount]; 351 iconCount = [self buttonCount];
359 352
360 [containerView_ resizeToWidth:[self containerWidthWithButtonCount:iconCount] 353 [containerView_ resizeToWidth:[self containerWidthWithButtonCount:iconCount]
361 animate:animate]; 354 animate:animate];
362 NSRect frame = animate ? [containerView_ animationEndFrame] : 355 NSRect frame = animate ? [containerView_ animationEndFrame] :
363 [containerView_ frame]; 356 [containerView_ frame];
364 357
365 [self showChevronIfNecessaryInFrame:frame animate:animate]; 358 [self showChevronIfNecessaryInFrame:frame animate:animate];
366 359
367 if (!animate) { 360 if (!animate) {
368 [[NSNotificationCenter defaultCenter] 361 [[NSNotificationCenter defaultCenter]
369 postNotificationName:kBrowserActionVisibilityChangedNotification 362 postNotificationName:kBrowserActionVisibilityChangedNotification
370 object:self]; 363 object:self];
371 } 364 }
372 } 365 }
373 366
374 - (NSView*)browserActionViewForExtension:(const Extension*)extension {
375 for (BrowserActionButton* button in [buttons_ allValues]) {
376 if ([button extension] == extension)
377 return button;
378 }
379 NOTREACHED();
380 return nil;
381 }
382
383 - (CGFloat)savedWidth { 367 - (CGFloat)savedWidth {
384 if (!toolbarModel_) 368 if (!toolbarModel_)
385 return 0; 369 return 0;
386 370
387 int savedButtonCount = toolbarModel_->GetVisibleIconCount(); 371 int savedButtonCount = toolbarModel_->GetVisibleIconCount();
388 if (savedButtonCount < 0 || // all icons are visible 372 if (savedButtonCount < 0 || // all icons are visible
389 static_cast<NSUInteger>(savedButtonCount) > [self buttonCount]) 373 static_cast<NSUInteger>(savedButtonCount) > [self buttonCount])
390 savedButtonCount = [self buttonCount]; 374 savedButtonCount = [self buttonCount];
391 return [self containerWidthWithButtonCount:savedButtonCount]; 375 return [self containerWidthWithButtonCount:savedButtonCount];
392 } 376 }
393 377
394 - (NSPoint)popupPointForBrowserAction:(const Extension*)extension { 378 - (NSPoint)popupPointForId:(const std::string&)id {
395 if (!extensions::ExtensionActionManager::Get(profile_)-> 379 NSButton* button = [self buttonForId:id];
396 GetBrowserAction(*extension)) {
397 return NSZeroPoint;
398 }
399
400 NSButton* button = [self buttonForExtension:extension];
401 if (!button) 380 if (!button)
402 return NSZeroPoint; 381 return NSZeroPoint;
403 382
404 if ([hiddenButtons_ containsObject:button]) 383 if ([hiddenButtons_ containsObject:button])
405 button = chevronMenuButton_.get(); 384 button = chevronMenuButton_.get();
406 385
407 // Anchor point just above the center of the bottom. 386 // Anchor point just above the center of the bottom.
408 const NSRect bounds = [button bounds]; 387 const NSRect bounds = [button bounds];
409 DCHECK([button isFlipped]); 388 DCHECK([button isFlipped]);
410 NSPoint anchor = NSMakePoint(NSMidX(bounds), 389 NSPoint anchor = NSMakePoint(NSMidX(bounds),
(...skipping 17 matching lines...) Expand all
428 if (effect == NSViewAnimationFadeInEffect) { 407 if (effect == NSViewAnimationFadeInEffect) {
429 return NO; 408 return NO;
430 } else if (effect == NSViewAnimationFadeOutEffect) { 409 } else if (effect == NSViewAnimationFadeOutEffect) {
431 return YES; 410 return YES;
432 } 411 }
433 412
434 NOTREACHED(); 413 NOTREACHED();
435 return YES; 414 return YES;
436 } 415 }
437 416
438 - (void)activateBrowserAction:(const std::string&)extension_id { 417 - (void)activateBrowserAction:(const std::string&)id {
439 const Extension* extension = extensions::ExtensionRegistry::Get( 418 BrowserActionButton* button = [self buttonForId:id];
440 browser_->profile())->enabled_extensions().GetByID(extension_id);
441 if (!extension)
442 return;
443
444 BrowserActionButton* button = [self buttonForExtension:extension];
445 // |button| can be nil when the browser action has its button hidden. 419 // |button| can be nil when the browser action has its button hidden.
446 if (button) 420 if (button)
447 [self browserActionClicked:button]; 421 [self browserActionClicked:button];
448 } 422 }
449 423
424 - (content::WebContents*)currentWebContents {
425 return browser_->tab_strip_model()->GetActiveWebContents();
426 }
427
450 #pragma mark - 428 #pragma mark -
451 #pragma mark NSMenuDelegate 429 #pragma mark NSMenuDelegate
452 430
453 - (void)menuNeedsUpdate:(NSMenu*)menu { 431 - (void)menuNeedsUpdate:(NSMenu*)menu {
454 [menu removeAllItems]; 432 [menu removeAllItems];
455 433
456 // See menu_button.h for documentation on why this is needed. 434 // See menu_button.h for documentation on why this is needed.
457 [menu addItemWithTitle:@"" action:nil keyEquivalent:@""]; 435 [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
458 436
459 for (BrowserActionButton* button in hiddenButtons_.get()) { 437 for (BrowserActionButton* button in hiddenButtons_.get()) {
460 NSString* name = base::SysUTF8ToNSString([button extension]->name()); 438 NSString* name =
439 base::SysUTF16ToNSString([button viewController]->GetActionName());
461 NSMenuItem* item = 440 NSMenuItem* item =
462 [menu addItemWithTitle:name 441 [menu addItemWithTitle:name
463 action:@selector(chevronItemSelected:) 442 action:@selector(chevronItemSelected:)
464 keyEquivalent:@""]; 443 keyEquivalent:@""];
465 [item setRepresentedObject:button]; 444 [item setRepresentedObject:button];
466 [item setImage:[button compositedImage]]; 445 [item setImage:[button compositedImage]];
467 [item setTarget:self]; 446 [item setTarget:self];
468 [item setEnabled:[button isEnabled]]; 447 [item setEnabled:[button isEnabled]];
469 } 448 }
470 } 449 }
(...skipping 18 matching lines...) Expand all
489 468
490 - (void)createActionButtonForExtension:(const Extension*)extension 469 - (void)createActionButtonForExtension:(const Extension*)extension
491 withIndex:(NSUInteger)index { 470 withIndex:(NSUInteger)index {
492 // Show the container if it's the first button. Otherwise it will be shown 471 // Show the container if it's the first button. Otherwise it will be shown
493 // already. 472 // already.
494 if ([self buttonCount] == 0) 473 if ([self buttonCount] == 0)
495 [containerView_ setHidden:NO]; 474 [containerView_ setHidden:NO];
496 475
497 NSRect buttonFrame = NSMakeRect(0.0, kBrowserActionOriginYOffset, 476 NSRect buttonFrame = NSMakeRect(0.0, kBrowserActionOriginYOffset,
498 kBrowserActionWidth, kBrowserActionHeight); 477 kBrowserActionWidth, kBrowserActionHeight);
478 ExtensionAction* extensionAction =
479 extensions::ExtensionActionManager::Get(browser_->profile())->
480 GetExtensionAction(*extension);
481 CHECK(extensionAction)
Avi (use Gerrit) 2014/10/30 23:21:26 Again, why not DCHECK?
Devlin 2014/10/31 17:50:43 Ditto and done. :)
482 << "Don't create a BrowserActionButton if there is no browser action.";
483 scoped_ptr<ToolbarActionViewController> viewController(
484 new ExtensionActionViewController(extension, browser_, extensionAction));
485 // TODO(devlin): Move ContextMenuController stuff to
486 // ExtensionActionViewController.
487 ExtensionActionContextMenuController* menuController =
488 [[ExtensionActionContextMenuController alloc]
489 initWithExtension:extension
490 browser:browser_
491 extensionAction:extensionAction];
499 BrowserActionButton* newButton = 492 BrowserActionButton* newButton =
500 [[[BrowserActionButton alloc] 493 [[[BrowserActionButton alloc]
501 initWithFrame:buttonFrame 494 initWithFrame:buttonFrame
502 extension:extension 495 viewController:viewController.Pass()
503 browser:browser_ 496 controller:self
504 tabId:[self currentTabId]] autorelease]; 497 menuController:menuController] autorelease];
505 [newButton setTarget:self]; 498 [newButton setTarget:self];
506 [newButton setAction:@selector(browserActionClicked:)]; 499 [newButton setAction:@selector(browserActionClicked:)];
507 NSString* buttonKey = base::SysUTF8ToNSString(extension->id()); 500 NSString* buttonKey = base::SysUTF8ToNSString(extension->id());
508 if (!buttonKey) 501 if (!buttonKey)
509 return; 502 return;
510 [buttons_ setObject:newButton forKey:buttonKey]; 503 [buttons_ setObject:newButton forKey:buttonKey];
511 504
512 [self positionActionButtonsAndAnimate:NO]; 505 [self positionActionButtonsAndAnimate:NO];
513 506
514 [[NSNotificationCenter defaultCenter] 507 [[NSNotificationCenter defaultCenter]
(...skipping 30 matching lines...) Expand all
545 [containerView_ setMaxWidth: 538 [containerView_ setMaxWidth:
546 [self containerWidthWithButtonCount:[self buttonCount]]]; 539 [self containerWidthWithButtonCount:[self buttonCount]]];
547 [containerView_ setNeedsDisplay:YES]; 540 [containerView_ setNeedsDisplay:YES];
548 } 541 }
549 542
550 - (void)positionActionButtonsAndAnimate:(BOOL)animate { 543 - (void)positionActionButtonsAndAnimate:(BOOL)animate {
551 NSUInteger i = 0; 544 NSUInteger i = 0;
552 for (ExtensionList::const_iterator iter = 545 for (ExtensionList::const_iterator iter =
553 toolbarModel_->toolbar_items().begin(); 546 toolbarModel_->toolbar_items().begin();
554 iter != toolbarModel_->toolbar_items().end(); ++iter) { 547 iter != toolbarModel_->toolbar_items().end(); ++iter) {
555 BrowserActionButton* button = [self buttonForExtension:(iter->get())]; 548 BrowserActionButton* button = [self buttonForId:(iter->get()->id())];
556 if (!button) 549 if (!button)
557 continue; 550 continue;
558 if (![button isBeingDragged]) 551 if (![button isBeingDragged])
559 [self moveButton:button toIndex:i animate:animate]; 552 [self moveButton:button toIndex:i animate:animate];
560 ++i; 553 ++i;
561 } 554 }
562 } 555 }
563 556
564 - (void)updateButtonOpacity { 557 - (void)updateButtonOpacity {
565 for (BrowserActionButton* button in [buttons_ allValues]) { 558 for (BrowserActionButton* button in [buttons_ allValues]) {
566 NSRect buttonFrame = [button frame]; 559 NSRect buttonFrame = [button frame];
567 if (NSContainsRect([containerView_ bounds], buttonFrame)) { 560 if (NSContainsRect([containerView_ bounds], buttonFrame)) {
568 if ([button alphaValue] != 1.0) 561 if ([button alphaValue] != 1.0)
569 [button setAlphaValue:1.0]; 562 [button setAlphaValue:1.0];
570 563
571 continue; 564 continue;
572 } 565 }
573 CGFloat intersectionWidth = 566 CGFloat intersectionWidth =
574 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame)); 567 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame));
575 CGFloat alpha = std::max(static_cast<CGFloat>(0.0), 568 CGFloat alpha = std::max(static_cast<CGFloat>(0.0),
576 intersectionWidth / NSWidth(buttonFrame)); 569 intersectionWidth / NSWidth(buttonFrame));
577 [button setAlphaValue:alpha]; 570 [button setAlphaValue:alpha];
578 [button setNeedsDisplay:YES]; 571 [button setNeedsDisplay:YES];
579 } 572 }
580 } 573 }
581 574
582 - (BrowserActionButton*)buttonForExtension:(const Extension*)extension { 575 - (BrowserActionButton*)buttonForId:(const std::string&)id {
583 NSString* extensionId = base::SysUTF8ToNSString(extension->id()); 576 NSString* nsId = base::SysUTF8ToNSString(id);
584 DCHECK(extensionId); 577 DCHECK(nsId);
585 if (!extensionId) 578 if (!nsId)
586 return nil; 579 return nil;
587 return [buttons_ objectForKey:extensionId]; 580 return [buttons_ objectForKey:nsId];
588 } 581 }
589 582
590 - (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount { 583 - (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount {
591 // Left-side padding which works regardless of whether a button or 584 // Left-side padding which works regardless of whether a button or
592 // chevron leads. 585 // chevron leads.
593 CGFloat width = kBrowserActionLeftPadding; 586 CGFloat width = kBrowserActionLeftPadding;
594 587
595 // Include the buttons and padding between. 588 // Include the buttons and padding between.
596 if (buttonCount > 0) { 589 if (buttonCount > 0) {
597 width += buttonCount * kBrowserActionWidth; 590 width += buttonCount * kBrowserActionWidth;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 - (void)containerDragging:(NSNotification*)notification { 631 - (void)containerDragging:(NSNotification*)notification {
639 [[NSNotificationCenter defaultCenter] 632 [[NSNotificationCenter defaultCenter]
640 postNotificationName:kBrowserActionGrippyDraggingNotification 633 postNotificationName:kBrowserActionGrippyDraggingNotification
641 object:self]; 634 object:self];
642 } 635 }
643 636
644 - (void)containerDragFinished:(NSNotification*)notification { 637 - (void)containerDragFinished:(NSNotification*)notification {
645 for (ExtensionList::const_iterator iter = 638 for (ExtensionList::const_iterator iter =
646 toolbarModel_->toolbar_items().begin(); 639 toolbarModel_->toolbar_items().begin();
647 iter != toolbarModel_->toolbar_items().end(); ++iter) { 640 iter != toolbarModel_->toolbar_items().end(); ++iter) {
648 BrowserActionButton* button = [self buttonForExtension:(iter->get())]; 641 BrowserActionButton* button = [self buttonForId:(iter->get()->id())];
649 NSRect buttonFrame = [button frame]; 642 NSRect buttonFrame = [button frame];
650 if (NSContainsRect([containerView_ bounds], buttonFrame)) 643 if (NSContainsRect([containerView_ bounds], buttonFrame))
651 continue; 644 continue;
652 645
653 CGFloat intersectionWidth = 646 CGFloat intersectionWidth =
654 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame)); 647 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame));
655 // Pad the threshold by 5 pixels in order to have the buttons hide more 648 // Pad the threshold by 5 pixels in order to have the buttons hide more
656 // easily. 649 // easily.
657 if (([containerView_ grippyPinned] && intersectionWidth > 0) || 650 if (([containerView_ grippyPinned] && intersectionWidth > 0) ||
658 (intersectionWidth <= (NSWidth(buttonFrame) / 2) + 5.0)) { 651 (intersectionWidth <= (NSWidth(buttonFrame) / 2) + 5.0)) {
(...skipping 18 matching lines...) Expand all
677 // Determine what index the dragged button should lie in, alter the model and 670 // Determine what index the dragged button should lie in, alter the model and
678 // reposition the buttons. 671 // reposition the buttons.
679 CGFloat dragThreshold = std::floor(kBrowserActionWidth / 2); 672 CGFloat dragThreshold = std::floor(kBrowserActionWidth / 2);
680 BrowserActionButton* draggedButton = [notification object]; 673 BrowserActionButton* draggedButton = [notification object];
681 NSRect draggedButtonFrame = [draggedButton frame]; 674 NSRect draggedButtonFrame = [draggedButton frame];
682 675
683 NSUInteger index = 0; 676 NSUInteger index = 0;
684 for (ExtensionList::const_iterator iter = 677 for (ExtensionList::const_iterator iter =
685 toolbarModel_->toolbar_items().begin(); 678 toolbarModel_->toolbar_items().begin();
686 iter != toolbarModel_->toolbar_items().end(); ++iter) { 679 iter != toolbarModel_->toolbar_items().end(); ++iter) {
687 BrowserActionButton* button = [self buttonForExtension:(iter->get())]; 680 BrowserActionButton* button = [self buttonForId:(iter->get()->id())];
688 CGFloat intersectionWidth = 681 CGFloat intersectionWidth =
689 NSWidth(NSIntersectionRect(draggedButtonFrame, [button frame])); 682 NSWidth(NSIntersectionRect(draggedButtonFrame, [button frame]));
690 683
691 if (intersectionWidth > dragThreshold && button != draggedButton && 684 if (intersectionWidth > dragThreshold && button != draggedButton &&
692 ![button isAnimating] && index < [self visibleButtonCount]) { 685 ![button isAnimating] && index < [self visibleButtonCount]) {
693 toolbarModel_->MoveExtensionIcon([draggedButton extension]->id(), index); 686 toolbarModel_->MoveExtensionIcon([draggedButton viewController]->GetId(),
687 index);
694 [self positionActionButtonsAndAnimate:YES]; 688 [self positionActionButtonsAndAnimate:YES];
695 return; 689 return;
696 } 690 }
697 ++index; 691 ++index;
698 } 692 }
699 } 693 }
700 694
701 - (void)actionButtonDragFinished:(NSNotification*)notification { 695 - (void)actionButtonDragFinished:(NSNotification*)notification {
702 [self showChevronIfNecessaryInFrame:[containerView_ frame] animate:YES]; 696 [self showChevronIfNecessaryInFrame:[containerView_ frame] animate:YES];
703 [self positionActionButtonsAndAnimate:YES]; 697 [self positionActionButtonsAndAnimate:YES];
(...skipping 17 matching lines...) Expand all
721 } 715 }
722 } else if (![hiddenButtons_ containsObject:button]) { 716 } else if (![hiddenButtons_ containsObject:button]) {
723 [hiddenButtons_ addObject:button]; 717 [hiddenButtons_ addObject:button];
724 [button removeFromSuperview]; 718 [button removeFromSuperview];
725 [button setAlphaValue:0.0]; 719 [button setAlphaValue:0.0];
726 } 720 }
727 } 721 }
728 722
729 - (BOOL)browserActionClicked:(BrowserActionButton*)button 723 - (BOOL)browserActionClicked:(BrowserActionButton*)button
730 shouldGrant:(BOOL)shouldGrant { 724 shouldGrant:(BOOL)shouldGrant {
731 const Extension* extension = [button extension]; 725 return [button viewController]->ExecuteAction(shouldGrant);
732 switch (extensions::ExtensionActionAPI::Get(profile_)->ExecuteExtensionAction(
733 extension, browser_, shouldGrant)) {
734 case ExtensionAction::ACTION_NONE:
735 break;
736 case ExtensionAction::ACTION_SHOW_POPUP: {
737 GURL popupUrl = extensions::ExtensionActionManager::Get(profile_)->
738 GetBrowserAction(*extension)->GetPopupUrl([self currentTabId]);
739 NSPoint arrowPoint = [self popupPointForBrowserAction:extension];
740 [ExtensionPopupController showURL:popupUrl
741 inBrowser:browser_
742 anchoredAt:arrowPoint
743 arrowLocation:info_bubble::kTopRight
744 devMode:NO];
745 return YES;
746 }
747 }
748 return NO;
749 } 726 }
750 727
751 - (BOOL)browserActionClicked:(BrowserActionButton*)button { 728 - (BOOL)browserActionClicked:(BrowserActionButton*)button {
752 return [self browserActionClicked:button 729 return [self browserActionClicked:button
753 shouldGrant:YES]; 730 shouldGrant:YES];
754 } 731 }
755 732
756 - (void)showChevronIfNecessaryInFrame:(NSRect)frame animate:(BOOL)animate { 733 - (void)showChevronIfNecessaryInFrame:(NSRect)frame animate:(BOOL)animate {
757 [self setChevronHidden:([self buttonCount] == [self visibleButtonCount]) 734 [self setChevronHidden:([self buttonCount] == [self visibleButtonCount])
758 inFrame:frame 735 inFrame:frame
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 - (void)chevronItemSelected:(id)menuItem { 803 - (void)chevronItemSelected:(id)menuItem {
827 [self browserActionClicked:[menuItem representedObject]]; 804 [self browserActionClicked:[menuItem representedObject]];
828 } 805 }
829 806
830 - (void)updateGrippyCursors { 807 - (void)updateGrippyCursors {
831 [containerView_ setCanDragLeft:[hiddenButtons_ count] > 0]; 808 [containerView_ setCanDragLeft:[hiddenButtons_ count] > 0];
832 [containerView_ setCanDragRight:[self visibleButtonCount] > 0]; 809 [containerView_ setCanDragRight:[self visibleButtonCount] > 0];
833 [[containerView_ window] invalidateCursorRectsForView:containerView_]; 810 [[containerView_ window] invalidateCursorRectsForView:containerView_];
834 } 811 }
835 812
836 - (int)currentTabId {
837 content::WebContents* active_tab =
838 browser_->tab_strip_model()->GetActiveWebContents();
839 if (!active_tab)
840 return -1;
841
842 return SessionTabHelper::FromWebContents(active_tab)->session_id().id();
843 }
844
845 #pragma mark - 813 #pragma mark -
846 #pragma mark Testing Methods 814 #pragma mark Testing Methods
847 815
848 - (BrowserActionButton*)buttonWithIndex:(NSUInteger)index { 816 - (BrowserActionButton*)buttonWithIndex:(NSUInteger)index {
849 const extensions::ExtensionList& toolbar_items = 817 const extensions::ExtensionList& toolbar_items =
850 toolbarModel_->toolbar_items(); 818 toolbarModel_->toolbar_items();
851 if (index < toolbar_items.size()) { 819 if (index < toolbar_items.size()) {
852 const Extension* extension = toolbar_items[index].get(); 820 const Extension* extension = toolbar_items[index].get();
853 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())]; 821 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())];
854 } 822 }
855 return nil; 823 return nil;
856 } 824 }
857 825
858 @end 826 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698