-
Notifications
You must be signed in to change notification settings - Fork 13
/
TNTabView.j
561 lines (441 loc) · 15.9 KB
/
TNTabView.j
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
/*
* TNTabView.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPButton.j>
@import <AppKit/CPControl.j>
@import <AppKit/CPTabViewItem.j>
@import <AppKit/CPTheme.j>
@import <AppKit/CPView.j>
@import <AppKit/CPScrollView.j>
@import <AppKit/CPMenu.j>
@import <AppKit/CPViewController.j>
@import <AppKit/CPPopover.j>
@import <AppKit/CPTableView.j>
@import "TNTabViewItemPrototype.j"
@global CPPopoverBehaviorTransient
var TNTabViewDelegate_tabView_shouldSelectTabViewItem_ = 1 << 1,
TNTabViewDelegate_tabView_didSelectTabViewItem_ = 1 << 2,
TNTabViewDelegate_tabView_willSelectTabViewItem_ = 1 << 3,
TNTabViewDelegate_tabViewDidChangeNumberOfTabViewItems_ = 1 << 4;
@protocol TNTabViewDelegate <CPObject>
@optional
- (BOOL)tabView:(TNTabView)aTabView shouldSelectTabViewItem:(CPTabViewItem)aTabViewItem;
- (void)tabView:(TNTabView)aTabView didSelectTabViewItem:(CPTabViewItem)aTabViewItem;
- (void)tabView:(TNTabView)aTabView willSelectTabViewItem:(CPTabViewItem)aTabViewItem;
- (void)tabViewDidChangeNumberOfTabViewItems:(TNTabView)aTabView;
@end
/*! @ingroup tnkit
this class is a sort of CPTabView (protocol compliant)
*/
@implementation TNTabView : CPControl <CPTableViewDelegate, CPTableViewDataSource>
{
id <TNTabViewDelegate> _delegate @accessors(property=delegate);
CPArray _itemObjects;
CPView _currentSelectedItemView;
CPView _viewTabs;
int _currentSelectedIndex;
CPView _contentView;
TNTabItemPrototype _tabItemViewPrototype;
unsigned _implementedDelegateMethods;
}
#pragma mark -
#pragma mark Initialization
/*! initialize the TNTabView
@param aFrame the frame of the view
*/
- (TNTabView)initWithFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
[self _init];
}
return self;
}
- (void)_init
{
_viewTabs = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, [self frameSize].width, [TNTabItemPrototype size].height)];
[_viewTabs setAutoresizingMask:CPViewWidthSizable];
_viewTabs._DOMElement.style.borderBottom = "1px solid #F2F2F2";
_viewTabs._DOMElement.style.borderTop = "1px solid #F2F2F2";
_viewTabs._DOMElement.style.boxSizing = @"border-box";
_viewTabs._DOMElement.style.MozBoxSizing = @"border-box";
_viewTabs._DOMElement.style.WebkitBoxSizing = @"border-box";
_contentView = [[CPView alloc] initWithFrame:CGRectMake(0, [TNTabItemPrototype size].height, CGRectGetWidth([self frame]), CGRectGetHeight([self frame]) - [TNTabItemPrototype size].height)];
[_contentView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
_currentSelectedIndex = -1;
_itemObjects = [];
_tabItemViewPrototype = [TNTabItemPrototype new];
[self addSubview:_viewTabs];
[self addSubview:_contentView];
[self setTabViewBackgroundColor:[CPColor whiteColor]];
}
#pragma mark -
#pragma mark Delegate methods
- (void)setDelegate:(id <TNTabViewDelegate>)aDelegate
{
if (aDelegate == _delegate)
return;
_implementedDelegateMethods = 0;
_delegate = aDelegate;
if ([_delegate respondsToSelector:@selector(tabViewDidChangeNumberOfTabViewItems:)])
_implementedDelegateMethods |= TNTabViewDelegate_tabViewDidChangeNumberOfTabViewItems_;
if ([_delegate respondsToSelector:@selector(tabView:willSelectTabViewItem:)])
_implementedDelegateMethods |= TNTabViewDelegate_tabView_willSelectTabViewItem_;
if ([_delegate respondsToSelector:@selector(tabView:didSelectTabViewItem:)])
_implementedDelegateMethods |= TNTabViewDelegate_tabView_didSelectTabViewItem_;
if ([_delegate respondsToSelector:@selector(tabView:shouldSelectTabViewItem:)])
_implementedDelegateMethods |= TNTabViewDelegate_tabView_shouldSelectTabViewItem_;
}
#pragma mark -
#pragma mark Getters / Setters
/*! set the background color for the tabs view
@param aColor the color
*/
- (void)setTabViewBackgroundColor:(CPColor)aColor
{
[_viewTabs setBackgroundColor:aColor];
}
/*! set the background color for the tabs view
@param aColor the color
*/
- (void)setContentBackgroundColor:(CPColor)aColor
{
[_contentView setBackgroundColor:aColor];
}
/*! set the prototype view for items
@param anItemPrototype a subclass of TNTabItemPrototype that represent the prototype
*/
- (void)setTabItemViewPrototype:(TNTabItemPrototype)anItemPrototype
{
_tabItemViewPrototype = anItemPrototype;
}
/*! @ignore
Returns a new copy of the current item view prototype
@return a new copy of current TNTabItemPrototype
*/
- (void)_newTabItemPrototype
{
return [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:_tabItemViewPrototype]];
}
- (void)_getTabItemAtIndex:(int)anIndex
{
if (anIndex == -1 || anIndex >= [_itemObjects count])
return nil;
return [_itemObjects[anIndex] tabViewItem];
}
- (void)_getTabViewAtIndex:(int)anIndex
{
if (anIndex >= [_itemObjects count])
return nil;
return _itemObjects[anIndex];
}
#pragma mark -
#pragma mark Events
/*! stop the event progation
*/
- (void)mouseDown:(CPEvent)anEvent
{
}
#pragma mark -
#pragma mark CPTabView protocol
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)addTabViewItem:(CPTabViewItem)anItem
{
[self insertTabViewItem:anItem atIndex:[_itemObjects count]];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)insertTabViewItem:(CPTabViewItem)anItem atIndex:(int)anIndex
{
var previousSelectedItem = [self selectedTabViewItem],
itemView = [self _newTabItemPrototype];
[itemView setTabViewItem:anItem];
[itemView setTarget:self];
[itemView setAction:@selector(_tabItemCliked:)];
[_itemObjects insertObject:itemView atIndex:anIndex];
[self _sendDelegateMethodTabViewDidChangeNumberOfTabViewItems];
[self setNeedsLayout];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (int)indexOfTabViewItem:(CPTabViewItem)anItem
{
for (var i = 0; i < [_itemObjects count]; i++)
if ([_itemObjects[i] tabViewItem] == anItem)
return i;
return CPNotFound;
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (int)indexOfTabViewItemWithIdentifier:(CPString)anIdentifer
{
for (var i = 0; i < [_itemObjects count]; i++)
if ([[_itemObjects[i] tabViewItem] identifier] == anIdentifer)
return i;
return CPNotFound;
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)numberOfTabViewItems
{
return [_itemObjects count];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)removeTabViewItem:(CPTabViewItem)anItem
{
var itemIndex = [self indexOfTabViewItemWithIdentifier:[anItem identifier]],
currentItemView = [self _getTabViewAtIndex:itemIndex],
associatedView = [[currentItemView tabViewItem] view];
[associatedView removeFromSuperview];
[currentItemView removeFromSuperview];
[_itemObjects removeObjectAtIndex:[self indexOfTabViewItemWithIdentifier:[anItem identifier]]];
[self _sendDelegateMethodTabViewDidChangeNumberOfTabViewItems];
[self setNeedsLayout];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)selectTabViewItem:(CPTabViewItem)anItem
{
[self selectTabViewItemAtIndex:[self indexOfTabViewItem:anItem]];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)selectedTabViewItem
{
return [self _getTabItemAtIndex:_currentSelectedIndex];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)selectedTabViewIndex
{
return _currentSelectedIndex;
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)selectFirstTabViewItem:(id)aSender
{
[self selectTabViewItemAtIndex:0];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)selectLastTabViewItem:(id)aSender
{
[self selectTabViewItemAtIndex:[_itemObjects count] - 1];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)selectNextTabViewItem:(id)aSender
{
var nextIndex = _currentSelectedIndex + 1;
if (nextIndex > [self numberOfTabViewItems] - 1)
nextIndex = 0;
[self selectTabViewItemAtIndex:nextIndex];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)selectPreviousTabViewItem:(id)aSender
{
var nextIndex = _currentSelectedIndex - 1;
if (nextIndex < 0)
nextIndex = [self numberOfTabViewItems] - 1;
[self selectTabViewItemAtIndex:nextIndex];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (void)selectTabViewItemAtIndex:(int)anIndex
{
var pendingItem = [self _getTabItemAtIndex:anIndex];
if (![self _sendDelegateMethodShouldSelectTabViewItem:pendingItem])
return;
[self _sendDelegateMethodWillSelectTabViewItem:pendingItem];
[[[self selectedTabViewItem] view] removeFromSuperview];
[_currentSelectedItemView unsetThemeState:TNTabItemPrototypeThemeStateSelected];
_currentSelectedItemView = [self _getTabViewAtIndex:anIndex];
[_currentSelectedItemView setThemeState:TNTabItemPrototypeThemeStateSelected];
if (pendingItem)
{
[[pendingItem view] setFrame:[_contentView bounds]];
[_contentView addSubview:[pendingItem view]];
}
_currentSelectedIndex = anIndex;
[self _sendDelegateMethodDidSelectTabViewItem:pendingItem];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (CPTabViewItem)tabViewItemAtIndex:(int)anIndex
{
return [self _getTabItemAtIndex:anIndex];
}
/*! implement CPTabViewProtocol
see documentation for CPTabView
*/
- (CPArray)tabViewItems
{
var ret = [CPArray array];
for (var i = 0; i < [_itemObjects count]; i++)
[ret addObject:[_itemObjects[i] tabViewItem]];
return ret;
}
#pragma mark -
#pragma mark Responder Chain
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void)keyUp:(CPEvent)anEvent
{
var keyCode = [anEvent keyCode];
if (keyCode == CPLeftArrowKeyCode)
[self selectPreviousTabViewItem:self];
else if (keyCode == CPRightArrowKeyCode)
[self selectNextTabViewItem:self];
else
[super keyUp:anEvent];
}
#pragma mark -
#pragma mark Layouting
/*! layout the TNTabView
*/
- (void)layoutSubviews
{
var tabSize = [_viewTabs frameSize].width,
margin = [_tabItemViewPrototype margin],
totalSize = 0,
widths = [],
shouldAddPopoverButton = NO;
for (var i = 0; i < [_itemObjects count]; i++)
{
var item = _itemObjects[i],
width = [item width];
[widths addObject:width];
totalSize += width + margin;
}
while (totalSize >= tabSize)
{
margin -= 3;
totalSize -= [widths count] * 3;
}
var currentXOrigin = tabSize / 2 - totalSize / 2 + margin / 2;
for (var i = 0; i < [widths count]; i++)
{
var itemView = _itemObjects[i],
width = widths[i],
itemFrame = [itemView frame],
tabContentView = [[itemView tabViewItem] view];
itemFrame.size.width = width;
itemFrame.origin.x = currentXOrigin;
itemFrame.origin.y = [_viewTabs frameSize].height / 2 - itemFrame.size.height / 2;
[itemView setIndex:i];
[itemView setFrame:itemFrame];
[tabContentView setFrame:[_contentView bounds]];
[self addSubview:itemView];
currentXOrigin += width + margin;
}
}
#pragma mark -
#pragma mark Actions
/*! @ignore
*/
- (@action)_tabItemCliked:(id)aSender
{
[[self window] makeFirstResponder:self];
if (_currentSelectedIndex == [aSender index])
return;
[self selectTabViewItemAtIndex:[aSender index]];
}
@end
var TNTabViewDelegateKey = @"TNTabViewDelegateKey",
TNTabViewItemObjectsKey = @"TNTabViewItemObjectsKey",
TNTabViewCurrentSelectedItemViewKey = @"TNTabViewCurrentSelectedItemViewKey",
TNTabViewViewTabsKey = @"TNTabViewViewTabsKey",
TNTabViewCurrentSelectedIndexKey = @"TNTabViewCurrentSelectedIndexKey",
TNTabViewContentViewKey = @"TNTabViewContentViewKey",
TNTabViewtTabItemViewPrototypeKey = @"TNTabViewtTabItemViewPrototypeKey",
TNTabViewtNextTabItemViewPrototypeKey = @"TNTabViewtNextTabItemViewPrototypeKey",
TNTabViewtTabItemTableViewPrototypeKey = @"TNTabViewtTabItemTableViewPrototypeKey";
@implementation TNTabView (TNTabViewCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
if (self = [super initWithCoder:aCoder])
{
[self _init];
_delegate = [aCoder decodeObjectForKey:TNTabViewDelegateKey] || _delegate;
_itemObjects = [aCoder decodeObjectForKey:TNTabViewItemObjectsKey] || _itemObjects;
_currentSelectedItemView = [aCoder decodeObjectForKey:TNTabViewCurrentSelectedItemViewKey] || _currentSelectedItemView;
_viewTabs = [aCoder decodeObjectForKey:TNTabViewViewTabsKey] || _viewTabs;
_currentSelectedIndex = [aCoder decodeObjectForKey:TNTabViewCurrentSelectedIndexKey] || _currentSelectedIndex;
_contentView = [aCoder decodeObjectForKey:TNTabViewContentViewKey] || _contentView;
_tabItemViewPrototype = [aCoder decodeObjectForKey:TNTabViewtTabItemViewPrototypeKey] || _tabItemViewPrototype;
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeObject:_delegate forKey:TNTabViewDelegateKey];
[aCoder encodeObject:_itemObjects forKey:TNTabViewItemObjectsKey];
[aCoder encodeObject:_currentSelectedItemView forKey:TNTabViewCurrentSelectedItemViewKey];
[aCoder encodeObject:_viewTabs forKey:TNTabViewViewTabsKey];
[aCoder encodeObject:_currentSelectedIndex forKey:TNTabViewCurrentSelectedIndexKey];
[aCoder encodeObject:_contentView forKey:TNTabViewContentViewKey];
[aCoder encodeObject:_tabItemViewPrototype forKey:TNTabViewtTabItemViewPrototypeKey];
}
@end
@implementation TNTabView (TNTabViewDelegate)
- (BOOL)_sendDelegateMethodShouldSelectTabViewItem:(CPTabViewItem)aTabViewItem
{
if (!(_implementedDelegateMethods & TNTabViewDelegate_tabView_shouldSelectTabViewItem_))
return YES;
return [_delegate tabView:self shouldSelectTabViewItem:aTabViewItem];
}
- (void)_sendDelegateMethodDidSelectTabViewItem:(CPTabViewItem)aTabViewItem
{
if (!(_implementedDelegateMethods & TNTabViewDelegate_tabView_didSelectTabViewItem_))
return;
return [_delegate tabView:self didSelectTabViewItem:aTabViewItem];
}
- (void)_sendDelegateMethodWillSelectTabViewItem:(CPTabViewItem)aTabViewItem
{
if (!(_implementedDelegateMethods & TNTabViewDelegate_tabView_willSelectTabViewItem_))
return;
[_delegate tabView:self willSelectTabViewItem:aTabViewItem];
}
- (void)_sendDelegateMethodTabViewDidChangeNumberOfTabViewItems
{
if (!(_implementedDelegateMethods & TNTabViewDelegate_tabViewDidChangeNumberOfTabViewItems_))
return;
[_delegate tabViewDidChangeNumberOfTabViewItems:self];
}
@end