KMHomeFastToolViewController.swift 12 KB

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