123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- //
- // 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
- var currentWindowController: NSWindowController?
- // MARK: init
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- initializeUI()
- mainBox.menu = tableCellMenu
- documentName.maximumNumberOfLines = 2
- mainBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
- if !self.isSelect {
- if mouseEntered {
- self.mainBox.fillColor = NSColor(hex: "#EDEEF0")
- self.mainBox.borderWidth = 0.0
- self.documentName.textColor = NSColor(hex: "#252629")
- self.mainBox.cornerRadius = 8.0
- } else {
- self.mainBox.fillColor = .clear
- self.mainBox.borderWidth = 0.0
- 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", alpha: 0.6)
- mainBox.borderWidth = 1.0
- mainBox.borderColor = NSColor(hex: "#CED0D4")
- mainBox.cornerRadius = 8.0
- } else {
- isSelect = false
- mainBox.fillColor = .clear
- mainBox.borderWidth = 0.0
- 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: [URL] = []
- if selectUrls.count > 0 {
- for selecturl in self.selectUrls {
- selects.append(selecturl)
- }
- } else {
- selects.append(filePathUrl!)
- }
- if UserDefaults.standard.bool(forKey: "kHistoryDeleteNOReminderKey") {
- historyFileDeleteAction(selects)
- } else {
- let historyFileDeleteVC: KMHistoryFileDeleteWindowController = KMHistoryFileDeleteWindowController.init(windowNibName: NSNib.Name("KMHistoryFileDeleteWindowController"))
- historyFileDeleteVC.indexPaths = selects
- self.currentWindowController = historyFileDeleteVC
- historyFileDeleteVC.deleteCallback = { [weak self](indexPaths: [URL], windowController: KMHistoryFileDeleteWindowController) -> Void in
- if self != nil {
- self?.currentWindowController = nil
- self!.historyFileDeleteAction(indexPaths)
- }
- }
- self.view.window?.beginSheet(historyFileDeleteVC.window!)
- }
- }
-
- func historyFileDeleteAction(_ indexPaths: [URL]) -> Void {
- let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
- NSDocumentController.shared.clearRecentDocuments(nil)
- DispatchQueue.main.asyncAfter(deadline: .now()) { [self] in
- for (_, url) in urls.enumerated() {
- if !indexPaths.contains(url) {
- NSDocumentController.shared.noteNewRecentDocumentURL(url)
- }
- }
- NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMHomeFileRectChange"), object: self.view.window)
- }
- }
-
- @IBAction func favoriteButtonAction(_ sender: NSButton) {
- print("收藏文件按钮")
- }
- }
|