KMBatchOperateRemoveHeaderFooterViewController.swift 6.3 KB

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