[vlc-commits] [Git][videolan/vlc][master] 11 commits: macosx: Add VLCCollectionViewItemSizing class

Steve Lhomme (@robUx4) gitlab at videolan.org
Tue Feb 11 09:34:27 UTC 2025



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
8d15cd84 by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Add VLCCollectionViewItemSizing class

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
1d67fa94 by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Add method to UI units to retrieve both item size and number of items in row

Also use this for the old item size method

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
7c21557c by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: More efficiently calculate item sizing for dynamic collection views

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
6c83be93 by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Prevent resizing collection view items beyond two items in row

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
e4e20d86 by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Prevent resizing collection view items beyond unreasonable small size

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
783fe646 by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Prevent crash from trying to access collectionViewContentSize before layout finalised

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
465920a2 by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Provide unclamped row item count in item sizing object

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
049391fa by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Fix pressing CMD - having no visual effect after collection view becomes smaller

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
e839c874 by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Move kMinItemsInCollectionViewRow into a property of VLCLibraryUIUnits

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
97980fd4 by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Prevent uint overflow causing item sizing to get crushed

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -
d9076332 by Claudio Cambra at 2025-02-11T09:20:40+00:00
macosx: Fix wrong operator used in calculation of collection view adjustment

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -


3 changed files:

- modules/gui/macosx/library/VLCLibraryCollectionView.m
- modules/gui/macosx/library/VLCLibraryUIUnits.h
- modules/gui/macosx/library/VLCLibraryUIUnits.m


Changes:

=====================================
modules/gui/macosx/library/VLCLibraryCollectionView.m
=====================================
@@ -22,6 +22,7 @@
 
 #import "VLCLibraryCollectionView.h"
 
+#import "library/VLCLibraryCollectionViewDelegate.h"
 #import "library/VLCLibraryCollectionViewFlowLayout.h"
 #import "library/VLCLibraryUIUnits.h"
 
@@ -42,6 +43,7 @@ NSString * const VLCLibraryCollectionViewItemAdjustmentSmaller = @"VLCLibraryCol
 - (void)keyDown:(NSEvent *)event
 {
     if (![self.collectionViewLayout isKindOfClass:VLCLibraryCollectionViewFlowLayout.class] ||
+        ![self.delegate isKindOfClass:VLCLibraryCollectionViewDelegate.class] ||
         !(event.modifierFlags & NSCommandKeyMask)) {
         return;
     }
@@ -51,6 +53,20 @@ NSString * const VLCLibraryCollectionViewItemAdjustmentSmaller = @"VLCLibraryCol
         return;
     }
 
+    VLCLibraryCollectionViewFlowLayout * const layout =
+        (VLCLibraryCollectionViewFlowLayout *)self.collectionViewLayout;
+    VLCLibraryCollectionViewDelegate * const delegate =
+        (VLCLibraryCollectionViewDelegate *)self.delegate;
+    VLCCollectionViewItemSizing * const currentRowItemSizing =
+        [VLCLibraryUIUnits adjustedItemSizingForCollectionView:self
+                                                    withLayout:layout
+                                          withItemsAspectRatio:delegate.itemsAspectRatio];
+    if (currentRowItemSizing.rowItemCount <= VLCLibraryUIUnits.collectionViewMinItemsInRow && key == '+') {
+        return;
+    } else if (currentRowItemSizing.itemSize.width <= VLCLibraryUIUnits.collectionViewItemMinimumWidth && key == '-') {
+        return;
+    }
+
     NSUserDefaults * const defaults = NSUserDefaults.standardUserDefaults;
     NSInteger collectionViewAdjustment =
         [defaults integerForKey:VLCLibraryCollectionViewItemAdjustmentKey];
@@ -61,7 +77,29 @@ NSString * const VLCLibraryCollectionViewItemAdjustmentSmaller = @"VLCLibraryCol
             [NSNotification notificationWithName:VLCLibraryCollectionViewItemAdjustmentBigger
                                           object:self];
     } else if (key == '-') {
-        collectionViewAdjustment++;
+        if (currentRowItemSizing.rowItemCount == VLCLibraryUIUnits.collectionViewMinItemsInRow && currentRowItemSizing.unclampedRowItemCount <= VLCLibraryUIUnits.collectionViewMinItemsInRow) {
+            // The adjustment works independently of the size of the collection view, which can lead
+            // to situations where the adjustment is very negative and is being clamped to ensure we
+            // do not go below the minimum items in row value. However, that means that when trying
+            // to make collection view items smaller via a larger adjustment value, the user could
+            // press CMD - multiple times before any change is visible to them (this can happen when
+            // the collection view size has shrunk, for instance).
+            //
+            // To work around this we make a big adjustment that immediately provides visual
+            // feedback. To do this we calculate what the adjustment must be to get the row item
+            // count to be the minimum row item count value + 1.
+            //
+            // The unclampedRowItemCount is the number of items in row post-adjustment, so we need
+            // to retrieve the original, unclamped, unadjusted row item count. We then work out the
+            // difference between the unclamped&unadjusted row item count and the minimum row item
+            // count:
+            //   <adjustment> = <min items in row> - (<unclamped row item count> - <old adjustment>)
+            //
+            // To finish, we add 1 to visually change the row item count and show more items.
+            collectionViewAdjustment = VLCLibraryUIUnits.collectionViewMinItemsInRow - (currentRowItemSizing.unclampedRowItemCount - collectionViewAdjustment) + 1;
+        } else {
+            ++collectionViewAdjustment;
+        }
         notification =
             [NSNotification notificationWithName:VLCLibraryCollectionViewItemAdjustmentSmaller
                                           object:self];


=====================================
modules/gui/macosx/library/VLCLibraryUIUnits.h
=====================================
@@ -33,6 +33,14 @@ NS_ASSUME_NONNULL_BEGIN
 
 extern NSString * const VLCLibraryCollectionViewItemAdjustmentKey;
 
+ at interface VLCCollectionViewItemSizing: NSObject
+
+ at property (readwrite) NSInteger unclampedRowItemCount;
+ at property (readwrite) NSUInteger rowItemCount;
+ at property (readwrite) NSSize itemSize;
+
+ at end
+
 @interface VLCLibraryUIUnits : NSObject
 
 // Note that these values are not necessarily linked to the layout defined in the .xib files.
@@ -58,6 +66,8 @@ extern NSString * const VLCLibraryCollectionViewItemAdjustmentKey;
 
 @property (class, readonly) const CGFloat dynamicCollectionViewItemMinimumWidth;
 @property (class, readonly) const CGFloat dynamicCollectionViewItemMaximumWidth;
+ at property (class, readonly) const CGFloat collectionViewItemMinimumWidth;
+ at property (class, readonly) const unsigned short collectionViewMinItemsInRow;
 
 @property (class, readonly) const CGFloat collectionViewItemSpacing;
 @property (class, readonly) const NSEdgeInsets collectionViewSectionInsets;
@@ -88,6 +98,10 @@ extern NSString * const VLCLibraryCollectionViewItemAdjustmentKey;
                                                      withLayout:(VLCLibraryCollectionViewFlowLayout *)collectionViewLayout
                                            withItemsAspectRatio:(VLCLibraryCollectionViewItemAspectRatio)itemsAspectRatio;
 
++ (VLCCollectionViewItemSizing *)adjustedItemSizingForCollectionView:(NSCollectionView *)collectionView
+                                                          withLayout:(VLCLibraryCollectionViewFlowLayout *)collectionViewLayout
+                                                withItemsAspectRatio:(VLCLibraryCollectionViewItemAspectRatio)itemsAspectRatio;
+
 @end
 
 NS_ASSUME_NONNULL_END


=====================================
modules/gui/macosx/library/VLCLibraryUIUnits.m
=====================================
@@ -36,6 +36,9 @@
 
 NSString * const VLCLibraryCollectionViewItemAdjustmentKey = @"VLCLibraryCollectionViewItemAdjustmentKey";
 
+ at implementation VLCCollectionViewItemSizing
+ at end
+
 @implementation VLCLibraryUIUnits
 
 + (const CGFloat)largeSpacing
@@ -113,6 +116,16 @@ NSString * const VLCLibraryCollectionViewItemAdjustmentKey = @"VLCLibraryCollect
     return 200;
 }
 
++ (const CGFloat)collectionViewItemMinimumWidth
+{
+    return 40;
+}
+
++ (const unsigned short)collectionViewMinItemsInRow
+{
+    return 2;
+}
+
 + (const CGFloat)collectionViewItemSpacing
 {
     return [self largeSpacing];
@@ -128,40 +141,41 @@ NSString * const VLCLibraryCollectionViewItemAdjustmentKey = @"VLCLibraryCollect
                                                      withLayout:(VLCLibraryCollectionViewFlowLayout *)collectionViewLayout
                                            withItemsAspectRatio:(VLCLibraryCollectionViewItemAspectRatio)itemsAspectRatio
 {
-    uint numItemsInRow = 5;
-    static const uint kMinItemsInCollectionViewRow = 1;
-
-    NSSize itemSize = [self itemSizeForCollectionView:collectionView
-                                           withLayout:collectionViewLayout
-                                 withItemsAspectRatio:itemsAspectRatio
-                               withNumberOfItemsInRow:numItemsInRow];
-
-    while (itemSize.width > VLCLibraryUIUnits.dynamicCollectionViewItemMaximumWidth) {
-        ++numItemsInRow;
-        itemSize = [self itemSizeForCollectionView:collectionView
-                                        withLayout:collectionViewLayout
-                              withItemsAspectRatio:itemsAspectRatio
-                            withNumberOfItemsInRow:numItemsInRow];
-    }
-    while (itemSize.width < VLCLibraryUIUnits.dynamicCollectionViewItemMinimumWidth && numItemsInRow > kMinItemsInCollectionViewRow) {
-        --numItemsInRow;
-        itemSize = [self itemSizeForCollectionView:collectionView
-                                        withLayout:collectionViewLayout
-                              withItemsAspectRatio:itemsAspectRatio
-                            withNumberOfItemsInRow:numItemsInRow];
-    }
+    return [self adjustedItemSizingForCollectionView:collectionView
+                                          withLayout:collectionViewLayout
+                                withItemsAspectRatio:itemsAspectRatio].itemSize;
+}
+
++ (VLCCollectionViewItemSizing *)adjustedItemSizingForCollectionView:(NSCollectionView *)collectionView
+                                                          withLayout:(VLCLibraryCollectionViewFlowLayout *)collectionViewLayout
+                                                withItemsAspectRatio:(VLCLibraryCollectionViewItemAspectRatio)itemsAspectRatio
+{
+    const NSEdgeInsets sectionInsets = collectionViewLayout.sectionInset;
+    const CGFloat interItemSpacing = collectionViewLayout.minimumInteritemSpacing;
+    const CGFloat availableWidth =
+        collectionView.bounds.size.width - (sectionInsets.left + sectionInsets.right);
+    const float maxPossibleNumItemsInRow =
+        floor(availableWidth / (VLCLibraryUIUnits.dynamicCollectionViewItemMinimumWidth + interItemSpacing));
+    const float minPossibleNumItemsInRow =
+        MIN(ceil(availableWidth / (VLCLibraryUIUnits.dynamicCollectionViewItemMaximumWidth + interItemSpacing)), maxPossibleNumItemsInRow);
 
     const NSInteger adjustment =
         [NSUserDefaults.standardUserDefaults integerForKey:VLCLibraryCollectionViewItemAdjustmentKey];
-    if (adjustment != 0 && numItemsInRow + adjustment > kMinItemsInCollectionViewRow) {
-        numItemsInRow += adjustment;
-        itemSize = [self itemSizeForCollectionView:collectionView
-                                        withLayout:collectionViewLayout
-                              withItemsAspectRatio:itemsAspectRatio
-                            withNumberOfItemsInRow:numItemsInRow];
-    }
+    const uint midPossibleNumItemsInRow =
+        round((minPossibleNumItemsInRow + maxPossibleNumItemsInRow) / 2.0);
+
+    const int unclampedNumItemsInRow = midPossibleNumItemsInRow + adjustment;
+    const uint numItemsInRow = MAX(unclampedNumItemsInRow, VLCLibraryUIUnits.collectionViewMinItemsInRow);
+    const NSSize itemSize = [self itemSizeForCollectionView:collectionView
+                                                 withLayout:collectionViewLayout
+                                       withItemsAspectRatio:itemsAspectRatio
+                                     withNumberOfItemsInRow:numItemsInRow];
 
-    return itemSize;
+    VLCCollectionViewItemSizing * const itemSizing = [[VLCCollectionViewItemSizing alloc] init];
+    itemSizing.itemSize = itemSize;
+    itemSizing.rowItemCount = numItemsInRow;
+    itemSizing.unclampedRowItemCount = unclampedNumItemsInRow;
+    return itemSizing;
 }
 
 + (const NSSize)itemSizeForCollectionView:(NSCollectionView *)collectionView
@@ -169,9 +183,7 @@ NSString * const VLCLibraryCollectionViewItemAdjustmentKey = @"VLCLibraryCollect
                      withItemsAspectRatio:(VLCLibraryCollectionViewItemAspectRatio)itemsAspectRatio
                    withNumberOfItemsInRow:(uint)numItemsInRow
 {
-    NSParameterAssert(numItemsInRow > 0);
-    NSParameterAssert(collectionView);
-    NSParameterAssert(collectionViewLayout);
+    NSParameterAssert(numItemsInRow > 0 && collectionView && collectionViewLayout);
 
     const NSEdgeInsets sectionInsets = collectionViewLayout.sectionInset;
     const CGFloat interItemSpacing = collectionViewLayout.minimumInteritemSpacing;



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/adfdbcb0f334a23f8d4e8756b203233e64f25078...d90763320fca2b8ae04fad6d0ae8bb7e117ce2cc

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/adfdbcb0f334a23f8d4e8756b203233e64f25078...d90763320fca2b8ae04fad6d0ae8bb7e117ce2cc
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list