|
@@ -37,9 +37,11 @@ class KMNQuickToolWindowController: NSWindowController, NSCollectionViewDelegate
|
|
|
private var handler: ((String?) -> Void)!
|
|
|
|
|
|
var quickToolsItemMutableArray: [Int] = []
|
|
|
+
|
|
|
+ var draggedItemIndexPath: IndexPath?
|
|
|
|
|
|
- var indexPathsOfItemsBeingShowItemDragged: Set<IndexPath> = []
|
|
|
-
|
|
|
+ var changeIndexPaths: Set<IndexPath> = []
|
|
|
+
|
|
|
lazy var showDates: [KMNHomeQuickToolMode] = {
|
|
|
var fullDatas: [KMNHomeQuickToolMode] = []
|
|
|
for itemRaw in KMNHomeQuickToolManager.defaultManager.fullToolsItemMutableArray {
|
|
@@ -63,9 +65,13 @@ class KMNQuickToolWindowController: NSWindowController, NSCollectionViewDelegate
|
|
|
|
|
|
showCollectionView.delegate = self
|
|
|
showCollectionView.dataSource = self
|
|
|
- showCollectionView.isSelectable = true
|
|
|
+ showCollectionView.isSelectable = true //支持拖拽需设置未True
|
|
|
|
|
|
showCollectionView.register(KMNQuickToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMNQuickToolShowCollectionViewItem"))
|
|
|
+
|
|
|
+ showCollectionView.registerForDraggedTypes([.string])
|
|
|
+ showCollectionView.setDraggingSourceOperationMask(.every, forLocal: false)
|
|
|
+ showCollectionView.setDraggingSourceOperationMask(.every, forLocal: true)
|
|
|
}
|
|
|
|
|
|
func reloadData() {
|
|
@@ -123,6 +129,21 @@ class KMNQuickToolWindowController: NSWindowController, NSCollectionViewDelegate
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private func updateCellOpacity(at location: NSPoint, opacity: CGFloat) {
|
|
|
+ let pointInCollectionView = showCollectionView.convert(location, from: nil)
|
|
|
+ if let indexPath = showCollectionView.indexPathForItem(at: pointInCollectionView) {
|
|
|
+ if let item = showCollectionView.item(at: indexPath) as? KMNQuickToolCollectionViewItem {
|
|
|
+ item.view.alphaValue = opacity
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private func resetCellOpacity() {
|
|
|
+ for item in showCollectionView.visibleItems() {
|
|
|
+ item.view.alphaValue = 1.0
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
//MARK: - Action
|
|
|
@objc func cancelButtonClicked(_ sender: NSView) {
|
|
@@ -131,16 +152,21 @@ class KMNQuickToolWindowController: NSWindowController, NSCollectionViewDelegate
|
|
|
|
|
|
@objc func applyButtonClicked(_ sender: NSView) {
|
|
|
var seleteDatas:[Int] = []
|
|
|
+ var fullDatas:[Int] = []
|
|
|
+
|
|
|
for i in 0 ... showDates.count-1 {
|
|
|
let indexPath = NSIndexPath(forItem: i, inSection: 0)
|
|
|
if let cell = showCollectionView.item(at: indexPath as IndexPath) as? KMNQuickToolCollectionViewItem {
|
|
|
if cell.itemCardView.properties.isSelected == true {
|
|
|
seleteDatas.append(cell.quickToolModel.quickToolType.rawValue)
|
|
|
}
|
|
|
+ fullDatas.append(cell.quickToolModel.quickToolType.rawValue)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
KMNHomeQuickToolManager.defaultManager.quickToolsItemMutableArray = seleteDatas
|
|
|
+ KMNHomeQuickToolManager.defaultManager.fullToolsItemMutableArray = fullDatas
|
|
|
+
|
|
|
self.delegate?.quickToolWindowControllerUpdate()
|
|
|
|
|
|
parentWindow?.endSheet(self.window!)
|
|
@@ -198,7 +224,7 @@ class KMNQuickToolWindowController: NSWindowController, NSCollectionViewDelegate
|
|
|
}
|
|
|
|
|
|
public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
|
|
|
- return NSEdgeInsetsMake(0, 0, 0, 0)
|
|
|
+ return NSEdgeInsetsMake(0, 8, 0, 0)
|
|
|
}
|
|
|
|
|
|
//MARK: - NSCollectionViewDelegate
|
|
@@ -206,30 +232,69 @@ class KMNQuickToolWindowController: NSWindowController, NSCollectionViewDelegate
|
|
|
func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
|
|
|
return true
|
|
|
}
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
|
|
|
-
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
|
|
|
+ return String(indexPath.item) as NSPasteboardWriting
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
|
|
|
+ resetCellOpacity()
|
|
|
+ updateCellOpacity(at: draggingInfo.draggingLocation, opacity: 0.5)
|
|
|
+ return .move
|
|
|
}
|
|
|
|
|
|
func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) {
|
|
|
- indexPathsOfItemsBeingShowItemDragged = indexPaths
|
|
|
+ draggedItemIndexPath = indexPaths.first
|
|
|
}
|
|
|
-
|
|
|
|
|
|
- func collectionView(_ collectionView: NSCollectionView, validateDrop info: NSDraggingInfo, proposedItem: NSCollectionViewItem?, proposedChildIndex index: Int) -> NSDragOperation {
|
|
|
- return .move
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
|
|
|
+ let result = false
|
|
|
+
|
|
|
+ let toItemIndex = indexPath.item
|
|
|
+ guard let fromItemIndex = draggedItemIndexPath?.item else {
|
|
|
+ return result
|
|
|
}
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, acceptDrop info: NSDraggingInfo, toItemAt indexPath: IndexPath?) -> Bool {
|
|
|
- guard let draggingItem = info.draggingPasteboard.pasteboardItems?.first,
|
|
|
- let sourceIndex = draggingItem.string(forType: .string),
|
|
|
- let sourceIndexInt = Int(sourceIndex),
|
|
|
- let destinationIndex = indexPath?.item else { return false }
|
|
|
|
|
|
- let mode = showDates.remove(at: sourceIndexInt)
|
|
|
- showDates.insert(mode, at: destinationIndex)
|
|
|
- collectionView.animator().reloadData()
|
|
|
+ // 确保索引有效
|
|
|
+ if fromItemIndex < 0 || fromItemIndex >= showDates.count || toItemIndex < 0 || toItemIndex > showDates.count {
|
|
|
+ return result // 无效索引
|
|
|
+ }
|
|
|
+ var indexPaths: Set<IndexPath> = []
|
|
|
+
|
|
|
+ if dropOperation == .before {
|
|
|
+ let num = showDates[fromItemIndex]
|
|
|
+ showDates.insert(num, at: toItemIndex)
|
|
|
+
|
|
|
+ if toItemIndex > fromItemIndex { // 向后移动
|
|
|
+ showDates.remove(at: fromItemIndex)
|
|
|
+ } else {
|
|
|
+ showDates.remove(at: fromItemIndex + 1)
|
|
|
+ }
|
|
|
+
|
|
|
+ for i in min(fromItemIndex, toItemIndex)..<(max(fromItemIndex, toItemIndex)+1) {
|
|
|
+ indexPaths.insert(IndexPath(item: i, section: 0))
|
|
|
+ }
|
|
|
+ showCollectionView.reloadItems(at: indexPaths)
|
|
|
+ } else {
|
|
|
+ let fromIndexPath = IndexPath(item: fromItemIndex, section: 0)
|
|
|
+ indexPaths = [indexPath, fromIndexPath]
|
|
|
+
|
|
|
+ showDates.swapAt(fromItemIndex, toItemIndex)
|
|
|
+ showCollectionView.reloadItems(at: indexPaths)
|
|
|
+ }
|
|
|
+ changeIndexPaths = indexPaths
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
|
|
|
+ for targetIndexPath in changeIndexPaths {
|
|
|
+ let targetItem = collectionView.item(at: targetIndexPath)
|
|
|
+ targetItem?.view.shakeAnimation()
|
|
|
+ }
|
|
|
+
|
|
|
+ changeIndexPaths = []
|
|
|
+ resetCellOpacity()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|