KMCompressView.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // KMCompressView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMCompressViewCancelAction = (_ view: KMCompressView ) -> Void
  10. typealias KMCompressViewBatchAction = (_ view: KMCompressView ) -> Void
  11. typealias KMCompressViewCompressAction = (_ view: KMCompressView, _ model: KMCompressSettingModel, _ showView: NSView?) -> Void
  12. class KMCompressView: BaseXibView {
  13. @IBOutlet weak var compressTitleLabel: NSTextField!
  14. @IBOutlet weak var previewBox: KMBox!
  15. @IBOutlet weak var titleLabel: NSTextField!
  16. @IBOutlet weak var sizeLabel: NSTextField!
  17. @IBOutlet weak var finalSizeLabel: NSTextField!
  18. @IBOutlet weak var fileImageView: NSImageView!
  19. @IBOutlet weak var progressSizeLabel: NSTextField!
  20. @IBOutlet weak var progress: NSProgressIndicator!
  21. @IBOutlet weak var modesLabel: NSTextField!
  22. @IBOutlet weak var compressContentView: KMCompressContentView!
  23. @IBOutlet weak var compressButton: ComponentButton!
  24. @IBOutlet weak var cancelButton: ComponentButton!
  25. @IBOutlet weak var batchButton: ComponentButton!
  26. var cancelAction: KMCompressViewCancelAction?
  27. var batchAction: KMCompressViewBatchAction?
  28. var compressAction: KMCompressViewCompressAction?
  29. var model: KMCompressSettingModel = KMCompressSettingModel(modelsType: .standard)
  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. updateUI()
  47. updateUILanguage()
  48. }
  49. func updateUI() {
  50. previewBox.fillColor = NSColor.black.withAlphaComponent(0.04)
  51. previewBox.cornerRadius = 4.0
  52. progress.doubleValue = 0;
  53. progress.isHidden = true
  54. progressSizeLabel.isHidden = true
  55. self.compressContentView.selectTypeAction = {[unowned self] view, tModel in
  56. self.model = tModel
  57. }
  58. compressContentView.compressAction = {[unowned self] view, tModel, showView in
  59. guard let callBack = compressAction else { return }
  60. callBack(self, self.model, showView)
  61. }
  62. compressButton.properties = ComponentButtonProperty(type: .primary, size: .xs, buttonText: KMLocalizedString("Compress"), keepPressState: false)
  63. compressButton.setTarget(self, action: #selector(compressButtonAction(_:)))
  64. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .xs, buttonText: KMLocalizedString("Cancel"), keepPressState: false)
  65. cancelButton.setTarget(self, action: #selector(cancelButtonAction(_:)))
  66. batchButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .xs, buttonText: KMLocalizedString("Batch"), keepPressState: false)
  67. batchButton.setTarget(self, action: #selector(batchButtonAction(_:)))
  68. }
  69. override func updateUIThemeColor() {
  70. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label")
  71. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular")
  72. sizeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label")
  73. sizeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular")
  74. compressTitleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  75. compressTitleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  76. modesLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  77. modesLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  78. }
  79. override func updateUILanguage() {
  80. }
  81. func reloadData() {
  82. if KMCompressSettingModel.isSaveCustomData() {
  83. self.compressContentView.model = KMCompressSettingModel(modelsType: .custom)
  84. } else {
  85. self.compressContentView.model = KMCompressSettingModel(modelsType: .standard)
  86. }
  87. let url = documentURL ?? URL(string: "")
  88. if url != nil {
  89. let filePath = url!.path
  90. let attrib = try? FileManager.default.attributesOfItem(atPath: filePath)
  91. if attrib != nil {
  92. let sizeFloat: Float = attrib![FileAttributeKey(rawValue: "NSFileSize")] as? Float ?? 0.0
  93. sizeLabel.stringValue = self.fileSizeString(sizeFloat)
  94. titleLabel.stringValue = filePath.lastPathComponent
  95. finalSizeLabel.isHidden = true
  96. let pdfDocument = CPDFDocument(url: url)
  97. pdfDocument?.unlock(withPassword: self.password)
  98. let page = pdfDocument?.page(at: 0) ?? CPDFPage()
  99. let image = page.thumbnail(of: page.size)
  100. // let image: NSImage = NSImage.previewForFile(path: url!, ofSize: fileImageView.bounds.size, asIcon: true) ?? NSImage()
  101. fileImageView.image = image;
  102. }
  103. }
  104. }
  105. func fileSizeString(_ fSize: Float) -> String {
  106. let fileSize = fSize / 1024
  107. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  108. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  109. return String(format: "%0.1f %@", size, unit)
  110. }
  111. }
  112. //MARK: Action
  113. extension KMCompressView {
  114. @IBAction func batchButtonAction(_ sender: Any) {
  115. guard let callBack = batchAction else { return }
  116. callBack(self)
  117. }
  118. @IBAction func cancelButtonAction(_ sender: Any) {
  119. guard let callBack = cancelAction else { return }
  120. callBack(self)
  121. }
  122. @IBAction func compressButtonAction(_ sender: Any) {
  123. guard let callBack = compressAction else { return }
  124. callBack(self, self.model, nil)
  125. }
  126. }