KMMainViewController.swift 137 KB

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