KMBatchOperateCompressViewController.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // KMBatchOperateCompressViewController.swift
  3. // PDF Master
  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?
  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. var 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. }
  58. override func viewDidLoad() {
  59. super.viewDidLoad()
  60. self.localizedLanguage()
  61. self.configuUI()
  62. NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  63. }
  64. func localizedLanguage() {
  65. self.titleLabel.stringValue = NSLocalizedString("Optimization Options", comment: "")
  66. self.actionButton.title = NSLocalizedString("Compress", comment: "")
  67. self.dpi300Label.stringValue = NSLocalizedString("Large File Size(High Quality)", comment: "")
  68. self.dpi150Label.stringValue = NSLocalizedString("Medium File Size(Medium Quality)", comment: "")
  69. self.dpi72Label.stringValue = NSLocalizedString("Small File Size(Recommended Quality)", comment: "")
  70. self.dpi50Label.stringValue = NSLocalizedString("Minimum File Size(Low Quality)", comment: "")
  71. self.otherChoiceButton.title = NSLocalizedString("Other", comment: "")
  72. self.lowLabel.stringValue = NSLocalizedString("Low Quality", comment: "")
  73. self.highLabel.stringValue = NSLocalizedString("High Quality", comment: "")
  74. self.lowLabel.stringValue = NSLocalizedString("Low Quality", comment: "")
  75. }
  76. func configuUI() {
  77. self.view.wantsLayer = true
  78. self.updateViewColor()
  79. self.dpi300Button.setTitleColor(KMAppearance.Layout.h0Color())
  80. self.dpi150Button.setTitleColor(KMAppearance.Layout.h0Color())
  81. self.dpi72Button.setTitleColor(KMAppearance.Layout.h0Color())
  82. self.dpi50Button.setTitleColor(KMAppearance.Layout.h0Color())
  83. self.otherChoiceButton.setTitleColor(KMAppearance.Layout.h0Color())
  84. self.currentType = 60
  85. self.dpi72Button.state = .on
  86. self.dpi50Button.state = .off
  87. self.dpi300Button.state = .off
  88. self.dpi150Button.state = .off
  89. self.otherChoiceButton.state = .off
  90. self.highLabel.isEnabled = false
  91. self.lowLabel.isEnabled = false
  92. self.comprssSlider.isEnabled = false
  93. self.comprssSlider.tipEnabled = false
  94. self.lowLabel.textColor = KMAppearance.Layout.h0Color()
  95. self.highLabel.textColor = KMAppearance.Layout.h0Color()
  96. self.lowLabel.alphaValue = 0.4
  97. self.highLabel.alphaValue = 0.4
  98. self.actionButton.wantsLayer = true
  99. if self.files!.count > 0 {
  100. self.actionButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  101. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  102. self.haveFiles = true
  103. } else {
  104. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  105. self.actionButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  106. self.haveFiles = false
  107. }
  108. self.actionButton.layer!.cornerRadius = 1.0
  109. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
  110. }
  111. func updateViewColor() {
  112. if KMAppearance.isDarkMode() {
  113. self.view.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  114. } else {
  115. self.view.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  116. }
  117. }
  118. @objc func themeChanged(notification: Notification) {
  119. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  120. self.updateViewColor()
  121. }
  122. }
  123. @objc func batchFilesCountNotification(notification: Notification) {
  124. let files = notification.object as? [Any]
  125. if files!.count > 0 {
  126. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  127. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  128. self.haveFiles = true
  129. } else {
  130. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  131. self.actionButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  132. self.haveFiles = false
  133. }
  134. }
  135. @IBAction func buttonClicked_Action(_ sender: Any) {
  136. if !self.haveFiles { return }
  137. //点击开始
  138. if (sender as AnyObject).tag == 1 {
  139. self.beginBatchOperation()
  140. } else if (sender as AnyObject).tag == 0 {
  141. self.cancelBatchOperation()
  142. }
  143. }
  144. @IBAction func comprssVauleAction(_ sender: Any) {
  145. self.currentType = (comprssSlider.integerValue) as NSNumber;
  146. }
  147. @IBAction func buttonClicked_ChooseDPI(_ sender: NSButton) {
  148. self.dpi300Button.state = .off
  149. self.dpi150Button.state = .off
  150. self.dpi72Button.state = .off
  151. self.dpi50Button.state = .off
  152. self.otherChoiceButton.state = .off
  153. sender.state = .on
  154. if sender.tag == 0 {
  155. self.currentType = 120
  156. } else if sender.tag == 1 {
  157. self.currentType = 90
  158. } else if sender.tag == 2 {
  159. self.currentType = 60
  160. } else if sender.tag == 3 {
  161. self.currentType = 30
  162. } else {
  163. self.currentType = (self.comprssSlider.integerValue) as NSNumber
  164. }
  165. if sender.tag == 4 {
  166. highLabel.isEnabled = true
  167. lowLabel.isEnabled = true
  168. comprssSlider.isEnabled = true
  169. comprssSlider.tipEnabled = true
  170. lowLabel.textColor = KMAppearance.Layout.h0Color()
  171. highLabel.textColor = KMAppearance.Layout.h0Color()
  172. self.lowLabel.alphaValue = 1
  173. self.highLabel.alphaValue = 1
  174. } else {
  175. highLabel.isEnabled = false
  176. lowLabel.isEnabled = false
  177. comprssSlider.isEnabled = false
  178. comprssSlider.tipEnabled = false
  179. lowLabel.textColor = KMAppearance.Layout.h0Color()
  180. highLabel.textColor = KMAppearance.Layout.h0Color()
  181. self.lowLabel.alphaValue = 0.4
  182. self.highLabel.alphaValue = 0.4
  183. }
  184. }
  185. }