NotiVC.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // NotiVC.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/9/14.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class NotiVC: NSViewController {
  10. @IBOutlet var notificationView: ComponentNotification!
  11. @IBOutlet var typeBox: NSComboBox!
  12. @IBOutlet var titleField: NSTextField!
  13. @IBOutlet var subTitleField: NSTextField!
  14. @IBOutlet weak var showImageAction: NSButton!
  15. @IBOutlet weak var viewHeightConst: NSLayoutConstraint!
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. // Do view setup here.
  19. self.typeBox.selectItem(at: 0)
  20. self.reloadData()
  21. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: titleField)
  22. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: titleField)
  23. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: titleField)
  24. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: subTitleField)
  25. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: subTitleField)
  26. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: subTitleField)
  27. }
  28. func reloadData() {
  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 firstBtnProperty = ComponentButtonProperty(type: .text_primary, size: .s, state: .normal, onlyIcon: false, showLeftIcon: false, showRightIcon: false, buttonText: "Later")
  48. let secondBtnProperty = ComponentButtonProperty(type: .primary, size: .s, state: .normal, onlyIcon: false, showLeftIcon: false, showRightIcon: false, buttonText: "View")
  49. let property: ComponentNotificationProperty = ComponentNotificationProperty(messageType: type,
  50. image: image,
  51. text: text,
  52. subText: subText,
  53. firstButtonProperty: firstBtnProperty,
  54. secondButtonProperty: secondBtnProperty)
  55. self.notificationView.properties = property
  56. self.viewHeightConst.constant = self.notificationView.properties.propertyInfo.viewHeight
  57. }
  58. @IBAction func clickAction(_ sender: Any) {
  59. self.reloadData()
  60. }
  61. //MARK: - TextNotification
  62. @objc func textFieldDidBeginEditingNotification(_ notification: Notification) {
  63. }
  64. @objc func textFieldDidChangeNotification(_ notification: Notification) {
  65. self.reloadData()
  66. }
  67. @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
  68. print("textFieldDidEndEditingNotification")
  69. self.reloadData()
  70. }
  71. }