KMBatesTemplateController.swift 8.6 KB

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