KMMainViewController.swift 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827
  1. //
  2. // KMMainViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objcMembers class KMMainViewController: KMBaseViewController, NSTextFieldDelegate {
  10. @IBOutlet var contendBox: NSBox!
  11. @IBOutlet var toolbarBox: NSBox! //工具栏Box
  12. @IBOutlet var bottomContendBox: NSBox! //
  13. @IBOutlet var sidebarBox: NSBox! //左侧边栏Box
  14. @IBOutlet var infoContendSplitView: KMPDFSplitView!
  15. @IBOutlet var infoSplitLeftView: NSView!
  16. @IBOutlet var infoSplitRightView: NSView!
  17. @IBOutlet var infoSplitCenterView: NSView!
  18. @IBOutlet var pdfSplitView: NSSplitView!
  19. @IBOutlet var pdfSplitTopView: NSView!
  20. @IBOutlet var pdfSplitBottomView: NSView!
  21. @IBOutlet var toolbarBoxHeightConst: NSLayoutConstraint!
  22. @IBOutlet var infoSplitViewLeftConst: NSLayoutConstraint!
  23. @IBOutlet var infoSplitViewRightConst: NSLayoutConstraint!
  24. var viewManager: KMPDFViewManager = KMPDFViewManager.init()
  25. var toolbarManager: KMPDFToolbarManager = KMPDFToolbarManager.init()
  26. var listView: CPDFListView = CPDFListView.init()
  27. var document: CPDFDocument?
  28. var myDocument: NSDocument?
  29. var insertDocuments: Set<CPDFDocument> = [] //插入新文档时,SDK会出现崩溃,临时记录
  30. //工具栏
  31. private var pdfToolbarController: KMPDFToolbarController?
  32. //BOTA SideBar
  33. private var sideBarController: KMPDFSideBarController?
  34. //页面编辑
  35. private var pageEditViewController: KMNPageEditViewController?
  36. //DisplaySetting
  37. private var displaySettingController: KMNDisplayViewController?
  38. //SPlitPDF分屏视图
  39. private var splitPDFController: KMSplitPDFViewController?
  40. private var pdfBottomToolbar: KMSplitToolbar?
  41. //PPT操作界面
  42. var presentationTopViewController: KMPresentationTopViewController?
  43. //Edit
  44. var editToolbarView: KMEditToolbarView?
  45. //水印
  46. var watermarkViewController: KMWatermarkController?
  47. //背景
  48. var backgroundViewController: KMBackgroundController?
  49. //Header&Footer
  50. var headerFooterViewController: KMHeaderFooterController?
  51. //Bates
  52. var batesViewController: KMBatesController?
  53. //Crop
  54. var cropController: KMCropController?
  55. //左边
  56. var botaViewController: KMNLeftSideViewController?
  57. //右边
  58. var rightSideController: KMRightSideController?
  59. //MARK: - 旧代码,有需要用到的拿出来,写好备注
  60. @IBOutlet weak var leftView: NSView!
  61. var model = KMMainModel()
  62. //自动滚动
  63. var autoFlowOptionsSheetController: KMAutoFlowOptionsSheetController?
  64. //Search
  65. var searchIndex: Int = 0
  66. //Form
  67. var formAlertView: KMFormAlertView?
  68. //Secure
  69. var secureAlertView: KMSecureAlertView?
  70. //对比
  71. var isCompareModel: Bool = false {
  72. didSet {
  73. }
  74. }
  75. //合并
  76. var mergeWindowController: KMMergeWindowController?
  77. //密码弹窗
  78. var passwordWindow: KMPasswordInputWindow?
  79. //对比
  80. var compressWIndowControllerNew: KMCompressWIndowControllerNew?
  81. //
  82. var securityWindowController: KMSecurityWindowController?
  83. //引导
  84. var guideInfoWindowController: KMGuideInfoWindowController?
  85. //春季活动
  86. var recommondPopWindowVC: KMRecommondPopWindow?
  87. var removeAllAnnotationsStore = KMPDFViewRemoveAllAnnotationsStore()
  88. private var _needSave = false
  89. var needSave: Bool {
  90. set {
  91. _needSave = newValue
  92. if (_needSave == false) {
  93. self.clearIsPDFDocumentEdited()
  94. }
  95. }
  96. get {
  97. return _needSave
  98. }
  99. }
  100. var isPDFDocumentEdited: Bool {
  101. get {
  102. return self.model.isPDFDocumentEdited
  103. }
  104. }
  105. var leftSideViewController: KMLeftSideViewController = KMLeftSideViewController.init(type: KMLeftMethodMode())
  106. var searchResults: [KMSearchMode] = []
  107. var mwcFlags: MwcFlags = MwcFlags()
  108. weak var browserWindowController: KMBrowserWindowController? //慎直接使用这个方法
  109. var cropSettingWindowController: KMCropSettingWindowController!
  110. var currentWindowController: NSWindowController!
  111. var savedNormalSetup: NSMutableDictionary = NSMutableDictionary()
  112. //数字签名
  113. var digitalSignController: KMPDFDigitalSignViewController?
  114. var redactController: KMPDFRedactViewController!
  115. let CPDFOfficeLeftSidePaneWidthKey = "CPDFOfficeLeftSidePaneWidthKey"
  116. let CPDFOfficeRightSidePaneWidthKey = "CPDFOfficeRightSidePaneWidthKey"
  117. var extract: KMExtractImageWindowController?
  118. var functionWidth: Double {
  119. get {
  120. if self.viewManager.isPDFReadMode {
  121. if !self.model.isShowBOTA {
  122. return 0
  123. }
  124. }
  125. return 48-4
  126. }
  127. }
  128. var pageNumber: UInt?
  129. var pdfEditController: KMPDFEditViewController? {
  130. get {
  131. return self.getPDFEditController()
  132. }
  133. }
  134. var autoSaveTimer: Timer?
  135. private var _documentFirstLoad: Bool = true
  136. var eventMonitor: Any?
  137. var keyEventMonitor: Any?
  138. var mouseRightMenuEvent: NSEvent?
  139. lazy private var homeVC: KMNHomeViewController? = {
  140. let vc = KMNHomeViewController()
  141. return vc
  142. }()
  143. fileprivate var _secureOptions: [CPDFDocumentWriteOption : Any]?
  144. var secureOptions: [CPDFDocumentWriteOption : Any]? {
  145. get {
  146. return self._secureOptions
  147. }
  148. }
  149. var documentAttribute: [CPDFDocumentAttribute : Any]?
  150. fileprivate var _removeSecureFlag = false
  151. var removeSecureFlag: Bool {
  152. get {
  153. return self._removeSecureFlag
  154. }
  155. }
  156. fileprivate var _saveWatermarkFlag = false
  157. var saveWatermarkFlag: Bool {
  158. get {
  159. return self._saveWatermarkFlag
  160. }
  161. }
  162. private var mainWindow_: NSWindow?
  163. var mainWindow: NSWindow? {
  164. get {
  165. return self.mainWindow_
  166. }
  167. set {
  168. self.mainWindow_ = newValue
  169. }
  170. }
  171. var setDocument: CPDFDocument? {
  172. get {
  173. return document
  174. }
  175. set {
  176. if document != newValue {
  177. document = newValue
  178. }
  179. listView.document = document
  180. botaViewController?.changeDocument(document: document)
  181. }
  182. }
  183. var setPageNumber: UInt {
  184. get {
  185. return pageNumber!
  186. }
  187. set {
  188. let pageCount = listView.document?.pageCount ?? 0
  189. var value = newValue
  190. if value > pageCount {
  191. value = pageCount
  192. }
  193. if value > 0 && listView.currentPage().pageIndex() != value-1 {
  194. listView.go(to: listView.document?.page(at: value-1))
  195. }
  196. if pageNumber != value {
  197. pageNumber = value
  198. }
  199. }
  200. }
  201. var distanceMeasureInfoWindowController: CDistanceMeasureInfoWindowController?
  202. var perimeterMeasureInfoWindowController: CPerimeterMeasureInfoWindowController?
  203. var areaMeasureInfoWindowController: CAreaMeasureInfoWindowController?
  204. var srHanddler: KMSearchReplaceHanddler = KMSearchReplaceHanddler()
  205. //MARK: - func
  206. deinit {
  207. NotificationCenter.default.removeObserver(self)
  208. self.listView.delegate = nil
  209. self.listView.document?.delegate = nil
  210. self.removeEventMonitor()
  211. self.removeKeyEventMonitor()
  212. }
  213. override func viewDidLoad() {
  214. super.viewDidLoad()
  215. // Do view setup here.
  216. toolbarManager.pdfViewManager = viewManager
  217. }
  218. override func viewDidAppear() {
  219. super.viewDidAppear()
  220. setupUI()
  221. }
  222. func setupUI() {
  223. initPDFView()
  224. initToolbar()
  225. initSideBar()
  226. setUpSplitView()
  227. }
  228. private func documentLoadComplete() {
  229. initBotaView()
  230. }
  231. func leftSidePaneIsOpen() -> Bool {
  232. if(botaViewController != nil) {
  233. return infoContendSplitView.isSubviewCollapsed(botaViewController!.view)
  234. }
  235. return false
  236. }
  237. private func toggleOpenLeftSide(pdfSideBarType: KMPDFSidebarType) {
  238. if(leftSidePaneIsOpen() == false) {
  239. infoContendSplitView.setPosition(264, ofDividerAt: 0)
  240. }
  241. if pdfSideBarType == .search {
  242. KMPrint(" search")
  243. } else if pdfSideBarType == .thumbnail {
  244. botaViewController?.leftsideType = pdfSideBarType
  245. KMPrint(" thumbnail")
  246. } else if pdfSideBarType == .outline {
  247. botaViewController?.outlineViewC.handdler.pdfView = listView
  248. botaViewController?.leftsideType = pdfSideBarType
  249. } else if pdfSideBarType == .bookmark {
  250. botaViewController?.bookmarkViewC.handdler.pdfView = listView
  251. botaViewController?.leftsideType = pdfSideBarType
  252. } else if pdfSideBarType == .annotation {
  253. KMPrint(" annotation")
  254. }
  255. }
  256. private func toggleCloseLeftSide() {
  257. if(leftSidePaneIsOpen() == true) {
  258. infoContendSplitView.setPosition(-20, ofDividerAt: 0)
  259. }
  260. }
  261. //MARK: - PDFView
  262. func initPDFView() {
  263. listView.autoresizingMask = [.width, .height]
  264. listView.delegate = self
  265. listView.pdfListViewDelegate = self
  266. listView.document = self.document
  267. reloadPDFSplitInfo()
  268. }
  269. func updatePDFViewAnnotationMode() {
  270. let toolbarMode = viewManager.toolMode
  271. let subToolMode = viewManager.subToolMode
  272. if toolbarMode == .None {
  273. listView.annotationType = .unkown
  274. listView.toolMode = .noteToolMode
  275. } else if toolbarMode == .Markup {
  276. listView.toolMode = .noteToolMode
  277. } else if toolbarMode == .Edit {
  278. if subToolMode == .None {
  279. if listView.toolMode != .editPDFToolMode {
  280. listView.toolMode = .editPDFToolMode
  281. listView.configPDFEditingInfo()
  282. }
  283. listView.annotationType = .editTextImage
  284. listView.setShouAddEdit([])
  285. listView.change([.text, .image])
  286. removeCropController()
  287. } else if subToolMode == .Edit_text {
  288. if listView.toolMode != .editPDFToolMode {
  289. listView.toolMode = .editPDFToolMode
  290. listView.configPDFEditingInfo()
  291. }
  292. listView.annotationType = .addText
  293. listView.setShouAddEdit([.text])
  294. listView.change(.text)
  295. } else if subToolMode == .Edit_Image {
  296. if listView.toolMode != .editPDFToolMode {
  297. listView.toolMode = .editPDFToolMode
  298. listView.configPDFEditingInfo()
  299. }
  300. listView.annotationType = .addImage
  301. listView.setShouAddEdit([.image])
  302. listView.change(.image)
  303. } else if subToolMode == .Edit_Link {
  304. listView.toolMode = .noteToolMode
  305. listView.annotationType = .link
  306. } else if subToolMode == .Edit_Crop {
  307. // listView.toolMode = .selectToolMode
  308. showCropController()
  309. }
  310. } else if toolbarMode == .Form {
  311. } else if toolbarMode == .Fill {
  312. }
  313. }
  314. //MARK: - SplitView
  315. func setUpSplitView() {
  316. infoContendSplitView.wantsLayer = true
  317. infoContendSplitView.layer?.backgroundColor = NSColor.clear.cgColor
  318. infoContendSplitView.delegate = self
  319. infoContendSplitView.layer?.masksToBounds = true
  320. infoContendSplitView.setPosition(-10, ofDividerAt: 0)
  321. infoContendSplitView.setPosition(CGRectGetWidth(infoContendSplitView.frame), ofDividerAt: 1)
  322. }
  323. func setupSplitPDFController() {
  324. if splitPDFController == nil {
  325. splitPDFController = KMSplitPDFViewController.init()
  326. }
  327. splitPDFController?.view.frame = pdfSplitBottomView.bounds
  328. splitPDFController?.view.autoresizingMask = [.height, .width]
  329. splitPDFController?.viewManager = self.viewManager
  330. splitPDFController?.delegate = self
  331. splitPDFController?.reloadData()
  332. pdfSplitBottomView.addSubview(splitPDFController!.view)
  333. }
  334. //MARK: - 工具栏
  335. func initToolbar() {
  336. toolbarBox.borderWidth = 0
  337. if pdfToolbarController == nil {
  338. pdfToolbarController = KMPDFToolbarController.init()
  339. }
  340. pdfToolbarController?.view.frame = toolbarBox.bounds
  341. pdfToolbarController?.view.autoresizingMask = [.width, .height]
  342. pdfToolbarController?.delegate = self
  343. toolbarBox.contentView = pdfToolbarController?.view
  344. pdfToolbarController?.viewManager = viewManager
  345. pdfToolbarController?.toolbarManager = toolbarManager
  346. pdfToolbarController?.pdfView = listView
  347. pdfToolbarController?.setUpData()
  348. refreshToolbarView()
  349. }
  350. func refreshToolbarView() {
  351. let _manager = viewManager
  352. if _manager.isPageEditMode {
  353. toolbarBoxHeightConst.constant = 80
  354. } else if _manager.editType == .watermark || _manager.editType == .background || _manager.editType == .header_Footer || _manager.editType == .bates {
  355. toolbarBoxHeightConst.constant = 40
  356. } else if _manager.toolMode == .Markup ||
  357. _manager.toolMode == .Edit ||
  358. _manager.toolMode == .Form ||
  359. _manager.toolMode == .Fill ||
  360. _manager.toolMode == .Convert ||
  361. _manager.toolMode == .Protect ||
  362. _manager.toolMode == .Tools {
  363. toolbarBoxHeightConst.constant = 80
  364. } else {
  365. toolbarBoxHeightConst.constant = 40
  366. }
  367. }
  368. func toolbarViewModeChanged() {
  369. updatePDFViewAnnotationMode()
  370. if viewManager.showRightSide == true {
  371. openRightPane()
  372. } else {
  373. closeRightPane()
  374. }
  375. }
  376. //MARK: - 侧边栏
  377. func initSideBar() {
  378. sidebarBox.borderWidth = 0
  379. if sideBarController == nil {
  380. sideBarController = KMPDFSideBarController.init()
  381. }
  382. sideBarController?.view.frame = sidebarBox.bounds
  383. sideBarController?.view.autoresizingMask = [.width, .height]
  384. sidebarBox.contentView = sideBarController?.view
  385. sideBarController?.pdfView = listView
  386. sideBarController?.delegate = self
  387. sideBarController?.pdfViewManager = viewManager
  388. sideBarController?.reloadData()
  389. }
  390. //MARK: - 侧边栏
  391. func initBotaView() {
  392. if botaViewController == nil {
  393. botaViewController = KMNLeftSideViewController(listView.document)
  394. }
  395. botaViewController?.leftSideViewDelegate = self
  396. botaViewController?.view.frame = infoSplitLeftView.bounds
  397. botaViewController?.view.autoresizingMask = [.width, .height]
  398. if botaViewController != nil {
  399. infoSplitLeftView?.addSubview(botaViewController!.view)
  400. }
  401. }
  402. //MARK: - 右侧属性栏
  403. func initRightSideController() {
  404. if rightSideController == nil {
  405. rightSideController = KMRightSideController.init()
  406. }
  407. rightSideController?.view.frame = CGRectMake(0, 0, 264, 680)
  408. rightSideController?.view.autoresizingMask = [.height, .maxXMargin]
  409. }
  410. func removeRightSideController() {
  411. rightSideController?.view.removeFromSuperview()
  412. rightSideController = nil
  413. }
  414. @objc func openRightPane() -> Void {
  415. initRightSideController()
  416. rightSideController?.view.frame = CGRectMake(CGRectGetWidth(bottomContendBox.frame)-264, 0, 264, CGRectGetHeight(bottomContendBox.frame))
  417. bottomContendBox.addSubview(rightSideController!.view)
  418. infoSplitViewRightConst.constant = 264
  419. rightSideController?.viewManager = self.viewManager
  420. }
  421. @objc func closeRightPane() -> Void {
  422. removeRightSideController()
  423. infoSplitViewRightConst.constant = 0
  424. }
  425. //MARK: - PDFDisplayView
  426. func updatePDFDisplaySettingView() {
  427. if viewManager.showDisplayView {
  428. infoSplitViewLeftConst.constant = 264
  429. } else {
  430. infoSplitViewLeftConst.constant = 44
  431. }
  432. if viewManager.showDisplayView {
  433. if displaySettingController == nil {
  434. displaySettingController = KMNDisplayViewController.init()
  435. }
  436. displaySettingController?.view.frame = CGRectMake(0, 0, 264, CGRectGetHeight(bottomContendBox.frame))
  437. displaySettingController?.view.autoresizingMask = [.height, .maxXMargin]
  438. bottomContendBox.addSubview(displaySettingController!.view)
  439. displaySettingController?.pdfView = self.listView
  440. displaySettingController?.viewManager = self.viewManager
  441. displaySettingController?.delegate = self
  442. displaySettingController?.reloadData()
  443. } else {
  444. displaySettingController?.view.removeFromSuperview()
  445. displaySettingController = nil
  446. }
  447. }
  448. //MARK: - 页面编辑
  449. func enterPageEditMode() {
  450. pageEditViewController = KMNPageEditViewController(self.document)
  451. if(pageEditViewController != nil) {
  452. bottomContendBox.addSubview(pageEditViewController!.view)
  453. pageEditViewController?.view.frame = bottomContendBox.bounds
  454. pageEditViewController?.thumbnailBaseViewDelegate = self
  455. pageEditViewController?.selectionIndexPaths = [IndexPath(item: listView.currentPageIndex, section: 0)]
  456. pageEditViewController?.view.autoresizingMask = [.width,.height]
  457. pageEditViewController?.currentUndoManager = listView.undoManager
  458. toolbarManager.page_pageInfo_Property.text = String(listView.currentPageIndex + 1)
  459. pdfToolbarController?.refreshSecondToolbarItemsState()
  460. }
  461. }
  462. func exitPageEditMode() {
  463. if pageEditViewController != nil {
  464. pageEditViewController?.view.removeFromSuperview()
  465. pageEditViewController = nil
  466. }
  467. }
  468. //MARK: - 阅读模式
  469. func openPDFReadMode() {
  470. if viewManager.showDisplayView {
  471. viewManager.showDisplayView = false
  472. pdfToolbarController?.reloadLeftIconView()
  473. updatePDFDisplaySettingView()
  474. }
  475. infoSplitViewLeftConst.constant = 0
  476. toolbarBoxHeightConst.constant = 0
  477. view.window?.makeFirstResponder(listView)
  478. var readModeMessage: ComponentMessage = ComponentMessage()
  479. readModeMessage.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString("Read Mode On"))
  480. readModeMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - readModeMessage.properties.propertyInfo.viewWidth)/2,
  481. CGRectGetHeight(self.view.frame) - readModeMessage.properties.propertyInfo.viewHeight - 8,
  482. readModeMessage.properties.propertyInfo.viewWidth,
  483. readModeMessage.properties.propertyInfo.viewHeight)
  484. readModeMessage.reloadData()
  485. readModeMessage.show(inView: self.view, autoHideSeconde: 2)
  486. }
  487. func exitPDFReadMode() {
  488. viewManager.isPDFReadMode = false
  489. updatePDFDisplaySettingView()
  490. refreshToolbarView()
  491. }
  492. //MARK: - PPT
  493. func togglePresentation(_ sender: Any?) {
  494. if self.canExitPresentation() {
  495. exitFullScreen()
  496. } else if self.canEnterPresentation() {
  497. NotificationCenter.default.addObserver(self, selector: #selector(willEnterInteractionModeNotification), name: NSWindow.willEnterInteractionModeNotification, object: nil)
  498. NotificationCenter.default.addObserver(self, selector: #selector(didEnterInteractionModeNotification), name: NSWindow.didEnterInteractionModeNotification, object: nil)
  499. NotificationCenter.default.addObserver(self, selector: #selector(willShowFullScreenNotification), name: NSWindow.willShowFullScreenNotification, object: nil)
  500. NotificationCenter.default.addObserver(self, selector: #selector(didShowFullScreenNotification), name: NSWindow.didShowFullScreenNotification, object: nil)
  501. view.window?.enterPresentation(provider: self)
  502. }
  503. }
  504. func enterPresentationMode() {
  505. let scrollView = listView.documentView().enclosingScrollView
  506. savedNormalSetup.setValue(scrollView?.hasHorizontalScroller, forKey: KMMainModel.Key.kHasHorizontalScroller)
  507. savedNormalSetup.setValue(scrollView?.hasVerticalScroller, forKey: KMMainModel.Key.kHasVerticalsCroller)
  508. savedNormalSetup.setValue(scrollView?.autohidesScrollers, forKey: KMMainModel.Key.kAutoHidesScrollers)
  509. listView.backgroundColor = NSColor.clear
  510. listView.setDisplay(.singlePage)
  511. listView.autoScales = true
  512. listView.displayBox = .cropBox
  513. listView.displaysPageBreaks = false
  514. scrollView?.autohidesScrollers = true
  515. scrollView?.hasHorizontalScroller = false
  516. scrollView?.hasVerticalScroller = false
  517. listView.setCurrentSelection(nil, animate: true)
  518. }
  519. func exitPresentationMode() {
  520. NotificationCenter.default.removeObserver(self, name: NSWindow.willEnterInteractionModeNotification, object: nil)
  521. NotificationCenter.default.removeObserver(self, name: NSWindow.didEnterInteractionModeNotification, object: nil)
  522. NotificationCenter.default.removeObserver(self, name: NSWindow.willShowFullScreenNotification, object: nil)
  523. NotificationCenter.default.removeObserver(self, name: NSWindow.willShowFullScreenNotification, object: nil)
  524. }
  525. func exitFullScreen() {
  526. if self.canExitPresentation() == true {
  527. let mainDocument = self.myDocument as? KMMainDocument
  528. let browserWindowController = mainDocument?.browser?.windowController as? KMBrowserWindowController
  529. browserWindowController?.exitFullscreen()
  530. }
  531. }
  532. func exitFullscreenMode() {
  533. if self.interactionMode == .presentation {
  534. self.exitPresentationMode()
  535. }
  536. self.applyPDFSettings(self.savedNormalSetup)
  537. self.savedNormalSetup.removeAllObjects()
  538. listView.layoutDocumentView()
  539. listView.requiresDisplay()
  540. if let backgroundColor = UserDefaults.standard.color(forKey: KMBackgroundColorKey) {
  541. listView.backgroundColor = backgroundColor
  542. }
  543. }
  544. func applyPDFSettings(_ setup: NSDictionary) {
  545. if let data = setup.object(forKey: KMMainModel.Key.kAutoScales) as? NSNumber {
  546. self.listView.autoScales = data.boolValue
  547. }
  548. if self.listView.autoScales == false {
  549. if let data = setup.object(forKey: KMMainModel.Key.kScaleFactor) as? NSNumber {
  550. self.listView.scaleFactor = data.floatValue.cgFloat
  551. }
  552. }
  553. if let data = setup.object(forKey: KMMainModel.Key.kDisplayMode) as? NSNumber {
  554. self.listView.setDisplay(CPDFDisplayViewMode(rawValue: data.intValue) ?? .singlePage)
  555. }
  556. if let data = setup.object(forKey: KMMainModel.Key.kDisplaysAsBook) as? NSNumber {
  557. self.listView.displaysAsBook = data.boolValue
  558. }
  559. if let data = setup.object(forKey: KMMainModel.Key.kDisplaysPageBreaks) as? NSNumber {
  560. self.listView.displaysPageBreaks = data.boolValue
  561. }
  562. if let data = setup.object(forKey: KMMainModel.Key.kDisplayBox) as? NSNumber {
  563. }
  564. self.listView.layoutDocumentView()
  565. }
  566. func currentPDFSettings() -> NSDictionary {
  567. let setup = NSMutableDictionary()
  568. setup[KMMainModel.Key.kDisplaysPageBreaks] = NSNumber(value: listView.displaysPageBreaks)
  569. setup[KMMainModel.Key.kDisplaysAsBook] = NSNumber(value: listView.displaysAsBook)
  570. setup[KMMainModel.Key.kDisplayBox] = NSNumber(value: listView.displayBox.rawValue)
  571. setup[KMMainModel.Key.kScaleFactor] = NSNumber(value: listView.scaleFactor)
  572. setup[KMMainModel.Key.kAutoScales] = NSNumber(value: listView.autoScales)
  573. setup[KMMainModel.Key.kDisplayMode] = NSNumber(value: listView.fetchDisplayViewMode().rawValue)
  574. return setup
  575. }
  576. func canEnterFullscreen() -> Bool {
  577. if (mwcFlags.isSwitchingFullScreen != 0) {
  578. return false
  579. }
  580. if useNativeFullScreen() {
  581. return interactionMode == .normal || interactionMode == .presentation
  582. } else {
  583. return !self.listView.document.isLocked && (interactionMode == .normal || interactionMode == .presentation) && self.view.window?.tabbedWindows?.count ?? 0 < 2
  584. }
  585. }
  586. override func canEnterPresentation() -> Bool {
  587. let can = super.canEnterPresentation()
  588. if can == false {
  589. return false
  590. }
  591. guard let doc = self.listView.document, doc.isLocked == false else {
  592. return false
  593. }
  594. return can
  595. }
  596. func fadeOutFullScreenWindow() {
  597. guard let fullScreenWindow = self.view.window as? KMFullScreenWindow else {
  598. NSSound.beep()
  599. return
  600. }
  601. let mainWindow = fullScreenWindow.interactionParent
  602. let collectionBehavior = mainWindow?.collectionBehavior
  603. mainWindow?.alphaValue = 0
  604. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  605. mainWindow?.animationBehavior = .none
  606. }
  607. // trick to make sure the main window shows up in the same space as the fullscreen window
  608. fullScreenWindow.addChildWindow(mainWindow!, ordered: .below)
  609. fullScreenWindow.removeChildWindow(mainWindow!)
  610. fullScreenWindow.level = .popUpMenu
  611. // these can change due to the child window trick
  612. mainWindow?.level = .normal
  613. mainWindow?.alphaValue = 1.0
  614. mainWindow?.collectionBehavior = collectionBehavior!
  615. mainWindow?.display()
  616. mainWindow?.makeFirstResponder(self.listView)
  617. mainWindow?.recalculateKeyViewLoop()
  618. // mainWindow?.delegate = self
  619. mainWindow?.makeKey()
  620. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  621. mainWindow?.animationBehavior = .default
  622. }
  623. NSApp.removeWindowsItem(fullScreenWindow)
  624. fullScreenWindow.fadeOut()
  625. }
  626. //MARK: - PDF分屏视图
  627. func reloadPDFSplitInfo() {
  628. if listView.viewSplitMode == .disable {
  629. pdfSplitView.isHidden = true
  630. listView.frame = infoSplitCenterView.bounds
  631. infoSplitCenterView.addSubview(listView)
  632. if splitPDFController != nil {
  633. splitPDFController = nil
  634. }
  635. } else {
  636. pdfSplitView.isHidden = false
  637. listView.frame = pdfSplitTopView.bounds
  638. pdfSplitTopView.addSubview(listView)
  639. setUpPDFBottomToolbar()
  640. setupSplitPDFController()
  641. if listView.viewSplitMode == .horizontal {
  642. pdfSplitView.isVertical = false
  643. } else if listView.viewSplitMode == .vertical {
  644. pdfSplitView.isVertical = true
  645. }
  646. }
  647. }
  648. func setUpPDFBottomToolbar() {
  649. if pdfBottomToolbar != nil {
  650. pdfBottomToolbar = nil
  651. }
  652. pdfBottomToolbar = KMSplitToolbar.init()
  653. pdfBottomToolbar?.frame = CGRectMake(CGRectGetWidth(pdfSplitTopView.frame)/2-144, 20, 288, 40)
  654. pdfBottomToolbar?.pdfView = self.listView
  655. pdfBottomToolbar?.reloadData()
  656. pdfSplitTopView.addSubview(pdfBottomToolbar!)
  657. }
  658. func reloadPDFBottomToolbar() {
  659. if viewManager.splitShowBottomBar {
  660. pdfBottomToolbar?.isHidden = false
  661. pdfBottomToolbar?.reloadData()
  662. } else {
  663. pdfBottomToolbar?.isHidden = true
  664. }
  665. }
  666. //MARK: - Edit模式
  667. func showEditToolbarView() {
  668. if editToolbarView == nil {
  669. editToolbarView = KMEditToolbarView()
  670. }
  671. editToolbarView?.frame = toolbarBox.bounds
  672. editToolbarView?.delegate = self
  673. editToolbarView?.autoresizingMask = [.width, .height]
  674. toolbarBox.contentView = editToolbarView
  675. }
  676. func exitEditToolbarView() {
  677. listView.isHidden = false
  678. viewManager.editType = .none
  679. editToolbarView?.removeFromSuperview()
  680. editToolbarView = nil
  681. watermarkViewController?.view.removeFromSuperview()
  682. watermarkViewController = nil
  683. backgroundViewController?.view.removeFromSuperview()
  684. backgroundViewController = nil
  685. headerFooterViewController?.view.removeFromSuperview()
  686. headerFooterViewController = nil
  687. batesViewController?.view.removeFromSuperview()
  688. batesViewController = nil
  689. refreshToolbarView()
  690. toolbarBox.contentView = pdfToolbarController?.view
  691. }
  692. //MARK: - Crop裁剪
  693. func showCropController() {
  694. listView.isHidden = true
  695. if cropController == nil {
  696. cropController = KMCropController.init()
  697. }
  698. cropController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  699. cropController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  700. // cropController?.delegate = self
  701. bottomContendBox.addSubview(cropController!.view)
  702. updateCropDocument()
  703. }
  704. func updateCropDocument() {
  705. guard let controller = cropController else { return }
  706. controller.pdfDocument = nil
  707. let editDocument = CPDFDocument.init()
  708. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  709. editDocument?.insertPageObject(page, at: 0)
  710. controller.pdfDocument = editDocument
  711. controller.reloadData()
  712. }
  713. func removeCropController() {
  714. listView.isHidden = false
  715. if cropController != nil {
  716. cropController?.view.removeFromSuperview()
  717. cropController = nil
  718. }
  719. }
  720. //MARK: - Watermark水印
  721. func showWatermarkController() {
  722. listView.isHidden = true
  723. viewManager.editType = .watermark
  724. showEditToolbarView()
  725. editToolbarView?.editType = .watermark
  726. if KMWatermarkManager.defaultManager.watermarks.count == 0 {
  727. editToolbarView?.editSubType = .add
  728. } else {
  729. editToolbarView?.editSubType = .template
  730. }
  731. editToolbarView?.reloadData()
  732. if watermarkViewController == nil {
  733. watermarkViewController = KMWatermarkController.init()
  734. }
  735. watermarkViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  736. watermarkViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  737. watermarkViewController?.delegate = self
  738. bottomContendBox.addSubview(watermarkViewController!.view)
  739. watermarkViewController?.editSubType = editToolbarView?.editSubType ?? .template
  740. updateWatermarkDocument()
  741. }
  742. func updateWatermarkDocument() {
  743. guard let controller = watermarkViewController else { return }
  744. controller.pdfDocument = nil
  745. let editDocument = CPDFDocument.init()
  746. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  747. editDocument?.insertPageObject(page, at: 0)
  748. watermarkViewController?.pdfDocument = editDocument
  749. watermarkViewController?.reloadData()
  750. }
  751. //移除文档水印
  752. func removePDFWatermark() {
  753. let watermarks = self.listView.document.watermarks()
  754. if (watermarks == nil || watermarks!.count <= 0) {
  755. let alert = NSAlert()
  756. alert.alertStyle = .warning
  757. alert.messageText = NSLocalizedString("Could not find a removable watermark in this document. If you see a watermark, it was not added with PDF Reader Pro and therefore cannot be detected.", comment: "")
  758. alert.addButton(withTitle: NSLocalizedString("Confirm", comment: ""))
  759. alert.runModal()
  760. return
  761. }
  762. let alert = NSAlert()
  763. alert.alertStyle = .warning
  764. alert.messageText = NSLocalizedString("Are you sure you want to remove the watermark?", comment: "")
  765. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  766. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  767. let result = alert.runModal()
  768. if (result == .alertFirstButtonReturn) {
  769. for watermark in watermarks! {
  770. listView.document.removeWatermark(watermark)
  771. }
  772. listView.layoutDocumentView()
  773. }
  774. }
  775. func batchAddWatermark() {
  776. }
  777. func batchRemoveWatermark() {
  778. }
  779. //MARK: - Background背景
  780. func showBackgroundController() {
  781. listView.isHidden = true
  782. viewManager.editType = .background
  783. showEditToolbarView()
  784. editToolbarView?.editType = .background
  785. if KMBackgroundManager.defaultManager.datas.count == 0 {
  786. editToolbarView?.editSubType = .add
  787. } else {
  788. editToolbarView?.editSubType = .template
  789. }
  790. editToolbarView?.reloadData()
  791. if backgroundViewController == nil {
  792. backgroundViewController = KMBackgroundController.init()
  793. }
  794. backgroundViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  795. backgroundViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  796. backgroundViewController?.delegate = self
  797. bottomContendBox.addSubview(backgroundViewController!.view)
  798. backgroundViewController?.editSubType = editToolbarView?.editSubType ?? .template
  799. updateBackgroundDocument()
  800. }
  801. func updateBackgroundDocument() {
  802. guard let controller = backgroundViewController else { return }
  803. controller.pdfDocument = nil
  804. let editDocument = CPDFDocument.init()
  805. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  806. editDocument?.insertPageObject(page, at: 0)
  807. backgroundViewController?.pdfDocument = editDocument
  808. backgroundViewController?.reloadData()
  809. }
  810. func removePDFBackground() {
  811. let background = listView.document.background()
  812. background?.clear()
  813. listView.document?.refreshPageData()
  814. listView.layoutDocumentView()
  815. }
  816. func batchAddBackground() {
  817. }
  818. func batchRemoveBackground() {
  819. }
  820. //MARK: - header&footer
  821. func showHeaderFooterController() {
  822. listView.isHidden = true
  823. viewManager.editType = .header_Footer
  824. showEditToolbarView()
  825. editToolbarView?.editType = .header_Footer
  826. if KMHeaderFooterManager.defaultManager.headFooterObjects.count == 0 {
  827. editToolbarView?.editSubType = .add
  828. } else {
  829. editToolbarView?.editSubType = .template
  830. }
  831. editToolbarView?.reloadData()
  832. if headerFooterViewController == nil {
  833. headerFooterViewController = KMHeaderFooterController.init()
  834. }
  835. headerFooterViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  836. headerFooterViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  837. headerFooterViewController?.totalPDFCount = Int(listView.document.pageCount)
  838. headerFooterViewController?.delegate = self
  839. bottomContendBox.addSubview(headerFooterViewController!.view)
  840. headerFooterViewController?.editSubType = editToolbarView?.editSubType ?? .template
  841. updateHeaderFooterDocument()
  842. }
  843. func updateHeaderFooterDocument() {
  844. guard let controller = headerFooterViewController else { return }
  845. controller.pdfDocument = nil
  846. let editDocument = CPDFDocument.init()
  847. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  848. editDocument?.insertPageObject(page, at: 0)
  849. headerFooterViewController?.pdfDocument = editDocument
  850. headerFooterViewController?.reloadData()
  851. }
  852. func removeHeaderFooter() {
  853. let headerFooter = listView.document.headerFooter()
  854. headerFooter?.clear()
  855. listView.document?.refreshPageData()
  856. listView.layoutDocumentView()
  857. }
  858. func batchAddHeaderFooter() {
  859. }
  860. func batchRemoveHeaderFooter() {
  861. }
  862. //MARK: - Bates
  863. func showBatesController() {
  864. listView.isHidden = true
  865. viewManager.editType = .bates
  866. showEditToolbarView()
  867. editToolbarView?.editType = viewManager.editType
  868. if KMBatesManager.defaultManager.datas.count == 0 {
  869. editToolbarView?.editSubType = .add
  870. } else {
  871. editToolbarView?.editSubType = .template
  872. }
  873. editToolbarView?.reloadData()
  874. if batesViewController == nil {
  875. batesViewController = KMBatesController.init()
  876. }
  877. batesViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  878. batesViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  879. batesViewController?.delegate = self
  880. batesViewController?.totalPDFCount = Int(listView.document.pageCount)
  881. bottomContendBox.addSubview(batesViewController!.view)
  882. batesViewController?.editSubType = editToolbarView?.editSubType ?? .template
  883. updateBatesDocument()
  884. }
  885. func updateBatesDocument() {
  886. guard let controller = batesViewController else { return }
  887. controller.pdfDocument = nil
  888. let editDocument = CPDFDocument.init()
  889. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  890. editDocument?.insertPageObject(page, at: 0)
  891. batesViewController?.pdfDocument = editDocument
  892. batesViewController?.reloadData()
  893. }
  894. func removePDFBates() {
  895. let bates = listView.document.bates()
  896. bates?.clear()
  897. listView.document?.refreshPageData()
  898. listView.layoutDocumentView()
  899. }
  900. func batchAddBates() {
  901. }
  902. func batchRemoveBates() {
  903. }
  904. }
  905. //MARK: - NSSplitViewDelegate
  906. extension KMMainViewController: NSSplitViewDelegate {
  907. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  908. return true
  909. }
  910. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  911. return true
  912. }
  913. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  914. return proposedMaximumPosition
  915. }
  916. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  917. return proposedMinimumPosition
  918. }
  919. func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  920. if dividerIndex == 0 {
  921. if proposedPosition > CGRectGetWidth(infoContendSplitView.frame)/2 - 10 {
  922. print(CGRectGetWidth(infoContendSplitView.frame)/2 - 10, NSDate())
  923. return CGRectGetWidth(infoContendSplitView.frame)/2 - 10
  924. }
  925. } else if dividerIndex == 1 {
  926. print(CGRectGetWidth(infoContendSplitView.frame)/2 - 10, NSDate())
  927. }
  928. return proposedPosition
  929. }
  930. func splitViewDidResizeSubviews(_ notification: Notification) {
  931. let splitView = notification.object as? NSSplitView
  932. if((splitView?.isEqual(to: infoContendSplitView)) == true) {
  933. leftSplitViewResizeFinish()
  934. }
  935. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(splitViewResizeFinish), object: nil)
  936. self.perform(#selector(splitViewResizeFinish), with: nil, afterDelay: 0.15)
  937. }
  938. @objc func leftSplitViewResizeFinish() {
  939. botaViewController?.changeLeftSideBounds()
  940. }
  941. @objc func splitViewResizeFinish() {
  942. if infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) {
  943. if viewManager.pdfSideBarType != .none {
  944. viewManager.pdfSideBarType = .none
  945. sideBarController?.reloadBOTAData()
  946. }
  947. }
  948. }
  949. }
  950. //MARK: - KMPDFSideBarControllerDelegate 左侧Sidebar代理
  951. extension KMMainViewController: KMPDFSideBarControllerDelegate {
  952. func kmPDFSideBarControllerDidSidebarTypeUpdated(_ view: KMPDFSideBarController) {
  953. if viewManager.pdfSideBarType == .none {
  954. toggleCloseLeftSide()
  955. } else {
  956. toggleOpenLeftSide(pdfSideBarType: viewManager.pdfSideBarType)
  957. }
  958. }
  959. func kmPDFSideBarControllerDidGotoPage(_ view: KMPDFSideBarController, _ pageIndex: Int) {
  960. listView.go(toPageIndex: pageIndex, animated: true)
  961. }
  962. }
  963. //MARK: - KMPDFToolbarControllerDelegate 工具栏代理
  964. extension KMMainViewController: KMPDFToolbarControllerDelegate {
  965. func kmPDFToolbarControllerDidToolbarItemClicked(_ controller: KMPDFToolbarController, _ itemIdentifier: String) {
  966. print("toolbar点击", itemIdentifier)
  967. if itemIdentifier == KMPDFToolbar_ViewDisplay_Identifier {
  968. //Display
  969. updatePDFDisplaySettingView()
  970. } else if itemIdentifier == KMPDFToolbar_PageEdit_Identifier {
  971. if viewManager.isPageEditMode == true {
  972. enterPageEditMode()
  973. } else {
  974. exitPageEditMode()
  975. }
  976. } else if itemIdentifier == KMPDFToolbar_Markup_Identifier {
  977. } else if itemIdentifier == KMPDFToolbar_Edit_Identifier {
  978. } else if itemIdentifier == KMPDFToolbar_edit_link_Identifier {
  979. //Link
  980. } else if itemIdentifier == KMPDFToolbar_Form_Identifier {
  981. } else if itemIdentifier == KMPDFToolbar_Fill_Identifier {
  982. } else if itemIdentifier == KMPDFToolbar_Convert_Identifier {
  983. } else if itemIdentifier == KMPDFToolbar_Protect_Identifier {
  984. } else if itemIdentifier == KMPDFToolbar_Tools_Identifier {
  985. } else if itemIdentifier == KMPDFToolbar_convert_word_Identifier {
  986. let winC = KMConvertWordWindowController()
  987. let model = KMDocumentModel(url: listView.document.documentURL)
  988. winC.documentModel = model
  989. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  990. } else if itemIdentifier == KMPDFToolbar_convert_ppt_Identifier {
  991. let winC = KMConvertPPTsWindowController()
  992. winC.subType = 1
  993. let model = KMDocumentModel(url: listView.document.documentURL)
  994. winC.documentModel = model
  995. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  996. } else if itemIdentifier == KMPDFToolbar_convert_RTF_Identifier {
  997. let winC = KMConvertPPTsWindowController()
  998. winC.subType = 2
  999. let model = KMDocumentModel(url: listView.document.documentURL)
  1000. winC.documentModel = model
  1001. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1002. } else if itemIdentifier == KMPDFToolbar_convert_Text_Identifier {
  1003. let winC = KMConvertPPTsWindowController()
  1004. winC.subType = 4
  1005. let model = KMDocumentModel(url: listView.document.documentURL)
  1006. winC.documentModel = model
  1007. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1008. } else if itemIdentifier == KMPDFToolbar_convert_CSV_Identifier {
  1009. let winC = KMConvertPPTsWindowController()
  1010. winC.subType = 5
  1011. let model = KMDocumentModel(url: listView.document.documentURL)
  1012. winC.documentModel = model
  1013. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1014. } else if itemIdentifier == KMPDFToolbar_convert_excel_Identifier {
  1015. let winC = KMConvertExcelWindowController()
  1016. let model = KMDocumentModel(url: listView.document.documentURL)
  1017. winC.documentModel = model
  1018. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1019. } else if itemIdentifier == KMPDFToolbar_convert_HTML_Identifier {
  1020. let winC = KMConvertHtmlWindowController()
  1021. let model = KMDocumentModel(url: listView.document.documentURL)
  1022. winC.documentModel = model
  1023. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1024. } else if itemIdentifier == KMPDFToolbar_convert_Json_Identifier {
  1025. let winC = KMConvertJsonWindowController()
  1026. let model = KMDocumentModel(url: listView.document.documentURL)
  1027. winC.documentModel = model
  1028. winC.own_beginSheetModal(for: view.window, completionHandler: nil)
  1029. } else if itemIdentifier == KMPDFToolbar_convert_image_Identifier {
  1030. let winC = KMConvertImageWindowController()
  1031. let model = KMDocumentModel(url: listView.document.documentURL)
  1032. winC.documentModel = model
  1033. winC.own_beginSheetModal(for: view.window, completionHandler: nil)
  1034. } else if itemIdentifier == KMPDFToolbar_convert_imageToPDF_Identifier {
  1035. NSApplication.ShowImageToPDFWindow()
  1036. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertFile_Identifier {
  1037. pageEditViewController?.insertFromPDFAction()
  1038. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertBlank_Identifier {
  1039. pageEditViewController?.insertFromBlankAction()
  1040. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertClip_Identifier {
  1041. pageEditViewController?.insertFromClipboardAction()
  1042. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertScanner_Identifier {
  1043. pageEditViewController?.insertFromScannerAction()
  1044. } else if itemIdentifier == KMPDFToolbar_PageEdit_Extract_Identifier {
  1045. pageEditViewController?.extractPDFAction()
  1046. } else if itemIdentifier == KMPDFToolbar_PageEdit_Replace_Identifier {
  1047. pageEditViewController?.replacePDFAction()
  1048. } else if itemIdentifier == KMPDFToolbar_PageEdit_Split_Identifier {
  1049. pageEditViewController?.splitPDFAction()
  1050. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reverse_Identifier {
  1051. pageEditViewController?.reversePDFAction()
  1052. } else if itemIdentifier == KMPDFToolbar_PageEdit_LeftRotate_Identifier {
  1053. pageEditViewController?.rotatePageLeftAction()
  1054. } else if itemIdentifier == KMPDFToolbar_PageEdit_RightRotate_Identifier{
  1055. pageEditViewController?.rotatePageRightAction()
  1056. } else if itemIdentifier == KMPDFToolbar_PageEdit_Delete_Identifier {
  1057. pageEditViewController?.deletePageAction()
  1058. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reduce_Identifier {
  1059. pageEditViewController?.zoomOutPageAction()
  1060. if(pageEditViewController?.canZoomInPageSize() == false) {
  1061. toolbarManager.page_Increase_Property.isDisabled = true
  1062. } else {
  1063. toolbarManager.page_Increase_Property.isDisabled = false
  1064. }
  1065. if(pageEditViewController?.canZoomOutPageSize() == false) {
  1066. toolbarManager.page_Reduce_Property.isDisabled = true
  1067. } else {
  1068. toolbarManager.page_Reduce_Property.isDisabled = false
  1069. }
  1070. controller.refreshSecondToolbarItemsState()
  1071. } else if itemIdentifier == KMPDFToolbar_PageEdit_Increase_Identifier {
  1072. pageEditViewController?.zoomInPageAction()
  1073. if(pageEditViewController?.canZoomInPageSize() == false) {
  1074. toolbarManager.page_Increase_Property.isDisabled = true
  1075. } else {
  1076. toolbarManager.page_Increase_Property.isDisabled = false
  1077. }
  1078. if(pageEditViewController?.canZoomOutPageSize() == false) {
  1079. toolbarManager.page_Reduce_Property.isDisabled = true
  1080. } else {
  1081. toolbarManager.page_Reduce_Property.isDisabled = false
  1082. }
  1083. controller.refreshSecondToolbarItemsState()
  1084. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_oddPage_Identifier {
  1085. pageEditViewController?.thumbnailChoosePageStyle = .odd
  1086. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1087. toolbarManager.page_pageInfo_Property.creatable = false
  1088. controller.refreshSecondToolbarItemsState()
  1089. }
  1090. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_EvenPage_Identifier {
  1091. pageEditViewController?.thumbnailChoosePageStyle = .even
  1092. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1093. toolbarManager.page_pageInfo_Property.creatable = false
  1094. controller.refreshSecondToolbarItemsState()
  1095. }
  1096. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_PortraitPage_Identifier {
  1097. pageEditViewController?.thumbnailChoosePageStyle = .horizontal
  1098. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1099. toolbarManager.page_pageInfo_Property.creatable = false
  1100. controller.refreshSecondToolbarItemsState()
  1101. }
  1102. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_LandscapePage_Identifier {
  1103. pageEditViewController?.thumbnailChoosePageStyle = .vertical
  1104. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1105. toolbarManager.page_pageInfo_Property.creatable = false
  1106. controller.refreshSecondToolbarItemsState()
  1107. }
  1108. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_AllPage_Identifier {
  1109. pageEditViewController?.thumbnailChoosePageStyle = .allPage
  1110. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1111. toolbarManager.page_pageInfo_Property.creatable = false
  1112. controller.refreshSecondToolbarItemsState()
  1113. }
  1114. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_CustomPage_Identifier {
  1115. pageEditViewController?.thumbnailChoosePageStyle = .custom
  1116. toolbarManager.page_pageInfo_Property.text = nil
  1117. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  1118. toolbarManager.page_pageInfo_Property.creatable = true
  1119. }
  1120. controller.refreshSecondToolbarItemsState()
  1121. } else if itemIdentifier == KMPDFToolbar_edit_addWatermark_Identifier {
  1122. //
  1123. showWatermarkController()
  1124. } else if itemIdentifier == KMPDFToolbar_edit_removeWatermark_Identifier {
  1125. removePDFWatermark()
  1126. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddWatermark_Identifier {
  1127. batchAddWatermark()
  1128. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveWatermark_Identifier {
  1129. batchRemoveWatermark()
  1130. } else if itemIdentifier == KMPDFToolbar_edit_addBG_Identifier {
  1131. showBackgroundController()
  1132. } else if itemIdentifier == KMPDFToolbar_edit_removeBG_Identifier {
  1133. removePDFBackground()
  1134. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddBG_Identifier {
  1135. batchAddBackground()
  1136. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveBG_Identifier {
  1137. batchRemoveBackground()
  1138. } else if itemIdentifier == KMPDFToolbar_edit_addHF_Identifier {
  1139. showHeaderFooterController()
  1140. } else if itemIdentifier == KMPDFToolbar_edit_removeHF_Identifier {
  1141. removeHeaderFooter()
  1142. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddHF_Identifier {
  1143. batchAddHeaderFooter()
  1144. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveHF_Identifier {
  1145. batchRemoveHeaderFooter()
  1146. } else if itemIdentifier == KMPDFToolbar_edit_addBates_Identifier {
  1147. showBatesController()
  1148. } else if itemIdentifier == KMPDFToolbar_edit_removeBates_Identifier {
  1149. removePDFBates()
  1150. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddBates_Identifier {
  1151. batchAddBates()
  1152. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveBates_Identifier {
  1153. batchRemoveBates()
  1154. } else if(itemIdentifier == KMPDFToolbar_undo_Identifier) {
  1155. listView.undoManager?.undo()
  1156. } else if(itemIdentifier == KMPDFToolbar_redo_Identifier) {
  1157. listView.undoManager?.redo()
  1158. } else {
  1159. print("click else")
  1160. }
  1161. refreshToolbarView()
  1162. toolbarViewModeChanged()
  1163. }
  1164. func kmPDFToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  1165. }
  1166. func kmPDFToolbarControllerDidSelectTextDidChange(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  1167. }
  1168. func kmPDFToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  1169. if view.properties == toolbarManager.page_pageInfo_Property {
  1170. if viewManager.isPageEditMode == true {
  1171. let fileAttribute = KMNFileAttribute()
  1172. fileAttribute.password = listView.document?.password ?? ""
  1173. fileAttribute.pdfDocument = listView.document
  1174. fileAttribute.filePath = listView.document?.documentURL.path ?? ""
  1175. fileAttribute.bAllPage = false
  1176. fileAttribute.pagesType = .PagesString
  1177. fileAttribute.pagesString = view.properties.text ?? ""
  1178. let fetchSelectPages = fileAttribute.fetchSelectPages()
  1179. if (fetchSelectPages.isEmpty) {
  1180. let alert = NSAlert()
  1181. alert.alertStyle = .critical
  1182. alert.messageText = String(format: "%@ %@", fileAttribute.filePath.lastPathComponent, KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  1183. alert.runModal()
  1184. toolbarManager.page_pageInfo_Property.text = ""
  1185. controller.refreshSecondToolbarItemsState()
  1186. } else {
  1187. var tIndexPaths: Set<IndexPath> = []
  1188. for i in 0 ..< fetchSelectPages.count {
  1189. tIndexPaths.insert(IndexPath(item: (fetchSelectPages[i] - 1), section: 0))
  1190. }
  1191. pageEditViewController?.selectionIndexPaths = tIndexPaths
  1192. }
  1193. }
  1194. }
  1195. }
  1196. func kmPDFToolbarControllerDidExitPageEditMode(_ controller: KMPDFToolbarController) {
  1197. exitPageEditMode()
  1198. }
  1199. }
  1200. //MARK: - KMNDisplayViewControllerDelegate代理
  1201. extension KMMainViewController: KMNDisplayViewControllerDelegate {
  1202. //Display Mode
  1203. func displayViewControllerDidDisplayModeChanged(_ controller: KMNDisplayViewController) {
  1204. listView.layoutDocumentView()
  1205. }
  1206. //阅读模式
  1207. func displayViewControllerDidReadModeUpdated(_ controller: KMNDisplayViewController) {
  1208. if viewManager.isPDFReadMode {
  1209. openPDFReadMode()
  1210. } else {
  1211. exitPDFReadMode()
  1212. }
  1213. }
  1214. //PPT
  1215. func displayViewControllerDidGotoSlideShow(_ controller: KMNDisplayViewController) {
  1216. togglePresentation(nil)
  1217. }
  1218. //SplitView
  1219. func displayViewControllerDidSplitModeChanged(_ controller: KMNDisplayViewController) {
  1220. reloadPDFSplitInfo()
  1221. }
  1222. func displayViewControllerDidSplitFileChanged(_ controller: KMNDisplayViewController) {
  1223. splitPDFController?.reloadData()
  1224. }
  1225. func displayViewControllerDidToolbarStateChanged(_ controller: KMNDisplayViewController) {
  1226. splitPDFController?.refreshToolbarState()
  1227. reloadPDFBottomToolbar()
  1228. }
  1229. }
  1230. //MARK: - PPT
  1231. extension KMMainViewController: KMInteractionProviderProtocol {
  1232. func providerContentView(fullScreenWindow: NSWindow, inset: CGFloat) -> NSView? {
  1233. if(interactionMode == .presentation) {
  1234. if listView.presentationDrawView == nil {
  1235. listView.createPresentationDraw()
  1236. }
  1237. presentationTopViewController = KMPresentationTopViewController.init(nibName: "KMPresentationTopViewController", bundle: nil)
  1238. presentationTopViewController?.pdfView = listView
  1239. presentationTopViewController?.delegate = self
  1240. presentationTopViewController?.isSelectionPre = true
  1241. listView.isPresentationMode = true
  1242. presentationTopViewController?.view.frame = CGRect(x: 0, y: (fullScreenWindow.contentView?.bounds.height ?? 0) - 42, width: fullScreenWindow.contentView?.bounds.width ?? 0, height: 42)
  1243. if((presentationTopViewController) != nil) {
  1244. fullScreenWindow.contentView?.addSubview(presentationTopViewController!.view)
  1245. }
  1246. } else {
  1247. listView.frame = NSInsetRect(fullScreenWindow.contentView?.bounds ?? .zero, 0, 0)
  1248. }
  1249. fullScreenWindow.contentView?.addSubview(listView)
  1250. if(interactionMode == .presentation) {
  1251. let frame = fullScreenWindow.frame
  1252. listView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height-42)
  1253. listView.autoresizingMask = [.width, .maxYMargin]
  1254. }
  1255. return view
  1256. }
  1257. @objc func willEnterInteractionModeNotification(_ sender: Notification) {
  1258. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1259. return
  1260. }
  1261. let interactionMode = sender.userInfo?[NSWindow.UserInfo.interactionModeKey] as? KMInteractionMode
  1262. if interactionMode == .presentation {
  1263. let backgroundColor = NSColor.black
  1264. let level = UserDefaults.standard.bool(forKey: "SKUseNormalLevelForPresentationKey") ? NSWindow.Level.normal : NSWindow.Level.popUpMenu
  1265. let wasInteractionMode = self.interactionMode
  1266. if wasInteractionMode == .normal {
  1267. self.savedNormalSetup.setDictionary(self.currentPDFSettings() as! [AnyHashable : Any])
  1268. }
  1269. if wasInteractionMode == .legacyFullScreen {
  1270. self.enterPresentationMode()
  1271. self.listView.frame = (self.view.window?.contentView?.bounds)!
  1272. self.view.window?.contentView?.addSubview(listView)
  1273. // self.view.window?.backgroundColor = backgroundColor
  1274. self.view.window?.level = level
  1275. self.listView.layoutDocumentView()
  1276. self.listView.requiresDisplay()
  1277. self.forceSubwindowsOnTop(false)
  1278. }
  1279. } else {
  1280. KMPrint("2")
  1281. }
  1282. }
  1283. @objc func didEnterInteractionModeNotification(_ sender: Notification) {
  1284. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1285. return
  1286. }
  1287. if self.interactionMode == .presentation {
  1288. self.listView.layoutDocumentView()
  1289. self.listView.requiresDisplay()
  1290. }
  1291. }
  1292. @objc func willShowFullScreenNotification(_ sender: Notification) {
  1293. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1294. return
  1295. }
  1296. if self.interactionMode == .presentation {
  1297. let view = self.view.window?.firstResponder as? NSView
  1298. if let data = view?.isDescendant(of: self.pdfSplitView), data {
  1299. self.view.window?.makeFirstResponder(nil)
  1300. }
  1301. }
  1302. }
  1303. @objc func didShowFullScreenNotification(_ sender: Notification) {
  1304. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1305. return
  1306. }
  1307. if self.interactionMode == .presentation {
  1308. self.enterPresentationMode()
  1309. }
  1310. }
  1311. }
  1312. // MARK: -KMPresentationTopViewControllerDelegate (幻灯片)
  1313. extension KMMainViewController: KMPresentationTopViewControllerDelegate {
  1314. func presentationTopViewExit(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  1315. self.exitFullScreen()
  1316. }
  1317. func presentationTopViewClear(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  1318. listView.presentationDrawView?.clear()
  1319. }
  1320. func presentationTopViewUndo(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  1321. let presentationDrawView = listView.presentationDrawView
  1322. if presentationDrawView?.canUndo() == true {
  1323. presentationDrawView?.undo()
  1324. }
  1325. }
  1326. func presentationTopViewType(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton, isSeletion: Bool) {
  1327. listView.isPresentationMode = isSeletion
  1328. if listView.isEnterPresentationDrawMode() == true {
  1329. listView.exitPresentationDrawMode()
  1330. }
  1331. }
  1332. func presentationTopViewDrawColor(_ presentationTopViewController: KMPresentationTopViewController, withView: NSView,color:NSColor?) {
  1333. if color == nil{
  1334. listView.exitPresentationDrawMode()
  1335. } else {
  1336. if listView.isEnterPresentationDrawMode() == false {
  1337. listView.enterPresentationDrawMode()
  1338. }
  1339. listView.changePresentationDrawModelColor(color)
  1340. }
  1341. }
  1342. }
  1343. //MARK: - KMSplitPDFViewControllerDelegate SplitPDFView分屏视图
  1344. extension KMMainViewController: KMSplitPDFViewControllerDelegate {
  1345. func splitPDFViewControllerDidUpdateFilePath(_ controller: KMSplitPDFViewController) {
  1346. displaySettingController?.reloadData()
  1347. }
  1348. func splitPDFViewControllerDidUpdatePDFScale(_ controller: KMSplitPDFViewController) {
  1349. if let scaleFactor = controller.pdfView?.scaleFactor {
  1350. listView.scaleFactor = scaleFactor
  1351. }
  1352. }
  1353. func splitPDFViewControllerDidUpdatePDFPageIndex(_ controller: KMSplitPDFViewController) {
  1354. if let pageIndex = controller.pdfView?.currentPageIndex {
  1355. listView.go(toPageIndex: pageIndex, animated: false)
  1356. }
  1357. }
  1358. }
  1359. //MARK: - Edit相关代理
  1360. extension KMMainViewController: KMEditToolbarViewDelegate {
  1361. func kmEditToolbarViewDidUpdateMode(_ view: KMEditToolbarView) {
  1362. if view.editType == .watermark {
  1363. watermarkViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1364. watermarkViewController?.reloadData()
  1365. } else if view.editType == .background {
  1366. backgroundViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1367. backgroundViewController?.reloadData()
  1368. } else if view.editType == .header_Footer {
  1369. headerFooterViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1370. headerFooterViewController?.reloadData()
  1371. } else if view.editType == .bates {
  1372. batesViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1373. batesViewController?.reloadData()
  1374. }
  1375. }
  1376. func kmEditToolbarViewDidChooseBatch(_ view: KMEditToolbarView) {
  1377. }
  1378. func kmEditToolbarViewDidChooseApply(_ view: KMEditToolbarView) {
  1379. let pageIndex = view.getSelectedPageIndex(listView.document)
  1380. if pageIndex.isEmpty {
  1381. let alert = NSAlert()
  1382. alert.alertStyle = .critical
  1383. alert.messageText = KMLocalizedString("Invalid page range or the page number is out of range. Please try again.")
  1384. alert.runModal()
  1385. return
  1386. }
  1387. let pageString = view.getSelectedPageString(listView.document, pageIndex)
  1388. if view.editType == .watermark {
  1389. if let model = watermarkViewController?.currentWatermarkData {
  1390. let watermark = KMPDFWatermarkData.returnWaterMarkWith(model, listView.document)
  1391. watermark.pageString = pageString
  1392. listView.document.addWatermark(watermark)
  1393. listView.layoutDocumentView()
  1394. }
  1395. exitEditToolbarView()
  1396. } else if view.editType == .background {
  1397. if let model = backgroundViewController?.backgroundModel {
  1398. if let background = listView.document.background() {
  1399. KMBackgroundManager.defaultManager.updateBackground(background, withModel: model)
  1400. background.pageString = pageString
  1401. background.update()
  1402. listView.document?.refreshPageData()
  1403. listView.layoutDocumentView()
  1404. }
  1405. }
  1406. exitEditToolbarView()
  1407. } else if view.editType == .header_Footer {
  1408. exitEditToolbarView()
  1409. } else if view.editType == .bates {
  1410. exitEditToolbarView()
  1411. }
  1412. }
  1413. func kmEditToolbarViewDidChooseExit(_ view: KMEditToolbarView) {
  1414. if view.editType == .watermark {
  1415. if view.applyEnable {
  1416. let alert = NSAlert()
  1417. alert.messageText = NSLocalizedString("There are unapplied watermark settings, do you want to apply them?", comment: "")
  1418. alert.informativeText = NSLocalizedString("If not, the changes will be lost.", comment: "")
  1419. alert.addButton(withTitle: NSLocalizedString("Apply", comment: ""))
  1420. alert.addButton(withTitle: NSLocalizedString("Don't Apply", comment: ""))
  1421. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  1422. let result = alert.runModal()
  1423. if (result == .alertFirstButtonReturn) {
  1424. self.kmEditToolbarViewDidChooseApply(view)
  1425. } else if (result == .alertSecondButtonReturn) {
  1426. //"Don't Apply"
  1427. exitEditToolbarView()
  1428. } else if (result == .alertThirdButtonReturn) {
  1429. //Cancel
  1430. }
  1431. } else {
  1432. exitEditToolbarView()
  1433. }
  1434. } else if view.editType == .background {
  1435. exitEditToolbarView()
  1436. } else if view.editType == .header_Footer {
  1437. exitEditToolbarView()
  1438. } else if view.editType == .bates {
  1439. exitEditToolbarView()
  1440. }
  1441. }
  1442. }
  1443. //MARK: - KMWatermarkControllerDelegate 水印相关代理
  1444. extension KMMainViewController: KMWatermarkControllerDelegate {
  1445. func kmWatermarkControllerDidUpdateMode(_ view: KMWatermarkController) {
  1446. editToolbarView?.editSubType = view.editSubType
  1447. editToolbarView?.reloadData()
  1448. }
  1449. func kmWatermarkControllerDidWatermarkUpdated(_ view: KMWatermarkController) {
  1450. var applyEnable = true
  1451. if let model = view.currentWatermarkData {
  1452. if model.watermarkType == .text {
  1453. if model.text == nil || model.text?.count == 0 {
  1454. applyEnable = false
  1455. }
  1456. } else if model.watermarkType == .image {
  1457. if model.imagePath == nil {
  1458. applyEnable = false
  1459. }
  1460. }
  1461. } else {
  1462. applyEnable = false
  1463. }
  1464. editToolbarView?.applyEnable = applyEnable
  1465. editToolbarView?.reloadData()
  1466. }
  1467. }
  1468. //MARK: - KMBackgroundControllerDelegate 背景代理
  1469. extension KMMainViewController: KMBackgroundControllerDelegate {
  1470. func kmBackgroundControllerDidUpdateMode(_ view: KMBackgroundController) {
  1471. editToolbarView?.editSubType = view.editSubType
  1472. }
  1473. func kmBackgroundControllerDidWatermarkUpdated(_ view: KMBackgroundController) {
  1474. var applyEnable = true
  1475. if let model = view.backgroundModel {
  1476. if model.type == .image {
  1477. if model.imagePath == nil {
  1478. applyEnable = false
  1479. }
  1480. }
  1481. } else {
  1482. applyEnable = false
  1483. }
  1484. editToolbarView?.applyEnable = applyEnable
  1485. editToolbarView?.reloadData()
  1486. }
  1487. }
  1488. //MARK: - KMHeaderFooterControllerDelegate 页眉页脚代理
  1489. extension KMMainViewController: KMHeaderFooterControllerDelegate {
  1490. func kmHeaderFooterControllerDidUpdateMode(_ view: KMHeaderFooterController) {
  1491. editToolbarView?.editSubType = view.editSubType
  1492. }
  1493. func kmHeaderFooterControllerDidModelDataUpdated(_ view: KMHeaderFooterController) {
  1494. var applyEnable = true
  1495. if let model = view.headerFooterModel {
  1496. if model.topLeftString.count == 0 && model.topCenterString.count == 0 && model.topRightString.count == 0 &&
  1497. model.bottomLeftString.count == 0 && model.bottomCenterString.count == 0 && model.bottomRightString.count == 0 {
  1498. applyEnable = false
  1499. }
  1500. } else {
  1501. applyEnable = false
  1502. }
  1503. editToolbarView?.applyEnable = applyEnable
  1504. editToolbarView?.reloadData()
  1505. }
  1506. }
  1507. //MARK: - KMBatesControllerDelegate Bates贝茨码代理
  1508. extension KMMainViewController: KMBatesControllerDelegate {
  1509. func kmBatesControllerDidUpdateMode(_ view: KMBatesController) {
  1510. editToolbarView?.editSubType = view.editSubType
  1511. editToolbarView?.reloadData()
  1512. }
  1513. func kmBatesControllerDidModelDataUpdated(_ view: KMBatesController) {
  1514. var applyEnable = true
  1515. if let model = view.batesModel {
  1516. if model.topLeftString.count == 0 && model.topCenterString.count == 0 && model.topRightString.count == 0 &&
  1517. model.bottomLeftString.count == 0 && model.bottomCenterString.count == 0 && model.bottomRightString.count == 0 {
  1518. applyEnable = false
  1519. }
  1520. } else {
  1521. applyEnable = false
  1522. }
  1523. editToolbarView?.applyEnable = applyEnable
  1524. editToolbarView?.reloadData()
  1525. }
  1526. }
  1527. //MARK: - CPDFViewDelegate PDFView代理
  1528. extension KMMainViewController: CPDFViewDelegate,CPDFListViewDelegate {
  1529. func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
  1530. sideBarController?.reloadPageTurnerData()
  1531. documentLoadComplete()
  1532. }
  1533. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  1534. sideBarController?.reloadPageTurnerData()
  1535. var indexpaths: Set<IndexPath> = []
  1536. indexpaths.insert(IndexPath(item: listView.currentPageIndex, section: 0))
  1537. botaViewController?.thumnailViewController?.selectionIndexPaths = indexpaths
  1538. //分屏视图
  1539. reloadPDFBottomToolbar()
  1540. if viewManager.splitSyncScroll {
  1541. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  1542. splitPDFController?.outPDFFirst = true
  1543. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  1544. var index = pdfView.currentPageIndex
  1545. if index > document.pageCount {
  1546. index = Int(splitPDFView.document.pageCount - 1)
  1547. }
  1548. splitPDFView.go(toPageIndex: index, animated: false)
  1549. }
  1550. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  1551. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  1552. } else if splitPDFController?.outPDFFirst == true {
  1553. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  1554. var index = pdfView.currentPageIndex
  1555. if index > document.pageCount {
  1556. index = Int(splitPDFView.document.pageCount - 1)
  1557. }
  1558. splitPDFView.go(toPageIndex: index, animated: false)
  1559. }
  1560. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  1561. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  1562. }
  1563. }
  1564. //水印
  1565. updateWatermarkDocument()
  1566. //
  1567. }
  1568. func pdfViewScaleDidChanged(_ pdfView: CPDFView!) {
  1569. pdfToolbarController?.reloadSelectZoomView()
  1570. reloadPDFBottomToolbar()
  1571. //分屏视图
  1572. if viewManager.splitSyncScroll {
  1573. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  1574. splitPDFController?.outPDFFirst = true
  1575. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  1576. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  1577. } else if splitPDFController?.outPDFFirst == true {
  1578. if let splitPDFView = splitPDFController?.pdfView {
  1579. splitPDFView.scaleFactor = pdfView.scaleFactor
  1580. }
  1581. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  1582. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  1583. }
  1584. }
  1585. }
  1586. func pdfViewDidClick(onLink pdfView: CPDFView!, withURL url: String!) {
  1587. if let urlString = url, urlString == kKMPurchaseProductURLString {
  1588. //跳转订阅比较表
  1589. return
  1590. }
  1591. if url.hasPrefix("smb://") {
  1592. NSWorkspace.shared.openFile(url)
  1593. } else {
  1594. KMTools.openURL(urlString: url)
  1595. }
  1596. }
  1597. func pdfViewPerformURL(_ pdfView: CPDFView!, withContent content: String!) {
  1598. KMPrint("pdfViewPerformURL")
  1599. if content != nil {
  1600. NSWorkspace.shared.open(URL(string: content)!)
  1601. } else {
  1602. let alert = NSAlert()
  1603. alert.alertStyle = .critical
  1604. alert.informativeText = NSLocalizedString("The hyperlink is invalid.", comment: "")
  1605. alert.messageText = ""
  1606. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1607. alert.runModal()
  1608. return
  1609. }
  1610. }
  1611. func pdfViewPerformPrint(_ pdfView: CPDFView!) {
  1612. KMPrint("pdfViewPerformPrint")
  1613. self.showPrintWindow()
  1614. }
  1615. func pdfViewPerformGo(toPage pdfView: CPDFView!) {
  1616. KMPrint("pdfViewPerformGo")
  1617. }
  1618. func pdfViewOpenPDF(_ pdfView: CPDFView!, forRemoteGoTo action: CPDFAction!) {
  1619. KMPrint("pdfViewOpenPDF")
  1620. }
  1621. func pdfViewPerformReset(_ pdfView: CPDFView!) {
  1622. KMPrint("pdfViewPerformReset")
  1623. pdfView.document?.resetForm()
  1624. }
  1625. func pdfViewEditingBlockDidChanged(_ pdfView: CPDFView!) {
  1626. KMPrint("pdfViewEditingBlockDidChanged")
  1627. }
  1628. func pdfViewAsBookBookmark() -> NSImage! {
  1629. return NSImage(named: "KMImageNameBookmarkIcon")!
  1630. }
  1631. //MARK: -编辑模块
  1632. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  1633. // 文本区块 选中文本已经变化
  1634. }
  1635. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  1636. //编辑模块变化
  1637. rightSideController?.reloadDataWithPDFView(pdfView: (pdfView as! CPDFListView))
  1638. if pdfView is CPDFListView {
  1639. (pdfView as! CPDFListView).isEditImage = false
  1640. }
  1641. }
  1642. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  1643. if editArea != nil && (editArea is CPDFEditImageArea){
  1644. self.listView.cropAreas = editArea as? CPDFEditImageArea
  1645. }
  1646. }
  1647. //编辑PDF 创建图片区域回调
  1648. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1649. if (pdfView as! CPDFListView).isEditImage {
  1650. return
  1651. }
  1652. if rect.size.width < 5 && rect.size.height < 5 {
  1653. pdfView.updateEditing([])
  1654. return
  1655. }
  1656. let panel = NSOpenPanel()
  1657. panel.allowsMultipleSelection = false
  1658. panel.allowedFileTypes = ["png","jpg"]
  1659. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1660. if response == .OK {
  1661. var filePath = panel.url?.path
  1662. let image = NSImage.init(contentsOf: panel.url!)
  1663. //图片自适应范围
  1664. if image != nil {
  1665. var imageRect = rect
  1666. let imageSize = image!.size
  1667. var previewSize = rect.size
  1668. var isChangeSize = false
  1669. if previewSize.width == 0 && previewSize.height == 0 {
  1670. previewSize = CGSize(width: 500, height: 500)
  1671. isChangeSize = true
  1672. }
  1673. var scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  1674. if scale < 1 { // 大于 500
  1675. } else {
  1676. let wh = max(imageSize.width, imageSize.height)
  1677. if wh >= 72 {
  1678. scale = min(scale, 1)
  1679. } else {
  1680. scale = min(72 / imageSize.width, 72 / imageSize.height)
  1681. }
  1682. }
  1683. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  1684. if isChangeSize {
  1685. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  1686. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  1687. } else {
  1688. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  1689. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  1690. }
  1691. imageRect.size = newSize
  1692. let limitWidth = 1920.0
  1693. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  1694. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  1695. targetSize: CGSize(width: limitWidth, height: limitWidth),
  1696. maxSizeInBytes: 1024 * 1024 * 5,
  1697. targetCompression: 1.0)
  1698. }
  1699. //自适应page
  1700. let pageRect = self.listView.currentPage().bounds ?? .zero
  1701. if imageRect.width > pageRect.width ||
  1702. imageRect.height > pageRect.height {
  1703. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  1704. imageRect = CGRect(x: imageRect.origin.x,
  1705. y: imageRect.origin.y,
  1706. width: imageRect.width * pageScale,
  1707. height: imageRect.height * pageScale)
  1708. }
  1709. if imageRect.origin.x < 0 {
  1710. imageRect.origin.x = 5
  1711. }
  1712. if imageRect.origin.y < 0 {
  1713. imageRect.origin.y = 5
  1714. }
  1715. if imageRect.origin.x + imageRect.width > pageRect.width ||
  1716. imageRect.origin.y + imageRect.height > pageRect.height {
  1717. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  1718. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  1719. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  1720. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  1721. }
  1722. DispatchQueue.main.async {
  1723. self.listView.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  1724. }
  1725. }
  1726. }
  1727. }
  1728. }
  1729. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  1730. if rect.size.width < 5 && rect.size.height < 5 {
  1731. let areas = self.listView.km_EditingAreas()
  1732. if let area = areas.last {
  1733. if let data = area as? CPDFEditTextArea {
  1734. if let str = data.editTextAreaString(), str.isEmpty {
  1735. self.listView.remove(with: area)
  1736. } else {
  1737. self.listView.updateEditing([])
  1738. }
  1739. }
  1740. }
  1741. return
  1742. }
  1743. var newRect = rect
  1744. if rect.size.equalTo(.zero) {
  1745. newRect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  1746. } else {
  1747. newRect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  1748. }
  1749. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  1750. let fontSize = model.fontSize
  1751. let fontColor = model.color
  1752. let fontAlign = model.alignment
  1753. NSColorPanel.shared.color = fontColor
  1754. let cfont = CPDFFont(familyName: model.fontName, fontStyle: model.fontStyle)
  1755. let fontNameZ = CPDFFont.convertAppleFont(cfont)
  1756. let font = NSFont(name: fontNameZ ?? "Helvetica", size: fontSize)
  1757. let attri = CEditAttributes()
  1758. attri.font = font!
  1759. attri.fontColor = fontColor
  1760. attri.alignment = fontAlign
  1761. attri.isBold = model.bold
  1762. attri.isItalic = model.italic
  1763. self.listView.createStringBounds(newRect, with: attri, page: page)
  1764. }
  1765. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1766. rightSideController?.reloadEditingAreas()
  1767. }
  1768. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1769. rightSideController?.reloadEditingAreas()
  1770. }
  1771. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  1772. rightSideController?.reloadEditingAreas()
  1773. }
  1774. func pdfViewEditingSelectCharDidChanged(_ pdfView: CPDFView!) {
  1775. rightSideController?.reloadEditingAreas()
  1776. }
  1777. func pdfViewEditingExitCropMode(_ pdfView: CPDFView!, forEditing editingArea: CPDFEditImageArea!) {
  1778. rightSideController?.reloadEditingAreas()
  1779. }
  1780. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  1781. let areas = self.listView.km_editingImageAreas()
  1782. if areas.count == 1 {
  1783. if let data = areas.first as? CPDFEditImageArea {
  1784. let updating = self.listView.editAreaBoundUpdating
  1785. if updating {
  1786. self.listView.editAreaBoundUpdating = false
  1787. }
  1788. }
  1789. }
  1790. }
  1791. func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  1792. }
  1793. func pdfListViewKeyDownIsContinue(_ pdfListView: CPDFListView!, theEvent: NSEvent!) -> Bool {
  1794. let command = theEvent.modifierFlags.contains(.command)
  1795. let control = theEvent.modifierFlags.contains(.control)
  1796. KMPrint(theEvent.keyCode)
  1797. if self.listView.isEditing() == true {
  1798. if control && theEvent.keyCode == 11 { // ctr + b
  1799. self.listView.setEditingTextarea_Bold()
  1800. rightSideController?.reloadEditingAreas()
  1801. return false
  1802. } else if control && theEvent.keyCode == 34 { // ctr +i
  1803. self.listView.setEditingTextarea_Italic()
  1804. rightSideController?.reloadEditingAreas()
  1805. return false
  1806. } else if theEvent.keyCode == 36 { // enter
  1807. if self.listView.isCropMode {
  1808. self.listView.cropComfirmAction()
  1809. rightSideController?.reloadEditingAreas()
  1810. return false
  1811. }
  1812. } else if theEvent.keyCode == 53 { // Cancel
  1813. if self.listView.isCropMode {
  1814. self.listView.cropCancelAction()
  1815. rightSideController?.reloadEditingAreas()
  1816. return false
  1817. }
  1818. }
  1819. }
  1820. if (theEvent.keyCode == 11 && command) { // command + B [添加书签]
  1821. self.menuItemBookMarkClick_add(sender: NSMenuItem())
  1822. return false
  1823. } else if (command && control && theEvent.keyCode == 14) { // command + control + E [注释 橡皮擦]
  1824. return false
  1825. } else if (theEvent.keyCode == 123) { // 向左
  1826. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  1827. return false
  1828. } else {
  1829. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage()) {
  1830. self.listView.goToPreviousPage(nil)
  1831. return false
  1832. }
  1833. }
  1834. } else if (theEvent.keyCode == 126) { // 向上
  1835. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  1836. return false
  1837. } else {
  1838. if (self.listView.isContinousScroll()) {
  1839. return true
  1840. }
  1841. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToPreviousPage()) {
  1842. self.listView.goToPreviousPage(nil)
  1843. return false
  1844. }
  1845. }
  1846. } else if (theEvent.keyCode == 124) { // 向右
  1847. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  1848. return false
  1849. } else {
  1850. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage()) {
  1851. self.listView.goToNextPage(nil)
  1852. return false
  1853. }
  1854. }
  1855. } else if (theEvent.keyCode == 125) { // 向下
  1856. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  1857. return false
  1858. } else {
  1859. if (self.listView.isContinousScroll()) {
  1860. return true
  1861. }
  1862. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToNextPage()) {
  1863. self.listView.goToNextPage(nil)
  1864. return false
  1865. }
  1866. }
  1867. } else if (theEvent.keyCode == 36) {
  1868. if self.listView.annotationType == .addImage || self.listView.annotationType == .addText {
  1869. if self.listView.isEditImage {
  1870. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  1871. }
  1872. }
  1873. }
  1874. if theEvent.keyCode == 53 {
  1875. //ESC
  1876. // self.exitFullScreen()
  1877. if viewManager.isPDFReadMode {
  1878. self.exitPDFReadMode()
  1879. }
  1880. self.leftSideViewCancelSelect()
  1881. }
  1882. return true
  1883. }
  1884. func pdfListViewMenuValidate(_ pdfListView: CPDFListView!, menuItem: NSMenuItem!, isTakesEffect: UnsafeMutablePointer<ObjCBool>!) -> Bool {
  1885. guard let action = menuItem.action else {
  1886. isTakesEffect.pointee = false
  1887. return false
  1888. }
  1889. if (KMSystemMenu.isEditSelector(sel: action)) {
  1890. if (KMSystemMenu.Edit.deleteSelector == action) {
  1891. isTakesEffect.pointee = true
  1892. return self.listView.activeAnnotations.count > 0
  1893. } else if (KMSystemMenu.Edit.copySelector == action) {
  1894. isTakesEffect.pointee = true
  1895. return true//self.listView.canCopy()
  1896. } else if (KMSystemMenu.Edit.cutSelector == action) {
  1897. isTakesEffect.pointee = true
  1898. return self.listView.canCopy()
  1899. } else if (KMSystemMenu.Edit.pasteSelector == action) {
  1900. isTakesEffect.pointee = true
  1901. return self.listView.canPaste()
  1902. }
  1903. }
  1904. isTakesEffect.pointee = false
  1905. return false
  1906. }
  1907. //MARK: - CPDFListViewDelegate
  1908. func cPDFListView(_ pdfListView: CPDFListView, didDelete annotation: CPDFAnnotation, in pdfPage: CPDFPage) {
  1909. self.leftSideViewController.updateThumbnail(at: Int(pdfPage.pageIndex()))
  1910. }
  1911. func pdfListViewChangeatioActiveAnnotations(_ pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!, isRightMenu: Bool) {
  1912. self.view.window?.makeFirstResponder(self.listView)
  1913. if isRightMenu {
  1914. } else if annotations.count > 0 {
  1915. if annotations.count > 1 {
  1916. let fristAnnotation = annotations.first
  1917. var isSameAnnotation = true
  1918. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  1919. for annotation in annotations {
  1920. let cunrrentClassName = NSStringFromClass(annotation.classForCoder)
  1921. if (className == "CPDFSquareAnnotation") ||
  1922. (className == "CPDFCircleAnnotation") ||
  1923. (className == "CPDFLineAnnotation") {
  1924. if (cunrrentClassName != "CPDFSquareAnnotation") &&
  1925. (cunrrentClassName != "CPDFCircleAnnotation") &&
  1926. (cunrrentClassName != "CPDFLineAnnotation") {
  1927. isSameAnnotation = false
  1928. }
  1929. } else {
  1930. if className != cunrrentClassName {
  1931. isSameAnnotation = false
  1932. }
  1933. }
  1934. }
  1935. if isSameAnnotation == false {
  1936. } else {
  1937. self.openRightPane()
  1938. }
  1939. self.rightSideController?.reloadDataWithPDFView(pdfView: pdfListView)
  1940. } else {
  1941. let fristAnnotation = annotations.first
  1942. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  1943. if self.viewManager.isPDFReadMode {
  1944. self.closeRightPane()
  1945. } else {
  1946. if className != "CPDFStampAnnotation" &&
  1947. className != "CPDFSignatureAnnotation" &&
  1948. className != "CPDFListStampAnnotation" {
  1949. self.openRightPane()
  1950. }
  1951. self.rightSideController?.reloadDataWithPDFView(pdfView: pdfListView)
  1952. }
  1953. }
  1954. if (listView.activeAnnotation.isKind(of: CPDFLineAnnotation.self)) {
  1955. if (!(listView.activeAnnotation as! CPDFLineAnnotation).isMeasure) {
  1956. cancelMeasureType()
  1957. } else {
  1958. if distanceMeasureInfoWindowController == nil {
  1959. let measureInfo = CPDFDistanceMeasureInfo()
  1960. distanceMeasureInfoWindowController = CDistanceMeasureInfoWindowController()
  1961. distanceMeasureInfoWindowController?.measureInfo = measureInfo
  1962. distanceMeasureInfoWindowController?.delegate = self
  1963. }
  1964. }
  1965. } else if (!listView.activeAnnotation.isKind(of: CPDFPolygonAnnotation.self) && !listView.activeAnnotation.isKind(of: CPDFPolylineAnnotation.self)) {
  1966. cancelMeasureType()
  1967. } else if (listView.activeAnnotation.isKind(of: CPDFPolygonAnnotation.self) || listView.activeAnnotation.isKind(of: CPDFPolylineAnnotation.self)) {
  1968. if perimeterMeasureInfoWindowController == nil {
  1969. let measureInfo = CPDFPerimeterMeasureInfo()
  1970. perimeterMeasureInfoWindowController = CPerimeterMeasureInfoWindowController()
  1971. perimeterMeasureInfoWindowController?.measureInfo = measureInfo
  1972. perimeterMeasureInfoWindowController?.delegate = self
  1973. }
  1974. if areaMeasureInfoWindowController == nil {
  1975. let measureInfo = CPDFAreaMeasureInfo()
  1976. areaMeasureInfoWindowController = CAreaMeasureInfoWindowController()
  1977. areaMeasureInfoWindowController?.measureInfo = measureInfo
  1978. areaMeasureInfoWindowController?.delegate = self
  1979. }
  1980. }
  1981. } else if (annotations.count == 0){
  1982. if pdfListView.annotationType == .unkown {
  1983. self.closeRightPane()
  1984. } else {
  1985. if self.viewManager.isPDFReadMode {
  1986. self.closeRightPane()
  1987. } else {
  1988. self.openRightPane()
  1989. }
  1990. }
  1991. }
  1992. }
  1993. func pdfListViewChangedAnnotationType(_ pdfListView: CPDFListView!, for annotationType: CAnnotationType) {
  1994. if(annotationType == .unkown) {
  1995. self.closeRightPane()
  1996. }
  1997. let aType = annotationType
  1998. if aType.isMarkup() || aType == .anchored || aType == .freeText || aType.isSquare() || aType == .link {
  1999. KMDataManager.ud_set(annotationType.rawValue, forKey: SKLastAnnotationModeKey)
  2000. }
  2001. }
  2002. ///开始定位链接注释
  2003. func pdfListViewLinkDestinationStart(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  2004. }
  2005. ///刷新链接注释
  2006. func pdfListViewLinkDestinationEnd(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  2007. }
  2008. func pdfListViewMenuItemsEditing(at point: CGPoint, for page: CPDFPage!, menuItems: [NSMenuItem]!) -> [NSMenuItem]! {
  2009. if (listView.toolMode != CToolMode.editPDFToolMode) {
  2010. return menuItems
  2011. }
  2012. var tMenuItems = menuItems;
  2013. if(listView.isSelectEditCharRange() ||
  2014. listView.isSelecteditArea(with: point)) {
  2015. tMenuItems?.append(NSMenuItem.separator())
  2016. }
  2017. let areas = self.listView.editingAreas() ?? []
  2018. if areas.count == 1 {
  2019. let fristAreas = areas.first
  2020. if fristAreas is CPDFEditImageArea {
  2021. self.listView.selectImageAreas = fristAreas as? CPDFEditImageArea
  2022. if self.listView.isEditImage {
  2023. tMenuItems?.removeAll()
  2024. tMenuItems?.append(self.corpImageMenuItem())
  2025. tMenuItems?.append(self.cancelCorpImageMenuItem())
  2026. tMenuItems?.append(self.restoreCorpImageMenuItem())
  2027. } else {
  2028. tMenuItems?.append(NSMenuItem.separator())
  2029. tMenuItems?.append(self.cutImageArea())
  2030. tMenuItems?.append(self.replaceImageArea())
  2031. tMenuItems?.append(self.exportImageArea())
  2032. }
  2033. } else {
  2034. if tMenuItems?.count != 1 {
  2035. tMenuItems?.swapAt(0, 1)
  2036. }
  2037. }
  2038. } else if areas.count == 0 {
  2039. tMenuItems?.append(NSMenuItem.separator())
  2040. tMenuItems?.append(self.addText())
  2041. tMenuItems?.append(self.addImage())
  2042. }
  2043. return tMenuItems
  2044. }
  2045. func pdfListViewMenu(forEvent pdfListView: CPDFListView!, for theEvent: NSEvent!, click menu: AutoreleasingUnsafeMutablePointer<NSMenu?>!, isMoveSelectAnno: Bool) {
  2046. self.mouseRightMenuEvent = theEvent
  2047. var currentMenu : NSMenu = menu.pointee!
  2048. if let activeAnno = listView.activeAnnotation as? KMTableAnnotation {//Table
  2049. var pagePoint = NSPoint()
  2050. _ = self.listView.pageAndPoint(&pagePoint, for: theEvent, nearest: true)
  2051. currentMenu.removeAllItems()
  2052. let annotation = activeAnno
  2053. annotation.completeEditCellText()
  2054. if !(NSIsEmptyRect(annotation.drawRect)) {
  2055. annotation.drawLine(pagePoint)
  2056. NotificationCenter.default.post(name: NSNotification.Name.KMPDFViewTableAnnotationDidChange, object: self, userInfo: ["point": NSValue(point: pagePoint)])
  2057. }
  2058. if (annotation.rowNumber - annotation.currentCell.row - 1) < 0 {
  2059. return
  2060. }
  2061. currentMenu = tableMenu(currentMenu, withTable: listView.activeAnnotation as! KMTableAnnotation, point: pagePoint)
  2062. listView.needsDisplay = true
  2063. return
  2064. }
  2065. var pagePoint: NSPoint = .zero
  2066. if let page = self.listView.pageAndPoint(&pagePoint, for: theEvent, nearest: true) {
  2067. let anno = page.annotation(at: pagePoint)
  2068. let item1 = NSMenuItem(title: NSLocalizedString("Delete", comment: ""), action: #selector(menuItemActionMeasureDelete), target: self)
  2069. item1.representedObject = anno
  2070. let item2 = NSMenuItem(title: NSLocalizedString("Edit Note", comment: ""), action: #selector(menuItemActionMeasureEditNote), target: self)
  2071. item2.representedObject = anno
  2072. let item3 = NSMenuItem(title: NSLocalizedString("Settings", comment: ""), action: #selector(menuItemActionMeasureSetting), target: self)
  2073. item3.representedObject = anno
  2074. if let data = anno as? CPDFPolygonAnnotation { // 多变形
  2075. currentMenu.removeAllItems()
  2076. currentMenu.insertItem(item1, at: 0)
  2077. currentMenu.insertItem(item2, at: 1)
  2078. currentMenu.insertItem(item3, at: 2)
  2079. return
  2080. }
  2081. if let data = anno as? CPDFPolylineAnnotation {
  2082. currentMenu.removeAllItems()
  2083. currentMenu.insertItem(item1, at: 0)
  2084. currentMenu.insertItem(item2, at: 1)
  2085. currentMenu.insertItem(item3, at: 2)
  2086. return
  2087. }
  2088. if let data = anno as? CPDFLineAnnotation, data.isMeasure {
  2089. currentMenu.removeAllItems()
  2090. currentMenu.insertItem(item1, at: 0)
  2091. currentMenu.insertItem(item2, at: 1)
  2092. currentMenu.insertItem(item3, at: 2)
  2093. return
  2094. }
  2095. }
  2096. if (listView.toolMode == .selectToolMode){
  2097. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2098. currentMenu.insertItem(self.printingMenu(), at: 3)
  2099. currentMenu.insertItem(self.setTTSStype(), at: 3)
  2100. currentMenu.insertItem(self.setCropStype(), at: 3)
  2101. currentMenu.insertItem(self.setSnapshotStype(), at: 3)
  2102. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  2103. export.submenu = self.exportMenu()
  2104. currentMenu.insertItem(export, at: 3)
  2105. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2106. if listView.activeAnnotation == nil{
  2107. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  2108. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2109. }
  2110. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 3)
  2111. currentMenu.insertItem(self.addReadModelStype(), at: currentMenu.items.count - 3)
  2112. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2113. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  2114. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  2115. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  2116. return
  2117. }
  2118. if (listView.toolMode == .moveToolMode || listView.toolMode == .magnifyToolMode){
  2119. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2120. currentMenu.insertItem(self.setTTSStype(), at: 0)
  2121. currentMenu.insertItem(self.setCropStype(), at: 0)
  2122. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  2123. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2124. currentMenu.insertItem(self.addOutlineStype(), at: 0)
  2125. currentMenu.insertItem(self.addBookmarkMenu(), at: 0)
  2126. if listView.activeAnnotation == nil{
  2127. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2128. currentMenu.insertItem(self.setAnnotationToolStype(), at: 0)
  2129. }
  2130. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2131. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  2132. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  2133. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  2134. return
  2135. }
  2136. if currentMenu.items.count > 3 {
  2137. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 3)
  2138. currentMenu.insertItem(self.addReadModelStype(), at: currentMenu.items.count - 3)
  2139. }
  2140. if listView.currentSelection != nil && listView.activeAnnotations.count < 1{
  2141. if listView.currentSelection.selectionType() == .text {
  2142. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2143. currentMenu.insertItem(self.setSearchBaiduStype(), at: 3)
  2144. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2145. currentMenu.insertItem(self.setLookUpStype(), at: 3)
  2146. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2147. currentMenu.insertItem(self.addOutlineStype(), at: 3)
  2148. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2149. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  2150. currentMenu.insertItem(self.setTTSStype(), at: 3)
  2151. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2152. currentMenu.insertItem(self.setShareStype(), at: 3)
  2153. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2154. }
  2155. currentMenu.insertItem(self.enterAnnotationStype(), at: 3)
  2156. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2157. if listView.currentSelection.selectionType() == .image{
  2158. currentMenu.insertItem(NSMenuItem.separator(), at: 6)
  2159. currentMenu.insertItem(self.addOutlineStype(), at: 6)
  2160. currentMenu.insertItem(NSMenuItem.separator(), at: 6)
  2161. currentMenu.insertItem(self.setAnnotationToolStype(), at: 6)
  2162. }
  2163. if listView.currentSelection.selectionType() == .text {
  2164. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2165. currentMenu.insertItem(self.setTranslateStype(), at: currentMenu.items.count)
  2166. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2167. }
  2168. }
  2169. if listView.activeAnnotation != nil || isMoveSelectAnno {
  2170. if let data = self.listView.activeAnnotation?.type?.lowercased(), data == "stamp"{
  2171. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2172. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  2173. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2174. }else{
  2175. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2176. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  2177. if let anno = self.listView.activeAnnotation, anno.isKind(of: CPDFStampAnnotation.self) {
  2178. } else {
  2179. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2180. currentMenu.insertItem(self.setShareStype(), at: currentMenu.items.count - 15)
  2181. }
  2182. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2183. }
  2184. }
  2185. if listView.activeAnnotation == nil && listView.currentSelection == nil{
  2186. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2187. if(listView.toolMode == .selectToolMode) {
  2188. if NSIsEmptyRect(listView.currentSelectionRect()) {
  2189. currentMenu.insertItem(self.zoomSelectionMenuItem(), at: 0)
  2190. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2191. }
  2192. currentMenu.insertItem(self.printingMenu(), at: 0)
  2193. currentMenu.insertItem(self.setTTSStype(), at: 0)
  2194. currentMenu.insertItem(self.setCropStype(), at: 0)
  2195. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  2196. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  2197. export.submenu = self.exportMenu()
  2198. currentMenu.insertItem(export, at: currentMenu.items.count)
  2199. }else{
  2200. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2201. currentMenu.insertItem(self.setTTSStype(), at: 2)
  2202. currentMenu.insertItem(self.setCropStype(), at: 2)
  2203. currentMenu.insertItem(self.setSnapshotStype(), at: 2)
  2204. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2205. currentMenu.insertItem(self.addOutlineStype(), at: 2)
  2206. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2207. currentMenu.insertItem(self.enterAnnotationStype(), at: 2)
  2208. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2209. if(currentMenu.items.count > 4) {
  2210. currentMenu.insertItem(NSMenuItem.separator(), at: 5)
  2211. }
  2212. if(currentMenu.items.count > 5) {
  2213. currentMenu.insertItem(self.addBookmarkMenu(), at: 6)
  2214. }
  2215. currentMenu.insertItem(self.setAutoScrollStype(), at: currentMenu.items.count)
  2216. }
  2217. currentMenu.insertItem(self.setAnnotationToolStype(), at: 5)
  2218. }
  2219. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2220. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  2221. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  2222. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  2223. for item in currentMenu.items {
  2224. if (item.action == NSSelectorFromString("menuItemClick_HidenorShowNote:")) {
  2225. // 显示与隐藏注释 item action 截取
  2226. item.action = #selector(menuItemClick_HidenorShowNote)
  2227. item.target = self
  2228. break
  2229. }
  2230. }
  2231. }
  2232. func pdfListViewAddAnnotations(_ pdfListView: CPDFListView!, forAdd annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  2233. var addRedact = false
  2234. for anno in annotations {
  2235. if (anno.isKind(of: CPDFRedactAnnotation.self)) {
  2236. addRedact = true
  2237. } else if anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation {
  2238. anno.contents = pdfPage?.string(for: anno.bounds) ?? ""
  2239. }
  2240. }
  2241. self.model.hasAddRedact = addRedact
  2242. if self.listView.toolMode == .moveToolMode {
  2243. self.listView.toolMode = .textToolMode
  2244. self.listView.annotationType = .unkown
  2245. }
  2246. if (self.model.rightMouseEventing) {
  2247. self.model.rightMouseEventing = false
  2248. }
  2249. self.leftSideViewController.refreshUIForAddAnnotation(annos: annotations, page: pdfPage)
  2250. }
  2251. func pdfListViewRemoveAnnotations(_ pdfListView: CPDFListView!, forRemove annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  2252. self.leftSideViewController.annoList_refreshUIForDeleteAnnotations(annos: annotations, page: pdfPage)
  2253. }
  2254. func pdfListViewDidSelectionEnd(_ pdfListView: CPDFListView!) {
  2255. if (!self.listView.isEqual(to: pdfListView)) {
  2256. return
  2257. }
  2258. if (self.listView.toolMode != .selectToolMode) {
  2259. return
  2260. }
  2261. }
  2262. func pdfListViewKeyDowClosePanel(_ speedy: CPDFViewSidebarSpeedMode, event theEvent: NSEvent!) {
  2263. if(speedy == .right) {
  2264. self.toggleRightPane()
  2265. } else if (speedy == .left) {
  2266. self.menuItemAction_hiddenLeftSide(speedy)
  2267. }
  2268. }
  2269. func pdfListViewEventMarkupColor(with annotation: CPDFAnnotation!) -> [NSColor]! {
  2270. if (annotation.isKind(of: CPDFMarkupAnnotation.self)) {
  2271. if (annotation as! CPDFMarkupAnnotation).markupType() == .highlight {
  2272. return KMAnnotationPropertiesColorManager.manager.markHighlightColors
  2273. } else {
  2274. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  2275. }
  2276. } else {
  2277. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  2278. }
  2279. }
  2280. func pdfListViewHaveDocumentAttribute() -> Bool {
  2281. if(!self.listView.document.allowsCopying) {
  2282. self.removeOwnerPassword()
  2283. return false
  2284. }
  2285. return true
  2286. }
  2287. func pdfListView(_ sender: CPDFListView!, showSnapshotAtPageNumber pageNum: Int, for rect: NSRect, scaleFactor: CGFloat, autoFits: Bool) {
  2288. let swc = KMSnapshotWindowController(windowNibName: "SnapshotWindow")
  2289. swc.delegate = self
  2290. swc.setPdfDocument(self.listView.document, goToPageNumber: pageNum, rect: rect, scaleFactor: scaleFactor, autoFits: autoFits)
  2291. swc.forceOnTop = self.interactionMode != .normal
  2292. self.myDocument?.addWindowController(swc)
  2293. }
  2294. func pdfListView(_ pdfView: CPDFListView!, documentDataDidChanged docData: Any!, withInfo info: [AnyHashable : Any]!) {
  2295. if let data = info?[CPDFListView.outlineKey] as? Bool, data { // 大纲改变
  2296. guard let ol = docData as? CPDFOutline else {
  2297. return
  2298. }
  2299. let add = info?[CPDFListView.outlineAddKey] as? Bool ?? false
  2300. let remove = info?[CPDFListView.outlineRemoveKey] as? Bool ?? false
  2301. if add {
  2302. self.leftSideViewController.addOutlineAfter(ol)
  2303. }
  2304. if remove {
  2305. self.leftSideViewController.removeOutlineAfter(ol)
  2306. }
  2307. let demote = info?[CPDFListView.outlineDemoteKey] as? Bool ?? false
  2308. let promote = info?[CPDFListView.outlinePromoteKey] as? Bool ?? false
  2309. if demote {
  2310. self.leftSideViewController.demoteOutlineAfter(ol)
  2311. }
  2312. if promote {
  2313. self.leftSideViewController.promoteOutlineAfter(ol)
  2314. }
  2315. }
  2316. }
  2317. //TextEdit
  2318. func pdfListViewDidTextFontChanged(_ pdfListView: CPDFListView!) {
  2319. }
  2320. func pdfListViewDidTextColorChanged(_ pdfListView: CPDFListView!, with color: NSColor!) {
  2321. }
  2322. func pdfListViewAnnotationMeasureInfoChange(_ pdfListView: CPDFListView!, with annotation: CPDFAnnotation!) {
  2323. guard let data = annotation else {
  2324. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  2325. distanceMeasureInfoWindowController?.clearData()
  2326. }
  2327. return
  2328. }
  2329. if let lineAnnotation = annotation as? CPDFLineAnnotation {
  2330. handleLineAnnotation(lineAnnotation)
  2331. } else if let polylineAnnotation = annotation as? CPDFPolylineAnnotation {
  2332. handlePolylineAnnotation(polylineAnnotation)
  2333. } else if let polygonAnnotation = annotation as? CPDFPolygonAnnotation {
  2334. handlePolygonAnnotation(polygonAnnotation)
  2335. }
  2336. }
  2337. func pdfListViewMeasureCancel(_ pdfListView: CPDFListView!) {
  2338. cancelMeasureType()
  2339. }
  2340. func tableMenu(_ menu: NSMenu, withTable table: KMTableAnnotation, point: CGPoint) -> NSMenu {
  2341. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  2342. 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"]
  2343. let actions = ["formAnnotTextEdit:", "", "addRowAbove:", "addRowBelow:", "", "addColumnBefore:", "addColumnAfter:", "", "deleteRow:", "deleteColumn:", "deleteTabel", "cutCell:", "copyCell:", "pasteCell:", "pasteAndMatchStyle:", "deleteCellContents:", "clearAll:"]
  2344. for i in 0..<itemTitles.count {
  2345. var item: NSMenuItem? = nil
  2346. if itemTitles[i] == "" {
  2347. item = NSMenuItem.separator()
  2348. menu.insertItem(item!, at: i)
  2349. } else {
  2350. item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  2351. item!.target = self
  2352. item!.action = NSSelectorFromString(actions[i])
  2353. if itemTitles[i] == "Paste" /*&& !_copyCellData*/ {
  2354. item!.action = nil
  2355. } else if itemTitles[i] == "Paste and Match Style" /*&& !_copyCellData */{
  2356. item!.action = nil
  2357. } else if itemTitles[i] == "Add Row Above" {
  2358. let path1 = table.crossLines[table.rowNumber - table.currentCell.row]
  2359. let path2 = table.crossLines[table.rowNumber - table.currentCell.row - 1]
  2360. if (path1 as AnyObject).lineJoinStyle == NSBezierPath.LineJoinStyle.round && table.headerCount() >= 5 {
  2361. item!.action = nil
  2362. } else if (path2 as AnyObject).lineJoinStyle == NSBezierPath.LineJoinStyle.bevel && table.footerCount() >= 5 {
  2363. item!.action = nil
  2364. }
  2365. }
  2366. item!.title = NSLocalizedString(item!.title, comment: "")
  2367. item!.representedObject = NSValue(point: point)
  2368. menu.insertItem(item!, at: i)
  2369. }
  2370. }
  2371. } else {
  2372. let itemTitles = ["Cut", "Copy", "Paste", "Delete"]
  2373. let actions = ["cut:", "copy:", "paste:", "delete:"]
  2374. for i in 0..<itemTitles.count {
  2375. let item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  2376. item.target = self
  2377. item.action = NSSelectorFromString(actions[i])
  2378. item.title = NSLocalizedString(item.title, comment: "")
  2379. menu.insertItem(item, at: i)
  2380. item.representedObject = NSValue(point: point)
  2381. }
  2382. }
  2383. return menu
  2384. }
  2385. private func handleLineAnnotation(_ annotation: CPDFLineAnnotation) {
  2386. if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  2387. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  2388. distanceMeasureInfoWindowController?.showWindow(self)
  2389. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  2390. areaMeasureInfoWindowController?.hideFloatingWindow()
  2391. distanceMeasureInfoWindowController?.showWindow(self)
  2392. } else if distanceMeasureInfoWindowController?.window?.isVisible == false {
  2393. distanceMeasureInfoWindowController?.showWindow(self)
  2394. }
  2395. let measureInfo = annotation.measureInfo
  2396. let startPoint = annotation.startPoint
  2397. let endPoint = annotation.endPoint
  2398. let angle = atan2(endPoint.y - startPoint.y, endPoint.x - startPoint.x) * (180.0 / .pi)
  2399. distanceMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  2400. distanceMeasureInfoWindowController?.xLabel.stringValue = String(format: "%.0f", abs(endPoint.x - startPoint.x))
  2401. distanceMeasureInfoWindowController?.yLabel.stringValue = String(format: "%.0f", abs(endPoint.y - startPoint.y))
  2402. distanceMeasureInfoWindowController?.reloadData(with: measureInfo!)
  2403. }
  2404. private func handlePolylineAnnotation(_ annotation: CPDFPolylineAnnotation) {
  2405. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  2406. distanceMeasureInfoWindowController?.hideFloatingWindow()
  2407. perimeterMeasureInfoWindowController?.showWindow(self)
  2408. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  2409. areaMeasureInfoWindowController?.hideFloatingWindow()
  2410. perimeterMeasureInfoWindowController?.showWindow(self)
  2411. } else if perimeterMeasureInfoWindowController?.window?.isVisible == false {
  2412. perimeterMeasureInfoWindowController?.showWindow(self)
  2413. }
  2414. let measureInfo = annotation.measureInfo
  2415. let savePoints = annotation.savePoints()
  2416. var angle: CGFloat = 0
  2417. if savePoints.count >= 3 {
  2418. let count = savePoints.count
  2419. let startPoint = savePoints[count - 3].pointValue
  2420. let midPoint = savePoints[count - 2].pointValue
  2421. let endPoint = savePoints.last!.pointValue
  2422. angle = angleBetweenPoints(startPoint, midPoint, endPoint)
  2423. }
  2424. angle = 180 - angle
  2425. perimeterMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  2426. perimeterMeasureInfoWindowController?.reloadData(with: measureInfo!)
  2427. }
  2428. private func handlePolygonAnnotation(_ annotation: CPDFPolygonAnnotation) {
  2429. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  2430. distanceMeasureInfoWindowController?.hideFloatingWindow()
  2431. areaMeasureInfoWindowController?.showWindow(self)
  2432. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  2433. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  2434. areaMeasureInfoWindowController?.showWindow(self)
  2435. } else if areaMeasureInfoWindowController?.window?.isVisible == false {
  2436. areaMeasureInfoWindowController?.showWindow(self)
  2437. }
  2438. let measureInfo = annotation.measureInfo
  2439. let savePoints = annotation.savePoints
  2440. var angle: CGFloat = 0
  2441. if savePoints.count >= 3 {
  2442. let count = savePoints.count
  2443. let startPoint = (savePoints[count - 3] as AnyObject).pointValue
  2444. let midPoint = (savePoints[count - 2] as AnyObject).pointValue
  2445. let endPoint = (savePoints.lastObject as AnyObject).pointValue
  2446. angle = angleBetweenPoints(startPoint!, midPoint!, endPoint!)
  2447. }
  2448. angle = 180 - angle
  2449. areaMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  2450. areaMeasureInfoWindowController?.reloadData(measureInfo!)
  2451. }
  2452. private func angleBetweenPoints(_ startPoint: CGPoint, _ midPoint: CGPoint, _ endPoint: CGPoint) -> CGFloat {
  2453. let vector1 = CGPoint(x: midPoint.x - startPoint.x, y: midPoint.y - startPoint.y)
  2454. let vector2 = CGPoint(x: endPoint.x - midPoint.x, y: endPoint.y - midPoint.y)
  2455. let dotProduct = vector1.x * vector2.x + vector1.y * vector2.y
  2456. let magnitude1 = sqrt(vector1.x * vector1.x + vector1.y * vector1.y)
  2457. let magnitude2 = sqrt(vector2.x * vector2.x + vector2.y * vector2.y)
  2458. return acos(dotProduct / (magnitude1 * magnitude2)) * (180.0 / .pi)
  2459. }
  2460. @objc func pdfUpdatedFinish() {
  2461. splitPDFController?.inPDFFirst = false
  2462. splitPDFController?.outPDFFirst = false
  2463. }
  2464. }
  2465. //MARK: - KMNThumbnailBaseViewDelegate
  2466. extension KMMainViewController: KMNThumbnailBaseViewDelegate {
  2467. func clickThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,currentIndex:Int) {
  2468. exitPageEditMode()
  2469. viewManager.isPageEditMode = false
  2470. pdfToolbarController?.reloadPageEditView()
  2471. }
  2472. func insertPDFThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, pdfDocment: CPDFDocument?) {
  2473. if(pdfDocment != nil) {
  2474. insertDocuments.insert(pdfDocment!)
  2475. }
  2476. }
  2477. func changeIndexPathsThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, selectionIndexPaths: Set<IndexPath>, selectionStrings: String) {
  2478. toolbarManager.page_pageInfo_Property.text = selectionStrings
  2479. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  2480. toolbarManager.page_pageInfo_Property.creatable = true
  2481. }
  2482. pdfToolbarController?.refreshSecondToolbarItemsState()
  2483. }
  2484. }
  2485. extension KMMainViewController: KMNLeftSideViewControllerDelegate {
  2486. func enterPageEditLeftSideViewController(leftSideViewController: KMNLeftSideViewController) {
  2487. if viewManager.isPageEditMode == false {
  2488. viewManager.isPageEditMode = true
  2489. if(pdfToolbarController != nil) {
  2490. kmPDFToolbarControllerDidToolbarItemClicked(pdfToolbarController!, KMPDFToolbar_PageEdit_Identifier)
  2491. pdfToolbarController?.reloadSecondToolbar()
  2492. }
  2493. }
  2494. }
  2495. func changeSelectePageLeftSideViewController(leftSideViewController: KMNLeftSideViewController, pageIndex: Int) {
  2496. if(listView.currentPageIndex != pageIndex) {
  2497. listView.go(toPageIndex: pageIndex, animated: true)
  2498. }
  2499. }
  2500. func addBookmarkForLeftC(controller: KMNLeftSideViewController, bookmark: CPDFBookmark?, info: [String : Any]?) {
  2501. if let result = info?["result"] as? Bool {
  2502. if result == false {
  2503. let message = KMNCustomAlertView.alertView(message: KMLocalizedString("This page has been bookmarked"), type: .normal_custom, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
  2504. }
  2505. }
  2506. }
  2507. }
  2508. //MARK: -
  2509. //MARK: -
  2510. //MARK: -
  2511. //MARK: - 旧代码,需要用到的内容需要拖出来,写好注释
  2512. extension KMMainViewController {
  2513. func awakeFromNibFunction() {
  2514. self.addBackgroundMaskView()
  2515. listView.delegate = self
  2516. listView.pdfListViewDelegate = self
  2517. if (document != nil) {
  2518. self.listView.document = self.document
  2519. self.listView.document?.delegate = self
  2520. let autoScale = listView.autoScales
  2521. if !autoScale {
  2522. listView.scaleFactor = 1.0
  2523. }
  2524. }
  2525. self.initPDFLeftViewVC()
  2526. self.leftSideViewController.mainViewController = self
  2527. }
  2528. func viewDidAppearFunction() {
  2529. //春季活动
  2530. if ((KMAdvertisementManager.manager.info.popWindowContent) != nil) {
  2531. if KMAdvertisementManager.manager.info.popWindowContent!.content!.count > 0 {
  2532. let info = KMAdvertisementManager.manager.info.popWindowContent!.content?.first
  2533. if KMAdvertisementManager.checkAdvertisementValid(info!) {
  2534. self.loadRecommondPopWindow()
  2535. }
  2536. }
  2537. }
  2538. self.addEventMonitor()
  2539. self.view.window?.makeFirstResponder(self.listView)
  2540. // 更新属性页面的信息
  2541. NotificationCenter.default.post(name: KMInfoWindowC.windowDidBecomeMainNotification, object: self.myDocument)
  2542. self.interfaceThemeDidChanged(self.view.window?.appearance?.name ?? (NSApp.appearance?.name ?? .aqua))
  2543. if (self.document == nil) {
  2544. return
  2545. }
  2546. if (self.document == nil || self.document!.isLocked == false) {
  2547. self.loadFunctionGuide()
  2548. }
  2549. if (self.document?.isLocked == false) {
  2550. if self.model.needConvertNotes && self.tabViewIsDragging() == false {
  2551. self.showConvertNotesProgress()
  2552. }
  2553. return
  2554. }
  2555. if (self.view.window == nil) {
  2556. return
  2557. }
  2558. if (self.model.password != nil) {
  2559. if let data = self.listView.document?.unlock(withPassword: self.model.password), data {
  2560. self.model.isSaveKeyChain = false
  2561. if self.model.needConvertNotes && self.tabViewIsDragging() == false {
  2562. self.showConvertNotesProgress()
  2563. }
  2564. return
  2565. }
  2566. }
  2567. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  2568. if self.view.window != nil && self.tabViewIsDragging() == false {
  2569. self.passwordWindow = KMPasswordInputWindow.openWindow(window: self.view.window!, url: self.document!.documentURL) { [weak self] result , password in
  2570. if (result == .cancel) {
  2571. (self?.myDocument as? KMMainDocument)?.browser?.closeTab()
  2572. return
  2573. }
  2574. self?.model.isSaveKeyChain = true
  2575. self?.listView.document = self?.document
  2576. self?.document?.unlock(withPassword: password)
  2577. }
  2578. } else {
  2579. if self.model.needConvertNotes && self.tabViewIsDragging() == false {
  2580. self.showConvertNotesProgress()
  2581. }
  2582. }
  2583. }
  2584. }
  2585. func viewWillDisappearFunction() {
  2586. if self.interactionMode != .presentation {
  2587. self.removeEventMonitor()
  2588. }
  2589. }
  2590. func viewWillLayoutFunction() {
  2591. if (KMTools.isFullScreen(self.view.window ?? NSWindow())) { // 全屏
  2592. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundFullScreenColor
  2593. } else {
  2594. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor
  2595. }
  2596. if let guideWC = self.guideInfoWindowController{
  2597. var rect = self.view.window!.frame
  2598. rect.size.height -= 20
  2599. guideWC.window?.setFrame(rect, display: false)
  2600. guideWC.window?.minSize = rect.size
  2601. guideWC.window?.maxSize = rect.size
  2602. guideWC.show()
  2603. }
  2604. }
  2605. func viewDidLoadOld() {
  2606. mwcFlags.settingUpWindow = 1
  2607. self.srHanddler.pdfView = self.listView
  2608. self.initToolbar()
  2609. if (UserDefaults.standard.object(forKey: CPDFOfficeLeftSidePaneWidthKey) != nil) {
  2610. UserDefaults.standard.set(256, forKey: CPDFOfficeLeftSidePaneWidthKey)
  2611. UserDefaults.standard.synchronize()
  2612. }
  2613. if (UserDefaults.standard.object(forKey: CPDFOfficeRightSidePaneWidthKey) != nil) {
  2614. UserDefaults.standard.set(256, forKey: CPDFOfficeRightSidePaneWidthKey)
  2615. UserDefaults.standard.synchronize()
  2616. }
  2617. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  2618. if (self.listView.document != nil) {
  2619. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document?.documentURL.path ?? "")
  2620. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document?.documentURL.path ?? "")
  2621. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < (self.listView.document?.pageCount ?? 0)) {
  2622. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  2623. if (pageScale != nil) {
  2624. self.listView.scaleFactor = CGFloat(pageScale!)
  2625. }
  2626. self.listView.go(toPageIndex: pageNumber!, animated: false)
  2627. }
  2628. } else {
  2629. self._goToFirstPageForFristAppear()
  2630. }
  2631. }
  2632. } else {
  2633. self._goToFirstPageForFristAppear()
  2634. }
  2635. NotificationCenter.default.addObserver(self, selector: #selector(rename(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerRename"), object: nil)
  2636. NotificationCenter.default.addObserver(self, selector: #selector(closeTab(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseTabs"), object: nil)
  2637. NotificationCenter.default.addObserver(self, selector: #selector(showInFinder(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerShowInFinder"), object: nil)
  2638. NotificationCenter.default.addObserver(self, selector: #selector(preferenceDidChangeNotification), name: KMPreferenceManager.didChangeNotification, object: nil)
  2639. NotificationCenter.default.addObserver(self, selector: #selector(documentDidUnlockNotification), name: Notification.Name("CPDFDocumentDidUnlockNotification"), object: nil)
  2640. NotificationCenter.default.addObserver(self, selector: #selector(annotationsAttributeHasChange), name: NSNotification.Name.CPDFListViewAnnotationsAttributeHasChange, object:nil)
  2641. NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminateNotification), name: NSApplication.willTerminateNotification, object: nil)
  2642. NotificationCenter.default.addObserver(self, selector: #selector(CPDFDocumentPageCountChangedNotification), name: NSNotification.Name.init(rawValue: "CPDFDocumentPageCountChangedNotification"), object: nil)
  2643. NotificationCenter.default.addObserver(self, selector: #selector(CEditPDFToolModeChangeStateUnkownNotification), name: Notification.Name.init("CEditPDFToolModeChangeStateUnkown"), object: nil)
  2644. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.listView)
  2645. NotificationCenter.default.addObserver(self, selector: #selector(handleDisplayBoxChangedNotification), name: NSNotification.Name.CPDFViewDisplayBoxChanged, object: self.listView)
  2646. NotificationCenter.default.addObserver(self, selector: #selector(didAddContentViewNotification), name: NSWindow.didAddContentViewNotification, object: nil)
  2647. NotificationCenter.default.addObserver(self, selector: #selector(addAutoSaveEvent), name: AutoSaveManager.kTimeValueChangedNotificationName, object: nil)
  2648. NotificationCenter.default.addObserver(self, selector: #selector(didRemoveAnnotationNotification), name: NSNotification.Name.CPDFPageDidRemoveAnnotation, object: nil)
  2649. Task {
  2650. self.addAutoSaveEvent()
  2651. }
  2652. self.closeRightPane()
  2653. self.addKeyEventMonitor()
  2654. self.addAdsBannerView()
  2655. var snapshotSetups: NSArray?
  2656. if KMPreferenceManager.shared.rememberSnapshot {
  2657. if let fileUrl = (self.myDocument as? KMMainDocument)?.fileURL {
  2658. snapshotSetups = SKBookmarkController.shared().snapshotsForRecentDocument(at: fileUrl) as NSArray?
  2659. }
  2660. }
  2661. if let cnt = snapshotSetups?.count, cnt > 0 {
  2662. if let data = self.listView.document?.isLocked, data {
  2663. self.savedNormalSetup.setObject(snapshotSetups as Any, forKey: "snapshots" as NSCopying)
  2664. } else {
  2665. self.showSnapshots(setups: snapshotSetups)
  2666. }
  2667. }
  2668. let readModel = UserDefaults.standard.bool(forKey: "kKMPDFViewIsReadMode")
  2669. if readModel == true {
  2670. self.openPDFReadMode()
  2671. }
  2672. let hasWindowSetup = savedNormalSetup.count > 0
  2673. if UserDefaults.standard.dictionary(forKey: KMDefaultPDFDisplaySettingsKey) != nil {
  2674. let pdfSettings: NSDictionary = hasWindowSetup ? savedNormalSetup : UserDefaults.standard.dictionary(forKey: KMDefaultPDFDisplaySettingsKey)! as NSDictionary
  2675. self.applyPDFSettings(pdfSettings)
  2676. } else {
  2677. self.applyPDFSettings(savedNormalSetup)
  2678. }
  2679. //文字
  2680. let fontManager = NSFontManager.shared
  2681. fontManager.target = self
  2682. fontManager.action = #selector(changeFont(_:))
  2683. }
  2684. //MARK: - PDFListView
  2685. func initPDFLeftViewVC() {
  2686. var frame = self.leftView.frame
  2687. frame.size.width += 44
  2688. self.leftView.frame = frame
  2689. leftSideViewController.isFirst = true
  2690. leftSideViewController.listView = self.listView
  2691. leftSideViewController.view.frame = CGRect(x: 0, y:0 , width: self.leftView.frame.size.width, height: self.leftView.frame.size.height)
  2692. leftSideViewController.view.autoresizingMask = [.height,.width]
  2693. leftSideViewController.delegate = self
  2694. self.leftView.addSubview(leftSideViewController.view)
  2695. }
  2696. func addAdsBannerView() {
  2697. #if VERSION_FREE
  2698. if !IAPProductsManager.default().isAvailableAllFunction(){
  2699. guard let document = self.listView.document else {
  2700. return
  2701. }
  2702. if !document.isLocked {
  2703. }
  2704. NotificationCenter.default.addObserver(self, selector: #selector(purchaseStateUpdateNoti), name: NSNotification.Name(rawValue: "KMIAPProductPurchasedNotification"), object: nil)
  2705. NotificationCenter.default.addObserver(self, selector: #selector(purchaseStateUpdateNoti), name: NSNotification.Name(rawValue: "kDeviceActivateNotification"), object: nil)
  2706. }
  2707. #endif
  2708. }
  2709. // MARK: Private Methods
  2710. private func _isArabicLanguage() -> Bool {
  2711. return NSLocalizedString("Right click a color and select “Change Color...“.", comment: "") == "انقر بزر الماوس الأيمن فوق اللون وحدد \"تغيير اللون...\"."
  2712. }
  2713. internal func removeNotifications() {
  2714. NotificationCenter.default.removeObserver(self)
  2715. self.leftSideViewController.clearAnnotationFilterData()
  2716. self.leftSideViewController.clearNotification()
  2717. }
  2718. func checkShouldAutoOpenLeftVC() {
  2719. if KMPreference.shared.showLeftSideBar == false {
  2720. return
  2721. }
  2722. if self.model.leftPanelOpen {
  2723. return
  2724. }
  2725. let readModel = UserDefaults.standard.bool(forKey: "kKMPDFViewIsReadMode")
  2726. if readModel == true {
  2727. return
  2728. }
  2729. DispatchQueue.main.async {
  2730. self.leftSideViewController.showThumbnail()
  2731. }
  2732. }
  2733. func applyLeftSideWidth(_ leftSideWidth: CGFloat, rightSideWidth: CGFloat) -> Void {
  2734. }
  2735. func removeAllAnnotations() {
  2736. let alert = NSAlert()
  2737. alert.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  2738. alert.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  2739. alert.addButton(withTitle: NSLocalizedString("No", comment:""))
  2740. if (alert.runModal() != .alertFirstButtonReturn) {
  2741. return
  2742. }
  2743. DispatchQueue.main.async {
  2744. self.removeAllAnnotationsStore.store(t: self.listView)
  2745. }
  2746. }
  2747. @objc func cancelMeasureType() {
  2748. self.hideMeasureFloatingWindows()
  2749. }
  2750. func hideMeasureFloatingWindows() {
  2751. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  2752. distanceMeasureInfoWindowController?.hideFloatingWindow()
  2753. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  2754. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  2755. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  2756. areaMeasureInfoWindowController?.hideFloatingWindow()
  2757. }
  2758. }
  2759. func showMeasureFloatingWindowsIfNeed() {
  2760. let toolMode = self.listView.toolMode
  2761. if toolMode != .measureToolMode {
  2762. return
  2763. }
  2764. let type = self.listView.annotationType
  2765. if type == .line {
  2766. self.distanceMeasureInfoWindowController?.window?.orderFront(nil)
  2767. } else if type == .polyLine {
  2768. self.perimeterMeasureInfoWindowController?.window?.orderFront(nil)
  2769. } else if type == .polyGon {
  2770. self.areaMeasureInfoWindowController?.window?.orderFront(nil)
  2771. } else if type == .square {
  2772. self.areaMeasureInfoWindowController?.window?.orderFront(nil)
  2773. }
  2774. }
  2775. // MARK: - 标记密文
  2776. func enterRedact() {
  2777. if !IAPProductsManager.default().isAvailableAllFunction(){
  2778. let winC = KMPurchaseCompareWindowController.sharedInstance()
  2779. winC?.kEventName = "Reading_Redact_BuyNow"
  2780. winC?.showWindow(nil)
  2781. return
  2782. }
  2783. if self.listView.document?.allowsPrinting == false || self.listView.document?.allowsCopying == false {
  2784. Task {
  2785. _ = await KMAlertTool.runModel(message: KMLocalizedString("This is a secured document. Editing is not permitted."))
  2786. }
  2787. return
  2788. }
  2789. if self.hasEnterRedact() {
  2790. self.exitRedact()
  2791. return
  2792. }
  2793. self.leftSideViewController.thumbnailTableView.isEnabled = false
  2794. self.leftSideViewController.tocOutlineView.isEnabled = false
  2795. self.leftSideViewController.noteOutlineView.isEnabled = false
  2796. self.leftSideViewController.findTableView.isEnabled = false
  2797. self.leftSideViewController.groupedFindTableView.isEnabled = false
  2798. self.leftSideViewController.snapshotTableView.isEnabled = false
  2799. let ttsWindowC = KMTTSWindowController.share
  2800. if ttsWindowC.pdfView?.document?.documentURL.path == self.listView.document?.documentURL.path {
  2801. if let data = ttsWindowC.window?.isVisible, data {
  2802. ttsWindowC.stopSpeaking()
  2803. ttsWindowC.close()
  2804. }
  2805. }
  2806. NSColorPanel.shared.showsAlpha = false
  2807. redactController = KMPDFRedactViewController(url: self.listView.document!.documentURL, password: self.listView.document?.password)
  2808. self.addChild(redactController)
  2809. redactController.view.autoresizingMask = [.width, .height]
  2810. self.listView.isHidden = true
  2811. redactController.scaleFactor = self.listView.scaleFactor
  2812. redactController.titleBack = { [weak self] title in
  2813. self?.view.window?.title = title
  2814. }
  2815. redactController.callback = { [weak self] result, currentPageIndex, saveResult, saveUrl in
  2816. self?.listView.go(toPageIndex: self!.redactController.redactPdfView.currentPageIndex, animated: false)
  2817. if result == false { // 退出
  2818. self?.exitRedact()
  2819. return
  2820. }
  2821. let controller = self?._getPDFRedactController()
  2822. controller?.redactPdfView.newAddAnnotation.removeAll()
  2823. self?.exitRedact()
  2824. if saveResult {
  2825. let newDocument = CPDFDocument(url: saveUrl)
  2826. if let data = newDocument?.isLocked, data {
  2827. newDocument?.unlock(withPassword: self?.listView.document?.password ?? "")
  2828. }
  2829. self?.document = newDocument
  2830. self?.listView.document = newDocument
  2831. self?.listView.layoutDocumentView()
  2832. }
  2833. }
  2834. redactController.setCurrentPageIndex(self.listView.currentPageIndex)
  2835. }
  2836. func exitRedact() {
  2837. self.leftSideViewController.thumbnailTableView.isEnabled = true
  2838. self.leftSideViewController.tocOutlineView.isEnabled = true
  2839. self.leftSideViewController.noteOutlineView.isEnabled = true
  2840. self.leftSideViewController.findTableView.isEnabled = true
  2841. self.leftSideViewController.groupedFindTableView.isEnabled = true
  2842. self.leftSideViewController.snapshotTableView.isEnabled = true
  2843. let controller = self._getPDFRedactController()
  2844. if let data = controller {
  2845. if data.redactPdfView.newAddAnnotation.count > 0 {
  2846. KMAlertTool.runModel(message: "", informative: KMLocalizedString("There are unapplied redactions in this file. Exit will not save redaction."), buttons: [KMLocalizedString("Exit"), KMLocalizedString("Cancel")]) { response in
  2847. if response == .alertFirstButtonReturn {
  2848. controller?.redactPdfView.newAddAnnotation.removeAll()
  2849. self.exitRedact()
  2850. }
  2851. }
  2852. return
  2853. }
  2854. }
  2855. NSColorPanel.shared.showsAlpha = true
  2856. controller?.redactPdfView.resignMonitor()
  2857. controller?.view.removeFromSuperview()
  2858. controller?.removeFromParent()
  2859. self.listView.isHidden = false
  2860. self.listView.annotationType = .unkown
  2861. }
  2862. func hasEnterRedact() -> Bool {
  2863. return self._getPDFRedactController() != nil
  2864. }
  2865. //MARK: - AI
  2866. func showAITypeChooseView(aiConfigType: AIConfigType) -> Void {
  2867. if (self.document != nil) {
  2868. AIChatInfoManager.defaultManager.currentFilePath = (self.document?.documentURL.path)!
  2869. } else {
  2870. AIChatInfoManager.defaultManager.currentFilePath = ""
  2871. }
  2872. let windowVC: AINewConfigWindowController = AINewConfigWindowController.currentWC()
  2873. windowVC.chooseCurFileHandle = {[unowned self] windowVC in
  2874. if AIChatInfoManager.defaultManager.currentFilePath.isEmpty == false {
  2875. let documentArray = NSDocumentController.shared.documents
  2876. var didFileEdit: Bool = false
  2877. var curDoc: KMMainDocument!
  2878. for document in documentArray {
  2879. if document.fileURL?.path == AIChatInfoManager.defaultManager.currentFilePath {
  2880. didFileEdit = document.isDocumentEdited
  2881. curDoc = document as! KMMainDocument
  2882. break
  2883. }
  2884. }
  2885. if didFileEdit {
  2886. let tempFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(AIChatInfoManager.defaultManager.currentFilePath.lastPathComponent)
  2887. if FileManager.default.fileExists(atPath: tempFileURL.path) {
  2888. do {
  2889. try FileManager.default.removeItem(at: tempFileURL)
  2890. } catch {
  2891. }
  2892. }
  2893. if curDoc != nil {
  2894. curDoc.mainViewController?.SaveTempPDFDocumentToURLPath(tempPath: tempFileURL.path)
  2895. }
  2896. }
  2897. windowVC.window?.becomeMain()
  2898. }
  2899. }
  2900. if windowVC.window?.isVisible == true && windowVC.didSetOriginFrame == true {
  2901. } else {
  2902. var windowRect = windowVC.window?.frame
  2903. windowRect!.origin.x = NSMaxX(self.view.window!.frame) - (windowRect?.size.width)!
  2904. windowRect!.origin.y = NSMaxY(self.view.window!.frame) - (windowRect?.size.height)! - 64
  2905. windowVC.window?.setFrame(windowRect!, display: true)
  2906. windowVC.didSetOriginFrame = true
  2907. }
  2908. windowVC.eventLabel = "AITools_Tbr"
  2909. windowVC.showWindow(nil)
  2910. if (aiConfigType != .none) {
  2911. windowVC.eventLabel = "AITools_Start"
  2912. if self.listView.currentSelection?.string()?.isEmpty == false {
  2913. windowVC.setCurrentPDFSelection(self.listView.currentSelection.string())
  2914. }
  2915. windowVC.chooseAIFunctionWithType(aiConfigType)
  2916. }
  2917. }
  2918. @objc func aiTipIconViewShowStateChangeNoti() {
  2919. }
  2920. //MARK: - 引导
  2921. func loadFunctionGuide() -> Void {
  2922. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  2923. if self.view.window != nil {
  2924. self.loadOpenFileFunctionGuide(.openFileNormal)
  2925. }
  2926. }
  2927. }
  2928. func loadOpenFileFunctionGuide(_ showType: KMGuideInfoType) -> Void {
  2929. if showType == .openFileNormal && KMGuideInfoWindowController.availableShow(.openFileNormal) {
  2930. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  2931. guard let guideWC = self.guideInfoWindowController else { return }
  2932. guideWC.type = .openFileNormal
  2933. // guideWC.openPanelRect = (self.view.window?.contentView?.convert(leftPanelItem.frame, from: leftPanelItem.superview)) ?? .zero
  2934. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  2935. guideWC.normalGuideFinishHandle = { [weak self] windowVC in
  2936. }
  2937. guideWC.finishHandle = { [weak self] windowVC, type in
  2938. if type == .windowNewFinish ||
  2939. type == . windowDigitalFinish {
  2940. self?.checkFirstTrialController()
  2941. }
  2942. }
  2943. guideWC.openFileToggleHandle = { [weak self] windowVC, type in
  2944. self?.checkFirstTrialController()
  2945. }
  2946. var rect = self.view.window!.frame
  2947. rect.size.height -= 20
  2948. guideWC.window?.setFrame(rect, display: false)
  2949. guideWC.window?.minSize = rect.size
  2950. guideWC.window?.maxSize = rect.size
  2951. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  2952. guideWC.show()
  2953. } else if showType == .digitalSignGuide && KMGuideInfoWindowController.availableShow(.digitalSignGuide) {
  2954. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  2955. guard let guideWC = self.guideInfoWindowController else { return }
  2956. guideWC.type = .digitalSignGuide
  2957. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  2958. guideWC.finishHandle = { [weak self] windowVC, type in
  2959. self?.checkFirstTrialController()
  2960. }
  2961. var rect = self.view.window!.frame
  2962. rect.size.height -= 20
  2963. guideWC.window?.setFrame(rect, display: false)
  2964. guideWC.window?.minSize = rect.size
  2965. guideWC.window?.maxSize = rect.size
  2966. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  2967. guideWC.show()
  2968. } else if showType == .pdfCompareGuide && KMGuideInfoWindowController.availableShow(.pdfCompareGuide) {
  2969. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  2970. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  2971. guard let guideWC = self.guideInfoWindowController else { return }
  2972. guideWC.type = .pdfCompareGuide
  2973. guard let win = self.view.window else {
  2974. return
  2975. }
  2976. guideWC.finishHandle = { [weak self] winC, type in
  2977. if type == .windowNewFinish {
  2978. DispatchQueue.main.async {
  2979. self?.loadOpenFileFunctionGuide(.measureGuide)
  2980. }
  2981. return
  2982. }
  2983. }
  2984. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  2985. var rect = self.view.window!.frame
  2986. rect.size.height -= 20
  2987. guideWC.window?.setFrame(rect, display: false)
  2988. guideWC.window?.minSize = rect.size
  2989. guideWC.window?.maxSize = rect.size
  2990. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  2991. guideWC.show()
  2992. }
  2993. } else if showType == .measureGuide && KMGuideInfoWindowController.availableShow(.measureGuide) {
  2994. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  2995. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  2996. guard let guideWC = self.guideInfoWindowController else { return }
  2997. guard let _ = self.view.window else {
  2998. return
  2999. }
  3000. guideWC.type = .measureGuide
  3001. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  3002. var rect = self.view.window?.frame ?? .zero
  3003. rect.size.height -= 20
  3004. guideWC.window?.setFrame(rect, display: false)
  3005. guideWC.window?.minSize = rect.size
  3006. guideWC.window?.maxSize = rect.size
  3007. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  3008. guideWC.show()
  3009. }
  3010. } else if showType == .convertGuide && KMGuideInfoWindowController.availableShow(.convertGuide) {
  3011. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  3012. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  3013. guard let guideWC = self.guideInfoWindowController else { return }
  3014. guideWC.type = .convertGuide
  3015. guard let win = self.view.window else {
  3016. return
  3017. }
  3018. guideWC.purchaseHandle = { [weak self] windowVC in
  3019. #if VERSION_DMG
  3020. if IAPProductsManager.default().isAvailableAllFunction() {
  3021. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  3022. //Convert:
  3023. self?.showAllConvertWindow(convertT: .Word)
  3024. } else {
  3025. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  3026. limitWC.continueBlock = { windowController in
  3027. }
  3028. limitWC.window?.center()
  3029. limitWC.showWindow(nil)
  3030. }
  3031. } else {
  3032. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  3033. }
  3034. #else
  3035. if IAPProductsManager.default().isAvailableAllFunction() {
  3036. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  3037. //Convert:
  3038. } else {
  3039. var vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  3040. vc.showWindow(nil)
  3041. }
  3042. } else {
  3043. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  3044. }
  3045. #endif
  3046. }
  3047. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  3048. var rect = self.view.window?.frame ?? .zero
  3049. rect.size.height -= 20
  3050. guideWC.window?.setFrame(rect, display: false)
  3051. guideWC.window?.minSize = rect.size
  3052. guideWC.window?.maxSize = rect.size
  3053. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  3054. guideWC.show()
  3055. }
  3056. } else {
  3057. }
  3058. }
  3059. func checkFirstTrialController() -> Void {
  3060. #if VERSION_DMG
  3061. //打开文档后引导相关
  3062. if VerificationManager.default().status == .none {
  3063. let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  3064. let lastVersion = UserDefaults.standard.object(forKey: "SKLastTrialVersionMainDocumentLaunchedKey") as? String ?? ""
  3065. if lastVersion.isEmpty || lastVersion != appVersion {
  3066. UserDefaults.standard.setValue(appVersion, forKey: "SKLastTrialVersionMainDocumentLaunchedKey")
  3067. UserDefaults.standard.synchronize()
  3068. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  3069. }
  3070. }
  3071. #endif
  3072. }
  3073. // MARK: - 页面编辑
  3074. open func enterPageEdit(_ pages: [Int] = []) {
  3075. if let doc = self.listView.document {
  3076. if doc.allowsCopying == false || doc.allowsPrinting == false {
  3077. KMBaseWindowController.checkPassword(url: doc.documentURL, type: .owner) { result, pwd in
  3078. if result && pwd.isEmpty == false {
  3079. self.listView.document?.unlock(withPassword: pwd)
  3080. Task { @MainActor in
  3081. self.enterPageEdit(pages)
  3082. }
  3083. } else {
  3084. self.exitPageEdit()
  3085. }
  3086. }
  3087. return
  3088. }
  3089. }
  3090. //选中page
  3091. var tPages = pages
  3092. if tPages.count == 0 {
  3093. if self.leftSideViewController.type.methodType == .Thumbnail {
  3094. tPages = self.leftSideViewController.thumb_fetchSelectedRows() ?? [self.listView.currentPageIndex]
  3095. }
  3096. }
  3097. if (hasEnterPageEdit()) {
  3098. exitPageEdit()
  3099. return
  3100. }
  3101. let controller = KMPDFEditViewController(self.listView.document)
  3102. controller.selectedPages = tPages
  3103. controller.listView = self.listView
  3104. self.addChild(controller)
  3105. controller.view.autoresizingMask = [.width,.height]
  3106. self.listView.isHidden = true
  3107. controller.itemClick = { [weak self] index, params in
  3108. if (index == 1) { /// 双击退出
  3109. self?.enterEditMode(self!.leftSideViewController, [])
  3110. DispatchQueue.main.async {
  3111. let pageIndex: Int = params.first as! Int
  3112. self?.listView.go(toPageIndex: pageIndex, animated: true)
  3113. }
  3114. } else if (index == 2) { // 打印
  3115. self?.showPrintWindow(pageRange: KMPrintPageRange(type: .custom, selectPages: params.first as! [Int]))
  3116. }
  3117. }
  3118. controller.documentEditedCallback = { [weak self] params in
  3119. self?.recordIsPDFDocumentEdited()
  3120. }
  3121. controller.selectionDidChange = { selectedIndexs in
  3122. var indexSet = IndexSet()
  3123. for indexPath in selectedIndexs {
  3124. indexSet.insert(indexPath.item)
  3125. }
  3126. if indexSet.count != 0 {
  3127. }
  3128. }
  3129. }
  3130. open func exitPageEdit() {
  3131. let editController = getPDFEditController()
  3132. if (editController == nil) {
  3133. return
  3134. }
  3135. self.listView.annotationType = .highlight
  3136. // FIXME: - sdk修复插入特定文档crash后,这行代码可以去掉
  3137. self.leftSideViewController.model.insertedDocumentSet.formUnion(editController?.model.insertedDocumentSet ?? [])
  3138. editController?.view.removeFromSuperview()
  3139. editController?.removeFromParent()
  3140. self.listView.isHidden = false
  3141. self.listView.layoutDocumentView()
  3142. self.view.window?.makeFirstResponder(self.listView)
  3143. self.listView.annotationType = .unkown
  3144. self.listView.go(toPageIndex: editController!.listViewCurrentIndex, animated: false)
  3145. if let data = editController?.isEdited, data {
  3146. self.leftSideViewController.reloadThumbnailDataIfNeed()
  3147. self.leftSideViewController.note_reloadDataIfNeed()
  3148. self.leftSideViewController.refreshUIOfOutlineIfNeed()
  3149. self.leftSideViewController.refreshUIOfSeachListIfNeed()
  3150. self.leftSideViewController.refreshUIOfBookmarkIfNeed()
  3151. }
  3152. }
  3153. open func hasEnterPageEdit() -> Bool {
  3154. return self.getPDFEditController() != nil
  3155. }
  3156. // MARK: - 数字签名
  3157. func hasShowDigitalSign() -> Bool {
  3158. return self.digitalSignController?.view.superview != nil
  3159. }
  3160. func canEnterDigitalSign() -> Bool {
  3161. guard let doc = self.listView.document else {
  3162. return false
  3163. }
  3164. return doc.allowsPrinting && doc.allowsCopying
  3165. }
  3166. func enterDigitalSign() {
  3167. self.listView.toolMode = .textToolMode
  3168. if self.hasShowDigitalSign() {
  3169. self.exitDigitalSign()
  3170. } else {
  3171. if self.needSaveDocument() {
  3172. self.saveDocumentWithProgressAlert { [unowned self] params in
  3173. if (self.listView.document != nil) {
  3174. self.showDigitalSignWindow(withFilePathURL: self.listView.document.documentURL)
  3175. }
  3176. }
  3177. return
  3178. }
  3179. if (self.listView.document != nil) {
  3180. self.showDigitalSignWindow(withFilePathURL: self.listView.document.documentURL)
  3181. }
  3182. }
  3183. }
  3184. func exitDigitalSign() {
  3185. self.digitalSignController?.view.removeFromSuperview()
  3186. // KMDocumentDigitalSignToolbarItemIdentifier
  3187. }
  3188. func showDigitalSignWindow(withFilePathURL fileURL: URL) {
  3189. if !IAPProductsManager.default().isAvailableAllFunction(){
  3190. let winC = KMPurchaseCompareWindowController.sharedInstance()
  3191. winC?.kEventName = "Reading_DigitalSign_BuyNow"
  3192. winC?.showWindow(nil)
  3193. return
  3194. }
  3195. if hasShowDigitalSign() {
  3196. self.exitDigitalSign()
  3197. }
  3198. var currentPageIndex = listView.document?.index(for: listView.currentPage()) ?? 0
  3199. var password: String = ""
  3200. password = listView.document?.password ?? ""
  3201. digitalSignController = KMPDFDigitalSignViewController()
  3202. digitalSignController?.currentPageIndex = Int(currentPageIndex)
  3203. digitalSignController?.url = listView.document?.documentURL
  3204. digitalSignController?.password = password
  3205. digitalSignController?.scaleFactor = listView.scaleFactor
  3206. digitalSignController?.titleChangeBlock = { title, index in
  3207. currentPageIndex = UInt(index)
  3208. }
  3209. digitalSignController?.buttonActionBlock = { [weak self] type, isChanged in
  3210. if type == .cancel {
  3211. if let page = self?.listView.document?.page(at: currentPageIndex) {
  3212. self?.listView.go(to: page)
  3213. }
  3214. self?.exitDigitalSign()
  3215. }
  3216. }
  3217. }
  3218. // MARK: - Toolbar
  3219. func toolbarItemClickForExitMode(_ toolbarItem: KMToolbarItemView) {
  3220. if(toolbarItem.itemIdentifier != KMDocumentPageToolbarItemIdentifier) {
  3221. if (hasEnterPageEdit()) {
  3222. self.exitPageEdit()
  3223. }
  3224. }
  3225. if toolbarItem.itemIdentifier != KMDocumentRedactToolbarItemIdentifier {
  3226. if self.hasEnterRedact() {
  3227. self.exitRedact()
  3228. }
  3229. }
  3230. if toolbarItem.itemIdentifier != KMDocumentDigitalSignToolbarItemIdentifier {
  3231. if self.hasShowDigitalSign() {
  3232. self.exitDigitalSign()
  3233. }
  3234. }
  3235. if toolbarItem.itemIdentifier != KMDocumentEditToolbarItemIdentifier && toolbarItem.itemIdentifier != KMRightControlToolbarItemIdentifier && toolbarItem.itemIdentifier != KMLeftControlToolbarItemIdentifier {
  3236. }
  3237. }
  3238. // MARK: - Private Methods
  3239. private func getPDFEditController() -> KMPDFEditViewController? {
  3240. var editController: KMPDFEditViewController?
  3241. for controller in self.children {
  3242. if (controller.isKind(of: KMPDFEditViewController.self)) {
  3243. editController = (controller as! KMPDFEditViewController)
  3244. break
  3245. }
  3246. }
  3247. return editController
  3248. }
  3249. private func _getPDFRedactController() -> KMPDFRedactViewController? {
  3250. var controller: KMPDFRedactViewController?
  3251. for childC in self.children {
  3252. if (childC.isKind(of: KMPDFRedactViewController.self)) {
  3253. controller = (childC as! KMPDFRedactViewController)
  3254. break
  3255. }
  3256. }
  3257. return controller
  3258. }
  3259. private func addBackgroundMaskView() {
  3260. self.removeBackgroundMaskView()
  3261. }
  3262. private func removeBackgroundMaskView() {
  3263. }
  3264. private func _goToFirstPageForFristAppear() {
  3265. DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.1) {
  3266. self.listView.go(toPageIndex: 0, animated: false)
  3267. }
  3268. }
  3269. func isPDFPageCountExceedsLimit(filePath: String) -> Bool {
  3270. let url = URL(fileURLWithPath: filePath)
  3271. guard let document = PDFDocument(url: url) else {
  3272. return false
  3273. }
  3274. let pageCount = document.pageCount
  3275. return pageCount > 30
  3276. }
  3277. // MARK: - Redact 【标记密文】
  3278. func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
  3279. let windowController = KMRedactConfirmWindowController(type)
  3280. self.currentWindowController = windowController
  3281. self.view.window?.beginSheet(windowController.window!)
  3282. windowController.itemClick = { [weak self] index in
  3283. if (index == 2) { /// 取消
  3284. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  3285. self?.currentWindowController = nil
  3286. callback()
  3287. return
  3288. }
  3289. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  3290. self?.currentWindowController = nil
  3291. let panel = NSSavePanel()
  3292. panel.nameFieldStringValue = "[新文件]"+((self?.listView.document?.documentURL.lastPathComponent) ?? "")
  3293. let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  3294. button.state = .on
  3295. panel.accessoryView = button
  3296. panel.isExtensionHidden = true
  3297. panel.beginSheetModal(for: (self?.view.window!)!) { response in
  3298. if response != .OK {
  3299. callback()
  3300. return
  3301. }
  3302. if (type == .redactOne) {
  3303. let anno = self!.listView.activeAnnotation
  3304. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  3305. callback()
  3306. return
  3307. }
  3308. (anno as! CPDFRedactAnnotation).applyRedaction()
  3309. } else if (type == .redactAll) {
  3310. self?.listView.document?.applyRedactions()
  3311. } else if (type == .eraserOne) {
  3312. let anno = self!.listView.activeAnnotation
  3313. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  3314. callback()
  3315. return
  3316. }
  3317. anno?.page.erasureRedact(from: anno!.bounds)
  3318. } else if (type == .eraserAll) {
  3319. KMRedactTools.eraserDocument((self?.listView.document)!) { result, errorAnno in
  3320. if (result == false) {
  3321. callback()
  3322. return
  3323. }
  3324. }
  3325. }
  3326. self!.listView.document?.write(to: panel.url)
  3327. if (button.state == .on) {
  3328. NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
  3329. }
  3330. } else {
  3331. NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
  3332. }
  3333. callback()
  3334. }
  3335. }
  3336. }
  3337. // MARK: - Secure 【安全】
  3338. public func showSecureLimitTip() {
  3339. self.hiddenSecureLimitTip()
  3340. if self.secureAlertView == nil {
  3341. self.secureAlertView = KMSecureAlertView()
  3342. self.secureAlertView?.show(in: self.listView)
  3343. self.secureAlertView?.closeAction = { [unowned self] view in
  3344. self.hiddenSecureLimitTip()
  3345. self.removeFromAlertView()
  3346. self.showFormAlertView()
  3347. }
  3348. self.secureAlertView?.passwordAction = { [unowned self] view in
  3349. self.removeOwnerPassword()
  3350. self.removeFromAlertView()
  3351. self.showFormAlertView()
  3352. }
  3353. }
  3354. }
  3355. func removeOwnerPassword() {
  3356. guard let doc = self.listView.document else {
  3357. NSSound.beep()
  3358. return
  3359. }
  3360. if doc.allowsCopying && doc.allowsPrinting {
  3361. NSSound.beep()
  3362. return
  3363. }
  3364. _ = KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: doc.documentURL) { [weak self] result, password in
  3365. if result == .cancel { /// 关闭
  3366. return
  3367. }
  3368. /// 解密成功
  3369. self?.hiddenSecureLimitTip()
  3370. self?.model.isSaveKeyChain = false
  3371. self?.listView.document?.unlock(withPassword: password)
  3372. }
  3373. }
  3374. public func hiddenSecureLimitTip() {
  3375. self.secureAlertView?.removeFromSuperview()
  3376. self.secureAlertView = nil
  3377. }
  3378. //MARK: - Form
  3379. func showFormAlertView() {
  3380. if (formAlertView == nil) {
  3381. formAlertView = KMFormAlertView()
  3382. formAlertView?.isCloseSecureView = self.secureAlertView != nil ? false : true
  3383. formAlertView?.showInView(self.listView)
  3384. } else {
  3385. self.removeFromAlertView()
  3386. }
  3387. }
  3388. func removeFromAlertView() {
  3389. formAlertView?.removeFromSuperview()
  3390. formAlertView = nil
  3391. }
  3392. override func mouseMoved(with event: NSEvent) {
  3393. self.view.window?.mouseMoved(with: event)
  3394. }
  3395. func savePageNumberIfNeed() {
  3396. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  3397. let scaleFactor = self.listView.scaleFactor ?? 0
  3398. if scaleFactor <= 0 {
  3399. return
  3400. }
  3401. if self.listView.document != nil {
  3402. KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
  3403. KMPreferenceManager.shared.setPageScale(Float(self.listView.scaleFactor), forKey: self.listView.document.documentURL.path)
  3404. }
  3405. }
  3406. }
  3407. // MARK: - 显示合并窗口
  3408. public func showMergeWindow(url: URL? = nil, _ password: String?) {
  3409. DispatchQueue.main.async {
  3410. var documentURL = url
  3411. if documentURL == nil {
  3412. documentURL = self.listView.document?.documentURL
  3413. }
  3414. guard let _url = documentURL else { return }
  3415. guard let document = PDFDocument(url: _url) else { return }
  3416. self.mergeWindowController = KMMergeWindowController(document: document, password: password ?? "")
  3417. self.mergeWindowController!.oriDucumentUrl = self.listView.document?.documentURL
  3418. self.mergeWindowController!.pageIndex = self.listView.currentPageIndex
  3419. self.mergeWindowController!.cancelAction = { [unowned self] controller in
  3420. self.view.window?.endSheet(mergeWindowController!.window!)
  3421. }
  3422. self.mergeWindowController!.mergeAction = { [unowned self] controller, filePath in
  3423. self.view.window?.endSheet(mergeWindowController!.window!)
  3424. }
  3425. self.view.window?.beginSheet(self.mergeWindowController!.window!)
  3426. }
  3427. }
  3428. // MARK: - 显示加密弹窗
  3429. public func showSecureWindow(_ url: URL) {
  3430. self.securityWindowController = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
  3431. guard let securityWindowController = securityWindowController else { return }
  3432. securityWindowController.documentURL = self.listView.document?.documentURL
  3433. securityWindowController.batchAction = { [unowned self] controller, files in
  3434. self.view.window?.endSheet((securityWindowController.window)!)
  3435. let batchWindowController = KMBatchOperateWindowController.sharedWindowController
  3436. let batchOperateFile = KMBatchOperateFile(filePath: self.document?.documentURL.path ?? "", type: .AddPassword)
  3437. batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
  3438. batchWindowController.window?.makeKeyAndOrderFront("")
  3439. }
  3440. securityWindowController.doneAction = { [unowned self] controller, options, attribute in
  3441. let openPanel = NSOpenPanel()
  3442. openPanel.canChooseFiles = false
  3443. openPanel.canChooseDirectories = true
  3444. openPanel.canCreateDirectories = true
  3445. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { (result) in
  3446. if result == NSApplication.ModalResponse.OK {
  3447. for fileURL in openPanel.urls {
  3448. let document = CPDFDocument(url: self.document?.documentURL)
  3449. if document != nil {
  3450. document!.setDocumentAttributes(attribute)
  3451. let path = fileURL.path.stringByAppendingPathComponent(url.deletingPathExtension().lastPathComponent) + "_SetPassword" + "." + url.pathExtension
  3452. let success = document!.write(to: NSURL(fileURLWithPath: path) as URL, withOptions: options)
  3453. if success {
  3454. self.view.window?.endSheet((securityWindowController.window)!)
  3455. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  3456. }
  3457. }
  3458. }
  3459. }
  3460. }
  3461. }
  3462. securityWindowController.cancelAction = { [unowned self] controller in
  3463. self.view.window?.endSheet((securityWindowController.window)!)
  3464. }
  3465. NSWindow.currentWindow().beginSheet(securityWindowController.window!)
  3466. }
  3467. // MARK: - 保存文档
  3468. internal func needSaveDocument() -> Bool {
  3469. if (self.isPDFDocumentEdited) {
  3470. return self.isPDFDocumentEdited
  3471. }
  3472. if (self.needSave) {
  3473. return self.needSave
  3474. }
  3475. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  3476. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  3477. return false
  3478. }
  3479. return true
  3480. }
  3481. internal func saveDocument(overlookDocumentIfEdited overlook: Bool = false) {
  3482. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  3483. if (overlook) {
  3484. document?.save(nil)
  3485. return
  3486. }
  3487. if (self.isPDFDocumentEdited) {
  3488. self.clearIsPDFDocumentEdited()
  3489. self.needSave = false
  3490. document?.save(nil)
  3491. return
  3492. }
  3493. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  3494. return
  3495. }
  3496. document?.save(nil)
  3497. }
  3498. internal func asyncSaveDocument(overlookDocumentIfEdited overlook: Bool = false, callback:@escaping KMCommonBlock) {
  3499. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  3500. if (overlook) {
  3501. DispatchQueue.main.async {
  3502. document?.save(nil)
  3503. callback()
  3504. }
  3505. return
  3506. }
  3507. if (self.isPDFDocumentEdited) {
  3508. self.clearIsPDFDocumentEdited()
  3509. self.needSave = false
  3510. DispatchQueue.main.async {
  3511. document?.save(nil)
  3512. callback()
  3513. }
  3514. return
  3515. }
  3516. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  3517. callback()
  3518. return
  3519. }
  3520. DispatchQueue.main.async {
  3521. document?.save(nil)
  3522. callback()
  3523. }
  3524. }
  3525. internal func saveDocumentWithProgressAlert(callback:@escaping KMCommonBlock) {
  3526. // 显示进度
  3527. AutoSaveManager.manager.isSaving = true
  3528. self.showProgressWindow(message: NSLocalizedString("Save", comment: "") + "PDF")
  3529. self.progressC?.maxValue = 3.0
  3530. self.progressC?.increment(by: 1.0)
  3531. // 保存文档
  3532. self.asyncSaveDocument { [weak self] params in
  3533. // 执行进度 [假进度]
  3534. self?.progressC?.increment(by: 1.0)
  3535. self?.progressC?.increment(by: 1.0)
  3536. DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
  3537. // 隐藏进度
  3538. self?.hiddenProgressWindow()
  3539. DispatchQueue.main.asyncAfter(deadline: .now()+1) {
  3540. AutoSaveManager.manager.isSaving = false
  3541. }
  3542. // 回调
  3543. callback()
  3544. }
  3545. }
  3546. }
  3547. func SaveTempPDFDocumentToURLPath(tempPath: String) {
  3548. self.document?.write(toFile: tempPath)
  3549. }
  3550. // MARK: - 定时保存
  3551. func addAutoSaveEvent() {
  3552. if (self.autoSaveTimer != nil) {
  3553. self.autoSaveTimer?.invalidate()
  3554. self.autoSaveTimer = nil
  3555. }
  3556. if self.document != nil {
  3557. self.autoSaveTimer = Timer.scheduledTimer(withTimeInterval: AutoSaveManager.manager.timeInterval * 60, repeats: true, block: { [weak self] timer in
  3558. self?.autoSaveTimerAction(timer)
  3559. })
  3560. }
  3561. self.checkAutoSaveInfo()
  3562. }
  3563. func checkAutoSaveInfo() {
  3564. guard let cnt = AutoSaveManager.manager.autoSavePaths?.count, cnt > 0 else {
  3565. return
  3566. }
  3567. if AutoSaveManager.manager.autoSaveAlertShow {
  3568. return
  3569. }
  3570. AutoSaveManager.manager.autoSaveDidEndAction = false
  3571. AutoSaveManager.manager.autoSaveAlertShow = true
  3572. let blockSaveWindow = AutoSavePopController()
  3573. blockSaveWindow.cancelHandle = { [weak self] windowController in
  3574. AutoSaveManager.manager.autoSaveDidEndAction = true
  3575. AutoSaveManager.manager.clearCache()
  3576. self?.km_quick_endSheet()
  3577. }
  3578. blockSaveWindow.confirmHandle = { [weak self] windowController in
  3579. self?.km_quick_endSheet()
  3580. self?.saveAutoSaveInfo()
  3581. }
  3582. self.km_beginSheet(windowC: blockSaveWindow)
  3583. }
  3584. func saveAutoSaveInfo() {
  3585. let openPanel = NSOpenPanel()
  3586. openPanel.canChooseDirectories = true
  3587. openPanel.canChooseFiles = false
  3588. openPanel.allowsMultipleSelection = false
  3589. let win = NSApp.keyWindow != nil ? NSApp.keyWindow : self.view.window
  3590. openPanel.beginSheetModal(for: win!) { result in
  3591. if (result == .OK) {
  3592. let folderPath = openPanel.url?.path ?? openPanel.url?.absoluteString
  3593. for path in AutoSaveManager.manager.opendPaths ?? [] {
  3594. let _path = path as? String
  3595. var newPath = "\(folderPath ?? "")/\(_path?.lastPathComponent ?? "")"
  3596. newPath = self.getValidFilePath(newPath)
  3597. do {
  3598. try FileManager.default.moveItem(atPath: _path ?? "", toPath: newPath)
  3599. } catch {
  3600. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: newPath)])
  3601. }
  3602. }
  3603. AutoSaveManager.manager.clearCache()
  3604. }
  3605. AutoSaveManager.manager.autoSaveDidEndAction = true
  3606. }
  3607. }
  3608. func autoSaveTimerAction(_ timer: Timer) {
  3609. if (self.document == nil || self.listView.document?.documentURL.path == nil) {
  3610. return
  3611. }
  3612. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  3613. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  3614. return
  3615. }
  3616. if let data = self.document?.isLocked, data {
  3617. return
  3618. }
  3619. if AutoSaveManager.manager.autoSaveEnabled == false {
  3620. return
  3621. }
  3622. let documentArray = NSDocumentController.shared.documents
  3623. var didFileEdit = false
  3624. for doc in documentArray {
  3625. if doc.fileURL?.path == self.document?.documentURL.path {
  3626. didFileEdit = doc.isDocumentEdited
  3627. break
  3628. }
  3629. }
  3630. if (didFileEdit == false) {
  3631. return
  3632. }
  3633. AutoSaveManager.manager.isSaving = true
  3634. let savePath = AutoSaveManager.manager.autoSaveWithPath(self.listView.document?.documentURL.path ?? "")
  3635. if (!self.document!.isLocked) {
  3636. self.document?.write(to: URL(fileURLWithPath: savePath))
  3637. }
  3638. DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
  3639. AutoSaveManager.manager.isSaving = false
  3640. }
  3641. }
  3642. func removeAutoSaveInfo() {
  3643. if self.autoSaveTimer != nil {
  3644. self.autoSaveTimer?.invalidate()
  3645. self.autoSaveTimer = nil
  3646. }
  3647. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  3648. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  3649. return
  3650. }
  3651. if AutoSaveManager.manager.autoSaveEnabled == false {
  3652. return
  3653. }
  3654. if self.document == nil || self.listView.document?.documentURL.path == nil {
  3655. return
  3656. }
  3657. AutoSaveManager.manager.removeAutoSavePath(self.listView.document?.documentURL.path ?? "")
  3658. }
  3659. // MARK: - 选择 PDFDisplay 模式
  3660. public func setPDFDisplay(pdfViewMode: CPDFDisplayViewMode) {
  3661. listView.setDisplay(pdfViewMode)
  3662. }
  3663. // MARK: - 选择缩放模式
  3664. @objc public func selectZoom(_ type: KMPDFZoomType) {
  3665. switch type {
  3666. case .width:
  3667. self.listView.autoScales = true
  3668. break
  3669. case .fit:
  3670. if let pageHeight = self.listView.currentPage()?.size.height, pageHeight > 0 {
  3671. let pdfviewHeight = self.listView.bounds.size.height
  3672. self.listView.scaleFactor = pdfviewHeight/pageHeight
  3673. self.listView.autoScales = false
  3674. }
  3675. break
  3676. case .actualSize:
  3677. if self.listView.scaleFactor != 1.0 {
  3678. self.listView.scaleFactor = 1.0
  3679. self.listView.autoScales = false
  3680. }
  3681. break
  3682. }
  3683. }
  3684. internal func createPdf(index:Int) {
  3685. }
  3686. // MARK - Event 监听
  3687. private func addEventMonitor() {
  3688. if (self.eventMonitor != nil) {
  3689. self.removeEventMonitor()
  3690. }
  3691. self.eventMonitor = NSEvent.addLocalMonitorForEvents(matching: [.scrollWheel, .leftMouseDown, .leftMouseUp]) { [weak self] event in
  3692. if (event.type == .scrollWheel && event.modifierFlags.contains(.option)) { // Alt + 鼠标滚轮
  3693. self?.listView.magnifyWheel(event)
  3694. return nil
  3695. } else if event.type == .leftMouseDown {
  3696. let point = event.locationInView(self?.listView ?? NSView())
  3697. let presentationDrawView = self?.listView.presentationDrawView
  3698. if let data = self?.interactionMode, data == .presentation,CGRectContainsPoint(self?.listView.frame ?? .zero, point),presentationDrawView?.isHidden == true { // 幻灯片模式下
  3699. if point.x >= (self?.listView.frame.maxX ?? 0) / 2 {
  3700. let can = self?.listView.canGoToNextPage() ?? false
  3701. if can {
  3702. self?.listView.goToNextPage(nil)
  3703. }
  3704. } else {
  3705. let can = self?.listView.canGoToPreviousPage() ?? false
  3706. if can {
  3707. self?.listView.goToPreviousPage(nil)
  3708. }
  3709. }
  3710. return nil
  3711. }
  3712. }
  3713. return event
  3714. }
  3715. }
  3716. func addKeyEventMonitor() {
  3717. if (self.keyEventMonitor != nil) {
  3718. self.removeKeyEventMonitor()
  3719. }
  3720. keyEventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
  3721. if event.keyCode == 53 {
  3722. if let data = self?.interactionMode, data == .presentation { // 幻灯片模式下
  3723. self?.exitFullScreen()
  3724. return event
  3725. }
  3726. if self?.listView.toolMode == .editPDFToolMode {
  3727. if self != nil {
  3728. //使用editingSelectionString获取内容文字
  3729. if self!.listView.editingAreas() != nil {
  3730. if self!.listView.editingAreas().count > 0 && self!.listView.isEditable() {
  3731. self!.listView.clearEditingSelectCharItem()
  3732. } else if self!.listView.editingAreas().count > 0 {
  3733. if self?.listView.annotationType == .addImage ||
  3734. self?.listView.annotationType == .addText {
  3735. }
  3736. self?.listView.endEditIsRemoveBlock(with: self!.listView.editingAreas().first as? CPDFEditArea)
  3737. self?.listView.updateEditing([])
  3738. self?.listView.isEditImage = false
  3739. self?.listView.setNeedsDisplayPageViewFor(self!.listView.currentPage())
  3740. if self?.listView.annotationType == .addImage {
  3741. self?.listView.change([.text, .image])
  3742. }
  3743. self?.listView.annotationType = .editTextImage
  3744. self?.closeRightPane()
  3745. } else if(self?.listView.annotationType == .addImage || self!.listView.annotationType == .addText) {
  3746. if self?.listView.annotationType == .addImage ||
  3747. self?.listView.annotationType == .addText {
  3748. }
  3749. self?.listView.setShouAddEdit([])
  3750. self?.listView.change([.text, .image])
  3751. self?.listView.annotationType = .editTextImage
  3752. self?.closeRightPane()
  3753. }
  3754. } else {
  3755. if self?.listView.annotationType == .addImage ||
  3756. self?.listView.annotationType == .addText {
  3757. }
  3758. }
  3759. }
  3760. }
  3761. } else {
  3762. if let data = self?.interactionMode, data == .presentation { // 幻灯片模式下
  3763. self?.listView.keyDown(with: event)
  3764. return event
  3765. } else {
  3766. let cmd = event.modifierFlags.contains(.command)
  3767. let shift = event.modifierFlags.contains(.shift)
  3768. if event.keyCode == 6 { // z
  3769. let editPDFIng = self?.listView.isEditing() ?? false
  3770. if cmd && shift { // 恢复
  3771. let can = self?.listView.canEditTextRedo() ?? false
  3772. if can == false {
  3773. return event
  3774. }
  3775. if editPDFIng {
  3776. _ = CustomAlertView.alertView(message: NSLocalizedString("Redo", comment: ""), fromView: self!.view, withStyle: .black)
  3777. }
  3778. } else if cmd { // 撤回
  3779. let can = self?.listView.canEditTextUndo() ?? false
  3780. if can == false {
  3781. return event
  3782. }
  3783. if editPDFIng {
  3784. _ = CustomAlertView.alertView(message: NSLocalizedString("Undo", comment: ""), fromView: self!.view, withStyle: .black)
  3785. }
  3786. }
  3787. }
  3788. }
  3789. }
  3790. return event
  3791. }
  3792. }
  3793. func removeKeyEventMonitor() {
  3794. if (self.keyEventMonitor != nil) {
  3795. KMPrint("removeKeyEventMonitor 已移除事件监听")
  3796. NSEvent.removeMonitor(self.keyEventMonitor as Any)
  3797. self.keyEventMonitor = nil
  3798. }
  3799. }
  3800. private func removeEventMonitor() {
  3801. if (self.eventMonitor != nil) {
  3802. KMPrint("已移除事件监听")
  3803. NSEvent.removeMonitor(self.eventMonitor as Any)
  3804. self.eventMonitor = nil
  3805. }
  3806. }
  3807. // MARK: - Tools
  3808. func pdfViewCanHorizontalScroll() -> Bool {
  3809. let scroll = self.listView.scroll()
  3810. if (scroll == nil) {
  3811. return false
  3812. }
  3813. return scroll?.horizontalScroller?.isHidden == nil ? false : !(scroll!.horizontalScroller!.isHidden)
  3814. }
  3815. func pdfViewCanVerticalScroll() -> Bool {
  3816. let scroll = self.listView.scroll()
  3817. if (scroll == nil) {
  3818. return false
  3819. }
  3820. return scroll?.verticalScroller?.isHidden == nil ? false : !(scroll!.verticalScroller!.isHidden)
  3821. }
  3822. // MARK: - Public Methods
  3823. // 清理数据 [eg. 通知]
  3824. public func clearData() {
  3825. KMThumbnailCache.shared.clearCache()
  3826. self.removeNotifications()
  3827. if (self.listView.spellingTag() > 0) {
  3828. NSSpellChecker.shared.closeSpellDocument(withTag: self.listView.spellingTag())
  3829. }
  3830. self.removeAutoSaveInfo()
  3831. self.myDocument = nil
  3832. }
  3833. public func clearSecureOptions() {
  3834. self._secureOptions = nil
  3835. self.documentAttribute = nil
  3836. }
  3837. public func recordRemoveSecureFlag() {
  3838. self._removeSecureFlag = true
  3839. self.clearSecureOptions()
  3840. self.recordIsPDFDocumentEdited(type: .removePassword)
  3841. self._needSave = true
  3842. }
  3843. public func clearRemoveSecureFlag() {
  3844. self._removeSecureFlag = false
  3845. }
  3846. public func clearSaveWatermarkFlag() {
  3847. km_synchronized(self) {
  3848. self._saveWatermarkFlag = false
  3849. }
  3850. }
  3851. public func recordIsPDFDocumentEdited(type: KMSubscribeWaterMarkType = .none) {
  3852. km_synchronized(self) {
  3853. self.model.isPDFDocumentEdited = true
  3854. if type == .editText || type == .editImage {
  3855. self.leftSideViewController.updateThumbnail(at: self.listView.currentPageIndex)
  3856. }
  3857. if let _document = self.myDocument {
  3858. KMTools.setDocumentEditedState(document: _document)
  3859. }
  3860. }
  3861. }
  3862. public func clearIsPDFDocumentEdited() {
  3863. km_synchronized(self) {
  3864. self.model.isPDFDocumentEdited = false
  3865. }
  3866. }
  3867. func showSnapshots(setups: NSArray?) {
  3868. if self.listView.document != nil {
  3869. for setup in setups ?? [] {
  3870. let swc = KMSnapshotWindowController()
  3871. swc.delegate = self
  3872. swc.setPdfDocument(self.listView.document, setup: setup as? NSDictionary)
  3873. swc.setForceOnTop(self.interactionMode != .normal)
  3874. self.myDocument?.addWindowController(swc)
  3875. }
  3876. }
  3877. }
  3878. func dealDocumentDidLoaded() {
  3879. self.removeBackgroundMaskView()
  3880. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  3881. self.showSecureLimitTip()
  3882. }
  3883. if self.model.needConvertNotes {
  3884. self.showConvertNotesProgress()
  3885. }
  3886. if (self._documentFirstLoad) {
  3887. self.checkShouldAutoOpenLeftVC()
  3888. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  3889. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document?.documentURL.path ?? "")
  3890. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document?.documentURL.path ?? "")
  3891. if (pageScale != nil) {
  3892. self.listView.scaleFactor = CGFloat(pageScale!)
  3893. }
  3894. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < (self.listView.document?.pageCount ?? 0)) {
  3895. self.listView.go(toPageIndex: pageNumber!, animated: false)
  3896. } else {
  3897. self._goToFirstPageForFristAppear()
  3898. }
  3899. } else {
  3900. self._goToFirstPageForFristAppear()
  3901. }
  3902. self._documentFirstLoad = false
  3903. }
  3904. }
  3905. func tabViewIsDragging() -> Bool {
  3906. let level = self.view.window?.level ?? .normal
  3907. return level == .floating
  3908. }
  3909. // MARK: - Noti Actions
  3910. internal func documentDidUnlockNotification(_ sender: Notification) {
  3911. if (self.listView.document != nil && self.listView.document.isEqual(to: sender.object)) {
  3912. if (self.myDocument == nil) {
  3913. return
  3914. }
  3915. if (self.listView.document.allowsPrinting && self.listView.document.allowsCopying) {
  3916. self.hiddenSecureLimitTip()
  3917. }
  3918. let isUnlockFromKeychain = (self.myDocument as? KMMainDocument)?.isUnlockFromKeychain ?? false
  3919. if (isUnlockFromKeychain || self.model.isSaveKeyChain == false) {
  3920. return
  3921. }
  3922. let type = KMPreferenceManager.shared.savePasswordType
  3923. if (type == .never) {
  3924. return
  3925. }
  3926. if (type == .always) {
  3927. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  3928. return
  3929. }
  3930. // 保存到钥匙串
  3931. let alert = NSAlert()
  3932. alert.messageText = NSLocalizedString("Remember Password?", comment: "")
  3933. alert.informativeText = NSLocalizedString("Do you want to save this password in your Keychain?", comment: "")
  3934. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  3935. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  3936. if (alert.runModal() == .alertFirstButtonReturn) { // 保存密码
  3937. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  3938. return
  3939. }
  3940. }
  3941. }
  3942. func annotationsAttributeHasChange(_ sender: Notification) {
  3943. guard let dict = sender.object as? [String : Any] else {
  3944. return
  3945. }
  3946. if let anno = dict["object"] as? CPDFAnnotation {
  3947. let value = dict["keyPath"] as? String ?? ""
  3948. let didEnd = dict["didEnd"] as? Bool ?? false
  3949. if didEnd {
  3950. if value == CPDFAnnotationBoundsKey {
  3951. if anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation {
  3952. anno.contents = anno.page?.string(for: anno.bounds) ?? ""
  3953. }
  3954. }
  3955. if anno.km_isMeasure() && anno.contents == nil {
  3956. anno.contents = anno.string() ?? ""
  3957. }
  3958. self.leftSideViewController.refreshUIForAnnoAttributeDidChange(anno, attributes: ["keyPath" : value])
  3959. } else {
  3960. if value != CPDFAnnotationBoundsKey && value != CPDFAnnotationStartPointKey && value != CPDFAnnotationEndPointKey && value != CPDFAnnotationPathsKey { // 改变bounds(箭头、直线注释 开始点和结束点, 手绘注释的paths)的操作会卡顿,比如移动
  3961. self.leftSideViewController.refreshUIForAnnoAttributeDidChange(anno, attributes: ["keyPath" : value])
  3962. }
  3963. }
  3964. }
  3965. }
  3966. internal func applicationWillTerminateNotification(_ sender: Notification) {
  3967. self.savePageNumberIfNeed()
  3968. self.saveDocument()
  3969. }
  3970. func CPDFDocumentPageCountChangedNotification(_ sender: Notification) {
  3971. self.leftSideViewController.refreshUIForAnnoAttributeDidChange(nil, attributes: nil)
  3972. }
  3973. func CEditPDFToolModeChangeStateUnkownNotification(_ sender: Notification) {
  3974. var editSelectd = false
  3975. if (self.listView.annotationType == .addText || self.listView.annotationType == .addImage) && self.listView.toolMode == .editPDFToolMode {
  3976. editSelectd = true
  3977. }
  3978. if self.listView.toolMode == .editPDFToolMode {
  3979. if editSelectd {
  3980. }
  3981. }
  3982. }
  3983. @objc func handlePageChangedNotification(_ sender: Notification) {
  3984. if self.mwcFlags.isSwitchingFullScreen > 0 {
  3985. return
  3986. }
  3987. let page = self.listView.currentPage()
  3988. let pageIndex = page?.pageIndex() ?? 0
  3989. self.leftSideViewController.thumb_selectRowIndexsIfNeed(IndexSet(integer: IndexSet.Element(pageIndex)))
  3990. self.leftSideViewController.thumbnailTableView.needsDisplay = true
  3991. self.leftSideViewController.tocOutlineView.needsDisplay = true
  3992. }
  3993. @objc func handleDisplayBoxChangedNotification(_ sender: Notification) {
  3994. self.leftSideViewController.reloadThumbnailDataIfNeed()
  3995. }
  3996. }