123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- //
- // 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
- }
- }
- }
|