KMMainViewController.swift 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600
  1. //
  2. // KMMainViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Cocoa
  8. @objcMembers class KMMainViewController: KMBaseViewController, NSTextFieldDelegate,KMPresentationTopViewControllerDelegate {
  9. @IBOutlet var PDFContendView: NSView!
  10. @IBOutlet var centerContentView: NSView!
  11. @IBOutlet var listView: CPDFListView!
  12. @IBOutlet var secondaryPdfView: KMSecondaryPDFView?
  13. @IBOutlet weak var readContentView: NSView!
  14. @IBOutlet weak var tipCurrentPageBox: KMBox!
  15. @IBOutlet weak var rightView: NSView!
  16. @IBOutlet weak var leftView: NSView!
  17. @IBOutlet weak var mianSplitView: KMSplitView!
  18. @IBOutlet weak var pdfSplitView: KMSplitView!
  19. @IBOutlet weak var newPDFSplitView: KMSplitView!
  20. @IBOutlet weak var pdfContentView: NSView!
  21. @IBOutlet weak var pdfSplitSecondView: NSBox!
  22. @IBOutlet weak var locationPageView: NSView!
  23. @IBOutlet weak var tipLabel: NSTextField!
  24. @IBOutlet weak var toplayoutConstraint: NSLayoutConstraint!
  25. @IBOutlet var childToolbarController: KMToolbarViewController!
  26. @IBOutlet var toolbarController: KMToolbarController!
  27. @IBOutlet weak var toolbarBox: NSBox!
  28. @IBOutlet weak var heightOffset: NSLayoutConstraint!
  29. //阅读模式界面
  30. @IBOutlet weak var readModelView: KMReadModelView!
  31. @IBOutlet weak var bottomAreaView: KMBox!
  32. @IBOutlet weak var readModelViewWidthConstraint: NSLayoutConstraint!
  33. //页码显示器
  34. @IBOutlet weak var pageNumberDisplayView: KMPageNumberDisplayView!
  35. @IBOutlet weak var tipCurrentPageBoxWidthConstraint: NSLayoutConstraint!
  36. @IBOutlet weak var topTipBox: NSBox!
  37. @IBOutlet weak var exitFullButton: NSButton!
  38. var model = KMMainModel()
  39. var isReadMode: Bool = false
  40. var readAlertView: CustomAlertView?
  41. var readLeftMethodType: BotaType = .None
  42. var readLeftPanelOpen = false
  43. var readLastLeftPanWidth = 0.0
  44. var readLeftViewShowPanel = false
  45. var readRightPanelOpen = false
  46. var readToolbarType: KMToolbarViewType = .None
  47. var readToolbarItemIdentifier: String = ""
  48. var readToolMode: CToolMode = .textToolMode
  49. var readAnnotationType: CAnnotationType = .unkown
  50. var readSubViewType: RightSubViewType = .None
  51. //自动滚动
  52. var autoFlowOptionsSheetController: KMAutoFlowOptionsSheetController?
  53. //Search
  54. var searchIndex: Int = 0
  55. //Form
  56. var formAlertView: KMFormAlertView?
  57. //Secure
  58. var secureAlertView: KMSecureAlertView?
  59. //对比
  60. var isCompareModel: Bool = false {
  61. didSet {
  62. self.toolbarController.updataItemVisible()
  63. }
  64. }
  65. //合并
  66. var mergeWindowController: KMMergeWindowController?
  67. //水印
  68. var watermarkWindowController: KMWatermarkWindowController?
  69. //密码弹窗
  70. var passwordWindow: KMPasswordInputWindow?
  71. //对比
  72. var compressWIndowControllerNew: KMCompressWIndowControllerNew?
  73. //
  74. var securityWindowController: KMSecurityWindowController?
  75. //引导
  76. var guideInfoWindowController: KMGuideInfoWindowController?
  77. var presentationTopViewController: KMPresentationTopViewController?
  78. //春季活动
  79. var recommondPopWindowVC: KMRecommondPopWindow?
  80. private var _needSave = false
  81. var needSave: Bool {
  82. set {
  83. _needSave = newValue
  84. if (_needSave == false) {
  85. self.clearIsPDFDocumentEdited()
  86. }
  87. }
  88. get {
  89. return _needSave
  90. }
  91. }
  92. var isPDFDocumentEdited: Bool {
  93. get {
  94. return self.model.isPDFDocumentEdited
  95. }
  96. }
  97. var leftSideViewController: KMLeftSideViewController = KMLeftSideViewController.init(type: KMLeftMethodMode())
  98. var rightSideViewController: KMRightSideViewController!
  99. var searchResults: [KMSearchMode] = []
  100. var mwcFlags: MwcFlags = MwcFlags()
  101. var document: CPDFDocument?
  102. var myDocument: NSDocument?
  103. weak var browserWindowController: KMBrowserWindowController? //慎直接使用这个方法
  104. var cropSettingWindowController: KMCropSettingWindowController!
  105. var currentWindowController: NSWindowController!
  106. var savedNormalSetup: NSMutableDictionary = NSMutableDictionary()
  107. //数字签名
  108. var digitalSignController: KMPDFDigitalSignViewController?
  109. var redactController: KMPDFRedactViewController!
  110. let CPDFOfficeLeftSidePaneWidthKey = "CPDFOfficeLeftSidePaneWidthKey"
  111. let CPDFOfficeRightSidePaneWidthKey = "CPDFOfficeRightSidePaneWidthKey"
  112. var extract: KMExtractImageWindowController?
  113. var functionWidth: Double {
  114. get {
  115. if self.isReadMode {
  116. if !self.model.isShowBOTA {
  117. return 0
  118. }
  119. }
  120. return 48-4
  121. }
  122. }
  123. var pageNumber: UInt?
  124. var openSecondaryPdfView: KMSecondaryViewController?
  125. var secondaryPdfContentView: NSView?
  126. var lastSplitPDFHeight: Float = 0.0
  127. var pdfEditController: KMPDFEditViewController? {
  128. get {
  129. return self.getPDFEditController()
  130. }
  131. }
  132. var autoSaveTimer: Timer?
  133. private var _documentFirstLoad: Bool = true
  134. var eventMonitor: Any?
  135. var keyEventMonitor: Any?
  136. var mouseRightMenuEvent: NSEvent?
  137. var aiTranslationWindow: KMAITranslationWindowController?
  138. var aiTranslationConfirWC: KMAITranslationConfirmWindowController?
  139. lazy private var homeVC: KMHomeViewController? = {
  140. let vc = KMHomeViewController()
  141. return vc
  142. }()
  143. private var background_mask: NSView?
  144. fileprivate var _secureOptions: [CPDFDocumentWriteOption : Any]?
  145. var secureOptions: [CPDFDocumentWriteOption : Any]? {
  146. get {
  147. return self._secureOptions
  148. }
  149. }
  150. var documentAttribute: [CPDFDocumentAttribute : Any]?
  151. fileprivate var _removeSecureFlag = false
  152. var removeSecureFlag: Bool {
  153. get {
  154. return self._removeSecureFlag
  155. }
  156. }
  157. fileprivate var _saveWatermarkFlag = false
  158. var saveWatermarkFlag: Bool {
  159. get {
  160. return self._saveWatermarkFlag
  161. }
  162. }
  163. private var mainWindow_: NSWindow?
  164. var mainWindow: NSWindow? {
  165. get {
  166. return self.mainWindow_
  167. }
  168. set {
  169. self.mainWindow_ = newValue
  170. }
  171. }
  172. let editPDFHanddler = KMEditPDfHanddler()
  173. var distanceMeasureInfoWindowController: CDistanceMeasureInfoWindowController?
  174. var perimeterMeasureInfoWindowController: CPerimeterMeasureInfoWindowController?
  175. var areaMeasureInfoWindowController: CAreaMeasureInfoWindowController?
  176. var srHanddler: KMSearchReplaceHanddler = KMSearchReplaceHanddler()
  177. deinit {
  178. NotificationCenter.default.removeObserver(self)
  179. self.listView.delegate = nil
  180. self.listView.document?.delegate = nil
  181. self.editPDFHanddler.clearData()
  182. self.removeEventMonitor()
  183. self.removeKeyEventMonitor()
  184. }
  185. override func awakeFromNib() {
  186. super.awakeFromNib()
  187. self.addBackgroundMaskView()
  188. self.PDFContendView.backgroundColor(NSColor.km_init(hex: "FFFFFF"))
  189. listView.delegate = self
  190. listView.pdfListViewDelegate = self
  191. // listView.editingConfig().isSupportMultipleSelectEditingArea = true
  192. if (document != nil) {
  193. // if (self.document!.isLocked) {
  194. //
  195. // } else {
  196. // }
  197. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  198. self.listView.document = self.document
  199. self.listView.document?.delegate = self
  200. // }
  201. let autoScale = listView.autoScales
  202. if !autoScale {
  203. listView.scaleFactor = 1.0
  204. }
  205. }
  206. self.initPDFLeftViewVC()
  207. self.initRightSideView()
  208. self.toolbarController.listView = self.listView
  209. self.toolbarController.mainViewController = self
  210. self.leftSideViewController.mainViewController = self
  211. self.newPDFSplitView.delegate = self
  212. }
  213. override func viewDidAppear() {
  214. super.viewDidAppear()
  215. // //春季活动
  216. // if ((KMAdvertisementManager.manager.info.popWindowContent) != nil) {
  217. // if KMAdvertisementManager.manager.info.popWindowContent!.content!.count > 0 {
  218. // let info = KMAdvertisementManager.manager.info.popWindowContent!.content?.first
  219. // if KMAdvertisementManager.checkAdvertisementValid(info!) {
  220. // self.loadRecommondPopWindow()
  221. // }
  222. // }
  223. // }
  224. //刷新前一页后一页按钮
  225. self.updateNextAndPreViousButtonState()
  226. KMLightMemberManager.manager.canShowAdvancedView = false
  227. // Task { @MainActor in
  228. // await KMLightMemberManager.manager.canUseAdvanced(needNetworking: true)
  229. // }
  230. self.addEventMonitor()
  231. self.view.window?.makeFirstResponder(self.listView)
  232. // 更新属性页面的信息
  233. NotificationCenter.default.post(name: KMInfoWindowC.windowDidBecomeMainNotification, object: self.myDocument)
  234. self.interfaceThemeDidChanged(self.view.window?.appearance?.name ?? (NSApp.appearance?.name ?? .aqua))
  235. if (self.document == nil) {
  236. return
  237. }
  238. if (self.document == nil || self.document!.isLocked == false) {
  239. self.loadFunctionGuide()
  240. }
  241. if (self.document?.isLocked == false) {
  242. if self.model.needConvertNotes && self.tabViewIsDragging() == false {
  243. self.showConvertNotesProgress()
  244. }
  245. return
  246. }
  247. if (self.view.window == nil) {
  248. return
  249. }
  250. if (self.model.password != nil) {
  251. if let data = self.listView.document?.unlock(withPassword: self.model.password), data {
  252. self.model.isSaveKeyChain = false
  253. if self.model.needConvertNotes && self.tabViewIsDragging() == false {
  254. self.showConvertNotesProgress()
  255. }
  256. return
  257. }
  258. }
  259. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  260. if self.view.window != nil && self.tabViewIsDragging() == false {
  261. self.passwordWindow = KMPasswordInputWindow.openWindow(window: self.view.window!, url: self.document!.documentURL) { [weak self] result , password in
  262. if (result == .cancel) {
  263. // self.browserWindowController?.browser?.closeTab()
  264. (self?.myDocument as? KMMainDocument)?.browser?.closeTab()
  265. return
  266. }
  267. self?.model.isSaveKeyChain = true
  268. self?.listView?.document = self?.document
  269. self?.document?.unlock(withPassword: password)
  270. }
  271. } else {
  272. if self.model.needConvertNotes && self.tabViewIsDragging() == false {
  273. self.showConvertNotesProgress()
  274. }
  275. }
  276. }
  277. }
  278. override func viewWillDisappear() {
  279. super.viewWillDisappear()
  280. if self.interactionMode != .presentation {
  281. self.removeEventMonitor()
  282. }
  283. self.editPDFHanddler.hiddenWindows()
  284. }
  285. override func viewWillLayout() {
  286. super.viewWillLayout()
  287. if (KMTools.isFullScreen(self.view.window ?? NSWindow())) { // 全屏
  288. self.exitFullButton.isHidden = false
  289. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundFullScreenColor
  290. } else {
  291. self.exitFullButton.isHidden = true
  292. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor
  293. }
  294. if let guideWC = self.guideInfoWindowController{
  295. var rect = self.view.window!.frame
  296. rect.size.height -= 20
  297. guideWC.window?.setFrame(rect, display: false)
  298. guideWC.window?.minSize = rect.size
  299. guideWC.window?.maxSize = rect.size
  300. guideWC.show()
  301. }
  302. }
  303. override func viewDidLoad() {
  304. super.viewDidLoad()
  305. mwcFlags.settingUpWindow = 1
  306. toolbarController.delegate = self
  307. self.editPDFHanddler.viewC = self
  308. self.srHanddler.pdfView = self.listView
  309. //TODO: 先让项目运行,看后面怎么调整这段逻辑,目前最外层是 KMBrowserWindowController
  310. toolbarBox.contentView = toolbarController.view
  311. if (UserDefaults.standard.object(forKey: CPDFOfficeLeftSidePaneWidthKey) != nil) {
  312. UserDefaults.standard.set(256, forKey: CPDFOfficeLeftSidePaneWidthKey)
  313. UserDefaults.standard.synchronize()
  314. }
  315. if (UserDefaults.standard.object(forKey: CPDFOfficeRightSidePaneWidthKey) != nil) {
  316. UserDefaults.standard.set(256, forKey: CPDFOfficeRightSidePaneWidthKey)
  317. UserDefaults.standard.synchronize()
  318. }
  319. let position = mianSplitView.maxPossiblePositionOfDivider(at: 1)
  320. mianSplitView.setPosition(position, ofDividerAt: 0)
  321. mianSplitView.setPosition(mianSplitView.minPossiblePositionOfDivider(at: 0), ofDividerAt: 0)
  322. pdfSplitView.setPosition(mianSplitView.maxPossiblePositionOfDivider(at: 1), ofDividerAt: 0)
  323. self.locationPageView.wantsLayer = true;
  324. self.locationPageView.layer?.backgroundColor = NSColor(red: 189.0/255.0, green: 223.0/255.0, blue: 253.0/255.0, alpha: 1).cgColor
  325. self.tipLabel.stringValue = NSLocalizedString("Please use the scroll bar, thumbnail tool to locate the target page, click or box the area to select the target range.", comment: "")
  326. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  327. if (self.listView.document != nil) {
  328. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document?.documentURL.path ?? "")
  329. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document?.documentURL.path ?? "")
  330. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < (self.listView.document?.pageCount ?? 0)) {
  331. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  332. if (pageScale != nil) {
  333. self.listView.scaleFactor = CGFloat(pageScale!)
  334. }
  335. self.listView.go(toPageIndex: pageNumber!, animated: false)
  336. }
  337. } else {
  338. self._goToFirstPageForFristAppear()
  339. }
  340. }
  341. } else {
  342. self._goToFirstPageForFristAppear()
  343. }
  344. //阅读页面
  345. readModelView.delegate = self
  346. pageNumberDisplayView.delegate = self
  347. tipCurrentPageBox.moveCallback = { [unowned self] mouseEntered, mouseBox in
  348. if !isReadMode {
  349. if mouseEntered {
  350. self.pageNumberDisplayView.hover = true
  351. } else {
  352. self.pageNumberDisplayView.hover = false
  353. }
  354. }
  355. }
  356. NotificationCenter.default.addObserver(self, selector: #selector(rename(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerRename"), object: nil)
  357. NotificationCenter.default.addObserver(self, selector: #selector(closeTab(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseTabs"), object: nil)
  358. NotificationCenter.default.addObserver(self, selector: #selector(showInFinder(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerShowInFinder"), object: nil)
  359. NotificationCenter.default.addObserver(self, selector: #selector(preferenceDidChangeNotification), name: KMPreferenceManager.didChangeNotification, object: nil)
  360. NotificationCenter.default.addObserver(self, selector: #selector(documentDidUnlockNotification), name: Notification.Name("CPDFDocumentDidUnlockNotification"), object: nil)
  361. NotificationCenter.default.addObserver(self, selector: #selector(annotationsAttributeHasChange), name: NSNotification.Name.CPDFListViewAnnotationsAttributeHasChange, object:nil)
  362. NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminateNotification), name: NSApplication.willTerminateNotification, object: nil)
  363. NotificationCenter.default.addObserver(self, selector: #selector(KMPDFViewCurrentPageDidChangedNotification), name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: nil)
  364. NotificationCenter.default.addObserver(self, selector: #selector(CPDFDocumentPageCountChangedNotification), name: NSNotification.Name.init(rawValue: "CPDFDocumentPageCountChangedNotification"), object: nil)
  365. NotificationCenter.default.addObserver(self, selector: #selector(CEditPDFToolModeChangeStateUnkownNotification), name: Notification.Name.init("CEditPDFToolModeChangeStateUnkown"), object: nil)
  366. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.listView)
  367. NotificationCenter.default.addObserver(self, selector: #selector(handleDisplayBoxChangedNotification), name: NSNotification.Name.CPDFViewDisplayBoxChanged, object: self.listView)
  368. // 互动模式
  369. NotificationCenter.default.addObserver(self, selector: #selector(willEnterInteractionModeNotification), name: NSWindow.willEnterInteractionModeNotification, object: nil)
  370. NotificationCenter.default.addObserver(self, selector: #selector(didEnterInteractionModeNotification), name: NSWindow.didEnterInteractionModeNotification, object: nil)
  371. NotificationCenter.default.addObserver(self, selector: #selector(willShowFullScreenNotification), name: NSWindow.willShowFullScreenNotification, object: nil)
  372. NotificationCenter.default.addObserver(self, selector: #selector(didShowFullScreenNotification), name: NSWindow.didShowFullScreenNotification, object: nil)
  373. NotificationCenter.default.addObserver(self, selector: #selector(didAddContentViewNotification), name: NSWindow.didAddContentViewNotification, object: nil)
  374. NotificationCenter.default.addObserver(self, selector: #selector(addAutoSaveEvent), name: AutoSaveManager.kTimeValueChangedNotificationName, object: nil)
  375. NotificationCenter.default.addObserver(self, selector: #selector(didRemoveAnnotationNotification), name: NSNotification.Name.CPDFPageDidRemoveAnnotation, object: nil)
  376. Task {
  377. self.addAutoSaveEvent()
  378. }
  379. self.toolbarController.selectItem(KMDocumentAnnotationToolbarItemIdentifier)
  380. self.closeRightPane()
  381. self.addKeyEventMonitor()
  382. self.addAdsBannerView()
  383. //检测OCR包是否需要更新
  384. #if VERSION_DMG
  385. // KMResourceDownloadManager.manager.checkDocumentAIVersion()
  386. #endif
  387. // Open snapshots?
  388. var snapshotSetups: NSArray?
  389. // if (hasWindowSetup)
  390. // snapshotSetups = [savedNormalSetup objectForKey:SNAPSHOTS_KEY];
  391. // else if ([sud boolForKey:SKRememberSnapshotsKey])
  392. if KMPreferenceManager.shared.rememberSnapshot {
  393. // snapshotSetups = [[SKBookmarkController sharedBookmarkController] snapshotsForRecentDocumentAtURL:[(NSDocument *)[self document] fileURL]];
  394. if let fileUrl = (self.myDocument as? KMMainDocument)?.fileURL {
  395. snapshotSetups = SKBookmarkController.shared().snapshotsForRecentDocument(at: fileUrl) as NSArray?
  396. }
  397. }
  398. if let cnt = snapshotSetups?.count, cnt > 0 {
  399. if let data = self.listView?.document?.isLocked, data {
  400. self.savedNormalSetup.setObject(snapshotSetups as Any, forKey: "snapshots" as NSCopying)
  401. } else {
  402. self.showSnapshots(setups: snapshotSetups)
  403. }
  404. }
  405. let readModel = UserDefaults.standard.bool(forKey: "kKMPDFViewIsReadMode")
  406. if readModel == true {
  407. self.openReadModel()
  408. }
  409. let hasWindowSetup = savedNormalSetup.count > 0
  410. if UserDefaults.standard.dictionary(forKey: KMDefaultPDFDisplaySettingsKey) != nil {
  411. let pdfSettings: NSDictionary = hasWindowSetup ? savedNormalSetup : UserDefaults.standard.dictionary(forKey: KMDefaultPDFDisplaySettingsKey)! as NSDictionary
  412. self.applyPDFSettings(pdfSettings)
  413. } else {
  414. self.applyPDFSettings(savedNormalSetup)
  415. }
  416. //文字
  417. let fontManager = NSFontManager.shared
  418. fontManager.target = self
  419. fontManager.action = #selector(changeFont(_:))
  420. }
  421. //MARK: - PDFListView
  422. func initPDFLeftViewVC() {
  423. var frame = self.leftView.frame
  424. frame.size.width += 44
  425. self.leftView.frame = frame
  426. leftSideViewController.isFirst = true
  427. leftSideViewController.listView = self.listView
  428. leftSideViewController.view.frame = CGRect(x: 0, y:0 , width: self.leftView.frame.size.width, height: self.leftView.frame.size.height)
  429. leftSideViewController.view.autoresizingMask = [.height,.width]
  430. leftSideViewController.delegate = self
  431. self.leftView.addSubview(leftSideViewController.view)
  432. }
  433. func initRightSideView() {
  434. self.rightSideViewController = KMRightSideViewController.init()
  435. self.rightSideViewController.view.frame = CGRect(x: 0, y: 0, width: self.rightView.frame.width, height: self.rightView.frame.size.height)
  436. self.rightSideViewController.view.autoresizingMask = [.height,.width]
  437. self.rightSideViewController.listView = self.listView
  438. // self.rightSideViewController.view.isHidden = true
  439. self.rightSideViewController.isHidden = true
  440. self.rightSideViewController.delegate = self
  441. self.rightView.addSubview(self.rightSideViewController.view)
  442. self.rightSideViewController.mainController = self
  443. self.rightSideViewController.propertyDidChange = { [weak self] model in
  444. if let anno = model as? CSelfSignAnnotation {
  445. self?.leftSideViewController.refreshUIForAnnoAttributeDidChange(anno, attributes: nil)
  446. }
  447. }
  448. }
  449. func addAdsBannerView() {
  450. #if VERSION_FREE
  451. if !IAPProductsManager.default().isAvailableAllFunction(){
  452. guard let document = self.listView.document else {
  453. return
  454. }
  455. if !document.isLocked {
  456. KMAdsManager.defaultManager.beginSheetModalForView(self.readContentView, directions: .down, adPosY: 30, animated: false) { pageIndex in
  457. }
  458. }
  459. NotificationCenter.default.addObserver(self, selector: #selector(purchaseStateUpdateNoti), name: NSNotification.Name(rawValue: "KMIAPProductPurchasedNotification"), object: nil)
  460. NotificationCenter.default.addObserver(self, selector: #selector(purchaseStateUpdateNoti), name: NSNotification.Name(rawValue: "kDeviceActivateNotification"), object: nil)
  461. }
  462. #endif
  463. //加载底部banner
  464. // - (void)loadingAdsManager {
  465. // #if VERSION_FREE
  466. // if(![self.pdfDocument isLocked]) {
  467. // if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
  468. // [[KMAdsManager defaultManager] beginSheetModalForView:self.pdfView
  469. // directions:KMADViewDirectionsDown
  470. // animated:NO
  471. // completionHandler:nil];
  472. // }
  473. // [[NSNotificationCenter defaultCenter] addObserverForName:KMIAPProductPurchasedNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){
  474. // if ([IAPProductsManager defaultManager].isAvailableAllFunction) {
  475. // [[KMAdsManager defaultManager] dismissSheetModalForView:self.pdfView];
  476. // }
  477. // }];
  478. // [[NSNotificationCenter defaultCenter] addObserverForName:kDeviceActivateStatusChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){
  479. // if ([IAPProductsManager defaultManager].isAvailableAllFunction) {
  480. // [[KMAdsManager defaultManager] dismissSheetModalForView:self.pdfView];
  481. // }
  482. // }];
  483. // }
  484. // #endif
  485. // }
  486. }
  487. // MARK: Private Methods
  488. // 是否阿拉伯语言
  489. private func _isArabicLanguage() -> Bool {
  490. return NSLocalizedString("Right click a color and select “Change Color...“.", comment: "") == "انقر بزر الماوس الأيمن فوق اللون وحدد \"تغيير اللون...\"."
  491. }
  492. internal func removeNotifications() {
  493. NotificationCenter.default.removeObserver(self)
  494. self.leftSideViewController.clearAnnotationFilterData()
  495. self.leftSideViewController.clearNotification()
  496. }
  497. func checkShouldAutoOpenLeftVC() {
  498. if KMPreference.shared.showLeftSideBar == false {
  499. return
  500. }
  501. if self.model.leftPanelOpen {
  502. return
  503. }
  504. let readModel = UserDefaults.standard.bool(forKey: "kKMPDFViewIsReadMode")
  505. if readModel == true {
  506. return
  507. }
  508. DispatchQueue.main.async {
  509. self.leftSideViewController.showThumbnail()
  510. self.toolbarController.findItem(KMLeftControlToolbarItemIdentifier)?.isSelected = true
  511. }
  512. }
  513. func applyLeftSideWidth(_ leftSideWidth: CGFloat, rightSideWidth: CGFloat) -> Void {
  514. mianSplitView.setPosition(leftSideWidth, ofDividerAt: 0)
  515. mianSplitView.setPosition(mianSplitView.maxPossiblePositionOfDivider(at: 1) - mianSplitView.dividerThickness - rightSideWidth, ofDividerAt: 1)
  516. self.model.lastLeftPanWidth = leftSideWidth
  517. self.model.lastRightPanWidth = rightSideWidth
  518. }
  519. internal var removeAllAnnotationsStore = KMPDFViewRemoveAllAnnotationsStore()
  520. internal func removeAllAnnotations() {
  521. let alert = NSAlert()
  522. alert.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  523. alert.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  524. alert.addButton(withTitle: NSLocalizedString("No", comment:""))
  525. if (alert.runModal() != .alertFirstButtonReturn) {
  526. return
  527. }
  528. DispatchQueue.main.async {
  529. self.removeAllAnnotationsStore.store(t: self.listView)
  530. }
  531. }
  532. @objc func cancelMeasureType() {
  533. self.hideMeasureFloatingWindows()
  534. self.toolbarController?.findItem(KMToolbarMeasureItemIdentifier)?.isSelected = false
  535. }
  536. func hideMeasureFloatingWindows() {
  537. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  538. distanceMeasureInfoWindowController?.hideFloatingWindow()
  539. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  540. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  541. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  542. areaMeasureInfoWindowController?.hideFloatingWindow()
  543. }
  544. }
  545. func showMeasureFloatingWindowsIfNeed() {
  546. let toolMode = self.listView?.toolMode ?? .none
  547. if toolMode != .measureToolMode {
  548. return
  549. }
  550. let type = self.listView?.annotationType ?? .none
  551. if type == .line {
  552. self.distanceMeasureInfoWindowController?.window?.orderFront(nil)
  553. } else if type == .polyLine {
  554. self.perimeterMeasureInfoWindowController?.window?.orderFront(nil)
  555. } else if type == .polyGon {
  556. self.areaMeasureInfoWindowController?.window?.orderFront(nil)
  557. } else if type == .square {
  558. self.areaMeasureInfoWindowController?.window?.orderFront(nil)
  559. }
  560. }
  561. // MARK: Set Methods
  562. var setDocument: CPDFDocument? {
  563. get {
  564. return document
  565. }
  566. set {
  567. if document != newValue {
  568. document = newValue
  569. }
  570. listView.document = document
  571. listView.document?.delegate = self
  572. self.listView.layoutDocumentView()
  573. self.leftSideViewController.refreshUIForDocumentChanged()
  574. }
  575. }
  576. var setPageNumber: UInt {
  577. get {
  578. return pageNumber!
  579. }
  580. set {
  581. let pageCount = listView.document?.pageCount ?? 0
  582. var value = newValue
  583. if value > pageCount {
  584. value = pageCount
  585. }
  586. if value > 0 && listView.currentPage().pageIndex() != value-1 {
  587. listView.go(to: listView.document?.page(at: value-1))
  588. }
  589. if pageNumber != value {
  590. pageNumber = value
  591. }
  592. }
  593. }
  594. // MARK: - 标记密文
  595. func enterRedact() {
  596. if !IAPProductsManager.default().isAvailableAllFunction(){
  597. let winC = KMPurchaseCompareWindowController.sharedInstance()
  598. winC?.kEventName = "Reading_Redact_BuyNow"
  599. winC?.showWindow(nil)
  600. return
  601. }
  602. AccountTools.canUseAdvance_success { can in
  603. if can {
  604. self.oem_enterRedact()
  605. }
  606. }
  607. }
  608. func oem_enterRedact() {
  609. if self.listView.document?.allowsPrinting == false || self.listView.document?.allowsCopying == false {
  610. Task {
  611. _ = await KMAlertTool.runModel(message: KMLocalizedString("This is a secured document. Editing is not permitted.", nil))
  612. }
  613. return
  614. }
  615. if self.hasEnterRedact() {
  616. self.exitRedact()
  617. return
  618. }
  619. self.commitEditingIfNeed()
  620. self.leftSideViewController.thumbnailTableView.isEnabled = false
  621. self.leftSideViewController.tocOutlineView.isEnabled = false
  622. self.leftSideViewController.noteOutlineView.isEnabled = false
  623. self.leftSideViewController.findTableView.isEnabled = false
  624. self.leftSideViewController.groupedFindTableView.isEnabled = false
  625. self.leftSideViewController.snapshotTableView.isEnabled = false
  626. let ttsWindowC = KMTTSWindowController.share
  627. if ttsWindowC.pdfView?.document?.documentURL.path == self.listView.document?.documentURL.path {
  628. if let data = ttsWindowC.window?.isVisible, data {
  629. ttsWindowC.stopSpeaking()
  630. ttsWindowC.close()
  631. }
  632. }
  633. NSColorPanel.shared.showsAlpha = false
  634. redactController = KMPDFRedactViewController(url: self.listView.document!.documentURL, password: self.listView.document?.password)
  635. self.addChild(redactController)
  636. self.PDFContendView.addSubview(redactController.view)
  637. redactController.view.frame = self.PDFContendView.bounds
  638. redactController.view.autoresizingMask = [.width, .height]
  639. self.listView.isHidden = true
  640. redactController.scaleFactor = self.listView.scaleFactor
  641. redactController.titleBack = { [weak self] title in
  642. self?.view.window?.title = title
  643. }
  644. redactController.callback = { [weak self] result, currentPageIndex, saveResult, saveUrl in
  645. self?.listView.go(toPageIndex: self!.redactController.redactPdfView.currentPageIndex, animated: false)
  646. if result == false { // 退出
  647. self?.exitRedact()
  648. return
  649. }
  650. let controller = self?._getPDFRedactController()
  651. controller?.redactPdfView.newAddAnnotation.removeAll()
  652. self?.exitRedact()
  653. if saveResult {
  654. let newDocument = CPDFDocument(url: saveUrl)
  655. if let data = newDocument?.isLocked, data {
  656. newDocument?.unlock(withPassword: self?.listView.document?.password ?? "")
  657. }
  658. self?.document = newDocument
  659. self?.listView.document = newDocument
  660. self?.listView.layoutDocumentView()
  661. }
  662. }
  663. redactController.setCurrentPageIndex(self.listView.currentPageIndex)
  664. }
  665. func exitRedact() {
  666. self.leftSideViewController.thumbnailTableView.isEnabled = true
  667. self.leftSideViewController.tocOutlineView.isEnabled = true
  668. self.leftSideViewController.noteOutlineView.isEnabled = true
  669. self.leftSideViewController.findTableView.isEnabled = true
  670. self.leftSideViewController.groupedFindTableView.isEnabled = true
  671. self.leftSideViewController.snapshotTableView.isEnabled = true
  672. let controller = self._getPDFRedactController()
  673. if let data = controller {
  674. if data.redactPdfView.newAddAnnotation.count > 0 {
  675. KMAlertTool.runModel(message: "", informative: KMLocalizedString("There are unapplied redactions in this file. Exit will not save redaction.", nil), buttons: [KMLocalizedString("Exit", nil), KMLocalizedString("Cancel", nil)]) { response in
  676. if response == .alertFirstButtonReturn {
  677. controller?.redactPdfView.newAddAnnotation.removeAll()
  678. self.exitRedact()
  679. }
  680. }
  681. return
  682. }
  683. }
  684. NSColorPanel.shared.showsAlpha = true
  685. self.toolbarController.findItem(KMDocumentRedactToolbarItemIdentifier)?.isSelected = false
  686. // self.toolbarController.toolbarType = .None
  687. // self.listView.toolMode = .moveToolMode
  688. controller?.redactPdfView.resignMonitor()
  689. controller?.view.removeFromSuperview()
  690. controller?.removeFromParent()
  691. self.listView.isHidden = false
  692. // self.listView.layoutDocumentView()
  693. // self.view.window?.makeFirstResponder(self.listView)
  694. self.listView.annotationType = .unkown
  695. }
  696. func hasEnterRedact() -> Bool {
  697. return self._getPDFRedactController() != nil
  698. }
  699. //MARK: - AI
  700. func showAITypeChooseView(aiConfigType: AIConfigType) -> Void {
  701. if (self.document != nil) {
  702. AIChatInfoManager.defaultManager.currentFilePath = (self.document?.documentURL.path)!
  703. } else {
  704. AIChatInfoManager.defaultManager.currentFilePath = ""
  705. }
  706. let windowVC: AINewConfigWindowController = AINewConfigWindowController.currentWC()
  707. windowVC.chooseCurFileHandle = {[unowned self] windowVC in
  708. if AIChatInfoManager.defaultManager.currentFilePath.isEmpty == false {
  709. let documentArray = NSDocumentController.shared.documents
  710. var didFileEdit: Bool = false
  711. var curDoc: KMMainDocument!
  712. for document in documentArray {
  713. if document.fileURL?.path == AIChatInfoManager.defaultManager.currentFilePath {
  714. didFileEdit = document.isDocumentEdited
  715. curDoc = document as! KMMainDocument
  716. break
  717. }
  718. }
  719. if didFileEdit {
  720. let tempFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(AIChatInfoManager.defaultManager.currentFilePath.lastPathComponent)
  721. if FileManager.default.fileExists(atPath: tempFileURL.path) {
  722. do {
  723. try FileManager.default.removeItem(at: tempFileURL)
  724. } catch {
  725. }
  726. }
  727. if curDoc != nil {
  728. curDoc.mainViewController?.SaveTempPDFDocumentToURLPath(tempPath: tempFileURL.path)
  729. }
  730. }
  731. windowVC.window?.becomeMain()
  732. }
  733. }
  734. if windowVC.window?.isVisible == true && windowVC.didSetOriginFrame == true {
  735. } else {
  736. var windowRect = windowVC.window?.frame
  737. windowRect!.origin.x = NSMaxX(self.view.window!.frame) - (windowRect?.size.width)!
  738. windowRect!.origin.y = NSMaxY(self.view.window!.frame) - (windowRect?.size.height)! - 64
  739. windowVC.window?.setFrame(windowRect!, display: true)
  740. windowVC.didSetOriginFrame = true
  741. }
  742. windowVC.eventLabel = "AITools_Tbr"
  743. windowVC.showWindow(nil)
  744. if (aiConfigType != .none) {
  745. windowVC.eventLabel = "AITools_Start"
  746. if self.listView.currentSelection?.string()?.isEmpty == false {
  747. windowVC.setCurrentPDFSelection(self.listView.currentSelection.string())
  748. }
  749. windowVC.chooseAIFunctionWithType(aiConfigType)
  750. }
  751. }
  752. @objc func aiTipIconViewShowStateChangeNoti() {
  753. }
  754. //MARK: - 引导
  755. func loadFunctionGuide() -> Void {
  756. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  757. if self.view.window != nil {
  758. self.loadOpenFileFunctionGuide(.openFileNormal)
  759. }
  760. }
  761. }
  762. func loadOpenFileFunctionGuide(_ showType: KMGuideInfoType) -> Void {
  763. if showType == .openFileNormal && KMGuideInfoWindowController.availableShow(.openFileNormal) {
  764. let leftPanelItem:KMToolbarItemView = self.toolbarController.findItem("KMLeftControlToolbarItemIdentifier")!
  765. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  766. guard let guideWC = self.guideInfoWindowController else { return }
  767. guideWC.type = .openFileNormal
  768. guideWC.openPanelRect = (self.view.window?.contentView?.convert(leftPanelItem.frame, from: leftPanelItem.superview)) ?? .zero
  769. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  770. guideWC.normalGuideFinishHandle = { [weak self] windowVC in
  771. let rightPanelItem = self?.toolbarController.findItem(KMRightControlToolbarItemIdentifier)
  772. let digitalPanelItem = self?.toolbarController.findItem(KMDocumentSignToolbarItemIdentifier)
  773. if let data = rightPanelItem, data.isHidden {
  774. let view = self?.toolbarController.mainToolBarView?.toolbar?.moreButton
  775. windowVC.rightPanelRect = (self?.view.window?.contentView?.convert(view?.frame ?? .zero, from: view?.superview)) ?? .zero
  776. } else {
  777. windowVC.rightPanelRect = (self?.view.window?.contentView?.convert(rightPanelItem?.frame ?? .zero, from: rightPanelItem?.superview)) ?? .zero
  778. }
  779. guideWC.digitalBoxRect = (self?.view.window?.contentView?.convert(digitalPanelItem?.frame ?? .zero, from: digitalPanelItem?.superview)) ?? .zero
  780. }
  781. guideWC.finishHandle = { [weak self] windowVC, type in
  782. if type == .windowNewFinish ||
  783. type == . windowDigitalFinish {
  784. self?.checkFirstTrialController()
  785. }
  786. }
  787. guideWC.openFileToggleHandle = { [weak self] windowVC, type in
  788. self?.checkFirstTrialController()
  789. }
  790. var rect = self.view.window!.frame
  791. rect.size.height -= 20
  792. guideWC.window?.setFrame(rect, display: false)
  793. guideWC.window?.minSize = rect.size
  794. guideWC.window?.maxSize = rect.size
  795. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  796. guideWC.show()
  797. } else if showType == .digitalSignGuide && KMGuideInfoWindowController.availableShow(.digitalSignGuide) {
  798. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  799. guard let guideWC = self.guideInfoWindowController else { return }
  800. guideWC.type = .digitalSignGuide
  801. guard let digitalPanelItem = self.toolbarController.findItem(KMDocumentSignToolbarItemIdentifier) else {
  802. return
  803. }
  804. guideWC.digitalBoxRect = (self.view.window?.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview))!
  805. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  806. guideWC.finishHandle = { [weak self] windowVC, type in
  807. self?.checkFirstTrialController()
  808. }
  809. var rect = self.view.window!.frame
  810. rect.size.height -= 20
  811. guideWC.window?.setFrame(rect, display: false)
  812. guideWC.window?.minSize = rect.size
  813. guideWC.window?.maxSize = rect.size
  814. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  815. guideWC.show()
  816. } else if showType == .pdfCompareGuide && KMGuideInfoWindowController.availableShow(.pdfCompareGuide) {
  817. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  818. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  819. guard let guideWC = self.guideInfoWindowController else { return }
  820. guideWC.type = .pdfCompareGuide
  821. guard let digitalPanelItem = self.toolbarController.findItem(KMDocumentSignToolbarItemIdentifier) else {
  822. return
  823. }
  824. guard let win = self.view.window else {
  825. return
  826. }
  827. guideWC.digitalBoxRect = (win.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview)) ?? .zero
  828. let compareItem = self.toolbarController.findItem(KMToolbarComparisonItemIdentifier)
  829. guideWC.compareItemRect = (win.contentView?.convert(compareItem?.frame ?? .zero, from: compareItem?.superview)) ?? .zero
  830. guideWC.finishHandle = { [weak self] winC, type in
  831. if type == .windowNewFinish {
  832. DispatchQueue.main.async {
  833. self?.loadOpenFileFunctionGuide(.measureGuide)
  834. }
  835. return
  836. }
  837. }
  838. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  839. var rect = self.view.window!.frame
  840. rect.size.height -= 20
  841. guideWC.window?.setFrame(rect, display: false)
  842. guideWC.window?.minSize = rect.size
  843. guideWC.window?.maxSize = rect.size
  844. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  845. guideWC.show()
  846. }
  847. } else if showType == .measureGuide && KMGuideInfoWindowController.availableShow(.measureGuide) {
  848. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  849. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  850. guard let guideWC = self.guideInfoWindowController else { return }
  851. guard let _ = self.view.window else {
  852. return
  853. }
  854. guideWC.type = .measureGuide
  855. guard let digitalPanelItem = self.toolbarController.findItem(KMDocumentSignToolbarItemIdentifier) else {
  856. return
  857. }
  858. guideWC.digitalBoxRect = (self.view.window?.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview)) ?? .zero
  859. let compareItem:KMToolbarItemView = self.toolbarController.findItem(KMToolbarMeasureItemIdentifier)!
  860. guideWC.compareItemRect = (self.view.window?.contentView?.convert(compareItem.frame, from: compareItem.superview)) ?? .zero
  861. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  862. var rect = self.view.window?.frame ?? .zero
  863. rect.size.height -= 20
  864. guideWC.window?.setFrame(rect, display: false)
  865. guideWC.window?.minSize = rect.size
  866. guideWC.window?.maxSize = rect.size
  867. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  868. guideWC.show()
  869. }
  870. } else if showType == .convertGuide && KMGuideInfoWindowController.availableShow(.convertGuide) {
  871. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  872. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  873. guard let guideWC = self.guideInfoWindowController else { return }
  874. guideWC.type = .convertGuide
  875. guard let digitalPanelItem = self.toolbarController.findItem(KMDocumentSignToolbarItemIdentifier) else {
  876. return
  877. }
  878. guard let win = self.view.window else {
  879. return
  880. }
  881. guideWC.digitalBoxRect = (self.view.window?.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview)) ?? .zero
  882. guideWC.purchaseHandle = { [weak self] windowVC in
  883. #if VERSION_DMG
  884. if IAPProductsManager.default().isAvailableAllFunction() {
  885. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  886. //Convert:
  887. self?.showAllConvertWindow(convertT: .Word)
  888. } else {
  889. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  890. limitWC.continueBlock = { windowController in
  891. }
  892. limitWC.window?.center()
  893. limitWC.showWindow(nil)
  894. }
  895. } else {
  896. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  897. }
  898. #else
  899. if IAPProductsManager.default().isAvailableAllFunction() {
  900. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  901. //Convert:
  902. } else {
  903. var vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  904. vc.showWindow(nil)
  905. }
  906. } else {
  907. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  908. }
  909. #endif
  910. }
  911. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  912. var rect = self.view.window?.frame ?? .zero
  913. rect.size.height -= 20
  914. guideWC.window?.setFrame(rect, display: false)
  915. guideWC.window?.minSize = rect.size
  916. guideWC.window?.maxSize = rect.size
  917. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  918. guideWC.show()
  919. }
  920. } else {
  921. }
  922. }
  923. func checkFirstTrialController() -> Void {
  924. #if VERSION_DMG
  925. //打开文档后引导相关
  926. if VerificationManager.default().status == .none {
  927. let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  928. let lastVersion = UserDefaults.standard.object(forKey: "SKLastTrialVersionMainDocumentLaunchedKey") as? String ?? ""
  929. if lastVersion.isEmpty || lastVersion != appVersion {
  930. UserDefaults.standard.setValue(appVersion, forKey: "SKLastTrialVersionMainDocumentLaunchedKey")
  931. UserDefaults.standard.synchronize()
  932. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  933. }
  934. }
  935. #endif
  936. }
  937. // MARK: - 页面编辑
  938. open func enterPageEdit(_ pages: [Int] = []) {
  939. if let doc = self.listView?.document {
  940. if doc.allowsCopying == false || doc.allowsPrinting == false {
  941. KMBaseWindowController.checkPassword(url: doc.documentURL, type: .owner) { result, pwd in
  942. if result && pwd.isEmpty == false {
  943. self.listView?.document?.unlock(withPassword: pwd)
  944. Task { @MainActor in
  945. self.enterPageEdit(pages)
  946. }
  947. } else {
  948. self.exitPageEdit()
  949. }
  950. }
  951. return
  952. }
  953. }
  954. //选中page
  955. var tPages = pages
  956. if tPages.count == 0 {
  957. if self.leftSideViewController.type.methodType == .Thumbnail {
  958. tPages = self.leftSideViewController.thumb_fetchSelectedRows() ?? [self.listView.currentPageIndex]
  959. }
  960. }
  961. if (hasEnterPageEdit()) {
  962. exitPageEdit()
  963. return
  964. }
  965. if (self.toolbarController != nil && self.toolbarController.mainToolBarView != nil) {
  966. let toolBarView: KMToolbarViewController = self.toolbarController.mainToolBarView!
  967. for (key, value) in toolBarView.toolbarItems {
  968. if (key == KMRightControlToolbarItemIdentifier || key == KMLeftControlToolbarItemIdentifier) {
  969. (value as! KMToolbarItemView).unEnabled = true
  970. }
  971. }
  972. }
  973. self.editPDFHanddler.clearData()
  974. let controller = KMPDFEditViewController(self.listView.document)
  975. controller.selectedPages = tPages
  976. controller.listView = self.listView
  977. self.addChild(controller)
  978. self.PDFContendView.addSubview(controller.view)
  979. controller.view.frame = self.PDFContendView.bounds
  980. controller.view.autoresizingMask = [.width,.height]
  981. self.listView.isHidden = true
  982. controller.itemClick = { [weak self] index, params in
  983. if (index == 1) { /// 双击退出
  984. self?.enterEditMode(self!.leftSideViewController, [])
  985. DispatchQueue.main.async {
  986. let pageIndex: Int = params.first as! Int
  987. self?.listView.go(toPageIndex: pageIndex, animated: true)
  988. }
  989. } else if (index == 2) { // 打印
  990. self?.showPrintWindow(pageRange: KMPrintPageRange(type: .custom, selectPages: params.first as! [Int]))
  991. }
  992. }
  993. controller.documentEditedCallback = { [weak self] params in
  994. self?.recordIsPDFDocumentEdited()
  995. }
  996. controller.selectionDidChange = { selectedIndexs in
  997. var indexSet = IndexSet()
  998. for indexPath in selectedIndexs {
  999. indexSet.insert(indexPath.item)
  1000. }
  1001. if indexSet.count != 0 {
  1002. // self?.leftSideViewController.thumbnailViewController.selectPages(indexs: indexSet, needScroll: true)
  1003. // self?.listView.go(toPageIndex: indexSet.first!, animated: false)
  1004. }
  1005. }
  1006. }
  1007. open func exitPageEdit() {
  1008. if (self.toolbarController != nil && self.toolbarController.mainToolBarView != nil) {
  1009. let toolBarView: KMToolbarViewController = self.toolbarController.mainToolBarView!
  1010. for (key, value) in toolBarView.toolbarItems {
  1011. if (key == KMRightControlToolbarItemIdentifier || key == KMLeftControlToolbarItemIdentifier) {
  1012. (value as! KMToolbarItemView).unEnabled = false
  1013. }
  1014. }
  1015. }
  1016. self.toolbarController.findItem(KMDocumentPageToolbarItemIdentifier)?.isSelected = false
  1017. let editController = getPDFEditController()
  1018. if (editController == nil) {
  1019. return
  1020. }
  1021. self.listView.annotationType = .highlight
  1022. // FIXME: - sdk修复插入特定文档crash后,这行代码可以去掉
  1023. self.leftSideViewController.model.insertedDocumentSet.formUnion(editController?.model.insertedDocumentSet ?? [])
  1024. editController?.view.removeFromSuperview()
  1025. editController?.removeFromParent()
  1026. self.listView.isHidden = false
  1027. self.listView.layoutDocumentView()
  1028. self.view.window?.makeFirstResponder(self.listView)
  1029. self.listView.annotationType = .unkown
  1030. self.listView.go(toPageIndex: editController!.listViewCurrentIndex, animated: false)
  1031. if let data = editController?.isEdited, data {
  1032. self.leftSideViewController.reloadThumbnailDataIfNeed()
  1033. self.leftSideViewController.note_reloadDataIfNeed()
  1034. self.leftSideViewController.refreshUIOfOutlineIfNeed()
  1035. self.leftSideViewController.refreshUIOfSeachListIfNeed()
  1036. self.leftSideViewController.refreshUIOfBookmarkIfNeed()
  1037. }
  1038. }
  1039. open func hasEnterPageEdit() -> Bool {
  1040. return self.getPDFEditController() != nil
  1041. }
  1042. // MARK: - Edit PDF
  1043. func enterEditPDF() {
  1044. self.editPDFHanddler.enterEditPDF()
  1045. }
  1046. // MARK: - 数字签名
  1047. func hasShowDigitalSign() -> Bool {
  1048. return self.digitalSignController?.view.superview != nil
  1049. }
  1050. func canEnterDigitalSign() -> Bool {
  1051. guard let doc = self.listView?.document else {
  1052. return false
  1053. }
  1054. return doc.allowsPrinting && doc.allowsCopying
  1055. }
  1056. func enterDigitalSign() {
  1057. self.listView.toolMode = .textToolMode
  1058. if self.hasShowDigitalSign() {
  1059. self.exitDigitalSign()
  1060. } else {
  1061. if self.needSaveDocument() {
  1062. self.saveDocumentWithProgressAlert { [unowned self] params in
  1063. if (self.listView.document != nil) {
  1064. self.showDigitalSignWindow(withFilePathURL: self.listView.document.documentURL)
  1065. }
  1066. }
  1067. return
  1068. }
  1069. if (self.listView.document != nil) {
  1070. self.showDigitalSignWindow(withFilePathURL: self.listView.document.documentURL)
  1071. }
  1072. }
  1073. }
  1074. func exitDigitalSign() {
  1075. self.digitalSignController?.view.removeFromSuperview()
  1076. // self.digitalSignController = nil
  1077. // KMDocumentDigitalSignToolbarItemIdentifier
  1078. self.toolbarController.findItem(KMDocumentSignToolbarItemIdentifier)?.isSelected = false
  1079. }
  1080. func showDigitalSignWindow(withFilePathURL fileURL: URL) {
  1081. if !IAPProductsManager.default().isAvailableAllFunction(){
  1082. let winC = KMPurchaseCompareWindowController.sharedInstance()
  1083. winC?.kEventName = "Reading_DigitalSign_BuyNow"
  1084. winC?.showWindow(nil)
  1085. return
  1086. }
  1087. AccountTools.canUseAdvance_success { can in
  1088. if can {
  1089. self.oem_showDigitalSignWindow(withFilePathURL: fileURL)
  1090. }
  1091. }
  1092. }
  1093. func oem_showDigitalSignWindow(withFilePathURL fileURL: URL) {
  1094. if hasShowDigitalSign() {
  1095. self.exitDigitalSign()
  1096. }
  1097. var currentPageIndex = listView.document?.index(for: listView.currentPage()) ?? 0
  1098. var password: String = ""
  1099. self.toolbarController.toolbarType = .None
  1100. // if digitalSignController == nil {
  1101. password = listView.document?.password ?? ""
  1102. digitalSignController = KMPDFDigitalSignViewController()
  1103. // }
  1104. digitalSignController?.currentPageIndex = Int(currentPageIndex)
  1105. digitalSignController?.url = listView.document?.documentURL
  1106. digitalSignController?.password = password
  1107. digitalSignController?.scaleFactor = listView.scaleFactor
  1108. digitalSignController?.titleChangeBlock = { title, index in
  1109. currentPageIndex = UInt(index)
  1110. }
  1111. digitalSignController?.buttonActionBlock = { [weak self] type, isChanged in
  1112. if type == .cancel {
  1113. if let page = self?.listView?.document?.page(at: currentPageIndex) {
  1114. self?.listView.go(to: page)
  1115. }
  1116. self?.exitDigitalSign()
  1117. }
  1118. }
  1119. if let digitalSignView = digitalSignController?.view, let splitViewSuperview = mianSplitView.superview {
  1120. digitalSignView.frame = splitViewSuperview.bounds
  1121. digitalSignView.autoresizingMask = [.width, .height]
  1122. splitViewSuperview.addSubview(digitalSignView)
  1123. digitalSignController?.setCurrentPageIndex(Int(currentPageIndex))
  1124. }
  1125. }
  1126. // MARK: - Toolbar
  1127. func toolbarItemClickForExitMode(_ toolbarItem: KMToolbarItemView) {
  1128. if(toolbarItem.itemIdentifier != KMDocumentPageToolbarItemIdentifier) {
  1129. if (hasEnterPageEdit()) {
  1130. self.exitPageEdit()
  1131. }
  1132. }
  1133. if toolbarItem.itemIdentifier != KMDocumentRedactToolbarItemIdentifier {
  1134. if self.hasEnterRedact() {
  1135. self.exitRedact()
  1136. }
  1137. }
  1138. if toolbarItem.itemIdentifier != KMDocumentDigitalSignToolbarItemIdentifier {
  1139. if self.hasShowDigitalSign() {
  1140. self.exitDigitalSign()
  1141. }
  1142. }
  1143. if toolbarItem.itemIdentifier != KMDocumentEditToolbarItemIdentifier && toolbarItem.itemIdentifier != KMRightControlToolbarItemIdentifier && toolbarItem.itemIdentifier != KMLeftControlToolbarItemIdentifier {
  1144. // self.editPDFHanddler.clearData()
  1145. self.commitEditingIfNeed()
  1146. }
  1147. }
  1148. // MARK: - Private Methods
  1149. private func getPDFEditController() -> KMPDFEditViewController? {
  1150. var editController: KMPDFEditViewController?
  1151. for controller in self.children {
  1152. if (controller.isKind(of: KMPDFEditViewController.self)) {
  1153. editController = (controller as! KMPDFEditViewController)
  1154. break
  1155. }
  1156. }
  1157. return editController
  1158. }
  1159. private func _getPDFRedactController() -> KMPDFRedactViewController? {
  1160. var controller: KMPDFRedactViewController?
  1161. for childC in self.children {
  1162. if (childC.isKind(of: KMPDFRedactViewController.self)) {
  1163. controller = (childC as! KMPDFRedactViewController)
  1164. break
  1165. }
  1166. }
  1167. return controller
  1168. }
  1169. private func addBackgroundMaskView() {
  1170. self.removeBackgroundMaskView()
  1171. if let superview = self.mianSplitView.superview {
  1172. let view = NSView()
  1173. superview.addSubview(view)
  1174. view.frame = superview.bounds
  1175. view.autoresizingMask = [.width, .height]
  1176. view.wantsLayer = true
  1177. view.layer?.backgroundColor = .white
  1178. self.background_mask = view
  1179. }
  1180. }
  1181. private func removeBackgroundMaskView() {
  1182. self.background_mask?.removeFromSuperview()
  1183. self.background_mask = nil
  1184. }
  1185. private func _goToFirstPageForFristAppear() {
  1186. DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.1) {
  1187. self.listView.go(toPageIndex: 0, animated: false)
  1188. }
  1189. }
  1190. func isPDFPageCountExceedsLimit(filePath: String) -> Bool {
  1191. let url = URL(fileURLWithPath: filePath)
  1192. guard let document = PDFDocument(url: url) else {
  1193. return false
  1194. }
  1195. let pageCount = document.pageCount
  1196. return pageCount > 30
  1197. }
  1198. // MARK: - Redact 【标记密文】
  1199. func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
  1200. let windowController = KMRedactConfirmWindowController(type)
  1201. self.currentWindowController = windowController
  1202. self.view.window?.beginSheet(windowController.window!)
  1203. windowController.itemClick = { [weak self] index in
  1204. if (index == 2) { /// 取消
  1205. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1206. self?.currentWindowController = nil
  1207. callback()
  1208. return
  1209. }
  1210. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1211. self?.currentWindowController = nil
  1212. let panel = NSSavePanel()
  1213. panel.nameFieldStringValue = "[新文件]"+((self?.listView.document?.documentURL.lastPathComponent) ?? "")
  1214. let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  1215. button.state = .on
  1216. panel.accessoryView = button
  1217. panel.isExtensionHidden = true
  1218. panel.beginSheetModal(for: (self?.view.window!)!) { response in
  1219. if response != .OK {
  1220. callback()
  1221. return
  1222. }
  1223. if (type == .redactOne) {
  1224. let anno = self!.listView.activeAnnotation
  1225. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  1226. callback()
  1227. return
  1228. }
  1229. (anno as! CPDFRedactAnnotation).applyRedaction()
  1230. } else if (type == .redactAll) {
  1231. self?.listView.document?.applyRedactions()
  1232. } else if (type == .eraserOne) {
  1233. let anno = self!.listView?.activeAnnotation
  1234. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  1235. callback()
  1236. return
  1237. }
  1238. anno?.page.erasureRedact(from: anno!.bounds)
  1239. } else if (type == .eraserAll) {
  1240. KMRedactTools.eraserDocument((self?.listView.document)!) { result, errorAnno in
  1241. if (result == false) {
  1242. callback()
  1243. return
  1244. }
  1245. }
  1246. }
  1247. self!.listView.document?.write(to: panel.url)
  1248. if (button.state == .on) {
  1249. NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
  1250. }
  1251. } else {
  1252. NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
  1253. }
  1254. callback()
  1255. }
  1256. }
  1257. }
  1258. // MARK: - Secure 【安全】
  1259. public func showSecureLimitTip() {
  1260. self.hiddenSecureLimitTip()
  1261. if self.secureAlertView == nil {
  1262. self.secureAlertView = KMSecureAlertView()
  1263. self.secureAlertView?.show(in: self.listView)
  1264. self.secureAlertView?.closeAction = { [unowned self] view in
  1265. self.hiddenSecureLimitTip()
  1266. self.removeFromAlertView()
  1267. self.showFormAlertView()
  1268. }
  1269. self.secureAlertView?.passwordAction = { [unowned self] view in
  1270. self.removeOwnerPassword()
  1271. self.removeFromAlertView()
  1272. self.showFormAlertView()
  1273. }
  1274. }
  1275. }
  1276. func removeOwnerPassword() {
  1277. guard let doc = self.listView?.document else {
  1278. NSSound.beep()
  1279. return
  1280. }
  1281. if doc.allowsCopying && doc.allowsPrinting {
  1282. NSSound.beep()
  1283. return
  1284. }
  1285. _ = KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: doc.documentURL) { [weak self] result, password in
  1286. if result == .cancel { /// 关闭
  1287. return
  1288. }
  1289. /// 解密成功
  1290. self?.hiddenSecureLimitTip()
  1291. self?.model.isSaveKeyChain = false
  1292. self?.listView.document?.unlock(withPassword: password)
  1293. }
  1294. }
  1295. public func hiddenSecureLimitTip() {
  1296. self.secureAlertView?.removeFromSuperview()
  1297. self.secureAlertView = nil
  1298. }
  1299. //MARK: - Form
  1300. func showFormAlertView() {
  1301. if (formAlertView == nil) {
  1302. formAlertView = KMFormAlertView()
  1303. formAlertView?.isCloseSecureView = self.secureAlertView != nil ? false : true
  1304. formAlertView?.showInView(self.listView)
  1305. } else {
  1306. self.removeFromAlertView()
  1307. }
  1308. }
  1309. func removeFromAlertView() {
  1310. formAlertView?.removeFromSuperview()
  1311. formAlertView = nil
  1312. }
  1313. override func mouseMoved(with event: NSEvent) {
  1314. self.view.window?.mouseMoved(with: event)
  1315. }
  1316. func savePageNumberIfNeed() {
  1317. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  1318. let scaleFactor = self.listView?.scaleFactor ?? 0
  1319. if scaleFactor <= 0 {
  1320. return
  1321. }
  1322. if self.listView.document != nil {
  1323. KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
  1324. KMPreferenceManager.shared.setPageScale(Float(self.listView.scaleFactor), forKey: self.listView.document.documentURL.path)
  1325. }
  1326. }
  1327. }
  1328. // MARK: - 显示合并窗口
  1329. public func showMergeWindow(url: URL? = nil, _ password: String?) {
  1330. DispatchQueue.main.async {
  1331. var documentURL = url
  1332. if documentURL == nil {
  1333. documentURL = self.listView.document?.documentURL
  1334. }
  1335. guard let _url = documentURL else { return }
  1336. guard let document = PDFDocument(url: _url) else { return }
  1337. self.mergeWindowController = KMMergeWindowController(document: document, password: password ?? "")
  1338. self.mergeWindowController!.oriDucumentUrl = self.listView.document?.documentURL
  1339. self.mergeWindowController!.pageIndex = self.listView.currentPageIndex
  1340. self.mergeWindowController!.cancelAction = { [unowned self] controller in
  1341. self.view.window?.endSheet(mergeWindowController!.window!)
  1342. }
  1343. self.mergeWindowController!.mergeAction = { [unowned self] controller, filePath in
  1344. self.view.window?.endSheet(mergeWindowController!.window!)
  1345. }
  1346. self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
  1347. self.view.window?.beginSheet(self.mergeWindowController!.window!)
  1348. }
  1349. }
  1350. // MARK: - 显示加密弹窗
  1351. public func showSecureWindow(_ url: URL) {
  1352. self.securityWindowController = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
  1353. guard let securityWindowController = securityWindowController else { return }
  1354. securityWindowController.documentURL = self.listView.document?.documentURL
  1355. securityWindowController.batchAction = { [unowned self] controller, files in
  1356. self.view.window?.endSheet((securityWindowController.window)!)
  1357. self.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
  1358. let batchWindowController = KMBatchOperateWindowController.sharedWindowController
  1359. let batchOperateFile = KMBatchOperateFile(filePath: self.document?.documentURL.path ?? "", type: .AddPassword)
  1360. batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
  1361. batchWindowController.window?.makeKeyAndOrderFront("")
  1362. }
  1363. securityWindowController.doneAction = { [unowned self] controller, options, attribute in
  1364. let openPanel = NSOpenPanel()
  1365. openPanel.canChooseFiles = false
  1366. openPanel.canChooseDirectories = true
  1367. openPanel.canCreateDirectories = true
  1368. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { (result) in
  1369. if result == NSApplication.ModalResponse.OK {
  1370. for fileURL in openPanel.urls {
  1371. let document = CPDFDocument(url: self.document?.documentURL)
  1372. if document != nil {
  1373. document!.setDocumentAttributes(attribute)
  1374. let path = fileURL.path.stringByAppendingPathComponent(url.deletingPathExtension().lastPathComponent) + "_SetPassword" + "." + url.pathExtension
  1375. let success = document!.write(to: NSURL(fileURLWithPath: path) as URL, withOptions: options)
  1376. if success {
  1377. self.view.window?.endSheet((securityWindowController.window)!)
  1378. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  1379. }
  1380. }
  1381. }
  1382. }
  1383. }
  1384. }
  1385. securityWindowController.cancelAction = { [unowned self] controller in
  1386. self.view.window?.endSheet((securityWindowController.window)!)
  1387. }
  1388. NSWindow.currentWindow().beginSheet(securityWindowController.window!)
  1389. }
  1390. // MARK: - 保存文档
  1391. internal func needSaveDocument() -> Bool {
  1392. if (self.isPDFDocumentEdited) {
  1393. return self.isPDFDocumentEdited
  1394. }
  1395. if (self.needSave) {
  1396. return self.needSave
  1397. }
  1398. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  1399. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  1400. return false
  1401. }
  1402. return true
  1403. }
  1404. internal func saveDocument(overlookDocumentIfEdited overlook: Bool = false) {
  1405. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  1406. if (overlook) {
  1407. document?.save(nil)
  1408. return
  1409. }
  1410. if (self.isPDFDocumentEdited) {
  1411. self.clearIsPDFDocumentEdited()
  1412. self.needSave = false
  1413. document?.save(nil)
  1414. return
  1415. }
  1416. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  1417. return
  1418. }
  1419. document?.save(nil)
  1420. }
  1421. internal func asyncSaveDocument(overlookDocumentIfEdited overlook: Bool = false, callback:@escaping KMCommonBlock) {
  1422. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  1423. if (overlook) {
  1424. DispatchQueue.main.async {
  1425. document?.save(nil)
  1426. callback()
  1427. }
  1428. return
  1429. }
  1430. if (self.isPDFDocumentEdited) {
  1431. self.clearIsPDFDocumentEdited()
  1432. self.needSave = false
  1433. DispatchQueue.main.async {
  1434. document?.save(nil)
  1435. callback()
  1436. }
  1437. return
  1438. }
  1439. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  1440. callback()
  1441. return
  1442. }
  1443. DispatchQueue.main.async {
  1444. document?.save(nil)
  1445. callback()
  1446. }
  1447. }
  1448. internal func saveDocumentWithProgressAlert(callback:@escaping KMCommonBlock) {
  1449. // 显示进度
  1450. AutoSaveManager.manager.isSaving = true
  1451. self.showProgressWindow(message: NSLocalizedString("Save", comment: "") + "PDF")
  1452. self.progressC?.maxValue = 3.0
  1453. self.progressC?.increment(by: 1.0)
  1454. // 保存文档
  1455. self.asyncSaveDocument { [weak self] params in
  1456. // 执行进度 [假进度]
  1457. self?.progressC?.increment(by: 1.0)
  1458. self?.progressC?.increment(by: 1.0)
  1459. // DispatchQueue.main.async {
  1460. DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
  1461. // 隐藏进度
  1462. self?.hiddenProgressWindow()
  1463. DispatchQueue.main.asyncAfter(deadline: .now()+1) {
  1464. AutoSaveManager.manager.isSaving = false
  1465. }
  1466. // 回调
  1467. callback()
  1468. }
  1469. }
  1470. }
  1471. func SaveTempPDFDocumentToURLPath(tempPath: String) {
  1472. self.document?.write(toFile: tempPath)
  1473. }
  1474. // MARK: - 定时保存
  1475. func addAutoSaveEvent() {
  1476. if (self.autoSaveTimer != nil) {
  1477. self.autoSaveTimer?.invalidate()
  1478. self.autoSaveTimer = nil
  1479. }
  1480. if self.document != nil {
  1481. self.autoSaveTimer = Timer.scheduledTimer(withTimeInterval: AutoSaveManager.manager.timeInterval * 60, repeats: true, block: { [weak self] timer in
  1482. self?.autoSaveTimerAction(timer)
  1483. })
  1484. }
  1485. self.checkAutoSaveInfo()
  1486. }
  1487. func checkAutoSaveInfo() {
  1488. guard let cnt = AutoSaveManager.manager.autoSavePaths?.count, cnt > 0 else {
  1489. return
  1490. }
  1491. if AutoSaveManager.manager.autoSaveAlertShow {
  1492. return
  1493. }
  1494. AutoSaveManager.manager.autoSaveDidEndAction = false
  1495. AutoSaveManager.manager.autoSaveAlertShow = true
  1496. let blockSaveWindow = AutoSavePopController()
  1497. blockSaveWindow.cancelHandle = { [weak self] windowController in
  1498. AutoSaveManager.manager.autoSaveDidEndAction = true
  1499. AutoSaveManager.manager.clearCache()
  1500. self?.km_quick_endSheet()
  1501. }
  1502. blockSaveWindow.confirmHandle = { [weak self] windowController in
  1503. self?.km_quick_endSheet()
  1504. self?.saveAutoSaveInfo()
  1505. }
  1506. self.km_beginSheet(windowC: blockSaveWindow)
  1507. }
  1508. func saveAutoSaveInfo() {
  1509. let openPanel = NSOpenPanel()
  1510. openPanel.canChooseDirectories = true
  1511. openPanel.canChooseFiles = false
  1512. openPanel.allowsMultipleSelection = false
  1513. let win = NSApp.keyWindow != nil ? NSApp.keyWindow : self.view.window
  1514. openPanel.beginSheetModal(for: win!) { result in
  1515. if (result == .OK) {
  1516. let folderPath = openPanel.url?.path ?? openPanel.url?.absoluteString
  1517. for path in AutoSaveManager.manager.opendPaths ?? [] {
  1518. let _path = path as? String
  1519. var newPath = "\(folderPath ?? "")/\(_path?.lastPathComponent ?? "")"
  1520. newPath = self.getValidFilePath(newPath)
  1521. do {
  1522. try FileManager.default.moveItem(atPath: _path ?? "", toPath: newPath)
  1523. } catch {
  1524. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: newPath)])
  1525. }
  1526. }
  1527. AutoSaveManager.manager.clearCache()
  1528. }
  1529. AutoSaveManager.manager.autoSaveDidEndAction = true
  1530. }
  1531. }
  1532. func autoSaveTimerAction(_ timer: Timer) {
  1533. if (self.document == nil || self.listView?.document?.documentURL.path == nil) {
  1534. return
  1535. }
  1536. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  1537. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  1538. return
  1539. }
  1540. if let data = self.document?.isLocked, data {
  1541. return
  1542. }
  1543. if AutoSaveManager.manager.autoSaveEnabled == false {
  1544. return
  1545. }
  1546. let documentArray = NSDocumentController.shared.documents
  1547. var didFileEdit = false
  1548. for doc in documentArray {
  1549. if doc.fileURL?.path == self.document?.documentURL.path {
  1550. didFileEdit = doc.isDocumentEdited
  1551. break
  1552. }
  1553. }
  1554. if (didFileEdit == false) {
  1555. return
  1556. }
  1557. AutoSaveManager.manager.isSaving = true
  1558. let savePath = AutoSaveManager.manager.autoSaveWithPath(self.listView?.document?.documentURL.path ?? "")
  1559. if (!self.document!.isLocked) {
  1560. self.document?.write(to: URL(fileURLWithPath: savePath))
  1561. }
  1562. DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
  1563. AutoSaveManager.manager.isSaving = false
  1564. }
  1565. }
  1566. func removeAutoSaveInfo() {
  1567. if self.autoSaveTimer != nil {
  1568. self.autoSaveTimer?.invalidate()
  1569. self.autoSaveTimer = nil
  1570. }
  1571. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  1572. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  1573. return
  1574. }
  1575. if AutoSaveManager.manager.autoSaveEnabled == false {
  1576. return
  1577. }
  1578. if self.document == nil || self.listView?.document?.documentURL.path == nil {
  1579. return
  1580. }
  1581. AutoSaveManager.manager.removeAutoSavePath(self.listView?.document?.documentURL.path ?? "")
  1582. }
  1583. // MARK: - 选择 PDFDisplay 模式
  1584. @objc public func selectDisplay(display: KMPDFDisplayType, viewSettingIsReload: Bool = true) {
  1585. let toolModel = self.listView.toolMode
  1586. self.isReadMode = false
  1587. switch display {
  1588. case .singlePage:
  1589. self.listView.setDisplay(.singlePage)
  1590. break
  1591. case .singlePageContinuous:
  1592. self.listView.setDisplay(.singlePageContinuous)
  1593. break
  1594. case .twoUp:
  1595. self.listView.setDisplay(.twoUp)
  1596. break
  1597. case .twoUpContinuous:
  1598. self.listView.setDisplay(.twoUpContinuous)
  1599. break
  1600. case .bookMode:
  1601. self.listView.displaysAsBook = true
  1602. self.listView.displayTwoUp = true
  1603. self.listView.displayDirection = .horizontal
  1604. break
  1605. case .bookContinuous:
  1606. self.listView.displaysAsBook = true
  1607. self.listView.displayTwoUp = true
  1608. self.listView.displayDirection = .vertical
  1609. break
  1610. case .readModel:
  1611. self.openReadModel()
  1612. break
  1613. case .readContinuous:
  1614. self.openReadModel()
  1615. break
  1616. }
  1617. self.listView.layoutDocumentView()
  1618. // if (viewSettingIsReload && self.leftSideViewController.panelSetViewController.isViewLoaded) {
  1619. // self.leftSideViewController.panelSetViewController.reloadListViewModel()
  1620. // }
  1621. if (toolModel == .editPDFToolMode) {
  1622. if self.rightSideViewController.eidtPDFImageProperty != nil {
  1623. self.rightSideViewController.eidtPDFImageProperty?.cancelCutImageAction("")
  1624. self.rightSideViewController.isHidden = true
  1625. self.closeRightPane()
  1626. }
  1627. }
  1628. }
  1629. // MARK: - 选择缩放模式
  1630. @objc public func selectZoom(_ type: KMPDFZoomType) {
  1631. switch type {
  1632. case .width:
  1633. self.listView.autoScales = true
  1634. // self.listView.autoScales = false
  1635. break
  1636. case .fit:
  1637. // self.listView.autoScales = !self.listView.autoScales
  1638. if let pageHeight = self.listView.currentPage()?.size.height, pageHeight > 0 {
  1639. let pdfviewHeight = self.listView.bounds.size.height
  1640. self.listView.scaleFactor = pdfviewHeight/pageHeight
  1641. self.listView.autoScales = false
  1642. }
  1643. break
  1644. case .actualSize:
  1645. if self.listView.scaleFactor != 1.0 {
  1646. self.listView.scaleFactor = 1.0
  1647. self.listView.autoScales = false
  1648. }
  1649. break
  1650. }
  1651. }
  1652. internal func createPdf(index:Int) {
  1653. if index == 1 {
  1654. self.homeVC?.openBlankPage()
  1655. } else if index == 4 {
  1656. self.homeVC?.importFromCamera()
  1657. } else if index == 5 {
  1658. self.homeVC?.importFromScanner()
  1659. } else if index == 3 {
  1660. self.homeVC?.importFromWebPage()
  1661. } else if index == 2 {
  1662. self.homeVC?.newFromImages()
  1663. }
  1664. }
  1665. // MARK - Event 监听
  1666. private func addEventMonitor() {
  1667. if (self.eventMonitor != nil) {
  1668. self.removeEventMonitor()
  1669. }
  1670. // KMPrint("已添加事件监听")
  1671. self.eventMonitor = NSEvent.addLocalMonitorForEvents(matching: [.scrollWheel, .leftMouseDown]) { [weak self] event in
  1672. if (event.type == .scrollWheel && event.modifierFlags.contains(.option)) { // Alt + 鼠标滚轮
  1673. self?.listView.magnifyWheel(event)
  1674. return nil
  1675. } else if event.type == .leftMouseDown {
  1676. let point = event.locationInView(self?.listView ?? NSView())
  1677. let presentationDrawView = self?.listView.presentationDrawView
  1678. if let data = self?.interactionMode, data == .presentation,CGRectContainsPoint(self?.listView.frame ?? .zero, point),presentationDrawView?.isHidden == true { // 幻灯片模式下
  1679. if point.x >= (self?.listView.frame.maxX ?? 0) / 2 {
  1680. let can = self?.listView?.canGoToNextPage() ?? false
  1681. if can {
  1682. self?.listView.goToNextPage(nil)
  1683. }
  1684. } else {
  1685. let can = self?.listView?.canGoToPreviousPage() ?? false
  1686. if can {
  1687. self?.listView.goToPreviousPage(nil)
  1688. }
  1689. }
  1690. return nil
  1691. }
  1692. }
  1693. return event
  1694. }
  1695. }
  1696. func addKeyEventMonitor() {
  1697. if (self.keyEventMonitor != nil) {
  1698. self.removeKeyEventMonitor()
  1699. }
  1700. keyEventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
  1701. // print(event.keyCode)
  1702. if event.keyCode == 53 {
  1703. if let data = self?.interactionMode, data == .presentation { // 幻灯片模式下
  1704. if let data = self?.canExitPresentation(), data {
  1705. let mainDocument = self?.myDocument as? KMMainDocument
  1706. let browserWindowController = mainDocument?.browser?.windowController as? KMBrowserWindowController
  1707. browserWindowController?.exitFullscreen()
  1708. }
  1709. return nil
  1710. }
  1711. if self?.listView.toolMode == .editPDFToolMode {
  1712. if self != nil {
  1713. //使用editingSelectionString获取内容文字
  1714. if self!.listView.editingAreas() != nil {
  1715. if self!.listView.editingAreas().count > 0 && self!.listView.isEditable() {
  1716. self!.listView.clearEditingSelectCharItem()
  1717. } else if self!.listView.editingAreas().count > 0 {
  1718. if self?.listView.annotationType == .addImage ||
  1719. self?.listView.annotationType == .addText {
  1720. let textItem = self?.toolbarController.findItem(KMToolbarAddTextEditPDFItemIdentifier)
  1721. let imageItem = self?.toolbarController.findItem(KMToolbarAddImageEditPDFItemIdentifier)
  1722. textItem?.isSelected = false
  1723. imageItem?.isSelected = false
  1724. }
  1725. self?.rightSideViewController.isHidden = true
  1726. self?.listView.endEditIsRemoveBlock(with: self!.listView.editingAreas().first as? CPDFEditArea)
  1727. self?.listView.updateEditing([])
  1728. self?.listView.isEditImage = false
  1729. self?.listView.setNeedsDisplayPageViewFor(self!.listView.currentPage())
  1730. if self?.listView.annotationType == .addImage {
  1731. self?.listView.change([.text, .image])
  1732. }
  1733. self?.listView.annotationType = .editTextImage
  1734. self?.closeRightPane()
  1735. } else if(self?.listView.annotationType == .addImage || self!.listView.annotationType == .addText) {
  1736. if self?.listView.annotationType == .addImage ||
  1737. self?.listView.annotationType == .addText {
  1738. let textItem = self?.toolbarController.findItem(KMToolbarAddTextEditPDFItemIdentifier)
  1739. let imageItem = self?.toolbarController.findItem(KMToolbarAddImageEditPDFItemIdentifier)
  1740. textItem?.isSelected = false
  1741. imageItem?.isSelected = false
  1742. }
  1743. self?.rightSideViewController.isHidden = true
  1744. self?.listView.setShouAddEdit([])
  1745. self?.listView.change([.text, .image])
  1746. self?.listView.annotationType = .editTextImage
  1747. self?.closeRightPane()
  1748. }
  1749. } else {
  1750. if self?.listView.annotationType == .addImage ||
  1751. self?.listView.annotationType == .addText {
  1752. let textItem = self?.toolbarController.findItem(KMToolbarAddTextEditPDFItemIdentifier)
  1753. let imageItem = self?.toolbarController.findItem(KMToolbarAddImageEditPDFItemIdentifier)
  1754. textItem?.isSelected = false
  1755. imageItem?.isSelected = false
  1756. }
  1757. }
  1758. }
  1759. }
  1760. } else {
  1761. if let data = self?.interactionMode, data == .presentation { // 幻灯片模式下
  1762. self?.listView.keyDown(with: event)
  1763. return nil
  1764. } else {
  1765. let cmd = event.modifierFlags.contains(.command)
  1766. let shift = event.modifierFlags.contains(.shift)
  1767. if event.keyCode == 6 { // z
  1768. let editPDFIng = self?.editPDFHanddler.isEditing ?? false
  1769. if cmd && shift { // 恢复
  1770. let can = self?.editPDFHanddler.listView?.canEditTextRedo() ?? false
  1771. if can == false {
  1772. return event
  1773. }
  1774. if editPDFIng {
  1775. _ = CustomAlertView.alertView(message: NSLocalizedString("Redo", comment: ""), fromView: self!.view, withStyle: .black)
  1776. // if let data = self?.listView.canEditTextRedo(), data {
  1777. // self?.listView.editTextRedo()
  1778. // }
  1779. }
  1780. } else if cmd { // 撤回
  1781. let can = self?.editPDFHanddler.listView?.canEditTextUndo() ?? false
  1782. if can == false {
  1783. return event
  1784. }
  1785. if editPDFIng {
  1786. _ = CustomAlertView.alertView(message: NSLocalizedString("Undo", comment: ""), fromView: self!.view, withStyle: .black)
  1787. // if let data = self?.listView.canEditTextUndo(), data {
  1788. // self?.listView.editTextUndo()
  1789. // }
  1790. }
  1791. }
  1792. }
  1793. }
  1794. }
  1795. return event
  1796. }
  1797. }
  1798. func removeKeyEventMonitor() {
  1799. if (self.keyEventMonitor != nil) {
  1800. KMPrint("removeKeyEventMonitor 已移除事件监听")
  1801. NSEvent.removeMonitor(self.keyEventMonitor as Any)
  1802. self.keyEventMonitor = nil
  1803. }
  1804. }
  1805. private func removeEventMonitor() {
  1806. if (self.eventMonitor != nil) {
  1807. KMPrint("已移除事件监听")
  1808. NSEvent.removeMonitor(self.eventMonitor as Any)
  1809. self.eventMonitor = nil
  1810. }
  1811. }
  1812. // MARK: - Tools
  1813. func pdfViewCanHorizontalScroll() -> Bool {
  1814. let scroll = self.listView.scroll()
  1815. if (scroll == nil) {
  1816. return false
  1817. }
  1818. return scroll?.horizontalScroller?.isHidden == nil ? false : !(scroll!.horizontalScroller!.isHidden)
  1819. }
  1820. func pdfViewCanVerticalScroll() -> Bool {
  1821. let scroll = self.listView.scroll()
  1822. if (scroll == nil) {
  1823. return false
  1824. }
  1825. return scroll?.verticalScroller?.isHidden == nil ? false : !(scroll!.verticalScroller!.isHidden)
  1826. }
  1827. // MARK: - Public Methods
  1828. // 清理数据 [eg. 通知]
  1829. public func clearData() {
  1830. KMThumbnailCache.shared.clearCache()
  1831. self.removeNotifications()
  1832. if (self.listView.spellingTag() > 0) {
  1833. NSSpellChecker.shared.closeSpellDocument(withTag: self.listView.spellingTag())
  1834. }
  1835. self.removeAutoSaveInfo()
  1836. KMAdsManager.defaultManager.dismissSheetModal(for: self.readContentView)
  1837. self.myDocument = nil
  1838. }
  1839. public func clearSecureOptions() {
  1840. self._secureOptions = nil
  1841. self.documentAttribute = nil
  1842. }
  1843. public func recordRemoveSecureFlag() {
  1844. self._removeSecureFlag = true
  1845. self.clearSecureOptions()
  1846. self.recordIsPDFDocumentEdited(type: .removePassword)
  1847. self._needSave = true
  1848. }
  1849. public func clearRemoveSecureFlag() {
  1850. self._removeSecureFlag = false
  1851. }
  1852. public func clearSaveWatermarkFlag() {
  1853. km_synchronized(self) {
  1854. self._saveWatermarkFlag = false
  1855. }
  1856. }
  1857. public func recordIsPDFDocumentEdited(type: KMSubscribeWaterMarkType = .none) {
  1858. km_synchronized(self) {
  1859. self.model.isPDFDocumentEdited = true
  1860. if type == .editText || type == .editImage {
  1861. self.leftSideViewController.updateThumbnail(at: self.listView.currentPageIndex)
  1862. }
  1863. if let _document = self.myDocument {
  1864. KMTools.setDocumentEditedState(document: _document)
  1865. }
  1866. }
  1867. }
  1868. public func clearIsPDFDocumentEdited() {
  1869. km_synchronized(self) {
  1870. self.model.isPDFDocumentEdited = false
  1871. }
  1872. }
  1873. func showSnapshots(setups: NSArray?) {
  1874. if self.listView.document != nil {
  1875. for setup in setups ?? [] {
  1876. let swc = KMSnapshotWindowController()
  1877. swc.delegate = self
  1878. swc.setPdfDocument(self.listView.document, setup: setup as? NSDictionary)
  1879. swc.setForceOnTop(self.interactionMode != .normal)
  1880. self.myDocument?.addWindowController(swc)
  1881. }
  1882. }
  1883. }
  1884. func dealDocumentDidLoaded() {
  1885. self.removeBackgroundMaskView()
  1886. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  1887. self.showSecureLimitTip()
  1888. }
  1889. // let readModel = UserDefaults.standard.bool(forKey: "kKMPDFViewIsReadMode")
  1890. // if readModel == true {
  1891. // self.openReadModel()
  1892. // }
  1893. if self.model.needConvertNotes {
  1894. self.showConvertNotesProgress()
  1895. }
  1896. if (self._documentFirstLoad) {
  1897. self.checkShouldAutoOpenLeftVC()
  1898. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  1899. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document?.documentURL.path ?? "")
  1900. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document?.documentURL.path ?? "")
  1901. if (pageScale != nil) {
  1902. self.listView.scaleFactor = CGFloat(pageScale!)
  1903. }
  1904. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < (self.listView.document?.pageCount ?? 0)) {
  1905. self.listView.go(toPageIndex: pageNumber!, animated: false)
  1906. } else {
  1907. self._goToFirstPageForFristAppear()
  1908. }
  1909. } else {
  1910. self._goToFirstPageForFristAppear()
  1911. }
  1912. self._documentFirstLoad = false
  1913. }
  1914. }
  1915. func tabViewIsDragging() -> Bool {
  1916. let level = self.view.window?.level ?? .normal
  1917. return level == .floating
  1918. }
  1919. // MARK: - Noti Actions
  1920. internal func documentDidUnlockNotification(_ sender: Notification) {
  1921. if (self.listView.document != nil && self.listView.document.isEqual(to: sender.object)) {
  1922. if (self.myDocument == nil) {
  1923. return
  1924. }
  1925. if (self.listView.document.allowsPrinting && self.listView.document.allowsCopying) {
  1926. self.hiddenSecureLimitTip()
  1927. }
  1928. let isUnlockFromKeychain = (self.myDocument as? KMMainDocument)?.isUnlockFromKeychain ?? false
  1929. if (isUnlockFromKeychain || self.model.isSaveKeyChain == false) {
  1930. return
  1931. }
  1932. let type = KMPreferenceManager.shared.savePasswordType
  1933. if (type == .never) {
  1934. return
  1935. }
  1936. if (type == .always) {
  1937. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  1938. return
  1939. }
  1940. // 保存到钥匙串
  1941. let alert = NSAlert()
  1942. alert.messageText = NSLocalizedString("Remember Password?", comment: "")
  1943. alert.informativeText = NSLocalizedString("Do you want to save this password in your Keychain?", comment: "")
  1944. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  1945. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  1946. if (alert.runModal() == .alertFirstButtonReturn) { // 保存密码
  1947. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  1948. return
  1949. }
  1950. }
  1951. }
  1952. func annotationsAttributeHasChange(_ sender: Notification) {
  1953. guard let dict = sender.object as? [String : Any] else {
  1954. return
  1955. }
  1956. if let anno = dict["object"] as? CPDFAnnotation {
  1957. let value = dict["keyPath"] as? String ?? ""
  1958. let didEnd = dict["didEnd"] as? Bool ?? false
  1959. if didEnd {
  1960. if value == CPDFAnnotationBoundsKey {
  1961. if anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation {
  1962. anno.contents = anno.page?.string(for: anno.bounds) ?? ""
  1963. }
  1964. }
  1965. if anno.km_isMeasure() && anno.contents == nil {
  1966. anno.contents = anno.string() ?? ""
  1967. }
  1968. self.leftSideViewController.refreshUIForAnnoAttributeDidChange(anno, attributes: ["keyPath" : value])
  1969. } else {
  1970. if value != CPDFAnnotationBoundsKey && value != CPDFAnnotationStartPointKey && value != CPDFAnnotationEndPointKey && value != CPDFAnnotationPathsKey { // 改变bounds(箭头、直线注释 开始点和结束点, 手绘注释的paths)的操作会卡顿,比如移动
  1971. self.leftSideViewController.refreshUIForAnnoAttributeDidChange(anno, attributes: ["keyPath" : value])
  1972. }
  1973. }
  1974. }
  1975. }
  1976. internal func applicationWillTerminateNotification(_ sender: Notification) {
  1977. self.savePageNumberIfNeed()
  1978. self.saveDocument()
  1979. }
  1980. func KMPDFViewCurrentPageDidChangedNotification(_ sender: Notification) {
  1981. if self.isReadMode {
  1982. self.readModelView.currentPageIndex = self.listView.currentPageIndex
  1983. }
  1984. //刷新前一页后一页按钮
  1985. self.updateNextAndPreViousButtonState()
  1986. }
  1987. func CPDFDocumentPageCountChangedNotification(_ sender: Notification) {
  1988. if self.isReadMode {
  1989. self.readModelView.totalPagesCount = Int(self.listView.document?.pageCount ?? 0)
  1990. }
  1991. //刷新前一页后一页按钮
  1992. self.updateNextAndPreViousButtonState()
  1993. self.leftSideViewController.refreshUIForAnnoAttributeDidChange(nil, attributes: nil)
  1994. }
  1995. func CEditPDFToolModeChangeStateUnkownNotification(_ sender: Notification) {
  1996. var editSelectd = false
  1997. if (self.listView.annotationType == .addText || self.listView.annotationType == .addImage) && self.listView.toolMode == .editPDFToolMode {
  1998. editSelectd = true
  1999. }
  2000. if self.listView.toolMode == .editPDFToolMode {
  2001. if editSelectd {
  2002. self.toolbarController.cancelSelected(KMToolbarAddTextEditPDFItemIdentifier)
  2003. }
  2004. }
  2005. }
  2006. @objc func handlePageChangedNotification(_ sender: Notification) {
  2007. // When the PDFView is changing scale, or when view settings change when switching fullscreen modes,
  2008. // a lot of wrong page change notifications may be send, which we better ignore.
  2009. // Full screen switching and zooming should not change the current page anyway.
  2010. if self.mwcFlags.isSwitchingFullScreen > 0 {
  2011. // if ([pdfView isZooming] || mwcFlags.isSwitchingFullScreen) {
  2012. // [self updatePageNumber];
  2013. // [self updateLeftStatus];
  2014. return
  2015. }
  2016. //
  2017. let page = self.listView.currentPage()
  2018. let pageIndex = page?.pageIndex() ?? 0
  2019. //
  2020. // if ([lastViewedPages count] == 0) {
  2021. // [lastViewedPages addPointer:(void *)pageIndex];
  2022. // } else if ((NSUInteger)[lastViewedPages pointerAtIndex:0] != pageIndex) {
  2023. // [lastViewedPages insertPointer:(void *)pageIndex atIndex:0];
  2024. // if ([lastViewedPages count] > 5)
  2025. // [lastViewedPages setCount:5];
  2026. // }
  2027. self.leftSideViewController.thumb_selectRowIndexsIfNeed(IndexSet(integer: IndexSet.Element(pageIndex)))
  2028. self.leftSideViewController.thumbnailTableView.needsDisplay = true
  2029. self.leftSideViewController.tocOutlineView.needsDisplay = true
  2030. //
  2031. // [self updatePageNumber];
  2032. // [self updatePageLabel];
  2033. //
  2034. // [self updateOutlineSelection];
  2035. // [self updateNoteSelection];
  2036. // [self updateThumbnailSelection];
  2037. //
  2038. // if (beforeMarkedPageIndex != NSNotFound && [[pdfView currentPage] pageIndex] != markedPageIndex)
  2039. // beforeMarkedPageIndex = NSNotFound;
  2040. //
  2041. // [self synchronizeWindowTitleWithDocumentName];
  2042. // [self updateLeftStatus];
  2043. // if ([[NSUserDefaults standardUserDefaults] boolForKey:SKDisplayPageBoundsKey])
  2044. // [self updateRightStatus];
  2045. // if ([self interactionMode] == SKPresentationMode)
  2046. // [[self presentationNotesDocument] setCurrentPage:[[[self presentationNotesDocument] pdfDocument] pageAtIndex:[page pageIndex]]];
  2047. }
  2048. @objc func handleDisplayBoxChangedNotification(_ sender: Notification) {
  2049. self.leftSideViewController.reloadThumbnailDataIfNeed()
  2050. // if ([[NSUserDefaults standardUserDefaults] boolForKey:SKDisplayPageBoundsKey])
  2051. // [self updateRightStatus];
  2052. }
  2053. @objc func willEnterInteractionModeNotification(_ sender: Notification) {
  2054. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  2055. return
  2056. }
  2057. let interactionMode = sender.userInfo?[NSWindow.UserInfo.interactionModeKey] as? KMInteractionMode
  2058. if interactionMode == .presentation {
  2059. let backgroundColor = NSColor.black
  2060. let level = UserDefaults.standard.bool(forKey: "SKUseNormalLevelForPresentationKey") ? NSWindow.Level.normal : NSWindow.Level.popUpMenu
  2061. let wasInteractionMode = self.interactionMode
  2062. if wasInteractionMode == .normal {
  2063. self.savedNormalSetup.setDictionary(self.currentPDFSettings() as! [AnyHashable : Any])
  2064. }
  2065. // if findController.view().window() != nil {
  2066. // findController.toggleAboveView(nil, animate: false)
  2067. // }
  2068. if wasInteractionMode == .legacyFullScreen {
  2069. self.enterPresentationMode()
  2070. // updatePresentationOptions(for: self.view.window!)
  2071. self.pdfSplitView.frame = CGRect(x: 0, y: 0, width: NSWidth(centerContentView.bounds), height: NSHeight(centerContentView.bounds)-1)
  2072. self.centerContentView.addSubview(pdfSplitView)
  2073. self.listView.frame = (self.view.window?.contentView?.bounds)!
  2074. self.view.window?.contentView?.addSubview(listView)
  2075. self.view.window?.backgroundColor = backgroundColor
  2076. self.view.window?.level = level
  2077. self.listView.layoutDocumentView()
  2078. self.listView.requiresDisplay()
  2079. self.forceSubwindowsOnTop(false)
  2080. self.hideLeftSideWindow()
  2081. self.hideRightSideWindow()
  2082. self.removeBlankingWindows()
  2083. }
  2084. } else {
  2085. KMPrint("2")
  2086. }
  2087. }
  2088. @objc func didEnterInteractionModeNotification(_ sender: Notification) {
  2089. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  2090. return
  2091. }
  2092. if self.interactionMode == .presentation {
  2093. // if self.pdfView().currentPage()?.isEqual(page) == false {
  2094. // self.pdfView().go(to: page)
  2095. // }
  2096. // pdfView().setInteractionMode(SKPresentationMode)
  2097. self.listView?.layoutDocumentView()
  2098. self.listView?.requiresDisplay()
  2099. }
  2100. }
  2101. @objc func willShowFullScreenNotification(_ sender: Notification) {
  2102. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  2103. return
  2104. }
  2105. if self.interactionMode == .presentation {
  2106. let view = self.view.window?.firstResponder as? NSView
  2107. if let data = view?.isDescendant(of: self.pdfSplitView), data {
  2108. self.view.window?.makeFirstResponder(nil)
  2109. }
  2110. }
  2111. }
  2112. @objc func didShowFullScreenNotification(_ sender: Notification) {
  2113. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  2114. return
  2115. }
  2116. if self.interactionMode == .presentation {
  2117. self.enterPresentationMode()
  2118. }
  2119. }
  2120. @objc func didAddContentViewNotification(_ sender: Notification) {
  2121. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  2122. return
  2123. }
  2124. if self.interactionMode == .presentation {
  2125. }
  2126. }
  2127. @objc func purchaseStateUpdateNoti() {
  2128. if IAPProductsManager.default().isAvailableAllFunction() {
  2129. KMAdsManager.defaultManager.dismissSheetModal(for: self.readContentView)
  2130. }
  2131. }
  2132. //!!!: - CPDFFreeTextAnnotation 空注释时会删除,删除时sdk内部删除的,只能接受通知来刷选UI【正常的注释删除会走两遍】
  2133. @objc func didRemoveAnnotationNotification(_ sender: Notification) {
  2134. guard let anno = sender.object as? CPDFAnnotation else {
  2135. return
  2136. }
  2137. if anno.page?.document != self.listView.document {
  2138. return
  2139. }
  2140. self.leftSideViewController.annoList_refreshUIForDeleteAnnotations(annos: [anno], page: anno.page)
  2141. }
  2142. // MARK: Split View
  2143. func changePDFDocument(isChange: Bool, replaceBlock: @escaping (String) -> Void) {
  2144. let openPanel = NSOpenPanel()
  2145. openPanel.allowedFileTypes = ["pdf", "PDF"]
  2146. openPanel.allowsMultipleSelection = false
  2147. guard let mainWindow = NSApp.mainWindow else {
  2148. return
  2149. }
  2150. openPanel.beginSheetModal(for: mainWindow) { [weak self] response in
  2151. if response == NSApplication.ModalResponse.OK {
  2152. guard let url = openPanel.url else {
  2153. return
  2154. }
  2155. if let document = CPDFDocument(url: url) {
  2156. self?.secondaryPdfView?.document = nil
  2157. self?.secondaryPdfView?.document = document
  2158. if isChange {
  2159. self!.openSecondaryPdfView!.view.removeFromSuperview()
  2160. }
  2161. replaceBlock(document.documentURL?.path ?? "")
  2162. } else {
  2163. let alert = NSAlert()
  2164. alert.alertStyle = .critical
  2165. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  2166. alert.runModal()
  2167. }
  2168. }
  2169. }
  2170. }
  2171. }
  2172. extension KMMainViewController {
  2173. func currentSetup() -> [String: Any] {
  2174. var setup: [String: Any] = [:]
  2175. var point = NSZeroPoint
  2176. // var rotated = listView.currentPage().rotation
  2177. if listView == nil {
  2178. return setup
  2179. }
  2180. let pageIndex = listView.currentPageIndexAndPoint(&point, rotated: nil)
  2181. setup[kWindowFrameKey] = NSStringFromRect(mainWindow?.frame ?? NSZeroRect)
  2182. setup[KMMainModel.Key.kLeftSidePaneWidth] = self.model.lastLeftPanWidth
  2183. setup[KMMainModel.Key.kRightSidePaneWidth] = self.model.lastRightPanWidth
  2184. setup[KMMainModel.Key.pageIndex] = pageIndex
  2185. // if rotated != 0 {
  2186. // setup[SCROLLPOINT_KEY] = NSStringFromPoint(point)
  2187. // }
  2188. // if !snapshots.isEmpty {
  2189. // setup[SNAPSHOTS_KEY] = snapshots.map { $0[SKSnapshotCurrentSetupKey] }
  2190. // }
  2191. // if interactionMode == SKNormalMode {
  2192. // setup.merge(currentPDFSettings(), uniquingKeysWith: { $1 })
  2193. // } else {
  2194. // setup.merge(savedNormalSetup, uniquingKeysWith: { $1 })
  2195. // ["HASHORIZONTALSCROLLER_KEY", "HASVERTICALSCROLLER_KEY", "AUTOHIDESSCROLLERS_KEY", "LOCKED_KEY"].forEach { setup.removeValue(forKey: $0) }
  2196. // }
  2197. return setup
  2198. }
  2199. // MARK: - Recommond活动
  2200. func loadRecommondPopWindow() {
  2201. if IAPProductsManager.default().isAvailableAllFunction() {
  2202. return
  2203. }
  2204. if let info = KMAdvertisementManager.manager.info.popWindowContent?.content?.first {
  2205. if recommondPopWindowVC == nil {
  2206. recommondPopWindowVC = KMRecommondPopWindow.default()
  2207. }
  2208. recommondPopWindowVC?.recommondInfo = info
  2209. guard let windowFrame = self.view.window?.frame, let popWindowFrame = recommondPopWindowVC?.window?.frame else {
  2210. return
  2211. }
  2212. let x = windowFrame.minX + (windowFrame.size.width - popWindowFrame.size.width) / 2.0
  2213. let y = windowFrame.minY + (windowFrame.size.height - popWindowFrame.size.height) / 2.0
  2214. recommondPopWindowVC?.window?.setFrame(NSRect(x: x, y: y, width: popWindowFrame.size.width, height: popWindowFrame.size.height), display: true)
  2215. recommondPopWindowVC?.window?.orderFront((Any).self)
  2216. recommondPopWindowVC?.window?.becomeMain()
  2217. UserDefaults.standard.set("Show", forKey: info.version ?? "")
  2218. UserDefaults.standard.synchronize()
  2219. }
  2220. }
  2221. }
  2222. extension KMMainViewController {
  2223. func changeFont(_ sender: NSFontManager) {
  2224. KMPrint("changeFont ...")
  2225. if ((self.listView.activeAnnotation?.isKind(of: CPDFFreeTextAnnotation.self)) != nil) {
  2226. let annotation: CPDFFreeTextAnnotation = self.listView.activeAnnotation as! CPDFFreeTextAnnotation
  2227. var font = NSFont(name: annotation.fontName() ?? "Helvetica", size: (annotation.fontSize()) )
  2228. font = sender.convert(font!)
  2229. annotation.fontSize = font?.pointSize ?? 12
  2230. self.listView.commitEditAnnotationFreeText(annotation)
  2231. self.listView.setNeedsDisplay(annotation)
  2232. // self.listView.setNeedsDisplayAnnotationViewFor(annotation.page)
  2233. }
  2234. }
  2235. }