KMHomeHistoryFileViewController.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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.setValue(true, forKey: "shouldHideAnchor")
  160. 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)
  161. vc.customBox.fillColor = NSColor(hex: "#F6F6F6", alpha: 0.6)
  162. vc.downCallback = { [weak self](downEntered: Bool, count: String) -> Void in
  163. if self != nil {
  164. if downEntered {
  165. if count == NSLocalizedString("Show in Finder", comment: "") {
  166. self!.buttonItemClick_ShowPath(self!.moreButton as Any)
  167. } else if count == NSLocalizedString("Remove from Recents", comment: "") {
  168. self!.buttonItemClick_ClosePath(self!.moreButton as Any)
  169. }
  170. }
  171. }
  172. }
  173. }
  174. @IBAction func buttonItemClick_ShowPath(_ sender: Any) {
  175. if FileManager.default.fileExists(atPath: file!.path) {
  176. NSWorkspace.shared.activateFileViewerSelecting([file!])
  177. }
  178. }
  179. @IBAction func buttonItemClick_ClosePath(_ sender: Any) {
  180. var selects: [String] = []
  181. if selectUrls.count > 0 {
  182. for selecturl in self.selectUrls {
  183. selects.append(selecturl.path)
  184. }
  185. } else {
  186. selects.append(file!.path)
  187. }
  188. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  189. NSDocumentController.shared.clearRecentDocuments(nil)
  190. DispatchQueue.main.asyncAfter(deadline: .now()) { [self] in
  191. for (_, url) in urls.enumerated() {
  192. if !selects.contains(url.path) {
  193. NSDocumentController.shared.noteNewRecentDocumentURL(url)
  194. }
  195. }
  196. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMHomeFileRectChange"), object: self.window)
  197. }
  198. }
  199. }
  200. class KMHomeHistoryFileViewController: NSViewController, NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout, NSTableViewDelegate, NSTableViewDataSource {
  201. @IBOutlet weak var historyFileLabel: NSTextField!
  202. @IBOutlet weak var modeBox: NSBox!
  203. @IBOutlet weak var modeBoxHeight: NSLayoutConstraint!
  204. @IBOutlet weak var thumbnailBox: KMBox!
  205. @IBOutlet weak var thumbnailImageView: NSImageView!
  206. @IBOutlet weak var listBox: KMBox!
  207. @IBOutlet weak var listImageView: NSImageView!
  208. @IBOutlet weak var deleteBox: KMBox!
  209. @IBOutlet weak var deleteBoxHeight: NSLayoutConstraint!
  210. @IBOutlet weak var deleteImageView: NSImageView!
  211. @IBOutlet weak var thumbnailSCrollView: NSScrollView!
  212. @IBOutlet weak var historyFileCollectionView: KMHistoryFileCollectionView!
  213. @IBOutlet weak var listScrollView: NSScrollView!
  214. @IBOutlet weak var historyFileTableView: KMHistoryFileTableView!
  215. @IBOutlet weak var emptyMainBox: NSBox!
  216. @IBOutlet weak var emptyBox: NSBox!
  217. @IBOutlet weak var emptyImageView: NSImageView!
  218. @IBOutlet weak var emptyTitleLabel: NSTextField!
  219. @IBOutlet weak var emptySubtitleLabel: NSTextField!
  220. @IBOutlet weak var emptyHovBox: KMMoveBox!
  221. var files: [Any] = []
  222. var selectFiles: [URL] = []
  223. var selectFiles_shift: [Int] = []
  224. var showMode: HistoryFileShowMode!
  225. open weak var delete: KMHomeHistoryFileViewControllerDelegate?
  226. var allowMultipleChoices_cmd: Bool!
  227. var allowMultipleChoices_shift: Bool!
  228. var multipleChoices: [KMBox] = []
  229. var multipleChoicesInts: [Int] = []
  230. var deleteButtonVC: KMDesignButton!
  231. // MARK: Init
  232. override func viewDidLoad() {
  233. super.viewDidLoad()
  234. // Do view setup here.
  235. deleteButtonVC = KMDesignButton.init(withType: .Image)
  236. historyFileCollectionView.register(KMHistoryFileCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileCollectionViewItem"))
  237. initializeUI()
  238. initLocalization()
  239. allowMultipleChoices_cmd = false
  240. allowMultipleChoices_shift = false
  241. if UserDefaults.standard.bool(forKey: "kFileListViewListModeKey") {
  242. showMode = .List
  243. } else {
  244. showMode = .Thumbnail
  245. }
  246. refreshMode()
  247. self.listBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  248. if self != nil {
  249. if downEntered {
  250. if (self!.showMode == .Thumbnail) {
  251. self!.showMode = .List
  252. self!.refreshMode()
  253. }
  254. }
  255. }
  256. }
  257. self.thumbnailBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  258. if self != nil {
  259. if downEntered {
  260. if (self!.showMode == .List) {
  261. self!.showMode = .Thumbnail
  262. self!.refreshMode()
  263. }
  264. }
  265. }
  266. }
  267. self.deleteBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  268. if self != nil {
  269. if self!.deleteButtonVC.enabled {
  270. if downEntered {
  271. self!.deleteButtonVC.state = .Sel
  272. } else {
  273. self!.deleteButtonVC.state = .Norm
  274. }
  275. self!.deleteButtonVC.updateUI()
  276. }
  277. }
  278. }
  279. self.emptyHovBox.move = { [unowned self](mouseEntered: Bool) -> Void in
  280. if mouseEntered {
  281. emptyImageView.image = NSImage(named: "icon_empty_add_hov")
  282. } else {
  283. emptyImageView.image = NSImage(named: "icon_empty_add_norm")
  284. }
  285. }
  286. }
  287. override func viewDidAppear() {
  288. super.viewDidAppear()
  289. reloadData()
  290. }
  291. func initializeUI() {
  292. deleteBox.fillColor = .clear
  293. deleteBox.contentView = deleteButtonVC.view
  294. deleteButtonVC.target = self
  295. deleteButtonVC.action = #selector(deleteAction(_:))
  296. deleteButtonVC.image = NSImage(named: "icon_btn_clear_false_norm")!
  297. deleteButtonVC.button(type: .Sec, size: .m, height: deleteBoxHeight)
  298. historyFileLabel.font = NSFont(name: "SFProText-Semibold", size: 20)
  299. historyFileLabel.textColor = NSColor(red: 37/255.0, green: 38/255.0, blue: 41/255.0, alpha: 1.0)
  300. modeBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg")
  301. modeBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  302. emptyImageView.image = NSImage(named: "icon_empty_add_norm")
  303. emptyTitleLabel.textColor = NSColor(hex: "#616469")
  304. emptyTitleLabel.font = NSFont(name: "SFProText-Regular", size: 14.0)
  305. emptySubtitleLabel.textColor = NSColor(hex: "#94989C")
  306. emptySubtitleLabel.font = NSFont(name: "SFProText-Regular", size: 12.0)
  307. }
  308. func initLocalization() {
  309. let fastToolParagraphStyle = NSMutableParagraphStyle()
  310. fastToolParagraphStyle.lineSpacing = 28.0
  311. historyFileLabel.attributedStringValue = NSAttributedString(string: NSLocalizedString("Recent", comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: fastToolParagraphStyle])
  312. emptyTitleLabel.stringValue = NSLocalizedString("No recently opened files", comment: "")
  313. emptySubtitleLabel.stringValue = NSLocalizedString("Click to open the file or drag the file directly here to open the file.", comment: "")
  314. }
  315. // MARK: Get && Set
  316. // MARK: Private Methods
  317. func refreshMode() {
  318. switch showMode {
  319. case .Thumbnail:
  320. listBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg-item.unsel")
  321. listBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  322. thumbnailBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg-item.sel")
  323. thumbnailBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  324. listScrollView.isHidden = true
  325. thumbnailSCrollView.isHidden = false
  326. UserDefaults.standard.setValue(false, forKey: "kFileListViewListModeKey")
  327. break
  328. case .List:
  329. listBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg-item.sel")
  330. listBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  331. thumbnailBox.fillColor = KMDesignToken.shared.fill(withToken: "segmented.bg-item.unsel")
  332. thumbnailBox.cornerRadius = KMDesignToken.shared.borderRadius(withToken: "segmented.bg").stringToCGFloat()
  333. listScrollView.isHidden = false
  334. thumbnailSCrollView.isHidden = true
  335. UserDefaults.standard.setValue(true, forKey: "kFileListViewListModeKey")
  336. break
  337. default:
  338. break
  339. }
  340. UserDefaults.standard.synchronize()
  341. reloadData()
  342. }
  343. func reloadData() -> Void {
  344. files.removeAll()
  345. for url in NSDocumentController.shared.recentDocumentURLs {
  346. if FileManager.default.fileExists(atPath: url.path) {
  347. self.files.append(url)
  348. }
  349. }
  350. let fileNumber = KMPreferenceManager.shared.getData(forKey: KMPreference.documentMaximunDisplayNumberKey) as? Int ?? 10
  351. if fileNumber <= files.count {
  352. let arr1 = files.prefix(fileNumber)
  353. self.files = Array(arr1)
  354. }
  355. thumbnailSCrollView.isHidden = true
  356. listScrollView.isHidden = true
  357. emptyMainBox.isHidden = true
  358. if files.count > 0 {
  359. deleteButtonVC.enabled = true
  360. deleteButtonVC.state = .Norm
  361. if showMode == .Thumbnail {
  362. thumbnailSCrollView.isHidden = false
  363. historyFileCollectionView.reloadData()
  364. } else {
  365. listScrollView.isHidden = false
  366. historyFileTableView.reloadData()
  367. }
  368. } else {
  369. deleteButtonVC.enabled = false
  370. deleteButtonVC.state = .Disabled
  371. emptyMainBox.isHidden = false
  372. historyFileCollectionView.reloadData()
  373. historyFileTableView.reloadData()
  374. }
  375. deleteButtonVC.updateUI()
  376. }
  377. func multipleChoicesAction(choiceBox box: KMBox, choiceRow row: Int) -> Void {
  378. if allowMultipleChoices_cmd {
  379. if multipleChoices.contains(box) {
  380. box.fillColor = .clear
  381. multipleChoices.removeObject(box)
  382. } else {
  383. multipleChoices.append(box)
  384. }
  385. for box in multipleChoices {
  386. box.fillColor = NSColor(red: 37/255.0, green: 139/255.0, blue: 251/255.0, alpha: 1.0)
  387. }
  388. } else if allowMultipleChoices_shift {
  389. for box in multipleChoices {
  390. box.fillColor = NSColor(red: 37/255.0, green: 139/255.0, blue: 251/255.0, alpha: 1.0)
  391. }
  392. } else {
  393. for iBox in multipleChoices {
  394. iBox.fillColor = .clear
  395. }
  396. multipleChoices.removeAll()
  397. multipleChoices.append(box)
  398. box.fillColor = NSColor(red: 37/255.0, green: 139/255.0, blue: 251/255.0, alpha: 1.0)
  399. }
  400. multipleChoicesIntsAction(choiceRow: row)
  401. }
  402. func multipleChoicesIntsAction(choiceRow row: Int) -> Void {
  403. if allowMultipleChoices_cmd {
  404. if multipleChoicesInts.contains(row) {
  405. multipleChoicesInts.removeObject(row)
  406. } else {
  407. multipleChoicesInts.append(row)
  408. }
  409. } else if allowMultipleChoices_shift {
  410. } else {
  411. multipleChoicesInts.removeAll()
  412. multipleChoicesInts.append(row)
  413. }
  414. }
  415. override func flagsChanged(with event: NSEvent) {
  416. super.flagsChanged(with: event)
  417. if event.type == .flagsChanged {
  418. if event.keyCode == 55 { // cmd
  419. if allowMultipleChoices_cmd {
  420. selectFiles_shift.removeAll()
  421. allowMultipleChoices_cmd = false
  422. } else {
  423. allowMultipleChoices_cmd = true
  424. allowMultipleChoices_shift = false
  425. }
  426. } else if event.keyCode == 56 { // shift
  427. if allowMultipleChoices_shift {
  428. selectFiles_shift.removeAll()
  429. allowMultipleChoices_shift = false
  430. } else {
  431. allowMultipleChoices_shift = true
  432. allowMultipleChoices_cmd = false
  433. }
  434. }
  435. }
  436. }
  437. // MARK: Action
  438. @IBAction func deleteAction(_ sender: NSButton) -> Void {
  439. if selectFiles.count == 0 {
  440. for url in NSDocumentController.shared.recentDocumentURLs {
  441. if FileManager.default.fileExists(atPath: url.path) {
  442. selectFiles.append(url)
  443. }
  444. }
  445. self.delete?.historyFileViewController!(self, deleteDocuments: selectFiles)
  446. } else if selectFiles.count > 0 {
  447. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  448. NSDocumentController.shared.clearRecentDocuments(nil)
  449. DispatchQueue.main.asyncAfter(deadline: .now()) { [self] in
  450. for (_, url) in urls.enumerated() {
  451. if !selectFiles.contains(url) {
  452. NSDocumentController.shared.noteNewRecentDocumentURL(url)
  453. }
  454. }
  455. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMHomeFileRectChange"), object: NSApp.mainWindow)
  456. }
  457. }
  458. }
  459. @IBAction func openFileAction(_ sender: NSButton) {
  460. let openPanel = NSOpenPanel()
  461. openPanel.allowedFileTypes = ["pdf", "PDF"]
  462. openPanel.allowsMultipleSelection = false
  463. openPanel.beginSheetModal(for: self.view.window!) { result in
  464. if result == .OK {
  465. if !openPanel.url!.path.isPDFValid() {
  466. let alert = NSAlert()
  467. alert.alertStyle = .critical
  468. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  469. alert.runModal()
  470. return
  471. }
  472. NSDocumentController.shared.openDocument(withContentsOf: openPanel.url!, display: true) { document, documentWasAlreadyOpen, error in
  473. if error != nil {
  474. NSApp.presentError(error!)
  475. } else {
  476. }
  477. }
  478. }
  479. }
  480. }
  481. // MARK: NSCollectionViewDataSource
  482. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  483. return files.count
  484. }
  485. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  486. if collectionView.isEqual(to: historyFileCollectionView) {
  487. let item: KMHistoryFileCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileCollectionViewItem"), for: indexPath) as! KMHistoryFileCollectionViewItem
  488. let i: Int = indexPath.item
  489. let url: URL = files[i] as! URL
  490. item.selectUrls = selectFiles
  491. item.refreshUI(url)
  492. item.mainBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  493. if self != nil {
  494. if downEntered {
  495. if self!.allowMultipleChoices_shift {
  496. if self!.selectFiles_shift.count == 0 {
  497. self!.selectFiles.append(url)
  498. self!.selectFiles_shift.append(i)
  499. } else if self!.selectFiles_shift.count == 1 {
  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.append(i)
  512. } else if self!.selectFiles_shift.count == 2 {
  513. let first = self!.selectFiles_shift[0] as Int
  514. self!.selectFiles.removeAll()
  515. if first > i {
  516. for path in self!.files[i...first] {
  517. self!.selectFiles.append(path as! URL)
  518. }
  519. } else {
  520. for path in self!.files[first...i] {
  521. self!.selectFiles.append(path as! URL)
  522. }
  523. }
  524. self!.selectFiles_shift[1] = i
  525. }
  526. } else if self!.allowMultipleChoices_cmd {
  527. if self!.selectFiles.contains(url) {
  528. self!.selectFiles.removeObject(url)
  529. } else {
  530. self!.selectFiles.append(url)
  531. }
  532. if self!.selectFiles_shift.contains(i) {
  533. self!.selectFiles_shift.removeObject(i)
  534. } else {
  535. self!.selectFiles_shift.append(i)
  536. }
  537. } else {
  538. self!.selectFiles.removeAll()
  539. self!.selectFiles_shift.removeAll()
  540. self!.selectFiles.append(url)
  541. self!.selectFiles_shift.append(i)
  542. }
  543. collectionView.reloadData()
  544. }
  545. }
  546. }
  547. item.mainBox.doubleCallback = { [weak self](downEntered: Bool, mouseBox: KMBox) -> Void in
  548. if self != nil {
  549. if downEntered {
  550. self!.selectFiles.removeAll()
  551. var indexs: [URL] = []
  552. let index: URL = self!.files[i] as! URL
  553. indexs.append(index)
  554. self!.delete?.historyFileViewController!(self!, didSelectItemsAt: indexs)
  555. }
  556. }
  557. }
  558. return item
  559. }
  560. return NSCollectionViewItem.init()
  561. }
  562. // MARK: NSCollectionViewDelegate
  563. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  564. }
  565. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  566. if collectionView.isEqual(to: historyFileCollectionView) {
  567. return CGSize(width: 226, height: 248)
  568. }
  569. return CGSize(width: 226, height: 248)
  570. }
  571. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  572. return 16
  573. }
  574. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  575. return 16
  576. }
  577. // MARK: NSTableViewDelegate
  578. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  579. return 72.0;
  580. }
  581. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  582. let url: URL = files[row] as! URL
  583. let identifier = tableColumn?.identifier
  584. let cellView: KMHomeHistoryFileTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMHomeHistoryFileTableviewCell
  585. cellView.selectUrls = selectFiles
  586. cellView.initializeUI(url)
  587. cellView.highlightCell(multipleChoicesInts, row)
  588. cellView.mainBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  589. if self != nil {
  590. if downEntered {
  591. if self!.allowMultipleChoices_shift {
  592. if self!.selectFiles_shift.count == 0 {
  593. self!.selectFiles.append(url)
  594. self!.selectFiles_shift.append(row)
  595. } else if self!.selectFiles_shift.count == 1 {
  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.append(row)
  608. } else if self!.selectFiles_shift.count == 2 {
  609. let first = self!.selectFiles_shift[0] as Int
  610. self!.selectFiles.removeAll()
  611. if first > row {
  612. for path in self!.files[row...first] {
  613. self!.selectFiles.append(path as! URL)
  614. }
  615. } else {
  616. for path in self!.files[first...row] {
  617. self!.selectFiles.append(path as! URL)
  618. }
  619. }
  620. self!.selectFiles_shift[1] = row
  621. }
  622. } else if self!.allowMultipleChoices_cmd {
  623. if self!.selectFiles.contains(url) {
  624. self!.selectFiles.removeObject(url)
  625. } else {
  626. self!.selectFiles.append(url)
  627. }
  628. if self!.selectFiles_shift.contains(row) {
  629. self!.selectFiles_shift.removeObject(row)
  630. } else {
  631. self!.selectFiles_shift.append(row)
  632. }
  633. } else {
  634. self!.selectFiles.removeAll()
  635. self!.selectFiles_shift.removeAll()
  636. self!.selectFiles.append(url)
  637. self!.selectFiles_shift.append(row)
  638. }
  639. tableView.reloadData()
  640. }
  641. }
  642. }
  643. cellView.mainBox.doubleCallback = { [weak self](downEntered: Bool, mouseBox: KMBox) -> Void in
  644. if self != nil {
  645. if downEntered {
  646. self!.selectFiles.removeAll()
  647. var indexs: [URL] = []
  648. let index: URL = self!.files[row] as! URL
  649. indexs.append(index)
  650. self!.delete?.historyFileViewController!(self!, didSelectItemsAt: indexs)
  651. }
  652. }
  653. }
  654. return cellView
  655. }
  656. // MARK: NSTableViewDataSource
  657. func numberOfRows(in tableView: NSTableView) -> Int {
  658. return files.count
  659. }
  660. }