123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- //
- // KMCompressView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/11/8.
- //
- import Cocoa
- import KMComponentLibrary
- typealias KMCompressViewCancelAction = (_ view: KMCompressView ) -> Void
- typealias KMCompressViewBatchAction = (_ view: KMCompressView ) -> Void
- typealias KMCompressViewCompressAction = (_ view: KMCompressView, _ model: KMCompressSettingModel, _ showView: NSView?) -> Void
- class KMCompressView: BaseXibView {
- @IBOutlet weak var compressTitleLabel: NSTextField!
- @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 modesLabel: NSTextField!
- @IBOutlet weak var compressContentView: KMCompressContentView!
-
- @IBOutlet weak var compressButton: ComponentButton!
- @IBOutlet weak var cancelButton: ComponentButton!
- @IBOutlet weak var batchButton: ComponentButton!
-
- var cancelAction: KMCompressViewCancelAction?
- var batchAction: KMCompressViewBatchAction?
- var compressAction: KMCompressViewCompressAction?
-
- var model: KMCompressSettingModel = KMCompressSettingModel(modelsType: .standard)
-
- 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() {
- updateUI()
- updateUILanguage()
- }
-
- func updateUI() {
- previewBox.fillColor = NSColor.black.withAlphaComponent(0.04)
- previewBox.cornerRadius = 4.0
-
- progress.doubleValue = 0;
- progress.isHidden = true
- progressSizeLabel.isHidden = true
-
- self.compressContentView.selectTypeAction = {[unowned self] view, tModel in
- self.model = tModel
- }
-
- compressContentView.compressAction = {[unowned self] view, tModel, showView in
- guard let callBack = compressAction else { return }
-
-
- callBack(self, self.model, showView)
- }
-
- compressButton.properties = ComponentButtonProperty(type: .primary, size: .xs, buttonText: KMLocalizedString("Compress"), keepPressState: false)
- compressButton.setTarget(self, action: #selector(compressButtonAction(_:)))
-
- cancelButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .xs, buttonText: KMLocalizedString("Cancel"), keepPressState: false)
- cancelButton.setTarget(self, action: #selector(cancelButtonAction(_:)))
-
- batchButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .xs, buttonText: KMLocalizedString("Batch"), keepPressState: false)
- batchButton.setTarget(self, action: #selector(batchButtonAction(_:)))
- }
-
- override func updateUIThemeColor() {
- titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label")
- titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular")
-
- sizeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label")
- sizeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular")
-
- compressTitleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
- compressTitleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
-
- modesLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
- modesLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
- }
-
- override func updateUILanguage() {
- }
-
- func reloadData() {
- if KMCompressSettingModel.isSaveCustomData() {
- self.compressContentView.model = KMCompressSettingModel(modelsType: .custom)
- } else {
- self.compressContentView.model = KMCompressSettingModel(modelsType: .standard)
- }
-
- 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;
- }
- }
- }
- 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 }
-
-
- callBack(self, self.model, nil)
- }
- }
|