From efc5b8faaa2cd4a801fef81bca35cff93e42ca7f Mon Sep 17 00:00:00 2001 From: cacaodev Date: Fri, 30 Nov 2012 13:51:43 +0100 Subject: [PATCH] Update min/maxItemSize lazylly instead of subclassing -awakeFromCib Binders can set values before -awakeFromCib. We were too late and the bound content was not visible. --- AppKit/CPCollectionView.j | 41 ++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/AppKit/CPCollectionView.j b/AppKit/CPCollectionView.j index 810e25eff..e47b57793 100644 --- a/AppKit/CPCollectionView.j +++ b/AppKit/CPCollectionView.j @@ -99,6 +99,7 @@ id _delegate; CPEvent _mouseDownEvent; + BOOL _needsMinMaxItemSizeUpdate; } - (id)initWithFrame:(CGRect)aFrame @@ -124,6 +125,7 @@ _selectionIndexes = [CPIndexSet indexSet]; _allowsEmptySelection = YES; _isSelectable = YES; + _needsMinMaxItemSizeUpdate = YES; } return self; @@ -410,6 +412,25 @@ [self tile]; } +- (void)_updateMinMaxItemSizeIfNeeded +{ + if (!_needsMinMaxItemSizeUpdate) + return; + + var prototypeView = [_itemPrototype view]; + + if (prototypeView) + { + if (CGSizeEqualToSize(_minItemSize, CGSizeMakeZero())) + _minItemSize = [prototypeView frameSize]; + + if (CGSizeEqualToSize(_maxItemSize, CGSizeMakeZero()) && !([prototypeView autoresizingMask] & CPViewWidthSizable)) + _maxItemSize = [prototypeView frameSize]; + } + + _needsMinMaxItemSizeUpdate = NO; +} + /* @ignore */ - (void)tile { @@ -418,6 +439,8 @@ if (width == _tileWidth) return; + [self _updateMinMaxItemSizeIfNeeded]; + // We try to fit as many views per row as possible. Any remaining space is then // either proportioned out to the views (if their minSize != maxSize) or used as // margin @@ -977,22 +1000,6 @@ var CPCollectionViewMinItemSizeKey = @"CPCollectionViewMinItemSizeK @implementation CPCollectionView (CPCoding) -- (void)awakeFromCib -{ - [super awakeFromCib]; - - var prototypeView = [_itemPrototype view]; - if (prototypeView && (CGSizeEqualToSize(_minItemSize, CGSizeMakeZero()) || CGSizeEqualToSize(_maxItemSize, CGSizeMakeZero()))) - { - var item = _itemPrototype; - - if (CGSizeEqualToSize(_minItemSize, CGSizeMakeZero())) - _minItemSize = [prototypeView frameSize]; - else if (CGSizeEqualToSize(_maxItemSize, CGSizeMakeZero())) - _maxItemSize = [prototypeView frameSize]; - } -} - - (id)initWithCoder:(CPCoder)aCoder { self = [super initWithCoder:aCoder]; @@ -1024,6 +1031,8 @@ var CPCollectionViewMinItemSizeKey = @"CPCollectionViewMinItemSizeK _selectionIndexes = [CPIndexSet indexSet]; _allowsEmptySelection = YES; + _needsMinMaxItemSizeUpdate = YES; + [self setAutoresizesSubviews:NO]; } return self;