Skip to content

Commit c4296bd

Browse files
committed
updated
1 parent 5330ecf commit c4296bd

File tree

3 files changed

+85
-53
lines changed

3 files changed

+85
-53
lines changed

Sources/AdvancedCollectionTableView/DiffableDataSource/NSCollectionView/CollectionViewDiffableDataSource.swift

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,19 @@ open class CollectionViewDiffableDataSource<Section: Identifiable & Hashable, El
924924
/// A Boolean value that indicates whether reordering elements is animated.
925925
public var animates: Bool = true
926926

927+
/**
928+
The handler that determines if elements can be dropped to another element while reordering. The default value is `nil` which indicates that elements can't be inserted.
929+
930+
To enable dropping of elements to another item while reordering, you also have to provide ``didDrop``.
931+
*/
932+
var canDrop: ((_ elements: [Element], _ target: Element) -> Bool)?
933+
934+
/// The handler that that gets called before dropping elements to another element.
935+
var willDrop: ((_ elements: [Element], _ target: Element, _ transaction: DiffableDataSourceTransaction<Section, Element>) -> Void)?
936+
937+
/// The handler that that gets called after dropping elements to another element.
938+
var didDrop: ((_ elements: [Element], _ target: Element, _ transaction: DiffableDataSourceTransaction<Section, Element>) -> ())?
939+
927940
/*
928941
var droppable: Bool {
929942
canDrop != nil && didDrop != nil
@@ -994,20 +1007,13 @@ open class CollectionViewDiffableDataSource<Section: Identifiable & Hashable, El
9941007

9951008
/// Handlers for dragging pasteboard items inside the collection view.
9961009
public struct DroppingHandlers {
997-
/// The handler that determines whether the proposed drop can be dropped to an element.
998-
public var canDropInto: ((_ dropInfo: DropInfo, _ element: Element) -> Bool)?
999-
/// The handler that gets called when pasteboard content is dropped to an element.
1000-
public var didDropInto: ((_ dropInfo: DropInfo, _ element: Element)->())?
1001-
var isDroppableInto: Bool {
1002-
canDropInto != nil && didDropInto != nil
1003-
}
1004-
10051010
/**
10061011
The handler that determines whether the pasteboard content can be dropped to the collection view.
10071012

10081013
- Parameter dropInfo: The information about the proposed drop.
10091014
*/
10101015
public var canDrop: ((_ dropInfo: DropInfo) -> Bool)?
1016+
10111017
/**
10121018
The handler that gets called when pasteboard content is about to drop inside the collection view.
10131019

@@ -1017,6 +1023,7 @@ open class CollectionViewDiffableDataSource<Section: Identifiable & Hashable, El
10171023
- transaction: The transaction for the drop.
10181024
*/
10191025
public var willDrop: ((_ dropInfo: DropInfo, _ newElements: [Element], _ transaction: DiffableDataSourceTransaction<Section, Element>) -> ())?
1026+
10201027
/**
10211028
The handler that gets called when pasteboard content was dropped inside the collection view.
10221029

@@ -1026,10 +1033,22 @@ open class CollectionViewDiffableDataSource<Section: Identifiable & Hashable, El
10261033
- transaction: The transaction for the drop.
10271034
*/
10281035
public var didDrop: ((_ dropInfo: DropInfo, _ newElements: [Element], _ transaction: DiffableDataSourceTransaction<Section, Element>) -> ())?
1036+
10291037
/// The handler that determinates the elements for the proposed drop.
10301038
public var elements: ((_ dropInfo: DropInfo) -> ([Element]))?
1039+
10311040
/// A Boolean value that indicates whether dropping elements is animated.
10321041
public var animates: Bool = true
1042+
1043+
/// The handler that determines whether the proposed drop can be dropped to an element.
1044+
public var canDropInto: ((_ dropInfo: DropInfo, _ element: Element) -> Bool)?
1045+
1046+
/// The handler that gets called when pasteboard content is dropped to an element.
1047+
public var didDropInto: ((_ dropInfo: DropInfo, _ element: Element)->())?
1048+
1049+
var isDroppableInto: Bool {
1050+
canDropInto != nil && didDropInto != nil
1051+
}
10331052
}
10341053
}
10351054

Sources/AdvancedCollectionTableView/DiffableDataSource/NSOutlineView/OutlineViewDiffableDataSource.swift

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
5151
var draggedItems: [ItemIdentifierType] = []
5252
var draggedParent: ItemIdentifierType?
5353
var draggedIndexes: [Int] = []
54-
var dropItems: [ItemIdentifierType] = []
55-
var dropContent: [PasteboardReading] = []
54+
var canDrop: NSDragOperation = []
5655
var isApplyingSnapshot = false
5756
var didApplyGroupItems = false
5857
lazy var groupRowTableColumn = NSTableColumn()
@@ -670,6 +669,9 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
670669
children = currentSnapshot.children(of: parents[0])
671670
}
672671
let indexes = self.draggedItems.compactMap({ children.firstIndex(of: $0 ) })
672+
673+
if session.source as? NSOutlineView === outlineView {
674+
}
673675
if (parents.isEmpty || parents.count == 1), indexes.isIncrementing() {
674676
self.draggedParent = parents.first
675677
self.draggedIndexes = indexes.sorted()
@@ -680,8 +682,6 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
680682
draggedItems = []
681683
draggedIndexes = []
682684
draggedParent = nil
683-
dropItems = []
684-
dropContent = []
685685
}
686686

687687
/*
@@ -775,12 +775,9 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
775775
isApplyingSnapshot = false
776776
}
777777
*/
778-
779-
var previewIndex: Int? = nil
780-
var previewParent: ItemIdentifierType? = nil
781-
var previewItems: [ItemIdentifierType] = []
782-
778+
783779
public func outlineView(_ outlineView: NSOutlineView, validateDrop info: any NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
780+
canDrop = []
784781
if info.draggingSource as? NSOutlineView === outlineView {
785782
if let item = item as? ItemIdentifierType, draggedItems.contains(item) {
786783
return []
@@ -800,18 +797,18 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
800797
}
801798
*/
802799
return reorderingHandlers.canReorder?(draggedItems, item as? ItemIdentifierType) ?? true == true ? .move : []
803-
} else if let canDrop = droppingHandlers.canDrop {
804-
dropItems = []
805-
if canDrop(info.dropInfo(for: outlineView), item as? ItemIdentifierType), let items = droppingHandlers.items?(info.dropInfo(for: outlineView), item as? ItemIdentifierType), !items.isEmpty {
806-
dropItems = items
807-
return .move
800+
}
801+
if info.draggingSource as? NSTableView !== outlineView {
802+
if let canDrag = droppingHandlers.canDrop {
803+
self.canDrop = canDrag(info.dropInfo(for: outlineView), item as? ItemIdentifierType)
804+
return self.canDrop
808805
}
809806
}
810807
return []
811808
}
812809

813810
public func outlineView(_ outlineView: NSOutlineView, acceptDrop info: any NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
814-
if let sourceOutlineView = info.draggingSource as? NSOutlineView, sourceOutlineView === outlineView {
811+
if info.draggingSource as? NSOutlineView === outlineView {
815812
var snapshot = currentSnapshot
816813
var index = index
817814
if index == -1 {
@@ -830,13 +827,17 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
830827
apply(snapshot, reorderingHandlers.animates ? .animated : .withoutAnimation)
831828
reorderingHandlers.didReorder?(transaction)
832829
return true
833-
} else if !dropItems.isEmpty {
830+
}
831+
if info.draggingSource as? NSOutlineView !== outlineView, canDrop != [] {
832+
let dropInfo = info.dropInfo(for: outlineView)
833+
let item = item as? ItemIdentifierType
834+
let items = droppingHandlers.items?(dropInfo) ?? []
834835
var snapshot = snapshot()
835-
snapshot.insert(dropItems, atIndex: index, of: item as? ItemIdentifierType)
836+
snapshot.insert(items, atIndex: index, of: item)
836837
let transaction = OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>.init(initial: currentSnapshot, final: snapshot)
837-
droppingHandlers.willDrop?(info.dropInfo(for: outlineView), item as? ItemIdentifierType, dropItems, transaction)
838-
apply(snapshot, droppingHandlers.animates ? .animated : .withoutAnimation)
839-
droppingHandlers.didDrop?(info.dropInfo(for: outlineView), item as? ItemIdentifierType, dropItems, transaction)
838+
droppingHandlers.willDrop?(dropInfo, items, item, transaction)
839+
apply(transaction.finalSnapshot, droppingHandlers.animates ? .animated : .withoutAnimation)
840+
droppingHandlers.didDrop?(dropInfo, items, item, transaction)
840841
return true
841842
}
842843
return false
@@ -1013,15 +1014,20 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
10131014
/// Handlers for dropping items inside the outline view.
10141015
public struct DroppingHandlers {
10151016
/// The handler that determines whether a drop with the pasteboard content is accepted.
1016-
public var canDrop: ((_ dropInfo: DropInfo, _ parent: ItemIdentifierType?) -> Bool)?
1017+
public var canDrop: ((_ dropInfo: DropInfo, _ target: ItemIdentifierType?) -> NSDragOperation)?
1018+
10171019
/// The handler that determinates the items to be inserted for the pasteboard content.
1018-
public var items: ((_ dropInfo: DropInfo, _ parent: ItemIdentifierType?) -> ([ItemIdentifierType]))?
1020+
public var items: ((_ dropInfo: DropInfo) -> ([ItemIdentifierType]))?
1021+
10191022
/// The handler that gets called before new items are dropped.
1020-
public var willDrop: ((_ dropInfo: DropInfo, _ parent: ItemIdentifierType?, _ newItems: [ItemIdentifierType], _ transaction: OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>) -> ())?
1023+
public var willDrop: ((_ dropInfo: DropInfo, _ newItems: [ItemIdentifierType], _ target: ItemIdentifierType?, _ transaction: OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>) -> ())?
1024+
10211025
/// The handler that gets called after new items are dropped.
1022-
public var didDrop: ((_ dropInfo: DropInfo, _ parent: ItemIdentifierType?, _ newItems: [ItemIdentifierType], _ transaction: OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>) -> ())?
1026+
public var didDrop: ((_ dropInfo: DropInfo, _ newItems: [ItemIdentifierType], _ target: ItemIdentifierType?, _ transaction: OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>) -> ())?
1027+
10231028
/// A Boolean value that indicates whether dropping items is animated.
10241029
public var animates: Bool = false
1030+
10251031
/// A Boolean value that indicates whether the dropped items are previewed.
10261032
public var previewItems = true
10271033
}

Sources/AdvancedCollectionTableView/DiffableDataSource/NSTableView/TableViewDiffableDataSource.swift

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,14 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
535535
if !dragingRowIndexes.isEmpty {
536536
let items = dragingRowIndexes.compactMap { item(forRow: $0) }
537537
dragingRowIndexes = []
538-
if dropTargetRow != nil, let didDrop = reorderingHandlers.didDrop, let target = item(forRow: row) {
538+
if dropTargetRow != nil, let target = item(forRow: row) {
539539
dropTargetRow = nil
540-
didDrop(items, target)
540+
var snapshot = currentSnapshot
541+
snapshot.deleteItems(items)
542+
let transaction = DiffableDataSourceTransaction(initial: currentSnapshot, final: snapshot)
543+
reorderingHandlers.willDrop?(items, target, transaction)
544+
apply(transaction.finalSnapshot, reorderingHandlers.animates ? .animated : .withoutAnimation)
545+
reorderingHandlers.didDrop?(items, target, transaction)
541546
} else {
542547
let transaction = moveItemsTransaction(items, to: row)
543548
reorderingHandlers.willReorder?(transaction)
@@ -571,6 +576,7 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
571576
// MARK: Dragging
572577

573578
open func tableView(_ tableView: NSTableView, draggingSession: NSDraggingSession, willBeginAt _: NSPoint, forRowIndexes rowIndexes: IndexSet) {
579+
574580
draggingSession.animatesToStartingPositionsOnCancelOrFail = false
575581
if deletingHandlers.isDeletableByDraggingOutside, let itemsToDelete = deletingHandlers.canDelete?(rowIndexes.compactMap({item(forRow: $0)})), !itemsToDelete.isEmpty {
576582
dragDeleteItems = itemsToDelete
@@ -1107,18 +1113,21 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
11071113
*/
11081114
public var canDrop: ((_ items: [Item], _ target: Item) -> Bool)?
11091115

1116+
/// The handler that that gets called before dropping items to another item.
1117+
public var willDrop: ((_ items: [Item], _ target: Item, _ transaction: DiffableDataSourceTransaction<Section, Item>) -> Void)?
1118+
11101119
/// The handler that that gets called after dropping items.
1111-
public var didDrop: ((_ items: [Item], _ target: Item) -> ())?
1120+
public var didDrop: ((_ items: [Item], _ target: Item, _ transaction: DiffableDataSourceTransaction<Section, Item>) -> ())?
11121121

11131122
/// A Boolean value that indicates whether reordering items is animated.
11141123
public var animates: Bool = false
11151124

1116-
/// A Boolean value that indicates whether rows reorder immediately while the user drags them.
1117-
var reorderImmediately: Bool = true
1118-
11191125
var droppable: Bool {
11201126
canDrop != nil && didDrop != nil
11211127
}
1128+
1129+
/// A Boolean value that indicates whether rows reorder immediately while the user drags them.
1130+
var reorderImmediately: Bool = true
11221131
}
11231132

11241133
/**
@@ -1227,22 +1236,16 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
12271236

12281237
/// Handlers for dropping items inside the table view.
12291238
public struct DroppingHandlers {
1230-
/// The handler that determines whether the proposed drop can be dropped to an item.
1231-
public var canDropInto: ((_ dropInfo: DropInfo, _ item: Item) -> Bool)?
1232-
/// The handler that gets called when pasteboard content is dropped to an item.
1233-
public var didDropInto: ((_ dropInfo: DropInfo, _ item: Item)->())?
1234-
var isDroppableInto: Bool {
1235-
canDropInto != nil && didDropInto != nil
1236-
}
1237-
12381239
/**
12391240
The handler that determines whether the pasteboard content can be dropped to the collection view.
12401241

12411242
- Parameter dropInfo: The information about the proposed drop.
12421243
*/
12431244
public var canDrop: ((_ dropInfo: DropInfo) -> Bool)?
1245+
12441246
/// The handler that determinates the items for the proposed drop.
12451247
public var items: ((_ dropInfo: DropInfo) -> ([Item]))?
1248+
12461249
/**
12471250
The handler that gets called when pasteboard content is about to drop inside the collection view.
12481251

@@ -1252,6 +1255,7 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
12521255
- transaction: The transaction for the drop.
12531256
*/
12541257
public var willDrop: ((_ dropInfo: DropInfo, _ newItems: [Item], _ transaction: DiffableDataSourceTransaction<Section, Item>) -> ())?
1258+
12551259
/**
12561260
The handler that gets called when pasteboard content was dropped inside the collection view.
12571261

@@ -1261,16 +1265,19 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
12611265
- transaction: The transaction for the drop.
12621266
*/
12631267
public var didDrop: ((_ dropInfo: DropInfo, _ newItems: [Item], _ transaction: DiffableDataSourceTransaction<Section, Item>) -> ())?
1268+
12641269
/// A Boolean value that indicates whether dropping items is animated.
12651270
public var animates: Bool = true
1266-
1267-
/// The handler that determines whether items can be dropped on another item.
1268-
var canDropItems: ((_ items: [Item], _ target: Item) -> (Bool))?
1269-
/// The handler that gets called before items are dropped on another item.
1270-
var willDropItems: ((_ content: [Item], _ target: Item, _ transaction: DiffableDataSourceTransaction<Section, Item>?) -> ())?
1271-
/// The handler that gets called after items are dropped on another item.
1272-
var didDropItems: ((_ content: [Item], _ target: Item, _ transaction: DiffableDataSourceTransaction<Section, Item>?) -> ())?
12731271

1272+
/// The handler that determines whether the proposed drop can be dropped to an item.
1273+
public var canDropInto: ((_ dropInfo: DropInfo, _ item: Item) -> Bool)?
1274+
1275+
/// The handler that gets called when pasteboard content is dropped to an item.
1276+
public var didDropInto: ((_ dropInfo: DropInfo, _ item: Item)->())?
1277+
1278+
var isDroppableInto: Bool {
1279+
canDropInto != nil && didDropInto != nil
1280+
}
12741281
}
12751282
}
12761283

0 commit comments

Comments
 (0)