KMFormActionButtonPopWindowController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // KMFormActionButtonPopWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/2/2.
  6. //
  7. import Cocoa
  8. @objcMembers
  9. class KMFormActionButtonPopWindowController: NSWindowController {
  10. @IBOutlet private weak var actionLabel: NSTextField!
  11. @IBOutlet private weak var actionComboBox: NSComboBox!
  12. @IBOutlet private weak var label: NSTextField!
  13. @IBOutlet private weak var labelTextField: NSTextField!
  14. @IBOutlet private weak var presenceLabel: NSTextField!
  15. @IBOutlet private weak var visibleComboBox: NSComboBox!
  16. @IBOutlet private weak var emailButton: NSButton!
  17. @IBOutlet private weak var emailTextField: NSTextField!
  18. @IBOutlet private weak var urlButton: NSButton!
  19. @IBOutlet private weak var urlTextField: NSTextField!
  20. @IBOutlet private weak var cancelButton: NSButton!
  21. @IBOutlet private weak var okButton: NSButton!
  22. private var settingButtonWidget: CPDFButtonWidgetAnnotation?
  23. private var isEdit: Bool = false
  24. private var handler: ((Int) -> Void)!
  25. // MARK: Dealloc
  26. deinit {
  27. NotificationCenter.default.removeObserver(self)
  28. }
  29. // MARK: init Methods
  30. convenience init(buttonWidget: CPDFButtonWidgetAnnotation, isEdit: Bool) {
  31. self.init(windowNibName: "KMFormActionButtonPopWindowController")
  32. settingButtonWidget = buttonWidget
  33. self.isEdit = isEdit
  34. }
  35. // MARK: Life Cycle
  36. override func windowDidLoad() {
  37. super.windowDidLoad()
  38. localizedString()
  39. if isEdit {
  40. configuViews()
  41. }
  42. emailTextField.inputContext?.allowedInputSourceLocales = [NSAllRomanInputSourcesLocaleIdentifier]
  43. urlTextField.inputContext?.allowedInputSourceLocales = [NSAllRomanInputSourcesLocaleIdentifier]
  44. }
  45. // MARK: Private Methods
  46. func localizedString() {
  47. cancelButton.title = NSLocalizedString("Cancel", comment: "")
  48. okButton.title = NSLocalizedString("OK", comment: "")
  49. actionLabel.stringValue = NSLocalizedString("Action", comment: "")
  50. presenceLabel.stringValue = NSLocalizedString("Button Field", comment: "")
  51. label.stringValue = NSLocalizedString("Label", comment: "")
  52. labelTextField.stringValue = NSLocalizedString("Submit", comment: "")
  53. actionComboBox.removeAllItems()
  54. actionComboBox.stringValue = NSLocalizedString("Submit", comment: "")
  55. actionComboBox.addItems(withObjectValues: [
  56. NSLocalizedString("Submit", comment: ""),
  57. NSLocalizedString("Print", comment: "")
  58. ])
  59. actionComboBox.selectItem(at: 0)
  60. visibleComboBox.removeAllItems()
  61. visibleComboBox.stringValue = NSLocalizedString("Visible", comment: "")
  62. visibleComboBox.addItems(withObjectValues: [
  63. NSLocalizedString("Visible", comment: ""),
  64. NSLocalizedString("Hidden", comment: ""),
  65. NSLocalizedString("Visible but doesn't print", comment: "")
  66. ])
  67. emailButton.title = NSLocalizedString("Submit to Email", comment: "")
  68. urlButton.title = NSLocalizedString("Submit to URL", comment: "")
  69. urlTextField.isEnabled = false
  70. emailTextField.isEnabled = true
  71. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange), name: NSControl.textDidChangeNotification, object: labelTextField)
  72. }
  73. func configuViews() {
  74. labelTextField.stringValue = settingButtonWidget?.caption() ?? NSLocalizedString("Submit", comment: "")
  75. let shouldDisplay = settingButtonWidget?.shouldDisplay() ?? false
  76. let shouldPrint = settingButtonWidget?.shouldPrint() ?? false
  77. if shouldDisplay && !shouldPrint {
  78. visibleComboBox.selectItem(at: 2)
  79. } else if shouldDisplay && shouldPrint {
  80. visibleComboBox.selectItem(at: 0)
  81. } else if shouldPrint && !shouldDisplay {
  82. visibleComboBox.selectItem(at: 0)
  83. }
  84. // if let mouseUpAction = settingButtonWidget.mouseUpAction, mouseUpAction as CPDFNamedAction {
  85. if let mouseUpAction = settingButtonWidget?.mouseUpAction() as? CPDFAnnotation {
  86. actionComboBox.selectItem(at: 1)
  87. urlButton.isEnabled = false
  88. emailButton.isEnabled = false
  89. urlTextField.isEnabled = false
  90. emailTextField.isEnabled = false
  91. } else {
  92. actionComboBox.selectItem(at: 0)
  93. urlButton.isEnabled = true
  94. emailButton.isEnabled = true
  95. urlTextField.isEnabled = true
  96. emailTextField.isEnabled = true
  97. if let urlAction = settingButtonWidget!.mouseUpAction as? CPDFURLAction {
  98. if urlAction.url().fileURL.absoluteString.hasPrefix("mailto:") {
  99. urlButton.state = .off
  100. emailButton.state = .on
  101. emailTextField.stringValue = urlAction.url().fileURL.absoluteString
  102. } else {
  103. urlButton.state = .on
  104. emailButton.state = .off
  105. urlTextField.stringValue = urlAction.url().fileURL.absoluteString
  106. }
  107. }
  108. }
  109. }
  110. @IBAction private func dismissSheet(_ sender: Any) {
  111. if #available(macOS 10.13, *) {
  112. NSApp.endSheet(window!, returnCode: (sender as? NSControl)?.tag ?? 0)
  113. } else {
  114. NSApp.endSheet(window!)
  115. }
  116. window!.orderOut(self)
  117. }
  118. @objc private func didEndSheet(_ sheet: NSWindow?, returnCode: Int, contextInfo: UnsafeMutableRawPointer?) {
  119. if contextInfo != nil && self.handler != nil {
  120. self.handler!(returnCode)
  121. }
  122. }
  123. func beginSheetModal(for window: NSWindow?, completionHandler handler: ((Int) -> Void)?) {
  124. if window != nil {
  125. window!.beginSheet(self.window!) { ModalResponse in
  126. self.handler?(ModalResponse.rawValue)
  127. }
  128. }
  129. self.handler = handler
  130. }
  131. // MARK: Button Actions
  132. @IBAction func comboBoxClicked_ActionSelect(_ sender: Any) {
  133. guard let comboBox = sender as? NSComboBox else {
  134. return
  135. }
  136. if comboBox.indexOfSelectedItem == 1 {
  137. emailButton.isEnabled = false
  138. urlButton.isEnabled = false
  139. urlTextField.stringValue = ""
  140. emailTextField.stringValue = ""
  141. urlTextField.isEnabled = false
  142. emailTextField.isEnabled = false
  143. labelTextField.stringValue = NSLocalizedString("Print", comment: "")
  144. } else {
  145. emailButton.isEnabled = true
  146. urlButton.isEnabled = true
  147. urlTextField.isEnabled = true
  148. emailTextField.isEnabled = true
  149. labelTextField.stringValue = NSLocalizedString("Submit", comment: "")
  150. }
  151. }
  152. @IBAction func buttonClicked_MailOrURLSelect(_ sender: Any) {
  153. guard let button = sender as? NSButton else {
  154. return
  155. }
  156. if button.tag == 1 {
  157. emailButton.state = NSControl.StateValue.off
  158. urlButton.state = NSControl.StateValue.on
  159. emailTextField.isEnabled = false
  160. urlTextField.isEnabled = true
  161. } else {
  162. emailButton.state = NSControl.StateValue.on
  163. urlButton.state = NSControl.StateValue.off
  164. urlTextField.isEnabled = false
  165. emailTextField.isEnabled = true
  166. }
  167. }
  168. @IBAction func buttonClicked_CancelSetting(_ sender: Any) {
  169. dismissSheet(cancelButton)
  170. }
  171. @IBAction func buttonClicked_ConfirmSetting(_ sender: Any) {
  172. if actionComboBox.indexOfSelectedItem == 0 {
  173. var url: String?
  174. if urlButton.state == .on {
  175. if urlTextField.stringValue.hasPrefix("http://") || urlTextField.stringValue.hasPrefix("https://") {
  176. url = urlTextField.stringValue
  177. } else {
  178. url = "http://\(urlTextField.stringValue)"
  179. }
  180. } else {
  181. if emailTextField.stringValue.hasPrefix("mailto:") {
  182. url = emailTextField.stringValue
  183. } else {
  184. url = "mailto:\(emailTextField.stringValue)"
  185. }
  186. }
  187. if let action = CPDFURLAction(url: url) {
  188. // settingButtonWidget.mouseUpAction = action
  189. }
  190. } else {
  191. if let action = CPDFNamedAction(name: .print) {
  192. // settingButtonWidget.mouseUpAction = action
  193. }
  194. }
  195. if visibleComboBox.indexOfSelectedItem == 0 {
  196. settingButtonWidget?.setShouldDisplay(true)
  197. settingButtonWidget?.setShouldPrint(true)
  198. } else if visibleComboBox.indexOfSelectedItem == 1 {
  199. settingButtonWidget?.setShouldDisplay(false)
  200. settingButtonWidget?.setShouldPrint(true)
  201. } else {
  202. settingButtonWidget?.setShouldDisplay(true)
  203. settingButtonWidget?.setShouldPrint(false)
  204. }
  205. settingButtonWidget?.setCaption(labelTextField.stringValue)
  206. NotificationCenter.default.post(name: NSNotification.Name("KMFormActionButtonChange"), object: nil, userInfo: nil)
  207. dismissSheet(okButton)
  208. }
  209. // MARK: NSNotification Action
  210. @objc func textFieldDidChange() {
  211. settingButtonWidget?.setCaption(labelTextField.stringValue)
  212. NotificationCenter.default.post(name: Notification.Name("KMFormActionButtonLabelTextField"), object: self)
  213. }
  214. }