/
PSMTabBarControl.m
1983 lines (1650 loc) · 62.7 KB
/
PSMTabBarControl.m
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// PSMTabBarControl.m
// PSMTabBarControl
//
// Created by John Pannell on 10/13/05.
// Copyright 2005 Positive Spin Media. All rights reserved.
//
#import "PSMTabBarControl.h"
#import "PSMTabBarCell.h"
#import "PSMOverflowPopUpButton.h"
#import "PSMRolloverButton.h"
#import "PSMTabStyle.h"
#import "PSMMetalTabStyle.h"
#import "PSMTabDragAssistant.h"
#import "PSMTabBarController.h"
@interface PSMTabBarControl (Private)
// constructor/destructor
- (void)initAddedProperties;
// accessors
- (NSEvent *)lastMouseDownEvent;
- (void)setLastMouseDownEvent:(NSEvent *)event;
// contents
- (void)addTabViewItem:(NSTabViewItem *)item;
- (void)removeTabForCell:(PSMTabBarCell *)cell;
// draw
- (void)update;
- (void)update:(BOOL)animate;
- (void)_setupTrackingRectsForCell:(PSMTabBarCell *)cell;
- (void)_positionOverflowMenu;
- (void)_checkWindowFrame;
// actions
- (void)overflowMenuAction:(id)sender;
- (void)closeTabClick:(id)sender;
- (void)tabClick:(id)sender;
- (void)tabNothing:(id)sender;
// notification handlers
- (void)frameDidChange:(NSNotification *)notification;
- (void)windowDidMove:(NSNotification *)aNotification;
- (void)windowDidUpdate:(NSNotification *)notification;
// NSTabView delegate
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)tabView;
// archiving
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
// convenience
- (void)_bindPropertiesForCell:(PSMTabBarCell *)cell andTabViewItem:(NSTabViewItem *)item;
- (id)cellForPoint:(NSPoint)point cellFrame:(NSRectPointer)outFrame;
- (void)_animateCells:(NSTimer *)timer;
@end
@implementation PSMTabBarControl
#pragma mark -
#pragma mark Characteristics
+ (NSBundle *)bundle;
{
static NSBundle *bundle = nil;
if (!bundle) bundle = [NSBundle bundleForClass:[PSMTabBarControl class]];
return bundle;
}
/*!
@method availableCellWidth
@abstract The number of pixels available for cells
@discussion Calculates the number of pixels available for cells based on margins and the window resize badge.
@returns Returns the amount of space for cells.
*/
- (CGFloat)availableCellWidth
{
return [self frame].size.width - [style leftMarginForTabBarControl] - [style rightMarginForTabBarControl] - _resizeAreaCompensation;
}
/*!
@method genericCellRect
@abstract The basic rect for a tab cell.
@discussion Creates a generic frame for a tab cell based on the current control state.
@returns Returns a basic rect for a tab cell.
*/
- (NSRect)genericCellRect
{
NSRect aRect=[self frame];
aRect.origin.x = [style leftMarginForTabBarControl];
aRect.origin.y = 0.0;
aRect.size.width = [self availableCellWidth];
aRect.size.height = [style tabCellHeight];
return aRect;
}
#pragma mark -
#pragma mark Constructor/destructor
- (void)initAddedProperties
{
_cells = [[NSMutableArray alloc] initWithCapacity:10];
_controller = [[PSMTabBarController alloc] initWithTabBarControl:self];
_animationTimer = nil;
// default config
_currentStep = kPSMIsNotBeingResized;
_orientation = PSMTabBarHorizontalOrientation;
_canCloseOnlyTab = NO;
_disableTabClose = NO;
_showAddTabButton = NO;
_hideForSingleTab = NO;
_sizeCellsToFit = NO;
_isHidden = NO;
_awakenedFromNib = NO;
_automaticallyAnimates = NO;
_useOverflowMenu = YES;
_allowsBackgroundTabClosing = YES;
_allowsResizing = NO;
_selectsTabsOnMouseDown = NO;
_alwaysShowActiveTab = NO;
_allowsScrubbing = NO;
_cellMinWidth = 100;
_cellMaxWidth = 280;
_cellOptimumWidth = 130;
_tearOffStyle = PSMTabBarTearOffAlphaWindow;
style = [[PSMMetalTabStyle alloc] init];
// the overflow button/menu
NSRect overflowButtonRect = NSMakeRect([self frame].size.width - [style rightMarginForTabBarControl] + 1, 0, [style rightMarginForTabBarControl] - 1, [self frame].size.height);
_overflowPopUpButton = [[PSMOverflowPopUpButton alloc] initWithFrame:overflowButtonRect pullsDown:YES];
[_overflowPopUpButton setAutoresizingMask:NSViewNotSizable | NSViewMinXMargin];
[_overflowPopUpButton setHidden:YES];
[self addSubview:_overflowPopUpButton];
[self _positionOverflowMenu];
// new tab button
NSRect addTabButtonRect = NSMakeRect([self frame].size.width - [style rightMarginForTabBarControl] + 1, 3.0, 16.0, 16.0);
_addTabButton = [[PSMRolloverButton alloc] initWithFrame:addTabButtonRect];
if (_addTabButton) {
NSImage *newButtonImage = [style addTabButtonImage];
if (newButtonImage) {
[_addTabButton setUsualImage:newButtonImage];
}
newButtonImage = [style addTabButtonPressedImage];
if (newButtonImage) {
[_addTabButton setAlternateImage:newButtonImage];
}
newButtonImage = [style addTabButtonRolloverImage];
if (newButtonImage) {
[_addTabButton setRolloverImage:newButtonImage];
}
[_addTabButton setTitle:@""];
[_addTabButton setImagePosition:NSImageOnly];
[_addTabButton setButtonType:NSMomentaryChangeButton];
[_addTabButton setBordered:NO];
[_addTabButton setBezelStyle:NSShadowlessSquareBezelStyle];
[self addSubview:_addTabButton];
if (_showAddTabButton) {
[_addTabButton setHidden:NO];
} else {
[_addTabButton setHidden:YES];
}
[_addTabButton setNeedsDisplay:YES];
}
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization
[self initAddedProperties];
[self registerForDraggedTypes:[NSArray arrayWithObjects:@"PSMTabBarControlItemPBType", nil]];
// resize
[self setPostsFrameChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(frameDidChange:) name:NSViewFrameDidChangeNotification object:self];
}
[self setTarget:self];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
//stop any animations that may be running
[_animationTimer invalidate];
[_animationTimer release]; _animationTimer = nil;
[_showHideAnimationTimer invalidate];
[_showHideAnimationTimer release]; _showHideAnimationTimer = nil;
//Also unwind the spring, if it's wound.
[_springTimer invalidate];
[_springTimer release]; _springTimer = nil;
//unbind all the items to prevent crashing
//not sure if this is necessary or not
NSEnumerator *enumerator = [[[_cells copy] autorelease] objectEnumerator];
PSMTabBarCell *nextCell;
while ( (nextCell = [enumerator nextObject]) ) {
[self removeTabForCell:nextCell];
}
[_overflowPopUpButton release];
[_cells release];
[_controller release];
[tabView release];
[_addTabButton release];
[partnerView release];
[_lastMouseDownEvent release];
[style release];
[self unregisterDraggedTypes];
[super dealloc];
}
- (void)awakeFromNib
{
// build cells from existing tab view items
NSArray *existingItems = [tabView tabViewItems];
NSEnumerator *e = [existingItems objectEnumerator];
NSTabViewItem *item;
while ( (item = [e nextObject]) ) {
if (![[self representedTabViewItems] containsObject:item]) {
[self addTabViewItem:item];
}
}
}
- (void)viewWillMoveToWindow:(NSWindow *)aWindow
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:NSWindowDidBecomeKeyNotification object:nil];
[center removeObserver:self name:NSWindowDidResignKeyNotification object:nil];
[center removeObserver:self name:NSWindowDidUpdateNotification object:nil];
[center removeObserver:self name:NSWindowDidMoveNotification object:nil];
if (_showHideAnimationTimer)
{
[_showHideAnimationTimer invalidate];
[_showHideAnimationTimer release]; _showHideAnimationTimer = nil;
}
if (aWindow)
{
[center addObserver:self selector:@selector(windowStatusDidChange:) name:NSWindowDidBecomeKeyNotification object:aWindow];
[center addObserver:self selector:@selector(windowStatusDidChange:) name:NSWindowDidResignKeyNotification object:aWindow];
[center addObserver:self selector:@selector(windowDidUpdate:) name:NSWindowDidUpdateNotification object:aWindow];
[center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:aWindow];
}
}
- (void)windowStatusDidChange:(NSNotification *)notification
{
[self setNeedsDisplay:YES];
}
#pragma mark - Accessors
- (NSMutableArray *)cells
{
return _cells;
}
- (NSEvent *)lastMouseDownEvent
{
return _lastMouseDownEvent;
}
- (void)setLastMouseDownEvent:(NSEvent *)event
{
[event retain];
[_lastMouseDownEvent release];
_lastMouseDownEvent = event;
}
- (id)delegate
{
return delegate;
}
- (void)setDelegate:(id)object
{
delegate = object;
NSMutableArray *types = [NSMutableArray arrayWithObject:@"PSMTabBarControlItemPBType"];
//Update the allowed drag types
if ([self delegate] && [[self delegate] respondsToSelector:@selector(allowedDraggedTypesForTabView:)]) {
[types addObjectsFromArray:[[self delegate] allowedDraggedTypesForTabView:tabView]];
}
[self unregisterDraggedTypes];
[self registerForDraggedTypes:types];
}
- (NSTabView *)tabView
{
return tabView;
}
- (void)setTabView:(NSTabView *)view
{
[view retain];
[tabView release];
tabView = view;
}
- (id<PSMTabStyle>)style
{
return style;
}
- (NSString *)styleName
{
return [style name];
}
- (void)setStyle:(id <PSMTabStyle>)newStyle
{
if (style != newStyle)
{
[style autorelease];
style = [newStyle retain];
// restyle add tab button
if (_addTabButton)
{
NSImage *newButtonImage = [style addTabButtonImage];
if (newButtonImage)
{
[_addTabButton setUsualImage:newButtonImage];
}
newButtonImage = [style addTabButtonPressedImage];
if (newButtonImage)
{
[_addTabButton setAlternateImage:newButtonImage];
}
newButtonImage = [style addTabButtonRolloverImage];
if (newButtonImage)
{
[_addTabButton setRolloverImage:newButtonImage];
}
}
[self update];
}
}
- (void)setStyleNamed:(NSString *)name
{
id <PSMTabStyle> newStyle;
newStyle = [[PSMMetalTabStyle alloc] init];
[self setStyle:newStyle];
[newStyle release];
}
- (PSMTabBarOrientation)orientation
{
return _orientation;
}
- (void)setOrientation:(PSMTabBarOrientation)value
{
PSMTabBarOrientation lastOrientation = _orientation;
_orientation = value;
if (_tabBarWidth < 10) {
_tabBarWidth = 120;
}
if (lastOrientation != _orientation) {
[[self style] setOrientation:_orientation];
[self _positionOverflowMenu]; //move the overflow popup button to the right place
[self update:NO];
}
}
- (BOOL)canCloseOnlyTab
{
return _canCloseOnlyTab;
}
- (void)setCanCloseOnlyTab:(BOOL)value
{
_canCloseOnlyTab = value;
if ([_cells count] == 1) {
[self update];
}
}
- (BOOL)disableTabClose
{
return _disableTabClose;
}
- (void)setDisableTabClose:(BOOL)value
{
_disableTabClose = value;
[self update];
}
- (BOOL)hideForSingleTab
{
return _hideForSingleTab;
}
- (void)setHideForSingleTab:(BOOL)value
{
_hideForSingleTab = value;
[self update];
}
- (BOOL)showAddTabButton
{
return _showAddTabButton;
}
- (void)setShowAddTabButton:(BOOL)value
{
_showAddTabButton = value;
if (!NSIsEmptyRect([_controller addButtonRect]))
[_addTabButton setFrame:[_controller addButtonRect]];
[_addTabButton setHidden:!_showAddTabButton];
[_addTabButton setNeedsDisplay:YES];
[self update];
}
- (NSInteger)cellMinWidth
{
return _cellMinWidth;
}
- (void)setCellMinWidth:(NSInteger)value
{
_cellMinWidth = value;
[self update];
}
- (NSInteger)cellMaxWidth
{
return _cellMaxWidth;
}
- (void)setCellMaxWidth:(NSInteger)value
{
_cellMaxWidth = value;
[self update];
}
- (NSInteger)cellOptimumWidth
{
return _cellOptimumWidth;
}
- (void)setCellOptimumWidth:(NSInteger)value
{
_cellOptimumWidth = value;
[self update];
}
- (BOOL)sizeCellsToFit
{
return _sizeCellsToFit;
}
- (void)setSizeCellsToFit:(BOOL)value
{
_sizeCellsToFit = value;
[self update];
}
- (BOOL)useOverflowMenu
{
return _useOverflowMenu;
}
- (void)setUseOverflowMenu:(BOOL)value
{
_useOverflowMenu = value;
[self update];
}
- (PSMRolloverButton *)addTabButton
{
return _addTabButton;
}
- (PSMOverflowPopUpButton *)overflowPopUpButton
{
return _overflowPopUpButton;
}
- (BOOL)allowsBackgroundTabClosing
{
return _allowsBackgroundTabClosing;
}
- (void)setAllowsBackgroundTabClosing:(BOOL)value
{
_allowsBackgroundTabClosing = value;
}
- (BOOL)allowsResizing
{
return _allowsResizing;
}
- (void)setAllowsResizing:(BOOL)value
{
_allowsResizing = value;
}
- (BOOL)selectsTabsOnMouseDown
{
return _selectsTabsOnMouseDown;
}
- (void)setSelectsTabsOnMouseDown:(BOOL)value
{
_selectsTabsOnMouseDown = value;
}
- (BOOL)automaticallyAnimates
{
return _automaticallyAnimates;
}
- (void)setAutomaticallyAnimates:(BOOL)value
{
_automaticallyAnimates = value;
}
- (BOOL)alwaysShowActiveTab
{
return _alwaysShowActiveTab;
}
- (void)setAlwaysShowActiveTab:(BOOL)value
{
_alwaysShowActiveTab = value;
}
- (BOOL)allowsScrubbing
{
return _allowsScrubbing;
}
- (void)setAllowsScrubbing:(BOOL)value
{
_allowsScrubbing = value;
}
- (PSMTabBarTearOffStyle)tearOffStyle
{
return _tearOffStyle;
}
- (void)setTearOffStyle:(PSMTabBarTearOffStyle)tearOffStyle
{
_tearOffStyle = tearOffStyle;
}
#pragma mark -
#pragma mark Functionality
- (void)addTabViewItem:(NSTabViewItem *)item
{
// create cell
PSMTabBarCell *cell = [[PSMTabBarCell alloc] initWithControlView:self];
NSRect cellRect, lastCellFrame = [[_cells lastObject] frame];
if ([self orientation] == PSMTabBarHorizontalOrientation) {
cellRect = [self genericCellRect];
cellRect.size.width = 30;
cellRect.origin.x = lastCellFrame.origin.x + lastCellFrame.size.width;
} else {
cellRect = /*lastCellFrame*/[self genericCellRect];
cellRect.size.width = lastCellFrame.size.width;
cellRect.size.height = 0;
cellRect.origin.y = lastCellFrame.origin.y + lastCellFrame.size.height;
}
[cell setRepresentedObject:item];
[cell setFrame:cellRect];
// bind it up
[self bindPropertiesForCell:cell andTabViewItem:item];
// add to collection
[_cells addObject:cell];
[cell release];
if ([_cells count] == [tabView numberOfTabViewItems]) {
[self update]; // don't update unless all are accounted for!
}
}
- (void)removeTabForCell:(PSMTabBarCell *)cell
{
NSTabViewItem *item = [cell representedObject];
// unbind
[[cell indicator] unbind:@"animate"];
[[cell indicator] unbind:@"hidden"];
[cell unbind:@"hasIcon"];
[cell unbind:@"hasLargeImage"];
[cell unbind:@"title"];
[cell unbind:@"count"];
[cell unbind:@"countColor"];
[cell unbind:@"isEdited"];
if ([item identifier] != nil) {
if ([[item identifier] respondsToSelector:@selector(isProcessing)]) {
[[item identifier] removeObserver:cell forKeyPath:@"isProcessing"];
}
}
if ([item identifier] != nil) {
if ([[item identifier] respondsToSelector:@selector(icon)]) {
[[item identifier] removeObserver:cell forKeyPath:@"icon"];
}
}
if ([item identifier] != nil) {
if ([[item identifier] respondsToSelector:@selector(objectCount)]) {
[[item identifier] removeObserver:cell forKeyPath:@"objectCount"];
}
}
if ([item identifier] != nil) {
if ([[item identifier] respondsToSelector:@selector(countColor)]) {
[[item identifier] removeObserver:cell forKeyPath:@"countColor"];
}
}
if ([item identifier] != nil) {
if ([[item identifier] respondsToSelector:@selector(largeImage)]) {
[[item identifier] removeObserver:cell forKeyPath:@"largeImage"];
}
}
if ([item identifier] != nil) {
if ([[item identifier] respondsToSelector:@selector(isEdited)]) {
[[item identifier] removeObserver:cell forKeyPath:@"isEdited"];
}
}
// stop watching identifier
[item removeObserver:self forKeyPath:@"identifier"];
// remove indicator
if ([[self subviews] containsObject:[cell indicator]]) {
[[cell indicator] removeFromSuperview];
}
// remove tracking
[[NSNotificationCenter defaultCenter] removeObserver:cell];
if ([cell closeButtonTrackingTag] != 0) {
[self removeTrackingRect:[cell closeButtonTrackingTag]];
[cell setCloseButtonTrackingTag:0];
}
if ([cell cellTrackingTag] != 0) {
[self removeTrackingRect:[cell cellTrackingTag]];
[cell setCellTrackingTag:0];
}
// pull from collection
[_cells removeObject:cell];
[self update];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// did the tab's identifier change?
if ([keyPath isEqualToString:@"identifier"]) {
NSEnumerator *e = [_cells objectEnumerator];
PSMTabBarCell *cell;
while ( (cell = [e nextObject]) ) {
if ([cell representedObject] == object) {
[self _bindPropertiesForCell:cell andTabViewItem:object];
}
}
}
}
#pragma mark -
#pragma mark Hide/Show
- (void)hideTabBar:(BOOL)hide animate:(BOOL)animate
{
if (!_awakenedFromNib || (_isHidden && hide) || (!_isHidden && !hide)) return;
[[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
_isHidden = hide;
if (hide)
{
[_overflowPopUpButton removeFromSuperview];
[_addTabButton removeFromSuperview];
}
else
{
[self addSubview:_overflowPopUpButton];
[self addSubview:_addTabButton];
}
CGFloat partnerOriginalSize, partnerOriginalOrigin, myOriginalSize, myOriginalOrigin, partnerTargetSize, partnerTargetOrigin, myTargetSize, myTargetOrigin;
// current (original) values
myOriginalSize = [self frame].size.height;
myOriginalOrigin = [self frame].origin.y;
if (partnerView)
{
partnerOriginalSize = [partnerView frame].size.height;
partnerOriginalOrigin = [partnerView frame].origin.y;
}
else
{
partnerOriginalSize = [[self window] frame].size.height;
partnerOriginalOrigin = [[self window] frame].origin.y;
}
if (partnerView)
{
// above or below me?
if ((myOriginalOrigin - 22) > partnerOriginalOrigin)
{
// partner is below me
if (_isHidden)
{
// I'm shrinking
myTargetOrigin = myOriginalOrigin + 21;
myTargetSize = myOriginalSize - 21;
partnerTargetOrigin = partnerOriginalOrigin;
partnerTargetSize = partnerOriginalSize + 21;
}
else
{
// I'm growing
myTargetOrigin = myOriginalOrigin - 21;
myTargetSize = myOriginalSize + 21;
partnerTargetOrigin = partnerOriginalOrigin;
partnerTargetSize = partnerOriginalSize - 21;
}
}
else
{
// partner is above me
if (_isHidden)
{
// I'm shrinking
myTargetOrigin = myOriginalOrigin;
myTargetSize = myOriginalSize - 21;
partnerTargetOrigin = partnerOriginalOrigin - 21;
partnerTargetSize = partnerOriginalSize + 21;
}
else
{
// I'm growing
myTargetOrigin = myOriginalOrigin;
myTargetSize = myOriginalSize + 21;
partnerTargetOrigin = partnerOriginalOrigin + 21;
partnerTargetSize = partnerOriginalSize - 21;
}
}
}
else
{
// for window movement
if (_isHidden)
{
// I'm shrinking
myTargetOrigin = myOriginalOrigin;
myTargetSize = myOriginalSize - 21;
partnerTargetOrigin = partnerOriginalOrigin + 21;
partnerTargetSize = partnerOriginalSize - 21;
}
else
{
// I'm growing
myTargetOrigin = myOriginalOrigin;
myTargetSize = myOriginalSize + 21;
partnerTargetOrigin = partnerOriginalOrigin - 21;
partnerTargetSize = partnerOriginalSize + 21;
}
}
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.1];
[[self animator] setFrame:NSMakeRect([self frame].origin.x, myTargetOrigin, NSWidth([self frame]), myTargetSize)];
if (partnerView)
{
[[partnerView animator] setFrame:NSMakeRect([partnerView frame].origin.x, partnerTargetOrigin, NSWidth([partnerView frame]), partnerTargetSize)];
}
else
{
[[[self window] animator] setFrame:NSMakeRect([[self window] frame].origin.x, partnerTargetOrigin, NSWidth([[self window] frame]), partnerTargetSize)];
}
[NSAnimationContext endGrouping];
}
- (BOOL)isTabBarHidden
{
return _isHidden;
}
- (BOOL)isAnimating
{
return _animationTimer != nil;
}
- (id)partnerView
{
return partnerView;
}
- (void)setPartnerView:(id)view
{
[partnerView release];
[view retain];
partnerView = view;
}
#pragma mark -
#pragma mark Drawing
- (BOOL)isFlipped
{
return YES;
}
- (void)drawRect:(NSRect)rect
{
[style drawTabBar:self inRect:rect];
}
- (void)update
{
[self update:_automaticallyAnimates];
}
- (void)update:(BOOL)animate
{
// make sure all of our tabs are accounted for before updating
if ([[self tabView] numberOfTabViewItems] != [_cells count]) {
return;
}
// hide/show? (these return if already in desired state)
if ( (_hideForSingleTab) && ([_cells count] <= 1) ) {
[self hideTabBar:YES animate:YES];
return;
} else {
[self hideTabBar:NO animate:YES];
}
[self removeAllToolTips];
[_controller layoutCells]; //eventually we should only have to call this when we know something has changed
PSMTabBarCell *currentCell;
NSMenu *overflowMenu = [_controller overflowMenu];
[_overflowPopUpButton setHidden:(overflowMenu == nil)];
[_overflowPopUpButton setMenu:overflowMenu];
if (_animationTimer) {
[_animationTimer invalidate];
[_animationTimer release]; _animationTimer = nil;
}
if (animate) {
NSMutableArray *targetFrames = [NSMutableArray arrayWithCapacity:[_cells count]];
for (NSInteger i = 0; i < [_cells count]; i++) {
// currentCell = [_cells objectAtIndex:i];
//we're going from NSRect -> NSValue -> NSRect -> NSValue here - oh well
[targetFrames addObject:[NSValue valueWithRect:[_controller cellFrameAtIndex:i]]];
}
[_addTabButton setHidden:!_showAddTabButton];
NSAnimation *animation = [[NSAnimation alloc] initWithDuration:0.50 animationCurve:NSAnimationEaseInOut];
[animation setAnimationBlockingMode:NSAnimationNonblocking];
[animation startAnimation];
_animationTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0
target:self
selector:@selector(_animateCells:)
userInfo:[NSArray arrayWithObjects:targetFrames, animation, nil]
repeats:YES] retain];
[animation release];
[[NSRunLoop currentRunLoop] addTimer:_animationTimer forMode:NSEventTrackingRunLoopMode];
[self _animateCells:_animationTimer];
} else {
for (NSInteger i = 0; i < [_cells count]; i++) {
currentCell = [_cells objectAtIndex:i];
[currentCell setFrame:[_controller cellFrameAtIndex:i]];
if (![currentCell isInOverflowMenu]) {
[self _setupTrackingRectsForCell:currentCell];
}
}
[_addTabButton setFrame:[_controller addButtonRect]];
[_addTabButton setHidden:!_showAddTabButton];
[self setNeedsDisplay:YES];
}
}
- (void)_animateCells:(NSTimer *)timer
{
NSAnimation *animation = [[timer userInfo] objectAtIndex:1];
NSArray *targetFrames = [[timer userInfo] objectAtIndex:0];
PSMTabBarCell *currentCell;
NSInteger cellCount = [_cells count];
if ((cellCount > 0) && [animation isAnimating]) {
//compare our target position with the current position and move towards the target
for (NSInteger i = 0; i < [targetFrames count] && i < cellCount; i++) {
currentCell = [_cells objectAtIndex:i];
NSRect cellFrame = [currentCell frame], targetFrame = [[targetFrames objectAtIndex:i] rectValue];
CGFloat sizeChange;
CGFloat originChange;
if ([self orientation] == PSMTabBarHorizontalOrientation) {
sizeChange = (targetFrame.size.width - cellFrame.size.width) * [animation currentProgress];
originChange = (targetFrame.origin.x - cellFrame.origin.x) * [animation currentProgress];
cellFrame.size.width += sizeChange;
cellFrame.origin.x += originChange;
} else {
sizeChange = (targetFrame.size.height - cellFrame.size.height) * [animation currentProgress];
originChange = (targetFrame.origin.y - cellFrame.origin.y) * [animation currentProgress];
cellFrame.size.height += sizeChange;
cellFrame.origin.y += originChange;
}
[currentCell setFrame:cellFrame];
//highlight the cell if the mouse is over it
NSPoint mousePoint = [self convertPoint:[[self window] mouseLocationOutsideOfEventStream] fromView:nil];
NSRect closeRect = [currentCell closeButtonRectForFrame:cellFrame];
[currentCell setHighlighted:NSMouseInRect(mousePoint, cellFrame, [self isFlipped])];
[currentCell setCloseButtonOver:NSMouseInRect(mousePoint, closeRect, [self isFlipped])];
}
if (_showAddTabButton) {
//animate the add tab button
NSRect target = [_controller addButtonRect], frame = [_addTabButton frame];
frame.origin.x += (target.origin.x - frame.origin.x) * [animation currentProgress];
[_addTabButton setFrame:frame];
}
} else {
//put all the cells where they should be in their final position
if (cellCount > 0) {
for (NSInteger i = 0; i < [targetFrames count] && i < cellCount; i++) {
PSMTabBarCell *currentCell = [_cells objectAtIndex:i];
NSRect cellFrame = [currentCell frame], targetFrame = [[targetFrames objectAtIndex:i] rectValue];
if ([self orientation] == PSMTabBarHorizontalOrientation) {