diff options
author | Damiano Galassi <[email protected]> | 2020-10-05 12:47:13 +0200 |
---|---|---|
committer | Damiano Galassi <[email protected]> | 2020-10-05 12:47:13 +0200 |
commit | 3311ea190feef5dc98f13923dbeb6b05061b1c32 (patch) | |
tree | b19639feafea2088fcc72f1ab4a17606342848b9 | |
parent | 25d75b82bf6c69f2ac45a95e669e88bb69453333 (diff) |
MacGui: use a non deprecated api for table views drag & drop.
-rw-r--r-- | macosx/HBLanguagesSelection.m | 58 | ||||
-rw-r--r-- | macosx/HBPasteboardItem.h | 23 | ||||
-rw-r--r-- | macosx/HBPasteboardItem.m | 42 | ||||
-rw-r--r-- | macosx/HBQueueController.m | 2 | ||||
-rw-r--r-- | macosx/HBQueueTableViewController.m | 56 | ||||
-rw-r--r-- | macosx/HandBrake.xcodeproj/project.pbxproj | 6 |
6 files changed, 119 insertions, 68 deletions
diff --git a/macosx/HBLanguagesSelection.m b/macosx/HBLanguagesSelection.m index 2fb456927..9b6125f8c 100644 --- a/macosx/HBLanguagesSelection.m +++ b/macosx/HBLanguagesSelection.m @@ -5,6 +5,7 @@ It may be used under the terms of the GNU General Public License. */ #import "HBLanguagesSelection.h" +#import "HBPasteboardItem.h" #include "handbrake/lang.h" @implementation HBLang @@ -140,8 +141,6 @@ @end -NSString *kHBLanguagesDragRowsType = @"kHBLanguagesDragRowsType"; - @implementation HBLanguageArrayController - (void)setShowSelectedOnly:(BOOL)showSelectedOnly @@ -171,17 +170,16 @@ NSString *kHBLanguagesDragRowsType = @"kHBLanguagesDragRowsType"; - (void)awakeFromNib { - [self.tableView registerForDraggedTypes:@[kHBLanguagesDragRowsType]]; + [self.tableView registerForDraggedTypes:@[tableViewIndex]]; self.isDragginEnabled = YES; } #pragma mark - NSTableView Delegate -- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard +- (nullable id <NSPasteboardWriting>)tableView:(NSTableView *)tableView pasteboardWriterForRow:(NSInteger)row { if (self.isDragginEnabled) - { - NSData *data = nil; + { if (self.showSelectedOnly) { // If the show selected only filter @@ -191,27 +189,14 @@ NSString *kHBLanguagesDragRowsType = @"kHBLanguagesDragRowsType"; self.showSelectedOnly = NO; NSArray *unfilteredArray = [self arrangedObjects]; - NSMutableIndexSet *unfilteredIndexes = [NSMutableIndexSet indexSet]; - NSUInteger currentIndex = [rowIndexes firstIndex]; - while (currentIndex != NSNotFound) - { - NSUInteger newIndex = [unfilteredArray indexOfObject:filteredArray[currentIndex]]; - [unfilteredIndexes addIndex:newIndex]; - currentIndex = [rowIndexes indexGreaterThanIndex:currentIndex]; - } - - data = [NSKeyedArchiver archivedDataWithRootObject:unfilteredIndexes]; - } - else - { - data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes]; + row = [unfilteredArray indexOfObject:filteredArray[row]]; } - - [pboard declareTypes:@[kHBLanguagesDragRowsType] owner:self]; - [pboard setData:data forType:kHBLanguagesDragRowsType]; + return [[HBPasteboardItem alloc] initWithIndex:row]; + } + else + { + return nil; } - - return self.isDragginEnabled; } - (NSDragOperation)tableView:(NSTableView *)view @@ -222,10 +207,10 @@ NSString *kHBLanguagesDragRowsType = @"kHBLanguagesDragRowsType"; NSDragOperation dragOp = NSDragOperationNone; // if drag source is our own table view, it's a move or a copy - if ([info draggingSource] == view) + if (info.draggingSource == view) { // At a minimum, allow move - dragOp = NSDragOperationMove; + dragOp = NSDragOperationMove; } [view setDropRow:row dropOperation:NSTableViewDropAbove]; @@ -235,25 +220,28 @@ NSString *kHBLanguagesDragRowsType = @"kHBLanguagesDragRowsType"; - (BOOL)tableView:(NSTableView *)view acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation { - if (([info draggingSourceOperationMask] == NSDragOperationCopy)) + if (info.draggingSourceOperationMask == NSDragOperationCopy) { return NO; } - if ([info draggingSource] == view) + if (info.draggingSource == view) { - // Get the index set from the pasteBoard. - NSData *rowData = [[info draggingPasteboard] dataForType:kHBLanguagesDragRowsType]; - NSIndexSet *indexSet = [NSKeyedUnarchiver unarchiveObjectWithData:rowData]; + NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init]; + for (NSPasteboardItem *item in info.draggingPasteboard.pasteboardItems) + { + NSNumber *index = [item propertyListForType:tableViewIndex]; + [indexes addIndex:index.integerValue]; + } - NSUInteger i = [indexSet countOfIndexesInRange:NSMakeRange(0, row)]; + NSUInteger i = [indexes countOfIndexesInRange:NSMakeRange(0, row)]; // Rearrange the objects. - [self moveObjectsInArrangedObjectsFromIndexes:indexSet toIndex:row]; + [self moveObjectsInArrangedObjectsFromIndexes:indexes toIndex:row]; // Update the selection. row -= i; - NSIndexSet *selectionSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(row, [indexSet count])]; + NSIndexSet *selectionSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(row, indexes.count)]; [view selectRowIndexes:selectionSet byExtendingSelection:NO]; return YES; diff --git a/macosx/HBPasteboardItem.h b/macosx/HBPasteboardItem.h new file mode 100644 index 000000000..23f41876a --- /dev/null +++ b/macosx/HBPasteboardItem.h @@ -0,0 +1,23 @@ +// +// HBPasteboardWriter.h +// HandBrake +// +// Created by Damiano Galassi on 04/10/20. +// Copyright © 2020 HandBrake. All rights reserved. +// + +#import <Cocoa/Cocoa.h> + +NS_ASSUME_NONNULL_BEGIN + +extern NSPasteboardType const tableViewIndex; + +@interface HBPasteboardItem : NSObject<NSPasteboardWriting> + +- (instancetype)initWithIndex:(NSInteger)index; + +@property (nonatomic, readonly) NSInteger index; + +@end + +NS_ASSUME_NONNULL_END diff --git a/macosx/HBPasteboardItem.m b/macosx/HBPasteboardItem.m new file mode 100644 index 000000000..439ec2a48 --- /dev/null +++ b/macosx/HBPasteboardItem.m @@ -0,0 +1,42 @@ +// +// HBPasteboardWriter.m +// HandBrake +// +// Created by Damiano Galassi on 04/10/20. +// Copyright © 2020 HandBrake. All rights reserved. +// + +#import "HBPasteboardItem.h" + +NSPasteboardType const tableViewIndex = @"fr.handbrake.tableViewIndex"; + +@implementation HBPasteboardItem + +- (instancetype)initWithIndex:(NSInteger)index +{ + self = [super init]; + if (self) + { + _index = index; + } + return self; +} + +- (NSArray<NSPasteboardType> *)writableTypesForPasteboard:(NSPasteboard *)pasteboard +{ + return @[tableViewIndex]; +} + +- (nullable id)pasteboardPropertyListForType:(nonnull NSPasteboardType)type +{ + if ([type isEqualTo:tableViewIndex]) + { + return @(self.index); + } + else + { + return nil; + } +} + +@end diff --git a/macosx/HBQueueController.m b/macosx/HBQueueController.m index 928663581..d972a1896 100644 --- a/macosx/HBQueueController.m +++ b/macosx/HBQueueController.m @@ -20,7 +20,7 @@ @import HandBrakeKit; -@interface HBQueueController () <NSUserNotificationCenterDelegate, HBQueueTableViewControllerDelegate, HBQueueDetailsViewControllerDelegate> +@interface HBQueueController () <NSToolbarItemValidation, NSMenuItemValidation, NSUserNotificationCenterDelegate, HBQueueTableViewControllerDelegate, HBQueueDetailsViewControllerDelegate> @property (nonatomic) NSSplitViewController *splitViewController; @property (nonatomic) HBQueueTableViewController *tableViewController; diff --git a/macosx/HBQueueTableViewController.m b/macosx/HBQueueTableViewController.m index 92e834f5e..84090af5a 100644 --- a/macosx/HBQueueTableViewController.m +++ b/macosx/HBQueueTableViewController.m @@ -11,17 +11,12 @@ #import "HBQueueItemView.h" #import "HBQueueItemWorkingView.h" #import "NSArray+HBAdditions.h" +#import "HBPasteboardItem.h" -// Pasteboard type for or drag operations -#define HBQueueDragDropPboardType @"HBQueueCustomTableViewPboardType" - -@interface HBQueueTableViewController () <NSTableViewDataSource, NSTableViewDelegate, HBQueueItemViewDelegate> +@interface HBQueueTableViewController () <NSMenuItemValidation, NSTableViewDataSource, NSTableViewDelegate, HBQueueItemViewDelegate> @property (nonatomic, weak, readonly) HBQueue *queue; -@property (nonatomic) NSArray<id<HBQueueItem>> *dragNodesArray; - @property (nonatomic, strong) id<HBQueueTableViewControllerDelegate> delegate; - @property (nonatomic, weak) IBOutlet HBTableView *tableView; @end @@ -48,8 +43,7 @@ [super viewDidLoad]; // lets setup our queue list table view for drag and drop here - [self.tableView registerForDraggedTypes:@[HBQueueDragDropPboardType]]; - [self.tableView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES]; + [self.tableView registerForDraggedTypes:@[tableViewIndex]]; [self.tableView setVerticalMotionCanBeginDrag:YES]; [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidAddItemNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) { @@ -353,41 +347,39 @@ [self removeSelectedQueueItem:tableView]; } -- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard +- (nullable id <NSPasteboardWriting>)tableView:(NSTableView *)tableView pasteboardWriterForRow:(NSInteger)row { - NSArray<id<HBQueueItem>> *items = [self.queue.items objectsAtIndexes:rowIndexes]; - // Dragging is only allowed of the pending items. - if (items[0].state != HBQueueItemStateReady) - { - return NO; - } - - self.dragNodesArray = items; - - // Provide data for our custom type, and simple NSStrings. - [pboard declareTypes:@[HBQueueDragDropPboardType] owner:self]; - - // the actual data doesn't matter since DragDropSimplePboardType drags aren't recognized by anyone but us!. - [pboard setData:[NSData data] forType:HBQueueDragDropPboardType]; - - return YES; + return [[HBPasteboardItem alloc] initWithIndex:row]; } - (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation { - // Don't allow dropping ONTO an item since they can't really contain any children. - BOOL isOnDropTypeProposal = dropOperation == NSTableViewDropOn; - if (isOnDropTypeProposal) + NSDragOperation dragOp = NSDragOperationNone; + + // if drag source is our own table view, it's a move or a copy + if (info.draggingSource == tableView) { - return NSDragOperationNone; + // At a minimum, allow move + dragOp = NSDragOperationMove; } - return NSDragOperationMove; + [tableView setDropRow:row dropOperation:NSTableViewDropAbove]; + + return dragOp; } - (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation { - [self.queue moveItems:self.dragNodesArray toIndex:row]; + NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init]; + for (NSPasteboardItem *item in info.draggingPasteboard.pasteboardItems) + { + NSNumber *index = [item propertyListForType:tableViewIndex]; + [indexes addIndex:index.integerValue]; + } + + NSArray *items = [self.queue.items objectsAtIndexes:indexes]; + [self.queue moveItems:items toIndex:row]; + return YES; } diff --git a/macosx/HandBrake.xcodeproj/project.pbxproj b/macosx/HandBrake.xcodeproj/project.pbxproj index c4cc6c1b3..046267bfb 100644 --- a/macosx/HandBrake.xcodeproj/project.pbxproj +++ b/macosx/HandBrake.xcodeproj/project.pbxproj @@ -313,6 +313,7 @@ A9DF49271C884C4E008AC14A /* HBJobTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DF49221C884C4E008AC14A /* HBJobTests.m */; }; A9DF49281C884C4E008AC14A /* HBJobUndoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DF49231C884C4E008AC14A /* HBJobUndoTests.m */; }; A9DF492A1C884C4E008AC14A /* HBPresetsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DF49261C884C4E008AC14A /* HBPresetsTests.m */; }; + A9E05A082529E77F000E0E9A /* HBPasteboardItem.m in Sources */ = {isa = PBXBuildFile; fileRef = A9E05A072529E77F000E0E9A /* HBPasteboardItem.m */; }; A9E1467B16BC2ABD00C307BC /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A9E1467A16BC2ABD00C307BC /* QuartzCore.framework */; }; A9E1468016BC2AD800C307BC /* NextTemplate.pdf in Resources */ = {isa = PBXBuildFile; fileRef = A9E1467C16BC2AD800C307BC /* NextTemplate.pdf */; }; A9E1468116BC2AD800C307BC /* PauseTemplate.pdf in Resources */ = {isa = PBXBuildFile; fileRef = A9E1467D16BC2AD800C307BC /* PauseTemplate.pdf */; }; @@ -1022,6 +1023,8 @@ A9DF49221C884C4E008AC14A /* HBJobTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBJobTests.m; sourceTree = "<group>"; }; A9DF49231C884C4E008AC14A /* HBJobUndoTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBJobUndoTests.m; sourceTree = "<group>"; }; A9DF49261C884C4E008AC14A /* HBPresetsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBPresetsTests.m; sourceTree = "<group>"; }; + A9E05A062529E77F000E0E9A /* HBPasteboardItem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HBPasteboardItem.h; sourceTree = "<group>"; }; + A9E05A072529E77F000E0E9A /* HBPasteboardItem.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HBPasteboardItem.m; sourceTree = "<group>"; }; A9E1467A16BC2ABD00C307BC /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = /System/Library/Frameworks/QuartzCore.framework; sourceTree = "<absolute>"; }; A9E1467C16BC2AD800C307BC /* NextTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = NextTemplate.pdf; sourceTree = "<group>"; }; A9E1467D16BC2AD800C307BC /* PauseTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = PauseTemplate.pdf; sourceTree = "<group>"; }; @@ -1581,6 +1584,8 @@ A943873F22ED911B005F701B /* HBJob+HBAdditions.h */, A943874022ED911B005F701B /* HBJob+HBAdditions.m */, 273F20BD14ADC09F0021BE6D /* main.m */, + A9E05A062529E77F000E0E9A /* HBPasteboardItem.h */, + A9E05A072529E77F000E0E9A /* HBPasteboardItem.m */, ); name = Others; sourceTree = "<group>"; @@ -2537,6 +2542,7 @@ A9D0FA771C1C284D0003F2A9 /* HBFocusRingView.m in Sources */, A91AFD0C1A948827009BECED /* HBOutputFileWriter.m in Sources */, A9D363512209C08600D8EFEA /* HBQueueItemView.m in Sources */, + A9E05A082529E77F000E0E9A /* HBPasteboardItem.m in Sources */, A95121E61B5F7BE700FD773D /* NSArray+HBAdditions.m in Sources */, A96664B51CCE48F700DA4A57 /* HBPictureHUDController.m in Sources */, A957EBCD218DBE5900007988 /* HBAutoNamer.m in Sources */, |