KMSigntureViewItem.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // KMSigntureViewItem.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/10/9.
  6. //
  7. import Cocoa
  8. @objcMembers class KMSigntureViewItem: NSCollectionViewItem {
  9. @IBOutlet var contendView: NSView!
  10. @IBOutlet var iconImg: NSImageView!
  11. @IBOutlet var deleteIconBtn: NSButton!
  12. weak var delegate: KMSigntureViewItemDelegate?
  13. var trackingArea: NSTrackingArea?
  14. var itemIndex: Int = 0
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. self.contendView.wantsLayer = true
  18. self.contendView.layer?.cornerRadius = 8.0
  19. self.contendView.layer?.masksToBounds = true
  20. self.contendView.layer?.borderWidth = 1.0
  21. self.contendView.layer?.borderColor = NSColor.clear.cgColor
  22. self.trackingArea = NSTrackingArea(rect: self.view.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways, .mouseMoved], owner: self, userInfo: nil)
  23. self.view.addTrackingArea(self.trackingArea!)
  24. self.deleteIconBtn.isHidden = true
  25. tableViewMenu()
  26. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  27. }
  28. deinit {
  29. DistributedNotificationCenter.default().removeObserver(self)
  30. }
  31. func tableViewMenu() {
  32. let menu = NSMenu()
  33. let item = menu.addItem(withTitle: NSLocalizedString("Output", comment: ""), action: nil, keyEquivalent: "")
  34. let tSubMenu = NSMenu()
  35. var tMenuItem = tSubMenu.insertItem(withTitle: NSLocalizedString("PNG", comment: ""), action: #selector(exportCustomizeStamp(_:)), keyEquivalent: "", at: 0)
  36. tMenuItem.tag = 0
  37. // tMenuItem = tSubMenu.insertItem(withTitle: NSLocalizedString("JPG", comment: ""), action: #selector(exportCustomizeStamp(_:)), keyEquivalent: "", at: 1)
  38. // tMenuItem.tag = 1
  39. tMenuItem = tSubMenu.insertItem(withTitle: NSLocalizedString("PDF", comment: ""), action: #selector(exportCustomizeStamp(_:)), keyEquivalent: "", at: 1)
  40. tMenuItem.tag = 2
  41. item.submenu = tSubMenu
  42. menu.addItem(NSMenuItem.separator())
  43. menu.addItem(withTitle: NSLocalizedString("Remove", comment: ""), action: #selector(deleteCustomizeStamp), keyEquivalent: "")
  44. menu.addItem(withTitle: NSLocalizedString("Remove All", comment: ""), action: #selector(deleteAllCustomizeStamp), keyEquivalent: "")
  45. self.view.menu = menu
  46. }
  47. var isSelect: Bool = false {
  48. didSet {
  49. updateBgColor()
  50. }
  51. }
  52. @objc func themeChanged(notification: NSNotification) {
  53. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  54. self.updateBgColor()
  55. }
  56. }
  57. func updateBgColor() {
  58. if KMAppearance.isDarkMode() {
  59. if isSelect {
  60. self.contendView.layer?.borderColor = NSColor(red: 37/255.0, green: 65/255.0, blue: 107/255.0, alpha: 1.0).cgColor
  61. self.contendView.layer?.backgroundColor = NSColor(red: 37/255.0, green: 65/255.0, blue: 107/255.0, alpha: 1.0).cgColor
  62. } else {
  63. self.contendView.layer?.borderColor = NSColor.clear.cgColor
  64. self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
  65. }
  66. }else {
  67. if isSelect {
  68. self.contendView.layer?.borderColor = NSColor(red: 217/255.0, green: 228/255.0, blue: 250/255.0, alpha: 1.0).cgColor
  69. self.contendView.layer?.backgroundColor = NSColor(red: 217/255.0, green: 228/255.0, blue: 250/255.0, alpha: 1.0).cgColor
  70. } else {
  71. self.contendView.layer?.borderColor = NSColor.clear.cgColor
  72. self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
  73. }
  74. }
  75. }
  76. override func mouseEntered(with event: NSEvent) {
  77. super.mouseEntered(with: event)
  78. if isSelect {
  79. self.deleteIconBtn.isHidden = false
  80. } else {
  81. self.deleteIconBtn.isHidden = false
  82. if KMAppearance.isDarkMode() {
  83. self.contendView.layer?.borderColor = NSColor(red: 37/255.0, green: 65/255.0, blue: 107/255.0, alpha: 1.0).cgColor
  84. self.contendView.layer?.backgroundColor = NSColor(red: 37/255.0, green: 65/255.0, blue: 107/255.0, alpha: 1.0).cgColor
  85. }else {
  86. self.contendView.layer?.borderColor = NSColor(red: 217/255.0, green: 228/255.0, blue: 250/255.0, alpha: 1.0).cgColor
  87. self.contendView.layer?.backgroundColor = NSColor(red: 217/255.0, green: 228/255.0, blue: 250/255.0, alpha: 1.0).cgColor
  88. }
  89. }
  90. }
  91. override func mouseExited(with event: NSEvent) {
  92. super.mouseExited(with: event)
  93. if isSelect {
  94. self.deleteIconBtn.isHidden = false
  95. } else {
  96. self.deleteIconBtn.isHidden = true
  97. self.contendView.layer?.borderColor = NSColor.clear.cgColor
  98. self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
  99. }
  100. }
  101. @objc func exportCustomizeStamp(_ sender: NSMenuItem) {
  102. if let delegate = self.delegate {
  103. delegate.kmSigntureViewItem(self, didClickMenuWithItem: sender)
  104. }
  105. }
  106. @objc func deleteCustomizeStamp() {
  107. if let delegate = self.delegate {
  108. delegate.kmSigntureViewItemDidClickDeleteMenuItem(self)
  109. }
  110. }
  111. @objc func deleteAllCustomizeStamp() {
  112. if let delegate = self.delegate {
  113. delegate.kmSigntureViewItemDidClickAllDeleteMenuItem(self)
  114. }
  115. }
  116. }
  117. @objc protocol KMSigntureViewItemDelegate: NSObjectProtocol {
  118. func kmSigntureViewItem(_ item: KMSigntureViewItem, didClickMenuWithItem menuItem: NSMenuItem)
  119. func kmSigntureViewItemDidClickDeleteMenuItem(_ item: KMSigntureViewItem)
  120. func kmSigntureViewItemDidClickAllDeleteMenuItem(_ item: KMSigntureViewItem)
  121. }