KMHFTemplateController.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // KMHFTemplateController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMHFTemplateControllerDelegate: AnyObject {
  10. @objc optional func templateControllerDidAddData(_ controller: KMHFTemplateController)
  11. @objc optional func templateControllerDidEditData(_ controller: KMHFTemplateController, _ data: KMHeaderFooterObject)
  12. //选中内容切换
  13. @objc optional func templateControllerDidSelectedChanged(_ controller: KMHFTemplateController)
  14. }
  15. class KMHFTemplateController: NSViewController {
  16. @IBOutlet var contendView: NSView!
  17. @IBOutlet var titleLabel: NSTextField!
  18. @IBOutlet var addButton: ComponentButton!
  19. @IBOutlet var emptyView: ComponentEmpty!
  20. @IBOutlet var scrollView: NSScrollView!
  21. @IBOutlet var collectionView: NSCollectionView!
  22. weak open var delegate: KMHFTemplateControllerDelegate?
  23. var selectedModel: KMHeaderFooterObject?
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. // Do view setup here.
  27. configUI()
  28. setupProperty()
  29. }
  30. func setupProperty() {
  31. addButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "watermark_template_add"), keepPressState: false)
  32. addButton.setTarget(self, action: #selector(buttonClicked(_:)))
  33. emptyView.properties = ComponentEmptyProperty(emptyType: .noWatermark, text: KMLocalizedString("No Template"), subText: KMLocalizedString("Here is the description."))
  34. }
  35. func configUI() {
  36. view.wantsLayer = true
  37. view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  38. titleLabel.stringValue = KMLocalizedString("Templates")
  39. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  40. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  41. collectionView.backgroundColors = [NSColor.clear]
  42. collectionView.wantsLayer = true
  43. collectionView.layer?.backgroundColor = NSColor.clear.cgColor
  44. collectionView.delegate = self
  45. collectionView.dataSource = self
  46. collectionView.allowsEmptySelection = true
  47. collectionView.allowsMultipleSelection = false
  48. collectionView.register(KMHeaderTemplateItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHeaderTemplateItem"))
  49. }
  50. //刷新所有数据
  51. public func reloadData() {
  52. if KMHeaderFooterManager.defaultManager.headFooterObjects.count == 0 {
  53. collectionView.isHidden = true
  54. emptyView.isHidden = false
  55. } else {
  56. emptyView.isHidden = true
  57. collectionView.isHidden = false
  58. collectionView.reloadData()
  59. updateSelectedState()
  60. }
  61. }
  62. public func updateSelectedState() {
  63. if let selData = selectedModel {
  64. if let index = KMHeaderFooterManager.defaultManager.headFooterObjects.firstIndex(of: selData) {
  65. let indexpath = IndexPath(item: index, section: 0)
  66. var set = Set<IndexPath>()
  67. set.insert(indexpath)
  68. collectionView.selectItems(at: set, scrollPosition: .top)
  69. }
  70. }
  71. }
  72. private func collectionViewSelectedChanged() {
  73. let indexs = collectionView.selectionIndexPaths
  74. if indexs.count > 0 {
  75. for index in indexs {
  76. let data = KMHeaderFooterManager.defaultManager.headFooterObjects[index.item]
  77. selectedModel = data
  78. }
  79. } else {
  80. selectedModel = nil
  81. }
  82. DispatchQueue.main.async {
  83. self.delegate?.templateControllerDidSelectedChanged?(self)
  84. }
  85. }
  86. //MARK: - action
  87. @objc func buttonClicked(_ sender: ComponentButton) {
  88. if sender == addButton {
  89. if selectedModel != nil {
  90. selectedModel = nil
  91. delegate?.templateControllerDidSelectedChanged?(self)
  92. }
  93. reloadData()
  94. delegate?.templateControllerDidAddData?(self)
  95. }
  96. }
  97. }
  98. //MARK: - NSCollectionViewDelegate, NSCollectionViewDataSource
  99. extension KMHFTemplateController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  100. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  101. return KMHeaderFooterManager.defaultManager.headFooterObjects.count
  102. }
  103. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  104. if indexPath.item >= KMHeaderFooterManager.defaultManager.headFooterObjects.count {
  105. return NSCollectionViewItem()
  106. }
  107. let item: KMHeaderTemplateItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHeaderTemplateItem"), for: indexPath) as! KMHeaderTemplateItem
  108. item.delegate = self
  109. item.dataModel = KMHeaderFooterManager.defaultManager.headFooterObjects[indexPath.item]
  110. item.reloadData()
  111. return item
  112. }
  113. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  114. var itemHeight = 56
  115. let dataModel = KMHeaderFooterManager.defaultManager.headFooterObjects[indexPath.item]
  116. if dataModel.topLeftString.count > 0 {
  117. itemHeight += 24
  118. itemHeight += 8
  119. }
  120. if dataModel.topCenterString.count > 0 {
  121. itemHeight += 24
  122. itemHeight += 8
  123. }
  124. if dataModel.topRightString.count > 0 {
  125. itemHeight += 24
  126. itemHeight += 8
  127. }
  128. if dataModel.bottomLeftString.count > 0 {
  129. itemHeight += 24
  130. itemHeight += 8
  131. }
  132. if dataModel.bottomCenterString.count > 0 {
  133. itemHeight += 24
  134. itemHeight += 8
  135. }
  136. if dataModel.bottomRightString.count > 0 {
  137. itemHeight += 24
  138. itemHeight += 8
  139. }
  140. itemHeight -= 8
  141. return CGSize(width: 232, height: itemHeight)
  142. }
  143. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  144. return 8
  145. }
  146. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  147. return 8
  148. }
  149. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  150. return NSEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
  151. }
  152. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  153. collectionViewSelectedChanged()
  154. }
  155. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  156. collectionViewSelectedChanged()
  157. }
  158. }
  159. //MARK: - kmNWatermarkTemplateItemDelegate
  160. extension KMHFTemplateController: KMHeaderTemplateItemDelegate {
  161. func kmHeaderTemplateItemDidEdit(_ view: KMHeaderTemplateItem) {
  162. if let data = view.dataModel {
  163. delegate?.templateControllerDidEditData?(self, data)
  164. }
  165. }
  166. func kmHeaderTemplateItemDidDelete(_ view: KMHeaderTemplateItem) {
  167. if let data = view.dataModel {
  168. let alert = NSAlert()
  169. alert.alertStyle = .critical
  170. alert.messageText = String(format: KMLocalizedString("Are you sure you want to remove the background?"))
  171. alert.addButton(withTitle: KMLocalizedString("Delete"))
  172. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  173. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  174. if response == .alertFirstButtonReturn {
  175. let _ = KMHeaderFooterManager.defaultManager.removeHeaderFooter(data)
  176. if data == self.selectedModel {
  177. self.selectedModel = nil
  178. self.delegate?.templateControllerDidSelectedChanged?(self)
  179. }
  180. self.reloadData()
  181. }
  182. }
  183. }
  184. }
  185. }