[vlc-commits] [Git][videolan/vlc][master] macosx: Unify VLCLibraryTableCellView nib construction in relevant class, deduplicate code

Steve Lhomme (@robUx4) gitlab at videolan.org
Fri Nov 18 06:31:44 UTC 2022



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
b2447a10 by Claudio Cambra at 2022-11-18T06:08:08+00:00
macosx: Unify VLCLibraryTableCellView nib construction in relevant class, deduplicate code

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

- - - - -


13 changed files:

- modules/gui/macosx/extensions/NSView+VLCAdditions.h
- modules/gui/macosx/extensions/NSView+VLCAdditions.m
- modules/gui/macosx/library/VLCLibraryAlbumTableCellView.h
- modules/gui/macosx/library/VLCLibraryAlbumTableCellView.m
- modules/gui/macosx/library/VLCLibraryAlbumTracksDataSource.m
- modules/gui/macosx/library/VLCLibraryAudioDataSource.m
- modules/gui/macosx/library/VLCLibrarySongTableCellView.h
- modules/gui/macosx/library/VLCLibrarySongTableCellView.m
- modules/gui/macosx/library/VLCLibraryTableCellView.h
- modules/gui/macosx/library/VLCLibraryTableCellView.m
- modules/gui/macosx/library/VLCLibraryVideoDataSource.m
- modules/gui/macosx/media-source/VLCMediaSourceBaseDataSource.m
- modules/gui/macosx/media-source/VLCMediaSourceDataSource.m


Changes:

=====================================
modules/gui/macosx/extensions/NSView+VLCAdditions.h
=====================================
@@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN
 
 @interface NSView (VLCAdditions)
 
++ (instancetype)fromNibNamed:(NSString *)nibName withClass:(Class)viewClass withOwner:(id)owner;
+
 @property (readonly) BOOL shouldShowDarkAppearance;
 
 - (void)enableSubviews:(BOOL)enabled;


=====================================
modules/gui/macosx/extensions/NSView+VLCAdditions.m
=====================================
@@ -27,6 +27,26 @@
 
 @implementation NSView (VLCAdditions)
 
++ (instancetype)fromNibNamed:(NSString *)nibName withClass:(Class)viewClass withOwner:(id)owner
+{
+    /* the following code saves us an instance of NSViewController which we don't need */
+    NSNib *nib = [[NSNib alloc] initWithNibNamed:nibName bundle:nil];
+    NSArray *topLevelObjects;
+    if (![nib instantiateWithOwner:owner topLevelObjects:&topLevelObjects]) {
+        NSAssert(1, @"Failed to load nib file to show view");
+        return nil;
+    }
+
+    for (id topLevelObject in topLevelObjects) {
+        if ([topLevelObject isKindOfClass:viewClass]) {
+            return topLevelObject;
+            break;
+        }
+    }
+
+    return nil;
+}
+
 - (BOOL)shouldShowDarkAppearance
 {
     if (@available(macOS 10.14, *)) {


=====================================
modules/gui/macosx/library/VLCLibraryAlbumTableCellView.h
=====================================
@@ -33,6 +33,7 @@ NS_ASSUME_NONNULL_BEGIN
 extern NSString *VLCAudioLibraryCellIdentifier;
 extern NSString *VLCLibraryAlbumTableCellTableViewColumnIdentifier;
 
++ (instancetype)fromNibWithOwner:(id)owner;
 + (CGFloat)defaultHeight;
 
 @property (readwrite, assign) IBOutlet VLCTrackingView *trackingView;


=====================================
modules/gui/macosx/library/VLCLibraryAlbumTableCellView.m
=====================================
@@ -21,9 +21,11 @@
  *****************************************************************************/
 
 #import "VLCLibraryAlbumTableCellView.h"
+
 #import "extensions/NSColor+VLCAdditions.h"
 #import "extensions/NSFont+VLCAdditions.h"
 #import "extensions/NSString+Helpers.h"
+#import "extensions/NSView+VLCAdditions.h"
 #import "views/VLCImageView.h"
 #import "views/VLCTrackingView.h"
 #import "main/VLCMain.h"
@@ -54,6 +56,13 @@ const CGFloat VLCLibraryAlbumTableCellViewSmallSpacing = 5;
 
 @implementation VLCLibraryAlbumTableCellView
 
++ (instancetype)fromNibWithOwner:(id)owner
+{
+    return (VLCLibraryAlbumTableCellView*)[NSView fromNibNamed:@"VLCLibraryAlbumTableCellView"
+                                                     withClass:[VLCLibraryAlbumTableCellView class]
+                                                     withOwner:owner];
+}
+
 + (CGFloat)defaultHeight
 {
     return VLCLibraryAlbumTableCellViewDefaultHeight;


=====================================
modules/gui/macosx/library/VLCLibraryAlbumTracksDataSource.m
=====================================
@@ -63,20 +63,7 @@ const CGFloat VLCLibraryTracksRowHeight = 40.;
     VLCLibrarySongTableCellView *cellView = [tableView makeViewWithIdentifier:VLCAudioLibrarySongCellIdentifier owner:self];
 
     if (cellView == nil) {
-        /* the following code saves us an instance of NSViewController which we don't need */
-        NSNib *nib = [[NSNib alloc] initWithNibNamed:@"VLCLibrarySongTableCellView" bundle:nil];
-        NSArray *topLevelObjects;
-        if (![nib instantiateWithOwner:self topLevelObjects:&topLevelObjects]) {
-            NSAssert(1, @"Failed to load nib file to show audio library items");
-            return nil;
-        }
-
-        for (id topLevelObject in topLevelObjects) {
-            if ([topLevelObject isKindOfClass:[VLCLibrarySongTableCellView class]]) {
-                cellView = topLevelObject;
-                break;
-            }
-        }
+        cellView = [VLCLibrarySongTableCellView fromNibWithOwner:self];
         cellView.identifier = VLCAudioLibrarySongCellIdentifier;
     }
 
@@ -89,4 +76,4 @@ const CGFloat VLCLibraryTracksRowHeight = 40.;
     return _tracks[row];
 }
 
- at end
\ No newline at end of file
+ at end


=====================================
modules/gui/macosx/library/VLCLibraryAudioDataSource.m
=====================================
@@ -287,20 +287,7 @@
     VLCLibraryTableCellView *cellView = [tableView makeViewWithIdentifier:VLCAudioLibraryCellIdentifier owner:self];
 
     if (cellView == nil) {
-        /* the following code saves us an instance of NSViewController which we don't need */
-        NSNib *nib = [[NSNib alloc] initWithNibNamed:@"VLCLibraryTableCellView" bundle:nil];
-        NSArray *topLevelObjects;
-        if (![nib instantiateWithOwner:self topLevelObjects:&topLevelObjects]) {
-            NSAssert(1, @"Failed to load nib file to show audio library items");
-            return nil;
-        }
-
-        for (id topLevelObject in topLevelObjects) {
-            if ([topLevelObject isKindOfClass:[VLCLibraryTableCellView class]]) {
-                cellView = topLevelObject;
-                break;
-            }
-        }
+        cellView = [VLCLibraryTableCellView fromNibWithOwner:self];
         cellView.identifier = VLCAudioLibraryCellIdentifier;
     }
 
@@ -454,20 +441,7 @@ viewForSupplementaryElementOfKind:(NSCollectionViewSupplementaryElementKind)kind
     VLCLibraryAlbumTableCellView *cellView = [tableView makeViewWithIdentifier:VLCAudioLibraryCellIdentifier owner:self];
 
     if (cellView == nil) {
-        /* the following code saves us an instance of NSViewController which we don't need */
-        NSNib *nib = [[NSNib alloc] initWithNibNamed:@"VLCLibraryAlbumTableCellView" bundle:nil];
-        NSArray *topLevelObjects;
-        if (![nib instantiateWithOwner:self topLevelObjects:&topLevelObjects]) {
-            NSAssert(1, @"Failed to load nib file to show audio library items");
-            return nil;
-        }
-
-        for (id topLevelObject in topLevelObjects) {
-            if ([topLevelObject isKindOfClass:[VLCLibraryAlbumTableCellView class]]) {
-                cellView = topLevelObject;
-                break;
-            }
-        }
+        cellView = [VLCLibraryAlbumTableCellView fromNibWithOwner:self];
         cellView.identifier = VLCAudioLibraryCellIdentifier;
     }
 


=====================================
modules/gui/macosx/library/VLCLibrarySongTableCellView.h
=====================================
@@ -31,6 +31,8 @@ NS_ASSUME_NONNULL_BEGIN
 
 extern NSString *VLCAudioLibrarySongCellIdentifier;
 
++ (instancetype)fromNibWithOwner:(id)owner;
+
 @property (readwrite, assign) IBOutlet VLCTrackingView *trackingView;
 @property (readwrite, assign) IBOutlet NSTextField *songNameTextField;
 @property (readwrite, assign) IBOutlet NSTextField *durationTextField;
@@ -43,4 +45,4 @@ extern NSString *VLCAudioLibrarySongCellIdentifier;
 
 @end
 
-NS_ASSUME_NONNULL_END
\ No newline at end of file
+NS_ASSUME_NONNULL_END


=====================================
modules/gui/macosx/library/VLCLibrarySongTableCellView.m
=====================================
@@ -21,9 +21,11 @@
  *****************************************************************************/
 
 #import "VLCLibrarySongTableCellView.h"
+
 #import "extensions/NSColor+VLCAdditions.h"
 #import "extensions/NSFont+VLCAdditions.h"
 #import "extensions/NSString+Helpers.h"
+#import "extensions/NSView+VLCAdditions.h"
 #import "views/VLCImageView.h"
 #import "views/VLCTrackingView.h"
 #import "main/VLCMain.h"
@@ -42,6 +44,13 @@ NSString *VLCAudioLibrarySongCellIdentifier = @"VLCAudioLibrarySongCellIdentifie
 
 @implementation VLCLibrarySongTableCellView
 
++ (instancetype)fromNibWithOwner:(id)owner
+{
+    return (VLCLibrarySongTableCellView*)[NSView fromNibNamed:@"VLCLibrarySongTableCellView"
+                                                     withClass:[VLCLibrarySongTableCellView class]
+                                                     withOwner:owner];
+}
+
 - (void)awakeFromNib
 {
     if(@available(macOS 10.14, *)) {


=====================================
modules/gui/macosx/library/VLCLibraryTableCellView.h
=====================================
@@ -31,6 +31,8 @@ NS_ASSUME_NONNULL_BEGIN
 
 @interface VLCLibraryTableCellView : NSTableCellView
 
++ (instancetype)fromNibWithOwner:(id)owner;
+
 @property (readwrite, assign) IBOutlet VLCTrackingView *trackingView;
 @property (readwrite, assign) IBOutlet NSTextField *singlePrimaryTitleTextField;
 @property (readwrite, assign) IBOutlet NSTextField *secondaryTitleTextField;


=====================================
modules/gui/macosx/library/VLCLibraryTableCellView.m
=====================================
@@ -24,6 +24,7 @@
 
 #import "extensions/NSFont+VLCAdditions.h"
 #import "extensions/NSString+Helpers.h"
+#import "extensions/NSView+VLCAdditions.h"
 #import "views/VLCImageView.h"
 #import "views/VLCTrackingView.h"
 #import "main/VLCMain.h"
@@ -35,6 +36,13 @@
 
 @implementation VLCLibraryTableCellView
 
++ (instancetype)fromNibWithOwner:(id)owner
+{
+    return (VLCLibraryTableCellView*)[NSView fromNibNamed:@"VLCLibraryTableCellView"
+                                                withClass:[VLCLibraryTableCellView class]
+                                                withOwner:owner];
+}
+
 - (void)awakeFromNib
 {
     self.singlePrimaryTitleTextField.font = [NSFont VLClibraryLargeCellTitleFont];


=====================================
modules/gui/macosx/library/VLCLibraryVideoDataSource.m
=====================================
@@ -291,20 +291,7 @@ writeItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths
     VLCLibraryTableCellView *cellView = [tableView makeViewWithIdentifier:@"VLCVideoLibraryTableViewCellIdentifier" owner:self];
     
     if (!cellView) {
-        /* the following code saves us an instance of NSViewController which we don't need */
-        NSNib *nib = [[NSNib alloc] initWithNibNamed:@"VLCLibraryTableCellView" bundle:nil];
-        NSArray *topLevelObjects;
-        if (![nib instantiateWithOwner:self topLevelObjects:&topLevelObjects]) {
-            NSAssert(1, @"Failed to load nib file to show audio library items");
-            return nil;
-        }
-
-        for (id topLevelObject in topLevelObjects) {
-            if ([topLevelObject isKindOfClass:[VLCLibraryTableCellView class]]) {
-                cellView = topLevelObject;
-                break;
-            }
-        }
+        cellView = [VLCLibraryTableCellView fromNibWithOwner:self];
         cellView.identifier = @"VLCVideoLibraryTableViewCellIdentifier";
     }
     


=====================================
modules/gui/macosx/media-source/VLCMediaSourceBaseDataSource.m
=====================================
@@ -305,20 +305,7 @@ referenceSizeForHeaderInSection:(NSInteger)section
     VLCLibraryTableCellView *cellView = [tableView makeViewWithIdentifier:VLCMediaSourceTableViewCellIdentifier owner:self];
 
     if (cellView == nil) {
-        /* the following code saves us an instance of NSViewController which we don't need */
-        NSNib *nib = [[NSNib alloc] initWithNibNamed:@"VLCLibraryTableCellView" bundle:nil];
-        NSArray *topLevelObjects;
-        if (![nib instantiateWithOwner:self topLevelObjects:&topLevelObjects]) {
-            NSAssert(1, @"Failed to load nib file to show audio library items");
-            return nil;
-        }
-
-        for (id topLevelObject in topLevelObjects) {
-            if ([topLevelObject isKindOfClass:[VLCLibraryTableCellView class]]) {
-                cellView = topLevelObject;
-                break;
-            }
-        }
+        cellView = [VLCLibraryTableCellView fromNibWithOwner:self];
         cellView.identifier = VLCMediaSourceTableViewCellIdentifier;
     }
     cellView.primaryTitleTextField.hidden = YES;


=====================================
modules/gui/macosx/media-source/VLCMediaSourceDataSource.m
=====================================
@@ -112,20 +112,7 @@
     VLCLibraryTableCellView *cellView = [tableView makeViewWithIdentifier:@"VLCMediaSourceTableViewCellIdentifier" owner:self];
 
     if (cellView == nil) {
-        /* the following code saves us an instance of NSViewController which we don't need */
-        NSNib *nib = [[NSNib alloc] initWithNibNamed:@"VLCLibraryTableCellView" bundle:nil];
-        NSArray *topLevelObjects;
-        if (![nib instantiateWithOwner:self topLevelObjects:&topLevelObjects]) {
-            NSAssert(1, @"Failed to load nib file to show audio library items");
-            return nil;
-        }
-
-        for (id topLevelObject in topLevelObjects) {
-            if ([topLevelObject isKindOfClass:[VLCLibraryTableCellView class]]) {
-                cellView = topLevelObject;
-                break;
-            }
-        }
+        cellView = [VLCLibraryTableCellView fromNibWithOwner:self];
         cellView.identifier = @"VLCMediaSourceTableViewCellIdentifier";
     }
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/b2447a10c9fa44401604591249c6bd9566098bdb

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/b2447a10c9fa44401604591249c6bd9566098bdb
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