123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- //
- // KMFormActionButtonPopWindowController.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2024/2/2.
- //
- import Cocoa
- @objcMembers
- class KMFormActionButtonPopWindowController: NSWindowController {
-
- @IBOutlet private weak var actionLabel: NSTextField!
- @IBOutlet private weak var actionComboBox: NSComboBox!
- @IBOutlet private weak var label: NSTextField!
- @IBOutlet private weak var labelTextField: NSTextField!
- @IBOutlet private weak var presenceLabel: NSTextField!
- @IBOutlet private weak var visibleComboBox: NSComboBox!
- @IBOutlet private weak var emailButton: NSButton!
- @IBOutlet private weak var emailTextField: NSTextField!
- @IBOutlet private weak var urlButton: NSButton!
- @IBOutlet private weak var urlTextField: NSTextField!
- @IBOutlet private weak var cancelButton: NSButton!
- @IBOutlet private weak var okButton: NSButton!
-
- private var settingButtonWidget: CPDFButtonWidgetAnnotation?
- private var isEdit: Bool = false
- private var handler: ((Int) -> Void)!
- // MARK: Dealloc
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
-
- // MARK: init Methods
- convenience init(buttonWidget: CPDFButtonWidgetAnnotation, isEdit: Bool) {
- self.init(windowNibName: "KMFormActionButtonPopWindowController")
-
- settingButtonWidget = buttonWidget
- self.isEdit = isEdit
- }
-
- // MARK: Life Cycle
-
- override func windowDidLoad() {
- super.windowDidLoad()
-
- localizedString()
- if isEdit {
- configuViews()
- }
- emailTextField.inputContext?.allowedInputSourceLocales = [NSAllRomanInputSourcesLocaleIdentifier]
- urlTextField.inputContext?.allowedInputSourceLocales = [NSAllRomanInputSourcesLocaleIdentifier]
- }
-
- // MARK: Private Methods
-
- func localizedString() {
- cancelButton.title = NSLocalizedString("Cancel", comment: "")
- okButton.title = NSLocalizedString("OK", comment: "")
- actionLabel.stringValue = NSLocalizedString("Action", comment: "")
- presenceLabel.stringValue = NSLocalizedString("Button Field", comment: "")
- label.stringValue = NSLocalizedString("Label", comment: "")
- labelTextField.stringValue = NSLocalizedString("Submit", comment: "")
-
- actionComboBox.removeAllItems()
- actionComboBox.stringValue = NSLocalizedString("Submit", comment: "")
- actionComboBox.addItems(withObjectValues: [
- NSLocalizedString("Submit", comment: ""),
- NSLocalizedString("Print", comment: "")
- ])
- actionComboBox.selectItem(at: 0)
-
- visibleComboBox.removeAllItems()
- visibleComboBox.stringValue = NSLocalizedString("Visible", comment: "")
- visibleComboBox.addItems(withObjectValues: [
- NSLocalizedString("Visible", comment: ""),
- NSLocalizedString("Hidden", comment: ""),
- NSLocalizedString("Visible but doesn't print", comment: "")
- ])
-
- emailButton.title = NSLocalizedString("Submit to Email", comment: "")
- urlButton.title = NSLocalizedString("Submit to URL", comment: "")
-
- urlTextField.isEnabled = false
- emailTextField.isEnabled = true
-
- NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange), name: NSControl.textDidChangeNotification, object: labelTextField)
- }
- func configuViews() {
- labelTextField.stringValue = settingButtonWidget?.caption() ?? NSLocalizedString("Submit", comment: "")
- let shouldDisplay = settingButtonWidget?.shouldDisplay() ?? false
- let shouldPrint = settingButtonWidget?.shouldPrint() ?? false
-
- if shouldDisplay && !shouldPrint {
- visibleComboBox.selectItem(at: 2)
- } else if shouldDisplay && shouldPrint {
- visibleComboBox.selectItem(at: 0)
- } else if shouldPrint && !shouldDisplay {
- visibleComboBox.selectItem(at: 0)
- }
-
- if settingButtonWidget?.mouseUpAction() is CPDFAnnotation {
- actionComboBox.selectItem(at: 1)
- urlButton.isEnabled = false
- emailButton.isEnabled = false
- urlTextField.isEnabled = false
- emailTextField.isEnabled = false
- } else {
- actionComboBox.selectItem(at: 0)
- urlButton.isEnabled = true
- emailButton.isEnabled = true
- urlTextField.isEnabled = true
- emailTextField.isEnabled = true
-
- if let urlAction = settingButtonWidget!.mouseUpAction() as? CPDFURLAction {
- if urlAction.url().fileURL.absoluteString.hasPrefix("mailto:") {
- urlButton.state = .off
- emailButton.state = .on
- emailTextField.stringValue = urlAction.url().fileURL.absoluteString
- } else {
- urlButton.state = .on
- emailButton.state = .off
- urlTextField.stringValue = urlAction.url().fileURL.absoluteString
- }
- }
- }
- }
-
- @IBAction private func dismissSheet(_ sender: Any) {
- if #available(macOS 10.13, *) {
- NSApp.endSheet(window!, returnCode: (sender as? NSControl)?.tag ?? 0)
- } else {
- NSApp.endSheet(window!)
- }
- window!.orderOut(self)
- }
-
- @objc private func didEndSheet(_ sheet: NSWindow?, returnCode: Int, contextInfo: UnsafeMutableRawPointer?) {
- if contextInfo != nil && self.handler != nil {
- self.handler!(returnCode)
- }
- }
-
- func beginSheetModal(for window: NSWindow?, completionHandler handler: ((Int) -> Void)?) {
- if window != nil {
- window!.beginSheet(self.window!) { ModalResponse in
- self.handler?(ModalResponse.rawValue)
- }
- }
- self.handler = handler
- }
-
- // MARK: Button Actions
-
- @IBAction func comboBoxClicked_ActionSelect(_ sender: Any) {
- guard let comboBox = sender as? NSComboBox else {
- return
- }
-
- if comboBox.indexOfSelectedItem == 1 {
- emailButton.isEnabled = false
- urlButton.isEnabled = false
- urlTextField.stringValue = ""
- emailTextField.stringValue = ""
- urlTextField.isEnabled = false
- emailTextField.isEnabled = false
-
- labelTextField.stringValue = NSLocalizedString("Print", comment: "")
- } else {
- emailButton.isEnabled = true
- urlButton.isEnabled = true
- urlTextField.isEnabled = true
- emailTextField.isEnabled = true
- labelTextField.stringValue = NSLocalizedString("Submit", comment: "")
- }
- }
- @IBAction func buttonClicked_MailOrURLSelect(_ sender: Any) {
- guard let button = sender as? NSButton else {
- return
- }
-
- if button.tag == 1 {
- emailButton.state = NSControl.StateValue.off
- urlButton.state = NSControl.StateValue.on
- emailTextField.isEnabled = false
- urlTextField.isEnabled = true
- } else {
- emailButton.state = NSControl.StateValue.on
- urlButton.state = NSControl.StateValue.off
- urlTextField.isEnabled = false
- emailTextField.isEnabled = true
- }
- }
- @IBAction func buttonClicked_CancelSetting(_ sender: Any) {
- dismissSheet(cancelButton as Any)
- }
-
- @IBAction func buttonClicked_ConfirmSetting(_ sender: Any) {
- if actionComboBox.indexOfSelectedItem == 0 {
- var url: String?
- if urlButton.state == .on {
- if urlTextField.stringValue.hasPrefix("http://") || urlTextField.stringValue.hasPrefix("https://") {
- url = urlTextField.stringValue
- } else {
- url = "http://\(urlTextField.stringValue)"
- }
- } else {
- if emailTextField.stringValue.hasPrefix("mailto:") {
- url = emailTextField.stringValue
- } else {
- url = "mailto:\(emailTextField.stringValue)"
- }
- }
- if let action = CPDFURLAction(url: url) {
- settingButtonWidget?.setMouseUpAction(action)
- }
- } else {
- if let action = CPDFNamedAction(name: .print) {
- settingButtonWidget?.setMouseUpAction(action)
- }
- }
-
- if visibleComboBox.indexOfSelectedItem == 0 {
- settingButtonWidget?.setShouldDisplay(true)
- settingButtonWidget?.setShouldPrint(true)
- } else if visibleComboBox.indexOfSelectedItem == 1 {
- settingButtonWidget?.setShouldDisplay(false)
- settingButtonWidget?.setShouldPrint(true)
- } else {
- settingButtonWidget?.setShouldDisplay(true)
- settingButtonWidget?.setShouldPrint(false)
- }
-
- settingButtonWidget?.setCaption(labelTextField.stringValue)
- NotificationCenter.default.post(name: NSNotification.Name("KMFormActionButtonChange"), object: nil, userInfo: nil)
- dismissSheet(okButton as Any)
- }
-
- // MARK: NSNotification Action
-
- @objc func textFieldDidChange() {
- settingButtonWidget?.setCaption(labelTextField.stringValue)
-
- NotificationCenter.default.post(name: Notification.Name("KMFormActionButtonLabelTextField"), object: self)
- }
- }
|