KMPDFToolsViewController.swift 27 KB

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