KMCompressView.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // KMCompressView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/8.
  6. //
  7. import Cocoa
  8. typealias KMCompressViewCancelAction = (_ view: KMCompressView ) -> Void
  9. typealias KMCompressViewBatchAction = (_ view: KMCompressView ) -> Void
  10. typealias KMCompressViewCompressAction = (_ view: KMCompressView, _ compressValue: Int) -> Void
  11. class KMCompressView: KMBaseXibView {
  12. @IBOutlet weak var previewBox: KMBox!
  13. @IBOutlet weak var titleLabel: NSTextField!
  14. @IBOutlet weak var sizeLabel: NSTextField!
  15. @IBOutlet weak var finalSizeLabel: NSTextField!
  16. @IBOutlet weak var fileImageView: NSImageView!
  17. @IBOutlet weak var progressSizeLabel: NSTextField!
  18. @IBOutlet weak var progress: NSProgressIndicator!
  19. @IBOutlet weak var selectLabel: NSTextField!
  20. @IBOutlet weak var qulityMatrix: NSMatrix!
  21. @IBOutlet weak var lowLabel: NSTextField!
  22. @IBOutlet weak var hightLabel: NSTextField!
  23. @IBOutlet weak var qualitySlider: KMSlider!
  24. @IBOutlet weak var cancelButton: NSButton!
  25. @IBOutlet weak var compressButton: NSButton!
  26. @IBOutlet weak var batchButton: NSButton!
  27. var cancelAction: KMCompressViewCancelAction?
  28. var batchAction: KMCompressViewBatchAction?
  29. var compressAction: KMCompressViewCompressAction?
  30. var documentURL: URL? {
  31. didSet {
  32. self.reloadData()
  33. }
  34. }
  35. var password: String = ""
  36. // private var datas: [String] = []
  37. // var selectIndex = 2
  38. //
  39. // var limit = true
  40. var oriDocumentUrl: URL?
  41. override func draw(_ dirtyRect: NSRect) {
  42. super.draw(dirtyRect)
  43. // Drawing code here.
  44. }
  45. override func setup() {
  46. qulityMatrix.selectCell(atRow: 2, column: 0)
  47. lowLabel.textColor = KMAppearance.Layout.h0Color()
  48. hightLabel.textColor = KMAppearance.Layout.h0Color()
  49. previewBox.fillColor = NSColor.black.withAlphaComponent(0.04)
  50. previewBox.cornerRadius = 4.0
  51. progress.doubleValue = 0;
  52. progress.isHidden = true
  53. progressSizeLabel.isHidden = true
  54. }
  55. override func updateLanguage() {
  56. selectLabel.stringValue = NSLocalizedString("Optimization Options", comment: "")
  57. lowLabel.stringValue = NSLocalizedString("Low Quality", comment: "")
  58. hightLabel.stringValue = NSLocalizedString("High Quality", comment: "")
  59. cancelButton.title = NSLocalizedString("Cancel", comment: "")
  60. compressButton.title = NSLocalizedString("Compress", comment: "")
  61. batchButton.title = NSLocalizedString("Batch", comment: "")
  62. for cell in qulityMatrix.cells {
  63. var title = cell.title
  64. if title == "Other" {
  65. title = "Customize"
  66. }
  67. cell.title = NSLocalizedString(title, comment: "")
  68. }
  69. }
  70. override func reloadData() {
  71. let url = documentURL ?? URL(string: "")
  72. if url != nil {
  73. let filePath = url!.path
  74. let attrib = try? FileManager.default.attributesOfItem(atPath: filePath)
  75. if attrib != nil {
  76. let sizeFloat: Float = attrib![FileAttributeKey(rawValue: "NSFileSize")] as? Float ?? 0.0
  77. sizeLabel.stringValue = self.fileSizeString(sizeFloat)
  78. titleLabel.stringValue = filePath.lastPathComponent
  79. finalSizeLabel.isHidden = true
  80. let pdfDocument = CPDFDocument(url: url)
  81. pdfDocument?.unlock(withPassword: self.password)
  82. let page = pdfDocument?.page(at: 0) ?? CPDFPage()
  83. let image = page.thumbnail(of: page.size)
  84. // let image: NSImage = NSImage.previewForFile(path: url!, ofSize: fileImageView.bounds.size, asIcon: true) ?? NSImage()
  85. fileImageView.image = image;
  86. }
  87. }
  88. self.updateQulityMatrixState()
  89. }
  90. func updateQulityMatrixState() {
  91. if qulityMatrix.selectedRow == 4 {
  92. hightLabel.isEnabled = true
  93. lowLabel.isEnabled = true
  94. qualitySlider.isEnabled = true
  95. lowLabel.alphaValue = 1
  96. hightLabel.alphaValue = 1
  97. qualitySlider.tipEnabled = true
  98. } else {
  99. hightLabel.isEnabled = false
  100. lowLabel.isEnabled = false
  101. qualitySlider.isEnabled = false
  102. lowLabel.alphaValue = 0.4
  103. hightLabel.alphaValue = 0.4
  104. qualitySlider.tipEnabled = false
  105. }
  106. }
  107. func fileSizeString(_ fSize: Float) -> String {
  108. let fileSize = fSize / 1024
  109. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  110. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  111. return String(format: "%0.1f %@", size, unit)
  112. }
  113. }
  114. //MARK: Action
  115. extension KMCompressView {
  116. @IBAction func batchButtonAction(_ sender: Any) {
  117. guard let callBack = batchAction else { return }
  118. callBack(self)
  119. }
  120. @IBAction func cancelButtonAction(_ sender: Any) {
  121. guard let callBack = cancelAction else { return }
  122. callBack(self)
  123. }
  124. @IBAction func compressButtonAction(_ sender: Any) {
  125. guard let callBack = compressAction else { return }
  126. var compressVale = 60
  127. if (qulityMatrix.selectedRow == 0) {
  128. compressVale = 120
  129. } else if (qulityMatrix.selectedRow == 1) {
  130. compressVale = 90
  131. } else if (qulityMatrix.selectedRow == 2) {
  132. compressVale = 60
  133. } else if (qulityMatrix.selectedRow == 3) {
  134. compressVale = 30
  135. } else {
  136. compressVale = qualitySlider.integerValue
  137. }
  138. callBack(self, compressVale)
  139. }
  140. @IBAction func qulityMatrixAction(_ sender: Any) {
  141. self.updateQulityMatrixState()
  142. }
  143. }