KMMainViewController+Action.swift 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
  1. //
  2. // KMMainViewController+Action.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Foundation
  8. extension KMMainViewController {
  9. func search(searchString: String, isCase: Bool) {
  10. let document = self.listView.document
  11. if ((document?.isFinding) != nil) {
  12. document?.cancelFindString()
  13. }
  14. if searchString == "" {
  15. self.searchResults = []
  16. self.leftSideViewController.searchViewController.searchResults = self.searchResults
  17. self.leftSideViewController.searchViewController.reloadData()
  18. } else {
  19. mwcFlags.wholeWordSearch = isCase == true ? 1 : 0
  20. var findArray : [[CPDFSelection]]
  21. if isCase {
  22. findArray = self.listView.document.findString(searchString) ?? []
  23. } else {
  24. findArray = self.listView.document.findString(searchString, with: .caseSensitive) ?? []
  25. }
  26. self.searchResults.removeAll()
  27. for selections in findArray {
  28. for selection in selections {
  29. let mode : KMSearchMode = KMSearchMode()
  30. mode.selection = selection
  31. mode.attributedString = KMOCToolClass.getAttributedString(with: selection, keyword: searchString)
  32. mode.selectionPageIndex = self.listView.document.index(for: selection.page)
  33. self.searchResults.insert(mode, at: self.searchResults.count)
  34. }
  35. }
  36. self.leftSideViewController.searchViewController.searchResults = self.searchResults
  37. self.leftSideViewController.searchViewController.reloadData()
  38. }
  39. }
  40. func removeSignatures(signatures:[CPDFSignature]) {
  41. for signature in signatures {
  42. self.listView.document.removeSignature(signature)
  43. }
  44. for i in 0..<self.listView.document.pageCount {
  45. let page : CPDFPage = self.listView.document.page(at: i)
  46. let annotations : [CPDFAnnotation] = page.annotations
  47. for j in 0..<annotations.count {
  48. let annotation = annotations[j]
  49. if annotation is CPDFSignatureWidgetAnnotation {
  50. (annotation as! CPDFSignatureWidgetAnnotation).updateAppearanceStream()
  51. }
  52. }
  53. }
  54. self.listView.setNeedsDisplayForVisiblePages()
  55. let tSignatures : [CPDFSignature] = self.listView.document.signatures()
  56. var mSignatures : [CPDFSignature] = []
  57. for sign in tSignatures {
  58. if sign.signers.count > 0 {
  59. mSignatures.append(sign)
  60. }
  61. }
  62. self.leftSideViewController.signatureViewController.signatures = signatures
  63. self.leftSideViewController.signatureViewController.reloadData()
  64. }
  65. //MARK: menu菜单
  66. func fontColorMenuItem()->NSMenuItem {
  67. let fontColorItem = NSMenuItem(title: NSLocalizedString("Text Color", comment: ""), action: #selector(menuItemEditingClick_FontColor), keyEquivalent: "")
  68. return fontColorItem;
  69. }
  70. func fontSizeMenuItem()->NSMenuItem {
  71. let currentFontSize = self.listView.editingTextFontSize()
  72. let fontSizes = self.fontSizes()
  73. let submenu = NSMenu(title: "")
  74. for i in 0 ... fontSizes.count - 1 {
  75. let fontSize : String = fontSizes.object(at: i) as! String
  76. let item = NSMenuItem(title:fontSize as! String, action: #selector(menuItemEditingClick_FontSize), keyEquivalent: "")
  77. item.target = self
  78. item.tag = i
  79. submenu.addItem(item)
  80. if (Int(currentFontSize) == Int(fontSize)) {
  81. item.state = .on
  82. }
  83. }
  84. let fontSizeItem = NSMenuItem(title: NSLocalizedString("Font Size", comment: ""), action:nil, keyEquivalent: "")
  85. fontSizeItem.submenu = submenu
  86. return fontSizeItem;
  87. }
  88. func corpImageMenuItem()->NSMenuItem {
  89. var corpImageItem = NSMenuItem(title: NSLocalizedString("Sure Crop", comment: ""), action: #selector(menuItemEditingClick_CropImage), target: self)!
  90. return corpImageItem
  91. }
  92. func cancelCorpImageMenuItem()->NSMenuItem {
  93. let cancelCorpImageItem = NSMenuItem(title: NSLocalizedString("NoApplyCrop", comment: ""), action: #selector(menuItemEditingClick_CancelCrop), target: self)!
  94. return cancelCorpImageItem
  95. }
  96. func cutImageArea()->NSMenuItem {
  97. let deleteItem = NSMenuItem(title: NSLocalizedString("cutting", comment: ""), action: #selector(menuItemEditingClick_CutImage), target: self)!
  98. return deleteItem
  99. }
  100. func editAddBlanMenu(menu:NSMenu){
  101. menu.insertItem(withTitle: NSLocalizedString("Add Text", comment: ""), action: #selector(addImageText), target: self, at: 0)
  102. menu.insertItem(withTitle: NSLocalizedString("Add Image", comment: ""), action: #selector(addImageText), target: self, at: 1)
  103. }
  104. func replaceImageArea()->NSMenuItem {
  105. let replaceItem = NSMenuItem(title: NSLocalizedString("Replace...", comment: ""), action: #selector(menuItemEditingClick_ReplaceImage), target: self)!
  106. return replaceItem
  107. }
  108. func exportImageArea()->NSMenuItem {
  109. let exportItem = NSMenuItem(title: NSLocalizedString("Export...", comment: ""), action: #selector(menuItemEditingClick_ExportImage), target: self)!
  110. return exportItem
  111. }
  112. func exportImageStampItem()->NSMenuItem {
  113. let exportItem = NSMenuItem(title: NSLocalizedString("Export...", comment: ""), action: #selector(menuItemEditingClick_ExportImage), target: self)!
  114. let menu = NSMenu()
  115. menu.insertItem(withTitle: NSLocalizedString("PNG", comment: ""), action:#selector(exportStampImage), target: self, tag:0, at: 0)
  116. menu.insertItem(withTitle: NSLocalizedString("PDF", comment: ""), action:#selector(exportStampImage), target: self, tag:2, at: 1)
  117. exportItem.submenu = menu
  118. return exportItem
  119. }
  120. func exportMenu() -> NSMenu {
  121. let menu = NSMenu()
  122. menu.insertItem(withTitle: NSLocalizedString("PNG", comment: ""), action:#selector(exportCorpImage), target: self, tag:0, at: 0)
  123. menu.insertItem(withTitle: NSLocalizedString("JPG", comment: ""), action:#selector(exportCorpImage), target: self, tag:1, at: 1)
  124. menu.insertItem(withTitle: NSLocalizedString("PDF", comment: ""), action:#selector(exportCorpImage), target: self, tag:2, at: 2)
  125. return menu
  126. }
  127. func cropMenu() -> NSMenu {
  128. let menu = NSMenu()
  129. menu.insertItem(withTitle: NSLocalizedString("Crop Current Page", comment: ""), action:#selector(cropCurrentPage), target: self, at: 0)
  130. menu.insertItem(withTitle: NSLocalizedString("Crop All Pages", comment: ""), action:#selector(cropAllPage), target: self, at: 1)
  131. menu.insertItem(withTitle: NSLocalizedString("Auto Crop – Separate", comment: ""), action:#selector(autoCropAll), target: self, at: 2)
  132. menu.insertItem(withTitle: NSLocalizedString("Auto Crop – Combined", comment: ""), action:#selector(autoCropAll), target: self, at: 2)
  133. return menu
  134. }
  135. func zoomSelectionMenuItem() -> NSMenuItem {
  136. let item = NSMenuItem(title: NSLocalizedString("Zoom To Selection", comment: ""), action: #selector(doZoomToAutoSelection), target: self)!
  137. return item
  138. }
  139. func setDefaultAnnotationPorpert(type:CAnnotationType) -> NSMenuItem {
  140. let item = NSMenuItem(title: NSLocalizedString("Set Default Porpert", comment: ""), action: #selector(defaultAnnotationPorpert), target: self, tag: type.rawValue)!
  141. return item
  142. }
  143. func enterAnnotationStype() -> NSMenuItem {
  144. let stypItem = NSMenuItem(title: NSLocalizedString("Add Annotation", comment: ""), action: nil, target: self)!
  145. let stypeMenu = NSMenu()
  146. stypeMenu.addItem(withTitle: NSLocalizedString("Freehand", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 0)
  147. stypeMenu.addItem(withTitle: NSLocalizedString("Text", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 1)
  148. stypeMenu.addItem(withTitle: NSLocalizedString("Note", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 2)
  149. stypeMenu.addItem(withTitle: NSLocalizedString("Rectangle", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 3)
  150. stypeMenu.addItem(withTitle: NSLocalizedString("Circle", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 4)
  151. stypeMenu.addItem(withTitle: NSLocalizedString("Arrow", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 5)
  152. stypeMenu.addItem(withTitle: NSLocalizedString("Line", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 6)
  153. if self.isReadMode {
  154. } else {
  155. stypeMenu.addItem(withTitle: NSLocalizedString("Link", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 7)
  156. stypeMenu.addItem(withTitle: NSLocalizedString("Stamp", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 8)
  157. stypeMenu.addItem(withTitle: NSLocalizedString("Signature", comment: ""), action: #selector(menuItemAnnotationClick_addStype), target: self, tag: 9)
  158. }
  159. stypItem.submenu = stypeMenu
  160. if self.listView.annotationType == .ink {
  161. stypeMenu.item(at: 0)?.state = .on
  162. } else if self.listView.annotationType == .freeText {
  163. stypeMenu.item(at: 1)?.state = .on
  164. } else if self.listView.annotationType == .anchored {
  165. stypeMenu.item(at: 2)?.state = .on
  166. } else if self.listView.annotationType == .square {
  167. stypeMenu.item(at: 3)?.state = .on
  168. } else if self.listView.annotationType == .circle {
  169. stypeMenu.item(at: 4)?.state = .on
  170. } else if self.listView.annotationType == .arrow {
  171. stypeMenu.item(at: 5)?.state = .on
  172. } else if self.listView.annotationType == .line {
  173. stypeMenu.item(at: 6)?.state = .on
  174. } else if self.listView.annotationType == .link {
  175. stypeMenu.item(at: 7)?.state = .on
  176. } else if self.listView.annotationType == .stamp {
  177. stypeMenu.item(at: 8)?.state = .on
  178. } else if self.listView.annotationType == .signSignature {
  179. stypeMenu.item(at: 9)?.state = .on
  180. }
  181. return stypItem
  182. }
  183. func setAnnotationToolStype() -> NSMenuItem {
  184. let stypItem = NSMenuItem(title: NSLocalizedString("Tool Mode", comment: ""), action: nil, target: self)!
  185. let stypeMenu = NSMenu()
  186. stypeMenu.addItem(withTitle: NSLocalizedString("Default Tool", comment: ""), action: #selector(menuItemAnnotationClick_toolModel), target: self, tag: 0)
  187. stypeMenu.addItem(withTitle: NSLocalizedString("Scroll Tool", comment: ""), action: #selector(menuItemAnnotationClick_toolModel), target: self, tag: 1)
  188. stypeMenu.addItem(withTitle: NSLocalizedString("Magnify", comment: ""), action: #selector(menuItemAnnotationClick_toolModel), target: self, tag: 2)
  189. stypeMenu.addItem(withTitle: NSLocalizedString("Select", comment: ""), action: #selector(menuItemAnnotationClick_toolModel), target: self, tag: 3)
  190. stypeMenu.addItem(withTitle: NSLocalizedString("Zoom to Selected Area", comment: ""), action: #selector(menuItemAnnotationClick_toolModel), target: self, tag: 4)
  191. stypItem.submenu = stypeMenu
  192. if self.toolbarController.toolbarType == .Annatiton {
  193. stypeMenu.item(at: 0)?.state = .on
  194. } else if self.toolbarController.toolbarType == .Move {
  195. stypeMenu.item(at: 1)?.state = .on
  196. } else if self.toolbarController.toolbarType == .Magnify {
  197. stypeMenu.item(at: 2)?.state = .on
  198. } else if self.toolbarController.toolbarType == .Select {
  199. stypeMenu.item(at: 3)?.state = .on
  200. } else if self.toolbarController.toolbarType == .SelectZoom {
  201. stypeMenu.item(at: 4)?.state = .on
  202. }
  203. return stypItem
  204. }
  205. func addReadModelStype() -> NSMenuItem {
  206. var stypItem = NSMenuItem(title: NSLocalizedString("Read Mode On", comment: ""), action: #selector(openReadModel), target: self)!
  207. if self.isReadMode {
  208. stypItem = NSMenuItem(title: NSLocalizedString("Read Mode Off", comment: ""), action: #selector(closeReadModel), target: self)!
  209. }
  210. return stypItem
  211. }
  212. func addHighlightLinksStype() -> NSMenuItem {
  213. let highlightLink = KMPreferenceManager.shared.highlightLinks
  214. var highlightLinkTitle = NSLocalizedString("Highlight Links", comment: "")
  215. if highlightLink {
  216. highlightLinkTitle = NSLocalizedString("Disable Highlight Links", comment: "")
  217. }
  218. var highlightLinksItem = NSMenuItem(title: highlightLinkTitle, action: #selector(highlightLinks), target: self)!
  219. return highlightLinksItem
  220. }
  221. func addAnnotationForStyleMenu(menu:NSMenu) {
  222. if menu == nil {
  223. return
  224. }
  225. let height = NSMenuItem(title: NSLocalizedString("Highlight", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 0)!
  226. let underline = NSMenuItem(title: NSLocalizedString("Underline", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 1)!
  227. let strickout = NSMenuItem(title: NSLocalizedString("Strickout", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 2)!
  228. let text = NSMenuItem(title: NSLocalizedString("Text", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 3)!
  229. let note = NSMenuItem(title: NSLocalizedString("Note", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 4)!
  230. let rectangle = NSMenuItem(title: NSLocalizedString("Rectangle", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 5)!
  231. let oval = NSMenuItem(title: NSLocalizedString("Oval", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 6)!
  232. let line = NSMenuItem(title: NSLocalizedString("Line", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 7)!
  233. let link = NSMenuItem(title: NSLocalizedString("Add Link", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 8)!
  234. let outline = NSMenuItem(title: NSLocalizedString("Add Outline", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 9)!
  235. // let tts = NSMenuItem(title: NSLocalizedString("TTS", comment: ""), action: #selector(menuItemAnnotationClick_add), target: self, tag: 10)!
  236. menu.insertItem(NSMenuItem.separator(), at: menu.items.count)
  237. if listView.currentSelection.selectionType() != .image {
  238. menu.insertItem(height, at: menu.items.count)
  239. menu.insertItem(underline, at: menu.items.count)
  240. menu.insertItem(strickout, at: menu.items.count)
  241. menu.insertItem(NSMenuItem.separator(), at: menu.items.count)
  242. }
  243. if self.isReadMode {
  244. } else {
  245. if listView.currentSelection.selectionType() != .image &&
  246. listView.currentSelection.selectionType() != .text {
  247. menu.insertItem(text, at: menu.items.count)
  248. menu.insertItem(note, at: menu.items.count)
  249. }
  250. }
  251. if self.isReadMode {
  252. } else {
  253. if listView.currentSelection.selectionType() != .image {
  254. menu.insertItem(NSMenuItem.separator(), at: menu.items.count)
  255. menu.insertItem(rectangle, at: menu.items.count)
  256. menu.insertItem(oval, at: menu.items.count)
  257. }
  258. }
  259. if self.isReadMode {
  260. } else {
  261. if listView.currentSelection.selectionType() != .image &&
  262. listView.currentSelection.selectionType() != .text {
  263. menu.insertItem(line, at: menu.items.count)
  264. }
  265. }
  266. menu.insertItem(NSMenuItem.separator(), at: menu.items.count)
  267. if self.isReadMode {
  268. } else {
  269. menu.insertItem(link, at: menu.items.count)
  270. }
  271. if self.isReadMode {
  272. } else {
  273. if listView.currentSelection.selectionType() != .image {
  274. menu.insertItem(outline, at: menu.items.count)
  275. }
  276. }
  277. menu.insertItem(NSMenuItem.separator(), at: menu.items.count)
  278. }
  279. func addBookmarkMenu() -> NSMenuItem {
  280. if self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) == nil {
  281. let bookMarkItem = NSMenuItem(title: NSLocalizedString("Add BookMark", comment: ""), action: #selector(menuItemBookMarkClick_add), target: self)!
  282. return bookMarkItem
  283. } else {
  284. let bookMarkItem = NSMenuItem(title: NSLocalizedString("Remove BookMark", comment: ""), action: #selector(menuItemBookMarkClick_add), target: self)!
  285. return bookMarkItem
  286. }
  287. }
  288. func findStringMenu() -> NSMenuItem {
  289. let menuItem = NSMenuItem(title: NSLocalizedString("Find", comment: ""), action: #selector(menuItemAnnotationClick_FindString), target: self)!
  290. menuItem.keyEquivalent = "f"
  291. return menuItem
  292. }
  293. func printingMenu() -> NSMenuItem {
  294. let menuItem = NSMenuItem(title: NSLocalizedString("Print", comment: ""), action: #selector(menuItemAnnotationClick_Print), keyEquivalent: "p")
  295. return menuItem
  296. }
  297. func fontSizes()->NSArray {
  298. return ["6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "24", "36", "48", "72", "96", "144", "288"]
  299. }
  300. //MARK: menuItem Action
  301. @objc func menuItemEditingClick_FontColor(sender: NSMenuItem) {
  302. let color = listView.editingSelectionFontColor()
  303. let panel = NSColorPanel.shared
  304. panel.setTarget(self)
  305. panel.setAction(#selector(fontColorChangeAction))
  306. panel.orderFront(nil)
  307. panel.showsAlpha = false
  308. panel.color = color ?? NSColor.black
  309. }
  310. @objc func fontColorChangeAction(sender: NSColorPanel) {
  311. self.listView.setEditingSelectionFontColor(sender.color)
  312. }
  313. @objc func menuItemEditingClick_FontSize(sender: NSMenuItem) {
  314. let fontSize = self.fontSizes().object(at: sender.tag)
  315. self.listView.setEditingSelectionFontSize(CGFloat(Int(fontSize as! String)!))
  316. }
  317. @objc func addImageText(sender: NSMenuItem) {
  318. }
  319. @objc func menuItemEditingClick_CropImage(sender: NSMenuItem) {
  320. if self.listView.cropAreas != nil && self.listView.selectImageAreas != nil{
  321. self.listView.cropEditImageArea(self.listView.selectImageAreas, withBounds: self.listView.cropAreas.cropRect)
  322. }
  323. }
  324. @objc func menuItemEditingClick_CancelCrop(sender: NSMenuItem) {
  325. if self.rightSideViewController.eidtPDFImageProperty != nil {
  326. self.rightSideViewController.eidtPDFImageProperty?.cancelCutImageAction("")
  327. } else {
  328. self.listView.exitCrop(with: self.listView.selectImageAreas)
  329. self.listView.cropAreas = nil
  330. self.listView.isEditImage = false
  331. }
  332. }
  333. @objc func menuItemEditingClick_CutImage(sender: NSMenuItem) {
  334. if self.listView.selectImageAreas != nil {
  335. self.listView.isEditImage = true
  336. self.listView.enterCrop(with: self.listView.selectImageAreas)
  337. }
  338. }
  339. @objc func menuItemEditingClick_ReplaceImage(sender: NSMenuItem) {
  340. if self.listView.selectImageAreas == nil {
  341. return
  342. }
  343. let panel = NSOpenPanel()
  344. panel.allowsMultipleSelection = false
  345. panel.allowedFileTypes = ["png","jpg"]
  346. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  347. if response == .OK {
  348. let openPath = panel.url?.path
  349. let s = self.listView.replace(self.listView.selectImageAreas, imagePath: openPath!)
  350. if s {
  351. }
  352. }
  353. }
  354. }
  355. @objc func menuItemEditingClick_ExportImage(sender: NSMenuItem) {
  356. if self.listView.selectImageAreas == nil {
  357. return
  358. }
  359. let panel = NSSavePanel()
  360. panel.nameFieldStringValue = "[新文件].png"
  361. let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  362. button.state = .on
  363. panel.accessoryView = button
  364. panel.isExtensionHidden = true
  365. let response = panel.runModal()
  366. if response == .OK {
  367. let url = panel.url
  368. let result = self.listView.extractImage(with: self.listView.selectImageAreas, toImagePath: url!.path)
  369. if result {
  370. if button.state == .on { /// 开启文档
  371. NSWorkspace.shared.openFile(url!.path)
  372. } else {
  373. }
  374. }
  375. }
  376. }
  377. @objc func menuItemAnnotationClick_toolModel(sender: NSMenuItem) {
  378. self.listView.toolMode = .noteToolMode
  379. var identifier = KMDocumentAnnotationToolbarItemIdentifier
  380. var model : KMToolbarViewType = .None
  381. switch sender.tag {
  382. case 0:
  383. identifier = KMDocumentAnnotationToolbarItemIdentifier
  384. model = .Annatiton
  385. case 1:
  386. identifier = KMToolbarMoveToolModeItemIdentifier
  387. model = .Move
  388. case 2:
  389. identifier = KMToolbarMagnifyToolModeItemIdentifier
  390. model = .Magnify
  391. case 3:
  392. identifier = KMToolbarSelectToolModeItemIdentifier
  393. model = .Select
  394. case 4:
  395. identifier = KMToolbarZoomToSelectionItemIdentifier
  396. model = .SelectZoom
  397. default:
  398. break
  399. }
  400. let item : KMToolBoxItem = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: identifier))!
  401. self.toolbarController.mainToolBarView?.delegate?.toolbarViewController?(self.toolbarController.mainToolBarView!, clickMode:model, toolbar: item, [])
  402. }
  403. @objc func menuItemAnnotationClick_add(sender : NSMenuItem) {
  404. var annotationType : CAnnotationType = .unkown
  405. switch sender.tag {
  406. case 0:
  407. annotationType = .highlight
  408. case 1:
  409. annotationType = .underline
  410. case 2:
  411. annotationType = .strikeOut
  412. case 3:
  413. annotationType = .freeText
  414. case 4:
  415. annotationType = .anchored
  416. case 5:
  417. annotationType = .square
  418. case 6:
  419. annotationType = .circle
  420. case 7:
  421. annotationType = .line
  422. case 8:
  423. annotationType = .link
  424. case 9:
  425. // var newOutline : CPDFOutline = CPDFOutline()
  426. // newOutline.label = "11111111"
  427. // let des = self.listView.currentDestination
  428. // if "\(des?.point.x ?? 0)" != "nan" {
  429. // newOutline.destination = self.listView.currentDestination
  430. // } else {
  431. // let destination : CPDFDestination = CPDFDestination(document: self.listView.document, pageIndex: self.listView.currentPageIndex, at: CGPoint(x: 0, y: 0), zoom: self.listView.scaleFactor)
  432. // newOutline.destination = destination
  433. // }
  434. // let current = self.listView.document.outlineRoot().insertChild(at: 0) ?? CPDFOutline()
  435. // let parent = current.parent ?? CPDFOutline()
  436. // let index = current.index + 1
  437. // parent.insertChild(newOutline, at: index)
  438. if self.leftPanelOpen == false || self.leftSideViewController.type.methodType != .Outline {
  439. self.leftSideViewController.refreshMethodType(methodType: .Outline)
  440. }
  441. self.leftSideViewController.outlineViewController.addItemAction()
  442. return
  443. case 10:
  444. annotationType = .unkown
  445. default:
  446. break
  447. }
  448. let annotation = self.listView.addAnnotation(with: annotationType, selection: self.listView.currentSelection, page: self.listView.currentSelection.page, bounds: self.listView.currentSelection.bounds)
  449. self.listView.currentSelection = nil;
  450. if (annotationType == .link) {
  451. self.toolbarController.selectItem(KMToolbarLinkAnnotationItemIdentifier)
  452. if (annotation != nil) {
  453. self.listView.updateActiveAnnotations([annotation!])
  454. }
  455. }
  456. }
  457. @objc func menuItemAnnotationClick_addStype(sender: NSMenuItem) {
  458. Task { @MainActor in
  459. if (sender.tag == 7 || sender.tag == 8 || sender.tag == 9) {
  460. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  461. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  462. return
  463. }
  464. }
  465. var point = mouseRightMenuEvent?.locationInWindow
  466. if (point == nil) {
  467. point = NSZeroPoint
  468. }
  469. let currentPoint: NSPoint = self.listView.convert(point!, from: self.listView.superview)
  470. let currentPage = self.listView.page(for: currentPoint, nearest: true)
  471. let pagePoint = self.listView.convert(currentPoint, to: currentPage)
  472. var annotation: CPDFAnnotation?
  473. if self.isReadMode {
  474. if (sender.tag == 0 || sender.tag == 7 || sender.tag == 8 || sender.tag == 9) { // Ink & Link & stamp & sign
  475. self.listView.toolMode = .noteToolMode
  476. }
  477. switch sender.tag {
  478. case 0:
  479. self.listView.annotationType = CAnnotationType.ink
  480. case 1:
  481. // self.listView.annotationType = CAnnotationType.freeText
  482. let defaultSize = self.listView.defaultSize(with: .freeText, in: currentPage)
  483. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  484. annotation = self.listView.addAnnotation(with: .freeText, selection: nil, page: currentPage, bounds: bounds)
  485. if ((annotation) != nil) {
  486. self.listView.updateActiveAnnotations([annotation!])
  487. self.listView.edit(annotation)
  488. }
  489. case 2:
  490. // self.listView.annotationType = CAnnotationType.anchored
  491. let defaultSize = self.listView.defaultSize(with: .anchored, in: currentPage)
  492. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  493. annotation = self.listView.addAnnotation(with: .anchored, selection: nil, page: currentPage, bounds: bounds)
  494. self.listView.edit(annotation)
  495. case 3:
  496. // self.listView.annotationType = CAnnotationType.square
  497. let defaultSize = self.listView.defaultSize(with: .square, in: currentPage)
  498. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  499. annotation = self.listView.addAnnotation(with: .square, selection: nil, page: currentPage, bounds: bounds)
  500. case 4:
  501. // self.listView.annotationType = CAnnotationType.circle
  502. let defaultSize = self.listView.defaultSize(with: .circle, in: currentPage)
  503. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  504. annotation = self.listView.addAnnotation(with: .circle, selection: nil, page: currentPage, bounds: bounds)
  505. case 5:
  506. // self.listView.annotationType = CAnnotationType.arrow
  507. let defaultSize = self.listView.defaultSize(with: .arrow, in: currentPage)
  508. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  509. annotation = self.listView.addAnnotation(with: .arrow, selection: nil, page: currentPage, bounds: bounds)
  510. case 6:
  511. // self.listView.annotationType = CAnnotationType.line
  512. let defaultSize = self.listView.defaultSize(with: .line, in: currentPage)
  513. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  514. annotation = self.listView.addAnnotation(with: .line, selection: nil, page: currentPage, bounds: bounds)
  515. case 7:
  516. self.listView.annotationType = CAnnotationType.link
  517. self.openRightPane()
  518. case 8:
  519. self.listView.annotationType = CAnnotationType.stamp
  520. self.openRightPane()
  521. case 9:
  522. self.listView.annotationType = CAnnotationType.signSignature
  523. self.openRightPane()
  524. default:
  525. break
  526. }
  527. self.rightSideViewController.isHidden = true
  528. self.rightSideViewController.subViewType = .AnnotationProperts
  529. } else {
  530. if (sender.tag == 0 || sender.tag == 7 || sender.tag == 8 || sender.tag == 9) { // Ink & Link & stamp & sign
  531. if(self.toolbarController.toolbarType == .None) {
  532. self.toolbarController.toolbarType = .Annatiton
  533. }
  534. self.listView.toolMode = .noteToolMode
  535. }
  536. switch sender.tag {
  537. case 0:
  538. self.listView.annotationType = CAnnotationType.ink
  539. case 1:
  540. // self.listView.annotationType = CAnnotationType.freeText
  541. let defaultSize = self.listView.defaultSize(with: .freeText, in: currentPage)
  542. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  543. annotation = self.listView.addAnnotation(with: .freeText, selection: nil, page: currentPage, bounds: bounds)
  544. // self.view.window?.makeFirstResponder()
  545. if ((annotation) != nil) {
  546. self.listView.updateActiveAnnotations([annotation!])
  547. self.listView.edit(annotation)
  548. }
  549. case 2:
  550. // self.listView.annotationType = CAnnotationType.anchored
  551. let defaultSize = self.listView.defaultSize(with: .anchored, in: currentPage)
  552. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  553. annotation = self.listView.addAnnotation(with: .anchored, selection: nil, page: currentPage, bounds: bounds)
  554. self.listView.edit(annotation)
  555. case 3:
  556. // self.listView.annotationType = CAnnotationType.square
  557. let defaultSize = self.listView.defaultSize(with: .square, in: currentPage)
  558. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  559. annotation = self.listView.addAnnotation(with: .square, selection: nil, page: currentPage, bounds: bounds)
  560. case 4:
  561. // self.listView.annotationType = CAnnotationType.circle
  562. let defaultSize = self.listView.defaultSize(with: .circle, in: currentPage)
  563. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  564. annotation = self.listView.addAnnotation(with: .circle, selection: nil, page: currentPage, bounds: bounds)
  565. case 5:
  566. // self.listView.annotationType = CAnnotationType.arrow
  567. let defaultSize = self.listView.defaultSize(with: .arrow, in: currentPage)
  568. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  569. annotation = self.listView.addAnnotation(with: .arrow, selection: nil, page: currentPage, bounds: bounds)
  570. case 6:
  571. // self.listView.annotationType = CAnnotationType.line
  572. let defaultSize = self.listView.defaultSize(with: .line, in: currentPage)
  573. let bounds = CPDFListViewRectFromCenterAndSize(CPDFListViewIntegralPoint(pagePoint), defaultSize)
  574. annotation = self.listView.addAnnotation(with: .line, selection: nil, page: currentPage, bounds: bounds)
  575. case 7:
  576. self.listView.annotationType = CAnnotationType.link
  577. self.openRightPane()
  578. case 8:
  579. self.listView.annotationType = CAnnotationType.stamp
  580. self.openRightPane()
  581. case 9:
  582. self.listView.annotationType = CAnnotationType.signSignature
  583. self.openRightPane()
  584. default:
  585. break
  586. }
  587. // self.rightSideViewController.view.isHidden = false
  588. self.rightSideViewController.isHidden = false
  589. // self.openRightPane()
  590. self.rightSideViewController.subViewType = .AnnotationProperts
  591. }
  592. if (annotation != nil) {
  593. self.listView.updateIsRightActiveAnnotations([annotation!])
  594. }
  595. }
  596. }
  597. @objc func menuItemBookMarkClick_add(sender:NSMenuItem) {
  598. if self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) == nil {
  599. let index = self.listView.currentPageIndex
  600. self.listView.document.addBookmark("\(NSLocalizedString("Page", comment: "")) \(index+1)", forPageIndex: UInt(index))
  601. self.listView.setNeedsDisplayForVisiblePages()
  602. } else {
  603. self.listView.document.removeBookmark(forPageIndex: UInt(self.listView.currentPageIndex))
  604. self.listView.setNeedsDisplayForVisiblePages()
  605. }
  606. if self.isReadMode {
  607. } else {
  608. if self.leftPanelOpen == false || self.leftSideViewController.type.methodType != .BookMark {
  609. self.leftSideViewController.refreshMethodType(methodType: .BookMark)
  610. }
  611. let bookMark = self.listView.document.bookmark(forPageIndex: UInt(self.listView.currentPageIndex))
  612. if bookMark != nil {
  613. let item = KMBookMarkItem()
  614. item.bookMark = bookMark!
  615. item.label = (bookMark?.label)!
  616. item.index = UInt(bookMark!.pageIndex)
  617. self.leftSideViewController.bookViewController.reloadData()
  618. self.leftSideViewController.bookViewController.addBookMarkAndEdit(newBookMark: item)
  619. } else {
  620. self.leftSideViewController.bookViewController.reloadData()
  621. }
  622. }
  623. }
  624. @objc func menuItemAnnotationClick_FindString(sender:NSMenuItem) {
  625. self.leftSideViewController.refreshMethodType(methodType: .Search)
  626. }
  627. @objc func menuItemAnnotationClick_Print(sender:NSMenuItem) {
  628. let rect = listView.currentSelectionRect()
  629. let page = listView.currentPage()
  630. let copyPage : CPDFPage = page!.copy() as! CPDFPage
  631. copyPage.setBounds(rect, for: .cropBox)
  632. let image : NSImage = copyPage.thumbnail(of:(copyPage.bounds(for: .cropBox)).size)
  633. // let pdfDocument : PDFDocument = PDFDocument()
  634. // let newpage : PDFPage = PDFPage(image: image)!
  635. // pdfDocument.insert(newpage, at: 0)
  636. // 执行右键操作后,需要取消框选区域
  637. if self.listView.toolMode == .selectToolMode {
  638. objc_sync_enter(self)
  639. self.listView.selectionRect = NSZeroRect
  640. self.listView.selectionPageIndex = UInt(NSNotFound)
  641. objc_sync_exit(self)
  642. }
  643. if (self.listView.document != nil && !self.listView.document.allowsPrinting) { // 有打印限制
  644. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self] result ,password in
  645. if (result == .cancel) {
  646. return
  647. }
  648. // 解除权限
  649. self?.isSaveKeyChain = false
  650. self?.listView.document.unlock(withPassword: password)
  651. // 隐藏提示
  652. self?.hiddenSecureLimitTip()
  653. // 去打印
  654. KMPrintWindowController.printImage(image: image)
  655. }
  656. return
  657. }
  658. KMPrintWindowController.printImage(image: image)
  659. }
  660. // MARK: Redact 【密文标记】
  661. @objc func redact_menuItemClick_delete(sender: NSMenuItem?) {
  662. self.listView.remove(self.listView.activeAnnotation)
  663. }
  664. @objc func redact_menuItemClick_setProperty(sender: NSMenuItem?) {
  665. let windowController = KMRedactPropertyWindowController(windowNibName: "KMRedactBaseWindowController")
  666. windowController.annotation = (self.listView.activeAnnotation as! CPDFRedactAnnotation)
  667. self.view.window?.beginSheet(windowController.window!)
  668. self.currentWindowController = windowController
  669. windowController.itemClick = { [weak self] index, value in
  670. if (index == 1) { /// 取消
  671. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  672. self?.currentWindowController = nil
  673. return
  674. }
  675. let windowController_redact = self?.currentWindowController as! KMRedactPropertyWindowController
  676. let annotaton: CPDFRedactAnnotation = self?.listView.activeAnnotation as! CPDFRedactAnnotation
  677. annotaton.setBorderColor(windowController_redact.outsideColor)
  678. annotaton.setInteriorColor(windowController_redact.fillColor)
  679. if (windowController_redact.isOver) {
  680. annotaton.setFontColor(windowController_redact.fontColor)
  681. annotaton.setAlignment(windowController_redact.aligement)
  682. annotaton.setFont(windowController_redact.font)
  683. annotaton.setOverlayText(windowController_redact.overText)
  684. }
  685. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  686. self?.currentWindowController = nil
  687. }
  688. }
  689. @objc func redact_menuItemClick_setCurrentPropertyToDefaultValue(sender: NSMenuItem?) {
  690. if (self.listView.activeAnnotation == nil || (self.listView.activeAnnotation.isKind(of: CPDFRedactAnnotation.self)) == false) {
  691. return
  692. }
  693. let annotation: CPDFRedactAnnotation = self.listView.activeAnnotation as! CPDFRedactAnnotation
  694. let model = CPDFAnnotationModel(annotationType: .redact)
  695. model?.setColor(annotation.borderColor())
  696. model?.setInteriorColor(annotation.interiorColor())
  697. let overlayText: String = annotation.overlayText()
  698. if (overlayText.isEmpty) {
  699. model?.setIsOverlayText(false)
  700. } else {
  701. model?.setIsOverlayText(true)
  702. model?.setOverlayText(overlayText)
  703. model?.setFontColor(annotation.fontColor())
  704. model?.setAlignment(annotation.alignment())
  705. model?.setFontName(annotation.font().fontName)
  706. model?.setFontSize(annotation.font().pointSize)
  707. }
  708. }
  709. @objc func redact_menuItemClick_MultiPageFlag(sender: NSMenuItem?) {
  710. let anno = self.listView.activeAnnotation
  711. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  712. return
  713. }
  714. let windowController = KMRedactMutilPageFlagWindowController(windowNibName: "KMRedactBaseWindowController")
  715. windowController.pageCount = Int(self.listView.document.pageCount)
  716. self.currentWindowController = windowController
  717. self.view.window?.beginSheet(windowController.window!)
  718. windowController.itemClick = { [weak self] index, value in
  719. if (index == 1) {
  720. self!.view.window?.endSheet(self!.currentWindowController.window!)
  721. self!.currentWindowController = nil
  722. return
  723. }
  724. let windowController_mutilPageFlag = self?.currentWindowController as! KMRedactMutilPageFlagWindowController
  725. let pageType = windowController_mutilPageFlag.pageType
  726. let pageString = windowController_mutilPageFlag.pageString
  727. if (pageType == 4) { /// 自定义页面
  728. let array = KMPageRangeTools.findSelectPage(pageRangeString: pageString, pageCount: Int((self?.listView.document.pageCount)!))
  729. if (array.count == 0) {
  730. let alert = NSAlert()
  731. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  732. alert.runModal()
  733. return
  734. }
  735. }
  736. self!.view.window?.endSheet(self!.currentWindowController.window!)
  737. self!.currentWindowController = nil
  738. let indexs = KMRedactTools.getPageIndexs(pageType, string: pageString, Int((self?.listView.document.pageCount)!))
  739. if (indexs.count == 0) {
  740. return
  741. }
  742. for i in indexs {
  743. let page: CPDFPage = (self?.listView.document.page(at: UInt(i)))!
  744. let redactAnno = KMRedactTools.createRedactAnnotation((self?.listView.document)!, anno as! CPDFRedactAnnotation)
  745. self?.listView.add(redactAnno, to: page)
  746. }
  747. }
  748. }
  749. @objc func redact_menuItemClick_apply(sender: NSMenuItem?) {
  750. self.exeRedactConfirm(.redactOne) {}
  751. }
  752. @objc func redact_menuItemClick_clear(sender: NSMenuItem?) {
  753. self.exeRedactConfirm(.eraserOne) {}
  754. }
  755. @objc func redact_menuItemClick_paste(sender: NSMenuItem?) {
  756. }
  757. @objc func exportStampImage(sender:NSMenuItem) {
  758. if listView.activeAnnotation != nil && ((listView.activeAnnotation is CPDFStampAnnotation) || (listView.activeAnnotation is CPDFSignatureAnnotation)) {
  759. var image : NSImage = NSImage()
  760. if (listView.activeAnnotation is CPDFStampAnnotation) {
  761. image = (listView.activeAnnotation as! CPDFStampAnnotation).stampImage()
  762. } else if (listView.activeAnnotation is CPDFSignatureAnnotation) {
  763. image = (listView.activeAnnotation as! CPDFSignatureAnnotation).signImage
  764. }
  765. let data = image.tiffRepresentation
  766. if sender.tag == 0 {
  767. let imageRep : NSBitmapImageRep = NSBitmapImageRep(data: data!) ?? NSBitmapImageRep()
  768. imageRep.size = image.size
  769. let imageData : Data = imageRep.representation(using: NSBitmapImageRep.FileType.png, properties: [:])!
  770. let savePanel = NSSavePanel()
  771. savePanel.allowedFileTypes = ["png"]
  772. savePanel.beginSheetModal(for: self.view.window!) { response in
  773. if (response != .OK) {
  774. return
  775. }
  776. if NSData(data: imageData).write(to: savePanel.url!, atomically: true) {
  777. NSWorkspace.shared.selectFile(savePanel.url?.path, inFileViewerRootedAtPath: "");
  778. }
  779. }
  780. } else {
  781. let pdfdocument = CPDFDocument()
  782. let signatureImagePath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first?.stringByAppendingPathComponent("signatureImage.png")
  783. if NSData(data: data!).write(to: URL(fileURLWithPath: signatureImagePath!), atomically: true) {
  784. pdfdocument?.insertPage(image.size, withImage: signatureImagePath, at: 0)
  785. let savePanel = NSSavePanel()
  786. savePanel.allowedFileTypes = ["pdf"]
  787. savePanel.beginSheetModal(for: self.view.window!) { response in
  788. if (response != .OK) {
  789. return
  790. }
  791. if pdfdocument!.write(to: savePanel.url!) {
  792. NSWorkspace.shared.selectFile(savePanel.url?.path, inFileViewerRootedAtPath: "");
  793. }
  794. }
  795. }
  796. }
  797. }
  798. }
  799. @objc func exportCorpImage(sender:NSMenuItem) {
  800. let rect = NSIntegralRect(listView.currentSelectionRect())
  801. let orgPage : CPDFPage = listView.currentSelectionPage() ?? listView.currentPage()
  802. let page : CPDFPage = orgPage.copy() as! CPDFPage
  803. page.setBounds(rect, for: .cropBox)
  804. let image = page.thumbnail(of: rect.size) ?? NSImage()
  805. let data = image.tiffRepresentation
  806. let imageRep : NSBitmapImageRep = NSBitmapImageRep(data: data!) ?? NSBitmapImageRep()
  807. imageRep.size = rect.size
  808. let savePanel = NSSavePanel()
  809. switch sender.tag {
  810. case 0:
  811. savePanel.allowedFileTypes = ["png"]
  812. let imageData : Data = imageRep.representation(using: NSBitmapImageRep.FileType.png, properties: [:])!
  813. savePanel.beginSheetModal(for: self.view.window!) { response in
  814. if (response != .OK) {
  815. return
  816. }
  817. if NSData(data: imageData).write(to: savePanel.url!, atomically: true) {
  818. NSWorkspace.shared.selectFile(savePanel.url?.path, inFileViewerRootedAtPath: "");
  819. }
  820. }
  821. case 1:
  822. savePanel.allowedFileTypes = ["jpg"]
  823. let imageData : Data = imageRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])!
  824. savePanel.beginSheetModal(for: self.view.window!) { response in
  825. if (response != .OK) {
  826. return
  827. }
  828. if NSData(data: imageData).write(to: savePanel.url!, atomically: true) {
  829. NSWorkspace.shared.selectFile(savePanel.url?.path, inFileViewerRootedAtPath: "");
  830. }
  831. }
  832. case 2:
  833. savePanel.allowedFileTypes = ["pdf"]
  834. let pdfdocument = CPDFDocument()
  835. let signatureImagePath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first?.stringByAppendingPathComponent("signatureImage.png")
  836. let imageData : Data = imageRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])!
  837. if NSData(data: imageData).write(to: URL(fileURLWithPath: signatureImagePath!), atomically: true) {
  838. pdfdocument?.insertPage(image.size, withImage: signatureImagePath, at: 0)
  839. savePanel.beginSheetModal(for: self.view.window!) { response in
  840. if (response != .OK) {
  841. return
  842. }
  843. if pdfdocument!.write(to: savePanel.url!) {
  844. NSWorkspace.shared.selectFile(savePanel.url?.path, inFileViewerRootedAtPath: "");
  845. }
  846. }
  847. }
  848. default:
  849. break
  850. }
  851. // 执行右键操作后,需要取消框选区域
  852. if self.listView.toolMode == .selectToolMode {
  853. objc_sync_enter(self)
  854. self.listView.selectionRect = NSZeroRect
  855. self.listView.selectionPageIndex = UInt(NSNotFound)
  856. objc_sync_exit(self)
  857. }
  858. }
  859. @IBAction func doZoomToAutoSelection(sender:NSMenuItem) {
  860. let rect = listView.currentSelectionRect()
  861. let page = listView.currentPage()
  862. if NSIsEmptyRect(rect) == false && page != nil {
  863. let isLegacy = NSScroller.responds(to: NSSelectorFromString("preferredScrollerStyle")) == false || NSScroller.preferredScrollerStyle == .legacy
  864. var bounds = listView.bounds
  865. var scale = 1.0
  866. if isLegacy {
  867. bounds.size.width -= NSScroller.scrollerWidth(for: .regular, scrollerStyle: listView.documentView().scrollerStyle)
  868. bounds.size.height -= NSScroller.scrollerWidth(for: .regular, scrollerStyle: listView.documentView().scrollerStyle)
  869. }
  870. if NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds) {
  871. scale = NSHeight(bounds) / NSHeight(rect)
  872. } else {
  873. scale = NSWidth(bounds) / NSWidth(rect)
  874. }
  875. listView.setScaleFactor(scale, animated: false)
  876. let scrollView = listView.scroll()
  877. if isLegacy && scrollView?.hasHorizontalScroller == false || scrollView?.hasVerticalScroller == false {
  878. if ((scrollView?.hasVerticalScroller) != nil) {
  879. bounds.size.width -= NSScroller.scrollerWidth(for: .regular, scrollerStyle: listView.documentView().scrollerStyle)
  880. }
  881. if ((scrollView?.hasHorizontalScroller) != nil) {
  882. bounds.size.height -= NSScroller.scrollerWidth(for: .regular, scrollerStyle: listView.documentView().scrollerStyle)
  883. }
  884. if NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds) {
  885. scale = NSHeight(bounds) / NSHeight(rect)
  886. } else {
  887. scale = NSWidth(bounds) / NSWidth(rect)
  888. }
  889. listView.setScaleFactor(scale, animated: false)
  890. }
  891. DispatchQueue.main.asyncAfter(deadline: .now() + 0.03) { [self] in
  892. let pagePoint = CGPoint(x: rect.origin.x, y: (rect.origin.y + rect.size.height))
  893. listView.go(toTargetPoint: pagePoint, on: page, at: .top)
  894. };
  895. }
  896. // 执行右键操作后,需要取消框选区域
  897. if self.listView.toolMode == .selectToolMode {
  898. objc_sync_enter(self)
  899. self.listView.selectionRect = NSZeroRect
  900. self.listView.selectionPageIndex = UInt(NSNotFound)
  901. objc_sync_exit(self)
  902. }
  903. }
  904. @IBAction func autoCropAll(sender:NSMenuItem) {
  905. }
  906. private func cropPagesToRects(rects:NSPointerArray) {
  907. }
  908. @IBAction func defaultAnnotationPorpert(sender:NSMenuItem) {
  909. let model : CPDFAnnotationModel = CPDFAnnotationModel(annotationType: CAnnotationType(rawValue: sender.tag)!)!
  910. switch sender.tag {
  911. case CAnnotationType.highlight.rawValue,CAnnotationType.underline.rawValue,CAnnotationType.strikeOut.rawValue:
  912. model.setColor((listView.activeAnnotation as! CPDFMarkupAnnotation).color)
  913. model.setOpacity((listView.activeAnnotation as! CPDFMarkupAnnotation).opacity)
  914. var red: CGFloat = 0.0
  915. var green: CGFloat = 0.0
  916. var blue: CGFloat = 0.0
  917. var alpha: CGFloat = 0.0
  918. (listView.activeAnnotation as! CPDFMarkupAnnotation).color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  919. if sender.tag == CAnnotationType.highlight.rawValue {
  920. KMPreferenceManager.shared.setData(data: [red, green, blue, (listView.activeAnnotation as! CPDFMarkupAnnotation).opacity], forKey: KMPreference.markupColorHighlightKey)
  921. } else if sender.tag == CAnnotationType.underline.rawValue {
  922. KMPreferenceManager.shared.setData(data: [red, green, blue, (listView.activeAnnotation as! CPDFMarkupAnnotation).opacity], forKey: KMPreference.markupColorUnderlineKey)
  923. } else if sender.tag == CAnnotationType.strikeOut.rawValue {
  924. KMPreferenceManager.shared.setData(data: [red, green, blue, (listView.activeAnnotation as! CPDFMarkupAnnotation).opacity], forKey: KMPreference.markupColorStrikthroughKey)
  925. }
  926. case CAnnotationType.ink.rawValue:
  927. model.setColor((listView.activeAnnotation as! CPDFInkAnnotation).color)
  928. model.setOpacity((listView.activeAnnotation as! CPDFInkAnnotation).opacity)
  929. model.setLineWidth((listView.activeAnnotation as! CPDFInkAnnotation).lineWidth())
  930. model.setStyle((listView.activeAnnotation as! CPDFInkAnnotation).borderStyle())
  931. var red: CGFloat = 0.0
  932. var green: CGFloat = 0.0
  933. var blue: CGFloat = 0.0
  934. var alpha: CGFloat = 0.0
  935. (listView.activeAnnotation as! CPDFInkAnnotation).color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  936. KMPreferenceManager.shared.setData(data: [red, green, blue, (listView.activeAnnotation as! CPDFInkAnnotation).opacity], forKey: KMPreference.markupColorPenKey)
  937. case CAnnotationType.freeText.rawValue:
  938. model.setColor((listView.activeAnnotation as! CPDFFreeTextAnnotation).color)
  939. model.setOpacity((listView.activeAnnotation as! CPDFFreeTextAnnotation).opacity)
  940. model.setFontColor((listView.activeAnnotation as! CPDFFreeTextAnnotation).fontColor)
  941. model.setFontName((listView.activeAnnotation as! CPDFFreeTextAnnotation).font.fontName)
  942. model.setFontSize((listView.activeAnnotation as! CPDFFreeTextAnnotation).font.pointSize)
  943. model.setAlignment((listView.activeAnnotation as! CPDFFreeTextAnnotation).alignment)
  944. var red: CGFloat = 0.0
  945. var green: CGFloat = 0.0
  946. var blue: CGFloat = 0.0
  947. var alpha: CGFloat = 0.0
  948. (listView.activeAnnotation as! CPDFFreeTextAnnotation).fontColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  949. KMPreferenceManager.shared.setData(data: [red, green, blue, (listView.activeAnnotation as! CPDFFreeTextAnnotation).opacity], forKey: KMPreference.markupColorTextKey)
  950. if (KMPreferenceManager.supportFonts.contains((listView.activeAnnotation as! CPDFFreeTextAnnotation).font.fontName)) {
  951. UserDefaults.standard.set((listView.activeAnnotation as! CPDFFreeTextAnnotation).font.fontName, forKey: KMPreference.markupFontTextStringKey)
  952. UserDefaults.standard.synchronize()
  953. }
  954. let alignment = (listView.activeAnnotation as! CPDFFreeTextAnnotation).alignment
  955. if (alignment == .left || alignment == .center || alignment == .right) {
  956. UserDefaults.standard.set(alignment.rawValue, forKey: KMPreference.markupFontTextAligmentKey)
  957. UserDefaults.standard.synchronize()
  958. }
  959. case CAnnotationType.anchored.rawValue:
  960. model.setColor((listView.activeAnnotation as! CPDFTextAnnotation).color)
  961. model.setAnchoredIconType((listView.activeAnnotation as! CPDFTextAnnotation).iconType())
  962. var red: CGFloat = 0.0
  963. var green: CGFloat = 0.0
  964. var blue: CGFloat = 0.0
  965. var alpha: CGFloat = 0.0
  966. (listView.activeAnnotation as! CPDFTextAnnotation).color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  967. KMPreferenceManager.shared.setData(data: [red, green, blue, alpha], forKey: KMPreference.markupColorNoteKey)
  968. case CAnnotationType.square.rawValue:
  969. model.setInteriorColor((listView.activeAnnotation as! CPDFSquareAnnotation).interiorColor)
  970. model.setColor((listView.activeAnnotation as! CPDFSquareAnnotation).color)
  971. model.setOpacity((listView.activeAnnotation as! CPDFSquareAnnotation).opacity)
  972. model.setLineWidth((listView.activeAnnotation as! CPDFSquareAnnotation).lineWidth())
  973. var red: CGFloat = 0.0
  974. var green: CGFloat = 0.0
  975. var blue: CGFloat = 0.0
  976. var alpha: CGFloat = 0.0
  977. (listView.activeAnnotation as! CPDFSquareAnnotation).interiorColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  978. KMPreferenceManager.shared.setData(data: [red, green, blue, (listView.activeAnnotation as! CPDFSquareAnnotation).interiorOpacity], forKey: KMPreference.markupColorRectangleFillKey)
  979. (listView.activeAnnotation as! CPDFSquareAnnotation).color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  980. KMPreferenceManager.shared.setData(data: [red, green, blue, (listView.activeAnnotation as! CPDFSquareAnnotation).opacity], forKey: KMPreference.markupColorRectangleBorderKey)
  981. case CAnnotationType.circle.rawValue:
  982. model.setInteriorColor((listView.activeAnnotation as! CPDFCircleAnnotation).interiorColor)
  983. model.setColor((listView.activeAnnotation as! CPDFCircleAnnotation).color)
  984. model.setOpacity((listView.activeAnnotation as! CPDFCircleAnnotation).opacity)
  985. model.setLineWidth((listView.activeAnnotation as! CPDFCircleAnnotation).lineWidth())
  986. var red: CGFloat = 0.0
  987. var green: CGFloat = 0.0
  988. var blue: CGFloat = 0.0
  989. var alpha: CGFloat = 0.0
  990. (listView.activeAnnotation as! CPDFCircleAnnotation).interiorColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  991. KMPreferenceManager.shared.setData(data: [red, green, blue, (listView.activeAnnotation as! CPDFCircleAnnotation).interiorOpacity], forKey: KMPreference.markupColorCircleFillKey)
  992. (listView.activeAnnotation as! CPDFCircleAnnotation).color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  993. KMPreferenceManager.shared.setData(data: [red, green, blue, (listView.activeAnnotation as! CPDFCircleAnnotation).opacity], forKey: KMPreference.markupColorCircleBorderKey)
  994. default:
  995. break
  996. }
  997. }
  998. @objc internal func menuItemClick_HidenorShowNote(sender: NSMenuItem?) {
  999. self.showOrHideNotes()
  1000. }
  1001. //MARK: action
  1002. @objc func cropCurrentPage() {
  1003. var rect = NSIntegralRect(self.listView.currentSelectionRect())
  1004. var page: CPDFPage?
  1005. if ((self.listView.currentSelectionPage()) != nil) {
  1006. page = self.listView.currentSelectionPage()
  1007. } else {
  1008. page = self.listView.currentPage()
  1009. }
  1010. if (NSIsEmptyRect(rect)) {
  1011. rect = KMCropTools.getPageForegroundBox(page!)
  1012. }
  1013. let index: UInt = (page?.pageIndex())!
  1014. cropPage(at: index, in: rect)
  1015. // 执行右键操作后,需要取消框选区域
  1016. if self.listView.toolMode == .selectToolMode {
  1017. objc_sync_enter(self)
  1018. self.listView.selectionRect = NSZeroRect
  1019. self.listView.selectionPageIndex = UInt(NSNotFound)
  1020. objc_sync_exit(self)
  1021. }
  1022. }
  1023. func cropPage(at index: UInt, in rect: NSRect) {
  1024. let oldRect = self.listView.document.page(at: index)?.bounds(for: .cropBox)
  1025. let undoManager = self.listView.undoManager
  1026. (undoManager?.prepare(withInvocationTarget: self) as? AnyObject)!.cropPage(at: index, in: oldRect!)
  1027. let page = self.listView.document.page(at: index)
  1028. let newRect = NSIntersectionRect(rect, (page?.bounds(for: .mediaBox))!)
  1029. page?.setBounds(newRect, for: .cropBox)
  1030. /// 刷新预览视图
  1031. self.listView.layoutDocumentView()
  1032. self.listView.displayBox = .cropBox
  1033. }
  1034. @objc private func cropAllPage() {
  1035. var size = NSZeroSize
  1036. for i in 0 ..< self.listView.document.pageCount {
  1037. let page = self.listView.document.page(at: i)
  1038. var rect = KMCropTools.getPageForegroundBox(page!)
  1039. size.width = fmax(size.width, NSWidth(rect))
  1040. size.height = fmax(size.height, NSHeight(rect))
  1041. }
  1042. var rectArray: Array<NSRect> = []
  1043. for i in 0 ..< self.listView.document.pageCount {
  1044. let page = self.listView.document.page(at: i)
  1045. var rect = KMCropTools.getPageForegroundBox(page!)
  1046. var bounds: NSRect = (page?.bounds(for: .mediaBox))!
  1047. if (rect.minX - bounds.minX > bounds.maxX-rect.maxX) {
  1048. rect.origin.x = rect.maxX-size.width
  1049. }
  1050. rect.origin.y = rect.maxY-size.height
  1051. rect.size = size
  1052. if (NSWidth(rect) > NSWidth(bounds)) {
  1053. rect.size.width = NSWidth(bounds)
  1054. }
  1055. if (NSHeight(rect) > NSHeight(bounds)) {
  1056. rect.size.height = NSHeight(bounds)
  1057. }
  1058. if (NSMinX(rect) < NSMinX(bounds)) {
  1059. rect.origin.x = NSMinX(bounds)
  1060. } else if (NSMaxX(rect) > NSMaxX(bounds)) {
  1061. rect.origin.x = NSMaxX(bounds) - NSWidth(rect)
  1062. }
  1063. if (NSMinY(rect) < NSMinY(bounds)) {
  1064. rect.origin.y = NSMinY(bounds)
  1065. } else if (NSMaxY(rect) > NSMaxY(bounds)) {
  1066. rect.origin.y = NSMaxY(bounds) - NSHeight(rect)
  1067. }
  1068. rectArray.append(rect)
  1069. }
  1070. cropPages(to: rectArray)
  1071. }
  1072. func cropPages(to rects: Array<NSRect>) {
  1073. let currentPage = self.listView.currentPage()
  1074. let visibleRect: NSRect = self.listView.convert(self.listView.convert(self.listView.documentView().visibleRect, from: self.listView.documentView()), to: self.listView.currentPage())
  1075. var oldRectArray: Array<NSRect> = []
  1076. for i in 0 ..< self.listView.document.pageCount {
  1077. let page = self.listView.document.page(at: i)
  1078. var rect = NSIntersectionRect(rects[Int(i)], (page?.bounds(for: .mediaBox))!)
  1079. let oldRect = page?.bounds(for: .cropBox)
  1080. oldRectArray.append(oldRect!)
  1081. page?.setBounds(rect, for: .cropBox)
  1082. }
  1083. let undoManager = self.listView.undoManager
  1084. (undoManager?.prepare(withInvocationTarget: self) as AnyObject).cropPages(to: oldRectArray)
  1085. /// 刷新预览视图
  1086. self.listView.layoutDocumentView()
  1087. self.listView.displayBox = .cropBox
  1088. self.listView.go(to: currentPage)
  1089. self.listView.go(to: visibleRect, on: currentPage)
  1090. }
  1091. private func cropCustomArea() {
  1092. self.listView.toolMode = .selectToolMode
  1093. self.listView.autoScales = true
  1094. self.listView.autoScales = false
  1095. var pageHeight: CGFloat = NSHeight(self.listView.currentPage().bounds(for: self.listView.displayBox))
  1096. if (self.listView.displaysPageBreaks) {
  1097. pageHeight += 8
  1098. }
  1099. var scaleFactor: CGFloat = fmax(self.listView.minimumScaleFactor, NSHeight(self.listView.frame)/pageHeight)
  1100. // if (scaleFactor < self.preView.scaleFactor) {
  1101. self.listView.scaleFactor = scaleFactor
  1102. // }
  1103. let tipView = KMCropTipView()
  1104. tipView.setString(string: "请框选裁剪区域")
  1105. tipView.frame = self.topTipBox.contentView!.frame
  1106. tipView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  1107. self.addTopTip(tipView)
  1108. tipView.enterAction = {
  1109. () in
  1110. var rect = NSIntegralRect(self.listView.currentSelectionRect())
  1111. if (NSIsEmptyRect(rect)) {
  1112. return
  1113. }
  1114. let window = KMCropSettingWindowController(windowNibName: "KMCropSettingWindowController")
  1115. self.view.window?.beginSheet(window.window!)
  1116. self.cropSettingWindowController = window
  1117. window.itemClick = { [self]
  1118. (index: Int) in
  1119. if (index == 1) { /// 取消
  1120. self.view.window?.endSheet((self.cropSettingWindowController?.window)!)
  1121. self.cropSettingWindowController = nil
  1122. return
  1123. }
  1124. let pageRangeType = self.cropSettingWindowController.pageRangeIndex
  1125. let pageCount: Int = Int(self.listView.document.pageCount)
  1126. var pages: Array<Int> = []
  1127. if (pageRangeType == 0) { /// 当前页面
  1128. pages.append(self.listView.currentPageIndex)
  1129. } else if (pageRangeType == 1) { /// 全部页面
  1130. for i in 0 ..< pageCount {
  1131. pages.append(i)
  1132. }
  1133. } else if (pageRangeType == 2) { /// 奇数页面
  1134. var string: String = ""
  1135. for i in 0 ..< pageCount {
  1136. if (i % 2 == 1) {
  1137. continue
  1138. }
  1139. pages.append(i)
  1140. }
  1141. } else if (pageRangeType == 3) { /// 偶数页面
  1142. var string: String = ""
  1143. for i in 0 ..< pageCount {
  1144. if (i % 2 == 0) {
  1145. continue
  1146. }
  1147. pages.append(i)
  1148. }
  1149. } else { /// 自定义
  1150. for i in self.cropSettingWindowController.pageRangePages {
  1151. pages.append(i)
  1152. }
  1153. }
  1154. if (pages.count < 0) {
  1155. let alert = NSAlert()
  1156. alert.messageText = "请选择页面"
  1157. alert.runModal()
  1158. return
  1159. }
  1160. var pageSize: NSSize = NSZeroSize
  1161. if (self.cropSettingWindowController.pageSize == "None") {
  1162. } else {
  1163. pageSize = KMCropTools.getPageSizeValue(self.cropSettingWindowController.pageSize)
  1164. }
  1165. for i in pages {
  1166. var page: CPDFPage = self.listView.document.page(at: UInt(i))
  1167. var rect = NSIntegralRect(self.listView.selectionRect)
  1168. if (NSIsEmptyRect(rect)) {
  1169. rect = KMCropTools.getPageForegroundBox(page)
  1170. }
  1171. var newRect = NSIntersectionRect(rect, (page.bounds(for: .mediaBox)))
  1172. page.setBounds(newRect, for: .cropBox)
  1173. if (pageSize.width == 0 && pageSize.height == 0) {
  1174. } else {
  1175. // let tiffData = page.pdfListViewTIFFData(for: rect)
  1176. let index: UInt = (page.pageIndex())
  1177. // let newPage: CPDFPage = nil
  1178. // self.listView.document.removePage(at: index)
  1179. // newPage.setBounds(NSMakeRect(0, 0, pageSize.width, pageSize.height), for: .cropBox)
  1180. // let result = self.preView.document.insertPageObject(newPage, at: index)
  1181. let result = self.listView.document.insertPage(pageSize, at: index)
  1182. }
  1183. }
  1184. /// 保存到临时路径
  1185. let toPath: String = self.listView.document.documentURL.path
  1186. let documentPath = NSTemporaryDirectory()
  1187. let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
  1188. if (FileManager.default.fileExists(atPath: tempPath)) {
  1189. try?FileManager.default.removeItem(atPath: tempPath)
  1190. }
  1191. let result = self.listView.document.write(to: URL(fileURLWithPath: tempPath))
  1192. if (result) {
  1193. if (FileManager.default.fileExists(atPath: toPath)) {
  1194. try?FileManager.default.removeItem(atPath: toPath)
  1195. }
  1196. try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
  1197. } else {
  1198. try?FileManager.default.removeItem(atPath: tempPath)
  1199. }
  1200. DispatchQueue.main.async {
  1201. self.listView.toolMode = .textToolMode
  1202. /// 刷新预览视图
  1203. self.listView.layoutDocumentView()
  1204. self.listView.displayBox = .cropBox
  1205. }
  1206. self.view.window?.endSheet((self.cropSettingWindowController?.window)!)
  1207. self.cropSettingWindowController = nil
  1208. self.addTopTip(nil)
  1209. }
  1210. }
  1211. }
  1212. @objc private func shareDocument(sender:KMToolbarViewController) {
  1213. var doucumentURL : URL = self.listView.document.documentURL
  1214. if doucumentURL != nil {
  1215. let docDir = NSTemporaryDirectory()
  1216. let documentName : String = doucumentURL.path.lastPathComponent
  1217. let path = docDir.stringByAppendingPathComponent(documentName)
  1218. let writeSuccess = self.listView.document.write(to: URL(fileURLWithPath: path))
  1219. if writeSuccess == false {
  1220. __NSBeep()
  1221. return;
  1222. }
  1223. doucumentURL = URL(fileURLWithPath: path)
  1224. }
  1225. let array = [doucumentURL]
  1226. let picker = NSSharingServicePicker.init(items: array)
  1227. if sender.shareButton.window != nil {
  1228. picker.show(relativeTo: sender.shareButton.bounds, of: sender.shareButton, preferredEdge: NSRectEdge.minY)
  1229. } else {
  1230. picker.show(relativeTo: NSRect(x: (self.view.window?.contentView?.frame.size.width)!, y: (self.view.window?.contentView?.frame.size.height ?? 0)-8, width: 0, height: 0), of: self.view.window?.contentView ?? NSView(), preferredEdge: NSRectEdge.minY)
  1231. }
  1232. }
  1233. @objc private func shareFlatten(sender:KMToolbarViewController) {
  1234. let document = self.listView.document ?? CPDFDocument()
  1235. var path = document!.documentURL.path
  1236. if path != nil {
  1237. let docDir = NSTemporaryDirectory()
  1238. let documentName : String = path.lastPathComponent
  1239. path = docDir.stringByAppendingPathComponent(documentName)
  1240. }
  1241. let pathFolder = path.fileURL.deletingLastPathComponent().path
  1242. var tfileName = path.deletingPathExtension.lastPathComponent
  1243. let tStdFileSuffix = "_flatten"
  1244. tfileName = tfileName + tStdFileSuffix + ".pdf"
  1245. path = pathFolder + "/" + tfileName
  1246. let success : Bool = document!.writeFlatten(to: URL(fileURLWithPath: path))
  1247. if success {
  1248. let url = URL(fileURLWithPath: path)
  1249. let picker = NSSharingServicePicker.init(items: [url])
  1250. if sender.shareButton.window != nil {
  1251. picker.show(relativeTo: sender.shareButton.bounds, of: sender.shareButton, preferredEdge: NSRectEdge.minY)
  1252. } else {
  1253. picker.show(relativeTo: NSRect(x: (self.view.window?.contentView?.frame.size.width)!, y: (self.view.window?.contentView?.frame.size.height ?? 0)-8, width: 0, height: 0), of: self.view.window?.contentView ?? NSView(), preferredEdge: NSRectEdge.minY)
  1254. }
  1255. }
  1256. }
  1257. @objc private func shareOriginalPDF(sender:KMToolbarViewController) {
  1258. let document = self.listView.document ?? CPDFDocument()
  1259. var path = document!.documentURL.path
  1260. if path != nil {
  1261. let docDir = NSTemporaryDirectory()
  1262. let documentName : String = path.lastPathComponent
  1263. path = docDir.stringByAppendingPathComponent(documentName)
  1264. }
  1265. var writeSuccess = document!.write(to: URL(fileURLWithPath: path))
  1266. if writeSuccess == false {
  1267. __NSBeep()
  1268. return;
  1269. }
  1270. let newDocument = CPDFDocument(url: URL(fileURLWithPath: path))!
  1271. for i in 0 ... newDocument.pageCount-1 {
  1272. let page = newDocument.page(at: i)
  1273. var annotations : [CPDFAnnotation] = []
  1274. for annotation in page!.annotations {
  1275. annotations.append(annotation)
  1276. }
  1277. for annotation in annotations {
  1278. annotation.page.removeAnnotation(annotation)
  1279. }
  1280. }
  1281. writeSuccess = newDocument.write(to:URL(fileURLWithPath: path))
  1282. if writeSuccess {
  1283. let url = URL(fileURLWithPath: path)
  1284. let picker = NSSharingServicePicker.init(items: [url])
  1285. if sender.shareButton.window != nil {
  1286. picker.show(relativeTo: sender.shareButton.bounds, of: sender.shareButton, preferredEdge: NSRectEdge.minY)
  1287. } else {
  1288. picker.show(relativeTo: NSRect(x: (self.view.window?.contentView?.frame.size.width)!, y: (self.view.window?.contentView?.frame.size.height ?? 0)-8, width: 0, height: 0), of: self.view.window?.contentView ?? NSView(), preferredEdge: NSRectEdge.minY)
  1289. }
  1290. }
  1291. }
  1292. // 开启/关闭左边栏
  1293. @objc func toggleLeftPane() -> Void {
  1294. // if lastLeftPanWidth >= functionWidth {
  1295. leftPanelOpen = true
  1296. applyLeftSideWidth(panelWidth+functionWidth, rightSideWidth: lastRightPanWidth)
  1297. // } else {
  1298. // leftPanelOpen = false
  1299. // applyLeftSideWidth(functionWidth, rightSideWidth: lastRightPanWidth)
  1300. // }
  1301. }
  1302. // 开启左边栏
  1303. @objc func openLeftPane() -> Void {
  1304. leftPanelOpen = true
  1305. applyLeftSideWidth(panelWidth+functionWidth,rightSideWidth: lastRightPanWidth)
  1306. }
  1307. // 关闭左边栏
  1308. @objc func closeLeftPane() -> Void {
  1309. leftPanelOpen = false
  1310. applyLeftSideWidth(functionWidth, rightSideWidth: lastRightPanWidth)
  1311. }
  1312. // 开启/关闭右边栏
  1313. @objc func toggleRightPane() -> Void {
  1314. if lastRightPanWidth > 5 {
  1315. self.rightPanelIsOpen = false
  1316. applyLeftSideWidth(lastLeftPanWidth, rightSideWidth: 0)
  1317. } else {
  1318. self.rightPanelIsOpen = true
  1319. applyLeftSideWidth(lastLeftPanWidth, rightSideWidth: defaultRightWidth)
  1320. }
  1321. }
  1322. @objc func openRightPane() -> Void {
  1323. if (KMPreferenceManager.shared.autoExpandPropertyPanel) {
  1324. self.rightPanelIsOpen = true
  1325. applyLeftSideWidth(lastLeftPanWidth, rightSideWidth: defaultRightWidth)
  1326. }
  1327. }
  1328. @objc func closeRightPane() -> Void {
  1329. if (KMPreferenceManager.shared.autoExpandPropertyPanel) {
  1330. self.rightPanelIsOpen = false
  1331. applyLeftSideWidth(lastLeftPanWidth, rightSideWidth: 0)
  1332. }
  1333. }
  1334. func rename(_ sender: NSNotification) -> Void {
  1335. if (self.view.window == nil || self.view.window!.isVisible == false) {
  1336. return
  1337. }
  1338. let tabController = sender.object as? CTTabController
  1339. if tabController?.title == self.document?.documentURL.lastPathComponent {
  1340. let outputSavePanel = NSSavePanel()
  1341. outputSavePanel.title = NSLocalizedString("Rename", comment: "")
  1342. outputSavePanel.allowedFileTypes = ["pdf"]
  1343. outputSavePanel.nameFieldStringValue = (self.document?.documentURL.lastPathComponent)!
  1344. outputSavePanel.directoryURL = self.document?.documentURL.deletingLastPathComponent()
  1345. let result = outputSavePanel.runModal()
  1346. if result == .OK {
  1347. let pdfDocument = CPDFDocument(url: self.document?.documentURL)
  1348. let fileURL = pdfDocument?.documentURL
  1349. let fileManager = FileManager.default
  1350. let newFileURL = fileURL!.deletingLastPathComponent().appendingPathComponent(outputSavePanel.url!.lastPathComponent)
  1351. var result = true
  1352. do {
  1353. try fileManager.moveItem(at: fileURL!, to: newFileURL)
  1354. } catch {
  1355. result = false
  1356. print("Error renaming file! Threw: \(error.localizedDescription)")
  1357. }
  1358. if (result) {
  1359. tabController?.title = outputSavePanel.url!.lastPathComponent
  1360. if let newPdfDocument = CPDFDocument(url: newFileURL) {
  1361. self.isSaveKeyChain = false
  1362. newPdfDocument.unlock(withPassword: self.document?.password)
  1363. if (newPdfDocument.pageCount > 0) {
  1364. self.setDocument = newPdfDocument
  1365. }
  1366. }
  1367. }
  1368. } else {
  1369. outputSavePanel.close()
  1370. }
  1371. }
  1372. }
  1373. func showInFinder(_ sender: Any) -> Void {
  1374. if sender is NSNotification {
  1375. let tabController = (sender as! NSNotification).object as? CTTabController
  1376. if tabController?.title == self.document?.documentURL.lastPathComponent {
  1377. let file: URL = (self.myDocument?.fileURL)!
  1378. if FileManager.default.fileExists(atPath: file.path) {
  1379. NSWorkspace.shared.activateFileViewerSelecting([file])
  1380. }
  1381. }
  1382. } else {
  1383. let file: URL = (self.myDocument?.fileURL)!
  1384. if FileManager.default.fileExists(atPath: file.path) {
  1385. NSWorkspace.shared.activateFileViewerSelecting([file])
  1386. }
  1387. }
  1388. }
  1389. func showOrHideNotes() {
  1390. self.listView.hideNotes = !self.listView.hideNotes
  1391. self.toolbarController.isShowAllAnnotations = !self.listView.hideNotes
  1392. //BOTA 注释列表显示隐藏
  1393. self.leftSideViewController.annotationViewController.annotationShowState = self.listView.hideNotes ? .hidden : .none
  1394. self.leftSideViewController.thumbnailViewController.annotationShowState = self.listView.hideNotes ? .hidden : .none
  1395. }
  1396. func closeTab(_ sender: NSNotification) -> Void {
  1397. let tabController = sender.object as? CTTabController
  1398. if tabController?.title == self.document?.documentURL.lastPathComponent {
  1399. print("closeTab")
  1400. self.leftSideViewController.clearAnnotationFilterData()
  1401. self.leftSideViewController.clearNotification()
  1402. }
  1403. }
  1404. // MARK: -
  1405. // MARK: 显示窗口
  1406. internal func showCompressWindow() {
  1407. if let wc = self.currentWindowController as? KMCompressWindowController, let _ = wc.window?.isSheet {
  1408. KMPrint("压缩窗口已显示")
  1409. return
  1410. }
  1411. let windowController = KMCompressWindowController(windowNibName: "KMCompressWindowController")
  1412. self.view.window?.beginSheet(windowController.window!)
  1413. self.currentWindowController = windowController
  1414. windowController.documentURL = self.listView.document.documentURL
  1415. windowController.password = self.listView.document.password
  1416. windowController.itemClick = { [weak self] _ in
  1417. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1418. self?.currentWindowController = nil
  1419. self?.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
  1420. }
  1421. windowController.resultCallback = { [weak self] result, openDocument, fileURL, error in
  1422. if (result) {
  1423. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1424. self?.currentWindowController = nil
  1425. self?.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
  1426. if (openDocument) {
  1427. NSDocumentController.shared.openDocument(withContentsOf: fileURL, display: true) { document, result, error in }
  1428. } else {
  1429. NSWorkspace.shared.activateFileViewerSelecting([fileURL])
  1430. }
  1431. } else {
  1432. let alert = NSAlert()
  1433. alert.messageText = NSLocalizedString("Compress Faild", comment: "")
  1434. alert.runModal()
  1435. }
  1436. }
  1437. }
  1438. internal func showConvertWindow(type: KMToolbarType, identifier: String?) {
  1439. if let wc = self.currentWindowController as? KMConvertBaseWindowController, let _ = wc.window?.isSheet {
  1440. KMPrint("转档窗口已显示")
  1441. return
  1442. }
  1443. var windowController: KMConvertBaseWindowController?
  1444. if (type == .word) { /// Word
  1445. windowController = KMConvertWordWindowController()
  1446. } else if (type == .excel) {
  1447. windowController = KMConvertExcelWindowController()
  1448. } else if (type == .ppt || type == .rtf || type == .html || type == .conversion_text) {
  1449. windowController = KMConvertPPTsWindowController()
  1450. if (type == .ppt) {
  1451. windowController?.subType = 1
  1452. } else if (type == .rtf) {
  1453. windowController?.subType = 2
  1454. } else if (type == .html) {
  1455. windowController?.subType = 3
  1456. } else if (type == .conversion_text) {
  1457. windowController?.subType = 4
  1458. }
  1459. } else if (type == .csv) {
  1460. windowController = KMConvertCSVWindowController()
  1461. } else if (type == .conversion_image) {
  1462. windowController = KMConvertImageWindowController()
  1463. }
  1464. let model = KMDocumentModel(url: self.listView.document.documentURL)
  1465. if (self.listView.document.password != nil) {
  1466. let _ = model.unlock(self.listView.document.password)
  1467. }
  1468. windowController?.documentModel = model
  1469. windowController?.itemClick = { [weak self] index in
  1470. if (identifier != nil) {
  1471. self?.toolbarController.cancelSelected(identifier!)
  1472. }
  1473. if (self?.currentWindowController == nil) {
  1474. return
  1475. }
  1476. self?.view.window?.endSheet(self!.currentWindowController.window!)
  1477. self?.currentWindowController = nil
  1478. }
  1479. self.view.window?.beginSheet((windowController?.window)!)
  1480. self.currentWindowController = windowController
  1481. }
  1482. internal func showPrintWindow(pageRange: KMPrintPageRange = KMPrintPageRange(type: .allPage, selectPages: [])) {
  1483. if (self.listView.document != nil && !self.listView.document.allowsPrinting) { // 有打印限制
  1484. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self] result ,password in
  1485. if (result == .cancel) {
  1486. return
  1487. }
  1488. // 解除权限
  1489. self?.listView.document.unlock(withPassword: password)
  1490. // 隐藏提示
  1491. self?.hiddenSecureLimitTip()
  1492. // 去打印
  1493. KMPrintWindowController.openDocument(inputDocument: self?.listView.document, inputPageRange: pageRange)
  1494. }
  1495. return
  1496. }
  1497. KMPrintWindowController.openDocument(inputDocument: self.listView?.document, inputPageRange: pageRange)
  1498. }
  1499. // MARK: - Split View
  1500. @IBAction func secondaryViewOpenFile(_ sender: NSButton) -> Void {
  1501. }
  1502. func singlePageScreen(isSinglePage: Bool, doublePagesScreen isHorizontal: Bool) -> Void {
  1503. if !isSinglePage && isHorizontal {
  1504. self.openSecondaryPdfView?.changeBenefit(ofContrastLabelWidth: 208)
  1505. }
  1506. if (!isSinglePage && !isHorizontal) {
  1507. self.openSecondaryPdfView?.changeBenefit(ofContrastLabelWidth: 300)
  1508. }
  1509. self.displaySinglePageScreen(isSinglePage: isSinglePage, doublePageScreen: isHorizontal)
  1510. }
  1511. func displaySinglePageScreen(isSinglePage: Bool, doublePageScreen isHorizontal: Bool) -> Void {
  1512. if isSinglePage {
  1513. pdfSplitView.dividerStyle = .thin
  1514. pdfSplitView.setPosition(mianSplitView.maxPossiblePositionOfDivider(at: 1), ofDividerAt: 0, animate: true)
  1515. } else {
  1516. pdfSplitView.dividerStyle = .paneSplitter
  1517. pdfSplitView.isVertical = !isHorizontal
  1518. pdfSplitView.setPosition(500, ofDividerAt: 0, animate: true)
  1519. if secondaryPdfView.document != nil {
  1520. // secondaryPdfView.frame = pdfSplitSecondView.frame
  1521. // pdfSplitSecondView.contentView = secondaryPdfView
  1522. } else {
  1523. openSecondaryPdfView = KMSecondaryViewController.init()
  1524. openSecondaryPdfView?.view.frame = pdfSplitSecondView.frame
  1525. pdfSplitSecondView.contentView = openSecondaryPdfView?.view
  1526. openSecondaryPdfView?.delegate = self
  1527. }
  1528. }
  1529. }
  1530. func lastSplitPDFHeightFloat(_ rect: NSRect) -> Float {
  1531. if (pdfSplitView.isVertical) {
  1532. return Float(NSWidth(rect))
  1533. } else {
  1534. return Float(NSHeight(rect))
  1535. }
  1536. }
  1537. func displaySecondaryPDFView(withUrl url: URL) -> Bool {
  1538. let document = CPDFDocument.init(url: url)
  1539. if document != nil {
  1540. secondaryPdfView.frame = pdfSplitSecondView.frame
  1541. pdfSplitSecondView.contentView = secondaryPdfView
  1542. secondaryPdfView.document = nil
  1543. secondaryPdfView.document = document
  1544. return true
  1545. } else {
  1546. return false
  1547. }
  1548. }
  1549. func updateNextAndPreViousButtonState() {
  1550. let item = self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentNextPageToolbarItemIdentifier)
  1551. let toItem = self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentPreviousPageToolbarItemIdentifier)
  1552. if self.listView.canGoToNextPage() {
  1553. item?.unEnabled = false
  1554. } else {
  1555. item?.unEnabled = true
  1556. }
  1557. if self.listView.canGoToPreviousPage() {
  1558. toItem?.unEnabled = false
  1559. } else {
  1560. toItem?.unEnabled = true
  1561. }
  1562. }
  1563. func updateZoomInOutButtonState() {
  1564. let item : KMToolBoxItem = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentZoomToolbarItemIdentifier))!
  1565. let toItem : KMToolBoxItem = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentZoomOutToolbarItemIdentifier))!
  1566. if self.listView.canZoomIn {
  1567. item.unEnabled = false
  1568. } else {
  1569. item.unEnabled = true
  1570. }
  1571. if self.listView.canZoomOut {
  1572. toItem.unEnabled = false
  1573. } else {
  1574. toItem.unEnabled = true
  1575. }
  1576. }
  1577. }
  1578. // MARK: -
  1579. // MARK: - KMSecondaryViewControllerDelegate
  1580. extension KMMainViewController: KMSecondaryViewControllerDelegate {
  1581. func receivedFileUrl(_ url: URL!) {
  1582. let state = self.displaySecondaryPDFView(withUrl: url)
  1583. if !state {
  1584. let alert = NSAlert()
  1585. alert.alertStyle = .critical
  1586. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  1587. alert.runModal()
  1588. return
  1589. }
  1590. }
  1591. }
  1592. // MARK: -
  1593. // MARK: - KMMainToolbarControllerDelegate
  1594. extension KMMainViewController : KMMainToolbarControllerDelegate {
  1595. func changeModelAction(mode: CToolMode) {
  1596. self.listView.toolMode = mode
  1597. if mode == .editPDFToolMode {
  1598. self.childToolbarController.updateType(newType: .editPDF)
  1599. }
  1600. }
  1601. func toolbarViewController(_ viewController:KMToolbarViewController, zoomModel selectedTag:Int) {
  1602. switch selectedTag {
  1603. case 2:
  1604. // self.listView.autoScales = !self.listView.autoScales
  1605. self.listView.autoScales = true
  1606. break
  1607. case 1:
  1608. if self.listView.scaleFactor != 1.0 {
  1609. self.listView.scaleFactor = 1.0
  1610. self.listView.autoScales = false
  1611. }
  1612. break
  1613. case 0:
  1614. self.listView.autoScales = true
  1615. self.listView.autoScales = false
  1616. break
  1617. case 3:
  1618. self.listView.scaleFactor = 0.1
  1619. break
  1620. case 4:
  1621. self.listView.scaleFactor = 0.25
  1622. break
  1623. case 5:
  1624. self.listView.scaleFactor = 0.5
  1625. break
  1626. case 6:
  1627. self.listView.scaleFactor = 0.75
  1628. break
  1629. case 7:
  1630. self.listView.scaleFactor = 1.0
  1631. break
  1632. case 8:
  1633. self.listView.scaleFactor = 1.5
  1634. break
  1635. case 9:
  1636. self.listView.scaleFactor = 2.0
  1637. break
  1638. case 10:
  1639. self.listView.scaleFactor = 4.0
  1640. break
  1641. case 11:
  1642. self.listView.scaleFactor = 8.0
  1643. break
  1644. case 12:
  1645. self.listView.scaleFactor = 10.0
  1646. break
  1647. default:
  1648. break
  1649. }
  1650. viewController.zoomTextField.stringValue = "\(Int(self.listView.scaleFactor*100))%"
  1651. }
  1652. func toolbarViewController(_ viewController: KMToolbarViewController, zoomSting: String) {
  1653. var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0)
  1654. if scale > 100 {
  1655. scale = 100
  1656. }
  1657. self.listView.scaleFactor = scale
  1658. viewController.zoomTextField.stringValue = "\(Int(self.listView.scaleFactor*100))%"
  1659. }
  1660. func changePDFViewZoomInAction() {
  1661. self.listView.zoomIn(nil)
  1662. self.updateZoomInOutButtonState()
  1663. }
  1664. func changePDFViewZoomOutAction() {
  1665. self.listView.zoomOut(nil)
  1666. self.updateZoomInOutButtonState()
  1667. }
  1668. func changePDFViewGotoNextPageAction() {
  1669. self.listView.goToNextPage(nil)
  1670. self.updateNextAndPreViousButtonState()
  1671. }
  1672. func changePDFViewGoToPreviousPageAction() {
  1673. self.listView.goToPreviousPage(nil)
  1674. self.updateNextAndPreViousButtonState()
  1675. }
  1676. func showPDFViewPrintViewController() {
  1677. // self.menuItemAnnotationClick_Print(sender: NSMenuItem())
  1678. self.showPrintWindow()
  1679. }
  1680. func toolbarViewController(_ viewController: KMToolbarViewController, shareDocument item: NSMenuItem) {
  1681. self.shareDocument(sender: viewController)
  1682. }
  1683. func toolbarViewController(_ viewController: KMToolbarViewController, shareFlatten item: NSMenuItem) {
  1684. self.shareFlatten(sender: viewController)
  1685. }
  1686. func toolbarViewController(_ viewController: KMToolbarViewController, shareOriginalPDF item: NSMenuItem) {
  1687. self.shareOriginalPDF(sender: viewController)
  1688. }
  1689. func toolbarViewController(_ viewController: KMToolbarViewController, scanOCRModel selectedTag: Int) {
  1690. if(0 == selectedTag) {
  1691. self.documentAIViewController?.enteredIncreaseAllPage()
  1692. self.documentAIViewController?.updateToolState(false)
  1693. } else {
  1694. // self.documentAIViewController?.recognitionPageString("1", with: .english)
  1695. // self.documentAIViewController?.recognitionPartModel(with: .english)
  1696. // self.documentAIViewController?.updateToolState(true)
  1697. }
  1698. // 0 : Scan 1:OCR Text
  1699. }
  1700. func mainToolDidClicked(_ toolController: KMToolbarController, _ type: KMToolbarViewType, _ item: KMToolBoxItem , _ pages: [Int]) {
  1701. if(type != .Page) {
  1702. if (hasEnterPageEdit()) {
  1703. self.exitPageEdit()
  1704. }
  1705. }
  1706. if(type != .ScanOCR) {
  1707. if ((self.documentAIViewController?.view.superview) != nil) {
  1708. self.listView.isHidden = false
  1709. self.documentAIViewController?.creatRecognitionDocument()
  1710. self.documentAIViewController?.view .removeFromSuperview()
  1711. self.documentAIViewController = nil;
  1712. self.listView.layoutDocumentView()
  1713. }
  1714. }
  1715. if (type == .Page) {
  1716. if (hasEnterPageEdit()) {
  1717. self.exitPageEdit()
  1718. } else {
  1719. self.enterPageEdit(pages)
  1720. }
  1721. } else if(type == .ScanOCR) {
  1722. if((self.documentAIViewController?.view.superview) != nil) {
  1723. let alert = NSAlert()
  1724. alert.messageText = NSLocalizedString("If you need to edit again after exit, you need to re-execute OCR recognition, sure you want to exit?", comment: "")
  1725. alert.addButton(withTitle: "YES")
  1726. alert.addButton(withTitle: "NO")
  1727. let result = alert.runModal()
  1728. if (result == .alertFirstButtonReturn) { /// 取消
  1729. self.listView.isHidden = false
  1730. self.documentAIViewController?.creatRecognitionDocument()
  1731. self.documentAIViewController?.view .removeFromSuperview()
  1732. self.documentAIViewController = nil;
  1733. self.listView.layoutDocumentView()
  1734. } else {
  1735. self.toolbarController.toolbarType = .ScanOCR
  1736. }
  1737. } else {
  1738. self.documentAIViewController = KMDocumentAIViewController(pdfView: self.listView)
  1739. self.PDFContendView.addSubview(self.documentAIViewController?.view ?? KMDocumentAIViewController().view);
  1740. self.documentAIViewController?.view.frame = self.PDFContendView.bounds
  1741. self.documentAIViewController?.view.autoresizingMask = NSView.AutoresizingMask([.width,.height])
  1742. self.listView.isHidden = true //不影藏的话会出现鼠标事件的崩溃
  1743. }
  1744. }
  1745. }
  1746. func clickChildTool(type: KMToolbarType, index: Int) {
  1747. Task { @MainActor in
  1748. if (type == .compress) { /// 压缩
  1749. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  1750. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  1751. self.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
  1752. return
  1753. }
  1754. if (self.needSaveDocument()) {
  1755. self.saveDocumentWithProgressAlert { [unowned self] params in
  1756. self.showCompressWindow()
  1757. }
  1758. return
  1759. }
  1760. self.showCompressWindow()
  1761. return
  1762. }
  1763. if ((KMToolbarType.word.rawValue ... KMToolbarType.conversion_image.rawValue).contains(type.rawValue)) { /// 转档
  1764. var identifier: String?
  1765. if (type == .word) { /// Word
  1766. identifier = KMToolbarConversionWordItemIdentifier
  1767. } else if (type == .excel) {
  1768. identifier = KMToolbarConversionExcelItemIdentifier
  1769. } else if (type == .ppt) {
  1770. identifier = KMToolbarConversionPPTItemIdentifier
  1771. } else if (type == .rtf) {
  1772. identifier = KMToolbarConversionRTFItemIdentifier
  1773. } else if (type == .html) {
  1774. identifier = KMToolbarConversionHTMLItemIdentifier
  1775. } else if (type == .conversion_text) {
  1776. identifier = KMToolbarConversionTextItemIdentifier
  1777. } else if (type == .csv) {
  1778. identifier = KMToolbarConversionCSVItemIdentifier
  1779. } else if (type == .conversion_image) {
  1780. identifier = KMToolbarConversionImageItemIdentifier
  1781. }
  1782. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  1783. if (identifier != nil) {
  1784. self.toolbarController.cancelSelected(identifier!)
  1785. }
  1786. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  1787. return
  1788. }
  1789. if (self.needSaveDocument()) {
  1790. self.saveDocumentWithProgressAlert { [unowned self] params in
  1791. self.showConvertWindow(type: type, identifier: identifier)
  1792. }
  1793. return
  1794. }
  1795. self.showConvertWindow(type: type, identifier: identifier)
  1796. return
  1797. }
  1798. if (type == .merge) { /// 合并
  1799. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  1800. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  1801. self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
  1802. return
  1803. }
  1804. if (self.listView.document.allowsCopying == false || self.listView.document.allowsPrinting == false) {
  1805. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [unowned self] result , password in
  1806. if (result == .cancel) {
  1807. return
  1808. }
  1809. if (self.needSaveDocument()) {
  1810. self.saveDocumentWithProgressAlert { [unowned self] params in
  1811. self.showMergeWindow(password)
  1812. }
  1813. return
  1814. }
  1815. self.showMergeWindow(password)
  1816. }
  1817. return
  1818. }
  1819. if (self.needSaveDocument()) {
  1820. self.saveDocumentWithProgressAlert { [unowned self] params in
  1821. self.showMergeWindow(self.listView.document.password)
  1822. }
  1823. return
  1824. }
  1825. self.showMergeWindow(self.listView.document.password)
  1826. return
  1827. }
  1828. if (type == .crop) {
  1829. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  1830. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  1831. return
  1832. }
  1833. if (index == 1) {
  1834. self.cropCurrentPage()
  1835. return
  1836. }
  1837. if (index == 2) {
  1838. self.cropAllPage()
  1839. return
  1840. }
  1841. //// 自定义裁剪区域
  1842. // self.cropCustomArea()
  1843. } else if (type == .bates || type == .headerAndFooter || type == .background || type == .watermark) {
  1844. if (index == 0) {
  1845. var controller: KMWatermarkAdjectivePreViewBaseController!
  1846. if (type == .bates) {
  1847. controller = KMBatesPreviewController()
  1848. } else if (type == .headerAndFooter) {
  1849. controller = KMHeaderFooterPreviewController()
  1850. } else if (type == .background) {
  1851. controller = KMBackgroundController()
  1852. } else if (type == .watermark) {
  1853. controller = KMWatermarkViewController()
  1854. }
  1855. controller.applyDocument = self.listView.document
  1856. controller.view.frame = self.view.bounds
  1857. controller.view.autoresizingMask = [.width, .height]
  1858. controller.documentURL = self.listView.document.documentURL
  1859. self.view.addSubview(controller.view)
  1860. self.addChild(controller)
  1861. controller.itemClick = { [weak self] index, param in
  1862. if (index == 0 || index == 1) { /// 取消 和 应用
  1863. var controller_watermarkAdjective_preView: KMWatermarkAdjectivePreViewBaseController?
  1864. for i in 0 ..< self!.children.count {
  1865. let vc: NSViewController = self!.children[self!.children.count-1-i]
  1866. if (vc.isKind(of: KMWatermarkAdjectivePreViewBaseController.self) == true) {
  1867. controller_watermarkAdjective_preView = vc as! KMWatermarkAdjectivePreViewBaseController
  1868. break
  1869. }
  1870. }
  1871. if (controller_watermarkAdjective_preView == nil) {
  1872. return
  1873. }
  1874. controller_watermarkAdjective_preView!.view.removeFromSuperview()
  1875. controller_watermarkAdjective_preView?.removeFromParent()
  1876. // if (index == 1) {
  1877. self?.listView.document = CPDFDocument(url: self?.listView.document.documentURL)
  1878. self?.listView.layoutDocumentView()
  1879. self?.listView.setNeedsDisplayForVisiblePages()
  1880. // }
  1881. return
  1882. }
  1883. }
  1884. return
  1885. }
  1886. if (index == 1) { /// 取消
  1887. self.toolbarController.exitWatermarkAdjective()
  1888. // self.rightSideViewController.view.isHidden = true
  1889. self.rightSideViewController.isHidden = true
  1890. self.rightSideViewController.subViewType = .None
  1891. self.closeRightPane()
  1892. return
  1893. }
  1894. if (index == 5) { /// 批量
  1895. return
  1896. }
  1897. if (index == 4) { //// 移除
  1898. if (type == .watermark) {
  1899. let watermarks = self.listView.document.watermarks()
  1900. if (watermarks == nil || watermarks!.count <= 0) {
  1901. let alert = NSAlert()
  1902. alert.alertStyle = .warning
  1903. alert.messageText = NSLocalizedString("无法在本文件中找到可删除的水印。如果您看到水印,其不是使用PDF Master添加的,因此无法被检测到。", comment: "")
  1904. alert.addButton(withTitle: NSLocalizedString("确认", comment: ""))
  1905. alert.addButton(withTitle: NSLocalizedString("取消", comment: ""))
  1906. alert.runModal()
  1907. return
  1908. }
  1909. let alert = NSAlert()
  1910. alert.alertStyle = .warning
  1911. alert.messageText = NSLocalizedString("确定要删除文件水印吗?", comment: "")
  1912. alert.addButton(withTitle: NSLocalizedString("删除", comment: ""))
  1913. alert.addButton(withTitle: NSLocalizedString("取消", comment: ""))
  1914. let result = alert.runModal()
  1915. if (result != .alertFirstButtonReturn) {
  1916. return
  1917. }
  1918. }
  1919. let oType: KMWatermarkAdjectiveType = KMWatermarkAdjectiveTools.KMWatermarkAdjectiveType(from: type)
  1920. KMWatermarkAdjectiveTools.delete(oType, self.listView, self.listView.document.documentURL.path) {
  1921. result in
  1922. DispatchQueue.main.async {
  1923. self.listView.layoutDocumentView()
  1924. self.listView.setNeedsDisplayForVisiblePages()
  1925. }
  1926. if (result) {
  1927. let alert = NSAlert()
  1928. alert.alertStyle = .warning
  1929. alert.messageText = "成功"
  1930. alert.runModal()
  1931. } else {
  1932. let alert = NSAlert()
  1933. alert.alertStyle = .critical
  1934. alert.messageText = "失败"
  1935. alert.runModal()
  1936. }
  1937. }
  1938. return
  1939. }
  1940. if (index == 2) { /// 应用
  1941. let model = self.rightSideViewController.model
  1942. if (type == .bates || type == .headerAndFooter) {
  1943. if (model == nil || (model?.isKind(of: KMBatesModel.self) == false)) {
  1944. let alert = NSAlert()
  1945. alert.alertStyle = .critical
  1946. alert.messageText = "没有找到模型"
  1947. alert.runModal()
  1948. return
  1949. }
  1950. if ((model as! KMBatesModel).hasVaild == false) {
  1951. let alert = NSAlert()
  1952. alert.alertStyle = .critical
  1953. alert.messageText = "没有找到内容"
  1954. alert.runModal()
  1955. return
  1956. }
  1957. }
  1958. KMWatermarkAdjectiveTools.apply(model!, self.listView, self.listView.document.documentURL.path) {
  1959. result in
  1960. DispatchQueue.main.async {
  1961. self.listView.layoutDocumentView()
  1962. self.listView.setNeedsDisplayForVisiblePages()
  1963. }
  1964. if (result) {
  1965. let alert = NSAlert()
  1966. alert.alertStyle = .warning
  1967. alert.messageText = "成功"
  1968. alert.runModal()
  1969. } else {
  1970. let alert = NSAlert()
  1971. alert.alertStyle = .critical
  1972. alert.messageText = "失败"
  1973. alert.runModal()
  1974. }
  1975. }
  1976. return
  1977. }
  1978. /// 新增
  1979. // self.rightSideViewController.view.isHidden = false
  1980. self.rightSideViewController.isHidden = false
  1981. self.rightSideViewController.subViewType = KMWatermarkAdjectiveTools.KMToolBarTypeToRightSubViewType(type)
  1982. self.openRightPane()
  1983. } else if (type == .redact) { /// 标记密文
  1984. if (index == 1) { /// 取消
  1985. if (self.hasAddRedact) {
  1986. let alert = NSAlert()
  1987. alert.messageText = NSLocalizedString("You can save a document with unapplied cipher text and edit it again. If you do not save, all changes will be lost.", comment: "")
  1988. alert.addButton(withTitle: NSLocalizedString("Don’t Save", comment: ""))
  1989. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  1990. let result = alert.runModal()
  1991. if (result == .alertFirstButtonReturn) {
  1992. self.listView.toolMode = .moveToolMode
  1993. self.toolbarController.exitRedact()
  1994. }
  1995. return
  1996. }
  1997. self.listView.toolMode = .moveToolMode
  1998. self.toolbarController.exitRedact()
  1999. return
  2000. }
  2001. if (index == 2) { /// 擦除
  2002. self.exeRedactConfirm(.eraserAll) {}
  2003. return
  2004. }
  2005. if (index == 3) { /// 应用
  2006. self.exeRedactConfirm(.redactAll) {}
  2007. return
  2008. }
  2009. if (index == 4) { /// 文本&图像
  2010. self.listView.toolMode = .redactToolMode
  2011. return
  2012. }
  2013. if (index == 5) { /// 页面
  2014. let windowController = KMRedactPageRangeWindowController(windowNibName: "KMRedactBaseWindowController")
  2015. await self.view.window?.beginSheet(windowController.window!)
  2016. self.currentWindowController = windowController
  2017. windowController.itemClick = { [weak self] index, value in
  2018. if (index == 1) {
  2019. let topBarView = self?.toolbarController.fetchTopBarView()
  2020. if (topBarView == nil || topBarView?.isKind(of: KMRedactTopToolBar.self) == false) {
  2021. return
  2022. }
  2023. (topBarView as! KMRedactTopToolBar).selectItem(0)
  2024. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  2025. self?.currentWindowController = nil
  2026. return
  2027. }
  2028. let windowController_pageRange = self?.currentWindowController as! KMRedactPageRangeWindowController
  2029. let pageType = windowController_pageRange.pageType
  2030. let pageString = windowController_pageRange.pageString
  2031. if (pageType == 5) { /// 自定义页面
  2032. let array = KMPageRangeTools.findSelectPage(pageRangeString: pageString, pageCount: Int((self?.listView.document.pageCount)!))
  2033. if (array.count == 0) {
  2034. let alert = NSAlert()
  2035. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  2036. alert.runModal()
  2037. return
  2038. }
  2039. }
  2040. self!.view.window?.endSheet(self!.currentWindowController.window!)
  2041. self!.currentWindowController = nil
  2042. var indexs: IndexSet = []
  2043. if (pageType == 1) { /// 当前页面
  2044. indexs.insert((self?.listView.currentPageIndex)!)
  2045. } else {
  2046. indexs = KMRedactTools.getPageIndexs(pageType-1, string: pageString, Int((self?.listView.document.pageCount)!))
  2047. }
  2048. if (indexs.count == 0) {
  2049. return
  2050. }
  2051. for i in indexs {
  2052. let page: CPDFPage = (self?.listView.document.page(at: UInt(i)))!
  2053. let redactAnno = CPDFRedactAnnotation(pdfListViewNoteWith: self?.listView.document)
  2054. redactAnno?.bounds = page.bounds
  2055. self?.listView.add(redactAnno, to: page)
  2056. }
  2057. }
  2058. return
  2059. }
  2060. } else if (type == .secure) { /// 安全
  2061. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  2062. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  2063. return
  2064. }
  2065. if (index == 1) { /// 设置密码
  2066. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  2067. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self] result, password in
  2068. if (result == .cancel) {
  2069. return
  2070. }
  2071. self?.isSaveKeyChain = false
  2072. self?.listView.document.unlock(withPassword: password)
  2073. self?.hiddenSecureLimitTip()
  2074. self?.showSecureWindow((self?.listView.document.documentURL)!)
  2075. }
  2076. return
  2077. }
  2078. self.showSecureWindow(self.listView.document.documentURL)
  2079. return
  2080. }
  2081. /// 删除安全性设置
  2082. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  2083. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self] result, password in
  2084. if (result == .cancel) {
  2085. return
  2086. }
  2087. self?.isSaveKeyChain = false
  2088. self?.listView.document.unlock(withPassword: password)
  2089. self?.hiddenSecureLimitTip()
  2090. let result = KMPasswordInputWindow.saveDocumentForRemovePassword((self?.listView.document)!)
  2091. if (result) {
  2092. self?.setDocument = CPDFDocument(url: self?.listView.document.documentURL)
  2093. }
  2094. let tip = KMRemovePasswordResultTipView()
  2095. tip.result = result ? .success : .failure
  2096. tip.showInView(superView: (self?.listView.superview)!)
  2097. }
  2098. return
  2099. }
  2100. let alert = NSAlert()
  2101. alert.messageText = NSLocalizedString("Are you sure you want to remove the security settings for the file?", comment: "")
  2102. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  2103. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  2104. let response = alert.runModal()
  2105. if (response != .alertFirstButtonReturn) {
  2106. return
  2107. }
  2108. let result = KMPasswordInputWindow.saveDocumentForRemovePassword((self.listView.document)!)
  2109. let tip = KMRemovePasswordResultTipView()
  2110. if result {
  2111. tip.result = .success
  2112. self._isPDFDocumentEdited = false
  2113. self.needSave = false
  2114. self.hiddenSecureLimitTip()
  2115. self.setDocument = CPDFDocument(url: self.listView.document.documentURL)
  2116. } else {
  2117. tip.result = .failure
  2118. }
  2119. tip.showInView(superView: self.listView.superview!)
  2120. }
  2121. }
  2122. }
  2123. }
  2124. // MARK: -
  2125. // MARK: - Mouse
  2126. extension KMMainViewController {
  2127. override func mouseDown(with event: NSEvent) {
  2128. super.mouseDown(with: event)
  2129. print("KMMainViewController mouseDown")
  2130. self.leftSideViewCancelSelect()
  2131. self.pageNumberDisplayView.isEdit = false
  2132. }
  2133. }
  2134. //MARK: LeftSideViewController
  2135. extension KMMainViewController {
  2136. func leftSideViewCancelSelect() {
  2137. switch self.leftSideViewController.type.methodType {
  2138. // case .Thumbnail:
  2139. // self.leftSideViewController.thumbnailViewController.cancelSelect()
  2140. case .Outline:
  2141. self.leftSideViewController.outlineViewController.cancelSelect()
  2142. case .BookMark:
  2143. self.leftSideViewController.bookViewController.cancelSelect()
  2144. case .Search:
  2145. self.leftSideViewController.searchViewController.cancelSelect()
  2146. case .Annotation:
  2147. self.leftSideViewController.annotationViewController.cancelSelect()
  2148. default:
  2149. print("其他")
  2150. }
  2151. }
  2152. }
  2153. extension KMMainViewController: KMReadModelViewDelegate {
  2154. func firstPageButtonDidChange(view: KMReadModelView, sender: Any) {
  2155. self.listView.goToFirstPage(nil)
  2156. }
  2157. func beforePageButtonDidChange(view: KMReadModelView, sender: Any) {
  2158. self.listView.goToPreviousPage(nil)
  2159. }
  2160. func afterPageButtonDidChange(view: KMReadModelView, sender: Any) {
  2161. self.listView.goToNextPage(nil)
  2162. }
  2163. func lastPageButtonDidChange(view: KMReadModelView, sender: Any) {
  2164. self.listView.goToLastPage(nil)
  2165. }
  2166. func currentPageButtonDidChange(view: KMReadModelView, sender: Any, pageIndex: Int) {
  2167. self.listView.go(toPageIndex: max(pageIndex - 1, 0), animated: true)
  2168. }
  2169. func scaleDidChange(view: KMReadModelView, sender: Any, type: KMReadModelViewScaleType) {
  2170. var scale: Double = 1.0
  2171. switch type {
  2172. case .fitWidth:
  2173. self.listView.autoScales = !self.listView.autoScales
  2174. scale = self.listView.scaleFactor
  2175. break
  2176. case .actualSize:
  2177. if self.listView.scaleFactor != 1.0 {
  2178. self.listView.scaleFactor = 1.0
  2179. self.listView.autoScales = false
  2180. }
  2181. scale = self.listView.scaleFactor
  2182. break
  2183. case .autoSize:
  2184. self.listView.autoScales = true
  2185. self.listView.autoScales = false
  2186. scale = self.listView.scaleFactor
  2187. break
  2188. case ._10:
  2189. scale = 0.1
  2190. break
  2191. case ._25:
  2192. scale = 0.25
  2193. break
  2194. case ._50:
  2195. scale = 0.5
  2196. break
  2197. case ._100:
  2198. scale = 1.0
  2199. break
  2200. case ._150:
  2201. scale = 1.5
  2202. break
  2203. case ._200:
  2204. scale = 2.0
  2205. break
  2206. case ._400:
  2207. scale = 4.0
  2208. break
  2209. case ._800:
  2210. scale = 8.0
  2211. break
  2212. default:
  2213. break
  2214. }
  2215. self.listView.scaleFactor = scale
  2216. self.toolbarController.mainViewController?.childToolbarController.zoomTextField.stringValue = "\(Int(self.listView.scaleFactor*100))%"
  2217. }
  2218. func cancelReadModel(view: KMReadModelView, sender: Any) {
  2219. if self.isReadMode {
  2220. self.closeReadModel()
  2221. }
  2222. }
  2223. func readModelViewWidthChange(view: KMReadModelView, width: CGFloat) {
  2224. // if self.readModelViewWidthConstraint.constant != width {
  2225. self.readModelViewWidthConstraint.constant = width
  2226. // }
  2227. }
  2228. }
  2229. extension KMMainViewController: KMPageNumberDisplayViewDelegate {
  2230. func gotoPageIndex(view: KMPageNumberDisplayView, pageIndex: Int) {
  2231. self.listView.go(toPageIndex: pageIndex, animated: true)
  2232. }
  2233. func updateWidth(view: KMPageNumberDisplayView, width: CGFloat) {
  2234. if self.tipCurrentPageBoxWidthConstraint.constant != width {
  2235. self.tipCurrentPageBoxWidthConstraint.constant = width
  2236. }
  2237. }
  2238. }