KMCompressView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 cancelButton: NSButton!
  20. @IBOutlet weak var compressButton: NSButton!
  21. @IBOutlet weak var batchButton: NSButton!
  22. @IBOutlet weak var modelsLabel: NSTextField!
  23. var cancelAction: KMCompressViewCancelAction?
  24. var batchAction: KMCompressViewBatchAction?
  25. var compressAction: KMCompressViewCompressAction?
  26. var documentURL: URL? {
  27. didSet {
  28. self.reloadData()
  29. }
  30. }
  31. var password: String = ""
  32. var oriDocumentUrl: URL?
  33. override func draw(_ dirtyRect: NSRect) {
  34. super.draw(dirtyRect)
  35. // Drawing code here.
  36. }
  37. override func setup() {
  38. self.modelsLabel.textColor = KMAppearance.Layout.h1Color()
  39. self.titleLabel.textColor = KMAppearance.Layout.h1Color()
  40. self.sizeLabel.textColor = KMAppearance.Layout.h2Color()
  41. self.compressButton.setTitleColor(KMAppearance.Layout.h1Color())
  42. self.cancelButton.setTitleColor(KMAppearance.Layout.h1Color())
  43. self.batchButton.setTitleColor(KMAppearance.Layout.h1Color())
  44. previewBox.fillColor = NSColor.black.withAlphaComponent(0.04)
  45. previewBox.cornerRadius = 4.0
  46. progress.doubleValue = 0;
  47. progress.isHidden = true
  48. progressSizeLabel.isHidden = true
  49. }
  50. override func updateLanguage() {
  51. cancelButton.title = NSLocalizedString("Cancel", comment: "")
  52. compressButton.title = NSLocalizedString("Compress", comment: "")
  53. batchButton.title = NSLocalizedString("Batch", comment: "")
  54. }
  55. override func reloadData() {
  56. let url = documentURL ?? URL(string: "")
  57. if url != nil {
  58. let filePath = url!.path
  59. let attrib = try? FileManager.default.attributesOfItem(atPath: filePath)
  60. if attrib != nil {
  61. let sizeFloat: Float = attrib![FileAttributeKey(rawValue: "NSFileSize")] as? Float ?? 0.0
  62. sizeLabel.stringValue = self.fileSizeString(sizeFloat)
  63. titleLabel.stringValue = filePath.lastPathComponent
  64. finalSizeLabel.isHidden = true
  65. let pdfDocument = CPDFDocument(url: url)
  66. pdfDocument?.unlock(withPassword: self.password)
  67. let page = pdfDocument?.page(at: 0) ?? CPDFPage()
  68. let image = page.thumbnail(of: page.size)
  69. fileImageView.image = image;
  70. }
  71. }
  72. }
  73. func fileSizeString(_ fSize: Float) -> String {
  74. let fileSize = fSize / 1024
  75. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  76. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  77. return String(format: "%0.1f %@", size, unit)
  78. }
  79. }
  80. //MARK: Action
  81. extension KMCompressView {
  82. @IBAction func batchButtonAction(_ sender: Any) {
  83. guard let callBack = batchAction else { return }
  84. callBack(self)
  85. }
  86. @IBAction func cancelButtonAction(_ sender: Any) {
  87. guard let callBack = cancelAction else { return }
  88. callBack(self)
  89. }
  90. @IBAction func compressButtonAction(_ sender: Any) {
  91. guard let callBack = compressAction else { return }
  92. var compressVale = 60
  93. callBack(self, compressVale)
  94. }
  95. }