KMHFTemplateController.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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: KMHeaderFooterModel)
  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: KMHeaderFooterModel?
  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 = false
  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. } else {
  71. collectionView.deselectAll(nil)
  72. }
  73. }
  74. private func collectionViewSelectedChanged() {
  75. let indexs = collectionView.selectionIndexPaths
  76. if indexs.count > 0 {
  77. for index in indexs {
  78. let data = KMHeaderFooterManager.defaultManager.headFooterObjects[index.item]
  79. selectedModel = data
  80. }
  81. } else {
  82. selectedModel = nil
  83. }
  84. DispatchQueue.main.async {
  85. self.delegate?.templateControllerDidSelectedChanged?(self)
  86. }
  87. }
  88. //MARK: - action
  89. @objc func buttonClicked(_ sender: ComponentButton) {
  90. if sender == addButton {
  91. if selectedModel != nil {
  92. selectedModel = nil
  93. delegate?.templateControllerDidSelectedChanged?(self)
  94. }
  95. reloadData()
  96. delegate?.templateControllerDidAddData?(self)
  97. }
  98. }
  99. }
  100. //MARK: - NSCollectionViewDelegate, NSCollectionViewDataSource
  101. extension KMHFTemplateController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  102. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  103. return KMHeaderFooterManager.defaultManager.headFooterObjects.count
  104. }
  105. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  106. if indexPath.item >= KMHeaderFooterManager.defaultManager.headFooterObjects.count {
  107. return NSCollectionViewItem()
  108. }
  109. let item: KMHeaderTemplateItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHeaderTemplateItem"), for: indexPath) as! KMHeaderTemplateItem
  110. item.delegate = self
  111. item.dataModel = KMHeaderFooterManager.defaultManager.headFooterObjects[indexPath.item]
  112. item.reloadData()
  113. return item
  114. }
  115. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  116. var itemHeight = 56
  117. let dataModel = KMHeaderFooterManager.defaultManager.headFooterObjects[indexPath.item]
  118. if dataModel.topLeftString.count > 0 {
  119. itemHeight += 24
  120. itemHeight += 8
  121. }
  122. if dataModel.topCenterString.count > 0 {
  123. itemHeight += 24
  124. itemHeight += 8
  125. }
  126. if dataModel.topRightString.count > 0 {
  127. itemHeight += 24
  128. itemHeight += 8
  129. }
  130. if dataModel.bottomLeftString.count > 0 {
  131. itemHeight += 24
  132. itemHeight += 8
  133. }
  134. if dataModel.bottomCenterString.count > 0 {
  135. itemHeight += 24
  136. itemHeight += 8
  137. }
  138. if dataModel.bottomRightString.count > 0 {
  139. itemHeight += 24
  140. itemHeight += 8
  141. }
  142. itemHeight -= 8
  143. return CGSize(width: 232, height: itemHeight)
  144. }
  145. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  146. return 8
  147. }
  148. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  149. return 8
  150. }
  151. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  152. return NSEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
  153. }
  154. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  155. collectionViewSelectedChanged()
  156. }
  157. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  158. collectionViewSelectedChanged()
  159. }
  160. }
  161. //MARK: - kmNWatermarkTemplateItemDelegate
  162. extension KMHFTemplateController: KMHeaderTemplateItemDelegate {
  163. func kmHeaderTemplateItemDidEdit(_ view: KMHeaderTemplateItem) {
  164. if let data = view.dataModel {
  165. delegate?.templateControllerDidEditData?(self, data)
  166. }
  167. }
  168. func kmHeaderTemplateItemDidDelete(_ view: KMHeaderTemplateItem) {
  169. if let data = view.dataModel {
  170. let alert = NSAlert()
  171. alert.alertStyle = .critical
  172. alert.messageText = String(format: KMLocalizedString("Are you sure you want to remove the background?"))
  173. alert.addButton(withTitle: KMLocalizedString("Delete"))
  174. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  175. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  176. if response == .alertFirstButtonReturn {
  177. let _ = KMHeaderFooterManager.defaultManager.removeHeaderFooter(data)
  178. if data == self.selectedModel {
  179. self.selectedModel = nil
  180. self.delegate?.templateControllerDidSelectedChanged?(self)
  181. }
  182. self.reloadData()
  183. }
  184. }
  185. }
  186. }
  187. }