From 1a74b4c692368624a47c5bc6c4fcbb7462df81ce Mon Sep 17 00:00:00 2001 From: daboe01 Date: Sun, 13 Jul 2025 19:53:33 +0200 Subject: [PATCH 1/2] fixed: CPCollectionview displayed an erroneous offset before the first row of content --- AppKit/CPCollectionView.j | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/AppKit/CPCollectionView.j b/AppKit/CPCollectionView.j index 9c6a7f508..0e09ff892 100644 --- a/AppKit/CPCollectionView.j +++ b/AppKit/CPCollectionView.j @@ -566,14 +566,14 @@ var HORIZONTAL_MARGIN = 2; - (void)_computeGridWithSize:(CGSize)aSuperviewSize count:(Function)countRef { - var width = aSuperviewSize.width, - height = aSuperviewSize.height, - itemSize = CGSizeMakeCopy(_minItemSize), - maxItemSizeWidth = _maxItemSize.width, - maxItemSizeHeight = _maxItemSize.height, - itemsCount = [_items count], - numberOfRows, - numberOfColumns; + var width = aSuperviewSize.width, + height = aSuperviewSize.height, + itemSize = CGSizeMakeCopy(_minItemSize), + maxItemSizeWidth = _maxItemSize.width, + maxItemSizeHeight = _maxItemSize.height, + itemsCount = [_items count], + numberOfRows, + numberOfColumns; numberOfColumns = FLOOR(width / itemSize.width); @@ -600,9 +600,12 @@ var HORIZONTAL_MARGIN = 2; if (_maxNumberOfRows > 0) numberOfRows = MIN(numberOfRows, _maxNumberOfRows); - height = MAX(height, numberOfRows * (_minItemSize.height + _verticalMargin)); + // calculate the required height: (sum of item heights) + (sum of margins between items). + var requiredHeight = (numberOfRows * _minItemSize.height) + (MAX(0, numberOfRows - 1) * _verticalMargin); + height = MAX(height, requiredHeight); - var itemSizeHeight = FLOOR(height / numberOfRows) - _verticalMargin; + // calculate individual item height based on the total available height. + var itemSizeHeight = (numberOfRows > 0) ? FLOOR((height - (MAX(0, numberOfRows - 1) * _verticalMargin)) / numberOfRows) : 0; if (maxItemSizeHeight > 0) itemSizeHeight = MIN(itemSizeHeight, maxItemSizeHeight); @@ -621,7 +624,7 @@ var HORIZONTAL_MARGIN = 2; _horizontalMargin = _uniformSubviewsResizing ? FLOOR((aFrameSize.width - numberOfColumns * anItemSize.width) / (numberOfColumns + 1)) : HORIZONTAL_MARGIN; var x = _horizontalMargin, - y = -anItemSize.height; + y = -anItemSize.height; [displayItems enumerateObjectsUsingBlock:function(item, idx, stop) { @@ -636,7 +639,11 @@ var HORIZONTAL_MARGIN = 2; if (idx % numberOfColumns == 0) { x = _horizontalMargin; - y += _verticalMargin + anItemSize.height; + // For the first row, don't add a margin. For all subsequent rows, add the margin. + if (idx === 0) + y += anItemSize.height; + else + y += _verticalMargin + anItemSize.height; } [view setFrameOrigin:CGPointMake(x, y)]; From a0f9cb5183150f3b5fa554e5cd312e29722e474b Mon Sep 17 00:00:00 2001 From: daboe01 Date: Sun, 13 Jul 2025 19:55:34 +0200 Subject: [PATCH 2/2] formatting --- AppKit/CPCollectionView.j | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/AppKit/CPCollectionView.j b/AppKit/CPCollectionView.j index 0e09ff892..076e76d1d 100644 --- a/AppKit/CPCollectionView.j +++ b/AppKit/CPCollectionView.j @@ -566,14 +566,14 @@ var HORIZONTAL_MARGIN = 2; - (void)_computeGridWithSize:(CGSize)aSuperviewSize count:(Function)countRef { - var width = aSuperviewSize.width, - height = aSuperviewSize.height, - itemSize = CGSizeMakeCopy(_minItemSize), - maxItemSizeWidth = _maxItemSize.width, - maxItemSizeHeight = _maxItemSize.height, - itemsCount = [_items count], - numberOfRows, - numberOfColumns; + var width = aSuperviewSize.width, + height = aSuperviewSize.height, + itemSize = CGSizeMakeCopy(_minItemSize), + maxItemSizeWidth = _maxItemSize.width, + maxItemSizeHeight = _maxItemSize.height, + itemsCount = [_items count], + numberOfRows, + numberOfColumns; numberOfColumns = FLOOR(width / itemSize.width);