123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- //
- // KMCompressView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/11/8.
- //
- import Cocoa
- typealias KMCompressViewCancelAction = (_ view: KMCompressView ) -> Void
- typealias KMCompressViewBatchAction = (_ view: KMCompressView ) -> Void
- typealias KMCompressViewCompressAction = (_ view: KMCompressView, _ compressValue: Int) -> Void
- class KMCompressView: KMBaseXibView {
- @IBOutlet weak var previewBox: KMBox!
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var sizeLabel: NSTextField!
- @IBOutlet weak var finalSizeLabel: NSTextField!
- @IBOutlet weak var fileImageView: NSImageView!
- @IBOutlet weak var progressSizeLabel: NSTextField!
- @IBOutlet weak var progress: NSProgressIndicator!
- @IBOutlet weak var cancelButton: NSButton!
- @IBOutlet weak var compressButton: NSButton!
- @IBOutlet weak var batchButton: NSButton!
- @IBOutlet weak var modelsLabel: NSTextField!
-
- var cancelAction: KMCompressViewCancelAction?
- var batchAction: KMCompressViewBatchAction?
- var compressAction: KMCompressViewCompressAction?
-
- var documentURL: URL? {
- didSet {
- self.reloadData()
- }
- }
- var password: String = ""
- var oriDocumentUrl: URL?
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
- override func setup() {
- self.modelsLabel.textColor = KMAppearance.Layout.h1Color()
- self.titleLabel.textColor = KMAppearance.Layout.h1Color()
- self.sizeLabel.textColor = KMAppearance.Layout.h2Color()
-
- self.compressButton.setTitleColor(KMAppearance.Layout.h1Color())
- self.cancelButton.setTitleColor(KMAppearance.Layout.h1Color())
- self.batchButton.setTitleColor(KMAppearance.Layout.h1Color())
-
-
- previewBox.fillColor = NSColor.black.withAlphaComponent(0.04)
- previewBox.cornerRadius = 4.0
-
- progress.doubleValue = 0;
- progress.isHidden = true
- progressSizeLabel.isHidden = true
- }
-
- override func updateLanguage() {
- cancelButton.title = NSLocalizedString("Cancel", comment: "")
- compressButton.title = NSLocalizedString("Compress", comment: "")
- batchButton.title = NSLocalizedString("Batch", comment: "")
- }
-
- override func reloadData() {
- let url = documentURL ?? URL(string: "")
- if url != nil {
- let filePath = url!.path
- let attrib = try? FileManager.default.attributesOfItem(atPath: filePath)
- if attrib != nil {
- let sizeFloat: Float = attrib![FileAttributeKey(rawValue: "NSFileSize")] as? Float ?? 0.0
- sizeLabel.stringValue = self.fileSizeString(sizeFloat)
-
- titleLabel.stringValue = filePath.lastPathComponent
- finalSizeLabel.isHidden = true
-
- let pdfDocument = CPDFDocument(url: url)
- pdfDocument?.unlock(withPassword: self.password)
- let page = pdfDocument?.page(at: 0) ?? CPDFPage()
- let image = page.thumbnail(of: page.size)
- fileImageView.image = image;
- }
- }
- }
-
- func fileSizeString(_ fSize: Float) -> String {
- let fileSize = fSize / 1024
- let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
- let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
- return String(format: "%0.1f %@", size, unit)
- }
- }
- //MARK: Action
- extension KMCompressView {
- @IBAction func batchButtonAction(_ sender: Any) {
- guard let callBack = batchAction else { return }
-
- callBack(self)
- }
-
- @IBAction func cancelButtonAction(_ sender: Any) {
- guard let callBack = cancelAction else { return }
-
- callBack(self)
- }
-
- @IBAction func compressButtonAction(_ sender: Any) {
- guard let callBack = compressAction else { return }
-
- var compressVale = 60
- callBack(self, compressVale)
- }
- }
|