KMMainViewController+MenuAction.swift 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. //
  2. // KMMainViewController+MenuAction.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/2/19.
  6. //
  7. import Foundation
  8. // MARK: File Menu
  9. extension KMMainViewController {
  10. @IBAction func menuItemClick_mergePDF(_ sender: Any) {
  11. }
  12. @IBAction func menuItemClick_Compress(_ sender: Any) {
  13. }
  14. @IBAction func menuItemClick_Convert(_ sender: Any) {
  15. }
  16. @IBAction func menuItemAction_ConvertToWord(_ sender: Any) {
  17. }
  18. @IBAction func menuItemAction_ConvertToExcel(_ sender: Any) {
  19. }
  20. @IBAction func menuItemAction_ConvertToPPT(_ sender: Any) {
  21. }
  22. @IBAction func menuItemAction_ConvertToRTF(_ sender: Any) {
  23. }
  24. @IBAction func menuItemAction_ConvertToHTML(_ sender: Any) {
  25. }
  26. @IBAction func menuItemAction_ConvertToText(_ sender: Any) {
  27. }
  28. @IBAction func menuItemAction_ConvertToCSV(_ sender: Any) {
  29. }
  30. @IBAction func menuItemAction_ConvertToImage(_ sender: Any) {
  31. }
  32. @IBAction func menuItemClick_SettingPassword(_ sender: Any) {
  33. }
  34. @IBAction func menuItemClick_RemovePassword(_ sender: Any) {
  35. }
  36. @IBAction func menuItemAction_closeWindow(_ sender: Any) {
  37. self.view.window?.close()
  38. }
  39. @IBAction func menuItemAction_closeAllWindows(_ sender: Any) {
  40. for window in NSApp.windows {
  41. window.close()
  42. }
  43. }
  44. @IBAction func menuItemAction_closeTagPage(_ sender: Any) {
  45. self.browserWindowController?.browser?.closeTab()
  46. }
  47. @IBAction func menuItemAction_showInFinder(_ sender: Any) {
  48. NSWorkspace.shared.activateFileViewerSelecting([self.listView.document.documentURL])
  49. }
  50. @IBAction func menuItemAction_property(_ sender: Any) {
  51. KMInfoWindowController.shared.showWindow(sender)
  52. }
  53. @IBAction func menuItemAction_print(_ sender: Any) {
  54. self.showPrintWindow()
  55. }
  56. }
  57. // MARK: Edit Menu
  58. @objc protocol KMEditMenuProtocol: NSObjectProtocol {
  59. @objc optional func undo(_ sender: Any)
  60. @objc optional func redo(_ sender: Any)
  61. @objc optional func cut(_ sender: Any)
  62. @objc optional func copy(_ sender: Any)
  63. @objc optional func paste(_ sender: Any)
  64. @objc optional func delete(_ sender: Any)
  65. }
  66. extension KMMainViewController: KMEditMenuProtocol {
  67. @IBAction func menuItemAction_find(_ sender: Any) {
  68. if (self.leftSideViewController.isShowPanel) {
  69. self.leftSideViewController.showPanelView(show: false)
  70. }
  71. let isSearch = self.leftSideViewController.type.methodType == .Search
  72. self.leftSideViewController.refreshMethodType(methodType: .Search)
  73. if (isSearch) { // 关闭
  74. self.view.window?.makeFirstResponder(self.listView)
  75. }
  76. }
  77. }
  78. // MARK: - View Menu
  79. extension KMMainViewController {
  80. @IBAction func menuItemAction_adjustWidth(_ sender: Any) {
  81. self.selectZoom(.width)
  82. }
  83. @IBAction func menuItemAction_adjustPage(_ sender: Any) {
  84. self.selectZoom(.fit)
  85. }
  86. @IBAction func menuItemAction_size(_ sender: Any) {
  87. self.selectZoom(.actualSize)
  88. }
  89. @IBAction func menuItemAction_zoomOut(_ sender: Any) {
  90. if (self.listView.canZoomOut) {
  91. self.listView.zoomOut(nil)
  92. }
  93. }
  94. @IBAction func menuItemAction_zoomIn(_ sender: Any) {
  95. if (self.listView.canZoomIn) {
  96. self.listView.zoomIn(nil)
  97. }
  98. }
  99. // page show
  100. @IBAction func menuItemAction_singlePage(_ sender: Any) {
  101. }
  102. @IBAction func menuItemAction_singlePageContinue(_ sender: Any) {
  103. }
  104. @IBAction func menuItemAction_doublePage(_ sender: Any) {
  105. }
  106. @IBAction func menuItemAction_doublePageContinue(_ sender: Any) {
  107. }
  108. @IBAction func menuItemAction_bookMode(_ sender: Any) {
  109. }
  110. @IBAction func menuItemAction_readMode(_ sender: Any) {
  111. if (viewManager.isPDFReadMode) {
  112. self.exitPDFReadMode()
  113. } else {
  114. self.openPDFReadMode()
  115. }
  116. }
  117. @IBAction func menuItemAction_showSplitPage(_ sender: Any) {
  118. self.listView.displaysPageBreaks = !(self.listView.displaysPageBreaks ?? false)
  119. self.listView.layoutDocumentView()
  120. }
  121. @IBAction func menuItemAction_autoScrol(_ sender: Any) {
  122. }
  123. @IBAction func menuItemAction_autoScrolSetting(_ sender: Any) {
  124. }
  125. // rotate
  126. @IBAction func menuItemAction_rotateLeft(_ sender: Any) {
  127. self.rotateLeft(sender)
  128. }
  129. func rotatePageItems(pageItems: [KMThumbnailPageItem]) {
  130. let tempPageItems = pageItems
  131. for pageItem in tempPageItems {
  132. pageItem.page.rotation = pageItem.rotate
  133. }
  134. self.listView.layoutDocumentView()
  135. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMPDFViewRotatePage"), object: self.listView.document)
  136. self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  137. var tempPageItems: [KMThumbnailPageItem] = []
  138. for pageItem in pageItems {
  139. let item = KMThumbnailPageItem(page: pageItem.page, rotate: pageItem.oldRotate, oldRotate: pageItem.rotate)
  140. tempPageItems.append(item)
  141. }
  142. self?.rotatePageItems(pageItems: tempPageItems)
  143. }
  144. }
  145. @IBAction func menuItemAction_rotateRight(_ sender: Any) {
  146. self.rotateRight(sender)
  147. }
  148. @IBAction func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  149. self.rotateAllLeft(sender)
  150. }
  151. @IBAction func menuItemAction_rotateAllPageRight(_ sender: Any) {
  152. self.rotateAllRight(sender)
  153. }
  154. // split screen
  155. @IBAction func menuItemAction_splitScreenVerti(_ sender: Any) {
  156. }
  157. @IBAction func menuItemAction_splitScreenHorti(_ sender: Any) {
  158. }
  159. @IBAction func menuItemAction_splitScreenNo(_ sender: Any) {
  160. }
  161. @IBAction func menuItemAction_view_readMode(_ sender: Any) {
  162. if (viewManager.isPDFReadMode) {
  163. self.exitPDFReadMode()
  164. } else {
  165. self.openPDFReadMode()
  166. }
  167. }
  168. @IBAction func menuItemAction_enterFullScreen(_ sender: Any) {
  169. }
  170. @IBAction func menuItemAction_hiddenLeftSide(_ sender: Any) {
  171. if (self.leftSideViewController.type.methodType != .None) {
  172. self.leftSideViewController.selectType(self.leftSideViewController.type.methodType)
  173. } else {
  174. }
  175. }
  176. @IBAction func menuItemAction_hiddenRightSide(_ sender: Any) {
  177. }
  178. @IBAction func menuItemAction_thumai(_ sender: Any) {
  179. self.leftSideViewController.switchType(.Thumbnail)
  180. }
  181. @IBAction func menuItemAction_outline(_ sender: Any) {
  182. self.leftSideViewController.switchType(.Outline)
  183. }
  184. @IBAction func menuItemAction_bookmark(_ sender: Any) {
  185. self.leftSideViewController.switchType(.Annotation)
  186. }
  187. @IBAction func menuItemAction_annotation(_ sender: Any) {
  188. self.leftSideViewController.switchType(.Annotation)
  189. }
  190. @IBAction func menuItemAction_search(_ sender: Any) {
  191. self.leftSideViewController.switchType(.Search)
  192. }
  193. @IBAction func menuItemAction_topic(_ sender: Any) {
  194. }
  195. @IBAction func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  196. }
  197. @IBAction func chooseTransition(_ sender: Any?) {
  198. let presentationSheetController = SKPresentationOptionsSheetController()
  199. presentationSheetController.controller = self
  200. self.km_beginSheet(windowC: presentationSheetController)
  201. presentationSheetController.closeCallBack = {[weak self] vc in
  202. self?.km_quick_endSheet()
  203. }
  204. }
  205. @IBAction func toggleAutoFlow(_ sender: Any?) {
  206. if (self.listView.isAutoFlow()) {
  207. self.listView.stopAutoFlow()
  208. } else {
  209. self.listView.startAutoFlow()
  210. }
  211. }
  212. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  213. autoFlowOptionsSheetController = KMAutoFlowOptionsSheetController(windowNibName: "KMAutoFlowOptionsSheetController")
  214. NSWindow.currentWindow().beginSheet((autoFlowOptionsSheetController?.window)!) { responce in }
  215. }
  216. @IBAction func toggleReadingBar(_ sender: Any?) {
  217. KMPrint("toggleReadingBar")
  218. }
  219. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  220. if self.model.leftPanelOpen {
  221. self.closeLeftPane()
  222. } else {
  223. self.openLeftPane()
  224. }
  225. }
  226. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  227. guard let item = sender as? NSMenuItem else {
  228. NSSound.beep()
  229. return
  230. }
  231. if item.tag == 3 {
  232. self.leftSideViewController.switchType(.snapshot)
  233. }
  234. }
  235. @IBAction func splitViewAction(_ sender: Any?) {
  236. guard let item = sender as? NSMenuItem else {
  237. return
  238. }
  239. if item.tag < 2 && IAPProductsManager.default().isAvailableAllFunction() == false {
  240. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  241. return
  242. }
  243. self.splitView(withTag: item.tag)
  244. }
  245. @IBAction func themesColor(_ sender: Any?) {
  246. guard let item = sender as? NSMenuItem else {
  247. NSSound.beep()
  248. return
  249. }
  250. let mode = item.tag
  251. if mode > 0 {
  252. if !IAPProductsManager.default().isAvailableAllFunction() {
  253. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  254. return
  255. }
  256. }
  257. if mode == 0 {
  258. self.listView.displayMode = .normal
  259. } else if mode == 1 {
  260. self.listView.displayMode = .soft
  261. } else if mode == 2 {
  262. self.listView.displayMode = .night
  263. } else if mode == 3 {
  264. self.listView.displayMode = .green
  265. } else {
  266. self.listView.displayMode = .custom
  267. var data: Data? = nil
  268. if let themesData = UserDefaults.standard.object(forKey: "kKMPDFViewModeThemesArray") as? Data {
  269. data = themesData
  270. }
  271. let appArray = NSKeyedUnarchiver.unarchiveObject(with: data ?? Data()) as? [Any] ?? []
  272. let mutableArray = appArray
  273. if mutableArray.count > 0 {
  274. let themesArray = mutableArray as! [NSColor]
  275. let color = themesArray[mode]
  276. self.listView.displayModeCustomColor = color
  277. }
  278. }
  279. self.listView.layoutDocumentView()
  280. self.listView.setNeedsDisplayForVisiblePages()
  281. UserDefaults.standard.set(mode, forKey: "kPDFViewModeKey")
  282. UserDefaults.standard.synchronize()
  283. }
  284. @IBAction func toggleStatusBar(_ sender: Any?) {
  285. KMPrint("toggleStatusBar ...")
  286. }
  287. @IBAction func toggleToolbarShow(_ sender: Any?) {
  288. }
  289. }
  290. // MARK: - Annotation Menu
  291. extension KMMainViewController {
  292. @IBAction func menuItemAction_highlight(_ sender: Any) {
  293. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarHighlightAnnotationItemIdentifier)
  294. }
  295. @IBAction func menuItemAction_underline(_ sender: Any) {
  296. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarUnderlineAnnotationItemIdentifier)
  297. }
  298. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  299. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarStrikeOutAnnotationItemIdentifier)
  300. }
  301. @IBAction func menuItemAction_wavyLine(_ sender: Any) {
  302. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarSquigglyAnnotationItemIdentifier)
  303. }
  304. @IBAction func menuItemAction_freehand(_ sender: Any) {
  305. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarInkAnnotationItemIdentifier)
  306. }
  307. @IBAction func menuItemAction_text(_ sender: Any) {
  308. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarFreeTextAnnotationItemIdentifier)
  309. }
  310. @IBAction func menuItemAction_note(_ sender: Any) {
  311. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarAnchoredAnnotationItemIdentifier)
  312. }
  313. @IBAction func menuItemAction_squre(_ sender: Any) {
  314. UserDefaults.standard.set(6, forKey: KMToolBarToolPDFShapeStyle)
  315. }
  316. @IBAction func menuItemAction_circle(_ sender: Any) {
  317. UserDefaults.standard.set(7, forKey: KMToolBarToolPDFShapeStyle)
  318. }
  319. @IBAction func menuItemAction_arrow(_ sender: Any) {
  320. UserDefaults.standard.set(5, forKey: KMToolBarToolPDFShapeStyle)
  321. }
  322. @IBAction func menuItemAction_line(_ sender: Any) {
  323. UserDefaults.standard.set(4, forKey: KMToolBarToolPDFShapeStyle)
  324. }
  325. // link
  326. @IBAction func menuItemAction_link(_ sender: Any) {
  327. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  328. }
  329. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  330. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  331. }
  332. @IBAction func menuItemAction_linkHttps(_ sender: Any) {
  333. }
  334. @IBAction func menuItemAction_linkEmail(_ sender: Any) {
  335. }
  336. @IBAction func notesLink(_ sender: Any?) {
  337. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  338. }
  339. // stamp
  340. @IBAction func menuItemAction_stamp(_ sender: Any) {
  341. self.mainMenuUpdateAnnotationStyle(identifier: KMAnnotationStampToolbarItemIdentifier)
  342. }
  343. @IBAction func menuItemAction_stampStandard(_ sender: Any) {
  344. }
  345. @IBAction func menuItemAction_stampDynamic(_ sender: Any) {
  346. }
  347. @IBAction func menuItemAction_stampCustom(_ sender: Any) {
  348. }
  349. @IBAction func notesStamp(_ sender: Any?) {
  350. self.mainMenuUpdateAnnotationStyle(identifier: KMAnnotationStampToolbarItemIdentifier)
  351. }
  352. @IBAction func menuItemAction_signure(_ sender: Any) {
  353. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarSignSignatureAnnotationItemIdentifier)
  354. }
  355. @IBAction func image(_ sender: Any?) {
  356. }
  357. @IBAction func table(_ sender: Any?) {
  358. }
  359. @IBAction func addForm(_ sender: Any?) {
  360. guard let item = sender as? NSMenuItem else {
  361. NSSound.beep()
  362. return
  363. }
  364. var identifiers: String?
  365. let idx = item.tag
  366. if idx == 0 { // raido
  367. identifiers = KMToolbarToolRadioButtonItemIdentifier
  368. } else if idx == 1 { // checkbox
  369. identifiers = KMToolbarToolCheckBoxItemIdentifier
  370. } else if idx == 2 { // textfiled
  371. identifiers = KMToolbarToolTextFieldItemIdentifier
  372. } else if idx == 3 { // combobox
  373. identifiers = KMToolbarToolPullDownmenuItemIdentifier
  374. } else if idx == 4 { // listbox
  375. identifiers = KMToolbarToolListBoxItemIdentifier
  376. } else if idx == 5 { // button
  377. identifiers = KMToolbarToolButtonIdentifier
  378. } else if idx == 6 { // sign
  379. identifiers = KMToolbarToolSignBoxIdentifier
  380. }
  381. if let itemID = identifiers {
  382. }
  383. }
  384. @IBAction func editNote(_ sender: Any?) {
  385. if self.listView.hideNotes {
  386. NSSound.beep()
  387. return
  388. }
  389. if let anno = self.listView.activeAnnotation {
  390. self.listView.edit(anno)
  391. }
  392. }
  393. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  394. self.showOrHideNotes()
  395. }
  396. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  397. self.removeAllAnnotations()
  398. }
  399. private func mainMenuUpdateAnnotationStyle(identifier : String) {
  400. }
  401. }
  402. // MARK: - KMSystemPDFMenuProtocol
  403. extension KMMainViewController: KMSystemPDFMenuProtocol {
  404. func changeDisplayBox(_ sender: Any?) {
  405. guard let item = sender as? NSMenuItem else {
  406. NSSound.beep()
  407. return
  408. }
  409. let mode = item.tag
  410. if mode == 0 {
  411. self.listView.displayBox = .mediaBox
  412. } else if mode == 1 {
  413. self.listView.displayBox = .cropBox
  414. }
  415. }
  416. func toggleAutoScale(_ sender: Any?) {
  417. self.listView.autoScales = !self.listView.autoScales
  418. }
  419. func doZoomToActualSize(_ sender: Any?) {
  420. self.selectZoom(.actualSize)
  421. }
  422. func doZoomToFit(_ sender: Any?) {
  423. self.selectZoom(.fit)
  424. }
  425. func doZoomIn(_ sender: Any?) {
  426. if (self.listView.canZoomIn) {
  427. self.listView.zoomIn(nil)
  428. }
  429. if self.digitalSignController?.pdfView != nil {
  430. if (self.digitalSignController?.pdfView.canZoomIn) == true {
  431. self.digitalSignController?.pdfView.zoomIn(nil)
  432. }
  433. }
  434. if self.redactController?.redactPdfView != nil {
  435. if (self.redactController?.redactPdfView.canZoomIn) == true {
  436. self.redactController?.redactPdfView.zoomIn(nil)
  437. }
  438. }
  439. }
  440. func doZoomOut(_ sender: Any?) {
  441. if (self.listView.canZoomOut) {
  442. self.listView.zoomOut(nil)
  443. }
  444. if self.digitalSignController?.pdfView != nil {
  445. if (self.digitalSignController?.pdfView.canZoomOut) == true {
  446. self.digitalSignController?.pdfView.zoomOut(nil)
  447. }
  448. }
  449. if self.redactController?.redactPdfView != nil {
  450. if (self.redactController?.redactPdfView.canZoomOut) == true {
  451. self.redactController?.redactPdfView.zoomOut(nil)
  452. }
  453. }
  454. }
  455. func doMarqueeZoomTool(_ sender: Any?) {
  456. }
  457. func highlightFormFiled(_ sender: Any?) {
  458. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  459. CPDFKitConfig.sharedInstance().setEnableFormFieldHighlight(!enabled)
  460. self.listView.setNeedsDisplayForVisiblePages()
  461. }
  462. func highlightLinks(_ sender: Any?) {
  463. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  464. CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(!enabled)
  465. self.listView.setNeedsDisplayForVisiblePages()
  466. }
  467. func resetForm(_ sender: Any?) {
  468. self.listView.resetFormAnnotation()
  469. }
  470. func removeAllAnnotation(_ sender: Any?) {
  471. self.listView.removeAllAnnotation()
  472. }
  473. func savePDFSettingToDefaults(_ sender: Any?) {
  474. if self.interactionMode == .normal {
  475. UserDefaults.standard.sync_setValue(self.currentPDFSettings(), forKey: KMDefaultPDFDisplaySettingsKey)
  476. } else if self.interactionMode != .presentation {
  477. UserDefaults.standard.sync_setValue(self.currentPDFSettings(), forKey: KMDefaultFullScreenPDFDisplaySettingsKey)
  478. }
  479. if listView.displaysAsBook && self.listView.displayTwoUp {
  480. if listView.fetchDisplayViewMode() == .twoUp {
  481. } else {
  482. }
  483. } else {
  484. }
  485. self.savePageNumberIfNeed()
  486. }
  487. }
  488. // MARK: - goto Menu
  489. extension KMMainViewController {
  490. @IBAction func menuItemAction_nextPage(_ sender: Any) {
  491. if (self.listView.canGoToNextPage()) {
  492. self.listView.goToNextPage(nil)
  493. }
  494. }
  495. @IBAction func menuItemAction_forwardPage(_ sender: Any) {
  496. if (self.listView.canGoToPreviousPage()) {
  497. self.listView.goToPreviousPage(nil)
  498. }
  499. }
  500. @IBAction func menuItemAction_firstPage(_ sender: Any) {
  501. if (self.listView.canGoToFirstPage()) {
  502. self.listView.goToFirstPage(nil)
  503. }
  504. }
  505. @IBAction func menuItemAction_lastPage(_ sender: Any) {
  506. if (self.listView.canGoToLastPage()) {
  507. self.listView.goToLastPage(nil)
  508. }
  509. }
  510. @IBAction func menuItemAction_forward(_ sender: Any) {
  511. if (self.listView.km_canGoForward()) {
  512. self.listView.km_goForward(nil)
  513. }
  514. }
  515. @IBAction func menuItemAction_goback(_ sender: Any) {
  516. if (self.listView.km_canGoBack()) {
  517. self.listView.km_goBack(nil)
  518. }
  519. }
  520. @IBAction func menuItemAction_gotoPage(_ sender: Any) {
  521. let sheet = KMTextFieldSheetController.init(windowNibName: "PageSheet")
  522. var pages : [String] = []
  523. for i in 0 ..< self.listView.document.pageCount {
  524. pages.append("\(i)")
  525. }
  526. sheet.callback = { [weak self] stringValue in
  527. guard let index = Int(stringValue) else {
  528. return
  529. }
  530. if (self?.listView == nil) {
  531. return
  532. }
  533. if (index > 0 && index <= self!.listView.document.pageCount) {
  534. self?.listView.go(toPageIndex: index-1, animated: true)
  535. }
  536. }
  537. sheet.showWindow(nil)
  538. sheet.pageBox.addItems(withObjectValues: pages)
  539. sheet.stringValue = "\(self.listView.currentPageIndex+1)"
  540. }
  541. @IBAction func goToMarkedPage(_ sender: Any?) {
  542. guard let pdfDoc = self.listView.document, pdfDoc.isLocked == false && pdfDoc.pageCount > 0 else {
  543. NSSound.beep()
  544. return
  545. }
  546. guard let currentPageIndex = self.listView.currentPage()?.pageIndex() else {
  547. NSSound.beep()
  548. return
  549. }
  550. if self.model.beforeMarkedPageIndex != NSNotFound {
  551. self.listView.goToPage(at: min(UInt(self.model.beforeMarkedPageIndex), pdfDoc.pageCount-1), point: self.model.beforeMarkedPagePoint)
  552. } else if (currentPageIndex != self.model.markedPageIndex) {
  553. let lastPageIndex = self.listView.currentPageIndexAndPoint(&self.model.beforeMarkedPagePoint, rotated: nil)
  554. self.listView.goToPage(at: min(UInt(self.model.markedPageIndex), pdfDoc.pageCount-1), point: self.model.markedPagePoint)
  555. self.model.beforeMarkedPageIndex = Int(lastPageIndex)
  556. }
  557. }
  558. @IBAction func markPage(_ sender: Any?) {
  559. self.model.markedPageIndex = Int(self.listView.currentPageIndexAndPoint(&self.model.markedPagePoint, rotated: nil))
  560. self.model.beforeMarkedPageIndex = NSNotFound
  561. }
  562. }
  563. // MARK: - tool Menu
  564. extension KMMainViewController: KMSystemToolMenuProtocol {
  565. func getInfo(_ sender: Any?) {
  566. KMInfoWindowController.shared.showWindow(sender)
  567. }
  568. @IBAction func menuItemAction_textTool(_ sender: Any) {
  569. self.listView.toolMode = .textToolMode
  570. self.listView.annotationType = .unkown
  571. }
  572. @IBAction func menuItemAction_scrolTool(_ sender: Any) {
  573. }
  574. @IBAction func menuItemAction_zoomOutTool(_ sender: Any) {
  575. }
  576. @IBAction func menuItemAction_selectTool(_ sender: Any) {
  577. }
  578. func changeToolMode(_ sender: Any?) {
  579. let value = KMDataManager.ud_integer(forKey: SKLastAnnotationModeKey)
  580. var aType: CAnnotationType = .highlight
  581. if let data = CAnnotationType(rawValue: value) {
  582. aType = data
  583. }
  584. if aType.isMarkup() || aType.isSquare() || aType == .freeText || aType == .anchored {
  585. } else {
  586. aType = .highlight
  587. }
  588. var itemId: String = KMToolbarHighlightAnnotationItemIdentifier
  589. if aType == .highlight {
  590. } else if aType == .underline {
  591. itemId = KMToolbarUnderlineAnnotationItemIdentifier
  592. } else if aType == .strikeOut {
  593. itemId = KMToolbarStrikeOutAnnotationItemIdentifier
  594. } else if aType == .ink {
  595. itemId = KMToolbarInkAnnotationItemIdentifier
  596. } else if aType == .freeText {
  597. itemId = KMToolbarFreeTextAnnotationItemIdentifier
  598. } else if aType == .anchored {
  599. itemId = KMToolbarAnchoredAnnotationItemIdentifier
  600. } else if aType == .square {
  601. itemId = KMToolbarSquareAnnotationItemIdentifier
  602. } else if aType == .circle {
  603. itemId = KMToolbarCircleAnnotationItemIdentifier
  604. } else if aType == .line {
  605. itemId = KMToolbarLineAnnotationItemIdentifier
  606. } else if aType == .arrow {
  607. itemId = KMToolbarArrowAnnotationItemIdentifier
  608. } else if aType == .link {
  609. itemId = KMToolbarLinkAnnotationItemIdentifier
  610. }
  611. }
  612. @IBAction func menuItemAction_redact(_ sender: Any) {
  613. }
  614. @IBAction func menuItemAction_warkmark(_ sender: Any) {
  615. }
  616. @IBAction func menuItemAction_background(_ sender: Any) {
  617. }
  618. @IBAction func menuItemAction_headerfooter(_ sender: Any) {
  619. }
  620. @IBAction func menuItemAction_bates(_ sender: Any) {
  621. }
  622. @IBAction func menuItemAction_batch(_ sender: Any) {
  623. }
  624. func compression(_ sender: Any?) {
  625. }
  626. func merge(_ sender: Any?) {
  627. }
  628. func convert(_ sender: Any?) {
  629. }
  630. func imageToPDF(_ sender: Any?) {
  631. KMApp.ShowImageToPDFWindow()
  632. }
  633. func ocr(_ sender: Any?) {
  634. }
  635. func extractImage(_ sender: Any?) {
  636. self.extractImageAction(num: 1)
  637. }
  638. func pageEdit(_ sender: Any?) {
  639. }
  640. func textEditAction(_ sender: Any?) {
  641. }
  642. func security(_ sender: Any?) {
  643. }
  644. func decryption(_ sender: Any?) {
  645. }
  646. func selectToolModel(_ sender: Any?) {
  647. }
  648. func takeSnapshot(_ sender: Any?) {
  649. self.listView.takeSnapshot(sender)
  650. }
  651. func takeSnapshotSelectContent(_ sender: Any?) {
  652. self.listView.takeSnapshotSelectContent(sender)
  653. }
  654. func changeAnnotationMode(_ sender: Any?) {
  655. if let data = sender as? NSMenuItem {
  656. var itemId: String?
  657. if data.tag == 8 {
  658. itemId = KMToolbarHighlightAnnotationItemIdentifier
  659. } else if data.tag == 9 {
  660. itemId = KMToolbarUnderlineAnnotationItemIdentifier
  661. } else if data.tag == 10 {
  662. itemId = KMToolbarStrikeOutAnnotationItemIdentifier
  663. }
  664. else if data.tag == 13 {
  665. itemId = KMToolbarInkAnnotationItemIdentifier
  666. } else if data.tag == 3 {
  667. itemId = KMToolbarFreeTextAnnotationItemIdentifier
  668. } else if data.tag == 1 {
  669. itemId = KMToolbarAnchoredAnnotationItemIdentifier
  670. } else if data.tag == 6 {
  671. itemId = KMToolbarSquareAnnotationItemIdentifier
  672. } else if data.tag == 7 {
  673. itemId = KMToolbarCircleAnnotationItemIdentifier
  674. }
  675. else if data.tag == 4 {
  676. itemId = KMToolbarLineAnnotationItemIdentifier
  677. } else if data.tag == 2 {
  678. itemId = KMToolbarLinkAnnotationItemIdentifier
  679. }
  680. if let data = itemId {
  681. }
  682. }
  683. }
  684. func addWatermark(_ sender: Any?) {
  685. }
  686. func removeWatermark(_ sender: Any?) {
  687. }
  688. func batchAddWatermark(_ sender: Any?) {
  689. }
  690. func batchRemoveWatermark(_ sender: Any?) {
  691. }
  692. func templateWatermark(_ sender: Any?) {
  693. }
  694. func addBackground(_ sender: Any?) {
  695. }
  696. func removeBackground(_ sender: Any?) {
  697. }
  698. func batchAddBackground(_ sender: Any?) {
  699. }
  700. func batchRemoveBackground(_ sender: Any?) {
  701. }
  702. func templateBackground(_ sender: Any?) {
  703. }
  704. func addHeaderFooter(_ sender: Any?) {
  705. }
  706. func removeHeaderFooter(_ sender: Any?) {
  707. }
  708. func batchAddHeaderFooter(_ sender: Any?) {
  709. }
  710. func batchRemoveHeaderFooter(_ sender: Any?) {
  711. }
  712. func templateHeaderFooter(_ sender: Any?) {
  713. }
  714. func addHeaderFooterBates(_ sender: Any?) {
  715. }
  716. func removeHeaderFooterBates(_ sender: Any?) {
  717. }
  718. func batchAddBates(_ sender: Any?) {
  719. }
  720. func batchRemoveBates(_ sender: Any?) {
  721. }
  722. func templateBates(_ sender: Any?) {
  723. }
  724. func togglePoster(_ sender: Any?) {
  725. self.saveDocument()
  726. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .poster)
  727. }
  728. func toggleMultiple(_ sender: Any?) {
  729. self.saveDocument()
  730. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .multipage)
  731. }
  732. func toggleBooklet(_ sender: Any?) {
  733. self.saveDocument()
  734. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
  735. }
  736. func rotateLeft(_ sender: Any?) {
  737. if IAPProductsManager.default().isAvailableAllFunction() == false {
  738. let winC = KMPurchaseCompareWindowController.sharedInstance()
  739. winC?.kEventName = "Reading_Rotate_BuyNow"
  740. winC?.kEventTag = 1
  741. winC?.showWindow(nil)
  742. return
  743. }
  744. Task { @MainActor in
  745. let page : CPDFPage = self.listView.currentPage() ?? CPDFPage()
  746. let rotation = page.rotation
  747. page.leftRotate()
  748. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  749. }
  750. }
  751. func rotateRight(_ sender: Any?) {
  752. if IAPProductsManager.default().isAvailableAllFunction() == false {
  753. let winC = KMPurchaseCompareWindowController.sharedInstance()
  754. winC?.kEventName = "Reading_Rotate_BuyNow"
  755. winC?.kEventTag = 1
  756. winC?.showWindow(nil)
  757. return
  758. }
  759. Task { @MainActor in
  760. let page : CPDFPage = self.listView.currentPage() ?? CPDFPage()
  761. let rotation = page.rotation
  762. page.rightRotate()
  763. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  764. }
  765. }
  766. func rotateAllLeft(_ sender: Any?) {
  767. if IAPProductsManager.default().isAvailableAllFunction() == false {
  768. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  769. return
  770. }
  771. Task { @MainActor in
  772. var pageItems: [KMThumbnailPageItem] = []
  773. for index in 0 ... self.listView.document.pageCount - 1 {
  774. let page: CPDFPage = self.listView.document.page(at: index)
  775. let rotation = page.rotation
  776. page.leftRotate()
  777. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  778. }
  779. self.rotatePageItems(pageItems: pageItems)
  780. }
  781. }
  782. func rotateAllRight(_ sender: Any?) {
  783. if IAPProductsManager.default().isAvailableAllFunction() == false {
  784. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  785. return
  786. }
  787. Task { @MainActor in
  788. var pageItems: [KMThumbnailPageItem] = []
  789. for index in 0 ... self.listView.document.pageCount - 1 {
  790. let page: CPDFPage = self.listView.document.page(at: index)
  791. let rotation = page.rotation
  792. page.rightRotate()
  793. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  794. }
  795. self.rotatePageItems(pageItems: pageItems)
  796. }
  797. }
  798. func autoSelectContent(_ sender: Any?) {
  799. self.listView.autoSelectContent(sender)
  800. }
  801. }
  802. // MARK: - window Menu
  803. extension KMMainViewController {
  804. @IBAction func menuItemAction_showForwardTagPage(_ sender: Any) {
  805. (self.myDocument as? KMMainDocument)?.browser.selectPreviousTab()
  806. }
  807. @IBAction func menuItemAction_showNextTagPage(_ sender: Any) {
  808. (self.myDocument as? KMMainDocument)?.browser.selectNextTab()
  809. }
  810. @IBAction func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  811. self.browserWindowController?.openNewWindow(sender)
  812. }
  813. @IBAction func menuItemAction_mergeAllWindow(_ sender: Any) {
  814. ((self.myDocument as? KMMainDocument)?.browser?.windowController as? KMBrowserWindowController)?.mergeAllWindow(sender)
  815. }
  816. @IBAction func menuItemAction_currentWindowName(_ sender: Any) {
  817. }
  818. @IBAction func performFit(_ sender: Any?) {
  819. if self.interactionMode != .normal {
  820. NSSound.beep()
  821. return
  822. }
  823. let displayMode = self.listView.fetchDisplayViewMode()
  824. let scaleFactor = self.listView.scaleFactor
  825. let autoScales = self.listView.autoScales
  826. var isSingleRow = false
  827. if displayMode == .singlePage || displayMode == .twoUp {
  828. isSingleRow = true
  829. } else if displayMode == .singlePageContinuous || self.listView.displaysAsBook {
  830. isSingleRow = (self.listView.document?.pageCount ?? 0) <= 1
  831. } else {
  832. isSingleRow = (self.listView.document?.pageCount ?? 0) <= 2
  833. }
  834. var frame = self.view.window?.frame ?? .zero
  835. var size: NSSize = .zero
  836. let oldSize = self.listView.frame.size
  837. let documentView = self.listView.documentView()
  838. let documentRect = documentView?.convert(documentView?.bounds ?? .zero, to: nil) ?? .zero
  839. let page = self.listView.currentPage()
  840. let box = self.listView.displayBox
  841. let PAGE_BREAK_MARGIN: CGFloat = 8.0
  842. let margin = self.listView.displaysPageBreaks ? PAGE_BREAK_MARGIN : 0.0
  843. // Calculate the new size for the pdfView
  844. size.width = NSWidth(documentRect)
  845. if (autoScales) {
  846. size.width /= scaleFactor
  847. }
  848. if (isSingleRow) {
  849. size.height = NSHeight(documentRect)
  850. } else {
  851. let rect = self.listView.convert(page?.bounds(for: box) ?? .zero, from: page)
  852. size.height = NSHeight(rect) + margin * scaleFactor
  853. if NSScroller.responds(to: NSSelectorFromString("preferredScrollerStyle")) == false {
  854. let w = NSScroller.scrollerWidth(for: .regular, scrollerStyle: .legacy)
  855. size.width += w
  856. } else {
  857. if NSScroller.preferredScrollerStyle == .legacy {
  858. let w = NSScroller.scrollerWidth(for: .regular, scrollerStyle: .legacy)
  859. size.width += w
  860. }
  861. }
  862. }
  863. if (autoScales) {
  864. size.height /= scaleFactor
  865. }
  866. // Calculate the new size for the window
  867. size.width = ceil(NSWidth(frame) + size.width - oldSize.width)
  868. size.height = ceil(NSHeight(frame) + size.height - oldSize.height)
  869. // Align the window frame from the old topleft point and constrain to the screen
  870. frame.origin.y = NSMaxY(frame) - size.height
  871. frame.size = size
  872. if let screen = self.view.window?.screen ?? NSScreen.main {
  873. frame = self.view.window?.constrainFrameRect(frame, to: screen) ?? .zero
  874. }
  875. self.view.window?.setFrame(frame, display: self.view.window?.isVisible ?? false)
  876. if displayMode == .singlePageContinuous || displayMode == .twoUpContinuous {
  877. self.listView.go(to: NSInsetRect(page?.bounds(for: box) ?? .zero, -0.5 * margin, -0.5 * margin), on: page)
  878. }
  879. }
  880. }
  881. extension KMMainViewController {
  882. override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  883. if (menuItem.action == #selector(menuItemAction_currentWindowName)) {
  884. menuItem.title = (self.myDocument?.fileURL?.lastPathComponent) ?? ""
  885. return true
  886. }
  887. let act = menuItem.action
  888. if act == #selector(menuItemAnnotationClick_addStype) ||
  889. act == #selector(menuItemAnnotationClick_add) ||
  890. act == #selector(menuItemAnnotationClick_toolModel) ||
  891. act == #selector(changeAnnotationMode_itemAction) {
  892. return !(self.listView.hideNotes ?? false)
  893. }
  894. if let data = act, KMSystemMenu.isAnnotationSelector(sel: data) {
  895. if KMSystemMenu.Annotation.hiddenAllSelector == act {
  896. } else {
  897. return !(self.listView.hideNotes ?? false)
  898. }
  899. }
  900. if (menuItem.action == #selector(menuItemAction_thumai) ||
  901. menuItem.action == #selector(menuItemAction_outline) ||
  902. menuItem.action == #selector(menuItemAction_bookmark) ||
  903. menuItem.action == #selector(menuItemAction_annotation) ||
  904. menuItem.action == #selector(menuItemAction_search)) {
  905. menuItem.state = .off
  906. let type = self.leftSideViewController.type.methodType
  907. if (menuItem.action == #selector(menuItemAction_thumai) && type == .Thumbnail) {
  908. menuItem.state = .on
  909. }
  910. if (menuItem.action == #selector(menuItemAction_outline) && type == .Outline) {
  911. menuItem.state = .on
  912. }
  913. if (menuItem.action == #selector(menuItemAction_bookmark) && type == .Annotation) {
  914. menuItem.state = .on
  915. }
  916. if (menuItem.action == #selector(menuItemAction_annotation) && type == .Annotation) {
  917. menuItem.state = .on
  918. }
  919. if (menuItem.action == #selector(menuItemAction_search) && type == .Search) {
  920. menuItem.state = .on
  921. }
  922. }
  923. if (menuItem.action == #selector(menuItemAction_hiddenLeftSide)) {
  924. if (self.model.leftPanelOpen) {
  925. menuItem.title = NSLocalizedString("Hide Left Side Panel", comment: "")
  926. } else {
  927. menuItem.title = NSLocalizedString("Show Left Side Panel", comment: "")
  928. }
  929. }
  930. if (menuItem.action == #selector(menuItemAction_hiddenRightSide)) {
  931. if (self.model.rightPanelIsOpen) {
  932. menuItem.title = NSLocalizedString("Hide Right Side Panel", comment: "")
  933. } else {
  934. menuItem.title = NSLocalizedString("Show Right Side Panel", comment: "")
  935. }
  936. }
  937. if (menuItem.action == #selector(menuItemAction_hiddenAllAnnotation)) {
  938. if (self.listView.hideNotes) {
  939. menuItem.title = NSLocalizedString("Show Notes", comment: "")
  940. } else {
  941. menuItem.title = NSLocalizedString("Hide Notes", comment: "")
  942. }
  943. }
  944. if (menuItem.action == #selector(menuItemAction_showForwardTagPage) ||
  945. menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  946. let browser = self.browserWindowController?.browser
  947. if (browser != nil && (browser is KMBrowser)) {
  948. if (menuItem.action == #selector(menuItemAction_showForwardTagPage)) {
  949. return (browser as! KMBrowser).canSelectPreviousTab()
  950. }
  951. if (menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  952. return (browser as! KMBrowser).canSelectNextTab()
  953. }
  954. }
  955. }
  956. if (menuItem.action == #selector(menuItemAction_mergeAllWindow)) {
  957. if let _browserWindowC = ((self.myDocument as? KMMainDocument)?.browser.windowController as? KMBrowserWindowController) {
  958. return _browserWindowC.canMergeAllWindow()
  959. }
  960. }
  961. if (menuItem.action == #selector(menuItemAction_nextPage) ||
  962. menuItem.action == #selector(menuItemAction_forwardPage) ||
  963. menuItem.action == #selector(menuItemAction_firstPage) ||
  964. menuItem.action == #selector(menuItemAction_lastPage) ||
  965. menuItem.action == #selector(menuItemAction_forward) ||
  966. menuItem.action == #selector(menuItemAction_goback)) {
  967. if self.hasEnterPageEdit() {
  968. return false
  969. }
  970. if (menuItem.action == #selector(menuItemAction_nextPage)) {
  971. if self.listView.isEditing() && self.listView.isEditable() {
  972. return false
  973. }
  974. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage())
  975. }
  976. if (menuItem.action == #selector(menuItemAction_forwardPage)) {
  977. if self.listView.isEditing() && self.listView.isEditable() {
  978. return false
  979. }
  980. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage())
  981. }
  982. if (menuItem.action == #selector(menuItemAction_firstPage)) {
  983. return self.listView.canGoToFirstPage()
  984. }
  985. if (menuItem.action == #selector(menuItemAction_lastPage)) {
  986. return self.listView.canGoToLastPage()
  987. }
  988. if (menuItem.action == #selector(menuItemAction_forward)) {
  989. return self.listView.km_canGoForward()
  990. }
  991. if (menuItem.action == #selector(menuItemAction_goback)) {
  992. return self.listView.km_canGoBack()
  993. }
  994. }
  995. if menuItem.action == #selector(changeAnnotationMode) {
  996. let type = self.listView.annotationType
  997. if type == .highlight {
  998. menuItem.state = menuItem.tag == 8 ? .on : .off
  999. } else if type == .underline {
  1000. menuItem.state = menuItem.tag == 9 ? .on : .off
  1001. } else if type == .strikeOut {
  1002. menuItem.state = menuItem.tag == 10 ? .on : .off
  1003. } else if type == .ink {
  1004. menuItem.state = menuItem.tag == 13 ? .on : .off
  1005. } else if type == .freeText {
  1006. menuItem.state = menuItem.tag == 3 ? .on : .off
  1007. } else if type == .anchored {
  1008. menuItem.state = menuItem.tag == 1 ? .on : .off
  1009. } else if type == .square {
  1010. menuItem.state = menuItem.tag == 6 ? .on : .off
  1011. } else if type == .circle {
  1012. menuItem.state = menuItem.tag == 7 ? .on : .off
  1013. } else if type == .line {
  1014. menuItem.state = menuItem.tag == 4 ? .on : .off
  1015. } else if type == .link {
  1016. menuItem.state = menuItem.tag == 2 ? .on : .off
  1017. } else {
  1018. menuItem.state = .off
  1019. }
  1020. return !self.listView.hideNotes
  1021. }
  1022. if menuItem.action == #selector(changeAnnotationMode_itemAction) {
  1023. if self.listView.annotationType.rawValue == menuItem.tag {
  1024. menuItem.state = .on
  1025. } else {
  1026. menuItem.state = .off
  1027. }
  1028. return true
  1029. }
  1030. if menuItem.action == #selector(menuItemAction_print) {
  1031. if self.listView.document.permissionsStatus == .user && !self.listView.document.allowsPrinting {
  1032. return false
  1033. }
  1034. }
  1035. if menuItem.action == #selector(highlightFormFiled) {
  1036. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  1037. menuItem.state = enabled ? .on : .off
  1038. return true
  1039. }
  1040. if menuItem.action == #selector(highlightLinks) {
  1041. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  1042. menuItem.state = enabled ? .on : .off
  1043. return true
  1044. }
  1045. guard let action = menuItem.action else {
  1046. return false
  1047. }
  1048. if KMSystemMenu.isToolSelector(sel: action) {
  1049. if action == KMSystemMenu.Tool.autoSelectContentSelector {
  1050. let toolMode = self.listView.toolMode ?? .none
  1051. return toolMode == .selectToolMode
  1052. } else if action == KMSystemMenu.Tool.textToolSelector {
  1053. let toolMode = self.listView.toolMode ?? .textToolMode
  1054. menuItem.state = toolMode == .textToolMode ? .on : .off
  1055. return true
  1056. } else if action == KMSystemMenu.Tool.scrolToolSelector {
  1057. let toolMode = self.listView.toolMode ?? .textToolMode
  1058. menuItem.state = toolMode == .moveToolMode ? .on : .off
  1059. return true
  1060. } else if action == KMSystemMenu.Tool.zoomToolSelector {
  1061. let toolMode = self.listView.toolMode ?? .textToolMode
  1062. menuItem.state = toolMode == .magnifyToolMode ? .on : .off
  1063. return true
  1064. } else if action == KMSystemMenu.Tool.selectToolSelector {
  1065. let toolMode = self.listView.toolMode ?? .textToolMode
  1066. menuItem.state = toolMode == .selectToolMode ? .on : .off
  1067. return true
  1068. } else if action == KMSystemMenu.Tool.changeToolModeSelector {
  1069. let aType = self.listView.annotationType ?? .unkown
  1070. if aType.isMarkup() || aType == .freeText || aType == .anchored || aType.isSquare() || aType == .link {
  1071. menuItem.state = .on
  1072. } else {
  1073. menuItem.state = .off
  1074. }
  1075. return true
  1076. }
  1077. }
  1078. if KMSystemMenu.isViewSelector(sel: action) {
  1079. if action == KMSystemMenu.View.toggleLeftSidePaneSelector {
  1080. if self.model.leftPanelOpen {
  1081. menuItem.title = NSLocalizedString("Hide Panel", comment: "Menu item title")
  1082. } else {
  1083. menuItem.title = NSLocalizedString("Show Panel", comment: "Menu item title")
  1084. }
  1085. return true
  1086. } else if action == KMSystemMenu.View.changeLeftSidePaneStateSelector {
  1087. if menuItem.tag == 3 { // 快照
  1088. menuItem.state = self.leftSideViewController.type.methodType == .snapshot ? .on : .off
  1089. }
  1090. return true
  1091. } else if action == KMSystemMenu.View.themesColorSelector {
  1092. let mode = menuItem.tag
  1093. var viewMode: KMPDFViewMode = .normal
  1094. if let storedViewMode = UserDefaults.standard.value(forKey: "kPDFViewModeKey") as? Int {
  1095. viewMode = KMPDFViewMode(rawValue: UInt(storedViewMode)) ?? .normal
  1096. }
  1097. if mode == 0 {
  1098. menuItem.state = viewMode == .normal ? .on : .off
  1099. } else if mode == 1 {
  1100. menuItem.state = viewMode == .soft ? .on : .off
  1101. } else if mode == 2 {
  1102. menuItem.state = viewMode == .night ? .on : .off
  1103. } else if mode == 3 {
  1104. menuItem.state = viewMode == .green ? .on : .off
  1105. } else if mode == 4 {
  1106. menuItem.state = viewMode == .themes1 ? .on : .off
  1107. } else if mode == 5 {
  1108. menuItem.state = viewMode == .themes2 ? .on : .off
  1109. } else if mode == 6 {
  1110. menuItem.state = viewMode == .themes3 ? .on : .off
  1111. } else if mode == 7 {
  1112. menuItem.state = viewMode == .themes4 ? .on : .off
  1113. }
  1114. var data: Data? = nil
  1115. if let themesData = UserDefaults.standard.object(forKey: "kKMPDFViewModeThemesArray") as? Data {
  1116. data = themesData
  1117. }
  1118. var themesCount = 0
  1119. let appArray = NSKeyedUnarchiver.unarchiveObject(with: data ?? Data()) as? [Any] ?? []
  1120. if appArray.count > 0 {
  1121. themesCount = menuItem.tag - appArray.count
  1122. } else {
  1123. themesCount = menuItem.tag - 4
  1124. }
  1125. if themesCount >= 0 {
  1126. menuItem.isHidden = true
  1127. return false
  1128. }else {
  1129. menuItem.isHidden = false
  1130. return true
  1131. }
  1132. } else if action == KMSystemMenu.View.splitViewActionSelector {
  1133. let mode = menuItem.tag
  1134. if mode == 0 { // hor
  1135. menuItem.state = self.listView.viewSplitMode == .horizontal ? .on : .off
  1136. } else if mode == 1 { // verti
  1137. menuItem.state = self.listView.viewSplitMode == .vertical ? .on : .off
  1138. } else if mode == 2 { // single
  1139. menuItem.state = self.listView.viewSplitMode == .disable ? .on : .off
  1140. }
  1141. } else if action == KMSystemMenu.View.toggleToolbarShowSelector {
  1142. }
  1143. }
  1144. if KMSystemMenu.isAnnotationSelector(sel: action) {
  1145. if action == KMSystemMenu.Annotation.editNoteSelector {
  1146. if self.interactionMode == .presentation {
  1147. return false
  1148. }
  1149. guard let anno = self.listView.activeAnnotation else {
  1150. return false
  1151. }
  1152. return anno.isEditable()
  1153. }
  1154. }
  1155. if action == NSSelectorFromString("changeDisplayBox:") {
  1156. if menuItem.tag == 0 {
  1157. menuItem.state = self.listView.displayBox == .mediaBox ? .on : .off
  1158. } else if menuItem.tag == 1 {
  1159. menuItem.state = self.listView.displayBox == .cropBox ? .on : .off
  1160. }
  1161. }
  1162. if KMSystemMenu.isFileSelector(sel: action) {
  1163. if action == KMSystemMenu.File.exportNoteSelector {
  1164. let cnt = self.leftSideViewController.annoListModel?.datas.count ?? 0
  1165. return cnt > 0
  1166. }
  1167. }
  1168. return true
  1169. }
  1170. }