KMLeftSideViewController+Thumbnail.swift 25 KB

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