KMHomeRightView.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. weak open var delegate: KMHomeRightViewDelegate?
  28. //MARK: - func
  29. public override func draw(_ dirtyRect: NSRect) {
  30. super.draw(dirtyRect)
  31. // Drawing code here.
  32. }
  33. public required init?(coder decoder: NSCoder) {
  34. super.init(coder: decoder)
  35. }
  36. override init(frame frameRect: NSRect) {
  37. super.init(frame: frameRect)
  38. }
  39. public override func awakeFromNib() {
  40. super.awakeFromNib()
  41. self.configUI()
  42. }
  43. public override func layout() {
  44. super.layout()
  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. public func reloadData() {
  66. HistoryFilesManager.manager.refreshHistoryFile()
  67. collectionView.reloadData()
  68. }
  69. //刷新快捷工具列表
  70. public func reloadQuickTools() {
  71. quickToolsView.reloadData()
  72. }
  73. private func collectionViewSelectedChanged() {
  74. let indexs = collectionView.selectionIndexPaths
  75. HistoryFilesManager.manager.selectFiles.removeAll()
  76. if indexs.count >= 0 {
  77. for index in indexs {
  78. let url = HistoryFilesManager.manager.files[index.item]
  79. HistoryFilesManager.manager.selectFiles.append(url)
  80. }
  81. }
  82. filesHeaderView.updateDeleteButtonState()
  83. }
  84. //显示右键菜单
  85. func showFileMoreActionMenu(point: CGPoint) {
  86. var viewHeight: CGFloat = 8
  87. var menuItemArr: [ComponentMenuitemProperty] = []
  88. var items: [(String, String)] = [("Show in Finder", "ShowInFinderKey"),
  89. ("File Information", "FileInformationKey"),
  90. ("Remove from Recent", "RemovefromRecentKey"),
  91. (" ", " "),
  92. ("Share", "ShareKey"),
  93. ("Print", "PrintKey")]
  94. if collectionView.selectionIndexPaths.count > 1 {
  95. items = [("Show in Finder", "ShowInFinderKey"),
  96. ("Remove from Recent", "RemovefromRecentKey"),
  97. (" ", " "),
  98. ("Share", "ShareKey")]
  99. }
  100. for (i, value) in items {
  101. if i == " " {
  102. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  103. menuItemArr.append(property)
  104. viewHeight += 8
  105. } else {
  106. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  107. itemSelected: false,
  108. isDisabled: false,
  109. keyEquivalent: nil,
  110. text: KMLocalizedString(i, comment: ""),
  111. identify: value)
  112. if i == "Print" {
  113. properties_Menuitem.keyEquivalent = "⌘ P"
  114. } else if i == "Share" {
  115. properties_Menuitem.righticon = NSImage(named: "menuItem_arrowRight")
  116. let subItemProperty1: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  117. itemSelected: false,
  118. isDisabled: false,
  119. keyEquivalent: nil,
  120. text: KMLocalizedString("111111", comment: ""),
  121. identify: value)
  122. let subItemProperty2: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  123. itemSelected: false,
  124. isDisabled: false,
  125. keyEquivalent: nil,
  126. text: KMLocalizedString("22222", comment: ""),
  127. identify: value)
  128. properties_Menuitem.subPropertys = [subItemProperty1, subItemProperty2]
  129. }
  130. menuItemArr.append(properties_Menuitem)
  131. viewHeight += 36
  132. }
  133. }
  134. if groupView == nil {
  135. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  136. }
  137. groupView.groupDelegate = self
  138. groupView?.frame = CGRectMake(0, 0, 180, viewHeight)
  139. groupView.updateGroupInfo(menuItemArr)
  140. groupView?.showWithPoint(CGPoint(x: point.x + 10, y: point.y - viewHeight/2), inView: self, withBackView: true)
  141. self.menuActionIndexPaths = collectionView.selectionIndexPaths
  142. }
  143. //MARK: -删除文件列表
  144. func historyFileDeleteAction(_ indexPaths: [URL]) -> Void {
  145. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  146. NSDocumentController.shared.clearRecentDocuments(nil)
  147. DispatchQueue.main.asyncAfter(deadline: .now()) { [weak self] in
  148. guard let weakSelf = self else { return }
  149. for (_, url) in urls.enumerated() {
  150. if !indexPaths.contains(url) {
  151. NSDocumentController.shared.noteNewRecentDocumentURL(url)
  152. }
  153. }
  154. weakSelf.delegate?.homeRightViewDidRecentFilesUpdated?(weakSelf)
  155. weakSelf.reloadData()
  156. }
  157. }
  158. @objc func emptyViewAddFileClicked(_ sender: NSView) {
  159. let openPanel = NSOpenPanel()
  160. openPanel.allowedFileTypes = ["pdf", "PDF"]
  161. openPanel.allowsMultipleSelection = false
  162. openPanel.beginSheetModal(for: self.window!) { [weak self] result in
  163. if result == NSApplication.ModalResponse.OK {
  164. if let url = openPanel.url {
  165. guard let weakSelf = self else { return }
  166. weakSelf.delegate?.homeRightViewDidChooseFileToOpen?(weakSelf, url)
  167. }
  168. }
  169. }
  170. }
  171. //MARK: - MouseEvent
  172. public override func rightMouseDown(with event: NSEvent) {
  173. super.rightMouseDown(with: event)
  174. if HistoryFilesManager.manager.showMode == .Thumbnail {
  175. let point = convert(event.locationInWindow, from: nil)
  176. let collectionPoint = convert(point, to: collectionView)
  177. if let indexPath = collectionView.indexPathForItem(at: collectionPoint) {
  178. if collectionView.selectionIndexPaths.contains(indexPath) {
  179. } else {
  180. collectionView.deselectAll(nil)
  181. collectionView.selectItems(at: [indexPath], scrollPosition: .nearestHorizontalEdge)
  182. }
  183. self.showFileMoreActionMenu(point: point)
  184. }
  185. self.collectionViewSelectedChanged()
  186. }
  187. }
  188. }
  189. //MARK: - NSCollectionViewDelegate, NSCollectionViewDataSource
  190. extension KMHomeRightView: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  191. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  192. return 2
  193. }
  194. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  195. if section == 0 {
  196. return 0
  197. }
  198. return HistoryFilesManager.manager.files.count
  199. }
  200. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  201. if indexPath.item >= HistoryFilesManager.manager.files.count {
  202. return NSCollectionViewItem()
  203. }
  204. if HistoryFilesManager.manager.showMode == .Thumbnail {
  205. let item: KMHistoryFileThumbItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileThumbItem"), for: indexPath) as! KMHistoryFileThumbItem
  206. item.fileURL = HistoryFilesManager.manager.files[indexPath.item]
  207. item.reloadData()
  208. return item
  209. } else if HistoryFilesManager.manager.showMode == .List {
  210. let item: KMHistoryFileListItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMHistoryFileListItem"), for: indexPath) as! KMHistoryFileListItem
  211. item.fileURL = HistoryFilesManager.manager.files[indexPath.item]
  212. item.reloadData()
  213. item.delegate = self
  214. return item
  215. }
  216. return NSCollectionViewItem()
  217. }
  218. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  219. if HistoryFilesManager.manager.showMode == .Thumbnail {
  220. return CGSize(width: 172, height: 248)
  221. } else if HistoryFilesManager.manager.showMode == .List {
  222. let width = collectionView.frame.size.width - 80
  223. return CGSize(width: width, height: 80)
  224. }
  225. return CGSize(width: 0.01, height: 0.01)
  226. }
  227. public func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
  228. if kind == NSCollectionView.elementKindSectionHeader {
  229. if indexPath.section == 0 {
  230. //quick tools
  231. quickToolsView = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier("kmHomeQuickToolsView"), for: indexPath) as! KMHomeQuickToolsView
  232. quickToolsView.delegate = self
  233. quickToolsView.reloadData()
  234. return quickToolsView
  235. } else if indexPath.section == 1 {
  236. //Recently
  237. filesHeaderView = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier("kmHomeFilesHeaderView"), for: indexPath) as! KMHomeFilesHeaderView
  238. filesHeaderView.delegate = self
  239. return filesHeaderView
  240. }
  241. } else if kind == NSCollectionView.elementKindSectionFooter {
  242. let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier("KMHomeFilesEmptyHeaderView"), for: indexPath) as! KMHomeFilesEmptyHeaderView
  243. if self.filesEmptyView.superview != nil {
  244. self.filesEmptyView.removeFromSuperview()
  245. }
  246. if indexPath.section == 1 &&
  247. HistoryFilesManager.manager.files.count == 0 {
  248. filesEmptyView.frame = CGRectMake((CGRectGetWidth(collectionView.frame)-520)/2, (344-184)/2, 520, 184)
  249. filesEmptyView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  250. filesEmptyView.emptyView.setTarget(self, action: #selector(emptyViewAddFileClicked(_:)))
  251. view.addSubview(self.filesEmptyView)
  252. }
  253. return view
  254. }
  255. return NSView()
  256. }
  257. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  258. return 8
  259. }
  260. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  261. return 0.01
  262. }
  263. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
  264. if section == 0 {
  265. if KMNHomeQuickToolManager.defaultManager.collapseTools {
  266. return NSSize(width: collectionView.frame.size.width, height: 172+32+2) //折叠
  267. } else {
  268. return NSSize(width: collectionView.frame.size.width, height: 228+32)
  269. }
  270. } else if section == 1 {
  271. return NSSize(width: collectionView.frame.size.width, height: 28)
  272. }
  273. return NSSize(width: 0, height: 0.01)
  274. }
  275. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForFooterInSection section: Int) -> NSSize {
  276. if section == 0 {
  277. return NSSize(width: 0, height: 40-16)
  278. } else if section == 1 {
  279. if HistoryFilesManager.manager.files.count == 0 {
  280. return NSSize(width: collectionView.frame.size.width, height: 344)
  281. }
  282. }
  283. return NSSize(width: 0, height: 0.01)
  284. }
  285. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  286. return NSEdgeInsetsMake(12, 40, 0, 40)
  287. }
  288. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  289. self.collectionViewSelectedChanged()
  290. }
  291. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  292. self.collectionViewSelectedChanged()
  293. }
  294. }
  295. //MARK: - KMHomeFilesHeaderViewDelegate
  296. extension KMHomeRightView: KMHomeFilesHeaderViewDelegate {
  297. public func homeFilesShowModeDidUpdate(_ view: KMHomeFilesHeaderView) {
  298. self.reloadData()
  299. }
  300. public func homeFilesHeaderViewDeleteButtonClicked(_ view: KMHomeFilesHeaderView) {
  301. if UserDefaults.standard.object(forKey: "HomeFilesDeleteConfirmKey") != nil {
  302. var selects: [URL] = []
  303. if HistoryFilesManager.manager.selectFiles.count > 0 {
  304. for selecturl in HistoryFilesManager.manager.selectFiles {
  305. selects.append(selecturl)
  306. }
  307. }
  308. HistoryFilesManager.manager.historyFileDeleteAction(selects)
  309. self.reloadData()
  310. return
  311. }
  312. let alert = NSAlert()
  313. alert.alertStyle = .informational
  314. alert.informativeText = NSLocalizedString("Remove Selected Files from your Recent Files?", comment: "")
  315. alert.messageText = KMLocalizedString("The file will disappear from the recent list.")
  316. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  317. alert.addButton(withTitle: KMLocalizedString("Delete"))
  318. alert.showsSuppressionButton = true
  319. let response = alert.runModal()
  320. if response.rawValue == 1001 {
  321. var selects: [URL] = []
  322. if HistoryFilesManager.manager.selectFiles.count > 0 {
  323. for selecturl in HistoryFilesManager.manager.selectFiles {
  324. selects.append(selecturl)
  325. }
  326. }
  327. HistoryFilesManager.manager.historyFileDeleteAction(selects)
  328. self.reloadData()
  329. if alert.suppressionButton?.state == .on {
  330. UserDefaults.standard.set("YES", forKey: "HomeFilesDeleteConfirmKey")
  331. UserDefaults.standard.synchronize()
  332. }
  333. }
  334. }
  335. }
  336. //MARK: - KMHomeQuickToolsViewDelegate
  337. extension KMHomeRightView: KMHomeQuickToolsViewDelegate {
  338. public func homeQuickToolsViewDidCollapseStateChanged(_ view: KMHomeQuickToolsView) {
  339. DispatchQueue.main.async {
  340. self.reloadData()
  341. }
  342. }
  343. public func homeQuickToolsViewDidManageTools(_ view: KMHomeQuickToolsView) {
  344. DispatchQueue.main.async {
  345. self.delegate?.homeRightViewDidManageQuickTools?(self)
  346. }
  347. }
  348. public func homeQuickToolsViewDidItemClicked(_ view: KMHomeQuickToolsView, _ toolType: HomeQuickToolType) {
  349. DispatchQueue.main.async {
  350. self.delegate?.homeRightViewDidQuickToolsItemClicked?(self, toolType)
  351. }
  352. }
  353. }
  354. //MARK: - ComponentGroupDelegate
  355. extension KMHomeRightView: ComponentGroupDelegate {
  356. public func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  357. var selects: [URL] = []
  358. let indexs = self.menuActionIndexPaths
  359. if indexs.count >= 0 {
  360. for index in indexs {
  361. let url = HistoryFilesManager.manager.files[index.item]
  362. selects.append(url)
  363. }
  364. }
  365. if menuItemProperty?.identify == "ShowInFinderKey" {
  366. NSWorkspace.shared.activateFileViewerSelecting(selects)
  367. } else if menuItemProperty?.identify == "FileInformationKey" {
  368. } else if menuItemProperty?.identify == "RemovefromRecentKey" {
  369. self.historyFileDeleteAction(selects)
  370. } else if menuItemProperty?.identify == "PrintKey" {
  371. }
  372. }
  373. }
  374. //MARK: - KMHistoryFileListItemDelegate
  375. extension KMHomeRightView: KMHistoryFileListItemDelegate {
  376. public func historyFileListItemDidClickedMore(_ view: KMHistoryFileListItem) {
  377. if let indexPath = collectionView.indexPath(for: view) {
  378. if collectionView.selectionIndexPaths.contains(indexPath) {
  379. } else {
  380. collectionView.deselectAll(nil)
  381. collectionView.selectItems(at: [indexPath], scrollPosition: .nearestHorizontalEdge)
  382. }
  383. }
  384. var point = view.moreButton.superview?.convert(view.moreButton.frame.origin, to: self) ?? CGPointZero
  385. point.x -= 216
  386. self.showFileMoreActionMenu(point: point)
  387. }
  388. }