KMBGTemplateController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. //删除数据时调用
  10. let kBackgroundListDeleteAllNotiName = NSNotification.Name("kBackgroundListDeleteAllNotiName")
  11. let kBackgroundListDeleteNotiName = NSNotification.Name("kBackgroundListDeleteNotiName")
  12. @objc protocol KMBGTemplateControllerDelegate: AnyObject {
  13. @objc optional func templateControllerDidAddData(_ controller: KMBGTemplateController)
  14. @objc optional func templateControllerDidEditData(_ controller: KMBGTemplateController, _ data: KMBackgroundModel)
  15. //选中内容切换
  16. @objc optional func templateControllerDidSelectedChanged(_ controller: KMBGTemplateController)
  17. }
  18. class KMBGTemplateController: NSViewController {
  19. @IBOutlet var contendView: NSView!
  20. @IBOutlet var titleLabel: NSTextField!
  21. @IBOutlet var addButton: ComponentButton!
  22. @IBOutlet var emptyView: ComponentEmpty!
  23. @IBOutlet var scrollView: NSScrollView!
  24. @IBOutlet var collectionView: NSCollectionView!
  25. var groupView: ComponentGroup?
  26. var editCell: KMBGTemplateItem?
  27. weak open var delegate: KMBGTemplateControllerDelegate?
  28. var selectedBackground: KMBackgroundModel?
  29. deinit {
  30. NotificationCenter.default.removeObserver(self)
  31. }
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. // Do view setup here.
  35. configUI()
  36. setupProperty()
  37. reloadData()
  38. NotificationCenter.default.addObserver(self, selector: #selector(deleteItemNoti), name: kBackgroundListDeleteNotiName, object: nil)
  39. NotificationCenter.default.addObserver(self, selector: #selector(deleteAllItemsNoti), name: kBackgroundListDeleteAllNotiName, object: nil)
  40. }
  41. func setupProperty() {
  42. addButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "watermark_template_add"), keepPressState: false)
  43. addButton.setTarget(self, action: #selector(buttonClicked(_:)))
  44. emptyView.properties = ComponentEmptyProperty(emptyType: .noWatermark, text: KMLocalizedString("No Template"), subText: KMLocalizedString("Here is the description."))
  45. }
  46. func configUI() {
  47. view.wantsLayer = true
  48. view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  49. titleLabel.stringValue = KMLocalizedString("Templates")
  50. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  51. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  52. collectionView.backgroundColors = [NSColor.clear]
  53. collectionView.wantsLayer = true
  54. collectionView.layer?.backgroundColor = NSColor.clear.cgColor
  55. collectionView.delegate = self
  56. collectionView.dataSource = self
  57. collectionView.allowsEmptySelection = false
  58. collectionView.allowsMultipleSelection = false
  59. collectionView.register(KMBGTemplateItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBGTemplateItem"))
  60. }
  61. //刷新所有数据
  62. public func reloadData() {
  63. if KMBackgroundManager.defaultManager.datas.count == 0 {
  64. collectionView.isHidden = true
  65. emptyView.isHidden = false
  66. } else {
  67. emptyView.isHidden = true
  68. collectionView.isHidden = false
  69. collectionView.reloadData()
  70. updateSelectedState()
  71. }
  72. }
  73. func updateSelectedState() {
  74. if let selData = selectedBackground {
  75. if let index = KMBackgroundManager.defaultManager.datas.firstIndex(of: selData) {
  76. let indexpath = IndexPath(item: index, section: 0)
  77. var set = Set<IndexPath>()
  78. set.insert(indexpath)
  79. collectionView.selectItems(at: set, scrollPosition: .top)
  80. }
  81. } else {
  82. collectionView.deselectAll(nil)
  83. }
  84. }
  85. private func collectionViewSelectedChanged() {
  86. let indexs = collectionView.selectionIndexPaths
  87. if indexs.count > 0 {
  88. for index in indexs {
  89. let data = KMBackgroundManager.defaultManager.datas[index.item]
  90. selectedBackground = data
  91. }
  92. } else {
  93. selectedBackground = nil
  94. }
  95. delegate?.templateControllerDidSelectedChanged?(self)
  96. }
  97. func showGroupView(_ items: [String], _ point: CGPoint) {
  98. var viewHeight: CGFloat = 8
  99. var menuItemArr: [ComponentMenuitemProperty] = []
  100. for i in items {
  101. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  102. itemSelected: false,
  103. isDisabled: false,
  104. keyEquivalent: nil,
  105. text: KMLocalizedString(i),
  106. identifier: i)
  107. menuItemArr.append(properties_Menuitem)
  108. viewHeight += 36
  109. }
  110. if groupView == nil {
  111. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  112. }
  113. groupView?.groupDelegate = self
  114. var viewPoint = point
  115. viewPoint.y -= viewHeight
  116. groupView?.frame = CGRectMake(310, 0, 200, viewHeight)
  117. groupView?.updateGroupInfo(menuItemArr)
  118. groupView?.showWithPoint(viewPoint, relativeTo: nil)
  119. }
  120. //MARK: - action
  121. @objc func buttonClicked(_ sender: ComponentButton) {
  122. if sender == addButton {
  123. if selectedBackground != nil {
  124. selectedBackground = nil
  125. reloadData()
  126. delegate?.templateControllerDidSelectedChanged?(self)
  127. }
  128. delegate?.templateControllerDidAddData?(self)
  129. }
  130. }
  131. //MARK: - Noti
  132. @objc func deleteItemNoti(_ noti: Notification) {
  133. guard let notiTag = noti.object as? String else {
  134. return
  135. }
  136. if notiTag == self.selectedBackground?.backgroundID {
  137. self.selectedBackground = nil
  138. self.delegate?.templateControllerDidSelectedChanged?(self)
  139. }
  140. self.reloadData()
  141. }
  142. @objc func deleteAllItemsNoti() {
  143. if let _ = self.selectedBackground {
  144. self.selectedBackground = nil
  145. self.delegate?.templateControllerDidSelectedChanged?(self)
  146. }
  147. self.reloadData()
  148. }
  149. override func rightMouseUp(with event: NSEvent) {
  150. super.rightMouseUp(with: event)
  151. var items: [String] = []
  152. var point = self.view.convert(event.locationInWindow, from: nil)
  153. let collectionPoint = self.view.convert(point, to: collectionView)
  154. self.editCell = nil
  155. if let indexPath = self.collectionView.indexPathForItem(at: collectionPoint) {
  156. let cellView = collectionView.item(at: indexPath) as? KMBGTemplateItem
  157. self.editCell = cellView
  158. items.append(KMLocalizedString("Rename"))
  159. items.append(KMLocalizedString("Edit"))
  160. items.append(KMLocalizedString("Delete"))
  161. items.append(KMLocalizedString("Remove All"))
  162. self.showGroupView(items, event.locationInWindow)
  163. }
  164. }
  165. func showRenameWindow() {
  166. let saveWindow: KMWatermarkSaveWindow = KMWatermarkSaveWindow(windowNibName: "KMWatermarkSaveWindow")
  167. if let item = self.editCell, let name = item.backgroundModel?.name {
  168. saveWindow.nameValue = name
  169. } else {
  170. saveWindow.nameValue = ""
  171. }
  172. saveWindow.saveHandler = {[weak self] string in
  173. guard let weakSelf = self else { return }
  174. DispatchQueue.main.async {
  175. weakSelf.editCell?.backgroundModel?.name = string ?? ""
  176. if let model = weakSelf.editCell?.backgroundModel, KMBackgroundManager.defaultManager.updateTemplate(model: model) {
  177. weakSelf.reloadData()
  178. }
  179. }
  180. }
  181. saveWindow.own_beginSheetModal(for: view.window) { string in
  182. }
  183. saveWindow.titleLabel.stringValue = KMLocalizedString("Rename")
  184. }
  185. }
  186. //MARK: - NSCollectionViewDelegate, NSCollectionViewDataSource
  187. extension KMBGTemplateController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  188. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  189. return KMBackgroundManager.defaultManager.datas.count
  190. }
  191. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  192. if indexPath.item >= KMBackgroundManager.defaultManager.datas.count {
  193. return NSCollectionViewItem()
  194. }
  195. let item: KMBGTemplateItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBGTemplateItem"), for: indexPath) as! KMBGTemplateItem
  196. item.delegate = self
  197. item.backgroundModel = KMBackgroundManager.defaultManager.datas[indexPath.item]
  198. if let selData = selectedBackground {
  199. if item.backgroundModel == selData {
  200. item.isSelected = true
  201. }
  202. }
  203. item.reloadData()
  204. return item
  205. }
  206. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  207. return CGSize(width: 168, height: 237)
  208. }
  209. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  210. return 8
  211. }
  212. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  213. return 8
  214. }
  215. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  216. return NSEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
  217. }
  218. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  219. collectionViewSelectedChanged()
  220. }
  221. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  222. collectionViewSelectedChanged()
  223. }
  224. }
  225. //MARK: - kmNWatermarkTemplateItemDelegate
  226. extension KMBGTemplateController: KMBGTemplateItemDelegate {
  227. func kmNBGTemplateItemDidEdit(_ view: KMBGTemplateItem) {
  228. if let data = view.backgroundModel {
  229. delegate?.templateControllerDidEditData?(self, data)
  230. }
  231. }
  232. func kmNBGTemplateItemDidDelete(_ view: KMBGTemplateItem) {
  233. if let data = view.backgroundModel {
  234. let alert = NSAlert()
  235. alert.alertStyle = .critical
  236. alert.messageText = String(format: KMLocalizedString("Are you sure you want to remove the background?"))
  237. alert.addButton(withTitle: KMLocalizedString("Delete"))
  238. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  239. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  240. if response == .alertFirstButtonReturn {
  241. let _ = KMBackgroundManager.defaultManager.deleteTemplate(model: data)
  242. NotificationCenter.default.post(name: kBackgroundListDeleteNotiName, object: data.backgroundID)
  243. if data == self.selectedBackground {
  244. self.selectedBackground = nil
  245. self.delegate?.templateControllerDidSelectedChanged?(self)
  246. }
  247. self.reloadData()
  248. }
  249. }
  250. }
  251. }
  252. func kmNBGTemplateItemDidDeleteAll() {
  253. let alert = NSAlert()
  254. alert.alertStyle = .critical
  255. alert.messageText = String(format: KMLocalizedString("Are you sure you want to remove the all backgrounds?"))
  256. alert.addButton(withTitle: KMLocalizedString("Delete"))
  257. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  258. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  259. if response == .alertFirstButtonReturn {
  260. let _ = KMBackgroundManager.defaultManager.deleteAllTemplates()
  261. if let _ = self.selectedBackground {
  262. self.selectedBackground = nil
  263. self.delegate?.templateControllerDidSelectedChanged?(self)
  264. }
  265. self.reloadData()
  266. NotificationCenter.default.post(name: kBackgroundListDeleteAllNotiName, object: nil)
  267. }
  268. }
  269. }
  270. // func kmNBGTemplateItemDidUpdateSelectedState(_ view: KMBGTemplateItem) {
  271. // if view.itemSelected {
  272. // selectedBackground = view.backgroundModel
  273. // } else {
  274. // selectedBackground = nil
  275. // }
  276. //
  277. // collectionViewSelectedChanged()
  278. // }
  279. }
  280. //MARK: - ComponentGroupDelegate
  281. extension KMBGTemplateController: ComponentGroupDelegate {
  282. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  283. if menuItemProperty?.identifier == KMLocalizedString("Rename") {
  284. self.showRenameWindow()
  285. } else if menuItemProperty?.identifier == KMLocalizedString("Edit") {
  286. if let cell = self.editCell {
  287. self.kmNBGTemplateItemDidEdit(cell)
  288. }
  289. } else if menuItemProperty?.identifier == KMLocalizedString("Delete") {
  290. if let cell = self.editCell {
  291. self.kmNBGTemplateItemDidDelete(cell)
  292. }
  293. } else if menuItemProperty?.identifier == KMLocalizedString("Remove All") {
  294. self.kmNBGTemplateItemDidDeleteAll()
  295. }
  296. }
  297. }