KMToolbarConfigViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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])
  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.removedCollectionView.delegate = self
  51. self.removedCollectionView.dataSource = self
  52. self.removedCollectionView.register(KMToolbarConfigViewItem.self, forItemWithIdentifier: self.cellIdentifier_)
  53. (self.removedCollectionView.collectionViewLayout as? NSCollectionViewFlowLayout)?.scrollDirection = .horizontal
  54. removedCollectionView.registerForDraggedTypes([NSPasteboard.PasteboardType.string])
  55. removedCollectionView.setDraggingSourceOperationMask(.every, forLocal: false)
  56. removedCollectionView.setDraggingSourceOperationMask(.every, forLocal: true)
  57. self.removedCollectionView.isSelectable = true
  58. self.tipLabel.stringValue = NSLocalizedString("Drag and drop to add, remove and reorder the tools.", comment: "")
  59. self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
  60. self.cancelButton.target = self
  61. self.cancelButton.action = #selector(buttonClick)
  62. self.confirmButton.title = NSLocalizedString("Confirm", comment: "")
  63. self.confirmButton.target = self
  64. self.confirmButton.action = #selector(buttonClick)
  65. }
  66. @objc func buttonClick(_ sender: NSButton) {
  67. var resp: NSApplication.ModalResponse = .cancel
  68. if self.confirmButton.isEqual(to: sender) {
  69. resp = .OK
  70. }
  71. guard let block = self.callback else {
  72. return
  73. }
  74. block(resp)
  75. }
  76. }
  77. extension KMToolbarConfigViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  78. func numberOfSections(in collectionView: NSCollectionView) -> Int {
  79. return 1
  80. }
  81. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  82. if self.removedCollectionView.isEqual(to: collectionView) {
  83. return self.model.removedCellIdentifiers?.count ?? 0
  84. }
  85. if let cnt = self.model.allowedCellIdentifiers?.count {
  86. return cnt + 2
  87. }
  88. return 0
  89. }
  90. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  91. if self.removedCollectionView.isEqual(to: collectionView) {
  92. let cell = collectionView.makeItem(withIdentifier: self.cellIdentifier_, for: indexPath) as! KMToolbarConfigViewItem
  93. let itemId = self.model.removedCellIdentifiers?[indexPath.item] ?? ""
  94. let item = KMToolbarConfigTBItemView(itemIdentifier: itemId)
  95. self.model.setupMainItem(item)
  96. cell.itemView = item
  97. return cell
  98. }
  99. let cell = collectionView.makeItem(withIdentifier: self.cellIdentifier_, for: indexPath) as! KMToolbarConfigViewItem
  100. if self.model.isFirstSegI(at: indexPath.item) {
  101. cell.itemView = nil
  102. } else if self.model.isSecondSegI(at: indexPath.item) {
  103. cell.itemView = nil
  104. } else {
  105. var idx = indexPath.item
  106. if self.model.isRight(at: idx) {
  107. idx = idx - 2
  108. } else if self.model.isCenter(at: idx) {
  109. idx = idx - 1
  110. }
  111. let itemId = self.model.allowedCellIdentifiers?[idx] ?? ""
  112. let item = KMToolbarConfigTBItemView(itemIdentifier: itemId)
  113. self.model.setupMainItem(item)
  114. cell.itemView = item
  115. }
  116. return cell
  117. }
  118. // Layout
  119. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  120. if self.removedCollectionView.isEqual(to: collectionView) {
  121. let itemId = self.model.removedCellIdentifiers?[indexPath.item] ?? ""
  122. let item = KMToolbarItemView(itemIdentifier: itemId)
  123. self.model.setupMainItem(item)
  124. return NSSize(width: item.itemWidth, height: 48)
  125. }
  126. if self.model.isFirstSegI(at: indexPath.item) {
  127. return NSSize(width: self.model.segItemWidth, height: 48)
  128. } else if self.model.isSecondSegI(at: indexPath.item) {
  129. return NSSize(width: self.model.segItemWidth, height: 48)
  130. }
  131. var idx = indexPath.item
  132. if self.model.isRight(at: idx) {
  133. idx = idx - 2
  134. } else if self.model.isCenter(at: idx) {
  135. idx = idx - 1
  136. }
  137. let itemId = self.model.allowedCellIdentifiers?[idx] ?? ""
  138. let item = KMToolbarItemView(itemIdentifier: itemId)
  139. self.model.setupMainItem(item)
  140. return NSSize(width: item.itemWidth, height: 48)
  141. }
  142. // 纵向间距
  143. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  144. return 5
  145. }
  146. // 横向间距
  147. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  148. if self.removedCollectionView.isEqual(to: collectionView) {
  149. return 12
  150. }
  151. return 5
  152. }
  153. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  154. return .init(top: 6, left: 10, bottom: 6, right: 10)
  155. }
  156. func collectionView(_ collectionView: NSCollectionView, shouldSelectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
  157. return indexPaths
  158. }
  159. // Drag & Drop
  160. func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
  161. if self.removedCollectionView.isEqual(to: collectionView) {
  162. return true
  163. }
  164. guard let ip = indexPaths.first else {
  165. return false
  166. }
  167. if self.model.isSeg(at: ip.item) {
  168. return false
  169. }
  170. return true
  171. }
  172. func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
  173. return String(indexPath.item) as NSPasteboardWriting
  174. }
  175. func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  176. if collectionView.isEqual(self.removedCollectionView) {
  177. return .move
  178. }
  179. if collectionView.isEqual(self.collectionView) {
  180. // if indexPathsOfItemsBeingShowItemDragged.count != 0 {
  181. return .move
  182. // } else {
  183. // return .copy
  184. // }
  185. }
  186. return []
  187. }
  188. func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  189. guard let draggingSource = draggingInfo.draggingSource as? NSCollectionView else {
  190. return false
  191. }
  192. if collectionView.isEqual(self.removedCollectionView) {
  193. let isIn = self.removedCollectionView.isEqual(to: draggingSource)
  194. if isIn { // 暂不支持内部拖拽排序
  195. return false
  196. }
  197. // 外部拖拽
  198. if let data = draggingInfo.draggingPasteboard.pasteboardItems?.first?.string(forType: .string) {
  199. if let itemIdx = Int(data) {
  200. if self.model.removeItem(at: itemIdx) {
  201. self.collectionView.reloadData()
  202. self.removedCollectionView.reloadData()
  203. return true
  204. }
  205. }
  206. }
  207. return false
  208. }
  209. // 显示区域
  210. let isIn = self.collectionView.isEqual(to: draggingSource)
  211. if isIn { // 内部拖拽
  212. if let data = draggingInfo.draggingPasteboard.pasteboardItems?.first?.string(forType: .string) {
  213. if let itemIdx = Int(data) {
  214. if itemIdx == indexPath.item { // 无效拖拽
  215. return false
  216. }
  217. if self.model.moveItem(itemIdx: itemIdx, to: indexPath.item) {
  218. self.collectionView.reloadData()
  219. return true
  220. }
  221. }
  222. }
  223. return false
  224. }
  225. // 外部拖拽
  226. if let data = draggingInfo.draggingPasteboard.pasteboardItems?.first?.string(forType: .string) {
  227. if let itemIdx = Int(data) {
  228. if let itemId = self.model.removedCellIdentifiers?.safe_element(for: itemIdx) as? String {
  229. if self.model.addItem(itemId: itemId, at: indexPath.item) {
  230. self.collectionView.reloadData()
  231. self.removedCollectionView.reloadData()
  232. return true
  233. }
  234. }
  235. }
  236. }
  237. return false
  238. }
  239. }