KMMainViewController+Action.swift 119 KB

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