KMHistoryFileCollectionViewItem.swift 5.2 KB

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