// // KMHistoryFileCollectionViewItem.swift // PDF Master // // Created by wanjun on 2022/10/28. // import Cocoa import QuickLook class KMHistoryFileCollectionViewItem: NSCollectionViewItem { @IBOutlet weak var mainBox: KMBox! @IBOutlet weak var imageBox: NSBox! @IBOutlet weak var fileImageView: NSImageView! @IBOutlet weak var favoriteButton: NSButton! @IBOutlet weak var documentName: NSTextField! var filePathUrl: URL? = nil var selectUrls: [URL] = [] var isSelect: Bool = false // MARK: init override func viewDidLoad() { super.viewDidLoad() // Do view setup here. initializeUI() mainBox.menu = tableCellMenu documentName.maximumNumberOfLines = 2 mainBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in if self != nil { if !self!.isSelect { if mouseEntered { self!.mainBox.fillColor = NSColor(hex: "#EDEEF0", alpha: 0.6) self!.documentName.textColor = NSColor(hex: "#252629") self!.mainBox.cornerRadius = 8.0 } else { self!.mainBox.fillColor = .clear self!.documentName.textColor = NSColor(hex: "#252629") self!.mainBox.cornerRadius = 0.0 } } } } } func initializeUI() { } // MARK: private func refreshUI(_ path: URL) { filePathUrl = path if selectUrls.contains(path) { isSelect = true mainBox.fillColor = NSColor(hex: "#CED0D4") mainBox.cornerRadius = 8.0 } else { isSelect = false mainBox.fillColor = .clear mainBox.cornerRadius = 0.0 } favoriteButton.isHidden = true imageBox.fillColor = .clear documentName.textColor = NSColor(hex: "#252629") documentName.stringValue = filePathUrl!.lastPathComponent mainBox.toolTip = filePathUrl!.lastPathComponent let image: NSImage = NSImage.previewForFile(path: filePathUrl!, ofSize: fileImageView.frame.size, asIcon: true) ?? NSImage() fileImageView.image = image } // MARK: Menu lazy var tableCellMenu: NSMenu = { let tableCellMenu = NSMenu() var item = NSMenuItem() item.title = NSLocalizedString("Show in Finder", comment: "") item.action = #selector(buttonItemClick_ShowPath) tableCellMenu.addItem(item) item = NSMenuItem() item.title = NSLocalizedString("Remove from Recents", comment: "") item.action = #selector(buttonItemClick_ClosePath) tableCellMenu.addItem(item) return tableCellMenu }() // MARK: Action @IBAction func buttonItemClick_ShowPath(sender: AnyObject) { if FileManager.default.fileExists(atPath: filePathUrl!.path) { NSWorkspace.shared.activateFileViewerSelecting([filePathUrl!]) } } @IBAction func buttonItemClick_ClosePath(sender: AnyObject) { var selects: [String] = [] if selectUrls.count > 0 { for selecturl in self.selectUrls { selects.append(selecturl.path) } } else { selects.append(filePathUrl!.path) } let urls: Array = NSDocumentController.shared.recentDocumentURLs NSDocumentController.shared.clearRecentDocuments(nil) DispatchQueue.main.asyncAfter(deadline: .now()) { [self] in for (_, url) in urls.enumerated() { if !selects.contains(url.path) { NSDocumentController.shared.noteNewRecentDocumentURL(url) } } NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMHomeFileRectChange"), object: self.view.window) } } @IBAction func favoriteButtonAction(_ sender: NSButton) { print("收藏文件按钮") } }