KMBatchOperateRemoveHeaderFooterViewController.swift 5.5 KB

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