KMMainViewController+MenuAction.swift 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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_saveAsFlattenedPDF(_ sender: Any) {
  71. Task { @MainActor in
  72. #if VERSION_DMG
  73. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  74. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  75. return
  76. }
  77. #endif
  78. if await (KMLightMemberManager.manager.canPayFunction() == false) {
  79. let _ = KMSubscribeWaterMarkWindowController.show(window: self.view.window!) { isSubscribeSuccess, isWaterMarkExport, isClose in
  80. if (isClose) {
  81. return
  82. }
  83. if (isSubscribeSuccess) {
  84. self.saveAsFlattenedPDFAction()
  85. return
  86. }
  87. if (isWaterMarkExport) {
  88. self.saveAsFlattenedPDFAction(limit: true)
  89. return
  90. }
  91. }
  92. return
  93. }
  94. self.saveAsFlattenedPDFAction()
  95. }
  96. }
  97. func saveAsFlattenedPDFAction(limit: Bool = false) {
  98. DispatchQueue.main.async {
  99. NSPanel.savePanel(self.view.window!, true) { panel in
  100. panel.nameFieldStringValue = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_flatten" + ".pdf"
  101. } completion: { response, url, isOpen in
  102. if (response == .cancel) {
  103. return
  104. }
  105. var result = false
  106. if (limit) {
  107. let data = KMTools.saveWatermarkDocumentForFlatten(document: self.listView.document, to: url!)
  108. result = data != nil
  109. } else {
  110. result = self.listView.document.writeFlatten(to: url!)
  111. }
  112. if (!result) {
  113. return
  114. }
  115. if (isOpen) {
  116. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  117. }
  118. } else {
  119. NSWorkspace.shared.activateFileViewerSelecting([url!])
  120. }
  121. }
  122. }
  123. }
  124. }
  125. // MARK: Edit Menu
  126. @objc protocol KMEditMenuProtocol: NSObjectProtocol {
  127. @objc optional func undo(_ sender: Any)
  128. @objc optional func redo(_ sender: Any)
  129. @objc optional func cut(_ sender: Any)
  130. @objc optional func copy(_ sender: Any)
  131. @objc optional func paste(_ sender: Any)
  132. @objc optional func delete(_ sender: Any)
  133. }
  134. extension KMMainViewController: KMEditMenuProtocol {
  135. @IBAction func menuItemAction_find(_ sender: Any) {
  136. if (self.leftSideViewController.isShowPanel) {
  137. self.toolbarController.cancelSelected(KMLeftControlToolbarItemIdentifier)
  138. self.toolbarController.toolbarType = .None
  139. self.leftSideViewController.showPanelView(show: false)
  140. }
  141. // else {
  142. let isSearch = self.leftSideViewController.type.methodType == .Search
  143. self.leftSideViewController.refreshMethodType(methodType: .Search)
  144. if (isSearch) { // 关闭
  145. self.view.window?.makeFirstResponder(self.listView)
  146. }
  147. // }
  148. }
  149. }
  150. // MARK: View Menu
  151. extension KMMainViewController {
  152. // scale
  153. @IBAction func menuItemAction_adjustWidth(_ sender: Any) {
  154. self.selectZoom(.width)
  155. }
  156. @IBAction func menuItemAction_adjustPage(_ sender: Any) {
  157. self.selectZoom(.fit)
  158. }
  159. @IBAction func menuItemAction_size(_ sender: Any) {
  160. self.selectZoom(.actualSize)
  161. }
  162. @IBAction func menuItemAction_zoomOut(_ sender: Any) {
  163. if (self.listView.canZoomOut) {
  164. self.listView.zoomOut(nil)
  165. }
  166. }
  167. @IBAction func menuItemAction_zoomIn(_ sender: Any) {
  168. if (self.listView.canZoomIn) {
  169. self.listView.zoomIn(nil)
  170. }
  171. }
  172. // page show
  173. @IBAction func menuItemAction_singlePage(_ sender: Any) {
  174. self.selectDisplay(display: .singlePage)
  175. }
  176. @IBAction func menuItemAction_singlePageContinue(_ sender: Any) {
  177. self.selectDisplay(display: .singlePageContinuous)
  178. }
  179. @IBAction func menuItemAction_doublePage(_ sender: Any) {
  180. self.selectDisplay(display: .twoUp)
  181. }
  182. @IBAction func menuItemAction_doublePageContinue(_ sender: Any) {
  183. self.selectDisplay(display: .twoUpContinuous)
  184. }
  185. @IBAction func menuItemAction_bookMode(_ sender: Any) {
  186. self.selectDisplay(display: .bookMode)
  187. }
  188. @IBAction func menuItemAction_readMode(_ sender: Any) {
  189. if (self.isReadMode) {
  190. self.closeReadModel()
  191. } else {
  192. self.openReadModel()
  193. }
  194. }
  195. @IBAction func menuItemAction_showSplitPage(_ sender: Any) {
  196. self.listView?.displaysPageBreaks = !(self.listView?.displaysPageBreaks ?? false)
  197. self.listView.layoutDocumentView()
  198. }
  199. @IBAction func menuItemAction_autoScrol(_ sender: Any) {
  200. }
  201. @IBAction func menuItemAction_autoScrolSetting(_ sender: Any) {
  202. }
  203. // rotate
  204. @IBAction func menuItemAction_rotateLeft(_ sender: Any) {
  205. Task { @MainActor in
  206. #if VERSION_DMG
  207. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  208. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  209. return
  210. }
  211. #endif
  212. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  213. let rotation = page.rotation
  214. page.leftRotate()
  215. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  216. }
  217. }
  218. func rotatePageItems(pageItems: [KMThumbnailPageItem]) {
  219. let tempPageItems = pageItems
  220. for pageItem in tempPageItems {
  221. pageItem.page.rotation = pageItem.rotate
  222. }
  223. self.listView.layoutDocumentView()
  224. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMPDFViewRotatePage"), object: self.listView.document)
  225. self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
  226. var tempPageItems: [KMThumbnailPageItem] = []
  227. for pageItem in pageItems {
  228. let item = KMThumbnailPageItem(page: pageItem.page, rotate: pageItem.oldRotate, oldRotate: pageItem.rotate)
  229. tempPageItems.append(item)
  230. }
  231. self.rotatePageItems(pageItems: tempPageItems)
  232. }
  233. }
  234. @IBAction func menuItemAction_rotateRight(_ sender: Any) {
  235. Task { @MainActor in
  236. #if VERSION_DMG
  237. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  238. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  239. return
  240. }
  241. #endif
  242. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  243. let rotation = page.rotation
  244. page.rightRotate()
  245. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  246. }
  247. }
  248. @IBAction func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  249. Task { @MainActor in
  250. #if VERSION_DMG
  251. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  252. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  253. return
  254. }
  255. #endif
  256. var pageItems: [KMThumbnailPageItem] = []
  257. for index in 0 ... self.listView.document.pageCount - 1 {
  258. let page: CPDFPage = self.listView.document.page(at: index)
  259. let rotation = page.rotation
  260. page.leftRotate()
  261. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  262. }
  263. self.rotatePageItems(pageItems: pageItems)
  264. }
  265. }
  266. @IBAction func menuItemAction_rotateAllPageRight(_ sender: Any) {
  267. Task { @MainActor in
  268. #if VERSION_DMG
  269. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  270. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  271. return
  272. }
  273. #endif
  274. var pageItems: [KMThumbnailPageItem] = []
  275. for index in 0 ... self.listView.document.pageCount - 1 {
  276. let page : CPDFPage = self.listView.document.page(at: index)
  277. let rotation = page.rotation
  278. page.rightRotate()
  279. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  280. }
  281. self.rotatePageItems(pageItems: pageItems)
  282. }
  283. }
  284. // split screen
  285. @IBAction func menuItemAction_splitScreenVerti(_ sender: Any) {
  286. }
  287. @IBAction func menuItemAction_splitScreenHorti(_ sender: Any) {
  288. }
  289. @IBAction func menuItemAction_splitScreenNo(_ sender: Any) {
  290. }
  291. @IBAction func menuItemAction_view_readMode(_ sender: Any) {
  292. if (self.isReadMode) {
  293. self.closeReadModel()
  294. } else {
  295. self.openReadModel()
  296. }
  297. }
  298. @IBAction func menuItemAction_enterFullScreen(_ sender: Any) {
  299. }
  300. @IBAction func menuItemAction_hiddenLeftSide(_ sender: Any) {
  301. if (self.leftSideViewController.type.methodType != .None) {
  302. self.leftSideViewController.selectType(self.leftSideViewController.type.methodType)
  303. } else {
  304. self.toolbarController.selectItem(KMLeftControlToolbarItemIdentifier)
  305. }
  306. }
  307. @IBAction func menuItemAction_hiddenRightSide(_ sender: Any) {
  308. self.toolbarController.selectItem(KMRightControlToolbarItemIdentifier)
  309. }
  310. @IBAction func menuItemAction_thumai(_ sender: Any) {
  311. self.leftSideViewController.switchType(.Thumbnail)
  312. }
  313. @IBAction func menuItemAction_outline(_ sender: Any) {
  314. self.leftSideViewController.switchType(.Outline)
  315. }
  316. @IBAction func menuItemAction_bookmark(_ sender: Any) {
  317. self.leftSideViewController.switchType(.Annotation)
  318. }
  319. @IBAction func menuItemAction_annotation(_ sender: Any) {
  320. self.leftSideViewController.switchType(.Annotation)
  321. }
  322. @IBAction func menuItemAction_search(_ sender: Any) {
  323. self.leftSideViewController.switchType(.Search)
  324. }
  325. @IBAction func menuItemAction_topic(_ sender: Any) {
  326. }
  327. @IBAction func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  328. }
  329. @IBAction func togglePresentation(_ sender: Any?) {
  330. KMPrint("togglePresentation")
  331. // if ([self canExitPresentation])
  332. // [self exitFullscreen];
  333. // else if ([self canEnterPresentation])
  334. // [self enterPresentation];
  335. }
  336. @IBAction func chooseTransition(_ sender: Any?) {
  337. KMPrint("chooseTransition")
  338. // presentationSheetController = [[SKPresentationOptionsSheetController alloc] initForController:self];
  339. //
  340. // [presentationSheetController beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {
  341. // SKDESTROY(presentationSheetController);
  342. // }];
  343. }
  344. @IBAction func toggleAutoFlow(_ sender: Any?) {
  345. KMPrint("toggleAutoFlow")
  346. if (self.listView.isAutoFlow()) {
  347. self.listView.stopAutoFlow()
  348. } else {
  349. self.listView.startAutoFlow()
  350. }
  351. }
  352. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  353. KMPrint("chooseAutoFlowSetting")
  354. autoFlowOptionsSheetController = KMAutoFlowOptionsSheetController(windowNibName: "KMAutoFlowOptionsSheetController")
  355. NSWindow.currentWindow().beginSheet((autoFlowOptionsSheetController?.window)!) { responce in
  356. }
  357. }
  358. @IBAction func toggleReadingBar(_ sender: Any?) {
  359. KMPrint("toggleReadingBar")
  360. }
  361. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  362. if self.leftPanelOpen {
  363. self.closeLeftPane()
  364. } else {
  365. self.openLeftPane()
  366. }
  367. }
  368. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  369. guard let item = sender as? NSMenuItem else {
  370. NSSound.beep()
  371. return
  372. }
  373. if item.tag == 3 {
  374. self.leftSideViewController.switchType(.snapshot)
  375. }
  376. }
  377. @IBAction func splitViewAction(_ sender: Any?) {
  378. KMPrint("splitViewAction")
  379. }
  380. @IBAction func themesColor(_ sender: Any?) {
  381. guard let item = sender as? NSMenuItem else {
  382. __NSBeep()
  383. return
  384. }
  385. let mode = item.tag
  386. if mode == 0 {
  387. self.listView.displayMode = .normal
  388. } else if mode == 1 {
  389. self.listView.displayMode = .soft
  390. } else if mode == 2 {
  391. self.listView.displayMode = .night
  392. } else if mode == 3 {
  393. self.listView.displayMode = .green
  394. }
  395. self.listView.setNeedsDisplayForVisiblePages()
  396. }
  397. @IBAction func toggleStatusBar(_ sender: Any?) {
  398. KMPrint("toggleStatusBar ...")
  399. }
  400. @IBAction func toggleToolbarShow(_ sender: Any?) {
  401. KMPrint("toggleToolbarShow ...")
  402. }
  403. }
  404. // MARK: Annotation Menu
  405. extension KMMainViewController {
  406. @IBAction func menuItemAction_highlight(_ sender: Any) {
  407. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarHighlightAnnotationItemIdentifier)
  408. }
  409. @IBAction func menuItemAction_underline(_ sender: Any) {
  410. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarUnderlineAnnotationItemIdentifier)
  411. }
  412. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  413. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarStrikeOutAnnotationItemIdentifier)
  414. }
  415. @IBAction func menuItemAction_freehand(_ sender: Any) {
  416. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarInkAnnotationItemIdentifier)
  417. }
  418. @IBAction func menuItemAction_text(_ sender: Any) {
  419. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarFreeTextAnnotationItemIdentifier)
  420. }
  421. @IBAction func menuItemAction_note(_ sender: Any) {
  422. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarAnchoredAnnotationItemIdentifier)
  423. }
  424. @IBAction func menuItemAction_squre(_ sender: Any) {
  425. UserDefaults.standard.set(6, forKey: KMToolBarToolPDFShapeStyle)
  426. if (self.listView.annotationType == .square) {
  427. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  428. } else {
  429. if (self.listView.annotationType == .circle || self.listView.annotationType == .line || self.listView.annotationType == .arrow) {
  430. self.listView.annotationType = .square
  431. self.rightSideViewController.annotationProperties.annotationMode = .square
  432. } else {
  433. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  434. }
  435. }
  436. }
  437. @IBAction func menuItemAction_circle(_ sender: Any) {
  438. UserDefaults.standard.set(7, forKey: KMToolBarToolPDFShapeStyle)
  439. if (self.listView.annotationType == .circle) { // 取消
  440. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  441. } else {
  442. if (self.listView.annotationType == .square || self.listView.annotationType == .line || self.listView.annotationType == .arrow) { // 切换
  443. self.listView.annotationType = .circle
  444. self.rightSideViewController.annotationProperties.annotationMode = .circle
  445. } else { // 选中
  446. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  447. }
  448. }
  449. }
  450. @IBAction func menuItemAction_arrow(_ sender: Any) {
  451. UserDefaults.standard.set(5, forKey: KMToolBarToolPDFShapeStyle)
  452. if (self.listView.annotationType == .arrow) { // 取消
  453. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  454. } else { // 选中
  455. if (self.listView.annotationType == .square || self.listView.annotationType == .circle || self.listView.annotationType == .line) { // 切换
  456. self.listView.annotationType = .arrow
  457. self.rightSideViewController.annotationProperties.annotationMode = .arrow
  458. } else {
  459. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  460. }
  461. }
  462. }
  463. @IBAction func menuItemAction_line(_ sender: Any) {
  464. UserDefaults.standard.set(4, forKey: KMToolBarToolPDFShapeStyle)
  465. if (self.listView.annotationType == .line) { // 取消
  466. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  467. } else { // 选中
  468. if (self.listView.annotationType == .square || self.listView.annotationType == .circle || self.listView.annotationType == .arrow) { // 切换
  469. self.listView.annotationType = .line
  470. self.rightSideViewController.annotationProperties.annotationMode = .line
  471. } else {
  472. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  473. }
  474. }
  475. }
  476. // link
  477. @IBAction func menuItemAction_link(_ sender: Any) {
  478. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  479. }
  480. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  481. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  482. }
  483. @IBAction func menuItemAction_linkHttps(_ sender: Any) {
  484. }
  485. @IBAction func menuItemAction_linkEmail(_ sender: Any) {
  486. }
  487. // stamp
  488. @IBAction func menuItemAction_stamp(_ sender: Any) {
  489. self.mainMenuUpdateAnnotationStyle(identifier: KMAnnotationStampToolbarItemIdentifier)
  490. }
  491. @IBAction func menuItemAction_stampStandard(_ sender: Any) {
  492. }
  493. @IBAction func menuItemAction_stampDynamic(_ sender: Any) {
  494. }
  495. @IBAction func menuItemAction_stampCustom(_ sender: Any) {
  496. }
  497. @IBAction func menuItemAction_signure(_ sender: Any) {
  498. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarSignSignatureAnnotationItemIdentifier)
  499. }
  500. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  501. self.showOrHideNotes()
  502. }
  503. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  504. self.removeAllAnnotations()
  505. }
  506. private func mainMenuUpdateAnnotationStyle(identifier : String) {
  507. if self.toolbarController.toolbarType != .Annatiton {
  508. let item : KMToolbarItemView = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentAnnotationToolbarItemIdentifier))!
  509. self.toolbarController.mainToolBarView?.delegate?.toolbarViewController?(self.toolbarController.mainToolBarView!, clickMode: .Annatiton, toolbar: item, [])
  510. }
  511. let childitem : KMToolbarItemView = (self.toolbarController.childToolBarView?.toolbarItemFindItemIdentifiers(value: identifier))!
  512. self.toolbarController.mainToolBarView?.delegate?.changeAnnotationModeAction?(item: childitem.clickButton)
  513. }
  514. }
  515. // MARK: - KMSystemPDFMenuProtocol
  516. extension KMMainViewController: KMSystemPDFMenuProtocol {
  517. func changeDisplayMode(_ sender: Any?) {
  518. guard let item = sender as? NSMenuItem else {
  519. NSSound.beep()
  520. return
  521. }
  522. let mode = item.tag
  523. if mode == 0 {
  524. self.selectDisplay(display: .singlePage)
  525. } else if mode == 1 {
  526. self.selectDisplay(display: .singlePageContinuous)
  527. } else if mode == 2 {
  528. self.selectDisplay(display: .twoUp)
  529. } else if mode == 3 {
  530. self.selectDisplay(display: .twoUpContinuous)
  531. }
  532. }
  533. func toggleDisplayAsBook(_ sender: Any?) {
  534. self.selectDisplay(display: .bookMode)
  535. }
  536. func toggleDisplayPageBreaks(_ sender: Any?) {
  537. self.listView?.displaysPageBreaks = !(self.listView?.displaysPageBreaks ?? false)
  538. self.listView.layoutDocumentView()
  539. }
  540. func changeDisplayBox(_ sender: Any?) {
  541. guard let item = sender as? NSMenuItem else {
  542. NSSound.beep()
  543. return
  544. }
  545. let mode = item.tag
  546. if mode == 0 {
  547. self.listView.displayBox = .mediaBox
  548. } else if mode == 1 {
  549. self.listView.displayBox = .cropBox
  550. }
  551. }
  552. func toggleAutoScale(_ sender: Any?) {
  553. // if ([self interactionMode] == SKPresentationMode)
  554. // [pdfView toggleAutoActualSize:sender];
  555. // else
  556. // [pdfView setAutoScales:[pdfView autoScales] == NO];
  557. self.listView.autoScales = !self.listView.autoScales
  558. }
  559. func doZoomToActualSize(_ sender: Any?) {
  560. self.selectZoom(.actualSize)
  561. }
  562. func doZoomToFit(_ sender: Any?) {
  563. self.selectZoom(.fit)
  564. }
  565. func doZoomIn(_ sender: Any?) {
  566. if (self.listView.canZoomIn) {
  567. self.listView.zoomIn(nil)
  568. }
  569. }
  570. func doZoomOut(_ sender: Any?) {
  571. if (self.listView.canZoomOut) {
  572. self.listView.zoomOut(nil)
  573. }
  574. }
  575. func doMarqueeZoomTool(_ sender: Any?) {
  576. let isSelected = self.toolbarController.findItem(KMToolbarZoomToSelectionItemIdentifier)?.isSelected ?? false
  577. if isSelected == false {
  578. self.toolbarController.clickItem(KMToolbarZoomToSelectionItemIdentifier)
  579. }
  580. }
  581. func highlightFormFiled(_ sender: Any?) {
  582. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  583. CPDFKitConfig.sharedInstance().setEnableFormFieldHighlight(!enabled)
  584. self.listView.setNeedsDisplayForVisiblePages()
  585. }
  586. func highlightLinks(_ sender: Any?) {
  587. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  588. CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(!enabled)
  589. self.listView.setNeedsDisplayForVisiblePages()
  590. }
  591. func resetForm(_ sender: Any?) {
  592. self.listView.resetFormAnnotation()
  593. }
  594. func removeAllAnnotation(_ sender: Any?) {
  595. self.listView.removeAllAnnotation()
  596. }
  597. func savePDFSettingToDefaults(_ sender: Any?) {
  598. // if ([self interactionMode] == SKNormalMode)
  599. // [[NSUserDefaults standardUserDefaults] setObject:[self currentPDFSettings] forKey:SKDefaultPDFDisplaySettingsKey];
  600. // else if ([self interactionMode] != SKPresentationMode)
  601. // [[NSUserDefaults standardUserDefaults] setObject:[self currentPDFSettings] forKey:SKDefaultFullScreenPDFDisplaySettingsKey];
  602. KMPrint("savePDFSettingToDefaults")
  603. }
  604. }
  605. // MARK: goto Menu
  606. extension KMMainViewController {
  607. @IBAction func menuItemAction_nextPage(_ sender: Any) {
  608. if (self.listView.canGoToNextPage()) {
  609. self.listView.goToNextPage(nil)
  610. }
  611. }
  612. @IBAction func menuItemAction_forwardPage(_ sender: Any) {
  613. if (self.listView.canGoToPreviousPage()) {
  614. self.listView.goToPreviousPage(nil)
  615. }
  616. }
  617. @IBAction func menuItemAction_firstPage(_ sender: Any) {
  618. if (self.listView.canGoToFirstPage()) {
  619. self.listView.goToFirstPage(nil)
  620. }
  621. }
  622. @IBAction func menuItemAction_lastPage(_ sender: Any) {
  623. if (self.listView.canGoToLastPage()) {
  624. self.listView.goToLastPage(nil)
  625. }
  626. }
  627. @IBAction func menuItemAction_forward(_ sender: Any) {
  628. if (self.listView.km_canGoForward()) {
  629. self.listView.km_goForward(nil)
  630. }
  631. }
  632. @IBAction func menuItemAction_goback(_ sender: Any) {
  633. if (self.listView.km_canGoBack()) {
  634. self.listView.km_goBack(nil)
  635. }
  636. }
  637. @IBAction func menuItemAction_gotoPage(_ sender: Any) {
  638. let sheet = KMTextFieldSheetController.init(windowNibName: "PageSheet")
  639. var pages : [String] = []
  640. for i in 0 ..< self.listView.document.pageCount {
  641. pages.append("\(i)")
  642. }
  643. sheet.callback = { [weak self] stringValue in
  644. if (stringValue == nil) {
  645. return
  646. }
  647. guard let index = Int(stringValue) else {
  648. return
  649. }
  650. if (self?.listView == nil) {
  651. return
  652. }
  653. if (index > 0 && index <= self!.listView.document.pageCount) {
  654. self?.listView.go(toPageIndex: index-1, animated: true)
  655. }
  656. }
  657. sheet.showWindow(nil)
  658. sheet.pageBox.addItems(withObjectValues: pages)
  659. sheet.stringValue = "\(self.listView.currentPageIndex+1)"
  660. }
  661. @IBAction func goToMarkedPage(_ sender: Any?) {
  662. guard let pdfDoc = self.listView.document, pdfDoc.isLocked == false && pdfDoc.pageCount > 0 else {
  663. NSSound.beep()
  664. return
  665. }
  666. guard let currentPageIndex = self.listView.currentPage()?.pageIndex() else {
  667. NSSound.beep()
  668. return
  669. }
  670. // if (markedPageIndex == NSNotFound || [pdfDoc isLocked] || [pdfDoc pageCount] == 0) {
  671. // NSBeep();
  672. // } else if (beforeMarkedPageIndex != NSNotFound) {
  673. if self.beforeMarkedPageIndex != NSNotFound {
  674. self.listView.goToPage(at: min(UInt(self.beforeMarkedPageIndex), pdfDoc.pageCount-1), point: self.beforeMarkedPagePoint)
  675. } else if (currentPageIndex != self.markedPageIndex) {
  676. // NSUInteger lastPageIndex = [pdfView currentPageIndexAndPoint:&beforeMarkedPagePoint rotated:NULL];
  677. let lastPageIndex = self.listView.currentPageIndexAndPoint(&self.beforeMarkedPagePoint, rotated: nil)
  678. self.listView.goToPage(at: min(UInt(self.markedPageIndex), pdfDoc.pageCount-1), point: self.markedPagePoint)
  679. self.beforeMarkedPageIndex = Int(lastPageIndex)
  680. }
  681. }
  682. @IBAction func markPage(_ sender: Any?) {
  683. self.markedPageIndex = Int(self.listView.currentPageIndexAndPoint(&self.markedPagePoint, rotated: nil))
  684. self.beforeMarkedPageIndex = NSNotFound
  685. }
  686. }
  687. // MARK: tool Menu
  688. extension KMMainViewController: KMSystemToolMenuProtocol {
  689. func getInfo(_ sender: Any?) {
  690. KMInfoWindowController.shared.showWindow(sender)
  691. }
  692. @IBAction func menuItemAction_textTool(_ sender: Any) {
  693. self.toolbarController.selectItem(KMToolbarZoomToSelectionItemIdentifier)
  694. }
  695. @IBAction func menuItemAction_scrolTool(_ sender: Any) {
  696. self.toolbarController.selectItem(KMToolbarMoveToolModeItemIdentifier)
  697. }
  698. @IBAction func menuItemAction_zoomOutTool(_ sender: Any) {
  699. self.toolbarController.selectItem(KMToolbarMagnifyToolModeItemIdentifier)
  700. }
  701. @IBAction func menuItemAction_selectTool(_ sender: Any) {
  702. self.toolbarController.selectItem(KMToolbarSelectToolModeItemIdentifier)
  703. }
  704. func changeToolMode(_ sender: Any?) {
  705. let isSelected = self.toolbarController.findItem(KMToolbarHighlightAnnotationItemIdentifier)?.isSelected ?? false
  706. if isSelected == false {
  707. self.toolbarController.selectItem(KMToolbarHighlightAnnotationItemIdentifier)
  708. }
  709. }
  710. @IBAction func menuItemAction_redact(_ sender: Any) {
  711. self.toolbarController.clickItem(KMDocumentRedactToolbarItemIdentifier)
  712. }
  713. @IBAction func menuItemAction_warkmark(_ sender: Any) {
  714. self.toolbarController.delegate?.clickChildTool?(type: .watermark, index: 0)
  715. }
  716. @IBAction func menuItemAction_background(_ sender: Any) {
  717. self.toolbarController.delegate?.clickChildTool?(type: .background, index: 0)
  718. }
  719. @IBAction func menuItemAction_headerfooter(_ sender: Any) {
  720. self.toolbarController.delegate?.clickChildTool?(type: .headerAndFooter, index: 0)
  721. }
  722. @IBAction func menuItemAction_bates(_ sender: Any) {
  723. self.toolbarController.delegate?.clickChildTool?(type: .bates, index: 0)
  724. }
  725. @IBAction func menuItemAction_batch(_ sender: Any) {
  726. // KMBatchWindowController.openFile(self.listView.document?.documentURL, .Batch)
  727. }
  728. func compression(_ sender: Any?) {
  729. self.toolbarController.delegate?.clickChildTool?(type: .compress, index: 0)
  730. }
  731. func merge(_ sender: Any?) {
  732. self.toolbarController.delegate?.clickChildTool?(type: .merge, index: 0)
  733. }
  734. func convert(_ sender: Any?) {
  735. self.toolbarController.delegate?.clickChildTool?(type: .word, index: 0)
  736. }
  737. func imageToPDF(_ sender: Any?) {
  738. KMApp.ShowImageToPDFWindow()
  739. }
  740. func ocr(_ sender: Any?) {
  741. self.toolbarController.clickItem(KMDocumentScanOCRToolbarItemIdentifier)
  742. }
  743. func extractImage(_ sender: Any?) {
  744. self.extractImageAction(num: 1)
  745. }
  746. func pageEdit(_ sender: Any?) {
  747. self.toolbarController.clickItem(KMDocumentPageToolbarItemIdentifier)
  748. }
  749. func textEditAction(_ sender: Any?) {
  750. self.toolbarController.clickItem(KMDocumentEditToolbarItemIdentifier)
  751. }
  752. func security(_ sender: Any?) {
  753. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 1)
  754. }
  755. func decryption(_ sender: Any?) {
  756. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 2)
  757. }
  758. func cropAll(_ sender: Any?) {
  759. // NSRect rect[2] = {NSIntegralRect([pdfView currentSelectionRect]), NSZeroRect};
  760. // NSPointerArray *rectArray = [NSPointerArray rectPointerArray];
  761. // BOOL emptySelection = NSIsEmptyRect(rect[0]);
  762. //
  763. // if (emptySelection) {
  764. // NSInteger i, j, count = [[pdfView document] pageCount];
  765. // rect[0] = rect[1] = NSZeroRect;
  766. //
  767. // if (count == 0)
  768. // return;
  769. //
  770. // [self beginProgressSheetWithMessage:[NSLocalizedString(@"Cropping Pages", @"Message for progress sheet") stringByAppendingEllipsis] maxValue:MIN(18, count)];
  771. //
  772. // if (count == 1) {
  773. // rect[0] = [[[pdfView document] pageAtIndex:0] foregroundBox];
  774. // [self incrementProgressSheet];
  775. // } else if (count < 19) {
  776. // for (i = 0; i < count; i++) {
  777. // rect[i % 2] = NSUnionRect(rect[i % 2], [[[pdfView document] pageAtIndex:i] foregroundBox]);
  778. // [self incrementProgressSheet];
  779. // }
  780. // } else {
  781. // NSInteger start[3] = {1, (count - 5) / 2, count - 6};
  782. // for (j = 0; j < 3; j++) {
  783. // for (i = start[j]; i < start[j] + 6; i++) {
  784. // rect[i % 2] = NSUnionRect(rect[i % 2], [[[pdfView document] pageAtIndex:i] foregroundBox]);
  785. // [self incrementProgressSheet];
  786. // }
  787. // }
  788. // }
  789. // CGFloat w = fmax(NSWidth(rect[0]), NSWidth(rect[1]));
  790. // CGFloat h = fmax(NSHeight(rect[0]), NSHeight(rect[1]));
  791. // for (j = 0; j < 2; j++)
  792. // rect[j] = NSMakeRect(floor(NSMidX(rect[j]) - 0.5 * w), floor(NSMidY(rect[j]) - 0.5 * h), w, h);
  793. // [rectArray addPointer:rect];
  794. // [rectArray addPointer:rect + 1];
  795. // } else {
  796. // [rectArray addPointer:rect];
  797. // }
  798. //
  799. // [self cropPagesToRects:rectArray];
  800. // [pdfView setCurrentSelectionRect:NSZeroRect];
  801. self.cropAllPage()
  802. }
  803. func crop(_ sender: Any?) {
  804. self.cropCurrentPage()
  805. }
  806. func autoCropAll(_ sender: Any?) {
  807. #warning("暂时这么处理,后续调整")
  808. self.cropAllPage()
  809. }
  810. func smartAutoCropAll(_ sender: Any?) {
  811. #warning("暂时这么处理,后续调整")
  812. self.cropAllPage()
  813. }
  814. func cropOptionsModel(_ sender: Any?) {
  815. KMPrint("cropOptionsModel ...")
  816. }
  817. func selectToolModel(_ sender: Any?) {
  818. self.toolbarController.clickItem(KMToolbarSelectToolModeItemIdentifier)
  819. }
  820. func takeSnapshot(_ sender: Any?) {
  821. self.listView.takeSnapshot(sender)
  822. }
  823. func takeSnapshotSelectContent(_ sender: Any?) {
  824. self.listView.takeSnapshotSelectContent(sender)
  825. }
  826. func changeAnnotationMode(_ sender: Any?) {
  827. if let data = sender as? NSMenuItem {
  828. self.changeAnnotationMode_itemAction(sender: data)
  829. }
  830. }
  831. func addWatermark(_ sender: Any?) {
  832. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  833. if item == nil {
  834. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  835. }
  836. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  837. }
  838. func removeWatermark(_ sender: Any?) {
  839. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  840. if item == nil {
  841. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  842. }
  843. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  844. }
  845. func batchAddWatermark(_ sender: Any?) {
  846. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  847. if item == nil {
  848. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  849. }
  850. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  851. }
  852. func batchRemoveWatermark(_ sender: Any?) {
  853. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  854. if item == nil {
  855. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  856. }
  857. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  858. }
  859. func templateWatermark(_ sender: Any?) {
  860. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  861. if item == nil {
  862. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  863. }
  864. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  865. }
  866. func addBackground(_ sender: Any?) {
  867. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  868. if item == nil {
  869. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  870. }
  871. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  872. }
  873. func removeBackground(_ sender: Any?) {
  874. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  875. if item == nil {
  876. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  877. }
  878. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  879. }
  880. func batchAddBackground(_ sender: Any?) {
  881. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  882. if item == nil {
  883. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  884. }
  885. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  886. }
  887. func batchRemoveBackground(_ sender: Any?) {
  888. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  889. if item == nil {
  890. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  891. }
  892. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil) }
  893. func templateBackground(_ sender: Any?) {
  894. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  895. if item == nil {
  896. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  897. }
  898. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  899. }
  900. func addHeaderFooter(_ sender: Any?) {
  901. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  902. if item == nil {
  903. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  904. }
  905. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  906. }
  907. func removeHeaderFooter(_ sender: Any?) {
  908. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  909. if item == nil {
  910. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  911. }
  912. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  913. }
  914. func batchAddHeaderFooter(_ sender: Any?) {
  915. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  916. if item == nil {
  917. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  918. }
  919. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  920. }
  921. func batchRemoveHeaderFooter(_ sender: Any?) {
  922. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  923. if item == nil {
  924. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  925. }
  926. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  927. }
  928. func templateHeaderFooter(_ sender: Any?) {
  929. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  930. if item == nil {
  931. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  932. }
  933. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  934. }
  935. func addHeaderFooterBates(_ sender: Any?) {
  936. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  937. if item == nil {
  938. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  939. }
  940. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  941. }
  942. func removeHeaderFooterBates(_ sender: Any?) {
  943. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  944. if item == nil {
  945. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  946. }
  947. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  948. }
  949. func batchAddBates(_ sender: Any?) {
  950. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  951. if item == nil {
  952. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  953. }
  954. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  955. }
  956. func batchRemoveBates(_ sender: Any?) {
  957. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  958. if item == nil {
  959. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  960. }
  961. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  962. }
  963. func templateBates(_ sender: Any?) {
  964. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  965. if item == nil {
  966. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  967. }
  968. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  969. }
  970. func togglePoster(_ sender: Any?) {
  971. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .poster)
  972. }
  973. func toggleMultiple(_ sender: Any?) {
  974. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .multipage)
  975. }
  976. func toggleBooklet(_ sender: Any?) {
  977. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
  978. }
  979. func rotateLeft(_ sender: Any?) {
  980. Task { @MainActor in
  981. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  982. let rotation = page.rotation
  983. page.leftRotate()
  984. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  985. }
  986. }
  987. func rotateRight(_ sender: Any?) {
  988. Task { @MainActor in
  989. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  990. let rotation = page.rotation
  991. page.rightRotate()
  992. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  993. }
  994. }
  995. func rotateAllLeft(_ sender: Any?) {
  996. Task { @MainActor in
  997. var pageItems: [KMThumbnailPageItem] = []
  998. for index in 0 ... self.listView.document.pageCount - 1 {
  999. let page: CPDFPage = self.listView.document.page(at: index)
  1000. let rotation = page.rotation
  1001. page.leftRotate()
  1002. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  1003. }
  1004. self.rotatePageItems(pageItems: pageItems)
  1005. }
  1006. }
  1007. func rotateAllRight(_ sender: Any?) {
  1008. Task { @MainActor in
  1009. var pageItems: [KMThumbnailPageItem] = []
  1010. for index in 0 ... self.listView.document.pageCount - 1 {
  1011. let page: CPDFPage = self.listView.document.page(at: index)
  1012. let rotation = page.rotation
  1013. page.leftRotate()
  1014. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  1015. }
  1016. self.rotatePageItems(pageItems: pageItems)
  1017. }
  1018. }
  1019. func autoSelectContent(_ sender: Any?) {
  1020. self.listView.autoSelectContent(sender)
  1021. }
  1022. }
  1023. // MARK: window Menu
  1024. extension KMMainViewController {
  1025. @IBAction func menuItemAction_showForwardTagPage(_ sender: Any) {
  1026. (self.myDocument as? KMMainDocument)?.browser.selectPreviousTab()
  1027. }
  1028. @IBAction func menuItemAction_showNextTagPage(_ sender: Any) {
  1029. (self.myDocument as? KMMainDocument)?.browser.selectNextTab()
  1030. }
  1031. @IBAction func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  1032. self.browserWindowController?.openNewWindow(sender)
  1033. }
  1034. @IBAction func menuItemAction_mergeAllWindow(_ sender: Any) {
  1035. ((self.myDocument as? KMMainDocument)?.browser.windowController as? KMBrowserWindowController)?.mergeAllWindow(sender)
  1036. }
  1037. @IBAction func menuItemAction_currentWindowName(_ sender: Any) {
  1038. }
  1039. }
  1040. extension KMMainViewController: NSMenuItemValidation, NSMenuDelegate {
  1041. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  1042. if (menuItem.action == #selector(menuItemAction_currentWindowName)) {
  1043. menuItem.title = (self.myDocument?.fileURL!.lastPathComponent)!
  1044. return true
  1045. }
  1046. if (menuItem.action == #selector(menuItemAction_thumai) ||
  1047. menuItem.action == #selector(menuItemAction_outline) ||
  1048. menuItem.action == #selector(menuItemAction_bookmark) ||
  1049. menuItem.action == #selector(menuItemAction_annotation) ||
  1050. menuItem.action == #selector(menuItemAction_search)) {
  1051. menuItem.state = .off
  1052. let type = self.leftSideViewController.type.methodType
  1053. if (menuItem.action == #selector(menuItemAction_thumai) && type == .Thumbnail) {
  1054. menuItem.state = .on
  1055. }
  1056. if (menuItem.action == #selector(menuItemAction_outline) && type == .Outline) {
  1057. menuItem.state = .on
  1058. }
  1059. if (menuItem.action == #selector(menuItemAction_bookmark) && type == .Annotation) {
  1060. menuItem.state = .on
  1061. }
  1062. if (menuItem.action == #selector(menuItemAction_annotation) && type == .Annotation) {
  1063. menuItem.state = .on
  1064. }
  1065. if (menuItem.action == #selector(menuItemAction_search) && type == .Search) {
  1066. menuItem.state = .on
  1067. }
  1068. }
  1069. if (menuItem.action == #selector(menuItemAction_hiddenLeftSide)) {
  1070. if (self.leftPanelOpen) {
  1071. menuItem.title = NSLocalizedString("Hide Left Side Panel", comment: "")
  1072. } else {
  1073. menuItem.title = NSLocalizedString("Show Left Side Panel", comment: "")
  1074. }
  1075. }
  1076. if (menuItem.action == #selector(menuItemAction_hiddenRightSide)) {
  1077. if (self.rightPanelIsOpen) {
  1078. menuItem.title = NSLocalizedString("Hide Right Side Panel", comment: "")
  1079. } else {
  1080. menuItem.title = NSLocalizedString("Show Right Side Panel", comment: "")
  1081. }
  1082. }
  1083. if (menuItem.action == #selector(menuItemAction_hiddenAllAnnotation)) {
  1084. if (self.listView.hideNotes) {
  1085. menuItem.title = NSLocalizedString("Show Notes", comment: "")
  1086. } else {
  1087. menuItem.title = NSLocalizedString("Hide Notes", comment: "")
  1088. }
  1089. }
  1090. if (menuItem.action == #selector(menuItemAction_showForwardTagPage) ||
  1091. menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  1092. let browser = self.browserWindowController?.browser
  1093. if (browser != nil && (browser is KMBrowser)) {
  1094. if (menuItem.action == #selector(menuItemAction_showForwardTagPage)) {
  1095. return (browser as! KMBrowser).canSelectPreviousTab()
  1096. }
  1097. if (menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  1098. return (browser as! KMBrowser).canSelectNextTab()
  1099. }
  1100. }
  1101. }
  1102. if (menuItem.action == #selector(menuItemAction_mergeAllWindow)) {
  1103. if let _browserWindowC = ((self.myDocument as? KMMainDocument)?.browser.windowController as? KMBrowserWindowController) {
  1104. return _browserWindowC.canMergeAllWindow()
  1105. }
  1106. }
  1107. if (menuItem.action == #selector(menuItemAction_nextPage) ||
  1108. menuItem.action == #selector(menuItemAction_forwardPage) ||
  1109. menuItem.action == #selector(menuItemAction_firstPage) ||
  1110. menuItem.action == #selector(menuItemAction_lastPage) ||
  1111. menuItem.action == #selector(menuItemAction_forward) ||
  1112. menuItem.action == #selector(menuItemAction_goback)) {
  1113. if (menuItem.action == #selector(menuItemAction_nextPage)) {
  1114. if self.listView.isEditing() && self.listView.isEditable() {
  1115. return false
  1116. }
  1117. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage())
  1118. }
  1119. if (menuItem.action == #selector(menuItemAction_forwardPage)) {
  1120. if self.listView.isEditing() && self.listView.isEditable() {
  1121. return false
  1122. }
  1123. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage())
  1124. }
  1125. if (menuItem.action == #selector(menuItemAction_firstPage)) {
  1126. return self.listView.canGoToFirstPage()
  1127. }
  1128. if (menuItem.action == #selector(menuItemAction_lastPage)) {
  1129. return self.listView.canGoToLastPage()
  1130. }
  1131. if (menuItem.action == #selector(menuItemAction_forward)) {
  1132. return self.listView.km_canGoForward()
  1133. }
  1134. if (menuItem.action == #selector(menuItemAction_goback)) {
  1135. return self.listView.km_canGoBack()
  1136. }
  1137. }
  1138. if menuItem.action == #selector(changeAnnotationMode) || menuItem.action == #selector(changeAnnotationMode_itemAction) {
  1139. // if ([[menuItem menu] numberOfItems] > ANNOTATION_MODE_COUNT)
  1140. // [menuItem setState:[self toolMode] == SKNoteToolMode && [self annotationMode] == (SKNoteType)[menuItem tag] ? NSOnState : NSOffState];
  1141. // else
  1142. // [menuItem setState:[self annotationMode] == (SKNoteType)[menuItem tag] ? NSOnState : NSOffState];
  1143. if self.listView.annotationType.rawValue == menuItem.tag {
  1144. menuItem.state = .on
  1145. } else {
  1146. menuItem.state = .off
  1147. }
  1148. return true
  1149. }
  1150. guard let action = menuItem.action else {
  1151. return false
  1152. }
  1153. if KMSystemMenu.isToolSelector(sel: action) {
  1154. if action == KMSystemMenu.Tool.autoSelectContentSelector {
  1155. let toolMode = self.listView?.toolMode ?? .none
  1156. return toolMode == .selectToolMode
  1157. }
  1158. }
  1159. if KMSystemMenu.isViewSelector(sel: action) {
  1160. if action == KMSystemMenu.View.toggleLeftSidePaneSelector {
  1161. if self.leftPanelOpen {
  1162. menuItem.title = NSLocalizedString("Hide Panel", comment: "Menu item title")
  1163. } else {
  1164. menuItem.title = NSLocalizedString("Show Panel", comment: "Menu item title")
  1165. }
  1166. return true
  1167. } else if action == KMSystemMenu.View.changeLeftSidePaneStateSelector {
  1168. if menuItem.tag == 3 { // 快照
  1169. menuItem.state = self.leftSideViewController.type.methodType == .snapshot ? .on : .off
  1170. }
  1171. return true
  1172. } else if action == KMSystemMenu.View.themesColorSelector {
  1173. let mode = menuItem.tag
  1174. if mode == 0 {
  1175. menuItem.state = self.listView.displayMode == .normal ? .on : .off
  1176. } else if mode == 1 {
  1177. menuItem.state = self.listView.displayMode == .soft ? .on : .off
  1178. } else if mode == 2 {
  1179. menuItem.state = self.listView.displayMode == .night ? .on : .off
  1180. } else if mode == 3 {
  1181. menuItem.state = self.listView.displayMode == .green ? .on : .off
  1182. }
  1183. return true
  1184. }
  1185. }
  1186. return true
  1187. }
  1188. }