KMBGTemplateController.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // KMBGTemplateController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/7.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMBGTemplateControllerDelegate: AnyObject {
  10. @objc optional func templateControllerDidAddData(_ controller: KMBGTemplateController)
  11. @objc optional func templateControllerDidEditData(_ controller: KMBGTemplateController, _ data: KMBackgroundModel)
  12. //选中内容切换
  13. @objc optional func templateControllerDidSelectedChanged(_ controller: KMBGTemplateController)
  14. }
  15. class KMBGTemplateController: 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: KMBGTemplateControllerDelegate?
  23. var selectedBackground: KMBackgroundModel?
  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(KMBGTemplateItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBGTemplateItem"))
  50. }
  51. //刷新所有数据
  52. public func reloadData() {
  53. if KMBackgroundManager.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. func updateSelectedState() {
  64. if let selData = selectedBackground {
  65. if let index = KMBackgroundManager.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 = KMBackgroundManager.defaultManager.datas[index.item]
  78. selectedBackground = data
  79. }
  80. } else {
  81. selectedBackground = nil
  82. }
  83. delegate?.templateControllerDidSelectedChanged?(self)
  84. }
  85. //MARK: - action
  86. @objc func buttonClicked(_ sender: ComponentButton) {
  87. if sender == addButton {
  88. selectedBackground = nil
  89. reloadData()
  90. delegate?.templateControllerDidSelectedChanged?(self)
  91. delegate?.templateControllerDidAddData?(self)
  92. }
  93. }
  94. }
  95. //MARK: - NSCollectionViewDelegate, NSCollectionViewDataSource
  96. extension KMBGTemplateController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  97. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  98. return KMBackgroundManager.defaultManager.datas.count
  99. }
  100. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  101. if indexPath.item >= KMBackgroundManager.defaultManager.datas.count {
  102. return NSCollectionViewItem()
  103. }
  104. let item: KMBGTemplateItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBGTemplateItem"), for: indexPath) as! KMBGTemplateItem
  105. item.delegate = self
  106. item.backgroundModel = KMBackgroundManager.defaultManager.datas[indexPath.item]
  107. if let selData = selectedBackground {
  108. if item.backgroundModel == selData {
  109. item.isSelected = true
  110. }
  111. }
  112. item.reloadData()
  113. return item
  114. }
  115. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  116. return CGSize(width: 168, height: 237)
  117. }
  118. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  119. return 8
  120. }
  121. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  122. return 8
  123. }
  124. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  125. return NSEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
  126. }
  127. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  128. collectionViewSelectedChanged()
  129. }
  130. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  131. collectionViewSelectedChanged()
  132. }
  133. }
  134. //MARK: - kmNWatermarkTemplateItemDelegate
  135. extension KMBGTemplateController: KMBGTemplateItemDelegate {
  136. func kmNBGTemplateItemDidEdit(_ view: KMBGTemplateItem) {
  137. if let data = view.backgroundModel {
  138. delegate?.templateControllerDidEditData?(self, data)
  139. }
  140. }
  141. func kmNBGTemplateItemDidDelete(_ view: KMBGTemplateItem) {
  142. if let data = view.backgroundModel {
  143. let alert = NSAlert()
  144. alert.alertStyle = .critical
  145. alert.messageText = String(format: KMLocalizedString("Are you sure you want to remove the background?"))
  146. alert.addButton(withTitle: KMLocalizedString("Delete"))
  147. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  148. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  149. if response == .alertFirstButtonReturn {
  150. let _ = KMBackgroundManager.defaultManager.deleteTemplate(model: data)
  151. if data == self.selectedBackground {
  152. self.selectedBackground = nil
  153. self.delegate?.templateControllerDidSelectedChanged?(self)
  154. }
  155. self.reloadData()
  156. }
  157. }
  158. }
  159. }
  160. }