KMToolbarConfigViewController.swift 11 KB

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