KMCompressView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. cell.title = NSLocalizedString(cell.title, comment: "")
  64. }
  65. }
  66. override func reloadData() {
  67. let url = documentURL ?? URL(string: "")
  68. if url != nil {
  69. let filePath = url!.path
  70. let attrib = try? FileManager.default.attributesOfItem(atPath: filePath)
  71. if attrib != nil {
  72. let sizeFloat: Float = attrib![FileAttributeKey(rawValue: "NSFileSize")] as? Float ?? 0.0
  73. sizeLabel.stringValue = self.fileSizeString(sizeFloat)
  74. titleLabel.stringValue = filePath.lastPathComponent
  75. finalSizeLabel.isHidden = true
  76. let pdfDocument = CPDFDocument(url: url)
  77. pdfDocument?.unlock(withPassword: self.password)
  78. let page = pdfDocument?.page(at: 0) ?? CPDFPage()
  79. let image = page.thumbnail(of: page.size)
  80. // let image: NSImage = NSImage.previewForFile(path: url!, ofSize: fileImageView.bounds.size, asIcon: true) ?? NSImage()
  81. fileImageView.image = image;
  82. }
  83. }
  84. self.updateQulityMatrixState()
  85. }
  86. func updateQulityMatrixState() {
  87. if qulityMatrix.selectedRow == 4 {
  88. hightLabel.isEnabled = true
  89. lowLabel.isEnabled = true
  90. qualitySlider.isEnabled = true
  91. lowLabel.alphaValue = 1
  92. hightLabel.alphaValue = 1
  93. qualitySlider.tipEnabled = true
  94. } else {
  95. hightLabel.isEnabled = false
  96. lowLabel.isEnabled = false
  97. qualitySlider.isEnabled = false
  98. lowLabel.alphaValue = 0.4
  99. hightLabel.alphaValue = 0.4
  100. qualitySlider.tipEnabled = false
  101. }
  102. }
  103. func fileSizeString(_ fSize: Float) -> String {
  104. let fileSize = fSize / 1024
  105. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  106. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  107. return String(format: "%0.1f %@", size, unit)
  108. }
  109. }
  110. //MARK: Action
  111. extension KMCompressView {
  112. @IBAction func batchButtonAction(_ sender: Any) {
  113. guard let callBack = batchAction else { return }
  114. callBack(self)
  115. }
  116. @IBAction func cancelButtonAction(_ sender: Any) {
  117. guard let callBack = cancelAction else { return }
  118. callBack(self)
  119. }
  120. @IBAction func compressButtonAction(_ sender: Any) {
  121. guard let callBack = compressAction else { return }
  122. var compressVale = 60
  123. if (qulityMatrix.selectedRow == 0) {
  124. compressVale = 120
  125. } else if (qulityMatrix.selectedRow == 1) {
  126. compressVale = 90
  127. } else if (qulityMatrix.selectedRow == 2) {
  128. compressVale = 60
  129. } else if (qulityMatrix.selectedRow == 3) {
  130. compressVale = 30
  131. } else {
  132. compressVale = qualitySlider.integerValue
  133. }
  134. callBack(self, compressVale)
  135. }
  136. @IBAction func qulityMatrixAction(_ sender: Any) {
  137. self.updateQulityMatrixState()
  138. }
  139. }