KMHistoryFileCollectionViewItem.swift 5.9 KB

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