// // KMURLToPDFWindowController.swift // PDF Master // // Created by wanjun on 2023/1/29. // import Cocoa class KMURLToPDFWindowController: NSWindowController { @IBOutlet weak var title: NSTextField! @IBOutlet weak var urlView: NSView! @IBOutlet weak var urlTextFieldBox: NSBox! @IBOutlet weak var fileView: NSView! @IBOutlet weak var fileTextFieldBox: NSBox! @IBOutlet weak var fileButtonBox: NSBox! @IBOutlet weak var pageConfigurationView: NSView! @IBOutlet weak var pageConfigurationLabel: NSTextField! @IBOutlet weak var pageSizeLabel: NSTextField! @IBOutlet weak var pageSizePopUpButton: KMPopUpButton! @IBOutlet weak var spacingLanel: NSTextField! @IBOutlet weak var stepperBox: NSBox! @IBOutlet weak var mmLabel: NSTextField! @IBOutlet weak var cancelView: NSView! @IBOutlet weak var openView: NSView! var urlButtonVC: KMDesignButton! var urlTextfieldVC: KMTextfieldVC! var fileButtonVC: KMDesignButton! var fileTextfieldVC: KMTextfieldVC! var openFileVC: KMDesignButton! var stepperVC: KMStepperView! var cancelVC: KMDesignButton! var openVC: KMDesignButton! var filePath: String = "" var gap: Float = 0.0 var pageSize: CGSize = NSZeroSize override func windowDidLoad() { super.windowDidLoad() // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. urlButtonVC = KMDesignButton.init(withType: .RadioButton) urlTextfieldVC = KMTextfieldVC.init(nibName: "KMTextfieldVC", bundle: nil) fileButtonVC = KMDesignButton.init(withType: .RadioButton) fileTextfieldVC = KMTextfieldVC.init(nibName: "KMTextfieldVC", bundle: nil) openFileVC = KMDesignButton.init(withType: .Image) stepperVC = KMStepperView.init(nibName: "KMStepperView", bundle: nil) cancelVC = KMDesignButton.init(withType: .Text) openVC = KMDesignButton.init(withType: .Text) pageSizePopUpButton.removeAllItems() self.initializeUI() self.initLocalization() refreshUI() } // MARK: Init func initializeUI() { self.window?.backgroundColor = .white title.font = NSFont.boldSystemFont(ofSize: 16.0) title.textColor = NSColor(red: 37/255.0, green: 41/255.0, blue: 41/255.0, alpha: 1.0) urlView.addSubview(urlButtonVC.view) urlButtonVC.image = NSImage(named: "KMRadioButtonUnSelect")! urlButtonVC.target = self urlButtonVC.action = #selector(buttonChangeType) urlButtonVC.state = .Checked // let itemSpacing = KMDesignToken.shared.itemSpacing(withToken: "checkbox-radio.bg.def") // urlButtonVC.radio_label = itemSpacing.stringToCGFloat() // let typography = KMDesignToken.shared.typography(withToken: "checkbox-radio.mac-text.def") // let fill = KMDesignToken.shared.fill(withToken: "checkbox-radio.mac-text.def") urlTextFieldBox.contentView = urlTextfieldVC.view urlTextfieldVC.borderColor = .gray urlTextfieldVC.borderWidth = 1.0 urlTextfieldVC.cornerRadius = 4.0 urlTextfieldVC.width = 400.0 urlTextfieldVC.height = 32.0 urlTextfieldVC.editable = true urlTextfieldVC.becomeFirstResponder = true urlTextfieldVC.delete = self fileView.addSubview(fileButtonVC.view) fileButtonVC.image = NSImage(named: "KMRadioButtonUnSelect")! fileButtonVC.target = self fileButtonVC.action = #selector(buttonChangeType) fileTextFieldBox.contentView = fileTextfieldVC.view fileTextfieldVC.borderColor = .gray fileTextfieldVC.borderWidth = 1.0 fileTextfieldVC.cornerRadius = 4.0 fileTextfieldVC.width = 360.0 fileTextfieldVC.height = 32.0 fileTextfieldVC.editable = false fileTextfieldVC.delete = self fileButtonBox.contentView = openFileVC.view openFileVC.image = NSImage(named: "KMFileIcon")! openFileVC.borderColor = .gray openFileVC.borderWidth = 1.0 openFileVC.cornerRadius = 4.0 openFileVC.width = 32.0 openFileVC.height = 32.0 openFileVC.target = self openFileVC.action = #selector(selectFileAction) openFileVC.enabled = false stepperBox.contentView = stepperVC.view stepperVC.borderColor = .gray stepperVC.borderWidth = 1.0 stepperVC.cornerRadius = 4.0 stepperVC.width = 72.0 stepperVC.height = 32.0 stepperVC.delete = self stepperVC.enabled = false cancelView.addSubview(cancelVC.view) cancelVC.borderColor = .gray cancelVC.borderWidth = 1.0 cancelVC.cornerRadius = 4.0 cancelVC.width = 80.0 cancelVC.height = 32.0 cancelVC.textColor = .black cancelVC.target = self cancelVC.action = #selector(cancelAction) openView.addSubview(openVC.view) openVC.borderColor = .gray openVC.borderWidth = 1.0 openVC.cornerRadius = 4.0 openVC.width = 80.0 openVC.height = 32.0 openVC.background = NSColor(red: 23/255.0, green: 112/255.0, blue: 244/255.0, alpha: 1.0) openVC.textColor = .white openVC.target = self openVC.action = #selector(startAction) } func initLocalization() { title.stringValue = NSLocalizedString("New From Web Page", comment: "") urlButtonVC.stringValue = NSLocalizedString("URL", comment: "") urlTextfieldVC.placeholderString = NSLocalizedString("https://pdfreaderpro.com", comment: "") fileButtonVC.stringValue = NSLocalizedString("File", comment: "") fileTextfieldVC.placeholderString = NSLocalizedString("Select Files(.html,.webarchive)", comment: "") pageConfigurationLabel.stringValue = NSLocalizedString("Page Configuration", comment: "") pageSizeLabel.stringValue = NSLocalizedString("Page Size:", comment: "") spacingLanel.stringValue = NSLocalizedString("Spacing:", comment: "") mmLabel.stringValue = NSLocalizedString("mm:", comment: "") cancelVC.stringValue = NSLocalizedString("Cancel", comment: "") openVC.stringValue = NSLocalizedString("Open", comment: "") pageSizePopUpButton.addItems(withTitles: [NSLocalizedString("Automatic resizing", comment: ""), "4A0 1682 × 2378 mm", "2A0 1189 × 1682 mm", "A0 841 × 1189 mm", "A1 594 × 841 mm", "A2 420 × 594 mm", "A3 297 × 420 mm", "A4 210 × 297 mm", "A5 148 × 210 mm", "A6 105 × 148 mm", "A7 74 × 105 mm", "A8 52 × 74 mm", "A9 37 × 52 mm", "A10 26 × 37 mm"]) } func refreshUI() -> Void { urlButtonVC.updateUI() urlTextfieldVC.updateUI() fileButtonVC.updateUI() fileTextfieldVC.updateUI() openFileVC.updateUI() stepperVC.updateUI() cancelVC.updateUI() openVC.updateUI() } // MARK: Private Methods func kUrlToPDFFolderPath() -> String { // let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last // let append2 = searchPath!.stringByAppendingPathComponent("WebPage") let append1: NSString = "WebPage" let append2 = append1.kUrlToPDFFolderPath() return append2! } @objc func beginSheetModal(for window: NSWindow, completionHandler handler: ((_ filePath: String) -> Void)?) { window.beginSheet(self.window!) { [weak self] returnCode in if self != nil { handler!(self!.filePath) } } } func formatFloat(_ f: Float) -> String { if (fmodf(f, 1) == 0) { //如果有一位小数点 return String(format: "%.0f",f) } else if (fmodf(f*10, 1) == 0) { //如果有两位小数点 return String(format: "%.1f",f) } else { return String(format: "%.2f",f) } } // MARK: Action @objc func buttonChangeType(_ sender: NSButton) -> Void { if (sender == urlButtonVC.button) { if urlButtonVC.state == .Norm { urlButtonVC.image = NSImage(named: "KMRadioButtonSelect")! urlButtonVC.state = .Checked fileButtonVC.image = NSImage(named: "KMRadioButtonUnSelect")! fileButtonVC.state = .Norm urlTextfieldVC.editable = true urlTextfieldVC.becomeFirstResponder = true fileTextfieldVC.stringValue = "" openFileVC.enabled = false if urlTextfieldVC.stringValue.count > 0 { // openVC.enabled = true } else { // openVC.enabled = false } } } else if (sender == fileButtonVC.button) { if fileButtonVC.state == .Norm { urlButtonVC.image = NSImage(named: "KMRadioButtonUnSelect")! urlButtonVC.state = .Norm fileButtonVC.image = NSImage(named: "KMRadioButtonSelect")! fileButtonVC.state = .Checked urlTextfieldVC.editable = false urlTextfieldVC.stringValue = "" openFileVC.enabled = true if fileTextfieldVC.stringValue.count > 0 { // openVC.enabled = true } else { // openVC.enabled = false } } } refreshUI() } @objc func selectFileAction(_ sender: NSButton) -> Void { urlButtonVC.state = .Norm fileButtonVC.state = .Checked urlTextfieldVC.editable = false urlTextfieldVC.stringValue = "" // openVC.enabled = false let openPanel = NSOpenPanel() openPanel.canChooseFiles = true openPanel.canChooseDirectories = false openPanel.allowsMultipleSelection = false openPanel.allowedFileTypes = ["HTML","html","webarchive","htm"] openPanel.beginSheetModal(for: self.window!) { [self] response in if response == .OK { fileTextfieldVC.stringValue = openPanel.url!.path // openVC.enabled = true refreshUI() } } } @objc func cancelAction(_ sender: NSButton) -> Void { if (sender == cancelVC.button) { NSApp.mainWindow!.endSheet(self.window!) self.window?.orderOut(self) } } @objc func startAction(_ sender: NSButton) -> Void { if (sender == openVC.button) { if FileManager.default.fileExists(atPath: kUrlToPDFFolderPath()) { try? FileManager.default.createDirectory(atPath: kUrlToPDFFolderPath(), withIntermediateDirectories: false) } var url: URL var fileName: String = "" if urlButtonVC.state == .Checked { let urlstring = urlTextfieldVC.stringValue var tUrl = URL(string: urlstring) if tUrl?.scheme == nil { tUrl = URL(string: String(format: "http://%@", urlstring)) } url = tUrl! } else { url = URL(fileURLWithPath: fileTextfieldVC.stringValue) fileName = fileTextfieldVC.stringValue.lastPathComponent.deletingPathExtension.lastPathComponent } var string = stepperVC.text let unitScanle: Float = (595.0/21.0) * 2.54 if Float(string)! <= 0 { stepperVC.text = "0" string = "0" } else if Float(string)! * unitScanle > Float(pageSize.width)/2 { let maxF = pageSize.width/(string.stringToCGFloat() * 2) stepperVC.text = String(format: "%f",maxF) string = String(format: "%f",maxF) } gap = Float(formatFloat(Float(string.stringToCGFloat()) * unitScanle))! // if url != nil { // let convert = KMConvertURLToPDF.shareInstance() // convert.fileName = fileName // convert.convertUrl(toPDF: [url], toPath: kUrlToPDFFolderPath(), pageSize: pageSize, gap: CGFloat(gap)) { value in // // } completionHandler: { successArray, failArray in // if failArray.count < 1 { // let filePath: String = successArray.first as! String // if FileManager.default.fileExists(atPath: filePath) { // self.filePath = filePath // NSApp.mainWindow!.endSheet(self.window!) // self.window?.orderOut(self) // } // } else { // let alert = NSAlert() // alert.alertStyle = .critical // alert.informativeText = NSLocalizedString("Conversion Failed", comment: "") // alert.messageText = NSLocalizedString("", comment: "") // alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) // alert.runModal() // } // } // } } } @IBAction func comboBoxItemClick_PageSize(_ sender: KMPopUpButton) { switch pageSizePopUpButton.indexOfSelectedItem { case 0: pageSize = CGSize(width: 0, height: 0) break case 1: pageSize = CGSize(width: 4760, height: 6736) break case 2: pageSize = CGSize(width: 3368, height: 4760) break case 3: pageSize = CGSize(width: 2380, height: 3368) break case 4: pageSize = CGSize(width: 1684, height: 2380) break case 5: pageSize = CGSize(width: 1190, height: 1684) break case 6: pageSize = CGSize(width: 842, height: 1190) break case 7: pageSize = CGSize(width: 595, height: 842) break case 8: pageSize = CGSize(width: 420, height: 595) break case 9: pageSize = CGSize(width: 298, height: 420) break default: pageSize = CGSize(width: 210, height: 298) break } if (pageSizePopUpButton.indexOfSelectedItem != 0) { stepperVC.text = "0" stepperVC.enabled = true } else { stepperVC.text = "0" stepperVC.enabled = false } refreshUI() } } extension KMURLToPDFWindowController: KMStepperViewDelegate { func km_controlTextDidEndEditing(_ obj: KMStepperView) { if obj == stepperVC { var string: String = obj.text let unitScanle: Float = (595.0/21.0) * 2.54 if (string.stringToCGFloat() <= 0) { stepperVC.text = "0" string = "0" } else if (Float(string.stringToCGFloat()) * unitScanle > Float(pageSize.width)/2) { let maxF = pageSize.width / (string.stringToCGFloat() * 2) stepperVC.text = String(format: "%f",maxF) string = String(format: "%f",maxF) } gap = Float(formatFloat(Float(string.stringToCGFloat()) * unitScanle))! refreshUI() } } func km_controlTextDidChange(_ obj: KMStepperView) { if obj == stepperVC { if (urlButtonVC.state == .Checked) { if (urlTextfieldVC.stringValue.count > 0) { // openVC.enabled = true } else { // openVC.enabled = false } } else { if (fileTextfieldVC.stringValue.count > 0) { // openVC.enabled = true } else { // openVC.enabled = false } } } refreshUI() } func km_controlClick(_ obj: KMStepperView) { if (obj.text.stringToCGFloat() < 0) { stepperVC.text = "0" } let unitScanle = (595.0/21.0) * 2.54 let formatter = formatFloat(Float(obj.text.stringToCGFloat() * unitScanle)) gap = Float(formatter)! refreshUI() } } extension KMURLToPDFWindowController: KMTextfieldVCDelegate { func km_controlTextDidEndEditing(_ obj: KMTextfieldVC) { } func km_controlTextDidChange(_ obj: KMTextfieldVC) { if urlButtonVC.state == .Checked { if urlTextfieldVC.stringValue.count > 0 { openVC.enabled = true refreshUI() } else { openVC.enabled = false refreshUI() } } else { if fileTextfieldVC.stringValue.count > 0 { openVC.enabled = true refreshUI() } else { openVC.enabled = false refreshUI() } } } }