KMCompressWindowController.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // KMCompressWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/11/25.
  6. //
  7. import Cocoa
  8. typealias KMCompressWindowControllerItemClick = (Int)->()
  9. typealias KMCompressWindowControllerResultCallback = (_ result: Bool,_ openDocuemt: Bool, _ fileURL: URL, _ error: String)->()
  10. class KMCompressWindowController: NSWindowController {
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var largeBox: NSBox!
  13. @IBOutlet weak var standardBox: NSBox!
  14. @IBOutlet weak var smallBox: NSBox!
  15. @IBOutlet weak var minimumBox: NSBox!
  16. @IBOutlet weak var batchButton: NSButton!
  17. @IBOutlet weak var cancelBox: NSBox!
  18. @IBOutlet weak var compressBox: NSBox!
  19. var documentURL: URL!
  20. var password: String!
  21. private var datas: [String] = []
  22. var selectIndex = 2
  23. var itemClick : KMCompressWindowControllerItemClick!
  24. var resultCallback : KMCompressWindowControllerResultCallback!
  25. var largeVC : KMDesignButton!
  26. var standardVC : KMDesignButton!
  27. var smallVC : KMDesignButton!
  28. var minimumVC : KMDesignButton!
  29. var cancelVC : KMDesignButton!
  30. var compressVC : KMDesignButton!
  31. deinit {
  32. KMPrint("KMCompressWindowController 已释放")
  33. }
  34. override func windowDidLoad() {
  35. super.windowDidLoad()
  36. self.window?.appearance = NSAppearance(named: .aqua)
  37. largeVC = KMDesignButton.init(withType: .RadioButton)
  38. standardVC = KMDesignButton.init(withType: .RadioButton)
  39. smallVC = KMDesignButton.init(withType: .RadioButton)
  40. minimumVC = KMDesignButton.init(withType: .RadioButton)
  41. cancelVC = KMDesignButton.init(withType: .Text)
  42. compressVC = KMDesignButton.init(withType: .Text)
  43. largeBox.fillColor = .clear
  44. largeBox.contentView = largeVC.view
  45. standardBox.fillColor = .clear
  46. standardBox.contentView = standardVC.view
  47. smallBox.fillColor = .clear
  48. smallBox.contentView = smallVC.view
  49. minimumBox.fillColor = .clear
  50. minimumBox.contentView = minimumVC.view
  51. cancelBox.fillColor = .clear
  52. cancelBox.contentView = cancelVC.view
  53. compressBox.fillColor = .clear
  54. compressBox.contentView = compressVC.view
  55. titleLabel.stringValue = NSLocalizedString("Compress", comment: "")
  56. batchButton.isBordered = false
  57. batchButton.title = NSLocalizedString("Batch", comment: "")
  58. batchButton.wantsLayer = true
  59. batchButton.layer?.borderWidth = 1
  60. batchButton.layer?.borderColor = NSColor.black.cgColor
  61. batchButton.layer?.cornerRadius = 4
  62. batchButton.target = self
  63. batchButton.action = #selector(batchButtonAction)
  64. batchButton.isHidden = true
  65. cancelVC.target = self
  66. cancelVC.action = #selector(cancelButtonAction)
  67. cancelVC.stringValue = NSLocalizedString("Cancel", comment: "")
  68. cancelVC.button(type: .Sec, size: .m)
  69. cancelVC.button.keyEquivalent = KMKeyEquivalent.esc.string()
  70. compressVC.target = self
  71. compressVC.action = #selector(compressButtonAction)
  72. compressVC.stringValue = NSLocalizedString("Compress", comment: "")
  73. compressVC.button(type: .Cta, size: .m)
  74. compressVC.button.keyEquivalent = KMKeyEquivalent.enter
  75. largeVC.target = self
  76. largeVC.action = #selector(self.radioButtonAction(_:))
  77. largeVC.stringValue = NSLocalizedString("Large File Size", comment: "")
  78. largeVC.checkbox_radio()
  79. standardVC.target = self
  80. standardVC.action = #selector(self.radioButtonAction(_:))
  81. standardVC.stringValue = NSLocalizedString("Standard File Size", comment: "")
  82. standardVC.checkbox_radio()
  83. smallVC.target = self
  84. smallVC.action = #selector(self.radioButtonAction(_:))
  85. smallVC.stringValue = NSLocalizedString("Small File Size", comment: "")
  86. smallVC.checkbox_radio()
  87. minimumVC.target = self
  88. minimumVC.action = #selector(self.radioButtonAction(_:))
  89. minimumVC.stringValue = NSLocalizedString("Minimum File Size", comment: "")
  90. minimumVC.checkbox_radio()
  91. self.initUIProperty()
  92. smallVC.state = .Checked
  93. }
  94. private func initUIProperty() {
  95. self.titleLabel.textColor = NSColor.titleColor()
  96. self.titleLabel.font = .SFProTextSemibold(16)
  97. self.batchButton.setTitleColor(NSColor.buttonTitleColor())
  98. self.batchButton.layer?.borderColor = NSColor.buttonBorderColor().cgColor
  99. self.batchButton.font = .SFProTextRegular(14)
  100. }
  101. @objc func radioButtonAction(_ sender: NSButton) {
  102. largeVC.state = .Norm
  103. standardVC.state = .Norm
  104. smallVC.state = .Norm
  105. minimumVC.state = .Norm
  106. if (largeVC.button == sender) {
  107. largeVC.state = .Checked
  108. self.selectIndex = 0
  109. } else if (standardVC.button == sender) {
  110. standardVC.state = .Checked
  111. self.selectIndex = 1
  112. } else if (smallVC.button == sender) {
  113. smallVC.state = .Checked
  114. self.selectIndex = 2
  115. } else if (minimumVC.button == sender) {
  116. minimumVC.state = .Checked
  117. self.selectIndex = 3
  118. }
  119. }
  120. @objc func batchButtonAction() {
  121. guard let callback = itemClick else {
  122. return
  123. }
  124. callback(1)
  125. }
  126. @objc func cancelButtonAction() {
  127. guard let callback = itemClick else {
  128. return
  129. }
  130. callback(2)
  131. }
  132. @objc func compressButtonAction() {
  133. if documentURL == nil {
  134. return
  135. }
  136. NSPanel.savePanel(self.window!, true) { panel in
  137. panel.nameFieldStringValue = ""+self.documentURL.deletingPathExtension().lastPathComponent+"_Compressed"
  138. panel.allowedFileTypes = ["pdf"]
  139. } completion: { response, url, isOpen in
  140. if (response == .cancel) {
  141. return
  142. }
  143. self.beginLoading()
  144. DispatchQueue.global().async {
  145. let docuemt = CPDFDocument.init(url: self.documentURL)
  146. if (docuemt?.isLocked)! && self.password != nil {
  147. docuemt?.unlock(withPassword: self.password)
  148. }
  149. var option = 60
  150. if self.selectIndex == 0 {
  151. option = 120
  152. } else if self.selectIndex == 1 {
  153. option = 90
  154. } else if self.selectIndex == 2 {
  155. option = 60
  156. } else if self.selectIndex == 3 {
  157. option = 30
  158. }
  159. let result = docuemt?.writeOptimize(to: url, withOptions: [.imageQualityOption : option])
  160. DispatchQueue.main.async {
  161. self.endLoading()
  162. guard let callback = self.resultCallback else {
  163. if result! {
  164. if isOpen { /// 开启文档
  165. if !url!.path.isPDFValid() {
  166. let alert = NSAlert()
  167. alert.alertStyle = .critical
  168. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  169. alert.runModal()
  170. return
  171. }
  172. NSDocumentController.shared.openDocument(withContentsOf: url!, display: true) { document, alreadyOpen, error in
  173. }
  174. } else {
  175. NSWorkspace.shared.activateFileViewerSelecting([url!])
  176. }
  177. }
  178. return
  179. }
  180. callback(result!, isOpen, url!, "")
  181. }
  182. }
  183. }
  184. }
  185. func getUniqueFilePath(filePath: String) -> String {
  186. var i: Int = 0
  187. var isDirectory: ObjCBool = false
  188. var uniqueFilePath = filePath
  189. let fileManager = FileManager.default
  190. fileManager.fileExists(atPath: uniqueFilePath, isDirectory: &isDirectory)
  191. if isDirectory.boolValue {
  192. var path: String = ""
  193. while fileManager.fileExists(atPath: uniqueFilePath) {
  194. i += 1
  195. path = filePath
  196. path.append("(\(i))")
  197. uniqueFilePath = path
  198. }
  199. } else {
  200. let fileURL = URL(fileURLWithPath: filePath)
  201. var path: String = ""
  202. while fileManager.fileExists(atPath: uniqueFilePath) {
  203. i += 1
  204. path = fileURL.deletingPathExtension().path
  205. path.append("(\(i))")
  206. path.append(".")
  207. path.append(fileURL.pathExtension)
  208. uniqueFilePath = path
  209. }
  210. }
  211. return uniqueFilePath
  212. }
  213. }
  214. extension KMCompressWindowController: NSTableViewDataSource {
  215. func numberOfRows(in tableView: NSTableView) -> Int {
  216. datas.count
  217. }
  218. }
  219. extension KMCompressWindowController: KMLoadingProtocol {
  220. func beginLoading() {
  221. self.window?.contentView?.beginLoading()
  222. }
  223. func endLoading() {
  224. self.window?.contentView?.endLoading()
  225. }
  226. }