KMHomeHistoryFileViewController.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. //
  2. // KMHomeHistoryFileViewController.swift
  3. // PDF Office
  4. //
  5. // Created by wanjun on 2022/10/28.
  6. //
  7. import Cocoa
  8. public enum HistoryFileShowMode : Int {
  9. case Thumbnail = 0
  10. case List
  11. }
  12. @objc protocol KMHomeHistoryFileViewControllerDelegate {
  13. @objc optional func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, deleteDocuments indexPaths: [URL])
  14. @objc optional func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, didSelectItemsAt indexPaths: [URL])
  15. }
  16. class KMHomeHistoryFileTableviewCell: NSTableCellView {
  17. @IBOutlet weak var fileImageView: NSImageView!
  18. @IBOutlet weak var documentName: NSTextField!
  19. @IBOutlet weak var documentType: NSTextField!
  20. @IBOutlet weak var documentSize: NSTextField!
  21. @IBOutlet weak var moreButton: NSButton!
  22. @IBOutlet weak var lastModificationTime: NSTextField!
  23. @IBOutlet weak var mainBox: KMBox!
  24. var file: URL?
  25. var selectUrls: [URL] = []
  26. var isSelect: Bool = false
  27. // MARK: Init
  28. override func awakeFromNib() {
  29. super.awakeFromNib()
  30. mainBox.menu = tableCellMenu
  31. documentName.maximumNumberOfLines = 1
  32. mainBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  33. if self != nil {
  34. if !self!.isSelect {
  35. if mouseEntered {
  36. self!.documentName.textColor = NSColor(hex: "#252629")
  37. self!.documentType.textColor = NSColor(hex: "#94989C")
  38. self!.documentSize.textColor = NSColor(hex: "#94989C")
  39. self!.lastModificationTime.textColor = NSColor(hex: "#94989C")
  40. self!.mainBox.fillColor = NSColor(hex: "#CED0D4", alpha: 0.6)
  41. self!.mainBox.cornerRadius = 4.0
  42. } else {
  43. self!.documentName.textColor = NSColor(hex: "#252629")
  44. self!.documentType.textColor = NSColor(hex: "#94989C")
  45. self!.documentSize.textColor = NSColor(hex: "#94989C")
  46. self!.lastModificationTime.textColor = NSColor(hex: "#94989C")
  47. self!.mainBox.fillColor = .clear
  48. self!.mainBox.cornerRadius = 0.0
  49. }
  50. }
  51. }
  52. }
  53. }
  54. // MARK: Private Methods
  55. func initializeUI(_ url: URL) -> Void {
  56. file = url
  57. let attrib = try? FileManager.default.attributesOfItem(atPath: url.path) as? Dictionary<FileAttributeKey , Any>
  58. let dateFormatter: DateFormatter = DateFormatter.init()
  59. let fileDate: Date = attrib![FileAttributeKey(rawValue: "NSFileModificationDate")] as! Date
  60. var fileTime: String = ""
  61. if fileDate.isToday() {
  62. dateFormatter.dateFormat = "HH:mm"
  63. } else if isSameWeek(withDate: fileDate) {
  64. dateFormatter.dateFormat = "EEE, HH:mm"
  65. } else {
  66. dateFormatter.dateFormat = "MMM d, yyyy"
  67. }
  68. let fileName = url.lastPathComponent
  69. let fileType = url.pathExtension.isEmpty ? "" : url.pathExtension
  70. let sizeFloat: Float = attrib![FileAttributeKey(rawValue: "NSFileSize")] as? Float ?? 0.0
  71. let fileSize = fileSizeString(sizeFloat).isEmpty ? "" : fileSizeString(sizeFloat)
  72. let lastTime = dateFormatter.string(from: fileDate)
  73. if fileDate.isToday() {
  74. fileTime = String(format: "%@, %@", NSLocalizedString("Today", comment: ""), lastTime)
  75. } else if isSameWeek(withDate: fileDate) {
  76. fileTime = lastTime
  77. } else {
  78. fileTime = lastTime
  79. }
  80. let paragraphStyle = NSMutableParagraphStyle()
  81. paragraphStyle.lineSpacing = 22.0
  82. documentName.attributedStringValue = NSAttributedString(string: fileName, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  83. documentType.attributedStringValue = NSAttributedString(string: fileType, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  84. documentSize.attributedStringValue = NSAttributedString(string: fileSize, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  85. lastModificationTime.attributedStringValue = NSAttributedString(string: fileTime, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  86. mainBox.toolTip = fileName
  87. if selectUrls.contains(url) {
  88. isSelect = true
  89. mainBox.fillColor = NSColor(hex: "#CED0D4")
  90. mainBox.cornerRadius = 4.0
  91. } else {
  92. isSelect = false
  93. mainBox.fillColor = .clear
  94. mainBox.cornerRadius = 0.0
  95. }
  96. documentName.backgroundColor = .clear
  97. documentName.textColor = NSColor(hex: "#252629")
  98. documentName.font = NSFont(name: "SFProText-Regular", size: 14)
  99. documentType.textColor = NSColor(hex: "#94989C")
  100. documentSize.textColor = NSColor(hex: "#94989C")
  101. documentName.backgroundColor = .clear
  102. lastModificationTime.textColor = NSColor(hex: "#94989C")
  103. lastModificationTime.backgroundColor = .clear
  104. moreButton.image = NSImage(named: "KMHomeMoreTools")
  105. }
  106. func highlightCell(_ rows: Array<Int>, _ row: Int) -> Void {
  107. for i in rows {
  108. if i == row {
  109. mainBox.fillColor = NSColor(hex: "#CED0D4", alpha: 0.6)
  110. }
  111. }
  112. }
  113. func fileSizeString(_ fSize: Float) -> String {
  114. let fileSize = fSize / 1024
  115. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  116. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  117. return String(format: "%0.1f %@", size, unit)
  118. }
  119. func isSameWeek (withDate date: Date) -> Bool {
  120. let currentWeekOfYear = getWeekOfYear(date: Date.init())
  121. let targetWeekOfYear = getWeekOfYear(date: date)
  122. if targetWeekOfYear == currentWeekOfYear {
  123. return false
  124. } else {
  125. return true
  126. }
  127. }
  128. func getWeekOfYear(date: Date) -> Int {
  129. let components = Calendar.current.dateComponents([Calendar.Component.weekOfYear], from: date)
  130. return components.weekOfYear ?? 0
  131. }
  132. // MARK: Menu
  133. lazy var tableCellMenu: NSMenu = {
  134. let tableCellMenu = NSMenu()
  135. var item = NSMenuItem()
  136. item.title = NSLocalizedString("Show in Finder", comment: "")
  137. item.action = #selector(buttonItemClick_ShowPath)
  138. tableCellMenu.addItem(item)
  139. item = NSMenuItem()
  140. item.title = NSLocalizedString("Remove from Recents", comment: "")
  141. item.action = #selector(buttonItemClick_ClosePath)
  142. tableCellMenu.addItem(item)
  143. return tableCellMenu
  144. }()
  145. // MARK: Action
  146. @IBAction func moreButtonAction(_ sender: NSButton) {
  147. let popViewDataArr = [NSLocalizedString("Show in Finder", comment: ""),
  148. NSLocalizedString("Remove from Recents", comment: "")]
  149. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(popViewDataArr)
  150. let createFilePopover: NSPopover = NSPopover.init()
  151. createFilePopover.contentViewController = vc
  152. createFilePopover.animates = true
  153. createFilePopover.behavior = .semitransient
  154. createFilePopover.show(relativeTo: CGRect(x: moreButton.bounds.origin.x, y: 10, width: moreButton.bounds.size.width, height: moreButton.bounds.size.height), of: moreButton, preferredEdge: .minY)
  155. vc.downCallback = { [weak self](downEntered: Bool, count: String) -> Void in
  156. if self != nil {
  157. if downEntered {
  158. if count == NSLocalizedString("Show in Finder", comment: "") {
  159. self!.buttonItemClick_ShowPath(self!.moreButton as Any)
  160. } else if count == NSLocalizedString("Remove from Recents", comment: "") {
  161. self!.buttonItemClick_ClosePath(self!.moreButton as Any)
  162. }
  163. }
  164. }
  165. }
  166. }
  167. @IBAction func buttonItemClick_ShowPath(_ sender: Any) {
  168. if FileManager.default.fileExists(atPath: file!.path) {
  169. NSWorkspace.shared.activateFileViewerSelecting([file!])
  170. }
  171. }
  172. @IBAction func buttonItemClick_ClosePath(_ sender: Any) {
  173. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  174. NSDocumentController.shared.clearRecentDocuments(nil)
  175. DispatchQueue.main.asyncAfter(deadline: .now()) { [self] in
  176. for (_, url) in urls.enumerated() {
  177. if url.path != file!.path {
  178. NSDocumentController.shared.noteNewRecentDocumentURL(url)
  179. }
  180. }
  181. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMHomeFileRectChange"), object: self.window)
  182. }
  183. }
  184. }
  185. class KMHomeHistoryFileViewController: NSViewController, NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout, NSTableViewDelegate, NSTableViewDataSource {
  186. @IBOutlet weak var historyFileLabel: NSTextField!
  187. @IBOutlet weak var modeBox: NSBox!
  188. @IBOutlet weak var modeBoxHeight: NSLayoutConstraint!
  189. @IBOutlet weak var thumbnailBox: KMBox!
  190. @IBOutlet weak var thumbnailImageView: NSImageView!
  191. @IBOutlet weak var listBox: KMBox!
  192. @IBOutlet weak var listImageView: NSImageView!
  193. @IBOutlet weak var deleteBox: KMBox!
  194. @IBOutlet weak var deleteBoxHeight: NSLayoutConstraint!
  195. @IBOutlet weak var deleteImageView: NSImageView!
  196. @IBOutlet weak var thumbnailSCrollView: NSScrollView!
  197. @IBOutlet weak var historyFileCollectionView: KMHistoryFileCollectionView!
  198. @IBOutlet weak var listScrollView: NSScrollView!
  199. @IBOutlet weak var historyFileTableView: KMHistoryFileTableView!
  200. @IBOutlet weak var emptyMainBox: NSBox!
  201. @IBOutlet weak var emptyBox: NSBox!
  202. @IBOutlet weak var emptyImageView: NSImageView!
  203. @IBOutlet weak var emptyTitleLabel: NSTextField!
  204. @IBOutlet weak var emptySubtitleLabel: NSTextField!
  205. @IBOutlet weak var emptyHovBox: KMMoveBox!
  206. var files: [Any] = []
  207. var selectFiles: [URL] = []
  208. var selectFiles_shift: [Int] = []
  209. var showMode: HistoryFileShowMode!
  210. open weak var delete: KMHomeHistoryFileViewControllerDelegate?
  211. var allowMultipleChoices_cmd: Bool!
  212. var allowMultipleChoices_shift: Bool!
  213. var multipleChoices: [KMBox] = []
  214. var multipleChoicesInts: [Int] = []
  215. var deleteButtonVC: KMDesignButton!
  216. // MARK: Init
  217. override func viewDidLoad() {
  218. super.viewDidLoad()
  219. // Do view setup here.
  220. deleteButtonVC = KMDesignButton.init(withType: .Image)
  221. historyFileCollectionView.register(KMHistoryFileCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileCollectionViewItem"))
  222. historyFileTableView.doubleAction = #selector(doubleClick(_:))
  223. initializeUI()
  224. initLocalization()
  225. allowMultipleChoices_cmd = false
  226. allowMultipleChoices_shift = false
  227. if UserDefaults.standard.bool(forKey: "kFileListViewListModeKey") {
  228. showMode = .List
  229. } else {
  230. showMode = .Thumbnail
  231. }
  232. refreshMode()
  233. self.listBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox) -> Void in
  234. if self != nil {
  235. if downEntered {
  236. if (self!.showMode == .Thumbnail) {
  237. self!.showMode = .List
  238. self!.refreshMode()
  239. }
  240. }
  241. }
  242. }
  243. self.thumbnailBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox) -> Void in
  244. if self != nil {
  245. if downEntered {
  246. if (self!.showMode == .List) {
  247. self!.showMode = .Thumbnail
  248. self!.refreshMode()
  249. }
  250. }
  251. }
  252. }
  253. self.deleteBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox) -> Void in
  254. if self != nil {
  255. if self!.deleteButtonVC.enabled {
  256. if downEntered {
  257. self!.deleteButtonVC.state = .Sel
  258. } else {
  259. self!.deleteButtonVC.state = .Norm
  260. }
  261. self!.deleteButtonVC.updateUI()
  262. }
  263. }
  264. }
  265. self.emptyHovBox.move = { [unowned self](mouseEntered: Bool) -> Void in
  266. if mouseEntered {
  267. emptyImageView.image = NSImage(named: "icon_empty_add_hov")
  268. } else {
  269. emptyImageView.image = NSImage(named: "icon_empty_add_norm")
  270. }
  271. }
  272. }
  273. override func viewDidAppear() {
  274. super.viewDidAppear()
  275. reloadData()
  276. }
  277. func initializeUI() {
  278. deleteBox.fillColor = .clear
  279. deleteBox.contentView = deleteButtonVC.view
  280. deleteButtonVC.target = self
  281. deleteButtonVC.action = #selector(deleteAction(_:))
  282. deleteButtonVC.image = NSImage(named: "icon_btn_clear_false_norm")!
  283. deleteButtonVC.button(type: .Sec, size: .m, height: deleteBoxHeight)
  284. historyFileLabel.font = NSFont(name: "SFProText-Semibold", size: 20)
  285. historyFileLabel.textColor = NSColor(red: 37/255.0, green: 38/255.0, blue: 41/255.0, alpha: 1.0)
  286. modeBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg")
  287. modeBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  288. emptyImageView.image = NSImage(named: "icon_empty_add_norm")
  289. emptyTitleLabel.textColor = NSColor(hex: "#616469")
  290. emptyTitleLabel.font = NSFont(name: "SFProText-Regular", size: 14.0)
  291. emptySubtitleLabel.textColor = NSColor(hex: "#94989C")
  292. emptySubtitleLabel.font = NSFont(name: "SFProText-Regular", size: 12.0)
  293. }
  294. func initLocalization() {
  295. let fastToolParagraphStyle = NSMutableParagraphStyle()
  296. fastToolParagraphStyle.lineSpacing = 28.0
  297. historyFileLabel.attributedStringValue = NSAttributedString(string: NSLocalizedString("Recent", comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: fastToolParagraphStyle])
  298. emptyTitleLabel.stringValue = NSLocalizedString("No recently opened file", comment: "")
  299. emptySubtitleLabel.stringValue = NSLocalizedString("Click Open File or drag-and-drop file here to open them.", comment: "")
  300. }
  301. // MARK: Get && Set
  302. // MARK: Private Methods
  303. func refreshMode() {
  304. switch showMode {
  305. case .Thumbnail:
  306. listBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg-item.unsel")
  307. listBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  308. thumbnailBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg-item.sel")
  309. thumbnailBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  310. listScrollView.isHidden = true
  311. thumbnailSCrollView.isHidden = false
  312. UserDefaults.standard.setValue(false, forKey: "kFileListViewListModeKey")
  313. break
  314. case .List:
  315. listBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg-item.sel")
  316. listBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  317. thumbnailBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg-item.unsel")
  318. thumbnailBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  319. listScrollView.isHidden = false
  320. thumbnailSCrollView.isHidden = true
  321. UserDefaults.standard.setValue(true, forKey: "kFileListViewListModeKey")
  322. break
  323. default:
  324. break
  325. }
  326. UserDefaults.standard.synchronize()
  327. reloadData()
  328. }
  329. func reloadData() -> Void {
  330. files.removeAll()
  331. for url in NSDocumentController.shared.recentDocumentURLs {
  332. if FileManager.default.fileExists(atPath: url.path) {
  333. self.files.append(url)
  334. }
  335. }
  336. let fileNumber = KMPreferenceManager.shared.getData(forKey: KMDocumentMaximunDisplayNumberKey) as? Int ?? 10
  337. if fileNumber <= files.count {
  338. let arr1 = files.prefix(fileNumber)
  339. self.files = Array(arr1)
  340. }
  341. thumbnailSCrollView.isHidden = true
  342. listScrollView.isHidden = true
  343. emptyMainBox.isHidden = true
  344. if files.count > 0 {
  345. deleteButtonVC.enabled = true
  346. deleteButtonVC.state = .Norm
  347. if showMode == .Thumbnail {
  348. thumbnailSCrollView.isHidden = false
  349. historyFileCollectionView.reloadData()
  350. } else {
  351. listScrollView.isHidden = false
  352. historyFileTableView.reloadData()
  353. }
  354. } else {
  355. deleteButtonVC.enabled = false
  356. deleteButtonVC.state = .Disabled
  357. emptyMainBox.isHidden = false
  358. }
  359. deleteButtonVC.updateUI()
  360. }
  361. func multipleChoicesAction(choiceBox box: KMBox, choiceRow row: Int) -> Void {
  362. if allowMultipleChoices_cmd {
  363. if multipleChoices.contains(box) {
  364. box.fillColor = .clear
  365. multipleChoices.removeObject(box)
  366. } else {
  367. multipleChoices.append(box)
  368. }
  369. for box in multipleChoices {
  370. box.fillColor = NSColor(red: 37/255.0, green: 139/255.0, blue: 251/255.0, alpha: 1.0)
  371. }
  372. } else if allowMultipleChoices_shift {
  373. for box in multipleChoices {
  374. box.fillColor = NSColor(red: 37/255.0, green: 139/255.0, blue: 251/255.0, alpha: 1.0)
  375. }
  376. } else {
  377. for iBox in multipleChoices {
  378. iBox.fillColor = .clear
  379. }
  380. multipleChoices.removeAll()
  381. multipleChoices.append(box)
  382. box.fillColor = NSColor(red: 37/255.0, green: 139/255.0, blue: 251/255.0, alpha: 1.0)
  383. }
  384. multipleChoicesIntsAction(choiceRow: row)
  385. }
  386. func multipleChoicesIntsAction(choiceRow row: Int) -> Void {
  387. if allowMultipleChoices_cmd {
  388. if multipleChoicesInts.contains(row) {
  389. multipleChoicesInts.removeObject(row)
  390. } else {
  391. multipleChoicesInts.append(row)
  392. }
  393. } else if allowMultipleChoices_shift {
  394. } else {
  395. multipleChoicesInts.removeAll()
  396. multipleChoicesInts.append(row)
  397. }
  398. }
  399. override func flagsChanged(with event: NSEvent) {
  400. super.flagsChanged(with: event)
  401. if event.type == .flagsChanged {
  402. if event.keyCode == 55 { // cmd
  403. if allowMultipleChoices_cmd {
  404. selectFiles_shift.removeAll()
  405. allowMultipleChoices_cmd = false
  406. } else {
  407. allowMultipleChoices_cmd = true
  408. allowMultipleChoices_shift = false
  409. }
  410. } else if event.keyCode == 56 { // shift
  411. if allowMultipleChoices_shift {
  412. selectFiles_shift.removeAll()
  413. allowMultipleChoices_shift = false
  414. } else {
  415. allowMultipleChoices_shift = true
  416. allowMultipleChoices_cmd = false
  417. }
  418. }
  419. }
  420. }
  421. // MARK: Action
  422. @IBAction func deleteAction(_ sender: NSButton) -> Void {
  423. if selectFiles.count == 0 {
  424. for url in NSDocumentController.shared.recentDocumentURLs {
  425. if FileManager.default.fileExists(atPath: url.path) {
  426. selectFiles.append(url)
  427. }
  428. }
  429. self.delete?.historyFileViewController!(self, deleteDocuments: selectFiles)
  430. } else if selectFiles.count > 0 {
  431. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  432. NSDocumentController.shared.clearRecentDocuments(nil)
  433. DispatchQueue.main.asyncAfter(deadline: .now()) { [self] in
  434. for (_, url) in urls.enumerated() {
  435. if !selectFiles.contains(url) {
  436. NSDocumentController.shared.noteNewRecentDocumentURL(url)
  437. }
  438. }
  439. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMHomeFileRectChange"), object: NSApp.mainWindow)
  440. }
  441. }
  442. }
  443. @IBAction func openFileAction(_ sender: NSButton) {
  444. let openPanel = NSOpenPanel()
  445. openPanel.allowedFileTypes = ["pdf", "PDF"]
  446. openPanel.allowsMultipleSelection = false
  447. openPanel.beginSheetModal(for: self.view.window!) { result in
  448. if result == .OK {
  449. NSDocumentController.shared.openDocument(withContentsOf: openPanel.url!, display: true) { document, documentWasAlreadyOpen, error in
  450. if error != nil {
  451. NSApp.presentError(error!)
  452. } else {
  453. }
  454. }
  455. }
  456. }
  457. }
  458. @IBAction func doubleClick(_ sender: Any) -> Void {
  459. if sender is KMHistoryFileTableView {
  460. selectFiles.removeAll()
  461. let rowNumber = historyFileTableView.clickedRow
  462. var indexs: [URL] = []
  463. let index: URL = files[rowNumber] as! URL
  464. indexs.append(index)
  465. self.delete?.historyFileViewController!(self, didSelectItemsAt: indexs)
  466. }
  467. }
  468. // MARK: NSCollectionViewDataSource
  469. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  470. return files.count
  471. }
  472. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  473. if collectionView.isEqual(to: historyFileCollectionView) {
  474. let item: KMHistoryFileCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileCollectionViewItem"), for: indexPath) as! KMHistoryFileCollectionViewItem
  475. let i: Int = indexPath.item
  476. let url: URL = files[i] as! URL
  477. item.selectUrls = selectFiles
  478. item.refreshUI(url)
  479. item.mainBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox) -> Void in
  480. if self != nil {
  481. if downEntered {
  482. if self!.allowMultipleChoices_shift {
  483. if self!.selectFiles_shift.count == 0 {
  484. self!.selectFiles.append(url)
  485. self!.selectFiles_shift.append(i)
  486. } else if self!.selectFiles_shift.count == 1 {
  487. let first = self!.selectFiles_shift[0] as Int
  488. self!.selectFiles.removeAll()
  489. if first > i {
  490. for path in self!.files[i...first] {
  491. self!.selectFiles.append(path as! URL)
  492. }
  493. } else {
  494. for path in self!.files[first...i] {
  495. self!.selectFiles.append(path as! URL)
  496. }
  497. }
  498. self!.selectFiles_shift.append(i)
  499. } else if self!.selectFiles_shift.count == 2 {
  500. let first = self!.selectFiles_shift[0] as Int
  501. self!.selectFiles.removeAll()
  502. if first > i {
  503. for path in self!.files[i...first] {
  504. self!.selectFiles.append(path as! URL)
  505. }
  506. } else {
  507. for path in self!.files[first...i] {
  508. self!.selectFiles.append(path as! URL)
  509. }
  510. }
  511. self!.selectFiles_shift[1] = i
  512. }
  513. } else if self!.allowMultipleChoices_cmd {
  514. if self!.selectFiles.contains(url) {
  515. self!.selectFiles.removeObject(url)
  516. } else {
  517. self!.selectFiles.append(url)
  518. }
  519. if self!.selectFiles_shift.contains(i) {
  520. self!.selectFiles_shift.removeObject(i)
  521. } else {
  522. self!.selectFiles_shift.append(i)
  523. }
  524. } else {
  525. self!.selectFiles.removeAll()
  526. self!.selectFiles_shift.removeAll()
  527. self!.selectFiles.append(url)
  528. self!.selectFiles_shift.append(i)
  529. }
  530. collectionView.reloadData()
  531. }
  532. }
  533. }
  534. item.mainBox.doubleCallback = { [weak self](downEntered: Bool, mouseBox: KMBox) -> Void in
  535. if self != nil {
  536. if downEntered {
  537. self!.selectFiles.removeAll()
  538. var indexs: [URL] = []
  539. let index: URL = self!.files[i] as! URL
  540. indexs.append(index)
  541. self!.delete?.historyFileViewController!(self!, didSelectItemsAt: indexs)
  542. }
  543. }
  544. }
  545. return item
  546. }
  547. return NSCollectionViewItem.init()
  548. }
  549. // MARK: NSCollectionViewDelegate
  550. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  551. }
  552. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  553. if collectionView.isEqual(to: historyFileCollectionView) {
  554. return CGSize(width: 226, height: 248)
  555. }
  556. return CGSize(width: 226, height: 248)
  557. }
  558. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  559. return 16
  560. }
  561. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  562. return 16
  563. }
  564. // MARK: NSTableViewDelegate
  565. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  566. return 72.0;
  567. }
  568. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  569. let url: URL = files[row] as! URL
  570. let identifier = tableColumn?.identifier
  571. let cellView: KMHomeHistoryFileTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMHomeHistoryFileTableviewCell
  572. cellView.selectUrls = selectFiles
  573. cellView.initializeUI(url)
  574. cellView.highlightCell(multipleChoicesInts, row)
  575. cellView.mainBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox) -> Void in
  576. if self != nil {
  577. if downEntered {
  578. if self!.allowMultipleChoices_shift {
  579. if self!.selectFiles_shift.count == 0 {
  580. self!.selectFiles.append(url)
  581. self!.selectFiles_shift.append(row)
  582. } else if self!.selectFiles_shift.count == 1 {
  583. let first = self!.selectFiles_shift[0] as Int
  584. self!.selectFiles.removeAll()
  585. if first > row {
  586. for path in self!.files[row...first] {
  587. self!.selectFiles.append(path as! URL)
  588. }
  589. } else {
  590. for path in self!.files[first...row] {
  591. self!.selectFiles.append(path as! URL)
  592. }
  593. }
  594. self!.selectFiles_shift.append(row)
  595. } else if self!.selectFiles_shift.count == 2 {
  596. let first = self!.selectFiles_shift[0] as Int
  597. self!.selectFiles.removeAll()
  598. if first > row {
  599. for path in self!.files[row...first] {
  600. self!.selectFiles.append(path as! URL)
  601. }
  602. } else {
  603. for path in self!.files[first...row] {
  604. self!.selectFiles.append(path as! URL)
  605. }
  606. }
  607. self!.selectFiles_shift[1] = row
  608. }
  609. } else if self!.allowMultipleChoices_cmd {
  610. if self!.selectFiles.contains(url) {
  611. self!.selectFiles.removeObject(url)
  612. } else {
  613. self!.selectFiles.append(url)
  614. }
  615. if self!.selectFiles_shift.contains(row) {
  616. self!.selectFiles_shift.removeObject(row)
  617. } else {
  618. self!.selectFiles_shift.append(row)
  619. }
  620. } else {
  621. self!.selectFiles.removeAll()
  622. self!.selectFiles_shift.removeAll()
  623. self!.selectFiles.append(url)
  624. self!.selectFiles_shift.append(row)
  625. }
  626. tableView.reloadData()
  627. }
  628. }
  629. }
  630. return cellView
  631. }
  632. // MARK: NSTableViewDataSource
  633. func numberOfRows(in tableView: NSTableView) -> Int {
  634. return files.count
  635. }
  636. }