ModalVC.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // ModalVC.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/9/25.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class ModalVC: NSViewController {
  10. @IBOutlet var typeBox: NSComboBox!
  11. @IBOutlet var titleField: NSTextField!
  12. @IBOutlet var subTitleField: NSTextField!
  13. @IBOutlet weak var showImageAction: NSButton!
  14. let modalView: ComponentModal = ComponentModal()
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. // Do view setup here.
  18. self.typeBox.selectItem(at: 0)
  19. self.reloadData()
  20. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: titleField)
  21. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: titleField)
  22. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: titleField)
  23. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: subTitleField)
  24. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: subTitleField)
  25. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: subTitleField)
  26. modalView.frame = CGRectMake(100, 100, 480, 172)
  27. }
  28. @IBAction func showModalAction(_ sender: Any) {
  29. var image: NSImage? = ComponentLibrary.shared.image(forResource: "test")
  30. self.showImageAction.isEnabled = false
  31. var type: ComponentMessageType = .info
  32. if self.typeBox.indexOfSelectedItem == 1 {
  33. type = .success
  34. } else if self.typeBox.indexOfSelectedItem == 2 {
  35. type = .warning
  36. } else if self.typeBox.indexOfSelectedItem == 3 {
  37. type = .error
  38. } else if self.typeBox.indexOfSelectedItem == 4 {
  39. type = .normal_custom
  40. self.showImageAction.isEnabled = true
  41. if self.showImageAction.state == .off {
  42. image = nil
  43. }
  44. }
  45. let text = self.titleField.stringValue
  46. let subText = self.subTitleField.stringValue
  47. let modalProperty: ComponentModalProperty = ComponentModalProperty(messageType: type,
  48. image: image,
  49. text: text,
  50. subText: subText)
  51. modalView.show(with: modalProperty, in: self.view.window)
  52. }
  53. func reloadData() {
  54. }
  55. @IBAction func clickAction(_ sender: Any) {
  56. self.reloadData()
  57. }
  58. //MARK: - TextNotification
  59. @objc func textFieldDidBeginEditingNotification(_ notification: Notification) {
  60. }
  61. @objc func textFieldDidChangeNotification(_ notification: Notification) {
  62. self.reloadData()
  63. }
  64. @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
  65. print("textFieldDidEndEditingNotification")
  66. self.reloadData()
  67. }
  68. }