KMBatesTemplateController.swift 8.6 KB

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