KMHistoryFileCollectionViewItem.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // KMHistoryFileCollectionViewItem.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/10/28.
  6. //
  7. import Cocoa
  8. import QuickLook
  9. class KMHistoryFileCollectionViewItem: NSCollectionViewItem {
  10. @IBOutlet weak var mainBox: KMBox!
  11. @IBOutlet weak var imageBox: NSBox!
  12. @IBOutlet weak var fileImageView: NSImageView!
  13. @IBOutlet weak var favoriteButton: NSButton!
  14. @IBOutlet weak var documentName: NSTextField!
  15. var filePathUrl: URL? = nil
  16. var selectUrls: [URL] = []
  17. var isSelect: Bool = false
  18. var currentWindowController: NSWindowController?
  19. // MARK: init
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. // Do view setup here.
  23. initializeUI()
  24. mainBox.menu = tableCellMenu
  25. documentName.maximumNumberOfLines = 2
  26. mainBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  27. if !self.isSelect {
  28. if mouseEntered {
  29. self.mainBox.fillColor = NSColor(hex: "#EDEEF0")
  30. self.mainBox.borderWidth = 0.0
  31. self.documentName.textColor = NSColor(hex: "#252629")
  32. self.mainBox.cornerRadius = 8.0
  33. } else {
  34. self.mainBox.fillColor = .clear
  35. self.mainBox.borderWidth = 0.0
  36. self.documentName.textColor = NSColor(hex: "#252629")
  37. self.mainBox.cornerRadius = 0.0
  38. }
  39. }
  40. }
  41. }
  42. func initializeUI() {
  43. }
  44. // MARK: private
  45. func refreshUI(_ path: URL) {
  46. filePathUrl = path
  47. if selectUrls.contains(path) {
  48. isSelect = true
  49. mainBox.fillColor = NSColor(hex: "#CED0D4", alpha: 0.6)
  50. mainBox.borderWidth = 1.0
  51. mainBox.borderColor = NSColor(hex: "#CED0D4")
  52. mainBox.cornerRadius = 8.0
  53. } else {
  54. isSelect = false
  55. mainBox.fillColor = .clear
  56. mainBox.borderWidth = 0.0
  57. mainBox.cornerRadius = 0.0
  58. }
  59. favoriteButton.isHidden = true
  60. imageBox.fillColor = .clear
  61. documentName.textColor = NSColor(hex: "#252629")
  62. documentName.stringValue = filePathUrl!.lastPathComponent
  63. mainBox.toolTip = filePathUrl!.lastPathComponent
  64. let image: NSImage = NSImage.previewForFile(path: filePathUrl!, ofSize: fileImageView.frame.size, asIcon: true) ?? NSImage()
  65. fileImageView.image = image
  66. }
  67. // MARK: Menu
  68. lazy var tableCellMenu: NSMenu = {
  69. let tableCellMenu = NSMenu()
  70. var item = NSMenuItem()
  71. item.title = NSLocalizedString("Show in Finder", comment: "")
  72. item.action = #selector(buttonItemClick_ShowPath)
  73. tableCellMenu.addItem(item)
  74. item = NSMenuItem()
  75. item.title = NSLocalizedString("Remove from Recents", comment: "")
  76. item.action = #selector(buttonItemClick_ClosePath)
  77. tableCellMenu.addItem(item)
  78. return tableCellMenu
  79. }()
  80. // MARK: Action
  81. @IBAction func buttonItemClick_ShowPath(sender: AnyObject) {
  82. if FileManager.default.fileExists(atPath: filePathUrl!.path) {
  83. NSWorkspace.shared.activateFileViewerSelecting([filePathUrl!])
  84. }
  85. }
  86. @IBAction func buttonItemClick_ClosePath(sender: AnyObject) {
  87. var selects: [URL] = []
  88. if selectUrls.count > 0 {
  89. for selecturl in self.selectUrls {
  90. selects.append(selecturl)
  91. }
  92. } else {
  93. selects.append(filePathUrl!)
  94. }
  95. if UserDefaults.standard.bool(forKey: "kHistoryDeleteNOReminderKey") {
  96. historyFileDeleteAction(selects)
  97. } else {
  98. let historyFileDeleteVC: KMHistoryFileDeleteWindowController = KMHistoryFileDeleteWindowController.init(windowNibName: NSNib.Name("KMHistoryFileDeleteWindowController"))
  99. historyFileDeleteVC.indexPaths = selects
  100. self.currentWindowController = historyFileDeleteVC
  101. historyFileDeleteVC.deleteCallback = { [weak self](indexPaths: [URL], windowController: KMHistoryFileDeleteWindowController) -> Void in
  102. if self != nil {
  103. self?.currentWindowController = nil
  104. self!.historyFileDeleteAction(indexPaths)
  105. }
  106. }
  107. self.view.window?.beginSheet(historyFileDeleteVC.window!)
  108. }
  109. }
  110. func historyFileDeleteAction(_ indexPaths: [URL]) -> Void {
  111. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  112. NSDocumentController.shared.clearRecentDocuments(nil)
  113. DispatchQueue.main.asyncAfter(deadline: .now()) { [self] in
  114. for (_, url) in urls.enumerated() {
  115. if !indexPaths.contains(url) {
  116. NSDocumentController.shared.noteNewRecentDocumentURL(url)
  117. }
  118. }
  119. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMHomeFileRectChange"), object: self.view.window)
  120. }
  121. }
  122. @IBAction func favoriteButtonAction(_ sender: NSButton) {
  123. print("收藏文件按钮")
  124. }
  125. }