Permalink
Browse files

Merge pull request #2496 from cacaodev/cptabview-initial-selection

Fixed: CPTabView should not allow empty selection
  • Loading branch information...
2 parents 0eb196e + 095864d commit c4bf67d133934ec62077f5272e50529046da55f4 @mrcarlberg mrcarlberg committed on GitHub Oct 21, 2016
Showing with 24 additions and 1 deletion.
  1. +12 −1 AppKit/CPTabView.j
  2. +12 −0 Tests/AppKit/CPTabViewTest.j
View
@@ -136,13 +136,19 @@ var CPTabViewDidSelectTabViewItemSelector = 1 << 1,
- (void)_insertTabViewItems:(CPArray)tabViewItems atIndexes:(CPIndexSet)indexes
{
+ var prevItemsCount = [self numberOfTabViewItems];
+
[_tabs insertSegments:tabViewItems atIndexes:indexes];
[tabViewItems makeObjectsPerformSelector:@selector(_setTabView:) withObject:self];
[self tileWithChangedItem:[tabViewItems firstObject]];
[self _reverseSetContent];
[self _sendDelegateTabViewDidChangeNumberOfTabViewItems];
+
+ // Do not allow empty selection if selection bindings are not enabled.
+ if (prevItemsCount == 0 && [self numberOfTabViewItems] > 0 && ![self _isSelectionBinded])
+ [self _selectTabViewItemAtIndex:0];
}
/*!
@@ -169,7 +175,7 @@ var CPTabViewDidSelectTabViewItemSelector = 1 << 1,
- (void)_didRemoveTabViewItem:(CPTabViewItem)aTabViewItem atIndex:(CPInteger)idx
{
// If the selection is managed by bindings, let the binder do that.
- if ([self binderForBinding:CPSelectionIndexesBinding] || [self binderForBinding:CPSelectedIndexBinding])
+ if ([self _isSelectionBinded])
return;
if (_selectedTabViewItem == aTabViewItem)
@@ -615,6 +621,11 @@ var CPTabViewDidSelectTabViewItemSelector = 1 << 1,
return [cls getBinding:aBinding forObject:self];
}
+- (BOOL)_isSelectionBinded
+{
+ return [self binderForBinding:CPSelectionIndexesBinding] || [self binderForBinding:CPSelectedIndexBinding];
+}
+
- (void)setItems:(CPArray)tabViewItems
{
if ([tabViewItems isEqualToArray:[_tabs segments]])
@@ -138,4 +138,16 @@
[self assert:[_tabView selectedTabViewItem] equals:_tabItem2];
}
+- (void)testTabViewItemSelectionNotEmpty
+{
+ // after insertion from empty and no explicit selection.
+ [self assert:[_tabView numberOfTabViewItems] equals:2];
+ [self assert:[_tabView selectedTabViewItem] equals:_tabItem1];
+
+ // Removes selected Item.
+ [_tabView removeTabViewItem:_tabItem1];
+ [self assert:[_tabView numberOfTabViewItems] equals:1];
+ [self assert:[_tabView selectedTabViewItem] equals:_tabItem2];
+}
+
@end

0 comments on commit c4bf67d

Please sign in to comment.