KMBatchOperateRemoveHeaderFooterViewController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // KMBatchOperateRemoveHeaderFooterViewController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/11/6.
  6. //
  7. import Cocoa
  8. class KMBatchOperateRemoveHeaderFooterViewController: KMBatchOperateBaseViewController {
  9. var isBates = false
  10. @IBOutlet var actionButton: NSButton!
  11. private var _haveFiles = false
  12. deinit {
  13. NotificationCenter.default.removeObserver(self)
  14. KMPrint("KMBatchOperateRemoveHeaderFooterViewController deinit.")
  15. }
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self._localizedlanguage()
  19. self._configuiUI()
  20. DistributedNotificationCenter.default.addObserver(self, selector: #selector(_themeChanged), name: Notification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  21. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil)
  22. }
  23. override var interfaceStatus: KMBatchOperateInterfaceStatus? {
  24. set {
  25. if (newValue == .PrepareProcess) {
  26. super.interfaceStatus = newValue
  27. DispatchQueue.main.asyncAfter(deadline: .now()+0.4) {
  28. var files: [URL] = []
  29. for url in self.successFilePathURLArray ?? [] {
  30. if (FileManager.default.fileExists(atPath: url.path)) {
  31. files.append(url)
  32. }
  33. }
  34. if(files.count > 0) {
  35. let workspace = NSWorkspace.shared
  36. workspace.activateFileViewerSelecting(files)
  37. }
  38. }
  39. self.actionButton.tag = 1
  40. if (self.isBates) {
  41. self.actionButton.title = KMLocalizedString("Remove Bates Numbers", nil)
  42. } else {
  43. self.actionButton.title = KMLocalizedString("Remove Header & Footer", nil)
  44. }
  45. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  46. } else {
  47. self.actionButton.tag = 0
  48. self.actionButton.title = KMLocalizedString("Cancel", nil)
  49. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  50. }
  51. }
  52. get {
  53. return super.interfaceStatus
  54. }
  55. }
  56. @IBAction func buttonClicked_Action(_ sender: NSButton) {
  57. if (!self._haveFiles) {
  58. return
  59. }
  60. if (sender.tag == 1) {
  61. self.beginBatchOperation()
  62. } else if (sender.tag == 0) {
  63. self.cancelBatchOperation()
  64. }
  65. }
  66. @objc func batchFilesCountNotification(notification: Notification) {
  67. let arr: Array? = notification.object as? [KMBatchOperateFile]
  68. self.files? = arr ?? []
  69. if arr?.count ?? 0 > 0{
  70. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  71. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  72. }else {
  73. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  74. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  75. }
  76. }
  77. }
  78. // MARK: - Private Methods
  79. extension KMBatchOperateRemoveHeaderFooterViewController {
  80. private func _localizedlanguage() {
  81. if (self.isBates) {
  82. self.actionButton.title = KMLocalizedString("Remove Bates Numbers", nil)
  83. } else {
  84. self.actionButton.title = KMLocalizedString("Remove Header & Footer", nil)
  85. }
  86. }
  87. private func _configuiUI() {
  88. self.view.wantsLayer = true
  89. self.actionButton.wantsLayer = true
  90. if let cnt = self.files?.count, cnt > 0 {
  91. self._haveFiles = true
  92. } else {
  93. self._haveFiles = false
  94. }
  95. self.actionButton.layer?.cornerRadius = 1.0
  96. self._updateViewColor()
  97. NotificationCenter.default.addObserver(self, selector: #selector(_batchFilesCountNotification), name: Notification.Name("KMBatchFilesCountNotification"), object: nil)
  98. }
  99. private func _updateViewColor() {
  100. if(KMAppearance.isDarkMode()){
  101. self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  102. } else {
  103. self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  104. }
  105. self._updateBtnColor()
  106. }
  107. @objc private func _batchFilesCountNotification(_ notification: Notification) {
  108. let files = notification.object as? NSArray
  109. if let cnt = files?.count, cnt > 0 {
  110. self._haveFiles = true
  111. } else {
  112. self._haveFiles = false
  113. }
  114. self._updateBtnColor()
  115. }
  116. private func _updateBtnColor() {
  117. if(KMAppearance.isDarkMode()){
  118. if (self._haveFiles) {
  119. self.actionButton.layer?.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 1).cgColor
  120. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  121. } else {
  122. self.actionButton.layer?.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 0.6).cgColor
  123. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  124. }
  125. }else {
  126. if (self._haveFiles) {
  127. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 1).cgColor
  128. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  129. } else {
  130. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 0.6).cgColor
  131. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  132. }
  133. }
  134. }
  135. @objc private func _themeChanged(_ notification: NSNotification) {
  136. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  137. self._updateViewColor()
  138. }
  139. }
  140. }
  141. //extension KMBatchOperateRemoveHeaderFooterViewController: KMBatchOperateProtocol {
  142. //}