// // KMMergeSettingWindowController.swift // PDF Master // // Created by tangchao on 2022/11/25. // import Cocoa typealias KMMergeSettingWindowControllerCancelClick = (_ window: NSWindow)->() typealias KMMergeSettingWindowControllerMergeClick = (_ windowController: NSWindowController)->() class KMMergeSettingWindowController: NSWindowController { @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var odd_even_mergeComboBox: NSButton! @IBOutlet weak var helpButton: NSButton! @IBOutlet weak var pageSizeLabel: NSTextField! @IBOutlet weak var pageSizeComboBox: NSComboBox! @IBOutlet weak var customSizeView: NSView! @IBOutlet weak var customWitdhView: NSView! @IBOutlet weak var customWidthTextField: NSTextField! @IBOutlet weak var customHeigthView: NSView! @IBOutlet weak var customHeightTextField: NSTextField! @IBOutlet weak var mergeButton: NSButton! @IBOutlet weak var cancelButton: NSButton! var selectedIndex: Int = 0 var cancelClick: KMMergeSettingWindowControllerCancelClick! var mergeClick: KMMergeSettingWindowControllerMergeClick! override func windowDidLoad() { super.windowDidLoad() titleLabel.stringValue = NSLocalizedString("合并设置", comment: "") odd_even_mergeComboBox.title = NSLocalizedString("奇偶页面穿插合并", comment: "") odd_even_mergeComboBox.state = .off helpButton.target = self helpButton.action = #selector(helpButtonAction) pageSizeLabel.stringValue = NSLocalizedString("页面大小", comment: "") pageSizeComboBox.stringValue = NSLocalizedString("原始页面大小", comment: "") pageSizeComboBox.removeAllItems() pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("原始页面大小", comment: ""), NSLocalizedString("A4", comment: ""), NSLocalizedString("A3", comment: ""), NSLocalizedString("U.S.Letter", comment: ""), NSLocalizedString("U.S.Legal", comment: ""), NSLocalizedString("自定义(595*841mm)", comment: "")]) pageSizeComboBox.delegate = self pageSizeComboBox.isEditable = false customSizeView.isHidden = true for view in [customWitdhView, customHeigthView] { view?.wantsLayer = true view?.layer?.borderWidth = 1 view?.layer?.borderColor = NSColor.black.cgColor view?.layer?.cornerRadius = 4 } for textField in [customWidthTextField, customHeightTextField] { textField?.focusRingType = .none textField?.delegate = self } mergeButton.title = NSLocalizedString("合并", comment: "") mergeButton.target = self mergeButton.action = #selector(mergeButtonAction) cancelButton.title = NSLocalizedString("取消", comment: "") cancelButton.target = self cancelButton.action = #selector(cancelButtonAction) } @objc func mergeButtonAction() { guard let callback = mergeClick else { return } callback(self) } @objc func cancelButtonAction() { guard let callback = cancelClick else { return } callback(self.window!) } @objc func helpButtonAction() { let popover = NSPopover() popover.behavior = .transient let controller = KMMergePopoverViewController.init(nibName: "KMMergePopoverViewController", bundle: nil) popover.contentViewController = controller popover .show(relativeTo: helpButton.bounds, of: helpButton, preferredEdge: .maxY) } } extension KMMergeSettingWindowController: NSComboBoxDelegate { func comboBoxSelectionDidChange(_ notification: Notification) { if notification.object as! NSComboBox != pageSizeComboBox { return } let box: NSComboBox = notification.object as! NSComboBox let index = box.indexOfSelectedItem if index < 0 { return } selectedIndex = index if index == 5 { self.customSizeView.isHidden = false let value: String = pageSizeComboBox.itemObjectValue(at: pageSizeComboBox.numberOfItems-1) as! String let array = value.components(separatedBy: "*") if array.count <= 1 { self.customWidthTextField.stringValue = "595" self.customHeightTextField.stringValue = "841" return } var heigth: String = "" heigth = (array.last?.components(separatedBy: "mm").first!)! var width: String = "" width = (array.first?.components(separatedBy: NSLocalizedString("(", comment: "")).last)! self.customWidthTextField.stringValue = width self.customHeightTextField.stringValue = heigth } else { self.customSizeView.isHidden = true } } func controlTextDidChange(_ obj: Notification) { if customWidthTextField.isEqual(to: obj.object) { let textField: NSTextField = obj.object as! NSTextField var value: String = "" for ch in textField.stringValue { if ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" { } else { if value.isEmpty && ch == "0" { } else { value.append(ch) } } } customWidthTextField.stringValue = value pageSizeComboBox.removeAllItems() var string: String = "" string.append(NSLocalizedString("自定义", comment: "")) string.append("(") string.append(customWidthTextField.stringValue) string.append("*") string.append(customHeightTextField.stringValue) string.append("mm)") pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("原始页面大小", comment: ""), "A4", "A3", "U.S.Letter", "U.S.Legal", string]) pageSizeComboBox.stringValue = string } else if customHeightTextField.isEqual(to: obj.object) { let textField: NSTextField = obj.object as! NSTextField var value: String = "" for ch in textField.stringValue { if ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" { } else { if value.isEmpty && ch == "0" { } else { value.append(ch) } } } customHeightTextField.stringValue = value pageSizeComboBox.removeAllItems() var string: String = "" string.append(NSLocalizedString("自定义", comment: "")) string.append("(") string.append(customWidthTextField.stringValue) string.append("*") string.append(customHeightTextField.stringValue) string.append("mm)") pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("原始页面大小", comment: ""), "A4", "A3", "U.S.Letter", "U.S.Legal", string]) pageSizeComboBox.stringValue = string } } }