KMTextFieldSheetController.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // KMTextFieldSheetController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/10/7.
  6. //
  7. import Cocoa
  8. class KMTextFieldSheetController: NSWindowController {
  9. @IBOutlet weak var textField: NSTextField!
  10. @IBOutlet weak var okButton: NSButton!
  11. @IBOutlet weak var cancelButton: NSButton!
  12. @IBOutlet weak var pageBox: NSComboBox!
  13. @IBOutlet weak var pageLabel: NSTextField!
  14. var _stringValue: String?
  15. var callback: ((String) -> Void)?
  16. static var windowController: KMTextFieldSheetController?
  17. deinit {
  18. print(#function)
  19. }
  20. override func windowDidLoad() {
  21. super.windowDidLoad()
  22. KMTextFieldSheetController.windowController = self
  23. pageLabel.stringValue = NSLocalizedString("Page", comment: "") + ":"
  24. self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
  25. self.okButton.title = NSLocalizedString("OK", comment: "")
  26. }
  27. var stringValue: String {
  28. get {
  29. return self.pageBox.stringValue
  30. }
  31. set {
  32. self.pageBox.stringValue = newValue
  33. }
  34. }
  35. @IBAction func okAction(_ sender: Any) {
  36. if let callback = callback, let sender = sender as? NSButton, sender == okButton {
  37. callback(stringValue)
  38. }
  39. if #available(macOS 10.13, *) {
  40. NSApp.endSheet(window!, returnCode: (sender as AnyObject).tag)
  41. } else {
  42. NSApp.endSheet(window!)
  43. }
  44. window!.orderOut(self)
  45. KMTextFieldSheetController.windowController = nil
  46. }
  47. }