KMHomeHistoryFileViewController.swift 32 KB

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