123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- //
- // KMSigntureViewItem.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/10/9.
- //
- import Cocoa
- @objcMembers class KMSigntureViewItem: NSCollectionViewItem {
- @IBOutlet var contendView: NSView!
- @IBOutlet var iconImg: NSImageView!
- @IBOutlet var deleteIconBtn: NSButton!
- weak var delegate: KMSigntureViewItemDelegate?
-
- var trackingArea: NSTrackingArea?
- var itemIndex: Int = 0
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- self.contendView.wantsLayer = true
- self.contendView.layer?.cornerRadius = 8.0
- self.contendView.layer?.masksToBounds = true
- self.contendView.layer?.borderWidth = 1.0
- self.contendView.layer?.borderColor = NSColor.clear.cgColor
- self.trackingArea = NSTrackingArea(rect: self.view.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways, .mouseMoved], owner: self, userInfo: nil)
- self.view.addTrackingArea(self.trackingArea!)
- self.deleteIconBtn.isHidden = true
- tableViewMenu()
-
- DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
- }
-
- deinit {
- DistributedNotificationCenter.default().removeObserver(self)
- }
- func tableViewMenu() {
- let menu = NSMenu()
- let item = menu.addItem(withTitle: NSLocalizedString("Output", comment: ""), action: nil, keyEquivalent: "")
- let tSubMenu = NSMenu()
- var tMenuItem = tSubMenu.insertItem(withTitle: NSLocalizedString("PNG", comment: ""), action: #selector(exportCustomizeStamp(_:)), keyEquivalent: "", at: 0)
- tMenuItem.tag = 0
- // tMenuItem = tSubMenu.insertItem(withTitle: NSLocalizedString("JPG", comment: ""), action: #selector(exportCustomizeStamp(_:)), keyEquivalent: "", at: 1)
- // tMenuItem.tag = 1
- tMenuItem = tSubMenu.insertItem(withTitle: NSLocalizedString("PDF", comment: ""), action: #selector(exportCustomizeStamp(_:)), keyEquivalent: "", at: 1)
- tMenuItem.tag = 2
- item.submenu = tSubMenu
- menu.addItem(NSMenuItem.separator())
- menu.addItem(withTitle: NSLocalizedString("Remove", comment: ""), action: #selector(deleteCustomizeStamp), keyEquivalent: "")
- menu.addItem(withTitle: NSLocalizedString("Remove All", comment: ""), action: #selector(deleteAllCustomizeStamp), keyEquivalent: "")
- self.view.menu = menu
- }
- var isSelect: Bool = false {
- didSet {
- updateBgColor()
- }
- }
- @objc func themeChanged(notification: NSNotification) {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
- self.updateBgColor()
- }
- }
-
- func updateBgColor() {
- if KMAppearance.isDarkMode() {
- if isSelect {
- self.contendView.layer?.borderColor = NSColor(red: 37/255.0, green: 65/255.0, blue: 107/255.0, alpha: 1.0).cgColor
- self.contendView.layer?.backgroundColor = NSColor(red: 37/255.0, green: 65/255.0, blue: 107/255.0, alpha: 1.0).cgColor
- } else {
- self.contendView.layer?.borderColor = NSColor.clear.cgColor
- self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
- }
- }else {
- if isSelect {
- self.contendView.layer?.borderColor = NSColor(red: 217/255.0, green: 228/255.0, blue: 250/255.0, alpha: 1.0).cgColor
- self.contendView.layer?.backgroundColor = NSColor(red: 217/255.0, green: 228/255.0, blue: 250/255.0, alpha: 1.0).cgColor
- } else {
- self.contendView.layer?.borderColor = NSColor.clear.cgColor
- self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
- }
- }
- }
-
- override func mouseEntered(with event: NSEvent) {
- super.mouseEntered(with: event)
- if isSelect {
- self.deleteIconBtn.isHidden = false
- } else {
- self.deleteIconBtn.isHidden = false
- if KMAppearance.isDarkMode() {
- self.contendView.layer?.borderColor = NSColor(red: 37/255.0, green: 65/255.0, blue: 107/255.0, alpha: 1.0).cgColor
- self.contendView.layer?.backgroundColor = NSColor(red: 37/255.0, green: 65/255.0, blue: 107/255.0, alpha: 1.0).cgColor
- }else {
- self.contendView.layer?.borderColor = NSColor(red: 217/255.0, green: 228/255.0, blue: 250/255.0, alpha: 1.0).cgColor
- self.contendView.layer?.backgroundColor = NSColor(red: 217/255.0, green: 228/255.0, blue: 250/255.0, alpha: 1.0).cgColor
- }
- }
- }
- override func mouseExited(with event: NSEvent) {
- super.mouseExited(with: event)
- if isSelect {
- self.deleteIconBtn.isHidden = false
- } else {
- self.deleteIconBtn.isHidden = true
- self.contendView.layer?.borderColor = NSColor.clear.cgColor
- self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
- }
- }
- @objc func exportCustomizeStamp(_ sender: NSMenuItem) {
- if let delegate = self.delegate {
- delegate.kmSigntureViewItem(self, didClickMenuWithItem: sender)
- }
- }
- @objc func deleteCustomizeStamp() {
- if let delegate = self.delegate {
- delegate.kmSigntureViewItemDidClickDeleteMenuItem(self)
- }
- }
- @objc func deleteAllCustomizeStamp() {
- if let delegate = self.delegate {
- delegate.kmSigntureViewItemDidClickAllDeleteMenuItem(self)
- }
- }
- }
- @objc protocol KMSigntureViewItemDelegate: NSObjectProtocol {
- func kmSigntureViewItem(_ item: KMSigntureViewItem, didClickMenuWithItem menuItem: NSMenuItem)
- func kmSigntureViewItemDidClickDeleteMenuItem(_ item: KMSigntureViewItem)
- func kmSigntureViewItemDidClickAllDeleteMenuItem(_ item: KMSigntureViewItem)
- }
|