KMBatchOperateCompressViewController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // KMBatchOperateCompressViewController.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by kdanmobile on 2023/10/31.
  6. //
  7. import Cocoa
  8. class KMBatchOperateCompressViewController: KMBatchOperateBaseViewController{
  9. @IBOutlet var titleLabel: NSTextField!
  10. @IBOutlet var dpi300Button: NSButton!
  11. @IBOutlet var dpi300Label: NSTextField!
  12. @IBOutlet var dpi150Button: NSButton!
  13. @IBOutlet var dpi150Label: NSTextField!
  14. @IBOutlet var dpi72Button: NSButton!
  15. @IBOutlet var dpi72Label: NSTextField!
  16. @IBOutlet var dpi50Button: NSButton!
  17. @IBOutlet var dpi50Label: NSTextField!
  18. @IBOutlet var otherChoiceButton: NSButton!
  19. @IBOutlet var actionButton: NSButton!
  20. @IBOutlet var comprssSlider: KMSlider!
  21. @IBOutlet var lowLabel: NSTextField!
  22. @IBOutlet var highLabel: NSTextField!
  23. var currentType: NSNumber = 60
  24. var savePath: String?
  25. var haveFiles: Bool = false
  26. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  27. set{
  28. super.interfaceStatus = newValue
  29. if newValue == .PrepareProcess {
  30. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  31. let files = NSMutableArray()
  32. for url in self.successFilePathURLArray! {
  33. if FileManager.default.fileExists(atPath: url.path) {
  34. files.add(url)
  35. }
  36. }
  37. if files.count > 0 {
  38. let workspace = NSWorkspace.shared
  39. workspace.activateFileViewerSelecting(files as! [URL])
  40. }
  41. }
  42. self.actionButton.tag = 1
  43. self.actionButton.title = NSLocalizedString("Compress", comment: "")
  44. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  45. } else {
  46. self.actionButton.tag = 0
  47. self.actionButton.title = NSLocalizedString("Cancel", comment: "")
  48. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  49. }
  50. }
  51. get{
  52. return super.interfaceStatus
  53. }
  54. }
  55. deinit {
  56. NotificationCenter.default.removeObserver(self)
  57. DistributedNotificationCenter.default().removeObserver(self)
  58. }
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. self.localizedLanguage()
  62. self.configuUI()
  63. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  64. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  65. }
  66. func localizedLanguage() {
  67. self.titleLabel.stringValue = NSLocalizedString("Optimization Options", comment: "")
  68. self.actionButton.title = NSLocalizedString("Compress", comment: "")
  69. self.dpi300Label.stringValue = NSLocalizedString("Large File Size(High Quality)", comment: "")
  70. self.dpi150Label.stringValue = NSLocalizedString("Medium File Size(Medium Quality)", comment: "")
  71. self.dpi72Label.stringValue = NSLocalizedString("Small File Size(Recommended Quality)", comment: "")
  72. self.dpi50Label.stringValue = NSLocalizedString("Minimum File Size(Low Quality)", comment: "")
  73. self.otherChoiceButton.title = NSLocalizedString("Other", comment: "")
  74. self.lowLabel.stringValue = NSLocalizedString("Low Quality", comment: "")
  75. self.highLabel.stringValue = NSLocalizedString("High Quality", comment: "")
  76. self.lowLabel.stringValue = NSLocalizedString("Low Quality", comment: "")
  77. }
  78. func configuUI() {
  79. self.view.wantsLayer = true
  80. self.updateViewColor()
  81. self.dpi300Button.setTitleColor(KMAppearance.Layout.h0Color())
  82. self.dpi150Button.setTitleColor(KMAppearance.Layout.h0Color())
  83. self.dpi72Button.setTitleColor(KMAppearance.Layout.h0Color())
  84. self.dpi50Button.setTitleColor(KMAppearance.Layout.h0Color())
  85. self.otherChoiceButton.setTitleColor(KMAppearance.Layout.h0Color())
  86. self.currentType = 60
  87. self.dpi72Button.state = .on
  88. self.dpi50Button.state = .off
  89. self.dpi300Button.state = .off
  90. self.dpi150Button.state = .off
  91. self.otherChoiceButton.state = .off
  92. self.highLabel.isEnabled = false
  93. self.lowLabel.isEnabled = false
  94. self.comprssSlider.isEnabled = false
  95. self.comprssSlider.tipEnabled = false
  96. self.lowLabel.textColor = KMAppearance.Layout.h0Color()
  97. self.highLabel.textColor = KMAppearance.Layout.h0Color()
  98. self.lowLabel.alphaValue = 0.4
  99. self.highLabel.alphaValue = 0.4
  100. self.actionButton.wantsLayer = true
  101. if self.files!.count > 0 {
  102. self.actionButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  103. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  104. self.haveFiles = true
  105. } else {
  106. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  107. self.actionButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  108. self.haveFiles = false
  109. }
  110. self.actionButton.layer!.cornerRadius = 1.0
  111. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
  112. }
  113. func updateViewColor() {
  114. if KMAppearance.isDarkMode() {
  115. self.view.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  116. } else {
  117. self.view.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  118. }
  119. }
  120. @objc func themeChanged(notification: Notification) {
  121. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  122. self.updateViewColor()
  123. }
  124. }
  125. @objc func batchFilesCountNotification(notification: Notification) {
  126. let files: Array? = notification.object as? [KMBatchOperateFile]
  127. self.files? = files ?? []
  128. if files!.count > 0 {
  129. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  130. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  131. self.haveFiles = true
  132. } else {
  133. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  134. self.actionButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  135. self.haveFiles = false
  136. }
  137. }
  138. @IBAction func buttonClicked_Action(_ sender: Any) {
  139. if !self.haveFiles { return }
  140. //点击开始
  141. if (sender as AnyObject).tag == 1 {
  142. self.beginBatchOperation()
  143. } else if (sender as AnyObject).tag == 0 {
  144. self.cancelBatchOperation()
  145. }
  146. }
  147. @IBAction func comprssVauleAction(_ sender: Any) {
  148. self.currentType = (comprssSlider.integerValue) as NSNumber;
  149. }
  150. @IBAction func buttonClicked_ChooseDPI(_ sender: NSButton) {
  151. self.dpi300Button.state = .off
  152. self.dpi150Button.state = .off
  153. self.dpi72Button.state = .off
  154. self.dpi50Button.state = .off
  155. self.otherChoiceButton.state = .off
  156. sender.state = .on
  157. if sender.tag == 0 {
  158. self.currentType = 120
  159. } else if sender.tag == 1 {
  160. self.currentType = 90
  161. } else if sender.tag == 2 {
  162. self.currentType = 60
  163. } else if sender.tag == 3 {
  164. self.currentType = 30
  165. } else {
  166. self.currentType = (self.comprssSlider.integerValue) as NSNumber
  167. }
  168. if sender.tag == 4 {
  169. highLabel.isEnabled = true
  170. lowLabel.isEnabled = true
  171. comprssSlider.isEnabled = true
  172. comprssSlider.tipEnabled = true
  173. lowLabel.textColor = KMAppearance.Layout.h0Color()
  174. highLabel.textColor = KMAppearance.Layout.h0Color()
  175. self.lowLabel.alphaValue = 1
  176. self.highLabel.alphaValue = 1
  177. } else {
  178. highLabel.isEnabled = false
  179. lowLabel.isEnabled = false
  180. comprssSlider.isEnabled = false
  181. comprssSlider.tipEnabled = false
  182. lowLabel.textColor = KMAppearance.Layout.h0Color()
  183. highLabel.textColor = KMAppearance.Layout.h0Color()
  184. self.lowLabel.alphaValue = 0.4
  185. self.highLabel.alphaValue = 0.4
  186. }
  187. }
  188. override func beginQueueOperation() {
  189. // hiddenWindowCloseButtonIfNeeded()
  190. self.successFilePathURLArray?.removeAll()
  191. for i in 0..<self.files!.count {
  192. let file = self.files![i]
  193. if file.fileType == .PDF {
  194. file.compressInfo.savePath = self.choosePath
  195. if file.status == .Waiting {
  196. let operation = KMCompressOperation.init(file: file, compressValue: self.currentType)
  197. operation.delegate = self
  198. operation.viewController = self
  199. self.queue?.addOperation(operation)
  200. }
  201. }
  202. }
  203. if self.queue?.operations.count ?? 0 > 0 {
  204. self.interfaceStatus = .Processing
  205. }
  206. }
  207. }