KMToolbarConfigViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // KMToolbarConfigViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/5/23.
  6. //
  7. import Cocoa
  8. typealias KMToolbarConfigViewControllerCallback = (NSApplication.ModalResponse) -> Void
  9. class KMToolbarConfigViewController: NSViewController {
  10. @IBOutlet weak var showLabel: NSTextField!
  11. @IBOutlet weak var collectionView: NSCollectionView!
  12. @IBOutlet weak var removedLabel: NSTextField!
  13. @IBOutlet weak var removedCollectionView: NSCollectionView!
  14. @IBOutlet weak var tipLabel: NSTextField!
  15. @IBOutlet weak var cancelButton: NSButton!
  16. @IBOutlet weak var confirmButton: NSButton!
  17. private let cellIdentifier_ = NSUserInterfaceItemIdentifier(rawValue: "ToolbarConfigCellIdentifier")
  18. var model = KMToolbarConfigModel()
  19. var callback: KMToolbarConfigViewControllerCallback?
  20. deinit {
  21. Swift.debugPrint("KMToolbarConfigViewController deinit.")
  22. }
  23. convenience init() {
  24. self.init(nibName: "KMToolbarConfigViewController", bundle: nil)
  25. }
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. // self.view.wantsLayer = true
  29. // self.view.layer?.backgroundColor = NSColor.red.cgColor
  30. self.initDefalutValue()
  31. }
  32. func initDefalutValue() {
  33. self.showLabel.stringValue = NSLocalizedString("Show", comment: "")
  34. self.collectionView.delegate = self
  35. self.collectionView.dataSource = self
  36. self.collectionView.register(KMToolbarConfigViewItem.self, forItemWithIdentifier: self.cellIdentifier_)
  37. (self.collectionView.collectionViewLayout as? NSCollectionViewFlowLayout)?.scrollDirection = .horizontal
  38. // self.collectionView.registerForDraggedTypes([kKMLocalForDraggedTypes, .fileURL, .string])
  39. collectionView.registerForDraggedTypes([NSPasteboard.PasteboardType.string, kKMLocalForDraggedTypes, .fileURL, .pdf])
  40. collectionView.setDraggingSourceOperationMask(.every, forLocal: false)
  41. collectionView.setDraggingSourceOperationMask(.every, forLocal: true)
  42. self.collectionView.allowsMultipleSelection = false
  43. // self.collectionView.enclosingScrollView?.drawsBackground = false
  44. self.collectionView.enclosingScrollView?.backgroundColor = .gridColor
  45. self.collectionView.enclosingScrollView?.wantsLayer = true
  46. self.collectionView.enclosingScrollView?.layer?.backgroundColor = NSColor.red.cgColor
  47. // self.collectionView.wantsLayer = true
  48. // self.collectionView.layer?.backgroundColor = NSColor.red.cgColor
  49. self.removedLabel.stringValue = NSLocalizedString("Hide", comment: "")
  50. self.tipLabel.stringValue = NSLocalizedString("Drag and drop to add, remove and reorder the tools.", comment: "")
  51. self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
  52. self.cancelButton.target = self
  53. self.cancelButton.action = #selector(buttonClick)
  54. self.confirmButton.title = NSLocalizedString("Confirm", comment: "")
  55. self.confirmButton.target = self
  56. self.confirmButton.action = #selector(buttonClick)
  57. }
  58. @objc func buttonClick(_ sender: NSButton) {
  59. var resp: NSApplication.ModalResponse = .cancel
  60. if self.confirmButton.isEqual(to: sender) {
  61. resp = .OK
  62. }
  63. guard let block = self.callback else {
  64. return
  65. }
  66. block(resp)
  67. }
  68. }
  69. extension KMToolbarConfigViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  70. func numberOfSections(in collectionView: NSCollectionView) -> Int {
  71. return 1
  72. }
  73. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  74. if self.removedCollectionView.isEqual(to: collectionView) {
  75. return 0
  76. }
  77. if let cnt = self.model.allowedCellIdentifiers?.count {
  78. return cnt + 2
  79. }
  80. return 0
  81. }
  82. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  83. if self.removedCollectionView.isEqual(to: collectionView) {
  84. return NSCollectionViewItem()
  85. }
  86. let cell = collectionView.makeItem(withIdentifier: self.cellIdentifier_, for: indexPath) as! KMToolbarConfigViewItem
  87. if self.model.isFirstSegI(at: indexPath.item) {
  88. cell.itemView = nil
  89. } else if self.model.isSecondSegI(at: indexPath.item) {
  90. cell.itemView = nil
  91. } else {
  92. var idx = indexPath.item
  93. if self.model.isRight(at: idx) {
  94. idx = idx - 2
  95. } else if self.model.isCenter(at: idx) {
  96. idx = idx - 1
  97. }
  98. let itemId = self.model.allowedCellIdentifiers?[idx] ?? ""
  99. let item = KMToolbarConfigTBItemView(itemIdentifier: itemId)
  100. self.model.setupMainItem(item)
  101. cell.itemView = item
  102. }
  103. return cell
  104. }
  105. // Layout
  106. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  107. if self.removedCollectionView.isEqual(to: collectionView) {
  108. return NSSize(width: 48, height: 48)
  109. }
  110. if self.model.isFirstSegI(at: indexPath.item) {
  111. return NSSize(width: self.model.segItemWidth, height: 48)
  112. } else if self.model.isSecondSegI(at: indexPath.item) {
  113. return NSSize(width: self.model.segItemWidth, height: 48)
  114. }
  115. var idx = indexPath.item
  116. if self.model.isRight(at: idx) {
  117. idx = idx - 2
  118. } else if self.model.isCenter(at: idx) {
  119. idx = idx - 1
  120. }
  121. let itemId = self.model.allowedCellIdentifiers?[idx] ?? ""
  122. let item = KMToolbarItemView(itemIdentifier: itemId)
  123. self.model.setupMainItem(item)
  124. return NSSize(width: item.itemWidth, height: 48)
  125. }
  126. // 纵向间距
  127. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  128. return 5
  129. }
  130. // 横向间距
  131. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  132. return 5
  133. }
  134. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  135. return .init(top: 6, left: 10, bottom: 6, right: 10)
  136. }
  137. func collectionView(_ collectionView: NSCollectionView, shouldSelectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
  138. return indexPaths
  139. }
  140. // Drag & Drop
  141. // func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
  142. // if let data = try? NSKeyedArchiver.archivedData(withRootObject: indexPaths, requiringSecureCoding: true) {
  143. // pasteboard.declareTypes([kKMLocalForDraggedTypes], owner: self)
  144. // pasteboard.setData(data, forType: kKMLocalForDraggedTypes)
  145. //
  146. //// self.dragedIndexPaths.removeAll()
  147. //// for indexPath in indexPaths {
  148. //// self.dragedIndexPaths.append(indexPath)
  149. //// }
  150. // }
  151. // return true
  152. // }
  153. // func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexes: IndexSet, to pasteboard: NSPasteboard) -> Bool {
  154. // if let data = try? NSKeyedArchiver.archivedData(withRootObject: indexes, requiringSecureCoding: true) {
  155. // pasteboard.declareTypes([kKMLocalForDraggedTypes], owner: self)
  156. // pasteboard.setData(data, forType: kKMLocalForDraggedTypes)
  157. //
  158. //// self.dragedIndexPaths.removeAll()
  159. //// for indexPath in indexPaths {
  160. //// self.dragedIndexPaths.append(indexPath)
  161. //// }
  162. // }
  163. // return true
  164. // }
  165. //
  166. // func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  167. // return .every
  168. // }
  169. //
  170. // func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  171. // return true
  172. // }
  173. func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
  174. if self.removedCollectionView.isEqual(to: collectionView) {
  175. return true
  176. }
  177. return true
  178. }
  179. func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
  180. return String(indexPath.item) as NSPasteboardWriting
  181. }
  182. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) {
  183. // if collectionView.isEqual(showCollectionView) {
  184. // indexPathsOfItemsBeingShowItemDragged = indexPaths
  185. // } else if collectionView.isEqual(hideCollectionView) {
  186. // indexPathsOfItemsBeingHideItemDragged = indexPaths
  187. // }
  188. }
  189. func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  190. if collectionView.isEqual(self.removedCollectionView) {
  191. return .move
  192. }
  193. if collectionView.isEqual(self.collectionView) {
  194. // if indexPathsOfItemsBeingShowItemDragged.count != 0 {
  195. return .move
  196. // } else {
  197. // return .copy
  198. // }
  199. }
  200. // else {
  201. // if indexPathsOfItemsBeingHideItemDragged.count != 0 {
  202. // return .move
  203. // } else {
  204. // return .copy
  205. // }
  206. // }
  207. return []
  208. }
  209. func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  210. var result = false
  211. // if collectionView.isEqual(showCollectionView) {
  212. // if indexPathsOfItemsBeingShowItemDragged.count != 0 {
  213. // var toItemIndex = indexPath.item
  214. // for fromIndexPath in indexPathsOfItemsBeingShowItemDragged {
  215. // let fromItemIndex = fromIndexPath.item
  216. // if fromItemIndex > toItemIndex {
  217. // moveCell(fromIndex: fromItemIndex, toIndex: toItemIndex, collectionView: collectionView)
  218. // collectionView.animator().moveItem(at: fromIndexPath, to: IndexPath(item: toItemIndex, section: indexPath.section))
  219. // toItemIndex += 1
  220. // }
  221. // }
  222. // var adjustedToItemIndex = indexPath.item
  223. // for fromIndexPath in indexPathsOfItemsBeingShowItemDragged.reversed() {
  224. // let fromItemIndex = fromIndexPath.item
  225. // if fromItemIndex < adjustedToItemIndex {
  226. // if adjustedToItemIndex >= showArray.count {
  227. // adjustedToItemIndex = showArray.count - 1
  228. // }
  229. // moveCell(fromIndex: fromItemIndex, toIndex: adjustedToItemIndex, collectionView: collectionView)
  230. // let adjustedToIndexPath = IndexPath(item: adjustedToItemIndex, section: indexPath.section)
  231. // collectionView.animator().moveItem(at: fromIndexPath, to: adjustedToIndexPath)
  232. // adjustedToItemIndex -= 1
  233. // }
  234. // }
  235. // result = true
  236. // } else if indexPathsOfItemsBeingHideItemDragged.count != 0 {
  237. // let toItemIndex = indexPathsOfItemsBeingHideItemDragged.first!.item
  238. // showArray.insert((hideArray[toItemIndex] as! Int) as NSNumber, at: indexPath.item)
  239. // hideArray.remove(at: toItemIndex)
  240. // result = true
  241. //
  242. // self.dataChange?(self, showArray)
  243. // }
  244. // } else if collectionView.isEqual(hideCollectionView) {
  245. // if indexPathsOfItemsBeingHideItemDragged.count != 0 {
  246. // var toItemIndex = indexPath.item
  247. // for fromIndexPath in indexPathsOfItemsBeingHideItemDragged {
  248. // let fromItemIndex = fromIndexPath.item
  249. // if fromItemIndex > toItemIndex {
  250. // moveCell(fromIndex: fromItemIndex, toIndex: toItemIndex, collectionView: collectionView)
  251. // collectionView.animator().moveItem(at: fromIndexPath, to: IndexPath(item: toItemIndex, section: indexPath.section))
  252. // toItemIndex += 1
  253. // }
  254. // }
  255. // var adjustedToItemIndex = indexPath.item - 1
  256. // for fromIndexPath in indexPathsOfItemsBeingHideItemDragged.reversed() {
  257. // let fromItemIndex = fromIndexPath.item
  258. // if fromItemIndex < adjustedToItemIndex {
  259. // if adjustedToItemIndex >= showArray.count {
  260. // adjustedToItemIndex = showArray.count - 1
  261. // }
  262. // moveCell(fromIndex: fromItemIndex, toIndex: adjustedToItemIndex, collectionView: collectionView)
  263. // let adjustedToIndexPath = IndexPath(item: adjustedToItemIndex, section: indexPath.section)
  264. // collectionView.animator().moveItem(at: fromIndexPath, to: adjustedToIndexPath)
  265. // adjustedToItemIndex -= 1
  266. // }
  267. // }
  268. // result = true
  269. // } else if indexPathsOfItemsBeingShowItemDragged.count != 0 {
  270. // let toItemIndex = indexPathsOfItemsBeingShowItemDragged.first!.item
  271. // hideArray.insert((showArray[toItemIndex] as! Int) as NSNumber, at: indexPath.item)
  272. // showArray.remove(at: toItemIndex)
  273. // result = true
  274. //
  275. // self.dataChange?(self, showArray)
  276. // }
  277. // }
  278. return result
  279. }
  280. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
  281. // if collectionView.isEqual(showCollectionView) {
  282. // indexPathsOfItemsBeingShowItemDragged.removeAll()
  283. // removeBox.borderColor = KMAppearance.Layout.h2Color()
  284. // removeButton.isEnabled = false
  285. // } else if collectionView.isEqual(hideCollectionView) {
  286. // indexPathsOfItemsBeingHideItemDragged.removeAll()
  287. // addBox.borderColor = KMAppearance.Layout.h2Color()
  288. // addButton.isEnabled = false
  289. // }
  290. // showCollectionView.reloadSections(IndexSet(integer: 0))
  291. // hideCollectionView.reloadSections(IndexSet(integer: 0))
  292. }
  293. }