1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "browser_actions_controller.h"
#include <cmath>
#include <string>
#include "app/mac/nsimage_cache.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_toolbar_model.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#import "chrome/browser/ui/cocoa/extensions/browser_action_button.h"
#import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h"
#import "chrome/browser/ui/cocoa/extensions/chevron_menu_button.h"
#import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
#import "chrome/browser/ui/cocoa/menu_button.h"
#include "chrome/common/extensions/extension_action.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/notification_source.h"
#include "chrome/common/pref_names.h"
#include "content/browser/tab_contents/tab_contents.h"
#import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h"
NSString* const kBrowserActionVisibilityChangedNotification =
@"BrowserActionVisibilityChangedNotification";
namespace {
const CGFloat kAnimationDuration = 0.2;
const CGFloat kChevronWidth = 14.0;
// Image used for the overflow button.
NSString* const kOverflowChevronsName =
@"browser_actions_overflow_Template.pdf";
// Since the container is the maximum height of the toolbar, we have
// to move the buttons up by this amount in order to have them look
// vertically centered within the toolbar.
const CGFloat kBrowserActionOriginYOffset = 5.0;
// The size of each button on the toolbar.
const CGFloat kBrowserActionHeight = 29.0;
const CGFloat kBrowserActionWidth = 29.0;
// The padding between browser action buttons.
const CGFloat kBrowserActionButtonPadding = 2.0;
// Padding between Omnibox and first button. Since the buttons have a
// pixel of internal padding, this needs an extra pixel.
const CGFloat kBrowserActionLeftPadding = kBrowserActionButtonPadding + 1.0;
// How far to inset from the bottom of the view to get the top border
// of the popup 2px below the bottom of the Omnibox.
const CGFloat kBrowserActionBubbleYOffset = 3.0;
} // namespace
@interface BrowserActionsController(Private)
// Used during initialization to create the BrowserActionButton objects from the
// stored toolbar model.
- (void)createButtons;
// Creates and then adds the given extension's action button to the container
// at the given index within the container. It does not affect the toolbar model
// object since it is called when the toolbar model changes.
- (void)createActionButtonForExtension:(const Extension*)extension
withIndex:(NSUInteger)index;
// Removes an action button for the given extension from the container. This
// method also does not affect the underlying toolbar model since it is called
// when the toolbar model changes.
- (void)removeActionButtonForExtension:(const Extension*)extension;
// Useful in the case of a Browser Action being added/removed from the middle of
// the container, this method repositions each button according to the current
// toolbar model.
- (void)positionActionButtonsAndAnimate:(BOOL)animate;
// During container resizing, buttons become more transparent as they are pushed
// off the screen. This method updates each button's opacity determined by the
// position of the button.
- (void)updateButtonOpacity;
// Returns the existing button with the given extension backing it; nil if it
// cannot be found or the extension's ID is invalid.
- (BrowserActionButton*)buttonForExtension:(const Extension*)extension;
// Returns the preferred width of the container given the number of visible
// buttons |buttonCount|.
- (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount;
// Returns the number of buttons that can fit in the container according to its
// current size.
- (NSUInteger)containerButtonCapacity;
// Notification handlers for events registered by the class.
// Updates each button's opacity, the cursor rects and chevron position.
- (void)containerFrameChanged:(NSNotification*)notification;
// Hides the chevron and unhides every hidden button so that dragging the
// container out smoothly shows the Browser Action buttons.
- (void)containerDragStart:(NSNotification*)notification;
// Sends a notification for the toolbar to reposition surrounding UI elements.
- (void)containerDragging:(NSNotification*)notification;
// Determines which buttons need to be hidden based on the new size, hides them
// and updates the chevron overflow menu. Also fires a notification to let the
// toolbar know that the drag has finished.
- (void)containerDragFinished:(NSNotification*)notification;
// Updates the image associated with the button should it be within the chevron
// menu.
- (void)actionButtonUpdated:(NSNotification*)notification;
// Adjusts the position of the surrounding action buttons depending on where the
// button is within the container.
- (void)actionButtonDragging:(NSNotification*)notification;
// Updates the position of the Browser Actions within the container. This fires
// when _any_ Browser Action button is done dragging to keep all open windows in
// sync visually.
- (void)actionButtonDragFinished:(NSNotification*)notification;
// Moves the given button both visually and within the toolbar model to the
// specified index.
- (void)moveButton:(BrowserActionButton*)button
toIndex:(NSUInteger)index
animate:(BOOL)animate;
// Handles when the given BrowserActionButton object is clicked.
- (void)browserActionClicked:(BrowserActionButton*)button;
// Returns whether the given extension should be displayed. Only displays
// incognito-enabled extensions in incognito mode. Otherwise returns YES.
- (BOOL)shouldDisplayBrowserAction:(const Extension*)extension;
// The reason |frame| is specified in these chevron functions is because the
// container may be animating and the end frame of the animation should be
// passed instead of the current frame (which may be off and cause the chevron
// to jump at the end of its animation).
// Shows the overflow chevron button depending on whether there are any hidden
// extensions within the frame given.
- (void)showChevronIfNecessaryInFrame:(NSRect)frame animate:(BOOL)animate;
// Moves the chevron to its correct position within |frame|.
- (void)updateChevronPositionInFrame:(NSRect)frame;
// Shows or hides the chevron, animating as specified by |animate|.
- (void)setChevronHidden:(BOOL)hidden
inFrame:(NSRect)frame
animate:(BOOL)animate;
// Handles when a menu item within the chevron overflow menu is selected.
- (void)chevronItemSelected:(id)menuItem;
// Clears and then populates the overflow menu based on the contents of
// |hiddenButtons_|.
- (void)updateOverflowMenu;
// Updates the container's grippy cursor based on the number of hidden buttons.
- (void)updateGrippyCursors;
// Returns the ID of the currently selected tab or -1 if none exists.
- (int)currentTabId;
@end
// A helper class to proxy extension notifications to the view controller's
// appropriate methods.
class ExtensionServiceObserverBridge : public NotificationObserver,
public ExtensionToolbarModel::Observer {
public:
ExtensionServiceObserverBridge(BrowserActionsController* owner,
Profile* profile) : owner_(owner) {
registrar_.Add(this, NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE,
Source<Profile>(profile));
}
// Overridden from NotificationObserver.
void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE: {
ExtensionPopupController* popup = [ExtensionPopupController popup];
if (popup && ![popup isClosing])
[popup close];
break;
}
default:
NOTREACHED() << L"Unexpected notification";
}
}
// ExtensionToolbarModel::Observer implementation.
void BrowserActionAdded(const Extension* extension, int index) {
[owner_ createActionButtonForExtension:extension withIndex:index];
[owner_ resizeContainerAndAnimate:NO];
}
void BrowserActionRemoved(const Extension* extension) {
[owner_ removeActionButtonForExtension:extension];
[owner_ resizeContainerAndAnimate:NO];
}
private:
// The object we need to inform when we get a notification. Weak. Owns us.
BrowserActionsController* owner_;
// Used for registering to receive notifications and automatic clean up.
NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(ExtensionServiceObserverBridge);
};
@implementation BrowserActionsController
@synthesize containerView = containerView_;
#pragma mark -
#pragma mark Public Methods
- (id)initWithBrowser:(Browser*)browser
containerView:(BrowserActionsContainerView*)container {
DCHECK(browser && container);
if ((self = [super init])) {
browser_ = browser;
profile_ = browser->profile();
if (!profile_->GetPrefs()->FindPreference(
prefs::kBrowserActionContainerWidth))
[BrowserActionsController registerUserPrefs:profile_->GetPrefs()];
observer_.reset(new ExtensionServiceObserverBridge(self, profile_));
ExtensionService* extensionsService = profile_->GetExtensionService();
// |extensionsService| can be NULL in Incognito.
if (extensionsService) {
toolbarModel_ = extensionsService->toolbar_model();
toolbarModel_->AddObserver(observer_.get());
}
containerView_ = container;
[containerView_ setPostsFrameChangedNotifications:YES];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(containerFrameChanged:)
name:NSViewFrameDidChangeNotification
object:containerView_];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(containerDragStart:)
name:kBrowserActionGrippyDragStartedNotification
object:containerView_];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(containerDragging:)
name:kBrowserActionGrippyDraggingNotification
object:containerView_];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(containerDragFinished:)
name:kBrowserActionGrippyDragFinishedNotification
object:containerView_];
// Listen for a finished drag from any button to make sure each open window
// stays in sync.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(actionButtonDragFinished:)
name:kBrowserActionButtonDragEndNotification
object:nil];
chevronAnimation_.reset([[NSViewAnimation alloc] init]);
[chevronAnimation_ gtm_setDuration:kAnimationDuration
eventMask:NSLeftMouseUpMask];
[chevronAnimation_ setAnimationBlockingMode:NSAnimationNonblocking];
hiddenButtons_.reset([[NSMutableArray alloc] init]);
buttons_.reset([[NSMutableDictionary alloc] init]);
[self createButtons];
[self showChevronIfNecessaryInFrame:[containerView_ frame] animate:NO];
[self updateGrippyCursors];
[container setResizable:!profile_->IsOffTheRecord()];
}
return self;
}
- (void)dealloc {
if (toolbarModel_)
toolbarModel_->RemoveObserver(observer_.get());
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)update {
for (BrowserActionButton* button in [buttons_ allValues]) {
[button setTabId:[self currentTabId]];
[button updateState];
}
}
- (NSUInteger)buttonCount {
return [buttons_ count];
}
- (NSUInteger)visibleButtonCount {
return [self buttonCount] - [hiddenButtons_ count];
}
- (MenuButton*)chevronMenuButton {
return chevronMenuButton_.get();
}
- (void)resizeContainerAndAnimate:(BOOL)animate {
int iconCount = toolbarModel_->GetVisibleIconCount();
if (iconCount < 0) // If no buttons are hidden.
iconCount = [self buttonCount];
[containerView_ resizeToWidth:[self containerWidthWithButtonCount:iconCount]
animate:animate];
NSRect frame = animate ? [containerView_ animationEndFrame] :
[containerView_ frame];
[self showChevronIfNecessaryInFrame:frame animate:animate];
if (!animate) {
[[NSNotificationCenter defaultCenter]
postNotificationName:kBrowserActionVisibilityChangedNotification
object:self];
}
}
- (NSView*)browserActionViewForExtension:(const Extension*)extension {
for (BrowserActionButton* button in [buttons_ allValues]) {
if ([button extension] == extension)
return button;
}
NOTREACHED();
return nil;
}
- (CGFloat)savedWidth {
if (!toolbarModel_)
return 0;
if (!profile_->GetPrefs()->HasPrefPath(prefs::kExtensionToolbarSize)) {
// Migration code to the new VisibleIconCount pref.
// TODO(mpcomplete): remove this at some point.
double predefinedWidth =
profile_->GetPrefs()->GetDouble(prefs::kBrowserActionContainerWidth);
if (predefinedWidth != 0) {
int iconWidth = kBrowserActionWidth + kBrowserActionButtonPadding;
int extraWidth = kChevronWidth;
toolbarModel_->SetVisibleIconCount(
(predefinedWidth - extraWidth) / iconWidth);
}
}
int savedButtonCount = toolbarModel_->GetVisibleIconCount();
if (savedButtonCount < 0 || // all icons are visible
static_cast<NSUInteger>(savedButtonCount) > [self buttonCount])
savedButtonCount = [self buttonCount];
return [self containerWidthWithButtonCount:savedButtonCount];
}
- (NSPoint)popupPointForBrowserAction:(const Extension*)extension {
if (!extension->browser_action())
return NSZeroPoint;
NSButton* button = [self buttonForExtension:extension];
if (!button)
return NSZeroPoint;
if ([hiddenButtons_ containsObject:button])
button = chevronMenuButton_.get();
// Anchor point just above the center of the bottom.
const NSRect bounds = [button bounds];
DCHECK([button isFlipped]);
NSPoint anchor = NSMakePoint(NSMidX(bounds),
NSMaxY(bounds) - kBrowserActionBubbleYOffset);
return [button convertPoint:anchor toView:nil];
}
- (BOOL)chevronIsHidden {
if (!chevronMenuButton_.get())
return YES;
if (![chevronAnimation_ isAnimating])
return [chevronMenuButton_ isHidden];
DCHECK([[chevronAnimation_ viewAnimations] count] > 0);
// The chevron is animating in or out. Determine which one and have the return
// value reflect where the animation is headed.
NSString* effect = [[[chevronAnimation_ viewAnimations] objectAtIndex:0]
valueForKey:NSViewAnimationEffectKey];
if (effect == NSViewAnimationFadeInEffect) {
return NO;
} else if (effect == NSViewAnimationFadeOutEffect) {
return YES;
}
NOTREACHED();
return YES;
}
+ (void)registerUserPrefs:(PrefService*)prefs {
prefs->RegisterDoublePref(prefs::kBrowserActionContainerWidth, 0);
}
#pragma mark -
#pragma mark Private Methods
- (void)createButtons {
if (!toolbarModel_)
return;
NSUInteger i = 0;
for (ExtensionList::iterator iter = toolbarModel_->begin();
iter != toolbarModel_->end(); ++iter) {
if (![self shouldDisplayBrowserAction:*iter])
continue;
[self createActionButtonForExtension:*iter withIndex:i++];
}
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(actionButtonUpdated:)
name:kBrowserActionButtonUpdatedNotification
object:nil];
CGFloat width = [self savedWidth];
[containerView_ resizeToWidth:width animate:NO];
}
- (void)createActionButtonForExtension:(const Extension*)extension
withIndex:(NSUInteger)index {
if (!extension->browser_action())
return;
if (![self shouldDisplayBrowserAction:extension])
return;
if (profile_->IsOffTheRecord())
index = toolbarModel_->OriginalIndexToIncognito(index);
// Show the container if it's the first button. Otherwise it will be shown
// already.
if ([self buttonCount] == 0)
[containerView_ setHidden:NO];
NSRect buttonFrame = NSMakeRect(0.0, kBrowserActionOriginYOffset,
kBrowserActionWidth, kBrowserActionHeight);
BrowserActionButton* newButton =
[[[BrowserActionButton alloc]
initWithFrame:buttonFrame
extension:extension
profile:profile_
tabId:[self currentTabId]] autorelease];
[newButton setTarget:self];
[newButton setAction:@selector(browserActionClicked:)];
NSString* buttonKey = base::SysUTF8ToNSString(extension->id());
if (!buttonKey)
return;
[buttons_ setObject:newButton forKey:buttonKey];
[self positionActionButtonsAndAnimate:NO];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(actionButtonDragging:)
name:kBrowserActionButtonDraggingNotification
object:newButton];
[containerView_ setMaxWidth:
[self containerWidthWithButtonCount:[self buttonCount]]];
[containerView_ setNeedsDisplay:YES];
}
- (void)removeActionButtonForExtension:(const Extension*)extension {
if (!extension->browser_action())
return;
NSString* buttonKey = base::SysUTF8ToNSString(extension->id());
if (!buttonKey)
return;
BrowserActionButton* button = [buttons_ objectForKey:buttonKey];
// This could be the case in incognito, where only a subset of extensions are
// shown.
if (!button)
return;
[button removeFromSuperview];
// It may or may not be hidden, but it won't matter to NSMutableArray either
// way.
[hiddenButtons_ removeObject:button];
[self updateOverflowMenu];
[buttons_ removeObjectForKey:buttonKey];
if ([self buttonCount] == 0) {
// No more buttons? Hide the container.
[containerView_ setHidden:YES];
} else {
[self positionActionButtonsAndAnimate:NO];
}
[containerView_ setMaxWidth:
[self containerWidthWithButtonCount:[self buttonCount]]];
[containerView_ setNeedsDisplay:YES];
}
- (void)positionActionButtonsAndAnimate:(BOOL)animate {
NSUInteger i = 0;
for (ExtensionList::iterator iter = toolbarModel_->begin();
iter != toolbarModel_->end(); ++iter) {
if (![self shouldDisplayBrowserAction:*iter])
continue;
BrowserActionButton* button = [self buttonForExtension:(*iter)];
if (!button)
continue;
if (![button isBeingDragged])
[self moveButton:button toIndex:i animate:animate];
++i;
}
}
- (void)updateButtonOpacity {
for (BrowserActionButton* button in [buttons_ allValues]) {
NSRect buttonFrame = [button frame];
if (NSContainsRect([containerView_ bounds], buttonFrame)) {
if ([button alphaValue] != 1.0)
[button setAlphaValue:1.0];
continue;
}
CGFloat intersectionWidth =
NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame));
CGFloat alpha = std::max(0.0f, intersectionWidth / NSWidth(buttonFrame));
[button setAlphaValue:alpha];
[button setNeedsDisplay:YES];
}
}
- (BrowserActionButton*)buttonForExtension:(const Extension*)extension {
NSString* extensionId = base::SysUTF8ToNSString(extension->id());
DCHECK(extensionId);
if (!extensionId)
return nil;
return [buttons_ objectForKey:extensionId];
}
- (CGFloat)containerWidthWithButtonCount:(NSUInteger)buttonCount {
// Left-side padding which works regardless of whether a button or
// chevron leads.
CGFloat width = kBrowserActionLeftPadding;
// Include the buttons and padding between.
if (buttonCount > 0) {
width += buttonCount * kBrowserActionWidth;
width += (buttonCount - 1) * kBrowserActionButtonPadding;
}
// Make room for the chevron if any buttons are hidden.
if ([self buttonCount] != [self visibleButtonCount]) {
// Chevron and buttons both include 1px padding w/in their bounds,
// so this leaves 2px between the last browser action and chevron,
// and also works right if the chevron is the only button.
width += kChevronWidth;
}
return width;
}
- (NSUInteger)containerButtonCapacity {
// Edge-to-edge span of the browser action buttons.
CGFloat actionSpan = [self savedWidth] - kBrowserActionLeftPadding;
// Add in some padding for the browser action on the end, then
// divide out to get the number of action buttons that fit.
return (actionSpan + kBrowserActionButtonPadding) /
(kBrowserActionWidth + kBrowserActionButtonPadding);
}
- (void)containerFrameChanged:(NSNotification*)notification {
[self updateButtonOpacity];
[[containerView_ window] invalidateCursorRectsForView:containerView_];
[self updateChevronPositionInFrame:[containerView_ frame]];
}
- (void)containerDragStart:(NSNotification*)notification {
[self setChevronHidden:YES inFrame:[containerView_ frame] animate:YES];
while([hiddenButtons_ count] > 0) {
[containerView_ addSubview:[hiddenButtons_ objectAtIndex:0]];
[hiddenButtons_ removeObjectAtIndex:0];
}
}
- (void)containerDragging:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter]
postNotificationName:kBrowserActionGrippyDraggingNotification
object:self];
}
- (void)containerDragFinished:(NSNotification*)notification {
for (ExtensionList::iterator iter = toolbarModel_->begin();
iter != toolbarModel_->end(); ++iter) {
BrowserActionButton* button = [self buttonForExtension:(*iter)];
NSRect buttonFrame = [button frame];
if (NSContainsRect([containerView_ bounds], buttonFrame))
continue;
CGFloat intersectionWidth =
NSWidth(NSIntersectionRect([containerView_ bounds], buttonFrame));
// Pad the threshold by 5 pixels in order to have the buttons hide more
// easily.
if (([containerView_ grippyPinned] && intersectionWidth > 0) ||
(intersectionWidth <= (NSWidth(buttonFrame) / 2) + 5.0)) {
[button setAlphaValue:0.0];
[button removeFromSuperview];
[hiddenButtons_ addObject:button];
}
}
[self updateOverflowMenu];
[self updateGrippyCursors];
if (!profile_->IsOffTheRecord())
toolbarModel_->SetVisibleIconCount([self visibleButtonCount]);
[[NSNotificationCenter defaultCenter]
postNotificationName:kBrowserActionGrippyDragFinishedNotification
object:self];
}
- (void)actionButtonUpdated:(NSNotification*)notification {
BrowserActionButton* button = [notification object];
if (![hiddenButtons_ containsObject:button])
return;
// +1 item because of the title placeholder. See |updateOverflowMenu|.
NSUInteger menuIndex = [hiddenButtons_ indexOfObject:button] + 1;
NSMenuItem* item = [[chevronMenuButton_ attachedMenu] itemAtIndex:menuIndex];
DCHECK(button == [item representedObject]);
[item setImage:[button compositedImage]];
}
- (void)actionButtonDragging:(NSNotification*)notification {
if (![self chevronIsHidden])
[self setChevronHidden:YES inFrame:[containerView_ frame] animate:YES];
// Determine what index the dragged button should lie in, alter the model and
// reposition the buttons.
CGFloat dragThreshold = std::floor(kBrowserActionWidth / 2);
BrowserActionButton* draggedButton = [notification object];
NSRect draggedButtonFrame = [draggedButton frame];
NSUInteger index = 0;
for (ExtensionList::iterator iter = toolbarModel_->begin();
iter != toolbarModel_->end(); ++iter) {
BrowserActionButton* button = [self buttonForExtension:(*iter)];
CGFloat intersectionWidth =
NSWidth(NSIntersectionRect(draggedButtonFrame, [button frame]));
if (intersectionWidth > dragThreshold && button != draggedButton &&
![button isAnimating] && index < [self visibleButtonCount]) {
toolbarModel_->MoveBrowserAction([draggedButton extension], index);
[self positionActionButtonsAndAnimate:YES];
return;
}
++index;
}
}
- (void)actionButtonDragFinished:(NSNotification*)notification {
[self showChevronIfNecessaryInFrame:[containerView_ frame] animate:YES];
[self positionActionButtonsAndAnimate:YES];
}
- (void)moveButton:(BrowserActionButton*)button
toIndex:(NSUInteger)index
animate:(BOOL)animate {
CGFloat xOffset = kBrowserActionLeftPadding +
(index * (kBrowserActionWidth + kBrowserActionButtonPadding));
NSRect buttonFrame = [button frame];
buttonFrame.origin.x = xOffset;
[button setFrame:buttonFrame animate:animate];
if (index < [self containerButtonCapacity]) {
// Make sure the button is within the visible container.
if ([button superview] != containerView_) {
[containerView_ addSubview:button];
[button setAlphaValue:1.0];
[hiddenButtons_ removeObjectIdenticalTo:button];
}
} else if (![hiddenButtons_ containsObject:button]) {
[hiddenButtons_ addObject:button];
[button removeFromSuperview];
[button setAlphaValue:0.0];
[self updateOverflowMenu];
}
}
- (void)browserActionClicked:(BrowserActionButton*)button {
int tabId = [self currentTabId];
if (tabId < 0) {
NOTREACHED() << "No current tab.";
return;
}
ExtensionAction* action = [button extension]->browser_action();
if (action->HasPopup(tabId)) {
GURL popupUrl = action->GetPopupUrl(tabId);
// If a popup is already showing, check if the popup URL is the same. If so,
// then close the popup.
ExtensionPopupController* popup = [ExtensionPopupController popup];
if (popup &&
[[popup window] isVisible] &&
[popup extensionHost]->GetURL() == popupUrl) {
[popup close];
return;
}
NSPoint arrowPoint = [self popupPointForBrowserAction:[button extension]];
[ExtensionPopupController showURL:popupUrl
inBrowser:browser_
anchoredAt:arrowPoint
arrowLocation:info_bubble::kTopRight
devMode:NO];
} else {
ExtensionService* service = profile_->GetExtensionService();
service->browser_event_router()->BrowserActionExecuted(
profile_, action->extension_id(), browser_);
}
}
- (BOOL)shouldDisplayBrowserAction:(const Extension*)extension {
// Only display incognito-enabled extensions while in incognito mode.
return (!profile_->IsOffTheRecord() ||
profile_->GetExtensionService()->IsIncognitoEnabled(extension));
}
- (void)showChevronIfNecessaryInFrame:(NSRect)frame animate:(BOOL)animate {
[self setChevronHidden:([self buttonCount] == [self visibleButtonCount])
inFrame:frame
animate:animate];
}
- (void)updateChevronPositionInFrame:(NSRect)frame {
CGFloat xPos = NSWidth(frame) - kChevronWidth;
NSRect buttonFrame = NSMakeRect(xPos,
kBrowserActionOriginYOffset,
kChevronWidth,
kBrowserActionHeight);
[chevronMenuButton_ setFrame:buttonFrame];
}
- (void)setChevronHidden:(BOOL)hidden
inFrame:(NSRect)frame
animate:(BOOL)animate {
if (hidden == [self chevronIsHidden])
return;
if (!chevronMenuButton_.get()) {
chevronMenuButton_.reset([[ChevronMenuButton alloc] init]);
[chevronMenuButton_ setBordered:NO];
[chevronMenuButton_ setShowsBorderOnlyWhileMouseInside:YES];
NSImage* chevronImage =
app::mac::GetCachedImageWithName(kOverflowChevronsName);
[chevronMenuButton_ setImage:chevronImage];
[containerView_ addSubview:chevronMenuButton_];
}
if (!hidden)
[self updateOverflowMenu];
[self updateChevronPositionInFrame:frame];
// Stop any running animation.
[chevronAnimation_ stopAnimation];
if (!animate) {
[chevronMenuButton_ setHidden:hidden];
return;
}
NSDictionary* animationDictionary;
if (hidden) {
animationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
chevronMenuButton_.get(), NSViewAnimationTargetKey,
NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey,
nil];
} else {
[chevronMenuButton_ setHidden:NO];
animationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
chevronMenuButton_.get(), NSViewAnimationTargetKey,
NSViewAnimationFadeInEffect, NSViewAnimationEffectKey,
nil];
}
[chevronAnimation_ setViewAnimations:
[NSArray arrayWithObject:animationDictionary]];
[chevronAnimation_ startAnimation];
}
- (void)chevronItemSelected:(id)menuItem {
[self browserActionClicked:[menuItem representedObject]];
}
- (void)updateOverflowMenu {
overflowMenu_.reset([[NSMenu alloc] initWithTitle:@""]);
// See menu_button.h for documentation on why this is needed.
[overflowMenu_ addItemWithTitle:@"" action:nil keyEquivalent:@""];
for (BrowserActionButton* button in hiddenButtons_.get()) {
NSString* name = base::SysUTF8ToNSString([button extension]->name());
NSMenuItem* item =
[overflowMenu_ addItemWithTitle:name
action:@selector(chevronItemSelected:)
keyEquivalent:@""];
[item setRepresentedObject:button];
[item setImage:[button compositedImage]];
[item setTarget:self];
}
[chevronMenuButton_ setAttachedMenu:overflowMenu_];
}
- (void)updateGrippyCursors {
[containerView_ setCanDragLeft:[hiddenButtons_ count] > 0];
[containerView_ setCanDragRight:[self visibleButtonCount] > 0];
[[containerView_ window] invalidateCursorRectsForView:containerView_];
}
- (int)currentTabId {
TabContents* selected_tab = browser_->GetSelectedTabContents();
if (!selected_tab)
return -1;
return selected_tab->controller().session_id().id();
}
#pragma mark -
#pragma mark Testing Methods
- (NSButton*)buttonWithIndex:(NSUInteger)index {
if (profile_->IsOffTheRecord())
index = toolbarModel_->IncognitoIndexToOriginal(index);
if (index < toolbarModel_->size()) {
const Extension* extension = toolbarModel_->GetExtensionByIndex(index);
return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())];
}
return nil;
}
@end
|