KMHomeQuickToolsCollectionView.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // KMHomeQuickToolsCollectionView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/10/31.
  6. //
  7. import Cocoa
  8. typealias KMHomeQuickToolsCollectionViewDataDidChange = (_ view: KMHomeQuickToolsCollectionView, _ showData: [NSNumber]) ->Void
  9. class KMHomeQuickToolsCollectionView: KMBaseXibView {
  10. @IBOutlet weak var showBox: NSBox!
  11. @IBOutlet weak var showLabel: NSTextField!
  12. @IBOutlet weak var removeBox: NSBox!
  13. @IBOutlet weak var removeButton: NSButton!
  14. @IBOutlet weak var removeButtonLayoutConstraint: NSLayoutConstraint!
  15. @IBOutlet weak var showCollectionView: NSCollectionView!
  16. @IBOutlet weak var showScrollView: NSScrollView!
  17. @IBOutlet weak var hideBox: NSBox!
  18. @IBOutlet weak var hideLabel: NSTextField!
  19. @IBOutlet weak var addBox: NSBox!
  20. @IBOutlet weak var addButton: NSButton!
  21. @IBOutlet weak var addButtonLayoutConstraint: NSLayoutConstraint!
  22. @IBOutlet weak var hideCollectionView: NSCollectionView!
  23. @IBOutlet weak var hideScrollView: NSScrollView!
  24. @IBOutlet weak var promptLabel: NSTextField!
  25. @IBOutlet weak var showCollectionBox: NSBox!
  26. @IBOutlet weak var hideCollectionBox: NSBox!
  27. var dataChange: KMHomeQuickToolsCollectionViewDataDidChange?
  28. var indexPathsOfItemsBeingShowItemDragged: Set<IndexPath> = []
  29. var indexPathsOfItemsBeingHideItemDragged: Set<IndexPath> = []
  30. var showArray: [NSNumber] = []
  31. var hideArray: [NSNumber] = []
  32. var selectShowItemMutableArray: [IndexPath] = []
  33. var selectHideItemMutableArray: [IndexPath] = []
  34. var collectionItemWidth: CGFloat = 0
  35. let collectionItemHeight: CGFloat = 32
  36. override func setup() {
  37. self.window?.title = NSLocalizedString("Advanced Editing Tools", comment: "")
  38. showLabel.stringValue = NSLocalizedString("Show", comment: "")
  39. hideLabel.stringValue = NSLocalizedString("Hide", comment: "")
  40. removeButton.title = NSLocalizedString("Remove", comment: "")
  41. addButton.title = NSLocalizedString("Add", comment: "")
  42. promptLabel.stringValue = NSLocalizedString("Drag and drop to add, remove, and reorder the tools.", comment: "")
  43. promptLabel.textColor = KMAppearance.Layout.h1Color()
  44. showScrollView.drawsBackground = false
  45. hideScrollView.drawsBackground = false
  46. removeBox.borderColor = NSColor.gridColor
  47. addBox.borderColor = NSColor.gridColor
  48. removeBox.fillColor = NSColor.gridColor
  49. addBox.fillColor = NSColor.gridColor
  50. removeButton.isEnabled = false
  51. addButton.isEnabled = false
  52. let removeButtonConstant = labelReturnedValue(withString: NSLocalizedString("Remove", comment: ""), height: 20, fontSize: 11.0)
  53. let addButtonConstant = labelReturnedValue(withString:NSLocalizedString("Add", comment: ""), height: 20.0, fontSize: 11.0)
  54. var constant: CGFloat = 0
  55. if removeButtonConstant > addButtonConstant {
  56. constant = removeButtonConstant + 30
  57. } else {
  58. constant = addButtonConstant + 30
  59. }
  60. removeButtonLayoutConstraint.constant = constant
  61. addButtonLayoutConstraint.constant = constant
  62. showCollectionView.enclosingScrollView?.scrollerStyle = .legacy
  63. hideCollectionView.enclosingScrollView?.scrollerStyle = .legacy
  64. let shadow = NSShadow()
  65. shadow.shadowColor = KMAppearance.Status.hovColor()
  66. shadow.shadowOffset = NSMakeSize(0, 1)
  67. showCollectionBox.wantsLayer = true
  68. showCollectionBox.shadow = shadow
  69. hideCollectionBox.wantsLayer = true
  70. hideCollectionBox.shadow = shadow
  71. showCollectionView.register(KMHomeQuickToolsWindowCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier("KMHomeQuickToolsWindowCollectionViewItem"))
  72. hideCollectionView.register(KMHomeQuickToolsWindowCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier("KMHomeQuickToolsWindowCollectionViewItem"))
  73. showCollectionView.delegate = self
  74. showCollectionView.dataSource = self
  75. hideCollectionView.delegate = self
  76. hideCollectionView.dataSource = self
  77. showCollectionView.layer?.cornerRadius = 3.0
  78. hideCollectionView.layer?.cornerRadius = 3.0
  79. registerForCollectionViewDragAndDrop()
  80. // self.showCollectionView.reloadData()
  81. // self.hideCollectionView.reloadData()
  82. }
  83. func showData(showArr: [NSNumber], hideArr: [NSNumber]) {
  84. collectionItemWidth = self.itemReturnedWidthValue(withShowArray: showArr, hideArray: hideArr, fontSize: 12, height: collectionItemHeight) + 40
  85. showArray = showArr
  86. hideArray = hideArr
  87. }
  88. func registerForCollectionViewDragAndDrop() {
  89. showCollectionView.registerForDraggedTypes([NSPasteboard.PasteboardType.string])
  90. showCollectionView.setDraggingSourceOperationMask(.every, forLocal: false)
  91. showCollectionView.setDraggingSourceOperationMask(.every, forLocal: true)
  92. hideCollectionView.registerForDraggedTypes([NSPasteboard.PasteboardType.string])
  93. hideCollectionView.setDraggingSourceOperationMask(.every, forLocal: false)
  94. hideCollectionView.setDraggingSourceOperationMask(.every, forLocal: true)
  95. }
  96. func moveCell(fromIndex: Int, toIndex: Int, collectionView: NSCollectionView) {
  97. if collectionView == showCollectionView {
  98. let fromNumber = showArray[fromIndex]
  99. var currentIndex = fromIndex
  100. if fromIndex <= toIndex {
  101. for _ in fromIndex..<toIndex {
  102. currentIndex = showArray.firstIndex(of: fromNumber) ?? fromIndex
  103. if currentIndex < toIndex {
  104. showArray.swapAt(currentIndex, currentIndex + 1)
  105. currentIndex += 1
  106. }
  107. }
  108. } else {
  109. for _ in toIndex..<fromIndex {
  110. currentIndex = showArray.firstIndex(of: fromNumber) ?? fromIndex
  111. if currentIndex > toIndex {
  112. showArray.swapAt(currentIndex, currentIndex - 1)
  113. currentIndex -= 1
  114. }
  115. }
  116. }
  117. } else if collectionView == hideCollectionView {
  118. let fromNumber = hideArray[fromIndex]
  119. var currentIndex = fromIndex
  120. if fromIndex <= toIndex {
  121. for _ in fromIndex..<toIndex {
  122. currentIndex = hideArray.firstIndex(of: fromNumber) ?? fromIndex
  123. if currentIndex < toIndex {
  124. hideArray.swapAt(currentIndex, currentIndex + 1)
  125. currentIndex += 1
  126. }
  127. }
  128. } else {
  129. for _ in toIndex..<fromIndex {
  130. currentIndex = hideArray.firstIndex(of: fromNumber) ?? fromIndex
  131. if currentIndex > toIndex {
  132. hideArray.swapAt(currentIndex, currentIndex - 1)
  133. currentIndex -= 1
  134. }
  135. }
  136. }
  137. }
  138. }
  139. func labelReturnedValue(withString value: String, height: CGFloat, fontSize: CGFloat) -> CGFloat {
  140. if let value = value as NSString? {
  141. let rect = value.boundingRect(with: CGSize(width: .greatestFiniteMagnitude, height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: NSFont.systemFont(ofSize: fontSize)], context: nil)
  142. return rect.size.width
  143. }
  144. return 0
  145. }
  146. }
  147. extension KMHomeQuickToolsCollectionView {
  148. @IBAction func removeButtonAction(_ sender: Any) {
  149. for indexPath in selectShowItemMutableArray {
  150. let itemCount = indexPath.item
  151. hideArray.append(showArray[itemCount])
  152. showArray.remove(at: itemCount)
  153. }
  154. removeBox.borderColor = NSColor.gridColor
  155. removeButton.isEnabled = false
  156. addBox.borderColor = NSColor.gridColor
  157. addButton.isEnabled = false
  158. hideCollectionView.reloadData()
  159. showCollectionView.reloadData()
  160. guard let callBack = dataChange else { return }
  161. callBack(self, showArray)
  162. }
  163. @IBAction func addButtonAction(_ sender: Any) {
  164. for indexPath in selectHideItemMutableArray {
  165. let itemCount = indexPath.item
  166. showArray.append(hideArray[itemCount])
  167. hideArray.remove(at: itemCount)
  168. }
  169. removeBox.borderColor = NSColor.gridColor
  170. addButton.isEnabled = false
  171. addBox.borderColor = NSColor.gridColor
  172. removeButton.isEnabled = false
  173. showCollectionView.reloadData()
  174. hideCollectionView.reloadData()
  175. guard let callBack = dataChange else { return }
  176. callBack(self, showArray)
  177. }
  178. }
  179. extension KMHomeQuickToolsCollectionView: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  180. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  181. let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHomeQuickToolsWindowCollectionViewItem"), for: indexPath) as! KMHomeQuickToolsWindowCollectionViewItem
  182. if collectionView.isEqual(showCollectionView) {
  183. item.model = KMQucikToolsModel(type: DataNavigationViewButtonActionType(rawValue: Int(truncating: showArray[indexPath.item])))
  184. } else if collectionView.isEqual(hideCollectionView) {
  185. item.model = KMQucikToolsModel(type: DataNavigationViewButtonActionType(rawValue: Int(truncating: hideArray[indexPath.item])))
  186. }
  187. return item
  188. }
  189. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  190. if collectionView.isEqual(showCollectionView) {
  191. return showArray.count
  192. } else if collectionView.isEqual(hideCollectionView) {
  193. return hideArray.count
  194. }
  195. return 0
  196. }
  197. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  198. let indexPathArr = Array(indexPaths)
  199. if collectionView.isEqual(showCollectionView) {
  200. self.selectShowItemMutableArray.removeAll()
  201. removeBox.borderColor = KMAppearance.Layout.h2Color()
  202. removeButton.isEnabled = true
  203. for indexPath in indexPathArr {
  204. self.selectShowItemMutableArray.append(indexPath)
  205. }
  206. } else if collectionView.isEqual(hideCollectionView) {
  207. self.selectHideItemMutableArray.removeAll()
  208. addBox.borderColor = KMAppearance.Layout.h2Color()
  209. addButton.isEnabled = true
  210. for indexPath in indexPathArr {
  211. self.selectHideItemMutableArray.append(indexPath)
  212. }
  213. }
  214. }
  215. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  216. return NSSize(width: collectionItemWidth, height: collectionItemHeight)
  217. }
  218. // NSCollectionViewDelegate Drag-and-Drop Methods
  219. func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
  220. return true
  221. }
  222. func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
  223. return String(indexPath.item) as NSPasteboardWriting
  224. }
  225. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) {
  226. if collectionView.isEqual(showCollectionView) {
  227. indexPathsOfItemsBeingShowItemDragged = indexPaths
  228. } else if collectionView.isEqual(hideCollectionView) {
  229. indexPathsOfItemsBeingHideItemDragged = indexPaths
  230. }
  231. }
  232. func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  233. if collectionView.isEqual(showCollectionView) {
  234. if indexPathsOfItemsBeingShowItemDragged.count != 0 {
  235. return .move
  236. } else {
  237. return .copy
  238. }
  239. } else if collectionView.isEqual(hideCollectionView) {
  240. if indexPathsOfItemsBeingHideItemDragged.count != 0 {
  241. return .move
  242. } else {
  243. return .copy
  244. }
  245. }
  246. return []
  247. }
  248. func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  249. var result = false
  250. if collectionView.isEqual(showCollectionView) {
  251. if indexPathsOfItemsBeingShowItemDragged.count != 0 {
  252. var toItemIndex = indexPath.item
  253. for fromIndexPath in indexPathsOfItemsBeingShowItemDragged {
  254. let fromItemIndex = fromIndexPath.item
  255. if fromItemIndex > toItemIndex {
  256. moveCell(fromIndex: fromItemIndex, toIndex: toItemIndex, collectionView: collectionView)
  257. collectionView.animator().moveItem(at: fromIndexPath, to: IndexPath(item: toItemIndex, section: indexPath.section))
  258. toItemIndex += 1
  259. }
  260. }
  261. var adjustedToItemIndex = indexPath.item
  262. for fromIndexPath in indexPathsOfItemsBeingShowItemDragged.reversed() {
  263. let fromItemIndex = fromIndexPath.item
  264. if fromItemIndex < adjustedToItemIndex {
  265. if adjustedToItemIndex >= showArray.count {
  266. adjustedToItemIndex = showArray.count - 1
  267. }
  268. moveCell(fromIndex: fromItemIndex, toIndex: adjustedToItemIndex, collectionView: collectionView)
  269. let adjustedToIndexPath = IndexPath(item: adjustedToItemIndex, section: indexPath.section)
  270. collectionView.animator().moveItem(at: fromIndexPath, to: adjustedToIndexPath)
  271. adjustedToItemIndex -= 1
  272. }
  273. }
  274. result = true
  275. } else if indexPathsOfItemsBeingHideItemDragged.count != 0 {
  276. let toItemIndex = indexPathsOfItemsBeingHideItemDragged.first!.item
  277. showArray.insert((hideArray[toItemIndex] as! Int) as NSNumber, at: indexPath.item)
  278. hideArray.remove(at: toItemIndex)
  279. result = true
  280. self.dataChange?(self, showArray)
  281. }
  282. } else if collectionView.isEqual(hideCollectionView) {
  283. if indexPathsOfItemsBeingHideItemDragged.count != 0 {
  284. var toItemIndex = indexPath.item
  285. for fromIndexPath in indexPathsOfItemsBeingHideItemDragged {
  286. let fromItemIndex = fromIndexPath.item
  287. if fromItemIndex > toItemIndex {
  288. moveCell(fromIndex: fromItemIndex, toIndex: toItemIndex, collectionView: collectionView)
  289. collectionView.animator().moveItem(at: fromIndexPath, to: IndexPath(item: toItemIndex, section: indexPath.section))
  290. toItemIndex += 1
  291. }
  292. }
  293. var adjustedToItemIndex = indexPath.item - 1
  294. for fromIndexPath in indexPathsOfItemsBeingHideItemDragged.reversed() {
  295. let fromItemIndex = fromIndexPath.item
  296. if fromItemIndex < adjustedToItemIndex {
  297. if adjustedToItemIndex >= showArray.count {
  298. adjustedToItemIndex = showArray.count - 1
  299. }
  300. moveCell(fromIndex: fromItemIndex, toIndex: adjustedToItemIndex, collectionView: collectionView)
  301. let adjustedToIndexPath = IndexPath(item: adjustedToItemIndex, section: indexPath.section)
  302. collectionView.animator().moveItem(at: fromIndexPath, to: adjustedToIndexPath)
  303. adjustedToItemIndex -= 1
  304. }
  305. }
  306. result = true
  307. } else if indexPathsOfItemsBeingShowItemDragged.count != 0 {
  308. let toItemIndex = indexPathsOfItemsBeingShowItemDragged.first!.item
  309. hideArray.insert((showArray[toItemIndex] as! Int) as NSNumber, at: indexPath.item)
  310. showArray.remove(at: toItemIndex)
  311. result = true
  312. self.dataChange?(self, showArray)
  313. }
  314. }
  315. return result
  316. }
  317. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
  318. if collectionView.isEqual(showCollectionView) {
  319. indexPathsOfItemsBeingShowItemDragged.removeAll()
  320. removeBox.borderColor = KMAppearance.Layout.h2Color()
  321. removeButton.isEnabled = false
  322. } else if collectionView.isEqual(hideCollectionView) {
  323. indexPathsOfItemsBeingHideItemDragged.removeAll()
  324. addBox.borderColor = KMAppearance.Layout.h2Color()
  325. addButton.isEnabled = false
  326. }
  327. showCollectionView.reloadSections(IndexSet(integer: 0))
  328. hideCollectionView.reloadSections(IndexSet(integer: 0))
  329. }
  330. }
  331. extension KMHomeQuickToolsCollectionView {
  332. func itemReturnedWidthValue(withShowArray showArr: [Any], hideArray hideArr: [Any], fontSize: CGFloat, height: CGFloat) -> CGFloat {
  333. var itemWidth: CGFloat = 0.0
  334. if showArr.count != 0 {
  335. for number in showArr {
  336. let value = KMQucikToolsModel(type: DataNavigationViewButtonActionType(rawValue: Int(truncating: number as! NSNumber))).titleString()
  337. let rect = value.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: NSFont.systemFont(ofSize: fontSize)], context: nil)
  338. if itemWidth < rect.size.width {
  339. itemWidth = rect.size.width
  340. }
  341. }
  342. }
  343. if hideArr.count != 0 {
  344. for number in hideArr {
  345. let value = KMQucikToolsModel(type: DataNavigationViewButtonActionType(rawValue: Int(truncating: number as! NSNumber))).titleString()
  346. let rect = value.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: NSFont.systemFont(ofSize: fontSize)], context: nil)
  347. if itemWidth < rect.size.width {
  348. itemWidth = rect.size.width
  349. }
  350. }
  351. }
  352. return itemWidth
  353. }
  354. }