KMLeftSideViewController+Thumbnail.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. //
  2. // KMLeftSideViewController+Thumbnail.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2024/1/12.
  6. //
  7. import Foundation
  8. extension KMLeftSideViewController.Key {
  9. static let thumbSizeScaling = "KMThumbnailSizeScalingKey"
  10. static let thumbDisplayPageSizeKey = "kKMThumbnailDisplayPageSizeKey"
  11. }
  12. // MARK: - Actions
  13. extension KMLeftSideViewController {
  14. public func refreshUIOfThumbnailIfNeed(preference: Bool = false) {
  15. if self.type.methodType != .Thumbnail {
  16. return
  17. }
  18. if preference {
  19. self.reloadThumbnailSize()
  20. }
  21. Task { @MainActor in
  22. self.thumbnailTableView.reloadData()
  23. }
  24. }
  25. public func reloadThumbnailDataIfNeed() {
  26. if self.type.methodType != .Thumbnail {
  27. return
  28. }
  29. self.resetThumbnails()
  30. }
  31. public func reloadThumbnailSize() {
  32. let defaultSize = roundf(KMPreference.shared.thumbPageSize)
  33. var thumbnailSize = Self.kTinySize
  34. if defaultSize < Self.kTinySize + Self.kFudgeSize {
  35. } else {
  36. if defaultSize < Self.kSmallSize + Self.kFudgeSize {
  37. thumbnailSize = Self.kSmallSize
  38. } else {
  39. if defaultSize < Self.kLargeSize + Self.kFudgeSize {
  40. thumbnailSize = Self.kLargeSize
  41. } else {
  42. thumbnailSize = Self.kHugeSize
  43. }
  44. }
  45. }
  46. if (abs(thumbnailSize - Float(self.thumbnailCacheSize)) > Self.kFudgeSize) {
  47. self.thumbnailCacheSize = thumbnailSize.cgFloat
  48. }
  49. Task { @MainActor in
  50. self.thumbnailTableView.reloadData()
  51. }
  52. }
  53. // 显示缩略图模块
  54. func showThumbnail() {
  55. if self.leftView.segmentedControl.selectedSegment == 0 {
  56. } else {
  57. self.leftView.segmentedControl.selectedSegment = 0
  58. }
  59. }
  60. func updateThumbnail(at index: Int) {
  61. if index < self.thumbnails.count {
  62. self.thumbnails[index].dirty = true
  63. /*
  64. 原问题:CrashKit - SKMainWindowController updateThumbnailAtPageIndex:] ([__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 17])
  65. 注释原因:缩略图高亮为自定义后,刷新会将高亮刷新到0行
  66. */
  67. // [leftSideController.thumbnailTableView reloadData];
  68. // BOOL visible = NO;
  69. // PDFPage *page = [[pdfView document] pageAtIndex:anIndex];
  70. // if([self.pdfView.visiblePages containsObject:page]) {
  71. // visible = YES;
  72. // }
  73. // NSInteger curIndex = [pdfView.document indexForPage:pdfView.currentPage];
  74. // if (need && curIndex != anIndex && !visible) {
  75. // [pdfView goToPage:[[pdfView document] pageAtIndex:anIndex]];
  76. // }
  77. }
  78. }
  79. func allThumbnailsNeedUpdate() {
  80. for thumb in self.thumbnails {
  81. thumb.dirty = true
  82. }
  83. self.reloadThumbnailSize()
  84. }
  85. func thumb_selectRowIndexsIfNeed(_ indexs: IndexSet) {
  86. let isReadMode = self.mainViewController?.isReadMode ?? false
  87. if self.type.methodType != .Thumbnail && isReadMode == false {
  88. return
  89. }
  90. if indexs.isEmpty {
  91. return
  92. }
  93. Task { @MainActor in
  94. self.stopRepeatLoad = true
  95. self.thumbnailTableView.selectRowIndexes(indexs, byExtendingSelection: false)
  96. self.stopRepeatLoad = false
  97. self.thumbnailTableView.scrollRowToVisible(indexs.first ?? 0)
  98. }
  99. }
  100. func thumb_initSubViews() {
  101. self.thumbnailZoomInButton.target = self
  102. self.thumbnailZoomInButton.action = #selector(thumbnailSizeScaling)
  103. self.thumbnailZoomInButton.tag = 1
  104. self.thumbnailZoomOutButton.target = self
  105. self.thumbnailZoomOutButton.action = #selector(thumbnailSizeScaling)
  106. self.thumbnailZoomOutButton.tag = 0
  107. self.thumbnailTableView.delegate = self
  108. self.thumbnailTableView.dataSource = self
  109. self.thumbnailTableView.thumbDelegate = self
  110. self.thumbnailTableView.botaDelegate = self
  111. self.thumbnailTableView.menu?.delegate = self
  112. self.thumbnailTableView.registerForDraggedTypes([.localDraggedTypes, .fileURL,.string,.pdf])
  113. self.thumbnailTableView.registerForDraggedTypes(NSFilePromiseReceiver.readableDraggedTypes.map { NSPasteboard.PasteboardType($0) })
  114. self.thumbnailTableView.setDraggingSourceOperationMask(.every, forLocal: false)
  115. }
  116. func thumb_initDefalutValue() {
  117. self.isDisplayPageSize = KMDataManager.ud_bool(forKey: Self.Key.thumbDisplayPageSizeKey)
  118. self.thumbnailView.wantsLayer = true
  119. self.thumbnailView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  120. self.thumbnailTitleLabel.stringValue = KMLocalizedString("Thumbnails", nil)
  121. self.thumbnailTitleLabel.textColor = KMAppearance.Layout.h0Color()
  122. self.thumbnailZoomInButton.toolTip = KMLocalizedString("Zoom In", nil)
  123. self.thumbnailZoomOutButton.toolTip = KMLocalizedString("Zoom Out", nil)
  124. self.thumbnailTableView.backgroundColor = KMAppearance.Layout.l0Color()
  125. self.thumbnailTableView.allowsMultipleSelection = true
  126. self.thumbnailTableView.selectionHighlightStyle = .none
  127. }
  128. func thumb_fetchSelectedRows() -> [Int]? {
  129. return self.thumbnailTableView.selectedRowIndexes.sorted()
  130. }
  131. func updateThumbnailSelection() {
  132. let pageIndex = self.currentPageIndex()
  133. self.updatingThumbnailSelection = true
  134. self.thumbnailTableView.km_safe_selectRowIndexes(IndexSet(integer: pageIndex), byExtendingSelection: false)
  135. self.thumbnailTableView.scrollRowToVisible(pageIndex)
  136. self.updatingThumbnailSelection = false
  137. }
  138. }
  139. // MARK: - Others
  140. extension KMLeftSideViewController {}
  141. // MARK: - KMThumbnailTableViewDelegate
  142. extension KMLeftSideViewController: KMThumbnailTableViewDelegate {
  143. func tableView(_ tableView: NSTableView, highlightLevelForRow row: Int) -> UInt {
  144. if tableView.isEqual(to: self.thumbnailTableView) {
  145. // NSUInteger i, iMax = [lastViewedPages count];
  146. // for (i = 0; i < iMax; i++) {
  147. // if (row == (NSInteger)[lastViewedPages pointerAtIndex:i])
  148. // return i;
  149. // }
  150. }
  151. return UInt.max
  152. }
  153. func tableView(_ tableView: NSTableView, commandSelectRow rowIndex: Int) -> Bool {
  154. if tableView.isEqual(to: self.thumbnailTableView) {
  155. // NSRect rect = [[[pdfView document] pageAtIndex:row] boundsForBox:kPDFDisplayBoxCropBox];
  156. //
  157. // rect.origin.y = NSMidY(rect) - 0.5 * SNAPSHOT_HEIGHT;
  158. // rect.size.height = SNAPSHOT_HEIGHT;
  159. // [self showSnapshotAtPageNumber:row forRect:rect scaleFactor:[pdfView scaleFactor] autoFits:NO];
  160. let thumbailTabCell = tableView.view(atColumn: 0, row: rowIndex, makeIfNecessary: true) as? KMThumbnailTableviewCell
  161. thumbailTabCell?.isSelectCell = true
  162. var rowIndexSet = IndexSet()
  163. for i in self.thumbnailTableView.selectedRowIndexes {
  164. rowIndexSet.insert(i)
  165. }
  166. rowIndexSet.insert(rowIndex)
  167. self.thumbnailTableView.selectRowIndexes(rowIndexSet, byExtendingSelection: true)
  168. return true
  169. }
  170. return false
  171. }
  172. func tableView(_ tableView: NSTableView, shiftSelectRow rowIndex: Int) -> Bool {
  173. if tableView.isEqual(to: self.thumbnailTableView) {
  174. if (self.thumbnailTableView.selectedRowIndexes.count == 0) {
  175. let thumbailTabCell = tableView.view(atColumn: 0, row: rowIndex, makeIfNecessary: true) as? KMThumbnailTableviewCell
  176. thumbailTabCell?.isSelectCell = true
  177. var rowIndexSet = IndexSet()
  178. self.thumbnailTableView.selectRowIndexes(rowIndexSet, byExtendingSelection: true)
  179. return true
  180. } else if (self.thumbnailTableView.selectedRowIndexes.count == 1 && self.thumbnailTableView.selectedRowIndexes.first == rowIndex) {
  181. return false
  182. } else {
  183. var fristIndex = self.thumbnailTableView.selectedRowIndexes.first ?? Int.min
  184. var lastIndex = self.thumbnailTableView.selectedRowIndexes.last ?? Int.max
  185. for idx in self.thumbnailTableView.selectedRowIndexes {
  186. if(idx < fristIndex) {
  187. fristIndex = idx
  188. }
  189. if(idx > lastIndex) {
  190. lastIndex = idx
  191. }
  192. }
  193. if(rowIndex < fristIndex) {
  194. fristIndex = rowIndex
  195. } else if (rowIndex > lastIndex) {
  196. lastIndex = rowIndex
  197. }
  198. var rowIndexSet = IndexSet()
  199. for i in fristIndex ... lastIndex {
  200. rowIndexSet.insert(i)
  201. }
  202. self.thumbnailTableView.selectRowIndexes(rowIndexSet, byExtendingSelection: true)
  203. self.thumbnailTableView.ks_reloadData()
  204. return true
  205. }
  206. }
  207. return false
  208. }
  209. }
  210. // MARK: - Undo & Redo
  211. extension KMLeftSideViewController {
  212. @objc dynamic func tableView(_ tv: NSTableView, rotateRowsWithIndexes rowIndexes: NSIndexSet) {
  213. if tv.isEqual(to: self.thumbnailTableView) {
  214. for idx in rowIndexes {
  215. if (idx != NSNotFound) {
  216. let page = self.pdfDocument()?.page(at: UInt(idx))
  217. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).rotatePage(page, pageAt: idx)
  218. page?.rightRotate()
  219. self.layoutDocumentView()
  220. self.resetThumbnails()
  221. // NSInteger pageIndex = MIN(idx, [[pdfView document] pageCount]-1);
  222. // [pdfView goToPage:[[pdfView document] pageAtIndex:pageIndex]];
  223. }
  224. self.thumbnailTableView.selectRowIndexes(rowIndexes as IndexSet, byExtendingSelection: true)
  225. }
  226. }
  227. }
  228. @objc dynamic func rotatePage(_ page: CPDFPage?, pageAt index: Int) {
  229. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).tableView(self.thumbnailTableView, rotateRowsWithIndexes: NSIndexSet(index: index))
  230. page?.leftRotate()
  231. self.layoutDocumentView()
  232. self.resetThumbnails()
  233. }
  234. @objc dynamic func insertPage(_ page: CPDFPage, pageAt index: Int) {
  235. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject)._undo_removePage(page, at: index)
  236. self.pdfDocument()?.insertPageObject(page, at: UInt(index))
  237. self.layoutDocumentView()
  238. self.resetThumbnails(ks: false)
  239. let pageIndex = min(index, self.pageCount()-1)
  240. Task { @MainActor in
  241. self.listView?.go(toPageIndex: pageIndex, animated: false)
  242. }
  243. }
  244. @objc private func _undo_removePage(_ page: CPDFPage, at idx: Int) {
  245. self.tableView(self.thumbnailTableView, deleteRowsWithIndexes: IndexSet(integer: idx))
  246. }
  247. @objc dynamic func insertPages(_ selectedIndexSet: IndexSet, pageAt index: Int) {
  248. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).deletePages(selectedIndexSet, pageAt: index)
  249. self.layoutDocumentView()
  250. self.resetThumbnails(ks: false)
  251. Task { @MainActor in
  252. if let pageIndex = selectedIndexSet.first {
  253. self.listView?.go(toPageIndex: pageIndex, animated: false)
  254. }
  255. }
  256. }
  257. @objc dynamic func deletePages(_ selectedIndexSet: IndexSet, pageAt index: Int) {
  258. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).insertPages(selectedIndexSet, pageAt: index)
  259. for idx in selectedIndexSet {
  260. if idx < self.pageCount() {
  261. self.pdfDocument()?.removePage(at: UInt(idx))
  262. }
  263. }
  264. self.layoutDocumentView()
  265. self.resetThumbnails()
  266. let pageIndex = min(index, self.pageCount()-1)
  267. self.listView?.go(toPageIndex: pageIndex, animated: false)
  268. }
  269. }
  270. // MARK: - Menu Actions
  271. extension KMLeftSideViewController {
  272. @objc func cutPage(_ sender: AnyObject?) {
  273. if IAPProductsManager.default().isAvailableAllFunction() == false {
  274. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  275. return
  276. }
  277. self.tableView(self.thumbnailTableView, cutRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes)
  278. }
  279. @objc func copyPage(_ sender: AnyObject?) {
  280. self.tableView(self.thumbnailTableView, copyRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes)
  281. }
  282. @objc func pastePage(_ sender: AnyObject?) {
  283. self.tableView(self.thumbnailTableView, pasteFromPasteboard: nil)
  284. }
  285. @objc func deletePage(_ sender: AnyObject?) {
  286. if IAPProductsManager.default().isAvailableAllFunction() == false {
  287. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  288. return
  289. }
  290. self.tableView(self.thumbnailTableView, deleteRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes)
  291. }
  292. @objc func rotatePageMenuAction(_ sender: AnyObject?) {
  293. if IAPProductsManager.default().isAvailableAllFunction() == false {
  294. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  295. return
  296. }
  297. self.tableView(self.thumbnailTableView, rotateRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes as NSIndexSet)
  298. }
  299. @objc func quickInsert(_ sender: AnyObject?) {
  300. if IAPProductsManager.default().isAvailableAllFunction() == false {
  301. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  302. return
  303. }
  304. let idx = self.thumbnailTableView.selectedRowIndexes.first ?? NSNotFound
  305. if idx == NSNotFound || idx >= self.pageCount() {
  306. return
  307. }
  308. let result = self.listView?.insertPage(KMNormalBlankSize, at: idx+1) ?? false
  309. if result == false {
  310. return
  311. }
  312. var selectedIndexSet = IndexSet()
  313. selectedIndexSet.insert(idx+1)
  314. self.insertPages(selectedIndexSet, pageAt: idx)
  315. }
  316. @objc func insert(_ sender: AnyObject?) {
  317. if IAPProductsManager.default().isAvailableAllFunction() == false {
  318. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  319. return
  320. }
  321. guard let document = self.pdfDocument() else {
  322. return
  323. }
  324. let idx = self.thumbnailTableView.selectedRowIndexes.first ?? NSNotFound
  325. if idx == NSNotFound || idx >= document.pageCount {
  326. return
  327. }
  328. if document.allowsCopying == false || document.allowsPrinting == false {
  329. Task {
  330. _ = await KMAlertTool.runModel(message: KMLocalizedString("This is a secured document. Editing is not permitted.", nil))
  331. }
  332. return
  333. }
  334. let winC = KMPDFEditInsertBlankPageWindow(document: document)
  335. winC.insertLocation = 3
  336. winC.currentPage = idx + 1
  337. winC.callback = { [weak self] pdfDoc, _, pages, insertI in
  338. guard let myWinC = self?.kmCurrentWindowC as? KMPDFEditInsertBlankPageWindow else {
  339. self?.km_quick_endSheet()
  340. return
  341. }
  342. if let _ = pages {
  343. var pageSize = myWinC.pageSize
  344. let direction = myWinC.pageRotation == 0 ? 0 : 1
  345. if (direction == 0) { // 纵向
  346. if (pageSize.width > pageSize.height) { // 需要交换
  347. let tmp = pageSize.width
  348. pageSize.width = pageSize.height
  349. pageSize.height = tmp
  350. } else {
  351. // no things.
  352. }
  353. } else { // 横向
  354. if (pageSize.width > pageSize.height) {
  355. // no things.
  356. } else { // 需要交换
  357. let tmp = pageSize.width
  358. pageSize.width = pageSize.height
  359. pageSize.height = tmp
  360. }
  361. }
  362. /// 插入位置
  363. let document = CPDFDocument()
  364. document?.insertPage(pageSize, at: 0)
  365. if let page = document?.page(at: 0) {
  366. self?.insertPage(page, pageAt: insertI)
  367. }
  368. }
  369. self?.km_quick_endSheet()
  370. }
  371. self.km_beginSheet(windowC: winC)
  372. }
  373. @objc func insertPDF(_ sender: AnyObject?) {
  374. if IAPProductsManager.default().isAvailableAllFunction() == false {
  375. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  376. return
  377. }
  378. guard let document = self.pdfDocument() else {
  379. return
  380. }
  381. let idx = self.thumbnailTableView.selectedRowIndexes.first ?? NSNotFound
  382. if idx == NSNotFound || idx >= document.pageCount {
  383. return
  384. }
  385. if document.allowsCopying == false || document.allowsPrinting == false {
  386. Task {
  387. _ = await KMAlertTool.runModel(message: KMLocalizedString("This is a secured document. Editing is not permitted.", nil))
  388. }
  389. return
  390. }
  391. let panel = NSOpenPanel()
  392. panel.allowedFileTypes = ["pdf"]
  393. panel.beginSheetModal(for: self.view.window!) { response in
  394. if response == .cancel {
  395. return
  396. }
  397. for fileURL in panel.urls {
  398. let pdfDoc = CPDFDocument(url: fileURL)
  399. if let data = pdfDoc?.isLocked, data {
  400. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  401. KMBaseWindowController.checkPassword(url: fileURL, type: .owner) { [unowned self] success, resultPassword in
  402. if (resultPassword.isEmpty == false) {
  403. let insertVC = KMPDFEditInsertPageWindow(document: document, path: fileURL, password: resultPassword)
  404. insertVC.insertLocation = 3
  405. insertVC.currentPage = idx + 1
  406. self.km_beginSheet(windowC: insertVC)
  407. insertVC.callback = { [weak self] pdfDoc, pwd, pages, insertIdx in
  408. var indexs = IndexSet()
  409. guard let _winC = self?.kmCurrentWindowC as? KMPDFEditInsertPageWindow, _winC.insertDocument != nil else {
  410. self?.km_quick_endSheet()
  411. return
  412. }
  413. let doc = _winC.insertDocument!
  414. // self?.model.insertedDocument = doc
  415. self?.model.insertedDocumentSet.insert(doc)
  416. let fileAttribute = _winC.fileAttribute
  417. var insertIndex = insertIdx
  418. var insertPages: [CPDFPage] = []
  419. for number in fileAttribute.fetchSelectPages() {
  420. if let page = doc.page(at: UInt(number-1)) {
  421. insertPages.append(page)
  422. indexs.insert(insertIndex)
  423. insertIndex += 1
  424. }
  425. }
  426. for (i, page) in insertPages.enumerated() {
  427. self?.pdfDocument()?.insertPageObject(page, at: UInt(insertIdx + i))
  428. }
  429. self?.insertPages(indexs, pageAt: insertIdx)
  430. self?.km_quick_endSheet()
  431. }
  432. }
  433. }
  434. }
  435. } else {
  436. let insertVC = KMPDFEditInsertPageWindow(document: document, path: fileURL)
  437. insertVC.insertLocation = 3
  438. insertVC.currentPage = idx + 1
  439. self.km_beginSheet(windowC: insertVC)
  440. insertVC.callback = { [weak self] pdfDoc, pwd, pages, insertIdx in
  441. var indexs = IndexSet()
  442. guard let _winC = self?.kmCurrentWindowC as? KMPDFEditInsertPageWindow, _winC.insertDocument != nil else {
  443. self?.km_quick_endSheet()
  444. return
  445. }
  446. let doc = _winC.insertDocument!
  447. // self?.model.insertedDocument = doc
  448. self?.model.insertedDocumentSet.insert(doc)
  449. let fileAttribute = _winC.fileAttribute
  450. var insertIndex = insertIdx
  451. var insertPages: [CPDFPage] = []
  452. for number in fileAttribute.fetchSelectPages() {
  453. if let page = doc.page(at: UInt(number-1)) {
  454. insertPages.append(page)
  455. indexs.insert(insertIndex)
  456. insertIndex += 1
  457. }
  458. }
  459. for (i, page) in insertPages.enumerated() {
  460. self?.pdfDocument()?.insertPageObject(page, at: UInt(insertIdx + i))
  461. }
  462. self?.insertPages(indexs, pageAt: insertIdx)
  463. self?.km_quick_endSheet()
  464. }
  465. }
  466. }
  467. }
  468. }
  469. @objc func extractPage(_ sender: AnyObject?) {
  470. if IAPProductsManager.default().isAvailableAllFunction() == false {
  471. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  472. return
  473. }
  474. self.tableView(self.thumbnailTableView, extractRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes)
  475. }
  476. @objc func pageEdit(_ sender: AnyObject?) {
  477. self.delegate?.controller?(controller: self, itemClick: nil, itemKey: .pageEdit, params: nil)
  478. }
  479. @objc func displayPageSize(_ sender: AnyObject?) {
  480. if IAPProductsManager.default().isAvailableAllFunction() == false {
  481. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  482. return
  483. }
  484. self.isDisplayPageSize = !self.isDisplayPageSize
  485. KMDataManager.ud_set(self.isDisplayPageSize, forKey: Self.Key.thumbDisplayPageSizeKey)
  486. Task { @MainActor in
  487. self.thumbnailTableView.ks_reloadData()
  488. }
  489. }
  490. @objc func sharePage(_ sender: AnyObject?) {
  491. if IAPProductsManager.default().isAvailableAllFunction() == false {
  492. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  493. return
  494. }
  495. guard let document = self.pdfDocument() else {
  496. KMPrint("document is nil.", beep: true)
  497. return
  498. }
  499. let ris = self.thumbnailTableView.selectedRowIndexes
  500. if ris.isEmpty {
  501. KMPrint("empty selection.", beep: true)
  502. return
  503. }
  504. let pdf = CPDFDocument()
  505. pdf?.importPages(ris, from: document, at: 0)
  506. var fileName = self.fileNameWithSelectedPages(ris)
  507. if (fileName.count > 50) {
  508. fileName = fileName.substring(to: 50)
  509. }
  510. let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
  511. var cachesDir = paths.first ?? ""
  512. cachesDir = "\(cachesDir)/\(fileName).pdf"
  513. let url = URL(fileURLWithPath: cachesDir)
  514. let winC = KMProgressWindowController()
  515. self.km_beginSheet(windowC: winC) { resp, obj in }
  516. DispatchQueue.global().async {
  517. let sucess = pdf?.write(toFile: cachesDir) ?? false
  518. if (sucess) {
  519. DispatchQueue.main.async {
  520. let represent = (sender as? NSMenuItem)?.representedObject as? NSSharingService
  521. represent?.perform(withItems: [url])
  522. self.km_quick_endSheet()
  523. winC.close()
  524. }
  525. } else {
  526. self.km_quick_endSheet()
  527. winC.close()
  528. }
  529. }
  530. }
  531. }