KMHistoryFileCollectionViewItem.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: update
  68. // MARK: Menu
  69. lazy var tableCellMenu: NSMenu = {
  70. let tableCellMenu = NSMenu()
  71. var item = NSMenuItem()
  72. item.title = NSLocalizedString("Show in Finder", comment: "")
  73. item.action = #selector(buttonItemClick_ShowPath)
  74. tableCellMenu.addItem(item)
  75. item = NSMenuItem()
  76. item.title = NSLocalizedString("Remove from Recents", comment: "")
  77. item.action = #selector(buttonItemClick_ClosePath)
  78. tableCellMenu.addItem(item)
  79. return tableCellMenu
  80. }()
  81. // MARK: Action
  82. @IBAction func buttonItemClick_ShowPath(sender: AnyObject) {
  83. if FileManager.default.fileExists(atPath: filePathUrl!.path) {
  84. NSWorkspace.shared.activateFileViewerSelecting([filePathUrl!])
  85. }
  86. }
  87. @IBAction func buttonItemClick_ClosePath(sender: AnyObject) {
  88. var selects: [URL] = []
  89. if selectUrls.count > 0 {
  90. for selecturl in self.selectUrls {
  91. selects.append(selecturl)
  92. }
  93. } else {
  94. selects.append(filePathUrl!)
  95. }
  96. if UserDefaults.standard.bool(forKey: "kHistoryDeleteNOReminderKey") {
  97. historyFileDeleteAction(selects)
  98. } else {
  99. let historyFileDeleteVC: KMHistoryFileDeleteWindowController = KMHistoryFileDeleteWindowController.init(windowNibName: NSNib.Name("KMHistoryFileDeleteWindowController"))
  100. historyFileDeleteVC.indexPaths = selects
  101. self.currentWindowController = historyFileDeleteVC
  102. historyFileDeleteVC.deleteCallback = { [weak self](indexPaths: [URL], windowController: KMHistoryFileDeleteWindowController) -> Void in
  103. if self != nil {
  104. self?.currentWindowController = nil
  105. self!.historyFileDeleteAction(indexPaths)
  106. }
  107. }
  108. self.view.window?.beginSheet(historyFileDeleteVC.window!)
  109. }
  110. }
  111. func historyFileDeleteAction(_ indexPaths: [URL]) -> Void {
  112. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  113. NSDocumentController.shared.clearRecentDocuments(nil)
  114. DispatchQueue.main.asyncAfter(deadline: .now()) { [self] in
  115. for (_, url) in urls.enumerated() {
  116. if !indexPaths.contains(url) {
  117. NSDocumentController.shared.noteNewRecentDocumentURL(url)
  118. }
  119. }
  120. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMHomeFileRectChange"), object: self.view.window)
  121. }
  122. }
  123. @IBAction func favoriteButtonAction(_ sender: NSButton) {
  124. print("收藏文件按钮")
  125. }
  126. }