|
@@ -10,8 +10,11 @@ import Cocoa
|
|
|
typealias KMToolbarConfigViewControllerCallback = (NSApplication.ModalResponse) -> Void
|
|
|
class KMToolbarConfigViewController: NSViewController {
|
|
|
|
|
|
+ @IBOutlet weak var showLabel: NSTextField!
|
|
|
@IBOutlet weak var collectionView: NSCollectionView!
|
|
|
+ @IBOutlet weak var removedLabel: NSTextField!
|
|
|
@IBOutlet weak var removedCollectionView: NSCollectionView!
|
|
|
+ @IBOutlet weak var tipLabel: NSTextField!
|
|
|
@IBOutlet weak var cancelButton: NSButton!
|
|
|
@IBOutlet weak var confirmButton: NSButton!
|
|
|
|
|
@@ -39,11 +42,28 @@ class KMToolbarConfigViewController: NSViewController {
|
|
|
}
|
|
|
|
|
|
func initDefalutValue() {
|
|
|
+ self.showLabel.stringValue = NSLocalizedString("Show", comment: "")
|
|
|
self.collectionView.delegate = self
|
|
|
self.collectionView.dataSource = self
|
|
|
|
|
|
self.collectionView.register(KMToolbarConfigViewItem.self, forItemWithIdentifier: self.cellIdentifier_)
|
|
|
(self.collectionView.collectionViewLayout as? NSCollectionViewFlowLayout)?.scrollDirection = .horizontal
|
|
|
+// self.collectionView.registerForDraggedTypes([kKMLocalForDraggedTypes, .fileURL, .string])
|
|
|
+ collectionView.registerForDraggedTypes([NSPasteboard.PasteboardType.string, kKMLocalForDraggedTypes, .fileURL, .pdf])
|
|
|
+ collectionView.setDraggingSourceOperationMask(.every, forLocal: false)
|
|
|
+ collectionView.setDraggingSourceOperationMask(.every, forLocal: true)
|
|
|
+
|
|
|
+ self.collectionView.allowsMultipleSelection = false
|
|
|
+// self.collectionView.enclosingScrollView?.drawsBackground = false
|
|
|
+ self.collectionView.enclosingScrollView?.backgroundColor = .gridColor
|
|
|
+ self.collectionView.enclosingScrollView?.wantsLayer = true
|
|
|
+ self.collectionView.enclosingScrollView?.layer?.backgroundColor = NSColor.red.cgColor
|
|
|
+// self.collectionView.wantsLayer = true
|
|
|
+// self.collectionView.layer?.backgroundColor = NSColor.red.cgColor
|
|
|
+
|
|
|
+ self.removedLabel.stringValue = NSLocalizedString("Hide", comment: "")
|
|
|
+
|
|
|
+ self.tipLabel.stringValue = NSLocalizedString("Drag and drop to add, remove and reorder the tools.", comment: "")
|
|
|
|
|
|
self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
|
|
|
self.cancelButton.target = self
|
|
@@ -72,6 +92,10 @@ extension KMToolbarConfigViewController: NSCollectionViewDelegate, NSCollectionV
|
|
|
}
|
|
|
|
|
|
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
|
+ if self.removedCollectionView.isEqual(to: collectionView) {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
if let cnt = self.model.allowedCellIdentifiers?.count {
|
|
|
return cnt + 2
|
|
|
}
|
|
@@ -79,21 +103,25 @@ extension KMToolbarConfigViewController: NSCollectionViewDelegate, NSCollectionV
|
|
|
}
|
|
|
|
|
|
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
|
|
|
+ if self.removedCollectionView.isEqual(to: collectionView) {
|
|
|
+ return NSCollectionViewItem()
|
|
|
+ }
|
|
|
+
|
|
|
let cell = collectionView.makeItem(withIdentifier: self.cellIdentifier_, for: indexPath) as! KMToolbarConfigViewItem
|
|
|
- if indexPath.item == self.model.leftCount {
|
|
|
+ if self.model.isFirstSegI(at: indexPath.item) {
|
|
|
cell.itemView = nil
|
|
|
- } else if indexPath.item == (self.model.leftCount + self.model.centerCount) {
|
|
|
+ } else if self.model.isSecondSegI(at: indexPath.item) {
|
|
|
cell.itemView = nil
|
|
|
} else {
|
|
|
var idx = indexPath.item
|
|
|
- if idx > (self.model.leftCount + self.model.centerCount) {
|
|
|
+ if self.model.isRight(at: idx) {
|
|
|
idx = idx - 2
|
|
|
- } else if idx > self.model.leftCount {
|
|
|
+ } else if self.model.isCenter(at: idx) {
|
|
|
idx = idx - 1
|
|
|
}
|
|
|
|
|
|
let itemId = self.model.allowedCellIdentifiers?[idx] ?? ""
|
|
|
- let item = KMToolbarItemView(itemIdentifier: itemId)
|
|
|
+ let item = KMToolbarConfigTBItemView(itemIdentifier: itemId)
|
|
|
self.model.setupMainItem(item)
|
|
|
cell.itemView = item
|
|
|
}
|
|
@@ -104,16 +132,20 @@ extension KMToolbarConfigViewController: NSCollectionViewDelegate, NSCollectionV
|
|
|
// Layout
|
|
|
|
|
|
func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
|
|
|
- if indexPath.item == self.model.leftCount {
|
|
|
+ if self.removedCollectionView.isEqual(to: collectionView) {
|
|
|
+ return NSSize(width: 48, height: 48)
|
|
|
+ }
|
|
|
+
|
|
|
+ if self.model.isFirstSegI(at: indexPath.item) {
|
|
|
return NSSize(width: self.model.segItemWidth, height: 48)
|
|
|
- } else if indexPath.item == (self.model.leftCount + self.model.centerCount) {
|
|
|
+ } else if self.model.isSecondSegI(at: indexPath.item) {
|
|
|
return NSSize(width: self.model.segItemWidth, height: 48)
|
|
|
}
|
|
|
|
|
|
var idx = indexPath.item
|
|
|
- if idx > (self.model.leftCount + self.model.centerCount) {
|
|
|
+ if self.model.isRight(at: idx) {
|
|
|
idx = idx - 2
|
|
|
- } else if idx > self.model.leftCount {
|
|
|
+ } else if self.model.isCenter(at: idx) {
|
|
|
idx = idx - 1
|
|
|
}
|
|
|
let itemId = self.model.allowedCellIdentifiers?[idx] ?? ""
|
|
@@ -136,5 +168,172 @@ extension KMToolbarConfigViewController: NSCollectionViewDelegate, NSCollectionV
|
|
|
return .init(top: 6, left: 10, bottom: 6, right: 10)
|
|
|
}
|
|
|
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, shouldSelectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
|
|
|
+ return indexPaths
|
|
|
+ }
|
|
|
+
|
|
|
+ // Drag & Drop
|
|
|
+
|
|
|
+// func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
|
|
|
+// if let data = try? NSKeyedArchiver.archivedData(withRootObject: indexPaths, requiringSecureCoding: true) {
|
|
|
+// pasteboard.declareTypes([kKMLocalForDraggedTypes], owner: self)
|
|
|
+// pasteboard.setData(data, forType: kKMLocalForDraggedTypes)
|
|
|
+//
|
|
|
+//// self.dragedIndexPaths.removeAll()
|
|
|
+//// for indexPath in indexPaths {
|
|
|
+//// self.dragedIndexPaths.append(indexPath)
|
|
|
+//// }
|
|
|
+// }
|
|
|
+// return true
|
|
|
+// }
|
|
|
+
|
|
|
+// func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexes: IndexSet, to pasteboard: NSPasteboard) -> Bool {
|
|
|
+// if let data = try? NSKeyedArchiver.archivedData(withRootObject: indexes, requiringSecureCoding: true) {
|
|
|
+// pasteboard.declareTypes([kKMLocalForDraggedTypes], owner: self)
|
|
|
+// pasteboard.setData(data, forType: kKMLocalForDraggedTypes)
|
|
|
+//
|
|
|
+//// self.dragedIndexPaths.removeAll()
|
|
|
+//// for indexPath in indexPaths {
|
|
|
+//// self.dragedIndexPaths.append(indexPath)
|
|
|
+//// }
|
|
|
+// }
|
|
|
+// return true
|
|
|
+// }
|
|
|
+//
|
|
|
+// func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
|
|
|
+// return .every
|
|
|
+// }
|
|
|
+//
|
|
|
+// func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
|
|
|
+// return true
|
|
|
+// }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
|
|
|
+ if self.removedCollectionView.isEqual(to: collectionView) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
|
|
|
+ return String(indexPath.item) as NSPasteboardWriting
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) {
|
|
|
+// if collectionView.isEqual(showCollectionView) {
|
|
|
+// indexPathsOfItemsBeingShowItemDragged = indexPaths
|
|
|
+// } else if collectionView.isEqual(hideCollectionView) {
|
|
|
+// indexPathsOfItemsBeingHideItemDragged = indexPaths
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
|
|
|
+ if collectionView.isEqual(self.removedCollectionView) {
|
|
|
+ return .move
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if collectionView.isEqual(self.collectionView) {
|
|
|
+// if indexPathsOfItemsBeingShowItemDragged.count != 0 {
|
|
|
+ return .move
|
|
|
+// } else {
|
|
|
+// return .copy
|
|
|
+// }
|
|
|
+ }
|
|
|
+// else {
|
|
|
+// if indexPathsOfItemsBeingHideItemDragged.count != 0 {
|
|
|
+// return .move
|
|
|
+// } else {
|
|
|
+// return .copy
|
|
|
+// }
|
|
|
+// }
|
|
|
+ return []
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
|
|
|
+ var result = false
|
|
|
+// if collectionView.isEqual(showCollectionView) {
|
|
|
+// if indexPathsOfItemsBeingShowItemDragged.count != 0 {
|
|
|
+// var toItemIndex = indexPath.item
|
|
|
+// for fromIndexPath in indexPathsOfItemsBeingShowItemDragged {
|
|
|
+// let fromItemIndex = fromIndexPath.item
|
|
|
+// if fromItemIndex > toItemIndex {
|
|
|
+// moveCell(fromIndex: fromItemIndex, toIndex: toItemIndex, collectionView: collectionView)
|
|
|
+// collectionView.animator().moveItem(at: fromIndexPath, to: IndexPath(item: toItemIndex, section: indexPath.section))
|
|
|
+// toItemIndex += 1
|
|
|
+// }
|
|
|
+// }
|
|
|
+// var adjustedToItemIndex = indexPath.item
|
|
|
+// for fromIndexPath in indexPathsOfItemsBeingShowItemDragged.reversed() {
|
|
|
+// let fromItemIndex = fromIndexPath.item
|
|
|
+// if fromItemIndex < adjustedToItemIndex {
|
|
|
+// if adjustedToItemIndex >= showArray.count {
|
|
|
+// adjustedToItemIndex = showArray.count - 1
|
|
|
+// }
|
|
|
+// moveCell(fromIndex: fromItemIndex, toIndex: adjustedToItemIndex, collectionView: collectionView)
|
|
|
+// let adjustedToIndexPath = IndexPath(item: adjustedToItemIndex, section: indexPath.section)
|
|
|
+// collectionView.animator().moveItem(at: fromIndexPath, to: adjustedToIndexPath)
|
|
|
+// adjustedToItemIndex -= 1
|
|
|
+// }
|
|
|
+// }
|
|
|
+// result = true
|
|
|
+// } else if indexPathsOfItemsBeingHideItemDragged.count != 0 {
|
|
|
+// let toItemIndex = indexPathsOfItemsBeingHideItemDragged.first!.item
|
|
|
+// showArray.insert((hideArray[toItemIndex] as! Int) as NSNumber, at: indexPath.item)
|
|
|
+// hideArray.remove(at: toItemIndex)
|
|
|
+// result = true
|
|
|
+//
|
|
|
+// self.dataChange?(self, showArray)
|
|
|
+// }
|
|
|
+// } else if collectionView.isEqual(hideCollectionView) {
|
|
|
+// if indexPathsOfItemsBeingHideItemDragged.count != 0 {
|
|
|
+// var toItemIndex = indexPath.item
|
|
|
+// for fromIndexPath in indexPathsOfItemsBeingHideItemDragged {
|
|
|
+// let fromItemIndex = fromIndexPath.item
|
|
|
+// if fromItemIndex > toItemIndex {
|
|
|
+// moveCell(fromIndex: fromItemIndex, toIndex: toItemIndex, collectionView: collectionView)
|
|
|
+// collectionView.animator().moveItem(at: fromIndexPath, to: IndexPath(item: toItemIndex, section: indexPath.section))
|
|
|
+// toItemIndex += 1
|
|
|
+// }
|
|
|
+// }
|
|
|
+// var adjustedToItemIndex = indexPath.item - 1
|
|
|
+// for fromIndexPath in indexPathsOfItemsBeingHideItemDragged.reversed() {
|
|
|
+// let fromItemIndex = fromIndexPath.item
|
|
|
+// if fromItemIndex < adjustedToItemIndex {
|
|
|
+// if adjustedToItemIndex >= showArray.count {
|
|
|
+// adjustedToItemIndex = showArray.count - 1
|
|
|
+// }
|
|
|
+// moveCell(fromIndex: fromItemIndex, toIndex: adjustedToItemIndex, collectionView: collectionView)
|
|
|
+// let adjustedToIndexPath = IndexPath(item: adjustedToItemIndex, section: indexPath.section)
|
|
|
+// collectionView.animator().moveItem(at: fromIndexPath, to: adjustedToIndexPath)
|
|
|
+// adjustedToItemIndex -= 1
|
|
|
+// }
|
|
|
+// }
|
|
|
+// result = true
|
|
|
+// } else if indexPathsOfItemsBeingShowItemDragged.count != 0 {
|
|
|
+// let toItemIndex = indexPathsOfItemsBeingShowItemDragged.first!.item
|
|
|
+// hideArray.insert((showArray[toItemIndex] as! Int) as NSNumber, at: indexPath.item)
|
|
|
+// showArray.remove(at: toItemIndex)
|
|
|
+// result = true
|
|
|
+//
|
|
|
+// self.dataChange?(self, showArray)
|
|
|
+// }
|
|
|
+// }
|
|
|
+ return result
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
|
|
|
+// if collectionView.isEqual(showCollectionView) {
|
|
|
+// indexPathsOfItemsBeingShowItemDragged.removeAll()
|
|
|
+// removeBox.borderColor = KMAppearance.Layout.h2Color()
|
|
|
+// removeButton.isEnabled = false
|
|
|
+// } else if collectionView.isEqual(hideCollectionView) {
|
|
|
+// indexPathsOfItemsBeingHideItemDragged.removeAll()
|
|
|
+// addBox.borderColor = KMAppearance.Layout.h2Color()
|
|
|
+// addButton.isEnabled = false
|
|
|
+// }
|
|
|
+// showCollectionView.reloadSections(IndexSet(integer: 0))
|
|
|
+// hideCollectionView.reloadSections(IndexSet(integer: 0))
|
|
|
+ }
|
|
|
|
|
|
}
|