KMMainViewController+Action.swift 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  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. if self.rightPanelIsOpen {
  1340. return
  1341. }
  1342. self.rightPanelIsOpen = true
  1343. self.toolbarController.selectItem(KMRightControlToolbarItemIdentifier)
  1344. applyLeftSideWidth(lastLeftPanWidth, rightSideWidth: defaultRightWidth)
  1345. }
  1346. }
  1347. @objc func closeRightPane() -> Void {
  1348. if (KMPreferenceManager.shared.autoExpandPropertyPanel) {
  1349. self.rightPanelIsOpen = false
  1350. self.toolbarController.cancelSelected(KMRightControlToolbarItemIdentifier)
  1351. applyLeftSideWidth(lastLeftPanWidth, rightSideWidth: 0)
  1352. }
  1353. }
  1354. func rename(_ sender: NSNotification) -> Void {
  1355. if (self.view.window == nil || self.view.window!.isVisible == false) {
  1356. return
  1357. }
  1358. let tabController = sender.object as? CTTabController
  1359. if tabController?.title == self.document?.documentURL.lastPathComponent {
  1360. let outputSavePanel = NSSavePanel()
  1361. outputSavePanel.title = NSLocalizedString("Rename", comment: "")
  1362. outputSavePanel.allowedFileTypes = ["pdf"]
  1363. outputSavePanel.nameFieldStringValue = (self.document?.documentURL.lastPathComponent)!
  1364. outputSavePanel.directoryURL = self.document?.documentURL.deletingLastPathComponent()
  1365. let result = outputSavePanel.runModal()
  1366. if result == .OK {
  1367. let pdfDocument = CPDFDocument(url: self.document?.documentURL)
  1368. let fileURL = pdfDocument?.documentURL
  1369. let fileManager = FileManager.default
  1370. let newFileURL = fileURL!.deletingLastPathComponent().appendingPathComponent(outputSavePanel.url!.lastPathComponent)
  1371. var result = true
  1372. do {
  1373. try fileManager.moveItem(at: fileURL!, to: newFileURL)
  1374. } catch {
  1375. result = false
  1376. print("Error renaming file! Threw: \(error.localizedDescription)")
  1377. }
  1378. if (result) {
  1379. tabController?.title = outputSavePanel.url!.lastPathComponent
  1380. if let newPdfDocument = CPDFDocument(url: newFileURL) {
  1381. self.isSaveKeyChain = false
  1382. newPdfDocument.unlock(withPassword: self.document?.password)
  1383. if (newPdfDocument.pageCount > 0) {
  1384. self.setDocument = newPdfDocument
  1385. }
  1386. }
  1387. }
  1388. } else {
  1389. outputSavePanel.close()
  1390. }
  1391. }
  1392. }
  1393. func showInFinder(_ sender: Any) -> Void {
  1394. if sender is NSNotification {
  1395. let tabController = (sender as! NSNotification).object as? CTTabController
  1396. if tabController?.title == self.document?.documentURL.lastPathComponent {
  1397. let file: URL = (self.myDocument?.fileURL)!
  1398. if FileManager.default.fileExists(atPath: file.path) {
  1399. NSWorkspace.shared.activateFileViewerSelecting([file])
  1400. }
  1401. }
  1402. } else {
  1403. let file: URL = (self.myDocument?.fileURL)!
  1404. if FileManager.default.fileExists(atPath: file.path) {
  1405. NSWorkspace.shared.activateFileViewerSelecting([file])
  1406. }
  1407. }
  1408. }
  1409. func showOrHideNotes() {
  1410. self.listView.hideNotes = !self.listView.hideNotes
  1411. self.toolbarController.isShowAllAnnotations = !self.listView.hideNotes
  1412. //BOTA 注释列表显示隐藏
  1413. self.leftSideViewController.annotationViewController.annotationShowState = self.listView.hideNotes ? .hidden : .none
  1414. self.leftSideViewController.thumbnailViewController.annotationShowState = self.listView.hideNotes ? .hidden : .none
  1415. }
  1416. func closeTab(_ sender: NSNotification) -> Void {
  1417. let tabController = sender.object as? CTTabController
  1418. if tabController?.title == self.document?.documentURL.lastPathComponent {
  1419. print("closeTab")
  1420. self.leftSideViewController.clearAnnotationFilterData()
  1421. self.leftSideViewController.clearNotification()
  1422. }
  1423. }
  1424. // MARK: -
  1425. // MARK: 显示窗口
  1426. internal func showCompressWindow() {
  1427. if let wc = self.currentWindowController as? KMCompressWindowController, let _ = wc.window?.isSheet {
  1428. KMPrint("压缩窗口已显示")
  1429. return
  1430. }
  1431. let windowController = KMCompressWindowController(windowNibName: "KMCompressWindowController")
  1432. self.view.window?.beginSheet(windowController.window!)
  1433. self.currentWindowController = windowController
  1434. windowController.documentURL = self.listView.document.documentURL
  1435. windowController.password = self.listView.document.password
  1436. windowController.itemClick = { [weak self] _ in
  1437. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1438. self?.currentWindowController = nil
  1439. self?.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
  1440. }
  1441. windowController.resultCallback = { [weak self] result, openDocument, fileURL, error in
  1442. if (result) {
  1443. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1444. self?.currentWindowController = nil
  1445. self?.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
  1446. if (openDocument) {
  1447. NSDocumentController.shared.openDocument(withContentsOf: fileURL, display: true) { document, result, error in }
  1448. } else {
  1449. NSWorkspace.shared.activateFileViewerSelecting([fileURL])
  1450. }
  1451. } else {
  1452. let alert = NSAlert()
  1453. alert.messageText = NSLocalizedString("Compress Faild", comment: "")
  1454. alert.runModal()
  1455. }
  1456. }
  1457. }
  1458. internal func showConvertWindow(type: KMToolbarType, identifier: String?) {
  1459. if let wc = self.currentWindowController as? KMConvertBaseWindowController, let _ = wc.window?.isSheet {
  1460. KMPrint("转档窗口已显示")
  1461. return
  1462. }
  1463. var windowController: KMConvertBaseWindowController?
  1464. if (type == .word) { /// Word
  1465. windowController = KMConvertWordWindowController()
  1466. } else if (type == .excel) {
  1467. windowController = KMConvertExcelWindowController()
  1468. } else if (type == .ppt || type == .rtf || type == .html || type == .conversion_text) {
  1469. windowController = KMConvertPPTsWindowController()
  1470. if (type == .ppt) {
  1471. windowController?.subType = 1
  1472. } else if (type == .rtf) {
  1473. windowController?.subType = 2
  1474. } else if (type == .html) {
  1475. windowController?.subType = 3
  1476. } else if (type == .conversion_text) {
  1477. windowController?.subType = 4
  1478. }
  1479. } else if (type == .csv) {
  1480. windowController = KMConvertCSVWindowController()
  1481. } else if (type == .conversion_image) {
  1482. windowController = KMConvertImageWindowController()
  1483. }
  1484. let model = KMDocumentModel(url: self.listView.document.documentURL)
  1485. if (self.listView.document.password != nil) {
  1486. let _ = model.unlock(self.listView.document.password)
  1487. }
  1488. model.currentIndex = self.listView.currentPageIndex
  1489. windowController?.documentModel = model
  1490. windowController?.itemClick = { [weak self] index in
  1491. if (identifier != nil) {
  1492. self?.toolbarController.cancelSelected(identifier!)
  1493. }
  1494. if (self?.currentWindowController == nil) {
  1495. return
  1496. }
  1497. self?.view.window?.endSheet(self!.currentWindowController.window!)
  1498. self?.currentWindowController = nil
  1499. }
  1500. self.view.window?.beginSheet((windowController?.window)!)
  1501. self.currentWindowController = windowController
  1502. }
  1503. internal func showPrintWindow(pageRange: KMPrintPageRange = KMPrintPageRange(type: .allPage, selectPages: [])) {
  1504. if (self.listView.document != nil && !self.listView.document.allowsPrinting) { // 有打印限制
  1505. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self] result ,password in
  1506. if (result == .cancel) {
  1507. return
  1508. }
  1509. // 解除权限
  1510. self?.listView.document.unlock(withPassword: password)
  1511. // 隐藏提示
  1512. self?.hiddenSecureLimitTip()
  1513. // 去打印
  1514. KMPrintWindowController.openDocument(inputDocument: self?.listView.document, inputPageRange: pageRange)
  1515. }
  1516. return
  1517. }
  1518. KMPrintWindowController.openDocument(inputDocument: self.listView?.document, inputPageRange: pageRange)
  1519. }
  1520. // MARK: - Split View
  1521. @IBAction func secondaryViewOpenFile(_ sender: NSButton) -> Void {
  1522. }
  1523. func singlePageScreen(isSinglePage: Bool, doublePagesScreen isHorizontal: Bool) -> Void {
  1524. if !isSinglePage && isHorizontal {
  1525. self.openSecondaryPdfView?.changeBenefit(ofContrastLabelWidth: 208)
  1526. }
  1527. if (!isSinglePage && !isHorizontal) {
  1528. self.openSecondaryPdfView?.changeBenefit(ofContrastLabelWidth: 300)
  1529. }
  1530. self.displaySinglePageScreen(isSinglePage: isSinglePage, doublePageScreen: isHorizontal)
  1531. }
  1532. func displaySinglePageScreen(isSinglePage: Bool, doublePageScreen isHorizontal: Bool) -> Void {
  1533. if isSinglePage {
  1534. pdfSplitView.dividerStyle = .thin
  1535. pdfSplitView.setPosition(mianSplitView.maxPossiblePositionOfDivider(at: 1), ofDividerAt: 0, animate: true)
  1536. } else {
  1537. pdfSplitView.dividerStyle = .paneSplitter
  1538. pdfSplitView.isVertical = !isHorizontal
  1539. pdfSplitView.setPosition(500, ofDividerAt: 0, animate: true)
  1540. if secondaryPdfView.document != nil {
  1541. // secondaryPdfView.frame = pdfSplitSecondView.frame
  1542. // pdfSplitSecondView.contentView = secondaryPdfView
  1543. } else {
  1544. openSecondaryPdfView = KMSecondaryViewController.init()
  1545. openSecondaryPdfView?.view.frame = pdfSplitSecondView.frame
  1546. pdfSplitSecondView.contentView = openSecondaryPdfView?.view
  1547. openSecondaryPdfView?.delegate = self
  1548. }
  1549. }
  1550. }
  1551. func lastSplitPDFHeightFloat(_ rect: NSRect) -> Float {
  1552. if (pdfSplitView.isVertical) {
  1553. return Float(NSWidth(rect))
  1554. } else {
  1555. return Float(NSHeight(rect))
  1556. }
  1557. }
  1558. func displaySecondaryPDFView(withUrl url: URL) -> Bool {
  1559. let document = CPDFDocument.init(url: url)
  1560. if document != nil {
  1561. secondaryPdfView.frame = pdfSplitSecondView.frame
  1562. pdfSplitSecondView.contentView = secondaryPdfView
  1563. secondaryPdfView.document = nil
  1564. secondaryPdfView.document = document
  1565. return true
  1566. } else {
  1567. return false
  1568. }
  1569. }
  1570. func updateNextAndPreViousButtonState() {
  1571. let item = self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentNextPageToolbarItemIdentifier)
  1572. let toItem = self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentPreviousPageToolbarItemIdentifier)
  1573. if self.listView.canGoToNextPage() {
  1574. item?.unEnabled = false
  1575. } else {
  1576. item?.unEnabled = true
  1577. }
  1578. if self.listView.canGoToPreviousPage() {
  1579. toItem?.unEnabled = false
  1580. } else {
  1581. toItem?.unEnabled = true
  1582. }
  1583. }
  1584. func updateZoomInOutButtonState() {
  1585. let item : KMToolBoxItem = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentZoomToolbarItemIdentifier))!
  1586. let toItem : KMToolBoxItem = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentZoomOutToolbarItemIdentifier))!
  1587. if self.listView.canZoomIn {
  1588. item.unEnabled = false
  1589. } else {
  1590. item.unEnabled = true
  1591. }
  1592. if self.listView.canZoomOut {
  1593. toItem.unEnabled = false
  1594. } else {
  1595. toItem.unEnabled = true
  1596. }
  1597. }
  1598. }
  1599. // MARK: -
  1600. // MARK: - KMSecondaryViewControllerDelegate
  1601. extension KMMainViewController: KMSecondaryViewControllerDelegate {
  1602. func receivedFileUrl(_ url: URL!) {
  1603. let state = self.displaySecondaryPDFView(withUrl: url)
  1604. if !state {
  1605. let alert = NSAlert()
  1606. alert.alertStyle = .critical
  1607. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  1608. alert.runModal()
  1609. return
  1610. }
  1611. }
  1612. }
  1613. // MARK: -
  1614. // MARK: - KMMainToolbarControllerDelegate
  1615. extension KMMainViewController : KMMainToolbarControllerDelegate {
  1616. func changeModelAction(mode: CToolMode) {
  1617. self.listView.toolMode = mode
  1618. if mode == .editPDFToolMode {
  1619. self.childToolbarController.updateType(newType: .editPDF)
  1620. }
  1621. }
  1622. func toolbarViewController(_ viewController:KMToolbarViewController, zoomModel selectedTag:Int) {
  1623. switch selectedTag {
  1624. case 2:
  1625. // self.listView.autoScales = !self.listView.autoScales
  1626. self.listView.autoScales = true
  1627. break
  1628. case 1:
  1629. if self.listView.scaleFactor != 1.0 {
  1630. self.listView.scaleFactor = 1.0
  1631. self.listView.autoScales = false
  1632. }
  1633. break
  1634. case 0:
  1635. self.listView.autoScales = true
  1636. self.listView.autoScales = false
  1637. break
  1638. case 3:
  1639. self.listView.scaleFactor = 0.1
  1640. break
  1641. case 4:
  1642. self.listView.scaleFactor = 0.25
  1643. break
  1644. case 5:
  1645. self.listView.scaleFactor = 0.5
  1646. break
  1647. case 6:
  1648. self.listView.scaleFactor = 0.75
  1649. break
  1650. case 7:
  1651. self.listView.scaleFactor = 1.0
  1652. break
  1653. case 8:
  1654. self.listView.scaleFactor = 1.5
  1655. break
  1656. case 9:
  1657. self.listView.scaleFactor = 2.0
  1658. break
  1659. case 10:
  1660. self.listView.scaleFactor = 4.0
  1661. break
  1662. case 11:
  1663. self.listView.scaleFactor = 8.0
  1664. break
  1665. case 12:
  1666. self.listView.scaleFactor = 10.0
  1667. break
  1668. default:
  1669. break
  1670. }
  1671. viewController.zoomTextField.stringValue = "\(Int(self.listView.scaleFactor*100))%"
  1672. }
  1673. func toolbarViewController(_ viewController: KMToolbarViewController, zoomSting: String) {
  1674. var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0)
  1675. if scale > 100 {
  1676. scale = 100
  1677. }
  1678. self.listView.scaleFactor = scale
  1679. viewController.zoomTextField.stringValue = "\(Int(self.listView.scaleFactor*100))%"
  1680. }
  1681. func changePDFViewZoomInAction() {
  1682. self.listView.zoomIn(nil)
  1683. self.updateZoomInOutButtonState()
  1684. }
  1685. func changePDFViewZoomOutAction() {
  1686. self.listView.zoomOut(nil)
  1687. self.updateZoomInOutButtonState()
  1688. }
  1689. func changePDFViewGotoNextPageAction() {
  1690. self.listView.goToNextPage(nil)
  1691. self.updateNextAndPreViousButtonState()
  1692. }
  1693. func changePDFViewGoToPreviousPageAction() {
  1694. self.listView.goToPreviousPage(nil)
  1695. self.updateNextAndPreViousButtonState()
  1696. }
  1697. func showPDFViewPrintViewController() {
  1698. // self.menuItemAnnotationClick_Print(sender: NSMenuItem())
  1699. self.showPrintWindow()
  1700. }
  1701. func toolbarViewController(_ viewController: KMToolbarViewController, shareDocument item: NSMenuItem) {
  1702. self.shareDocument(sender: viewController)
  1703. }
  1704. func toolbarViewController(_ viewController: KMToolbarViewController, shareFlatten item: NSMenuItem) {
  1705. self.shareFlatten(sender: viewController)
  1706. }
  1707. func toolbarViewController(_ viewController: KMToolbarViewController, shareOriginalPDF item: NSMenuItem) {
  1708. self.shareOriginalPDF(sender: viewController)
  1709. }
  1710. func toolbarViewController(_ viewController: KMToolbarViewController, scanOCRModel selectedTag: Int) {
  1711. if(0 == selectedTag) {
  1712. self.documentAIViewController?.enteredIncreaseAllPage()
  1713. self.documentAIViewController?.updateToolState(false)
  1714. } else {
  1715. // self.documentAIViewController?.recognitionPageString("1", with: .english)
  1716. // self.documentAIViewController?.recognitionPartModel(with: .english)
  1717. // self.documentAIViewController?.updateToolState(true)
  1718. }
  1719. // 0 : Scan 1:OCR Text
  1720. }
  1721. func mainToolDidClicked(_ toolController: KMToolbarController, _ type: KMToolbarViewType, _ item: KMToolBoxItem , _ pages: [Int]) {
  1722. print("mainToolDidClicked")
  1723. if(type != .Page) {
  1724. if (hasEnterPageEdit()) {
  1725. self.exitPageEdit()
  1726. }
  1727. }
  1728. if(type != .ScanOCR) {
  1729. if ((self.documentAIViewController?.view.superview) != nil) {
  1730. self.listView.isHidden = false
  1731. self.documentAIViewController?.creatRecognitionDocument()
  1732. self.documentAIViewController?.view .removeFromSuperview()
  1733. self.documentAIViewController = nil;
  1734. self.listView.layoutDocumentView()
  1735. }
  1736. }
  1737. if (type == .Page) {
  1738. if (hasEnterPageEdit()) {
  1739. self.exitPageEdit()
  1740. } else {
  1741. self.enterPageEdit(pages)
  1742. }
  1743. } else if(type == .ScanOCR) {
  1744. if((self.documentAIViewController?.view.superview) != nil) {
  1745. let alert = NSAlert()
  1746. alert.messageText = NSLocalizedString("If you need to edit again after exit, you need to re-execute OCR recognition, sure you want to exit?", comment: "")
  1747. alert.addButton(withTitle: "YES")
  1748. alert.addButton(withTitle: "NO")
  1749. let result = alert.runModal()
  1750. if (result == .alertFirstButtonReturn) { /// 取消
  1751. self.listView.isHidden = false
  1752. self.documentAIViewController?.creatRecognitionDocument()
  1753. self.documentAIViewController?.view .removeFromSuperview()
  1754. self.documentAIViewController = nil;
  1755. self.listView.layoutDocumentView()
  1756. } else {
  1757. self.toolbarController.toolbarType = .ScanOCR
  1758. }
  1759. } else {
  1760. self.documentAIViewController = KMDocumentAIViewController(pdfView: self.listView)
  1761. self.PDFContendView.addSubview(self.documentAIViewController?.view ?? KMDocumentAIViewController().view);
  1762. self.documentAIViewController?.view.frame = self.PDFContendView.bounds
  1763. self.documentAIViewController?.view.autoresizingMask = NSView.AutoresizingMask([.width,.height])
  1764. self.listView.isHidden = true //不影藏的话会出现鼠标事件的崩溃
  1765. }
  1766. }
  1767. }
  1768. func clickChildTool(type: KMToolbarType, index: Int) {
  1769. print("clickChildTool")
  1770. Task { @MainActor in
  1771. if (type == .compress) { /// 压缩
  1772. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  1773. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  1774. self.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
  1775. return
  1776. }
  1777. if (self.needSaveDocument()) {
  1778. self.saveDocumentWithProgressAlert { [unowned self] params in
  1779. self.showCompressWindow()
  1780. }
  1781. return
  1782. }
  1783. self.showCompressWindow()
  1784. return
  1785. }
  1786. if ((KMToolbarType.word.rawValue ... KMToolbarType.conversion_image.rawValue).contains(type.rawValue)) { /// 转档
  1787. var identifier: String?
  1788. if (type == .word) { /// Word
  1789. identifier = KMToolbarConversionWordItemIdentifier
  1790. } else if (type == .excel) {
  1791. identifier = KMToolbarConversionExcelItemIdentifier
  1792. } else if (type == .ppt) {
  1793. identifier = KMToolbarConversionPPTItemIdentifier
  1794. } else if (type == .rtf) {
  1795. identifier = KMToolbarConversionRTFItemIdentifier
  1796. } else if (type == .html) {
  1797. identifier = KMToolbarConversionHTMLItemIdentifier
  1798. } else if (type == .conversion_text) {
  1799. identifier = KMToolbarConversionTextItemIdentifier
  1800. } else if (type == .csv) {
  1801. identifier = KMToolbarConversionCSVItemIdentifier
  1802. } else if (type == .conversion_image) {
  1803. identifier = KMToolbarConversionImageItemIdentifier
  1804. }
  1805. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  1806. if (identifier != nil) {
  1807. self.toolbarController.cancelSelected(identifier!)
  1808. }
  1809. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  1810. return
  1811. }
  1812. if (self.needSaveDocument()) {
  1813. self.saveDocumentWithProgressAlert { [unowned self] params in
  1814. self.showConvertWindow(type: type, identifier: identifier)
  1815. }
  1816. return
  1817. }
  1818. self.showConvertWindow(type: type, identifier: identifier)
  1819. return
  1820. }
  1821. if (type == .merge) { /// 合并
  1822. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  1823. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  1824. self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
  1825. return
  1826. }
  1827. if (self.listView.document.allowsCopying == false || self.listView.document.allowsPrinting == false) {
  1828. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [unowned self] result , password in
  1829. if (result == .cancel) {
  1830. return
  1831. }
  1832. if (self.needSaveDocument()) {
  1833. self.saveDocumentWithProgressAlert { [unowned self] params in
  1834. self.showMergeWindow(password)
  1835. }
  1836. return
  1837. }
  1838. self.showMergeWindow(password)
  1839. }
  1840. return
  1841. }
  1842. if (self.needSaveDocument()) {
  1843. self.saveDocumentWithProgressAlert { [unowned self] params in
  1844. self.showMergeWindow(self.listView.document.password)
  1845. }
  1846. return
  1847. }
  1848. self.showMergeWindow(self.listView.document.password)
  1849. return
  1850. }
  1851. if (type == .crop) {
  1852. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  1853. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  1854. return
  1855. }
  1856. if (index == 1) {
  1857. self.cropCurrentPage()
  1858. return
  1859. }
  1860. if (index == 2) {
  1861. self.cropAllPage()
  1862. return
  1863. }
  1864. //// 自定义裁剪区域
  1865. // self.cropCustomArea()
  1866. } else if (type == .bates || type == .headerAndFooter || type == .background || type == .watermark) {
  1867. if (index == 0) {
  1868. var controller: KMWatermarkAdjectivePreViewBaseController!
  1869. if (type == .bates) {
  1870. controller = KMBatesPreviewController()
  1871. } else if (type == .headerAndFooter) {
  1872. controller = KMHeaderFooterPreviewController()
  1873. } else if (type == .background) {
  1874. controller = KMBackgroundController()
  1875. } else if (type == .watermark) {
  1876. controller = KMWatermarkViewController()
  1877. }
  1878. controller.applyDocument = self.listView.document
  1879. controller.view.frame = self.view.bounds
  1880. controller.view.autoresizingMask = [.width, .height]
  1881. controller.documentURL = self.listView.document.documentURL
  1882. self.view.addSubview(controller.view)
  1883. self.addChild(controller)
  1884. controller.itemClick = { [weak self] index, param in
  1885. if (index == 0 || index == 1) { /// 取消 和 应用
  1886. var controller_watermarkAdjective_preView: KMWatermarkAdjectivePreViewBaseController?
  1887. for i in 0 ..< self!.children.count {
  1888. let vc: NSViewController = self!.children[self!.children.count-1-i]
  1889. if (vc.isKind(of: KMWatermarkAdjectivePreViewBaseController.self) == true) {
  1890. controller_watermarkAdjective_preView = vc as! KMWatermarkAdjectivePreViewBaseController
  1891. break
  1892. }
  1893. }
  1894. if (controller_watermarkAdjective_preView == nil) {
  1895. return
  1896. }
  1897. controller_watermarkAdjective_preView!.view.removeFromSuperview()
  1898. controller_watermarkAdjective_preView?.removeFromParent()
  1899. // if (index == 1) {
  1900. self?.listView.document = CPDFDocument(url: self?.listView.document.documentURL)
  1901. self?.listView.layoutDocumentView()
  1902. self?.listView.setNeedsDisplayForVisiblePages()
  1903. // }
  1904. return
  1905. }
  1906. }
  1907. return
  1908. }
  1909. if (index == 1) { /// 取消
  1910. self.toolbarController.exitWatermarkAdjective()
  1911. // self.rightSideViewController.view.isHidden = true
  1912. self.rightSideViewController.isHidden = true
  1913. self.rightSideViewController.subViewType = .None
  1914. self.closeRightPane()
  1915. return
  1916. }
  1917. if (index == 5) { /// 批量
  1918. return
  1919. }
  1920. if (index == 4) { //// 移除
  1921. if (type == .watermark) {
  1922. let watermarks = self.listView.document.watermarks()
  1923. if (watermarks == nil || watermarks!.count <= 0) {
  1924. let alert = NSAlert()
  1925. alert.alertStyle = .warning
  1926. alert.messageText = NSLocalizedString("无法在本文件中找到可删除的水印。如果您看到水印,其不是使用PDF Master添加的,因此无法被检测到。", comment: "")
  1927. alert.addButton(withTitle: NSLocalizedString("确认", comment: ""))
  1928. alert.addButton(withTitle: NSLocalizedString("取消", comment: ""))
  1929. alert.runModal()
  1930. return
  1931. }
  1932. let alert = NSAlert()
  1933. alert.alertStyle = .warning
  1934. alert.messageText = NSLocalizedString("确定要删除文件水印吗?", comment: "")
  1935. alert.addButton(withTitle: NSLocalizedString("删除", comment: ""))
  1936. alert.addButton(withTitle: NSLocalizedString("取消", comment: ""))
  1937. let result = alert.runModal()
  1938. if (result != .alertFirstButtonReturn) {
  1939. return
  1940. }
  1941. }
  1942. let oType: KMWatermarkAdjectiveType = KMWatermarkAdjectiveTools.KMWatermarkAdjectiveType(from: type)
  1943. KMWatermarkAdjectiveTools.delete(oType, self.listView, self.listView.document.documentURL.path) {
  1944. result in
  1945. DispatchQueue.main.async {
  1946. self.listView.layoutDocumentView()
  1947. self.listView.setNeedsDisplayForVisiblePages()
  1948. }
  1949. if (result) {
  1950. let alert = NSAlert()
  1951. alert.alertStyle = .warning
  1952. alert.messageText = "成功"
  1953. alert.runModal()
  1954. } else {
  1955. let alert = NSAlert()
  1956. alert.alertStyle = .critical
  1957. alert.messageText = "失败"
  1958. alert.runModal()
  1959. }
  1960. }
  1961. return
  1962. }
  1963. if (index == 2) { /// 应用
  1964. let model = self.rightSideViewController.model
  1965. if (type == .bates || type == .headerAndFooter) {
  1966. if (model == nil || (model?.isKind(of: KMBatesModel.self) == false)) {
  1967. let alert = NSAlert()
  1968. alert.alertStyle = .critical
  1969. alert.messageText = "没有找到模型"
  1970. alert.runModal()
  1971. return
  1972. }
  1973. if ((model as! KMBatesModel).hasVaild == false) {
  1974. let alert = NSAlert()
  1975. alert.alertStyle = .critical
  1976. alert.messageText = "没有找到内容"
  1977. alert.runModal()
  1978. return
  1979. }
  1980. }
  1981. KMWatermarkAdjectiveTools.apply(model!, self.listView, self.listView.document.documentURL.path) {
  1982. result in
  1983. DispatchQueue.main.async {
  1984. self.listView.layoutDocumentView()
  1985. self.listView.setNeedsDisplayForVisiblePages()
  1986. }
  1987. if (result) {
  1988. let alert = NSAlert()
  1989. alert.alertStyle = .warning
  1990. alert.messageText = "成功"
  1991. alert.runModal()
  1992. } else {
  1993. let alert = NSAlert()
  1994. alert.alertStyle = .critical
  1995. alert.messageText = "失败"
  1996. alert.runModal()
  1997. }
  1998. }
  1999. return
  2000. }
  2001. /// 新增
  2002. // self.rightSideViewController.view.isHidden = false
  2003. self.rightSideViewController.isHidden = false
  2004. self.rightSideViewController.subViewType = KMWatermarkAdjectiveTools.KMToolBarTypeToRightSubViewType(type)
  2005. self.openRightPane()
  2006. } else if (type == .redact) { /// 标记密文
  2007. if (index == 1) { /// 取消
  2008. if (self.hasAddRedact) {
  2009. let alert = NSAlert()
  2010. 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: "")
  2011. alert.addButton(withTitle: NSLocalizedString("Don’t Save", comment: ""))
  2012. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  2013. let result = alert.runModal()
  2014. if (result == .alertFirstButtonReturn) {
  2015. self.listView.toolMode = .moveToolMode
  2016. self.toolbarController.exitRedact()
  2017. }
  2018. return
  2019. }
  2020. self.listView.toolMode = .moveToolMode
  2021. self.toolbarController.exitRedact()
  2022. return
  2023. }
  2024. if (index == 2) { /// 擦除
  2025. self.exeRedactConfirm(.eraserAll) {}
  2026. return
  2027. }
  2028. if (index == 3) { /// 应用
  2029. self.exeRedactConfirm(.redactAll) {}
  2030. return
  2031. }
  2032. if (index == 4) { /// 文本&图像
  2033. self.listView.toolMode = .redactToolMode
  2034. return
  2035. }
  2036. if (index == 5) { /// 页面
  2037. let windowController = KMRedactPageRangeWindowController(windowNibName: "KMRedactBaseWindowController")
  2038. await self.view.window?.beginSheet(windowController.window!)
  2039. self.currentWindowController = windowController
  2040. windowController.itemClick = { [weak self] index, value in
  2041. if (index == 1) {
  2042. let topBarView = self?.toolbarController.fetchTopBarView()
  2043. if (topBarView == nil || topBarView?.isKind(of: KMRedactTopToolBar.self) == false) {
  2044. return
  2045. }
  2046. (topBarView as! KMRedactTopToolBar).selectItem(0)
  2047. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  2048. self?.currentWindowController = nil
  2049. return
  2050. }
  2051. let windowController_pageRange = self?.currentWindowController as! KMRedactPageRangeWindowController
  2052. let pageType = windowController_pageRange.pageType
  2053. let pageString = windowController_pageRange.pageString
  2054. if (pageType == 5) { /// 自定义页面
  2055. let array = KMPageRangeTools.findSelectPage(pageRangeString: pageString, pageCount: Int((self?.listView.document.pageCount)!))
  2056. if (array.count == 0) {
  2057. let alert = NSAlert()
  2058. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  2059. alert.runModal()
  2060. return
  2061. }
  2062. }
  2063. self!.view.window?.endSheet(self!.currentWindowController.window!)
  2064. self!.currentWindowController = nil
  2065. var indexs: IndexSet = []
  2066. if (pageType == 1) { /// 当前页面
  2067. indexs.insert((self?.listView.currentPageIndex)!)
  2068. } else {
  2069. indexs = KMRedactTools.getPageIndexs(pageType-1, string: pageString, Int((self?.listView.document.pageCount)!))
  2070. }
  2071. if (indexs.count == 0) {
  2072. return
  2073. }
  2074. for i in indexs {
  2075. let page: CPDFPage = (self?.listView.document.page(at: UInt(i)))!
  2076. let redactAnno = CPDFRedactAnnotation(pdfListViewNoteWith: self?.listView.document)
  2077. redactAnno?.bounds = page.bounds
  2078. self?.listView.add(redactAnno, to: page)
  2079. }
  2080. }
  2081. return
  2082. }
  2083. } else if (type == .secure) { /// 安全
  2084. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  2085. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  2086. return
  2087. }
  2088. if (index == 1) { /// 设置密码
  2089. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  2090. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self] result, password in
  2091. if (result == .cancel) {
  2092. return
  2093. }
  2094. self?.isSaveKeyChain = false
  2095. self?.listView.document.unlock(withPassword: password)
  2096. self?.hiddenSecureLimitTip()
  2097. self?.showSecureWindow((self?.listView.document.documentURL)!)
  2098. }
  2099. return
  2100. }
  2101. self.showSecureWindow(self.listView.document.documentURL)
  2102. return
  2103. }
  2104. /// 删除安全性设置
  2105. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  2106. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self] result, password in
  2107. if (result == .cancel) {
  2108. return
  2109. }
  2110. self?.isSaveKeyChain = false
  2111. self?.listView.document.unlock(withPassword: password)
  2112. self?.hiddenSecureLimitTip()
  2113. let result = KMPasswordInputWindow.saveDocumentForRemovePassword((self?.listView.document)!)
  2114. if (result) {
  2115. self?.setDocument = CPDFDocument(url: self?.listView.document.documentURL)
  2116. }
  2117. let tip = KMRemovePasswordResultTipView()
  2118. tip.result = result ? .success : .failure
  2119. tip.showInView(superView: (self?.listView.superview)!)
  2120. }
  2121. return
  2122. }
  2123. let alert = NSAlert()
  2124. alert.messageText = NSLocalizedString("Are you sure you want to remove the security settings for the file?", comment: "")
  2125. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  2126. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  2127. let response = alert.runModal()
  2128. if (response != .alertFirstButtonReturn) {
  2129. return
  2130. }
  2131. let result = KMPasswordInputWindow.saveDocumentForRemovePassword((self.listView.document)!)
  2132. let tip = KMRemovePasswordResultTipView()
  2133. if result {
  2134. tip.result = .success
  2135. self._isPDFDocumentEdited = false
  2136. self.needSave = false
  2137. self.hiddenSecureLimitTip()
  2138. self.setDocument = CPDFDocument(url: self.listView.document.documentURL)
  2139. } else {
  2140. tip.result = .failure
  2141. }
  2142. tip.showInView(superView: self.listView.superview!)
  2143. }
  2144. }
  2145. }
  2146. }
  2147. // MARK: -
  2148. // MARK: - Mouse
  2149. extension KMMainViewController {
  2150. override func mouseDown(with event: NSEvent) {
  2151. super.mouseDown(with: event)
  2152. print("KMMainViewController mouseDown")
  2153. self.leftSideViewCancelSelect()
  2154. self.pageNumberDisplayView.isEdit = false
  2155. }
  2156. }
  2157. //MARK: LeftSideViewController
  2158. extension KMMainViewController {
  2159. func leftSideViewCancelSelect() {
  2160. switch self.leftSideViewController.type.methodType {
  2161. // case .Thumbnail:
  2162. // self.leftSideViewController.thumbnailViewController.cancelSelect()
  2163. case .Outline:
  2164. self.leftSideViewController.outlineViewController.cancelSelect()
  2165. case .BookMark:
  2166. self.leftSideViewController.bookViewController.cancelSelect()
  2167. case .Search:
  2168. self.leftSideViewController.searchViewController.cancelSelect()
  2169. case .Annotation:
  2170. self.leftSideViewController.annotationViewController.cancelSelect()
  2171. default:
  2172. print("其他")
  2173. }
  2174. }
  2175. }
  2176. extension KMMainViewController: KMReadModelViewDelegate {
  2177. func firstPageButtonDidChange(view: KMReadModelView, sender: Any) {
  2178. self.listView.goToFirstPage(nil)
  2179. }
  2180. func beforePageButtonDidChange(view: KMReadModelView, sender: Any) {
  2181. self.listView.goToPreviousPage(nil)
  2182. }
  2183. func afterPageButtonDidChange(view: KMReadModelView, sender: Any) {
  2184. self.listView.goToNextPage(nil)
  2185. }
  2186. func lastPageButtonDidChange(view: KMReadModelView, sender: Any) {
  2187. self.listView.goToLastPage(nil)
  2188. }
  2189. func currentPageButtonDidChange(view: KMReadModelView, sender: Any, pageIndex: Int) {
  2190. self.listView.go(toPageIndex: max(pageIndex - 1, 0), animated: true)
  2191. }
  2192. func scaleDidChange(view: KMReadModelView, sender: Any, type: KMReadModelViewScaleType) {
  2193. var scale: Double = 1.0
  2194. switch type {
  2195. case .fitWidth:
  2196. self.listView.autoScales = !self.listView.autoScales
  2197. scale = self.listView.scaleFactor
  2198. break
  2199. case .actualSize:
  2200. if self.listView.scaleFactor != 1.0 {
  2201. self.listView.scaleFactor = 1.0
  2202. self.listView.autoScales = false
  2203. }
  2204. scale = self.listView.scaleFactor
  2205. break
  2206. case .autoSize:
  2207. self.listView.autoScales = true
  2208. self.listView.autoScales = false
  2209. scale = self.listView.scaleFactor
  2210. break
  2211. case ._10:
  2212. scale = 0.1
  2213. break
  2214. case ._25:
  2215. scale = 0.25
  2216. break
  2217. case ._50:
  2218. scale = 0.5
  2219. break
  2220. case ._100:
  2221. scale = 1.0
  2222. break
  2223. case ._150:
  2224. scale = 1.5
  2225. break
  2226. case ._200:
  2227. scale = 2.0
  2228. break
  2229. case ._400:
  2230. scale = 4.0
  2231. break
  2232. case ._800:
  2233. scale = 8.0
  2234. break
  2235. default:
  2236. break
  2237. }
  2238. self.listView.scaleFactor = scale
  2239. self.toolbarController.mainViewController?.childToolbarController.zoomTextField.stringValue = "\(Int(self.listView.scaleFactor*100))%"
  2240. }
  2241. func cancelReadModel(view: KMReadModelView, sender: Any) {
  2242. if self.isReadMode {
  2243. self.closeReadModel()
  2244. }
  2245. }
  2246. func readModelViewWidthChange(view: KMReadModelView, width: CGFloat) {
  2247. // if self.readModelViewWidthConstraint.constant != width {
  2248. self.readModelViewWidthConstraint.constant = width
  2249. // }
  2250. }
  2251. }
  2252. extension KMMainViewController: KMPageNumberDisplayViewDelegate {
  2253. func gotoPageIndex(view: KMPageNumberDisplayView, pageIndex: Int) {
  2254. self.listView.go(toPageIndex: pageIndex, animated: true)
  2255. }
  2256. func updateWidth(view: KMPageNumberDisplayView, width: CGFloat) {
  2257. if self.tipCurrentPageBoxWidthConstraint.constant != width {
  2258. self.tipCurrentPageBoxWidthConstraint.constant = width
  2259. }
  2260. }
  2261. }