KMMainViewController.swift 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206
  1. //
  2. // KMMainViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Cocoa
  8. let DISPLAYSPAGEBREAKS_KEY = "displaysPageBreaks"
  9. let DISPLAYSASBOOK_KEY = "displaysAsBook"
  10. let DISPLAYMODE_KEY = "displayMode"
  11. let DISPLAYBOX_KEY = "displayBox"
  12. let HASHORIZONTALSCROLLER_KEY = "hasHorizontalScroller"
  13. let HASVERTICALSCROLLER_KEY = "hasVerticalScroller"
  14. let AUTOHIDESSCROLLERS_KEY = "autoHidesScrollers"
  15. let SCALEFACTOR_KEY = "scaleFactor"
  16. let AUTOSCALES_KEY = "autoScales"
  17. let PAGEINDEX_KEY = "pageIndex"
  18. let SCROLLPOINT_KEY = "scrollPoint"
  19. let LOCKED_KEY = "locked"
  20. @objcMembers class KMMainViewController: NSViewController,CPDFViewDelegate,CPDFListViewDelegate,NSTextFieldDelegate {
  21. @IBOutlet var PDFContendView: NSView!
  22. @IBOutlet var centerContentView: NSView!
  23. @IBOutlet var listView: CPDFListView!
  24. @IBOutlet var secondaryPdfView: KMSecondaryPDFView?
  25. @IBOutlet weak var readContentView: NSView!
  26. @IBOutlet weak var tipCurrentPageBox: KMBox!
  27. @IBOutlet weak var rightView: NSView!
  28. @IBOutlet weak var leftView: NSView!
  29. @IBOutlet weak var mianSplitView: KMSplitView!
  30. @IBOutlet weak var pdfSplitView: KMSplitView!
  31. @IBOutlet weak var newPDFSplitView: KMSplitView!
  32. @IBOutlet weak var pdfContentView: NSView!
  33. @IBOutlet weak var pdfSplitSecondView: NSBox!
  34. @IBOutlet weak var locationPageView: NSView!
  35. @IBOutlet weak var tipLabel: NSTextField!
  36. @IBOutlet weak var toplayoutConstraint: NSLayoutConstraint!
  37. @IBOutlet var childToolbarController: KMToolbarViewController!
  38. @IBOutlet var toolbarController: KMToolbarController!
  39. @IBOutlet weak var toolbarBox: NSBox!
  40. @IBOutlet weak var heightOffset: NSLayoutConstraint!
  41. //阅读模式界面
  42. @IBOutlet weak var readModelView: KMReadModelView!
  43. @IBOutlet weak var bottomAreaView: KMBox!
  44. @IBOutlet weak var readModelViewWidthConstraint: NSLayoutConstraint!
  45. var isReadMode: Bool = false
  46. var readLeftMethodType: BotaType = .None
  47. var readLeftPanelOpen = false
  48. var readLastLeftPanWidth = 0.0
  49. var readLeftViewShowPanel = false
  50. var readRightPanelOpen = false
  51. var readToolbarType: KMToolbarViewType = .None
  52. var readToolbarItemIdentifier: String = ""
  53. var readToolMode: CToolMode = .textToolMode
  54. var readAnnotationType: CAnnotationType = .unkown
  55. var readSubViewType: RightSubViewType = .None
  56. //页码显示器
  57. @IBOutlet weak var pageNumberDisplayView: KMPageNumberDisplayView!
  58. @IBOutlet weak var tipCurrentPageBoxWidthConstraint: NSLayoutConstraint!
  59. //自动滚动
  60. var autoFlowOptionsSheetController: KMAutoFlowOptionsSheetController?
  61. //AI相关
  62. var aiTipView: AITipIconView!
  63. var aiTypeChooseView: AITypeChooseView!
  64. //Search
  65. var searchIndex: Int = 0
  66. //Form
  67. var formAlertView: KMFormAlertView?
  68. private var _needSave = false
  69. var needSave: Bool {
  70. set {
  71. _needSave = newValue
  72. if (_needSave == false) {
  73. self.clearIsPDFDocumentEdited()
  74. self.hiddenSecureSuccessTip()
  75. }
  76. }
  77. get {
  78. return _needSave
  79. }
  80. }
  81. // 标识 pdf 已编辑
  82. fileprivate var _isPDFDocumentEdited = false
  83. // 标识 pdf文字图片已编辑
  84. internal var isPDFTextImageEdited = false
  85. var isPDFDocumentEdited: Bool {
  86. get {
  87. return _isPDFDocumentEdited
  88. }
  89. }
  90. var password: String?
  91. var leftSideViewController: KMLeftSideViewController = KMLeftSideViewController.init(type: KMLeftMethodMode())
  92. var rightSideViewController: KMRightSideViewController!
  93. var searchResults: [KMSearchMode] = []
  94. var mwcFlags: MwcFlags = MwcFlags()
  95. var isShowQuickTour: Bool = false
  96. var document: CPDFDocument?
  97. var myDocument: NSDocument?
  98. weak var browserWindowController: KMBrowserWindowController?
  99. var cropSettingWindowController: KMCropSettingWindowController!
  100. var currentWindowController: NSWindowController!
  101. // var interactionMode: SKInteractionMode = .SKNormalMode
  102. var savedNormalSetup: NSMutableDictionary = NSMutableDictionary()
  103. //数字签名
  104. var digitalSignController: KMPDFDigitalSignViewController?
  105. let CPDFOfficeLeftSidePaneWidthKey = "CPDFOfficeLeftSidePaneWidthKey"
  106. let CPDFOfficeRightSidePaneWidthKey = "CPDFOfficeRightSidePaneWidthKey"
  107. @IBOutlet weak var topTipBox: NSBox!
  108. @IBOutlet weak var exitFullButton: NSButton!
  109. var hasAddRedact: Bool = false
  110. var functionWidth: Double {
  111. get {
  112. if self.isReadMode {
  113. if !self.isShowBOTA {
  114. return 0
  115. }
  116. }
  117. return 48-4
  118. }
  119. }
  120. var isShowBOTA: Bool = false
  121. let panelWidth = 212.0
  122. let defaultRightWidth = 260.0
  123. var lastLeftPanWidth = 0.0
  124. var lastRightPanWidth = 0.0
  125. var leftPanelOpen: Bool = false
  126. var rightPanelIsOpen = false
  127. var pageNumber: UInt?
  128. var openSecondaryPdfView: KMSecondaryViewController?
  129. var secondaryPdfContentView: NSView?
  130. var lastSplitPDFHeight: Float = 0.0
  131. internal var isSaveKeyChain = true
  132. var rightMouseEventing = false
  133. var pdfEditController: KMPDFEditViewController? {
  134. get {
  135. return self.getPDFEditController()
  136. }
  137. }
  138. var autoSaveTimer: Timer?
  139. var progressController: SKProgressController?
  140. private var _documentFirstLoad: Bool = true
  141. var eventMonitor: Any?
  142. var keyEventMonitor: Any?
  143. var mouseRightMenuEvent: NSEvent?
  144. var aiTranslationWindow: KMAITranslationWindowController?
  145. var aiTranslationConfirWC: KMAITranslationConfirmWindowController?
  146. lazy private var homeVC: KMHomeViewController? = {
  147. let vc = KMHomeViewController()
  148. return vc
  149. }()
  150. private var background_mask: NSView?
  151. fileprivate var _secureOptions: [CPDFDocumentWriteOption : Any]?
  152. var secureOptions: [CPDFDocumentWriteOption : Any]? {
  153. get {
  154. return self._secureOptions
  155. }
  156. }
  157. var documentAttribute: [CPDFDocumentAttribute : Any]?
  158. fileprivate var _removeSecureFlag = false
  159. var removeSecureFlag: Bool {
  160. get {
  161. return self._removeSecureFlag
  162. }
  163. }
  164. fileprivate var _saveWatermarkFlag = false
  165. var saveWatermarkFlag: Bool {
  166. get {
  167. return self._saveWatermarkFlag
  168. }
  169. }
  170. var beforeMarkedPageIndex = NSNotFound
  171. var markedPageIndex = NSNotFound
  172. var beforeMarkedPagePoint = NSPoint.zero
  173. var markedPagePoint = NSPoint.zero
  174. var repeatTrialLoad: Bool = false
  175. var _mainWindow: NSWindow?
  176. var mainWindow: NSWindow? {
  177. get {
  178. return self._mainWindow
  179. }
  180. set {
  181. self._mainWindow = newValue
  182. }
  183. }
  184. deinit {
  185. NotificationCenter.default.removeObserver(self)
  186. self.stopAutoSaveTimer()
  187. self.removeEventMonitor()
  188. self.removeKeyEventMonitor()
  189. }
  190. override func awakeFromNib() {
  191. super.awakeFromNib()
  192. self.addBackgroundMaskView()
  193. // pdfSplitView.frame = NSMakeRect(0, 0, NSWidth(centerContentView.bounds), NSHeight(centerContentView.bounds)-1)
  194. // centerContentView.addSubview(pdfSplitView)
  195. self.PDFContendView.backgroundColor(NSColor.km_init(hex: "FFFFFF"))
  196. listView.delegate = self
  197. listView.pdfListViewDelegate = self
  198. // listView.editingConfig().isSupportMultipleSelectEditingArea = true
  199. if (document != nil) {
  200. // if (self.document!.isLocked) {
  201. //
  202. // } else {
  203. listView.document = document
  204. // }
  205. listView.document.delegate = self
  206. let autoScale = listView.autoScales
  207. if !autoScale {
  208. listView.scaleFactor = 1.0
  209. }
  210. }
  211. self.initPDFLeftViewVC()
  212. self.initRightSideView()
  213. self.toolbarController.listView = self.listView
  214. self.newPDFSplitView.delegate = self
  215. }
  216. override func viewDidAppear() {
  217. super.viewDidAppear()
  218. //刷新前一页后一页按钮
  219. self.updateNextAndPreViousButtonState()
  220. KMLightMemberManager.manager.canShowAdvancedView = false
  221. // Task { @MainActor in
  222. // await KMLightMemberManager.manager.canUseAdvanced(needNetworking: true)
  223. // }
  224. // self.addEventMonitor()
  225. self.view.window?.makeFirstResponder(self.listView)
  226. // 更新属性页面的信息
  227. NotificationCenter.default.post(name: KMInfoWindowC.windowDidBecomeMainNotification, object: self.myDocument)
  228. if (self.document!.isLocked == false) {
  229. self.loadFunctionGuide()
  230. self.loadAIIconView()
  231. }
  232. if (self.document == nil) {
  233. return
  234. }
  235. if (self.document!.isLocked == false) {
  236. return
  237. }
  238. if (self.view.window == nil) {
  239. return
  240. }
  241. if (self.password != nil) {
  242. self.isSaveKeyChain = false
  243. self.listView.document.unlock(withPassword: self.password)
  244. return
  245. }
  246. KMPasswordInputWindow.openWindow(window: self.view.window!, url: self.document!.documentURL) { result , password in
  247. if (result == .cancel) {
  248. self.browserWindowController?.browser.closeTab()
  249. return
  250. }
  251. self.isSaveKeyChain = true
  252. self.listView.document = self.document
  253. self.document?.unlock(withPassword: password)
  254. }
  255. }
  256. override func viewWillAppear() {
  257. super.viewWillAppear()
  258. self.reStartAutoSaveTimer()
  259. //是否弹出登录窗口
  260. // self.needShowRegisterView()
  261. }
  262. override func viewWillDisappear() {
  263. super.viewWillDisappear()
  264. // KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
  265. self.pauseAutoSaveTimer()
  266. self.removeEventMonitor()
  267. }
  268. override func viewWillLayout() {
  269. super.viewWillLayout()
  270. if (KMTools.isFullScreen(self.view.window ?? NSWindow())) { // 全屏
  271. self.exitFullButton.isHidden = false
  272. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundFullScreenColor
  273. } else {
  274. self.exitFullButton.isHidden = true
  275. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor
  276. }
  277. }
  278. override func viewDidLoad() {
  279. super.viewDidLoad()
  280. leftSideViewController.mainVC = self
  281. mwcFlags.settingUpWindow = 1
  282. toolbarController.delegate = self
  283. //TODO: 先让项目运行,看后面怎么调整这段逻辑,目前最外层是 KMBrowserWindowController
  284. toolbarBox.contentView = toolbarController.view
  285. // self.childToolbarController.updateType(newType: .Annatiton)
  286. // self.showChildToolbar(showToolbar: true)
  287. // self.toolbarController.editPDFButtonAction(item: NSMenuItem())
  288. if (UserDefaults.standard.object(forKey: CPDFOfficeLeftSidePaneWidthKey) != nil) {
  289. UserDefaults.standard.set(256, forKey: CPDFOfficeLeftSidePaneWidthKey)
  290. UserDefaults.standard.synchronize()
  291. }
  292. if (UserDefaults.standard.object(forKey: CPDFOfficeRightSidePaneWidthKey) != nil) {
  293. UserDefaults.standard.set(256, forKey: CPDFOfficeRightSidePaneWidthKey)
  294. UserDefaults.standard.synchronize()
  295. }
  296. let position = mianSplitView.maxPossiblePositionOfDivider(at: 1)
  297. mianSplitView.setPosition(position, ofDividerAt: 0)
  298. mianSplitView.setPosition(mianSplitView.minPossiblePositionOfDivider(at: 0), ofDividerAt: 0)
  299. pdfSplitView.setPosition(mianSplitView.maxPossiblePositionOfDivider(at: 1), ofDividerAt: 0)
  300. self.locationPageView.wantsLayer = true;
  301. self.locationPageView.layer?.backgroundColor = NSColor(red: 189.0/255.0, green: 223.0/255.0, blue: 253.0/255.0, alpha: 1).cgColor
  302. 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: "")
  303. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  304. if (self.listView.document != nil) {
  305. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document.documentURL.path)
  306. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document.documentURL.path)
  307. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < self.listView.document.pageCount) {
  308. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  309. if (pageScale != nil) {
  310. self.listView.scaleFactor = CGFloat(pageScale!)
  311. }
  312. self.listView.go(toPageIndex: pageNumber!, animated: false)
  313. }
  314. } else {
  315. self._goToFirstPageForFristAppear()
  316. }
  317. }
  318. } else {
  319. self._goToFirstPageForFristAppear()
  320. }
  321. //阅读页面
  322. readModelView.delegate = self
  323. pageNumberDisplayView.delegate = self
  324. tipCurrentPageBox.moveCallback = { [unowned self] mouseEntered, mouseBox in
  325. if mouseEntered {
  326. self.pageNumberDisplayView.hover = true
  327. // self.updatePageIndicatoreType()
  328. } else {
  329. self.pageNumberDisplayView.hover = false
  330. // self.updatePageIndicatoreType()
  331. }
  332. }
  333. NotificationCenter.default.addObserver(self, selector: #selector(rename(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerRename"), object: nil)
  334. NotificationCenter.default.addObserver(self, selector: #selector(closeTab(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseTabs"), object: nil)
  335. NotificationCenter.default.addObserver(self, selector: #selector(showInFinder(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerShowInFinder"), object: nil)
  336. NotificationCenter.default.addObserver(self, selector: #selector(preferenceDidChangeNotification), name: KMPreferenceManager.didChangeNotification, object: nil)
  337. NotificationCenter.default.addObserver(self, selector: #selector(documentDidUnlockNotification), name: Notification.Name("CPDFDocumentDidUnlockNotification"), object: nil)
  338. NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminateNotification), name: NSApplication.willTerminateNotification, object: nil)
  339. NotificationCenter.default.addObserver(self, selector: #selector(KMPDFViewCurrentPageDidChangedNotification), name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: nil)
  340. NotificationCenter.default.addObserver(self, selector: #selector(CPDFDocumentPageCountChangedNotification), name: NSNotification.Name.init(rawValue: "CPDFDocumentPageCountChangedNotification"), object: nil)
  341. NotificationCenter.default.addObserver(self, selector: #selector(CEditPDFToolModeChangeStateUnkownNotification), name: Notification.Name.init("CEditPDFToolModeChangeStateUnkown"), object: nil)
  342. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.listView)
  343. NotificationCenter.default.addObserver(self, selector: #selector(handleDisplayBoxChangedNotification), name: NSNotification.Name.CPDFViewDisplayBoxChanged, object: self.listView)
  344. // 互动模式
  345. NotificationCenter.default.addObserver(self, selector: #selector(willEnterInteractionModeNotification), name: NSWindow.willEnterInteractionModeNotification, object: nil)
  346. NotificationCenter.default.addObserver(self, selector: #selector(didEnterInteractionModeNotification), name: NSWindow.didEnterInteractionModeNotification, object: nil)
  347. NotificationCenter.default.addObserver(self, selector: #selector(willShowFullScreenNotification), name: NSWindow.willShowFullScreenNotification, object: nil)
  348. NotificationCenter.default.addObserver(self, selector: #selector(didShowFullScreenNotification), name: NSWindow.didShowFullScreenNotification, object: nil)
  349. NotificationCenter.default.addObserver(self, selector: #selector(didAddContentViewNotification), name: NSWindow.didAddContentViewNotification, object: nil)
  350. self.autoSaveTimeStartOrStopIfNeed()
  351. self.toolbarController.selectItem(KMDocumentAnnotationToolbarItemIdentifier)
  352. self.closeRightPane()
  353. self.addKeyEventMonitor()
  354. self.addAdsBannerView()
  355. //检测OCR包是否需要更新
  356. #if VERSION_DMG
  357. KMResourceDownloadManager.manager.checkDocumentAIVersion()
  358. #endif
  359. }
  360. //MARK: - KMToolbarViewControllerDelegate
  361. //MARK: - PDFListView
  362. func initPDFLeftViewVC() {
  363. var frame = self.leftView.frame
  364. frame.size.width += 44
  365. self.leftView.frame = frame
  366. leftSideViewController.listView = self.listView ?? CPDFListView()
  367. leftSideViewController.view.frame = CGRect(x: 0, y:0 , width: self.leftView.frame.size.width, height: self.leftView.frame.size.height)
  368. leftSideViewController.view.autoresizingMask = [.height,.width]
  369. leftSideViewController.delegate = self
  370. self.leftView.addSubview(leftSideViewController.view)
  371. }
  372. func initRightSideView() {
  373. self.rightSideViewController = KMRightSideViewController.init()
  374. self.rightSideViewController.view.frame = CGRect(x: 0, y: 0, width: self.rightView.frame.width, height: self.rightView.frame.size.height)
  375. self.rightSideViewController.view.autoresizingMask = [.height,.width]
  376. self.rightSideViewController.listView = self.listView
  377. // self.rightSideViewController.view.isHidden = true
  378. self.rightSideViewController.isHidden = true
  379. self.rightSideViewController.delegate = self
  380. self.rightView.addSubview(self.rightSideViewController.view)
  381. self.rightSideViewController.propertyDidChange = {
  382. [weak self] (model: AnyObject?) in
  383. let topBarView = self?.toolbarController.fetchTopBarView()
  384. if (topBarView == nil || ((topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self)) == false)) {
  385. return
  386. }
  387. /// Bates码、页眉页脚、背景、水印
  388. if (model == nil) {
  389. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: false)
  390. } else {
  391. if ((model?.isKind(of: KMBatesModel.self))!) {
  392. if ((model as! KMBatesModel).hasVaild) {
  393. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: true)
  394. } else {
  395. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: false)
  396. }
  397. } else if ((model?.isKind(of: KMHeaderFooterModel.self))!) {
  398. if ((model as! KMHeaderFooterModel).hasVaild) {
  399. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: true)
  400. } else {
  401. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: false)
  402. }
  403. } else if ((model?.isKind(of: KMBackgroundModel.self))!) {
  404. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: true)
  405. } else if ((model?.isKind(of: KMWatermarkModel.self))!) {
  406. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: true)
  407. }
  408. }
  409. }
  410. }
  411. func addAdsBannerView() {
  412. #if VERSION_FREE
  413. if !IAPProductsManager.default().isAvailableAllFunction(){
  414. guard let document = self.listView.document else {
  415. return
  416. }
  417. if !document.isLocked {
  418. KMAdsManager.defaultManager.beginSheetModalForView(self.readContentView, directions: .down, adPosY: 30, animated: false) { pageIndex in
  419. }
  420. }
  421. }
  422. #endif
  423. //加载底部banner
  424. // - (void)loadingAdsManager {
  425. // #if VERSION_FREE
  426. // if(![self.pdfDocument isLocked]) {
  427. // if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
  428. // [[KMAdsManager defaultManager] beginSheetModalForView:self.pdfView
  429. // directions:KMADViewDirectionsDown
  430. // animated:NO
  431. // completionHandler:nil];
  432. // }
  433. // [[NSNotificationCenter defaultCenter] addObserverForName:KMIAPProductPurchasedNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){
  434. // if ([IAPProductsManager defaultManager].isAvailableAllFunction) {
  435. // [[KMAdsManager defaultManager] dismissSheetModalForView:self.pdfView];
  436. // }
  437. // }];
  438. // [[NSNotificationCenter defaultCenter] addObserverForName:kDeviceActivateStatusChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){
  439. // if ([IAPProductsManager defaultManager].isAvailableAllFunction) {
  440. // [[KMAdsManager defaultManager] dismissSheetModalForView:self.pdfView];
  441. // }
  442. // }];
  443. // }
  444. // #endif
  445. // }
  446. }
  447. // MARK: Private Methods
  448. internal func removeNotifications() {
  449. NotificationCenter.default.removeObserver(self)
  450. self.leftSideViewController.clearAnnotationFilterData()
  451. self.leftSideViewController.clearNotification()
  452. }
  453. func checkShouldAutoOpenLeftVC() {
  454. // if (KMPreferenceManager.shared.leftSideNeedCloseWhenOpenFile()) {
  455. // return
  456. // }
  457. // if (KMPreferenceManager.shared.leftSideDisplayType == .showOutlineIfHas) {
  458. // let outlineRoot = self.listView.document.outlineRoot()
  459. // let hasOutline = (outlineRoot != nil && outlineRoot!.numberOfChildren > 0)
  460. // if (hasOutline == false) {
  461. // return
  462. // }
  463. //
  464. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  465. // self.leftSideViewController.refreshMethodType(methodType: .Outline)
  466. // }
  467. // return
  468. // }
  469. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  470. // let selectedIndex = UserDefaults.standard.integer(forKey: "KMBOTASelectedIndexKey")
  471. // let thumai = KMLeftMethodMode()
  472. // if selectedIndex == 0 {
  473. // thumai.methodType = .Thumbnail
  474. // } else if selectedIndex == 1 {
  475. // thumai.methodType = .Outline
  476. // } else if selectedIndex == 2 {
  477. // thumai.methodType = .BookMark
  478. // } else if selectedIndex == 3 {
  479. // thumai.methodType = .Annotation
  480. // } else if selectedIndex == 4 {
  481. // thumai.methodType = .Search
  482. // }
  483. //
  484. // self.leftSideViewController.refreshMethodType(methodType: thumai.methodType)
  485. // }
  486. if KMPreference.shared.showLeftSideBar == false {
  487. return
  488. }
  489. if self.leftPanelOpen {
  490. return
  491. }
  492. Task { @MainActor in
  493. self.leftSideViewController.showThumbnail()
  494. self.toolbarController.findItem(KMLeftControlToolbarItemIdentifier)?.isSelected = true
  495. }
  496. }
  497. func applyLeftSideWidth(_ leftSideWidth: CGFloat, rightSideWidth: CGFloat) -> Void {
  498. mianSplitView.setPosition(leftSideWidth, ofDividerAt: 0)
  499. mianSplitView.setPosition(mianSplitView.maxPossiblePositionOfDivider(at: 1) - mianSplitView.dividerThickness - rightSideWidth, ofDividerAt: 1)
  500. lastLeftPanWidth = leftSideWidth
  501. lastRightPanWidth = rightSideWidth
  502. }
  503. //MARK: 动画 暂未接入
  504. func animateSplitView(to position: CGFloat) {
  505. let frame1 = NSRect(x: 0, y: 0, width: position, height: mianSplitView.frame.height)
  506. let frame2 = NSRect(x: position, y: 0, width: mianSplitView.frame.width - position, height: mianSplitView.frame.height)
  507. // 创建一个新的动画上下文
  508. let animationContext = NSAnimationContext.current
  509. animationContext.duration = 0.5
  510. animationContext.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  511. // 启用隐式动画
  512. NSAnimationContext.current.allowsImplicitAnimation = true
  513. // 移动分隔条
  514. mianSplitView.setPosition(position, ofDividerAt: 0)
  515. mianSplitView.subviews[0].frame = frame1
  516. mianSplitView.subviews[1].frame = frame2
  517. }
  518. internal var removeAllAnnotationsStore = KMPDFViewRemoveAllAnnotationsStore()
  519. internal func removeAllAnnotations() {
  520. let alert = NSAlert()
  521. alert.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  522. alert.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  523. alert.addButton(withTitle: NSLocalizedString("No", comment:""))
  524. if (alert.runModal() != .alertFirstButtonReturn) {
  525. return
  526. }
  527. DispatchQueue.main.async {
  528. self.removeAllAnnotationsStore.store(t: self.listView)
  529. }
  530. }
  531. // MARK: Set Methods
  532. var setDocument: CPDFDocument? {
  533. get {
  534. return document
  535. }
  536. set {
  537. if document != newValue {
  538. document = newValue
  539. }
  540. listView.document = document
  541. listView.document.delegate = self
  542. self.listView.layoutDocumentView()
  543. }
  544. }
  545. var setPageNumber: UInt {
  546. get {
  547. return pageNumber!
  548. }
  549. set {
  550. let pageCount = listView.document.pageCount
  551. var value = newValue
  552. if value > pageCount {
  553. value = pageCount
  554. }
  555. if value > 0 && listView.currentPage().pageIndex() != value-1 {
  556. listView.go(to: listView.document.page(at: value-1))
  557. }
  558. if pageNumber != value {
  559. pageNumber = value
  560. }
  561. }
  562. }
  563. // MARK: - 标记密文
  564. func enterRedact() {
  565. if !IAPProductsManager.default().isAvailableAllFunction(){
  566. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  567. return
  568. }
  569. if self.listView.document.allowsPrinting == false || self.listView.document.allowsCopying == false {
  570. Task {
  571. _ = await KMAlertTool.runModel(message: KMLocalizedString("This is a secured document. Editing is not permitted.", nil))
  572. }
  573. return
  574. }
  575. if self.hasEnterRedact() {
  576. self.exitRedact()
  577. return
  578. }
  579. let ttsWindowC = KMTTSWindowController.share
  580. if ttsWindowC.pdfView?.document?.documentURL.path == self.listView.document.documentURL.path {
  581. if let data = ttsWindowC.window?.isVisible, data {
  582. ttsWindowC.stopSpeaking()
  583. ttsWindowC.close()
  584. }
  585. }
  586. NSColorPanel.shared.showsAlpha = false
  587. let controller = KMPDFRedactViewController(url: self.listView.document!.documentURL, password: self.listView.document.password)
  588. self.addChild(controller)
  589. self.PDFContendView.addSubview(controller.view)
  590. controller.view.frame = self.PDFContendView.bounds
  591. controller.view.autoresizingMask = [.width, .height]
  592. self.listView.isHidden = true
  593. controller.scaleFactor = self.listView.scaleFactor
  594. controller.titleBack = { [weak self] title in
  595. self?.view.window?.title = title
  596. }
  597. controller.callback = { [weak self] result, currentPageIndex, saveResult, saveUrl in
  598. if result == false { // 退出
  599. self?.exitRedact()
  600. return
  601. }
  602. let controller = self?._getPDFRedactController()
  603. controller?.redactPdfView.newAddAnnotation.removeAll()
  604. self?.exitRedact()
  605. if saveResult {
  606. let newDocument = CPDFDocument(url: saveUrl)
  607. if let data = newDocument?.isLocked, data {
  608. newDocument?.unlock(withPassword: self?.listView.document.password ?? "")
  609. }
  610. self?.document = newDocument
  611. self?.listView.document = newDocument
  612. self?.listView.layoutDocumentView()
  613. }
  614. }
  615. }
  616. func exitRedact() {
  617. let controller = self._getPDFRedactController()
  618. if let data = controller {
  619. if data.redactPdfView.newAddAnnotation.count > 0 {
  620. 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
  621. if response == .alertFirstButtonReturn {
  622. data.redactPdfView.newAddAnnotation.removeAll()
  623. self.exitRedact()
  624. }
  625. }
  626. return
  627. }
  628. }
  629. NSColorPanel.shared.showsAlpha = true
  630. self.toolbarController.findItem(KMDocumentRedactToolbarItemIdentifier)?.isSelected = false
  631. // self.toolbarController.toolbarType = .None
  632. // self.listView.toolMode = .moveToolMode
  633. controller?.redactPdfView.resignMonitor()
  634. controller?.view.removeFromSuperview()
  635. controller?.removeFromParent()
  636. self.listView.isHidden = false
  637. // self.listView.layoutDocumentView()
  638. // self.view.window?.makeFirstResponder(self.listView)
  639. self.listView.annotationType = .unkown
  640. }
  641. func hasEnterRedact() -> Bool {
  642. return self._getPDFRedactController() != nil
  643. }
  644. //MARK: - AI
  645. func loadAIIconView() -> Void {
  646. self.aiTipView = AITipIconView.createFromNib()
  647. self.aiTipView.clickHandle = { [weak self] view in
  648. self?.showAITypeChooseView()
  649. }
  650. self.aiTipView.frame = CGRectMake(CGRectGetWidth(self.view.frame)-84, CGRectGetHeight(self.view.frame)-84-64-50, 72, 72)
  651. self.aiTipView.autoresizingMask = [.minXMargin, .minYMargin]
  652. self.view.addSubview(self.aiTipView)
  653. }
  654. func showAITypeChooseView() -> Void {
  655. if AIInfoManager.default().aiInfo.infoDict.isEmpty == true {
  656. #if VERSION_DMG
  657. KMPurchaseCompareWindowController.dmgPurchaseInstance().showWindow(nil)
  658. #else
  659. AIPurchaseWindowController.currentWC().showWindow(nil)
  660. #endif
  661. return
  662. }
  663. let controller = NSViewController.init()
  664. let view = NSView.init()
  665. controller.view = view
  666. var string: String = NSLocalizedString("AI Summarize", comment: "")
  667. if string.count < NSLocalizedString("AI Rewrite", comment: "").count {
  668. string = NSLocalizedString("AI Rewrite", comment: "")
  669. }
  670. if string.count < NSLocalizedString("AI Proofread", comment: "").count {
  671. string = NSLocalizedString("AI Proofread", comment: "")
  672. }
  673. if string.count < NSLocalizedString("AI Translate", comment: "").count {
  674. string = NSLocalizedString("AI Translate", comment: "")
  675. }
  676. let font = NSFont.SFProTextRegularFont(13)
  677. var style = NSMutableParagraphStyle.init()
  678. style.alignment = .center
  679. style.lineBreakMode = .byCharWrapping
  680. let size: NSSize = string.boundingRect(with: NSSize(width: 1000, height: 100),
  681. options: NSString.DrawingOptions(rawValue: 3),
  682. attributes: [NSAttributedString.Key.font : NSFont.SFProTextRegularFont(13), NSAttributedString.Key.paragraphStyle : style]).size
  683. controller.view.frame = CGRectMake(0, 0, size.width+80, 160)
  684. if self.aiTypeChooseView == nil {
  685. self.aiTypeChooseView = AITypeChooseView.createFromNib()
  686. }
  687. self.aiTypeChooseView.clickHandle = { [weak self] view, type in
  688. DispatchQueue.main.async {
  689. self!.loadAIConfigWindowWithType(type)
  690. }
  691. }
  692. self.aiTypeChooseView.frame = controller.view.bounds
  693. self.aiTypeChooseView.autoresizingMask = [.width, .height]
  694. controller.view.addSubview(self.aiTypeChooseView)
  695. let popover = NSPopover.init()
  696. popover.contentViewController = controller
  697. popover.animates = true
  698. popover.behavior = .transient
  699. var rect = self.aiTipView.bounds
  700. rect.origin.y += 20
  701. popover.show(relativeTo: rect, of: self.aiTipView, preferredEdge: .minY)
  702. }
  703. func loadAIConfigWindowWithType(_ type: AIConfigType) -> Void {
  704. let windowVC = AIConfigWindowController.currentWC()
  705. windowVC.configType = type
  706. if type == .summarize {
  707. windowVC.window?.setFrame(CGRectMake(0, 0, 800, 500), display: true)
  708. } else if type == .reWriting {
  709. windowVC.window?.setFrame(CGRectMake(0, 0, 800, 460), display: true)
  710. } else if type == .proofreading {
  711. windowVC.window?.setFrame(CGRectMake(0, 0, 800, 460), display: true)
  712. } else if type == .translate {
  713. windowVC.window?.setFrame(CGRectMake(0, 0, 800, 545), display: true)
  714. }
  715. windowVC.cancelHandle = { [weak self] windowVC in
  716. self?.view.window?.endSheet(windowVC.window!)
  717. }
  718. self.view.window?.beginSheet(windowVC.window!)
  719. windowVC.refreshUI()
  720. }
  721. //MARK: - 引导
  722. func loadFunctionGuide() -> Void {
  723. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  724. if self.view.window != nil {
  725. self.loadOpenFileFunctionGuide(.openFileNormal)
  726. }
  727. }
  728. }
  729. func loadOpenFileFunctionGuide(_ showType: KMGuideInfoType) -> Void {
  730. if showType == .openFileNormal && KMGuideInfoWindowController.availableShow(.openFileNormal) {
  731. let leftPanelItem:KMToolbarItemView = self.toolbarController.findItem("KMLeftControlToolbarItemIdentifier")!
  732. let guideWC = KMGuideInfoWindowController.currentWC()
  733. guideWC.type = .openFileNormal
  734. guideWC.openPanelRect = (self.view.window?.contentView?.convert(leftPanelItem.frame, from: leftPanelItem.superview)) ?? CGRectZero
  735. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  736. guideWC.normalGuideFinishHandle = { [weak self] windowVC in
  737. let rightPanelItem = self?.toolbarController.findItem(KMRightControlToolbarItemIdentifier)
  738. let digitalPanelItem = self?.toolbarController.findItem(KMDocumentDigitalSignToolbarItemIdentifier)
  739. windowVC.rightPanelRect = (self!.view.window?.contentView?.convert(rightPanelItem?.frame ?? .zero, from: rightPanelItem?.superview)) ?? .zero
  740. guideWC.digitalBoxRect = (self!.view.window?.contentView?.convert(digitalPanelItem?.frame ?? .zero, from: digitalPanelItem?.superview)) ?? .zero
  741. }
  742. guideWC.finishHandle = { [weak self] windowVC, type in
  743. if type == .windowNewFinish ||
  744. type == . windowDigitalFinish {
  745. self?.checkFirstTrialController()
  746. }
  747. }
  748. guideWC.openFileToggleHandle = { [weak self] windowVC, type in
  749. self?.checkFirstTrialController()
  750. }
  751. var rect = self.view.window!.frame
  752. rect.size.height -= 20
  753. guideWC.window?.setFrame(rect, display: false)
  754. guideWC.window?.minSize = rect.size
  755. guideWC.window?.maxSize = rect.size
  756. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  757. guideWC.show()
  758. } else if showType == .digitalSignGuide && KMGuideInfoWindowController.availableShow(.digitalSignGuide) {
  759. let guideWC = KMGuideInfoWindowController.currentWC()
  760. guideWC.type = .digitalSignGuide
  761. let digitalPanelItem:KMToolbarItemView = self.toolbarController.findItem(KMDocumentDigitalSignToolbarItemIdentifier)!
  762. guideWC.digitalBoxRect = (self.view.window?.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview))!
  763. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  764. guideWC.finishHandle = { [weak self] windowVC, type in
  765. self?.checkFirstTrialController()
  766. }
  767. var rect = self.view.window!.frame
  768. rect.size.height -= 20
  769. guideWC.window?.setFrame(rect, display: false)
  770. guideWC.window?.minSize = rect.size
  771. guideWC.window?.maxSize = rect.size
  772. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  773. guideWC.show()
  774. } else if showType == .pdfCompareGuide && KMGuideInfoWindowController.availableShow(.pdfCompareGuide) {
  775. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  776. let guideWC = KMGuideInfoWindowController.currentWC()
  777. guideWC.type = .pdfCompareGuide
  778. let digitalPanelItem:KMToolbarItemView = self.toolbarController.findItem(KMDocumentDigitalSignToolbarItemIdentifier)!
  779. guideWC.digitalBoxRect = (self.view.window?.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview))!
  780. let compareItem:KMToolbarItemView = self.toolbarController.findItem(KMToolbarComparisonItemIdentifier)!
  781. guideWC.compareItemRect = (self.view.window?.contentView?.convert(compareItem.frame, from: compareItem.superview))!
  782. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  783. var rect = self.view.window!.frame
  784. rect.size.height -= 20
  785. guideWC.window?.setFrame(rect, display: false)
  786. guideWC.window?.minSize = rect.size
  787. guideWC.window?.maxSize = rect.size
  788. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  789. guideWC.show()
  790. }
  791. } else if showType == .convertGuide && KMGuideInfoWindowController.availableShow(.convertGuide) {
  792. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  793. let guideWC = KMGuideInfoWindowController.currentWC()
  794. guideWC.type = .convertGuide
  795. let digitalPanelItem:KMToolbarItemView = self.toolbarController.findItem(KMDocumentDigitalSignToolbarItemIdentifier)!
  796. guideWC.digitalBoxRect = (self.view.window?.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview))!
  797. guideWC.purchaseHandle = { [weak self] windowVC in
  798. #if VERSION_DMG
  799. if IAPProductsManager.default().isAvailableAllFunction() {
  800. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  801. //Convert:
  802. self?.showAllConvertWindow(convertT: .Word)
  803. } else {
  804. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  805. limitWC.continueBlock = { [weak self] windowController in
  806. }
  807. limitWC.window?.center()
  808. limitWC.showWindow(nil)
  809. }
  810. } else {
  811. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  812. }
  813. #else
  814. if IAPProductsManager.default().isAvailableAllFunction() {
  815. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  816. //Convert:
  817. } else {
  818. var vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  819. vc.showWindow(nil)
  820. }
  821. } else {
  822. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  823. }
  824. #endif
  825. }
  826. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  827. var rect = self.view.window!.frame
  828. rect.size.height -= 20
  829. guideWC.window?.setFrame(rect, display: false)
  830. guideWC.window?.minSize = rect.size
  831. guideWC.window?.maxSize = rect.size
  832. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  833. guideWC.show()
  834. }
  835. } else {
  836. }
  837. }
  838. func checkFirstTrialController() -> Void {
  839. #if VERSION_DMG
  840. //打开文档后引导相关
  841. if VerificationManager.default().status == .none {
  842. let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  843. let lastVersion = UserDefaults.standard.object(forKey: "SKLastTrialVersionMainDocumentLaunchedKey") as? String ?? ""
  844. if lastVersion == nil ||
  845. lastVersion != appVersion {
  846. UserDefaults.standard.setValue(appVersion, forKey: "SKLastTrialVersionMainDocumentLaunchedKey")
  847. UserDefaults.standard.synchronize()
  848. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  849. }
  850. }
  851. #endif
  852. }
  853. // MARK: - 页面编辑
  854. open func enterPageEdit(_ pages: [Int] = []) {
  855. //选中page
  856. var tPages = pages
  857. if tPages.count == 0 {
  858. tPages = self.leftSideViewController.selectPages ?? [self.listView.currentPageIndex]
  859. }
  860. if (hasEnterPageEdit()) {
  861. exitPageEdit()
  862. return
  863. }
  864. if (self.toolbarController != nil && self.toolbarController.mainToolBarView != nil) {
  865. let toolBarView: KMToolbarViewController = self.toolbarController.mainToolBarView!
  866. for (key, value) in toolBarView.toolbarItems {
  867. if (key == KMRightControlToolbarItemIdentifier || key == KMLeftControlToolbarItemIdentifier) {
  868. (value as! KMToolbarItemView).unEnabled = true
  869. }
  870. }
  871. }
  872. // #if VERSION_DMG
  873. // let controller = KMPDFEditViewController_dmg(self.listView.document)
  874. // #else
  875. let controller = KMPDFEditViewController(self.listView.document)
  876. // #endif
  877. controller.selectedPages = tPages
  878. controller.listView = self.listView
  879. self.addChild(controller)
  880. self.PDFContendView.addSubview(controller.view)
  881. controller.view.frame = self.PDFContendView.bounds
  882. controller.view.autoresizingMask = [.width,.height]
  883. self.listView.isHidden = true
  884. controller.itemClick = { [weak self] index, params in
  885. if (index == 1) { /// 双击退出
  886. self?.enterEditMode(self!.leftSideViewController, [])
  887. DispatchQueue.main.async {
  888. let pageIndex: Int = params.first as! Int
  889. self?.listView.go(toPageIndex: pageIndex, animated: true)
  890. }
  891. } else if (index == 2) { // 打印
  892. self?.showPrintWindow(pageRange: KMPrintPageRange(type: .custom, selectPages: params.first as! [Int]))
  893. }
  894. }
  895. controller.documentEditedCallback = { [weak self] params in
  896. self?.recordIsPDFDocumentEdited()
  897. // self?.needSave = true
  898. if let data = params.first as? KMPageEditType {
  899. self?.recordSaveWatermarkFlag(type: data.toSubscribeWaterMarkType())
  900. } else {
  901. self?.recordSaveWatermarkFlag()
  902. }
  903. }
  904. controller.selectionDidChange = { [weak self] selectedIndexs in
  905. var indexSet = IndexSet()
  906. for indexPath in selectedIndexs {
  907. indexSet.insert(indexPath.item)
  908. }
  909. if indexSet.count != 0 {
  910. // self?.leftSideViewController.thumbnailViewController.selectPages(indexs: indexSet, needScroll: true)
  911. // self?.listView.go(toPageIndex: indexSet.first!, animated: false)
  912. }
  913. }
  914. }
  915. open func exitPageEdit() {
  916. if (self.toolbarController != nil && self.toolbarController.mainToolBarView != nil) {
  917. let toolBarView: KMToolbarViewController = self.toolbarController.mainToolBarView!
  918. for (key, value) in toolBarView.toolbarItems {
  919. if (key == KMRightControlToolbarItemIdentifier || key == KMLeftControlToolbarItemIdentifier) {
  920. (value as! KMToolbarItemView).unEnabled = false
  921. }
  922. }
  923. }
  924. let editController = getPDFEditController()
  925. if (editController == nil) {
  926. return
  927. }
  928. self.listView.annotationType = .highlight
  929. editController?.view.removeFromSuperview()
  930. editController?.removeFromParent()
  931. self.listView.isHidden = false
  932. self.listView.layoutDocumentView()
  933. self.view.window?.makeFirstResponder(self.listView)
  934. self.listView.annotationType = .unkown
  935. self.listView.go(toPageIndex: editController!.listViewCurrentIndex, animated: false)
  936. if let data = editController?.isEdited, data {
  937. self.leftSideViewController.reloadThumbnailDataIfNeed()
  938. }
  939. }
  940. open func hasEnterPageEdit() -> Bool {
  941. return self.getPDFEditController() != nil
  942. }
  943. // MARK: - Private Methods
  944. private func getPDFEditController() -> KMPDFEditViewController? {
  945. var editController: KMPDFEditViewController?
  946. for controller in self.children {
  947. if (controller.isKind(of: KMPDFEditViewController.self)) {
  948. editController = (controller as! KMPDFEditViewController)
  949. break
  950. }
  951. }
  952. return editController
  953. }
  954. private func _getPDFRedactController() -> KMPDFRedactViewController? {
  955. var controller: KMPDFRedactViewController?
  956. for childC in self.children {
  957. if (childC.isKind(of: KMPDFRedactViewController.self)) {
  958. controller = (childC as! KMPDFRedactViewController)
  959. break
  960. }
  961. }
  962. return controller
  963. }
  964. private func addBackgroundMaskView() {
  965. self.removeBackgroundMaskView()
  966. if let superview = self.mianSplitView.superview {
  967. let view = NSView()
  968. superview.addSubview(view)
  969. view.frame = superview.bounds
  970. view.autoresizingMask = [.width, .height]
  971. view.wantsLayer = true
  972. view.layer?.backgroundColor = .white
  973. self.background_mask = view
  974. }
  975. }
  976. private func removeBackgroundMaskView() {
  977. self.background_mask?.removeFromSuperview()
  978. self.background_mask = nil
  979. }
  980. private func _goToFirstPageForFristAppear() {
  981. DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.1) {
  982. self.listView.go(toPageIndex: 0, animated: false)
  983. }
  984. }
  985. func isFileGreaterThan10MB(atPath filePath: String) -> Bool {
  986. let fileManager = FileManager.default
  987. do {
  988. let fileAttributes = try fileManager.attributesOfItem(atPath: filePath)
  989. if let fileSize = fileAttributes[.size] as? UInt64 {
  990. let megabyteSize = fileSize / (1024 * 1024)
  991. return megabyteSize >= 10
  992. }
  993. } catch {
  994. KMPrint("Error: \(error)")
  995. }
  996. return false
  997. }
  998. func isPDFPageCountExceedsLimit(filePath: String) -> Bool {
  999. let url = URL(fileURLWithPath: filePath)
  1000. guard let document = PDFDocument(url: url) else {
  1001. return false
  1002. }
  1003. let pageCount = document.pageCount
  1004. return pageCount > 30
  1005. }
  1006. // MARK: Redact 【标记密文】
  1007. func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
  1008. let windowController = KMRedactConfirmWindowController(type)
  1009. self.currentWindowController = windowController
  1010. self.view.window?.beginSheet(windowController.window!)
  1011. windowController.itemClick = { [weak self] index in
  1012. if (index == 2) { /// 取消
  1013. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1014. self?.currentWindowController = nil
  1015. callback()
  1016. return
  1017. }
  1018. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1019. self?.currentWindowController = nil
  1020. let panel = NSSavePanel()
  1021. panel.nameFieldStringValue = "[新文件]"+(self?.listView.document.documentURL.lastPathComponent)!
  1022. let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  1023. button.state = .on
  1024. panel.accessoryView = button
  1025. panel.isExtensionHidden = true
  1026. panel.beginSheetModal(for: (self?.view.window!)!) { response in
  1027. if response != .OK {
  1028. callback()
  1029. return
  1030. }
  1031. if (type == .redactOne) {
  1032. let anno = self!.listView.activeAnnotation
  1033. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  1034. callback()
  1035. return
  1036. }
  1037. (anno as! CPDFRedactAnnotation).applyRedaction()
  1038. } else if (type == .redactAll) {
  1039. self?.listView.document.applyRedactions()
  1040. } else if (type == .eraserOne) {
  1041. let anno = self!.listView.activeAnnotation
  1042. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  1043. callback()
  1044. return
  1045. }
  1046. anno?.page.erasureRedact(from: anno!.bounds)
  1047. } else if (type == .eraserAll) {
  1048. KMRedactTools.eraserDocument((self?.listView.document)!) { result, errorAnno in
  1049. if (result == false) {
  1050. callback()
  1051. return
  1052. }
  1053. }
  1054. }
  1055. self!.listView.document.write(to: panel.url)
  1056. if (button.state == .on) {
  1057. NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
  1058. }
  1059. } else {
  1060. NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
  1061. }
  1062. callback()
  1063. }
  1064. }
  1065. }
  1066. // MARK: Secure 【安全】
  1067. public func showSecureSuccessTip() {
  1068. let view: NSView = self.view
  1069. let tip = KMSecureEncryptSuccessTipView()
  1070. let size = NSSize(width: 379, height: 176)
  1071. tip.frame = NSMakeRect(view.frame.size.width-size.width-16, view.frame.size.height-size.height-88, size.width, size.height)
  1072. tip.autoresizingMask = [.minXMargin, .minYMargin]
  1073. view.addSubview(tip)
  1074. tip.itemClick = { [weak self] in
  1075. self!.hiddenSecureSuccessTip()
  1076. }
  1077. }
  1078. public func hiddenSecureSuccessTip() {
  1079. let view: NSView = self.view
  1080. var tip: KMSecureEncryptSuccessTipView?
  1081. for subview in view.subviews {
  1082. if (subview.isKind(of: KMSecureEncryptSuccessTipView.self)) {
  1083. tip = (subview as! KMSecureEncryptSuccessTipView)
  1084. break
  1085. }
  1086. }
  1087. if (tip == nil) {
  1088. return
  1089. }
  1090. tip?.removeFromSuperview()
  1091. }
  1092. public func showSecureLimitTip() {
  1093. self.hiddenSecureLimitTip()
  1094. let tip = KMSecureAlertView()
  1095. tip.show(in: self.listView)
  1096. tip.closeAction = { [unowned self] view in
  1097. self.hiddenSecureLimitTip()
  1098. }
  1099. tip.passwordAction = { [unowned self] view in
  1100. self.removeOwnerPassword()
  1101. }
  1102. }
  1103. func removeOwnerPassword() {
  1104. guard let doc = self.listView?.document else {
  1105. NSSound.beep()
  1106. return
  1107. }
  1108. if doc.allowsCopying && doc.allowsPrinting {
  1109. NSSound.beep()
  1110. return
  1111. }
  1112. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: doc.documentURL) { [weak self] result, password in
  1113. if result == .cancel { /// 关闭
  1114. return
  1115. }
  1116. /// 解密成功
  1117. self?.hiddenSecureLimitTip()
  1118. self?.isSaveKeyChain = false
  1119. self?.listView.document.unlock(withPassword: password)
  1120. }
  1121. }
  1122. public func hiddenSecureLimitTip() {
  1123. let view: NSView = self.view
  1124. var tip: KMSecureAlertView?
  1125. for subview in view.subviews {
  1126. if (subview.isKind(of: KMSecureAlertView.self)) {
  1127. tip = (subview as! KMSecureAlertView)
  1128. break
  1129. }
  1130. }
  1131. if (tip == nil) {
  1132. return
  1133. }
  1134. tip?.removeFromSuperview()
  1135. }
  1136. override func mouseMoved(with event: NSEvent) {
  1137. }
  1138. func savePageNumberIfNeed() {
  1139. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  1140. let scaleFactor = self.listView?.scaleFactor ?? 0
  1141. if scaleFactor <= 0 {
  1142. return
  1143. }
  1144. if self.listView.document != nil {
  1145. KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
  1146. KMPreferenceManager.shared.setPageScale(Float(self.listView.scaleFactor), forKey: self.listView.document.documentURL.path)
  1147. }
  1148. }
  1149. }
  1150. // MARK: -
  1151. // MARK: 退出全屏
  1152. @IBAction func exitFullScreen(_ sender: Any) {
  1153. if (self.view.window == nil) {
  1154. return
  1155. }
  1156. if (KMTools.isFullScreen(self.view.window!)) {
  1157. self.view.window?.toggleFullScreen(nil)
  1158. }
  1159. }
  1160. // MARK: -
  1161. // MARK: 显示合并窗口
  1162. public func showMergeWindow(url: URL? = nil, _ password: String?) {
  1163. DispatchQueue.main.async {
  1164. if let _url = url {
  1165. let document = PDFDocument(url: _url)
  1166. let windowController = KMMergeWindowController(document: document!, password: password ?? "")
  1167. windowController.oriDucumentUrl = self.listView.document.documentURL
  1168. windowController.pageIndex = self.listView.currentPageIndex
  1169. self.currentWindowController = windowController
  1170. windowController.cancelAction = { [unowned self] controller in
  1171. self.view.window?.endSheet((self.currentWindowController.window)!)
  1172. self.currentWindowController = nil
  1173. }
  1174. windowController.mergeAction = { [unowned self] controller, filePath in
  1175. self.view.window?.endSheet((self.currentWindowController.window)!)
  1176. self.currentWindowController = nil
  1177. let newDocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
  1178. if let data = newDocument?.isLocked, data {
  1179. newDocument?.unlock(withPassword: self.listView.document.password ?? "")
  1180. }
  1181. self.setDocument = newDocument
  1182. if self.leftSideViewController.type.methodType == .Thumbnail {
  1183. self.leftSideViewController.resetThumbnails()
  1184. }
  1185. }
  1186. self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
  1187. self.view.window?.beginSheet(windowController.window!)
  1188. } else {
  1189. let document = PDFDocument(url: self.listView.document.documentURL)
  1190. let windowController = KMMergeWindowController(document: document!, password: password ?? "")
  1191. self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
  1192. self.view.window!.km_beginSheet(windowC: windowController)
  1193. }
  1194. }
  1195. }
  1196. // MARK: -
  1197. // MARR: 显示加密弹窗
  1198. public func showSecureWindow(_ url: URL) {
  1199. // let windowController = KMSecureEncryptWindowController(windowNibName: "KMSecureEncryptWindowController")
  1200. // windowController.documentURL = url
  1201. // windowController.myDocument = self.listView.document
  1202. // self.currentWindowController = windowController
  1203. //
  1204. // windowController.itemClick = { [weak self] index in
  1205. // self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1206. // self?.currentWindowController = nil
  1207. // }
  1208. //
  1209. // windowController.resultCallback = { [weak self] result in
  1210. // let windowController_secure = self?.currentWindowController as! KMSecureEncryptWindowController
  1211. // self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1212. // self?.currentWindowController = nil
  1213. //
  1214. // self?._secureOptions = windowController_secure.options
  1215. // self?.needSave = true
  1216. // self?.recordIsPDFDocumentEdited(type: .setPassword)
  1217. //
  1218. // if (result) {
  1219. // self?.showSecureSuccessTip()
  1220. // self?.recordSaveWatermarkFlag(type: .setPassword)
  1221. // } else {
  1222. // let alert = NSAlert()
  1223. // alert.messageText = NSLocalizedString("Failure", comment: "")
  1224. // alert.runModal()
  1225. // }
  1226. // }
  1227. //
  1228. // self.view.window?.beginSheet(windowController.window!)
  1229. let controller = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
  1230. controller.pdfDocument = self.document
  1231. self.currentWindowController = controller
  1232. controller.batchAction = { [unowned self] controller, files in
  1233. self.view.window?.endSheet((self.currentWindowController.window)!)
  1234. self.currentWindowController = nil
  1235. self.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
  1236. let batchWindowController = KMBatchOperateWindowController.sharedWindowController
  1237. batchWindowController.window?.makeKeyAndOrderFront("")
  1238. let batchOperateFile = KMBatchOperateFile(filePath: files.first?.filePath ?? "")
  1239. batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
  1240. batchWindowController.window?.makeKeyAndOrderFront("")
  1241. }
  1242. controller.doneAction = { [unowned self] controller, options, attribute in
  1243. // let windowController_secure = self.currentWindowController as! KMSecureEncryptWindowController
  1244. let openPanel = NSOpenPanel()
  1245. openPanel.canChooseFiles = false
  1246. openPanel.canChooseDirectories = true
  1247. openPanel.canCreateDirectories = true
  1248. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { (result) in
  1249. if result == NSApplication.ModalResponse.OK {
  1250. for fileURL in openPanel.urls {
  1251. let document = CPDFDocument(url: self.document?.documentURL)
  1252. if document != nil {
  1253. document!.setDocumentAttributes(attribute)
  1254. let path = fileURL.path.stringByAppendingPathComponent(url.lastPathComponent)
  1255. let success = document!.write(to: NSURL(fileURLWithPath: path) as URL, withOptions: options)
  1256. if success {
  1257. self.view.window?.endSheet((self.currentWindowController.window)!)
  1258. self.currentWindowController = nil
  1259. NSWorkspace.shared.activateFileViewerSelecting([fileURL])
  1260. }
  1261. }
  1262. }
  1263. }
  1264. }
  1265. // self._secureOptions = options
  1266. // self.documentAttribute = attribute
  1267. // self.needSave = true
  1268. // self.recordIsPDFDocumentEdited(type: .setPassword)
  1269. //
  1270. // if (options.count != 0) {
  1271. // self.showSecureSuccessTip()
  1272. // self.recordSaveWatermarkFlag(type: .setPassword)
  1273. // } else {
  1274. // let alert = NSAlert()
  1275. // alert.messageText = NSLocalizedString("Failure", comment: "")
  1276. // alert.runModal()
  1277. // }
  1278. }
  1279. controller.cancelAction = { [unowned self] controller in
  1280. self.view.window?.endSheet((self.currentWindowController.window)!)
  1281. self.currentWindowController = nil
  1282. }
  1283. NSWindow.currentWindow().beginSheet(controller.window!)
  1284. }
  1285. // MARK: -
  1286. // MARK: 保存文档
  1287. internal func needSaveDocument() -> Bool {
  1288. if (self.isPDFDocumentEdited) {
  1289. return self.isPDFDocumentEdited
  1290. }
  1291. if (self.needSave) {
  1292. return self.needSave
  1293. }
  1294. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  1295. if (document?.isDocumentEdited == nil || document!.isDocumentEdited == false) {
  1296. return false
  1297. }
  1298. return true
  1299. }
  1300. internal func saveDocument(overlookDocumentIfEdited overlook: Bool = false) {
  1301. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  1302. if (overlook) {
  1303. document?.save(nil)
  1304. return
  1305. }
  1306. if (self.isPDFDocumentEdited) {
  1307. self.clearIsPDFDocumentEdited()
  1308. self.needSave = false
  1309. document?.save(nil)
  1310. return
  1311. }
  1312. if (document?.isDocumentEdited == nil || document!.isDocumentEdited == false) {
  1313. return
  1314. }
  1315. document?.save(nil)
  1316. }
  1317. internal func asyncSaveDocument(overlookDocumentIfEdited overlook: Bool = false, callback:@escaping KMCommonBlock) {
  1318. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  1319. if (overlook) {
  1320. DispatchQueue.main.async {
  1321. document?.save(nil)
  1322. callback()
  1323. }
  1324. return
  1325. }
  1326. if (self.isPDFDocumentEdited) {
  1327. self.clearIsPDFDocumentEdited()
  1328. self.needSave = false
  1329. DispatchQueue.main.async {
  1330. document?.save(nil)
  1331. callback()
  1332. }
  1333. return
  1334. }
  1335. if (document?.isDocumentEdited == nil || document!.isDocumentEdited == false) {
  1336. callback()
  1337. return
  1338. }
  1339. DispatchQueue.main.async {
  1340. document?.save(nil)
  1341. callback()
  1342. }
  1343. }
  1344. internal func saveDocumentWithProgressAlert(callback:@escaping KMCommonBlock) {
  1345. // 显示进度
  1346. self.showProgressWindow(message: NSLocalizedString("Save PDF", comment: ""))
  1347. self.progressController?.maxValue = 3.0
  1348. self.progressController?.increment(by: 1.0)
  1349. // 保存文档
  1350. self.asyncSaveDocument { [unowned self] params in
  1351. // 执行进度 [假进度]
  1352. self.progressController?.increment(by: 1.0)
  1353. self.progressController?.increment(by: 1.0)
  1354. // DispatchQueue.main.async {
  1355. DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
  1356. // 隐藏进度
  1357. self.hiddenProgressWindow()
  1358. // 回调
  1359. callback()
  1360. }
  1361. }
  1362. }
  1363. // MARK: -
  1364. // MARK: 选择 PDFDisplay 模式
  1365. @objc public func selectDisplay(display: KMPDFDisplayType, viewSettingIsReload: Bool = true) {
  1366. let toolModel = self.listView.toolMode
  1367. self.isReadMode = false
  1368. switch display {
  1369. case .singlePage:
  1370. self.listView.setDisplay(.singlePage)
  1371. break
  1372. case .singlePageContinuous:
  1373. self.listView.setDisplay(.singlePageContinuous)
  1374. break
  1375. case .twoUp:
  1376. self.listView.setDisplay(.twoUp)
  1377. break
  1378. case .twoUpContinuous:
  1379. self.listView.setDisplay(.twoUpContinuous)
  1380. break
  1381. case .bookMode:
  1382. self.listView.displaysAsBook = true
  1383. self.listView.displayTwoUp = true
  1384. self.listView.displayDirection = .horizontal
  1385. break
  1386. case .bookContinuous:
  1387. self.listView.displaysAsBook = true
  1388. self.listView.displayTwoUp = true
  1389. self.listView.displayDirection = .vertical
  1390. break
  1391. case .readModel:
  1392. self.openReadModel()
  1393. break
  1394. case .readContinuous:
  1395. self.openReadModel()
  1396. break
  1397. }
  1398. self.listView.layoutDocumentView()
  1399. // if (viewSettingIsReload && self.leftSideViewController.panelSetViewController.isViewLoaded) {
  1400. // self.leftSideViewController.panelSetViewController.reloadListViewModel()
  1401. // }
  1402. if (toolModel == .editPDFToolMode) {
  1403. if self.rightSideViewController.eidtPDFImageProperty != nil {
  1404. self.rightSideViewController.eidtPDFImageProperty?.cancelCutImageAction("")
  1405. self.rightSideViewController.isHidden = true
  1406. self.closeRightPane()
  1407. }
  1408. }
  1409. }
  1410. // MARK: - 选择缩放模式
  1411. @objc public func selectZoom(_ type: KMPDFZoomType) {
  1412. switch type {
  1413. case .width:
  1414. self.listView.autoScales = true
  1415. // self.listView.autoScales = false
  1416. break
  1417. case .fit:
  1418. // self.listView.autoScales = !self.listView.autoScales
  1419. if let pageHeight = self.listView.currentPage()?.size.height, pageHeight > 0 {
  1420. let pdfviewHeight = self.listView.bounds.size.height
  1421. self.listView.scaleFactor = pdfviewHeight/pageHeight
  1422. self.listView.autoScales = false
  1423. }
  1424. break
  1425. case .actualSize:
  1426. if self.listView.scaleFactor != 1.0 {
  1427. self.listView.scaleFactor = 1.0
  1428. self.listView.autoScales = false
  1429. }
  1430. break
  1431. }
  1432. }
  1433. // MARK: - 自动保存
  1434. internal func autoSaveTimeStartOrStopIfNeed() {
  1435. if (KMPreferenceManager.shared.autoSave == false) {
  1436. self.stopAutoSaveTimer()
  1437. return
  1438. }
  1439. self.startAutoSaveTimer(KMPreferenceManager.shared.autoSaveTimeInterval)
  1440. if (self.myDocument == nil) {
  1441. self.stopAutoSaveTimer()
  1442. return
  1443. }
  1444. let browser = self.browserWindowController?.browser
  1445. guard let activeDocument = browser?.activeTabContents() else {
  1446. return
  1447. }
  1448. if (self.myDocument!.isEqual(to: activeDocument) == false) {
  1449. self.pauseAutoSaveTimer()
  1450. return
  1451. }
  1452. }
  1453. private func startAutoSaveTimer(_ interval: TimeInterval) {
  1454. if (self.autoSaveTimer != nil) {
  1455. self.autoSaveTimer?.invalidate()
  1456. self.autoSaveTimer = nil
  1457. }
  1458. self.autoSaveTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] timer in
  1459. DispatchQueue.main.async {
  1460. #if DEBUG
  1461. Swift.debugPrint("文档已自动保存")
  1462. if let _document = self?.myDocument {
  1463. Swift.debugPrint(_document.fileURL as Any)
  1464. }
  1465. #endif
  1466. self?.saveDocument()
  1467. }
  1468. }
  1469. RunLoop.current.add(self.autoSaveTimer!, forMode: .common)
  1470. }
  1471. private func pauseAutoSaveTimer() {
  1472. self.autoSaveTimer?.fireDate = Date.distantFuture
  1473. }
  1474. private func reStartAutoSaveTimer() {
  1475. self.autoSaveTimer?.fireDate = Date()+KMPreferenceManager.shared.autoSaveTimeInterval
  1476. }
  1477. private func needShowRegisterView() {
  1478. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
  1479. if KMLightMemberManager.manager.checkPopupRegister() {
  1480. // Login & Logout
  1481. let window = NSApp.mainWindow ?? self.view.window
  1482. if KMLightMemberManager.manager.isLogin() {
  1483. } else if window != nil {
  1484. var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  1485. if email.count == 0 {
  1486. KMLoginWindowController.show(window: window!, .Batch, .register)
  1487. } else {
  1488. KMLoginWindowController.show(window: window!, .Batch, .login)
  1489. }
  1490. }
  1491. }
  1492. }
  1493. }
  1494. private func stopAutoSaveTimer() {
  1495. self.autoSaveTimer?.invalidate()
  1496. self.autoSaveTimer = nil
  1497. }
  1498. internal func createPdf(index:Int) {
  1499. if index == 1 {
  1500. self.homeVC?.openBlankPage()
  1501. } else if index == 4 {
  1502. self.homeVC?.importFromCamera()
  1503. } else if index == 5 {
  1504. self.homeVC?.importFromScanner()
  1505. } else if index == 3 {
  1506. self.homeVC?.importFromWebPage()
  1507. } else if index == 2 {
  1508. self.homeVC?.newFromImages()
  1509. }
  1510. }
  1511. // MARK: -
  1512. // MARK: Progress
  1513. func showProgressWindow(message: String = "") {
  1514. if (self.progressController != nil) {
  1515. self.hiddenProgressWindow()
  1516. }
  1517. let progress = SKProgressController()
  1518. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  1519. progress.window?.contentView?.wantsLayer = true
  1520. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  1521. progress.progressField.textColor = NSColor.white
  1522. progress.showClose = false
  1523. progress.message = message
  1524. self.progressController = progress
  1525. self.view.window?.beginSheet(progress.window!)
  1526. }
  1527. func hiddenProgressWindow() {
  1528. if (self.progressController != nil) {
  1529. self.view.window?.endSheet((self.progressController?.window)!)
  1530. self.progressController = nil
  1531. }
  1532. }
  1533. // MARK -
  1534. // MARK - Event 监听
  1535. private func addEventMonitor() {
  1536. if (self.eventMonitor != nil) {
  1537. self.removeEventMonitor()
  1538. }
  1539. KMPrint("已添加事件监听")
  1540. self.eventMonitor = NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) { [weak self] event in
  1541. if (event.type == .scrollWheel && event.modifierFlags.contains(.option)) { // Alt + 鼠标滚轮
  1542. self?.listView.magnifyWheel(event)
  1543. return nil
  1544. }
  1545. return event
  1546. }
  1547. }
  1548. func addKeyEventMonitor() {
  1549. if (self.keyEventMonitor != nil) {
  1550. self.removeKeyEventMonitor()
  1551. }
  1552. keyEventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
  1553. // print(event.keyCode)
  1554. if event.keyCode == 53 {
  1555. if let data = self?.interactionMode, data == .presentation { // 幻灯片模式下
  1556. if let data = self?.canExitPresentation(), data {
  1557. self?.browserWindowController?.exitFullscreen()
  1558. }
  1559. return nil
  1560. }
  1561. if self?.listView.toolMode == .editPDFToolMode {
  1562. if self != nil {
  1563. //使用editingSelectionString获取内容文字
  1564. if self!.listView.editingAreas() != nil {
  1565. if self!.listView.editingAreas().count > 0 && self!.listView.isEditable() {
  1566. self!.listView.clearEditingSelectCharItem()
  1567. } else if self!.listView.editingAreas().count > 0 {
  1568. if self?.listView.annotationType == .addImage ||
  1569. self?.listView.annotationType == .addText {
  1570. let textItem = self?.toolbarController.findItem(KMToolbarAddTextEditPDFItemIdentifier)
  1571. let imageItem = self?.toolbarController.findItem(KMToolbarAddImageEditPDFItemIdentifier)
  1572. textItem?.isSelected = false
  1573. imageItem?.isSelected = false
  1574. }
  1575. self?.rightSideViewController.isHidden = true
  1576. self?.listView.endEditIsRemoveBlock(with: self!.listView.editingAreas().first as? CPDFEditArea)
  1577. self?.listView.updateEditing([])
  1578. self?.listView.isEditImage = false
  1579. self?.listView.setNeedsDisplayPageViewFor(self!.listView.currentPage())
  1580. if self?.listView.annotationType == .addImage {
  1581. self?.listView.change([.text, .image])
  1582. }
  1583. self?.listView.annotationType = .editTextImage
  1584. self?.closeRightPane()
  1585. } else if(self?.listView.annotationType == .addImage || self!.listView.annotationType == .addText) {
  1586. if self?.listView.annotationType == .addImage ||
  1587. self?.listView.annotationType == .addText {
  1588. let textItem = self?.toolbarController.findItem(KMToolbarAddTextEditPDFItemIdentifier)
  1589. let imageItem = self?.toolbarController.findItem(KMToolbarAddImageEditPDFItemIdentifier)
  1590. textItem?.isSelected = false
  1591. imageItem?.isSelected = false
  1592. }
  1593. self?.rightSideViewController.isHidden = true
  1594. self?.listView.setShouAddEdit([])
  1595. self?.listView.change([.text, .image])
  1596. self?.listView.annotationType = .editTextImage
  1597. self?.closeRightPane()
  1598. }
  1599. } else {
  1600. if self?.listView.annotationType == .addImage ||
  1601. self?.listView.annotationType == .addText {
  1602. let textItem = self?.toolbarController.findItem(KMToolbarAddTextEditPDFItemIdentifier)
  1603. let imageItem = self?.toolbarController.findItem(KMToolbarAddImageEditPDFItemIdentifier)
  1604. textItem?.isSelected = false
  1605. imageItem?.isSelected = false
  1606. }
  1607. }
  1608. }
  1609. }
  1610. }
  1611. return event
  1612. }
  1613. }
  1614. func removeKeyEventMonitor() {
  1615. if (self.keyEventMonitor != nil) {
  1616. KMPrint("removeKeyEventMonitor 已移除事件监听")
  1617. NSEvent.removeMonitor(self.keyEventMonitor as Any)
  1618. self.keyEventMonitor = nil
  1619. }
  1620. }
  1621. private func removeEventMonitor() {
  1622. if (self.eventMonitor != nil) {
  1623. KMPrint("已移除事件监听")
  1624. NSEvent.removeMonitor(self.eventMonitor as Any)
  1625. self.eventMonitor = nil
  1626. }
  1627. }
  1628. // MARK: -
  1629. // MARK: Tools
  1630. func pdfViewCanHorizontalScroll() -> Bool {
  1631. let scroll = self.listView.scroll()
  1632. if (scroll == nil) {
  1633. return false
  1634. }
  1635. return scroll?.horizontalScroller?.isHidden == nil ? false : !(scroll!.horizontalScroller!.isHidden)
  1636. }
  1637. func pdfViewCanVerticalScroll() -> Bool {
  1638. let scroll = self.listView.scroll()
  1639. if (scroll == nil) {
  1640. return false
  1641. }
  1642. return scroll?.verticalScroller?.isHidden == nil ? false : !(scroll!.verticalScroller!.isHidden)
  1643. }
  1644. // MARK: - Public Methods
  1645. // 清理数据 [eg. 通知]
  1646. public func clearData() {
  1647. self.removeNotifications()
  1648. if (self.listView.spellingTag() > 0) {
  1649. NSSpellChecker.shared.closeSpellDocument(withTag: self.listView.spellingTag())
  1650. }
  1651. }
  1652. public func clearSecureOptions() {
  1653. self._secureOptions = nil
  1654. self.documentAttribute
  1655. }
  1656. public func recordRemoveSecureFlag() {
  1657. self._removeSecureFlag = true
  1658. self.clearSecureOptions()
  1659. self.recordIsPDFDocumentEdited(type: .removePassword)
  1660. self._needSave = true
  1661. }
  1662. public func clearRemoveSecureFlag() {
  1663. self._removeSecureFlag = false
  1664. }
  1665. public func recordSaveWatermarkFlag(type: KMSubscribeWaterMarkType = .none) {
  1666. // km_synchronized(self) {
  1667. // self._saveWatermarkFlag = true
  1668. // }
  1669. //
  1670. // if let _document = self.myDocument as? KMMainDocument {
  1671. // _document.recordTrackEvent(type: type)
  1672. // }
  1673. }
  1674. public func clearSaveWatermarkFlag() {
  1675. km_synchronized(self) {
  1676. self._saveWatermarkFlag = false
  1677. }
  1678. }
  1679. public func recordIsPDFDocumentEdited(type: KMSubscribeWaterMarkType = .none) {
  1680. km_synchronized(self) {
  1681. self._isPDFDocumentEdited = true
  1682. if let _document = self.myDocument {
  1683. KMTools.setDocumentEditedState(document: _document)
  1684. }
  1685. }
  1686. }
  1687. public func clearIsPDFDocumentEdited() {
  1688. km_synchronized(self) {
  1689. self._isPDFDocumentEdited = false
  1690. }
  1691. }
  1692. // MARK: - Noti Actions
  1693. internal func documentDidUnlockNotification(_ sender: Notification) {
  1694. if (self.listView.document != nil && self.listView.document.isEqual(to: sender.object)) {
  1695. self.loadAIIconView()
  1696. if (self.myDocument == nil) {
  1697. return
  1698. }
  1699. if (self.listView.document.allowsPrinting && self.listView.document.allowsCopying) {
  1700. self.hiddenSecureLimitTip()
  1701. }
  1702. if ((self.myDocument as! KMMainDocument).isUnlockFromKeychain || self.isSaveKeyChain == false) {
  1703. return
  1704. }
  1705. let type = KMPreferenceManager.shared.savePasswordType
  1706. if (type == .never) {
  1707. return
  1708. }
  1709. if (type == .always) {
  1710. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  1711. return
  1712. }
  1713. // 保存到钥匙串
  1714. let alert = NSAlert()
  1715. alert.messageText = NSLocalizedString("Remember Password?", comment: "")
  1716. alert.informativeText = NSLocalizedString("Do you want to save this password in your Keychain?", comment: "")
  1717. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  1718. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  1719. if (alert.runModal() == .alertFirstButtonReturn) { // 保存密码
  1720. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  1721. return
  1722. }
  1723. }
  1724. }
  1725. internal func applicationWillTerminateNotification(_ sender: Notification) {
  1726. self.savePageNumberIfNeed()
  1727. self.saveDocument()
  1728. }
  1729. func KMPDFViewCurrentPageDidChangedNotification(_ sender: Notification) {
  1730. if self.isReadMode {
  1731. self.readModelView.currentPageIndex = self.listView.currentPageIndex
  1732. }
  1733. //刷新前一页后一页按钮
  1734. self.updateNextAndPreViousButtonState()
  1735. }
  1736. func CPDFDocumentPageCountChangedNotification(_ sender: Notification) {
  1737. if self.isReadMode {
  1738. self.readModelView.totalPagesCount = Int(self.listView.document.pageCount)
  1739. }
  1740. //刷新前一页后一页按钮
  1741. self.updateNextAndPreViousButtonState()
  1742. }
  1743. func CEditPDFToolModeChangeStateUnkownNotification(_ sender: Notification) {
  1744. var editSelectd = false
  1745. if (self.listView.annotationType == .addText || self.listView.annotationType == .addImage) && self.listView.toolMode == .editPDFToolMode {
  1746. editSelectd = true
  1747. }
  1748. if self.listView.toolMode == .editPDFToolMode {
  1749. if editSelectd {
  1750. self.toolbarController.cancelSelected(KMToolbarAddTextEditPDFItemIdentifier)
  1751. }
  1752. }
  1753. }
  1754. @objc func handlePageChangedNotification(_ sender: Notification) {
  1755. // When the PDFView is changing scale, or when view settings change when switching fullscreen modes,
  1756. // a lot of wrong page change notifications may be send, which we better ignore.
  1757. // Full screen switching and zooming should not change the current page anyway.
  1758. if self.mwcFlags.isSwitchingFullScreen > 0 {
  1759. // if ([pdfView isZooming] || mwcFlags.isSwitchingFullScreen) {
  1760. // [self updatePageNumber];
  1761. // [self updateLeftStatus];
  1762. return
  1763. }
  1764. //
  1765. let page = self.listView.currentPage()
  1766. let pageIndex = page?.pageIndex() ?? 0
  1767. //
  1768. // if ([lastViewedPages count] == 0) {
  1769. // [lastViewedPages addPointer:(void *)pageIndex];
  1770. // } else if ((NSUInteger)[lastViewedPages pointerAtIndex:0] != pageIndex) {
  1771. // [lastViewedPages insertPointer:(void *)pageIndex atIndex:0];
  1772. // if ([lastViewedPages count] > 5)
  1773. // [lastViewedPages setCount:5];
  1774. // }
  1775. self.leftSideViewController.thumb_selectRowIndexsIfNeed(IndexSet(integer: IndexSet.Element(pageIndex)))
  1776. self.leftSideViewController.thumbnailTableView.needsDisplay = true
  1777. self.leftSideViewController.tocOutlineView.needsDisplay = true
  1778. //
  1779. // [self updatePageNumber];
  1780. // [self updatePageLabel];
  1781. //
  1782. // [self updateOutlineSelection];
  1783. // [self updateNoteSelection];
  1784. // [self updateThumbnailSelection];
  1785. //
  1786. // if (beforeMarkedPageIndex != NSNotFound && [[pdfView currentPage] pageIndex] != markedPageIndex)
  1787. // beforeMarkedPageIndex = NSNotFound;
  1788. //
  1789. // [self synchronizeWindowTitleWithDocumentName];
  1790. // [self updateLeftStatus];
  1791. // if ([[NSUserDefaults standardUserDefaults] boolForKey:SKDisplayPageBoundsKey])
  1792. // [self updateRightStatus];
  1793. // if ([self interactionMode] == SKPresentationMode)
  1794. // [[self presentationNotesDocument] setCurrentPage:[[[self presentationNotesDocument] pdfDocument] pageAtIndex:[page pageIndex]]];
  1795. }
  1796. @objc func handleDisplayBoxChangedNotification(_ sender: Notification) {
  1797. self.leftSideViewController.reloadThumbnailDataIfNeed()
  1798. // if ([[NSUserDefaults standardUserDefaults] boolForKey:SKDisplayPageBoundsKey])
  1799. // [self updateRightStatus];
  1800. }
  1801. @objc func willEnterInteractionModeNotification(_ sender: Notification) {
  1802. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1803. return
  1804. }
  1805. let interactionMode = sender.userInfo?[NSWindow.UserInfo.interactionModeKey] as? KMInteractionMode
  1806. if interactionMode == .presentation {
  1807. let backgroundColor = NSColor.black
  1808. let level = UserDefaults.standard.bool(forKey: "SKUseNormalLevelForPresentationKey") ? NSWindow.Level.normal : NSWindow.Level.popUpMenu
  1809. let page = self.listView.currentPage()
  1810. let wasInteractionMode = self.interactionMode
  1811. if wasInteractionMode == .normal {
  1812. self.savedNormalSetup.setDictionary(self.currentPDFSettings() as! [AnyHashable : Any])
  1813. }
  1814. // if findController.view().window() != nil {
  1815. // findController.toggleAboveView(nil, animate: false)
  1816. // }
  1817. if wasInteractionMode == .legacyFullScreen {
  1818. self.enterPresentationMode()
  1819. // updatePresentationOptions(for: self.view.window!)
  1820. self.pdfSplitView.frame = CGRect(x: 0, y: 0, width: CGRectGetWidth(centerContentView.bounds), height: CGRectGetHeight(centerContentView.bounds)-1)
  1821. self.centerContentView.addSubview(pdfSplitView)
  1822. self.listView.frame = (self.view.window?.contentView?.bounds)!
  1823. self.view.window?.contentView?.addSubview(listView)
  1824. self.view.window?.backgroundColor = backgroundColor
  1825. self.view.window?.level = level
  1826. self.listView.layoutDocumentView()
  1827. self.listView.requiresDisplay()
  1828. self.forceSubwindowsOnTop(false)
  1829. self.hideLeftSideWindow()
  1830. self.hideRightSideWindow()
  1831. self.removeBlankingWindows()
  1832. }
  1833. } else {
  1834. KMPrint("2")
  1835. }
  1836. }
  1837. @objc func didEnterInteractionModeNotification(_ sender: Notification) {
  1838. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1839. return
  1840. }
  1841. if self.interactionMode == .presentation {
  1842. // if _isShowToolbar {
  1843. // self.toolbarViewController.hiddenToolbar(true)
  1844. // }
  1845. //
  1846. // if self.pdfView().currentPage()?.isEqual(page) == false {
  1847. // self.pdfView().go(to: page)
  1848. // }
  1849. // pdfView().setInteractionMode(SKPresentationMode)
  1850. self.listView?.layoutDocumentView()
  1851. self.listView?.requiresDisplay()
  1852. }
  1853. }
  1854. @objc func willShowFullScreenNotification(_ sender: Notification) {
  1855. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1856. return
  1857. }
  1858. if self.interactionMode == .presentation {
  1859. let view = self.view.window?.firstResponder as? NSView
  1860. if let data = view?.isDescendant(of: self.pdfSplitView), data {
  1861. self.view.window?.makeFirstResponder(nil)
  1862. }
  1863. }
  1864. }
  1865. @objc func didShowFullScreenNotification(_ sender: Notification) {
  1866. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1867. return
  1868. }
  1869. if self.interactionMode == .presentation {
  1870. self.enterPresentationMode()
  1871. }
  1872. }
  1873. @objc func didAddContentViewNotification(_ sender: Notification) {
  1874. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1875. return
  1876. }
  1877. if self.interactionMode == .presentation {
  1878. }
  1879. }
  1880. //MARK: - PDFListViewDelegate
  1881. func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
  1882. // KMPrint("pdfViewDocumentDidLoaded")
  1883. self.removeBackgroundMaskView()
  1884. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  1885. self.showSecureLimitTip()
  1886. }
  1887. if (self._documentFirstLoad) {
  1888. self.checkShouldAutoOpenLeftVC()
  1889. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  1890. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document.documentURL.path)
  1891. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document.documentURL.path)
  1892. if (pageScale != nil) {
  1893. self.listView.scaleFactor = CGFloat(pageScale!)
  1894. }
  1895. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < self.listView.document.pageCount) {
  1896. self.listView.go(toPageIndex: pageNumber!, animated: false)
  1897. } else {
  1898. self._goToFirstPageForFristAppear()
  1899. }
  1900. } else {
  1901. self._goToFirstPageForFristAppear()
  1902. }
  1903. self._documentFirstLoad = false
  1904. }
  1905. let notification = Notification(name: Notification.Name(rawValue: "pdfViewDocumentDidLoaded"))
  1906. self.preferenceDidChangeNotification(notification:notification)
  1907. let leftWidthNumber = UserDefaults.standard.object(forKey: CPDFOfficeLeftSidePaneWidthKey) as? NSNumber ?? NSNumber(value: panelWidth + functionWidth)
  1908. let rightWidthNumber = UserDefaults.standard.object(forKey: CPDFOfficeRightSidePaneWidthKey) as? NSNumber ?? NSNumber(value: defaultRightWidth)
  1909. applyLeftSideWidth(leftWidthNumber.doubleValue, rightSideWidth: rightWidthNumber.doubleValue)
  1910. self.updatePageIndicatoreType()
  1911. }
  1912. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  1913. self.updatePageIndicatoreType()
  1914. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: self.document)
  1915. // KMPrint("KMPDFViewCurrentPageDidChanged")
  1916. }
  1917. func pdfViewScaleDidChanged(_ pdfView: CPDFView!) {
  1918. self.toolbarController.mainToolBarView?.zoomTextField.stringValue = "\(Int(self.listView.scaleFactor * 100))%"
  1919. self.updateZoomInOutButtonState()
  1920. // KMPrint("pdfViewScaleDidChanged")
  1921. }
  1922. func pdfViewDidClick(onLink pdfView: CPDFView!, withURL url: String!) {
  1923. if let urlString = url, urlString == kKMPurchaseProductURLString {
  1924. //跳转订阅比较表
  1925. let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
  1926. return
  1927. }
  1928. KMTools.openURL(urlString: url)
  1929. }
  1930. func pdfViewPerformURL(_ pdfView: CPDFView!, withContent content: String!) {
  1931. KMPrint("pdfViewPerformURL")
  1932. }
  1933. func pdfViewPerformPrint(_ pdfView: CPDFView!) {
  1934. KMPrint("pdfViewPerformPrint")
  1935. }
  1936. func pdfViewPerformGo(toPage pdfView: CPDFView!) {
  1937. KMPrint("pdfViewPerformGo")
  1938. }
  1939. func pdfViewOpenPDF(_ pdfView: CPDFView!, forRemoteGoTo action: CPDFAction!) {
  1940. KMPrint("pdfViewOpenPDF")
  1941. }
  1942. func pdfViewPerformReset(_ pdfView: CPDFView!) {
  1943. KMPrint("pdfViewPerformReset")
  1944. }
  1945. func pdfViewEditingBlockDidChanged(_ pdfView: CPDFView!) {
  1946. KMPrint("pdfViewEditingBlockDidChanged")
  1947. }
  1948. func pdfViewAsBookBookmark() -> NSImage! {
  1949. return NSImage(named: "KMImageNameUXIconPDFViewBookMark")!
  1950. }
  1951. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  1952. self.recordSaveWatermarkFlag()
  1953. if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddText {
  1954. self.rightSideViewController.eidtPDFTextProperty.reloadData()
  1955. self.rightSideViewController.eidtPDFTextProperty.updateTextTextPresuppositionState()
  1956. }
  1957. }
  1958. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  1959. let areas = self.listView.editingAreas()
  1960. if areas == nil || areas?.count ?? 0 == 0 {
  1961. if self.listView.toolMode == .editPDFToolMode {
  1962. if self.listView.annotationType == .addImage || self.listView.annotationType == .addText {
  1963. if self.listView.isEditImage {
  1964. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  1965. } else {
  1966. // if self.listView.annotationType == .addImage {
  1967. // self.closeRightPane()
  1968. // }
  1969. if self.listView.annotationType == .addImage {
  1970. if self.rightSideViewController.eidtPDFImageProperty != nil {
  1971. self.rightSideViewController.eidtPDFImageProperty.reloadData()
  1972. }
  1973. }
  1974. // self.openRightPane()
  1975. }
  1976. } else {
  1977. self.closeRightPane()
  1978. }
  1979. } else {
  1980. self.rightSideViewController.isHidden = true
  1981. self.closeRightPane()
  1982. if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddText && self.listView.annotationType == .addText {
  1983. self.rightSideViewController.eidtPDFTextProperty.initData()
  1984. }
  1985. }
  1986. if self.listView.isEdited() {
  1987. self.recordIsPDFDocumentEdited(type: .editText)
  1988. }
  1989. if self.listView.annotationType != .addText {
  1990. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kPDFViewEditingAreaDidChanged"), object: self.listView.document)
  1991. }
  1992. return
  1993. }
  1994. self.isPDFTextImageEdited = true
  1995. if (self.listView.annotationType == .addImage) && areas!.count > 0 {
  1996. var isImageArea = false
  1997. for i in 0 ... areas!.count-1 {
  1998. if areas![i] is CPDFEditImageArea {
  1999. isImageArea = true
  2000. }
  2001. }
  2002. if isImageArea {
  2003. // self.rightSideViewController.view.isHidden = false
  2004. self.rightSideViewController.isHidden = false
  2005. if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddImage {
  2006. self.rightSideViewController.subViewType = .EditPDFAddImage
  2007. self.rightSideViewController.eidtPDFImageProperty.reloadData()
  2008. }
  2009. self.openRightPane()
  2010. } else {
  2011. // self.rightSideViewController.view.isHidden = true
  2012. self.rightSideViewController.isHidden = true
  2013. self.closeRightPane()
  2014. }
  2015. } else if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddText && self.listView.annotationType == .addText {
  2016. // self.rightSideViewController.view.isHidden = false
  2017. self.rightSideViewController.isHidden = false
  2018. if self.listView.editingSelectionString().count != 0 {
  2019. self.rightSideViewController.eidtPDFTextProperty.reloadData()
  2020. } else {
  2021. self.rightSideViewController.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  2022. }
  2023. self.openRightPane()
  2024. } else {
  2025. var textsAreas : [CPDFEditTextArea] = []
  2026. var imagesAreas : [CPDFEditImageArea] = []
  2027. if self.listView.editingAreas()?.count ?? 0 < 1 {
  2028. return
  2029. }
  2030. for i in 0 ... areas!.count-1 {
  2031. if areas![i] is CPDFEditTextArea {
  2032. textsAreas.append(areas![i] as! CPDFEditTextArea)
  2033. }
  2034. if areas![i] is CPDFEditImageArea {
  2035. imagesAreas.append(areas![i] as! CPDFEditImageArea)
  2036. }
  2037. }
  2038. if textsAreas.count > 0 && textsAreas.count == areas!.count {
  2039. // self.rightSideViewController.view.isHidden = false
  2040. self.rightSideViewController.isHidden = false
  2041. self.rightSideViewController.subViewType = .EditPDFAddText
  2042. self.rightSideViewController.eidtPDFTextProperty?.reloadData()
  2043. self.openRightPane()
  2044. } else if imagesAreas.count > 0 {
  2045. // self.rightSideViewController.view.isHidden = false
  2046. self.rightSideViewController.isHidden = false
  2047. self.rightSideViewController.subViewType = .EditPDFAddImage
  2048. self.rightSideViewController.eidtPDFImageProperty?.reloadData()
  2049. self.openRightPane()
  2050. }
  2051. }
  2052. if self.listView.isEdited() {
  2053. self.recordIsPDFDocumentEdited(type: .editText)
  2054. if self.listView.annotationType != .addText {
  2055. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kPDFViewEditingAreaDidChanged"), object: self.listView.document)
  2056. }
  2057. }
  2058. }
  2059. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  2060. self.recordSaveWatermarkFlag()
  2061. if editArea != nil && (editArea is CPDFEditImageArea){
  2062. self.listView.cropAreas = editArea as? CPDFEditImageArea
  2063. }
  2064. }
  2065. //编辑PDF 创建图片区域回调
  2066. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  2067. self.recordSaveWatermarkFlag(type: .editImage)
  2068. if self.listView.isEditImage {
  2069. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  2070. } else {
  2071. let panel = NSOpenPanel()
  2072. panel.allowsMultipleSelection = false
  2073. panel.allowedFileTypes = ["png","jpg"]
  2074. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  2075. if response == .OK {
  2076. var filePath = panel.url?.path
  2077. var image = NSImage.init(contentsOf: panel.url!)
  2078. //图片自适应范围
  2079. if image != nil {
  2080. var imageRect = rect
  2081. let imageSize = image!.size
  2082. var previewSize = rect.size
  2083. var isChangeSize = false
  2084. if previewSize.width == 0 && previewSize.height == 0 {
  2085. previewSize = CGSize(width: 500, height: 500)
  2086. isChangeSize = true
  2087. }
  2088. let scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  2089. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  2090. if isChangeSize {
  2091. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  2092. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  2093. } else {
  2094. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  2095. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  2096. }
  2097. imageRect.size = newSize
  2098. let limitWidth = 1920.0
  2099. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  2100. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  2101. targetSize: CGSize(width: limitWidth, height: limitWidth),
  2102. maxSizeInBytes: 1024 * 1024 * 5,
  2103. targetCompression: 1.0)
  2104. }
  2105. //自适应page
  2106. let pageRect = self.listView.currentPage().bounds
  2107. if imageRect.width > pageRect.width ||
  2108. imageRect.height > pageRect.height {
  2109. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  2110. imageRect = CGRect(x: imageRect.origin.x,
  2111. y: imageRect.origin.y,
  2112. width: imageRect.width * pageScale,
  2113. height: imageRect.height * pageScale)
  2114. }
  2115. if imageRect.origin.x < 0 {
  2116. imageRect.origin.x = 5
  2117. }
  2118. if imageRect.origin.y < 0 {
  2119. imageRect.origin.y = 5
  2120. }
  2121. if imageRect.origin.x + imageRect.width > pageRect.width ||
  2122. imageRect.origin.y + imageRect.height > pageRect.height {
  2123. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  2124. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  2125. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  2126. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  2127. }
  2128. DispatchQueue.main.async {
  2129. self.listView.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  2130. self.isPDFTextImageEdited = true
  2131. self.recordIsPDFDocumentEdited(type: .editImage)
  2132. // self.asyncSaveDocument { params in
  2133. //
  2134. // }
  2135. }
  2136. }
  2137. }
  2138. }
  2139. }
  2140. }
  2141. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  2142. // print(rect)
  2143. var newrect = CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height)
  2144. if __CGSizeEqualToSize(rect.size, CGSize.zero) {
  2145. newrect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  2146. } else {
  2147. newrect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  2148. }
  2149. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  2150. let fontName = KMEditPDFTextManager.manager.fetchFontName(fontName: model.fontName)
  2151. let fontSize = model.fontSize
  2152. let fontColor = model.color
  2153. let fontAlign = model.alignment
  2154. let fontStyle = KMEditPDFTextManager.manager.fetchFontStyle(fontName: model.fontName)
  2155. NSColorPanel.shared.color = fontColor
  2156. let font = KMEditPDFTextManager.manager.fetchFont(fontName: fontName, style: fontStyle, size: fontSize)
  2157. let style = NSMutableParagraphStyle()
  2158. style.alignment = fontAlign
  2159. let attributes = [NSAttributedString.Key.font:font, NSAttributedString.Key.foregroundColor:fontColor,NSAttributedString.Key.paragraphStyle:style] as [NSAttributedString.Key : Any]
  2160. self.listView.createEmptyStringBounds(newrect,withAttributes: attributes as [NSAttributedString.Key : Any], page: page)
  2161. if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddText && self.listView.annotationType == .addText {
  2162. self.rightSideViewController.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  2163. }
  2164. // self.asyncSaveDocument { params in
  2165. //
  2166. // }
  2167. self.recordSaveWatermarkFlag(type: .editText)
  2168. }
  2169. // func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  2170. // debugPrint("pdfViewEditingDoubleClick")
  2171. // }
  2172. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  2173. debugPrint("pdfViewEditingOperationDidChanged")
  2174. if self.listView.isEdited() {
  2175. self.recordSaveWatermarkFlag()
  2176. }
  2177. }
  2178. // func pdfListViewEditAnnotation(_ pdfListView: CPDFListView!, for anotation: CPDFAnnotation!) {
  2179. // debugPrint("pdfListViewEditAnnotation")
  2180. // }
  2181. func pdfListViewKeyDownIsContinue(_ pdfListView: CPDFListView!, theEvent: NSEvent!) -> Bool {
  2182. let command = theEvent.modifierFlags.contains(.command)
  2183. let control = theEvent.modifierFlags.contains(.control)
  2184. KMPrint(theEvent.keyCode)
  2185. if (theEvent.keyCode == 11 && command) { // command + B [添加书签]
  2186. self.menuItemBookMarkClick_add(sender: NSMenuItem())
  2187. return false
  2188. } else if (command && control && theEvent.keyCode == 14) { // command + control + E [注释 橡皮擦]
  2189. return false
  2190. } else if (theEvent.keyCode == 123) { // 向左
  2191. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2192. return false
  2193. } else {
  2194. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage()) {
  2195. self.listView.goToPreviousPage(nil)
  2196. return false
  2197. }
  2198. }
  2199. } else if (theEvent.keyCode == 126) { // 向上
  2200. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2201. return false
  2202. } else {
  2203. if (self.listView.isContinousScroll()) {
  2204. return true
  2205. }
  2206. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToPreviousPage()) {
  2207. self.listView.goToPreviousPage(nil)
  2208. return false
  2209. }
  2210. }
  2211. } else if (theEvent.keyCode == 124) { // 向右
  2212. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2213. return false
  2214. } else {
  2215. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage()) {
  2216. self.listView.goToNextPage(nil)
  2217. return false
  2218. }
  2219. }
  2220. } else if (theEvent.keyCode == 125) { // 向下
  2221. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2222. return false
  2223. } else {
  2224. if (self.listView.isContinousScroll()) {
  2225. return true
  2226. }
  2227. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToNextPage()) {
  2228. self.listView.goToNextPage(nil)
  2229. return false
  2230. }
  2231. }
  2232. } else if (theEvent.keyCode == 36) {
  2233. if self.listView.annotationType == .addImage || self.listView.annotationType == .addText {
  2234. if self.listView.isEditImage {
  2235. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  2236. }
  2237. }
  2238. }
  2239. if theEvent.keyCode == 53 {
  2240. if self.isReadMode {
  2241. self.closeReadModel()
  2242. }
  2243. self.leftSideViewCancelSelect()
  2244. if (self.toolbarController.toolbarType.isToolMode()) {
  2245. self.toolbarController.selectItem(self.toolbarController.toolbarType.itemIdentifier())
  2246. }
  2247. }
  2248. return true
  2249. }
  2250. func pdfListViewMenuValidate(_ pdfListView: CPDFListView!, menuItem: NSMenuItem!, isTakesEffect: UnsafeMutablePointer<ObjCBool>!) -> Bool {
  2251. guard let action = menuItem.action else {
  2252. isTakesEffect.pointee = false
  2253. return false
  2254. }
  2255. if (KMSystemMenu.isEditSelector(sel: action)) {
  2256. if (KMSystemMenu.Edit.deleteSelector == action) {
  2257. isTakesEffect.pointee = true
  2258. return self.listView.activeAnnotations.count > 0
  2259. } else if (KMSystemMenu.Edit.copySelector == action) {
  2260. isTakesEffect.pointee = true
  2261. return true//self.listView.canCopy()
  2262. } else if (KMSystemMenu.Edit.cutSelector == action) {
  2263. isTakesEffect.pointee = true
  2264. return self.listView.canCopy()
  2265. } else if (KMSystemMenu.Edit.pasteSelector == action) {
  2266. isTakesEffect.pointee = true
  2267. return self.listView.canPaste()
  2268. }
  2269. }
  2270. isTakesEffect.pointee = false
  2271. return false
  2272. }
  2273. //MARK: -CPDFListViewDelegate
  2274. func cPDFListView(_ pdfListView: CPDFListView, didDelete annotation: CPDFAnnotation, in pdfPage: CPDFPage) {
  2275. self.leftSideViewController.updateThumbnail(at: Int(pdfPage.pageIndex()))
  2276. }
  2277. func pdfListViewChangeatioActiveAnnotations(_ pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!, isRightMenu: Bool) {
  2278. self.view.window?.makeFirstResponder(self.listView)
  2279. if isRightMenu {
  2280. } else if annotations.count > 0 {
  2281. if annotations.count > 1 {
  2282. let fristAnnotation = annotations.first
  2283. var isSameAnnotation = true
  2284. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  2285. for annotation in annotations {
  2286. let cunrrentClassName = NSStringFromClass(annotation.classForCoder)
  2287. if (className == "CPDFSquareAnnotation") ||
  2288. (className == "CPDFCircleAnnotation") ||
  2289. (className == "CPDFLineAnnotation") {
  2290. if (cunrrentClassName != "CPDFSquareAnnotation") &&
  2291. (cunrrentClassName != "CPDFCircleAnnotation") &&
  2292. (cunrrentClassName != "CPDFLineAnnotation") {
  2293. isSameAnnotation = false
  2294. }
  2295. } else {
  2296. if className != cunrrentClassName {
  2297. isSameAnnotation = false
  2298. }
  2299. }
  2300. }
  2301. if isSameAnnotation == false {
  2302. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  2303. // self.closeRightPane()
  2304. } else {
  2305. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  2306. self.openRightPane()
  2307. }
  2308. } else {
  2309. let fristAnnotation = annotations.first
  2310. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  2311. if self.isReadMode {
  2312. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  2313. self.closeRightPane()
  2314. } else {
  2315. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  2316. if className != "CPDFStampAnnotation" &&
  2317. className != "CPDFSignatureAnnotation" &&
  2318. className != "CPDFListStampAnnotation" {
  2319. self.openRightPane()
  2320. }
  2321. }
  2322. }
  2323. } else if (annotations.count == 0){
  2324. if pdfListView.annotationType == .unkown {
  2325. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  2326. self.closeRightPane()
  2327. } else {
  2328. if self.isReadMode {
  2329. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  2330. self.closeRightPane()
  2331. } else {
  2332. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  2333. self.openRightPane()
  2334. }
  2335. }
  2336. }
  2337. }
  2338. func pdfListViewChangedAnnotationType(_ pdfListView: CPDFListView!, for annotationType: CAnnotationType) {
  2339. if(annotationType == .unkown) {
  2340. // self.rightSideViewController.view.isHidden = true
  2341. self.rightSideViewController.isHidden = true
  2342. self.closeRightPane()
  2343. }
  2344. }
  2345. ///开始定位链接注释
  2346. func pdfListViewLinkDestinationStart(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  2347. if self.locationPageView.superview == nil {
  2348. self.locationPageView.frame = CGRect(x: 0, y: pdfListView.frame.maxY-32, width: pdfListView.frame.width, height: 32)
  2349. pdfListView.addSubview(self.locationPageView)
  2350. }
  2351. }
  2352. ///刷新链接注释
  2353. func pdfListViewLinkDestinationEnd(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  2354. if self.locationPageView.superview != nil {
  2355. self.locationPageView.removeFromSuperview()
  2356. }
  2357. if self.rightSideViewController.subViewType == .AnnotationProperts && pdfListView.annotationType == .link {
  2358. self.rightSideViewController.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  2359. }
  2360. }
  2361. func pdfListViewMenuItemsEditing(at point: CGPoint, for page: CPDFPage!, menuItems: [NSMenuItem]!) -> [NSMenuItem]! {
  2362. if (listView.toolMode != CToolMode.editPDFToolMode) {
  2363. return menuItems
  2364. }
  2365. var tMenuItems = menuItems;
  2366. if(listView.isSelectEditCharRange() ||
  2367. listView.isSelecteditArea(with: point)) {
  2368. tMenuItems?.append(NSMenuItem.separator())
  2369. // tMenuItems?.append(self.fontColorMenuItem())
  2370. // tMenuItems?.append(self.fontSizeMenuItem())
  2371. }
  2372. let areas = self.listView.editingAreas() ?? []
  2373. if areas.count == 1 {
  2374. let fristAreas = areas.first
  2375. if fristAreas is CPDFEditImageArea {
  2376. self.listView.selectImageAreas = fristAreas as? CPDFEditImageArea
  2377. if self.listView.isEditImage {
  2378. tMenuItems?.removeAll()
  2379. tMenuItems?.append(self.corpImageMenuItem())
  2380. tMenuItems?.append(self.cancelCorpImageMenuItem())
  2381. tMenuItems?.append(self.restoreCorpImageMenuItem())
  2382. } else {
  2383. tMenuItems?.append(NSMenuItem.separator())
  2384. tMenuItems?.append(self.cutImageArea())
  2385. tMenuItems?.append(self.replaceImageArea())
  2386. tMenuItems?.append(self.exportImageArea())
  2387. }
  2388. } else {
  2389. tMenuItems?.swapAt(0, 1)
  2390. }
  2391. } else if areas.count == 0 {
  2392. tMenuItems?.append(NSMenuItem.separator())
  2393. tMenuItems?.append(self.addText())
  2394. tMenuItems?.append(self.addImage())
  2395. }
  2396. return tMenuItems
  2397. }
  2398. func tableMenu(_ menu: NSMenu, withTable table: KMTableAnnotation, point: CGPoint) -> NSMenu {
  2399. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  2400. let itemTitles = ["Edit", "", "Add Row Above", "Add Row Below", "", "Add Column Before", "Add Column After", "", "Delete Row", "Delete Column", "Delete Table", "Cut", "Copy", "Paste", "Paste and Match Style", "Delete Cell Contents", "Clear All"]
  2401. let actions = ["formAnnotTextEdit:", "", "addRowAbove:", "addRowBelow:", "", "addColumnBefore:", "addColumnAfter:", "", "deleteRow:", "deleteColumn:", "deleteTabel", "cutCell:", "copyCell:", "pasteCell:", "pasteAndMatchStyle:", "deleteCellContents:", "clearAll:"]
  2402. for i in 0..<itemTitles.count {
  2403. var item: NSMenuItem? = nil
  2404. if itemTitles[i] == "" {
  2405. item = NSMenuItem.separator()
  2406. menu.insertItem(item!, at: i)
  2407. } else {
  2408. item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  2409. item!.target = self
  2410. item!.action = NSSelectorFromString(actions[i])
  2411. if itemTitles[i] == "Paste" /*&& !_copyCellData*/ {
  2412. item!.action = nil
  2413. } else if itemTitles[i] == "Paste and Match Style" /*&& !_copyCellData */{
  2414. item!.action = nil
  2415. } else if itemTitles[i] == "Add Row Above" {
  2416. let path1 = table.crossLines[table.rowNumber - table.currentCell.row]
  2417. let path2 = table.crossLines[table.rowNumber - table.currentCell.row - 1]
  2418. if path1.lineJoinStyle == NSBezierPath.LineJoinStyle.round && table.headerCount() >= 5 {
  2419. item!.action = nil
  2420. } else if path2.lineJoinStyle == NSBezierPath.LineJoinStyle.bevel && table.footerCount() >= 5 {
  2421. item!.action = nil
  2422. }
  2423. }
  2424. item!.title = NSLocalizedString(item!.title, comment: "")
  2425. item!.representedObject = NSValue(point: point)
  2426. menu.insertItem(item!, at: i)
  2427. }
  2428. }
  2429. } else {
  2430. let itemTitles = ["Cut", "Copy", "Paste", "Delete"]
  2431. let actions = ["cut:", "copy:", "paste:", "delete:"]
  2432. for i in 0..<itemTitles.count {
  2433. let item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  2434. item.target = self
  2435. item.action = NSSelectorFromString(actions[i])
  2436. item.title = NSLocalizedString(item.title, comment: "")
  2437. menu.insertItem(item, at: i)
  2438. item.representedObject = NSValue(point: point)
  2439. }
  2440. }
  2441. return menu
  2442. }
  2443. func pdfListViewMenu(forEvent pdfListView: CPDFListView!, for theEvent: NSEvent!, click menu: AutoreleasingUnsafeMutablePointer<NSMenu?>!, isMoveSelectAnno: Bool) {
  2444. self.mouseRightMenuEvent = theEvent
  2445. var currentMenu : NSMenu = menu.pointee!
  2446. if let activeAnno = listView.activeAnnotation as? KMTableAnnotation {//Table
  2447. var pagePoint = NSPoint()
  2448. _ = self.listView.pageAndPoint(&pagePoint, for: theEvent, nearest: true)
  2449. currentMenu.removeAllItems()
  2450. let annotation = activeAnno
  2451. annotation.completeEditCellText()
  2452. if !(NSIsEmptyRect(annotation.drawRect)) {
  2453. annotation.drawLine(point: pagePoint)
  2454. NotificationCenter.default.post(name: NSNotification.Name.KMPDFViewTableAnnotationDidChange, object: self, userInfo: ["point": NSValue(point: pagePoint)])
  2455. }
  2456. if (annotation.rowNumber - annotation.currentCell.row - 1) < 0 {
  2457. return
  2458. }
  2459. currentMenu = tableMenu(currentMenu, withTable: listView.activeAnnotation as! KMTableAnnotation, point: pagePoint)
  2460. listView.needsDisplay = true
  2461. return
  2462. }
  2463. if (listView.toolMode == .selectToolMode){
  2464. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2465. currentMenu.insertItem(self.printingMenu(), at: 3)
  2466. currentMenu.insertItem(self.setTTSStype(), at: 3)
  2467. currentMenu.insertItem(self.setCropStype(), at: 3)
  2468. currentMenu.insertItem(self.setSnapshotStype(), at: 3)
  2469. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  2470. export.submenu = self.exportMenu()
  2471. currentMenu.insertItem(export, at: 3)
  2472. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2473. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  2474. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2475. return
  2476. }
  2477. if (listView.toolMode == .moveToolMode || listView.toolMode == .magnifyToolMode){
  2478. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2479. currentMenu.insertItem(self.setTTSStype(), at: 0)
  2480. currentMenu.insertItem(self.setCropStype(), at: 0)
  2481. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  2482. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2483. currentMenu.insertItem(self.addOutlineStype(), at: 0)
  2484. currentMenu.insertItem(self.addBookmarkMenu(), at: 0)
  2485. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2486. currentMenu.insertItem(self.setAnnotationToolStype(), at: 0)
  2487. return
  2488. }
  2489. if currentMenu.items.count > 3 {
  2490. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 3)
  2491. currentMenu.insertItem(self.addReadModelStype(), at: currentMenu.items.count - 3)
  2492. }
  2493. if listView.currentSelection != nil{
  2494. if listView.currentSelection.selectionType() == .text {
  2495. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2496. currentMenu.insertItem(self.setSearchBaiduStype(), at: 3)
  2497. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2498. currentMenu.insertItem(self.setLookUpStype(), at: 3)
  2499. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2500. currentMenu.insertItem(self.addOutlineStype(), at: 3)
  2501. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2502. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  2503. currentMenu.insertItem(self.setTTSStype(), at: 3)
  2504. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2505. currentMenu.insertItem(self.setShareStype(), at: 3)
  2506. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2507. }
  2508. currentMenu.insertItem(self.enterAnnotationStype(), at: 3)
  2509. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2510. if listView.currentSelection.selectionType() == .image{
  2511. currentMenu.insertItem(self.setCutStype(), at: 1)
  2512. currentMenu.insertItem(self.setDeleteStype(), at: 4)
  2513. currentMenu.insertItem(NSMenuItem.separator(), at: 6)
  2514. currentMenu.insertItem(self.setEditNoteStype(), at: 6)
  2515. currentMenu.insertItem(self.setRotateStype(), at: 6)
  2516. currentMenu.insertItem(self.setLinesStype(), at: 6)
  2517. currentMenu.insertItem(self.setColorsStype(), at: 6)
  2518. }
  2519. if listView.currentSelection.selectionType() == .text {
  2520. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2521. currentMenu.insertItem(self.setTranslateStype(), at: currentMenu.items.count)
  2522. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2523. // currentMenu.insertItem(self.setServicesStype(), at: currentMenu.items.count)
  2524. }
  2525. }
  2526. if listView.activeAnnotation != nil || isMoveSelectAnno {
  2527. if let data = self.listView?.activeAnnotation?.type?.lowercased(), data == "stamp"{
  2528. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2529. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  2530. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2531. }else{
  2532. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2533. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  2534. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2535. currentMenu.insertItem(self.setAnnotationToolStype(), at: currentMenu.items.count - 15)
  2536. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2537. currentMenu.insertItem(self.addBookmarkMenu(), at: currentMenu.items.count - 15)
  2538. currentMenu.insertItem(self.addOutlineStype(), at: currentMenu.items.count - 15)
  2539. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2540. currentMenu.insertItem(self.setSnapshotStype(), at: currentMenu.items.count - 15)
  2541. currentMenu.insertItem(self.setCropStype(), at: currentMenu.items.count - 15)
  2542. currentMenu.insertItem(self.setTTSStype(), at: currentMenu.items.count - 15)
  2543. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2544. }
  2545. }
  2546. if listView.activeAnnotation == nil && listView.currentSelection == nil{
  2547. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2548. if(listView.toolMode == .selectToolMode) {
  2549. if NSIsEmptyRect(listView.currentSelectionRect()) {
  2550. currentMenu.insertItem(self.zoomSelectionMenuItem(), at: 0)
  2551. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2552. }
  2553. currentMenu.insertItem(self.printingMenu(), at: 0)
  2554. currentMenu.insertItem(self.setTTSStype(), at: 0)
  2555. currentMenu.insertItem(self.setCropStype(), at: 0)
  2556. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  2557. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  2558. export.submenu = self.exportMenu()
  2559. currentMenu.insertItem(export, at: currentMenu.items.count)
  2560. }else{
  2561. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2562. currentMenu.insertItem(self.setTTSStype(), at: 2)
  2563. currentMenu.insertItem(self.setCropStype(), at: 2)
  2564. currentMenu.insertItem(self.setSnapshotStype(), at: 2)
  2565. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2566. currentMenu.insertItem(self.addOutlineStype(), at: 2)
  2567. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2568. currentMenu.insertItem(self.enterAnnotationStype(), at: 2)
  2569. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2570. if(currentMenu.items.count > 4) {
  2571. currentMenu.insertItem(NSMenuItem.separator(), at: 5)
  2572. }
  2573. if(currentMenu.items.count > 5) {
  2574. currentMenu.insertItem(self.addBookmarkMenu(), at: 6)
  2575. }
  2576. // if (currentMenu.items.count > 6 && self.isReadMode) {
  2577. // currentMenu.removeItem(currentMenu.item(withTitle: "Zoom")!)
  2578. // }
  2579. // currentMenu.insertItem(self.findStringMenu(), at: currentMenu.items.count)
  2580. currentMenu.insertItem(self.setAutoScrollStype(), at: currentMenu.items.count)
  2581. }
  2582. currentMenu.insertItem(self.setAnnotationToolStype(), at: 5)
  2583. }
  2584. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2585. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  2586. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  2587. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  2588. for item in currentMenu.items {
  2589. if (item.action == NSSelectorFromString("menuItemClick_HidenorShowNote:")) {
  2590. // 显示与隐藏注释 item action 截取
  2591. item.action = #selector(menuItemClick_HidenorShowNote)
  2592. item.target = self
  2593. break
  2594. }
  2595. }
  2596. }
  2597. func pdfListViewAddAnnotations(_ pdfListView: CPDFListView!, forAdd annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  2598. var addRedact = false
  2599. var saveWatermark = false
  2600. var saveWatermarkType: KMSubscribeWaterMarkType = .none
  2601. for anno in annotations {
  2602. if (anno.isKind(of: CPDFRedactAnnotation.self)) {
  2603. addRedact = true
  2604. // break
  2605. } else if (anno.isKind(of: CPDFLinkAnnotation.self)) { // link 注释
  2606. saveWatermark = true
  2607. saveWatermarkType = .link
  2608. } else if (anno.isKind(of: CPDFListStampAnnotation.self)) { // 图章注释
  2609. saveWatermark = true
  2610. saveWatermarkType = .stamp
  2611. } else if (anno.isKind(of: CPDFListSignatureAnnotation.self)) { // 签名注释
  2612. saveWatermark = true
  2613. saveWatermarkType = .sign
  2614. }
  2615. }
  2616. if (saveWatermark) {
  2617. self.recordSaveWatermarkFlag(type: saveWatermarkType)
  2618. }
  2619. self.hasAddRedact = addRedact
  2620. if self.isReadMode || self.listView.toolMode == .moveToolMode {
  2621. self.listView.toolMode = .textToolMode
  2622. self.listView.annotationType = .unkown
  2623. self.toolbarController.toolbarType = .Annatiton
  2624. }
  2625. if (self.rightMouseEventing) {
  2626. self.rightMouseEventing = false
  2627. if (self.toolbarController.ignoreCurrentAnnotationTypeChange && self.listView.annotationType == .ink) {
  2628. self.listView.toolMode = .textToolMode
  2629. self.listView.annotationType = .unkown
  2630. }
  2631. }
  2632. self.toolbarController.ignoreCurrentAnnotationTypeChange = false
  2633. self.leftSideViewController.updateThumbnail(at: Int(pdfPage?.pageIndex() ?? 0))
  2634. }
  2635. func pdfListViewDidSelectionEnd(_ pdfListView: CPDFListView!) {
  2636. if (!self.listView.isEqual(to: pdfListView)) {
  2637. return
  2638. }
  2639. if (self.listView.toolMode != .selectToolMode) {
  2640. return
  2641. }
  2642. if (self.topTipBox.isHidden || self.topTipBox.contentView?.subviews.count == 0) {
  2643. return
  2644. }
  2645. let tipView = self.topTipBox.contentView?.subviews.first
  2646. if (tipView?.isKind(of: KMCropTipView.self) == false) {
  2647. return
  2648. }
  2649. (tipView as! KMCropTipView).setString(string: "请按 Enter 键确定裁剪区域")
  2650. }
  2651. func pdfListViewKeyDowClosePanel(_ speedy: CPDFViewSidebarSpeedMode, event theEvent: NSEvent!) {
  2652. if(speedy == .right) {
  2653. self.toggleRightPane()
  2654. } else if (speedy == .left) {
  2655. self.menuItemAction_hiddenLeftSide(speedy)
  2656. }
  2657. }
  2658. func pdfListViewEventMarkupColor(with annotation: CPDFAnnotation!) -> [NSColor]! {
  2659. if (annotation.isKind(of: CPDFMarkupAnnotation.self)) {
  2660. if (annotation as! CPDFMarkupAnnotation).markupType() == .highlight {
  2661. return KMAnnotationPropertiesColorManager.manager.markHighlightColors
  2662. } else {
  2663. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  2664. }
  2665. } else {
  2666. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  2667. }
  2668. }
  2669. func pdfListViewHaveDocumentAttribute() -> Bool {
  2670. if(!self.listView.document.allowsCopying) {
  2671. self.removeOwnerPassword()
  2672. return false
  2673. }
  2674. return true
  2675. }
  2676. func addTopTip(_ view: NSView?) {
  2677. if (Thread.isMainThread) {
  2678. if (view == nil) {
  2679. let contentView: NSView = self.topTipBox.contentView!
  2680. for subview in contentView.subviews {
  2681. subview.removeFromSuperview()
  2682. }
  2683. self.topTipBox.isHidden = true
  2684. return
  2685. }
  2686. let contentView: NSView = self.topTipBox.contentView!
  2687. for subview in contentView.subviews {
  2688. subview.removeFromSuperview()
  2689. }
  2690. self.topTipBox.isHidden = false
  2691. self.topTipBox.contentView?.addSubview(view!)
  2692. } else {
  2693. DispatchQueue.main.async {
  2694. if (view == nil) {
  2695. let contentView: NSView = self.topTipBox.contentView!
  2696. for subview in contentView.subviews {
  2697. subview.removeFromSuperview()
  2698. }
  2699. self.topTipBox.isHidden = true
  2700. return
  2701. }
  2702. let contentView: NSView = self.topTipBox.contentView!
  2703. for subview in contentView.subviews {
  2704. subview.removeFromSuperview()
  2705. }
  2706. self.topTipBox.isHidden = false
  2707. self.topTipBox.contentView?.addSubview(view!)
  2708. }
  2709. }
  2710. }
  2711. func pdfListView(_ sender: CPDFListView!, showSnapshotAtPageNumber pageNum: Int, for rect: NSRect, scaleFactor: CGFloat, autoFits: Bool) {
  2712. let swc = KMSnapshotWindowController(windowNibName: "SnapshotWindow")
  2713. swc.delegate = self
  2714. swc.setPdfDocument(self.listView.document, goToPageNumber: pageNum, rect: rect, scaleFactor: scaleFactor, autoFits: autoFits)
  2715. swc.forceOnTop = self.interactionMode != .normal
  2716. self.myDocument?.addWindowController(swc)
  2717. }
  2718. // MARK: Split View
  2719. func changePDFDocument(isChange: Bool, replaceBlock: @escaping (String) -> Void) {
  2720. let openPanel = NSOpenPanel()
  2721. openPanel.allowedFileTypes = ["pdf", "PDF"]
  2722. openPanel.allowsMultipleSelection = false
  2723. guard let mainWindow = NSApp.mainWindow else {
  2724. return
  2725. }
  2726. openPanel.beginSheetModal(for: mainWindow) { [weak self] response in
  2727. if response == NSApplication.ModalResponse.OK {
  2728. guard let url = openPanel.url else {
  2729. return
  2730. }
  2731. if let document = CPDFDocument(url: url) {
  2732. self?.secondaryPdfView?.document = nil
  2733. self?.secondaryPdfView?.document = document
  2734. if isChange {
  2735. self!.openSecondaryPdfView!.view.removeFromSuperview()
  2736. }
  2737. replaceBlock(document.documentURL?.path ?? "")
  2738. } else {
  2739. let alert = NSAlert()
  2740. alert.alertStyle = .critical
  2741. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  2742. alert.runModal()
  2743. }
  2744. }
  2745. }
  2746. }
  2747. }
  2748. // MARK: - CPDFDocumentDelegate
  2749. extension KMMainViewController: CPDFDocumentDelegate {
  2750. func documentDidBeginDocumentFind(_ document: CPDFDocument!) {
  2751. self.leftSideViewController.documentDidBeginFind()
  2752. // [statusBar setProgressIndicatorStyle:SKProgressIndicatorBarStyle];
  2753. // [[statusBar progressIndicator] setMaxValue:[[note object] pageCount]];
  2754. // [[statusBar progressIndicator] setDoubleValue:0.0];
  2755. // [statusBar startAnimation:self];
  2756. // [self willChangeValueForKey:SEARCHRESULTS_KEY];
  2757. // [self willChangeValueForKey:GROUPEDSEARCHRESULTS_KEY];
  2758. }
  2759. func documentDidEndDocumentFind(_ document: CPDFDocument!) {
  2760. self.leftSideViewController.documentDidEndFind()
  2761. // [self didChangeValueForKey:GROUPEDSEARCHRESULTS_KEY];
  2762. // [self didChangeValueForKey:SEARCHRESULTS_KEY];
  2763. // [statusBar stopAnimation:self];
  2764. // [statusBar setProgressIndicatorStyle:SKProgressIndicatorNone];
  2765. // NSArray *highlights = [[NSArray alloc] initWithArray:searchResults copyItems:YES];
  2766. // [highlights setValue:[NSColor yellowColor] forKey:@"color"];
  2767. // [self.pdfView setHighlightedSelections:highlights];
  2768. // [highlights release];
  2769. }
  2770. }
  2771. extension KMMainViewController: KMEditImagePropertyViewControllerDelegate {
  2772. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) {
  2773. self.isPDFTextImageEdited = true
  2774. }
  2775. }
  2776. extension KMMainViewController: KMSnapshotWindowControllerDelegate {
  2777. func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
  2778. self.leftSideViewController.snapshotControllerWillClose(controller)
  2779. }
  2780. func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
  2781. if isMiniaturize && self.interactionMode != .presentation {
  2782. if self.interactionMode != .legacyFullScreen && self.leftPanelOpen == false {
  2783. self.toggleLeftPane()
  2784. } else if self.interactionMode == .legacyFullScreen {
  2785. // else if ([self interactionMode] == SKLegacyFullScreenMode && ([rightSideWindow state] == NSDrawerClosedState || [rightSideWindow state] == NSDrawerClosingState)) {
  2786. // [rightSideWindow expand];
  2787. // [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hideRightSideWindow:) userInfo:NULL repeats:NO];
  2788. }
  2789. var row = NSNotFound
  2790. for (i,sp) in self.leftSideViewController.snapshots.enumerated() {
  2791. if controller.isEqual(sp.windowC) {
  2792. row = i
  2793. }
  2794. }
  2795. if (row != NSNotFound) {
  2796. self.leftSideViewController.leftView.segmentedControl.selectedSegment = 3
  2797. self.leftSideViewController.snapshotTableView.scrollRowToVisible(row)
  2798. }
  2799. }
  2800. return self.leftSideViewController.snapshotController(controller, miniaturizedRect: isMiniaturize)
  2801. }
  2802. func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
  2803. self.leftSideViewController.snapshotControllerDidFinishSetup(controller)
  2804. }
  2805. }