KMPDFToolsViewController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. //
  2. // KMPDFToolsViewController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/11/16.
  6. //
  7. import Cocoa
  8. @objc protocol KMPDFToolsViewControllerDelegate {
  9. @objc optional func pdfToolsViewController(_ viewController: KMPDFToolsViewController, didSelectItemsAt indexPaths: [Int])
  10. }
  11. class KMPDFToolsViewController: NSViewController {
  12. @IBOutlet weak var topBox: NSBox!
  13. @IBOutlet weak var pdfToolLabel: NSTextField!
  14. @IBOutlet weak var customizeBox: NSBox!
  15. @IBOutlet weak var customizeBoxHeight: NSLayoutConstraint!
  16. @IBOutlet weak var foldOrUnflodBox: NSBox!
  17. @IBOutlet weak var foldOrUnflodBoxHeight: NSLayoutConstraint!
  18. @IBOutlet weak var middleBox: NSBox!
  19. @IBOutlet weak var homeFastToolLabel: NSTextField!
  20. @IBOutlet weak var confirmBox: NSBox!
  21. @IBOutlet weak var confirmBoxHeight: NSLayoutConstraint!
  22. @IBOutlet weak var cancelBox: NSBox!
  23. @IBOutlet weak var cancelBoxHeight: NSLayoutConstraint!
  24. @IBOutlet weak var homeFastToolBox: NSBox!
  25. @IBOutlet weak var homeFastToolScrollView: NSScrollView!
  26. @IBOutlet weak var homeFastToolCollectionView: KMPDFToolsCollectionView!
  27. @IBOutlet weak var bottomBox: NSBox!
  28. @IBOutlet weak var moreToolLabel: NSTextField!
  29. @IBOutlet weak var moreToolBox: NSBox!
  30. @IBOutlet weak var moreToolScrollView: NSScrollView!
  31. @IBOutlet weak var moreToolCollectionView: KMPDFToolsCollectionView!
  32. @IBOutlet var homeViewController: KMHomeViewController!
  33. @IBOutlet weak var homeFastToolBoxHeightConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var moreToolBoxHeightConstraint: NSLayoutConstraint!
  35. @IBOutlet weak var moreToolBoxTopConstraint: NSLayoutConstraint!
  36. var customizeVC: KMDesignButton!
  37. var foldOrUnflodVC: KMDesignButton!
  38. var confirmVC: KMDesignButton!
  39. var cancelVC: KMDesignButton!
  40. var isFold: Bool!
  41. var isCustomize: Bool! = false
  42. var quickToolsItemMutableArray: [Int] = []
  43. var moreToolsItemMutableArray: [Int] = []
  44. open weak var delete: KMPDFToolsViewControllerDelegate?
  45. var collectionItemWidth: Int! = 230
  46. var homeFastToolItemsDragged: Set<IndexPath> = []
  47. var moreTooltemsDragged: Set<IndexPath> = []
  48. var createFilePopover: NSPopover = NSPopover.init()
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. // Do view setup here.
  52. customizeVC = KMDesignButton.init(withType: .Text)
  53. foldOrUnflodVC = KMDesignButton.init(withType: .Image)
  54. confirmVC = KMDesignButton.init(withType: .Text)
  55. cancelVC = KMDesignButton.init(withType: .Text)
  56. homeFastToolCollectionView.register(KMFastToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFastToolCollectionViewItem"))
  57. moreToolCollectionView.register(KMFastToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFastToolCollectionViewItem"))
  58. isFold = UserDefaults.standard.bool(forKey: "kHomeQucikToolsTypeKey")
  59. let moreToolsArray = UserDefaults.standard.array(forKey: "MoreToolsItemArrayKey") as? [Int] ?? []
  60. if (moreToolsArray.count > 0) {
  61. moreToolsItemMutableArray = moreToolsArray
  62. } else {
  63. moreToolsItemMutableArray = moreToolsFullArray()
  64. UserDefaults.standard.set(moreToolsItemMutableArray, forKey: "MoreToolsItemArrayKey")
  65. UserDefaults.standard.synchronize()
  66. }
  67. let quickToolsArray = UserDefaults.standard.array(forKey: "HomeQucikToolsItemArrayKey") as? [Int] ?? []
  68. if (quickToolsArray.count > 0) {
  69. quickToolsItemMutableArray = quickToolsArray
  70. } else {
  71. quickToolsItemMutableArray = quickToolsFullArray()
  72. UserDefaults.standard.set(quickToolsItemMutableArray, forKey: "HomeQucikToolsItemArrayKey")
  73. UserDefaults.standard.synchronize()
  74. }
  75. registerForCollectionViewDragAndDrop()
  76. }
  77. override func viewDidAppear() {
  78. super.viewDidAppear()
  79. isFold = UserDefaults.standard.bool(forKey: "kHomeQucikToolsTypeKey")
  80. customStateChange(isCustomize)
  81. foldStateChange(isFold)
  82. self.view.postsBoundsChangedNotifications = true
  83. NotificationCenter.default.addObserver(self, selector: #selector(boundsDidChangeNotification), name: NSView.frameDidChangeNotification, object: self.view)
  84. reloadData()
  85. }
  86. // MARK: Init
  87. func initializeUI() -> Void {
  88. topBox.fillColor = .clear
  89. middleBox.fillColor = .clear
  90. bottomBox.fillColor = .clear
  91. customizeBox.fillColor = .clear
  92. customizeBox.contentView = customizeVC.view
  93. customizeVC.target = self
  94. customizeVC.action = #selector(customButtonAction(_:))
  95. customizeVC.button(type: .Sec, size: .m, height: customizeBoxHeight)
  96. foldOrUnflodBox.fillColor = .clear
  97. foldOrUnflodBox.contentView = foldOrUnflodVC.view
  98. foldOrUnflodVC.image = NSImage(named: "icon_btn_more_false_false")!
  99. foldOrUnflodVC.target = self
  100. foldOrUnflodVC.action = #selector(foldOrUnflodButtonAction(_:))
  101. foldOrUnflodVC.button(type: .Sec, size: .m, height: foldOrUnflodBoxHeight)
  102. pdfToolLabel.textColor = NSColor.km_init(hex: "#000000")
  103. pdfToolLabel.font = NSFont(name: "PingFangSC-Semibold", size: 16.0)
  104. homeFastToolLabel.font = NSFont(name: "SFProText-Semibold", size: 14.0)
  105. homeFastToolLabel.textColor = NSColor.km_init(hex: "#616469")
  106. moreToolLabel.font = NSFont(name: "SFProText-Semibold", size: 14.0)
  107. moreToolLabel.textColor = NSColor.km_init(hex: "#616469")
  108. confirmBox.fillColor = .clear
  109. confirmBox.contentView = confirmVC.view
  110. confirmVC.target = self
  111. confirmVC.action = #selector(confirmAction(_:))
  112. confirmVC.button(type: .Cta, size: .m, height: confirmBoxHeight)
  113. cancelBox.fillColor = .clear
  114. cancelBox.contentView = cancelVC.view
  115. cancelVC.target = self
  116. cancelVC.action = #selector(cancelAction(_:))
  117. cancelVC.button(type: .Sec, size: .m, height: cancelBoxHeight)
  118. }
  119. func initLocalization() -> Void {
  120. pdfToolLabel.stringValue = NSLocalizedString("PDF Tools", comment: "")
  121. customizeVC.stringValue = NSLocalizedString("Custom", comment: "")
  122. customizeVC.toolTip = NSLocalizedString("Custom", comment: "")
  123. if isFold {
  124. foldOrUnflodVC.title = NSLocalizedString("Expand", comment: "")
  125. foldOrUnflodVC.toolTip = NSLocalizedString("Expand", comment: "")
  126. } else {
  127. foldOrUnflodVC.title = NSLocalizedString("Collapse", comment: "")
  128. foldOrUnflodVC.toolTip = NSLocalizedString("Collapse", comment: "")
  129. }
  130. homeFastToolLabel.stringValue = NSLocalizedString("Quick Tools", comment: "")
  131. confirmVC.stringValue = NSLocalizedString("Apply", comment: "")
  132. confirmVC.toolTip = NSLocalizedString("Apply", comment: "")
  133. cancelVC.stringValue = NSLocalizedString("Cancel", comment: "")
  134. cancelVC.toolTip = NSLocalizedString("Cancel", comment: "")
  135. moreToolLabel.stringValue = NSLocalizedString("More Tools", comment: "")
  136. }
  137. // MARK: private
  138. func quickToolsFullArray() -> [Int] {
  139. return [DataNavigationViewButtonActionType.PDFToWord.rawValue,
  140. DataNavigationViewButtonActionType.PDFToExcel.rawValue,
  141. DataNavigationViewButtonActionType.PDFToPPT.rawValue,
  142. DataNavigationViewButtonActionType.PDFToImage.rawValue,
  143. DataNavigationViewButtonActionType.Compression.rawValue,
  144. DataNavigationViewButtonActionType.Security.rawValue,
  145. DataNavigationViewButtonActionType.MergePDF.rawValue,
  146. DataNavigationViewButtonActionType.ImageToPDF.rawValue]
  147. }
  148. func moreToolsFullArray() -> [Int] {
  149. return [DataNavigationViewButtonActionType.Watermark.rawValue,
  150. DataNavigationViewButtonActionType.Background.rawValue,
  151. DataNavigationViewButtonActionType.HeaderAndFooter.rawValue,
  152. DataNavigationViewButtonActionType.BatesCode.rawValue,
  153. DataNavigationViewButtonActionType.BatchRemove.rawValue,
  154. DataNavigationViewButtonActionType.MarkCipher.rawValue,
  155. DataNavigationViewButtonActionType.Print.rawValue,
  156. DataNavigationViewButtonActionType.PageEdit.rawValue,
  157. DataNavigationViewButtonActionType.Insert.rawValue,
  158. DataNavigationViewButtonActionType.BreakUp.rawValue,
  159. DataNavigationViewButtonActionType.Extract.rawValue,
  160. DataNavigationViewButtonActionType.Batch.rawValue,
  161. DataNavigationViewButtonActionType.ConvertPDF.rawValue]
  162. }
  163. @IBAction func boundsDidChangeNotification(_ notification: NSNotification) -> Void {
  164. reloadData()
  165. }
  166. func reloadData() -> Void {
  167. initializeUI()
  168. initLocalization()
  169. collectionItemWidth = Int((homeFastToolCollectionView.bounds.width - (20 * 3))/4)
  170. dynamicsChangeMoreToolBoxHeight()
  171. homeFastToolCollectionView.reloadData()
  172. moreToolCollectionView.reloadData()
  173. }
  174. func customStateChange(_ custom: Bool) -> Void {
  175. confirmBox.isHidden = true
  176. cancelBox.isHidden = true
  177. customizeVC.isHidden = true
  178. foldOrUnflodVC.isHidden = true
  179. if custom {
  180. confirmBox.isHidden = false
  181. cancelBox.isHidden = false
  182. } else {
  183. customizeVC.isHidden = false
  184. foldOrUnflodVC.isHidden = false
  185. }
  186. reloadData()
  187. }
  188. func foldStateChange(_ fold: Bool) -> Void {
  189. if fold {
  190. homeFastToolBoxHeightConstraint.constant = 152
  191. dynamicsChangeMoreToolBoxHeight()
  192. UserDefaults.standard.setValue(true, forKey: "kHomeQucikToolsTypeKey")
  193. } else {
  194. homeFastToolBoxHeightConstraint.constant = 248
  195. dynamicsChangeMoreToolBoxHeight()
  196. UserDefaults.standard.setValue(false, forKey: "kHomeQucikToolsTypeKey")
  197. }
  198. UserDefaults.standard.synchronize()
  199. reloadData()
  200. }
  201. func dynamicsChangeMoreToolBoxHeight() -> Void {
  202. let height = self.view.frame.height - middleBox.frame.height - topBox.frame.height - moreToolBoxTopConstraint.constant
  203. if isFold {
  204. if height > 320 {
  205. moreToolBoxHeightConstraint.constant = 320
  206. } else {
  207. moreToolBoxHeightConstraint.constant = height
  208. }
  209. } else {
  210. if height > 512 {
  211. moreToolBoxHeightConstraint.constant = 512
  212. } else {
  213. moreToolBoxHeightConstraint.constant = height
  214. }
  215. }
  216. }
  217. func registerForCollectionViewDragAndDrop() -> Void {
  218. homeFastToolCollectionView.registerForDraggedTypes([.string])
  219. homeFastToolCollectionView.setDraggingSourceOperationMask(.every, forLocal: false)
  220. homeFastToolCollectionView.setDraggingSourceOperationMask(.every, forLocal: true)
  221. moreToolCollectionView.registerForDraggedTypes([.string])
  222. moreToolCollectionView.setDraggingSourceOperationMask(.every, forLocal: false)
  223. moreToolCollectionView.setDraggingSourceOperationMask(.every, forLocal: true)
  224. }
  225. func moveCell(index fromIndex: Int, toIndex: Int, collectionView: NSCollectionView) -> Void {
  226. if collectionView.isEqual(to: homeFastToolCollectionView) {
  227. let currentIndex: Int = quickToolsItemMutableArray[fromIndex]
  228. if fromIndex <= toIndex {
  229. }
  230. } else if collectionView.isEqual(to: moreToolCollectionView) {
  231. }
  232. }
  233. func createPopoverAction(_ data: [String], view: NSView) {
  234. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(data)
  235. self.createFilePopover.contentViewController = vc
  236. self.createFilePopover.animates = true
  237. self.createFilePopover.behavior = .semitransient
  238. self.createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  239. self.createFilePopover.show(relativeTo: CGRect(x: view.bounds.origin.x, y: 10, width: view.bounds.size.width, height: view.bounds.size.height), of: view, preferredEdge: .minY)
  240. vc.downCallback = {(downEntered: Bool, count: String) -> Void in
  241. if downEntered {
  242. self.moreToolsAction(vc)
  243. }
  244. }
  245. }
  246. // MARK: Action
  247. // 是否开启自定义模式
  248. @IBAction func customButtonAction(_ sender: NSButton) {
  249. isCustomize = !isCustomize
  250. customStateChange(isCustomize)
  251. }
  252. @IBAction func foldOrUnflodButtonAction(_ sender: NSButton) {
  253. if self.createFilePopover.isShown {
  254. self.createFilePopover.close()
  255. } else {
  256. if isFold {
  257. self.createPopoverAction([NSLocalizedString("Expand", comment: "")], view: foldOrUnflodBox)
  258. } else {
  259. self.createPopoverAction([NSLocalizedString("Collapse", comment: "")], view: foldOrUnflodBox)
  260. }
  261. }
  262. }
  263. func moreToolsAction(_ viewController: KMHomePopViewController) {
  264. isFold = !isFold
  265. foldStateChange(isFold)
  266. if isFold {
  267. viewController.changePopViewCellData(0, content: NSLocalizedString("Expand", comment: ""))
  268. } else {
  269. viewController.changePopViewCellData(0, content: NSLocalizedString("Collapse", comment: ""))
  270. }
  271. }
  272. @IBAction func cancelAction(_ sender: Any) {
  273. isCustomize = !isCustomize
  274. customStateChange(isCustomize)
  275. }
  276. @IBAction func confirmAction(_ sender: Any) {
  277. UserDefaults.standard.set(moreToolsItemMutableArray, forKey: "MoreToolsItemArrayKey")
  278. UserDefaults.standard.set(quickToolsItemMutableArray, forKey: "HomeQucikToolsItemArrayKey")
  279. UserDefaults.standard.synchronize()
  280. isCustomize = !isCustomize
  281. customStateChange(isCustomize)
  282. }
  283. }
  284. // MARK: - NSCollectionViewDataSource
  285. extension KMPDFToolsViewController: NSCollectionViewDataSource {
  286. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  287. if collectionView.isEqual(to: homeFastToolCollectionView) {
  288. return quickToolsItemMutableArray.count
  289. } else if collectionView.isEqual(to: moreToolCollectionView) {
  290. return moreToolsItemMutableArray.count
  291. }
  292. return 0
  293. }
  294. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  295. if collectionView.isEqual(to: homeFastToolCollectionView) {
  296. let item: KMFastToolCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFastToolCollectionViewItem"), for: indexPath) as! KMFastToolCollectionViewItem
  297. item.card(bg: "card.has-desc.bg.norm", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing")
  298. item.card(bg: "card.has-desc.bg.hov", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Hov)
  299. item.card(bg: "card.has-desc.bg.sel", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Sel)
  300. item.card(bg: "card.has-desc.bg.custom", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Custom)
  301. item.card(bg: "card.has-desc.bg.none", state: .None)
  302. item.card(bg: "card.has-desc.bg.change", titleText: "card.win-text.title", descText: "card.win-text.desc", spacing: "card.item-spacing", state: .Change)
  303. let i: Int = indexPath.item
  304. let index: Int = quickToolsItemMutableArray[i]
  305. item.quickToolsCollectionCellItem(type: DataNavigationViewButtonActionType(rawValue: index) ?? .Batch)
  306. item.refreshHoverStatus(!isCustomize, !isCustomize)
  307. item.refreshUIColor(isCustomize)
  308. return item
  309. } else if collectionView.isEqual(to: moreToolCollectionView) {
  310. let item: KMFastToolCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFastToolCollectionViewItem"), for: indexPath) as! KMFastToolCollectionViewItem
  311. item.card(bg: "card.has-desc.bg.norm", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing")
  312. item.card(bg: "card.has-desc.bg.hov", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Hov)
  313. item.card(bg: "card.has-desc.bg.sel", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Sel)
  314. item.card(bg: "card.has-desc.bg.custom", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Custom)
  315. item.card(bg: "card.has-desc.bg.none", state: .None)
  316. item.card(bg: "card.has-desc.bg.change", titleText: "card.win-text.title", descText: "card.win-text.desc", spacing: "card.item-spacing", state: .Change)
  317. let i: Int = indexPath.item
  318. let index: Int = moreToolsItemMutableArray[i]
  319. item.quickToolsCollectionCellItem(type: DataNavigationViewButtonActionType(rawValue: index) ?? .Batch)
  320. item.refreshHoverStatus(!isCustomize, !isCustomize)
  321. return item
  322. }
  323. return NSCollectionViewItem.init()
  324. }
  325. }
  326. // MARK: - NSCollectionViewDelegate
  327. extension KMPDFToolsViewController: NSCollectionViewDelegate {
  328. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  329. if !isCustomize {
  330. var indexs: [Int] = []
  331. if collectionView.isEqual(to: homeFastToolCollectionView) {
  332. for index in indexPaths {
  333. let i: Int = index.item
  334. let index: Int = quickToolsItemMutableArray[i]
  335. indexs.append(index)
  336. }
  337. } else if collectionView.isEqual(to: moreToolCollectionView) {
  338. for index in indexPaths {
  339. let i: Int = index.item
  340. let index: Int = moreToolsItemMutableArray[i]
  341. indexs.append(index)
  342. }
  343. }
  344. self.delete?.pdfToolsViewController!(self, didSelectItemsAt: indexs)
  345. collectionView.deselectAll([])
  346. }
  347. }
  348. func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
  349. if isCustomize {
  350. return true
  351. }
  352. return false
  353. }
  354. func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
  355. guard let model = indexPath as? IndexPath else {
  356. return nil
  357. }
  358. let pastBoard = NSPasteboardItem.init()
  359. pastBoard.setString(String(format: "%ld", model.item), forType: NSPasteboard.PasteboardType.string)
  360. return pastBoard
  361. }
  362. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) {
  363. if collectionView.isEqual(to: homeFastToolCollectionView) {
  364. homeFastToolItemsDragged = indexPaths
  365. } else if collectionView.isEqual(to: moreToolCollectionView) {
  366. moreTooltemsDragged = indexPaths
  367. }
  368. }
  369. func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  370. if collectionView.isEqual(to: homeFastToolCollectionView) {
  371. if !homeFastToolItemsDragged.isEmpty {
  372. return .move
  373. } else {
  374. return .copy
  375. }
  376. } else if collectionView.isEqual(to: moreToolCollectionView) {
  377. if !moreTooltemsDragged.isEmpty {
  378. return .move
  379. } else {
  380. return .copy
  381. }
  382. }
  383. return []
  384. }
  385. func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  386. var result: Bool = false
  387. if collectionView.isEqual(to: homeFastToolCollectionView) {
  388. if !homeFastToolItemsDragged.isEmpty {
  389. var toItemIndex = indexPath.item
  390. for fromIndexPath in homeFastToolItemsDragged.sorted() {
  391. let fromItemIndex = fromIndexPath.item
  392. if fromItemIndex > toItemIndex {
  393. homeFastToolCollectionView.animator().moveItem(at: IndexPath(item: fromItemIndex, section: indexPath.section), to: IndexPath(item: toItemIndex, section: indexPath.section))
  394. toItemIndex += 1
  395. }
  396. }
  397. var adjustedToItemIndex = indexPath.item
  398. for fromIndexPath in homeFastToolItemsDragged.sorted(by: >) {
  399. let fromItemIndex = fromIndexPath.item
  400. if fromItemIndex < adjustedToItemIndex {
  401. if adjustedToItemIndex >= quickToolsItemMutableArray.count {
  402. adjustedToItemIndex = quickToolsItemMutableArray.count - 1;
  403. }
  404. let adjustedToIndexPath = IndexPath(item: adjustedToItemIndex, section: indexPath.section)
  405. homeFastToolCollectionView.animator().moveItem(at: IndexPath(item: fromItemIndex, section: indexPath.section), to: adjustedToIndexPath)
  406. adjustedToItemIndex -= 1
  407. }
  408. }
  409. result = true
  410. } else if !moreTooltemsDragged.isEmpty {
  411. let array = Array(moreTooltemsDragged)
  412. let toItemIndex = array[0].item
  413. let insertIndex = quickToolsItemMutableArray[indexPath.item]
  414. quickToolsItemMutableArray.remove(at: indexPath.item)
  415. quickToolsItemMutableArray.insert(moreToolsItemMutableArray[toItemIndex], at: indexPath.item)
  416. moreToolsItemMutableArray.remove(at: toItemIndex)
  417. // moreToolsItemMutableArray.insert(insertIndex, at: toItemIndex)
  418. moreToolsItemMutableArray.insert(insertIndex, at: 0)
  419. result = true
  420. }
  421. } else if collectionView.isEqual(to: moreToolCollectionView) {
  422. if !moreTooltemsDragged.isEmpty {
  423. var toItemIndex = indexPath.item
  424. for fromIndexPath in moreTooltemsDragged.sorted() {
  425. let fromItemIndex = fromIndexPath.item
  426. if fromItemIndex > toItemIndex {
  427. moreToolCollectionView.animator().moveItem(at: IndexPath(item: fromItemIndex, section: indexPath.section), to: IndexPath(item: toItemIndex, section: indexPath.section))
  428. toItemIndex += 1
  429. }
  430. }
  431. var adjustedToItemIndex = indexPath.item - 1
  432. for fromIndexPath in moreTooltemsDragged.sorted(by: >) {
  433. let fromItemIndex = fromIndexPath.item
  434. if fromItemIndex < adjustedToItemIndex {
  435. if adjustedToItemIndex >= quickToolsItemMutableArray.count {
  436. adjustedToItemIndex = quickToolsItemMutableArray.count - 1
  437. }
  438. let adjustedToIndexPath = IndexPath(item: adjustedToItemIndex, section: indexPath.section)
  439. moreToolCollectionView.animator().moveItem(at: IndexPath(item: fromItemIndex, section: indexPath.section), to: adjustedToIndexPath)
  440. adjustedToItemIndex -= 1
  441. }
  442. }
  443. result = true
  444. } else if !homeFastToolItemsDragged.isEmpty {
  445. let array = Array(homeFastToolItemsDragged)
  446. let toItemIndex = array[0].item
  447. let insertIndex = moreToolsItemMutableArray[indexPath.item]
  448. moreToolsItemMutableArray.remove(at: indexPath.item)
  449. moreToolsItemMutableArray.insert(quickToolsItemMutableArray[toItemIndex], at: indexPath.item)
  450. quickToolsItemMutableArray.remove(at: toItemIndex)
  451. // quickToolsItemMutableArray.insert(insertIndex, at: toItemIndex)
  452. quickToolsItemMutableArray.insert(insertIndex, at: 0)
  453. result = true
  454. }
  455. }
  456. return result
  457. }
  458. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
  459. if collectionView.isEqual(to: homeFastToolCollectionView) {
  460. homeFastToolItemsDragged = Set<IndexPath>()
  461. } else if collectionView.isEqual(to: moreToolCollectionView) {
  462. moreTooltemsDragged = Set<IndexPath>()
  463. }
  464. homeFastToolCollectionView.reloadSections(IndexSet(integer: 0))
  465. moreToolCollectionView.reloadSections(IndexSet(integer: 0))
  466. }
  467. }
  468. // MARK: - NSCollectionViewDelegateFlowLayout
  469. extension KMPDFToolsViewController: NSCollectionViewDelegateFlowLayout {
  470. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  471. if collectionView.isEqual(to: homeFastToolCollectionView) {
  472. if isFold {
  473. return CGSize(width: collectionItemWidth, height: 68)
  474. } else {
  475. return CGSize(width: collectionItemWidth, height: 116)
  476. }
  477. } else if collectionView.isEqual(to: moreToolCollectionView) {
  478. if isFold {
  479. return CGSize(width: collectionItemWidth, height: 68)
  480. } else {
  481. return CGSize(width: collectionItemWidth, height: 116)
  482. }
  483. }
  484. return CGSize(width: collectionItemWidth, height: 116)
  485. }
  486. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  487. if collectionView.isEqual(to: homeFastToolCollectionView) {
  488. return 16
  489. } else if collectionView.isEqual(to: moreToolCollectionView) {
  490. return 16
  491. }
  492. return 16
  493. }
  494. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  495. if collectionView.isEqual(to: homeFastToolCollectionView) {
  496. return 0
  497. } else if collectionView.isEqual(to: moreToolCollectionView) {
  498. return 0
  499. }
  500. return 0
  501. }
  502. }