vlc | branch: master | Felix Paul Kühne <fe...@feepk.net> | Thu Jun 20 22:07:47 
2019 +0200| [e78831df7bdc537a12001b30a8a18ba345deb734] | committer: Felix Paul 
Kühne

macosx/library: implement sorting

Note that this respected by the video view only for now

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=e78831df7bdc537a12001b30a8a18ba345deb734
---

 .../package/macosx/VLC.xcodeproj/project.pbxproj   |   6 +
 modules/gui/macosx/Makefile.am                     |   2 +
 modules/gui/macosx/UI/VLCLibraryWindow.xib         |  17 +--
 modules/gui/macosx/library/VLCLibraryController.h  |  25 ++++
 modules/gui/macosx/library/VLCLibraryController.m  |   9 ++
 modules/gui/macosx/library/VLCLibraryModel.h       |   2 +
 modules/gui/macosx/library/VLCLibraryModel.m       |  26 +++-
 .../library/VLCLibrarySortingMenuController.h      |  33 +++++
 .../library/VLCLibrarySortingMenuController.m      | 134 +++++++++++++++++++++
 modules/gui/macosx/library/VLCLibraryWindow.h      |   1 +
 modules/gui/macosx/library/VLCLibraryWindow.m      |  10 ++
 po/POTFILES.in                                     |   2 +
 12 files changed, 259 insertions(+), 8 deletions(-)

diff --git a/extras/package/macosx/VLC.xcodeproj/project.pbxproj 
b/extras/package/macosx/VLC.xcodeproj/project.pbxproj
index 88cfed3435..e2aef9ee67 100644
--- a/extras/package/macosx/VLC.xcodeproj/project.pbxproj
+++ b/extras/package/macosx/VLC.xcodeproj/project.pbxproj
@@ -105,6 +105,7 @@
                7D1BF28A22A153E20027C50F /* VLCRoundedCornerTextField.m in 
Sources */ = {isa = PBXBuildFile; fileRef = 7D1BF28922A153E20027C50F /* 
VLCRoundedCornerTextField.m */; };
                7D1BF28D22A192000027C50F /* VLCPlaylistSortingMenuController.m 
in Sources */ = {isa = PBXBuildFile; fileRef = 7D1BF28C22A192000027C50F /* 
VLCPlaylistSortingMenuController.m */; };
                7D20081A2289835C002679DF /* VLCTrackingView.m in Sources */ = 
{isa = PBXBuildFile; fileRef = 7D2008192289835C002679DF /* VLCTrackingView.m 
*/; };
+               7D22A8F422BC14F80063ECD2 /* VLCLibrarySortingMenuController.m 
in Sources */ = {isa = PBXBuildFile; fileRef = 7D22A8F322BC14F80063ECD2 /* 
VLCLibrarySortingMenuController.m */; };
                7D28E6362275B4820098D30E /* NSColor+VLCAdditions.m in Sources 
*/ = {isa = PBXBuildFile; fileRef = 7D28E6352275B4820098D30E /* 
NSColor+VLCAdditions.m */; };
                7D28E6392275B7340098D30E /* NSFont+VLCAdditions.m in Sources */ 
= {isa = PBXBuildFile; fileRef = 7D28E6382275B7340098D30E /* 
NSFont+VLCAdditions.m */; };
                7D2E0EDB20CD204D0033A221 /* VLCWindow.m in Sources */ = {isa = 
PBXBuildFile; fileRef = 7D2E0ED920CD204D0033A221 /* VLCWindow.m */; };
@@ -457,6 +458,8 @@
                7D1BF28C22A192000027C50F /* VLCPlaylistSortingMenuController.m 
*/ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = 
VLCPlaylistSortingMenuController.m; sourceTree = "<group>"; };
                7D2008182289835C002679DF /* VLCTrackingView.h */ = {isa = 
PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VLCTrackingView.h; 
sourceTree = "<group>"; };
                7D2008192289835C002679DF /* VLCTrackingView.m */ = {isa = 
PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = 
VLCTrackingView.m; sourceTree = "<group>"; };
+               7D22A8F222BC14F80063ECD2 /* VLCLibrarySortingMenuController.h 
*/ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = 
VLCLibrarySortingMenuController.h; sourceTree = "<group>"; };
+               7D22A8F322BC14F80063ECD2 /* VLCLibrarySortingMenuController.m 
*/ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = 
VLCLibrarySortingMenuController.m; sourceTree = "<group>"; };
                7D28E6342275B4820098D30E /* NSColor+VLCAdditions.h */ = {isa = 
PBXFileReference; lastKnownFileType = sourcecode.c.h; path = 
"NSColor+VLCAdditions.h"; sourceTree = "<group>"; };
                7D28E6352275B4820098D30E /* NSColor+VLCAdditions.m */ = {isa = 
PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = 
"NSColor+VLCAdditions.m"; sourceTree = "<group>"; };
                7D28E6372275B7340098D30E /* NSFont+VLCAdditions.h */ = {isa = 
PBXFileReference; lastKnownFileType = sourcecode.c.h; path = 
"NSFont+VLCAdditions.h"; sourceTree = "<group>"; };
@@ -1030,6 +1033,8 @@
                                7DE2F0432282C84A0040DD0A /* 
VLCLibraryAudioDataSource.m */,
                                7DFBDCAF226A518400B700A5 /* 
VLCLibraryMenuController.h */,
                                7DFBDCB0226A518400B700A5 /* 
VLCLibraryMenuController.m */,
+                               7D22A8F222BC14F80063ECD2 /* 
VLCLibrarySortingMenuController.h */,
+                               7D22A8F322BC14F80063ECD2 /* 
VLCLibrarySortingMenuController.m */,
                                7DFBDCBF226DC16200B700A5 /* VLCInputItem.h */,
                                7DFBDCC0226DC16200B700A5 /* VLCInputItem.m */,
                        );
@@ -1737,6 +1742,7 @@
                                7D0F64062202047900FDB91F /* 
VLCLibraryCollectionViewItem.m in Sources */,
                                7D713D322201AE350042BEB7 /* VLCLibraryWindow.m 
in Sources */,
                                7DF14FBD2270CB1C0008ABE4 /* 
VLCMediaSourceCollectionViewItem.m in Sources */,
+                               7D22A8F422BC14F80063ECD2 /* 
VLCLibrarySortingMenuController.m in Sources */,
                                1C3113C91E508C6900D4DD76 /* VLCOutput.m in 
Sources */,
                                1C1C62011F8260A90052DD4F /* 
VLCWrappableTextField.m in Sources */,
                                6BBB05DA1EEFEA29003A1019 /* VLCHUDOutlineView.m 
in Sources */,
diff --git a/modules/gui/macosx/Makefile.am b/modules/gui/macosx/Makefile.am
index 325858c2dc..13931cbf69 100644
--- a/modules/gui/macosx/Makefile.am
+++ b/modules/gui/macosx/Makefile.am
@@ -70,6 +70,8 @@ libmacosx_plugin_la_SOURCES = \
        gui/macosx/library/VLCLibraryMenuController.m \
        gui/macosx/library/VLCLibraryModel.h \
        gui/macosx/library/VLCLibraryModel.m \
+       gui/macosx/library/VLCLibrarySortingMenuController.h \
+       gui/macosx/library/VLCLibrarySortingMenuController.m \
        gui/macosx/library/VLCLibraryTableCellView.h \
        gui/macosx/library/VLCLibraryTableCellView.m \
        gui/macosx/library/VLCLibraryVideoDataSource.h \
diff --git a/modules/gui/macosx/UI/VLCLibraryWindow.xib 
b/modules/gui/macosx/UI/VLCLibraryWindow.xib
index 646999ac2c..cc505f6055 100644
--- a/modules/gui/macosx/UI/VLCLibraryWindow.xib
+++ b/modules/gui/macosx/UI/VLCLibraryWindow.xib
@@ -461,6 +461,9 @@
                             <behavior key="behavior" pushIn="YES" 
lightByBackground="YES" lightByGray="YES"/>
                             <font key="font" metaFont="system"/>
                         </buttonCell>
+                        <connections>
+                            <action selector="sortLibrary:" 
target="QvC-M9-y7g" id="VJd-p4-owD"/>
+                        </connections>
                     </button>
                     <button verticalHuggingPriority="750" 
translatesAutoresizingMaskIntoConstraints="NO" id="Rhu-eh-ywm">
                         <rect key="frame" x="56" y="328" width="45" 
height="32"/>
@@ -561,7 +564,7 @@
                     <rect key="frame" x="0.0" y="498" width="242" 
height="310"/>
                     <clipView key="contentView" copiesOnScroll="NO" 
id="J5s-sy-il6">
                         <rect key="frame" x="0.0" y="0.0" width="242" 
height="310"/>
-                        <autoresizingMask key="autoresizingMask" 
widthSizable="YES" heightSizable="YES"/>
+                        <autoresizingMask key="autoresizingMask"/>
                         <subviews>
                             <collectionView selectable="YES" id="hnE-Hj-MZo">
                                 <rect key="frame" x="0.0" y="0.0" width="242" 
height="310"/>
@@ -591,7 +594,7 @@
                     <rect key="frame" x="0.0" y="0.0" width="242" 
height="498"/>
                     <clipView key="contentView" copiesOnScroll="NO" 
id="OKa-dt-1yY">
                         <rect key="frame" x="0.0" y="0.0" width="242" 
height="498"/>
-                        <autoresizingMask key="autoresizingMask" 
widthSizable="YES" heightSizable="YES"/>
+                        <autoresizingMask key="autoresizingMask"/>
                         <subviews>
                             <collectionView selectable="YES" id="B8x-e8-7zp">
                                 <rect key="frame" x="0.0" y="0.0" width="242" 
height="498"/>
@@ -639,7 +642,7 @@
             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" 
flexibleMinY="YES"/>
             <clipView key="contentView" id="tI4-x3-55j">
                 <rect key="frame" x="0.0" y="0.0" width="242" height="291"/>
-                <autoresizingMask key="autoresizingMask"/>
+                <autoresizingMask key="autoresizingMask" widthSizable="YES" 
heightSizable="YES"/>
                 <subviews>
                     <collectionView id="r7v-GI-W1U">
                         <rect key="frame" x="0.0" y="0.0" width="242" 
height="291"/>
@@ -669,7 +672,7 @@
                     <autoresizingMask key="autoresizingMask" 
flexibleMaxX="YES" flexibleMinY="YES"/>
                     <clipView key="contentView" id="dmB-cB-az6">
                         <rect key="frame" x="0.0" y="0.0" width="155" 
height="327"/>
-                        <autoresizingMask key="autoresizingMask" 
widthSizable="YES" heightSizable="YES"/>
+                        <autoresizingMask key="autoresizingMask"/>
                         <subviews>
                             <tableView verticalHuggingPriority="750" 
allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" 
columnReordering="NO" columnSelection="YES" multipleSelection="NO" 
emptySelection="NO" autosaveColumns="NO" rowHeight="36" rowSizeStyle="large" 
viewBased="YES" id="dNP-8u-8iI">
                                 <rect key="frame" x="0.0" y="0.0" width="155" 
height="327"/>
@@ -729,7 +732,7 @@
                     <autoresizingMask key="autoresizingMask" 
flexibleMaxX="YES" flexibleMinY="YES"/>
                     <clipView key="contentView" id="qva-RZ-DvL">
                         <rect key="frame" x="0.0" y="0.0" width="154" 
height="327"/>
-                        <autoresizingMask key="autoresizingMask" 
widthSizable="YES" heightSizable="YES"/>
+                        <autoresizingMask key="autoresizingMask"/>
                         <subviews>
                             <tableView verticalHuggingPriority="750" 
allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" 
columnReordering="NO" columnSelection="YES" multipleSelection="NO" 
autosaveColumns="NO" viewBased="YES" id="LNt-ot-2wU">
                                 <rect key="frame" x="0.0" y="0.0" width="154" 
height="327"/>
@@ -789,7 +792,7 @@
                     <autoresizingMask key="autoresizingMask" 
flexibleMaxX="YES" flexibleMinY="YES"/>
                     <clipView key="contentView" id="xCC-h9-931">
                         <rect key="frame" x="0.0" y="0.0" width="197" 
height="327"/>
-                        <autoresizingMask key="autoresizingMask" 
widthSizable="YES" heightSizable="YES"/>
+                        <autoresizingMask key="autoresizingMask"/>
                         <subviews>
                             <tableView verticalHuggingPriority="750" 
allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" 
columnReordering="NO" multipleSelection="NO" autosaveColumns="NO" 
rowSizeStyle="automatic" viewBased="YES" id="4ll-T2-J16">
                                 <rect key="frame" x="0.0" y="0.0" width="197" 
height="327"/>
@@ -860,7 +863,7 @@
                     <rect key="frame" x="0.0" y="0.0" width="500" 
height="302"/>
                     <clipView key="contentView" id="2oa-WL-dxA">
                         <rect key="frame" x="0.0" y="0.0" width="500" 
height="302"/>
-                        <autoresizingMask key="autoresizingMask" 
widthSizable="YES" heightSizable="YES"/>
+                        <autoresizingMask key="autoresizingMask"/>
                         <subviews>
                             <collectionView id="QAt-jP-zE7">
                                 <rect key="frame" x="0.0" y="0.0" width="500" 
height="302"/>
diff --git a/modules/gui/macosx/library/VLCLibraryController.h 
b/modules/gui/macosx/library/VLCLibraryController.h
index 93e0371a7e..0c16554b1c 100644
--- a/modules/gui/macosx/library/VLCLibraryController.h
+++ b/modules/gui/macosx/library/VLCLibraryController.h
@@ -22,6 +22,8 @@
 
 #import <Foundation/Foundation.h>
 
+#import <vlc_media_library.h>
+
 @class VLCLibraryModel;
 @class VLCMediaLibraryMediaItem;
 
@@ -42,6 +44,29 @@ NS_ASSUME_NONNULL_BEGIN
 
 - (int)clearHistory;
 
+/**
+ * Sort the entire library representation based on:
+ * @param sortCriteria the criteria used for sorting
+ * @param descending sort ascending or descending.
+ */
+- (void)sortByCriteria:(enum vlc_ml_sorting_criteria_t)sortCriteria 
andDescending:(bool)descending;
+
+/**
+ * Initially, the library is unsorted until the user decides to do so
+ * Until then, the unsorted state is retained.
+ */
+@property (readonly) BOOL unsorted;
+
+/**
+ * The last key used for sorting
+ */
+@property (readonly) enum vlc_ml_sorting_criteria_t lastSortingCriteria;
+
+/**
+ * The last order used for sorting
+ */
+@property (readonly) bool descendingLibrarySorting;
+
 @end
 
 NS_ASSUME_NONNULL_END
diff --git a/modules/gui/macosx/library/VLCLibraryController.m 
b/modules/gui/macosx/library/VLCLibraryController.m
index 30f76131bb..0d882e72e3 100644
--- a/modules/gui/macosx/library/VLCLibraryController.m
+++ b/modules/gui/macosx/library/VLCLibraryController.m
@@ -44,6 +44,7 @@
     if (self) {
         _p_libraryInstance = vlc_ml_instance_get(getIntf());
         _libraryModel = [[VLCLibraryModel alloc] 
initWithLibrary:_p_libraryInstance];
+        _unsorted = YES;
 
         NSNotificationCenter *defaultNotificationCenter = 
[NSNotificationCenter defaultCenter];
         [defaultNotificationCenter addObserver:self
@@ -154,4 +155,12 @@
     return vlc_ml_clear_history(_p_libraryInstance);
 }
 
+- (void)sortByCriteria:(enum vlc_ml_sorting_criteria_t)sortCriteria 
andDescending:(bool)descending
+{
+    _unsorted = NO;
+    _lastSortingCriteria = sortCriteria;
+    _descendingLibrarySorting = descending;
+    [_libraryModel sortByCriteria:sortCriteria andDescending:descending];
+}
+
 @end
diff --git a/modules/gui/macosx/library/VLCLibraryModel.h 
b/modules/gui/macosx/library/VLCLibraryModel.h
index 80e04dd06c..28cf976979 100644
--- a/modules/gui/macosx/library/VLCLibraryModel.h
+++ b/modules/gui/macosx/library/VLCLibraryModel.h
@@ -67,6 +67,8 @@ extern NSString *VLCLibraryModelMediaItemUpdated;
 
 - (nullable NSArray <VLCMediaLibraryAlbum *>*)listAlbumsOfParentType:(enum 
vlc_ml_parent_type)parentType forID:(int64_t)ID;
 
+- (void)sortByCriteria:(enum vlc_ml_sorting_criteria_t)sortCriteria 
andDescending:(bool)descending;
+
 @end
 
 NS_ASSUME_NONNULL_END
diff --git a/modules/gui/macosx/library/VLCLibraryModel.m 
b/modules/gui/macosx/library/VLCLibraryModel.m
index 09ae2c3532..bfd0bd71f1 100644
--- a/modules/gui/macosx/library/VLCLibraryModel.m
+++ b/modules/gui/macosx/library/VLCLibraryModel.m
@@ -45,6 +45,9 @@ NSString *VLCLibraryModelMediaItemUpdated = 
@"VLCLibraryModelMediaItemUpdated";
     NSArray *_cachedVideoMedia;
     NSArray *_cachedRecentMedia;
     NSNotificationCenter *_defaultNotificationCenter;
+
+    enum vlc_ml_sorting_criteria_t _sortCriteria;
+    bool _sortDescending;
 }
 
 - (void)updateCachedListOfAudioMedia;
@@ -88,6 +91,8 @@ static void libraryCallback(void *p_data, const 
vlc_ml_event_t *p_event)
 {
     self = [super init];
     if (self) {
+        _sortCriteria = VLC_ML_SORTING_DEFAULT;
+        _sortDescending = NO;
         _p_mediaLibrary = library;
         _p_eventCallback = vlc_ml_event_register_callback(_p_mediaLibrary, 
libraryCallback, (__bridge void *)self);
         _defaultNotificationCenter = [NSNotificationCenter defaultCenter];
@@ -249,7 +254,12 @@ static void libraryCallback(void *p_data, const 
vlc_ml_event_t *p_event)
 
 - (void)updateCachedListOfVideoMedia
 {
-    vlc_ml_media_list_t *p_media_list = 
vlc_ml_list_video_media(_p_mediaLibrary, NULL);
+    vlc_ml_query_params_t queryParameters;
+    memset(&queryParameters, 0, sizeof(vlc_ml_query_params_t));
+    queryParameters.i_nbResults = 20;
+    queryParameters.i_sort = _sortCriteria;
+    queryParameters.b_desc = _sortDescending;
+    vlc_ml_media_list_t *p_media_list = 
vlc_ml_list_video_media(_p_mediaLibrary, &queryParameters);
     if (p_media_list == NULL) {
         return;
     }
@@ -278,6 +288,7 @@ static void libraryCallback(void *p_data, const 
vlc_ml_event_t *p_event)
     vlc_ml_query_params_t queryParameters;
     memset(&queryParameters, 0, sizeof(vlc_ml_query_params_t));
     queryParameters.i_nbResults = 20;
+    // we don't set the sorting criteria here as they are not applicable to 
history
     vlc_ml_media_list_t *p_media_list = vlc_ml_list_history(_p_mediaLibrary, 
&queryParameters);
     if (p_media_list == NULL) {
         return;
@@ -348,4 +359,17 @@ static void libraryCallback(void *p_data, const 
vlc_ml_event_t *p_event)
     return [mutableArray copy];
 }
 
+- (void)sortByCriteria:(enum vlc_ml_sorting_criteria_t)sortCriteria 
andDescending:(bool)descending
+{
+    _sortCriteria = sortCriteria;
+    _sortDescending = descending;
+    [self dropCaches];
+}
+
+- (void)dropCaches
+{
+    _cachedVideoMedia = nil;
+    [_defaultNotificationCenter 
postNotificationName:VLCLibraryModelVideoMediaListUpdated object:nil];
+}
+
 @end
diff --git a/modules/gui/macosx/library/VLCLibrarySortingMenuController.h 
b/modules/gui/macosx/library/VLCLibrarySortingMenuController.h
new file mode 100644
index 0000000000..2dfe4109ca
--- /dev/null
+++ b/modules/gui/macosx/library/VLCLibrarySortingMenuController.h
@@ -0,0 +1,33 @@
+/*****************************************************************************
+ * VLCLibrarySortingMenuController.h: MacOS X interface module
+ *****************************************************************************
+ * Copyright (C) 2019 VLC authors and VideoLAN
+ *
+ * Authors: Felix Paul Kühne <fkuehne # videolan -dot- org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 
USA.
+ *****************************************************************************/
+
+#import <Cocoa/Cocoa.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface VLCLibrarySortingMenuController : NSObject
+
+@property (readonly) NSMenu *librarySortingMenu;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/modules/gui/macosx/library/VLCLibrarySortingMenuController.m 
b/modules/gui/macosx/library/VLCLibrarySortingMenuController.m
new file mode 100644
index 0000000000..a14798e397
--- /dev/null
+++ b/modules/gui/macosx/library/VLCLibrarySortingMenuController.m
@@ -0,0 +1,134 @@
+/*****************************************************************************
+ * VLCLibrarySortingMenuController.m: MacOS X interface module
+ *****************************************************************************
+ * Copyright (C) 2019 VLC authors and VideoLAN
+ *
+ * Authors: Felix Paul Kühne <fkuehne # videolan -dot- org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 
USA.
+ *****************************************************************************/
+
+#import "VLCLibrarySortingMenuController.h"
+
+#import "library/VLCLibraryController.h"
+#import "extensions/NSString+Helpers.h"
+#import "main/VLCMain.h"
+
+#import <vlc_media_library.h>
+
+@interface VLCLibrarySortingMenuController () <NSMenuDelegate>
+{
+    VLCLibraryController *_libraryController;
+    NSMenu *_librarySortingMenu;
+}
+@end
+
+@implementation VLCLibrarySortingMenuController
+
+- (instancetype)init
+{
+    self = [super init];
+    if (self) {
+        [self createMenu];
+        _libraryController = [[VLCMain sharedInstance] libraryController];
+    }
+    return self;
+}
+
+- (void)createMenu
+{
+    _librarySortingMenu = [[NSMenu alloc] init];
+
+    NSArray *titles = @[_NS("Default"),
+                        _NS("Alphabetically"),
+                        _NS("Duration"),
+                        _NS("Insertion Date"),
+                        _NS("Last Modification Date"),
+                        _NS("Release Date"),
+                        _NS("File Size"),
+                        _NS("Artist"),
+                        _NS("Play Count"),
+                        _NS("Album"),
+                        _NS("Filename"),
+                        _NS("Track Number")];
+    NSUInteger count = titles.count;
+    for (NSUInteger x = 0; x < count; x++) {
+        NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:titles[x] 
action:@selector(selectSortKey:) keyEquivalent:@""];
+        menuItem.target = self;
+        menuItem.tag = x;
+        [_librarySortingMenu addItem:menuItem];
+    }
+
+    [_librarySortingMenu addItem:[NSMenuItem separatorItem]];
+
+    titles = @[_NS("Ascending"),
+               _NS("Descending")];
+    count = titles.count;
+    for (NSUInteger x = 0; x < count; x++) {
+        NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:titles[x] 
action:@selector(selectSortOrder:) keyEquivalent:@""];
+        menuItem.target = self;
+        menuItem.tag = x + 100;
+        [_librarySortingMenu addItem:menuItem];
+    }
+
+    _librarySortingMenu.delegate = self;
+}
+
+- (void)menuNeedsUpdate:(NSMenu *)menu
+{
+    if (_libraryController.unsorted) {
+        return;
+    }
+    NSInteger count = _librarySortingMenu.numberOfItems;
+    for (NSInteger x = 0; x < count; x++) {
+        NSMenuItem *menuItem = [_librarySortingMenu itemAtIndex:x];
+        menuItem.state = NSOffState;
+    }
+
+    NSMenuItem *menuItem = [_librarySortingMenu 
itemWithTag:_libraryController.lastSortingCriteria];
+    menuItem.state = NSOnState;
+
+    menuItem = [_librarySortingMenu 
itemWithTag:_libraryController.descendingLibrarySorting + 100];
+    menuItem.state = NSOnState;
+}
+
+- (void)selectSortKey:(id)sender
+{
+    enum vlc_ml_sorting_criteria_t sortCriteria = (enum 
vlc_ml_sorting_criteria_t)[sender tag];
+    bool descending;
+    if (_libraryController.unsorted) {
+        /* we don't have an order and the user can only do a single selection 
- pick the most popular */
+        descending = NO;
+    } else {
+        descending = YES;
+    }
+
+    [_libraryController sortByCriteria:sortCriteria andDescending:descending];
+}
+
+- (void)selectSortOrder:(id)sender
+{
+    enum vlc_ml_sorting_criteria_t sortCriteria = (enum 
vlc_ml_sorting_criteria_t)[sender tag];
+    bool descending = ([sender tag] - 100);
+    if (_libraryController.unsorted) {
+        /* we don't have a key and the user can only do a single selection - 
pick the most popular */
+        sortCriteria = VLC_ML_SORTING_DEFAULT;
+    } else {
+        sortCriteria = _libraryController.lastSortingCriteria;
+    }
+
+    [_libraryController sortByCriteria:sortCriteria andDescending:descending];
+}
+@end
diff --git a/modules/gui/macosx/library/VLCLibraryWindow.h 
b/modules/gui/macosx/library/VLCLibraryWindow.h
index 7d1ac8fed6..e74163cc68 100644
--- a/modules/gui/macosx/library/VLCLibraryWindow.h
+++ b/modules/gui/macosx/library/VLCLibraryWindow.h
@@ -74,6 +74,7 @@ NS_ASSUME_NONNULL_BEGIN
 - (IBAction)repeatAction:(id)sender;
 - (IBAction)clearPlaylist:(id)sender;
 - (IBAction)sortPlaylist:(id)sender;
+- (IBAction)sortLibrary:(id)sender;
 - (IBAction)openMedia:(id)sender;
 - (IBAction)showAndHidePlaylist:(id)sender;
 
diff --git a/modules/gui/macosx/library/VLCLibraryWindow.m 
b/modules/gui/macosx/library/VLCLibraryWindow.m
index f19475b84b..c5d6bbe17b 100644
--- a/modules/gui/macosx/library/VLCLibraryWindow.m
+++ b/modules/gui/macosx/library/VLCLibraryWindow.m
@@ -39,6 +39,7 @@
 #import "library/VLCLibraryModel.h"
 #import "library/VLCLibraryCollectionViewSupplementaryElementView.h"
 #import "library/VLCLibraryAlternativeAudioViewController.h"
+#import "library/VLCLibrarySortingMenuController.h"
 
 #import "media-source/VLCMediaSourceCollectionViewItem.h"
 #import "media-source/VLCMediaSourceDataSource.h"
@@ -70,6 +71,7 @@ const CGFloat VLCLibraryWindowDefaultPlaylistWidth = 340.;
     VLCLibraryVideoDataSource *_libraryVideoDataSource;
     VLCLibraryAudioDataSource *_libraryAudioDataSource;
     VLCLibraryGroupDataSource *_libraryAudioGroupDataSource;
+    VLCLibrarySortingMenuController *_librarySortingMenuController;
     VLCMediaSourceDataSource *_mediaSourceDataSource;
     VLCLibraryAlternativeAudioViewController *_alternativeAudioViewController;
     VLCPlaylistSortingMenuController *_playlistSortingMenuController;
@@ -465,6 +467,14 @@ static int ShowController(vlc_object_t *p_this, const char 
*psz_variable,
     [NSMenu 
popUpContextMenu:_playlistSortingMenuController.playlistSortingMenu 
withEvent:[NSApp currentEvent] forView:sender];
 }
 
+- (IBAction)sortLibrary:(id)sender
+{
+    if (!_librarySortingMenuController) {
+        _librarySortingMenuController = [[VLCLibrarySortingMenuController 
alloc] init];
+    }
+    [NSMenu popUpContextMenu:_librarySortingMenuController.librarySortingMenu 
withEvent:[NSApp currentEvent] forView:sender];
+}
+
 - (IBAction)openMedia:(id)sender
 {
     [[[VLCMain sharedInstance] open] openFileGeneric];
diff --git a/po/POTFILES.in b/po/POTFILES.in
index e6f5357be7..9a3e4b312f 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -475,6 +475,8 @@ modules/gui/macosx/library/VLCLibraryMenuController.h
 modules/gui/macosx/library/VLCLibraryMenuController.m
 modules/gui/macosx/library/VLCLibraryModel.h
 modules/gui/macosx/library/VLCLibraryModel.m
+modules/gui/macosx/library/VLCLibrarySortingMenuController.h
+modules/gui/macosx/library/VLCLibrarySortingMenuController.m
 modules/gui/macosx/library/VLCLibraryTableCellView.h
 modules/gui/macosx/library/VLCLibraryTableCellView.m
 modules/gui/macosx/library/VLCLibraryVideoDataSource.h

_______________________________________________
vlc-commits mailing list
vlc-commits@videolan.org
https://mailman.videolan.org/listinfo/vlc-commits

Reply via email to