KMMainViewController+Action.swift 118 KB

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