// // KMNPDFInsertPDFWindowController.swift // PDF Reader Pro // // Created by 丁林圭 on 2024/10/22. // import Cocoa import KMComponentLibrary class KMNPDFInsertPDFWindowController: NSWindowController { private var parentWindow: NSWindow? private var handler: ((String?) -> Void)! @IBOutlet var titleLabel: NSTextField! @IBOutlet var fileLabel: NSTextField! @IBOutlet var fileNameInput: ComponentInputWithAddon! @IBOutlet var selectFileInput: ComponentInputAddon! @IBOutlet var pageRangeLabel: NSTextField! @IBOutlet var pageRangeSelect: ComponentSelect! @IBOutlet var positionLabel: NSTextField! @IBOutlet var firstRadioButton: ComponentRadio! @IBOutlet var lastRadioButton: ComponentRadio! @IBOutlet var pageRadioButton: ComponentRadio! @IBOutlet var pageNumInput: ComponentInputNumber! @IBOutlet var pageCountLabel: NSTextField! @IBOutlet var positionSelect: ComponentSelect! @IBOutlet var cancelButton: ComponentButton! @IBOutlet var insertButton: ComponentButton! @IBOutlet var cancelWidthButton:NSLayoutConstraint! @IBOutlet var insertWidthButton:NSLayoutConstraint! @IBOutlet var pageRadioWidthButton:NSLayoutConstraint! private var orgDocument:CPDFDocument? private var insertDocument:CPDFDocument? deinit { KMPrint("KMSearchReplaceWindowController deinit.") } override func windowDidLoad() { super.windowDidLoad() setUpProperty() } // convenience init(_ document: CPDFDocument?) { // self.init(windowNibName: "KMNPDFInsertPDFWindowController") // orgDocument = document // } // // convenience init(_ filePath: String,password:String?) { // self.init(windowNibName: "KMNPDFInsertPDFWindowController") // let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath)) // if password != nil { // document?.unlock(withPassword: password as String?) // } // // orgDocument = document // } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setUPPageSelctProperty() { pageRangeSelect.properties = ComponentSelectProperties(size: .s, state: .normal, isDisabled: false, isError: false, leftIcon: false, placeholder: nil, errorText: nil, creatable: false, text: KMLocalizedString("All Pages", comment: "")) var menuItemString:[String] = [KMLocalizedString("All Pages"), KMLocalizedString("Odd Pages Only"), KMLocalizedString("Even Pages Only"), KMLocalizedString("e.g. 1,3-5,10")] if insertDocument?.pageCount ?? 0 <= 1 { menuItemString.removeObject(KMLocalizedString("Even Pages Only")) } var menuItemArr: [ComponentMenuitemProperty] = [] for language in menuItemString { let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: language) menuItemArr.append(itemProperty) } pageRangeSelect.updateMenuItemsArr(menuItemArr) pageRangeSelect.delegate = self } private func setUPPositionSelctProperty() { positionSelect.properties = ComponentSelectProperties(size: .s, state: .normal, isDisabled: false, isError: false, leftIcon: false, placeholder: nil, errorText: nil, creatable: false, text: KMLocalizedString("After", comment: "")) var menuItemArr: [ComponentMenuitemProperty] = [] for language in [KMLocalizedString("After"),KMLocalizedString("Before")] { let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: language) menuItemArr.append(itemProperty) } positionSelect.updateMenuItemsArr(menuItemArr) positionSelect.delegate = self } private func setUpProperty() { titleLabel.stringValue = KMLocalizedString("Insert File") titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium") titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1") fileLabel.stringValue = KMLocalizedString("Form File") fileLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium") fileLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") pageRangeLabel.stringValue = KMLocalizedString("Page Range:") pageRangeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular") pageRangeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field-colorText-filled-nor") positionLabel.stringValue = KMLocalizedString("Insertion Position") positionLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium") positionLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") firstRadioButton.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("First"), checkboxType: .normal) lastRadioButton.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("First"), checkboxType: .normal) pageRadioButton.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("Page"), checkboxType: .normal) insertButton.properties = ComponentButtonProperty(type: .primary, size: .s, state: .normal, buttonText: KMLocalizedString("Apply")) insertButton.setTarget(self, action: #selector(insertButtonClicked(_ :))) insertWidthButton.constant = insertButton.properties.propertyInfo.viewWidth cancelButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, state: .normal, buttonText: KMLocalizedString("Cancel")) cancelButton.setTarget(self, action: #selector(cancelButtonClicked(_ :))) cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth firstRadioButton.setTarget(self, action: #selector(insertPositionAction(_:))) lastRadioButton.setTarget(self, action: #selector(insertPositionAction(_:))) pageRadioButton.setTarget(self, action: #selector(insertPositionAction(_:))) pageNumInput.properties = ComponentInputNumberProperty(alignment: .left, size: .s, state: .normal, isError: false, showErrorInfo: false, isDisabled: true, showPrefix: false, showSuffix: false, minSize: 5, maxSize: 99, text: SettingsManager.sharedInstance.autoSaveMinutes) pageNumInput.inputNumberDelegate = self let inputWithAddonProperty = ComponentInputWithAddonProperty(size: .s, isDisabled:false, addonType: .text) fileNameInput.properties = inputWithAddonProperty selectFileInput.properties = ComponentInputAddonProperty(size: .s, state: .normal, addOnBefore: false, onlyRead: false, addonType: .imageWithColor, iconImage:nil,text: KMLocalizedString("Browse")) selectFileInput.setTarget(self, action: #selector(chooseURLAction(_ :))) pageCountLabel.stringValue = "/" + String(format: "%d", orgDocument?.pageCount ?? 0) } func own_beginSheetModal(for window: NSWindow?, completionHandler handler: ((String?) -> Void)?) { if window != nil { parentWindow = window window!.beginSheet(self.window!) { ModalResponse in self.handler?(nil) } } self.handler = handler } //MARK: - Action @objc func cancelButtonClicked(_ sender: NSView) { parentWindow?.endSheet(self.window!) } @objc func insertButtonClicked(_ sender: NSView) { parentWindow?.endSheet(self.window!) } @objc func insertPositionAction(_ sender: NSView) { if sender == lastRadioButton { } else if sender == firstRadioButton { } else if sender == pageRadioButton { } } @objc func chooseURLAction(_ sender: NSView) { } } extension KMNPDFInsertPDFWindowController: ComponentInputNumberDelegate { func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) { } } extension KMNPDFInsertPDFWindowController: ComponentSelectDelegate { func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) { } }