123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- //
- // 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 selectLabel: NSTextField!
- @IBOutlet weak var qulityMatrix: NSMatrix!
-
- @IBOutlet weak var lowLabel: NSTextField!
- @IBOutlet weak var hightLabel: NSTextField!
- @IBOutlet weak var qualitySlider: KMSlider!
-
- @IBOutlet weak var cancelButton: NSButton!
- @IBOutlet weak var compressButton: NSButton!
- @IBOutlet weak var batchButton: NSButton!
-
- var cancelAction: KMCompressViewCancelAction?
- var batchAction: KMCompressViewBatchAction?
- var compressAction: KMCompressViewCompressAction?
-
- var documentURL: URL? {
- didSet {
- self.reloadData()
- }
- }
- var password: String = ""
- // private var datas: [String] = []
- // var selectIndex = 2
- //
- // var limit = true
- var oriDocumentUrl: URL?
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
- override func setup() {
- qulityMatrix.selectCell(atRow: 2, column: 0)
-
- lowLabel.textColor = KMAppearance.Layout.h0Color()
- hightLabel.textColor = KMAppearance.Layout.h0Color()
-
- previewBox.fillColor = NSColor.black.withAlphaComponent(0.04)
- previewBox.cornerRadius = 4.0
-
- progress.doubleValue = 0;
- progress.isHidden = true
- progressSizeLabel.isHidden = true
- }
-
- override func updateLanguage() {
- selectLabel.stringValue = NSLocalizedString("Optimization Options", comment: "")
- lowLabel.stringValue = NSLocalizedString("Low Quality", comment: "")
- hightLabel.stringValue = NSLocalizedString("High Quality", comment: "")
- cancelButton.title = NSLocalizedString("Cancel", comment: "")
- compressButton.title = NSLocalizedString("Compress", comment: "")
- batchButton.title = NSLocalizedString("Batch", comment: "")
-
- for cell in qulityMatrix.cells {
- var title = cell.title
- if title == "Other" {
- title = "Customize"
- }
- cell.title = NSLocalizedString(title, 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)
- // let image: NSImage = NSImage.previewForFile(path: url!, ofSize: fileImageView.bounds.size, asIcon: true) ?? NSImage()
- fileImageView.image = image;
- }
- }
-
- self.updateQulityMatrixState()
- }
-
- func updateQulityMatrixState() {
- if qulityMatrix.selectedRow == 4 {
- hightLabel.isEnabled = true
- lowLabel.isEnabled = true
- qualitySlider.isEnabled = true
- lowLabel.alphaValue = 1
- hightLabel.alphaValue = 1
- qualitySlider.tipEnabled = true
- } else {
- hightLabel.isEnabled = false
- lowLabel.isEnabled = false
- qualitySlider.isEnabled = false
- lowLabel.alphaValue = 0.4
- hightLabel.alphaValue = 0.4
- qualitySlider.tipEnabled = false
- }
- }
-
- 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
- if (qulityMatrix.selectedRow == 0) {
- compressVale = 120
- } else if (qulityMatrix.selectedRow == 1) {
- compressVale = 90
- } else if (qulityMatrix.selectedRow == 2) {
- compressVale = 60
- } else if (qulityMatrix.selectedRow == 3) {
- compressVale = 30
- } else {
- compressVale = qualitySlider.integerValue
- }
-
- callBack(self, compressVale)
- }
-
- @IBAction func qulityMatrixAction(_ sender: Any) {
- self.updateQulityMatrixState()
- }
- }
|