// // KMPageEditSplitSettingView.swift // PDF Master // // Created by tangchao on 2023/1/12. // import Cocoa class KMPageEditSplitSettingView: KMPageEditSettingBaseView { @IBOutlet weak var typeLabel: NSTextField! @IBOutlet weak var oneFileForOnePageRadio: NSButton! @IBOutlet weak var oneFileForOnePageView: NSView! @IBOutlet weak var oneFileForOnePageTextField: NSTextField! @IBOutlet weak var oneFileForOnePageStepper: NSStepper! @IBOutlet weak var oneFileForOnePageOtherLabel: NSTextField! @IBOutlet weak var splitMuitiFileRadio: NSButton! @IBOutlet weak var splitMuitiFileView: NSView! @IBOutlet weak var splitMuitiFileStepper: NSStepper! @IBOutlet weak var splitMuitiFileTextField: NSTextField! @IBOutlet weak var splitMuitiFileOtherLabel: NSTextField! @IBOutlet weak var pageRangeRadio: NSButton! @IBOutlet weak var pageRangeComboBox: NSComboBox! @IBOutlet weak var nameLabel: NSTextField! @IBOutlet weak var nameView: NSView! @IBOutlet weak var nameViewLabel: NSTextField! @IBOutlet weak var prefixForOrigFileNameCheck: NSButton! @IBOutlet weak var tagCheck: NSButton! @IBOutlet weak var sepCheck: NSButton! @IBOutlet weak var tagView: NSView! @IBOutlet weak var tagTextField: NSTextField! @IBOutlet weak var sepView: NSView! @IBOutlet weak var sepTextView: NSTextField! var type: Int { get { -1 } set { if (newValue == 0) { self.selectRadio(radio: self.oneFileForOnePageRadio) } else if (newValue == 1) { self.selectRadio(radio: self.splitMuitiFileRadio) } else if (newValue == 2) { self.selectRadio(radio: self.pageRangeRadio) } } } override func awakeFromNib() { super.awakeFromNib() self.typeLabel.stringValue = NSLocalizedString("Splitting Method", comment: "") self.oneFileForOnePageRadio.title = NSLocalizedString("Average per", comment: "") self.oneFileForOnePageTextField.stringValue = "1" self.oneFileForOnePageStepper.minValue = 1 self.oneFileForOnePageStepper.integerValue = 1 self.oneFileForOnePageOtherLabel.stringValue = NSLocalizedString("page split into a PDF file", comment: "") self.splitMuitiFileRadio.title = NSLocalizedString("Average split into", comment: "") self.splitMuitiFileTextField.stringValue = "1" self.splitMuitiFileStepper.minValue = 1 self.splitMuitiFileStepper.integerValue = 1 self.splitMuitiFileOtherLabel.stringValue = NSLocalizedString("PDF files", comment: "") self.pageRangeRadio.title = NSLocalizedString("Split by page atmosphere", comment: "") self.pageRangeComboBox.removeAllItems() self.pageRangeComboBox.addItems(withObjectValues: ["Odd Pages","Even Pages","Customize"]) self.pageRangeComboBox.placeholderString = "e.g. 1,3-5,10" self.pageRangeComboBox.selectItem(at: 0) self.pageRangeComboBox.isEditable = false self.pageRangeComboBox.delegate = self for radio in [self.oneFileForOnePageRadio, self.splitMuitiFileRadio, self.pageRangeRadio] { radio?.target = self radio?.action = #selector(radioAction) } self.selectRadio(radio: self.oneFileForOnePageRadio) self.nameLabel.stringValue = NSLocalizedString("File Naming", comment: "") self.prefixForOrigFileNameCheck.title = NSLocalizedString("Precede the original document name", comment: "") self.tagCheck.title = NSLocalizedString("Tags", comment: "") self.sepCheck.title = NSLocalizedString("Separator", comment: "") self.tagTextField.stringValue = "part" self.sepTextView.stringValue = "-" for textfield in [self.oneFileForOnePageTextField, self.splitMuitiFileTextField, self.tagTextField, self.sepTextView] { textfield?.isEditable = true textfield?.focusRingType = .none textfield?.isBordered = false textfield?.delegate = self } self.oneFileForOnePageTextField.formatter = TextFieldFormatter() self.splitMuitiFileTextField.formatter = TextFieldFormatter() for stepper in [self.oneFileForOnePageStepper, self.splitMuitiFileStepper] { stepper?.target = self stepper?.action = #selector(stepperAction) } for view in [self.oneFileForOnePageView, self.splitMuitiFileView] { view?.wantsLayer = true view?.layer?.backgroundColor = NSColor(white: 238.0/255.0, alpha: 1.0).cgColor view?.layer?.cornerRadius = 2 } for view in [self.nameView, self.tagView, self.sepView] { view?.wantsLayer = true view?.layer?.borderWidth = 1 view?.layer?.borderColor = NSColor.black.cgColor view?.layer?.cornerRadius = 2 } for check in [self.prefixForOrigFileNameCheck, self.tagCheck, self.sepCheck] { check?.target = self check?.action = #selector(checkAction) } } @objc func radioAction(sender: NSButton) { selectRadio(radio: sender) guard let callback = self.itemClick else { return } var index: Int = 0 if (sender.isEqual(to: self.splitMuitiFileRadio)) { index = 1 } else if (sender.isEqual(to: self.pageRangeRadio)) { index = 2 } callback(1, index) } @objc func stepperAction(sender: NSStepper) { var index: Int = 2 if (self.oneFileForOnePageStepper.isEqual(to: sender)) { self.oneFileForOnePageTextField.stringValue = "\(sender.integerValue)" } else if (self.splitMuitiFileStepper.isEqual(to: sender)) { self.splitMuitiFileTextField.stringValue = "\(sender.integerValue)" index = 3 } guard let callback = self.itemClick else { return } callback(index, sender.integerValue) } @objc func checkAction(sender: NSButton) { if (self.tagCheck.state == .on) { self.tagTextField.isEnabled = true } else { self.tagTextField.isEnabled = false } if (sepCheck.state == .on) { self.sepTextView.isEnabled = true } else { self.sepTextView.isEnabled = false } var index: Int = 6 if (sender.isEqual(to: self.prefixForOrigFileNameCheck)) { index = 6 } else if (sender.isEqual(to: self.tagCheck)) { index = 7 } else if (sender.isEqual(to: self.sepCheck)) { index = 8 } guard let callback = self.itemClick else { return } callback(index, sender.state == .on) } public func setPageRangeString(_ string: String) { self.pageRangeComboBox.stringValue = string self.pageRangeComboBox.isEditable = true self.window?.makeFirstResponder(self.pageRangeComboBox) } public func updateName(_ name: String) { self.nameViewLabel.stringValue = name } // MARK: Private Methods private func selectRadio(radio: NSButton) { for item in [self.oneFileForOnePageRadio, self.splitMuitiFileRadio, self.pageRangeRadio] { if (radio.isEqual(to: item)) { item?.state = .on } else { item?.state = .off } } if (self.oneFileForOnePageRadio.state == .on) { self.oneFileForOnePageTextField.isEnabled = true self.oneFileForOnePageStepper.isEnabled = true } else { self.oneFileForOnePageTextField.isEnabled = false self.oneFileForOnePageStepper.isEnabled = false } if (self.splitMuitiFileRadio.state == .on) { self.splitMuitiFileTextField.isEnabled = true self.splitMuitiFileStepper.isEnabled = true } else { self.splitMuitiFileTextField.isEnabled = false self.splitMuitiFileStepper.isEnabled = false } if (self.pageRangeRadio.state == .on) { self.pageRangeComboBox.isEnabled = true } else { self.pageRangeComboBox.isEnabled = false } } } extension KMPageEditSplitSettingView: NSTextFieldDelegate { func controlTextDidChange(_ obj: Notification) { if (self.oneFileForOnePageTextField.isEqual(to: obj.object)) { if (self.oneFileForOnePageTextField.integerValue <= 0) { self.oneFileForOnePageTextField.stringValue = "1" self.oneFileForOnePageStepper.integerValue = 1 } else if (self.oneFileForOnePageTextField.integerValue > Int(self.oneFileForOnePageStepper.maxValue)) { self.oneFileForOnePageTextField.stringValue = "\(Int(self.oneFileForOnePageStepper.maxValue))" self.oneFileForOnePageStepper.integerValue = Int(self.oneFileForOnePageStepper.maxValue) } else { self.oneFileForOnePageStepper.integerValue = self.oneFileForOnePageTextField.integerValue } guard let callback = self.itemClick else { return } callback(2, self.oneFileForOnePageStepper.integerValue) } else if (self.splitMuitiFileTextField.isEqual(to: obj.object)) { if (self.splitMuitiFileTextField.integerValue <= 0) { self.splitMuitiFileTextField.stringValue = "1" self.splitMuitiFileStepper.integerValue = 1 } else if (self.splitMuitiFileTextField.integerValue > Int(self.splitMuitiFileStepper.maxValue)) { self.splitMuitiFileTextField.stringValue = "\(Int(self.splitMuitiFileStepper.maxValue))" self.splitMuitiFileStepper.integerValue = Int(self.splitMuitiFileStepper.maxValue) } else { self.splitMuitiFileStepper.integerValue = self.splitMuitiFileTextField.integerValue } guard let callback = self.itemClick else { return } callback(3, self.splitMuitiFileStepper.integerValue) } else if (self.pageRangeComboBox.isEqual(to: obj.object)) { guard let callback = self.itemClick else { return } callback(5, self.pageRangeComboBox.stringValue) } else if (self.tagTextField.isEqual(to: obj.object)) { guard let callback = self.itemClick else { return } callback(9, self.tagTextField.stringValue) } else if (self.sepTextView.isEqual(to: obj.object)) { guard let callback = self.itemClick else { return } callback(10, self.sepTextView.stringValue) } } } extension KMPageEditSplitSettingView: NSComboBoxDelegate { func comboBoxSelectionDidChange(_ notification: Notification) { if (self.pageRangeComboBox.isEqual(to: notification.object)) { var index = self.pageRangeComboBox.indexOfSelectedItem if (index < 0) { index = 0 } if (index == 0) { /// 奇数页 self.pageRangeComboBox.isEditable = false } else if (index == 1) { /// 偶数页 self.pageRangeComboBox.isEditable = false } else { /// ziding self.pageRangeComboBox.isEditable = true DispatchQueue.main.async { self.pageRangeComboBox.stringValue = "" } } guard let callback = self.itemClick else { return } callback(4, index) } } }