KMHomeRightView.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. //
  2. // KMHomeRightView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/10.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc public protocol KMHomeRightViewDelegate: AnyObject {
  10. //点击管理快捷工具按钮
  11. @objc optional func homeRightViewDidManageQuickTools(_ view: KMHomeRightView)
  12. //点击快捷工具列表中的某一项
  13. @objc optional func homeRightViewDidQuickToolsItemClicked(_ view: KMHomeRightView, _ toolType: HomeQuickToolType)
  14. //最近文件列表删除更新结束后回调
  15. @objc optional func homeRightViewDidRecentFilesUpdated(_ view: KMHomeRightView)
  16. //选择打开文件
  17. @objc optional func homeRightViewDidChooseFileToOpen(_ view: KMHomeRightView, _ fileURL: URL)
  18. }
  19. public class KMHomeRightView: BaseXibView {
  20. @IBOutlet var scrollView: NSScrollView!
  21. @IBOutlet var collectionView: NSCollectionView!
  22. var filesHeaderView: KMHomeFilesHeaderView = KMHomeFilesHeaderView()
  23. var quickToolsView: KMHomeQuickToolsView = KMHomeQuickToolsView()
  24. var filesEmptyView: KMHistoryEmptyView = KMHistoryEmptyView()
  25. var groupView: ComponentGroup!
  26. var menuActionIndexPaths: Set<IndexPath> = []
  27. var groupActionView: NSView?
  28. var groupViewPoint: CGPoint = CGPointZero
  29. var refreshItemData: Bool = true
  30. weak open var delegate: KMHomeRightViewDelegate?
  31. //MARK: - func
  32. public override func draw(_ dirtyRect: NSRect) {
  33. super.draw(dirtyRect)
  34. // Drawing code here.
  35. }
  36. public required init?(coder decoder: NSCoder) {
  37. super.init(coder: decoder)
  38. }
  39. override init(frame frameRect: NSRect) {
  40. super.init(frame: frameRect)
  41. }
  42. public override func awakeFromNib() {
  43. super.awakeFromNib()
  44. configUI()
  45. }
  46. public func resetScrollerStyle() {
  47. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
  48. self.scrollView.scrollerStyle = .overlay
  49. }
  50. }
  51. func configUI() {
  52. collectionView.backgroundColors = [NSColor.clear]
  53. collectionView.wantsLayer = true
  54. collectionView.layer?.backgroundColor = NSColor.clear.cgColor
  55. collectionView.delegate = self
  56. collectionView.dataSource = self
  57. collectionView.allowsEmptySelection = true
  58. collectionView.register(KMHistoryFileThumbItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileThumbItem"))
  59. collectionView.register(KMHistoryFileListItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileListItem"))
  60. collectionView.register(KMHomeQuickToolsView.self, forSupplementaryViewOfKind: NSCollectionView.elementKindSectionHeader, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "kmHomeQuickToolsView"))
  61. collectionView.register(KMHomeFilesHeaderView.self, forSupplementaryViewOfKind: NSCollectionView.elementKindSectionHeader, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "kmHomeFilesHeaderView"))
  62. collectionView.register(KMHomeFilesEmptyHeaderView.self, forSupplementaryViewOfKind: NSCollectionView.elementKindSectionHeader, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHomeFilesEmptyHeaderView"))
  63. collectionView.register(KMHomeFilesEmptyHeaderView.self, forSupplementaryViewOfKind: NSCollectionView.elementKindSectionFooter, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHomeFilesEmptyHeaderView"))
  64. }
  65. //刷新所有数据
  66. public func reloadData() {
  67. HistoryFilesManager.manager.refreshHistoryFile()
  68. collectionView.reloadData()
  69. filesHeaderView.updateDeleteButtonState()
  70. }
  71. //刷新列表文件UI
  72. public func updateHistoryFileListItemUI() {
  73. if HistoryFilesManager.manager.showMode == .List {
  74. refreshItemData = false
  75. collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems())
  76. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  77. self.refreshItemData = true
  78. }
  79. }
  80. }
  81. //刷新快捷工具列表
  82. public func reloadQuickTools() {
  83. quickToolsView.reloadData()
  84. }
  85. public func updateQuickToolsUI() {
  86. quickToolsView.refreshUI()
  87. }
  88. //窗口大小变化时调用此方法
  89. public func windowFrameUpdated() {
  90. updateHistoryFileListItemUI()
  91. updateQuickToolsUI()
  92. }
  93. //MARK: - Private
  94. private func collectionViewSelectedChanged() {
  95. let indexs = collectionView.selectionIndexPaths
  96. HistoryFilesManager.manager.selectFiles.removeAll()
  97. if indexs.count >= 0 {
  98. for index in indexs {
  99. let url = HistoryFilesManager.manager.files[index.item]
  100. HistoryFilesManager.manager.selectFiles.append(url)
  101. }
  102. }
  103. filesHeaderView.updateDeleteButtonState()
  104. }
  105. //显示右键菜单
  106. func showFileMoreActionMenu(point: CGPoint) {
  107. var viewHeight: CGFloat = 8
  108. var menuItemArr: [ComponentMenuitemProperty] = []
  109. var items: [(String, String)] = [("Show in Finder", "ShowInFinderKey"),
  110. ("Remove from Recents", "RemovefromRecentKey"),
  111. (" ", " "),
  112. ("Share...", "ShareKey")]
  113. if collectionView.selectionIndexPaths.count > 1 {
  114. items = [("Show in Finder", "ShowInFinderKey"),
  115. ("Remove from Recents", "RemovefromRecentKey"),
  116. (" ", " "),
  117. ("Share...", "ShareKey")]
  118. }
  119. for (i, value) in items {
  120. if i == " " {
  121. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  122. menuItemArr.append(property)
  123. viewHeight += 8
  124. } else {
  125. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  126. itemSelected: false,
  127. isDisabled: false,
  128. keyEquivalent: nil,
  129. text: KMLocalizedString(i),
  130. identifier: value)
  131. if value == "FileInformationKey" {
  132. properties_Menuitem.keyEquivalent = "⌘ D"
  133. } else if value == "PrintKey" {
  134. properties_Menuitem.keyEquivalent = "⌘ P"
  135. }
  136. menuItemArr.append(properties_Menuitem)
  137. viewHeight += 36
  138. }
  139. }
  140. if groupView == nil {
  141. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  142. }
  143. groupView.groupDelegate = self
  144. groupView?.frame = CGRectMake(0, 0, 180, viewHeight)
  145. groupView.updateGroupInfo(menuItemArr)
  146. if HistoryFilesManager.manager.showMode == .Thumbnail {
  147. groupView.showWithPoint(CGPoint(x: point.x + 60 + CGRectGetWidth(groupView.frame), y: point.y - viewHeight), relativeTo: self)
  148. } else {
  149. groupView.showWithPoint(CGPoint(x: point.x + 40 + CGRectGetWidth(groupView.frame), y: point.y - viewHeight), relativeTo: self)
  150. }
  151. menuActionIndexPaths = collectionView.selectionIndexPaths
  152. }
  153. //MARK: -删除文件列表
  154. func historyFileDeleteAction(_ indexPaths: [URL]) -> Void {
  155. self.fileDeleteAction()
  156. }
  157. @objc func emptyViewAddFileClicked(_ sender: NSView) {
  158. let openPanel = NSOpenPanel()
  159. openPanel.allowedFileTypes = ["pdf", "PDF"]
  160. openPanel.allowsMultipleSelection = false
  161. openPanel.beginSheetModal(for: self.window!) { [weak self] result in
  162. if result == NSApplication.ModalResponse.OK {
  163. if let url = openPanel.url {
  164. guard let weakSelf = self else { return }
  165. weakSelf.delegate?.homeRightViewDidChooseFileToOpen?(weakSelf, url)
  166. }
  167. }
  168. }
  169. }
  170. func fileDeleteAction() {
  171. if UserDefaults.standard.object(forKey: "HomeFilesDeleteConfirmKey") != nil {
  172. var selects: [URL] = []
  173. if HistoryFilesManager.manager.selectFiles.count > 0 {
  174. for selecturl in HistoryFilesManager.manager.selectFiles {
  175. selects.append(selecturl)
  176. }
  177. }
  178. HistoryFilesManager.manager.historyFileDeleteAction(selects)
  179. self.reloadData()
  180. return
  181. }
  182. let alert = NSAlert()
  183. alert.alertStyle = .informational
  184. if HistoryFilesManager.manager.selectFiles.count > 0 {
  185. alert.informativeText = KMLocalizedString("Remove Selected Files from your Recent Files?")
  186. } else {
  187. alert.informativeText = KMLocalizedString("Remove Files from your Recent Files?")
  188. }
  189. alert.messageText = KMLocalizedString("The file will disappear from the recent list.")
  190. alert.addButton(withTitle: KMLocalizedString("Delete"))
  191. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  192. alert.showsSuppressionButton = true
  193. let response = alert.runModal()
  194. if response.rawValue == 1000 {
  195. var selects: [URL] = []
  196. if HistoryFilesManager.manager.selectFiles.count > 0 {
  197. for selecturl in HistoryFilesManager.manager.selectFiles {
  198. selects.append(selecturl)
  199. }
  200. } else {
  201. for selecturl in HistoryFilesManager.manager.files {
  202. selects.append(selecturl)
  203. }
  204. }
  205. HistoryFilesManager.manager.historyFileDeleteAction(selects)
  206. self.reloadData()
  207. if alert.suppressionButton?.state == .on {
  208. UserDefaults.standard.set("YES", forKey: "HomeFilesDeleteConfirmKey")
  209. UserDefaults.standard.synchronize()
  210. }
  211. }
  212. }
  213. //MARK: - MouseEvent
  214. public override func rightMouseDown(with event: NSEvent) {
  215. super.rightMouseDown(with: event)
  216. var point = convert(event.locationInWindow, from: nil)
  217. if HistoryFilesManager.manager.showMode == .List {
  218. point.x += 10
  219. }
  220. let collectionPoint = convert(point, to: collectionView)
  221. if let indexPath = collectionView.indexPathForItem(at: collectionPoint) {
  222. if collectionView.selectionIndexPaths.contains(indexPath) {
  223. } else {
  224. collectionView.deselectAll(nil)
  225. collectionView.selectItems(at: [indexPath], scrollPosition: .nearestHorizontalEdge)
  226. }
  227. groupActionView = collectionView.item(at: indexPath)?.view
  228. groupViewPoint = point
  229. showFileMoreActionMenu(point: point)
  230. }
  231. collectionViewSelectedChanged()
  232. }
  233. }
  234. //MARK: - NSCollectionViewDelegate, NSCollectionViewDataSource
  235. extension KMHomeRightView: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  236. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  237. return 2
  238. }
  239. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  240. if section == 0 {
  241. return 0
  242. }
  243. return HistoryFilesManager.manager.files.count
  244. }
  245. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  246. if indexPath.item >= HistoryFilesManager.manager.files.count {
  247. return NSCollectionViewItem()
  248. }
  249. if HistoryFilesManager.manager.showMode == .Thumbnail {
  250. let item: KMHistoryFileThumbItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileThumbItem"), for: indexPath) as! KMHistoryFileThumbItem
  251. item.fileURL = HistoryFilesManager.manager.files[indexPath.item]
  252. item.reloadData()
  253. item.delegate = self
  254. return item
  255. } else if HistoryFilesManager.manager.showMode == .List {
  256. let item: KMHistoryFileListItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileListItem"), for: indexPath) as! KMHistoryFileListItem
  257. item.fileURL = HistoryFilesManager.manager.files[indexPath.item]
  258. item.reloadData()
  259. item.delegate = self
  260. return item
  261. }
  262. return NSCollectionViewItem()
  263. }
  264. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  265. if HistoryFilesManager.manager.showMode == .Thumbnail {
  266. return CGSize(width: 172, height: 248)
  267. } else if HistoryFilesManager.manager.showMode == .List {
  268. print(collectionView.frame.size.width, self.frame.size.width)
  269. return CGSize(width: collectionView.frame.size.width-80, height: 80)
  270. }
  271. return CGSize(width: 0.01, height: 0.01)
  272. }
  273. public func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
  274. if kind == NSCollectionView.elementKindSectionHeader {
  275. if indexPath.section == 0 {
  276. //quick tools
  277. quickToolsView = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier("kmHomeQuickToolsView"), for: indexPath) as! KMHomeQuickToolsView
  278. quickToolsView.delegate = self
  279. quickToolsView.reloadData()
  280. return quickToolsView
  281. } else if indexPath.section == 1 {
  282. //Recently
  283. filesHeaderView = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier("kmHomeFilesHeaderView"), for: indexPath) as! KMHomeFilesHeaderView
  284. filesHeaderView.delegate = self
  285. return filesHeaderView
  286. }
  287. } else if kind == NSCollectionView.elementKindSectionFooter {
  288. let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier("KMHomeFilesEmptyHeaderView"), for: indexPath) as! KMHomeFilesEmptyHeaderView
  289. if self.filesEmptyView.superview != nil {
  290. self.filesEmptyView.removeFromSuperview()
  291. }
  292. if indexPath.section == 1 &&
  293. HistoryFilesManager.manager.files.count == 0 {
  294. filesEmptyView.frame = CGRectMake((CGRectGetWidth(collectionView.frame)-520)/2, (344-184)/2, 520, 184)
  295. filesEmptyView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  296. filesEmptyView.emptyView.setTarget(self, action: #selector(emptyViewAddFileClicked(_:)))
  297. view.addSubview(filesEmptyView)
  298. }
  299. return view
  300. }
  301. return NSView()
  302. }
  303. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  304. return 8
  305. }
  306. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  307. return 0.01
  308. }
  309. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
  310. if section == 0 {
  311. if KMNHomeQuickToolManager.defaultManager.collapseTools {
  312. if KMNHomeQuickToolManager.defaultManager.quickToolsItemMutableArray.count <= 4 {
  313. return NSSize(width: collectionView.frame.size.width, height: 172+32+2-60-14) //折叠
  314. }
  315. return NSSize(width: collectionView.frame.size.width, height: 172+32+2) //折叠
  316. } else {
  317. if KMNHomeQuickToolManager.defaultManager.quickToolsItemMutableArray.count <= 4 {
  318. return NSSize(width: collectionView.frame.size.width, height: 228+32-88-14)
  319. }
  320. return NSSize(width: collectionView.frame.size.width, height: 228+32)
  321. }
  322. } else if section == 1 {
  323. return NSSize(width: collectionView.frame.size.width, height: 28)
  324. }
  325. return NSSize(width: 0, height: 0.01)
  326. }
  327. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForFooterInSection section: Int) -> NSSize {
  328. if section == 0 {
  329. return NSSize(width: 0, height: 40-16)
  330. } else if section == 1 {
  331. if HistoryFilesManager.manager.files.count == 0 {
  332. return NSSize(width: collectionView.frame.size.width, height: 344)
  333. }
  334. }
  335. return NSSize(width: 0, height: 0.01)
  336. }
  337. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  338. return NSEdgeInsetsMake(12, 40, 0, 40)
  339. }
  340. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  341. self.collectionViewSelectedChanged()
  342. }
  343. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  344. self.collectionViewSelectedChanged()
  345. }
  346. }
  347. //MARK: - KMHomeFilesHeaderViewDelegate
  348. extension KMHomeRightView: KMHomeFilesHeaderViewDelegate {
  349. public func homeFilesShowModeDidUpdate(_ view: KMHomeFilesHeaderView) {
  350. self.reloadData()
  351. }
  352. public func homeFilesHeaderViewDeleteButtonClicked(_ view: KMHomeFilesHeaderView) {
  353. self.fileDeleteAction()
  354. }
  355. }
  356. //MARK: - KMHomeQuickToolsViewDelegate
  357. extension KMHomeRightView: KMHomeQuickToolsViewDelegate {
  358. public func homeQuickToolsViewDidCollapseStateChanged(_ view: KMHomeQuickToolsView) {
  359. DispatchQueue.main.async {
  360. self.reloadData()
  361. }
  362. }
  363. public func homeQuickToolsViewDidManageTools(_ view: KMHomeQuickToolsView) {
  364. DispatchQueue.main.async {
  365. self.delegate?.homeRightViewDidManageQuickTools?(self)
  366. }
  367. }
  368. public func homeQuickToolsViewDidItemClicked(_ view: KMHomeQuickToolsView, _ toolType: HomeQuickToolType) {
  369. DispatchQueue.main.async {
  370. self.delegate?.homeRightViewDidQuickToolsItemClicked?(self, toolType)
  371. }
  372. }
  373. }
  374. //MARK: - ComponentGroupDelegate
  375. extension KMHomeRightView: ComponentGroupDelegate {
  376. public func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  377. var selects: [URL] = []
  378. let indexs = self.menuActionIndexPaths
  379. if indexs.count >= 0 {
  380. for index in indexs {
  381. let url = HistoryFilesManager.manager.files[index.item]
  382. selects.append(url)
  383. }
  384. }
  385. if menuItemProperty?.identifier == "ShowInFinderKey" {
  386. NSWorkspace.shared.activateFileViewerSelecting(selects)
  387. } else if menuItemProperty?.identifier == "FileInformationKey" {
  388. } else if menuItemProperty?.identifier == "RemovefromRecentKey" {
  389. self.historyFileDeleteAction(selects)
  390. } else if menuItemProperty?.identifier == "ShareKey" {
  391. let picker = NSSharingServicePicker.init(items: selects)
  392. if let actionView = self.groupActionView {
  393. picker.show(relativeTo: actionView.bounds, of: actionView, preferredEdge: NSRectEdge.minY)
  394. } else {
  395. picker.show(relativeTo: NSRect(x: self.groupViewPoint.x, y: self.groupViewPoint.y, width: 0, height: 0), of: self.collectionView, preferredEdge: NSRectEdge.minY)
  396. }
  397. } else if menuItemProperty?.identifier == "PrintKey" {
  398. }
  399. }
  400. }
  401. //MARK: - KMHistoryFileListItemDelegate
  402. extension KMHomeRightView: KMHistoryFileListItemDelegate {
  403. public func historyFileListItemDidClickedMore(_ view: KMHistoryFileListItem) {
  404. if let indexPath = collectionView.indexPath(for: view) {
  405. if collectionView.selectionIndexPaths.contains(indexPath) {
  406. } else {
  407. collectionView.deselectAll(nil)
  408. collectionView.selectItems(at: [indexPath], scrollPosition: .nearestHorizontalEdge)
  409. }
  410. }
  411. groupActionView = view.moreButton
  412. var point = view.moreButton.superview?.convert(view.moreButton.frame.origin, to: self) ?? CGPointZero
  413. showFileMoreActionMenu(point: point)
  414. }
  415. public func historyFileListItemDidDoubleClicked(_ view: KMHistoryFileListItem) {
  416. if let url = view.fileURL {
  417. delegate?.homeRightViewDidChooseFileToOpen?(self, url)
  418. }
  419. }
  420. }
  421. //MARK: - KMHistoryFileThumbItemDelegate
  422. extension KMHomeRightView: KMHistoryFileThumbItemDelegate {
  423. func historyFileThumbItemDidDoubleClicked(_ view: KMHistoryFileThumbItem) {
  424. if let url = view.fileURL {
  425. delegate?.homeRightViewDidChooseFileToOpen?(self, url)
  426. }
  427. }
  428. }