KMMainViewController.swift 236 KB

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