KMHomeFastToolViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // KMHomeFastToolViewController.swift
  3. // PDF Master
  4. //
  5. // Home界面-便捷工具 模块
  6. // Created by wanjun on 2022/10/19.
  7. //
  8. import Cocoa
  9. @objc protocol KMHomeFastToolViewControllerDelegate {
  10. @objc optional func fastToolViewController(_ viewController: KMHomeFastToolViewController, foldChangeState: Bool)
  11. @objc optional func fastToolDidSelectAllTools(_ viewController: KMHomeFastToolViewController)
  12. @objc optional func fastToolViewController(_ viewController: KMHomeFastToolViewController, didSelectItemsAt indexPaths: [Int])
  13. }
  14. class KMHomeFastToolViewController: NSViewController {
  15. @IBOutlet weak var fastToolLabel: NSTextField!
  16. @IBOutlet weak var fastToolMoreBox: KMBox!
  17. @IBOutlet weak var fastToolMoreBoxHeight: NSLayoutConstraint!
  18. @IBOutlet weak var fastToolAllToolsBox: KMBox!
  19. @IBOutlet weak var fastToolAllToolsBoxHeight: NSLayoutConstraint!
  20. @IBOutlet weak var fastToolBox: NSBox!
  21. @IBOutlet weak var fastToolCollectionView: KMFastToolCollectionView!
  22. @IBOutlet weak var fastToolBoxHeightConstraint: NSLayoutConstraint!
  23. let ScrollerViewWidget: CGFloat = 15.0
  24. var collectionItemWidth: Int = 230
  25. var fastToolMoreButtonVC: KMDesignButton!
  26. var fastToolAllToolsButtonVC: KMDesignButton!
  27. open weak var delete: KMHomeFastToolViewControllerDelegate?
  28. var isFold: Bool!
  29. //快捷工具数据
  30. var quickToolsItemMutableArray: [Int] = []
  31. // MARK: init
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. // Do view setup here.
  35. fastToolMoreButtonVC = KMDesignButton.init(withType: .Image)
  36. fastToolAllToolsButtonVC = KMDesignButton.init(withType: .Text)
  37. fastToolCollectionView.register(KMFastToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFastToolCollectionViewItem"))
  38. reloadUserDefault()
  39. }
  40. override func viewDidAppear() {
  41. super.viewDidAppear()
  42. reloadUserDefault()
  43. self.view.postsBoundsChangedNotifications = true
  44. NotificationCenter.default.addObserver(self, selector: #selector(boundsDidChangeNotification), name: NSView.frameDidChangeNotification, object: self.view)
  45. reloadData()
  46. }
  47. func initializeUI() {
  48. fastToolLabel.font = NSFont(name: "SFProText-Semibold", size: 20)
  49. fastToolLabel.textColor = NSColor.init(hex: "#252629")
  50. fastToolAllToolsBox.fillColor = .clear
  51. fastToolAllToolsBox.contentView = fastToolAllToolsButtonVC.view
  52. fastToolAllToolsButtonVC.target = self
  53. fastToolAllToolsButtonVC.action = #selector(fastToolAllToolsAction(_:))
  54. fastToolAllToolsButtonVC.button(type: .Sec, size: .m, height: fastToolAllToolsBoxHeight)
  55. fastToolMoreBox.fillColor = .clear
  56. fastToolMoreBox.contentView = fastToolMoreButtonVC.view
  57. fastToolMoreButtonVC.target = self
  58. fastToolMoreButtonVC.action = #selector(fastToolMoreAction(_:))
  59. fastToolMoreButtonVC.image = NSImage(named: "icon_btn_more_false_false")!
  60. fastToolMoreButtonVC.button(type: .Sec, size: .m, height: fastToolMoreBoxHeight)
  61. }
  62. func initLocalization() {
  63. let fastToolParagraphStyle = NSMutableParagraphStyle()
  64. fastToolParagraphStyle.lineSpacing = 28.0
  65. fastToolLabel.attributedStringValue = NSAttributedString(string: NSLocalizedString("Quick Tools", comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: fastToolParagraphStyle])
  66. fastToolAllToolsButtonVC.stringValue = NSLocalizedString("Tools", comment: "")
  67. }
  68. // MARK: private
  69. func reloadData() -> Void {
  70. initializeUI()
  71. initLocalization()
  72. collectionItemWidth = Int((fastToolCollectionView.bounds.width - (20 * 3))/4)
  73. fastToolCollectionView.reloadData()
  74. fastToolAllToolsButtonVC.updateUI()
  75. fastToolMoreButtonVC.updateUI()
  76. }
  77. func refreshUI(_ isfold: Bool) {
  78. if isfold {
  79. fastToolBoxHeightConstraint.constant = 152 + ScrollerViewWidget
  80. UserDefaults.standard.setValue(true, forKey: "kHomeQucikToolsTypeKey")
  81. } else {
  82. fastToolBoxHeightConstraint.constant = 248 + ScrollerViewWidget
  83. UserDefaults.standard.setValue(false, forKey: "kHomeQucikToolsTypeKey")
  84. }
  85. UserDefaults.standard.synchronize()
  86. }
  87. func reloadUserDefault() -> Void {
  88. isFold = UserDefaults.standard.bool(forKey: "kHomeQucikToolsTypeKey")
  89. self.refreshUI(isFold)
  90. let quickToolsArray = UserDefaults.standard.array(forKey: "HomeQucikToolsItemArrayKey") as? [Int] ?? []
  91. if (quickToolsArray.count > 0) {
  92. quickToolsItemMutableArray = quickToolsArray
  93. } else {
  94. quickToolsItemMutableArray = quickToolsFullArray()
  95. UserDefaults.standard.set(quickToolsItemMutableArray, forKey: "HomeQucikToolsItemArrayKey")
  96. UserDefaults.standard.synchronize()
  97. }
  98. }
  99. func createPopoverAction(_ data: [String], view: NSView) {
  100. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(data)
  101. let createFilePopover: NSPopover = NSPopover.init()
  102. createFilePopover.contentViewController = vc
  103. createFilePopover.animates = true
  104. createFilePopover.behavior = .semitransient
  105. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  106. 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)
  107. vc.downCallback = {(downEntered: Bool, count: String) -> Void in
  108. if downEntered {
  109. self.moreToolsAction(vc)
  110. }
  111. }
  112. }
  113. func quickToolsFullArray() -> [Int] {
  114. return [DataNavigationViewButtonActionType.PDFToWord.rawValue,
  115. DataNavigationViewButtonActionType.ImageToPDF.rawValue,
  116. DataNavigationViewButtonActionType.PDFToExcel.rawValue,
  117. DataNavigationViewButtonActionType.MergePDF.rawValue,
  118. DataNavigationViewButtonActionType.PDFToPPT.rawValue,
  119. DataNavigationViewButtonActionType.Compression.rawValue,
  120. DataNavigationViewButtonActionType.PDFToImage.rawValue,
  121. DataNavigationViewButtonActionType.Security.rawValue]
  122. }
  123. // MARK: Action
  124. func moreToolsAction(_ viewController: KMHomePopViewController) {
  125. isFold = !isFold
  126. self.refreshUI(isFold)
  127. if isFold {
  128. viewController.changePopViewCellData(0, content: NSLocalizedString("Expand", comment: ""))
  129. } else {
  130. viewController.changePopViewCellData(0, content: NSLocalizedString("Collapse", comment: ""))
  131. }
  132. fastToolCollectionView.reloadData()
  133. self.delete?.fastToolViewController!(self, foldChangeState: isFold)
  134. }
  135. @IBAction func fastToolAllToolsAction(_ sender: NSButton) -> Void {
  136. self.delete?.fastToolDidSelectAllTools!(self)
  137. }
  138. @IBAction func fastToolMoreAction(_ sender: NSButton) -> Void {
  139. if isFold {
  140. self.createPopoverAction([NSLocalizedString("Expand", comment: "")], view: fastToolMoreBox)
  141. } else {
  142. self.createPopoverAction([NSLocalizedString("Collapse", comment: "")], view: fastToolMoreBox)
  143. }
  144. }
  145. // MARK: NSNotification Action
  146. @IBAction func boundsDidChangeNotification(_ notification: NSNotification) -> Void {
  147. reloadData()
  148. }
  149. }
  150. // MARK: - NSCollectionViewDataSource
  151. extension KMHomeFastToolViewController: NSCollectionViewDataSource {
  152. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  153. return quickToolsItemMutableArray.count
  154. }
  155. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  156. if collectionView.isEqual(fastToolCollectionView) {
  157. let item: KMFastToolCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFastToolCollectionViewItem"), for: indexPath) as! KMFastToolCollectionViewItem
  158. item.card(bg: "card.has-desc.bg.norm", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing")
  159. item.card(bg: "card.has-desc.bg.hov", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Hov)
  160. item.card(bg: "card.has-desc.bg.sel", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Sel)
  161. item.card(bg: "card.has-desc.bg.custom", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Custom)
  162. item.card(bg: "card.has-desc.bg.none", state: .None)
  163. item.card(bg: "card.has-desc.bg.change", titleText: "card.win-text.title", descText: "card.win-text.desc", spacing: "card.item-spacing", state: .Change)
  164. let i: Int = indexPath.item
  165. let index: Int = quickToolsItemMutableArray[i]
  166. item.quickToolsCollectionCellItem(type: DataNavigationViewButtonActionType(rawValue: index) ?? .Batch)
  167. item.fastToolItemBox.downCallback = { (downEntered: Bool, mouseBox: KMBox, event) -> Void in
  168. if downEntered {
  169. item.state = .Sel
  170. } else {
  171. item.state = .Norm
  172. }
  173. item.updateUI()
  174. }
  175. item.fastToolItemSubtitleLabel.isHidden = isFold
  176. return item
  177. }
  178. return NSCollectionViewItem.init()
  179. }
  180. }
  181. // MARK: - NSCollectionViewDelegate
  182. extension KMHomeFastToolViewController: NSCollectionViewDelegate {
  183. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  184. var indexs: [Int] = []
  185. for index in indexPaths {
  186. let i: Int = index.item
  187. let index: Int = quickToolsItemMutableArray[i]
  188. indexs.append(index)
  189. }
  190. self.delete?.fastToolViewController!(self, didSelectItemsAt: indexs)
  191. collectionView.deselectAll([])
  192. }
  193. }
  194. // MARK: - NSCollectionViewDelegateFlowLayout
  195. extension KMHomeFastToolViewController: NSCollectionViewDelegateFlowLayout {
  196. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  197. if collectionView.isEqual(to: fastToolCollectionView) {
  198. if isFold {
  199. return CGSize(width: collectionItemWidth, height: 68)
  200. } else {
  201. return CGSize(width: collectionItemWidth, height: 116)
  202. }
  203. }
  204. return CGSize(width: 230, height: 116)
  205. }
  206. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  207. return 16
  208. }
  209. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  210. return 8
  211. }
  212. // func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  213. // return NSEdgeInsets(top: 0, left: 0, bottom: 15, right: 0)
  214. // }
  215. }