KMFormActionButtonPopWindowController.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 settingButtonWidget?.mouseUpAction() is CPDFAnnotation {
  85. actionComboBox.selectItem(at: 1)
  86. urlButton.isEnabled = false
  87. emailButton.isEnabled = false
  88. urlTextField.isEnabled = false
  89. emailTextField.isEnabled = false
  90. } else {
  91. actionComboBox.selectItem(at: 0)
  92. urlButton.isEnabled = true
  93. emailButton.isEnabled = true
  94. urlTextField.isEnabled = true
  95. emailTextField.isEnabled = true
  96. if let urlAction = settingButtonWidget!.mouseUpAction() as? CPDFURLAction {
  97. if urlAction.url().fileURL.absoluteString.hasPrefix("mailto:") {
  98. urlButton.state = .off
  99. emailButton.state = .on
  100. emailTextField.stringValue = urlAction.url().fileURL.absoluteString
  101. } else {
  102. urlButton.state = .on
  103. emailButton.state = .off
  104. urlTextField.stringValue = urlAction.url().fileURL.absoluteString
  105. }
  106. }
  107. }
  108. }
  109. @IBAction private func dismissSheet(_ sender: Any) {
  110. if #available(macOS 10.13, *) {
  111. NSApp.endSheet(window!, returnCode: (sender as? NSControl)?.tag ?? 0)
  112. } else {
  113. NSApp.endSheet(window!)
  114. }
  115. window!.orderOut(self)
  116. }
  117. @objc private func didEndSheet(_ sheet: NSWindow?, returnCode: Int, contextInfo: UnsafeMutableRawPointer?) {
  118. if contextInfo != nil && self.handler != nil {
  119. self.handler!(returnCode)
  120. }
  121. }
  122. func beginSheetModal(for window: NSWindow?, completionHandler handler: ((Int) -> Void)?) {
  123. if window != nil {
  124. window!.beginSheet(self.window!) { ModalResponse in
  125. self.handler?(ModalResponse.rawValue)
  126. }
  127. }
  128. self.handler = handler
  129. }
  130. // MARK: Button Actions
  131. @IBAction func comboBoxClicked_ActionSelect(_ sender: Any) {
  132. guard let comboBox = sender as? NSComboBox else {
  133. return
  134. }
  135. if comboBox.indexOfSelectedItem == 1 {
  136. emailButton.isEnabled = false
  137. urlButton.isEnabled = false
  138. urlTextField.stringValue = ""
  139. emailTextField.stringValue = ""
  140. urlTextField.isEnabled = false
  141. emailTextField.isEnabled = false
  142. labelTextField.stringValue = NSLocalizedString("Print", comment: "")
  143. } else {
  144. emailButton.isEnabled = true
  145. urlButton.isEnabled = true
  146. urlTextField.isEnabled = true
  147. emailTextField.isEnabled = true
  148. labelTextField.stringValue = NSLocalizedString("Submit", comment: "")
  149. }
  150. }
  151. @IBAction func buttonClicked_MailOrURLSelect(_ sender: Any) {
  152. guard let button = sender as? NSButton else {
  153. return
  154. }
  155. if button.tag == 1 {
  156. emailButton.state = NSControl.StateValue.off
  157. urlButton.state = NSControl.StateValue.on
  158. emailTextField.isEnabled = false
  159. urlTextField.isEnabled = true
  160. } else {
  161. emailButton.state = NSControl.StateValue.on
  162. urlButton.state = NSControl.StateValue.off
  163. urlTextField.isEnabled = false
  164. emailTextField.isEnabled = true
  165. }
  166. }
  167. @IBAction func buttonClicked_CancelSetting(_ sender: Any) {
  168. dismissSheet(cancelButton as Any)
  169. }
  170. @IBAction func buttonClicked_ConfirmSetting(_ sender: Any) {
  171. if actionComboBox.indexOfSelectedItem == 0 {
  172. var url: String?
  173. if urlButton.state == .on {
  174. if urlTextField.stringValue.hasPrefix("http://") || urlTextField.stringValue.hasPrefix("https://") {
  175. url = urlTextField.stringValue
  176. } else {
  177. url = "http://\(urlTextField.stringValue)"
  178. }
  179. } else {
  180. if emailTextField.stringValue.hasPrefix("mailto:") {
  181. url = emailTextField.stringValue
  182. } else {
  183. url = "mailto:\(emailTextField.stringValue)"
  184. }
  185. }
  186. if let action = CPDFURLAction(url: url) {
  187. settingButtonWidget?.setMouseUpAction(action)
  188. }
  189. } else {
  190. if let action = CPDFNamedAction(name: .print) {
  191. settingButtonWidget?.setMouseUpAction(action)
  192. }
  193. }
  194. if visibleComboBox.indexOfSelectedItem == 0 {
  195. settingButtonWidget?.setShouldDisplay(true)
  196. settingButtonWidget?.setShouldPrint(true)
  197. } else if visibleComboBox.indexOfSelectedItem == 1 {
  198. settingButtonWidget?.setShouldDisplay(false)
  199. settingButtonWidget?.setShouldPrint(true)
  200. } else {
  201. settingButtonWidget?.setShouldDisplay(true)
  202. settingButtonWidget?.setShouldPrint(false)
  203. }
  204. settingButtonWidget?.setCaption(labelTextField.stringValue)
  205. NotificationCenter.default.post(name: NSNotification.Name("KMFormActionButtonChange"), object: nil, userInfo: nil)
  206. dismissSheet(okButton as Any)
  207. }
  208. // MARK: NSNotification Action
  209. @objc func textFieldDidChange() {
  210. settingButtonWidget?.setCaption(labelTextField.stringValue)
  211. NotificationCenter.default.post(name: Notification.Name("KMFormActionButtonLabelTextField"), object: self)
  212. }
  213. }