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

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, 2 months 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_action_view_controller_coc oa.h"
21 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h" 22 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
22 #import "chrome/browser/ui/cocoa/image_button_cell.h" 23 #import "chrome/browser/ui/cocoa/image_button_cell.h"
23 #import "chrome/browser/ui/cocoa/menu_button.h" 24 #import "chrome/browser/ui/cocoa/menu_button.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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 164
166 // Returns the ID of the currently selected tab or -1 if none exists. 165 // Returns the web contents of the currently-selected tab.
167 - (int)currentTabId; 166 - (content::WebContents*)currentWebContents;
168 @end 167 @end
169 168
170 // A helper class to proxy extension notifications to the view controller's 169 // A helper class to proxy extension notifications to the view controller's
171 // appropriate methods. 170 // appropriate methods.
172 class ExtensionServiceObserverBridge 171 class ExtensionServiceObserverBridge
173 : public content::NotificationObserver, 172 : public content::NotificationObserver,
174 public extensions::ExtensionToolbarModel::Observer { 173 public extensions::ExtensionToolbarModel::Observer {
175 public: 174 public:
176 ExtensionServiceObserverBridge(BrowserActionsController* owner, 175 ExtensionServiceObserverBridge(BrowserActionsController* owner,
177 Browser* browser) 176 Browser* browser)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 } 219 }
221 220
222 void ToolbarExtensionRemoved(const Extension* extension) override { 221 void ToolbarExtensionRemoved(const Extension* extension) override {
223 [owner_ removeActionButtonForExtension:extension]; 222 [owner_ removeActionButtonForExtension:extension];
224 [owner_ resizeContainerAndAnimate:NO]; 223 [owner_ resizeContainerAndAnimate:NO];
225 } 224 }
226 225
227 void ToolbarExtensionMoved(const Extension* extension, int index) override {} 226 void ToolbarExtensionMoved(const Extension* extension, int index) override {}
228 227
229 void ToolbarExtensionUpdated(const Extension* extension) override { 228 void ToolbarExtensionUpdated(const Extension* extension) override {
230 BrowserActionButton* button = [owner_ buttonForExtension:extension]; 229 BrowserActionButton* button = [owner_ buttonForId:extension->id()];
231 if (button) 230 if (button)
232 [button updateState]; 231 [button updateState];
233 } 232 }
234 233
235 bool ShowExtensionActionPopup(const Extension* extension, 234 bool ShowExtensionActionPopup(const Extension* extension,
236 bool grant_active_tab) override { 235 bool grant_active_tab) override {
237 // Do not override other popups and only show in active window. 236 // Do not override other popups and only show in active window.
238 ExtensionPopupController* popup = [ExtensionPopupController popup]; 237 ExtensionPopupController* popup = [ExtensionPopupController popup];
239 if (popup || !browser_->window()->IsActive()) 238 if (popup || !browser_->window()->IsActive())
240 return false; 239 return false;
241 240
242 BrowserActionButton* button = [owner_ buttonForExtension:extension]; 241 BrowserActionButton* button = [owner_ buttonForId:extension->id()];
243 return button && [owner_ browserActionClicked:button 242 return button && [button viewController]->ExecuteAction(grant_active_tab);
244 shouldGrant:grant_active_tab];
245 } 243 }
246 244
247 void ToolbarVisibleCountChanged() override {} 245 void ToolbarVisibleCountChanged() override {}
248 246
249 void ToolbarHighlightModeChanged(bool is_highlighting) override {} 247 void ToolbarHighlightModeChanged(bool is_highlighting) override {}
250 248
251 Browser* GetBrowser() override { return browser_; } 249 Browser* GetBrowser() override { return browser_; }
252 250
253 private: 251 private:
254 // The object we need to inform when we get a notification. Weak. Owns us. 252 // The object we need to inform when we get a notification. Weak. Owns us.
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 - (void)dealloc { 330 - (void)dealloc {
333 if (toolbarModel_) 331 if (toolbarModel_)
334 toolbarModel_->RemoveObserver(observer_.get()); 332 toolbarModel_->RemoveObserver(observer_.get());
335 333
336 [[NSNotificationCenter defaultCenter] removeObserver:self]; 334 [[NSNotificationCenter defaultCenter] removeObserver:self];
337 [super dealloc]; 335 [super dealloc];
338 } 336 }
339 337
340 - (void)update { 338 - (void)update {
341 for (BrowserActionButton* button in [buttons_ allValues]) { 339 for (BrowserActionButton* button in [buttons_ allValues]) {
342 [button setTabId:[self currentTabId]]; 340 [button setWebContents:[self currentWebContents]];
343 [button updateState]; 341 [button updateState];
344 } 342 }
345 } 343 }
346 344
347 - (NSUInteger)buttonCount { 345 - (NSUInteger)buttonCount {
348 return [buttons_ count]; 346 return [buttons_ count];
349 } 347 }
350 348
351 - (NSUInteger)visibleButtonCount { 349 - (NSUInteger)visibleButtonCount {
352 return [self buttonCount] - [hiddenButtons_ count]; 350 return [self buttonCount] - [hiddenButtons_ count];
(...skipping 11 matching lines...) Expand all
364 362
365 [self showChevronIfNecessaryInFrame:frame animate:animate]; 363 [self showChevronIfNecessaryInFrame:frame animate:animate];
366 364
367 if (!animate) { 365 if (!animate) {
368 [[NSNotificationCenter defaultCenter] 366 [[NSNotificationCenter defaultCenter]
369 postNotificationName:kBrowserActionVisibilityChangedNotification 367 postNotificationName:kBrowserActionVisibilityChangedNotification
370 object:self]; 368 object:self];
371 } 369 }
372 } 370 }
373 371
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 { 372 - (CGFloat)savedWidth {
384 if (!toolbarModel_) 373 if (!toolbarModel_)
385 return 0; 374 return 0;
386 375
387 int savedButtonCount = toolbarModel_->GetVisibleIconCount(); 376 int savedButtonCount = toolbarModel_->GetVisibleIconCount();
388 if (savedButtonCount < 0 || // all icons are visible 377 if (savedButtonCount < 0 || // all icons are visible
389 static_cast<NSUInteger>(savedButtonCount) > [self buttonCount]) 378 static_cast<NSUInteger>(savedButtonCount) > [self buttonCount])
390 savedButtonCount = [self buttonCount]; 379 savedButtonCount = [self buttonCount];
391 return [self containerWidthWithButtonCount:savedButtonCount]; 380 return [self containerWidthWithButtonCount:savedButtonCount];
392 } 381 }
393 382
394 - (NSPoint)popupPointForBrowserAction:(const Extension*)extension { 383 - (NSPoint)popupPointForId:(const std::string&)id {
395 if (!extensions::ExtensionActionManager::Get(profile_)-> 384 NSButton* button = [self buttonForId:id];
396 GetBrowserAction(*extension)) {
397 return NSZeroPoint;
398 }
399
400 NSButton* button = [self buttonForExtension:extension];
401 if (!button) 385 if (!button)
402 return NSZeroPoint; 386 return NSZeroPoint;
403 387
404 if ([hiddenButtons_ containsObject:button]) 388 if ([hiddenButtons_ containsObject:button])
405 button = chevronMenuButton_.get(); 389 button = chevronMenuButton_.get();
406 390
407 // Anchor point just above the center of the bottom. 391 // Anchor point just above the center of the bottom.
408 const NSRect bounds = [button bounds]; 392 const NSRect bounds = [button bounds];
409 DCHECK([button isFlipped]); 393 DCHECK([button isFlipped]);
410 NSPoint anchor = NSMakePoint(NSMidX(bounds), 394 NSPoint anchor = NSMakePoint(NSMidX(bounds),
(...skipping 17 matching lines...) Expand all
428 if (effect == NSViewAnimationFadeInEffect) { 412 if (effect == NSViewAnimationFadeInEffect) {
429 return NO; 413 return NO;
430 } else if (effect == NSViewAnimationFadeOutEffect) { 414 } else if (effect == NSViewAnimationFadeOutEffect) {
431 return YES; 415 return YES;
432 } 416 }
433 417
434 NOTREACHED(); 418 NOTREACHED();
435 return YES; 419 return YES;
436 } 420 }
437 421
438 - (void)activateBrowserAction:(const std::string&)extension_id { 422 - (void)activateBrowserAction:(const std::string&)id {
439 const Extension* extension = extensions::ExtensionRegistry::Get( 423 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. 424 // |button| can be nil when the browser action has its button hidden.
446 if (button) 425 if (button)
447 [self browserActionClicked:button]; 426 [self browserActionClicked:button];
448 } 427 }
449 428
450 #pragma mark - 429 #pragma mark -
451 #pragma mark NSMenuDelegate 430 #pragma mark NSMenuDelegate
452 431
453 - (void)menuNeedsUpdate:(NSMenu*)menu { 432 - (void)menuNeedsUpdate:(NSMenu*)menu {
454 [menu removeAllItems]; 433 [menu removeAllItems];
455 434
456 // See menu_button.h for documentation on why this is needed. 435 // See menu_button.h for documentation on why this is needed.
457 [menu addItemWithTitle:@"" action:nil keyEquivalent:@""]; 436 [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
458 437
459 for (BrowserActionButton* button in hiddenButtons_.get()) { 438 for (BrowserActionButton* button in hiddenButtons_.get()) {
460 NSString* name = base::SysUTF8ToNSString([button extension]->name()); 439 NSString* name =
440 base::SysUTF16ToNSString([button viewController]->GetActionName());
461 NSMenuItem* item = 441 NSMenuItem* item =
462 [menu addItemWithTitle:name 442 [menu addItemWithTitle:name
463 action:@selector(chevronItemSelected:) 443 action:@selector(chevronItemSelected:)
464 keyEquivalent:@""]; 444 keyEquivalent:@""];
465 [item setRepresentedObject:button]; 445 [item setRepresentedObject:button];
466 [item setImage:[button compositedImage]]; 446 [item setImage:[button compositedImage]];
467 [item setTarget:self]; 447 [item setTarget:self];
468 [item setEnabled:[button isEnabled]]; 448 [item setEnabled:[button isEnabled]];
469 } 449 }
470 } 450 }
(...skipping 18 matching lines...) Expand all
489 469
490 - (void)createActionButtonForExtension:(const Extension*)extension 470 - (void)createActionButtonForExtension:(const Extension*)extension
491 withIndex:(NSUInteger)index { 471 withIndex:(NSUInteger)index {
492 // Show the container if it's the first button. Otherwise it will be shown 472 // Show the container if it's the first button. Otherwise it will be shown
493 // already. 473 // already.
494 if ([self buttonCount] == 0) 474 if ([self buttonCount] == 0)
495 [containerView_ setHidden:NO]; 475 [containerView_ setHidden:NO];
496 476
497 NSRect buttonFrame = NSMakeRect(0.0, kBrowserActionOriginYOffset, 477 NSRect buttonFrame = NSMakeRect(0.0, kBrowserActionOriginYOffset,
498 kBrowserActionWidth, kBrowserActionHeight); 478 kBrowserActionWidth, kBrowserActionHeight);
479 ExtensionAction* extensionAction =
480 extensions::ExtensionActionManager::Get(browser_->profile())->
481 GetExtensionAction(*extension);
482 CHECK(extensionAction)
483 << "Don't create a BrowserActionButton if there is no browser action.";
484 scoped_ptr<ToolbarActionViewController> viewController(
485 new ExtensionActionViewControllerCocoa(extension,
486 browser_,
487 extensionAction));
488 // TODO(devlin): Move ContextMenuController stuff to
489 // ExtensionActionViewController.
490 ExtensionActionContextMenuController* menuController =
491 [[ExtensionActionContextMenuController alloc]
492 initWithExtension:extension
493 browser:browser_
494 extensionAction:extensionAction];
499 BrowserActionButton* newButton = 495 BrowserActionButton* newButton =
500 [[[BrowserActionButton alloc] 496 [[[BrowserActionButton alloc]
501 initWithFrame:buttonFrame 497 initWithFrame:buttonFrame
502 extension:extension 498 viewController:viewController.Pass()
503 browser:browser_ 499 webContents:[self currentWebContents]
504 tabId:[self currentTabId]] autorelease]; 500 controller:self
501 menuController:menuController] autorelease];
505 [newButton setTarget:self]; 502 [newButton setTarget:self];
506 [newButton setAction:@selector(browserActionClicked:)]; 503 [newButton setAction:@selector(browserActionClicked:)];
507 NSString* buttonKey = base::SysUTF8ToNSString(extension->id()); 504 NSString* buttonKey = base::SysUTF8ToNSString(extension->id());
508 if (!buttonKey) 505 if (!buttonKey)
509 return; 506 return;
510 [buttons_ setObject:newButton forKey:buttonKey]; 507 [buttons_ setObject:newButton forKey:buttonKey];
511 508
512 [self positionActionButtonsAndAnimate:NO]; 509 [self positionActionButtonsAndAnimate:NO];
513 510
514 [[NSNotificationCenter defaultCenter] 511 [[NSNotificationCenter defaultCenter]
(...skipping 30 matching lines...) Expand all
545 [containerView_ setMaxWidth: 542 [containerView_ setMaxWidth:
546 [self containerWidthWithButtonCount:[self buttonCount]]]; 543 [self containerWidthWithButtonCount:[self buttonCount]]];
547 [containerView_ setNeedsDisplay:YES]; 544 [containerView_ setNeedsDisplay:YES];
548 } 545 }
549 546
550 - (void)positionActionButtonsAndAnimate:(BOOL)animate { 547 - (void)positionActionButtonsAndAnimate:(BOOL)animate {
551 NSUInteger i = 0; 548 NSUInteger i = 0;
552 for (ExtensionList::const_iterator iter = 549 for (ExtensionList::const_iterator iter =
553 toolbarModel_->toolbar_items().begin(); 550 toolbarModel_->toolbar_items().begin();
554 iter != toolbarModel_->toolbar_items().end(); ++iter) { 551 iter != toolbarModel_->toolbar_items().end(); ++iter) {
555 BrowserActionButton* button = [self buttonForExtension:(iter->get())]; 552 BrowserActionButton* button = [self buttonForId:(iter->get()->id())];
556 if (!button) 553 if (!button)
557 continue; 554 continue;
558 if (![button isBeingDragged]) 555 if (![button isBeingDragged])
559 [self moveButton:button toIndex:i animate:animate]; 556 [self moveButton:button toIndex:i animate:animate];
560 ++i; 557 ++i;
561 } 558 }
562 } 559 }
563 560
564 - (void)updateButtonOpacity { 561 - (void)updateButtonOpacity {
565 for (BrowserActionButton* button in [buttons_ allValues]) { 562 for (BrowserActionButton* button in [buttons_ allValues]) {
566 NSRect buttonFrame = [button frame]; 563 NSRect buttonFrame = [button frame];
567 if (NSContainsRect([containerView_ bounds], buttonFrame)) { 564 if (NSContainsRect([containerView_ bounds], buttonFrame)) {
568 if ([button alphaValue] != 1.0) 565 if ([button alphaValue] != 1.0)
569 [button setAlphaValue:1.0]; 566 [button setAlphaValue:1.0];
570 567
571 continue; 568 continue;
572 } 569 }
573 CGFloat intersectionWidth = 570 CGFloat intersectionWidth =
574 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame)); 571 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame));
575 CGFloat alpha = std::max(static_cast<CGFloat>(0.0), 572 CGFloat alpha = std::max(static_cast<CGFloat>(0.0),
576 intersectionWidth / NSWidth(buttonFrame)); 573 intersectionWidth / NSWidth(buttonFrame));
577 [button setAlphaValue:alpha]; 574 [button setAlphaValue:alpha];
578 [button setNeedsDisplay:YES]; 575 [button setNeedsDisplay:YES];
579 } 576 }
580 } 577 }
581 578
582 - (BrowserActionButton*)buttonForExtension:(const Extension*)extension { 579 - (BrowserActionButton*)buttonForId:(const std::string&)id {
583 NSString* extensionId = base::SysUTF8ToNSString(extension->id()); 580 NSString* nsId = base::SysUTF8ToNSString(id);
584 DCHECK(extensionId); 581 DCHECK(nsId);
585 if (!extensionId) 582 if (!nsId)
586 return nil; 583 return nil;
587 return [buttons_ objectForKey:extensionId]; 584 return [buttons_ objectForKey:nsId];
588 } 585 }
589 586
590 - (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount { 587 - (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount {
591 // Left-side padding which works regardless of whether a button or 588 // Left-side padding which works regardless of whether a button or
592 // chevron leads. 589 // chevron leads.
593 CGFloat width = kBrowserActionLeftPadding; 590 CGFloat width = kBrowserActionLeftPadding;
594 591
595 // Include the buttons and padding between. 592 // Include the buttons and padding between.
596 if (buttonCount > 0) { 593 if (buttonCount > 0) {
597 width += buttonCount * kBrowserActionWidth; 594 width += buttonCount * kBrowserActionWidth;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 - (void)containerDragging:(NSNotification*)notification { 635 - (void)containerDragging:(NSNotification*)notification {
639 [[NSNotificationCenter defaultCenter] 636 [[NSNotificationCenter defaultCenter]
640 postNotificationName:kBrowserActionGrippyDraggingNotification 637 postNotificationName:kBrowserActionGrippyDraggingNotification
641 object:self]; 638 object:self];
642 } 639 }
643 640
644 - (void)containerDragFinished:(NSNotification*)notification { 641 - (void)containerDragFinished:(NSNotification*)notification {
645 for (ExtensionList::const_iterator iter = 642 for (ExtensionList::const_iterator iter =
646 toolbarModel_->toolbar_items().begin(); 643 toolbarModel_->toolbar_items().begin();
647 iter != toolbarModel_->toolbar_items().end(); ++iter) { 644 iter != toolbarModel_->toolbar_items().end(); ++iter) {
648 BrowserActionButton* button = [self buttonForExtension:(iter->get())]; 645 BrowserActionButton* button = [self buttonForId:(iter->get()->id())];
649 NSRect buttonFrame = [button frame]; 646 NSRect buttonFrame = [button frame];
650 if (NSContainsRect([containerView_ bounds], buttonFrame)) 647 if (NSContainsRect([containerView_ bounds], buttonFrame))
651 continue; 648 continue;
652 649
653 CGFloat intersectionWidth = 650 CGFloat intersectionWidth =
654 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame)); 651 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame));
655 // Pad the threshold by 5 pixels in order to have the buttons hide more 652 // Pad the threshold by 5 pixels in order to have the buttons hide more
656 // easily. 653 // easily.
657 if (([containerView_ grippyPinned] && intersectionWidth > 0) || 654 if (([containerView_ grippyPinned] && intersectionWidth > 0) ||
658 (intersectionWidth <= (NSWidth(buttonFrame) / 2) + 5.0)) { 655 (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 674 // Determine what index the dragged button should lie in, alter the model and
678 // reposition the buttons. 675 // reposition the buttons.
679 CGFloat dragThreshold = std::floor(kBrowserActionWidth / 2); 676 CGFloat dragThreshold = std::floor(kBrowserActionWidth / 2);
680 BrowserActionButton* draggedButton = [notification object]; 677 BrowserActionButton* draggedButton = [notification object];
681 NSRect draggedButtonFrame = [draggedButton frame]; 678 NSRect draggedButtonFrame = [draggedButton frame];
682 679
683 NSUInteger index = 0; 680 NSUInteger index = 0;
684 for (ExtensionList::const_iterator iter = 681 for (ExtensionList::const_iterator iter =
685 toolbarModel_->toolbar_items().begin(); 682 toolbarModel_->toolbar_items().begin();
686 iter != toolbarModel_->toolbar_items().end(); ++iter) { 683 iter != toolbarModel_->toolbar_items().end(); ++iter) {
687 BrowserActionButton* button = [self buttonForExtension:(iter->get())]; 684 BrowserActionButton* button = [self buttonForId:(iter->get()->id())];
688 CGFloat intersectionWidth = 685 CGFloat intersectionWidth =
689 NSWidth(NSIntersectionRect(draggedButtonFrame, [button frame])); 686 NSWidth(NSIntersectionRect(draggedButtonFrame, [button frame]));
690 687
691 if (intersectionWidth > dragThreshold && button != draggedButton && 688 if (intersectionWidth > dragThreshold && button != draggedButton &&
692 ![button isAnimating] && index < [self visibleButtonCount]) { 689 ![button isAnimating] && index < [self visibleButtonCount]) {
693 toolbarModel_->MoveExtensionIcon([draggedButton extension]->id(), index); 690 toolbarModel_->MoveExtensionIcon([draggedButton viewController]->GetId(),
691 index);
694 [self positionActionButtonsAndAnimate:YES]; 692 [self positionActionButtonsAndAnimate:YES];
695 return; 693 return;
696 } 694 }
697 ++index; 695 ++index;
698 } 696 }
699 } 697 }
700 698
701 - (void)actionButtonDragFinished:(NSNotification*)notification { 699 - (void)actionButtonDragFinished:(NSNotification*)notification {
702 [self showChevronIfNecessaryInFrame:[containerView_ frame] animate:YES]; 700 [self showChevronIfNecessaryInFrame:[containerView_ frame] animate:YES];
703 [self positionActionButtonsAndAnimate:YES]; 701 [self positionActionButtonsAndAnimate:YES];
(...skipping 17 matching lines...) Expand all
721 } 719 }
722 } else if (![hiddenButtons_ containsObject:button]) { 720 } else if (![hiddenButtons_ containsObject:button]) {
723 [hiddenButtons_ addObject:button]; 721 [hiddenButtons_ addObject:button];
724 [button removeFromSuperview]; 722 [button removeFromSuperview];
725 [button setAlphaValue:0.0]; 723 [button setAlphaValue:0.0];
726 } 724 }
727 } 725 }
728 726
729 - (BOOL)browserActionClicked:(BrowserActionButton*)button 727 - (BOOL)browserActionClicked:(BrowserActionButton*)button
730 shouldGrant:(BOOL)shouldGrant { 728 shouldGrant:(BOOL)shouldGrant {
731 const Extension* extension = [button extension]; 729 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 } 730 }
750 731
751 - (BOOL)browserActionClicked:(BrowserActionButton*)button { 732 - (BOOL)browserActionClicked:(BrowserActionButton*)button {
752 return [self browserActionClicked:button 733 return [self browserActionClicked:button
753 shouldGrant:YES]; 734 shouldGrant:YES];
754 } 735 }
755 736
756 - (void)showChevronIfNecessaryInFrame:(NSRect)frame animate:(BOOL)animate { 737 - (void)showChevronIfNecessaryInFrame:(NSRect)frame animate:(BOOL)animate {
757 [self setChevronHidden:([self buttonCount] == [self visibleButtonCount]) 738 [self setChevronHidden:([self buttonCount] == [self visibleButtonCount])
758 inFrame:frame 739 inFrame:frame
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 - (void)chevronItemSelected:(id)menuItem { 807 - (void)chevronItemSelected:(id)menuItem {
827 [self browserActionClicked:[menuItem representedObject]]; 808 [self browserActionClicked:[menuItem representedObject]];
828 } 809 }
829 810
830 - (void)updateGrippyCursors { 811 - (void)updateGrippyCursors {
831 [containerView_ setCanDragLeft:[hiddenButtons_ count] > 0]; 812 [containerView_ setCanDragLeft:[hiddenButtons_ count] > 0];
832 [containerView_ setCanDragRight:[self visibleButtonCount] > 0]; 813 [containerView_ setCanDragRight:[self visibleButtonCount] > 0];
833 [[containerView_ window] invalidateCursorRectsForView:containerView_]; 814 [[containerView_ window] invalidateCursorRectsForView:containerView_];
834 } 815 }
835 816
836 - (int)currentTabId { 817 - (content::WebContents*)currentWebContents {
837 content::WebContents* active_tab = 818 return browser_->tab_strip_model()->GetActiveWebContents();
838 browser_->tab_strip_model()->GetActiveWebContents();
839 if (!active_tab)
840 return -1;
841
842 return SessionTabHelper::FromWebContents(active_tab)->session_id().id();
843 } 819 }
844 820
845 #pragma mark - 821 #pragma mark -
846 #pragma mark Testing Methods 822 #pragma mark Testing Methods
847 823
848 - (BrowserActionButton*)buttonWithIndex:(NSUInteger)index { 824 - (BrowserActionButton*)buttonWithIndex:(NSUInteger)index {
849 const extensions::ExtensionList& toolbar_items = 825 const extensions::ExtensionList& toolbar_items =
850 toolbarModel_->toolbar_items(); 826 toolbarModel_->toolbar_items();
851 if (index < toolbar_items.size()) { 827 if (index < toolbar_items.size()) {
852 const Extension* extension = toolbar_items[index].get(); 828 const Extension* extension = toolbar_items[index].get();
853 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())]; 829 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())];
854 } 830 }
855 return nil; 831 return nil;
856 } 832 }
857 833
858 @end 834 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698