KMMainViewController+MenuAction.swift 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  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. self.toolbarController.delegate?.clickChildTool?(type: .merge, index: 0)
  12. }
  13. @IBAction func menuItemClick_Compress(_ sender: Any) {
  14. self.toolbarController.delegate?.clickChildTool?(type: .compress, index: 0)
  15. }
  16. @IBAction func menuItemClick_Convert(_ sender: Any) {
  17. }
  18. @IBAction func menuItemAction_ConvertToWord(_ sender: Any) {
  19. self.toolbarController.delegate?.clickChildTool?(type: .word, index: 0)
  20. }
  21. @IBAction func menuItemAction_ConvertToExcel(_ sender: Any) {
  22. self.toolbarController.delegate?.clickChildTool?(type: .excel, index: 0)
  23. }
  24. @IBAction func menuItemAction_ConvertToPPT(_ sender: Any) {
  25. self.toolbarController.delegate?.clickChildTool?(type: .ppt, index: 0)
  26. }
  27. @IBAction func menuItemAction_ConvertToRTF(_ sender: Any) {
  28. self.toolbarController.delegate?.clickChildTool?(type: .rtf, index: 0)
  29. }
  30. @IBAction func menuItemAction_ConvertToHTML(_ sender: Any) {
  31. self.toolbarController.delegate?.clickChildTool?(type: .html, index: 0)
  32. }
  33. @IBAction func menuItemAction_ConvertToText(_ sender: Any) {
  34. self.toolbarController.delegate?.clickChildTool?(type: .conversion_text, index: 0)
  35. }
  36. @IBAction func menuItemAction_ConvertToCSV(_ sender: Any) {
  37. self.toolbarController.delegate?.clickChildTool?(type: .csv, index: 0)
  38. }
  39. @IBAction func menuItemAction_ConvertToImage(_ sender: Any) {
  40. self.toolbarController.delegate?.clickChildTool?(type: .conversion_image, index: 0)
  41. }
  42. @IBAction func menuItemClick_SettingPassword(_ sender: Any) {
  43. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 1)
  44. }
  45. @IBAction func menuItemClick_RemovePassword(_ sender: Any) {
  46. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 2)
  47. }
  48. @IBAction func menuItemAction_closeWindow(_ sender: Any) {
  49. self.view.window?.close()
  50. }
  51. @IBAction func menuItemAction_closeAllWindows(_ sender: Any) {
  52. for window in NSApp.windows {
  53. window.close()
  54. }
  55. }
  56. @IBAction func menuItemAction_closeTagPage(_ sender: Any) {
  57. self.browserWindowController?.browser?.closeTab()
  58. }
  59. @IBAction func menuItemAction_showInFinder(_ sender: Any) {
  60. NSWorkspace.shared.activateFileViewerSelecting([self.listView.document.documentURL])
  61. }
  62. @IBAction func menuItemAction_property(_ sender: Any) {
  63. KMInfoWindowController.shared.showWindow(sender)
  64. }
  65. @IBAction func menuItemAction_print(_ sender: Any) {
  66. self.showPrintWindow()
  67. }
  68. }
  69. extension KMMainViewController: KMSystemFileMenuProtocol {
  70. @IBAction func menuItemClick_ExportNote(_ sender: Any) {
  71. self.leftSideViewController.exportNotes(sender as? NSMenuItem)
  72. }
  73. @IBAction func menuItemClick_saveAsFlattenedPDF(_ sender: Any) {
  74. Task { @MainActor in
  75. if IAPProductsManager.default().isAvailableAllFunction() == false {
  76. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  77. return
  78. }
  79. AccountTools.canUseAdvance_success { can in
  80. if can {
  81. self.saveAsFlattenedPDFAction()
  82. }
  83. }
  84. }
  85. }
  86. func saveAsFlattenedPDFAction(limit: Bool = false) {
  87. DispatchQueue.main.async {
  88. NSPanel.savePanel(self.view.window!, true) { panel in
  89. panel.nameFieldStringValue = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_flatten" + ".pdf"
  90. } completion: { response, url, isOpen in
  91. if (response == .cancel) {
  92. return
  93. }
  94. var result = false
  95. if (limit) {
  96. let data = KMTools.saveWatermarkDocumentForFlatten(document: self.listView.document, to: url!)
  97. result = data != nil
  98. } else {
  99. result = self.listView.document.writeFlatten(to: url!)
  100. }
  101. if (!result) {
  102. return
  103. }
  104. if (isOpen) {
  105. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  106. }
  107. } else {
  108. NSWorkspace.shared.activateFileViewerSelecting([url!])
  109. }
  110. }
  111. }
  112. }
  113. }
  114. // MARK: Edit Menu
  115. @objc protocol KMEditMenuProtocol: NSObjectProtocol {
  116. @objc optional func undo(_ sender: Any)
  117. @objc optional func redo(_ sender: Any)
  118. @objc optional func cut(_ sender: Any)
  119. @objc optional func copy(_ sender: Any)
  120. @objc optional func paste(_ sender: Any)
  121. @objc optional func delete(_ sender: Any)
  122. }
  123. extension KMMainViewController: KMEditMenuProtocol {
  124. @IBAction func menuItemAction_find(_ sender: Any) {
  125. if (self.leftSideViewController.isShowPanel) {
  126. self.toolbarController.cancelSelected(KMLeftControlToolbarItemIdentifier)
  127. self.toolbarController.toolbarType = .None
  128. self.leftSideViewController.showPanelView(show: false)
  129. }
  130. // else {
  131. let isSearch = self.leftSideViewController.type.methodType == .Search
  132. self.leftSideViewController.refreshMethodType(methodType: .Search)
  133. if (isSearch) { // 关闭
  134. self.view.window?.makeFirstResponder(self.listView)
  135. }
  136. // }
  137. }
  138. }
  139. // MARK: - View Menu
  140. extension KMMainViewController {
  141. @IBAction func menuItemAction_adjustWidth(_ sender: Any) {
  142. self.selectZoom(.width)
  143. }
  144. @IBAction func menuItemAction_adjustPage(_ sender: Any) {
  145. self.selectZoom(.fit)
  146. }
  147. @IBAction func menuItemAction_size(_ sender: Any) {
  148. self.selectZoom(.actualSize)
  149. }
  150. @IBAction func menuItemAction_zoomOut(_ sender: Any) {
  151. if (self.listView.canZoomOut) {
  152. self.listView.zoomOut(nil)
  153. }
  154. }
  155. @IBAction func menuItemAction_zoomIn(_ sender: Any) {
  156. if (self.listView.canZoomIn) {
  157. self.listView.zoomIn(nil)
  158. }
  159. }
  160. // page show
  161. @IBAction func menuItemAction_singlePage(_ sender: Any) {
  162. self.selectDisplay(display: .singlePage)
  163. }
  164. @IBAction func menuItemAction_singlePageContinue(_ sender: Any) {
  165. self.selectDisplay(display: .singlePageContinuous)
  166. }
  167. @IBAction func menuItemAction_doublePage(_ sender: Any) {
  168. self.selectDisplay(display: .twoUp)
  169. }
  170. @IBAction func menuItemAction_doublePageContinue(_ sender: Any) {
  171. self.selectDisplay(display: .twoUpContinuous)
  172. }
  173. @IBAction func menuItemAction_bookMode(_ sender: Any) {
  174. self.selectDisplay(display: .bookMode)
  175. }
  176. @IBAction func menuItemAction_readMode(_ sender: Any) {
  177. if (self.isReadMode) {
  178. self.closeReadModel()
  179. } else {
  180. self.openReadModel()
  181. }
  182. }
  183. @IBAction func menuItemAction_showSplitPage(_ sender: Any) {
  184. self.listView?.displaysPageBreaks = !(self.listView?.displaysPageBreaks ?? false)
  185. self.listView.layoutDocumentView()
  186. }
  187. @IBAction func menuItemAction_autoScrol(_ sender: Any) {
  188. }
  189. @IBAction func menuItemAction_autoScrolSetting(_ sender: Any) {
  190. }
  191. // rotate
  192. @IBAction func menuItemAction_rotateLeft(_ sender: Any) {
  193. self.rotateLeft(sender)
  194. }
  195. func rotatePageItems(pageItems: [KMThumbnailPageItem]) {
  196. let tempPageItems = pageItems
  197. for pageItem in tempPageItems {
  198. pageItem.page.rotation = pageItem.rotate
  199. }
  200. self.listView.layoutDocumentView()
  201. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMPDFViewRotatePage"), object: self.listView.document)
  202. self.listView.undoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  203. var tempPageItems: [KMThumbnailPageItem] = []
  204. for pageItem in pageItems {
  205. let item = KMThumbnailPageItem(page: pageItem.page, rotate: pageItem.oldRotate, oldRotate: pageItem.rotate)
  206. tempPageItems.append(item)
  207. }
  208. self?.rotatePageItems(pageItems: tempPageItems)
  209. }
  210. }
  211. @IBAction func menuItemAction_rotateRight(_ sender: Any) {
  212. self.rotateRight(sender)
  213. }
  214. @IBAction func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  215. self.rotateAllLeft(sender)
  216. }
  217. @IBAction func menuItemAction_rotateAllPageRight(_ sender: Any) {
  218. self.rotateAllRight(sender)
  219. }
  220. // split screen
  221. @IBAction func menuItemAction_splitScreenVerti(_ sender: Any) {
  222. }
  223. @IBAction func menuItemAction_splitScreenHorti(_ sender: Any) {
  224. }
  225. @IBAction func menuItemAction_splitScreenNo(_ sender: Any) {
  226. }
  227. @IBAction func menuItemAction_view_readMode(_ sender: Any) {
  228. if (self.isReadMode) {
  229. self.closeReadModel()
  230. } else {
  231. self.openReadModel()
  232. }
  233. }
  234. @IBAction func menuItemAction_enterFullScreen(_ sender: Any) {
  235. }
  236. @IBAction func menuItemAction_hiddenLeftSide(_ sender: Any) {
  237. if (self.leftSideViewController.type.methodType != .None) {
  238. self.leftSideViewController.selectType(self.leftSideViewController.type.methodType)
  239. } else {
  240. self.toolbarController.selectItem(KMLeftControlToolbarItemIdentifier)
  241. }
  242. }
  243. @IBAction func menuItemAction_hiddenRightSide(_ sender: Any) {
  244. self.toolbarController.selectItem(KMRightControlToolbarItemIdentifier)
  245. }
  246. @IBAction func menuItemAction_thumai(_ sender: Any) {
  247. self.leftSideViewController.switchType(.Thumbnail)
  248. }
  249. @IBAction func menuItemAction_outline(_ sender: Any) {
  250. self.leftSideViewController.switchType(.Outline)
  251. }
  252. @IBAction func menuItemAction_bookmark(_ sender: Any) {
  253. self.leftSideViewController.switchType(.Annotation)
  254. }
  255. @IBAction func menuItemAction_annotation(_ sender: Any) {
  256. self.leftSideViewController.switchType(.Annotation)
  257. }
  258. @IBAction func menuItemAction_search(_ sender: Any) {
  259. self.leftSideViewController.switchType(.Search)
  260. }
  261. @IBAction func menuItemAction_topic(_ sender: Any) {
  262. }
  263. @IBAction func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  264. }
  265. @IBAction func togglePresentation(_ sender: Any?) {
  266. if self.canExitPresentation() {
  267. self.exitFullScreen("")
  268. } else if self.canEnterPresentation(){
  269. self.enterPresentation()
  270. }
  271. }
  272. @IBAction func chooseTransition(_ sender: Any?) {
  273. let presentationSheetController = SKPresentationOptionsSheetController()
  274. presentationSheetController.controller = self
  275. // let window = NSWindow.currentWindow()
  276. self.km_beginSheet(windowC: presentationSheetController)
  277. presentationSheetController.closeCallBack = {[weak self] vc in
  278. self?.km_quick_endSheet()
  279. }
  280. }
  281. @IBAction func toggleAutoFlow(_ sender: Any?) {
  282. if (self.listView.isAutoFlow()) {
  283. self.listView.stopAutoFlow()
  284. } else {
  285. self.listView.startAutoFlow()
  286. }
  287. }
  288. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  289. autoFlowOptionsSheetController = KMAutoFlowOptionsSheetController(windowNibName: "KMAutoFlowOptionsSheetController")
  290. NSWindow.currentWindow().beginSheet((autoFlowOptionsSheetController?.window)!) { responce in }
  291. }
  292. @IBAction func toggleReadingBar(_ sender: Any?) {
  293. KMPrint("toggleReadingBar")
  294. }
  295. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  296. if self.model.leftPanelOpen {
  297. self.closeLeftPane()
  298. } else {
  299. self.openLeftPane()
  300. }
  301. }
  302. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  303. guard let item = sender as? NSMenuItem else {
  304. NSSound.beep()
  305. return
  306. }
  307. if item.tag == 3 {
  308. self.leftSideViewController.switchType(.snapshot)
  309. }
  310. }
  311. @IBAction func splitViewAction(_ sender: Any?) {
  312. guard let item = sender as? NSMenuItem else {
  313. return
  314. }
  315. if item.tag < 2 && IAPProductsManager.default().isAvailableAllFunction() == false {
  316. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  317. return
  318. }
  319. AccountTools.canUseAdvance_success { can in
  320. if can {
  321. self.splitView(withTag: item.tag)
  322. }
  323. }
  324. }
  325. @IBAction func themesColor(_ sender: Any?) {
  326. guard let item = sender as? NSMenuItem else {
  327. NSSound.beep()
  328. return
  329. }
  330. let mode = item.tag
  331. if mode > 0 {
  332. if !IAPProductsManager.default().isAvailableAllFunction() {
  333. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  334. return
  335. }
  336. }
  337. AccountTools.canUseAdvance_success { can in
  338. if can {
  339. self.oem_themesColor(sender)
  340. }
  341. }
  342. }
  343. func oem_themesColor(_ sender: Any?) {
  344. guard let item = sender as? NSMenuItem else {
  345. NSSound.beep()
  346. return
  347. }
  348. let mode = item.tag
  349. if mode == 0 {
  350. self.listView.displayMode = .normal
  351. } else if mode == 1 {
  352. self.listView.displayMode = .soft
  353. } else if mode == 2 {
  354. self.listView.displayMode = .night
  355. } else if mode == 3 {
  356. self.listView.displayMode = .green
  357. } else {
  358. self.listView.displayMode = .custom
  359. var data: Data? = nil
  360. if let themesData = UserDefaults.standard.object(forKey: "kKMPDFViewModeThemesArray") as? Data {
  361. data = themesData
  362. }
  363. let appArray = NSKeyedUnarchiver.unarchiveObject(with: data ?? Data()) as? [Any] ?? []
  364. let mutableArray = appArray
  365. if mutableArray.count > 0 {
  366. let themesArray = mutableArray as! [NSColor]
  367. let color = themesArray[mode]
  368. self.listView.displayModeCustomColor = color
  369. }
  370. }
  371. self.listView.layoutDocumentView()
  372. self.listView.setNeedsDisplayForVisiblePages()
  373. UserDefaults.standard.set(mode, forKey: "kPDFViewModeKey")
  374. UserDefaults.standard.synchronize()
  375. }
  376. @IBAction func toggleStatusBar(_ sender: Any?) {
  377. KMPrint("toggleStatusBar ...")
  378. }
  379. @IBAction func toggleToolbarShow(_ sender: Any?) {
  380. self.toolbarController.mainToolBarisVisable = !self.toolbarController.mainToolBarisVisable
  381. }
  382. }
  383. // MARK: - Annotation Menu
  384. extension KMMainViewController {
  385. @IBAction func menuItemAction_highlight(_ sender: Any) {
  386. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarHighlightAnnotationItemIdentifier)
  387. }
  388. @IBAction func menuItemAction_underline(_ sender: Any) {
  389. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarUnderlineAnnotationItemIdentifier)
  390. }
  391. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  392. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarStrikeOutAnnotationItemIdentifier)
  393. }
  394. @IBAction func menuItemAction_wavyLine(_ sender: Any) {
  395. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarSquigglyAnnotationItemIdentifier)
  396. }
  397. @IBAction func menuItemAction_freehand(_ sender: Any) {
  398. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarInkAnnotationItemIdentifier)
  399. }
  400. @IBAction func menuItemAction_text(_ sender: Any) {
  401. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarFreeTextAnnotationItemIdentifier)
  402. }
  403. @IBAction func menuItemAction_note(_ sender: Any) {
  404. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarAnchoredAnnotationItemIdentifier)
  405. }
  406. @IBAction func menuItemAction_squre(_ sender: Any) {
  407. UserDefaults.standard.set(6, forKey: KMToolBarToolPDFShapeStyle)
  408. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  409. }
  410. @IBAction func menuItemAction_circle(_ sender: Any) {
  411. UserDefaults.standard.set(7, forKey: KMToolBarToolPDFShapeStyle)
  412. self.toolbarController.clickItem(KMToolbarCircleAnnotationItemIdentifier)
  413. }
  414. @IBAction func menuItemAction_arrow(_ sender: Any) {
  415. UserDefaults.standard.set(5, forKey: KMToolBarToolPDFShapeStyle)
  416. self.toolbarController.clickItem(KMToolbarArrowAnnotationItemIdentifier)
  417. }
  418. @IBAction func menuItemAction_line(_ sender: Any) {
  419. UserDefaults.standard.set(4, forKey: KMToolBarToolPDFShapeStyle)
  420. self.toolbarController.clickItem(KMToolbarLineAnnotationItemIdentifier)
  421. }
  422. // link
  423. @IBAction func menuItemAction_link(_ sender: Any) {
  424. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  425. }
  426. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  427. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  428. }
  429. @IBAction func menuItemAction_linkHttps(_ sender: Any) {
  430. }
  431. @IBAction func menuItemAction_linkEmail(_ sender: Any) {
  432. }
  433. @IBAction func notesLink(_ sender: Any?) {
  434. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  435. }
  436. // stamp
  437. @IBAction func menuItemAction_stamp(_ sender: Any) {
  438. self.mainMenuUpdateAnnotationStyle(identifier: KMAnnotationStampToolbarItemIdentifier)
  439. }
  440. @IBAction func menuItemAction_stampStandard(_ sender: Any) {
  441. }
  442. @IBAction func menuItemAction_stampDynamic(_ sender: Any) {
  443. }
  444. @IBAction func menuItemAction_stampCustom(_ sender: Any) {
  445. }
  446. @IBAction func notesStamp(_ sender: Any?) {
  447. self.mainMenuUpdateAnnotationStyle(identifier: KMAnnotationStampToolbarItemIdentifier)
  448. }
  449. @IBAction func menuItemAction_signure(_ sender: Any) {
  450. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarSignSignatureAnnotationItemIdentifier)
  451. }
  452. @IBAction func image(_ sender: Any?) {
  453. self.toolbarController.clickItem(KMAnnotationImageToolbarItemIdentifier)
  454. }
  455. @IBAction func table(_ sender: Any?) {
  456. self.toolbarController.clickItem(KMAnnotationTableToolbarItemIdentifier)
  457. }
  458. @IBAction func addForm(_ sender: Any?) {
  459. guard let item = sender as? NSMenuItem else {
  460. NSSound.beep()
  461. return
  462. }
  463. var identifiers: String?
  464. let idx = item.tag
  465. if idx == 0 { // raido
  466. identifiers = KMToolbarToolRadioButtonItemIdentifier
  467. } else if idx == 1 { // checkbox
  468. identifiers = KMToolbarToolCheckBoxItemIdentifier
  469. } else if idx == 2 { // textfiled
  470. identifiers = KMToolbarToolTextFieldItemIdentifier
  471. } else if idx == 3 { // combobox
  472. identifiers = KMToolbarToolPullDownmenuItemIdentifier
  473. } else if idx == 4 { // listbox
  474. identifiers = KMToolbarToolListBoxItemIdentifier
  475. } else if idx == 5 { // button
  476. identifiers = KMToolbarToolButtonIdentifier
  477. } else if idx == 6 { // sign
  478. identifiers = KMToolbarToolSignBoxIdentifier
  479. }
  480. if let itemID = identifiers {
  481. self.toolbarController.clickItem(itemID)
  482. }
  483. }
  484. @IBAction func editNote(_ sender: Any?) {
  485. if self.listView.hideNotes {
  486. NSSound.beep()
  487. return
  488. }
  489. if let anno = self.listView.activeAnnotation {
  490. self.listView.edit(anno)
  491. }
  492. }
  493. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  494. self.showOrHideNotes()
  495. }
  496. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  497. self.removeAllAnnotations()
  498. }
  499. private func mainMenuUpdateAnnotationStyle(identifier : String) {
  500. if self.toolbarController.toolbarType != .Annatiton {
  501. self.toolbarController.clickItem(KMDocumentAnnotationToolbarItemIdentifier)
  502. }
  503. self.toolbarController.clickItem(identifier)
  504. }
  505. }
  506. // MARK: - KMSystemPDFMenuProtocol
  507. extension KMMainViewController: KMSystemPDFMenuProtocol {
  508. func changeDisplayMode(_ sender: Any?) {
  509. guard let item = sender as? NSMenuItem else {
  510. NSSound.beep()
  511. return
  512. }
  513. let mode = item.tag
  514. if mode == 0 {
  515. self.selectDisplay(display: .singlePage)
  516. } else if mode == 1 {
  517. self.selectDisplay(display: .singlePageContinuous)
  518. } else if mode == 2 {
  519. self.selectDisplay(display: .twoUp)
  520. } else if mode == 3 {
  521. self.selectDisplay(display: .twoUpContinuous)
  522. }
  523. }
  524. func toggleDisplayAsBook(_ sender: Any?) {
  525. self.selectDisplay(display: .bookMode)
  526. }
  527. func toggleDisplayPageBreaks(_ sender: Any?) {
  528. self.listView?.displaysPageBreaks = !(self.listView?.displaysPageBreaks ?? false)
  529. self.listView.layoutDocumentView()
  530. }
  531. func changeDisplayBox(_ sender: Any?) {
  532. guard let item = sender as? NSMenuItem else {
  533. NSSound.beep()
  534. return
  535. }
  536. let mode = item.tag
  537. if mode == 0 {
  538. self.listView.displayBox = .mediaBox
  539. } else if mode == 1 {
  540. self.listView.displayBox = .cropBox
  541. }
  542. }
  543. func toggleAutoScale(_ sender: Any?) {
  544. // if ([self interactionMode] == SKPresentationMode)
  545. // [pdfView toggleAutoActualSize:sender];
  546. // else
  547. // [pdfView setAutoScales:[pdfView autoScales] == NO];
  548. self.listView.autoScales = !self.listView.autoScales
  549. }
  550. func doZoomToActualSize(_ sender: Any?) {
  551. self.selectZoom(.actualSize)
  552. }
  553. func doZoomToFit(_ sender: Any?) {
  554. self.selectZoom(.fit)
  555. }
  556. func doZoomIn(_ sender: Any?) {
  557. if (self.listView.canZoomIn) {
  558. self.listView.zoomIn(nil)
  559. }
  560. if self.digitalSignController?.pdfView != nil {
  561. if (self.digitalSignController?.pdfView.canZoomIn) == true {
  562. self.digitalSignController?.pdfView.zoomIn(nil)
  563. }
  564. }
  565. if self.redactController?.redactPdfView != nil {
  566. if (self.redactController?.redactPdfView.canZoomIn) == true {
  567. self.redactController?.redactPdfView.zoomIn(nil)
  568. }
  569. }
  570. }
  571. func doZoomOut(_ sender: Any?) {
  572. if (self.listView.canZoomOut) {
  573. self.listView.zoomOut(nil)
  574. }
  575. if self.digitalSignController?.pdfView != nil {
  576. if (self.digitalSignController?.pdfView.canZoomOut) == true {
  577. self.digitalSignController?.pdfView.zoomOut(nil)
  578. }
  579. }
  580. if self.redactController?.redactPdfView != nil {
  581. if (self.redactController?.redactPdfView.canZoomOut) == true {
  582. self.redactController?.redactPdfView.zoomOut(nil)
  583. }
  584. }
  585. }
  586. func doMarqueeZoomTool(_ sender: Any?) {
  587. let isSelected = self.toolbarController.findItem(KMToolbarZoomToSelectionItemIdentifier)?.isSelected ?? false
  588. if isSelected == false {
  589. self.toolbarController.clickItem(KMToolbarZoomToSelectionItemIdentifier)
  590. }
  591. }
  592. func highlightFormFiled(_ sender: Any?) {
  593. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  594. CPDFKitConfig.sharedInstance().setEnableFormFieldHighlight(!enabled)
  595. self.listView.setNeedsDisplayForVisiblePages()
  596. }
  597. func highlightLinks(_ sender: Any?) {
  598. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  599. CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(!enabled)
  600. self.listView.setNeedsDisplayForVisiblePages()
  601. }
  602. func resetForm(_ sender: Any?) {
  603. self.listView.resetFormAnnotation()
  604. }
  605. func removeAllAnnotation(_ sender: Any?) {
  606. self.listView.removeAllAnnotation()
  607. }
  608. func savePDFSettingToDefaults(_ sender: Any?) {
  609. if self.interactionMode == .normal {
  610. UserDefaults.standard.sync_setValue(self.currentPDFSettings(), forKey: KMDefaultPDFDisplaySettingsKey)
  611. } else if self.interactionMode != .presentation {
  612. UserDefaults.standard.sync_setValue(self.currentPDFSettings(), forKey: KMDefaultFullScreenPDFDisplaySettingsKey)
  613. }
  614. if listView.displaysAsBook && self.listView.displayTwoUp {
  615. if listView.fetchDisplayViewMode() == .twoUp {
  616. KMPreferenceManager.shared.viewPageDisplayType = .bookMode
  617. } else {
  618. KMPreferenceManager.shared.viewPageDisplayType = .bookContinuous
  619. }
  620. } else {
  621. KMPreferenceManager.shared.viewPageDisplayType = KMPDFDisplayType(rawValue: listView.fetchDisplayViewMode().rawValue) ?? .singlePage
  622. }
  623. self.savePageNumberIfNeed()
  624. }
  625. }
  626. // MARK: - goto Menu
  627. extension KMMainViewController {
  628. @IBAction func menuItemAction_nextPage(_ sender: Any) {
  629. if (self.listView.canGoToNextPage()) {
  630. self.listView.goToNextPage(nil)
  631. }
  632. }
  633. @IBAction func menuItemAction_forwardPage(_ sender: Any) {
  634. if (self.listView.canGoToPreviousPage()) {
  635. self.listView.goToPreviousPage(nil)
  636. }
  637. }
  638. @IBAction func menuItemAction_firstPage(_ sender: Any) {
  639. if (self.listView.canGoToFirstPage()) {
  640. self.listView.goToFirstPage(nil)
  641. }
  642. }
  643. @IBAction func menuItemAction_lastPage(_ sender: Any) {
  644. if (self.listView.canGoToLastPage()) {
  645. self.listView.goToLastPage(nil)
  646. }
  647. }
  648. @IBAction func menuItemAction_forward(_ sender: Any) {
  649. if (self.listView.km_canGoForward()) {
  650. self.listView.km_goForward(nil)
  651. }
  652. }
  653. @IBAction func menuItemAction_goback(_ sender: Any) {
  654. if (self.listView.km_canGoBack()) {
  655. self.listView.km_goBack(nil)
  656. }
  657. }
  658. @IBAction func menuItemAction_gotoPage(_ sender: Any) {
  659. let sheet = KMTextFieldSheetController.init(windowNibName: "PageSheet")
  660. var pages : [String] = []
  661. for i in 0 ..< self.listView.document.pageCount {
  662. pages.append("\(i)")
  663. }
  664. sheet.callback = { [weak self] stringValue in
  665. guard let index = Int(stringValue) else {
  666. return
  667. }
  668. if (self?.listView == nil) {
  669. return
  670. }
  671. if (index > 0 && index <= self!.listView.document.pageCount) {
  672. self?.listView.go(toPageIndex: index-1, animated: true)
  673. }
  674. }
  675. sheet.showWindow(nil)
  676. sheet.pageBox.addItems(withObjectValues: pages)
  677. sheet.stringValue = "\(self.listView.currentPageIndex+1)"
  678. }
  679. @IBAction func goToMarkedPage(_ sender: Any?) {
  680. guard let pdfDoc = self.listView.document, pdfDoc.isLocked == false && pdfDoc.pageCount > 0 else {
  681. NSSound.beep()
  682. return
  683. }
  684. guard let currentPageIndex = self.listView.currentPage()?.pageIndex() else {
  685. NSSound.beep()
  686. return
  687. }
  688. if self.model.beforeMarkedPageIndex != NSNotFound {
  689. self.listView.goToPage(at: min(UInt(self.model.beforeMarkedPageIndex), pdfDoc.pageCount-1), point: self.model.beforeMarkedPagePoint)
  690. } else if (currentPageIndex != self.model.markedPageIndex) {
  691. // NSUInteger lastPageIndex = [pdfView currentPageIndexAndPoint:&beforeMarkedPagePoint rotated:NULL];
  692. let lastPageIndex = self.listView.currentPageIndexAndPoint(&self.model.beforeMarkedPagePoint, rotated: nil)
  693. self.listView.goToPage(at: min(UInt(self.model.markedPageIndex), pdfDoc.pageCount-1), point: self.model.markedPagePoint)
  694. self.model.beforeMarkedPageIndex = Int(lastPageIndex)
  695. }
  696. }
  697. @IBAction func markPage(_ sender: Any?) {
  698. self.model.markedPageIndex = Int(self.listView.currentPageIndexAndPoint(&self.model.markedPagePoint, rotated: nil))
  699. self.model.beforeMarkedPageIndex = NSNotFound
  700. }
  701. }
  702. // MARK: - tool Menu
  703. extension KMMainViewController: KMSystemToolMenuProtocol {
  704. func getInfo(_ sender: Any?) {
  705. KMInfoWindowController.shared.showWindow(sender)
  706. }
  707. @IBAction func menuItemAction_textTool(_ sender: Any) {
  708. self.listView?.toolMode = .textToolMode
  709. self.listView?.annotationType = .unkown
  710. }
  711. @IBAction func menuItemAction_scrolTool(_ sender: Any) {
  712. self.toolbarController.selectItem(KMToolbarMoveToolModeItemIdentifier)
  713. }
  714. @IBAction func menuItemAction_zoomOutTool(_ sender: Any) {
  715. self.toolbarController.selectItem(KMToolbarMagnifyToolModeItemIdentifier)
  716. }
  717. @IBAction func menuItemAction_selectTool(_ sender: Any) {
  718. self.toolbarController.selectItem(KMToolbarSelectToolModeItemIdentifier)
  719. }
  720. func changeToolMode(_ sender: Any?) {
  721. let value = KMDataManager.ud_integer(forKey: SKLastAnnotationModeKey)
  722. var aType: CAnnotationType = .highlight
  723. if let data = CAnnotationType(rawValue: value) {
  724. aType = data
  725. }
  726. if aType.isMarkup() || aType.isSquare() || aType == .freeText || aType == .anchored {
  727. } else {
  728. aType = .highlight
  729. }
  730. var itemId: String = KMToolbarHighlightAnnotationItemIdentifier
  731. if aType == .highlight {
  732. } else if aType == .underline {
  733. itemId = KMToolbarUnderlineAnnotationItemIdentifier
  734. } else if aType == .strikeOut {
  735. itemId = KMToolbarStrikeOutAnnotationItemIdentifier
  736. } else if aType == .ink {
  737. itemId = KMToolbarInkAnnotationItemIdentifier
  738. } else if aType == .freeText {
  739. itemId = KMToolbarFreeTextAnnotationItemIdentifier
  740. } else if aType == .anchored {
  741. itemId = KMToolbarAnchoredAnnotationItemIdentifier
  742. } else if aType == .square {
  743. itemId = KMToolbarSquareAnnotationItemIdentifier
  744. } else if aType == .circle {
  745. itemId = KMToolbarCircleAnnotationItemIdentifier
  746. } else if aType == .line {
  747. itemId = KMToolbarLineAnnotationItemIdentifier
  748. } else if aType == .arrow {
  749. itemId = KMToolbarArrowAnnotationItemIdentifier
  750. } else if aType == .link {
  751. itemId = KMToolbarLinkAnnotationItemIdentifier
  752. }
  753. let isSelected = self.toolbarController.findItem(itemId)?.isSelected ?? false
  754. if isSelected == false {
  755. self.toolbarController.selectItem(itemId)
  756. }
  757. }
  758. @IBAction func menuItemAction_redact(_ sender: Any) {
  759. self.toolbarController.clickItem(KMDocumentRedactToolbarItemIdentifier)
  760. }
  761. @IBAction func menuItemAction_warkmark(_ sender: Any) {
  762. self.toolbarController.delegate?.clickChildTool?(type: .watermark, index: 0)
  763. }
  764. @IBAction func menuItemAction_background(_ sender: Any) {
  765. self.toolbarController.delegate?.clickChildTool?(type: .background, index: 0)
  766. }
  767. @IBAction func menuItemAction_headerfooter(_ sender: Any) {
  768. self.toolbarController.delegate?.clickChildTool?(type: .headerAndFooter, index: 0)
  769. }
  770. @IBAction func menuItemAction_bates(_ sender: Any) {
  771. self.toolbarController.delegate?.clickChildTool?(type: .bates, index: 0)
  772. }
  773. @IBAction func menuItemAction_batch(_ sender: Any) {
  774. // KMBatchWindowController.openFile(self.listView.document?.documentURL, .Batch)
  775. }
  776. func compression(_ sender: Any?) {
  777. self.toolbarController.delegate?.clickChildTool?(type: .compress, index: 0)
  778. }
  779. func merge(_ sender: Any?) {
  780. self.toolbarController.delegate?.clickChildTool?(type: .merge, index: 0)
  781. }
  782. func convert(_ sender: Any?) {
  783. self.toolbarController.delegate?.clickChildTool?(type: .word, index: 0)
  784. }
  785. func imageToPDF(_ sender: Any?) {
  786. KMApp.ShowImageToPDFWindow()
  787. }
  788. func ocr(_ sender: Any?) {
  789. self.toolbarController.clickItem(KMDocumentScanOCRToolbarItemIdentifier)
  790. }
  791. func extractImage(_ sender: Any?) {
  792. self.extractImageAction(num: 1)
  793. }
  794. func pageEdit(_ sender: Any?) {
  795. self.toolbarController.clickItem(KMDocumentPageToolbarItemIdentifier)
  796. }
  797. func textEditAction(_ sender: Any?) {
  798. self.toolbarController.clickItem(KMDocumentEditToolbarItemIdentifier)
  799. }
  800. func security(_ sender: Any?) {
  801. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 1)
  802. }
  803. func decryption(_ sender: Any?) {
  804. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 2)
  805. }
  806. func cropAll(_ sender: Any?) {
  807. self.cropAllPage()
  808. }
  809. func crop(_ sender: Any?) {
  810. self.cropCurrentPage()
  811. }
  812. // 白边距
  813. func autoCropAll(_ sender: Any?) {
  814. self.smart_cropAllPage()
  815. }
  816. // 自动
  817. func smartAutoCropAll(_ sender: Any?) {
  818. self.auto_cropAllPage()
  819. }
  820. func cropOptionsModel(_ sender: Any?) {
  821. KMPrint("cropOptionsModel ...")
  822. }
  823. func selectToolModel(_ sender: Any?) {
  824. self.toolbarController.clickItem(KMToolbarSelectToolModeItemIdentifier)
  825. }
  826. func takeSnapshot(_ sender: Any?) {
  827. self.listView.takeSnapshot(sender)
  828. }
  829. func takeSnapshotSelectContent(_ sender: Any?) {
  830. self.listView.takeSnapshotSelectContent(sender)
  831. }
  832. func changeAnnotationMode(_ sender: Any?) {
  833. if let data = sender as? NSMenuItem {
  834. // self.changeAnnotationMode_itemAction(sender: data)
  835. var itemId: String?
  836. if data.tag == 8 {
  837. itemId = KMToolbarHighlightAnnotationItemIdentifier
  838. } else if data.tag == 9 {
  839. itemId = KMToolbarUnderlineAnnotationItemIdentifier
  840. } else if data.tag == 10 {
  841. itemId = KMToolbarStrikeOutAnnotationItemIdentifier
  842. }
  843. // else if data.tag == CAnnotationType.squiggly.rawValue {
  844. // itemId = KMToolbarSquigglyAnnotationItemIdentifier
  845. // }
  846. else if data.tag == 13 {
  847. itemId = KMToolbarInkAnnotationItemIdentifier
  848. } else if data.tag == 3 {
  849. itemId = KMToolbarFreeTextAnnotationItemIdentifier
  850. } else if data.tag == 1 {
  851. itemId = KMToolbarAnchoredAnnotationItemIdentifier
  852. } else if data.tag == 6 {
  853. itemId = KMToolbarSquareAnnotationItemIdentifier
  854. } else if data.tag == 7 {
  855. itemId = KMToolbarCircleAnnotationItemIdentifier
  856. }
  857. // else if data.tag == 4 {
  858. // itemId = KMToolbarArrowAnnotationItemIdentifier
  859. // }
  860. else if data.tag == 4 {
  861. itemId = KMToolbarLineAnnotationItemIdentifier
  862. } else if data.tag == 2 {
  863. itemId = KMToolbarLinkAnnotationItemIdentifier
  864. }
  865. if let data = itemId {
  866. self.toolbarController.clickItem(data)
  867. }
  868. }
  869. }
  870. func addWatermark(_ sender: Any?) {
  871. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  872. if item == nil {
  873. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  874. }
  875. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  876. }
  877. func removeWatermark(_ sender: Any?) {
  878. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  879. if item == nil {
  880. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  881. }
  882. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  883. }
  884. func batchAddWatermark(_ sender: Any?) {
  885. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  886. if item == nil {
  887. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  888. }
  889. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  890. }
  891. func batchRemoveWatermark(_ sender: Any?) {
  892. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  893. if item == nil {
  894. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  895. }
  896. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  897. }
  898. func templateWatermark(_ sender: Any?) {
  899. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  900. if item == nil {
  901. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  902. }
  903. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  904. }
  905. func addBackground(_ sender: Any?) {
  906. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  907. if item == nil {
  908. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  909. }
  910. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  911. }
  912. func removeBackground(_ sender: Any?) {
  913. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  914. if item == nil {
  915. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  916. }
  917. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  918. }
  919. func batchAddBackground(_ sender: Any?) {
  920. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  921. if item == nil {
  922. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  923. }
  924. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  925. }
  926. func batchRemoveBackground(_ sender: Any?) {
  927. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  928. if item == nil {
  929. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  930. }
  931. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil) }
  932. func templateBackground(_ sender: Any?) {
  933. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  934. if item == nil {
  935. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  936. }
  937. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  938. }
  939. func addHeaderFooter(_ sender: Any?) {
  940. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  941. if item == nil {
  942. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  943. }
  944. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  945. }
  946. func removeHeaderFooter(_ sender: Any?) {
  947. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  948. if item == nil {
  949. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  950. }
  951. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  952. }
  953. func batchAddHeaderFooter(_ sender: Any?) {
  954. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  955. if item == nil {
  956. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  957. }
  958. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  959. }
  960. func batchRemoveHeaderFooter(_ sender: Any?) {
  961. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  962. if item == nil {
  963. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  964. }
  965. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  966. }
  967. func templateHeaderFooter(_ sender: Any?) {
  968. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  969. if item == nil {
  970. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  971. }
  972. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  973. }
  974. func addHeaderFooterBates(_ sender: Any?) {
  975. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  976. if item == nil {
  977. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  978. }
  979. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  980. }
  981. func removeHeaderFooterBates(_ sender: Any?) {
  982. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  983. if item == nil {
  984. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  985. }
  986. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  987. }
  988. func batchAddBates(_ sender: Any?) {
  989. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  990. if item == nil {
  991. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  992. }
  993. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  994. }
  995. func batchRemoveBates(_ sender: Any?) {
  996. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  997. if item == nil {
  998. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  999. }
  1000. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  1001. }
  1002. func templateBates(_ sender: Any?) {
  1003. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  1004. if item == nil {
  1005. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  1006. }
  1007. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  1008. }
  1009. func togglePoster(_ sender: Any?) {
  1010. self.saveDocument()
  1011. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .poster)
  1012. }
  1013. func toggleMultiple(_ sender: Any?) {
  1014. self.saveDocument()
  1015. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .multipage)
  1016. }
  1017. func toggleBooklet(_ sender: Any?) {
  1018. self.saveDocument()
  1019. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
  1020. }
  1021. func rotateLeft(_ sender: Any?) {
  1022. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1023. let winC = KMPurchaseCompareWindowController.sharedInstance()
  1024. winC?.kEventName = "Reading_Rotate_BuyNow"
  1025. winC?.kEventTag = 1
  1026. winC?.showWindow(nil)
  1027. return
  1028. }
  1029. AccountTools.canUseAdvance_success { can in
  1030. if can {
  1031. self.oem_rotateLeft(sender)
  1032. }
  1033. }
  1034. }
  1035. func oem_rotateLeft(_ sender: Any?) {
  1036. Task { @MainActor in
  1037. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  1038. let rotation = page.rotation
  1039. page.leftRotate()
  1040. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  1041. self.handlePageBoundsDidChange(page: page, action: CPDFPage.Action.rotate)
  1042. }
  1043. }
  1044. func rotateRight(_ sender: Any?) {
  1045. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1046. let winC = KMPurchaseCompareWindowController.sharedInstance()
  1047. winC?.kEventName = "Reading_Rotate_BuyNow"
  1048. winC?.kEventTag = 1
  1049. winC?.showWindow(nil)
  1050. return
  1051. }
  1052. AccountTools.canUseAdvance_success { can in
  1053. if can {
  1054. self.oem_rotateRight(sender)
  1055. }
  1056. }
  1057. }
  1058. func oem_rotateRight(_ sender: Any?) {
  1059. Task { @MainActor in
  1060. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  1061. let rotation = page.rotation
  1062. page.rightRotate()
  1063. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  1064. self.handlePageBoundsDidChange(page: page, action: CPDFPage.Action.rotate)
  1065. }
  1066. }
  1067. func rotateAllLeft(_ sender: Any?) {
  1068. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1069. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  1070. return
  1071. }
  1072. AccountTools.canUseAdvance_success { can in
  1073. if can {
  1074. self.oem_rotateAllLeft(sender)
  1075. }
  1076. }
  1077. }
  1078. func oem_rotateAllLeft(_ sender: Any?) {
  1079. Task { @MainActor in
  1080. var pageItems: [KMThumbnailPageItem] = []
  1081. for index in 0 ... self.listView.document.pageCount - 1 {
  1082. let page: CPDFPage = self.listView.document.page(at: index)
  1083. let rotation = page.rotation
  1084. page.leftRotate()
  1085. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  1086. }
  1087. self.rotatePageItems(pageItems: pageItems)
  1088. self.handlePageBoundsDidChange(page: nil, action: CPDFPage.Action.rotate)
  1089. }
  1090. }
  1091. func rotateAllRight(_ sender: Any?) {
  1092. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1093. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  1094. return
  1095. }
  1096. AccountTools.canUseAdvance_success { can in
  1097. if can {
  1098. self.oem_rotateAllRight(sender)
  1099. }
  1100. }
  1101. }
  1102. func oem_rotateAllRight(_ sender: Any?) {
  1103. Task { @MainActor in
  1104. var pageItems: [KMThumbnailPageItem] = []
  1105. for index in 0 ... self.listView.document.pageCount - 1 {
  1106. let page: CPDFPage = self.listView.document.page(at: index)
  1107. let rotation = page.rotation
  1108. page.rightRotate()
  1109. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  1110. }
  1111. self.rotatePageItems(pageItems: pageItems)
  1112. self.handlePageBoundsDidChange(page: nil, action: CPDFPage.Action.rotate)
  1113. }
  1114. }
  1115. func autoSelectContent(_ sender: Any?) {
  1116. self.listView.autoSelectContent(sender)
  1117. }
  1118. }
  1119. // MARK: - window Menu
  1120. extension KMMainViewController {
  1121. @IBAction func menuItemAction_showForwardTagPage(_ sender: Any) {
  1122. (self.myDocument as? KMMainDocument)?.browser.selectPreviousTab()
  1123. }
  1124. @IBAction func menuItemAction_showNextTagPage(_ sender: Any) {
  1125. (self.myDocument as? KMMainDocument)?.browser.selectNextTab()
  1126. }
  1127. @IBAction func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  1128. self.browserWindowController?.openNewWindow(sender)
  1129. }
  1130. @IBAction func menuItemAction_mergeAllWindow(_ sender: Any) {
  1131. ((self.myDocument as? KMMainDocument)?.browser?.windowController as? KMBrowserWindowController)?.mergeAllWindow(sender)
  1132. }
  1133. @IBAction func menuItemAction_currentWindowName(_ sender: Any) {
  1134. }
  1135. @IBAction func performFit(_ sender: Any?) {
  1136. if self.interactionMode != .normal {
  1137. NSSound.beep()
  1138. return
  1139. }
  1140. let displayMode = self.listView.fetchDisplayViewMode()
  1141. let scaleFactor = self.listView.scaleFactor
  1142. let autoScales = self.listView.autoScales
  1143. var isSingleRow = false
  1144. if displayMode == .singlePage || displayMode == .twoUp {
  1145. isSingleRow = true
  1146. } else if displayMode == .singlePageContinuous || self.listView.displaysAsBook {
  1147. isSingleRow = (self.listView?.document?.pageCount ?? 0) <= 1
  1148. } else {
  1149. isSingleRow = (self.listView?.document?.pageCount ?? 0) <= 2
  1150. }
  1151. var frame = self.view.window?.frame ?? .zero
  1152. var size: NSSize = .zero
  1153. let oldSize = self.listView.frame.size
  1154. let documentView = self.listView.documentView()
  1155. let documentRect = documentView?.convert(documentView?.bounds ?? .zero, to: nil) ?? .zero
  1156. let page = self.listView.currentPage()
  1157. let box = self.listView.displayBox
  1158. let PAGE_BREAK_MARGIN: CGFloat = 8.0
  1159. let margin = self.listView.displaysPageBreaks ? PAGE_BREAK_MARGIN : 0.0
  1160. // Calculate the new size for the pdfView
  1161. size.width = NSWidth(documentRect)
  1162. if (autoScales) {
  1163. size.width /= scaleFactor
  1164. }
  1165. if (isSingleRow) {
  1166. size.height = NSHeight(documentRect)
  1167. } else {
  1168. let rect = self.listView.convert(page?.bounds(for: box) ?? .zero, from: page)
  1169. size.height = NSHeight(rect) + margin * scaleFactor
  1170. // if ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)] == NO || [NSScroller preferredScrollerStyle] == NSScrollerStyleLegacy)
  1171. // size.width += [NSScroller scrollerWidth];
  1172. if NSScroller.responds(to: NSSelectorFromString("preferredScrollerStyle")) == false {
  1173. let w = NSScroller.scrollerWidth(for: .regular, scrollerStyle: .legacy)
  1174. size.width += w
  1175. } else {
  1176. if NSScroller.preferredScrollerStyle == .legacy {
  1177. let w = NSScroller.scrollerWidth(for: .regular, scrollerStyle: .legacy)
  1178. size.width += w
  1179. }
  1180. }
  1181. }
  1182. if (autoScales) {
  1183. size.height /= scaleFactor
  1184. }
  1185. // Calculate the new size for the window
  1186. size.width = ceil(NSWidth(frame) + size.width - oldSize.width)
  1187. size.height = ceil(NSHeight(frame) + size.height - oldSize.height)
  1188. // Align the window frame from the old topleft point and constrain to the screen
  1189. frame.origin.y = NSMaxY(frame) - size.height
  1190. frame.size = size
  1191. if let screen = self.view.window?.screen ?? NSScreen.main {
  1192. frame = self.view.window?.constrainFrameRect(frame, to: screen) ?? .zero
  1193. }
  1194. self.view.window?.setFrame(frame, display: self.view.window?.isVisible ?? false)
  1195. if displayMode == .singlePageContinuous || displayMode == .twoUpContinuous {
  1196. self.listView.go(to: NSInsetRect(page?.bounds(for: box) ?? .zero, -0.5 * margin, -0.5 * margin), on: page)
  1197. }
  1198. }
  1199. }
  1200. extension KMMainViewController {
  1201. override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  1202. if (menuItem.action == #selector(menuItemAction_currentWindowName)) {
  1203. menuItem.title = (self.myDocument?.fileURL?.lastPathComponent) ?? ""
  1204. return true
  1205. }
  1206. let act = menuItem.action
  1207. if act == #selector(menuItemAnnotationClick_addStype) ||
  1208. act == #selector(menuItemAnnotationClick_add) ||
  1209. act == #selector(menuItemAnnotationClick_toolModel) ||
  1210. act == #selector(changeAnnotationMode_itemAction) {
  1211. return !(self.listView?.hideNotes ?? false)
  1212. }
  1213. if let data = act, KMSystemMenu.isAnnotationSelector(sel: data) {
  1214. if KMSystemMenu.Annotation.hiddenAllSelector == act {
  1215. // return true
  1216. } else {
  1217. return !(self.listView?.hideNotes ?? false)
  1218. }
  1219. }
  1220. if (menuItem.action == #selector(menuItemAction_thumai) ||
  1221. menuItem.action == #selector(menuItemAction_outline) ||
  1222. menuItem.action == #selector(menuItemAction_bookmark) ||
  1223. menuItem.action == #selector(menuItemAction_annotation) ||
  1224. menuItem.action == #selector(menuItemAction_search)) {
  1225. menuItem.state = .off
  1226. let type = self.leftSideViewController.type.methodType
  1227. if (menuItem.action == #selector(menuItemAction_thumai) && type == .Thumbnail) {
  1228. menuItem.state = .on
  1229. }
  1230. if (menuItem.action == #selector(menuItemAction_outline) && type == .Outline) {
  1231. menuItem.state = .on
  1232. }
  1233. if (menuItem.action == #selector(menuItemAction_bookmark) && type == .Annotation) {
  1234. menuItem.state = .on
  1235. }
  1236. if (menuItem.action == #selector(menuItemAction_annotation) && type == .Annotation) {
  1237. menuItem.state = .on
  1238. }
  1239. if (menuItem.action == #selector(menuItemAction_search) && type == .Search) {
  1240. menuItem.state = .on
  1241. }
  1242. }
  1243. if (menuItem.action == #selector(menuItemAction_hiddenLeftSide)) {
  1244. if (self.model.leftPanelOpen) {
  1245. menuItem.title = NSLocalizedString("Hide Left Side Panel", comment: "")
  1246. } else {
  1247. menuItem.title = NSLocalizedString("Show Left Side Panel", comment: "")
  1248. }
  1249. }
  1250. if (menuItem.action == #selector(menuItemAction_hiddenRightSide)) {
  1251. if (self.model.rightPanelIsOpen) {
  1252. menuItem.title = NSLocalizedString("Hide Right Side Panel", comment: "")
  1253. } else {
  1254. menuItem.title = NSLocalizedString("Show Right Side Panel", comment: "")
  1255. }
  1256. }
  1257. if (menuItem.action == #selector(menuItemAction_hiddenAllAnnotation)) {
  1258. if (self.listView.hideNotes) {
  1259. menuItem.title = NSLocalizedString("Show Notes", comment: "")
  1260. } else {
  1261. menuItem.title = NSLocalizedString("Hide Notes", comment: "")
  1262. }
  1263. }
  1264. if (menuItem.action == #selector(menuItemAction_showForwardTagPage) ||
  1265. menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  1266. let browser = self.browserWindowController?.browser
  1267. if (browser != nil && (browser is KMBrowser)) {
  1268. if (menuItem.action == #selector(menuItemAction_showForwardTagPage)) {
  1269. return (browser as! KMBrowser).canSelectPreviousTab()
  1270. }
  1271. if (menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  1272. return (browser as! KMBrowser).canSelectNextTab()
  1273. }
  1274. }
  1275. }
  1276. if (menuItem.action == #selector(menuItemAction_mergeAllWindow)) {
  1277. if let _browserWindowC = ((self.myDocument as? KMMainDocument)?.browser.windowController as? KMBrowserWindowController) {
  1278. return _browserWindowC.canMergeAllWindow()
  1279. }
  1280. }
  1281. if (menuItem.action == #selector(menuItemAction_nextPage) ||
  1282. menuItem.action == #selector(menuItemAction_forwardPage) ||
  1283. menuItem.action == #selector(menuItemAction_firstPage) ||
  1284. menuItem.action == #selector(menuItemAction_lastPage) ||
  1285. menuItem.action == #selector(menuItemAction_forward) ||
  1286. menuItem.action == #selector(menuItemAction_goback)) {
  1287. if self.hasEnterPageEdit() {
  1288. return false
  1289. }
  1290. if (menuItem.action == #selector(menuItemAction_nextPage)) {
  1291. if self.listView.isEditing() && self.listView.isEditable() {
  1292. return false
  1293. }
  1294. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage())
  1295. }
  1296. if (menuItem.action == #selector(menuItemAction_forwardPage)) {
  1297. if self.listView.isEditing() && self.listView.isEditable() {
  1298. return false
  1299. }
  1300. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage())
  1301. }
  1302. if (menuItem.action == #selector(menuItemAction_firstPage)) {
  1303. return self.listView.canGoToFirstPage()
  1304. }
  1305. if (menuItem.action == #selector(menuItemAction_lastPage)) {
  1306. return self.listView.canGoToLastPage()
  1307. }
  1308. if (menuItem.action == #selector(menuItemAction_forward)) {
  1309. return self.listView.km_canGoForward()
  1310. }
  1311. if (menuItem.action == #selector(menuItemAction_goback)) {
  1312. return self.listView.km_canGoBack()
  1313. }
  1314. }
  1315. if menuItem.action == #selector(changeAnnotationMode) {
  1316. let type = self.listView.annotationType
  1317. if type == .highlight {
  1318. menuItem.state = menuItem.tag == 8 ? .on : .off
  1319. } else if type == .underline {
  1320. menuItem.state = menuItem.tag == 9 ? .on : .off
  1321. } else if type == .strikeOut {
  1322. menuItem.state = menuItem.tag == 10 ? .on : .off
  1323. } else if type == .ink {
  1324. menuItem.state = menuItem.tag == 13 ? .on : .off
  1325. } else if type == .freeText {
  1326. menuItem.state = menuItem.tag == 3 ? .on : .off
  1327. } else if type == .anchored {
  1328. menuItem.state = menuItem.tag == 1 ? .on : .off
  1329. } else if type == .square {
  1330. menuItem.state = menuItem.tag == 6 ? .on : .off
  1331. } else if type == .circle {
  1332. menuItem.state = menuItem.tag == 7 ? .on : .off
  1333. } else if type == .line {
  1334. menuItem.state = menuItem.tag == 4 ? .on : .off
  1335. } else if type == .link {
  1336. menuItem.state = menuItem.tag == 2 ? .on : .off
  1337. } else {
  1338. menuItem.state = .off
  1339. }
  1340. return !self.listView.hideNotes
  1341. }
  1342. if menuItem.action == #selector(changeAnnotationMode_itemAction) {
  1343. // if ([[menuItem menu] numberOfItems] > ANNOTATION_MODE_COUNT)
  1344. // [menuItem setState:[self toolMode] == SKNoteToolMode && [self annotationMode] == (SKNoteType)[menuItem tag] ? NSOnState : NSOffState];
  1345. // else
  1346. // [menuItem setState:[self annotationMode] == (SKNoteType)[menuItem tag] ? NSOnState : NSOffState];
  1347. if self.listView.annotationType.rawValue == menuItem.tag {
  1348. menuItem.state = .on
  1349. } else {
  1350. menuItem.state = .off
  1351. }
  1352. return true
  1353. }
  1354. if menuItem.action == #selector(menuItemAction_print) {
  1355. if self.listView.document.permissionsStatus == .user && !self.listView.document.allowsPrinting {
  1356. return false
  1357. }
  1358. }
  1359. if menuItem.action == #selector(highlightFormFiled) {
  1360. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  1361. menuItem.state = enabled ? .on : .off
  1362. return true
  1363. }
  1364. if menuItem.action == #selector(highlightLinks) {
  1365. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  1366. menuItem.state = enabled ? .on : .off
  1367. return true
  1368. }
  1369. guard let action = menuItem.action else {
  1370. return false
  1371. }
  1372. if KMSystemMenu.isToolSelector(sel: action) {
  1373. if action == KMSystemMenu.Tool.autoSelectContentSelector {
  1374. let toolMode = self.listView?.toolMode ?? .none
  1375. return toolMode == .selectToolMode
  1376. } else if action == KMSystemMenu.Tool.textToolSelector {
  1377. let toolMode = self.listView?.toolMode ?? .textToolMode
  1378. menuItem.state = toolMode == .textToolMode ? .on : .off
  1379. return true
  1380. } else if action == KMSystemMenu.Tool.scrolToolSelector {
  1381. let toolMode = self.listView?.toolMode ?? .textToolMode
  1382. menuItem.state = toolMode == .moveToolMode ? .on : .off
  1383. return true
  1384. } else if action == KMSystemMenu.Tool.zoomToolSelector {
  1385. let toolMode = self.listView?.toolMode ?? .textToolMode
  1386. menuItem.state = toolMode == .magnifyToolMode ? .on : .off
  1387. return true
  1388. } else if action == KMSystemMenu.Tool.selectToolSelector {
  1389. let toolMode = self.listView?.toolMode ?? .textToolMode
  1390. menuItem.state = toolMode == .selectToolMode ? .on : .off
  1391. return true
  1392. } else if action == KMSystemMenu.Tool.changeToolModeSelector {
  1393. let aType = self.listView?.annotationType ?? .unkown
  1394. if aType.isMarkup() || aType == .freeText || aType == .anchored || aType.isSquare() || aType == .link {
  1395. menuItem.state = .on
  1396. } else {
  1397. menuItem.state = .off
  1398. }
  1399. return true
  1400. }
  1401. }
  1402. if KMSystemMenu.isViewSelector(sel: action) {
  1403. if action == KMSystemMenu.View.toggleLeftSidePaneSelector {
  1404. if self.model.leftPanelOpen {
  1405. menuItem.title = NSLocalizedString("Hide Panel", comment: "Menu item title")
  1406. } else {
  1407. menuItem.title = NSLocalizedString("Show Panel", comment: "Menu item title")
  1408. }
  1409. return true
  1410. } else if action == KMSystemMenu.View.changeLeftSidePaneStateSelector {
  1411. if menuItem.tag == 3 { // 快照
  1412. menuItem.state = self.leftSideViewController.type.methodType == .snapshot ? .on : .off
  1413. }
  1414. return true
  1415. } else if action == KMSystemMenu.View.themesColorSelector {
  1416. let mode = menuItem.tag
  1417. var viewMode: KMPDFViewMode = .normal
  1418. if let storedViewMode = UserDefaults.standard.value(forKey: "kPDFViewModeKey") as? Int {
  1419. viewMode = KMPDFViewMode(rawValue: UInt(storedViewMode)) ?? .normal
  1420. }
  1421. if mode == 0 {
  1422. menuItem.state = viewMode == .normal ? .on : .off
  1423. } else if mode == 1 {
  1424. menuItem.state = viewMode == .soft ? .on : .off
  1425. } else if mode == 2 {
  1426. menuItem.state = viewMode == .night ? .on : .off
  1427. } else if mode == 3 {
  1428. menuItem.state = viewMode == .green ? .on : .off
  1429. } else if mode == 4 {
  1430. menuItem.state = viewMode == .themes1 ? .on : .off
  1431. } else if mode == 5 {
  1432. menuItem.state = viewMode == .themes2 ? .on : .off
  1433. } else if mode == 6 {
  1434. menuItem.state = viewMode == .themes3 ? .on : .off
  1435. } else if mode == 7 {
  1436. menuItem.state = viewMode == .themes4 ? .on : .off
  1437. }
  1438. var data: Data? = nil
  1439. if let themesData = UserDefaults.standard.object(forKey: "kKMPDFViewModeThemesArray") as? Data {
  1440. data = themesData
  1441. }
  1442. var themesCount = 0
  1443. let appArray = NSKeyedUnarchiver.unarchiveObject(with: data ?? Data()) as? [Any] ?? []
  1444. if appArray.count > 0 {
  1445. themesCount = menuItem.tag - appArray.count
  1446. } else {
  1447. themesCount = menuItem.tag - 4
  1448. }
  1449. if themesCount >= 0 {
  1450. menuItem.isHidden = true
  1451. return false
  1452. }else {
  1453. menuItem.isHidden = false
  1454. return true
  1455. }
  1456. // return true
  1457. } else if action == KMSystemMenu.View.splitViewActionSelector {
  1458. let mode = menuItem.tag
  1459. if mode == 0 { // hor
  1460. menuItem.state = self.listView.viewSplitMode == .horizontal ? .on : .off
  1461. } else if mode == 1 { // verti
  1462. menuItem.state = self.listView.viewSplitMode == .vertical ? .on : .off
  1463. } else if mode == 2 { // single
  1464. menuItem.state = self.listView.viewSplitMode == .disable ? .on : .off
  1465. }
  1466. } else if action == KMSystemMenu.View.toggleToolbarShowSelector {
  1467. menuItem.title = self.toolbarController.mainToolBarisVisable ? KMLocalizedString("Hide Toolbar") : KMLocalizedString("Show Toolbar")
  1468. }
  1469. }
  1470. if KMSystemMenu.isAnnotationSelector(sel: action) {
  1471. if action == KMSystemMenu.Annotation.editNoteSelector {
  1472. if self.interactionMode == .presentation {
  1473. return false
  1474. }
  1475. guard let anno = self.listView?.activeAnnotation else {
  1476. return false
  1477. }
  1478. return anno.isEditable()
  1479. }
  1480. }
  1481. if action == NSSelectorFromString("changeDisplayBox:") {
  1482. if menuItem.tag == 0 {
  1483. menuItem.state = self.listView.displayBox == .mediaBox ? .on : .off
  1484. } else if menuItem.tag == 1 {
  1485. menuItem.state = self.listView.displayBox == .cropBox ? .on : .off
  1486. }
  1487. }
  1488. if KMSystemMenu.isFileSelector(sel: action) {
  1489. if action == KMSystemMenu.File.exportNoteSelector {
  1490. let cnt = self.leftSideViewController.annoListModel?.datas.count ?? 0
  1491. return cnt > 0
  1492. }
  1493. }
  1494. return true
  1495. }
  1496. }