KMBatchOperateRemoveWatermarkViewController.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // KMBatchOperateRemoveWatermarkViewController.swift
  3. // Cisdem PDFMaster
  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. DistributedNotificationCenter.default().removeObserver(self)
  48. }
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. localizedLanguage()
  52. configuUI()
  53. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil)
  54. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: Notification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  55. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  56. }
  57. @IBAction func buttonClicked_RemoveWatermark(_ sender: NSButton) {
  58. if !self.haveFiles { return }
  59. if sender.tag == 1 {
  60. var hasTask = false
  61. for i in 0..<(self.files?.count ?? 0) {
  62. let file = self.files?[i]
  63. if file?.status == .Waiting && file?.fileType == .PDF {
  64. hasTask = true
  65. break
  66. }
  67. }
  68. if !hasTask {
  69. NSSound.beep()
  70. return
  71. }
  72. let openPanel = NSOpenPanel()
  73. openPanel.canChooseFiles = false
  74. openPanel.canChooseDirectories = true
  75. openPanel.canCreateDirectories = true
  76. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  77. if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
  78. for fileURL in openPanel.urls {
  79. self.choosePath = fileURL.path
  80. if self.isBackground {
  81. self.beginRemoveBackground()
  82. } else {
  83. self.beginRemoveWatermark()
  84. }
  85. }
  86. }
  87. }
  88. } else if sender.tag == 0 {
  89. if self.queue?.operations.count ?? 0 > 0 {
  90. self.queue?.cancelAllOperations()
  91. }
  92. self.interfaceStatus = .PrepareProcess
  93. }
  94. }
  95. @objc func batchFilesCountNotification(notification: Notification) {
  96. let arr: Array? = notification.object as? [KMBatchOperateFile]
  97. self.files? = arr ?? []
  98. if self.files?.count ?? 0 > 0{
  99. self.haveFiles = true
  100. } else {
  101. self.haveFiles = false
  102. }
  103. updateBtnStatus()
  104. }
  105. @objc func themeChanged(notification: Notification) {
  106. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  107. self.updateViewColor()
  108. }
  109. }
  110. func updateViewColor() {
  111. self.view.wantsLayer = true
  112. if KMAppearance.isDarkMode() {
  113. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  114. self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  115. } else {
  116. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  117. self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  118. }
  119. self.updateBtnStatus()
  120. }
  121. func localizedLanguage() {
  122. self.titleLabel.stringValue = self.isBackground ? KMLocalizedString("Remove Background", nil) : KMLocalizedString("Remove Watermark", nil)
  123. 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("Cisdem PDFMaster 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)
  124. self.actionButton.title = self.titleLabel.stringValue
  125. }
  126. func configuUI() {
  127. self.view.wantsLayer = true
  128. self.view.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  129. self.titleLabel.font = NSFont.boldSystemFont(ofSize: 14)
  130. self.detailLabel.font = NSFont.systemFont(ofSize: 12)
  131. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  132. self.detailLabel.textColor = KMAppearance.Layout.h1Color()
  133. self.actionButton.font = NSFont.systemFont(ofSize: 13)
  134. self.actionButton.wantsLayer = true
  135. self.updateBtnStatus()
  136. self.actionButton.layer?.cornerRadius = 1.0
  137. self.bottomBaseView.wantsLayer = true
  138. self.bottomBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  139. self.updateViewColor()
  140. }
  141. func updateBtnStatus() {
  142. if KMAppearance.isDarkMode() {
  143. if self.files?.count ?? 0 > 0 {
  144. self.actionButton.layer!.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 1).cgColor
  145. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  146. self.haveFiles = true
  147. } else {
  148. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  149. self.actionButton.layer!.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 0.6).cgColor
  150. self.haveFiles = false
  151. }
  152. } else {
  153. if self.files?.count ?? 0 > 0 {
  154. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 1).cgColor
  155. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  156. self.haveFiles = true
  157. } else {
  158. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  159. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 0.6).cgColor
  160. self.haveFiles = false
  161. }
  162. }
  163. }
  164. func beginRemoveBackground() {
  165. self.hiddenWindowCloseButtonIfNeeded()
  166. self.successFilePathURLArray?.removeAll()
  167. for i in 0..<(self.files?.count ?? 0) {
  168. let file = self.files?[i]
  169. if file?.fileType == .PDF {
  170. file?.removeBackgroundInfo.savePath = self.choosePath
  171. if file?.status == .Waiting {
  172. let operation = KMBatchRemoveBackgroundOperation(file: file!)
  173. operation.delegate = self
  174. self.queue?.addOperation(operation)
  175. }
  176. }
  177. }
  178. if self.queue?.operations.count ?? 0 > 0 {
  179. self.interfaceStatus = .Processing
  180. }
  181. }
  182. func beginRemoveWatermark() {
  183. self.hiddenWindowCloseButtonIfNeeded()
  184. self.successFilePathURLArray?.removeAll()
  185. for i in 0..<(self.files?.count ?? 0) {
  186. let file = self.files?[i]
  187. if file?.fileType == .PDF {
  188. file?.removeWatermarkInfo.savePath = self.choosePath
  189. if file?.status == .Waiting {
  190. let operation = KMBatchRemoveWatermarkOperation(file: file!)
  191. operation.delegate = self
  192. self.queue?.addOperation(operation)
  193. }
  194. }
  195. }
  196. if self.queue?.operations.count ?? 0 > 0 {
  197. self.interfaceStatus = .Processing
  198. }
  199. }
  200. }