KMMainViewController+MenuAction.swift 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. //
  2. // KMMainViewController+MenuAction.swift
  3. // Cisdem PDFMaster
  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. self.km_beginSheet(windowC: presentationSheetController)
  270. presentationSheetController.closeCallBack = {[weak self] vc in
  271. self?.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. if self.digitalSignController?.pdfView != nil {
  535. if (self.digitalSignController?.pdfView.canZoomIn) == true {
  536. self.digitalSignController?.pdfView.zoomIn(nil)
  537. }
  538. }
  539. if self.redactController?.redactPdfView != nil {
  540. if (self.redactController?.redactPdfView.canZoomIn) == true {
  541. self.redactController?.redactPdfView.zoomIn(nil)
  542. }
  543. }
  544. }
  545. func doZoomOut(_ sender: Any?) {
  546. if (self.listView.canZoomOut) {
  547. self.listView.zoomOut(nil)
  548. }
  549. if self.digitalSignController?.pdfView != nil {
  550. if (self.digitalSignController?.pdfView.canZoomOut) == true {
  551. self.digitalSignController?.pdfView.zoomOut(nil)
  552. }
  553. }
  554. if self.redactController?.redactPdfView != nil {
  555. if (self.redactController?.redactPdfView.canZoomOut) == true {
  556. self.redactController?.redactPdfView.zoomOut(nil)
  557. }
  558. }
  559. }
  560. func doMarqueeZoomTool(_ sender: Any?) {
  561. let isSelected = self.toolbarController.findItem(KMToolbarZoomToSelectionItemIdentifier)?.isSelected ?? false
  562. if isSelected == false {
  563. self.toolbarController.clickItem(KMToolbarZoomToSelectionItemIdentifier)
  564. }
  565. }
  566. func highlightFormFiled(_ sender: Any?) {
  567. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  568. CPDFKitConfig.sharedInstance().setEnableFormFieldHighlight(!enabled)
  569. self.listView.setNeedsDisplayForVisiblePages()
  570. }
  571. func highlightLinks(_ sender: Any?) {
  572. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  573. CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(!enabled)
  574. self.listView.setNeedsDisplayForVisiblePages()
  575. }
  576. func resetForm(_ sender: Any?) {
  577. self.listView.resetFormAnnotation()
  578. }
  579. func removeAllAnnotation(_ sender: Any?) {
  580. self.listView.removeAllAnnotation()
  581. }
  582. func savePDFSettingToDefaults(_ sender: Any?) {
  583. if self.interactionMode == .normal {
  584. UserDefaults.standard.sync_setValue(self.currentPDFSettings(), forKey: KMDefaultPDFDisplaySettingsKey)
  585. } else if self.interactionMode != .presentation {
  586. UserDefaults.standard.sync_setValue(self.currentPDFSettings(), forKey: KMDefaultFullScreenPDFDisplaySettingsKey)
  587. }
  588. if listView.displaysAsBook && self.listView.displayTwoUp {
  589. if listView.fetchDisplayViewMode() == .twoUp {
  590. KMPreferenceManager.shared.viewPageDisplayType = .bookMode
  591. } else {
  592. KMPreferenceManager.shared.viewPageDisplayType = .bookContinuous
  593. }
  594. } else {
  595. KMPreferenceManager.shared.viewPageDisplayType = KMPDFDisplayType(rawValue: listView.fetchDisplayViewMode().rawValue) ?? .singlePage
  596. }
  597. self.savePageNumberIfNeed()
  598. }
  599. }
  600. // MARK: - goto Menu
  601. extension KMMainViewController {
  602. @IBAction func menuItemAction_nextPage(_ sender: Any) {
  603. if (self.listView.canGoToNextPage()) {
  604. self.listView.goToNextPage(nil)
  605. }
  606. }
  607. @IBAction func menuItemAction_forwardPage(_ sender: Any) {
  608. if (self.listView.canGoToPreviousPage()) {
  609. self.listView.goToPreviousPage(nil)
  610. }
  611. }
  612. @IBAction func menuItemAction_firstPage(_ sender: Any) {
  613. if (self.listView.canGoToFirstPage()) {
  614. self.listView.goToFirstPage(nil)
  615. }
  616. }
  617. @IBAction func menuItemAction_lastPage(_ sender: Any) {
  618. if (self.listView.canGoToLastPage()) {
  619. self.listView.goToLastPage(nil)
  620. }
  621. }
  622. @IBAction func menuItemAction_forward(_ sender: Any) {
  623. if (self.listView.km_canGoForward()) {
  624. self.listView.km_goForward(nil)
  625. }
  626. }
  627. @IBAction func menuItemAction_goback(_ sender: Any) {
  628. if (self.listView.km_canGoBack()) {
  629. self.listView.km_goBack(nil)
  630. }
  631. }
  632. @IBAction func menuItemAction_gotoPage(_ sender: Any) {
  633. let sheet = KMTextFieldSheetController.init(windowNibName: "PageSheet")
  634. var pages : [String] = []
  635. for i in 0 ..< self.listView.document.pageCount {
  636. pages.append("\(i)")
  637. }
  638. sheet.callback = { [weak self] stringValue in
  639. guard let index = Int(stringValue) else {
  640. return
  641. }
  642. if (self?.listView == nil) {
  643. return
  644. }
  645. if (index > 0 && index <= self!.listView.document.pageCount) {
  646. self?.listView.go(toPageIndex: index-1, animated: true)
  647. }
  648. }
  649. sheet.showWindow(nil)
  650. sheet.pageBox.addItems(withObjectValues: pages)
  651. sheet.stringValue = "\(self.listView.currentPageIndex+1)"
  652. }
  653. @IBAction func goToMarkedPage(_ sender: Any?) {
  654. guard let pdfDoc = self.listView.document, pdfDoc.isLocked == false && pdfDoc.pageCount > 0 else {
  655. NSSound.beep()
  656. return
  657. }
  658. guard let currentPageIndex = self.listView.currentPage()?.pageIndex() else {
  659. NSSound.beep()
  660. return
  661. }
  662. if self.model.beforeMarkedPageIndex != NSNotFound {
  663. self.listView.goToPage(at: min(UInt(self.model.beforeMarkedPageIndex), pdfDoc.pageCount-1), point: self.model.beforeMarkedPagePoint)
  664. } else if (currentPageIndex != self.model.markedPageIndex) {
  665. // NSUInteger lastPageIndex = [pdfView currentPageIndexAndPoint:&beforeMarkedPagePoint rotated:NULL];
  666. let lastPageIndex = self.listView.currentPageIndexAndPoint(&self.model.beforeMarkedPagePoint, rotated: nil)
  667. self.listView.goToPage(at: min(UInt(self.model.markedPageIndex), pdfDoc.pageCount-1), point: self.model.markedPagePoint)
  668. self.model.beforeMarkedPageIndex = Int(lastPageIndex)
  669. }
  670. }
  671. @IBAction func markPage(_ sender: Any?) {
  672. self.model.markedPageIndex = Int(self.listView.currentPageIndexAndPoint(&self.model.markedPagePoint, rotated: nil))
  673. self.model.beforeMarkedPageIndex = NSNotFound
  674. }
  675. }
  676. // MARK: - tool Menu
  677. extension KMMainViewController: KMSystemToolMenuProtocol {
  678. func getInfo(_ sender: Any?) {
  679. KMInfoWindowController.shared.showWindow(sender)
  680. }
  681. @IBAction func menuItemAction_textTool(_ sender: Any) {
  682. self.listView?.toolMode = .textToolMode
  683. self.listView?.annotationType = .unkown
  684. }
  685. @IBAction func menuItemAction_scrolTool(_ sender: Any) {
  686. self.toolbarController.selectItem(KMToolbarMoveToolModeItemIdentifier)
  687. }
  688. @IBAction func menuItemAction_zoomOutTool(_ sender: Any) {
  689. self.toolbarController.selectItem(KMToolbarMagnifyToolModeItemIdentifier)
  690. }
  691. @IBAction func menuItemAction_selectTool(_ sender: Any) {
  692. self.toolbarController.selectItem(KMToolbarSelectToolModeItemIdentifier)
  693. }
  694. func changeToolMode(_ sender: Any?) {
  695. let value = KMDataManager.ud_integer(forKey: SKLastAnnotationModeKey)
  696. var aType: CAnnotationType = .highlight
  697. if let data = CAnnotationType(rawValue: value) {
  698. aType = data
  699. }
  700. if aType.isMarkup() || aType.isSquare() || aType == .freeText || aType == .anchored {
  701. } else {
  702. aType = .highlight
  703. }
  704. var itemId: String = KMToolbarHighlightAnnotationItemIdentifier
  705. if aType == .highlight {
  706. } else if aType == .underline {
  707. itemId = KMToolbarUnderlineAnnotationItemIdentifier
  708. } else if aType == .strikeOut {
  709. itemId = KMToolbarStrikeOutAnnotationItemIdentifier
  710. } else if aType == .ink {
  711. itemId = KMToolbarInkAnnotationItemIdentifier
  712. } else if aType == .freeText {
  713. itemId = KMToolbarFreeTextAnnotationItemIdentifier
  714. } else if aType == .anchored {
  715. itemId = KMToolbarAnchoredAnnotationItemIdentifier
  716. } else if aType == .square {
  717. itemId = KMToolbarSquareAnnotationItemIdentifier
  718. } else if aType == .circle {
  719. itemId = KMToolbarCircleAnnotationItemIdentifier
  720. } else if aType == .line {
  721. itemId = KMToolbarLineAnnotationItemIdentifier
  722. } else if aType == .arrow {
  723. itemId = KMToolbarArrowAnnotationItemIdentifier
  724. } else if aType == .link {
  725. itemId = KMToolbarLinkAnnotationItemIdentifier
  726. }
  727. let isSelected = self.toolbarController.findItem(itemId)?.isSelected ?? false
  728. if isSelected == false {
  729. self.toolbarController.selectItem(itemId)
  730. }
  731. }
  732. @IBAction func menuItemAction_redact(_ sender: Any) {
  733. self.toolbarController.clickItem(KMDocumentRedactToolbarItemIdentifier)
  734. }
  735. @IBAction func menuItemAction_warkmark(_ sender: Any) {
  736. self.toolbarController.delegate?.clickChildTool?(type: .watermark, index: 0)
  737. }
  738. @IBAction func menuItemAction_background(_ sender: Any) {
  739. self.toolbarController.delegate?.clickChildTool?(type: .background, index: 0)
  740. }
  741. @IBAction func menuItemAction_headerfooter(_ sender: Any) {
  742. self.toolbarController.delegate?.clickChildTool?(type: .headerAndFooter, index: 0)
  743. }
  744. @IBAction func menuItemAction_bates(_ sender: Any) {
  745. self.toolbarController.delegate?.clickChildTool?(type: .bates, index: 0)
  746. }
  747. @IBAction func menuItemAction_batch(_ sender: Any) {
  748. // KMBatchWindowController.openFile(self.listView.document?.documentURL, .Batch)
  749. }
  750. func compression(_ sender: Any?) {
  751. self.toolbarController.delegate?.clickChildTool?(type: .compress, index: 0)
  752. }
  753. func merge(_ sender: Any?) {
  754. self.toolbarController.delegate?.clickChildTool?(type: .merge, index: 0)
  755. }
  756. func convert(_ sender: Any?) {
  757. self.toolbarController.delegate?.clickChildTool?(type: .word, index: 0)
  758. }
  759. func imageToPDF(_ sender: Any?) {
  760. KMApp.ShowImageToPDFWindow()
  761. }
  762. func ocr(_ sender: Any?) {
  763. self.toolbarController.clickItem(KMDocumentScanOCRToolbarItemIdentifier)
  764. }
  765. func extractImage(_ sender: Any?) {
  766. self.extractImageAction(num: 1)
  767. }
  768. func pageEdit(_ sender: Any?) {
  769. self.toolbarController.clickItem(KMDocumentPageToolbarItemIdentifier)
  770. }
  771. func textEditAction(_ sender: Any?) {
  772. self.toolbarController.clickItem(KMDocumentEditToolbarItemIdentifier)
  773. }
  774. func security(_ sender: Any?) {
  775. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 1)
  776. }
  777. func decryption(_ sender: Any?) {
  778. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 2)
  779. }
  780. func cropAll(_ sender: Any?) {
  781. self.cropAllPage()
  782. }
  783. func crop(_ sender: Any?) {
  784. self.cropCurrentPage()
  785. }
  786. // 白边距
  787. func autoCropAll(_ sender: Any?) {
  788. self.smart_cropAllPage()
  789. }
  790. // 自动
  791. func smartAutoCropAll(_ sender: Any?) {
  792. self.auto_cropAllPage()
  793. }
  794. func cropOptionsModel(_ sender: Any?) {
  795. KMPrint("cropOptionsModel ...")
  796. }
  797. func selectToolModel(_ sender: Any?) {
  798. self.toolbarController.clickItem(KMToolbarSelectToolModeItemIdentifier)
  799. }
  800. func takeSnapshot(_ sender: Any?) {
  801. self.listView.takeSnapshot(sender)
  802. }
  803. func takeSnapshotSelectContent(_ sender: Any?) {
  804. self.listView.takeSnapshotSelectContent(sender)
  805. }
  806. func changeAnnotationMode(_ sender: Any?) {
  807. if let data = sender as? NSMenuItem {
  808. self.changeAnnotationMode_itemAction(sender: data)
  809. }
  810. }
  811. func addWatermark(_ sender: Any?) {
  812. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  813. if item == nil {
  814. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  815. }
  816. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  817. }
  818. func removeWatermark(_ sender: Any?) {
  819. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  820. if item == nil {
  821. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  822. }
  823. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  824. }
  825. func batchAddWatermark(_ sender: Any?) {
  826. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  827. if item == nil {
  828. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  829. }
  830. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  831. }
  832. func batchRemoveWatermark(_ sender: Any?) {
  833. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  834. if item == nil {
  835. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  836. }
  837. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  838. }
  839. func templateWatermark(_ sender: Any?) {
  840. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  841. if item == nil {
  842. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  843. }
  844. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  845. }
  846. func addBackground(_ sender: Any?) {
  847. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  848. if item == nil {
  849. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  850. }
  851. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  852. }
  853. func removeBackground(_ sender: Any?) {
  854. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  855. if item == nil {
  856. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  857. }
  858. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  859. }
  860. func batchAddBackground(_ sender: Any?) {
  861. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  862. if item == nil {
  863. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  864. }
  865. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  866. }
  867. func batchRemoveBackground(_ sender: Any?) {
  868. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  869. if item == nil {
  870. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  871. }
  872. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil) }
  873. func templateBackground(_ 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: 5, info: nil)
  879. }
  880. func addHeaderFooter(_ sender: Any?) {
  881. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  882. if item == nil {
  883. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  884. }
  885. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  886. }
  887. func removeHeaderFooter(_ sender: Any?) {
  888. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  889. if item == nil {
  890. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  891. }
  892. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  893. }
  894. func batchAddHeaderFooter(_ sender: Any?) {
  895. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  896. if item == nil {
  897. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  898. }
  899. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  900. }
  901. func batchRemoveHeaderFooter(_ sender: Any?) {
  902. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  903. if item == nil {
  904. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  905. }
  906. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  907. }
  908. func templateHeaderFooter(_ sender: Any?) {
  909. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  910. if item == nil {
  911. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  912. }
  913. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  914. }
  915. func addHeaderFooterBates(_ sender: Any?) {
  916. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  917. if item == nil {
  918. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  919. }
  920. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  921. }
  922. func removeHeaderFooterBates(_ sender: Any?) {
  923. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  924. if item == nil {
  925. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  926. }
  927. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  928. }
  929. func batchAddBates(_ sender: Any?) {
  930. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  931. if item == nil {
  932. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  933. }
  934. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  935. }
  936. func batchRemoveBates(_ sender: Any?) {
  937. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  938. if item == nil {
  939. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  940. }
  941. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  942. }
  943. func templateBates(_ sender: Any?) {
  944. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  945. if item == nil {
  946. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  947. }
  948. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  949. }
  950. func togglePoster(_ sender: Any?) {
  951. self.saveDocument()
  952. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .poster)
  953. }
  954. func toggleMultiple(_ sender: Any?) {
  955. self.saveDocument()
  956. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .multipage)
  957. }
  958. func toggleBooklet(_ sender: Any?) {
  959. self.saveDocument()
  960. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
  961. }
  962. func rotateLeft(_ sender: Any?) {
  963. if IAPProductsManager.default().isAvailableAllFunction() == false {
  964. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  965. return
  966. }
  967. Task { @MainActor in
  968. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  969. let rotation = page.rotation
  970. page.leftRotate()
  971. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  972. self.handlePageBoundsDidChange(page: page, action: CPDFPage.Action.rotate)
  973. }
  974. }
  975. func rotateRight(_ sender: Any?) {
  976. if IAPProductsManager.default().isAvailableAllFunction() == false {
  977. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  978. return
  979. }
  980. Task { @MainActor in
  981. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  982. let rotation = page.rotation
  983. page.rightRotate()
  984. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  985. self.handlePageBoundsDidChange(page: page, action: CPDFPage.Action.rotate)
  986. }
  987. }
  988. func rotateAllLeft(_ sender: Any?) {
  989. if IAPProductsManager.default().isAvailableAllFunction() == false {
  990. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  991. return
  992. }
  993. Task { @MainActor in
  994. var pageItems: [KMThumbnailPageItem] = []
  995. for index in 0 ... self.listView.document.pageCount - 1 {
  996. let page: CPDFPage = self.listView.document.page(at: index)
  997. let rotation = page.rotation
  998. page.leftRotate()
  999. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  1000. }
  1001. self.rotatePageItems(pageItems: pageItems)
  1002. self.handlePageBoundsDidChange(page: nil, action: CPDFPage.Action.rotate)
  1003. }
  1004. }
  1005. func rotateAllRight(_ sender: Any?) {
  1006. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1007. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  1008. return
  1009. }
  1010. Task { @MainActor in
  1011. var pageItems: [KMThumbnailPageItem] = []
  1012. for index in 0 ... self.listView.document.pageCount - 1 {
  1013. let page: CPDFPage = self.listView.document.page(at: index)
  1014. let rotation = page.rotation
  1015. page.rightRotate()
  1016. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  1017. }
  1018. self.rotatePageItems(pageItems: pageItems)
  1019. self.handlePageBoundsDidChange(page: nil, action: CPDFPage.Action.rotate)
  1020. }
  1021. }
  1022. func autoSelectContent(_ sender: Any?) {
  1023. self.listView.autoSelectContent(sender)
  1024. }
  1025. }
  1026. // MARK: - window Menu
  1027. extension KMMainViewController {
  1028. @IBAction func menuItemAction_showForwardTagPage(_ sender: Any) {
  1029. (self.myDocument as? KMMainDocument)?.browser.selectPreviousTab()
  1030. }
  1031. @IBAction func menuItemAction_showNextTagPage(_ sender: Any) {
  1032. (self.myDocument as? KMMainDocument)?.browser.selectNextTab()
  1033. }
  1034. @IBAction func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  1035. self.browserWindowController?.openNewWindow(sender)
  1036. }
  1037. @IBAction func menuItemAction_mergeAllWindow(_ sender: Any) {
  1038. ((self.myDocument as? KMMainDocument)?.browser.windowController as? KMBrowserWindowController)?.mergeAllWindow(sender)
  1039. }
  1040. @IBAction func menuItemAction_currentWindowName(_ sender: Any) {
  1041. }
  1042. @IBAction func performFit(_ sender: Any?) {
  1043. if self.interactionMode != .normal {
  1044. NSSound.beep()
  1045. return
  1046. }
  1047. let displayMode = self.listView.fetchDisplayViewMode()
  1048. let scaleFactor = self.listView.scaleFactor
  1049. let autoScales = self.listView.autoScales
  1050. var isSingleRow = false
  1051. if displayMode == .singlePage || displayMode == .twoUp {
  1052. isSingleRow = true
  1053. } else if displayMode == .singlePageContinuous || self.listView.displaysAsBook {
  1054. isSingleRow = (self.listView?.document?.pageCount ?? 0) <= 1
  1055. } else {
  1056. isSingleRow = (self.listView?.document?.pageCount ?? 0) <= 2
  1057. }
  1058. var frame = self.view.window?.frame ?? .zero
  1059. var size: NSSize = .zero
  1060. let oldSize = self.listView.frame.size
  1061. let documentView = self.listView.documentView()
  1062. let documentRect = documentView?.convert(documentView?.bounds ?? .zero, to: nil) ?? .zero
  1063. let page = self.listView.currentPage()
  1064. let box = self.listView.displayBox
  1065. let PAGE_BREAK_MARGIN: CGFloat = 8.0
  1066. let margin = self.listView.displaysPageBreaks ? PAGE_BREAK_MARGIN : 0.0
  1067. // Calculate the new size for the pdfView
  1068. size.width = NSWidth(documentRect)
  1069. if (autoScales) {
  1070. size.width /= scaleFactor
  1071. }
  1072. if (isSingleRow) {
  1073. size.height = NSHeight(documentRect)
  1074. } else {
  1075. let rect = self.listView.convert(page?.bounds(for: box) ?? .zero, from: page)
  1076. size.height = NSHeight(rect) + margin * scaleFactor
  1077. // if ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)] == NO || [NSScroller preferredScrollerStyle] == NSScrollerStyleLegacy)
  1078. // size.width += [NSScroller scrollerWidth];
  1079. if NSScroller.responds(to: NSSelectorFromString("preferredScrollerStyle")) == false {
  1080. let w = NSScroller.scrollerWidth(for: .regular, scrollerStyle: .legacy)
  1081. size.width += w
  1082. } else {
  1083. if NSScroller.preferredScrollerStyle == .legacy {
  1084. let w = NSScroller.scrollerWidth(for: .regular, scrollerStyle: .legacy)
  1085. size.width += w
  1086. }
  1087. }
  1088. }
  1089. if (autoScales) {
  1090. size.height /= scaleFactor
  1091. }
  1092. // Calculate the new size for the window
  1093. size.width = ceil(NSWidth(frame) + size.width - oldSize.width)
  1094. size.height = ceil(NSHeight(frame) + size.height - oldSize.height)
  1095. // Align the window frame from the old topleft point and constrain to the screen
  1096. frame.origin.y = NSMaxY(frame) - size.height
  1097. frame.size = size
  1098. if let screen = self.view.window?.screen ?? NSScreen.main {
  1099. frame = self.view.window?.constrainFrameRect(frame, to: screen) ?? .zero
  1100. }
  1101. self.view.window?.setFrame(frame, display: self.view.window?.isVisible ?? false)
  1102. if displayMode == .singlePageContinuous || displayMode == .twoUpContinuous {
  1103. self.listView.go(to: NSInsetRect(page?.bounds(for: box) ?? .zero, -0.5 * margin, -0.5 * margin), on: page)
  1104. }
  1105. }
  1106. }
  1107. extension KMMainViewController {
  1108. override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  1109. if (menuItem.action == #selector(menuItemAction_currentWindowName)) {
  1110. menuItem.title = (self.myDocument?.fileURL?.lastPathComponent) ?? ""
  1111. return true
  1112. }
  1113. if (menuItem.action == #selector(menuItemAction_thumai) ||
  1114. menuItem.action == #selector(menuItemAction_outline) ||
  1115. menuItem.action == #selector(menuItemAction_bookmark) ||
  1116. menuItem.action == #selector(menuItemAction_annotation) ||
  1117. menuItem.action == #selector(menuItemAction_search)) {
  1118. menuItem.state = .off
  1119. let type = self.leftSideViewController.type.methodType
  1120. if (menuItem.action == #selector(menuItemAction_thumai) && type == .Thumbnail) {
  1121. menuItem.state = .on
  1122. }
  1123. if (menuItem.action == #selector(menuItemAction_outline) && type == .Outline) {
  1124. menuItem.state = .on
  1125. }
  1126. if (menuItem.action == #selector(menuItemAction_bookmark) && type == .Annotation) {
  1127. menuItem.state = .on
  1128. }
  1129. if (menuItem.action == #selector(menuItemAction_annotation) && type == .Annotation) {
  1130. menuItem.state = .on
  1131. }
  1132. if (menuItem.action == #selector(menuItemAction_search) && type == .Search) {
  1133. menuItem.state = .on
  1134. }
  1135. }
  1136. if (menuItem.action == #selector(menuItemAction_hiddenLeftSide)) {
  1137. if (self.model.leftPanelOpen) {
  1138. menuItem.title = NSLocalizedString("Hide Left Side Panel", comment: "")
  1139. } else {
  1140. menuItem.title = NSLocalizedString("Show Left Side Panel", comment: "")
  1141. }
  1142. }
  1143. if (menuItem.action == #selector(menuItemAction_hiddenRightSide)) {
  1144. if (self.model.rightPanelIsOpen) {
  1145. menuItem.title = NSLocalizedString("Hide Right Side Panel", comment: "")
  1146. } else {
  1147. menuItem.title = NSLocalizedString("Show Right Side Panel", comment: "")
  1148. }
  1149. }
  1150. if (menuItem.action == #selector(menuItemAction_hiddenAllAnnotation)) {
  1151. if (self.listView.hideNotes) {
  1152. menuItem.title = NSLocalizedString("Show Notes", comment: "")
  1153. } else {
  1154. menuItem.title = NSLocalizedString("Hide Notes", comment: "")
  1155. }
  1156. }
  1157. if (menuItem.action == #selector(menuItemAction_showForwardTagPage) ||
  1158. menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  1159. let browser = self.browserWindowController?.browser
  1160. if (browser != nil && (browser is KMBrowser)) {
  1161. if (menuItem.action == #selector(menuItemAction_showForwardTagPage)) {
  1162. return (browser as! KMBrowser).canSelectPreviousTab()
  1163. }
  1164. if (menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  1165. return (browser as! KMBrowser).canSelectNextTab()
  1166. }
  1167. }
  1168. }
  1169. if (menuItem.action == #selector(menuItemAction_mergeAllWindow)) {
  1170. if let _browserWindowC = ((self.myDocument as? KMMainDocument)?.browser.windowController as? KMBrowserWindowController) {
  1171. return _browserWindowC.canMergeAllWindow()
  1172. }
  1173. }
  1174. if (menuItem.action == #selector(menuItemAction_nextPage) ||
  1175. menuItem.action == #selector(menuItemAction_forwardPage) ||
  1176. menuItem.action == #selector(menuItemAction_firstPage) ||
  1177. menuItem.action == #selector(menuItemAction_lastPage) ||
  1178. menuItem.action == #selector(menuItemAction_forward) ||
  1179. menuItem.action == #selector(menuItemAction_goback)) {
  1180. if self.hasEnterPageEdit() {
  1181. return false
  1182. }
  1183. if (menuItem.action == #selector(menuItemAction_nextPage)) {
  1184. if self.listView.isEditing() && self.listView.isEditable() {
  1185. return false
  1186. }
  1187. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage())
  1188. }
  1189. if (menuItem.action == #selector(menuItemAction_forwardPage)) {
  1190. if self.listView.isEditing() && self.listView.isEditable() {
  1191. return false
  1192. }
  1193. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage())
  1194. }
  1195. if (menuItem.action == #selector(menuItemAction_firstPage)) {
  1196. return self.listView.canGoToFirstPage()
  1197. }
  1198. if (menuItem.action == #selector(menuItemAction_lastPage)) {
  1199. return self.listView.canGoToLastPage()
  1200. }
  1201. if (menuItem.action == #selector(menuItemAction_forward)) {
  1202. return self.listView.km_canGoForward()
  1203. }
  1204. if (menuItem.action == #selector(menuItemAction_goback)) {
  1205. return self.listView.km_canGoBack()
  1206. }
  1207. }
  1208. if menuItem.action == #selector(changeAnnotationMode) || menuItem.action == #selector(changeAnnotationMode_itemAction) {
  1209. // if ([[menuItem menu] numberOfItems] > ANNOTATION_MODE_COUNT)
  1210. // [menuItem setState:[self toolMode] == SKNoteToolMode && [self annotationMode] == (SKNoteType)[menuItem tag] ? NSOnState : NSOffState];
  1211. // else
  1212. // [menuItem setState:[self annotationMode] == (SKNoteType)[menuItem tag] ? NSOnState : NSOffState];
  1213. if self.listView.annotationType.rawValue == menuItem.tag {
  1214. menuItem.state = .on
  1215. } else {
  1216. menuItem.state = .off
  1217. }
  1218. return true
  1219. }
  1220. if menuItem.action == #selector(menuItemAction_print) {
  1221. if self.listView.document.permissionsStatus == .user && !self.listView.document.allowsPrinting {
  1222. return false
  1223. }
  1224. }
  1225. if menuItem.action == #selector(highlightFormFiled) {
  1226. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  1227. menuItem.state = enabled ? .on : .off
  1228. return true
  1229. }
  1230. if menuItem.action == #selector(highlightLinks) {
  1231. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  1232. menuItem.state = enabled ? .on : .off
  1233. return true
  1234. }
  1235. guard let action = menuItem.action else {
  1236. return false
  1237. }
  1238. if KMSystemMenu.isToolSelector(sel: action) {
  1239. if action == KMSystemMenu.Tool.autoSelectContentSelector {
  1240. let toolMode = self.listView?.toolMode ?? .none
  1241. return toolMode == .selectToolMode
  1242. } else if action == KMSystemMenu.Tool.textToolSelector {
  1243. let toolMode = self.listView?.toolMode ?? .textToolMode
  1244. menuItem.state = toolMode == .textToolMode ? .on : .off
  1245. return true
  1246. } else if action == KMSystemMenu.Tool.scrolToolSelector {
  1247. let toolMode = self.listView?.toolMode ?? .textToolMode
  1248. menuItem.state = toolMode == .moveToolMode ? .on : .off
  1249. return true
  1250. } else if action == KMSystemMenu.Tool.zoomToolSelector {
  1251. let toolMode = self.listView?.toolMode ?? .textToolMode
  1252. menuItem.state = toolMode == .magnifyToolMode ? .on : .off
  1253. return true
  1254. } else if action == KMSystemMenu.Tool.selectToolSelector {
  1255. let toolMode = self.listView?.toolMode ?? .textToolMode
  1256. menuItem.state = toolMode == .selectToolMode ? .on : .off
  1257. return true
  1258. } else if action == KMSystemMenu.Tool.changeToolModeSelector {
  1259. let aType = self.listView?.annotationType ?? .unkown
  1260. if aType.isMarkup() || aType == .freeText || aType == .anchored || aType.isSquare() || aType == .link {
  1261. menuItem.state = .on
  1262. } else {
  1263. menuItem.state = .off
  1264. }
  1265. return true
  1266. }
  1267. }
  1268. if KMSystemMenu.isViewSelector(sel: action) {
  1269. if action == KMSystemMenu.View.toggleLeftSidePaneSelector {
  1270. if self.model.leftPanelOpen {
  1271. menuItem.title = NSLocalizedString("Hide Panel", comment: "Menu item title")
  1272. } else {
  1273. menuItem.title = NSLocalizedString("Show Panel", comment: "Menu item title")
  1274. }
  1275. return true
  1276. } else if action == KMSystemMenu.View.changeLeftSidePaneStateSelector {
  1277. if menuItem.tag == 3 { // 快照
  1278. menuItem.state = self.leftSideViewController.type.methodType == .snapshot ? .on : .off
  1279. }
  1280. return true
  1281. } else if action == KMSystemMenu.View.themesColorSelector {
  1282. let mode = menuItem.tag
  1283. var viewMode: KMPDFViewMode = .normal
  1284. if let storedViewMode = UserDefaults.standard.value(forKey: "kPDFViewModeKey") as? Int {
  1285. viewMode = KMPDFViewMode(rawValue: UInt(storedViewMode)) ?? .normal
  1286. }
  1287. if mode == 0 {
  1288. menuItem.state = viewMode == .normal ? .on : .off
  1289. } else if mode == 1 {
  1290. menuItem.state = viewMode == .soft ? .on : .off
  1291. } else if mode == 2 {
  1292. menuItem.state = viewMode == .night ? .on : .off
  1293. } else if mode == 3 {
  1294. menuItem.state = viewMode == .green ? .on : .off
  1295. } else if mode == 4 {
  1296. menuItem.state = viewMode == .themes1 ? .on : .off
  1297. } else if mode == 5 {
  1298. menuItem.state = viewMode == .themes2 ? .on : .off
  1299. } else if mode == 6 {
  1300. menuItem.state = viewMode == .themes3 ? .on : .off
  1301. } else if mode == 7 {
  1302. menuItem.state = viewMode == .themes4 ? .on : .off
  1303. }
  1304. var data: Data? = nil
  1305. if let themesData = UserDefaults.standard.object(forKey: "kKMPDFViewModeThemesArray") as? Data {
  1306. data = themesData
  1307. }
  1308. var themesCount = 0
  1309. let appArray = NSKeyedUnarchiver.unarchiveObject(with: data ?? Data()) as? [Any] ?? []
  1310. if appArray.count > 0 {
  1311. themesCount = menuItem.tag - appArray.count
  1312. } else {
  1313. themesCount = menuItem.tag - 4
  1314. }
  1315. if themesCount >= 0 {
  1316. menuItem.isHidden = true
  1317. return false
  1318. }else {
  1319. menuItem.isHidden = false
  1320. return true
  1321. }
  1322. // return true
  1323. } else if action == KMSystemMenu.View.splitViewActionSelector {
  1324. let mode = menuItem.tag
  1325. if mode == 0 { // hor
  1326. menuItem.state = self.listView.viewSplitMode == .horizontal ? .on : .off
  1327. } else if mode == 1 { // verti
  1328. menuItem.state = self.listView.viewSplitMode == .vertical ? .on : .off
  1329. } else if mode == 2 { // single
  1330. menuItem.state = self.listView.viewSplitMode == .disable ? .on : .off
  1331. }
  1332. } else if action == KMSystemMenu.View.toggleToolbarShowSelector {
  1333. menuItem.title = self.toolbarController.mainToolBarisVisable ? KMLocalizedString("Hide Toolbar") : KMLocalizedString("Show Toolbar")
  1334. }
  1335. }
  1336. if KMSystemMenu.isAnnotationSelector(sel: action) {
  1337. if action == KMSystemMenu.Annotation.editNoteSelector {
  1338. if self.interactionMode == .presentation {
  1339. return false
  1340. }
  1341. guard let anno = self.listView?.activeAnnotation else {
  1342. return false
  1343. }
  1344. return anno.isEditable()
  1345. }
  1346. }
  1347. if action == NSSelectorFromString("changeDisplayBox:") {
  1348. if menuItem.tag == 0 {
  1349. menuItem.state = self.listView.displayBox == .mediaBox ? .on : .off
  1350. } else if menuItem.tag == 1 {
  1351. menuItem.state = self.listView.displayBox == .cropBox ? .on : .off
  1352. }
  1353. }
  1354. return true
  1355. }
  1356. }