123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- //
- // KMToolbarConfigViewController.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2024/5/23.
- //
- 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!
-
- private let cellIdentifier_ = NSUserInterfaceItemIdentifier(rawValue: "ToolbarConfigCellIdentifier")
-
- var model = KMToolbarConfigModel()
-
- var callback: KMToolbarConfigViewControllerCallback?
-
- deinit {
- Swift.debugPrint("KMToolbarConfigViewController deinit.")
- }
-
- convenience init() {
- self.init(nibName: "KMToolbarConfigViewController", bundle: nil)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
-
- // self.view.wantsLayer = true
- // self.view.layer?.backgroundColor = NSColor.red.cgColor
-
- self.initDefalutValue()
- }
-
- 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
- self.cancelButton.action = #selector(buttonClick)
- self.confirmButton.title = NSLocalizedString("Confirm", comment: "")
- self.confirmButton.target = self
- self.confirmButton.action = #selector(buttonClick)
- }
-
- @objc func buttonClick(_ sender: NSButton) {
- var resp: NSApplication.ModalResponse = .cancel
- if self.confirmButton.isEqual(to: sender) {
- resp = .OK
- }
-
- guard let block = self.callback else {
- return
- }
- block(resp)
- }
- }
- extension KMToolbarConfigViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
- func numberOfSections(in collectionView: NSCollectionView) -> Int {
- return 1
- }
-
- 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
- }
- return 0
- }
-
- 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 self.model.isFirstSegI(at: indexPath.item) {
- cell.itemView = nil
- } else if self.model.isSecondSegI(at: indexPath.item) {
- cell.itemView = nil
- } else {
- var idx = indexPath.item
- if self.model.isRight(at: idx) {
- idx = idx - 2
- } else if self.model.isCenter(at: idx) {
- idx = idx - 1
- }
-
- let itemId = self.model.allowedCellIdentifiers?[idx] ?? ""
- let item = KMToolbarConfigTBItemView(itemIdentifier: itemId)
- self.model.setupMainItem(item)
- cell.itemView = item
- }
-
- return cell
- }
-
- // Layout
-
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
- 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 self.model.isSecondSegI(at: indexPath.item) {
- return NSSize(width: self.model.segItemWidth, height: 48)
- }
-
- var idx = indexPath.item
- if self.model.isRight(at: idx) {
- idx = idx - 2
- } else if self.model.isCenter(at: idx) {
- idx = idx - 1
- }
- let itemId = self.model.allowedCellIdentifiers?[idx] ?? ""
- let item = KMToolbarItemView(itemIdentifier: itemId)
- self.model.setupMainItem(item)
- return NSSize(width: item.itemWidth, height: 48)
- }
-
- // 纵向间距
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
- return 5
- }
-
- // 横向间距
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
- return 5
- }
-
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
- 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))
- }
-
- }
|