123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- //
- // 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<URL> = 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("收藏文件按钮")
- }
- }
|