KMPDFViewController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. //
  2. // KMPDFViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/18.
  6. //
  7. import Cocoa
  8. class KMPDFViewController: NSViewController {
  9. @IBOutlet var contendBox: NSBox!
  10. @IBOutlet var toolbarBox: NSBox! //工具栏Box
  11. @IBOutlet var bottomContendBox: NSBox! //
  12. @IBOutlet var sidebarBox: NSBox! //左侧边栏Box
  13. @IBOutlet var infoContendSplitView: KMPDFSplitView!
  14. @IBOutlet var infoSplitLeftView: NSView!
  15. @IBOutlet var infoSplitRightView: NSView!
  16. @IBOutlet var infoSplitCenterView: NSView!
  17. @IBOutlet var pdfSplitView: NSSplitView!
  18. @IBOutlet var pdfSplitTopView: NSView!
  19. @IBOutlet var pdfSplitBottomView: NSView!
  20. @IBOutlet var toolbarBoxHeightConst: NSLayoutConstraint!
  21. @IBOutlet var infoSplitViewLeftConst: NSLayoutConstraint!
  22. var viewManager: KMPDFViewManager = KMPDFViewManager.init()
  23. var toolbarManager: KMPDFToolbarManager = KMPDFToolbarManager.init()
  24. var pdfView: CPDFListView!
  25. var document: CPDFDocument?
  26. var myDocument: NSDocument?
  27. var insertDocuments: Set<CPDFDocument> = [] //插入新文档时,SDK会出现崩溃,临时记录
  28. //工具栏
  29. private var pdfToolbarController: KMPDFToolbarController?
  30. //BOTA SideBar
  31. private var sideBarController: KMPDFSideBarController?
  32. //页面编辑
  33. private var pageEditViewController: KMNPageEditViewController?
  34. //DisplaySetting
  35. private var displaySettingController: KMNDisplayViewController?
  36. //SPlitPDF 第二个PDF
  37. private var splitPDFController: KMSplitPDFViewController?
  38. private var pdfBottomToolbar: KMSplitToolbar?
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. // Do view setup here.
  42. toolbarManager.pdfViewManager = viewManager
  43. }
  44. override func viewDidAppear() {
  45. super.viewDidAppear()
  46. setupUI()
  47. }
  48. func setupUI() {
  49. initPDFView()
  50. initToolbar()
  51. initSideBar()
  52. setUpSplitView()
  53. }
  54. //MARK: - PDFView
  55. func initPDFView() {
  56. if pdfView == nil {
  57. pdfView = CPDFListView.init(frame: CGRectMake(0, 0, 100, 100))
  58. pdfView?.autoresizingMask = [.width, .height]
  59. }
  60. pdfView.document = self.document
  61. pdfView.delegate = self
  62. pdfView.pdfListViewDelegate = self
  63. reloadPDFSplitInfo()
  64. }
  65. //MARK: - SplitView
  66. func setUpSplitView() {
  67. infoContendSplitView.wantsLayer = true
  68. infoContendSplitView.layer?.backgroundColor = NSColor.clear.cgColor
  69. infoContendSplitView.delegate = self
  70. infoContendSplitView.layer?.masksToBounds = true
  71. infoContendSplitView.setPosition(-10, ofDividerAt: 0)
  72. infoContendSplitView.setPosition(CGRectGetWidth(infoContendSplitView.frame), ofDividerAt: 1)
  73. }
  74. func setupSplitPDFController() {
  75. if splitPDFController == nil {
  76. splitPDFController = KMSplitPDFViewController.init()
  77. }
  78. splitPDFController?.view.frame = pdfSplitBottomView.bounds
  79. splitPDFController?.view.autoresizingMask = [.height, .width]
  80. splitPDFController?.viewManager = self.viewManager
  81. splitPDFController?.delegate = self
  82. splitPDFController?.reloadData()
  83. pdfSplitBottomView.addSubview(splitPDFController!.view)
  84. }
  85. //MARK: - 工具栏
  86. func initToolbar() {
  87. toolbarBox.borderWidth = 0
  88. if pdfToolbarController == nil {
  89. pdfToolbarController = KMPDFToolbarController.init()
  90. }
  91. pdfToolbarController?.view.frame = toolbarBox.bounds
  92. pdfToolbarController?.view.autoresizingMask = [.width, .height]
  93. pdfToolbarController?.delegate = self
  94. toolbarBox.contentView = pdfToolbarController?.view
  95. pdfToolbarController?.viewManager = viewManager
  96. pdfToolbarController?.toolbarManager = toolbarManager
  97. pdfToolbarController?.pdfView = pdfView
  98. pdfToolbarController?.setUpData()
  99. refreshToolbarView()
  100. }
  101. func refreshToolbarView() {
  102. let _manager = viewManager
  103. if _manager.toolMode == .Markup ||
  104. _manager.toolMode == .Edit ||
  105. _manager.toolMode == .Form ||
  106. _manager.toolMode == .Fill ||
  107. _manager.toolMode == .Convert ||
  108. _manager.toolMode == .Protect ||
  109. _manager.toolMode == .Tools {
  110. toolbarBoxHeightConst.constant = 80
  111. } else {
  112. if _manager.isPageEditMode {
  113. toolbarBoxHeightConst.constant = 80
  114. } else {
  115. toolbarBoxHeightConst.constant = 40
  116. }
  117. }
  118. }
  119. //MARK: - 侧边栏
  120. func initSideBar() {
  121. sidebarBox.borderWidth = 0
  122. if sideBarController == nil {
  123. sideBarController = KMPDFSideBarController.init()
  124. }
  125. sideBarController?.view.frame = sidebarBox.bounds
  126. sideBarController?.view.autoresizingMask = [.width, .height]
  127. sidebarBox.contentView = sideBarController?.view
  128. sideBarController?.pdfView = pdfView
  129. sideBarController?.delegate = self
  130. sideBarController?.pdfViewManager = viewManager
  131. sideBarController?.reloadData()
  132. }
  133. //MARK: - PDFDisplayView
  134. func updatePDFDisplaySettingView() {
  135. if viewManager.showDisplayView {
  136. infoSplitViewLeftConst.constant = 264
  137. } else {
  138. infoSplitViewLeftConst.constant = 44
  139. }
  140. if viewManager.showDisplayView {
  141. if displaySettingController == nil {
  142. displaySettingController = KMNDisplayViewController.init()
  143. }
  144. displaySettingController?.view.frame = CGRectMake(0, 0, 264, CGRectGetHeight(bottomContendBox.frame))
  145. displaySettingController?.view.autoresizingMask = [.height, .maxXMargin]
  146. bottomContendBox.addSubview(displaySettingController!.view)
  147. displaySettingController?.pdfView = self.pdfView
  148. displaySettingController?.viewManager = self.viewManager
  149. displaySettingController?.delegate = self
  150. displaySettingController?.reloadData()
  151. } else {
  152. displaySettingController?.view.removeFromSuperview()
  153. displaySettingController = nil
  154. }
  155. }
  156. //MARK: - 页面编辑
  157. func enterPageEditMode() {
  158. pageEditViewController = KMNPageEditViewController(self.document)
  159. if(pageEditViewController != nil) {
  160. bottomContendBox.addSubview(pageEditViewController!.view)
  161. pageEditViewController?.view.frame = bottomContendBox.bounds
  162. pageEditViewController?.selectionIndexPaths = [IndexPath(item: pdfView.currentPageIndex, section: 0)]
  163. pageEditViewController?.thumbnailBaseViewDelegate = self
  164. pageEditViewController?.view.autoresizingMask = [.width,.height]
  165. }
  166. }
  167. func exitPageEditMode() {
  168. if pageEditViewController != nil {
  169. pageEditViewController?.view.removeFromSuperview()
  170. pageEditViewController = nil
  171. }
  172. }
  173. //MARK: - 阅读模式
  174. func openPDFReadMode() {
  175. if viewManager.showDisplayView {
  176. viewManager.showDisplayView = false
  177. pdfToolbarController?.reloadLeftIconView()
  178. updatePDFDisplaySettingView()
  179. }
  180. infoSplitViewLeftConst.constant = 0
  181. toolbarBoxHeightConst.constant = 0
  182. }
  183. func exitPDFReadMode() {
  184. viewManager.isPDFReadMode = false
  185. infoSplitViewLeftConst.constant = 44
  186. refreshToolbarView()
  187. }
  188. //MARK: - PPT
  189. //MARK: - PDF分屏视图
  190. func reloadPDFSplitInfo() {
  191. if pdfView?.viewSplitMode == .disable {
  192. pdfSplitView.isHidden = true
  193. pdfView.frame = infoSplitCenterView.bounds
  194. infoSplitCenterView.addSubview(pdfView)
  195. if splitPDFController != nil {
  196. splitPDFController = nil
  197. }
  198. } else {
  199. pdfSplitView.isHidden = false
  200. pdfView.frame = pdfSplitTopView.bounds
  201. pdfSplitTopView.addSubview(pdfView)
  202. setUpPDFBottomToolbar()
  203. setupSplitPDFController()
  204. if pdfView.viewSplitMode == .horizontal {
  205. pdfSplitView.isVertical = true
  206. } else if pdfView.viewSplitMode == .vertical {
  207. pdfSplitView.isVertical = false
  208. }
  209. }
  210. }
  211. func setUpPDFBottomToolbar() {
  212. if pdfBottomToolbar != nil {
  213. pdfBottomToolbar = nil
  214. }
  215. pdfBottomToolbar = KMSplitToolbar.init()
  216. pdfBottomToolbar?.frame = CGRectMake(CGRectGetWidth(pdfSplitTopView.frame)/2-144, 20, 288, 40)
  217. pdfBottomToolbar?.pdfView = self.pdfView
  218. pdfBottomToolbar?.reloadData()
  219. pdfSplitTopView.addSubview(pdfBottomToolbar!)
  220. }
  221. func reloadPDFBottomToolbar() {
  222. if viewManager.splitShowBottomBar {
  223. pdfBottomToolbar?.isHidden = false
  224. pdfBottomToolbar?.reloadData()
  225. } else {
  226. pdfBottomToolbar?.isHidden = true
  227. }
  228. }
  229. }
  230. //MARK: - NSSplitViewDelegate
  231. extension KMPDFViewController: NSSplitViewDelegate {
  232. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  233. return true
  234. }
  235. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  236. return true
  237. }
  238. func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  239. if dividerIndex == 0 {
  240. if proposedPosition > CGRectGetWidth(infoContendSplitView.frame)/2 - 10 {
  241. print(CGRectGetWidth(infoContendSplitView.frame)/2 - 10, NSDate())
  242. return CGRectGetWidth(infoContendSplitView.frame)/2 - 10
  243. }
  244. } else if dividerIndex == 1 {
  245. print(CGRectGetWidth(infoContendSplitView.frame)/2 - 10, NSDate())
  246. }
  247. return proposedPosition
  248. }
  249. func splitViewDidResizeSubviews(_ notification: Notification) {
  250. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(splitViewResizeFinish), object: nil)
  251. self.perform(#selector(splitViewResizeFinish), with: nil, afterDelay: 0.15)
  252. }
  253. @objc func splitViewResizeFinish() {
  254. if infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) {
  255. if viewManager.pdfSideBarType != .none {
  256. viewManager.pdfSideBarType = .none
  257. sideBarController?.reloadBOTAData()
  258. }
  259. }
  260. }
  261. }
  262. //MARK: - KMPDFSideBarControllerDelegate 左侧Sidebar代理
  263. extension KMPDFViewController: KMPDFSideBarControllerDelegate {
  264. func kmPDFSideBarControllerDidSidebarTypeUpdated(_ view: KMPDFSideBarController) {
  265. if viewManager.pdfSideBarType == .none {
  266. infoContendSplitView.setPosition(-20, ofDividerAt: 0)
  267. } else if viewManager.pdfSideBarType == .search {
  268. infoContendSplitView.setPosition(264, ofDividerAt: 0)
  269. } else if viewManager.pdfSideBarType == .thumbnail {
  270. infoContendSplitView.setPosition(264, ofDividerAt: 0)
  271. } else if viewManager.pdfSideBarType == .outline {
  272. infoContendSplitView.setPosition(264, ofDividerAt: 0)
  273. } else if viewManager.pdfSideBarType == .bookmark {
  274. infoContendSplitView.setPosition(264, ofDividerAt: 0)
  275. } else if viewManager.pdfSideBarType == .annotation {
  276. infoContendSplitView.setPosition(264, ofDividerAt: 0)
  277. }
  278. }
  279. func kmPDFSideBarControllerDidGotoPage(_ view: KMPDFSideBarController, _ pageIndex: Int) {
  280. pdfView.go(toPageIndex: pageIndex, animated: true)
  281. }
  282. }
  283. //MARK: - KMPDFToolbarControllerDelegate 工具栏代理
  284. extension KMPDFViewController: KMPDFToolbarControllerDelegate {
  285. func kmPDFToolbarControllerDidToolbarItemClicked(_ controller: KMPDFToolbarController, _ itemIdentifier: String) {
  286. print("toolbar点击", itemIdentifier)
  287. if itemIdentifier == KMPDFToolbar_ViewDisplay_Identifier {
  288. updatePDFDisplaySettingView()
  289. } else if itemIdentifier == KMPDFToolbar_PageEdit_Identifier {
  290. if viewManager.isPageEditMode == true {
  291. enterPageEditMode()
  292. } else {
  293. exitPageEditMode()
  294. }
  295. } else if itemIdentifier == KMPDFToolbar_convert_word_Identifier {
  296. let winC = KMConvertWordWindowController()
  297. let model = KMDocumentModel(url: self.pdfView.document.documentURL)
  298. winC.documentModel = model
  299. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  300. } else if itemIdentifier == KMPDFToolbar_convert_ppt_Identifier {
  301. let winC = KMConvertPPTsWindowController()
  302. winC.subType = 1
  303. let model = KMDocumentModel(url: self.pdfView.document.documentURL)
  304. winC.documentModel = model
  305. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  306. } else if itemIdentifier == KMPDFToolbar_convert_RTF_Identifier {
  307. let winC = KMConvertPPTsWindowController()
  308. winC.subType = 2
  309. let model = KMDocumentModel(url: self.pdfView.document.documentURL)
  310. winC.documentModel = model
  311. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  312. } else if itemIdentifier == KMPDFToolbar_convert_Text_Identifier {
  313. let winC = KMConvertPPTsWindowController()
  314. winC.subType = 4
  315. let model = KMDocumentModel(url: self.pdfView.document.documentURL)
  316. winC.documentModel = model
  317. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  318. } else if itemIdentifier == KMPDFToolbar_convert_CSV_Identifier {
  319. let winC = KMConvertPPTsWindowController()
  320. winC.subType = 5
  321. let model = KMDocumentModel(url: self.pdfView.document.documentURL)
  322. winC.documentModel = model
  323. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  324. } else if itemIdentifier == KMPDFToolbar_convert_excel_Identifier {
  325. let winC = KMConvertExcelWindowController()
  326. let model = KMDocumentModel(url: self.pdfView.document.documentURL)
  327. winC.documentModel = model
  328. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  329. } else if itemIdentifier == KMPDFToolbar_convert_image_Identifier {
  330. let winC = KMConvertImageWindowController()
  331. let model = KMDocumentModel(url: self.pdfView.document.documentURL)
  332. winC.documentModel = model
  333. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  334. } else if itemIdentifier == KMPDFToolbar_convert_HTML_Identifier {
  335. let winC = KMConvertHtmlWindowController()
  336. let model = KMDocumentModel(url: self.pdfView.document.documentURL)
  337. winC.documentModel = model
  338. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  339. } else if itemIdentifier == KMPDFToolbar_convert_Json_Identifier {
  340. let winC = KMConvertJsonWindowController()
  341. let model = KMDocumentModel(url: self.pdfView.document.documentURL)
  342. winC.documentModel = model
  343. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  344. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertFile_Identifier {
  345. pageEditViewController?.insertFromPDFAction()
  346. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertBlank_Identifier {
  347. pageEditViewController?.insertFromBlankAction()
  348. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertClip_Identifier {
  349. pageEditViewController?.insertFromClipboardAction()
  350. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertScanner_Identifier {
  351. pageEditViewController?.insertFromScannerAction()
  352. } else if itemIdentifier == KMPDFToolbar_PageEdit_Extract_Identifier {
  353. pageEditViewController?.extractPDFAction()
  354. } else if itemIdentifier == KMPDFToolbar_PageEdit_Replace_Identifier {
  355. pageEditViewController?.replacePDFAction()
  356. } else if itemIdentifier == KMPDFToolbar_PageEdit_Split_Identifier {
  357. pageEditViewController?.splitPDFAction()
  358. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reverse_Identifier {
  359. pageEditViewController?.reversePDFAction()
  360. } else if itemIdentifier == KMPDFToolbar_PageEdit_LeftRotate_Identifier {
  361. pageEditViewController?.rotatePageLeftAction()
  362. } else if itemIdentifier == KMPDFToolbar_PageEdit_RightRotate_Identifier{
  363. pageEditViewController?.rotatePageRightAction()
  364. } else if itemIdentifier == KMPDFToolbar_PageEdit_Delete_Identifier {
  365. pageEditViewController?.deletePageAction()
  366. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reduce_Identifier {
  367. pageEditViewController?.zoomOutPageAction()
  368. if(pageEditViewController?.canZoomInPageSize() == false) {
  369. toolbarManager.page_Increase_Property.isDisabled = true
  370. } else {
  371. toolbarManager.page_Increase_Property.isDisabled = false
  372. }
  373. if(pageEditViewController?.canZoomOutPageSize() == false) {
  374. toolbarManager.page_Reduce_Property.isDisabled = true
  375. } else {
  376. toolbarManager.page_Reduce_Property.isDisabled = false
  377. }
  378. controller.refreshSecondToolbarItemsState()
  379. } else if itemIdentifier == KMPDFToolbar_PageEdit_Increase_Identifier {
  380. pageEditViewController?.zoomInPageAction()
  381. if(pageEditViewController?.canZoomInPageSize() == false) {
  382. toolbarManager.page_Increase_Property.isDisabled = true
  383. } else {
  384. toolbarManager.page_Increase_Property.isDisabled = false
  385. }
  386. if(pageEditViewController?.canZoomOutPageSize() == false) {
  387. toolbarManager.page_Reduce_Property.isDisabled = true
  388. } else {
  389. toolbarManager.page_Reduce_Property.isDisabled = false
  390. }
  391. controller.refreshSecondToolbarItemsState()
  392. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_oddPage_Identifier {
  393. pageEditViewController?.thumbnailChoosePageStyle = .odd
  394. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  395. toolbarManager.page_pageInfo_Property.creatable = false
  396. controller.refreshSecondToolbarItemsState()
  397. }
  398. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_EvenPage_Identifier {
  399. pageEditViewController?.thumbnailChoosePageStyle = .even
  400. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  401. toolbarManager.page_pageInfo_Property.creatable = false
  402. controller.refreshSecondToolbarItemsState()
  403. }
  404. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_PortraitPage_Identifier {
  405. pageEditViewController?.thumbnailChoosePageStyle = .horizontal
  406. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  407. toolbarManager.page_pageInfo_Property.creatable = false
  408. controller.refreshSecondToolbarItemsState()
  409. }
  410. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_LandscapePage_Identifier {
  411. pageEditViewController?.thumbnailChoosePageStyle = .vertical
  412. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  413. toolbarManager.page_pageInfo_Property.creatable = false
  414. controller.refreshSecondToolbarItemsState()
  415. }
  416. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_AllPage_Identifier {
  417. pageEditViewController?.thumbnailChoosePageStyle = .allPage
  418. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  419. toolbarManager.page_pageInfo_Property.creatable = false
  420. controller.refreshSecondToolbarItemsState()
  421. }
  422. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_CustomPage_Identifier {
  423. pageEditViewController?.thumbnailChoosePageStyle = .custom
  424. toolbarManager.page_pageInfo_Property.text = nil
  425. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  426. toolbarManager.page_pageInfo_Property.creatable = true
  427. }
  428. controller.refreshSecondToolbarItemsState()
  429. }
  430. refreshToolbarView()
  431. }
  432. }
  433. //MARK: - KMNDisplayViewControllerDelegate代理
  434. extension KMPDFViewController: KMNDisplayViewControllerDelegate {
  435. //Display Mode
  436. func displayViewControllerDidDisplayModeChanged(_ controller: KMNDisplayViewController) {
  437. pdfView.layoutDocumentView()
  438. }
  439. //阅读模式
  440. func displayViewControllerDidReadModeUpdated(_ controller: KMNDisplayViewController) {
  441. if viewManager.isPDFReadMode {
  442. openPDFReadMode()
  443. } else {
  444. exitPDFReadMode()
  445. }
  446. }
  447. //PPT
  448. func displayViewControllerDidGotoSlideShow(_ controller: KMNDisplayViewController) {
  449. }
  450. //SplitView
  451. func displayViewControllerDidSplitModeChanged(_ controller: KMNDisplayViewController) {
  452. reloadPDFSplitInfo()
  453. }
  454. func displayViewControllerDidSplitFileChanged(_ controller: KMNDisplayViewController) {
  455. splitPDFController?.reloadData()
  456. }
  457. func displayViewControllerDidToolbarStateChanged(_ controller: KMNDisplayViewController) {
  458. splitPDFController?.refreshToolbarState()
  459. reloadPDFBottomToolbar()
  460. }
  461. }
  462. //MARK: - KMSplitPDFViewControllerDelegate SplitPDFView分屏视图
  463. extension KMPDFViewController: KMSplitPDFViewControllerDelegate {
  464. func splitPDFViewControllerDidUpdateFilePath(_ controller: KMSplitPDFViewController) {
  465. displaySettingController?.reloadData()
  466. }
  467. func splitPDFViewControllerDidUpdatePDFScale(_ controller: KMSplitPDFViewController) {
  468. if let scaleFactor = controller.pdfView?.scaleFactor {
  469. pdfView.scaleFactor = scaleFactor
  470. }
  471. }
  472. func splitPDFViewControllerDidUpdatePDFPageIndex(_ controller: KMSplitPDFViewController) {
  473. if let pageIndex = controller.pdfView?.currentPageIndex {
  474. pdfView.go(toPageIndex: pageIndex, animated: false)
  475. }
  476. }
  477. }
  478. //MARK: - CPDFViewDelegate PDFView代理
  479. extension KMPDFViewController: CPDFViewDelegate {
  480. func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
  481. sideBarController?.reloadPageTurnerData()
  482. }
  483. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  484. sideBarController?.reloadPageTurnerData()
  485. reloadPDFBottomToolbar()
  486. //分屏视图
  487. if viewManager.splitSyncScroll {
  488. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  489. splitPDFController?.outPDFFirst = true
  490. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  491. var index = pdfView.currentPageIndex
  492. if index > document.pageCount {
  493. index = Int(splitPDFView.document.pageCount - 1)
  494. }
  495. splitPDFView.go(toPageIndex: index, animated: false)
  496. }
  497. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  498. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  499. } else if splitPDFController?.outPDFFirst == true {
  500. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  501. var index = pdfView.currentPageIndex
  502. if index > document.pageCount {
  503. index = Int(splitPDFView.document.pageCount - 1)
  504. }
  505. splitPDFView.go(toPageIndex: index, animated: false)
  506. }
  507. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  508. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  509. }
  510. }
  511. }
  512. func pdfViewScaleDidChanged(_ pdfView: CPDFView!) {
  513. pdfToolbarController?.reloadSelectZoomView()
  514. reloadPDFBottomToolbar()
  515. //分屏视图
  516. if viewManager.splitSyncScroll {
  517. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  518. splitPDFController?.outPDFFirst = true
  519. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  520. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  521. } else if splitPDFController?.outPDFFirst == true {
  522. if let splitPDFView = splitPDFController?.pdfView {
  523. splitPDFView.scaleFactor = pdfView.scaleFactor
  524. }
  525. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  526. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  527. }
  528. }
  529. }
  530. @objc func pdfUpdatedFinish() {
  531. splitPDFController?.inPDFFirst = false
  532. splitPDFController?.outPDFFirst = false
  533. }
  534. }
  535. extension KMPDFViewController: CPDFListViewDelegate {
  536. func pdfListViewKeyDownIsContinue(_ pdfListView: CPDFListView!, theEvent: NSEvent!) -> Bool {
  537. let command = theEvent.modifierFlags.contains(.command)
  538. let control = theEvent.modifierFlags.contains(.control)
  539. KMPrint(theEvent.keyCode)
  540. if pdfView.isEditing() {
  541. if control && theEvent.keyCode == 11 { // ctr + b
  542. return false
  543. } else if control && theEvent.keyCode == 34 { // ctr +i
  544. return false
  545. } else if theEvent.keyCode == 36 { // enter
  546. if pdfView.isCropMode {
  547. return false
  548. }
  549. }
  550. }
  551. if (theEvent.keyCode == 11 && command) { // command + B [添加书签]
  552. return false
  553. } else if (command && control && theEvent.keyCode == 14) { // command + control + E [注释 橡皮擦]
  554. return false
  555. } else if (theEvent.keyCode == 123) { // 向左
  556. if(self.pdfView.isEditing() && !self.pdfView.isSelecteditAreaNotEdit()) {
  557. return false
  558. } else {
  559. }
  560. } else if (theEvent.keyCode == 126) { // 向上
  561. if(self.pdfView.isEditing() && !self.pdfView.isSelecteditAreaNotEdit()) {
  562. return false
  563. } else {
  564. if (self.pdfView.isContinousScroll()) {
  565. return true
  566. }
  567. }
  568. } else if (theEvent.keyCode == 124) { // 向右
  569. if(self.pdfView.isEditing() && !self.pdfView.isSelecteditAreaNotEdit()) {
  570. return false
  571. } else {
  572. }
  573. } else if (theEvent.keyCode == 125) { // 向下
  574. if(self.pdfView.isEditing() && !self.pdfView.isSelecteditAreaNotEdit()) {
  575. return false
  576. } else {
  577. if (self.pdfView.isContinousScroll()) {
  578. return true
  579. }
  580. }
  581. } else if (theEvent.keyCode == 36) {
  582. if pdfView.annotationType == .addImage || pdfView.annotationType == .addText {
  583. if self.pdfView.isEditImage {
  584. }
  585. }
  586. }
  587. if theEvent.keyCode == 53 {
  588. //ESC
  589. if view.window?.styleMask.contains(.fullScreen) == true {
  590. view.window?.toggleFullScreen(nil)
  591. }
  592. //阅读模式
  593. if viewManager.isPDFReadMode {
  594. exitPDFReadMode()
  595. viewManager.isPDFReadMode = false
  596. return true
  597. }
  598. }
  599. return true
  600. }
  601. }
  602. //MARK: - KMNThumbnailBaseViewDelegate
  603. extension KMPDFViewController: KMNThumbnailBaseViewDelegate {
  604. func clickThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,currentIndex:Int) {
  605. exitPageEditMode()
  606. viewManager.isPageEditMode = false
  607. pdfToolbarController?.reloadPageEditView()
  608. }
  609. func insertPDFThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, pdfDocment: CPDFDocument?) {
  610. if(pdfDocment != nil) {
  611. insertDocuments.insert(pdfDocment!)
  612. }
  613. }
  614. func changeIndexPathsThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, selectionIndexPaths: Set<IndexPath>, selectionStrings: String) {
  615. toolbarManager.page_pageInfo_Property.text = selectionStrings
  616. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  617. toolbarManager.page_pageInfo_Property.creatable = true
  618. }
  619. pdfToolbarController?.refreshSecondToolbarItemsState()
  620. }
  621. }