KMBatchOperateRemoveWatermarkViewController.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. let arr: Array? = notification.object as? [KMBatchOperateFile]
  95. self.files? = arr ?? []
  96. if self.files?.count ?? 0 > 0{
  97. self.haveFiles = true
  98. } else {
  99. self.haveFiles = false
  100. }
  101. updateBtnStatus()
  102. }
  103. @objc func themeChanged(notification: Notification) {
  104. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  105. self.updateViewColor()
  106. }
  107. }
  108. func updateViewColor() {
  109. self.view.wantsLayer = true
  110. if KMAppearance.isDarkMode() {
  111. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  112. self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  113. } else {
  114. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  115. self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  116. }
  117. self.updateBtnStatus()
  118. }
  119. func localizedLanguage() {
  120. self.titleLabel.stringValue = self.isBackground ? KMLocalizedString("Remove Background", nil) : KMLocalizedString("Remove Watermark", nil)
  121. 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)
  122. self.actionButton.title = self.titleLabel.stringValue
  123. }
  124. func configuUI() {
  125. self.view.wantsLayer = true
  126. self.view.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  127. self.titleLabel.font = NSFont.boldSystemFont(ofSize: 14)
  128. self.detailLabel.font = NSFont.systemFont(ofSize: 12)
  129. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  130. self.detailLabel.textColor = KMAppearance.Layout.h1Color()
  131. self.actionButton.font = NSFont.systemFont(ofSize: 13)
  132. self.actionButton.wantsLayer = true
  133. self.updateBtnStatus()
  134. self.actionButton.layer?.cornerRadius = 1.0
  135. self.bottomBaseView.wantsLayer = true
  136. self.bottomBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  137. self.updateViewColor()
  138. }
  139. func updateBtnStatus() {
  140. if KMAppearance.isDarkMode() {
  141. if self.files?.count ?? 0 > 0 {
  142. self.actionButton.layer!.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 1).cgColor
  143. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  144. self.haveFiles = true
  145. } else {
  146. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  147. self.actionButton.layer!.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 0.6).cgColor
  148. self.haveFiles = false
  149. }
  150. } else {
  151. if self.files?.count ?? 0 > 0 {
  152. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 1).cgColor
  153. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  154. self.haveFiles = true
  155. } else {
  156. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  157. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 0.6).cgColor
  158. self.haveFiles = false
  159. }
  160. }
  161. }
  162. func beginRemoveBackground() {
  163. // self.hiddenWindowCloseButtonIfNeeded()
  164. // self.successFilePathURLArray?.removeAll()
  165. // for i in 0..<(self.files?.count ?? 0) {
  166. // let file = self.files?[i]
  167. // if file?.fileType == .PDF {
  168. // file?.removeBackgroundInfo.savePath = self.choosePath
  169. // if file?.status == .Waiting {
  170. // let operation = KMBatchRemoveBackgroundOperation(file: file)
  171. // operation.delegate = self
  172. // self.queue?.addOperation(operation)
  173. // }
  174. // }
  175. // }
  176. // if self.queue?.operations.count ?? 0 > 0 {
  177. // self.interfaceStatus = .Processing
  178. // }
  179. }
  180. func beginRemoveWatermark() {
  181. self.hiddenWindowCloseButtonIfNeeded()
  182. self.successFilePathURLArray?.removeAll()
  183. for i in 0..<(self.files?.count ?? 0) {
  184. let file = self.files?[i]
  185. if file?.fileType == .PDF {
  186. file?.removeWatermarkInfo.savePath = self.choosePath
  187. if file?.status == .Waiting {
  188. let operation = KMBatchRemoveWatermarkOperation(file: file!)
  189. operation.delegate = self
  190. self.queue?.addOperation(operation)
  191. }
  192. }
  193. }
  194. if self.queue?.operations.count ?? 0 > 0 {
  195. self.interfaceStatus = .Processing
  196. }
  197. }
  198. }