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

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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 // Shows or hides the chevron, animating as specified by |animate|. 158 // Shows or hides the chevron, animating as specified by |animate|.
160 - (void)setChevronHidden:(BOOL)hidden 159 - (void)setChevronHidden:(BOOL)hidden
161 inFrame:(NSRect)frame 160 inFrame:(NSRect)frame
162 animate:(BOOL)animate; 161 animate:(BOOL)animate;
163 162
164 // Handles when a menu item within the chevron overflow menu is selected. 163 // Handles when a menu item within the chevron overflow menu is selected.
165 - (void)chevronItemSelected:(id)menuItem; 164 - (void)chevronItemSelected:(id)menuItem;
166 165
167 // Updates the container's grippy cursor based on the number of hidden buttons. 166 // Updates the container's grippy cursor based on the number of hidden buttons.
168 - (void)updateGrippyCursors; 167 - (void)updateGrippyCursors;
169
170 // Returns the ID of the currently selected tab or -1 if none exists.
171 - (int)currentTabId;
172 @end 168 @end
173 169
174 // A helper class to proxy extension notifications to the view controller's 170 // A helper class to proxy extension notifications to the view controller's
175 // appropriate methods. 171 // appropriate methods.
176 class ExtensionServiceObserverBridge 172 class ExtensionServiceObserverBridge
177 : public content::NotificationObserver, 173 : public content::NotificationObserver,
178 public extensions::ExtensionToolbarModel::Observer { 174 public extensions::ExtensionToolbarModel::Observer {
179 public: 175 public:
180 ExtensionServiceObserverBridge(BrowserActionsController* owner, 176 ExtensionServiceObserverBridge(BrowserActionsController* owner,
181 Browser* browser) 177 Browser* browser)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 } 220 }
225 221
226 void ToolbarExtensionRemoved(const Extension* extension) override { 222 void ToolbarExtensionRemoved(const Extension* extension) override {
227 [owner_ removeActionButtonForExtension:extension]; 223 [owner_ removeActionButtonForExtension:extension];
228 [owner_ resizeContainerAndAnimate:NO]; 224 [owner_ resizeContainerAndAnimate:NO];
229 } 225 }
230 226
231 void ToolbarExtensionMoved(const Extension* extension, int index) override {} 227 void ToolbarExtensionMoved(const Extension* extension, int index) override {}
232 228
233 void ToolbarExtensionUpdated(const Extension* extension) override { 229 void ToolbarExtensionUpdated(const Extension* extension) override {
234 BrowserActionButton* button = [owner_ buttonForExtension:extension]; 230 BrowserActionButton* button = [owner_ buttonForId:extension->id()];
235 if (button) 231 if (button)
236 [button updateState]; 232 [button updateState];
237 } 233 }
238 234
239 bool ShowExtensionActionPopup(const Extension* extension, 235 bool ShowExtensionActionPopup(const Extension* extension,
240 bool grant_active_tab) override { 236 bool grant_active_tab) override {
241 // Do not override other popups and only show in active window. 237 // Do not override other popups and only show in active window.
242 ExtensionPopupController* popup = [ExtensionPopupController popup]; 238 ExtensionPopupController* popup = [ExtensionPopupController popup];
243 if (popup || !browser_->window()->IsActive()) 239 if (popup || !browser_->window()->IsActive())
244 return false; 240 return false;
245 241
246 BrowserActionButton* button = [owner_ buttonForExtension:extension]; 242 BrowserActionButton* button = [owner_ buttonForId:extension->id()];
247 return button && [owner_ browserActionClicked:button 243 return button && [button viewController]->ExecuteAction(grant_active_tab);
248 shouldGrant:grant_active_tab];
249 } 244 }
250 245
251 void ToolbarVisibleCountChanged() override {} 246 void ToolbarVisibleCountChanged() override {}
252 247
253 void ToolbarHighlightModeChanged(bool is_highlighting) override {} 248 void ToolbarHighlightModeChanged(bool is_highlighting) override {}
254 249
255 Browser* GetBrowser() override { return browser_; } 250 Browser* GetBrowser() override { return browser_; }
256 251
257 private: 252 private:
258 // The object we need to inform when we get a notification. Weak. Owns us. 253 // The object we need to inform when we get a notification. Weak. Owns us.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 335
341 - (void)dealloc { 336 - (void)dealloc {
342 if (toolbarModel_) 337 if (toolbarModel_)
343 toolbarModel_->RemoveObserver(observer_.get()); 338 toolbarModel_->RemoveObserver(observer_.get());
344 339
345 [[NSNotificationCenter defaultCenter] removeObserver:self]; 340 [[NSNotificationCenter defaultCenter] removeObserver:self];
346 [super dealloc]; 341 [super dealloc];
347 } 342 }
348 343
349 - (void)update { 344 - (void)update {
350 for (BrowserActionButton* button in [buttons_ allValues]) { 345 for (BrowserActionButton* button in [buttons_ allValues])
351 [button setTabId:[self currentTabId]];
352 [button updateState]; 346 [button updateState];
353 }
354 } 347 }
355 348
356 - (NSUInteger)buttonCount { 349 - (NSUInteger)buttonCount {
357 return [buttons_ count]; 350 return [buttons_ count];
358 } 351 }
359 352
360 - (NSUInteger)visibleButtonCount { 353 - (NSUInteger)visibleButtonCount {
361 return [self buttonCount] - [hiddenButtons_ count]; 354 return [self buttonCount] - [hiddenButtons_ count];
362 } 355 }
363 356
364 - (void)resizeContainerAndAnimate:(BOOL)animate { 357 - (void)resizeContainerAndAnimate:(BOOL)animate {
365 int iconCount = toolbarModel_->GetVisibleIconCount(); 358 int iconCount = toolbarModel_->GetVisibleIconCount();
366 if (iconCount < 0) // If no buttons are hidden. 359 if (iconCount < 0) // If no buttons are hidden.
367 iconCount = [self buttonCount]; 360 iconCount = [self buttonCount];
368 361
369 [containerView_ resizeToWidth:[self containerWidthWithButtonCount:iconCount] 362 [containerView_ resizeToWidth:[self containerWidthWithButtonCount:iconCount]
370 animate:animate]; 363 animate:animate];
371 NSRect frame = animate ? [containerView_ animationEndFrame] : 364 NSRect frame = animate ? [containerView_ animationEndFrame] :
372 [containerView_ frame]; 365 [containerView_ frame];
373 366
374 [self showChevronIfNecessaryInFrame:frame animate:animate]; 367 [self showChevronIfNecessaryInFrame:frame animate:animate];
375 368
376 if (!animate) { 369 if (!animate) {
377 [[NSNotificationCenter defaultCenter] 370 [[NSNotificationCenter defaultCenter]
378 postNotificationName:kBrowserActionVisibilityChangedNotification 371 postNotificationName:kBrowserActionVisibilityChangedNotification
379 object:self]; 372 object:self];
380 } 373 }
381 } 374 }
382 375
383 - (NSView*)browserActionViewForExtension:(const Extension*)extension {
384 for (BrowserActionButton* button in [buttons_ allValues]) {
385 if ([button extension] == extension)
386 return button;
387 }
388 NOTREACHED();
389 return nil;
390 }
391
392 - (CGFloat)savedWidth { 376 - (CGFloat)savedWidth {
393 if (!toolbarModel_) 377 if (!toolbarModel_)
394 return 0; 378 return 0;
395 379
396 int savedButtonCount = toolbarModel_->GetVisibleIconCount(); 380 int savedButtonCount = toolbarModel_->GetVisibleIconCount();
397 if (savedButtonCount < 0 || // all icons are visible 381 if (savedButtonCount < 0 || // all icons are visible
398 static_cast<NSUInteger>(savedButtonCount) > [self buttonCount]) 382 static_cast<NSUInteger>(savedButtonCount) > [self buttonCount])
399 savedButtonCount = [self buttonCount]; 383 savedButtonCount = [self buttonCount];
400 return [self containerWidthWithButtonCount:savedButtonCount]; 384 return [self containerWidthWithButtonCount:savedButtonCount];
401 } 385 }
402 386
403 - (NSPoint)popupPointForBrowserAction:(const Extension*)extension { 387 - (NSPoint)popupPointForId:(const std::string&)id {
404 if (!extensions::ExtensionActionManager::Get(profile_)-> 388 NSButton* button = [self buttonForId:id];
405 GetBrowserAction(*extension)) {
406 return NSZeroPoint;
407 }
408
409 NSButton* button = [self buttonForExtension:extension];
410 if (!button) 389 if (!button)
411 return NSZeroPoint; 390 return NSZeroPoint;
412 391
413 if ([hiddenButtons_ containsObject:button]) 392 if ([hiddenButtons_ containsObject:button])
414 button = chevronMenuButton_.get(); 393 button = chevronMenuButton_.get();
415 394
416 // Anchor point just above the center of the bottom. 395 // Anchor point just above the center of the bottom.
417 const NSRect bounds = [button bounds]; 396 const NSRect bounds = [button bounds];
418 DCHECK([button isFlipped]); 397 DCHECK([button isFlipped]);
419 NSPoint anchor = NSMakePoint(NSMidX(bounds), 398 NSPoint anchor = NSMakePoint(NSMidX(bounds),
(...skipping 17 matching lines...) Expand all
437 if (effect == NSViewAnimationFadeInEffect) { 416 if (effect == NSViewAnimationFadeInEffect) {
438 return NO; 417 return NO;
439 } else if (effect == NSViewAnimationFadeOutEffect) { 418 } else if (effect == NSViewAnimationFadeOutEffect) {
440 return YES; 419 return YES;
441 } 420 }
442 421
443 NOTREACHED(); 422 NOTREACHED();
444 return YES; 423 return YES;
445 } 424 }
446 425
447 - (void)activateBrowserAction:(const std::string&)extension_id { 426 - (void)activateBrowserAction:(const std::string&)id {
448 const Extension* extension = extensions::ExtensionRegistry::Get( 427 BrowserActionButton* button = [self buttonForId:id];
449 browser_->profile())->enabled_extensions().GetByID(extension_id);
450 if (!extension)
451 return;
452
453 BrowserActionButton* button = [self buttonForExtension:extension];
454 // |button| can be nil when the browser action has its button hidden. 428 // |button| can be nil when the browser action has its button hidden.
455 if (button) 429 if (button)
456 [self browserActionClicked:button]; 430 [self browserActionClicked:button];
457 } 431 }
458 432
433 - (content::WebContents*)currentWebContents {
434 return browser_->tab_strip_model()->GetActiveWebContents();
435 }
436
459 #pragma mark - 437 #pragma mark -
460 #pragma mark NSMenuDelegate 438 #pragma mark NSMenuDelegate
461 439
462 - (void)menuNeedsUpdate:(NSMenu*)menu { 440 - (void)menuNeedsUpdate:(NSMenu*)menu {
463 [menu removeAllItems]; 441 [menu removeAllItems];
464 442
465 // See menu_button.h for documentation on why this is needed. 443 // See menu_button.h for documentation on why this is needed.
466 [menu addItemWithTitle:@"" action:nil keyEquivalent:@""]; 444 [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
467 445
468 for (BrowserActionButton* button in hiddenButtons_.get()) { 446 for (BrowserActionButton* button in hiddenButtons_.get()) {
469 NSString* name = base::SysUTF8ToNSString([button extension]->name()); 447 NSString* name =
448 base::SysUTF16ToNSString([button viewController]->GetActionName());
470 NSMenuItem* item = 449 NSMenuItem* item =
471 [menu addItemWithTitle:name 450 [menu addItemWithTitle:name
472 action:@selector(chevronItemSelected:) 451 action:@selector(chevronItemSelected:)
473 keyEquivalent:@""]; 452 keyEquivalent:@""];
474 [item setRepresentedObject:button]; 453 [item setRepresentedObject:button];
475 [item setImage:[button compositedImage]]; 454 [item setImage:[button compositedImage]];
476 [item setTarget:self]; 455 [item setTarget:self];
477 [item setEnabled:[button isEnabled]]; 456 [item setEnabled:[button isEnabled]];
478 } 457 }
479 } 458 }
(...skipping 18 matching lines...) Expand all
498 477
499 - (void)createActionButtonForExtension:(const Extension*)extension 478 - (void)createActionButtonForExtension:(const Extension*)extension
500 withIndex:(NSUInteger)index { 479 withIndex:(NSUInteger)index {
501 // Show the container if it's the first button. Otherwise it will be shown 480 // Show the container if it's the first button. Otherwise it will be shown
502 // already. 481 // already.
503 if ([self buttonCount] == 0) 482 if ([self buttonCount] == 0)
504 [containerView_ setHidden:NO]; 483 [containerView_ setHidden:NO];
505 484
506 NSRect buttonFrame = NSMakeRect(0.0, kBrowserActionOriginYOffset, 485 NSRect buttonFrame = NSMakeRect(0.0, kBrowserActionOriginYOffset,
507 kBrowserActionWidth, kBrowserActionHeight); 486 kBrowserActionWidth, kBrowserActionHeight);
487 ExtensionAction* extensionAction =
488 extensions::ExtensionActionManager::Get(browser_->profile())->
489 GetExtensionAction(*extension);
490 DCHECK(extensionAction)
491 << "Don't create a BrowserActionButton if there is no browser action.";
492 scoped_ptr<ToolbarActionViewController> viewController(
493 new ExtensionActionViewController(extension, browser_, extensionAction));
494 // TODO(devlin): Move ContextMenuController stuff to
495 // ExtensionActionViewController.
496 ExtensionActionContextMenuController* menuController =
497 [[ExtensionActionContextMenuController alloc]
498 initWithExtension:extension
499 browser:browser_
500 extensionAction:extensionAction];
508 BrowserActionButton* newButton = 501 BrowserActionButton* newButton =
509 [[[BrowserActionButton alloc] 502 [[[BrowserActionButton alloc]
510 initWithFrame:buttonFrame 503 initWithFrame:buttonFrame
511 extension:extension 504 viewController:viewController.Pass()
512 browser:browser_ 505 controller:self
513 tabId:[self currentTabId]] autorelease]; 506 menuController:menuController] autorelease];
514 [newButton setTarget:self]; 507 [newButton setTarget:self];
515 [newButton setAction:@selector(browserActionClicked:)]; 508 [newButton setAction:@selector(browserActionClicked:)];
516 NSString* buttonKey = base::SysUTF8ToNSString(extension->id()); 509 NSString* buttonKey = base::SysUTF8ToNSString(extension->id());
517 if (!buttonKey) 510 if (!buttonKey)
518 return; 511 return;
519 [buttons_ setObject:newButton forKey:buttonKey]; 512 [buttons_ setObject:newButton forKey:buttonKey];
520 513
521 [self positionActionButtonsAndAnimate:NO]; 514 [self positionActionButtonsAndAnimate:NO];
522 515
523 [[NSNotificationCenter defaultCenter] 516 [[NSNotificationCenter defaultCenter]
(...skipping 30 matching lines...) Expand all
554 [containerView_ setMaxWidth: 547 [containerView_ setMaxWidth:
555 [self containerWidthWithButtonCount:[self buttonCount]]]; 548 [self containerWidthWithButtonCount:[self buttonCount]]];
556 [containerView_ setNeedsDisplay:YES]; 549 [containerView_ setNeedsDisplay:YES];
557 } 550 }
558 551
559 - (void)positionActionButtonsAndAnimate:(BOOL)animate { 552 - (void)positionActionButtonsAndAnimate:(BOOL)animate {
560 NSUInteger i = 0; 553 NSUInteger i = 0;
561 for (ExtensionList::const_iterator iter = 554 for (ExtensionList::const_iterator iter =
562 toolbarModel_->toolbar_items().begin(); 555 toolbarModel_->toolbar_items().begin();
563 iter != toolbarModel_->toolbar_items().end(); ++iter) { 556 iter != toolbarModel_->toolbar_items().end(); ++iter) {
564 BrowserActionButton* button = [self buttonForExtension:(iter->get())]; 557 BrowserActionButton* button = [self buttonForId:(iter->get()->id())];
565 if (!button) 558 if (!button)
566 continue; 559 continue;
567 if (![button isBeingDragged]) 560 if (![button isBeingDragged])
568 [self moveButton:button toIndex:i animate:animate]; 561 [self moveButton:button toIndex:i animate:animate];
569 ++i; 562 ++i;
570 } 563 }
571 } 564 }
572 565
573 - (void)updateButtonOpacity { 566 - (void)updateButtonOpacity {
574 for (BrowserActionButton* button in [buttons_ allValues]) { 567 for (BrowserActionButton* button in [buttons_ allValues]) {
575 NSRect buttonFrame = [button frame]; 568 NSRect buttonFrame = [button frame];
576 if (NSContainsRect([containerView_ bounds], buttonFrame)) { 569 if (NSContainsRect([containerView_ bounds], buttonFrame)) {
577 if ([button alphaValue] != 1.0) 570 if ([button alphaValue] != 1.0)
578 [button setAlphaValue:1.0]; 571 [button setAlphaValue:1.0];
579 572
580 continue; 573 continue;
581 } 574 }
582 CGFloat intersectionWidth = 575 CGFloat intersectionWidth =
583 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame)); 576 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame));
584 CGFloat alpha = std::max(static_cast<CGFloat>(0.0), 577 CGFloat alpha = std::max(static_cast<CGFloat>(0.0),
585 intersectionWidth / NSWidth(buttonFrame)); 578 intersectionWidth / NSWidth(buttonFrame));
586 [button setAlphaValue:alpha]; 579 [button setAlphaValue:alpha];
587 [button setNeedsDisplay:YES]; 580 [button setNeedsDisplay:YES];
588 } 581 }
589 } 582 }
590 583
591 - (BrowserActionButton*)buttonForExtension:(const Extension*)extension { 584 - (BrowserActionButton*)buttonForId:(const std::string&)id {
592 NSString* extensionId = base::SysUTF8ToNSString(extension->id()); 585 NSString* nsId = base::SysUTF8ToNSString(id);
593 DCHECK(extensionId); 586 DCHECK(nsId);
594 if (!extensionId) 587 if (!nsId)
595 return nil; 588 return nil;
596 return [buttons_ objectForKey:extensionId]; 589 return [buttons_ objectForKey:nsId];
597 } 590 }
598 591
599 - (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount { 592 - (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount {
600 // Left-side padding which works regardless of whether a button or 593 // Left-side padding which works regardless of whether a button or
601 // chevron leads. 594 // chevron leads.
602 CGFloat width = kBrowserActionLeftPadding; 595 CGFloat width = kBrowserActionLeftPadding;
603 596
604 // Include the buttons and padding between. 597 // Include the buttons and padding between.
605 if (buttonCount > 0) { 598 if (buttonCount > 0) {
606 width += buttonCount * kBrowserActionWidth; 599 width += buttonCount * kBrowserActionWidth;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 - (void)containerDragging:(NSNotification*)notification { 640 - (void)containerDragging:(NSNotification*)notification {
648 [[NSNotificationCenter defaultCenter] 641 [[NSNotificationCenter defaultCenter]
649 postNotificationName:kBrowserActionGrippyDraggingNotification 642 postNotificationName:kBrowserActionGrippyDraggingNotification
650 object:self]; 643 object:self];
651 } 644 }
652 645
653 - (void)containerDragFinished:(NSNotification*)notification { 646 - (void)containerDragFinished:(NSNotification*)notification {
654 for (ExtensionList::const_iterator iter = 647 for (ExtensionList::const_iterator iter =
655 toolbarModel_->toolbar_items().begin(); 648 toolbarModel_->toolbar_items().begin();
656 iter != toolbarModel_->toolbar_items().end(); ++iter) { 649 iter != toolbarModel_->toolbar_items().end(); ++iter) {
657 BrowserActionButton* button = [self buttonForExtension:(iter->get())]; 650 BrowserActionButton* button = [self buttonForId:(iter->get()->id())];
658 NSRect buttonFrame = [button frame]; 651 NSRect buttonFrame = [button frame];
659 if (NSContainsRect([containerView_ bounds], buttonFrame)) 652 if (NSContainsRect([containerView_ bounds], buttonFrame))
660 continue; 653 continue;
661 654
662 CGFloat intersectionWidth = 655 CGFloat intersectionWidth =
663 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame)); 656 NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame));
664 // Pad the threshold by 5 pixels in order to have the buttons hide more 657 // Pad the threshold by 5 pixels in order to have the buttons hide more
665 // easily. 658 // easily.
666 if (([containerView_ grippyPinned] && intersectionWidth > 0) || 659 if (([containerView_ grippyPinned] && intersectionWidth > 0) ||
667 (intersectionWidth <= (NSWidth(buttonFrame) / 2) + 5.0)) { 660 (intersectionWidth <= (NSWidth(buttonFrame) / 2) + 5.0)) {
(...skipping 25 matching lines...) Expand all
693 // Determine what index the dragged button should lie in, alter the model and 686 // Determine what index the dragged button should lie in, alter the model and
694 // reposition the buttons. 687 // reposition the buttons.
695 CGFloat dragThreshold = std::floor(kBrowserActionWidth / 2); 688 CGFloat dragThreshold = std::floor(kBrowserActionWidth / 2);
696 BrowserActionButton* draggedButton = [notification object]; 689 BrowserActionButton* draggedButton = [notification object];
697 NSRect draggedButtonFrame = [draggedButton frame]; 690 NSRect draggedButtonFrame = [draggedButton frame];
698 691
699 NSUInteger index = 0; 692 NSUInteger index = 0;
700 for (ExtensionList::const_iterator iter = 693 for (ExtensionList::const_iterator iter =
701 toolbarModel_->toolbar_items().begin(); 694 toolbarModel_->toolbar_items().begin();
702 iter != toolbarModel_->toolbar_items().end(); ++iter) { 695 iter != toolbarModel_->toolbar_items().end(); ++iter) {
703 BrowserActionButton* button = [self buttonForExtension:(iter->get())]; 696 BrowserActionButton* button = [self buttonForId:(iter->get()->id())];
704 CGFloat intersectionWidth = 697 CGFloat intersectionWidth =
705 NSWidth(NSIntersectionRect(draggedButtonFrame, [button frame])); 698 NSWidth(NSIntersectionRect(draggedButtonFrame, [button frame]));
706 699
707 if (intersectionWidth > dragThreshold && button != draggedButton && 700 if (intersectionWidth > dragThreshold && button != draggedButton &&
708 ![button isAnimating] && index < [self visibleButtonCount]) { 701 ![button isAnimating] && index < [self visibleButtonCount]) {
709 toolbarModel_->MoveExtensionIcon([draggedButton extension]->id(), index); 702 toolbarModel_->MoveExtensionIcon([draggedButton viewController]->GetId(),
703 index);
710 [self positionActionButtonsAndAnimate:YES]; 704 [self positionActionButtonsAndAnimate:YES];
711 return; 705 return;
712 } 706 }
713 ++index; 707 ++index;
714 } 708 }
715 } 709 }
716 710
717 - (void)actionButtonDragFinished:(NSNotification*)notification { 711 - (void)actionButtonDragFinished:(NSNotification*)notification {
718 [self showChevronIfNecessaryInFrame:[containerView_ frame] animate:YES]; 712 [self showChevronIfNecessaryInFrame:[containerView_ frame] animate:YES];
719 [self positionActionButtonsAndAnimate:YES]; 713 [self positionActionButtonsAndAnimate:YES];
(...skipping 17 matching lines...) Expand all
737 } 731 }
738 } else if (![hiddenButtons_ containsObject:button]) { 732 } else if (![hiddenButtons_ containsObject:button]) {
739 [hiddenButtons_ addObject:button]; 733 [hiddenButtons_ addObject:button];
740 [button removeFromSuperview]; 734 [button removeFromSuperview];
741 [button setAlphaValue:0.0]; 735 [button setAlphaValue:0.0];
742 } 736 }
743 } 737 }
744 738
745 - (BOOL)browserActionClicked:(BrowserActionButton*)button 739 - (BOOL)browserActionClicked:(BrowserActionButton*)button
746 shouldGrant:(BOOL)shouldGrant { 740 shouldGrant:(BOOL)shouldGrant {
747 const Extension* extension = [button extension]; 741 return [button viewController]->ExecuteAction(shouldGrant);
748 switch (extensions::ExtensionActionAPI::Get(profile_)->ExecuteExtensionAction(
749 extension, browser_, shouldGrant)) {
750 case ExtensionAction::ACTION_NONE:
751 break;
752 case ExtensionAction::ACTION_SHOW_POPUP: {
753 GURL popupUrl = extensions::ExtensionActionManager::Get(profile_)->
754 GetBrowserAction(*extension)->GetPopupUrl([self currentTabId]);
755 NSPoint arrowPoint = [self popupPointForBrowserAction:extension];
756 [ExtensionPopupController showURL:popupUrl
757 inBrowser:browser_
758 anchoredAt:arrowPoint
759 arrowLocation:info_bubble::kTopRight
760 devMode:NO];
761 return YES;
762 }
763 }
764 return NO;
765 } 742 }
766 743
767 - (BOOL)browserActionClicked:(BrowserActionButton*)button { 744 - (BOOL)browserActionClicked:(BrowserActionButton*)button {
768 return [self browserActionClicked:button 745 return [self browserActionClicked:button
769 shouldGrant:YES]; 746 shouldGrant:YES];
770 } 747 }
771 748
772 - (void)showChevronIfNecessaryInFrame:(NSRect)frame animate:(BOOL)animate { 749 - (void)showChevronIfNecessaryInFrame:(NSRect)frame animate:(BOOL)animate {
773 [self setChevronHidden:([self buttonCount] == [self visibleButtonCount]) 750 [self setChevronHidden:([self buttonCount] == [self visibleButtonCount])
774 inFrame:frame 751 inFrame:frame
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 - (void)chevronItemSelected:(id)menuItem { 819 - (void)chevronItemSelected:(id)menuItem {
843 [self browserActionClicked:[menuItem representedObject]]; 820 [self browserActionClicked:[menuItem representedObject]];
844 } 821 }
845 822
846 - (void)updateGrippyCursors { 823 - (void)updateGrippyCursors {
847 [containerView_ setCanDragLeft:[hiddenButtons_ count] > 0]; 824 [containerView_ setCanDragLeft:[hiddenButtons_ count] > 0];
848 [containerView_ setCanDragRight:[self visibleButtonCount] > 0]; 825 [containerView_ setCanDragRight:[self visibleButtonCount] > 0];
849 [[containerView_ window] invalidateCursorRectsForView:containerView_]; 826 [[containerView_ window] invalidateCursorRectsForView:containerView_];
850 } 827 }
851 828
852 - (int)currentTabId {
853 content::WebContents* active_tab =
854 browser_->tab_strip_model()->GetActiveWebContents();
855 if (!active_tab)
856 return -1;
857
858 return SessionTabHelper::FromWebContents(active_tab)->session_id().id();
859 }
860
861 #pragma mark - 829 #pragma mark -
862 #pragma mark Testing Methods 830 #pragma mark Testing Methods
863 831
864 - (BrowserActionButton*)buttonWithIndex:(NSUInteger)index { 832 - (BrowserActionButton*)buttonWithIndex:(NSUInteger)index {
865 const extensions::ExtensionList& toolbar_items = 833 const extensions::ExtensionList& toolbar_items =
866 toolbarModel_->toolbar_items(); 834 toolbarModel_->toolbar_items();
867 if (index < toolbar_items.size()) { 835 if (index < toolbar_items.size()) {
868 const Extension* extension = toolbar_items[index].get(); 836 const Extension* extension = toolbar_items[index].get();
869 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())]; 837 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())];
870 } 838 }
871 return nil; 839 return nil;
872 } 840 }
873 841
874 @end 842 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698