KMBatchOperateRemoveWatermarkViewController.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // KMBatchOperateRemoveWatermarkViewController.swift
  3. // PDF Master
  4. //
  5. // Created by liujiajie on 2023/11/6.
  6. //
  7. import Cocoa
  8. import StoreKit
  9. class KMBatchOperateRemoveWatermarkViewController: KMBatchOperateBaseViewController{
  10. var isBackground: Bool = false
  11. var haveFiles: Bool = false
  12. @IBOutlet var titleLabel: NSTextField!
  13. @IBOutlet var detailLabel: NSTextField!
  14. @IBOutlet var actionButton: NSButton!
  15. @IBOutlet var bottomBaseView: NSView!
  16. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  17. set{
  18. super.interfaceStatus = newValue
  19. if newValue == .PrepareProcess {
  20. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  21. let files = NSMutableArray()
  22. for url in self.successFilePathURLArray! {
  23. if FileManager.default.fileExists(atPath: url.path) {
  24. files.add(url)
  25. }
  26. }
  27. if files.count > 0 {
  28. let workspace = NSWorkspace.shared
  29. workspace.activateFileViewerSelecting(files as! [URL])
  30. }
  31. }
  32. self.actionButton.tag = 1
  33. self.actionButton.title = self.isBackground ? KMLocalizedString("Remove Background", nil) : KMLocalizedString("Remove Watermark", nil)
  34. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  35. } else {
  36. self.actionButton.tag = 0
  37. self.actionButton.title = KMLocalizedString("Cancel", nil)
  38. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  39. }
  40. }
  41. get{
  42. return super.interfaceStatus
  43. }
  44. }
  45. deinit {
  46. NotificationCenter.default.removeObserver(self)
  47. }
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. localizedLanguage()
  51. configuUI()
  52. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil)
  53. NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: Notification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  54. }
  55. @IBAction func buttonClicked_RemoveWatermark(_ sender: NSButton) {
  56. if !self.haveFiles { return }
  57. if sender.tag == 1 {
  58. var hasTask = false
  59. for i in 0..<(self.files?.count ?? 0) {
  60. let file = self.files?[i]
  61. if file?.status == .Waiting && file?.fileType == .PDF {
  62. hasTask = true
  63. break
  64. }
  65. }
  66. if !hasTask {
  67. NSSound.beep()
  68. return
  69. }
  70. let openPanel = NSOpenPanel()
  71. openPanel.canChooseFiles = false
  72. openPanel.canChooseDirectories = true
  73. openPanel.canCreateDirectories = true
  74. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  75. if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
  76. for fileURL in openPanel.urls {
  77. self.choosePath = fileURL.path
  78. if self.isBackground {
  79. self.beginRemoveBackground()
  80. } else {
  81. self.beginRemoveWatermark()
  82. }
  83. }
  84. }
  85. }
  86. } else if sender.tag == 0 {
  87. if self.queue?.operations.count ?? 0 > 0 {
  88. self.queue?.cancelAllOperations()
  89. }
  90. self.interfaceStatus = .PrepareProcess
  91. }
  92. }
  93. @objc func batchFilesCountNotification(notification: Notification) {
  94. var _: Array? = notification.object as? [Any]
  95. updateBtnStatus()
  96. }
  97. @objc func themeChanged(notification: Notification) {
  98. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  99. self.updateViewColor()
  100. }
  101. }
  102. func updateViewColor() {
  103. self.view.wantsLayer = true
  104. if KMAppearance.isDarkMode() {
  105. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  106. self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  107. } else {
  108. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  109. self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  110. }
  111. self.updateBtnStatus()
  112. }
  113. func localizedLanguage() {
  114. self.titleLabel.stringValue = self.isBackground ? KMLocalizedString("Remove Background", nil) : KMLocalizedString("Remove Watermark", nil)
  115. self.detailLabel.stringValue = self.isBackground ? KMLocalizedString("The PDF background remover helps you to remove images and color backgrounds from PDF files for an easy reading experience.", nil) : KMLocalizedString("PDF Reader Pro is a PDF watermark remover helping you to remove textual and image watermarks from PDF files. The quality of the final output is same as the original files.", nil)
  116. self.actionButton.title = self.titleLabel.stringValue
  117. }
  118. func configuUI() {
  119. self.view.wantsLayer = true
  120. self.view.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  121. self.titleLabel.font = NSFont.boldSystemFont(ofSize: 14)
  122. self.detailLabel.font = NSFont.systemFont(ofSize: 12)
  123. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  124. self.detailLabel.textColor = KMAppearance.Layout.h1Color()
  125. self.actionButton.font = NSFont.systemFont(ofSize: 13)
  126. self.actionButton.wantsLayer = true
  127. self.updateBtnStatus()
  128. self.actionButton.layer?.cornerRadius = 1.0
  129. self.bottomBaseView.wantsLayer = true
  130. self.bottomBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  131. self.updateViewColor()
  132. }
  133. func updateBtnStatus() {
  134. if KMAppearance.isDarkMode() {
  135. if self.files?.count ?? 0 > 0 {
  136. self.actionButton.layer!.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 1).cgColor
  137. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  138. self.haveFiles = true
  139. } else {
  140. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  141. self.actionButton.layer!.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 0.6).cgColor
  142. self.haveFiles = false
  143. }
  144. } else {
  145. if self.files?.count ?? 0 > 0 {
  146. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 1).cgColor
  147. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  148. self.haveFiles = true
  149. } else {
  150. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  151. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 0.6).cgColor
  152. self.haveFiles = false
  153. }
  154. }
  155. }
  156. func beginRemoveBackground() {
  157. // self.hiddenWindowCloseButtonIfNeeded()
  158. // self.successFilePathURLArray?.removeAll()
  159. // for i in 0..<(self.files?.count ?? 0) {
  160. // let file = self.files?[i]
  161. // if file?.fileType == .PDF {
  162. // file?.removeBackgroundInfo.savePath = self.choosePath
  163. // if file?.status == .Waiting {
  164. // let operation = KMBatchRemoveBackgroundOperation(file: file)
  165. // operation.delegate = self
  166. // self.queue?.addOperation(operation)
  167. // }
  168. // }
  169. // }
  170. // if self.queue?.operations.count ?? 0 > 0 {
  171. // self.interfaceStatus = .Processing
  172. // }
  173. }
  174. func beginRemoveWatermark() {
  175. self.hiddenWindowCloseButtonIfNeeded()
  176. self.successFilePathURLArray?.removeAll()
  177. for i in 0..<(self.files?.count ?? 0) {
  178. let file = self.files?[i]
  179. if file?.fileType == .PDF {
  180. file?.removeWatermarkInfo.savePath = self.choosePath
  181. if file?.status == .Waiting {
  182. let operation = KMBatchRemoveWatermarkOperation(file: file!)
  183. operation.delegate = self
  184. self.queue?.addOperation(operation)
  185. }
  186. }
  187. }
  188. if self.queue?.operations.count ?? 0 > 0 {
  189. self.interfaceStatus = .Processing
  190. }
  191. }
  192. }