KMMainViewController+MenuAction.swift 59 KB

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