KMMainViewController+UI.swift 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. //
  2. // KMMainViewController+UI.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Foundation
  8. extension KMMainViewController {
  9. // MARK: - 退出全屏
  10. @IBAction func exitFullScreen(_ sender: Any) {
  11. if (self.view.window == nil) {
  12. return
  13. }
  14. if (KMTools.isFullScreen(self.view.window!)) {
  15. self.view.window?.toggleFullScreen(nil)
  16. }
  17. }
  18. @IBAction func startSpeaking(_ sender: Any) {
  19. self.showTTSWindow()
  20. let ttsView = KMTTSWindowController.share
  21. ttsView.buttonItemClick_Play(ttsView.playButton)
  22. }
  23. @IBAction func stopSpeaking(_ sender: Any) {
  24. let ttsWindowC = KMTTSWindowController.share
  25. if ttsWindowC.pdfView?.document?.documentURL.path == self.listView.document?.documentURL.path {
  26. if let data = ttsWindowC.window?.isVisible, data {
  27. ttsWindowC.stopSpeaking()
  28. ttsWindowC.close()
  29. }
  30. }
  31. }
  32. }
  33. extension KMMainViewController {
  34. //通知
  35. func preferenceDidChangeNotification(notification:Notification) {
  36. let info : [AnyHashable : Any] = notification.userInfo ?? [:]
  37. if info.keys.contains(KMPreference.viewZoomScaleTypeKey) {
  38. self.selectZoom(KMPreferenceManager.shared.viewZoomScaleType)
  39. }
  40. if notification.name.rawValue == "pdfViewDocumentDidLoaded" || info.keys.contains(KMPreference.viewPageDisplayTypeKey) {
  41. self.selectDisplay(display: KMPreferenceManager.shared.viewPageDisplayType)
  42. }
  43. if info.keys.contains(KMPreference.displayBackgroundNormalColorKey) || notification.name.rawValue == "pdfViewDocumentDidLoaded" {
  44. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor
  45. self.listView.layoutDocumentView()
  46. }
  47. self.updatePageIndicatoreType()
  48. if (info.keys.contains(KMPreference.generalAuthorNameKey)) { // 作者名称
  49. CPDFKitConfig.sharedInstance().setAnnotationAuthor((info[KMPreference.generalAuthorNameKey] as! String))
  50. }
  51. if (info.keys.contains(KMPreference.highlightLinksKey)) {
  52. let hlLink = info[KMPreference.highlightLinksKey] as? Bool
  53. CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(hlLink == nil ? false : hlLink!)
  54. self.listView.setNeedsDisplayForVisiblePages()
  55. }
  56. if info.keys.contains(KMPreference.thumbPageSizeKey) {
  57. self.leftSideViewController.refreshUIOfThumbnailIfNeed(preference: true)
  58. }
  59. if info.keys.contains(KMPreference.thumbSnapshotSizeKey) {
  60. self.leftSideViewController.refreshUIOfSnapshotIfNeed(preference: true)
  61. }
  62. if info.keys.contains(KMPreference.outlineFontSizeKey) {
  63. self.leftSideViewController.updateTableFont()
  64. }
  65. if info.keys.contains(KMPreference.greekThresholdKey) {
  66. let value = KMPreference.shared.greekThreshold.cgFloat
  67. self.listView.setGreekingThreshold(value)
  68. self.secondaryPdfView?.setGreekingThreshold(value)
  69. }
  70. if info.keys.contains(KMPreference.antiAliasTextKey) {
  71. self.listView.setShouldAntiAlias(KMPreference.shared.antiAliasText)
  72. self.listView.applyDefaultInterpolationQuality()
  73. self.secondaryPdfView?.setShouldAntiAlias(KMPreference.shared.antiAliasText)
  74. self.secondaryPdfView?.applyDefaultInterpolationQuality()
  75. }
  76. if info.keys.contains(KMPreference.markupColorHighlightKey) {
  77. if let item = self.toolbarController.findItem(KMToolbarHighlightAnnotationItemIdentifier) {
  78. if item.isSelected {
  79. let color = KMPreference.shared.markupHighlightColor
  80. let imageV = KMNoteTypeImageView()
  81. item.image = imageV.noteTypeImage(withType: SKNHighlightString, color: color)
  82. }
  83. }
  84. if let item = self.toolbarController.findItem(KMToolbarSquigglyAnnotationItemIdentifier) {
  85. if item.isSelected {
  86. if let color = UserDefaults.standard.PDFListViewColor(forKey: CSquigglyNoteColorKey) {
  87. let imageV = KMNoteTypeImageView()
  88. item.image = imageV.noteTypeImage(withType: "Squiggly", color: color)
  89. }
  90. }
  91. }
  92. }
  93. if info.keys.contains(KMPreference.markupColorUnderlineKey) {
  94. if let item = self.toolbarController.findItem(KMToolbarUnderlineAnnotationItemIdentifier) {
  95. if item.isSelected {
  96. let color = KMPreference.shared.markupUnderlineColor
  97. let imageV = KMNoteTypeImageView()
  98. item.image = imageV.noteTypeImage(withType: SKNUnderlineString, color: color)
  99. }
  100. }
  101. }
  102. if info.keys.contains(KMPreference.markupColorStrikthroughKey) {
  103. if let item = self.toolbarController.findItem(KMToolbarStrikeOutAnnotationItemIdentifier) {
  104. if item.isSelected {
  105. let color = KMPreference.shared.markupStrikthroughColor
  106. let imageV = KMNoteTypeImageView()
  107. item.image = imageV.noteTypeImage(withType: SKNStrikeOutString, color: color)
  108. }
  109. }
  110. }
  111. if info.keys.contains(KMPreference.markupColorPenKey) {
  112. if let item = self.toolbarController.findItem(KMToolbarInkAnnotationItemIdentifier) {
  113. if item.isSelected {
  114. let color = KMPreference.shared.markupPenColor
  115. let imageV = KMNoteTypeImageView()
  116. item.image = imageV.noteTypeImage(withType: SKNInkString, color: color)
  117. }
  118. }
  119. }
  120. if info.keys.contains(KMPreference.editPDFPopWindowShowKey) {
  121. let show = KMPreference.shared.editPDFPopWindowIsShow
  122. let win = KMEditPDFPopToolBarWindow.shared
  123. if !show && win.isVisible {
  124. win.orderOut(nil)
  125. win.setIsVisible(false)
  126. }
  127. }
  128. }
  129. //刷新页面显示器
  130. func updatePageIndicatoreType () {
  131. if self.isReadMode {
  132. self.pageNumberDisplayView.outView()
  133. } else {
  134. if self.listView.document != nil {
  135. self.pageNumberDisplayView.totalPagesCount = Int(self.listView.document.pageCount)
  136. }
  137. self.pageNumberDisplayView.currentPageIndex = self.listView.currentPageIndex
  138. self.pageNumberDisplayView.displayType = KMPreferenceManager.shared.pageIndicatorType
  139. self.readContentView.addSubview(self.tipCurrentPageBox, positioned: .above, relativeTo: self.readContentView)
  140. }
  141. }
  142. func showConvertNotesProgress() {
  143. guard let pdfDocument = self.document else {
  144. return
  145. }
  146. guard let _ = self.listView?.document else {
  147. return
  148. }
  149. guard let _ = self.view.window else {
  150. return
  151. }
  152. let isNewCreated = (self.myDocument as? KMMainDocument)?.isNewCreated ?? false
  153. if isNewCreated {
  154. return
  155. }
  156. self.model.needConvertNotes = false
  157. Task { @MainActor in
  158. self.convertNotesUsingPDFDocument(pdfDocument)
  159. }
  160. }
  161. func convertNotesUsingPDFDocument(_ pdfDocument: CPDFDocument) {
  162. guard let doc = self.listView?.document else {
  163. return
  164. }
  165. DispatchQueue.main.async {
  166. self.beginProgressSheet(withMessage: "", maxValue: 0)
  167. let count = pdfDocument.pageCount
  168. DispatchQueue.global().async {
  169. // var addAnnotations = [CPDFAnnotation]()
  170. // var removeAnnotations = [CPDFAnnotation]()
  171. self.model.addAnnotations.removeAll()
  172. self.model.removeAnnotations.removeAll()
  173. for i in 0..<count {
  174. let page = pdfDocument.page(at: i)
  175. // var addAnnotations = [CPDFAnnotation]()
  176. // var removeAnnotations = [CPDFAnnotation]()
  177. for annotation in page?.annotations ?? [] {
  178. var newAnnotation: CPDFAnnotation?
  179. if let inkAnnotation = annotation as? CPDFInkAnnotation, inkAnnotation.contents.hasPrefix("<?xml version=\"1.0\" encoding=\"utf-8\"?>") {
  180. let table = KMTableAnnotation(KMNoteBounds: inkAnnotation.bounds, document: doc)
  181. table.border = inkAnnotation.border
  182. table.color = inkAnnotation.color
  183. table.createForm(withList: inkAnnotation.contents, andPaths: inkAnnotation.bezierPaths())
  184. table.updateAppearanceInk(withIsAdd: false)
  185. table.contents = annotation.contents
  186. newAnnotation = table
  187. }
  188. if let newAnnotation = newAnnotation {
  189. self.model.addAnnotations.append(newAnnotation)
  190. self.model.removeAnnotations.append(annotation)
  191. }
  192. }
  193. // for i in 0..<addAnnotations.count {
  194. // let newAnnotation = addAnnotations[i]
  195. // let annotation = removeAnnotations[i]
  196. //
  197. // // this is only to make sure markup annotations generate the lineRects, for thread safety
  198. // pdfView?.addAnnotation(with: newAnnotation, to: page)
  199. // pdfView?.remove(annotation)
  200. // if newAnnotation.contents != nil {
  201. // if newAnnotation.contents.count == 0 {
  202. // newAnnotation.autoUpdateString()
  203. // }
  204. // }
  205. // }
  206. }
  207. DispatchQueue.main.async {
  208. for i in 0..<self.model.addAnnotations.count {
  209. let newAnnotation = self.model.addAnnotations[i]
  210. let annotation = self.model.removeAnnotations[i]
  211. let page = annotation.page
  212. // this is only to make sure markup annotations generate the lineRects, for thread safety
  213. self.listView?.addAnnotation(with: newAnnotation, to: page)
  214. self.listView?.remove(annotation)
  215. if newAnnotation.contents != nil {
  216. if newAnnotation.contents.count == 0 {
  217. newAnnotation.autoUpdateString()
  218. }
  219. }
  220. }
  221. self.dismissProgressSheet()
  222. self.listView?.undoManager?.removeAllActions()
  223. self.undoManager?.removeAllActions()
  224. // 清空数据
  225. self.model.addAnnotations.removeAll()
  226. self.model.removeAnnotations.removeAll()
  227. }
  228. }
  229. }
  230. }
  231. // MARK: - KMInterfaceThemeChangedProtocol
  232. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  233. super.interfaceThemeDidChanged(appearance)
  234. self.toolbarController.interfaceThemeDidChanged(appearance)
  235. self.leftSideViewController.interfaceThemeDidChanged(appearance)
  236. self.rightSideViewController.interfaceThemeDidChanged(appearance)
  237. self.pdfEditController?.interfaceThemeDidChanged(appearance)
  238. self.mianSplitView.superview?.wantsLayer = true
  239. // let sView = self.listView.documentView()
  240. if KMAppearance.isDarkMode() {
  241. self.mianSplitView.superview?.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  242. // sView?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  243. } else {
  244. self.mianSplitView.superview?.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  245. // sView?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1)
  246. }
  247. }
  248. }
  249. // MARK: - KMToolbarControllerDelegate
  250. extension KMMainViewController: KMToolbarControllerDelegate {
  251. func toolbarController(_ viewController: KMToolbarController, heightOffsetChange heightOffset: Float, animated: Bool) {
  252. let heightConst = (animated ? self.heightOffset.animator() : self.heightOffset)
  253. if self.isReadMode {
  254. if self.toolbarController.toolbarType == .Annatiton {
  255. heightConst?.constant = CGFloat(heightOffset)
  256. } else {
  257. heightConst?.constant = CGFloat(0)
  258. }
  259. } else {
  260. heightConst?.constant = CGFloat(heightOffset)
  261. }
  262. self.refreshListViewToolMode()
  263. self.trackEvent(toolMode: self.listView.toolMode)
  264. if self.listView.annotationType == .editTextImage ||
  265. self.listView.annotationType == .addText ||
  266. self.listView.annotationType == .addImage {
  267. self.closeRightPane()
  268. }
  269. self.listView.annotationType = .unkown
  270. }
  271. func changeAnnotationModeAction(item: KMToolbarClickButton) {
  272. if self.listView.toolMode == .formToolMode {
  273. if !IAPProductsManager.default().isAvailableAllFunction(){
  274. let winC = KMPurchaseCompareWindowController.sharedInstance()
  275. if let type = CAnnotationType(rawValue: item.tag) {
  276. if type == .textField {
  277. winC?.kEventName = "Reading_TextField_BuyNow"
  278. } else if type == .checkBox {
  279. winC?.kEventName = "Reading_CheckBox_BuyNow"
  280. } else if type == .radioButton {
  281. winC?.kEventName = "Reading_RadioButton_BuyNow"
  282. } else if type == .listMenu {
  283. winC?.kEventName = "Reading_ListBox_BuyNow"
  284. } else if type == .comboBox {
  285. winC?.kEventName = "Reading_ComboBox_BuyNow"
  286. } else if type == .actionButton {
  287. winC?.kEventName = "Reading_Button_BuyNow"
  288. }
  289. }
  290. winC?.showWindow(nil)
  291. return
  292. }
  293. } else {
  294. let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
  295. if type == .signature {
  296. if !IAPProductsManager.default().isAvailableAllFunction(){
  297. let winC = KMPurchaseCompareWindowController.sharedInstance()
  298. winC?.kEventName = "Reading_ElectronicSign_BuyNow"
  299. winC?.showWindow(nil)
  300. return
  301. }
  302. }
  303. }
  304. if (self.model.rightMouseEventing) {
  305. self.model.rightMouseEventing = false
  306. }
  307. if (self.toolbarController.ignoreCurrentAnnotationTypeChange) {
  308. self.toolbarController.ignoreCurrentAnnotationTypeChange = false
  309. }
  310. var currentType = self.listView.annotationType
  311. // if (currentType == .eraser) {
  312. // currentType = .ink
  313. // }
  314. let isSelected = (currentType == CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown) && ((self.listView.toolMode == .noteToolMode) || (self.listView.toolMode == .selfSignMode) || (self.listView.toolMode == .formToolMode))
  315. var editSelectd = false
  316. if self.listView.annotationType == CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown && self.listView.toolMode == .editPDFToolMode {
  317. editSelectd = true
  318. }
  319. if isSelected {
  320. if self.listView.toolMode == .formToolMode {
  321. } else {
  322. self.listView.toolMode = .textToolMode
  323. }
  324. let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
  325. if type == .signature {
  326. self.toolbarController.findItem(KMToolbarToolSignBoxIdentifier)?.isSelected = false
  327. if self.listView.toolMode == .formToolMode {
  328. self.listView.toolMode = .noteToolMode
  329. }
  330. }
  331. self.listView.annotationType = .unkown
  332. self.closeRightPane()
  333. } else {
  334. let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
  335. self.trackEvent(annotationType: type)
  336. if type == .signature {
  337. if self.listView.toolMode != .formToolMode {
  338. self.listView.toolMode = .formToolMode
  339. self.toolbarController.findItem(KMToolbarToolSignBoxIdentifier)?.isSelected = true
  340. }
  341. }
  342. self.listView.annotationType = type
  343. if self.listView.currentSelection != nil {
  344. let annotation = self.listView.addAnnotation(with: CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown, selection: self.listView.currentSelection, page: self.listView.currentSelection.page, bounds: self.listView.currentSelection.bounds)
  345. if((annotation != nil) &&
  346. !self.listView.activeAnnotations.contains(annotation ?? self.listView.activeAnnotation)) {
  347. var newAnnonations : [CPDFAnnotation] = []
  348. newAnnonations.append(annotation!)
  349. self.listView.updateActiveAnnotations(newAnnonations)
  350. self.listView.setNeedsDisplayAnnotationViewFor(annotation?.page)
  351. }
  352. }
  353. if self.listView.toolMode == .formToolMode {
  354. } else {
  355. self.refreshListViewToolMode()
  356. }
  357. }
  358. if self.listView.toolMode != .editPDFToolMode && self.listView.annotationType != .unkown {
  359. self.rightSideViewController.isHidden = false
  360. let toolMode = self.listView?.toolMode ?? .none
  361. self.rightSideViewController.kEventTag = toolMode == .selfSignMode ? 1 : 0
  362. self.rightSideViewController.subViewType = .AnnotationProperts
  363. if !self.isReadMode {
  364. self.openRightPane()
  365. }
  366. } else if self.listView.toolMode == .editPDFToolMode {
  367. if editSelectd {
  368. self.rightSideViewController.isHidden = true
  369. self.listView.setShouAddEdit([])
  370. self.listView.change([.text, .image])
  371. self.listView.annotationType = .editTextImage
  372. self.closeRightPane()
  373. } else if self.listView.annotationType == .addText {
  374. self.rightSideViewController.isHidden = false
  375. if !self.isReadMode {
  376. self.openRightPane()
  377. }
  378. self.rightSideViewController.subViewType = .EditPDFAddText
  379. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_Btn" : "Btn_SubTbr_EditPDF_AddText"])
  380. self.listView.setShouAddEdit(.text)
  381. self.listView.change(.text)
  382. } else if self.listView.annotationType == .addImage {
  383. self.rightSideViewController.isHidden = false
  384. if !self.isReadMode {
  385. self.openRightPane()
  386. }
  387. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_Btn" : "Btn_SubTbr_EditPDF_AddImage"])
  388. // self.closeRightPane()
  389. self.rightSideViewController.subViewType = .EditPDFAddImage
  390. self.listView.setShouAddEdit(.image)
  391. self.listView.change(.image)
  392. self.rightSideViewController.eidtPDFImageProperty.reloadData()
  393. }
  394. } else {
  395. self.rightSideViewController.isHidden = true
  396. self.closeRightPane()
  397. }
  398. }
  399. func refreshListViewToolMode () {
  400. if self.toolbarController.toolbarType == .Annatiton {
  401. self.listView.toolMode = .noteToolMode
  402. } else if self.toolbarController.toolbarType == .Move {
  403. self.listView.toolMode = .moveToolMode
  404. } else if self.toolbarController.toolbarType == .Magnify {
  405. self.listView.toolMode = .magnifyToolMode
  406. } else if self.toolbarController.toolbarType == .Select {
  407. self.listView.toolMode = .selectToolMode
  408. } else if self.toolbarController.toolbarType == .SelectZoom {
  409. self.listView.toolMode = .selectZoomToolMode
  410. } else if self.toolbarController.toolbarType == .Page {
  411. } else if self.toolbarController.toolbarType == .Form {
  412. self.listView.toolMode = .formToolMode
  413. self.rightSideViewController.subViewType = .AnnotationProperts
  414. } else if self.toolbarController.toolbarType == .FillSign {
  415. self.listView.toolMode = .selfSignMode
  416. self.rightSideViewController.subViewType = .AnnotationProperts
  417. } else if self.toolbarController.toolbarType == .editPDF {
  418. self.listView.toolMode = .editPDFToolMode
  419. // self.listView.editingConfig().isShowEditingAreaHover = true
  420. // self.listView.editingConfig().editingBorderWidth = 1
  421. // self.listView.editingConfig().editingCenterRadius = 5
  422. // self.listView.editingConfig().editAreaMargin = 2
  423. // self.listView.editingConfig().editingHoverBorderColor = NSColor.init(red: 0, green: 136.0/255.0, blue: 1.0, alpha: 0.5)
  424. // self.listView.editingConfig().editingBorderColor = NSColor.init(red: 0, green: 0, blue: 0, alpha: 0.4)
  425. // self.listView.editingConfig().addEditAreaColor = NSColor.init(red: 23.0/255.0, green: 112.0/255.0, blue: 244.0/255.0, alpha: 0.5)
  426. // self.listView.editingConfig().editAreaMoveFillColor = NSColor.init(red: 23.0/255.0, green: 112.0/255.0, blue: 244.0/255.0, alpha: 0.1)
  427. self.listView.setShouAddEdit([])
  428. }else if self.toolbarController.toolbarType == .LeftPanel {
  429. self.toggleLeftPane()
  430. self.leftSideViewController.showPanelView(show: true)
  431. }else {
  432. self.listView.toolMode = .textToolMode
  433. }
  434. }
  435. func showPDFLayoutModeAction(show: Bool) {
  436. self.listView.annotationType = .unkown
  437. self.showOrHideNotes()
  438. }
  439. }
  440. // MARK: - NSSplitViewDelegate
  441. extension KMMainViewController : NSSplitViewDelegate {
  442. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  443. if splitView == self.mianSplitView {
  444. // return (subview == self.listView) == false
  445. } else if splitView == self.newPDFSplitView {
  446. return subview.isEqual(to: self.secondaryPdfContentView)
  447. }
  448. return false
  449. }
  450. func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool {
  451. if splitView == self.newPDFSplitView {
  452. if subview.isEqual(to: self.secondaryPdfContentView) {
  453. var position = self.newPDFSplitView.maxPossiblePositionOfDivider(at: dividerIndex)
  454. if self.newPDFSplitView.isSubviewCollapsed(subview) {
  455. if self.lastSplitPDFHeight <= 0.0 {
  456. let DEFAULT_SPLIT_PANE_HEIGHT: Float = 200.0
  457. self.lastSplitPDFHeight = DEFAULT_SPLIT_PANE_HEIGHT
  458. }
  459. if self.lastSplitPDFHeight.cgFloat > NSHeight(self.pdfContentView.frame) {
  460. self.lastSplitPDFHeight = floorf(0.5 * Float(NSHeight(self.pdfContentView.frame)))
  461. }
  462. position += self.lastSplitPDFHeight.cgFloat
  463. } else {
  464. self.lastSplitPDFHeight = Float(NSHeight(subview.frame))
  465. }
  466. self.pdfSplitView.setPosition(position, ofDividerAt: dividerIndex, animate: true)
  467. }
  468. return false
  469. }
  470. return false
  471. }
  472. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  473. if splitView == self.mianSplitView {
  474. return splitView == self.mianSplitView
  475. } else if splitView == self.newPDFSplitView {
  476. return false
  477. }
  478. return false
  479. }
  480. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  481. if splitView == mianSplitView {
  482. let splitViewWidth = splitView.frame.width
  483. let dividerIndexX0 = self.model.panelWidth + functionWidth
  484. if dividerIndex == 0 {
  485. if self.model.leftPanelOpen {
  486. return CGFloat(dividerIndexX0 * 1.5)
  487. } else {
  488. return functionWidth
  489. }
  490. } else if dividerIndex == 1 {
  491. return splitViewWidth
  492. }
  493. } else if splitView == self.newPDFSplitView {
  494. if dividerIndex == 0 {
  495. let MIN_SIDE_PANE_WIDTH = 270
  496. return proposedMaximumPosition + MIN_SIDE_PANE_WIDTH.cgFloat
  497. }
  498. }
  499. return proposedMaximumPosition
  500. }
  501. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  502. if splitView == mianSplitView {
  503. let splitViewWidth = splitView.frame.width
  504. let dividerIndexX1 = splitViewWidth - self.model.defaultRightWidth
  505. if dividerIndex == 0 {
  506. return functionWidth
  507. } else if dividerIndex == 1 {
  508. return dividerIndexX1
  509. }
  510. } else if splitView == self.newPDFSplitView {
  511. }
  512. return proposedMinimumPosition
  513. }
  514. func splitView(_ splitView: NSSplitView, resizeSubviewsWithOldSize oldSize: NSSize) {
  515. if splitView.isEqual(to: mianSplitView) {
  516. let leftView = splitView.subviews[0]
  517. let centerView = splitView.subviews[1]
  518. let rightView = splitView.subviews[2]
  519. // let centerView = self.pdfSplitView ?? splitView.subviews[1]
  520. // let rightView = self.rightView ?? splitView.subviews[2]
  521. // 左边是否收起
  522. let leftCollapsed = splitView.isSubviewCollapsed(leftView)
  523. // 右边是否收起
  524. let rightCollapsed = splitView.isSubviewCollapsed(rightView)
  525. var leftSize = leftView.frame.size
  526. var mainSize = centerView.frame.size
  527. var rightSize = rightView.frame.size
  528. var contentWidth = splitView.frame.width
  529. if leftCollapsed {
  530. leftSize.width = 0.0
  531. } else {
  532. contentWidth = contentWidth - splitView.dividerThickness
  533. }
  534. if rightCollapsed {
  535. rightSize.width = 0.0
  536. } else {
  537. contentWidth = contentWidth - splitView.dividerThickness
  538. }
  539. if contentWidth < leftSize.width + rightSize.width {
  540. var oldContentWidth = oldSize.width
  541. if leftCollapsed == false {
  542. oldContentWidth = oldContentWidth - splitView.dividerThickness
  543. }
  544. if rightCollapsed == false {
  545. oldContentWidth = oldContentWidth - splitView.dividerThickness
  546. }
  547. let resizeFactor = contentWidth / oldContentWidth
  548. leftSize.width = floor(resizeFactor*leftSize.width)
  549. rightSize.width = floor(resizeFactor*rightSize.width)
  550. }
  551. mainSize.width = contentWidth - leftSize.width - rightSize.width
  552. leftSize.height = splitView.frame.height
  553. rightSize.height = splitView.frame.height
  554. mainSize.height = splitView.frame.height
  555. if leftCollapsed == false {
  556. leftView.setFrameSize(leftSize)
  557. }
  558. if rightCollapsed == false {
  559. rightView.setFrameSize(rightSize)
  560. }
  561. centerView.setFrameSize(mainSize)
  562. } else if splitView == self.newPDFSplitView {
  563. // let firstV = splitView.subviews.first
  564. // let lastV = splitView.subviews.last
  565. // if firstV == lastV{
  566. // splitView.adjustSubviews()
  567. // return
  568. // }
  569. // if self.newPDFSplitView.isVertical { // 横向
  570. // // 左边是否收起
  571. // let leftCollapsed = splitView.isSubviewCollapsed(firstV!)
  572. // // 右边是否收起
  573. // let rightCollapsed = splitView.isSubviewCollapsed(lastV!)
  574. //
  575. // var contentSize = splitView.frame.size
  576. // firstV?.setFrameSize(NSMakeSize(contentSize.width * 0.5, contentSize.height))
  577. // lastV?.setFrameSize(NSMakeSize(contentSize.width * 0.5, contentSize.height))
  578. // } else { // 纵向
  579. // // 左边是否收起
  580. // let leftCollapsed = splitView.isSubviewCollapsed(firstV!)
  581. // // 右边是否收起
  582. // let rightCollapsed = splitView.isSubviewCollapsed(lastV!)
  583. //
  584. // var contentSize = splitView.frame.size
  585. // firstV?.setFrameSize(NSMakeSize(contentSize.width, contentSize.height * 0.5))
  586. // lastV?.setFrameSize(NSMakeSize(contentSize.width, contentSize.height * 0.5))
  587. // }
  588. }
  589. splitView.adjustSubviews()
  590. }
  591. func splitViewWillResizeSubviews(_ notification: Notification) {
  592. let defaultView = KMSplitView()
  593. let sender : NSSplitView = notification.object as? NSSplitView ?? defaultView
  594. if (sender == self.mianSplitView || sender == defaultView) && self.view.window?.frameAutosaveName != nil{
  595. // let leftWidth = self.mianSplitView.isSubviewCollapsed(self.leftView) ? 0.0 : leftSideViewController.view.frame.width
  596. // if leftPanelOpen {
  597. // if leftWidth < 100 && lastLeftPanWidth > functionWidth{
  598. // self.closeLeftPane()
  599. // }
  600. // }
  601. } else {
  602. if sender == self.pdfSplitView {
  603. KMPrint("")
  604. }
  605. }
  606. }
  607. func splitViewDidResizeSubviews(_ notification: Notification) {
  608. self.locationPageView.frame = CGRect(x: 0, y: self.listView.frame.maxY-32, width: self.listView.frame.width, height: 32)
  609. let defaultView = KMSplitView()
  610. let sender : NSSplitView = notification.object as? NSSplitView ?? defaultView
  611. if (sender == self.mianSplitView || sender == defaultView) && self.view.window?.frameAutosaveName != nil{
  612. let leftWidth = self.mianSplitView.isSubviewCollapsed(self.leftView) ? 0.0 : leftSideViewController.view.frame.width
  613. let rightWidth = self.mianSplitView.isSubviewCollapsed(self.rightView) ? 0.0 : rightSideViewController.view.frame.width
  614. UserDefaults.standard.set(leftWidth, forKey: CPDFOfficeLeftSidePaneWidthKey)
  615. UserDefaults.standard.set(rightWidth, forKey: CPDFOfficeRightSidePaneWidthKey)
  616. } else {
  617. if sender == self.pdfSplitView {
  618. KMPrint("")
  619. }
  620. }
  621. }
  622. func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  623. if splitView == self.mianSplitView {
  624. if dividerIndex == 0 {
  625. if self.model.leftPanelOpen {
  626. //当左边栏开启式,拖拽时不改变宽度,当宽度达到原宽度的一半时关闭窗口
  627. //如果 isShowPanel 为 true 那么将不会收缩
  628. if (proposedPosition < functionWidth + self.model.panelWidth * 0.5) && !self.leftSideViewController.isShowPanel {
  629. self.closeLeftPane()
  630. self.leftSideViewController.refreshMethodType(methodType: .None)
  631. return functionWidth
  632. } else if proposedPosition > self.model.panelWidth + functionWidth {
  633. return proposedPosition
  634. } else {
  635. return self.model.panelWidth + functionWidth
  636. }
  637. } else {
  638. return functionWidth
  639. }
  640. }
  641. } else if splitView == self.newPDFSplitView {
  642. }
  643. return proposedPosition
  644. }
  645. }
  646. // MARK: - KMLeftSideViewControllerDelegate
  647. extension KMMainViewController: KMLeftSideViewControllerDelegate {
  648. func controlStateChange(_ obj: KMLeftSideViewController, show: Bool) {
  649. if show {
  650. self.openLeftPane()
  651. } else {
  652. self.closeLeftPane()
  653. }
  654. }
  655. func enterEditMode(_ obj: KMLeftSideViewController, _ pages: [Int]) {
  656. let item : KMToolbarItemView = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentPageToolbarItemIdentifier))!
  657. self.toolbarController.mainToolBarView?.delegate?.toolbarViewController?(self.toolbarController.mainToolBarView!, clickMode: .Page, toolbar: item, pages)
  658. }
  659. func searchAction(searchString: String,isCase:Bool) {
  660. self.search(searchString: searchString,isCase: isCase)
  661. }
  662. func controller(_ controller: KMLeftSideViewController, dispayDidChange dispay: KMPDFDisplayType) {
  663. self.selectDisplay(display: dispay, viewSettingIsReload: false)
  664. }
  665. func controller(controller: KMLeftSideViewController, itemClick item: Any?, itemKey: KMItemKey, params: Any?) {
  666. if (itemKey == .print) { // 打印
  667. if params is NSImage {
  668. KMPrintWindowController.cpdf_printImage(image: params as! NSImage)
  669. return
  670. }
  671. var pageRange: KMPrintPageRange = KMPrintPageRange(type: .allPage, selectPages: [], reversePrintOrder: false)
  672. if (params != nil) {
  673. pageRange.type = .custom
  674. pageRange.selectPages = params as! [Int]
  675. }
  676. self.showPrintWindow(pageRange: pageRange)
  677. } else if (itemKey == .cut || itemKey == .paste || itemKey == .delete || itemKey == .leftRotate || itemKey == .rightRotate) {
  678. self.recordIsPDFDocumentEdited(type: itemKey.toSubscribeWaterMarkType())
  679. } else if itemKey == .pageEdit {
  680. self.toolbarController.clickItem(KMDocumentPageToolbarItemIdentifier)
  681. } else if itemKey == .demote { // 大纲降级
  682. guard let currentOutline: CPDFOutline = controller.tocOutlineView.km.clickedItem() else {
  683. return
  684. }
  685. self.listView?.demote(outline: currentOutline)
  686. } else if itemKey == .promote { // 大纲升级
  687. guard let currentOutline: CPDFOutline = controller.tocOutlineView.km.clickedItem() else {
  688. return
  689. }
  690. self.listView?.promote(outline: currentOutline)
  691. }
  692. }
  693. func controller(controller: KMLeftSideViewController, bookMarkDidChange bookMarks: [KMBookMarkItem]) {
  694. self.needSave = true
  695. self.recordIsPDFDocumentEdited()
  696. }
  697. func controller(controller: KMLeftSideViewController, rotateType: KMRotateType) {
  698. if rotateType == .clockwise {
  699. self.menuItemAction_rotateRight((Any).self)
  700. } else if rotateType == .anticlockwise {
  701. self.menuItemAction_rotateLeft((Any).self)
  702. }
  703. }
  704. func controller(controller: KMLeftSideViewController, listViewSelectionDidChange object: Any?, info: [String : Any]?) {
  705. if controller.thumbnailTableView.isEqual(to: object) {
  706. let row = controller.thumbnailTableView.selectedRow
  707. let curIndex = self.listView.currentPageIndex
  708. if (row != -1 && row != curIndex) {
  709. self.listView?.go(toPageIndex: row, animated: true)
  710. }
  711. }
  712. }
  713. }
  714. //MARK: - ReadModel
  715. extension KMMainViewController {
  716. @objc func openReadModel() {
  717. //保存进入阅读模式之前的状态
  718. readLeftMethodType = self.leftSideViewController.type.methodType
  719. readLeftPanelOpen = self.model.leftPanelOpen
  720. readLastLeftPanWidth = self.model.lastLeftPanWidth
  721. readLeftViewShowPanel = self.leftSideViewController.isShowPanel
  722. readRightPanelOpen = self.model.rightPanelIsOpen
  723. readToolbarType = self.toolbarController.toolbarType
  724. readToolMode = self.listView.toolMode
  725. readAnnotationType = self.listView.annotationType
  726. readSubViewType = self.rightSideViewController?.subViewType ?? .None
  727. if self.toolbarController.lastItemBox.isSelected {
  728. readToolbarItemIdentifier = self.toolbarController.lastItemBox.itemIdentifier ?? ""
  729. } else {
  730. readToolbarItemIdentifier = ""
  731. }
  732. if readToolMode == .editPDFToolMode { // 内容编辑
  733. // 退出内容编辑模式
  734. self.editPDFHanddler.clearData()
  735. }
  736. //设置阅读模式
  737. self.isReadMode = true
  738. self.readModelView.isHidden = false
  739. //
  740. //顶部菜单栏状态清空
  741. self.toolbarController.toolbarType = .None
  742. // self.toolbarController.view.isHidden = true
  743. self.toolbarController.cancelSelected(KMLeftControlToolbarItemIdentifier)
  744. self.toolbarController.cancelSelected(KMRightControlToolbarItemIdentifier)
  745. self.toolbarController.lastItemBox.isSelected = false
  746. //左侧菜单栏情况
  747. self.model.leftPanelOpen = false
  748. self.model.lastLeftPanWidth = 0
  749. self.leftSideViewController.refreshMethodType(methodType: .None)
  750. //阅读界面初始化
  751. self.readModelView.isHidden = false
  752. self.readModelView.currentPageIndex = self.listView.currentPageIndex
  753. self.readModelView.totalPagesCount = Int(self.listView?.document?.pageCount ?? 0)
  754. self.readModelView.beginTimer()
  755. // self.listView.autoScales = true
  756. // self.readModelView.scaleType = .autoSize
  757. self.readModelView.zoomButton.stringValue = (NSString(format: "%.0f", self.listView.scaleFactor * 100) as String) + "%"
  758. //关闭左侧右侧菜单栏
  759. self.closeLeftPane()
  760. self.closeRightPane()
  761. self.leftSideViewController.showPanelView(show: false)
  762. self.leftView.isHidden = true
  763. //弹出提示框
  764. if readAlertView != nil {
  765. readAlertView?.removeFromSuperview()
  766. }
  767. readAlertView = CustomAlertView.alertView(message: NSLocalizedString("Read Mode On", comment: ""), fromView: self.view, withStyle: .blue, backgroundColor: NSColor.km_init(hex: "#36383B"))
  768. UserDefaults.standard.set(isReadMode, forKey: "kKMPDFViewIsReadMode")
  769. UserDefaults.standard.synchronize()
  770. self.updatePageIndicatoreType()
  771. }
  772. @objc func closeReadModel() {
  773. if self.isReadMode {
  774. self.model.isShowBOTA = false
  775. self.readModelView.isHidden = true
  776. self.isReadMode = false
  777. self.readModelView.isHidden = true
  778. self.leftSideViewController.refreshMethodType(methodType: readLeftMethodType)
  779. self.model.leftPanelOpen = readLeftPanelOpen
  780. self.model.rightPanelIsOpen = readRightPanelOpen
  781. self.model.lastLeftPanWidth = readLastLeftPanWidth
  782. if self.model.leftPanelOpen {
  783. self.openLeftPane()
  784. }
  785. self.closeRightPane()
  786. self.toolbarController.toolbarType = .None
  787. self.toolbarController.cancelSelected(KMDocumentViewDisplayToolbarItemIdentifier)
  788. // if self.rightPanelIsOpen {
  789. // self.rightPanelIsOpen = false
  790. //// self.toolbarController.selectItem(KMRightControlToolbarItemIdentifier)
  791. // self.openRightPane()
  792. // }
  793. //
  794. // if readToolbarItemIdentifier.count == 0 {
  795. // self.toolbarController.toolbarType = .None
  796. // } else {
  797. // self.toolbarController.selectItem(readToolbarItemIdentifier)
  798. // }
  799. // self.toolbarController.selectItem(KMDocumentViewDisplayToolbarItemIdentifier)
  800. self.leftSideViewController.showPanelView(show: readLeftViewShowPanel)
  801. if readLeftViewShowPanel {
  802. self.toolbarController.selectItem(KMLeftControlToolbarItemIdentifier)
  803. }
  804. if self.readToolMode == .editPDFToolMode { // 内容编辑
  805. self.toolbarController.selectItem(KMDocumentEditToolbarItemIdentifier)
  806. } else {
  807. self.toolbarController.view.isHidden = false
  808. self.listView.toolMode = readToolMode
  809. self.listView.annotationType = readAnnotationType
  810. // self.toolbarController.toolbarType = .viewSetting
  811. }
  812. } else {
  813. self.toolbarController.toolbarType = .None
  814. }
  815. if readAlertView != nil {
  816. readAlertView?.removeFromSuperview()
  817. }
  818. readAlertView = CustomAlertView.alertView(message: NSLocalizedString("Read Mode Off", comment: ""), fromView: self.view, withStyle: .blue, backgroundColor: NSColor.km_init(hex: "#36383B"))
  819. UserDefaults.standard.set(isReadMode, forKey: "kKMPDFViewIsReadMode")
  820. UserDefaults.standard.synchronize()
  821. }
  822. @objc func addOutLineItemAction() {
  823. let labelString = "\(KMLocalizedString("Page", nil)) \((self.listView?.currentPageIndex ?? 0) + 1)"
  824. let label = self.listView?.currentSelection?.string() ?? labelString
  825. let dest = self.listView?.currentDestination
  826. if let row = self.leftSideViewController.selectedRowIndexes().last {
  827. if let ol = self.leftSideViewController.tocOutlineView.item(atRow: row) as? CPDFOutline {
  828. _ = self.listView.addOutline(for: ol.parent, label: label, dest: dest, at: ol.index+1)
  829. } else {
  830. let idx = self.listView?.document?.outlineRoot()?.numberOfChildren ?? 0
  831. _ = self.listView.addOutlineForRoot(label: label, dest:dest, at: idx)
  832. }
  833. } else {
  834. let idx = self.listView?.document?.outlineRoot()?.numberOfChildren ?? 0
  835. _ = self.listView.addOutlineForRoot(label: label, dest:dest, at: idx)
  836. }
  837. }
  838. @objc func showTTSView() {
  839. self.showTTSWindow()
  840. }
  841. @objc func showShareAction() {
  842. }
  843. @objc func lookUpAction() {
  844. let label = self.listView?.currentSelection?.string() ?? ""
  845. self.toolbarController.showFindBar()
  846. self.toolbarController.findSearchView.searchString(label)
  847. // self.toolbarController.findSearchView.find(Any.self)
  848. }
  849. @objc func searchBaiduAction() {
  850. let label = self.listView?.currentSelection?.string() ?? ""
  851. let query = label.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
  852. if let url = URL(string: "https://www.baidu.com/s?wd=\(query)") {
  853. NSWorkspace.shared.open(url)
  854. }
  855. }
  856. @objc func showInfoInFinder() {
  857. }
  858. @objc func NextPageAction() {
  859. }
  860. @objc func PreviousPageAction() {
  861. }
  862. @objc func TranslateItemAction() {
  863. // 获取选中的文本
  864. if let selection = self.listView?.currentSelection?.string() {
  865. // 进行翻译
  866. let escapedText = selection.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
  867. let urlStr = "https://translate.google.com/?sl=auto&amp;tl=zh-CN&amp;text=\(escapedText)"
  868. if let url = URL(string: urlStr) {
  869. NSWorkspace.shared.open(url)
  870. }
  871. } else {
  872. print("No text selected.")
  873. }
  874. }
  875. @objc func AutoScrollItemAction() {
  876. //增加判断,如果是正在滚动,就停止,否则就开始滚动
  877. self.listView.autoFlow()
  878. }
  879. @objc func shareFromService(sender: NSMenuItem) {
  880. if ((NSApp.mainWindow?.windowController is KMBrowserWindowController) == false) {
  881. return
  882. }
  883. var string = ""
  884. if let freeTextAnnotation = listView.activeAnnotation as? CPDFFreeTextAnnotation {
  885. string = freeTextAnnotation.contents ?? ""
  886. } else if let markupAnnotation = listView.activeAnnotation as? CPDFMarkupAnnotation {
  887. if let page = markupAnnotation.page {
  888. if let selection = page.selection(for: markupAnnotation.bounds) {
  889. string = selection.string() ?? ""
  890. }
  891. }
  892. } else {
  893. string = listView.currentSelection?.string() ?? ""
  894. }
  895. let windowControler = NSApp.mainWindow?.windowController as! KMBrowserWindowController
  896. let model = windowControler.browser?.tabStripModel
  897. if let cnt = model?.count(), cnt <= 0 {
  898. return
  899. }
  900. if let data = model?.activeTabContents().isHome, data {
  901. return
  902. }
  903. let document: KMMainDocument = model?.activeTabContents() as! KMMainDocument
  904. if string.count > 0 {
  905. let represent : NSSharingService = sender.representedObject as! NSSharingService
  906. represent.perform(withItems: [string])
  907. return
  908. }
  909. let represent = sender.representedObject as? NSSharingService
  910. represent?.perform(withItems: [string])
  911. }
  912. @objc func cutAction(sender: NSMenuItem) {
  913. }
  914. @objc func deleteAction(sender: NSMenuItem) {
  915. }
  916. @objc func ColorsItemAction(sender: NSMenuItem) {
  917. }
  918. @objc func LinesItemAction(sender: NSMenuItem) {
  919. }
  920. @objc func EditNoteItemAction(sender: NSMenuItem) {
  921. }
  922. @objc func AITranslateItemAction(sender: NSMenuItem) {
  923. self.showAITypeChooseView(aiConfigType: .translate)
  924. }
  925. @objc func AIProofreadItemAction(sender: NSMenuItem) {
  926. self.showAITypeChooseView(aiConfigType: .proofreading)
  927. }
  928. @objc func AIRewriteItemAction(sender: NSMenuItem) {
  929. self.showAITypeChooseView(aiConfigType: .reWriting)
  930. }
  931. // @objc func highlightLinks() {
  932. // let highlightLinks = KMPreferenceManager.shared.highlightLinks
  933. // KMPreferenceManager.shared.highlightLinks = !highlightLinks
  934. // }
  935. }
  936. // MARK: - KMEditImagePropertyViewControllerDelegate
  937. extension KMMainViewController: KMEditImagePropertyViewControllerDelegate {
  938. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) {
  939. self.model.isPDFTextImageEdited = true
  940. }
  941. }
  942. // MARK: - KMSnapshotWindowControllerDelegate
  943. extension KMMainViewController: KMSnapshotWindowControllerDelegate {
  944. func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
  945. self.leftSideViewController.snapshotControllerWillClose(controller)
  946. }
  947. func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
  948. if isMiniaturize && self.interactionMode != .presentation {
  949. if self.interactionMode != .legacyFullScreen && self.model.leftPanelOpen == false {
  950. self.toggleLeftPane()
  951. } else if self.interactionMode == .legacyFullScreen {
  952. // else if ([self interactionMode] == SKLegacyFullScreenMode && ([rightSideWindow state] == NSDrawerClosedState || [rightSideWindow state] == NSDrawerClosingState)) {
  953. // [rightSideWindow expand];
  954. // [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hideRightSideWindow:) userInfo:NULL repeats:NO];
  955. }
  956. var row = NSNotFound
  957. for (i,sp) in self.leftSideViewController.snapshots.enumerated() {
  958. if controller.isEqual(sp.windowC) {
  959. row = i
  960. }
  961. }
  962. if (row != NSNotFound) {
  963. if self.leftSideViewController.type.methodType != .snapshot {
  964. self.leftSideViewController.leftView.segmentedControl.selectedSegment = 4
  965. } else {
  966. self.leftSideViewController.refreshUIOfSnapshotIfNeed()
  967. }
  968. self.leftSideViewController.snapshotTableView.scrollRowToVisible(row)
  969. }
  970. }
  971. return self.leftSideViewController.snapshotController(controller, miniaturizedRect: isMiniaturize)
  972. }
  973. func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
  974. self.leftSideViewController.snapshotControllerDidFinishSetup(controller)
  975. }
  976. }
  977. // MARK: - CPDFDocumentDelegate
  978. extension KMMainViewController: CPDFDocumentDelegate {
  979. func documentDidBeginDocumentFind(_ document: CPDFDocument!) {
  980. self.leftSideViewController.documentDidBeginFind()
  981. // [statusBar setProgressIndicatorStyle:SKProgressIndicatorBarStyle];
  982. // [[statusBar progressIndicator] setMaxValue:[[note object] pageCount]];
  983. // [[statusBar progressIndicator] setDoubleValue:0.0];
  984. // [statusBar startAnimation:self];
  985. // [self willChangeValueForKey:SEARCHRESULTS_KEY];
  986. // [self willChangeValueForKey:GROUPEDSEARCHRESULTS_KEY];
  987. }
  988. func documentDidEndDocumentFind(_ document: CPDFDocument!) {
  989. self.leftSideViewController.documentDidEndFind()
  990. // [self didChangeValueForKey:GROUPEDSEARCHRESULTS_KEY];
  991. // [self didChangeValueForKey:SEARCHRESULTS_KEY];
  992. // [statusBar stopAnimation:self];
  993. // [statusBar setProgressIndicatorStyle:SKProgressIndicatorNone];
  994. // NSArray *highlights = [[NSArray alloc] initWithArray:searchResults copyItems:YES];
  995. // [highlights setValue:[NSColor yellowColor] forKey:@"color"];
  996. // [self.pdfView setHighlightedSelections:highlights];
  997. // [highlights release];
  998. }
  999. }
  1000. // MAKR: - CPDFViewDelegate,CPDFListViewDelegate
  1001. extension KMMainViewController: CPDFViewDelegate,CPDFListViewDelegate {
  1002. func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
  1003. self.dealDocumentDidLoaded()
  1004. let notification = Notification(name: Notification.Name(rawValue: "pdfViewDocumentDidLoaded"))
  1005. self.preferenceDidChangeNotification(notification:notification)
  1006. let leftWidthNumber = UserDefaults.standard.object(forKey: CPDFOfficeLeftSidePaneWidthKey) as? NSNumber ?? NSNumber(value: self.model.panelWidth + functionWidth)
  1007. let rightWidthNumber = UserDefaults.standard.object(forKey: CPDFOfficeRightSidePaneWidthKey) as? NSNumber ?? NSNumber(value: self.model.defaultRightWidth)
  1008. applyLeftSideWidth(leftWidthNumber.doubleValue, rightSideWidth: rightWidthNumber.doubleValue)
  1009. self.updatePageIndicatoreType()
  1010. }
  1011. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  1012. self.updatePageIndicatoreType()
  1013. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: self.document)
  1014. // KMPrint("KMPDFViewCurrentPageDidChanged")
  1015. }
  1016. func pdfViewScaleDidChanged(_ pdfView: CPDFView!) {
  1017. self.toolbarController.mainToolBarView?.zoomTextField.stringValue = "\(Int(self.listView.scaleFactor * 100))%"
  1018. }
  1019. func pdfViewDidClick(onLink pdfView: CPDFView!, withURL url: String!) {
  1020. if let urlString = url, urlString == kKMPurchaseProductURLString {
  1021. //跳转订阅比较表
  1022. let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
  1023. return
  1024. }
  1025. KMTools.openURL(urlString: url)
  1026. }
  1027. func pdfViewPerformURL(_ pdfView: CPDFView!, withContent content: String!) {
  1028. KMPrint("pdfViewPerformURL")
  1029. if content != nil {
  1030. NSWorkspace.shared.open(URL(string: content)!)
  1031. } else {
  1032. let alert = NSAlert()
  1033. alert.alertStyle = .critical
  1034. alert.informativeText = NSLocalizedString("The hyperlink is invalid.", comment: "")
  1035. alert.messageText = ""
  1036. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1037. alert.runModal()
  1038. return
  1039. }
  1040. }
  1041. func pdfViewPerformPrint(_ pdfView: CPDFView!) {
  1042. KMPrint("pdfViewPerformPrint")
  1043. self.showPrintWindow()
  1044. }
  1045. func pdfViewPerformGo(toPage pdfView: CPDFView!) {
  1046. KMPrint("pdfViewPerformGo")
  1047. }
  1048. func pdfViewOpenPDF(_ pdfView: CPDFView!, forRemoteGoTo action: CPDFAction!) {
  1049. KMPrint("pdfViewOpenPDF")
  1050. }
  1051. func pdfViewPerformReset(_ pdfView: CPDFView!) {
  1052. KMPrint("pdfViewPerformReset")
  1053. // self.listView.resetFormAnnotation()
  1054. pdfView.document?.resetForm()
  1055. }
  1056. func pdfViewEditingBlockDidChanged(_ pdfView: CPDFView!) {
  1057. KMPrint("pdfViewEditingBlockDidChanged")
  1058. }
  1059. func pdfViewAsBookBookmark() -> NSImage! {
  1060. return NSImage(named: "KMImageNameBookmarkIcon")!
  1061. }
  1062. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  1063. self.editPDFHanddler.pdfViewEditingSelectionDidChanged(pdfView)
  1064. }
  1065. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  1066. self.editPDFHanddler.pdfViewEditingAreaDidChanged(pdfView)
  1067. }
  1068. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  1069. self.editPDFHanddler.pdfViewEditingCropBoundsDidChanged(pdfView, editing: editArea)
  1070. }
  1071. //编辑PDF 创建图片区域回调
  1072. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1073. self.editPDFHanddler.pdfViewEditingAddImageArea(pdfView, add: page, add: rect)
  1074. }
  1075. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1076. self.editPDFHanddler.pdfViewEditingAddTextArea(pdfView, add: page, add: rect)
  1077. }
  1078. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1079. self.editPDFHanddler.pdfViewMobileEditingBegan(point, for: pdfView, forEditing: editingAreas)
  1080. }
  1081. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1082. self.editPDFHanddler.pdfViewMobileEditingMove(point, for: pdfView, forEditing: editingAreas)
  1083. }
  1084. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1085. self.editPDFHanddler.pdfViewMobileEditingEnd(point, for: pdfView, forEditing: editingAreas)
  1086. }
  1087. func pdfViewEditingSelectCharDidChanged(_ pdfView: CPDFView!) {
  1088. self.editPDFHanddler.pdfViewEditingSelectCharDidChanged(pdfView)
  1089. }
  1090. func pdfViewEditingExitCropMode(_ pdfView: CPDFView!, forEditing editingArea: CPDFEditImageArea!) {
  1091. self.editPDFHanddler.pdfViewEditingExitCropMode(pdfView, forEditing: editingArea)
  1092. }
  1093. func pdfListViewKeyDownIsContinue(_ pdfListView: CPDFListView!, theEvent: NSEvent!) -> Bool {
  1094. let command = theEvent.modifierFlags.contains(.command)
  1095. let control = theEvent.modifierFlags.contains(.control)
  1096. KMPrint(theEvent.keyCode)
  1097. if self.listView.isEditing() {
  1098. if control && theEvent.keyCode == 11 { // ctr + b
  1099. self.editPDFHanddler.fontBoldAction()
  1100. return false
  1101. } else if control && theEvent.keyCode == 34 { // ctr +i
  1102. self.editPDFHanddler.fontItalicAction()
  1103. return false
  1104. } else if theEvent.keyCode == 36 { // enter
  1105. if self.listView.isCropMode {
  1106. self.editPDFHanddler.cropComfirmAction()
  1107. return false
  1108. }
  1109. }
  1110. }
  1111. if (theEvent.keyCode == 11 && command) { // command + B [添加书签]
  1112. self.menuItemBookMarkClick_add(sender: NSMenuItem())
  1113. return false
  1114. } else if (command && control && theEvent.keyCode == 14) { // command + control + E [注释 橡皮擦]
  1115. return false
  1116. } else if (theEvent.keyCode == 123) { // 向左
  1117. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  1118. return false
  1119. } else {
  1120. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage()) {
  1121. self.listView.goToPreviousPage(nil)
  1122. return false
  1123. }
  1124. }
  1125. } else if (theEvent.keyCode == 126) { // 向上
  1126. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  1127. return false
  1128. } else {
  1129. if (self.listView.isContinousScroll()) {
  1130. return true
  1131. }
  1132. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToPreviousPage()) {
  1133. self.listView.goToPreviousPage(nil)
  1134. return false
  1135. }
  1136. }
  1137. } else if (theEvent.keyCode == 124) { // 向右
  1138. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  1139. return false
  1140. } else {
  1141. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage()) {
  1142. self.listView.goToNextPage(nil)
  1143. return false
  1144. }
  1145. }
  1146. } else if (theEvent.keyCode == 125) { // 向下
  1147. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  1148. return false
  1149. } else {
  1150. if (self.listView.isContinousScroll()) {
  1151. return true
  1152. }
  1153. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToNextPage()) {
  1154. self.listView.goToNextPage(nil)
  1155. return false
  1156. }
  1157. }
  1158. } else if (theEvent.keyCode == 36) {
  1159. if self.listView.annotationType == .addImage || self.listView.annotationType == .addText {
  1160. if self.listView.isEditImage {
  1161. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  1162. }
  1163. }
  1164. }
  1165. if theEvent.keyCode == 53 {
  1166. self.exitFullScreen(Any.self)
  1167. if self.isReadMode {
  1168. self.closeReadModel()
  1169. }
  1170. self.leftSideViewCancelSelect()
  1171. if (self.toolbarController.toolbarType.isToolMode()) {
  1172. self.toolbarController.selectItem(self.toolbarController.toolbarType.itemIdentifier())
  1173. }
  1174. }
  1175. return true
  1176. }
  1177. func pdfListViewMenuValidate(_ pdfListView: CPDFListView!, menuItem: NSMenuItem!, isTakesEffect: UnsafeMutablePointer<ObjCBool>!) -> Bool {
  1178. guard let action = menuItem.action else {
  1179. isTakesEffect.pointee = false
  1180. return false
  1181. }
  1182. if (KMSystemMenu.isEditSelector(sel: action)) {
  1183. if (KMSystemMenu.Edit.deleteSelector == action) {
  1184. isTakesEffect.pointee = true
  1185. return self.listView.activeAnnotations.count > 0
  1186. } else if (KMSystemMenu.Edit.copySelector == action) {
  1187. isTakesEffect.pointee = true
  1188. return true//self.listView.canCopy()
  1189. } else if (KMSystemMenu.Edit.cutSelector == action) {
  1190. isTakesEffect.pointee = true
  1191. return self.listView.canCopy()
  1192. } else if (KMSystemMenu.Edit.pasteSelector == action) {
  1193. isTakesEffect.pointee = true
  1194. return self.listView.canPaste()
  1195. }
  1196. }
  1197. isTakesEffect.pointee = false
  1198. return false
  1199. }
  1200. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  1201. self.editPDFHanddler.pdfViewEditingOperationDidChanged(pdfView)
  1202. }
  1203. func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  1204. self.editPDFHanddler.pdfViewEditingDoubleClick(pdfView, imageArea: editArea)
  1205. }
  1206. //MARK: -CPDFListViewDelegate
  1207. func cPDFListView(_ pdfListView: CPDFListView, didDelete annotation: CPDFAnnotation, in pdfPage: CPDFPage) {
  1208. self.leftSideViewController.updateThumbnail(at: Int(pdfPage.pageIndex()))
  1209. }
  1210. func pdfListViewChangeatioActiveAnnotations(_ pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!, isRightMenu: Bool) {
  1211. self.view.window?.makeFirstResponder(self.listView)
  1212. if isRightMenu {
  1213. } else if annotations.count > 0 {
  1214. if annotations.count > 1 {
  1215. let fristAnnotation = annotations.first
  1216. var isSameAnnotation = true
  1217. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  1218. for annotation in annotations {
  1219. let cunrrentClassName = NSStringFromClass(annotation.classForCoder)
  1220. if (className == "CPDFSquareAnnotation") ||
  1221. (className == "CPDFCircleAnnotation") ||
  1222. (className == "CPDFLineAnnotation") {
  1223. if (cunrrentClassName != "CPDFSquareAnnotation") &&
  1224. (cunrrentClassName != "CPDFCircleAnnotation") &&
  1225. (cunrrentClassName != "CPDFLineAnnotation") {
  1226. isSameAnnotation = false
  1227. }
  1228. } else {
  1229. if className != cunrrentClassName {
  1230. isSameAnnotation = false
  1231. }
  1232. }
  1233. }
  1234. if isSameAnnotation == false {
  1235. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  1236. // self.closeRightPane()
  1237. } else {
  1238. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  1239. self.openRightPane()
  1240. }
  1241. } else {
  1242. let fristAnnotation = annotations.first
  1243. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  1244. if self.isReadMode {
  1245. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  1246. self.closeRightPane()
  1247. } else {
  1248. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  1249. if className != "CPDFStampAnnotation" &&
  1250. className != "CPDFSignatureAnnotation" &&
  1251. className != "CPDFListStampAnnotation" {
  1252. self.openRightPane()
  1253. }
  1254. // if let lineAnnotation = fristAnnotation as? CPDFLineAnnotation {
  1255. // handleLineAnnotation(lineAnnotation)
  1256. // } else if let polylineAnnotation = fristAnnotation as? CPDFPolylineAnnotation {
  1257. // handlePolylineAnnotation(polylineAnnotation)
  1258. // } else if let polygonAnnotation = fristAnnotation as? CPDFPolygonAnnotation {
  1259. // handlePolygonAnnotation(polygonAnnotation)
  1260. // }
  1261. }
  1262. }
  1263. if (listView.activeAnnotation.isKind(of: CPDFLineAnnotation.self)) {
  1264. if (!(listView.activeAnnotation as! CPDFLineAnnotation).isMeasure) {
  1265. cancelMeasureType()
  1266. } else {
  1267. if distanceMeasureInfoWindowController == nil {
  1268. let measureInfo = CPDFDistanceMeasureInfo()
  1269. distanceMeasureInfoWindowController = CDistanceMeasureInfoWindowController()
  1270. distanceMeasureInfoWindowController?.measureInfo = measureInfo
  1271. distanceMeasureInfoWindowController?.delegate = self
  1272. }
  1273. }
  1274. } else if (!listView.activeAnnotation.isKind(of: CPDFPolygonAnnotation.self) && !listView.activeAnnotation.isKind(of: CPDFPolylineAnnotation.self)) {
  1275. cancelMeasureType()
  1276. } else if (listView.activeAnnotation.isKind(of: CPDFPolygonAnnotation.self) || listView.activeAnnotation.isKind(of: CPDFPolylineAnnotation.self)) {
  1277. if perimeterMeasureInfoWindowController == nil {
  1278. let measureInfo = CPDFPerimeterMeasureInfo()
  1279. perimeterMeasureInfoWindowController = CPerimeterMeasureInfoWindowController()
  1280. perimeterMeasureInfoWindowController?.measureInfo = measureInfo
  1281. perimeterMeasureInfoWindowController?.delegate = self
  1282. }
  1283. if areaMeasureInfoWindowController == nil {
  1284. let measureInfo = CPDFAreaMeasureInfo()
  1285. areaMeasureInfoWindowController = CAreaMeasureInfoWindowController()
  1286. areaMeasureInfoWindowController?.measureInfo = measureInfo
  1287. areaMeasureInfoWindowController?.delegate = self
  1288. }
  1289. }
  1290. } else if (annotations.count == 0){
  1291. if pdfListView.annotationType == .unkown {
  1292. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  1293. self.closeRightPane()
  1294. } else {
  1295. if self.isReadMode {
  1296. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  1297. self.closeRightPane()
  1298. } else {
  1299. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  1300. self.openRightPane()
  1301. }
  1302. }
  1303. }
  1304. }
  1305. func pdfListViewChangedAnnotationType(_ pdfListView: CPDFListView!, for annotationType: CAnnotationType) {
  1306. if(annotationType == .unkown) {
  1307. // self.rightSideViewController.view.isHidden = true
  1308. self.rightSideViewController.isHidden = true
  1309. self.closeRightPane()
  1310. }
  1311. let aType = annotationType
  1312. if aType.isMarkup() || aType == .anchored || aType == .freeText || aType.isSquare() || aType == .link {
  1313. KMDataManager.ud_set(annotationType.rawValue, forKey: SKLastAnnotationModeKey)
  1314. }
  1315. }
  1316. ///开始定位链接注释
  1317. func pdfListViewLinkDestinationStart(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  1318. if self.locationPageView.superview == nil {
  1319. self.locationPageView.frame = CGRect(x: 0, y: pdfListView.frame.maxY-32, width: pdfListView.frame.width, height: 32)
  1320. pdfListView.addSubview(self.locationPageView)
  1321. }
  1322. }
  1323. ///刷新链接注释
  1324. func pdfListViewLinkDestinationEnd(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  1325. if self.locationPageView.superview != nil {
  1326. self.locationPageView.removeFromSuperview()
  1327. }
  1328. if self.rightSideViewController.subViewType == .AnnotationProperts && pdfListView.annotationType == .link {
  1329. self.rightSideViewController.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  1330. }
  1331. }
  1332. func pdfListViewMenuItemsEditing(at point: CGPoint, for page: CPDFPage!, menuItems: [NSMenuItem]!) -> [NSMenuItem]! {
  1333. if (listView.toolMode != CToolMode.editPDFToolMode) {
  1334. return menuItems
  1335. }
  1336. var tMenuItems = menuItems;
  1337. if(listView.isSelectEditCharRange() ||
  1338. listView.isSelecteditArea(with: point)) {
  1339. tMenuItems?.append(NSMenuItem.separator())
  1340. // tMenuItems?.append(self.fontColorMenuItem())
  1341. // tMenuItems?.append(self.fontSizeMenuItem())
  1342. }
  1343. let areas = self.listView.editingAreas() ?? []
  1344. if areas.count == 1 {
  1345. let fristAreas = areas.first
  1346. if fristAreas is CPDFEditImageArea {
  1347. self.listView.selectImageAreas = fristAreas as? CPDFEditImageArea
  1348. if self.listView.isEditImage {
  1349. tMenuItems?.removeAll()
  1350. tMenuItems?.append(self.corpImageMenuItem())
  1351. tMenuItems?.append(self.cancelCorpImageMenuItem())
  1352. tMenuItems?.append(self.restoreCorpImageMenuItem())
  1353. } else {
  1354. tMenuItems?.append(NSMenuItem.separator())
  1355. tMenuItems?.append(self.cutImageArea())
  1356. tMenuItems?.append(self.replaceImageArea())
  1357. tMenuItems?.append(self.exportImageArea())
  1358. }
  1359. } else {
  1360. if tMenuItems?.count != 1 {
  1361. tMenuItems?.swapAt(0, 1)
  1362. }
  1363. }
  1364. } else if areas.count == 0 {
  1365. tMenuItems?.append(NSMenuItem.separator())
  1366. tMenuItems?.append(self.addText())
  1367. tMenuItems?.append(self.addImage())
  1368. }
  1369. return tMenuItems
  1370. }
  1371. func tableMenu(_ menu: NSMenu, withTable table: KMTableAnnotation, point: CGPoint) -> NSMenu {
  1372. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  1373. let itemTitles = ["Edit", "", "Add Row Above", "Add Row Below", "", "Add Column Before", "Add Column After", "", "Delete Row", "Delete Column", "Delete Table", "Cut", "Copy", "Paste", "Paste and Match Style", "Delete Cell Contents", "Clear All"]
  1374. let actions = ["formAnnotTextEdit:", "", "addRowAbove:", "addRowBelow:", "", "addColumnBefore:", "addColumnAfter:", "", "deleteRow:", "deleteColumn:", "deleteTabel", "cutCell:", "copyCell:", "pasteCell:", "pasteAndMatchStyle:", "deleteCellContents:", "clearAll:"]
  1375. for i in 0..<itemTitles.count {
  1376. var item: NSMenuItem? = nil
  1377. if itemTitles[i] == "" {
  1378. item = NSMenuItem.separator()
  1379. menu.insertItem(item!, at: i)
  1380. } else {
  1381. item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  1382. item!.target = self
  1383. item!.action = NSSelectorFromString(actions[i])
  1384. if itemTitles[i] == "Paste" /*&& !_copyCellData*/ {
  1385. item!.action = nil
  1386. } else if itemTitles[i] == "Paste and Match Style" /*&& !_copyCellData */{
  1387. item!.action = nil
  1388. } else if itemTitles[i] == "Add Row Above" {
  1389. let path1 = table.crossLines[table.rowNumber - table.currentCell.row]
  1390. let path2 = table.crossLines[table.rowNumber - table.currentCell.row - 1]
  1391. if (path1 as AnyObject).lineJoinStyle == NSBezierPath.LineJoinStyle.round && table.headerCount() >= 5 {
  1392. item!.action = nil
  1393. } else if (path2 as AnyObject).lineJoinStyle == NSBezierPath.LineJoinStyle.bevel && table.footerCount() >= 5 {
  1394. item!.action = nil
  1395. }
  1396. }
  1397. item!.title = NSLocalizedString(item!.title, comment: "")
  1398. item!.representedObject = NSValue(point: point)
  1399. menu.insertItem(item!, at: i)
  1400. }
  1401. }
  1402. } else {
  1403. let itemTitles = ["Cut", "Copy", "Paste", "Delete"]
  1404. let actions = ["cut:", "copy:", "paste:", "delete:"]
  1405. for i in 0..<itemTitles.count {
  1406. let item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  1407. item.target = self
  1408. item.action = NSSelectorFromString(actions[i])
  1409. item.title = NSLocalizedString(item.title, comment: "")
  1410. menu.insertItem(item, at: i)
  1411. item.representedObject = NSValue(point: point)
  1412. }
  1413. }
  1414. return menu
  1415. }
  1416. func pdfListViewMenu(forEvent pdfListView: CPDFListView!, for theEvent: NSEvent!, click menu: AutoreleasingUnsafeMutablePointer<NSMenu?>!, isMoveSelectAnno: Bool) {
  1417. self.mouseRightMenuEvent = theEvent
  1418. var currentMenu : NSMenu = menu.pointee!
  1419. if let activeAnno = listView.activeAnnotation as? KMTableAnnotation {//Table
  1420. var pagePoint = NSPoint()
  1421. _ = self.listView.pageAndPoint(&pagePoint, for: theEvent, nearest: true)
  1422. currentMenu.removeAllItems()
  1423. let annotation = activeAnno
  1424. annotation.completeEditCellText()
  1425. if !(NSIsEmptyRect(annotation.drawRect)) {
  1426. annotation.drawLine(pagePoint)
  1427. NotificationCenter.default.post(name: NSNotification.Name.KMPDFViewTableAnnotationDidChange, object: self, userInfo: ["point": NSValue(point: pagePoint)])
  1428. }
  1429. if (annotation.rowNumber - annotation.currentCell.row - 1) < 0 {
  1430. return
  1431. }
  1432. currentMenu = tableMenu(currentMenu, withTable: listView.activeAnnotation as! KMTableAnnotation, point: pagePoint)
  1433. listView.needsDisplay = true
  1434. return
  1435. }
  1436. var pagePoint: NSPoint = .zero
  1437. if let page = self.listView.pageAndPoint(&pagePoint, for: theEvent, nearest: true) {
  1438. let anno = page.annotation(at: pagePoint)
  1439. let item1 = NSMenuItem(title: NSLocalizedString("Delete", comment: ""), action: #selector(menuItemActionMeasureDelete), target: self)
  1440. item1.representedObject = anno
  1441. let item2 = NSMenuItem(title: NSLocalizedString("Edit Note", comment: ""), action: #selector(menuItemActionMeasureEditNote), target: self)
  1442. item2.representedObject = anno
  1443. let item3 = NSMenuItem(title: NSLocalizedString("Settings", comment: ""), action: #selector(menuItemActionMeasureSetting), target: self)
  1444. item3.representedObject = anno
  1445. if let data = anno as? CPDFPolygonAnnotation { // 多变形
  1446. currentMenu.removeAllItems()
  1447. currentMenu.insertItem(item1, at: 0)
  1448. currentMenu.insertItem(item2, at: 1)
  1449. currentMenu.insertItem(item3, at: 2)
  1450. return
  1451. }
  1452. if let data = anno as? CPDFPolylineAnnotation {
  1453. currentMenu.removeAllItems()
  1454. currentMenu.insertItem(item1, at: 0)
  1455. currentMenu.insertItem(item2, at: 1)
  1456. currentMenu.insertItem(item3, at: 2)
  1457. return
  1458. }
  1459. if let data = anno as? CPDFLineAnnotation, data.isMeasure {
  1460. currentMenu.removeAllItems()
  1461. currentMenu.insertItem(item1, at: 0)
  1462. currentMenu.insertItem(item2, at: 1)
  1463. currentMenu.insertItem(item3, at: 2)
  1464. return
  1465. }
  1466. }
  1467. if (listView.toolMode == .selectToolMode){
  1468. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1469. currentMenu.insertItem(self.printingMenu(), at: 3)
  1470. currentMenu.insertItem(self.setTTSStype(), at: 3)
  1471. currentMenu.insertItem(self.setCropStype(), at: 3)
  1472. currentMenu.insertItem(self.setSnapshotStype(), at: 3)
  1473. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  1474. export.submenu = self.exportMenu()
  1475. currentMenu.insertItem(export, at: 3)
  1476. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1477. if listView.activeAnnotation == nil{
  1478. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  1479. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1480. }
  1481. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 3)
  1482. currentMenu.insertItem(self.addReadModelStype(), at: currentMenu.items.count - 3)
  1483. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  1484. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  1485. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  1486. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  1487. return
  1488. }
  1489. if (listView.toolMode == .moveToolMode || listView.toolMode == .magnifyToolMode){
  1490. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  1491. currentMenu.insertItem(self.setTTSStype(), at: 0)
  1492. currentMenu.insertItem(self.setCropStype(), at: 0)
  1493. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  1494. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  1495. currentMenu.insertItem(self.addOutlineStype(), at: 0)
  1496. currentMenu.insertItem(self.addBookmarkMenu(), at: 0)
  1497. if listView.activeAnnotation == nil{
  1498. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  1499. currentMenu.insertItem(self.setAnnotationToolStype(), at: 0)
  1500. }
  1501. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  1502. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  1503. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  1504. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  1505. return
  1506. }
  1507. if currentMenu.items.count > 3 {
  1508. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 3)
  1509. currentMenu.insertItem(self.addReadModelStype(), at: currentMenu.items.count - 3)
  1510. }
  1511. if listView.currentSelection != nil && listView.activeAnnotations.count < 1{
  1512. if listView.currentSelection.selectionType() == .text {
  1513. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1514. currentMenu.insertItem(self.setSearchBaiduStype(), at: 3)
  1515. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1516. currentMenu.insertItem(self.setLookUpStype(), at: 3)
  1517. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1518. currentMenu.insertItem(self.addOutlineStype(), at: 3)
  1519. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1520. // if listView.activeAnnotation == nil{
  1521. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  1522. // }
  1523. currentMenu.insertItem(self.setTTSStype(), at: 3)
  1524. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1525. currentMenu.insertItem(self.setShareStype(), at: 3)
  1526. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1527. }
  1528. currentMenu.insertItem(self.enterAnnotationStype(), at: 3)
  1529. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  1530. if listView.currentSelection.selectionType() == .image{
  1531. //
  1532. // currentMenu.insertItem(self.setCutStype(), at: 1)
  1533. // }
  1534. // if listView.activeAnnotations.count > 0 {
  1535. // currentMenu.insertItem(self.setDeleteStype(), at: 3)
  1536. currentMenu.insertItem(NSMenuItem.separator(), at: 6)
  1537. currentMenu.insertItem(self.addOutlineStype(), at: 6)
  1538. currentMenu.insertItem(NSMenuItem.separator(), at: 6)
  1539. currentMenu.insertItem(self.setAnnotationToolStype(), at: 6)
  1540. // currentMenu.insertItem(self.setEditNoteStype(), at: 6)
  1541. // currentMenu.insertItem(self.setRotateStype(), at: 6)
  1542. // currentMenu.insertItem(self.setLinesStype(), at: 6)
  1543. // currentMenu.insertItem(self.setColorsStype(), at: 6)
  1544. // }
  1545. }
  1546. if listView.currentSelection.selectionType() == .text {
  1547. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  1548. currentMenu.insertItem(self.setTranslateStype(), at: currentMenu.items.count)
  1549. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  1550. // currentMenu.insertItem(self.setServicesStype(), at: currentMenu.items.count)
  1551. }
  1552. }
  1553. if listView.activeAnnotation != nil || isMoveSelectAnno {
  1554. if let data = self.listView?.activeAnnotation?.type?.lowercased(), data == "stamp"{
  1555. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  1556. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  1557. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  1558. }else{
  1559. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  1560. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  1561. // currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  1562. // currentMenu.insertItem(self.setAnnotationToolStype(), at: currentMenu.items.count - 15)
  1563. // currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  1564. // currentMenu.insertItem(self.addBookmarkMenu(), at: currentMenu.items.count - 15)
  1565. // currentMenu.insertItem(self.addOutlineStype(), at: currentMenu.items.count - 15)
  1566. // currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  1567. // currentMenu.insertItem(self.setSnapshotStype(), at: currentMenu.items.count - 15)
  1568. // currentMenu.insertItem(self.setCropStype(), at: currentMenu.items.count - 15)
  1569. // currentMenu.insertItem(self.setTTSStype(), at: currentMenu.items.count - 15)
  1570. if let anno = self.listView.activeAnnotation, anno.isKind(of: CPDFStampAnnotation.self) {
  1571. // currentMenu.insertItem(self.setRotateStype(), at: currentMenu.items.count - 15)
  1572. } else {
  1573. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  1574. currentMenu.insertItem(self.setShareStype(), at: currentMenu.items.count - 15)
  1575. }
  1576. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  1577. }
  1578. }
  1579. if listView.activeAnnotation == nil && listView.currentSelection == nil{
  1580. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  1581. if(listView.toolMode == .selectToolMode) {
  1582. if NSIsEmptyRect(listView.currentSelectionRect()) {
  1583. currentMenu.insertItem(self.zoomSelectionMenuItem(), at: 0)
  1584. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  1585. }
  1586. currentMenu.insertItem(self.printingMenu(), at: 0)
  1587. currentMenu.insertItem(self.setTTSStype(), at: 0)
  1588. currentMenu.insertItem(self.setCropStype(), at: 0)
  1589. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  1590. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  1591. export.submenu = self.exportMenu()
  1592. currentMenu.insertItem(export, at: currentMenu.items.count)
  1593. }else{
  1594. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  1595. currentMenu.insertItem(self.setTTSStype(), at: 2)
  1596. currentMenu.insertItem(self.setCropStype(), at: 2)
  1597. currentMenu.insertItem(self.setSnapshotStype(), at: 2)
  1598. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  1599. currentMenu.insertItem(self.addOutlineStype(), at: 2)
  1600. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  1601. currentMenu.insertItem(self.enterAnnotationStype(), at: 2)
  1602. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  1603. if(currentMenu.items.count > 4) {
  1604. currentMenu.insertItem(NSMenuItem.separator(), at: 5)
  1605. }
  1606. if(currentMenu.items.count > 5) {
  1607. currentMenu.insertItem(self.addBookmarkMenu(), at: 6)
  1608. }
  1609. // if (currentMenu.items.count > 6 && self.isReadMode) {
  1610. // currentMenu.removeItem(currentMenu.item(withTitle: "Zoom")!)
  1611. // }
  1612. // currentMenu.insertItem(self.findStringMenu(), at: currentMenu.items.count)
  1613. currentMenu.insertItem(self.setAutoScrollStype(), at: currentMenu.items.count)
  1614. }
  1615. currentMenu.insertItem(self.setAnnotationToolStype(), at: 5)
  1616. }
  1617. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  1618. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  1619. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  1620. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  1621. for item in currentMenu.items {
  1622. if (item.action == NSSelectorFromString("menuItemClick_HidenorShowNote:")) {
  1623. // 显示与隐藏注释 item action 截取
  1624. item.action = #selector(menuItemClick_HidenorShowNote)
  1625. item.target = self
  1626. break
  1627. }
  1628. }
  1629. }
  1630. func pdfListViewAddAnnotations(_ pdfListView: CPDFListView!, forAdd annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  1631. var addRedact = false
  1632. for anno in annotations {
  1633. if (anno.isKind(of: CPDFRedactAnnotation.self)) {
  1634. addRedact = true
  1635. } else if anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation {
  1636. anno.contents = pdfPage?.string(for: anno.bounds) ?? ""
  1637. }
  1638. }
  1639. self.model.hasAddRedact = addRedact
  1640. if /*self.isReadMode || */self.listView.toolMode == .moveToolMode {
  1641. self.listView.toolMode = .textToolMode
  1642. self.listView.annotationType = .unkown
  1643. self.toolbarController.toolbarType = .Annatiton
  1644. }
  1645. if (self.model.rightMouseEventing) {
  1646. self.model.rightMouseEventing = false
  1647. if (self.toolbarController.ignoreCurrentAnnotationTypeChange && self.listView.annotationType == .ink) {
  1648. self.listView.toolMode = .textToolMode
  1649. self.listView.annotationType = .unkown
  1650. }
  1651. }
  1652. self.toolbarController.ignoreCurrentAnnotationTypeChange = false
  1653. self.leftSideViewController.refreshUIForAddAnnotation(annos: annotations, page: pdfPage)
  1654. }
  1655. func pdfListViewRemoveAnnotations(_ pdfListView: CPDFListView!, forRemove annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  1656. self.leftSideViewController.annoList_refreshUIForDeleteAnnotations(annos: annotations, page: pdfPage)
  1657. }
  1658. func pdfListViewDidSelectionEnd(_ pdfListView: CPDFListView!) {
  1659. if (!self.listView.isEqual(to: pdfListView)) {
  1660. return
  1661. }
  1662. if (self.listView.toolMode != .selectToolMode) {
  1663. return
  1664. }
  1665. if (self.topTipBox.isHidden || self.topTipBox.contentView?.subviews.count == 0) {
  1666. return
  1667. }
  1668. let tipView = self.topTipBox.contentView?.subviews.first
  1669. if (tipView?.isKind(of: KMCropTipView.self) == false) {
  1670. return
  1671. }
  1672. (tipView as! KMCropTipView).setString(string: "请按 Enter 键确定裁剪区域")
  1673. }
  1674. func pdfListViewKeyDowClosePanel(_ speedy: CPDFViewSidebarSpeedMode, event theEvent: NSEvent!) {
  1675. if(speedy == .right) {
  1676. self.toggleRightPane()
  1677. } else if (speedy == .left) {
  1678. self.menuItemAction_hiddenLeftSide(speedy)
  1679. }
  1680. }
  1681. func pdfListViewEventMarkupColor(with annotation: CPDFAnnotation!) -> [NSColor]! {
  1682. if (annotation.isKind(of: CPDFMarkupAnnotation.self)) {
  1683. if (annotation as! CPDFMarkupAnnotation).markupType() == .highlight {
  1684. return KMAnnotationPropertiesColorManager.manager.markHighlightColors
  1685. } else {
  1686. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  1687. }
  1688. } else {
  1689. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  1690. }
  1691. }
  1692. func pdfListViewHaveDocumentAttribute() -> Bool {
  1693. if(!self.listView.document.allowsCopying) {
  1694. self.removeOwnerPassword()
  1695. return false
  1696. }
  1697. return true
  1698. }
  1699. func pdfListView(_ sender: CPDFListView!, showSnapshotAtPageNumber pageNum: Int, for rect: NSRect, scaleFactor: CGFloat, autoFits: Bool) {
  1700. let swc = KMSnapshotWindowController(windowNibName: "SnapshotWindow")
  1701. swc.delegate = self
  1702. swc.setPdfDocument(self.listView.document, goToPageNumber: pageNum, rect: rect, scaleFactor: scaleFactor, autoFits: autoFits)
  1703. swc.forceOnTop = self.interactionMode != .normal
  1704. self.myDocument?.addWindowController(swc)
  1705. }
  1706. func pdfListView(_ pdfView: CPDFListView!, documentDataDidChanged docData: Any!, withInfo info: [AnyHashable : Any]!) {
  1707. if let data = info?[CPDFListView.outlineKey] as? Bool, data { // 大纲改变
  1708. guard let ol = docData as? CPDFOutline else {
  1709. return
  1710. }
  1711. let add = info?[CPDFListView.outlineAddKey] as? Bool ?? false
  1712. let remove = info?[CPDFListView.outlineRemoveKey] as? Bool ?? false
  1713. if add {
  1714. self.leftSideViewController.addOutlineAfter(ol)
  1715. }
  1716. if remove {
  1717. self.leftSideViewController.removeOutlineAfter(ol)
  1718. }
  1719. let demote = info?[CPDFListView.outlineDemoteKey] as? Bool ?? false
  1720. let promote = info?[CPDFListView.outlinePromoteKey] as? Bool ?? false
  1721. if demote {
  1722. self.leftSideViewController.demoteOutlineAfter(ol)
  1723. }
  1724. if promote {
  1725. self.leftSideViewController.promoteOutlineAfter(ol)
  1726. }
  1727. }
  1728. }
  1729. //TextEdit
  1730. func pdfListViewDidTextFontChanged(_ pdfListView: CPDFListView!) {
  1731. self.rightSideViewController.eidtPDFTextProperty.reloadData()
  1732. }
  1733. func pdfListViewDidTextColorChanged(_ pdfListView: CPDFListView!, with color: NSColor!) {
  1734. self.rightSideViewController.eidtPDFTextProperty.fontColorChangeAction()
  1735. }
  1736. func pdfListViewAnnotationMeasureInfoChange(_ pdfListView: CPDFListView!, with annotation: CPDFAnnotation!) {
  1737. guard let data = annotation else {
  1738. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  1739. distanceMeasureInfoWindowController?.clearData()
  1740. }
  1741. // else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  1742. // perimeterMeasureInfoWindowController?.hideFloatingWindow()
  1743. // } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  1744. // areaMeasureInfoWindowController?.hideFloatingWindow()
  1745. // }
  1746. return
  1747. }
  1748. if let lineAnnotation = annotation as? CPDFLineAnnotation {
  1749. handleLineAnnotation(lineAnnotation)
  1750. } else if let polylineAnnotation = annotation as? CPDFPolylineAnnotation {
  1751. handlePolylineAnnotation(polylineAnnotation)
  1752. } else if let polygonAnnotation = annotation as? CPDFPolygonAnnotation {
  1753. handlePolygonAnnotation(polygonAnnotation)
  1754. }
  1755. }
  1756. func pdfListViewMeasureCancel(_ pdfListView: CPDFListView!) {
  1757. cancelMeasureType()
  1758. }
  1759. private func handleLineAnnotation(_ annotation: CPDFLineAnnotation) {
  1760. if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  1761. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  1762. distanceMeasureInfoWindowController?.showWindow(self)
  1763. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  1764. areaMeasureInfoWindowController?.hideFloatingWindow()
  1765. distanceMeasureInfoWindowController?.showWindow(self)
  1766. } else if distanceMeasureInfoWindowController?.window?.isVisible == false {
  1767. distanceMeasureInfoWindowController?.showWindow(self)
  1768. }
  1769. let measureInfo = annotation.measureInfo
  1770. let startPoint = annotation.startPoint
  1771. let endPoint = annotation.endPoint
  1772. let angle = atan2(endPoint.y - startPoint.y, endPoint.x - startPoint.x) * (180.0 / .pi)
  1773. distanceMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  1774. distanceMeasureInfoWindowController?.xLabel.stringValue = String(format: "%.0f", abs(endPoint.x - startPoint.x))
  1775. distanceMeasureInfoWindowController?.yLabel.stringValue = String(format: "%.0f", abs(endPoint.y - startPoint.y))
  1776. distanceMeasureInfoWindowController?.reloadData(with: measureInfo!)
  1777. }
  1778. private func handlePolylineAnnotation(_ annotation: CPDFPolylineAnnotation) {
  1779. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  1780. distanceMeasureInfoWindowController?.hideFloatingWindow()
  1781. perimeterMeasureInfoWindowController?.showWindow(self)
  1782. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  1783. areaMeasureInfoWindowController?.hideFloatingWindow()
  1784. perimeterMeasureInfoWindowController?.showWindow(self)
  1785. } else if perimeterMeasureInfoWindowController?.window?.isVisible == false {
  1786. perimeterMeasureInfoWindowController?.showWindow(self)
  1787. }
  1788. let measureInfo = annotation.measureInfo
  1789. let savePoints = annotation.savePoints()
  1790. var angle: CGFloat = 0
  1791. if savePoints.count >= 3 {
  1792. let count = savePoints.count
  1793. let startPoint = savePoints[count - 3].pointValue
  1794. let midPoint = savePoints[count - 2].pointValue
  1795. let endPoint = savePoints.last!.pointValue
  1796. angle = angleBetweenPoints(startPoint, midPoint, endPoint)
  1797. }
  1798. angle = 180 - angle
  1799. perimeterMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  1800. perimeterMeasureInfoWindowController?.reloadData(with: measureInfo!)
  1801. }
  1802. private func handlePolygonAnnotation(_ annotation: CPDFPolygonAnnotation) {
  1803. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  1804. distanceMeasureInfoWindowController?.hideFloatingWindow()
  1805. areaMeasureInfoWindowController?.showWindow(self)
  1806. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  1807. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  1808. areaMeasureInfoWindowController?.showWindow(self)
  1809. } else if areaMeasureInfoWindowController?.window?.isVisible == false {
  1810. areaMeasureInfoWindowController?.showWindow(self)
  1811. }
  1812. let measureInfo = annotation.measureInfo
  1813. let savePoints = annotation.savePoints
  1814. var angle: CGFloat = 0
  1815. if savePoints.count >= 3 {
  1816. let count = savePoints.count
  1817. let startPoint = (savePoints[count - 3] as AnyObject).pointValue
  1818. let midPoint = (savePoints[count - 2] as AnyObject).pointValue
  1819. let endPoint = (savePoints.lastObject as AnyObject).pointValue
  1820. angle = angleBetweenPoints(startPoint!, midPoint!, endPoint!)
  1821. }
  1822. angle = 180 - angle
  1823. areaMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  1824. areaMeasureInfoWindowController?.reloadData(measureInfo!)
  1825. }
  1826. private func angleBetweenPoints(_ startPoint: CGPoint, _ midPoint: CGPoint, _ endPoint: CGPoint) -> CGFloat {
  1827. let vector1 = CGPoint(x: midPoint.x - startPoint.x, y: midPoint.y - startPoint.y)
  1828. let vector2 = CGPoint(x: endPoint.x - midPoint.x, y: endPoint.y - midPoint.y)
  1829. let dotProduct = vector1.x * vector2.x + vector1.y * vector2.y
  1830. let magnitude1 = sqrt(vector1.x * vector1.x + vector1.y * vector1.y)
  1831. let magnitude2 = sqrt(vector2.x * vector2.x + vector2.y * vector2.y)
  1832. return acos(dotProduct / (magnitude1 * magnitude2)) * (180.0 / .pi)
  1833. }
  1834. }