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
|
// Copyright (c) 2009 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 "chrome/browser/cocoa/tab_strip_controller.h"
#include "app/l10n_util.h"
#include "base/sys_string_conversions.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/profile.h"
#import "chrome/browser/cocoa/tab_strip_view.h"
#import "chrome/browser/cocoa/tab_cell.h"
#import "chrome/browser/cocoa/tab_contents_controller.h"
#import "chrome/browser/cocoa/tab_controller.h"
#import "chrome/browser/cocoa/tab_strip_model_observer_bridge.h"
#import "chrome/browser/cocoa/tab_view.h"
#import "chrome/browser/cocoa/throbber_view.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_contents_view.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "grit/generated_resources.h"
NSString* const kTabStripNumberOfTabsChanged = @"kTabStripNumberOfTabsChanged";
// A simple view class that prevents the windowserver from dragging the
// area behind tabs. Sometimes core animation confuses it.
@interface TabStripControllerDragBlockingView : NSView
@end
@implementation TabStripControllerDragBlockingView
- (BOOL)mouseDownCanMoveWindow {return NO;}
- (void)drawRect:(NSRect)rect {}
@end
@implementation TabStripController
- (id)initWithView:(TabStripView*)view
switchView:(NSView*)switchView
model:(TabStripModel*)model {
DCHECK(view && switchView && model);
if ((self = [super init])) {
tabView_ = view;
switchView_ = switchView;
tabModel_ = model;
bridge_.reset(new TabStripModelObserverBridge(tabModel_, self));
tabContentsArray_.reset([[NSMutableArray alloc] init]);
tabArray_.reset([[NSMutableArray alloc] init]);
// Take the only child view present in the nib as the new tab button. For
// some reason, if the view is present in the nib apriori, it draws
// correctly. If we create it in code and add it to the tab view, it draws
// with all sorts of crazy artifacts.
newTabButton_ = [[tabView_ subviews] objectAtIndex:0];
DCHECK([newTabButton_ isKindOfClass:[NSButton class]]);
[newTabButton_ setTarget:nil];
[newTabButton_ setAction:@selector(commandDispatch:)];
[newTabButton_ setTag:IDC_NEW_TAB];
targetFrames_.reset([[NSMutableDictionary alloc] init]);
[tabView_ setWantsLayer:YES];
dragBlockingView_.reset([[TabStripControllerDragBlockingView alloc]
initWithFrame:NSZeroRect]);
[view addSubview:dragBlockingView_];
newTabTargetFrame_ = NSMakeRect(0, 0, 0, 0);
}
return self;
}
+ (CGFloat)defaultTabHeight {
return 24.0;
}
// Finds the associated TabContentsController at the given |index| and swaps
// out the sole child of the contentArea to display its contents.
- (void)swapInTabAtIndex:(NSInteger)index {
TabContentsController* controller = [tabContentsArray_ objectAtIndex:index];
// Resize the new view to fit the window
NSView* newView = [controller view];
NSRect frame = [switchView_ bounds];
[newView setFrame:frame];
// Remove the old view from the view hierarchy. We know there's only one
// child of |switchView_| because we're the one who put it there. There
// may not be any children in the case of a tab that's been closed, in
// which case there's no swapping going on.
NSArray* subviews = [switchView_ subviews];
if ([subviews count]) {
NSView* oldView = [subviews objectAtIndex:0];
[switchView_ replaceSubview:oldView with:newView];
} else {
[switchView_ addSubview:newView];
}
}
// Create a new tab view and set its cell correctly so it draws the way we want
// it to. It will be sized and positioned by |-layoutTabs| so there's no need to
// set the frame here. This also creates the view as hidden, it will be
// shown during layout.
- (TabController*)newTab {
TabController* controller = [[[TabController alloc] init] autorelease];
[controller setTarget:self];
[controller setAction:@selector(selectTab:)];
[[controller view] setHidden:YES];
return controller;
}
// Returns the number of tabs in the tab strip. This is just the number
// of TabControllers we know about as there's a 1-to-1 mapping from these
// controllers to a tab.
- (NSInteger)numberOfTabViews {
return [tabArray_ count];
}
// Returns the index of the subview |view|. Returns -1 if not present.
- (NSInteger)indexForTabView:(NSView*)view {
NSInteger index = 0;
for (TabController* current in tabArray_.get()) {
if ([current view] == view)
return index;
++index;
}
return -1;
}
// Returns the view at the given index, using the array of TabControllers to
// get the associated view. Returns nil if out of range.
- (NSView*)viewAtIndex:(NSUInteger)index {
if (index >= [tabArray_ count])
return NULL;
return [[tabArray_ objectAtIndex:index] view];
}
// Called when the user clicks a tab. Tell the model the selection has changed,
// which feeds back into us via a notification.
- (void)selectTab:(id)sender {
int index = [self indexForTabView:sender];
if (index >= 0 && tabModel_->ContainsIndex(index))
tabModel_->SelectTabContentsAt(index, true);
}
// Called when the user closes a tab. Asks the model to close the tab.
- (void)closeTab:(id)sender {
int index = [self indexForTabView:sender];
if (tabModel_->ContainsIndex(index)) {
TabContents* contents = tabModel_->GetTabContentsAt(index);
if (contents)
UserMetrics::RecordAction(L"CloseTab_Mouse", contents->profile());
if ([self numberOfTabViews] > 1) {
tabModel_->CloseTabContentsAt(index);
} else {
// Use the standard window close if this is the last tab
// this prevents the tab from being removed from the model until after
// the window dissapears
[[tabView_ window] performClose:nil];
}
}
}
- (void)insertPlaceholderForTab:(TabView*)tab
frame:(NSRect)frame
yStretchiness:(CGFloat)yStretchiness {
placeholderTab_ = tab;
placeholderFrame_ = frame;
placeholderStretchiness_ = yStretchiness;
[self layoutTabs];
}
// Lay out all tabs in the order of their TabContentsControllers, which matches
// the ordering in the TabStripModel. This call isn't that expensive, though
// it is O(n) in the number of tabs. Tabs will animate to their new position
// if the window is visible.
// TODO(pinkerton): Handle drag placeholders via proxy objects, perhaps a
// subclass of TabContentsController with everything stubbed out or by
// abstracting a base class interface.
// TODO(pinkerton): Note this doesn't do too well when the number of min-sized
// tabs would cause an overflow.
- (void)layoutTabs {
const float kIndentLeavingSpaceForControls = 64.0;
const float kTabOverlap = 20.0;
const float kNewTabButtonOffset = 8.0;
const float kMaxTabWidth = [TabController maxTabWidth];
const float kMinTabWidth = [TabController minTabWidth];
NSRect enclosingRect = NSZeroRect;
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.2];
BOOL visible = [[tabView_ window] isVisible];
float availableWidth =
NSWidth([tabView_ frame]) - NSWidth([newTabButton_ frame]);
float offset = kIndentLeavingSpaceForControls;
const float baseTabWidth =
MAX(MIN((availableWidth - offset) / [tabContentsArray_ count],
kMaxTabWidth),
kMinTabWidth);
CGFloat minX = NSMinX(placeholderFrame_);
NSUInteger i = 0;
NSInteger gap = -1;
for (TabController* tab in tabArray_.get()) {
BOOL isPlaceholder = [[tab view] isEqual:placeholderTab_];
NSRect tabFrame = [[tab view] frame];
tabFrame.size.height = [[self class] defaultTabHeight];
tabFrame.origin.y = 0;
tabFrame.origin.x = offset;
// If the tab is hidden, we consider it a new tab. We make it visible
// and animate it in.
BOOL newTab = [[tab view] isHidden];
if (newTab) {
[[tab view] setHidden:NO];
}
if (isPlaceholder) {
// Move the current tab to the correct location intantly.
// We need a duration or else it doesn't cancel an inflight animation.
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.000001];
tabFrame.origin.x = placeholderFrame_.origin.x;
// TODO(alcor): reenable this
//tabFrame.size.height += 10.0 * placeholderStretchiness_;
[[[tab view] animator] setFrame:tabFrame];
[NSAnimationContext endGrouping];
// Store the frame by identifier to aviod redundant calls to animator.
NSValue *identifier = [NSValue valueWithPointer:[tab view]];
[targetFrames_ setObject:[NSValue valueWithRect:tabFrame]
forKey:identifier];
continue;
} else {
// If our left edge is to the left of the placeholder's left, but our mid
// is to the right of it we should slide over to make space for it.
if (placeholderTab_ && gap < 0 && NSMidX(tabFrame) > minX) {
gap = i;
offset += NSWidth(tabFrame);
offset -= kTabOverlap;
tabFrame.origin.x = offset;
}
// Animate the tab in by putting it below the horizon.
if (newTab && visible) {
[[tab view] setFrame:NSOffsetRect(tabFrame, 0, -NSHeight(tabFrame))];
}
id frameTarget = visible ? [[tab view] animator] : [tab view];
tabFrame.size.width = [tab selected] ? kMaxTabWidth : baseTabWidth;
// Check the frame by identifier to avoid redundant calls to animator.
NSValue *identifier = [NSValue valueWithPointer:[tab view]];
NSValue *oldTargetValue = [targetFrames_ objectForKey:identifier];
if (!oldTargetValue ||
!NSEqualRects([oldTargetValue rectValue], tabFrame)) {
[frameTarget setFrame:tabFrame];
[targetFrames_ setObject:[NSValue valueWithRect:tabFrame]
forKey:identifier];
}
enclosingRect = NSUnionRect(tabFrame, enclosingRect);
}
if (offset < availableWidth) {
offset += NSWidth(tabFrame);
offset -= kTabOverlap;
}
i++;
}
NSRect newTabNewFrame = [newTabButton_ frame];
newTabNewFrame.origin =
NSMakePoint(MIN(availableWidth, offset + kNewTabButtonOffset), 0);
newTabNewFrame.origin.x = MAX(newTabNewFrame.origin.x,
NSMaxX(placeholderFrame_));
if (i > 0 && [newTabButton_ isHidden]) {
[[newTabButton_ animator] setHidden:NO];
}
if (!NSEqualRects(newTabTargetFrame_, newTabNewFrame)) {
[newTabButton_ setFrame:newTabNewFrame];
newTabTargetFrame_ = newTabNewFrame;
// Move the new tab button into place.
}
[NSAnimationContext endGrouping];
[dragBlockingView_ setFrame:enclosingRect];
}
// Handles setting the title of the tab based on the given |contents|. Uses
// a canned string if |contents| is NULL.
- (void)setTabTitle:(NSViewController*)tab withContents:(TabContents*)contents {
NSString* titleString = nil;
if (contents)
titleString = base::SysUTF16ToNSString(contents->GetTitle());
if (![titleString length]) {
titleString =
base::SysWideToNSString(
l10n_util::GetString(IDS_BROWSER_WINDOW_MAC_TAB_UNTITLED));
}
[tab setTitle:titleString];
}
// Called when a notification is received from the model to insert a new tab
// at |index|.
- (void)insertTabWithContents:(TabContents*)contents
atIndex:(NSInteger)index
inForeground:(bool)inForeground {
DCHECK(contents);
DCHECK(index == TabStripModel::kNoTab || tabModel_->ContainsIndex(index));
// TODO(pinkerton): handle tab dragging in here
// Make a new tab. Load the contents of this tab from the nib and associate
// the new controller with |contents| so it can be looked up later.
TabContentsController* contentsController =
[[[TabContentsController alloc] initWithNibName:@"TabContents"
contents:contents]
autorelease];
[tabContentsArray_ insertObject:contentsController atIndex:index];
// Make a new tab and add it to the strip. Keep track of its controller.
TabController* newController = [self newTab];
[tabArray_ insertObject:newController atIndex:index];
NSView* newView = [newController view];
// Set the originating frame to just below the strip so that it animates
// upwards as it's being initially layed out. Oddly, this works while doing
// something similar in |-layoutTabs| confuses the window server.
// TODO(pinkerton): I'm not happy with this animiation either, but it's
// a little better that just sliding over (maybe?).
[newView setFrame:NSOffsetRect([newView frame],
0, -[[self class] defaultTabHeight])];
[tabView_ addSubview:newView
positioned:inForeground ? NSWindowAbove : NSWindowBelow
relativeTo:nil];
[self setTabTitle:newController withContents:contents];
// We don't need to call |-layoutTabs| if the tab will be in the foreground
// because it will get called when the new tab is selected by the tab model.
if (!inForeground) {
[self layoutTabs];
}
// Send a broadcast that the number of tabs have changed.
[[NSNotificationCenter defaultCenter]
postNotificationName:kTabStripNumberOfTabsChanged
object:self];
}
// Called when a notification is received from the model to select a particular
// tab. Swaps in the toolbar and content area associated with |newContents|.
- (void)selectTabWithContents:(TabContents*)newContents
previousContents:(TabContents*)oldContents
atIndex:(NSInteger)index
userGesture:(bool)wasUserGesture {
// De-select all other tabs and select the new tab.
int i = 0;
for (TabController* current in tabArray_.get()) {
[current setSelected:(i == index) ? YES : NO];
++i;
}
// Make this the top-most tab in the strips's z order.
NSView* selectedTab = [self viewAtIndex:index];
[tabView_ addSubview:selectedTab positioned:NSWindowAbove relativeTo:nil];
// Tell the new tab contents it is about to become the selected tab. Here it
// can do things like make sure the toolbar is up to date.
TabContentsController* newController =
[tabContentsArray_ objectAtIndex:index];
[newController willBecomeSelectedTab];
// Relayout for new tabs and to let the selected tab grow to be larger in
// size than surrounding tabs if the user has many.
[self layoutTabs];
if (oldContents) {
oldContents->view()->StoreFocus();
oldContents->WasHidden();
}
// Swap in the contents for the new tab
[self swapInTabAtIndex:index];
if (newContents) {
newContents->DidBecomeSelected();
newContents->view()->RestoreFocus();
}
}
// Called when a notification is received from the model that the given tab
// has gone away. Remove all knowledge about this tab and it's associated
// controller and remove the view from the strip.
- (void)tabDetachedWithContents:(TabContents*)contents
atIndex:(NSInteger)index {
// Release the tab contents controller so those views get destroyed. This
// will remove all the tab content Cocoa views from the hierarchy. A
// subsequent "select tab" notification will follow from the model. To
// tell us what to swap in in its absence.
[tabContentsArray_ removeObjectAtIndex:index];
// Remove the |index|th view from the tab strip
NSView* tab = [self viewAtIndex:index];
[tab removeFromSuperview];
NSValue *identifier = [NSValue valueWithPointer:tab];
[targetFrames_ removeObjectForKey:identifier];
// Once we're totally done with the tab, delete its controller
[tabArray_ removeObjectAtIndex:index];
// Send a broadcast that the number of tabs have changed.
[[NSNotificationCenter defaultCenter]
postNotificationName:kTabStripNumberOfTabsChanged
object:self];
[self layoutTabs];
}
// A helper routine for creating an NSImageView to hold the fav icon for
// |contents|.
// TODO(pinkerton): fill in with code to use the real favicon, not the default
// for all cases.
- (NSImageView*)favIconImageViewForContents:(TabContents*)contents {
NSRect iconFrame = NSMakeRect(0, 0, 16, 16);
NSImageView* view = [[[NSImageView alloc] initWithFrame:iconFrame]
autorelease];
[view setImage:[NSImage imageNamed:@"nav"]];
return view;
}
// Called when a notification is received from the model that the given tab
// has been updated. |loading| will be YES when we only want to update the
// throbber state, not anything else about the (partially) loading tab.
- (void)tabChangedWithContents:(TabContents*)contents
atIndex:(NSInteger)index
loadingOnly:(BOOL)loading {
if (!loading)
[self setTabTitle:[tabArray_ objectAtIndex:index] withContents:contents];
// Update the current loading state, replacing the icon with a throbber, or
// vice versa. This will get called repeatedly with the same state during a
// load, so we need to make sure we're not creating the throbber view over and
// over.
if (contents) {
static NSImage* throbberImage = [[NSImage imageNamed:@"throbber"] retain];
static NSImage* throbberWaitingImage =
[[NSImage imageNamed:@"throbber_waiting"] retain];
TabController* tabController = [tabArray_ objectAtIndex:index];
NSImage* image = nil;
if (contents->waiting_for_response() && ![tabController waiting]) {
image = throbberWaitingImage;
[tabController setWaiting:YES];
} else if (contents->is_loading() && ![tabController loading]) {
image = throbberImage;
[tabController setLoading:YES];
}
if (image) {
NSRect frame = NSMakeRect(0, 0, 16, 16);
ThrobberView* throbber =
[[[ThrobberView alloc] initWithFrame:frame image:image] autorelease];
[tabController setIconView:throbber];
}
else if (!contents->is_loading()) {
// Set everything back to normal, we're done loading.
[tabController setIconView:[self favIconImageViewForContents:contents]];
[tabController setWaiting:NO];
[tabController setLoading:NO];
}
}
TabContentsController* updatedController =
[tabContentsArray_ objectAtIndex:index];
[updatedController tabDidChange:contents];
}
// Called when a tab is moved (usually by drag&drop). Keep our parallel arrays
// in sync with the tab strip model.
- (void)tabMovedWithContents:(TabContents*)contents
fromIndex:(NSInteger)from
toIndex:(NSInteger)to {
scoped_nsobject<TabContentsController> movedController(
[[tabContentsArray_ objectAtIndex:from] retain]);
[tabContentsArray_ removeObjectAtIndex:from];
[tabContentsArray_ insertObject:movedController.get() atIndex:to];
scoped_nsobject<TabView> movedView(
[[tabArray_ objectAtIndex:from] retain]);
[tabArray_ removeObjectAtIndex:from];
[tabArray_ insertObject:movedView.get() atIndex:to];
[self layoutTabs];
}
- (void)setFrameOfSelectedTab:(NSRect)frame {
NSView *view = [self selectedTabView];
NSValue *identifier = [NSValue valueWithPointer:view];
[targetFrames_ setObject:[NSValue valueWithRect:frame]
forKey:identifier];
[view setFrame:frame];
}
- (NSView *)selectedTabView {
int selectedIndex = tabModel_->selected_index();
return [self viewAtIndex:selectedIndex];
}
// Find the index based on the x coordinate of the placeholder. If there is
// no placeholder, this returns the end of the tab strip.
- (int)indexOfPlaceholder {
double placeholderX = placeholderFrame_.origin.x;
int index = 0;
int location = 0;
const int count = tabModel_->count();
while (index < count) {
NSView* curr = [self viewAtIndex:index];
// The placeholder tab works by changing the frame of the tab being dragged
// to be the bounds of the placeholder, so we need to skip it while we're
// iterating, otherwise we'll end up off by one. Note This only effects
// dragging to the right, not to the left.
if (curr == placeholderTab_) {
index++;
continue;
}
if (placeholderX <= NSMinX([curr frame]))
break;
index++;
location++;
}
return location;
}
// Move the given tab at index |from| in this window to the location of the
// current placeholder.
- (void)moveTabFromIndex:(NSInteger)from {
int toIndex = [self indexOfPlaceholder];
tabModel_->MoveTabContentsAt(from, toIndex, true);
}
// Drop a given TabContents at the location of the current placeholder. If there
// is no placeholder, it will go at the end. Used when dragging from another
// window when we don't have access to the TabContents as part of our strip.
- (void)dropTabContents:(TabContents*)contents {
int index = [self indexOfPlaceholder];
// Insert it into this tab strip. We want it in the foreground and to not
// inherit the current tab's group.
tabModel_->InsertTabContentsAt(index, contents, true, false);
}
// Return the rect, in WebKit coordinates (flipped), of the window's grow box
// in the coordinate system of the content area of the currently selected tab.
- (NSRect)selectedTabGrowBoxRect {
int selectedIndex = tabModel_->selected_index();
if (selectedIndex == TabStripModel::kNoTab) {
// When the window is initially being constructed, there may be no currently
// selected tab, so pick the first one. If there aren't any, just bail with
// an empty rect.
selectedIndex = 0;
}
TabContentsController* selectedController =
[tabContentsArray_ objectAtIndex:selectedIndex];
if (!selectedController)
return NSZeroRect;
return [selectedController growBoxRect];
}
@end
|