KMMainViewController+UI.swift 97 KB

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