KMHistoryFileCollectionViewItem.swift 4.1 KB

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