KMMainViewController.swift 143 KB

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