@@ -51,8 +51,7 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
51
51
var draggedItems : [ ItemIdentifierType ] = [ ]
52
52
var draggedParent : ItemIdentifierType ?
53
53
var draggedIndexes : [ Int ] = [ ]
54
- var dropItems : [ ItemIdentifierType ] = [ ]
55
- var dropContent : [ PasteboardReading ] = [ ]
54
+ var canDrop : NSDragOperation = [ ]
56
55
var isApplyingSnapshot = false
57
56
var didApplyGroupItems = false
58
57
lazy var groupRowTableColumn = NSTableColumn ( )
@@ -670,6 +669,9 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
670
669
children = currentSnapshot. children ( of: parents [ 0 ] )
671
670
}
672
671
let indexes = self . draggedItems. compactMap ( { children. firstIndex ( of: $0 ) } )
672
+
673
+ if session. source as? NSOutlineView === outlineView {
674
+ }
673
675
if ( parents. isEmpty || parents. count == 1 ) , indexes. isIncrementing ( ) {
674
676
self . draggedParent = parents. first
675
677
self . draggedIndexes = indexes. sorted ( )
@@ -680,8 +682,6 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
680
682
draggedItems = [ ]
681
683
draggedIndexes = [ ]
682
684
draggedParent = nil
683
- dropItems = [ ]
684
- dropContent = [ ]
685
685
}
686
686
687
687
/*
@@ -775,12 +775,9 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
775
775
isApplyingSnapshot = false
776
776
}
777
777
*/
778
-
779
- var previewIndex : Int ? = nil
780
- var previewParent : ItemIdentifierType ? = nil
781
- var previewItems : [ ItemIdentifierType ] = [ ]
782
-
778
+
783
779
public func outlineView( _ outlineView: NSOutlineView , validateDrop info: any NSDraggingInfo , proposedItem item: Any ? , proposedChildIndex index: Int ) -> NSDragOperation {
780
+ canDrop = [ ]
784
781
if info. draggingSource as? NSOutlineView === outlineView {
785
782
if let item = item as? ItemIdentifierType , draggedItems. contains ( item) {
786
783
return [ ]
@@ -800,18 +797,18 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
800
797
}
801
798
*/
802
799
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
808
805
}
809
806
}
810
807
return [ ]
811
808
}
812
809
813
810
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 {
815
812
var snapshot = currentSnapshot
816
813
var index = index
817
814
if index == - 1 {
@@ -830,13 +827,17 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
830
827
apply ( snapshot, reorderingHandlers. animates ? . animated : . withoutAnimation)
831
828
reorderingHandlers. didReorder ? ( transaction)
832
829
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) ?? [ ]
834
835
var snapshot = snapshot ( )
835
- snapshot. insert ( dropItems , atIndex: index, of: item as? ItemIdentifierType )
836
+ snapshot. insert ( items , atIndex: index, of: item)
836
837
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)
840
841
return true
841
842
}
842
843
return false
@@ -1013,15 +1014,20 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
1013
1014
/// Handlers for dropping items inside the outline view.
1014
1015
public struct DroppingHandlers {
1015
1016
/// 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
+
1017
1019
/// 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
+
1019
1022
/// 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
+
1021
1025
/// 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
+
1023
1028
/// A Boolean value that indicates whether dropping items is animated.
1024
1029
public var animates : Bool = false
1030
+
1025
1031
/// A Boolean value that indicates whether the dropped items are previewed.
1026
1032
public var previewItems = true
1027
1033
}
0 commit comments