KMCompressWindowController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // KMCompressWindowController.swift
  3. // PDF Reader Pro
  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 limit = true
  24. var oriDocumentUrl: URL?
  25. var itemClick : KMCompressWindowControllerItemClick!
  26. var resultCallback : KMCompressWindowControllerResultCallback!
  27. var largeVC : KMDesignButton!
  28. var standardVC : KMDesignButton!
  29. var smallVC : KMDesignButton!
  30. var minimumVC : KMDesignButton!
  31. var cancelVC : KMDesignButton!
  32. var compressVC : KMDesignButton!
  33. deinit {
  34. KMPrint("KMCompressWindowController 已释放")
  35. }
  36. override func windowDidLoad() {
  37. super.windowDidLoad()
  38. // self.window?.appearance = NSAppearance(named: .aqua)
  39. largeVC = KMDesignButton.init(withType: .RadioButton)
  40. standardVC = KMDesignButton.init(withType: .RadioButton)
  41. smallVC = KMDesignButton.init(withType: .RadioButton)
  42. minimumVC = KMDesignButton.init(withType: .RadioButton)
  43. cancelVC = KMDesignButton.init(withType: .Text)
  44. compressVC = KMDesignButton.init(withType: .Text)
  45. largeBox.fillColor = .clear
  46. largeBox.contentView = largeVC.view
  47. standardBox.fillColor = .clear
  48. standardBox.contentView = standardVC.view
  49. smallBox.fillColor = .clear
  50. smallBox.contentView = smallVC.view
  51. minimumBox.fillColor = .clear
  52. minimumBox.contentView = minimumVC.view
  53. cancelBox.fillColor = .clear
  54. cancelBox.contentView = cancelVC.view
  55. compressBox.fillColor = .clear
  56. compressBox.contentView = compressVC.view
  57. titleLabel.stringValue = NSLocalizedString("Compress", comment: "")
  58. batchButton.isBordered = false
  59. batchButton.title = NSLocalizedString("Batch", comment: "")
  60. batchButton.wantsLayer = true
  61. batchButton.layer?.borderWidth = 1
  62. batchButton.layer?.borderColor = NSColor.black.cgColor
  63. batchButton.layer?.cornerRadius = 4
  64. batchButton.target = self
  65. batchButton.action = #selector(batchButtonAction)
  66. batchButton.isHidden = true
  67. cancelVC.target = self
  68. cancelVC.action = #selector(cancelButtonAction)
  69. cancelVC.stringValue = NSLocalizedString("Cancel", comment: "")
  70. cancelVC.button(type: .Sec, size: .m)
  71. cancelVC.button.keyEquivalent = KMKeyEquivalent.esc.string()
  72. compressVC.target = self
  73. compressVC.action = #selector(compressButtonAction)
  74. compressVC.stringValue = NSLocalizedString("Compress", comment: "")
  75. compressVC.button(type: .Cta, size: .m)
  76. compressVC.button.keyEquivalent = KMKeyEquivalent.enter
  77. largeVC.target = self
  78. largeVC.action = #selector(self.radioButtonAction(_:))
  79. largeVC.stringValue = NSLocalizedString("Large File Size", comment: "")
  80. largeVC.checkbox_radio()
  81. standardVC.target = self
  82. standardVC.action = #selector(self.radioButtonAction(_:))
  83. standardVC.stringValue = NSLocalizedString("Standard File Size", comment: "")
  84. standardVC.checkbox_radio()
  85. smallVC.target = self
  86. smallVC.action = #selector(self.radioButtonAction(_:))
  87. smallVC.stringValue = NSLocalizedString("Small File Size", comment: "")
  88. smallVC.checkbox_radio()
  89. minimumVC.target = self
  90. minimumVC.action = #selector(self.radioButtonAction(_:))
  91. minimumVC.stringValue = NSLocalizedString("Minimum File Size", comment: "")
  92. minimumVC.checkbox_radio()
  93. self.initUIProperty()
  94. smallVC.state = .Checked
  95. }
  96. private func initUIProperty() {
  97. self.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  98. self.titleLabel.font = .SFProTextSemiboldFont(16)
  99. self.batchButton.setTitleColor(color: NSColor.km_init(hex: "#252629"))
  100. // self.batchButton.layer?.borderColor = NSColor.buttonBorderColor().cgColor
  101. self.batchButton.font = .SFProTextRegularFont(14)
  102. }
  103. @objc func radioButtonAction(_ sender: NSButton) {
  104. largeVC.state = .Norm
  105. standardVC.state = .Norm
  106. smallVC.state = .Norm
  107. minimumVC.state = .Norm
  108. if (largeVC.button == sender) {
  109. largeVC.state = .Checked
  110. self.selectIndex = 0
  111. } else if (standardVC.button == sender) {
  112. standardVC.state = .Checked
  113. self.selectIndex = 1
  114. } else if (smallVC.button == sender) {
  115. smallVC.state = .Checked
  116. self.selectIndex = 2
  117. } else if (minimumVC.button == sender) {
  118. minimumVC.state = .Checked
  119. self.selectIndex = 3
  120. }
  121. }
  122. @objc func batchButtonAction() {
  123. guard let callback = itemClick else {
  124. return
  125. }
  126. callback(1)
  127. }
  128. @objc func cancelButtonAction() {
  129. self._clearData()
  130. guard let callback = itemClick else {
  131. return
  132. }
  133. callback(2)
  134. }
  135. @objc func compressButtonAction() {
  136. if documentURL == nil {
  137. return
  138. }
  139. self._compressButtonAction()
  140. }
  141. func getUniqueFilePath(filePath: String) -> String {
  142. var i: Int = 0
  143. var isDirectory: ObjCBool = false
  144. var uniqueFilePath = filePath
  145. let fileManager = FileManager.default
  146. fileManager.fileExists(atPath: uniqueFilePath, isDirectory: &isDirectory)
  147. if isDirectory.boolValue {
  148. var path: String = ""
  149. while fileManager.fileExists(atPath: uniqueFilePath) {
  150. i += 1
  151. path = filePath
  152. path.append("(\(i))")
  153. uniqueFilePath = path
  154. }
  155. } else {
  156. let fileURL = URL(fileURLWithPath: filePath)
  157. var path: String = ""
  158. while fileManager.fileExists(atPath: uniqueFilePath) {
  159. i += 1
  160. path = fileURL.deletingPathExtension().path
  161. path.append("(\(i))")
  162. path.append(".")
  163. path.append(fileURL.pathExtension)
  164. uniqueFilePath = path
  165. }
  166. }
  167. return uniqueFilePath
  168. }
  169. private func _compressButtonAction(limit: Bool = false) {
  170. DispatchQueue.main.async {
  171. NSPanel.savePanel(self.window!, true) { panel in
  172. var url: URL = self.documentURL
  173. if (self.oriDocumentUrl != nil) {
  174. url = self.oriDocumentUrl!
  175. }
  176. panel.nameFieldStringValue = ""+url.deletingPathExtension().lastPathComponent+"_Compressed"
  177. panel.allowedFileTypes = ["pdf"]
  178. } completion: { response, url, isOpen in
  179. if (response == .cancel) {
  180. return
  181. }
  182. self.beginLoading()
  183. DispatchQueue.global().async {
  184. let docuemt = CPDFDocument.init(url: self.documentURL)
  185. if (docuemt?.isLocked)! && self.password != nil {
  186. docuemt?.unlock(withPassword: self.password)
  187. }
  188. var option = 60
  189. if self.selectIndex == 0 {
  190. option = 120
  191. } else if self.selectIndex == 1 {
  192. option = 90
  193. } else if self.selectIndex == 2 {
  194. option = 60
  195. } else if self.selectIndex == 3 {
  196. option = 30
  197. }
  198. //
  199. var result = false
  200. if (limit) {
  201. if let _document = docuemt, let _ = KMTools.saveWatermarkDocumentForCompress(document: _document, to: url!, imageQuality: option) {
  202. result = true
  203. }
  204. } else {
  205. if let data = docuemt?.writeOptimize(to: url, withOptions: [.imageQualityOption : option]) {
  206. result = data
  207. }
  208. }
  209. if (result) {
  210. self._clearData()
  211. }
  212. DispatchQueue.main.async {
  213. self.endLoading()
  214. guard let callback = self.resultCallback else {
  215. if (result) {
  216. if isOpen { /// 开启文档
  217. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  218. }
  219. } else {
  220. NSWorkspace.shared.activateFileViewerSelecting([url!])
  221. }
  222. }
  223. return
  224. }
  225. callback(result, isOpen, url!, "")
  226. }
  227. }
  228. }
  229. }
  230. }
  231. private func _clearData() {
  232. if let _ = self.oriDocumentUrl {
  233. if let data = self.documentURL?.path, FileManager.default.fileExists(atPath: data) {
  234. try?FileManager.default.removeItem(atPath: data)
  235. }
  236. }
  237. }
  238. }
  239. extension KMCompressWindowController: NSTableViewDataSource {
  240. func numberOfRows(in tableView: NSTableView) -> Int {
  241. datas.count
  242. }
  243. }
  244. extension KMCompressWindowController: KMLoadingProtocol {
  245. func beginLoading() {
  246. self.window?.contentView?.beginLoading()
  247. }
  248. func endLoading() {
  249. self.window?.contentView?.endLoading()
  250. }
  251. }