KMHistoryFileCollectionViewItem.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // MARK: init
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. // Do view setup here.
  22. initializeUI()
  23. mainBox.menu = tableCellMenu
  24. documentName.maximumNumberOfLines = 2
  25. mainBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  26. if !self.isSelect {
  27. if mouseEntered {
  28. self.mainBox.fillColor = NSColor(hex: "#EDEEF0")
  29. self.mainBox.borderWidth = 0.0
  30. self.documentName.textColor = NSColor(hex: "#252629")
  31. self.mainBox.cornerRadius = 8.0
  32. } else {
  33. self.mainBox.fillColor = .clear
  34. self.mainBox.borderWidth = 0.0
  35. self.documentName.textColor = NSColor(hex: "#252629")
  36. self.mainBox.cornerRadius = 0.0
  37. }
  38. }
  39. }
  40. }
  41. func initializeUI() {
  42. }
  43. // MARK: private
  44. func refreshUI(_ path: URL) {
  45. filePathUrl = path
  46. if selectUrls.contains(path) {
  47. isSelect = true
  48. mainBox.fillColor = NSColor(hex: "#CED0D4", alpha: 0.6)
  49. mainBox.borderWidth = 1.0
  50. mainBox.borderColor = NSColor(hex: "#CED0D4")
  51. mainBox.cornerRadius = 8.0
  52. } else {
  53. isSelect = false
  54. mainBox.fillColor = .clear
  55. mainBox.borderWidth = 0.0
  56. mainBox.cornerRadius = 0.0
  57. }
  58. favoriteButton.isHidden = true
  59. imageBox.fillColor = .clear
  60. documentName.textColor = NSColor(hex: "#252629")
  61. documentName.stringValue = filePathUrl!.lastPathComponent
  62. mainBox.toolTip = filePathUrl!.lastPathComponent
  63. let image: NSImage = NSImage.previewForFile(path: filePathUrl!, ofSize: fileImageView.frame.size, asIcon: true) ?? NSImage()
  64. fileImageView.image = image
  65. }
  66. // MARK: Menu
  67. lazy var tableCellMenu: NSMenu = {
  68. let tableCellMenu = NSMenu()
  69. var item = NSMenuItem()
  70. item.title = NSLocalizedString("Show in Finder", comment: "")
  71. item.action = #selector(buttonItemClick_ShowPath)
  72. tableCellMenu.addItem(item)
  73. item = NSMenuItem()
  74. item.title = NSLocalizedString("Remove from Recents", comment: "")
  75. item.action = #selector(buttonItemClick_ClosePath)
  76. tableCellMenu.addItem(item)
  77. return tableCellMenu
  78. }()
  79. // MARK: Action
  80. @IBAction func buttonItemClick_ShowPath(sender: AnyObject) {
  81. if FileManager.default.fileExists(atPath: filePathUrl!.path) {
  82. NSWorkspace.shared.activateFileViewerSelecting([filePathUrl!])
  83. }
  84. }
  85. @IBAction func buttonItemClick_ClosePath(sender: AnyObject) {
  86. var selects: [String] = []
  87. if selectUrls.count > 0 {
  88. for selecturl in self.selectUrls {
  89. selects.append(selecturl.path)
  90. }
  91. } else {
  92. selects.append(filePathUrl!.path)
  93. }
  94. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  95. NSDocumentController.shared.clearRecentDocuments(nil)
  96. DispatchQueue.main.asyncAfter(deadline: .now()) { [self] in
  97. for (_, url) in urls.enumerated() {
  98. if !selects.contains(url.path) {
  99. NSDocumentController.shared.noteNewRecentDocumentURL(url)
  100. }
  101. }
  102. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMHomeFileRectChange"), object: self.view.window)
  103. }
  104. }
  105. @IBAction func favoriteButtonAction(_ sender: NSButton) {
  106. print("收藏文件按钮")
  107. }
  108. }