KMBackgroundPropertyController.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // KMBackgroundPropertyController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/23.
  6. //
  7. import Cocoa
  8. protocol KMBackgroundPropertyDelegate: KMWatermarkAdjectivePropertyDelegate {
  9. }
  10. typealias KMBackgroundPropertyTemplateDidSelected = (_ model: KMBackgroundModel,_ isSelected: Bool)->()
  11. typealias KMBackgroundPropertyTemplateMenuItemClick = (_ itemIndex: Int, _ model: KMBackgroundModel)->()
  12. class KMBackgroundPropertyController: KMWatermarkAdjectivePropertyBaseController {
  13. var templateDidSelected: KMBackgroundPropertyTemplateDidSelected!
  14. var menuItemClick: KMBackgroundPropertyTemplateMenuItemClick!
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. self.showBack(show: false)
  18. self.titleLabel.stringValue = NSLocalizedString("Background", comment: "")
  19. self.segementControl.items = [NSLocalizedString("Color", comment: ""),NSLocalizedString("File", comment: "")]
  20. self.segementControl.itemClick = {
  21. [self] (index: Int) in
  22. self.tabView.selectTabViewItem(at: index)
  23. }
  24. let item = NSTabViewItem(identifier: "tabID")
  25. item.label = "tab1";
  26. let controller = KMBackgroundListController()
  27. item.viewController = controller
  28. controller.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new background.", comment: "")
  29. controller.delegate = self
  30. let item2 = NSTabViewItem(identifier: "tablID2")
  31. item2.label = "tab2";
  32. let controller2 = KMBackgroundListController()
  33. item2.viewController = controller2
  34. controller2.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new background.", comment: "")
  35. controller2.delegate = self
  36. self.tabView.addTabViewItem(item)
  37. self.tabView.addTabViewItem(item2)
  38. childViewController.append(controller)
  39. childViewController.append(controller2)
  40. reloadDatas()
  41. }
  42. func findTemplateModel(in controller: KMBackgroundListController, index: Int) -> KMBackgroundModel? {
  43. if (index >= controller.datas.count) {
  44. return nil
  45. }
  46. return (controller.datas[index] as! KMBackgroundModel)
  47. }
  48. func deleteTemplateModel(_ templateModel: KMBackgroundModel) -> Bool {
  49. return KMBackgroundManager.defaultManager.deleteTemplate(model: templateModel)
  50. }
  51. func deleteTemplateModel(in controller: KMBackgroundListController, index: Int) {
  52. if (index >= controller.datas.count) {
  53. return
  54. }
  55. let templateModel: KMBackgroundModel = controller.datas[index] as! KMBackgroundModel
  56. if (deleteTemplateModel(templateModel)) { /// 删除成功,刷新数据
  57. reloadDatas()
  58. }
  59. }
  60. func deleteAllTemplateModel(in controller: KMBackgroundListController) {
  61. if (controller.isEqual(to: childViewController.first)) {
  62. if (KMBackgroundManager.defaultManager.deleteAllColorTemplates()) {
  63. reloadDatas()
  64. }
  65. } else {
  66. if (KMBackgroundManager.defaultManager.deleteAllFileTemplates()) {
  67. reloadDatas()
  68. }
  69. }
  70. }
  71. func reloadDatas () {
  72. var colorModels: Array<KMBackgroundModel> = []
  73. var fileModels: Array<KMBackgroundModel> = []
  74. for model in KMBackgroundManager.defaultManager.datas {
  75. if (model.type == .file) { /// 文件
  76. fileModels.append(model)
  77. } else { /// 颜色
  78. colorModels.append(model)
  79. }
  80. }
  81. var count: Int = 0
  82. for controller in childViewController {
  83. let listController: KMBackgroundListController = controller as! KMBackgroundListController
  84. if (count == 0) {
  85. listController.initDatas(datas: colorModels)
  86. } else {
  87. listController.initDatas(datas: fileModels)
  88. }
  89. count += 1
  90. }
  91. }
  92. }
  93. extension KMBackgroundPropertyController: KMWatermarkAdjectiveListControllerDelegate {
  94. func controller(controller: KMWatermarkAdjectiveListController, shouldSelectAtRow: Int) -> Bool {
  95. return true
  96. }
  97. func controller(controller: KMWatermarkAdjectiveListController, didSelectAtRow: Int) {
  98. guard let callback = templateDidSelected else {
  99. return
  100. }
  101. let isSelected = controller.selectedTemplateID.isEmpty == false
  102. callback(findTemplateModel(in: controller as! KMBackgroundListController, index: didSelectAtRow)!, isSelected)
  103. }
  104. func controller(controller: KMWatermarkAdjectiveListController, didRemoveAtRow: Int) {
  105. deleteTemplateModel(in: controller as! KMBackgroundListController, index: didRemoveAtRow)
  106. }
  107. func controller(controller: KMWatermarkAdjectiveListController, didEditAtRow: Int) {
  108. guard let callback = menuItemClick else {
  109. return
  110. }
  111. callback(2, findTemplateModel(in: controller as! KMBackgroundListController, index: didEditAtRow)!)
  112. }
  113. func controller(controller: KMWatermarkAdjectiveListController, item: NSMenuItem, itemIndex: Int) {
  114. if (itemIndex == 1) { /// 删除
  115. deleteTemplateModel(in: controller as! KMBackgroundListController, index: item.tag)
  116. return
  117. }
  118. if (itemIndex == 2) { /// 删除全部
  119. let alert = NSAlert()
  120. alert.messageText = "确认删除全部模板吗?"
  121. alert.addButton(withTitle: "删除")
  122. alert.addButton(withTitle: "取消")
  123. let result = alert.runModal()
  124. if (result == .alertSecondButtonReturn) { /// 取消
  125. return
  126. }
  127. deleteAllTemplateModel(in: controller as! KMBackgroundListController)
  128. return
  129. }
  130. /// 编辑
  131. guard let callback = menuItemClick else {
  132. return
  133. }
  134. callback(2, findTemplateModel(in: controller as! KMBackgroundListController, index: item.tag)!)
  135. }
  136. }