KMSigntureViewItem.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // KMSigntureViewItem.swift
  3. // PDF Master
  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. }
  27. func tableViewMenu() {
  28. let menu = NSMenu()
  29. let item = menu.addItem(withTitle: NSLocalizedString("Export stamp", comment: ""), action: nil, keyEquivalent: "")
  30. let tSubMenu = NSMenu()
  31. var tMenuItem = tSubMenu.insertItem(withTitle: NSLocalizedString("PNG", comment: ""), action: #selector(exportCustomizeStamp(_:)), keyEquivalent: "", at: 0)
  32. tMenuItem.tag = 0
  33. // tMenuItem = tSubMenu.insertItem(withTitle: NSLocalizedString("JPG", comment: ""), action: #selector(exportCustomizeStamp(_:)), keyEquivalent: "", at: 1)
  34. // tMenuItem.tag = 1
  35. tMenuItem = tSubMenu.insertItem(withTitle: NSLocalizedString("PDF", comment: ""), action: #selector(exportCustomizeStamp(_:)), keyEquivalent: "", at: 1)
  36. tMenuItem.tag = 2
  37. item.submenu = tSubMenu
  38. menu.addItem(NSMenuItem.separator())
  39. menu.addItem(withTitle: NSLocalizedString("Delete stamp", comment: ""), action: #selector(deleteCustomizeStamp), keyEquivalent: "")
  40. menu.addItem(withTitle: NSLocalizedString("Delete All stamp", comment: ""), action: #selector(deleteAllCustomizeStamp), keyEquivalent: "")
  41. self.view.menu = menu
  42. }
  43. var isSelect: Bool = false {
  44. didSet {
  45. if isSelect {
  46. self.contendView.layer?.borderColor = NSColor(red: 206/255, green: 208/255, blue: 212/255, alpha: 0.6).cgColor
  47. self.contendView.layer?.backgroundColor = NSColor(red: 206/255, green: 208/255, blue: 212/255, alpha: 1).cgColor
  48. } else {
  49. self.contendView.layer?.borderColor = NSColor.clear.cgColor
  50. self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
  51. }
  52. }
  53. }
  54. override func mouseEntered(with event: NSEvent) {
  55. super.mouseEntered(with: event)
  56. if isSelect {
  57. } else {
  58. self.contendView.layer?.borderColor = NSColor(red: 237/255, green: 238/255, blue: 240/255, alpha: 1).cgColor
  59. self.contendView.layer?.backgroundColor = NSColor(red: 237/255, green: 238/255, blue: 240/255, alpha: 1).cgColor
  60. }
  61. }
  62. override func mouseExited(with event: NSEvent) {
  63. super.mouseExited(with: event)
  64. if isSelect {
  65. } else {
  66. self.contendView.layer?.borderColor = NSColor.clear.cgColor
  67. self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
  68. }
  69. }
  70. @objc func exportCustomizeStamp(_ sender: NSMenuItem) {
  71. if let delegate = self.delegate {
  72. delegate.kmSigntureViewItem(self, didClickMenuWithItem: sender)
  73. }
  74. }
  75. @objc func deleteCustomizeStamp() {
  76. if let delegate = self.delegate {
  77. delegate.kmSigntureViewItemDidClickDeleteMenuItem(self)
  78. }
  79. }
  80. @objc func deleteAllCustomizeStamp() {
  81. if let delegate = self.delegate {
  82. delegate.kmSigntureViewItemDidClickAllDeleteMenuItem(self)
  83. }
  84. }
  85. }
  86. @objc protocol KMSigntureViewItemDelegate: NSObjectProtocol {
  87. func kmSigntureViewItem(_ item: KMSigntureViewItem, didClickMenuWithItem menuItem: NSMenuItem)
  88. func kmSigntureViewItemDidClickDeleteMenuItem(_ item: KMSigntureViewItem)
  89. func kmSigntureViewItemDidClickAllDeleteMenuItem(_ item: KMSigntureViewItem)
  90. }