@@ -320,7 +320,9 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
320
320
public func previewImage( for item: Item ) -> NSImage ? {
321
321
let columns = tableView. tableColumns
322
322
guard !columns. isEmpty else { return nil }
323
- return NSImage ( combineHorizontal: columns. compactMap ( { previewImage ( for: item, tableColumn: $0, useColumnWidth: $0 !== columns. last!) } ) , alignment: . top)
323
+
324
+ Swift . print ( " previewImage " , columns. count, columns. compactMap ( { previewImage ( for: item, tableColumn: $0, useColumnWidth: $0 !== columns. last!) ? . size} ) . alignHorizontal ( alignment: . top) , NSImage ( combineHorizontalAlt: columns. compactMap ( { previewImage ( for: item, tableColumn: $0, useColumnWidth: $0 !== columns. last!) } ) , alignment: . top) ? . size ?? " nil " )
325
+ return NSImage ( combineHorizontalAlt: columns. compactMap ( { previewImage ( for: item, tableColumn: $0, useColumnWidth: $0 !== columns. last!) } ) , alignment: . top)
324
326
}
325
327
326
328
/// Returns a preview image of the table cell for the specified item and table column.
@@ -330,7 +332,8 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
330
332
331
333
/// Returns a preview image of the table row for the specified items.
332
334
public func previewImage( for items: [ Item ] ) -> NSImage ? {
333
- NSImage ( combineVertical: items. compactMap ( { previewImage ( for: $0) } ) . reversed ( ) , alignment: . left)
335
+ Swift . print ( " previewImage " , items. count)
336
+ return NSImage ( combineVerticalAlt: items. compactMap ( { previewImage ( for: $0) } ) . reversed ( ) , alignment: . left)
334
337
}
335
338
336
339
private func previewImage( for item: Item , tableColumn: NSTableColumn , useColumnWidth: Bool ) -> NSImage ? {
@@ -677,6 +680,7 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
677
680
678
681
public func tableView( _ tableView: NSTableView , updateDraggingItemsForDrag draggingInfo: NSDraggingInfo ) {
679
682
if canDrop, droppingHandlers. previewDroppedItems, let items = droppingHandlers. items ? ( draggingInfo. dropInfo ( for: tableView) ) , !items. isEmpty, let image = previewImage ( for: items) {
683
+ Swift . print ( " DraggingItems " , tableView. frame. size, image. size)
680
684
draggingInfo. setDraggedImage ( image)
681
685
}
682
686
/*
@@ -1464,3 +1468,47 @@ extension CGRect {
1464
1468
return sqrt ( dx * dx + dy * dy)
1465
1469
}
1466
1470
}
1471
+
1472
+ extension NSUIImage {
1473
+ /**
1474
+ Creates a new image by combining the specified images.
1475
+
1476
+ - Parameters:
1477
+ - images: The images to combine.
1478
+ - orientation: The orientation of the images when combining them.
1479
+ - alignment: The alignment of the images when combining them.
1480
+ - Returns: The combined images, or `nil` if the images couldn't be combined.
1481
+ */
1482
+ public convenience init ? ( combineVerticalAlt images: [ NSUIImage ] , alignment: HorizontalAlignment = . center) {
1483
+ guard let image = NSUIImage . combined ( images: images, vertical: true , alignment: alignment. rawValue) else { return nil }
1484
+ self . init ( size: image. size)
1485
+ lockFocus ( )
1486
+ defer { unlockFocus ( ) }
1487
+ image. draw ( at: . zero, from: CGRect ( origin: . zero, size: image. size) , operation: . copy, fraction: 1.0 )
1488
+ }
1489
+
1490
+ public convenience init ? ( combineHorizontalAlt images: [ NSUIImage ] , alignment: VerticalAlignment = . center) {
1491
+ guard let image = NSUIImage . combined ( images: images, vertical: false , alignment: alignment. rawValue) else { return nil }
1492
+ self . init ( size: image. size)
1493
+ lockFocus ( )
1494
+ defer { unlockFocus ( ) }
1495
+ image. draw ( at: . zero, from: CGRect ( origin: . zero, size: image. size) , operation: . copy, fraction: 1.0 )
1496
+ }
1497
+
1498
+ private static func combined( images: [ NSUIImage ] , vertical: Bool , alignment: Int ) -> NSUIImage ? {
1499
+ guard !images. isEmpty else { return nil }
1500
+ let rects = vertical ? images. map ( { $0. size} ) . alignVertical ( alignment: . init( rawValue: alignment) !) : images. map ( { $0. size} ) . alignHorizontal ( alignment: . init( rawValue: alignment) !)
1501
+ let finalImage = NSUIImage ( size: rects. union ( ) . size)
1502
+ finalImage. lockFocus ( )
1503
+ defer { finalImage. unlockFocus ( ) }
1504
+ var currentPoint : CGPoint = . zero
1505
+ for value in zip ( images, rects) {
1506
+ let image = value. 0
1507
+ let drawRect = value. 1
1508
+ image. draw ( in: drawRect)
1509
+ currentPoint = vertical ? CGPoint ( x: currentPoint. x, y: currentPoint. y + image. size. height) : CGPoint ( x: currentPoint. x + image. size. width, y: currentPoint. y)
1510
+ }
1511
+ Swift . print ( " combined " , finalImage. size, vertical, alignment, images. compactMap ( { $0. size} ) , rects, rects. union ( ) . size, NSGraphicsContext . current? . imageInterpolation. rawValue)
1512
+ return finalImage
1513
+ }
1514
+ }
0 commit comments