KMMainViewController.swift 342 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860
  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: KMNBaseViewController, 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: NSSplitView!
  21. @IBOutlet var infoSplitLeftView: NSView!
  22. @IBOutlet var infoSplitRightView: NSView!
  23. @IBOutlet var infoSplitCenterView: NSView!
  24. @IBOutlet var infoSplitCenterSubView: NSBox!
  25. @IBOutlet var pdfSplitView: NSSplitView!
  26. @IBOutlet var pdfSplitTopView: NSView!
  27. @IBOutlet var pdfSplitBottomView: NSView!
  28. @IBOutlet var toolbarBoxHeightConst: NSLayoutConstraint!
  29. @IBOutlet var infoSplitViewLeftConst: NSLayoutConstraint!
  30. @IBOutlet var infoSplitViewRightConst: NSLayoutConstraint!
  31. @IBOutlet var infoSplitViewTopConst: NSLayoutConstraint!
  32. var viewManager: KMPDFViewManager = KMPDFViewManager.init()
  33. var toolbarManager: KMPDFToolbarManager = KMPDFToolbarManager.init()
  34. var listView: CPDFListView = CPDFListView.init()
  35. var document: CPDFDocument?
  36. var myDocument: NSDocument?
  37. var isFirstOpen: Bool = true
  38. var insertDocuments: Set<CPDFDocument> = [] //插入新文档时,SDK会出现崩溃,临时记录
  39. //工具栏
  40. private var pdfToolbarController: KMPDFToolbarController?
  41. //BOTA SideBar
  42. private var sideBarController: KMPDFSideBarController?
  43. //页面编辑
  44. private var pageEditViewController: KMNPageEditViewController?
  45. //DisplaySetting
  46. private var displaySettingController: KMNDisplayViewController?
  47. //SPlitPDF分屏视图
  48. private var splitPDFController: KMSplitPDFViewController?
  49. private var pageNumberToolbar: KMPageNumberPromptView?
  50. //PPT操作界面
  51. var presentationTopViewController: KMPresentationTopViewController?
  52. //Edit
  53. var editToolbarView: KMEditToolbarView?
  54. //水印
  55. var watermarkViewController: KMWatermarkController?
  56. //背景
  57. var backgroundViewController: KMBackgroundController?
  58. //Header&Footer
  59. var headerFooterViewController: KMHeaderFooterController?
  60. //Bates
  61. var batesViewController: KMBatesController?
  62. //Crop
  63. var cropController: KMCropController?
  64. //左边
  65. var botaViewController: KMNLeftSideViewController?
  66. //右边
  67. var rightSideController: KMRightSideController?
  68. let alertTipViewController: KMNAlertTipViewController = KMNAlertTipViewController(nibName: "KMNAlertTipViewController", bundle: nil)
  69. //合并
  70. var mergeWindowController: KMMergeWindowController?
  71. //春季活动
  72. var recommondPopWindowVC: KMRecommondPopWindow?
  73. //数字签名状态
  74. var signaturestateVC: CDSignatureCertificateStateViewController = CDSignatureCertificateStateViewController.init()
  75. //PDFView右键菜单
  76. private var groupListMenuGroup: ComponentGroup? = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  77. var textFieldSheet: KMTextFieldSheetController = KMTextFieldSheetController.init(windowNibName: "KMTextFieldSheetController")
  78. @IBOutlet weak var leftView: NSView!
  79. var model = KMMainModel()
  80. //Search
  81. var searchIndex: Int = 0
  82. //对比
  83. var isCompareModel: Bool = false
  84. //密码弹窗
  85. var passwordWindow: KMPasswordInputWindow?
  86. var securityWindowController: KMSecurityWindowController?
  87. //压缩
  88. var compressWindowController: KMCompressWindowController?
  89. //引导
  90. var guideInfoWindowController: KMGuideInfoWindowController?
  91. //测量
  92. var distanceMeasureInfoWindowController: CDistanceMeasureInfoWindowController?
  93. var perimeterMeasureInfoWindowController: CPerimeterMeasureInfoWindowController?
  94. var areaMeasureInfoWindowController: CAreaMeasureInfoWindowController?
  95. //打印
  96. var bookletWindowController: KMPDFBookletWindowController?
  97. var multiplePrintWindowController: KMPDFMultiplePrintWindowController?
  98. var posterPrintWindowController: KMPDFPosterPrintWindowController?
  99. var removeAllAnnotationsStore = KMPDFViewRemoveAllAnnotationsStore()
  100. private var _needSave = false
  101. var needSave: Bool {
  102. set {
  103. _needSave = newValue
  104. if (_needSave == false) {
  105. self.clearIsPDFDocumentEdited()
  106. }
  107. }
  108. get {
  109. return _needSave
  110. }
  111. }
  112. var isPDFDocumentEdited: Bool {
  113. get {
  114. return self.model.isPDFDocumentEdited
  115. }
  116. }
  117. var newMwcFlags = KMNMWCFlags(settingUpWindow: true)
  118. var searchResults: [KMSearchMode] = []
  119. var mwcFlags: MwcFlags = MwcFlags()
  120. weak var browserWindowController: KMBrowserWindowController? //慎直接使用这个方法
  121. var currentWindowController: NSWindowController!
  122. var savedNormalSetup: NSMutableDictionary = NSMutableDictionary()
  123. let CPDFOfficeLeftSidePaneWidthKey = "CPDFOfficeLeftSidePaneWidthKey"
  124. let CPDFOfficeRightSidePaneWidthKey = "CPDFOfficeRightSidePaneWidthKey"
  125. var extract: KMExtractImageWindowController?
  126. var pageNumber: UInt?
  127. var autoSaveTimer: Timer?
  128. private var _documentFirstLoad: Bool = true
  129. var eventMonitor: Any?
  130. var keyEventMonitor: Any?
  131. var mouseRightMenuEvent: NSEvent?
  132. lazy private var homeVC: KMNHomeViewController? = {
  133. let vc = KMNHomeViewController()
  134. return vc
  135. }()
  136. fileprivate var _secureOptions: [CPDFDocumentWriteOption : Any]?
  137. var secureOptions: [CPDFDocumentWriteOption : Any]? {
  138. get {
  139. return self._secureOptions
  140. }
  141. }
  142. var documentAttribute: [CPDFDocumentAttribute : Any]?
  143. fileprivate var _removeSecureFlag = false
  144. var removeSecureFlag: Bool {
  145. get {
  146. return self._removeSecureFlag
  147. }
  148. }
  149. fileprivate var _saveWatermarkFlag = false
  150. var saveWatermarkFlag: Bool {
  151. get {
  152. return self._saveWatermarkFlag
  153. }
  154. }
  155. private var mainWindow_: NSWindow?
  156. var mainWindow: NSWindow? {
  157. get {
  158. return self.mainWindow_
  159. }
  160. set {
  161. self.mainWindow_ = newValue
  162. }
  163. }
  164. var setDocument: CPDFDocument? {
  165. get {
  166. return document
  167. }
  168. set {
  169. if document != newValue {
  170. document = newValue
  171. }
  172. listView.document = document
  173. botaViewController?.changeDocument(document: document)
  174. }
  175. }
  176. var setPageNumber: UInt {
  177. get {
  178. return pageNumber!
  179. }
  180. set {
  181. let pageCount = listView.document?.pageCount ?? 0
  182. var value = newValue
  183. if value > pageCount {
  184. value = pageCount
  185. }
  186. if value > 0 && listView.currentPage().pageIndex() != value-1 {
  187. listView.go(to: listView.document?.page(at: value-1))
  188. }
  189. if pageNumber != value {
  190. pageNumber = value
  191. }
  192. }
  193. }
  194. //MARK: - func
  195. deinit {
  196. NotificationCenter.default.removeObserver(self)
  197. self.listView.delegate = nil
  198. self.listView.document?.delegate = nil
  199. self.removeEventMonitor()
  200. }
  201. override func viewDidLoad() {
  202. super.viewDidLoad()
  203. // Do view setup here.
  204. setupData()
  205. setupUI()
  206. NotificationCenter.default.addObserver(self, selector: #selector(pdfViewScrollViewDidScroll), name: NSScrollView.didLiveScrollNotification, object: listView.documentView())
  207. NotificationCenter.default.addObserver(self, selector: #selector(pageCountChangedNotification), name: NSNotification.Name.CPDFDocumentPageCountChanged, object: listView.document)
  208. }
  209. override func viewDidAppear() {
  210. super.viewDidAppear()
  211. reloadPopUIWindow()
  212. addEventMonitor()
  213. addKeyEventMonitor()
  214. }
  215. override func viewDidDisappear() {
  216. super.viewDidDisappear()
  217. toggleClosePopUIWindow()
  218. }
  219. override func initContentView() {
  220. super.initContentView()
  221. }
  222. override func updateUIThemeColor() {
  223. super.updateUIThemeColor()
  224. }
  225. override func updateUILanguage() {
  226. super.updateUILanguage()
  227. }
  228. //MARK: - private
  229. func setupUI() {
  230. initPDFView()
  231. initToolbar()
  232. initSideBar()
  233. setUpSplitView()
  234. }
  235. func setupData() {
  236. toolbarManager.pdfViewManager = viewManager
  237. if (UserDefaults.standard.object(forKey: CPDFViewLeftSidePaneWidthKey) != nil) {
  238. UserDefaults.standard.set(MIN_SIDE_PANE_WIDTH, forKey: CPDFViewLeftSidePaneWidthKey)
  239. UserDefaults.standard.synchronize()
  240. }
  241. newMwcFlags.settingUpWindow = true
  242. }
  243. @objc func editFontColorItemPanelAction(_ sender: Any) {
  244. if let color = (sender as? NSColorPanel)?.color {
  245. listView.changeEditingTextarea_Color(color: color)
  246. }
  247. }
  248. //MARK: - document load成功
  249. private func documentLoadComplete() {
  250. initLeftSideController()
  251. addNotificationCenter()
  252. activityLoadMethod()
  253. let readModel = UserDefaults.standard.bool(forKey: CPDFViewIsReadModeKey)
  254. if readModel == true {
  255. self.openPDFReadMode()
  256. }
  257. reloadDigitalSigns()
  258. alertTipViewController.showInView(listView: listView, subView: infoSplitCenterView)
  259. alertTipViewController.formFieldHighlightCallback = { [weak self] in
  260. let value = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  261. CPDFAnnotation.updateHighlightFormFiled(self?.listView, highlightFormFiled: !value)
  262. self?.toolbarManager.refreshDefaultConfigItem()
  263. }
  264. alertTipViewController.enterPasswordCallback = { [weak self] in
  265. self?.removeOwnerPassword()
  266. }
  267. alertTipViewController.digitalDetailsCallback = { [weak self] in
  268. }
  269. alertTipViewController.redactApplyCallback = { [weak self] in
  270. self?.redactApplyAction()
  271. }
  272. alertTipViewController.heightCallback = { [weak self] height in
  273. self?.infoSplitViewTopConst.constant = height
  274. }
  275. newMwcFlags.settingUpWindow = false
  276. loadUserDefaultsData()
  277. }
  278. private func saveAsPath() {
  279. let saveAccessCtr = KMSavePanelAccessoryController()
  280. var fileName = listView.document.documentURL.deletingPathExtension().lastPathComponent
  281. fileName = String(format: "%@_%@.pdf", fileName, "Redact")
  282. let outputSavePanel = NSSavePanel()
  283. outputSavePanel.allowedFileTypes = ["pdf"]
  284. outputSavePanel.nameFieldStringValue = fileName
  285. outputSavePanel.accessoryView = saveAccessCtr.view
  286. outputSavePanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  287. if result == .OK {
  288. self.listView.document.applyRedactions()
  289. self.showProgressWindow(message: KMLocalizedString("Save") + "PDF")
  290. self.progressC?.maxValue = 3.0
  291. self.progressC?.increment(by: 1.0)
  292. let savePDFPath = outputSavePanel.url!.path
  293. // 执行进度 [假进度]
  294. self.progressC?.increment(by: 1.0)
  295. self.progressC?.increment(by: 1.0)
  296. let isSuccess = self.listView.document.write(toFile: savePDFPath)
  297. if (isSuccess) {
  298. if (saveAccessCtr.openAutomaticButton.state == .on) {
  299. NSDocumentController.shared.km_safe_openDocument(withContentsOf: outputSavePanel.url!, display: true) { _, _, _ in
  300. }
  301. } else {
  302. let url = URL(fileURLWithPath: savePDFPath)
  303. NSWorkspace.shared.activateFileViewerSelecting([url])
  304. }
  305. }
  306. self.hiddenProgressWindow()
  307. }
  308. }
  309. }
  310. private func reloadDigitalSigns() {
  311. let signatures = listView.document.signatures()
  312. for i in 0 ..< (signatures?.count ?? 0) {
  313. let signature:CPDFSignature = signatures?[i] ?? CPDFSignature()
  314. if signature.signers.count > 0 {
  315. signature.verifySignature(with: listView.document)//耗时,注意
  316. }
  317. }
  318. listView.signatures = signatures
  319. }
  320. //MARK: - 活动加载相关
  321. private func activityLoadMethod() {
  322. }
  323. private func addNotificationCenter() {
  324. NotificationCenter.default.addObserver(self, selector: #selector(annotationsAttributeHasChange), name: NSNotification.Name.CPDFListViewAnnotationsAttributeHasChange, object:nil)
  325. NotificationCenter.default.addObserver(self, selector: #selector(signatureStateChangeNoti), name: NSNotification.Name(rawValue: "CSignatureTrustCerDidChangeNotification"), object: nil)
  326. }
  327. private func loadUserDefaultsData() {
  328. applyLeftSideWidth(0, rightSideWidth: 0) //初始打开左边栏
  329. //初始化侧边栏打开内容
  330. if SettingsManager.sharedInstance.leftPanelType == .defaultOpen {
  331. if SettingsManager.sharedInstance.defaultOpen == .thumbnail {
  332. viewManager.pdfSideBarType = .thumbnail
  333. } else if SettingsManager.sharedInstance.defaultOpen == .outline {
  334. viewManager.pdfSideBarType = .outline
  335. } else if SettingsManager.sharedInstance.defaultOpen == .bookmark {
  336. viewManager.pdfSideBarType = .bookmark
  337. } else if SettingsManager.sharedInstance.defaultOpen == .annotation {
  338. viewManager.pdfSideBarType = .annotation
  339. }
  340. } else if SettingsManager.sharedInstance.leftPanelType == .sameAsLastOpen {
  341. if let value = UserDefaults.standard.value(forKey: "KMPDFSidebarTypeKey"), let data = value as? Int {
  342. let pdfSideBarType: KMPDFSidebarType = KMPDFSidebarType(rawValue: data) ?? .none
  343. viewManager.pdfSideBarType = pdfSideBarType
  344. }
  345. } else if SettingsManager.sharedInstance.leftPanelType == .hideLeftSide {
  346. viewManager.pdfSideBarType = .none
  347. } else if SettingsManager.sharedInstance.leftPanelType == .prioritizeOutline {
  348. }
  349. if viewManager.pdfSideBarType == .none {
  350. toggleCloseLeftSide()
  351. } else {
  352. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15) {
  353. self.toggleCloseLeftSide()
  354. self.toggleOpenLeftSide(pdfSideBarType: self.viewManager.pdfSideBarType)
  355. }
  356. }
  357. //Layout & Zoom
  358. let layoutType = SettingsManager.sharedInstance.layoutType
  359. if layoutType == .singlePage {
  360. self.updatePDFViewDisplayMode(viewMode: .singlePage)
  361. } else if layoutType == .singlePageContinue {
  362. self.updatePDFViewDisplayMode(viewMode: .singlePageContinuous)
  363. } else if layoutType == .twoPage {
  364. self.updatePDFViewDisplayMode(viewMode: .twoUp)
  365. } else if layoutType == .twoPageContinue {
  366. self.updatePDFViewDisplayMode(viewMode: .twoUpContinuous)
  367. } else if layoutType == .bookMode {
  368. self.updatePDFViewDisplayMode(isbookMode: true, direction: .horizontal)
  369. } else if layoutType == .bookModeContinue {
  370. self.updatePDFViewDisplayMode(isbookMode: true, direction: .vertical)
  371. }
  372. if self.listView.document != nil {
  373. if let pageIndex = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document.documentURL.path) {
  374. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15) {
  375. self.listView.go(toPageIndex: pageIndex, animated: false)
  376. }
  377. }
  378. if let scale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document.documentURL.path) {
  379. self.listView.scaleFactor = CGFloat(scale)
  380. } else {
  381. let zoomType = SettingsManager.sharedInstance.zoomType
  382. self.refreshPDFViewZoomInfo(zoomType)
  383. }
  384. }
  385. }
  386. func refreshPDFViewZoomInfo(_ zoomType: zoomInfoType) {
  387. if zoomType == .adaptationWidth {
  388. listView.autoScales = true
  389. } else if zoomType == .adapPage {
  390. let pageHeight = listView.currentPage().size.height
  391. let pdfviewHeight = listView.bounds.size.height
  392. listView.scaleFactor = pdfviewHeight/pageHeight
  393. listView.autoScales = false
  394. } else if zoomType == .actualSize {
  395. if listView.scaleFactor != 1.0 {
  396. listView.scaleFactor = 1.0
  397. listView.autoScales = false
  398. }
  399. } else if zoomType == .percent_10 {
  400. listView.scaleFactor = 0.1
  401. } else if zoomType == .percent_25 {
  402. listView.scaleFactor = 0.25
  403. } else if zoomType == .percent_50 {
  404. listView.scaleFactor = 0.5
  405. } else if zoomType == .percent_75 {
  406. listView.scaleFactor = 0.75
  407. } else if zoomType == .percent_100 {
  408. listView.scaleFactor = 1.0
  409. } else if zoomType == .percent_150 {
  410. listView.scaleFactor = 1.5
  411. } else if zoomType == .percent_200 {
  412. listView.scaleFactor = 2.0
  413. } else if zoomType == .percent_400 {
  414. listView.scaleFactor = 4.0
  415. } else if zoomType == .percent_800 {
  416. listView.scaleFactor = 8.0
  417. }
  418. }
  419. func applyLeftSideWidth(_ leftSideWidth: CGFloat, rightSideWidth: CGFloat) -> Void {
  420. infoContendSplitView.setPosition(infoContendSplitView.maxPossiblePositionOfDivider(at: 1) - infoContendSplitView.dividerThickness - rightSideWidth, ofDividerAt: 1)
  421. infoContendSplitView.setPosition(leftSideWidth, ofDividerAt: 0)
  422. }
  423. //MARK: - PDFView
  424. func initPDFView() {
  425. listView.autoresizingMask = [.width, .height]
  426. listView.autoScales = true
  427. listView.delegate = self
  428. listView.pdfListViewDelegate = self
  429. listView.document = self.document
  430. listView.pageBreakMargins = NSEdgeInsetsMake(10, 0, 10, 10)
  431. listView.hideNotes = false
  432. if let _ = UserDefaults.standard.value(forKey: "kPDFViewShowFormFieldNameKey") {
  433. listView.showFormFieldName = UserDefaults.getDefaultBoolValue(forKey: "kPDFViewShowFormFieldNameKey")
  434. } else {
  435. listView.showFormFieldName = true
  436. }
  437. reloadPDFSplitInfo()
  438. }
  439. func updatePDFViewAnnotationMode() {
  440. let toolbarMode = viewManager.toolMode
  441. let subToolMode = viewManager.subToolMode
  442. listView.isHidden = false
  443. if toolbarMode == .None {
  444. listView.toolMode = .CTextToolMode
  445. listView.annotationType = .unkown
  446. } else if toolbarMode == .Markup {
  447. if subToolMode == .None {
  448. listView.toolMode = .CTextToolMode
  449. } else {
  450. listView.toolMode = .CNoteToolMode
  451. }
  452. //MARK: -Markup
  453. if subToolMode == .None {
  454. listView.annotationType = .unkown
  455. } else if subToolMode == .Highlight {
  456. listView.annotationType = .highlight
  457. } else if subToolMode == .Underline {
  458. listView.annotationType = .underline
  459. } else if subToolMode == .Waveline {
  460. listView.annotationType = .squiggly
  461. } else if subToolMode == .Strikethrough {
  462. listView.annotationType = .strikeOut
  463. } else if subToolMode == .Text {
  464. listView.annotationType = .freeText
  465. } else if subToolMode == .Note {
  466. listView.annotationType = .anchored
  467. } else if subToolMode == .Pen {
  468. listView.annotationType = .ink
  469. } else if subToolMode == .Eraser {
  470. listView.annotationType = .eraser
  471. } else if subToolMode == .Rectangle {
  472. listView.annotationType = .square
  473. } else if subToolMode == .Circle {
  474. listView.annotationType = .circle
  475. } else if subToolMode == .Arrow {
  476. listView.annotationType = .arrow
  477. } else if subToolMode == .Line {
  478. listView.annotationType = .line
  479. } else if subToolMode == .Measure {
  480. listView.annotationType = CPDFMeasureDefaultInfo.default_measureType()
  481. refreshMeasureInfo()
  482. } else if subToolMode == .Stamp {
  483. listView.annotationType = .stamp
  484. } else if subToolMode == .Sign {
  485. listView.annotationType = .signSignature
  486. }
  487. } else if toolbarMode == .Edit {
  488. //MARK: -编辑
  489. if subToolMode == .None {
  490. if listView.toolMode != .CEditPDFToolMode {
  491. listView.toolMode = .CEditPDFToolMode
  492. listView.configPDFEditingInfo()
  493. }
  494. let editingPDFLoadType: CEditingLoadType = listView.editingPDFLoadType()
  495. listView.setShouAddEdit([])
  496. listView.change([.text, .image])
  497. } else if subToolMode == .Edit_text {
  498. if listView.toolMode != .CEditPDFToolMode {
  499. listView.toolMode = .CEditPDFToolMode
  500. listView.configPDFEditingInfo()
  501. }
  502. listView.setShouAddEdit([.text])
  503. listView.change(.text)
  504. } else if subToolMode == .Edit_Image {
  505. if listView.toolMode != .CEditPDFToolMode {
  506. listView.toolMode = .CEditPDFToolMode
  507. listView.configPDFEditingInfo()
  508. }
  509. listView.setShouAddEdit([.image])
  510. listView.change(.image)
  511. } else if subToolMode == .Edit_Link {
  512. listView.toolMode = .CEditLinkToolMode
  513. listView.annotationType = .link
  514. } else if subToolMode == .Edit_Crop {
  515. listView.isHidden = true
  516. listView.toolMode = .CCropToolMode
  517. }
  518. if viewManager.editType == .watermark ||
  519. viewManager.editType == .background ||
  520. viewManager.editType == .header_Footer ||
  521. viewManager.editType == .bates {
  522. listView.isHidden = true
  523. listView.toolMode = .CTextToolMode
  524. }
  525. if subToolMode != .Edit_Crop {
  526. removeCropController()
  527. }
  528. self.showOCREditAlert()
  529. } else if toolbarMode == .Form {
  530. //MARK: -Form表单
  531. listView.toolMode = .CFormToolMode
  532. if subToolMode == .None {
  533. listView.annotationType = .unkown
  534. } else if subToolMode == .Form_text {
  535. listView.annotationType = .textField
  536. } else if subToolMode == .Form_checkbox {
  537. listView.annotationType = .checkBox
  538. } else if subToolMode == .Form_radio {
  539. listView.annotationType = .radioButton
  540. } else if subToolMode == .Form_list {
  541. listView.annotationType = .listMenu
  542. } else if subToolMode == .Form_dropdown {
  543. listView.annotationType = .comboBox
  544. } else if subToolMode == .Form_OK {
  545. listView.annotationType = .actionButton
  546. } else if subToolMode == .Form_digitalSign {
  547. listView.annotationType = .signature
  548. }
  549. } else if toolbarMode == .Fill {
  550. //MARK: -填充
  551. if subToolMode == .None {
  552. listView.toolMode = .CTextToolMode
  553. } else {
  554. listView.toolMode = .CNoteToolMode
  555. }
  556. if subToolMode == .None {
  557. } else if subToolMode == .Fill_tick {
  558. listView.annotationType = .signTure
  559. } else if subToolMode == .fill_fork {
  560. listView.annotationType = .signFalse
  561. } else if subToolMode == .fill_rectangle {
  562. listView.annotationType = .signCircle
  563. } else if subToolMode == .fill_line {
  564. listView.annotationType = .signLine
  565. } else if subToolMode == .fill_dot {
  566. listView.annotationType = .signDot
  567. } else if subToolMode == .fill_date {
  568. listView.annotationType = .signDate
  569. } else if subToolMode == .fill_sign {
  570. listView.annotationType = .signSignature
  571. }
  572. } else if toolbarMode == .Convert {
  573. //MARK: -转档
  574. listView.toolMode = .CTextToolMode
  575. } else if toolbarMode == .Protect {
  576. //MARK: -Protect
  577. listView.toolMode = .CTextToolMode
  578. if subToolMode == .Redact {
  579. //密文
  580. listView.annotationType = .redact
  581. listView.toolMode = .CRedactToolMode
  582. self.enterRedactAlert()
  583. } else if subToolMode == .Digital_Sign {
  584. //数字签名
  585. listView.annotationType = .digitalSign
  586. listView.toolMode = .CDigitalSignToolMode
  587. self.enterDigitalSignature()
  588. }
  589. } else if toolbarMode == .Tools {
  590. //MARK: -工具
  591. // if subToolMode == .Tool_OCR {
  592. // listView.toolMode = .COCRToolMode
  593. // } else {
  594. // listView.toolMode = .CTextToolMode
  595. // }
  596. }
  597. if (toolbarMode != .Edit) {
  598. self.closeOCREditAlert()
  599. }
  600. }
  601. func showOrHideNotes() {
  602. self.listView.hideNotes = !self.listView.hideNotes
  603. let items = [toolbarManager.highlightProperty, toolbarManager.UnderlineProperty,
  604. toolbarManager.wavelineProperty, toolbarManager.strikethroughProperty,
  605. toolbarManager.textProperty, toolbarManager.noteProperty,
  606. toolbarManager.penProperty, toolbarManager.eraserProperty,
  607. toolbarManager.rectangleProperty, toolbarManager.circleProperty,
  608. toolbarManager.arrowProperty, toolbarManager.lineProperty,
  609. toolbarManager.measureProperty, toolbarManager.stampProperty,
  610. toolbarManager.signProperty]
  611. for item in items {
  612. item.isDisabled = listView.hideNotes
  613. }
  614. pdfToolbarController?.resetSecondToolbar(forceRefresh: true)
  615. }
  616. //MARK: - SplitView
  617. func setUpSplitView() {
  618. infoContendSplitView.wantsLayer = true
  619. infoContendSplitView.layer?.backgroundColor = NSColor.clear.cgColor
  620. infoContendSplitView.delegate = self
  621. infoContendSplitView.layer?.masksToBounds = true
  622. }
  623. func setupSplitPDFController() {
  624. if splitPDFController == nil {
  625. splitPDFController = KMSplitPDFViewController.init()
  626. }
  627. splitPDFController?.view.frame = pdfSplitBottomView.bounds
  628. splitPDFController?.view.autoresizingMask = [.height, .width]
  629. splitPDFController?.viewManager = self.viewManager
  630. splitPDFController?.delegate = self
  631. splitPDFController?.pdfView?.pdfListViewDelegate = self
  632. splitPDFController?.reloadData()
  633. pdfSplitBottomView.addSubview(splitPDFController!.view)
  634. }
  635. //MARK: - 工具栏
  636. func initToolbar() {
  637. toolbarBox.borderWidth = 0
  638. if pdfToolbarController == nil {
  639. pdfToolbarController = KMPDFToolbarController.init()
  640. }
  641. pdfToolbarController?.view.frame = toolbarBox.bounds
  642. pdfToolbarController?.view.autoresizingMask = [.width, .height]
  643. pdfToolbarController?.delegate = self
  644. toolbarBox.contentView = pdfToolbarController?.view
  645. pdfToolbarController?.viewManager = viewManager
  646. pdfToolbarController?.toolbarManager = toolbarManager
  647. pdfToolbarController?.pdfView = listView
  648. pdfToolbarController?.setUpData()
  649. refreshToolbarViewHeightInfo()
  650. if listView.showFormFieldName {
  651. toolbarManager.form_ShowName_Property.righticon = NSImage(named: "tick_Green")
  652. } else {
  653. toolbarManager.form_ShowName_Property.righticon = nil
  654. }
  655. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15) {
  656. self.pdfToolbarController?.clickWithIdentify(KMPDFToolbar_Markup_Identifier)
  657. }
  658. }
  659. func refreshToolbarViewHeightInfo() {
  660. let _viewManager = viewManager
  661. if viewManager.isPageEditMode {
  662. toolbarBoxHeightConst.constant = 80
  663. } else if viewManager.editType == .watermark ||
  664. viewManager.editType == .background ||
  665. viewManager.editType == .header_Footer ||
  666. viewManager.editType == .bates {
  667. toolbarBoxHeightConst.constant = 40
  668. } else if _viewManager.toolMode == .Markup ||
  669. _viewManager.toolMode == .Edit ||
  670. _viewManager.toolMode == .Form ||
  671. _viewManager.toolMode == .Fill ||
  672. _viewManager.toolMode == .Convert ||
  673. _viewManager.toolMode == .Protect ||
  674. _viewManager.toolMode == .Tools {
  675. toolbarBoxHeightConst.constant = 80
  676. if _viewManager.subToolMode == .Redact {
  677. toolbarBoxHeightConst.constant = 40
  678. }
  679. } else {
  680. toolbarBoxHeightConst.constant = 40
  681. }
  682. }
  683. func toolbarViewModeChanged() {
  684. updatePDFViewAnnotationMode()
  685. refreshToolbarRightViewInfo()
  686. if viewManager.toolMode != .Edit && viewManager.subToolMode != .Edit_Crop {
  687. removeCropController()
  688. }
  689. }
  690. func updatePDFViewToolsType(_ viewToolsType: KMPDFViewToolsType) {
  691. viewManager.viewToolsType = viewToolsType
  692. pdfToolbarController?.reloadToolsView()
  693. if let toolbarVC = pdfToolbarController {
  694. kmPDFToolbarControllerDidViewToolsChanged(toolbarVC)
  695. }
  696. }
  697. //MARK: - 右边属性栏
  698. func refreshToolbarRightViewInfo() {
  699. //统一刷新viewManager.showRightSide, 由这个参数控制右边属性边框的展开与隐藏
  700. if viewManager.showRightSide == true {
  701. toolbarManager.rightViewProperty.state = .pressed
  702. toggleOpenRightSide()
  703. rightSideController?.reloadDataWithPDFView(pdfView: listView)
  704. } else {
  705. toggleCloseRightSide()
  706. }
  707. if viewManager.showRightSide == true {
  708. toolbarManager.rightViewProperty.state = .pressed
  709. } else {
  710. toolbarManager.rightViewProperty.state = .normal
  711. }
  712. pdfToolbarController?.reloadRightToolsView()
  713. }
  714. //MARK: - 侧边工具栏
  715. func initSideBar() {
  716. sidebarBox.borderWidth = 0
  717. if sideBarController == nil {
  718. sideBarController = KMPDFSideBarController.init()
  719. }
  720. sideBarController?.view.frame = sidebarBox.bounds
  721. sideBarController?.view.autoresizingMask = [.width, .height]
  722. sidebarBox.contentView = sideBarController?.view
  723. sideBarController?.pdfView = listView
  724. sideBarController?.delegate = self
  725. sideBarController?.pdfViewManager = viewManager
  726. sideBarController?.reloadData()
  727. }
  728. func reloadSideBar() {
  729. sideBarController?.reloadData()
  730. if viewManager.editType == .none {
  731. sideBarController?.searchItem.isHidden = false
  732. sideBarController?.thumbnailItem.isHidden = false
  733. sideBarController?.outlineItem.isHidden = false
  734. sideBarController?.bookmarkItem.isHidden = false
  735. sideBarController?.annotationItem.isHidden = false
  736. sideBarController?.aiToolItem.isHidden = false
  737. } else {
  738. sideBarController?.searchItem.isHidden = true
  739. sideBarController?.thumbnailItem.isHidden = true
  740. sideBarController?.outlineItem.isHidden = true
  741. sideBarController?.bookmarkItem.isHidden = true
  742. sideBarController?.annotationItem.isHidden = true
  743. sideBarController?.aiToolItem.isHidden = true
  744. }
  745. }
  746. //MARK: - 左边侧边栏
  747. func initLeftSideController() {
  748. if botaViewController == nil {
  749. botaViewController = KMNLeftSideViewController(listView.document)
  750. }
  751. botaViewController?.leftSideViewDelegate = self
  752. botaViewController?.view.frame = infoSplitLeftView.bounds
  753. botaViewController?.view.autoresizingMask = [.width, .height]
  754. if botaViewController != nil {
  755. infoSplitLeftView?.addSubview(botaViewController!.view)
  756. }
  757. }
  758. private func leftSidePaneIsOpen() -> Bool {
  759. return !infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) //第一次点击时存在问题,待解决
  760. }
  761. func toggleOpenLeftSide(pdfSideBarType: KMPDFSidebarType) {
  762. if(leftSidePaneIsOpen() == false) {
  763. let leftWidthNumber = UserDefaults.standard.object(forKey: CPDFViewLeftSidePaneWidthKey) as? NSNumber ?? MIN_SIDE_PANE_WIDTH
  764. infoContendSplitView.setPosition(MIN_SIDE_PANE_WIDTH.doubleValue, ofDividerAt: 0) //暂时无法记录上一次打开的宽度
  765. }
  766. if pdfSideBarType == .search {
  767. botaViewController?.searchViewC.handdler.pdfView = listView
  768. botaViewController?.leftsideType = .search
  769. } else if pdfSideBarType == .thumbnail {
  770. botaViewController?.leftsideType = pdfSideBarType
  771. } else if pdfSideBarType == .outline {
  772. botaViewController?.outlineViewC.handdler.pdfView = listView
  773. botaViewController?.leftsideType = pdfSideBarType
  774. } else if pdfSideBarType == .bookmark {
  775. botaViewController?.bookmarkViewC.handdler.pdfView = listView
  776. botaViewController?.leftsideType = pdfSideBarType
  777. } else if pdfSideBarType == .annotation {
  778. botaViewController?.annoController.listView = listView
  779. botaViewController?.leftsideType = .annotation
  780. } else if pdfSideBarType == .aiTools {
  781. botaViewController?.leftsideType = .aiTools
  782. }
  783. }
  784. func toggleCloseLeftSide() {
  785. if(leftSidePaneIsOpen() == true) {
  786. infoContendSplitView.setPosition(0, ofDividerAt: 0)
  787. }
  788. }
  789. //MARK: - 右侧属性栏
  790. func initRightSideController() {
  791. if rightSideController == nil {
  792. rightSideController = KMRightSideController.init()
  793. rightSideController?.delegate = self
  794. }
  795. rightSideController?.view.frame = CGRectMake(0, 0, MIN_SIDE_PANE_WIDTH.doubleValue, 680)
  796. rightSideController?.view.autoresizingMask = [.height, .maxXMargin]
  797. }
  798. func removeRightSideController() {
  799. rightSideController?.view.removeFromSuperview()
  800. rightSideController = nil
  801. }
  802. @objc func toggleOpenRightSide() -> Void {
  803. if rightSideController != nil {
  804. return
  805. }
  806. initRightSideController()
  807. rightSideController?.view.frame = infoSplitRightView.bounds
  808. rightSideController?.view.autoresizingMask = [.width, .height]
  809. infoSplitRightView.addSubview(rightSideController!.view)
  810. infoContendSplitView.setPosition(CGRectGetWidth(view.frame)-MIN_SIDE_PANE_WIDTH.doubleValue, ofDividerAt: 1)
  811. rightSideController?.viewManager = self.viewManager
  812. rightSideController?.reloadDataWithPDFView(pdfView: listView)
  813. }
  814. @objc func toggleCloseRightSide() -> Void {
  815. removeRightSideController()
  816. infoContendSplitView.setPosition(CGRectGetWidth(view.frame), ofDividerAt: 1)
  817. viewManager.showRightSide = false
  818. pdfToolbarController?.reloadRightToolsView()
  819. }
  820. func refreshRightSide() -> Void {
  821. if let rightVC = rightSideController, let _ = rightSideController?.view.superview {
  822. rightVC.reloadDataWithPDFView(pdfView: listView)
  823. }
  824. }
  825. //MARK: - PDFDisplayView
  826. func updatePDFDisplaySettingView() {
  827. if viewManager.showDisplayView {
  828. infoSplitViewLeftConst.constant = 0
  829. infoContendSplitView.setPosition(MIN_SIDE_PANE_WIDTH.doubleValue, ofDividerAt: 0)
  830. } else {
  831. if viewManager.isPDFReadMode == false {
  832. infoSplitViewLeftConst.constant = 44
  833. if viewManager.pdfSideBarType == .none {
  834. toggleCloseLeftSide()
  835. } else {
  836. toggleOpenLeftSide(pdfSideBarType: viewManager.pdfSideBarType)
  837. }
  838. } else {
  839. toggleCloseLeftSide()
  840. }
  841. }
  842. if viewManager.showDisplayView {
  843. if displaySettingController == nil {
  844. displaySettingController = KMNDisplayViewController.init()
  845. }
  846. displaySettingController?.view.frame = CGRectMake(0, 0, MIN_SIDE_PANE_WIDTH.doubleValue, CGRectGetHeight(bottomContendBox.frame))
  847. displaySettingController?.view.autoresizingMask = [.height, .width]
  848. infoSplitLeftView.addSubview(displaySettingController!.view)
  849. displaySettingController?.pdfView = self.listView
  850. displaySettingController?.viewManager = self.viewManager
  851. displaySettingController?.delegate = self
  852. displaySettingController?.reloadData()
  853. } else {
  854. displaySettingController?.view.removeFromSuperview()
  855. displaySettingController = nil
  856. }
  857. }
  858. //MARK: - 页面编辑
  859. func enterPageEditMode() {
  860. pageEditViewController = KMNPageEditViewController(self.document)
  861. if(pageEditViewController != nil) {
  862. bottomContendBox.addSubview(pageEditViewController!.view)
  863. pageEditViewController?.view.frame = bottomContendBox.bounds
  864. pageEditViewController?.thumbnailBaseViewDelegate = self
  865. pageEditViewController?.selectionIndexPaths = [IndexPath(item: listView.currentPageIndex, section: 0)]
  866. pageEditViewController?.view.autoresizingMask = [.width,.height]
  867. pageEditViewController?.currentUndoManager = listView.undoManager
  868. toolbarManager.page_pageInfo_Property.text = String(listView.currentPageIndex + 1)
  869. pdfToolbarController?.refreshSecondToolbarItemsState()
  870. listView.isHidden = true
  871. }
  872. }
  873. func exitPageEditMode() {
  874. if pageEditViewController != nil {
  875. pageEditViewController?.view.removeFromSuperview()
  876. pageEditViewController = nil
  877. }
  878. listView.isHidden = false
  879. if listView.document?.isModified() == true {
  880. listView.layoutDocumentView()
  881. botaViewController?.reloadData()
  882. }
  883. }
  884. //MARK: - 阅读模式
  885. func openPDFReadMode() {
  886. if viewManager.showDisplayView {
  887. UserDefaults.setDefaultBoolValue(true, toKey: "ShowDisplayViewWhenExitPDFReadMode")
  888. viewManager.showDisplayView = false
  889. pdfToolbarController?.reloadLeftIconView()
  890. updatePDFDisplaySettingView()
  891. }
  892. if infoSplitViewLeftConst.constant != 0 {
  893. infoSplitViewLeftConst.constant = 0
  894. updatePDFDisplaySettingView()
  895. }
  896. if viewManager.pdfSideBarType != .none {
  897. UserDefaults.setDefaultIntValue(viewManager.pdfSideBarType.rawValue, toKey: "viewManagerPdfSideBarTypeRawValue")
  898. viewManager.pdfSideBarType = .none
  899. }
  900. toolbarBoxHeightConst.constant = 0
  901. view.window?.makeFirstResponder(listView)
  902. let readModeMessage: ComponentMessage = ComponentMessage()
  903. readModeMessage.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString("Read Mode On"))
  904. readModeMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - readModeMessage.properties.propertyInfo.viewWidth)/2,
  905. CGRectGetHeight(self.view.frame) - readModeMessage.properties.propertyInfo.viewHeight - 8,
  906. readModeMessage.properties.propertyInfo.viewWidth,
  907. readModeMessage.properties.propertyInfo.viewHeight)
  908. readModeMessage.reloadData()
  909. readModeMessage.show(inView: self.view, autoHideSeconde: 2)
  910. setUpPDFPageNumberToolbar()
  911. }
  912. func exitPDFReadMode() {
  913. viewManager.isPDFReadMode = false
  914. if UserDefaults.getDefaultBoolValue(forKey: "ShowDisplayViewWhenExitPDFReadMode") == true {
  915. if viewManager.showDisplayView == false {
  916. viewManager.showDisplayView = true
  917. pdfToolbarController?.reloadLeftIconView()
  918. }
  919. UserDefaults.setDefaultBoolValue(false, toKey: "ShowDisplayViewWhenExitPDFReadMode")
  920. }
  921. if let index = UserDefaults.getDefaultIntValue(forKey: "viewManagerPdfSideBarTypeRawValue") {
  922. let type = KMPDFSidebarType(rawValue: index) ?? .none
  923. viewManager.pdfSideBarType = type
  924. UserDefaults.standard.removeObject(forKey: "viewManagerPdfSideBarTypeRawValue")
  925. UserDefaults.standard.synchronize()
  926. }
  927. updatePDFDisplaySettingView()
  928. refreshToolbarViewHeightInfo()
  929. reloadPDFPageNumberToolbar()
  930. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
  931. let readModeMessage: ComponentMessage = ComponentMessage()
  932. readModeMessage.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString("Read Mode Off"))
  933. readModeMessage.frame = CGRectMake((CGRectGetWidth(self.infoSplitCenterView.frame) - readModeMessage.properties.propertyInfo.viewWidth)/2,
  934. CGRectGetHeight(self.infoSplitCenterView.frame) - readModeMessage.properties.propertyInfo.viewHeight - 8,
  935. readModeMessage.properties.propertyInfo.viewWidth,
  936. readModeMessage.properties.propertyInfo.viewHeight)
  937. readModeMessage.reloadData()
  938. readModeMessage.show(inView: self.infoSplitCenterView, autoHideSeconde: 2)
  939. }
  940. }
  941. //MARK: - PPT
  942. func togglePresentation(_ sender: Any?) {
  943. if self.canExitPresentation() {
  944. exitFullScreen()
  945. } else if self.canEnterPresentation() {
  946. NotificationCenter.default.addObserver(self, selector: #selector(willEnterInteractionModeNotification), name: NSWindow.willEnterInteractionModeNotification, object: nil)
  947. NotificationCenter.default.addObserver(self, selector: #selector(didEnterInteractionModeNotification), name: NSWindow.didEnterInteractionModeNotification, object: nil)
  948. NotificationCenter.default.addObserver(self, selector: #selector(willShowFullScreenNotification), name: NSWindow.willShowFullScreenNotification, object: nil)
  949. NotificationCenter.default.addObserver(self, selector: #selector(didShowFullScreenNotification), name: NSWindow.didShowFullScreenNotification, object: nil)
  950. view.window?.enterPresentation(provider: self)
  951. }
  952. }
  953. func enterPresentationMode() {
  954. let scrollView = listView.documentView().enclosingScrollView
  955. savedNormalSetup.setValue(scrollView?.hasHorizontalScroller, forKey: KMMainModel.Key.kHasHorizontalScroller)
  956. savedNormalSetup.setValue(scrollView?.hasVerticalScroller, forKey: KMMainModel.Key.kHasVerticalsCroller)
  957. savedNormalSetup.setValue(scrollView?.autohidesScrollers, forKey: KMMainModel.Key.kAutoHidesScrollers)
  958. listView.backgroundColor = NSColor.clear
  959. listView.setDisplay(.singlePage)
  960. listView.autoScales = true
  961. listView.displayBox = .cropBox
  962. listView.displaysPageBreaks = false
  963. scrollView?.autohidesScrollers = true
  964. scrollView?.hasHorizontalScroller = false
  965. scrollView?.hasVerticalScroller = false
  966. listView.setCurrentSelection(nil, animate: true)
  967. }
  968. func exitPresentationMode() {
  969. NotificationCenter.default.removeObserver(self, name: NSWindow.willEnterInteractionModeNotification, object: nil)
  970. NotificationCenter.default.removeObserver(self, name: NSWindow.didEnterInteractionModeNotification, object: nil)
  971. NotificationCenter.default.removeObserver(self, name: NSWindow.willShowFullScreenNotification, object: nil)
  972. NotificationCenter.default.removeObserver(self, name: NSWindow.willShowFullScreenNotification, object: nil)
  973. }
  974. func exitFullScreen() {
  975. if self.canExitPresentation() == true {
  976. let mainDocument = self.myDocument as? KMMainDocument
  977. let browserWindowController = mainDocument?.browser?.windowController as? KMBrowserWindowController
  978. browserWindowController?.exitFullscreen()
  979. }
  980. }
  981. func exitFullscreenMode() {
  982. if self.interactionMode == .presentation {
  983. self.exitPresentationMode()
  984. }
  985. self.applyPDFSettings(self.savedNormalSetup)
  986. self.savedNormalSetup.removeAllObjects()
  987. listView.layoutDocumentView()
  988. listView.requiresDisplay()
  989. if let backgroundColor = UserDefaults.standard.color(forKey: KMBackgroundColorKey) {
  990. listView.backgroundColor = backgroundColor
  991. }
  992. reloadPDFSplitInfo()
  993. }
  994. func applyPDFSettings(_ setup: NSDictionary) {
  995. if let data = setup.object(forKey: KMMainModel.Key.kAutoScales) as? NSNumber {
  996. self.listView.autoScales = data.boolValue
  997. }
  998. if self.listView.autoScales == false {
  999. if let data = setup.object(forKey: KMMainModel.Key.kScaleFactor) as? NSNumber {
  1000. self.listView.scaleFactor = data.floatValue.cgFloat
  1001. }
  1002. }
  1003. if let data = setup.object(forKey: KMMainModel.Key.kDisplayMode) as? NSNumber {
  1004. self.listView.setDisplay(CPDFDisplayViewMode(rawValue: data.intValue) ?? .singlePage)
  1005. }
  1006. if let data = setup.object(forKey: KMMainModel.Key.kDisplaysAsBook) as? NSNumber {
  1007. self.listView.displaysAsBook = data.boolValue
  1008. }
  1009. if let data = setup.object(forKey: KMMainModel.Key.kDisplaysPageBreaks) as? NSNumber {
  1010. self.listView.displaysPageBreaks = data.boolValue
  1011. }
  1012. if let data = setup.object(forKey: KMMainModel.Key.kDisplayBox) as? NSNumber {
  1013. }
  1014. self.listView.layoutDocumentView()
  1015. }
  1016. func currentPDFSettings() -> NSDictionary {
  1017. let setup = NSMutableDictionary()
  1018. setup[KMMainModel.Key.kDisplaysPageBreaks] = NSNumber(value: listView.displaysPageBreaks)
  1019. setup[KMMainModel.Key.kDisplaysAsBook] = NSNumber(value: listView.displaysAsBook)
  1020. setup[KMMainModel.Key.kDisplayBox] = NSNumber(value: listView.displayBox.rawValue)
  1021. setup[KMMainModel.Key.kScaleFactor] = NSNumber(value: listView.scaleFactor)
  1022. setup[KMMainModel.Key.kAutoScales] = NSNumber(value: listView.autoScales)
  1023. setup[KMMainModel.Key.kDisplayMode] = NSNumber(value: listView.fetchDisplayViewMode().rawValue)
  1024. return setup
  1025. }
  1026. func canEnterFullscreen() -> Bool {
  1027. if (mwcFlags.isSwitchingFullScreen != 0) {
  1028. return false
  1029. }
  1030. if useNativeFullScreen() {
  1031. return interactionMode == .normal || interactionMode == .presentation
  1032. } else {
  1033. return !self.listView.document.isLocked && (interactionMode == .normal || interactionMode == .presentation) && self.view.window?.tabbedWindows?.count ?? 0 < 2
  1034. }
  1035. }
  1036. override func canEnterPresentation() -> Bool {
  1037. let can = super.canEnterPresentation()
  1038. if can == false {
  1039. return false
  1040. }
  1041. guard let doc = self.listView.document, doc.isLocked == false else {
  1042. return false
  1043. }
  1044. return can
  1045. }
  1046. func fadeOutFullScreenWindow() {
  1047. guard let fullScreenWindow = self.view.window as? KMFullScreenWindow else {
  1048. NSSound.beep()
  1049. return
  1050. }
  1051. let mainWindow = fullScreenWindow.interactionParent
  1052. let collectionBehavior = mainWindow?.collectionBehavior
  1053. mainWindow?.alphaValue = 0
  1054. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  1055. mainWindow?.animationBehavior = .none
  1056. }
  1057. // trick to make sure the main window shows up in the same space as the fullscreen window
  1058. fullScreenWindow.addChildWindow(mainWindow!, ordered: .below)
  1059. fullScreenWindow.removeChildWindow(mainWindow!)
  1060. fullScreenWindow.level = .popUpMenu
  1061. // these can change due to the child window trick
  1062. mainWindow?.level = .normal
  1063. mainWindow?.alphaValue = 1.0
  1064. mainWindow?.collectionBehavior = collectionBehavior!
  1065. mainWindow?.display()
  1066. mainWindow?.makeFirstResponder(self.listView)
  1067. mainWindow?.recalculateKeyViewLoop()
  1068. // mainWindow?.delegate = self
  1069. mainWindow?.makeKey()
  1070. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  1071. mainWindow?.animationBehavior = .default
  1072. }
  1073. NSApp.removeWindowsItem(fullScreenWindow)
  1074. fullScreenWindow.fadeOut()
  1075. }
  1076. //MARK: - PDF分屏视图
  1077. func reloadPDFSplitInfo() {
  1078. if listView.viewSplitMode == .disable {
  1079. pdfSplitView.isHidden = true
  1080. listView.frame = infoSplitCenterSubView.bounds
  1081. infoSplitCenterSubView.addSubview(listView)
  1082. if splitPDFController != nil {
  1083. splitPDFController = nil
  1084. }
  1085. } else {
  1086. pdfSplitView.isHidden = false
  1087. listView.frame = pdfSplitTopView.bounds
  1088. pdfSplitTopView.addSubview(listView)
  1089. setUpPDFPageNumberToolbar()
  1090. setupSplitPDFController()
  1091. if listView.viewSplitMode == .horizontal {
  1092. pdfSplitView.isVertical = false
  1093. } else if listView.viewSplitMode == .vertical {
  1094. pdfSplitView.isVertical = true
  1095. }
  1096. }
  1097. }
  1098. func setUpPDFPageNumberToolbar() {
  1099. if pageNumberToolbar != nil {
  1100. pageNumberToolbar?.removeFromSuperview()
  1101. pageNumberToolbar = nil
  1102. }
  1103. if pageNumberToolbar == nil {
  1104. pageNumberToolbar = KMPageNumberPromptView.init()
  1105. }
  1106. pageNumberToolbar?.frame = CGRectMake(CGRectGetWidth(listView.frame)/2-144, 20, 288, 40)
  1107. pageNumberToolbar?.autoresizingMask = [.minXMargin, .maxXMargin, .maxYMargin]
  1108. pageNumberToolbar?.pdfView = self.listView
  1109. pageNumberToolbar?.reloadData()
  1110. pageNumberToolbar?.isHidden = true
  1111. listView.addSubview(pageNumberToolbar!)
  1112. reloadPDFPageNumberToolbar()
  1113. }
  1114. func reloadPDFPageNumberToolbar() {
  1115. if viewManager.isPDFReadMode == true ||
  1116. (viewManager.splitShowBottomBar && listView.viewSplitMode != .disable) {
  1117. pageNumberToolbar?.isHidden = false
  1118. pageNumberToolbar?.reloadData()
  1119. } else {
  1120. pageNumberToolbar?.isHidden = true
  1121. }
  1122. }
  1123. //MARK: - Edit模式
  1124. func showEditToolbarView() {
  1125. if editToolbarView == nil {
  1126. editToolbarView = KMEditToolbarView()
  1127. }
  1128. editToolbarView?.frame = toolbarBox.bounds
  1129. editToolbarView?.delegate = self
  1130. editToolbarView?.autoresizingMask = [.width, .height]
  1131. toolbarBox.contentView = editToolbarView
  1132. reloadSideBar()
  1133. }
  1134. func exitEditToolbarView() {
  1135. viewManager.editType = .none
  1136. viewManager.subToolMode = .None
  1137. editToolbarView?.removeFromSuperview()
  1138. editToolbarView = nil
  1139. watermarkViewController?.view.removeFromSuperview()
  1140. watermarkViewController = nil
  1141. backgroundViewController?.view.removeFromSuperview()
  1142. backgroundViewController = nil
  1143. headerFooterViewController?.view.removeFromSuperview()
  1144. headerFooterViewController = nil
  1145. batesViewController?.view.removeFromSuperview()
  1146. batesViewController = nil
  1147. refreshToolbarViewHeightInfo()
  1148. toolbarBox.contentView = pdfToolbarController?.view
  1149. updatePDFViewAnnotationMode()
  1150. reloadSideBar()
  1151. }
  1152. func updateEditModeDocumentWhenPageChanged() {
  1153. if viewManager.editType == .watermark {
  1154. updateWatermarkDocument()
  1155. } else if viewManager.editType == .background {
  1156. updateBackgroundDocument()
  1157. } else if viewManager.editType == .header_Footer {
  1158. updateHeaderFooterDocument()
  1159. } else if viewManager.editType == .bates {
  1160. updateBatesDocument()
  1161. } else if viewManager.subToolMode == .Edit_Crop {
  1162. updateCropDocument()
  1163. }
  1164. }
  1165. //MARK: - 数字签名
  1166. func writeSignatureToWidget(_ widget: CPDFSignatureWidgetAnnotation, _ path: String, _ password: String, _ config: CPDFSignatureConfig, _ isLock: Bool) ->() {
  1167. let fileName = listView.document.documentURL?.lastPathComponent
  1168. let fileNameWithoutExtension = URL(fileURLWithPath: fileName!).deletingPathExtension().lastPathComponent
  1169. let outputSavePanel = NSSavePanel()
  1170. outputSavePanel.directoryURL = listView.document.documentURL.deletingLastPathComponent()
  1171. outputSavePanel.title = NSLocalizedString("", comment: "Save as PDF")
  1172. outputSavePanel.allowedFileTypes = ["pdf"]
  1173. outputSavePanel.nameFieldStringValue = fileNameWithoutExtension + "_" + NSLocalizedString("Signed", comment: "")
  1174. let result = outputSavePanel.runModal()
  1175. if result == .OK {
  1176. let contentArr = NSMutableArray()
  1177. var locationStr = ""
  1178. var reasonStr = NSLocalizedString("none", comment: "")
  1179. for item in config.contents {
  1180. if item.key == NSLocalizedString("Reason", comment: "") {
  1181. if item.value == NSLocalizedString("<your signing reason here>", comment: "") {
  1182. item.value = " " + NSLocalizedString("none", comment: "")
  1183. }
  1184. reasonStr = item.value
  1185. } else if item.key == NSLocalizedString("Location", comment: "") {
  1186. if item.value == NSLocalizedString("<your signing location here>", comment: "") {
  1187. item.value = " "
  1188. }
  1189. locationStr = item.value
  1190. }
  1191. contentArr.add(item)
  1192. }
  1193. config.contents = contentArr as? [CPDFSignatureConfigItem]
  1194. widget.setFieldName(widget.getValidName(inPage: widget.page))
  1195. widget.signAppearanceConfig(config)
  1196. let success = listView.document.writeSignature(to: outputSavePanel.url, withWidget: widget, pkcs12Cert: path, password: password, location: locationStr, reason: reasonStr, permissions: .forbidChange)
  1197. widget.removeSignature()
  1198. if success {
  1199. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  1200. NSDocumentController.shared.openDocument(withContentsOf: outputSavePanel.url!, display: true) { document, documentWasAlreadyOpen, error in
  1201. if error != nil {
  1202. NSApp.presentError(error!)
  1203. return
  1204. }
  1205. }
  1206. }
  1207. } else {
  1208. let alert = NSAlert.init()
  1209. alert.messageText = NSLocalizedString("Save failed!", comment: "")
  1210. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1211. alert.runModal()
  1212. }
  1213. widget.page.removeAnnotation(widget)
  1214. listView.setNeedsDisplayAnnotationViewFor(widget.page)
  1215. } else {
  1216. widget.page.removeAnnotation(widget)
  1217. listView.setNeedsDisplayAnnotationViewFor(widget.page)
  1218. }
  1219. }
  1220. func popUpSignatureWidgetState(_ signature: CPDFSignature, _ pdfListView: CPDFListView) ->(){
  1221. signaturestateVC.signature = signature
  1222. signaturestateVC.pdfListView = pdfListView
  1223. signaturestateVC.actionBlock = { [weak self, weak signaturestateVC] stateVCSelf, actionType in
  1224. guard let weakSelf = self, let stateVC = signaturestateVC else { return }
  1225. if actionType == .cancel {
  1226. stateVC.dismiss(stateVCSelf)
  1227. } else if actionType == .confirm {
  1228. if let signer = signature.signers.first, let data = signer.certificates {
  1229. let signatureDetail = DSignatureDetailsViewController.init()
  1230. signatureDetail.certificates = data
  1231. signatureDetail.signature = signature
  1232. signatureDetail.pdfListView = pdfListView
  1233. stateVCSelf.presentAsSheet(signatureDetail)
  1234. } else {
  1235. NSSound.beep()
  1236. }
  1237. }
  1238. }
  1239. self.presentAsSheet(signaturestateVC)
  1240. signaturestateVC.reloadData()
  1241. }
  1242. func enterDigitalSignature() {
  1243. if UserDefaults.standard.object(forKey: "kDigitalSignature") != nil {
  1244. return
  1245. }
  1246. let alert = NSAlert()
  1247. alert.alertStyle = .informational
  1248. alert.messageText = KMLocalizedString("Using your mouse, click and drag to draw the area where you would like the signature to appear. Once you finish dragging out the desired area, you will be taken to the next step of the signing process. ")
  1249. alert.addButton(withTitle: KMLocalizedString("OK"))
  1250. alert.showsSuppressionButton = true
  1251. let response = alert.runModal()
  1252. if response.rawValue == 1000 {
  1253. if alert.suppressionButton?.state == .on {
  1254. UserDefaults.standard.set("YES", forKey: "kDigitalSignature")
  1255. UserDefaults.standard.synchronize()
  1256. }
  1257. }
  1258. }
  1259. // MARK: - 显示合并窗口
  1260. public func showMergeWindow(url: URL? = nil, _ password: String?) {
  1261. DispatchQueue.main.async {
  1262. var documentURL = url
  1263. if documentURL == nil {
  1264. documentURL = self.listView.document?.documentURL
  1265. }
  1266. guard let _url = documentURL else { return }
  1267. guard let document = PDFDocument(url: _url) else { return }
  1268. self.mergeWindowController = KMMergeWindowController(document: document, password: password ?? "")
  1269. self.mergeWindowController!.password = self.listView.document.password ?? ""
  1270. self.mergeWindowController!.oriDucumentUrl = self.listView.document?.documentURL
  1271. self.mergeWindowController!.pageIndex = self.listView.currentPageIndex
  1272. self.mergeWindowController!.cancelAction = { [unowned self] controller in
  1273. self.view.window?.endSheet(mergeWindowController!.window!)
  1274. }
  1275. self.mergeWindowController!.mergeAction = { [unowned self] controller, filePath in
  1276. self.view.window?.endSheet(mergeWindowController!.window!)
  1277. }
  1278. self.view.window?.beginSheet(self.mergeWindowController!.window!)
  1279. }
  1280. }
  1281. //MARK: - Crop裁剪
  1282. func showCropController() {
  1283. if cropController == nil {
  1284. cropController = KMCropController.init()
  1285. cropController?.view.frame = infoSplitCenterView.bounds
  1286. cropController?.view.autoresizingMask = [.width, .height]
  1287. cropController?.delegate = self
  1288. infoSplitCenterView.addSubview(cropController!.view)
  1289. if viewManager.showRightSide == false {
  1290. viewManager.showRightSide = true
  1291. refreshToolbarRightViewInfo()
  1292. }
  1293. updateCropDocument()
  1294. }
  1295. }
  1296. func updateCropDocument() {
  1297. guard let controller = cropController else { return }
  1298. controller.pdfDocument = nil
  1299. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1300. let editDocument = CPDFDocument.init()
  1301. editDocument?.insertPageObject(page, at: 0)
  1302. if let editPage = editDocument?.page(at: 0) {
  1303. let mediaRect = editPage.bounds(for: .mediaBox)
  1304. editPage.setBounds(mediaRect, for: .cropBox)
  1305. controller.selectionRect = page?.bounds(for: .cropBox) ?? .zero
  1306. if let cropVC = rightSideController?.edit_cropController {
  1307. cropVC.pageCropBounds = page?.bounds(for: .cropBox) ?? .zero
  1308. cropVC.reloadData()
  1309. }
  1310. }
  1311. controller.pdfDocument = editDocument
  1312. controller.reloadData()
  1313. }
  1314. func removeCropController() {
  1315. if cropController != nil {
  1316. cropController?.view.removeFromSuperview()
  1317. cropController = nil
  1318. toolbarManager.edit_crop_Property.state = .normal
  1319. }
  1320. }
  1321. // MARK: - Secure 【安全】
  1322. public func hiddenSecureLimitTip() {
  1323. }
  1324. func savePageNumberIfNeed() {
  1325. let scaleFactor = self.listView.scaleFactor
  1326. if scaleFactor <= 0 {
  1327. return
  1328. }
  1329. if self.listView.document != nil {
  1330. KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
  1331. KMPreferenceManager.shared.setPageScale(Float(scaleFactor), forKey: self.listView.document.documentURL.path)
  1332. }
  1333. }
  1334. // MARK: - 显示加密弹窗
  1335. public func showSecureWindow() {
  1336. guard let url = self.listView.document?.documentURL else {
  1337. return
  1338. }
  1339. self.securityWindowController = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
  1340. guard let securityWindowController = securityWindowController else { return }
  1341. securityWindowController.documentURL = self.listView.document?.documentURL
  1342. securityWindowController.batchAction = { [unowned self] controller, files in
  1343. self.view.window?.endSheet((securityWindowController.window)!)
  1344. let batchWindowController = KMBatchOperateWindowController.sharedWindowController
  1345. let batchOperateFile = KMBatchOperateFile(filePath: self.document?.documentURL.path ?? "", type: .AddPassword)
  1346. batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
  1347. batchWindowController.window?.makeKeyAndOrderFront("")
  1348. }
  1349. securityWindowController.doneAction = { [unowned self] controller, options, attribute in
  1350. let openPanel = NSOpenPanel()
  1351. openPanel.canChooseFiles = false
  1352. openPanel.canChooseDirectories = true
  1353. openPanel.canCreateDirectories = true
  1354. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { (result) in
  1355. if result == NSApplication.ModalResponse.OK {
  1356. for fileURL in openPanel.urls {
  1357. let document = CPDFDocument(url: self.document?.documentURL)
  1358. if document != nil {
  1359. document!.setDocumentAttributes(attribute)
  1360. let path = fileURL.path.stringByAppendingPathComponent(url.deletingPathExtension().lastPathComponent) + "_SetPassword" + "." + url.pathExtension
  1361. let success = document!.write(to: NSURL(fileURLWithPath: path) as URL, withOptions: options)
  1362. if success {
  1363. self.view.window?.endSheet((securityWindowController.window)!)
  1364. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  1365. }
  1366. }
  1367. }
  1368. }
  1369. }
  1370. }
  1371. securityWindowController.cancelAction = { [unowned self] controller in
  1372. self.view.window?.endSheet((securityWindowController.window)!)
  1373. }
  1374. NSWindow.currentWindow().beginSheet(securityWindowController.window!)
  1375. }
  1376. // MARK: - 移除文档密码
  1377. public func showRemoveSecureWindow() {
  1378. var isDocumentLocked: Bool = false
  1379. if self.document?.allowsCopying == false || self.document?.allowsPrinting == false {
  1380. isDocumentLocked = true
  1381. } else if (self.document?.password ?? "").count > 0 {
  1382. isDocumentLocked = true
  1383. }
  1384. if isDocumentLocked == false {
  1385. let alert = NSAlert()
  1386. alert.alertStyle = .warning
  1387. alert.messageText = KMLocalizedString("This document doesn’t contain any security settings and doesn‘t need to be removed.")
  1388. alert.addButton(withTitle: KMLocalizedString("OK"))
  1389. alert.beginSheetModal(for: NSWindow.currentWindow()) { returnCode in
  1390. }
  1391. } else {
  1392. let controller = KMRemovePasswordWindowController(windowNibName: "KMRemovePasswordWindowController")
  1393. controller.pdfDocument = self.document
  1394. self.currentWindowController = controller
  1395. controller.batchAction = { [unowned self] controller, files in
  1396. self.view.window?.endSheet((self.currentWindowController.window)!)
  1397. self.currentWindowController = nil
  1398. var array: [URL] = []
  1399. for item in files {
  1400. array.append(NSURL(fileURLWithPath: item.filePath) as URL)
  1401. }
  1402. self.showBatchWindow(type: .batchRemove, files: array)
  1403. }
  1404. controller.cancelAction = { [unowned self] controller in
  1405. self.view.window?.endSheet((self.currentWindowController.window)!)
  1406. self.currentWindowController = nil
  1407. }
  1408. controller.doneAction = { [unowned self] controller in
  1409. self.view.window?.endSheet((self.currentWindowController.window)!)
  1410. self.currentWindowController = nil
  1411. NSWindowController.checkPassword(url: self.document!.documentURL!, type: .owner, password: self.document?.password ?? "") { [unowned self] success, resultPassword in
  1412. if success {
  1413. let savePanel = NSSavePanel()
  1414. savePanel.nameFieldStringValue = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_RemovePassword"
  1415. savePanel.allowedFileTypes = ["pdf"]
  1416. savePanel.beginSheetModal(for: NSApp.mainWindow!) {[unowned self] result in
  1417. guard result == .OK else { return }
  1418. /// 删除安全性设置
  1419. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  1420. self.model.isSaveKeyChain = false
  1421. self.listView.document.unlock(withPassword: resultPassword)
  1422. }
  1423. let document = CPDFDocument.init(url: self.listView.document.documentURL)
  1424. guard let document = document else { return }
  1425. document.unlock(withPassword: resultPassword)
  1426. let success = document.writeDecrypt(to: savePanel.url)
  1427. if success {
  1428. self.hiddenSecureLimitTip()
  1429. NSWorkspace.shared.activateFileViewerSelecting([savePanel.url!])
  1430. } else {
  1431. self.hiddenSecureLimitTip()
  1432. }
  1433. }
  1434. }
  1435. }
  1436. }
  1437. NSWindow.currentWindow().beginSheet(controller.window!)
  1438. }
  1439. }
  1440. //MARK: - Unlock Document
  1441. func unlockPDFDocument() {
  1442. NSWindowController.checkPassword(url: self.document!.documentURL!, type: .owner, password: self.document?.password ?? "") { [unowned self] success, resultPassword in
  1443. self.listView.document.unlock(withPassword: resultPassword)
  1444. }
  1445. }
  1446. //MARK: - Watermark水印
  1447. func showWatermarkController() {
  1448. viewManager.editType = .watermark
  1449. showEditToolbarView()
  1450. editToolbarView?.editType = .watermark
  1451. if KMWatermarkManager.defaultManager.watermarks.count == 0 {
  1452. editToolbarView?.editSubType = .add
  1453. } else {
  1454. editToolbarView?.editSubType = .template
  1455. }
  1456. editToolbarView?.reloadData()
  1457. if watermarkViewController == nil {
  1458. watermarkViewController = KMWatermarkController.init()
  1459. }
  1460. watermarkViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1461. watermarkViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1462. watermarkViewController?.delegate = self
  1463. bottomContendBox.contentView?.addSubview(watermarkViewController!.view)
  1464. watermarkViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1465. watermarkViewController?.reloadData()
  1466. updateWatermarkDocument()
  1467. watermarkViewController?.loadData()
  1468. }
  1469. func updateWatermarkDocument() {
  1470. guard let controller = watermarkViewController else { return }
  1471. var editDocument = CPDFDocument.init()
  1472. if let vcDoc = controller.pdfDocument {
  1473. editDocument = vcDoc
  1474. }
  1475. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1476. editDocument?.insertPageObject(page, at: 0)
  1477. if editDocument?.pageCount == 2 {
  1478. let theIndex = IndexSet(integer: 1)
  1479. editDocument?.removePage(at: theIndex)
  1480. }
  1481. if watermarkViewController?.pdfDocument == nil {
  1482. watermarkViewController?.pdfDocument = editDocument
  1483. }
  1484. watermarkViewController?.resetUI()
  1485. watermarkViewController?.reloadData()
  1486. }
  1487. //移除文档水印
  1488. func removePDFWatermark() {
  1489. let watermarks = self.listView.document.watermarks()
  1490. if (watermarks == nil || watermarks!.count <= 0) {
  1491. let alert = NSAlert()
  1492. alert.alertStyle = .warning
  1493. 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: "")
  1494. alert.addButton(withTitle: NSLocalizedString("Confirm", comment: ""))
  1495. alert.runModal()
  1496. return
  1497. }
  1498. let alert = NSAlert()
  1499. alert.alertStyle = .warning
  1500. alert.messageText = NSLocalizedString("Are you sure you want to remove the watermark?", comment: "")
  1501. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  1502. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  1503. let result = alert.runModal()
  1504. if (result == .alertFirstButtonReturn) {
  1505. for watermark in watermarks! {
  1506. listView.document.removeWatermark(watermark)
  1507. }
  1508. listView.layoutDocumentView()
  1509. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Watermark removed"),
  1510. type: .success,
  1511. fromView: bottomContendBox,
  1512. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1513. }
  1514. }
  1515. func batchAddWatermark() {
  1516. self.showBatchWindow(type: .watermark, files: nil)
  1517. }
  1518. func batchRemoveWatermark() {
  1519. self.showBatchWindow(type: .batchRemove, files: nil)
  1520. }
  1521. //MARK: - PopUI
  1522. func reloadPopUIWindow() {
  1523. if listView.toolMode == .CSelectToolMode {
  1524. let pageRect = listView.currentSelectionRect()
  1525. let page:CPDFPage? = listView.currentSelectionPage()
  1526. if listView.selectionRect != CGRectZero && page != nil {
  1527. let positioningRect = listView.convert(pageRect, from: page)
  1528. if (CGRectIntersectsRect(positioningRect, listView.frame)) {
  1529. reloadPopUIOperation()
  1530. return
  1531. }
  1532. }
  1533. toggleClosePopUIWindow()
  1534. } else if listView.toolMode == .COCRToolMode {
  1535. let pageRect = listView.currentSelectionRect()
  1536. let page:CPDFPage? = listView.currentSelectionPage()
  1537. if listView.selectionRect != CGRectZero && page != nil {
  1538. let positioningRect = listView.convert(pageRect, from: page)
  1539. if (CGRectIntersectsRect(positioningRect, listView.frame)) {
  1540. reloadPopUIOperation()
  1541. return
  1542. }
  1543. }
  1544. toggleClosePopUIWindow()
  1545. } else if(listView.isEditing() == false) {
  1546. let activeAnnotations:[CPDFAnnotation] = listView.activeAnnotations as! [CPDFAnnotation]
  1547. if(activeAnnotations.count > 0) {
  1548. if let page = activeAnnotations.first?.page {
  1549. let pageRect = listView.selectionMultipleBounds(with: activeAnnotations)
  1550. let positioningRect = listView.convert(pageRect, from: page)
  1551. if (CGRectIntersectsRect(positioningRect, listView.frame)) {
  1552. reloadPopUIActiveAnnotations(activeAnnotations: activeAnnotations)
  1553. } else {
  1554. toggleClosePopUIWindow()
  1555. }
  1556. } else {
  1557. toggleClosePopUIWindow()
  1558. }
  1559. } else {
  1560. toggleClosePopUIWindow()
  1561. }
  1562. if(listView.popOver?.isShown == true || (groupListMenuGroup?.superview) != nil) { //右键菜单弹出时,或者Pop编辑框弹出时不显示Pop
  1563. toggleClosePopUIWindow()
  1564. }
  1565. } else {
  1566. let editAreas:[CPDFEditArea] = listView.km_EditingAreas()
  1567. if(editAreas.count > 0) {
  1568. if let page = editAreas.first?.page {
  1569. let pageRect = listView.selectionMultipleBounds(withEditArea: editAreas)
  1570. let positioningRect = listView.convert(pageRect, from: page)
  1571. if (CGRectIntersectsRect(positioningRect, listView.frame)) {
  1572. reloadPopUIContentEdits(editAreas: editAreas)
  1573. } else {
  1574. toggleClosePopUIWindow()
  1575. }
  1576. } else {
  1577. toggleClosePopUIWindow()
  1578. }
  1579. } else {
  1580. toggleClosePopUIWindow()
  1581. }
  1582. if(listView.popOver?.isShown == true || (groupListMenuGroup?.superview) != nil) { //右键菜单弹出时,或者Pop编辑框弹出时不显示Pop
  1583. toggleClosePopUIWindow()
  1584. }
  1585. }
  1586. }
  1587. func toggleClosePopUIWindow() {
  1588. let popWindow = KMNPopAnnotationWindowController.shared
  1589. if popWindow.window?.isVisible == true {
  1590. closeAnnotationPopWindow()
  1591. }
  1592. let editPopWindow = KMNPopContentEditWindowController.shared
  1593. if editPopWindow.window?.isVisible == true {
  1594. closePopContentEditWindow()
  1595. }
  1596. let operationWindow = KMNPopOperationWindowController.shared
  1597. if operationWindow.window?.isVisible == true {
  1598. closePopOperationWindow()
  1599. }
  1600. }
  1601. func closeAnnotationPopWindow() {
  1602. KMNPopAnnotationWindowController.shared.closeWindow(listView: listView)
  1603. }
  1604. func closePopContentEditWindow() {
  1605. KMNPopContentEditWindowController.shared.closeWindow(listView: listView)
  1606. }
  1607. func closePopOperationWindow() {
  1608. KMNPopOperationWindowController.shared.closeWindow(listView: listView)
  1609. }
  1610. func reloadPopUIOperation() {
  1611. if listView.selectionRect != CGRectZero {
  1612. let popWindow = KMNPopOperationWindowController.shared
  1613. popWindow.window?.makeKeyAndOrderFront(nil)
  1614. if popWindow.window?.isVisible == false {
  1615. listView.window?.addChildWindow(popWindow.window ?? NSWindow(), ordered: .above)
  1616. }
  1617. popWindow.listView = listView
  1618. if listView.toolMode == .CSelectToolMode {
  1619. popWindow.popType = .crop
  1620. popWindow.cropCurrentCallback = {[weak self] in
  1621. let rect = self?.listView.currentSelectionRect() ?? CGRect.zero
  1622. let orgPage : CPDFPage = self?.listView.currentSelectionPage() ?? CPDFPage()
  1623. self?.cropPages(atIndexs: [orgPage.pageIndex()], to: [rect])
  1624. self?.closePopOperationWindow()
  1625. }
  1626. } else if listView.toolMode == .COCRToolMode {
  1627. popWindow.popType = .ocr
  1628. popWindow.OCRAction = { [weak self] in
  1629. self?.convertSelectionRectOCR(rect: self?.listView.currentSelectionRect() ?? CGRectZero)
  1630. }
  1631. }
  1632. popWindow.updatePDFViewCallback = {[weak self] in
  1633. self?.closePopOperationWindow()
  1634. self?.listView.setNeedsDisplayForVisiblePages()
  1635. }
  1636. updatePopOperationPopWinodwFrame()
  1637. } else {
  1638. closePopOperationWindow()
  1639. }
  1640. }
  1641. func reloadPopUIActiveAnnotations(activeAnnotations:[CPDFAnnotation]) {
  1642. let annotationMode = KMNAnnotationPopMode(pdfAnnotations: activeAnnotations )
  1643. let popWindow = KMNPopAnnotationWindowController.shared
  1644. if annotationMode.popType == .popTypeNone || !SettingsManager.sharedInstance.showQuickActionBar {
  1645. closeAnnotationPopWindow()
  1646. } else {
  1647. popWindow.window?.makeKeyAndOrderFront(nil)
  1648. if popWindow.window?.isVisible == false {
  1649. listView.window?.addChildWindow(popWindow.window ?? NSWindow(), ordered: .above)
  1650. }
  1651. popWindow.listView = listView
  1652. popWindow.annotationPopMode = annotationMode
  1653. popWindow.isOpenPane = viewManager.showRightSide
  1654. updateAnnotationsPopWinodwFrame()
  1655. popWindow.updatePDFViewCallback = {[weak self] in
  1656. self?.rightSideController?.reloadDataWithPDFView(pdfView: self?.listView ?? CPDFListView())
  1657. self?.listView.setNeedsDisplayMultiAnnotations(annotationMode.annotations)
  1658. NotificationCenter.default.post(name: toolbarImageColorChangedNotificationName, object: nil)
  1659. }
  1660. popWindow.paneCallback = {[weak self] isOpen in
  1661. if isOpen == true {
  1662. self?.viewManager.showRightSide = true
  1663. } else {
  1664. self?.viewManager.showRightSide = false
  1665. }
  1666. self?.refreshToolbarRightViewInfo()
  1667. }
  1668. }
  1669. }
  1670. func reloadPopUIContentEdits(editAreas:[CPDFEditArea]) {
  1671. let editingAreas = listView.km_EditingAreas()
  1672. let editMode = KMNEditContentPopMode(currentEditAreas: editingAreas)
  1673. let popWindow = KMNPopContentEditWindowController.shared
  1674. if editMode.popType == .editNone || !SettingsManager.sharedInstance.showQuickActionBar {
  1675. closePopContentEditWindow()
  1676. } else {
  1677. popWindow.window?.makeKeyAndOrderFront(nil)
  1678. listView.window?.addChildWindow(popWindow.window ?? NSWindow(), ordered: .above)
  1679. popWindow.listView = listView
  1680. popWindow.editContentPopMode = editMode
  1681. popWindow.isOpenPane = viewManager.showRightSide
  1682. updateContentEditPopWinodwFrame()
  1683. popWindow.paneCallback = {[weak self] isOpen in
  1684. if isOpen == true && self?.viewManager.showRightSide == false {
  1685. self?.viewManager.showRightSide = false
  1686. } else {
  1687. self?.viewManager.showRightSide = true
  1688. }
  1689. self?.refreshToolbarRightViewInfo()
  1690. }
  1691. }
  1692. }
  1693. func updateAnnotationsPopWinodwFrame() {
  1694. let popWindow = KMNPopAnnotationWindowController.shared
  1695. if popWindow.window?.isVisible == true {
  1696. popWindow.updateFrame(listView: listView)
  1697. }
  1698. }
  1699. func updateContentEditPopWinodwFrame() {
  1700. let popWindow = KMNPopContentEditWindowController.shared
  1701. if popWindow.window?.isVisible == true {
  1702. popWindow.updateFrame(listView: listView)
  1703. }
  1704. }
  1705. func updatePopOperationPopWinodwFrame() {
  1706. let popWindow = KMNPopOperationWindowController.shared
  1707. if popWindow.window?.isVisible == true {
  1708. popWindow.updateFrame(listView: listView,page: listView.currentSelectionPage())
  1709. }
  1710. }
  1711. //MARK: - 安全
  1712. func removeOwnerPassword() {
  1713. guard let doc = listView.document else {
  1714. NSSound.beep()
  1715. return
  1716. }
  1717. if doc.permissionsStatus != .user {
  1718. NSSound.beep()
  1719. return
  1720. }
  1721. _ = KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: doc.documentURL) { [weak self] result, password in
  1722. if result == .cancel { /// 关闭
  1723. return
  1724. }
  1725. self?.listView.document?.unlock(withPassword: password)
  1726. if doc.permissionsStatus == .owner {
  1727. self?.alertTipViewController.reloadSecureAlertUI()
  1728. self?.alertTipViewController.reloadAlertUIFrame()
  1729. }
  1730. }
  1731. }
  1732. //MARK: - Background背景
  1733. func showBackgroundController() {
  1734. viewManager.editType = .background
  1735. showEditToolbarView()
  1736. editToolbarView?.editType = .background
  1737. if KMBackgroundManager.defaultManager.datas.count == 0 {
  1738. editToolbarView?.editSubType = .add
  1739. } else {
  1740. editToolbarView?.editSubType = .template
  1741. }
  1742. editToolbarView?.reloadData()
  1743. if backgroundViewController == nil {
  1744. backgroundViewController = KMBackgroundController.init()
  1745. }
  1746. backgroundViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1747. backgroundViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1748. backgroundViewController?.delegate = self
  1749. bottomContendBox.contentView?.addSubview(backgroundViewController!.view)
  1750. backgroundViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1751. backgroundViewController?.resetUI()
  1752. updateBackgroundDocument()
  1753. }
  1754. func updateBackgroundDocument() {
  1755. guard let controller = backgroundViewController else { return }
  1756. controller.pdfDocument = nil
  1757. let editDocument = CPDFDocument.init()
  1758. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1759. editDocument?.insertPageObject(page, at: 0)
  1760. backgroundViewController?.pdfDocument = editDocument
  1761. backgroundViewController?.reloadData()
  1762. }
  1763. func removePDFBackground() {
  1764. let alert = NSAlert()
  1765. alert.alertStyle = .warning
  1766. alert.messageText = NSLocalizedString("Are you sure you want to remove the background?", comment: "")
  1767. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  1768. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  1769. let result = alert.runModal()
  1770. if (result == .alertFirstButtonReturn) {
  1771. let background = listView.document.background()
  1772. background?.clear()
  1773. listView.document?.refreshPageData()
  1774. listView.layoutDocumentView()
  1775. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Background removed"),
  1776. type: .success,
  1777. fromView: bottomContendBox,
  1778. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1779. }
  1780. }
  1781. func batchAddBackground() {
  1782. self.showBatchWindow(type: .background, files: nil)
  1783. }
  1784. func batchRemoveBackground() {
  1785. self.showBatchWindow(type: .batchRemove, files: nil)
  1786. }
  1787. //MARK: - header&footer
  1788. func showHeaderFooterController() {
  1789. viewManager.editType = .header_Footer
  1790. showEditToolbarView()
  1791. editToolbarView?.editType = .header_Footer
  1792. if KMHeaderFooterManager.defaultManager.headFooterObjects.count == 0 {
  1793. editToolbarView?.editSubType = .add
  1794. } else {
  1795. editToolbarView?.editSubType = .template
  1796. }
  1797. editToolbarView?.reloadData()
  1798. if headerFooterViewController == nil {
  1799. headerFooterViewController = KMHeaderFooterController.init()
  1800. }
  1801. headerFooterViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1802. headerFooterViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1803. headerFooterViewController?.totalPDFCount = Int(listView.document.pageCount)
  1804. headerFooterViewController?.delegate = self
  1805. bottomContendBox.contentView?.addSubview(headerFooterViewController!.view)
  1806. headerFooterViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1807. updateHeaderFooterDocument()
  1808. }
  1809. func updateHeaderFooterDocument() {
  1810. guard let controller = headerFooterViewController else { return }
  1811. controller.pdfDocument = nil
  1812. let editDocument = CPDFDocument.init()
  1813. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1814. editDocument?.insertPageObject(page, at: 0)
  1815. headerFooterViewController?.totalPDFCount = Int(listView.document.pageCount)
  1816. headerFooterViewController?.pdfDocument = editDocument
  1817. headerFooterViewController?.resetUI()
  1818. headerFooterViewController?.reloadData()
  1819. }
  1820. func removeHeaderFooter() {
  1821. let alert = NSAlert()
  1822. alert.alertStyle = .warning
  1823. alert.messageText = NSLocalizedString("Are you sure you want to remove the Header & Footer?", comment: "")
  1824. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  1825. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  1826. let result = alert.runModal()
  1827. if (result == .alertFirstButtonReturn) {
  1828. let headerFooter = listView.document.headerFooter()
  1829. headerFooter?.clear()
  1830. listView.document?.refreshPageData()
  1831. listView.layoutDocumentView()
  1832. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Header & Footer removed"),
  1833. type: .success,
  1834. fromView: bottomContendBox,
  1835. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1836. }
  1837. }
  1838. func batchAddHeaderFooter() {
  1839. self.showBatchWindow(type: .headerAndFooter, files: nil)
  1840. }
  1841. func batchRemoveHeaderFooter() {
  1842. self.showBatchWindow(type: .batchRemove, files: nil)
  1843. }
  1844. //MARK: - Bates
  1845. func showBatesController() {
  1846. viewManager.editType = .bates
  1847. showEditToolbarView()
  1848. editToolbarView?.editType = viewManager.editType
  1849. if KMBatesManager.defaultManager.datas.count == 0 {
  1850. editToolbarView?.editSubType = .add
  1851. } else {
  1852. editToolbarView?.editSubType = .template
  1853. }
  1854. editToolbarView?.reloadData()
  1855. if batesViewController == nil {
  1856. batesViewController = KMBatesController.init()
  1857. }
  1858. batesViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1859. batesViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1860. batesViewController?.delegate = self
  1861. batesViewController?.totalPDFCount = Int(listView.document.pageCount)
  1862. bottomContendBox.contentView?.addSubview(batesViewController!.view)
  1863. batesViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1864. updateBatesDocument()
  1865. batesViewController?.resetUI()
  1866. }
  1867. func updateBatesDocument() {
  1868. guard let controller = batesViewController else { return }
  1869. controller.pdfDocument = nil
  1870. let editDocument = CPDFDocument.init()
  1871. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1872. editDocument?.insertPageObject(page, at: 0)
  1873. batesViewController?.pdfDocument = editDocument
  1874. batesViewController?.reloadData()
  1875. }
  1876. func removePDFBates() {
  1877. let alert = NSAlert()
  1878. alert.alertStyle = .warning
  1879. alert.messageText = NSLocalizedString("Are you sure you want to remove the Bates?", comment: "")
  1880. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  1881. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  1882. let result = alert.runModal()
  1883. if (result == .alertFirstButtonReturn) {
  1884. let bates = listView.document.bates()
  1885. bates?.clear()
  1886. listView.document?.refreshPageData()
  1887. listView.layoutDocumentView()
  1888. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Bates removed"),
  1889. type: .success,
  1890. fromView: bottomContendBox,
  1891. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1892. }
  1893. }
  1894. func batchAddBates() {
  1895. self.showBatchWindow(type: .batesNumber, files: nil)
  1896. }
  1897. func batchRemoveBates() {
  1898. self.showBatchWindow(type: .batchRemove, files: nil)
  1899. }
  1900. //MARK: - Crop Action
  1901. // 白边距,统一大小
  1902. @objc func auto_cropPagesWhiteMargin(_ pageIndexs: [UInt]) {
  1903. var size = NSZeroSize
  1904. for i in pageIndexs {
  1905. let page = self.listView.document.page(at: i)
  1906. let rect = KMCropTools.getPageForegroundBox(page!)
  1907. size.width = fmax(size.width, NSWidth(rect))
  1908. size.height = fmax(size.height, NSHeight(rect))
  1909. }
  1910. var rectArray: Array<NSRect> = []
  1911. for i in pageIndexs {
  1912. progressC?.increment(by: Double(i))
  1913. progressC?.doubleValue = Double(i)
  1914. let page = self.listView.document.page(at: i)
  1915. var rect = KMCropTools.getPageForegroundBox(page!)
  1916. let bounds: NSRect = (page?.bounds(for: .mediaBox))!
  1917. if (rect.minX - bounds.minX > bounds.maxX-rect.maxX) {
  1918. rect.origin.x = rect.maxX-size.width
  1919. }
  1920. rect.origin.y = rect.maxY-size.height
  1921. rect.size = size
  1922. if (NSWidth(rect) > NSWidth(bounds)) {
  1923. rect.size.width = NSWidth(bounds)
  1924. }
  1925. if (NSHeight(rect) > NSHeight(bounds)) {
  1926. rect.size.height = NSHeight(bounds)
  1927. }
  1928. if (NSMinX(rect) < NSMinX(bounds)) {
  1929. rect.origin.x = NSMinX(bounds)
  1930. } else if (NSMaxX(rect) > NSMaxX(bounds)) {
  1931. rect.origin.x = NSMaxX(bounds) - NSWidth(rect)
  1932. }
  1933. if (NSMinY(rect) < NSMinY(bounds)) {
  1934. rect.origin.y = NSMinY(bounds)
  1935. } else if (NSMaxY(rect) > NSMaxY(bounds)) {
  1936. rect.origin.y = NSMaxY(bounds) - NSHeight(rect)
  1937. }
  1938. rectArray.append(rect)
  1939. }
  1940. self.cropPages(atIndexs: pageIndexs, to: rectArray)
  1941. }
  1942. func cropPages(atIndexs pageIndexs: [UInt], to rects: Array<NSRect>) {
  1943. let currentPage = self.listView.currentPage()
  1944. let visibleRect: NSRect = self.listView.convert(self.listView.convert(self.listView.documentView().visibleRect, from: self.listView.documentView()), to: self.listView.currentPage())
  1945. var oldRectArray: Array<NSRect> = []
  1946. let rectCount = rects.count
  1947. for i in pageIndexs {
  1948. if let page = self.listView.document.page(at: i) {
  1949. let rect = NSIntersectionRect(rects[Int(i) % rectCount], (page.bounds(for: .mediaBox)))
  1950. let oldRect = page.bounds(for: .cropBox)
  1951. oldRectArray.append(oldRect)
  1952. page.setBounds(rect, for: .cropBox)
  1953. }
  1954. }
  1955. let undoManager = self.listView.undoManager
  1956. (undoManager?.prepare(withInvocationTarget: self) as AnyObject).cropPages(atIndexs: pageIndexs, to: oldRectArray)
  1957. /// 刷新预览视图
  1958. self.listView.layoutDocumentView()
  1959. self.listView.displayBox = .cropBox
  1960. self.listView.go(to: currentPage)
  1961. self.listView.go(to: visibleRect, on: currentPage)
  1962. }
  1963. //MARK: - 文件对比
  1964. func beginCompareAction(_ index: Int) {
  1965. let controller = KMCompareWindowController(windowNibName: "KMCompareWindowController")
  1966. self.currentWindowController = controller
  1967. controller.password = self.document?.password ?? ""
  1968. controller.filePath = (self.document?.documentURL.path)!
  1969. controller.cancelAction = { [unowned self] controller in
  1970. self.view.window?.endSheet((self.currentWindowController.window)!)
  1971. self.currentWindowController = nil
  1972. }
  1973. controller.contentComplete = { [unowned self] controller, pdfCompareContent, result, oldDocument, document in
  1974. self.view.window?.endSheet((self.currentWindowController.window)!)
  1975. self.currentWindowController = nil
  1976. self.openContentCompareVC(with: pdfCompareContent, results: result, oldDocument: oldDocument, document: document)
  1977. }
  1978. controller.coveringComplete = { [unowned self] controller, document in
  1979. self.view.window?.endSheet((self.currentWindowController.window)!)
  1980. self.currentWindowController = nil
  1981. self.openCoveringCompareVC(with: document)
  1982. }
  1983. if index == 1 {
  1984. controller.fileType = .content
  1985. } else {
  1986. controller.fileType = .coverting
  1987. }
  1988. NSWindow.currentWindow().beginSheet(controller.window!)
  1989. }
  1990. //文件对比
  1991. func openContentCompareVC(with pdfCompareContent: CPDFCompareContent?, results: [CPDFCompareResults], oldDocument: CPDFDocument, document: CPDFDocument) {
  1992. self.isCompareModel = true
  1993. let compareContentView = KMCompareContentView()
  1994. compareContentView.oldDocument = oldDocument
  1995. compareContentView.document = document
  1996. compareContentView.compareResults = results
  1997. compareContentView.saveHandle = { [unowned self] view in
  1998. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25) { [unowned self] in
  1999. let saveController = KMCompareSaveWindow(windowNibName: "KMCompareSaveWindow")
  2000. self.currentWindowController = saveController
  2001. saveController.cancelHandle = { [unowned self] controller in
  2002. self.view.window!.endSheet(controller.window!)
  2003. self.currentWindowController = nil
  2004. }
  2005. saveController.saveHandle = { [unowned self] controller, saveType in
  2006. let folderPath = controller.fileSaveFolderPath
  2007. if folderPath != nil {
  2008. if !FileManager.default.fileExists(atPath: folderPath) {
  2009. try? FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes: nil)
  2010. }
  2011. #if VERSION_DMG
  2012. #else
  2013. let url = URL(fileURLWithPath: folderPath)
  2014. let fileAccess = AppSandboxFileAccess()
  2015. fileAccess?.persistPermissionURL(url)
  2016. if let bookmarkData = try?url.bookmarkData(options: [.withSecurityScope]) {
  2017. fileAccess?.bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, for: url)
  2018. let urlString = url.path
  2019. let _url = URL(fileURLWithPath: urlString)
  2020. fileAccess?.bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, for: _url)
  2021. }
  2022. #endif
  2023. var savePath: String
  2024. switch saveType {
  2025. case 0:
  2026. let filePath = oldDocument.documentURL.path
  2027. let fileName = filePath.deletingPathExtension.lastPathComponent
  2028. savePath = "\(folderPath)/\(fileName)_compare\(filePath.extension)"
  2029. savePath = self.getValidFilePath(savePath)
  2030. oldDocument.write(to: URL(fileURLWithPath: savePath))
  2031. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: savePath)])
  2032. case 1:
  2033. let filePath = document.documentURL.path
  2034. let fileName = filePath.deletingPathExtension.lastPathComponent
  2035. savePath = "\(folderPath)/\(fileName)_compare\(filePath.extension)"
  2036. savePath = self.getValidFilePath(savePath)
  2037. document.write(to: URL(fileURLWithPath: savePath))
  2038. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: savePath)])
  2039. case 2:
  2040. let filePath = oldDocument.documentURL.path
  2041. let fileName = filePath.deletingPathExtension.lastPathComponent
  2042. savePath = "\(folderPath)/MergedCompareFile\(filePath.extension)"
  2043. savePath = self.getValidFilePath(savePath)
  2044. pdfCompareContent!.saveAsComparisonDocument(withFilePath: savePath)
  2045. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: savePath)])
  2046. default:
  2047. break
  2048. }
  2049. }
  2050. self.view.window!.endSheet(controller.window!)
  2051. self.currentWindowController = nil
  2052. }
  2053. NSWindow.currentWindow().beginSheet(saveController.window!)
  2054. }
  2055. }
  2056. compareContentView.closeHandle = { [unowned self] view in
  2057. self.isCompareModel = false
  2058. view.removeFromSuperview()
  2059. }
  2060. contendBox.addSubview(compareContentView)
  2061. compareContentView.frame = contendBox.bounds
  2062. compareContentView.autoresizingMask = [.width,.height]
  2063. }
  2064. func openCoveringCompareVC(with pdfDocument: CPDFDocument) {
  2065. self.isCompareModel = true
  2066. let coveringView = KMCompareCoveringView()
  2067. coveringView.pdfDocument = pdfDocument
  2068. coveringView.closeHandle = { [unowned self] view in
  2069. self.isCompareModel = false
  2070. view.removeFromSuperview()
  2071. }
  2072. coveringView.saveHandle = { [unowned self] view in
  2073. let savePanel = NSSavePanel()
  2074. savePanel.nameFieldStringValue = "untitled"
  2075. savePanel.allowedFileTypes = ["pdf"]
  2076. savePanel.beginSheetModal(for: NSWindow.currentWindow()) { result in
  2077. if result == .OK {
  2078. pdfDocument.write(to: savePanel.url!)
  2079. NSWorkspace.shared.activateFileViewerSelecting([savePanel.url!])
  2080. }
  2081. }
  2082. }
  2083. contendBox.addSubview(coveringView)
  2084. coveringView.frame = contendBox.bounds
  2085. coveringView.autoresizingMask = [.width,.height]
  2086. }
  2087. func getValidFilePath(_ oldPath: String) -> String {
  2088. let fileManager = FileManager.default
  2089. do {
  2090. let fileAttributes = try fileManager.attributesOfItem(atPath: oldPath)
  2091. guard let fileType = fileAttributes[FileAttributeKey.type] as? String else {
  2092. return oldPath
  2093. }
  2094. var i = 1
  2095. var newPath = oldPath
  2096. while fileManager.fileExists(atPath: newPath) {
  2097. if fileType == FileAttributeType.typeDirectory.rawValue {
  2098. newPath = oldPath + "(\(i))"
  2099. } else {
  2100. let fileExtension = (oldPath as NSString).pathExtension
  2101. newPath = ((oldPath as NSString).deletingPathExtension as NSString).appendingFormat("(\(i)).\(fileExtension)" as NSString) as String
  2102. }
  2103. i += 1
  2104. }
  2105. return newPath
  2106. } catch {
  2107. print("Error getting file attributes: \(error)")
  2108. return oldPath
  2109. }
  2110. }
  2111. //MARK: - TTS
  2112. @IBAction func startSpeaking(_ sender: Any?) {
  2113. self.showTTSWindow()
  2114. let ttsView = KMTTSWindowController.share
  2115. // ttsView.buttonItemClick_Play(ttsView.playButton)
  2116. }
  2117. @IBAction func stopSpeaking(_ sender: Any) {
  2118. let ttsWindowC = KMTTSWindowController.share
  2119. if ttsWindowC.pdfView?.document?.documentURL.path == self.listView.document?.documentURL.path {
  2120. if let data = ttsWindowC.window?.isVisible, data {
  2121. ttsWindowC.stopSpeaking()
  2122. ttsWindowC.close()
  2123. }
  2124. }
  2125. }
  2126. func showTTSWindow() {
  2127. var lastPDFView: CPDFView?
  2128. let ttsView = KMTTSWindowController.share
  2129. if (ttsView.window?.isVisible ?? false) {
  2130. lastPDFView = ttsView.pdfView
  2131. if lastPDFView?.document?.documentURL?.path == self.listView.document?.documentURL?.path {
  2132. lastPDFView = nil
  2133. ttsView.window?.orderOut(nil)
  2134. } else {
  2135. ttsView.pdfView = self.listView
  2136. ttsView.showWindow(nil)
  2137. }
  2138. } else {
  2139. ttsView.pdfView = self.listView
  2140. ttsView.showWindow(nil)
  2141. }
  2142. ttsView.closeWindowCallback = { (isCloseWindow: Bool) in
  2143. if isCloseWindow {
  2144. }
  2145. }
  2146. if let currentSelection = self.listView.currentSelection {
  2147. if let data = currentSelection.selectionsByLine, data.isEmpty == false {
  2148. ttsView.startSpeakingPDFSelection(currentSelection)
  2149. }
  2150. }
  2151. if let lastPDFView = lastPDFView {
  2152. lastPDFView.setHighlightedSelections([])
  2153. ttsView.stopSpeaking()
  2154. }
  2155. }
  2156. //MARK: 导出图片
  2157. func extractImageAction(num: Int) {
  2158. if !IAPProductsManager.default().isAvailableAllFunction(){
  2159. let winC = KMPurchaseCompareWindowController.sharedInstance()
  2160. winC?.kEventName = "Reading_ExtractImage_BuyNow"
  2161. winC?.showWindow(nil)
  2162. return
  2163. }
  2164. if !(self.listView.document.allowsPrinting || self.listView.document.allowsCopying) {
  2165. let alert = NSAlert()
  2166. alert.alertStyle = .critical
  2167. alert.messageText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
  2168. alert.runModal()
  2169. return
  2170. }
  2171. let document = self.listView.document
  2172. var fileURL = document?.documentURL
  2173. if num == 1 {
  2174. let pageCount = document?.pageCount ?? 0
  2175. let indeSet = NSMutableIndexSet()
  2176. indeSet.add(in: NSRange(location: 0, length: Int(pageCount)))
  2177. if indeSet.count == 0 {
  2178. return
  2179. }
  2180. let lastPathName = fileURL?.deletingPathExtension().lastPathComponent ?? ""
  2181. let tFileName = (String(format: "%@_Extract Images", lastPathName))
  2182. let outputSavePanel = NSSavePanel()
  2183. outputSavePanel.title = NSLocalizedString("Save as PDF", comment: "")
  2184. outputSavePanel.allowsOtherFileTypes = true
  2185. outputSavePanel.isExtensionHidden = true
  2186. outputSavePanel.canCreateDirectories = true
  2187. outputSavePanel.nameFieldStringValue = tFileName
  2188. outputSavePanel.beginSheetModal(for: self.view.window!, completionHandler: { (result) in
  2189. if result == NSApplication.ModalResponse.OK {
  2190. DispatchQueue.main.async {
  2191. self.beginProgressSheet(withMessage: NSLocalizedString("Extracting all pictures...", comment: "") + "...", maxValue: 0)
  2192. let tDestFile = outputSavePanel.url!.path
  2193. let uniquePath = KMExtractImageWindowController.createDestFolder(path: tDestFile, isUnique: false)
  2194. let pdfconverter = PDFConvertObject()
  2195. pdfconverter.extractResourcesFromPDF(at: fileURL?.path ?? "", pdfPassword: document?.password, selectIndexSet: indeSet as IndexSet, destDocPath: uniquePath, moreOptions: nil)
  2196. self.dismissProgressSheet()
  2197. let fileManager = FileManager.default
  2198. if fileManager.fileExists(atPath: tDestFile) {
  2199. let workspace = NSWorkspace.shared
  2200. let url = URL(fileURLWithPath: tDestFile)
  2201. workspace.activateFileViewerSelecting([url])
  2202. }
  2203. }
  2204. }
  2205. })
  2206. return
  2207. }
  2208. if fileURL != nil {
  2209. self.myDocument?.save(nil)
  2210. } else {
  2211. let myDocument = self.myDocument
  2212. let str = String(format: "%@.pdf", myDocument?.displayName ?? "")
  2213. let writeSuccess = document!.write(to: URL(fileURLWithPath: (kTempSavePath?.stringByAppendingPathComponent(str))!))
  2214. if writeSuccess {
  2215. var documentTemp = CPDFDocument(url: URL(fileURLWithPath: (kTempSavePath?.stringByAppendingPathComponent(str))!))
  2216. fileURL = document?.documentURL
  2217. } else {
  2218. NSSound.beep()
  2219. return
  2220. }
  2221. }
  2222. extract = KMExtractImageWindowController(windowNibName: "KMExtractImageWindowController")
  2223. extract?.docPath = fileURL?.path ?? ""
  2224. extract?.password = document?.password ?? ""
  2225. extract?.currentPage = self.listView.currentPageIndex
  2226. extract?.own_beginSheetModal(for: self.view.window, completionHandler: { result in
  2227. })
  2228. extract?.selectCurrentPageBtn()
  2229. }
  2230. func beginProgressSheet(withMessage message: String, maxValue: UInt) {
  2231. let progress = SKProgressController()
  2232. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  2233. progress.window?.contentView?.wantsLayer = true
  2234. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  2235. progress.progressField.textColor = NSColor.white
  2236. progress.message = NSLocalizedString("Converting...", comment: "")
  2237. progressC = progress
  2238. progressC?.message = message
  2239. if maxValue > 0 {
  2240. progressC?.indeterminate = false
  2241. progressC?.maxValue = Double(maxValue)
  2242. progressC?.progressBar.doubleValue = 0.3
  2243. } else {
  2244. progressC?.indeterminate = true
  2245. }
  2246. progressC?.own_beginSheetModal(for: self.view.window, completionHandler: { result in
  2247. })
  2248. }
  2249. func dismissProgressSheet() {
  2250. progressC?.stopAnimation()
  2251. progressC?.own_closeEndSheet()
  2252. progressC = nil
  2253. }
  2254. func converFilesToPath(files: Array<KMBatchOperateFile>) -> [String] {
  2255. let newArr = NSMutableArray()
  2256. for item in files {
  2257. newArr.add(item.filePath)
  2258. }
  2259. return newArr as! [String]
  2260. }
  2261. //MARK: - 打印
  2262. internal func showPrintWindow(pageRange: KMPrintPageRange = KMPrintPageRange(type: .allPage, selectPages: [])) {
  2263. self.saveDocument()
  2264. if (self.listView.document != nil && !self.listView.document.allowsPrinting) { // 有打印限制
  2265. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self] result ,password in
  2266. if (result == .cancel) {
  2267. return
  2268. }
  2269. // 解除权限
  2270. self?.listView.document.unlock(withPassword: password)
  2271. // 隐藏提示
  2272. self?.hiddenSecureLimitTip()
  2273. // 去打印
  2274. KMPrintWindowController.openDocument(inputDocument: self?.listView.document, inputPageRange: pageRange)
  2275. }
  2276. return
  2277. }
  2278. KMPrintWindowController.openDocument(inputDocument: self.listView.document, inputPageRange: pageRange)
  2279. }
  2280. //Poster
  2281. func showPosterPrintWindow() {
  2282. guard let document = self.document else { return }
  2283. guard let pdfDocument = PDFDocument(url: document.documentURL) else { return }
  2284. if self.posterPrintWindowController == nil {
  2285. posterPrintWindowController = KMPDFPosterPrintWindowController.init(pdfDocument: pdfDocument)
  2286. }
  2287. if let window = posterPrintWindowController?.window {
  2288. NSApp.mainWindow?.beginSheet(window, completionHandler: { response in
  2289. })
  2290. }
  2291. }
  2292. //Multiple
  2293. func showMultiplePrintWindow() {
  2294. guard let document = self.document else { return }
  2295. guard let pdfDocument = PDFDocument(url: document.documentURL) else { return }
  2296. if self.multiplePrintWindowController == nil {
  2297. multiplePrintWindowController = KMPDFMultiplePrintWindowController.init(pdfDocument: pdfDocument)
  2298. }
  2299. if let window = multiplePrintWindowController?.window {
  2300. NSApp.mainWindow?.beginSheet(window, completionHandler: { response in
  2301. })
  2302. }
  2303. }
  2304. //Booklet
  2305. func showBookletPrintWindow() {
  2306. guard let document = self.document else { return }
  2307. guard let pdfDocument = PDFDocument(url: document.documentURL) else { return }
  2308. if self.bookletWindowController == nil {
  2309. bookletWindowController = KMPDFBookletWindowController.init(document: pdfDocument)
  2310. }
  2311. if let window = bookletWindowController?.window {
  2312. NSApp.mainWindow?.beginSheet(window, completionHandler: { response in
  2313. })
  2314. }
  2315. }
  2316. //MARK: - FileInfo
  2317. func showFileInfo() {
  2318. KMInfoWindowController.shared.showWindow(nil)
  2319. }
  2320. //MARK: - Share Action
  2321. @objc private func shareDocument(sender: NSView) {
  2322. let document = self.listView.document ?? CPDFDocument()
  2323. if document?.documentURL == nil {
  2324. return
  2325. }
  2326. var doucumentURL : URL = self.listView.document.documentURL
  2327. if doucumentURL.path.count > 0 {
  2328. let docDir = NSTemporaryDirectory()
  2329. let documentName : String = doucumentURL.path.lastPathComponent
  2330. let path = docDir.stringByAppendingPathComponent(documentName)
  2331. let writeSuccess = self.listView.document.write(to: URL(fileURLWithPath: path))
  2332. if writeSuccess == false {
  2333. __NSBeep()
  2334. return;
  2335. }
  2336. doucumentURL = URL(fileURLWithPath: path)
  2337. }
  2338. let array = [doucumentURL]
  2339. let picker = NSSharingServicePicker.init(items: array)
  2340. if sender.window != nil {
  2341. picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
  2342. } else {
  2343. 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)
  2344. }
  2345. }
  2346. @objc private func shareFlatten(sender: NSView) {
  2347. let document = self.listView.document ?? CPDFDocument()
  2348. var path: String?
  2349. if document?.documentURL != nil {
  2350. path = document?.documentURL.path ?? ""
  2351. }
  2352. if path?.count ?? 0 > 0 {
  2353. let docDir = NSTemporaryDirectory()
  2354. let documentName : String = path?.lastPathComponent ?? ""
  2355. path = docDir.stringByAppendingPathComponent(documentName)
  2356. }
  2357. let pathFolder = path?.fileURL.deletingLastPathComponent().path
  2358. var tfileName = path?.deletingPathExtension.lastPathComponent
  2359. let tStdFileSuffix = "_flatten"
  2360. tfileName = (tfileName ?? "") + tStdFileSuffix + ".pdf"
  2361. path = (pathFolder ?? "") + "/" + (tfileName ?? "")
  2362. let success : Bool = document?.writeFlatten(to: URL(fileURLWithPath: path ?? "")) ?? false
  2363. if success {
  2364. let url = URL(fileURLWithPath: path ?? "")
  2365. let picker = NSSharingServicePicker.init(items: [url])
  2366. if sender.window != nil {
  2367. picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
  2368. } else {
  2369. 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)
  2370. }
  2371. }
  2372. }
  2373. @objc private func shareOriginalPDF(sender: NSView) {
  2374. guard let pdfDoc = self.listView.document else {
  2375. NSSound.beep()
  2376. return
  2377. }
  2378. if !pdfDoc.allowsCopying || !pdfDoc.allowsPrinting {
  2379. let alert = NSAlert()
  2380. alert.alertStyle = .critical
  2381. alert.messageText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
  2382. alert.runModal()
  2383. return
  2384. }
  2385. let document = self.listView.document ?? CPDFDocument()
  2386. var path: String?
  2387. if document?.documentURL != nil {
  2388. path = document?.documentURL.path ?? ""
  2389. }
  2390. if path?.count ?? 0 > 0 {
  2391. let docDir = NSTemporaryDirectory()
  2392. let documentName : String = path?.lastPathComponent ?? ""
  2393. path = docDir.stringByAppendingPathComponent(documentName)
  2394. }
  2395. var writeSuccess = document?.write(to: URL(fileURLWithPath: path ?? ""))
  2396. if writeSuccess == false {
  2397. __NSBeep()
  2398. return;
  2399. }
  2400. let newDocument = CPDFDocument(url: URL(fileURLWithPath: path ?? ""))
  2401. let cnt = newDocument?.pageCount ?? 0
  2402. for i in 0 ..< cnt {
  2403. let page = newDocument!.page(at: i)
  2404. var annotations : [CPDFAnnotation] = []
  2405. for annotation in page!.annotations {
  2406. annotations.append(annotation)
  2407. }
  2408. for annotation in annotations {
  2409. annotation.page.removeAnnotation(annotation)
  2410. }
  2411. }
  2412. writeSuccess = newDocument?.write(to:URL(fileURLWithPath: path ?? ""))
  2413. if writeSuccess ?? false {
  2414. let url = URL(fileURLWithPath: path ?? "")
  2415. let picker = NSSharingServicePicker.init(items: [url])
  2416. if sender.window != nil {
  2417. picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
  2418. } else {
  2419. 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)
  2420. }
  2421. }
  2422. }
  2423. @objc func shareFromService(sender: NSMenuItem) {
  2424. if ((NSApp.mainWindow?.windowController is KMBrowserWindowController) == false) {
  2425. return
  2426. }
  2427. var string = ""
  2428. if let freeTextAnnotation = listView.activeAnnotation as? CPDFFreeTextAnnotation {
  2429. string = freeTextAnnotation.contents ?? ""
  2430. } else if let markupAnnotation = listView.activeAnnotation as? CPDFMarkupAnnotation {
  2431. if let page = markupAnnotation.page {
  2432. if let selection = page.selection(for: markupAnnotation.bounds) {
  2433. string = selection.string() ?? ""
  2434. }
  2435. }
  2436. } else {
  2437. string = listView.currentSelection?.string() ?? ""
  2438. }
  2439. let windowControler = NSApp.mainWindow?.windowController as! KMBrowserWindowController
  2440. let model = windowControler.browser?.tabStripModel
  2441. if let cnt = model?.count(), cnt <= 0 {
  2442. return
  2443. }
  2444. if let data = model?.activeTabContents().isHome, data {
  2445. return
  2446. }
  2447. let document: KMMainDocument = model?.activeTabContents() as! KMMainDocument
  2448. if string.count > 0 {
  2449. let represent : NSSharingService = sender.representedObject as! NSSharingService
  2450. represent.perform(withItems: [string])
  2451. return
  2452. }
  2453. let represent = sender.representedObject as? NSSharingService
  2454. represent?.perform(withItems: [string])
  2455. }
  2456. //MARK: - PDFView Menu
  2457. func clickPresentationMenu(point:NSPoint)->KMNMenuStruct {
  2458. var viewHeight: CGFloat = 8
  2459. var menuItemArr: [ComponentMenuitemProperty] = []
  2460. var items: [(String, String)] = []
  2461. items.append(("Next Page", PDFViewMenuIdentifier_PageNext))
  2462. items.append(("Previous Page", PDFViewMenuIdentifier_PagePrevious))
  2463. items.append(("First", PDFViewMenuIdentifier_PageFirst))
  2464. items.append(("Last", PDFViewMenuIdentifier_PageLast))
  2465. items.append(("", ""))
  2466. items.append(("Laser pointer", PDFViewMenuIdentifier_Presentation_LaserPoint))
  2467. items.append(("Brush", PDFViewMenuIdentifier_Presentation_Brush))
  2468. items.append(("", ""))
  2469. items.append(("Exit Presentation", PDFViewMenuIdentifier_Presentation_Exit))
  2470. for (i, value) in items {
  2471. if value.count == 0 {
  2472. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  2473. menuItemArr.append(property)
  2474. viewHeight += 8
  2475. } else {
  2476. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  2477. itemSelected: false,
  2478. isDisabled: false,
  2479. keyEquivalent: nil,
  2480. text: KMLocalizedString(i),
  2481. identifier: value,representedObject: point)
  2482. if value == PDFViewMenuIdentifier_PageNext {
  2483. properties_Menuitem.keyEquivalent = "▶"
  2484. } else if value == PDFViewMenuIdentifier_PagePrevious {
  2485. properties_Menuitem.keyEquivalent = "◀"
  2486. } else if value == PDFViewMenuIdentifier_PageFirst {
  2487. properties_Menuitem.keyEquivalent = "⌘ ◀"
  2488. } else if value == PDFViewMenuIdentifier_PageLast {
  2489. properties_Menuitem.keyEquivalent = "⌘ ▶"
  2490. } else if value == PDFViewMenuIdentifier_Presentation_Exit {
  2491. properties_Menuitem.keyEquivalent = "ESC"
  2492. }
  2493. if(value == PDFViewMenuIdentifier_Presentation_LaserPoint) {
  2494. if((listView.presentationDrawView != nil) && listView.presentationDrawView.isHidden == false) {
  2495. } else {
  2496. properties_Menuitem.righticon = NSImage(named: "KMNImageNameMenuSelect")
  2497. }
  2498. } else if value == PDFViewMenuIdentifier_Presentation_Brush {
  2499. if((listView.presentationDrawView != nil) && listView.presentationDrawView.isHidden == false) {
  2500. properties_Menuitem.righticon = NSImage(named: "KMNImageNameMenuSelect")
  2501. } else {
  2502. }
  2503. }
  2504. menuItemArr.append(properties_Menuitem)
  2505. viewHeight += 36
  2506. }
  2507. }
  2508. let menuStruct = KMNMenuStruct(menuitems: menuItemArr, viewHeight: viewHeight)
  2509. return menuStruct
  2510. }
  2511. func gotoPage(_ sender: Any?) {
  2512. var pages : [String] = []
  2513. for i in 0 ..< self.listView.document.pageCount {
  2514. pages.append("\(i)")
  2515. }
  2516. self.textFieldSheet.callback = { [weak self] stringValue in
  2517. guard let index = Int(stringValue) else {
  2518. return
  2519. }
  2520. if (self?.listView == nil) {
  2521. return
  2522. }
  2523. if (index > 0 && index <= self!.listView.document.pageCount) {
  2524. self?.listView.go(toPageIndex: index-1, animated: true)
  2525. }
  2526. }
  2527. self.textFieldSheet.showWindow(nil)
  2528. self.textFieldSheet.pageBox.addItems(withObjectValues: pages)
  2529. self.textFieldSheet.stringValue = "\(self.listView.currentPageIndex+1)"
  2530. }
  2531. //MARK: - 添加书签
  2532. @objc func menuItemBookMarkClick_add(sender:NSMenuItem?) {
  2533. if self.listView.document?.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) == nil {
  2534. let index = self.listView.currentPageIndex
  2535. self.listView.document?.addBookmark("\(NSLocalizedString("Page", comment: "")) \(index+1)", forPageIndex: UInt(index))
  2536. self.listView.updateRender(true)
  2537. } else {
  2538. self.listView.document?.removeBookmark(forPageIndex: UInt(self.listView.currentPageIndex))
  2539. self.listView.updateRender(true)
  2540. }
  2541. botaViewController?.reloadData()
  2542. self.listView.undoManager?.setActionName("")//添加undo事件就可删除
  2543. }
  2544. //MARK: - 页面旋转
  2545. func rotateLeft(page:CPDFPage,listView:CPDFListView?) {
  2546. Task { @MainActor in
  2547. let rotation = page.rotation
  2548. page.leftRotate()
  2549. listView?.layoutDocumentView()
  2550. if(listView == self.listView) {
  2551. var pageIndexes = IndexSet()
  2552. pageIndexes.insert(Int(page.pageIndex()))
  2553. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexes)
  2554. botaViewController?.reloadData()
  2555. }
  2556. }
  2557. }
  2558. func rotateRight(page:CPDFPage,listView:CPDFListView?) {
  2559. Task { @MainActor in
  2560. let rotation = page.rotation
  2561. page.rightRotate()
  2562. listView?.layoutDocumentView()
  2563. if(listView == self.listView) {
  2564. var pageIndexes = IndexSet()
  2565. pageIndexes.insert(Int(page.pageIndex()))
  2566. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexes)
  2567. botaViewController?.reloadData()
  2568. }
  2569. }
  2570. }
  2571. //MARK: - 自动滚动
  2572. func isAutoFlowOn() -> Bool {
  2573. return listView.isAutoFlow()
  2574. }
  2575. func toggleAutoFlow(_ sender: Any?) {
  2576. if (listView.isAutoFlow()) {
  2577. listView.stopAutoFlow()
  2578. } else {
  2579. listView.startAutoFlow()
  2580. }
  2581. }
  2582. //MARK: - 高亮Form
  2583. func highlightFormFiled(_ sender: Any?) {
  2584. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  2585. CPDFKitConfig.sharedInstance().setEnableFormFieldHighlight(!enabled)
  2586. listView.setNeedsDisplayForVisiblePages()
  2587. }
  2588. //MARK: - 高亮Link
  2589. func highlightLinks(_ sender: Any?) {
  2590. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  2591. CPDFAnnotation.updateLinkFieldHighlight(listView, linkFieldHighlight: !enabled)
  2592. }
  2593. //MARK: - 重置Form
  2594. func resetForm(_ sender: Any?) {
  2595. listView.resetFormAnnotation()
  2596. }
  2597. //MARK: - 属性
  2598. @IBAction func menuItemAction_property(_ sender: Any?) {
  2599. showFileInfo()
  2600. }
  2601. //MARK: - 打印
  2602. @IBAction func menuItemAction_print(_ sender: Any?) {
  2603. self.showPrintWindow()
  2604. }
  2605. //MARK: - Flattened
  2606. func saveAsFlattenedPDFAction() {
  2607. DispatchQueue.main.async {
  2608. NSPanel.savePanel(self.view.window!, true) { panel in
  2609. panel.nameFieldStringValue = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_flatten" + ".pdf"
  2610. } completion: { response, url, isOpen in
  2611. if (response == .cancel) {
  2612. return
  2613. }
  2614. var result = self.listView.document.writeFlatten(to: url!)
  2615. if (!result) {
  2616. return
  2617. }
  2618. if (isOpen) {
  2619. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  2620. }
  2621. } else {
  2622. NSWorkspace.shared.activateFileViewerSelecting([url!])
  2623. }
  2624. }
  2625. }
  2626. }
  2627. func showInFinder() {
  2628. NSWorkspace.shared.activateFileViewerSelecting([self.listView.document.documentURL])
  2629. }
  2630. //MARK: - DisplayViewMode
  2631. func getPDFViewPageLayoutType() -> pageLayoutType {
  2632. if listView.displayMode() == .singlePage {
  2633. return .singlePage
  2634. } else if listView.displayMode() == .singlePageContinuous {
  2635. return .singlePageContinue
  2636. } else if listView.displayMode() == .twoUp {
  2637. if listView.displaysAsBook == true {
  2638. return .bookMode
  2639. }
  2640. return .twoPage
  2641. } else if listView.displayMode() == .twoUpContinuous {
  2642. if listView.displaysAsBook == true {
  2643. return .bookMode
  2644. }
  2645. return .twoPageContinue
  2646. }
  2647. return .singlePage
  2648. }
  2649. func updatePDFViewDisplayMode(viewMode mode: CPDFDisplayViewMode = .singlePage, isbookMode bookMode: Bool = false, direction directionValue: CPDFDisplayDirection? = nil) {
  2650. if bookMode == true {
  2651. //书本模式
  2652. listView.displaysAsBook = true
  2653. listView.displayTwoUp = true
  2654. if let value = directionValue {
  2655. listView.displayDirection = value
  2656. }
  2657. } else {
  2658. listView.setDisplay(mode)
  2659. }
  2660. listView.layoutDocumentView()
  2661. }
  2662. //MARK: - 新增大纲
  2663. @objc func addOutLineItemAction() {
  2664. botaViewController?.outlineViewC.addOutline()
  2665. }
  2666. //MARK: - 搜索
  2667. @objc func searchBaiduAction() {
  2668. let label = self.listView.currentSelection?.string() ?? ""
  2669. let query = label.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
  2670. if let url = URL(string: "https://www.baidu.com/s?wd=\(query)") {
  2671. NSWorkspace.shared.open(url)
  2672. }
  2673. }
  2674. //MARK: - 转档
  2675. func showConvertWindow(_ convertType: KMPDFConvertType) {
  2676. var winC: KMConvertBaseWindowController = KMConvertBaseWindowController()
  2677. if convertType == .word {
  2678. winC = KMConvertWordWindowController()
  2679. let model = KMDocumentModel(url: listView.document.documentURL)
  2680. winC.documentModel = model
  2681. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2682. } else if convertType == .ppt {
  2683. winC = KMConvertPPTsWindowController()
  2684. winC.subType = 1
  2685. let model = KMDocumentModel(url: listView.document.documentURL)
  2686. winC.documentModel = model
  2687. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2688. } else if convertType == .rtf {
  2689. winC = KMConvertPPTsWindowController()
  2690. winC.subType = 2
  2691. let model = KMDocumentModel(url: listView.document.documentURL)
  2692. winC.documentModel = model
  2693. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2694. } else if convertType == .text {
  2695. winC = KMConvertPPTsWindowController()
  2696. winC.subType = 4
  2697. let model = KMDocumentModel(url: listView.document.documentURL)
  2698. winC.documentModel = model
  2699. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2700. } else if convertType == .csv {
  2701. winC = KMConvertPPTsWindowController()
  2702. winC.subType = 5
  2703. let model = KMDocumentModel(url: listView.document.documentURL)
  2704. winC.documentModel = model
  2705. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2706. } else if convertType == .excel {
  2707. winC = KMConvertExcelWindowController()
  2708. let model = KMDocumentModel(url: listView.document.documentURL)
  2709. winC.documentModel = model
  2710. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2711. } else if convertType == .html {
  2712. winC = KMConvertHtmlWindowController()
  2713. let model = KMDocumentModel(url: listView.document.documentURL)
  2714. winC.documentModel = model
  2715. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2716. } else if convertType == .json {
  2717. winC = KMConvertJsonWindowController()
  2718. let model = KMDocumentModel(url: listView.document.documentURL)
  2719. winC.documentModel = model
  2720. winC.own_beginSheetModal(for: view.window, completionHandler: nil)
  2721. } else if convertType == .jpeg ||
  2722. convertType == .jpg ||
  2723. convertType == .png ||
  2724. convertType == .gif ||
  2725. convertType == .tiff ||
  2726. convertType == .tga ||
  2727. convertType == .bmp ||
  2728. convertType == .jp2{
  2729. winC = KMConvertImageWindowController()
  2730. let model = KMDocumentModel(url: listView.document.documentURL)
  2731. winC.documentModel = model
  2732. winC.own_beginSheetModal(for: view.window, completionHandler: nil)
  2733. if let settingView = winC.settingView as? KMConvertImageSettingView {
  2734. settingView.selectConvertType(convertType: convertType)
  2735. }
  2736. }
  2737. winC.batchAction = { [unowned self] in
  2738. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  2739. model.password = listView.document.password ?? ""
  2740. self.showBatchWindow(type: .convertPDF, subType: convertType.rawValue, files: [], fileModels: [model])
  2741. }
  2742. }
  2743. //MARK: - 选择Form注释
  2744. @objc func selectAllFormAnnotation () {
  2745. var formActiveAnnotations:[CPDFAnnotation] = []
  2746. for i in 0 ..< listView.document.pageCount {
  2747. let page = listView.document.page(at: i)
  2748. let annotations = page?.annotations
  2749. for j in 0 ..< (annotations?.count ?? 0) {
  2750. if let an = annotations?[j] as? CPDFAnnotation {
  2751. if (an.isKind(of: CPDFWidgetAnnotation.self) == true) {
  2752. formActiveAnnotations.append(an)
  2753. }
  2754. }
  2755. }
  2756. }
  2757. listView.updateActiveAnnotations(formActiveAnnotations)
  2758. }
  2759. @objc func selectAllNomerAnnotation () {
  2760. var normalActiveAnnotations:[CPDFAnnotation] = []
  2761. for i in 0 ..< listView.document.pageCount {
  2762. let page = listView.document.page(at: i)
  2763. let annotations = page?.annotations
  2764. for j in 0 ..< (annotations?.count ?? 0) {
  2765. if let an = annotations?[j] as? CPDFAnnotation {
  2766. if an.isKind(of: CPDFWidgetAnnotation.self) == true ||
  2767. an.isKind(of: CPDFLinkAnnotation.self) == true ||
  2768. an.isKind(of: CPDFRedactAnnotation.self) == true {
  2769. } else {
  2770. normalActiveAnnotations.append(an)
  2771. }
  2772. }
  2773. }
  2774. }
  2775. listView.updateActiveAnnotations(normalActiveAnnotations)
  2776. }
  2777. //MARK: 搜索 & 替换
  2778. func showSearchPopWindow(type: KMNBotaSearchType, keyborad: String?, replaceText: String?, results: [KMSearchMode]) {
  2779. let toolMode = self.listView.toolMode
  2780. let isEditing = self.listView.isEditing()
  2781. var winH: CGFloat = 112
  2782. if type == .replace {
  2783. if IAPProductsManager.default().isAvailableAllFunction() == false {
  2784. let winC = KMPurchaseCompareWindowController.sharedInstance()
  2785. winC?.kEventName = "Reading_ReplaceText_BuyNow"
  2786. winC?.showWindow(nil)
  2787. return
  2788. }
  2789. if toolMode == .CEditPDFToolMode && isEditing {
  2790. } else { // 进入内容编辑模式
  2791. viewManager.toolMode = .Edit
  2792. updatePDFViewAnnotationMode()
  2793. }
  2794. winH = 208
  2795. }
  2796. self.view.window?.makeFirstResponder(nil)
  2797. let winC = KMSearchReplaceWindowController(with: listView, type: type)
  2798. self.currentWindowController = winC
  2799. winC.replaceCallback = { [weak self] in
  2800. let toolMode = self?.listView.toolMode ?? .none
  2801. let isEditing = self?.listView.isEditing() ?? false
  2802. if toolMode == .CEditPDFToolMode && isEditing {
  2803. } else { // 进入内容编辑模式
  2804. self?.viewManager.toolMode = .Edit
  2805. self?.updatePDFViewAnnotationMode()
  2806. }
  2807. }
  2808. winC.itemClick = { [weak self] idx, params in
  2809. if idx == 1 {
  2810. self?.toggleOpenLeftSide(pdfSideBarType: .search)
  2811. guard let handdler = params.first as? KMNSearchHanddler else {
  2812. return
  2813. }
  2814. let viewC = self?.botaViewController?.searchViewC
  2815. viewC?.update(keyborad: handdler.searchKey, replaceKey: handdler.replaceKey, results: handdler.searchSectionResults)
  2816. }
  2817. }
  2818. let targetView = self.pdfToolbarController?.leftViewButton
  2819. let point = targetView?.convert(targetView?.frame.origin ?? .zero, to: nil) ?? .zero
  2820. // 200 248
  2821. let x = point.x + (self.view.window?.frame.origin.x ?? 0) - 32
  2822. let y = point.y + (self.view.window?.frame.origin.y ?? 0) - winH - 32
  2823. let winFramePoint = NSPoint(x: x, y: y)
  2824. winC.window?.setFrameOrigin(winFramePoint)
  2825. winC.update(keyborad: keyborad, replaceKey: replaceText, results: results)
  2826. self.view.window?.addChildWindow(winC.window!, ordered: .above)
  2827. }
  2828. // MARK: -显示加密弹窗
  2829. //MARK: - Redact密文
  2830. func showRedactProperty(readactAnnotation: CPDFRedactAnnotation?) {
  2831. let properties = KMRedactPropertiesWindowController()
  2832. properties.readactAnnotation = readactAnnotation
  2833. properties.own_beginSheetModal(for: self.view.window) { result in
  2834. }
  2835. }
  2836. //MARK: - 测量
  2837. func refreshMeasureInfo() {
  2838. if distanceMeasureInfoWindowController == nil {
  2839. distanceMeasureInfoWindowController = CDistanceMeasureInfoWindowController()
  2840. }
  2841. if perimeterMeasureInfoWindowController == nil {
  2842. perimeterMeasureInfoWindowController = CPerimeterMeasureInfoWindowController()
  2843. }
  2844. if areaMeasureInfoWindowController == nil {
  2845. areaMeasureInfoWindowController = CAreaMeasureInfoWindowController()
  2846. }
  2847. showMeasureFloatingWindowsIfNeed()
  2848. }
  2849. @objc func cancelMeasureType() {
  2850. self.hideMeasureFloatingWindows()
  2851. }
  2852. func hideMeasureFloatingWindows() {
  2853. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  2854. distanceMeasureInfoWindowController?.hideFloatingWindow()
  2855. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  2856. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  2857. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  2858. areaMeasureInfoWindowController?.hideFloatingWindow()
  2859. }
  2860. }
  2861. func showMeasureFloatingWindowsIfNeed() {
  2862. cancelMeasureType()
  2863. let toolMode = self.listView.toolMode
  2864. if toolMode != .CNoteToolMode {
  2865. return
  2866. }
  2867. if let data = self.listView.activeAnnotation as? CPDFPolylineAnnotation {
  2868. } else if let data = self.listView.activeAnnotation as? CPDFPolygonAnnotation {
  2869. } else if let data = self.listView.activeAnnotation as? CPDFLineAnnotation, data.isMeasure {
  2870. } else {
  2871. let type = self.listView.annotationType
  2872. if type == .measureLine {
  2873. distanceMeasureInfoWindowController?.measureInfo = listView.distanceMeasureInfo
  2874. distanceMeasureInfoWindowController?.window?.orderFront(nil)
  2875. } else if type == .measurePolyLine {
  2876. perimeterMeasureInfoWindowController?.measureInfo = listView.perimeterMeasureInfo
  2877. perimeterMeasureInfoWindowController?.window?.orderFront(nil)
  2878. } else if type == .measurePolyGon {
  2879. areaMeasureInfoWindowController?.measureInfo = listView.polygonAreaMeasureInfo
  2880. areaMeasureInfoWindowController?.window?.orderFront(nil)
  2881. } else if type == .measureSquare {
  2882. areaMeasureInfoWindowController?.measureInfo = listView.squareAreaMeasureInfo
  2883. areaMeasureInfoWindowController?.window?.orderFront(nil)
  2884. } else {
  2885. cancelMeasureType()
  2886. }
  2887. }
  2888. }
  2889. func showMeasureSettingWindow() {
  2890. let distanceSettingWC = CDistanceSettingWindowController(measureInfo: self.listView.distanceMeasureInfo)
  2891. self.distanceMeasureInfoWindowController?.hideFloatingWindow()
  2892. distanceSettingWC.delegate = self
  2893. distanceSettingWC.own_beginSheetModal(for: self.view.window) { string in
  2894. }
  2895. }
  2896. }
  2897. //MARK: Compress
  2898. extension KMMainViewController {
  2899. func showCompressController(_ url: URL) {
  2900. KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), type: .owner, url: url) { [unowned self] result, password in
  2901. if (result == .success) {
  2902. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  2903. model.password = password ?? ""
  2904. model.isLock = false
  2905. self.showCompressController(model: model)
  2906. }
  2907. }
  2908. }
  2909. func showCompressController(model: KMBatchProcessingTableViewModel) {
  2910. let document = CPDFDocument(url: model.filePath.fileURL)
  2911. document?.unlock(withPassword: model.password)
  2912. if !document!.allowsPrinting && !document!.allowsCopying {
  2913. KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), type: .owner, url: document!.documentURL) { [unowned self] result, password in
  2914. if (result == .success) {
  2915. // let model = KMBatchProcessingTableViewModel.initWithFilePath(url: model.filePath.fileURL)
  2916. model.password = password ?? ""
  2917. model.isLock = false
  2918. self.showCompress(model: model)
  2919. }
  2920. }
  2921. }
  2922. }
  2923. func showCompress(model: KMBatchProcessingTableViewModel) {
  2924. self.compressWindowController = KMCompressWindowController(windowNibName: "KMCompressWindowController")
  2925. self.compressWindowController?.password = model.password
  2926. self.compressWindowController?.documentURL = model.filePath.fileURL
  2927. let controllerWindow = self.view.window
  2928. controllerWindow?.beginSheet(self.compressWindowController!.window!)
  2929. self.compressWindowController?.itemClick = { [unowned self] in
  2930. controllerWindow?.endSheet((self.compressWindowController?.window)!)
  2931. }
  2932. self.compressWindowController?.batchAction = { [unowned self] view, filePaths in
  2933. controllerWindow?.endSheet((self.compressWindowController?.window)!)
  2934. self.showBatchWindow(type: .compress, files: filePaths)
  2935. }
  2936. self.compressWindowController?.resultCallback = { [unowned self] result, openDocument, fileURL, error in
  2937. controllerWindow?.endSheet((self.compressWindowController?.window)!)
  2938. if (result) {
  2939. if (openDocument) {
  2940. NSDocumentController.shared.openDocument(withContentsOf: fileURL, display: true) { document, result, error in }
  2941. } else {
  2942. NSWorkspace.shared.activateFileViewerSelecting([fileURL])
  2943. }
  2944. } else {
  2945. let alert = NSAlert()
  2946. alert.messageText = NSLocalizedString("Compress Faild", comment: "")
  2947. alert.runModal()
  2948. }
  2949. }
  2950. }
  2951. }
  2952. //MARK: - OCR
  2953. extension KMMainViewController {
  2954. //window
  2955. func showOCRWindow() {
  2956. if !IAPProductsManager.default().isAvailableAllFunction(){
  2957. let winC = KMPurchaseCompareWindowController.sharedInstance()
  2958. winC?.showWindow(nil)
  2959. return
  2960. }
  2961. let window = KMOCRSettingWindowController(windowNibName: "KMOCRSettingWindowController")
  2962. window.OCRAction = {[unowned self] controller, model in
  2963. self.convertOCRScanFile(window: window, document: self.listView.document, model: model)
  2964. }
  2965. window.cancelAction = {[unowned self] controller in
  2966. self.view.window?.endSheet(window.window ?? NSWindow())
  2967. }
  2968. self.view.window?.beginSheet(window.window ?? NSWindow())
  2969. // self.km_beginSheet(windowC: window)
  2970. }
  2971. func showOCREditAlert() {
  2972. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { [unowned self] in
  2973. alertTipViewController.showInView(listView: listView, subView: infoSplitCenterView)
  2974. alertTipViewController.reloadOCRAlertUI()
  2975. alertTipViewController.reloadAlertUIFrame()
  2976. alertTipViewController.OCRApplyCallback = { [unowned self] in
  2977. self.showOCRWindow()
  2978. }
  2979. }
  2980. }
  2981. func closeOCREditAlert() {
  2982. guard let view = alertTipViewController.OCRComponentAlert else { return }
  2983. alertTipViewController.reloadOCRAlertUI()
  2984. alertTipViewController.reloadAlertUIFrame()
  2985. }
  2986. func showOCRToolAlert(_ string: String) {
  2987. let alertView = ComponentMessage()
  2988. alertView.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString(string))
  2989. alertView.frame = CGRectMake((CGRectGetWidth(self.listView.frame) - alertView.properties.propertyInfo.viewWidth) / 2,
  2990. CGRectGetHeight(self.listView.frame) - alertView.properties.propertyInfo.viewHeight - 8,
  2991. alertView.properties.propertyInfo.viewWidth,
  2992. alertView.properties.propertyInfo.viewHeight)
  2993. alertView.reloadData()
  2994. self.listView.addSubview(alertView)
  2995. // 自动移除视图
  2996. DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
  2997. self.removeOCRToolAlert(alertView)
  2998. }
  2999. }
  3000. private func removeOCRToolAlert(_ alertView: NSView) {
  3001. // 淡出动画
  3002. NSAnimationContext.runAnimationGroup({ context in
  3003. context.duration = 0.3
  3004. alertView.animator().alphaValue = 0
  3005. }, completionHandler: {
  3006. alertView.removeFromSuperview()
  3007. })
  3008. }
  3009. //OCR
  3010. func convertOCRScanFile(window: KMOCRSettingWindowController, document: CPDFDocument, model: KMOCRModel) {
  3011. window.beginLoading()
  3012. //当前页面需要提前设置
  3013. if model.pageRangeType == .current {
  3014. model.pageRange = [self.listView.currentPageIndex]
  3015. }
  3016. KMOCRManager.manager.convertScanFile(document: document, model: model, progress: { progress in
  3017. }) { [weak self] document, text, error in
  3018. window.endLoading()
  3019. window.km_quick_endSheet()
  3020. if !model.saveAsPDF {
  3021. self?.listView.layoutDocumentView()
  3022. }
  3023. }
  3024. }
  3025. func convertOCR(document: CPDFDocument, model: KMOCRModel) {
  3026. // self.view.window?.windowController.beginLoading()
  3027. //当前页面需要提前设置
  3028. if model.pageRangeType == .current {
  3029. model.pageRange = [self.listView.currentPageIndex]
  3030. }
  3031. self.showProgressWindow(message: "正在转换中....")
  3032. KMOCRManager.manager.convertOCR(document: document, model: model, progress: { [weak self] progress in
  3033. self?.progressC?.message = "正在转换中...."
  3034. self?.progressC?.doubleValue = Double(progress * 100)
  3035. }) { [weak self] document, text, error in
  3036. // self?.view.window?.windowController.endLoading()
  3037. // window.km_quick_endSheet()
  3038. if !model.saveAsPDF {
  3039. self?.listView.layoutDocumentView()
  3040. }
  3041. self?.hiddenProgressWindow()
  3042. }
  3043. self.progressC?.closeBlock = {
  3044. print("手动取消中")
  3045. KMOCRManager.manager.cancelRecognition()
  3046. }
  3047. }
  3048. func convertOCRSaveAsTXT(text: String) {
  3049. NSPanel.savePanel(NSWindow.currentWindow()) { panel in
  3050. let url: URL = self.listView.document.documentURL
  3051. panel.nameFieldStringValue = ""+url.deletingPathExtension().lastPathComponent+"_OCR"
  3052. panel.allowedFileTypes = ["txt"]
  3053. } completion: { [unowned self] response, url in
  3054. if (response == .cancel) {
  3055. return
  3056. }
  3057. let saveAsPDFFilePath = url?.path ?? ""
  3058. let outputURL = URL(fileURLWithPath: saveAsPDFFilePath)
  3059. try? text.write(to: outputURL, atomically: true, encoding: .utf8)
  3060. }
  3061. }
  3062. func convertSelectionRectOCR(rect: NSRect) {
  3063. let rect = NSIntegralRect(rect)
  3064. let orgPage : CPDFPage = listView.currentSelectionPage() ?? CPDFPage()
  3065. if let page : CPDFPage = orgPage.copy() as? CPDFPage {
  3066. page.setBounds(rect, for: .cropBox)
  3067. let image = page.thumbnail(of: rect.size) ?? NSImage()
  3068. guard let model = self.rightSideController?.tool_OCRController?.model else { return }
  3069. model.pageRange = [Int(orgPage.pageIndex())]
  3070. KMOCRManager.manager.convertOCR(images: [image], model: model, progress: { progress in
  3071. }) { [weak self] document, text, error in
  3072. self?.rightSideController?.tool_OCRController?.model.text = text ?? ""
  3073. self?.rightSideController?.tool_OCRController?.reloadData()
  3074. //关闭窗口
  3075. self?.listView.selectionRect = NSZeroRect
  3076. self?.listView.selectionPageIndex = UInt(NSNotFound)
  3077. self?.closePopOperationWindow()
  3078. self?.listView.setNeedsDisplayForVisiblePages()
  3079. }
  3080. }
  3081. }
  3082. }
  3083. //MARK: Batch
  3084. extension KMMainViewController {
  3085. func showBatchWindow(type: KMBatchCollectionViewType, fileModels: [KMBatchProcessingTableViewModel]?) {
  3086. self.showBatchWindow(type: type, subType: 0, files: [], fileModels: fileModels)
  3087. }
  3088. func showBatchWindow(type: KMBatchCollectionViewType, files: [URL]?) {
  3089. self.showBatchWindow(type: type, subType: 0, files: files, fileModels: [])
  3090. }
  3091. func showBatchWindow(type: KMBatchCollectionViewType, subType: Int = 0, files: [URL]?, fileModels: [KMBatchProcessingTableViewModel]?) {
  3092. let batchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  3093. batchWindowController.window?.makeKeyAndOrderFront("")
  3094. if files?.count != 0 {
  3095. batchWindowController.inputData = files ?? []
  3096. }
  3097. if fileModels?.count != 0 {
  3098. batchWindowController.inputDataModel = fileModels ?? []
  3099. }
  3100. batchWindowController.type = type
  3101. batchWindowController.inputSubType = subType
  3102. }
  3103. }
  3104. //MARK: - 代理方法
  3105. //MARK: - NSSplitViewDelegate
  3106. extension KMMainViewController: NSSplitViewDelegate {
  3107. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  3108. if splitView == infoContendSplitView {
  3109. return subview.isEqual(to: infoSplitCenterView) == false
  3110. }
  3111. return false
  3112. }
  3113. func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool {
  3114. if splitView == infoContendSplitView {
  3115. if(subview.isEqual(to: infoSplitLeftView)) {
  3116. } else if(subview.isEqual(to: infoSplitRightView)) {
  3117. }
  3118. }
  3119. return false
  3120. }
  3121. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  3122. if splitView == infoContendSplitView {
  3123. return splitView == infoContendSplitView
  3124. } else if splitView == pdfSplitView {
  3125. return splitView == pdfSplitView
  3126. }
  3127. return false
  3128. }
  3129. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  3130. if splitView == infoContendSplitView {
  3131. if dividerIndex == 0 {
  3132. if viewManager.showDisplayView {
  3133. return MIN_SIDE_PANE_WIDTH.doubleValue
  3134. }
  3135. return infoContendSplitView.bounds.width/2
  3136. } else if (dividerIndex == 1) {
  3137. return proposedMaximumPosition - MIN_SIDE_PANE_WIDTH.doubleValue
  3138. }
  3139. }
  3140. return proposedMaximumPosition
  3141. }
  3142. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  3143. if(splitView == infoContendSplitView && dividerIndex == 0) {
  3144. return proposedMinimumPosition + MIN_SIDE_PANE_WIDTH.doubleValue
  3145. }
  3146. return proposedMinimumPosition
  3147. }
  3148. func splitViewDidResizeSubviews(_ notification: Notification) {
  3149. let splitView = notification.object as? NSSplitView
  3150. if(splitView == infoContendSplitView) {
  3151. leftSplitViewResizeFinish()
  3152. if(newMwcFlags.settingUpWindow == false && self.view.window?.frameAutosaveName != nil) {
  3153. let leftWidth = infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) ? 0.0 : infoSplitLeftView.frame.width
  3154. UserDefaults.standard.set(leftWidth, forKey: CPDFViewLeftSidePaneWidthKey)
  3155. UserDefaults.standard.synchronize()
  3156. }
  3157. if listView.isEditing() == false {
  3158. updateAnnotationsPopWinodwFrame()
  3159. } else {
  3160. updateContentEditPopWinodwFrame()
  3161. }
  3162. }
  3163. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(splitViewResizeFinish), object: nil)
  3164. self.perform(#selector(splitViewResizeFinish), with: nil, afterDelay: 0.15)
  3165. }
  3166. @objc func leftSplitViewResizeFinish() {
  3167. botaViewController?.changeLeftSideBounds()
  3168. }
  3169. @objc func splitViewResizeFinish() {
  3170. if infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) {
  3171. if viewManager.pdfSideBarType != .none {
  3172. viewManager.pdfSideBarType = .none
  3173. sideBarController?.reloadBOTAData()
  3174. }
  3175. }
  3176. }
  3177. }
  3178. //MARK: - KMPDFSideBarControllerDelegate 左侧Sidebar代理
  3179. extension KMMainViewController: KMPDFSideBarControllerDelegate {
  3180. func kmPDFSideBarControllerDidSidebarTypeUpdated(_ view: KMPDFSideBarController) {
  3181. if viewManager.pdfSideBarType == .none {
  3182. toggleCloseLeftSide()
  3183. } else {
  3184. toggleOpenLeftSide(pdfSideBarType: viewManager.pdfSideBarType)
  3185. }
  3186. }
  3187. func kmPDFSideBarControllerDidGotoPage(_ view: KMPDFSideBarController, _ pageIndex: Int) {
  3188. listView.go(toPageIndex: pageIndex, animated: true)
  3189. }
  3190. }
  3191. //MARK: - KMPDFToolbarControllerDelegate 工具栏代理
  3192. extension KMMainViewController: KMPDFToolbarControllerDelegate {
  3193. //MARK: -ViewTools发生变化时调用
  3194. func kmPDFToolbarControllerDidViewToolsChanged(_ controller: KMPDFToolbarController) {
  3195. let viewToolsType = viewManager.viewToolsType
  3196. if viewToolsType == .Select {
  3197. listView.toolMode = .CNoteToolMode
  3198. } else if viewToolsType == .Scroll {
  3199. listView.toolMode = .CMoveToolMode
  3200. } else if viewToolsType == .Content_Selection {
  3201. listView.toolMode = .CSelectToolMode
  3202. } else if viewToolsType == .Magnify {
  3203. listView.toolMode = .CMagnifyToolMode
  3204. } else if viewToolsType == .AreaZoom {
  3205. listView.toolMode = .CSelectZoomToolMode
  3206. }
  3207. refreshToolbarViewHeightInfo()
  3208. }
  3209. //MARK: -一级工具栏状态发生变化时调用
  3210. func kmPDFToolbarControllerDidToolModeChanged(_ controller: KMPDFToolbarController) {
  3211. refreshToolbarViewHeightInfo()
  3212. toolbarViewModeChanged()
  3213. }
  3214. //MARK: -点击工具栏按钮
  3215. func kmPDFToolbarControllerDidToolbarItemClicked(_ controller: KMPDFToolbarController, _ itemIdentifier: String) {
  3216. print("toolbar点击", itemIdentifier)
  3217. if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_PageEdit_Identifier).contains(itemIdentifier) {
  3218. //MARK: -页面编辑
  3219. if itemIdentifier == KMPDFToolbar_PageEdit_InsertFile_Identifier {
  3220. pageEditViewController?.insertFromPDFAction()
  3221. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertBlank_Identifier {
  3222. pageEditViewController?.insertFromBlankAction()
  3223. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertClip_Identifier {
  3224. pageEditViewController?.insertFromClipboardAction()
  3225. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertScanner_Identifier {
  3226. pageEditViewController?.insertFromScannerAction()
  3227. } else if itemIdentifier == KMPDFToolbar_PageEdit_Extract_Identifier {
  3228. pageEditViewController?.extractPDFAction()
  3229. } else if itemIdentifier == KMPDFToolbar_PageEdit_Replace_Identifier {
  3230. pageEditViewController?.replacePDFAction()
  3231. } else if itemIdentifier == KMPDFToolbar_PageEdit_Split_Identifier {
  3232. pageEditViewController?.splitPDFAction()
  3233. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reverse_Identifier {
  3234. pageEditViewController?.reversePDFAction()
  3235. } else if itemIdentifier == KMPDFToolbar_PageEdit_LeftRotate_Identifier {
  3236. pageEditViewController?.rotatePageLeftAction()
  3237. } else if itemIdentifier == KMPDFToolbar_PageEdit_RightRotate_Identifier{
  3238. pageEditViewController?.rotatePageRightAction()
  3239. } else if itemIdentifier == KMPDFToolbar_PageEdit_Delete_Identifier {
  3240. pageEditViewController?.deletePageAction()
  3241. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reduce_Identifier {
  3242. pageEditViewController?.zoomOutPageAction()
  3243. if(pageEditViewController?.canZoomInPageSize() == false) {
  3244. toolbarManager.page_Increase_Property.isDisabled = true
  3245. } else {
  3246. toolbarManager.page_Increase_Property.isDisabled = false
  3247. }
  3248. if(pageEditViewController?.canZoomOutPageSize() == false) {
  3249. toolbarManager.page_Reduce_Property.isDisabled = true
  3250. } else {
  3251. toolbarManager.page_Reduce_Property.isDisabled = false
  3252. }
  3253. controller.refreshSecondToolbarItemsState()
  3254. } else if itemIdentifier == KMPDFToolbar_PageEdit_Increase_Identifier {
  3255. pageEditViewController?.zoomInPageAction()
  3256. if(pageEditViewController?.canZoomInPageSize() == false) {
  3257. toolbarManager.page_Increase_Property.isDisabled = true
  3258. } else {
  3259. toolbarManager.page_Increase_Property.isDisabled = false
  3260. }
  3261. if(pageEditViewController?.canZoomOutPageSize() == false) {
  3262. toolbarManager.page_Reduce_Property.isDisabled = true
  3263. } else {
  3264. toolbarManager.page_Reduce_Property.isDisabled = false
  3265. }
  3266. controller.refreshSecondToolbarItemsState()
  3267. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_oddPage_Identifier {
  3268. pageEditViewController?.thumbnailChoosePageStyle = .odd
  3269. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3270. toolbarManager.page_pageInfo_Property.creatable = false
  3271. controller.refreshSecondToolbarItemsState()
  3272. }
  3273. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_EvenPage_Identifier {
  3274. pageEditViewController?.thumbnailChoosePageStyle = .even
  3275. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3276. toolbarManager.page_pageInfo_Property.creatable = false
  3277. controller.refreshSecondToolbarItemsState()
  3278. }
  3279. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_PortraitPage_Identifier {
  3280. pageEditViewController?.thumbnailChoosePageStyle = .horizontal
  3281. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3282. toolbarManager.page_pageInfo_Property.creatable = false
  3283. controller.refreshSecondToolbarItemsState()
  3284. }
  3285. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_LandscapePage_Identifier {
  3286. pageEditViewController?.thumbnailChoosePageStyle = .vertical
  3287. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3288. toolbarManager.page_pageInfo_Property.creatable = false
  3289. controller.refreshSecondToolbarItemsState()
  3290. }
  3291. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_AllPage_Identifier {
  3292. pageEditViewController?.thumbnailChoosePageStyle = .allPage
  3293. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3294. toolbarManager.page_pageInfo_Property.creatable = false
  3295. controller.refreshSecondToolbarItemsState()
  3296. }
  3297. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_CustomPage_Identifier {
  3298. pageEditViewController?.thumbnailChoosePageStyle = .custom
  3299. toolbarManager.page_pageInfo_Property.text = nil
  3300. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  3301. toolbarManager.page_pageInfo_Property.creatable = true
  3302. }
  3303. controller.refreshSecondToolbarItemsState()
  3304. }
  3305. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Markup_Identifier).contains(itemIdentifier) {
  3306. //MARK: -Markup
  3307. if itemIdentifier == KMPDFToolbar_eye_Identifier {
  3308. self.showOrHideNotes()
  3309. }
  3310. if viewManager.subToolMode == .None {
  3311. viewManager.showRightSide = false
  3312. } else if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  3313. viewManager.showRightSide = true
  3314. } else if viewManager.subToolMode == .Stamp || viewManager.subToolMode == .Sign {
  3315. viewManager.showRightSide = true
  3316. }
  3317. self.refreshToolbarRightViewInfo()
  3318. if let currentSelect = listView.currentSelection {
  3319. if(currentSelect.selectionType() == .text) {
  3320. let annotationType: CAnnotationType = KMPDFViewManager.getValidPDFAnnotationType(viewManager.subToolMode)
  3321. if annotationType != .unkown {
  3322. listView.addAnnotation(with: annotationType, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  3323. }
  3324. }
  3325. }
  3326. toolbarViewModeChanged()
  3327. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Edit_Identifier).contains(itemIdentifier) {
  3328. //MARK: -编辑
  3329. if itemIdentifier == KMPDFToolbar_edit_addWatermark_Identifier {
  3330. showWatermarkController()
  3331. } else if itemIdentifier == KMPDFToolbar_edit_removeWatermark_Identifier {
  3332. removePDFWatermark()
  3333. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddWatermark_Identifier {
  3334. batchAddWatermark()
  3335. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveWatermark_Identifier {
  3336. batchRemoveWatermark()
  3337. } else if itemIdentifier == KMPDFToolbar_edit_addBG_Identifier {
  3338. showBackgroundController()
  3339. } else if itemIdentifier == KMPDFToolbar_edit_removeBG_Identifier {
  3340. removePDFBackground()
  3341. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddBG_Identifier {
  3342. batchAddBackground()
  3343. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveBG_Identifier {
  3344. batchRemoveBackground()
  3345. } else if itemIdentifier == KMPDFToolbar_edit_addHF_Identifier {
  3346. showHeaderFooterController()
  3347. } else if itemIdentifier == KMPDFToolbar_edit_removeHF_Identifier {
  3348. removeHeaderFooter()
  3349. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddHF_Identifier {
  3350. batchAddHeaderFooter()
  3351. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveHF_Identifier {
  3352. batchRemoveHeaderFooter()
  3353. } else if itemIdentifier == KMPDFToolbar_edit_addBates_Identifier {
  3354. showBatesController()
  3355. } else if itemIdentifier == KMPDFToolbar_edit_removeBates_Identifier {
  3356. removePDFBates()
  3357. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddBates_Identifier {
  3358. batchAddBates()
  3359. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveBates_Identifier {
  3360. batchRemoveBates()
  3361. } else if itemIdentifier == KMPDFToolbar_edit_crop_Identifier {
  3362. showCropController()
  3363. }
  3364. if viewManager.subToolMode == .None {
  3365. viewManager.showRightSide = false
  3366. } else if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  3367. viewManager.showRightSide = true
  3368. }
  3369. self.refreshToolbarRightViewInfo()
  3370. toolbarViewModeChanged()
  3371. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Form_Identifier).contains(itemIdentifier) {
  3372. //MARK: -Form表单
  3373. if itemIdentifier == KMPDFToolbar_form_HighlightFields_Identifier {
  3374. let value = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  3375. CPDFAnnotation.updateHighlightFormFiled(listView, highlightFormFiled: !value)
  3376. self.toolbarManager.refreshDefaultConfigItem()
  3377. } else if itemIdentifier == KMPDFToolbar_form_ShowName_Identifier {
  3378. listView.showFormFieldName = !listView.showFormFieldName
  3379. if listView.showFormFieldName {
  3380. toolbarManager.form_ShowName_Property.righticon = NSImage(named: "tick_Green")
  3381. } else {
  3382. toolbarManager.form_ShowName_Property.righticon = nil
  3383. }
  3384. listView.setNeedsDisplayForVisiblePages()
  3385. } else if itemIdentifier == KMPDFToolbar_form_ClearForm_Identifier {
  3386. listView.resetFormAnnotation()
  3387. } else if itemIdentifier == KMPDFToolbar_form_Align_Left_Identifier {
  3388. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .left)
  3389. listView.setNeedsDisplayForVisiblePages()
  3390. } else if itemIdentifier == KMPDFToolbar_form_Align_Hori_Identifier {
  3391. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .horizontally)
  3392. listView.setNeedsDisplayForVisiblePages()
  3393. } else if itemIdentifier == KMPDFToolbar_form_Align_Right_Identifier {
  3394. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .right)
  3395. listView.setNeedsDisplayForVisiblePages()
  3396. } else if itemIdentifier == KMPDFToolbar_form_Align_Top_Identifier {
  3397. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .top)
  3398. listView.setNeedsDisplayForVisiblePages()
  3399. } else if itemIdentifier == KMPDFToolbar_form_Align_Vert_Identifier {
  3400. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .vertical)
  3401. listView.setNeedsDisplayForVisiblePages()
  3402. } else if itemIdentifier == KMPDFToolbar_form_Align_Bottom_Identifier {
  3403. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .bottom)
  3404. listView.setNeedsDisplayForVisiblePages()
  3405. } else if itemIdentifier == KMPDFToolbar_form_Distribute_Vert_Identifier {
  3406. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .disVertical)
  3407. listView.setNeedsDisplayForVisiblePages()
  3408. } else if itemIdentifier == KMPDFToolbar_form_Distribute_Hori_Identifier {
  3409. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .disHorizontally)
  3410. listView.setNeedsDisplayForVisiblePages()
  3411. }
  3412. if itemIdentifier == KMPDFToolbar_form_HighlightFields_Identifier ||
  3413. itemIdentifier == KMPDFToolbar_form_ShowName_Identifier ||
  3414. itemIdentifier == KMPDFToolbar_form_ClearForm_Identifier ||
  3415. itemIdentifier == KMPDFToolbar_form_Align_Left_Identifier ||
  3416. itemIdentifier == KMPDFToolbar_form_Align_Hori_Identifier ||
  3417. itemIdentifier == KMPDFToolbar_form_Align_Right_Identifier ||
  3418. itemIdentifier == KMPDFToolbar_form_Align_Top_Identifier ||
  3419. itemIdentifier == KMPDFToolbar_form_Align_Vert_Identifier ||
  3420. itemIdentifier == KMPDFToolbar_form_Align_Bottom_Identifier ||
  3421. itemIdentifier == KMPDFToolbar_form_Distribute_Vert_Identifier ||
  3422. itemIdentifier == KMPDFToolbar_form_Distribute_Hori_Identifier {
  3423. } else {
  3424. toolbarViewModeChanged()
  3425. }
  3426. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Fill_Identifier).contains(itemIdentifier) {
  3427. //MARK: -填充
  3428. if viewManager.subToolMode == .None {
  3429. viewManager.showRightSide = false
  3430. } else if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  3431. viewManager.showRightSide = true
  3432. }
  3433. self.refreshToolbarRightViewInfo()
  3434. toolbarViewModeChanged()
  3435. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Convert_Identifier).contains(itemIdentifier) {
  3436. //MARK: -转档
  3437. if itemIdentifier == KMPDFToolbar_convert_word_Identifier {
  3438. self.showConvertWindow(.word)
  3439. } else if itemIdentifier == KMPDFToolbar_convert_ppt_Identifier {
  3440. self.showConvertWindow(.ppt)
  3441. } else if itemIdentifier == KMPDFToolbar_convert_RTF_Identifier {
  3442. self.showConvertWindow(.rtf)
  3443. } else if itemIdentifier == KMPDFToolbar_convert_Text_Identifier {
  3444. self.showConvertWindow(.text)
  3445. } else if itemIdentifier == KMPDFToolbar_convert_CSV_Identifier {
  3446. self.showConvertWindow(.csv)
  3447. } else if itemIdentifier == KMPDFToolbar_convert_excel_Identifier {
  3448. self.showConvertWindow(.excel)
  3449. } else if itemIdentifier == KMPDFToolbar_convert_HTML_Identifier {
  3450. self.showConvertWindow(.html)
  3451. } else if itemIdentifier == KMPDFToolbar_convert_Json_Identifier {
  3452. self.showConvertWindow(.json)
  3453. } else if itemIdentifier == KMPDFToolbar_convert_image_Identifier {
  3454. self.showConvertWindow(.jpeg)
  3455. } else if itemIdentifier == KMPDFToolbar_convert_imageToPDF_Identifier {
  3456. NSApplication.ShowImageToPDFWindow()
  3457. }
  3458. self.refreshToolbarRightViewInfo()
  3459. toolbarViewModeChanged()
  3460. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Protect_Identifier).contains(itemIdentifier) {
  3461. //MARK: -Protect
  3462. if itemIdentifier == KMPDFToolbar_protect_redact_Identifier {
  3463. toolbarViewModeChanged()
  3464. if viewManager.showRightSide == true {
  3465. viewManager.showRightSide = false
  3466. pdfToolbarController?.reloadRightToolsView()
  3467. toggleCloseRightSide()
  3468. }
  3469. } else if itemIdentifier == KMPDFToolbar_protect_redact_Property_Identifier {
  3470. self.showRedactProperty(readactAnnotation: nil)
  3471. toolbarViewModeChanged()
  3472. } else if itemIdentifier == KMPDFToolbar_protect_redact_Apply_Identifier {
  3473. toolbarViewModeChanged()
  3474. } else if itemIdentifier == KMPDFToolbar_protect_redact_Exit_Identifier {
  3475. toolbarViewModeChanged()
  3476. } else if itemIdentifier == KMPDFToolbar_protect_security_Identifier {
  3477. self.showSecureWindow()
  3478. } else if itemIdentifier == KMPDFToolbar_protect_removeSecurity_Identifier {
  3479. self.showRemoveSecureWindow()
  3480. } else if itemIdentifier == KMPDFToolbar_protect_digitalSign_Identifier {
  3481. self.toolbarViewModeChanged()
  3482. }
  3483. alertTipViewController.reloadAlertUIFrame()
  3484. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Tools_Identifier).contains(itemIdentifier) {
  3485. //MARK: -工具
  3486. if itemIdentifier == KMPDFToolbar_tools_compress_Identifier {
  3487. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  3488. model.password = listView.document.password ?? ""
  3489. self.showCompressController(model: model)
  3490. } else if itemIdentifier == KMPDFToolbar_tools_batch_compress_Identifier {
  3491. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  3492. model.password = listView.document.password ?? ""
  3493. self.showBatchWindow(type: .compress, files: [], fileModels: [model])
  3494. } else if itemIdentifier == KMPDFToolbar_tools_OCR_Identifier {
  3495. viewManager.showRightSide = !viewManager.showRightSide
  3496. self.refreshToolbarRightViewInfo()
  3497. } else if itemIdentifier == KMPDFToolbar_tools_merge_Identifier {
  3498. self.showMergeWindow(self.listView.document.password)
  3499. } else if itemIdentifier == KMPDFToolbar_tools_TTS_Identifier {
  3500. self.showTTSWindow()
  3501. } else if itemIdentifier == KMPDFToolbar_tools_extractImage_Identifier {
  3502. self.extractImageAction(num: 2)
  3503. } else if itemIdentifier == KMPDFToolbar_tools_AITools_Identifier {
  3504. self.viewManager.pdfSideBarType = .aiTools
  3505. self.sideBarController?.reloadData()
  3506. if let sideVC = self.sideBarController {
  3507. self.kmPDFSideBarControllerDidSidebarTypeUpdated(sideVC)
  3508. }
  3509. } else if itemIdentifier == KMPDFToolbar_tools_compare_side_Identifier {
  3510. self.beginCompareAction(1)
  3511. } else if itemIdentifier == KMPDFToolbar_tools_compare_Overlay_Identifier {
  3512. self.beginCompareAction(2)
  3513. } else if itemIdentifier == KMPDFToolbar_tools_batch_Identifier {
  3514. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  3515. model.password = listView.document.password ?? ""
  3516. self.showBatchWindow(type: .convertPDF, files: [], fileModels: [model])
  3517. }
  3518. if viewManager.subToolMode == .None {
  3519. viewManager.showRightSide = false
  3520. } else if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  3521. viewManager.showRightSide = true
  3522. }
  3523. self.refreshToolbarRightViewInfo()
  3524. toolbarViewModeChanged()
  3525. } else if itemIdentifier == KMPDFToolbar_ViewDisplay_Identifier {
  3526. //MARK: -Display
  3527. updatePDFDisplaySettingView()
  3528. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Right_Identifiers).contains(itemIdentifier) {
  3529. if(itemIdentifier == KMPDFToolbar_undo_Identifier) {
  3530. //MARK: -Undo
  3531. listView.undoManager?.undo()
  3532. } else if(itemIdentifier == KMPDFToolbar_redo_Identifier) {
  3533. //MARK: -Redo
  3534. listView.undoManager?.redo()
  3535. } else if(itemIdentifier == KMPDFToolbar_fileInfo_Identifier) {
  3536. self.showFileInfo()
  3537. } else if(itemIdentifier == KMPDFToolbar_share_PDF_Identifier ||
  3538. itemIdentifier == KMPDFToolbar_share_Flattened_Identifier ||
  3539. itemIdentifier == KMPDFToolbar_share_Original_Identifier) {
  3540. //MARK: -Share
  3541. if(itemIdentifier == KMPDFToolbar_share_PDF_Identifier) {
  3542. if let view = controller.findViewWith(KMPDFToolbar_share_Identifier) {
  3543. shareDocument(sender: view)
  3544. }
  3545. } else if(itemIdentifier == KMPDFToolbar_share_Flattened_Identifier) {
  3546. if let view = controller.findViewWith(KMPDFToolbar_share_Identifier) {
  3547. shareFlatten(sender: view)
  3548. }
  3549. } else if(itemIdentifier == KMPDFToolbar_share_Original_Identifier) {
  3550. if let view = controller.findViewWith(KMPDFToolbar_share_Identifier) {
  3551. shareOriginalPDF(sender: view)
  3552. }
  3553. }
  3554. } else if itemIdentifier == KMPDFToolbar_save_Identifier {
  3555. myDocument?.save(nil)
  3556. } else if itemIdentifier == KMPDFToolbar_print_Identifier {
  3557. self.showPrintWindow()
  3558. } else if itemIdentifier == KMPDFToolbar_tts_Identifier {
  3559. self.showTTSWindow()
  3560. } else if itemIdentifier == KMPDFToolbar_ppt_Identifier {
  3561. self.togglePresentation(nil)
  3562. } else if itemIdentifier == KMPDFToolbar_batch_Identifier {
  3563. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  3564. model.password = listView.document.password ?? ""
  3565. self.showBatchWindow(type: .convertPDF, files: [], fileModels: [model])
  3566. }
  3567. } else if itemIdentifier == KMPDFToolbar_PageEdit_Identifier {
  3568. //MARK: -页面编辑
  3569. if viewManager.isPageEditMode == true {
  3570. enterPageEditMode()
  3571. } else {
  3572. exitPageEditMode()
  3573. }
  3574. } else if itemIdentifier == KMPDFToolbar_rightView_Identifier {
  3575. //MARK: -属性栏
  3576. self.refreshToolbarRightViewInfo()
  3577. } else {
  3578. print("click else")
  3579. }
  3580. refreshToolbarViewHeightInfo()
  3581. refreshRightSide()
  3582. }
  3583. func kmPDFToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  3584. }
  3585. func kmPDFToolbarControllerDidSelectTextDidChange(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  3586. }
  3587. func kmPDFToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  3588. if view.properties == toolbarManager.page_pageInfo_Property {
  3589. if viewManager.isPageEditMode == true {
  3590. let fileAttribute = KMNFileAttribute()
  3591. fileAttribute.password = listView.document?.password ?? ""
  3592. fileAttribute.pdfDocument = listView.document
  3593. fileAttribute.filePath = listView.document?.documentURL.path ?? ""
  3594. fileAttribute.bAllPage = false
  3595. fileAttribute.pagesType = .PagesString
  3596. fileAttribute.pagesString = view.properties.text ?? ""
  3597. let fetchSelectPages = fileAttribute.fetchSelectPages()
  3598. if (fetchSelectPages.isEmpty) {
  3599. let alert = NSAlert()
  3600. alert.alertStyle = .critical
  3601. alert.messageText = String(format: "%@ %@", fileAttribute.filePath.lastPathComponent, KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  3602. alert.runModal()
  3603. toolbarManager.page_pageInfo_Property.text = ""
  3604. controller.refreshSecondToolbarItemsState()
  3605. } else {
  3606. var tIndexPaths: Set<IndexPath> = []
  3607. for i in 0 ..< fetchSelectPages.count {
  3608. tIndexPaths.insert(IndexPath(item: (fetchSelectPages[i] - 1), section: 0))
  3609. }
  3610. pageEditViewController?.selectionIndexPaths = tIndexPaths
  3611. }
  3612. }
  3613. }
  3614. }
  3615. func kmPDFToolbarControllerDidExitPageEditMode(_ controller: KMPDFToolbarController) {
  3616. exitPageEditMode()
  3617. }
  3618. }
  3619. //MARK: - KMRightSideControllerDelegate右边属性栏代理
  3620. extension KMMainViewController: KMRightSideControllerDelegate {
  3621. func kmRightSideControllerRotateLeft(_ annotations: [CPDFStampAnnotation], withPDFView pdfView: CPDFListView?) {
  3622. CPDFStampAnnotation.rotateLeft(annotations, withPDFView: pdfView)
  3623. (undoManager?.prepare(withInvocationTarget: self) as AnyObject).kmRightSideControllerRotateRight(annotations, withPDFView: pdfView)
  3624. }
  3625. func kmRightSideControllerRotateRight(_ annotations: [CPDFStampAnnotation], withPDFView pdfView: CPDFListView?) {
  3626. CPDFStampAnnotation.rotateRight(annotations, withPDFView: pdfView)
  3627. (undoManager?.prepare(withInvocationTarget: self) as AnyObject).kmRightSideControllerRotateLeft(annotations, withPDFView: pdfView)
  3628. }
  3629. func kmRightSideControllerDidContendVCUpdated(_ controller: KMRightSideController) {
  3630. //MARK: -Crop
  3631. if (controller.contentViewController is KMCropPropertyController) {
  3632. if let cropVC = rightSideController?.edit_cropController {
  3633. cropVC.pdfView = controller.pdfView
  3634. if cropVC.delegate == nil {
  3635. cropVC.delegate = cropController
  3636. }
  3637. cropVC.reloadData()
  3638. }
  3639. }
  3640. reloadPopUIWindow()
  3641. }
  3642. func kmRightSideControllerOCRShowTypeChange(_ controller: KMRightSideController, _ type: KMOCRShowType) {
  3643. if type == .area {
  3644. self.listView.toolMode = .COCRToolMode
  3645. self.showOCRToolAlert(KMLocalizedString("Please select the area that needs OCR and click the “OCR” button to start"))
  3646. } else {
  3647. self.listView.toolMode = .CTextToolMode
  3648. }
  3649. }
  3650. func kmRightSideControllerOCRDoneAction(_ controller: KMRightSideController, _ model: KMOCRModel) {
  3651. if model.showType == .area {
  3652. self.convertOCRSaveAsTXT(text: model.text)
  3653. } else {
  3654. if model.saveAsPDF {
  3655. if model.saveType == .PDF {
  3656. } else {
  3657. }
  3658. } else {
  3659. }
  3660. self.convertOCR(document: self.listView.document, model: model)
  3661. }
  3662. }
  3663. //测量设置界面
  3664. func kmRightSideControllerShowMeasureSetting(_ controller: KMRightSideController) {
  3665. showMeasureSettingWindow()
  3666. }
  3667. //裁剪模块
  3668. func kmRightSideControllerDidRevertCropInfo(_ controller: KMRightSideController) {
  3669. if let cropVC = cropController {
  3670. cropVC.reloadSelectionRect()
  3671. self.kmCropControllerDidChangedSelectionOrMagnification(cropVC)
  3672. }
  3673. }
  3674. }
  3675. //MARK: - KMNDisplayViewControllerDelegate代理
  3676. extension KMMainViewController: KMNDisplayViewControllerDelegate {
  3677. //Display Mode
  3678. func displayViewControllerDidDisplayModeChanged(_ controller: KMNDisplayViewController) {
  3679. listView.layoutDocumentView()
  3680. }
  3681. //阅读模式
  3682. func displayViewControllerDidReadModeUpdated(_ controller: KMNDisplayViewController) {
  3683. if viewManager.isPDFReadMode {
  3684. openPDFReadMode()
  3685. } else {
  3686. exitPDFReadMode()
  3687. }
  3688. }
  3689. //PPT
  3690. func displayViewControllerDidGotoSlideShow(_ controller: KMNDisplayViewController) {
  3691. togglePresentation(nil)
  3692. }
  3693. //SplitView
  3694. func displayViewControllerDidSplitModeChanged(_ controller: KMNDisplayViewController) {
  3695. reloadPDFSplitInfo()
  3696. }
  3697. func displayViewControllerDidSplitFileChanged(_ controller: KMNDisplayViewController) {
  3698. splitPDFController?.reloadData()
  3699. }
  3700. func displayViewControllerDidToolbarStateChanged(_ controller: KMNDisplayViewController) {
  3701. splitPDFController?.refreshToolbarState()
  3702. reloadPDFPageNumberToolbar()
  3703. }
  3704. }
  3705. //MARK: - PPT
  3706. extension KMMainViewController: KMInteractionProviderProtocol {
  3707. func providerContentView(fullScreenWindow: NSWindow, inset: CGFloat) -> NSView? {
  3708. if(interactionMode == .presentation) {
  3709. if listView.presentationDrawView == nil {
  3710. listView.createPresentationDraw()
  3711. }
  3712. presentationTopViewController = KMPresentationTopViewController.init(nibName: "KMPresentationTopViewController", bundle: nil)
  3713. presentationTopViewController?.pdfView = listView
  3714. presentationTopViewController?.delegate = self
  3715. presentationTopViewController?.isSelectionPre = true
  3716. listView.isPresentationMode = true
  3717. presentationTopViewController?.view.frame = CGRect(x: 0, y: (fullScreenWindow.contentView?.bounds.height ?? 0) - 42, width: fullScreenWindow.contentView?.bounds.width ?? 0, height: 42)
  3718. if((presentationTopViewController) != nil) {
  3719. fullScreenWindow.contentView?.addSubview(presentationTopViewController!.view)
  3720. }
  3721. } else {
  3722. listView.frame = NSInsetRect(fullScreenWindow.contentView?.bounds ?? .zero, 0, 0)
  3723. }
  3724. fullScreenWindow.contentView?.addSubview(listView)
  3725. if(interactionMode == .presentation) {
  3726. let frame = fullScreenWindow.frame
  3727. listView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height-42)
  3728. listView.autoresizingMask = [.width, .maxYMargin]
  3729. }
  3730. return view
  3731. }
  3732. @objc func willEnterInteractionModeNotification(_ sender: Notification) {
  3733. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  3734. return
  3735. }
  3736. let interactionMode = sender.userInfo?[NSWindow.UserInfo.interactionModeKey] as? KMInteractionMode
  3737. if interactionMode == .presentation {
  3738. let backgroundColor = NSColor.black
  3739. let level = UserDefaults.standard.bool(forKey: "SKUseNormalLevelForPresentationKey") ? NSWindow.Level.normal : NSWindow.Level.popUpMenu
  3740. let wasInteractionMode = self.interactionMode
  3741. if wasInteractionMode == .normal {
  3742. self.savedNormalSetup.setDictionary(self.currentPDFSettings() as! [AnyHashable : Any])
  3743. }
  3744. if wasInteractionMode == .legacyFullScreen {
  3745. self.enterPresentationMode()
  3746. self.listView.frame = (self.view.window?.contentView?.bounds)!
  3747. self.view.window?.contentView?.addSubview(listView)
  3748. // self.view.window?.backgroundColor = backgroundColor
  3749. self.view.window?.level = level
  3750. self.listView.layoutDocumentView()
  3751. self.listView.requiresDisplay()
  3752. }
  3753. } else {
  3754. KMPrint("2")
  3755. }
  3756. }
  3757. @objc func didEnterInteractionModeNotification(_ sender: Notification) {
  3758. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  3759. return
  3760. }
  3761. if self.interactionMode == .presentation {
  3762. self.listView.layoutDocumentView()
  3763. self.listView.requiresDisplay()
  3764. }
  3765. }
  3766. @objc func willShowFullScreenNotification(_ sender: Notification) {
  3767. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  3768. return
  3769. }
  3770. if self.interactionMode == .presentation {
  3771. let view = self.view.window?.firstResponder as? NSView
  3772. if let data = view?.isDescendant(of: self.pdfSplitView), data {
  3773. self.view.window?.makeFirstResponder(nil)
  3774. }
  3775. }
  3776. }
  3777. @objc func didShowFullScreenNotification(_ sender: Notification) {
  3778. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  3779. return
  3780. }
  3781. if self.interactionMode == .presentation {
  3782. self.enterPresentationMode()
  3783. }
  3784. }
  3785. }
  3786. // MARK: -KMPresentationTopViewControllerDelegate (幻灯片)
  3787. extension KMMainViewController: KMPresentationTopViewControllerDelegate {
  3788. func presentationTopViewExit(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  3789. self.exitFullScreen()
  3790. }
  3791. func presentationTopViewClear(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  3792. listView.presentationDrawView?.clear()
  3793. }
  3794. func presentationTopViewUndo(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  3795. let presentationDrawView = listView.presentationDrawView
  3796. if presentationDrawView?.canUndo() == true {
  3797. presentationDrawView?.undo()
  3798. }
  3799. }
  3800. func presentationTopViewType(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton, isSeletion: Bool) {
  3801. listView.isPresentationMode = isSeletion
  3802. if listView.isEnterPresentationDrawMode() == true {
  3803. listView.exitPresentationDrawMode()
  3804. }
  3805. }
  3806. func presentationTopViewDrawColor(_ presentationTopViewController: KMPresentationTopViewController, withView: NSView,color:NSColor?) {
  3807. if color == nil{
  3808. listView.exitPresentationDrawMode()
  3809. } else {
  3810. if listView.isEnterPresentationDrawMode() == false {
  3811. listView.enterPresentationDrawMode()
  3812. }
  3813. listView.changePresentationDrawModelColor(color)
  3814. }
  3815. }
  3816. }
  3817. //MARK: - KMSplitPDFViewControllerDelegate SplitPDFView分屏视图
  3818. extension KMMainViewController: KMSplitPDFViewControllerDelegate {
  3819. func splitPDFViewControllerDidUpdateFilePath(_ controller: KMSplitPDFViewController) {
  3820. displaySettingController?.reloadData()
  3821. }
  3822. func splitPDFViewControllerDidUpdatePDFScale(_ controller: KMSplitPDFViewController) {
  3823. if let scaleFactor = controller.pdfView?.scaleFactor {
  3824. listView.scaleFactor = scaleFactor
  3825. }
  3826. }
  3827. func splitPDFViewControllerDidUpdatePDFPageIndex(_ controller: KMSplitPDFViewController) {
  3828. if let pageIndex = controller.pdfView?.currentPageIndex {
  3829. listView.go(toPageIndex: pageIndex, animated: false)
  3830. }
  3831. }
  3832. }
  3833. //MARK: - Edit编辑相关代理
  3834. extension KMMainViewController: KMEditToolbarViewDelegate {
  3835. func kmEditToolbarViewDidUpdateMode(_ view: KMEditToolbarView) {
  3836. if view.editType == .watermark {
  3837. watermarkViewController?.editSubType = editToolbarView?.editSubType ?? .template
  3838. watermarkViewController?.reloadData()
  3839. watermarkViewController?.loadData()
  3840. } else if view.editType == .background {
  3841. backgroundViewController?.editSubType = editToolbarView?.editSubType ?? .template
  3842. backgroundViewController?.resetUI()
  3843. backgroundViewController?.reloadData()
  3844. } else if view.editType == .header_Footer {
  3845. headerFooterViewController?.editSubType = editToolbarView?.editSubType ?? .template
  3846. headerFooterViewController?.resetUI()
  3847. headerFooterViewController?.reloadData()
  3848. } else if view.editType == .bates {
  3849. batesViewController?.editSubType = editToolbarView?.editSubType ?? .template
  3850. batesViewController?.resetUI()
  3851. batesViewController?.reloadData()
  3852. }
  3853. }
  3854. func kmEditToolbarViewDidChooseBatch(_ view: KMEditToolbarView) {
  3855. if view.editType == .watermark {
  3856. self.showBatchWindow(type: .watermark, files: nil)
  3857. } else if view.editType == .background {
  3858. self.showBatchWindow(type: .background, files: nil)
  3859. } else if view.editType == .header_Footer {
  3860. self.showBatchWindow(type: .headerAndFooter, files: nil)
  3861. } else if view.editType == .bates {
  3862. self.showBatchWindow(type: .batesNumber, files: nil)
  3863. }
  3864. }
  3865. func kmEditToolbarViewDidChooseApply(_ view: KMEditToolbarView) {
  3866. let pageInfo = view.pageRangeSelectView.getSelectedPageIndex(listView.document)
  3867. let pageIndex = pageInfo.0
  3868. let isCurrentPage = pageInfo.1
  3869. if pageIndex.isEmpty {
  3870. let alert = NSAlert()
  3871. alert.alertStyle = .critical
  3872. alert.messageText = KMLocalizedString("Invalid page range or the page number is out of range. Please try again.")
  3873. alert.runModal()
  3874. return
  3875. }
  3876. var pageString = view.pageRangeSelectView.getSelectedPageString(listView.document, pageIndex)
  3877. if isCurrentPage {
  3878. pageString = String(format: "%ld", listView.currentPageIndex)
  3879. }
  3880. if view.editType == .watermark {
  3881. if let model = watermarkViewController?.currentWatermarkData {
  3882. let watermark = KMWatermarkModel.returnWaterMarkWith(model, listView.document)
  3883. watermark.pageString = pageString
  3884. listView.document.addWatermark(watermark)
  3885. listView.layoutDocumentView()
  3886. }
  3887. exitEditToolbarView()
  3888. } else if view.editType == .background {
  3889. if let model = backgroundViewController?.backgroundModel {
  3890. if let background = listView.document.background() {
  3891. KMBackgroundManager.defaultManager.updateBackground(background, withModel: model)
  3892. background.pageString = pageString
  3893. background.update()
  3894. listView.document?.refreshPageData()
  3895. listView.layoutDocumentView()
  3896. }
  3897. }
  3898. exitEditToolbarView()
  3899. } else if view.editType == .header_Footer {
  3900. if let model = headerFooterViewController?.headerFooterModel {
  3901. if let headerFooter = listView.document.headerFooter() {
  3902. KMHeaderFooterManager.defaultManager.updateCPDFHeaderFooter(headerFooter, withModel: model, Int(listView.document.pageCount))
  3903. headerFooter.pageString = pageString
  3904. headerFooter.update()
  3905. listView.document?.refreshPageData()
  3906. listView.layoutDocumentView()
  3907. }
  3908. }
  3909. exitEditToolbarView()
  3910. } else if view.editType == .bates {
  3911. if let model = batesViewController?.batesModel {
  3912. if let bates = listView.document.bates() {
  3913. KMBatesManager.defaultManager.updateCPDFBates(bates, withModel: model, Int(listView.document.pageCount))
  3914. bates.pageString = pageString
  3915. bates.update()
  3916. listView.document?.refreshPageData()
  3917. listView.layoutDocumentView()
  3918. }
  3919. }
  3920. exitEditToolbarView()
  3921. }
  3922. }
  3923. func kmEditToolbarViewDidChooseExit(_ view: KMEditToolbarView) {
  3924. if view.applyEnable {
  3925. let alert = NSAlert()
  3926. if view.editType == .watermark {
  3927. alert.messageText = NSLocalizedString("There are unapplied Watermark settings, do you want to apply them?", comment: "")
  3928. } else if view.editType == .background {
  3929. alert.messageText = NSLocalizedString("There are unapplied Background settings, do you want to apply them?", comment: "")
  3930. } else if view.editType == .header_Footer {
  3931. alert.messageText = NSLocalizedString("There are unapplied Header & Footer settings, do you want to apply them?", comment: "")
  3932. } else if view.editType == .bates {
  3933. alert.messageText = NSLocalizedString("There are unapplied Bates settings, do you want to apply them?", comment: "")
  3934. }
  3935. alert.informativeText = NSLocalizedString("If not, the changes will be lost.", comment: "")
  3936. alert.addButton(withTitle: NSLocalizedString("Apply", comment: ""))
  3937. alert.addButton(withTitle: NSLocalizedString("Don't Apply", comment: ""))
  3938. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  3939. let result = alert.runModal()
  3940. if (result == .alertFirstButtonReturn) {
  3941. self.kmEditToolbarViewDidChooseApply(view)
  3942. } else if (result == .alertSecondButtonReturn) {
  3943. //"Don't Apply"
  3944. exitEditToolbarView()
  3945. } else if (result == .alertThirdButtonReturn) {
  3946. //Cancel
  3947. }
  3948. } else {
  3949. exitEditToolbarView()
  3950. }
  3951. }
  3952. }
  3953. //MARK: - KMCropControllerDelegate 裁剪相关代理
  3954. extension KMMainViewController: KMCropControllerDelegate {
  3955. func kmCropControllerDidCrop(_ controller: KMCropController, _ cropRect: CGRect, _ view: KMPageRangeSelectView) {
  3956. let rangeSelectResult = view.getSelectedPageIndex(listView.document)
  3957. let indexs = rangeSelectResult.0
  3958. let isCurrentPage = rangeSelectResult.1
  3959. var uIndexs: [UInt] = []
  3960. for index in indexs {
  3961. if index > 0 {
  3962. uIndexs.append(UInt(index-1))
  3963. }
  3964. }
  3965. if isCurrentPage {
  3966. uIndexs = [UInt(listView.currentPageIndex)]
  3967. }
  3968. cropPages(atIndexs: uIndexs, to: [cropRect])
  3969. removeCropController()
  3970. viewManager.subToolMode = .None
  3971. if let toolbarVC = self.pdfToolbarController {
  3972. toolbarVC.refreshSecondToolbarItemsState()
  3973. self.kmPDFToolbarControllerDidToolbarItemClicked(toolbarVC, KMPDFToolbar_edit_crop_Identifier)
  3974. }
  3975. }
  3976. func kmCropControllerDidCropSeparate(_ controller: KMCropController, _ view: KMPageRangeSelectView) {
  3977. let rangeSelectResult = view.getSelectedPageIndex(listView.document)
  3978. let indexs = rangeSelectResult.0
  3979. let isCurrentPage = rangeSelectResult.1
  3980. var rectArray: Array<NSRect> = []
  3981. var uIndexs: [UInt] = []
  3982. for index in indexs {
  3983. if index > 0 {
  3984. uIndexs.append(UInt(index-1))
  3985. let page = self.listView.document.page(at: UInt(index-1))
  3986. let rect = KMCropTools.getPageForegroundBox(page!)
  3987. rectArray.append(rect)
  3988. }
  3989. }
  3990. if isCurrentPage {
  3991. uIndexs = [UInt(listView.currentPageIndex)]
  3992. }
  3993. cropPages(atIndexs: uIndexs, to: rectArray)
  3994. removeCropController()
  3995. viewManager.subToolMode = .None
  3996. if let toolbarVC = self.pdfToolbarController {
  3997. toolbarVC.refreshSecondToolbarItemsState()
  3998. self.kmPDFToolbarControllerDidToolbarItemClicked(toolbarVC, KMPDFToolbar_edit_crop_Identifier)
  3999. }
  4000. }
  4001. func kmCropControllerDidCropAuto(_ controller: KMCropController, _ view: KMPageRangeSelectView) {
  4002. let rangeSelectResult = view.getSelectedPageIndex(listView.document)
  4003. let indexs = rangeSelectResult.0
  4004. let isCurrentPage = rangeSelectResult.1
  4005. var uIndexs: [UInt] = []
  4006. for index in indexs {
  4007. if index > 0 {
  4008. uIndexs.append(UInt(index-1))
  4009. }
  4010. }
  4011. if isCurrentPage {
  4012. uIndexs = [UInt(listView.currentPageIndex)]
  4013. }
  4014. auto_cropPagesWhiteMargin(uIndexs)
  4015. removeCropController()
  4016. viewManager.subToolMode = .None
  4017. if let toolbarVC = self.pdfToolbarController {
  4018. toolbarVC.refreshSecondToolbarItemsState()
  4019. self.kmPDFToolbarControllerDidToolbarItemClicked(toolbarVC, KMPDFToolbar_edit_crop_Identifier)
  4020. }
  4021. }
  4022. func kmCropControllerDidChangedSelectionOrMagnification(_ controller: KMCropController) {
  4023. if let cropVC = rightSideController?.edit_cropController {
  4024. if cropVC.pdfView != controller.pdfView {
  4025. cropVC.pdfView = controller.pdfView
  4026. }
  4027. cropVC.cropSeparateOn = false
  4028. cropVC.cropAutoOn = false
  4029. cropVC.reloadData()
  4030. }
  4031. }
  4032. }
  4033. //MARK: - KMWatermarkControllerDelegate 水印相关代理
  4034. extension KMMainViewController: KMWatermarkControllerDelegate {
  4035. func kmWatermarkControllerDidUpdateMode(_ view: KMWatermarkController) {
  4036. editToolbarView?.editSubType = view.editSubType
  4037. editToolbarView?.reloadData()
  4038. }
  4039. func kmWatermarkControllerDidWatermarkUpdated(_ view: KMWatermarkController) {
  4040. var applyEnable = true
  4041. if let model = view.currentWatermarkData {
  4042. if model.watermarkType == .text {
  4043. if model.text == nil || model.text?.count == 0 {
  4044. applyEnable = false
  4045. }
  4046. } else if model.watermarkType == .image {
  4047. if model.imagePath == nil {
  4048. applyEnable = false
  4049. }
  4050. }
  4051. } else {
  4052. applyEnable = false
  4053. }
  4054. editToolbarView?.applyEnable = applyEnable
  4055. editToolbarView?.reloadData()
  4056. }
  4057. }
  4058. //MARK: - KMBackgroundControllerDelegate 背景代理
  4059. extension KMMainViewController: KMBackgroundControllerDelegate {
  4060. func kmBackgroundControllerDidUpdateMode(_ view: KMBackgroundController) {
  4061. editToolbarView?.editSubType = view.editSubType
  4062. }
  4063. func kmBackgroundControllerDidBGDataUpdated(_ view: KMBackgroundController) {
  4064. var applyEnable = true
  4065. if let model = view.backgroundModel {
  4066. if model.type == .image {
  4067. if model.imagePath == nil {
  4068. applyEnable = false
  4069. }
  4070. }
  4071. } else {
  4072. applyEnable = false
  4073. }
  4074. editToolbarView?.applyEnable = applyEnable
  4075. editToolbarView?.reloadData()
  4076. }
  4077. }
  4078. //MARK: - KMHeaderFooterControllerDelegate 页眉页脚代理
  4079. extension KMMainViewController: KMHeaderFooterControllerDelegate {
  4080. func kmHeaderFooterControllerDidUpdateModeType(_ view: KMHeaderFooterController) {
  4081. editToolbarView?.editSubType = view.editSubType
  4082. }
  4083. func kmHeaderFooterControllerDidModelDataUpdated(_ view: KMHeaderFooterController) {
  4084. var applyEnable = true
  4085. if let model = view.headerFooterModel {
  4086. if model.topLeftString.count == 0 && model.topCenterString.count == 0 && model.topRightString.count == 0 &&
  4087. model.bottomLeftString.count == 0 && model.bottomCenterString.count == 0 && model.bottomRightString.count == 0 {
  4088. applyEnable = false
  4089. }
  4090. } else {
  4091. applyEnable = false
  4092. }
  4093. editToolbarView?.applyEnable = applyEnable
  4094. editToolbarView?.reloadData()
  4095. }
  4096. }
  4097. //MARK: - KMBatesControllerDelegate Bates贝茨码代理
  4098. extension KMMainViewController: KMBatesControllerDelegate {
  4099. func kmBatesControllerDidUpdateMode(_ view: KMBatesController) {
  4100. editToolbarView?.editSubType = view.editSubType
  4101. editToolbarView?.reloadData()
  4102. }
  4103. func kmBatesControllerDidModelDataUpdated(_ view: KMBatesController) {
  4104. var applyEnable = true
  4105. if let model = view.batesModel {
  4106. if model.topLeftString.count == 0 && model.topCenterString.count == 0 && model.topRightString.count == 0 &&
  4107. model.bottomLeftString.count == 0 && model.bottomCenterString.count == 0 && model.bottomRightString.count == 0 {
  4108. applyEnable = false
  4109. }
  4110. } else {
  4111. applyEnable = false
  4112. }
  4113. editToolbarView?.applyEnable = applyEnable
  4114. editToolbarView?.reloadData()
  4115. }
  4116. }
  4117. //MARK: - CPDFViewDelegate PDFView代理
  4118. extension KMMainViewController: CPDFViewDelegate,CPDFListViewDelegate {
  4119. func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
  4120. sideBarController?.reloadPageTurnerData()
  4121. documentLoadComplete()
  4122. }
  4123. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  4124. sideBarController?.reloadPageTurnerData()
  4125. botaViewController?.currentPageDidChangedAction(listView: listView)
  4126. //分屏视图
  4127. reloadPDFPageNumberToolbar()
  4128. if viewManager.splitSyncScroll {
  4129. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  4130. splitPDFController?.outPDFFirst = true
  4131. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  4132. var index = pdfView.currentPageIndex
  4133. if index > document.pageCount {
  4134. index = Int(splitPDFView.document.pageCount - 1)
  4135. }
  4136. splitPDFView.go(toPageIndex: index, animated: false)
  4137. }
  4138. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  4139. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  4140. } else if splitPDFController?.outPDFFirst == true {
  4141. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  4142. var index = pdfView.currentPageIndex
  4143. if index > document.pageCount {
  4144. index = Int(splitPDFView.document.pageCount - 1)
  4145. }
  4146. splitPDFView.go(toPageIndex: index, animated: false)
  4147. }
  4148. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  4149. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  4150. }
  4151. }
  4152. //水印
  4153. updateEditModeDocumentWhenPageChanged()
  4154. //
  4155. }
  4156. func pdfViewScaleDidChanged(_ pdfView: CPDFView!) {
  4157. pdfToolbarController?.reloadSelectZoomView()
  4158. reloadPDFPageNumberToolbar()
  4159. reloadPopUIWindow()
  4160. //分屏视图
  4161. if viewManager.splitSyncScroll {
  4162. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  4163. splitPDFController?.outPDFFirst = true
  4164. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  4165. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  4166. } else if splitPDFController?.outPDFFirst == true {
  4167. if let splitPDFView = splitPDFController?.pdfView {
  4168. splitPDFView.scaleFactor = pdfView.scaleFactor
  4169. }
  4170. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  4171. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  4172. }
  4173. }
  4174. }
  4175. func pdfViewDidClick(onLink pdfView: CPDFView!, withURL url: String!) {
  4176. if let urlString = url, urlString == Store_Link {
  4177. //跳转订阅比较表
  4178. return
  4179. }
  4180. if url.hasPrefix("smb://") {
  4181. NSWorkspace.shared.openFile(url)
  4182. } else {
  4183. KMTools.openURL(urlString: url)
  4184. }
  4185. }
  4186. func pdfViewPerformURL(_ pdfView: CPDFView!, withContent content: String!) {
  4187. KMPrint("pdfViewPerformURL")
  4188. if content != nil {
  4189. NSWorkspace.shared.open(URL(string: content)!)
  4190. } else {
  4191. let alert = NSAlert()
  4192. alert.alertStyle = .critical
  4193. alert.informativeText = NSLocalizedString("The hyperlink is invalid.", comment: "")
  4194. alert.messageText = ""
  4195. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  4196. alert.runModal()
  4197. return
  4198. }
  4199. }
  4200. func pdfViewPerformPrint(_ pdfView: CPDFView!) {
  4201. KMPrint("pdfViewPerformPrint")
  4202. self.showPrintWindow()
  4203. }
  4204. func pdfViewPerformGo(toPage pdfView: CPDFView!) {
  4205. KMPrint("pdfViewPerformGo")
  4206. }
  4207. func pdfViewOpenPDF(_ pdfView: CPDFView!, forRemoteGoTo action: CPDFAction!) {
  4208. KMPrint("pdfViewOpenPDF")
  4209. }
  4210. func pdfViewPerformReset(_ pdfView: CPDFView!) {
  4211. KMPrint("pdfViewPerformReset")
  4212. pdfView.document?.resetForm()
  4213. }
  4214. func pdfViewEditingBlockDidChanged(_ pdfView: CPDFView!) {
  4215. KMPrint("pdfViewEditingBlockDidChanged")
  4216. }
  4217. func pdfViewAsBookBookmark() -> NSImage! {
  4218. return NSImage(named: "KMImageNameBookmarkIcon")!
  4219. }
  4220. //MARK: -编辑模块
  4221. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  4222. // 文本区块 选中文本已经变化
  4223. reloadPopUIWindow()
  4224. }
  4225. func pdfViewPDFSelectionAttributeDidChanged(_ pdfView: CPDFView!) {
  4226. reloadPopUIWindow()
  4227. }
  4228. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  4229. //编辑模块变化
  4230. rightSideController?.reloadDataWithPDFView(pdfView: (pdfView as! CPDFListView))
  4231. if pdfView is CPDFListView {
  4232. (pdfView as! CPDFListView).isEditImage = false
  4233. }
  4234. if let areas = pdfView.editingAreas(), areas.count > 0 {
  4235. if viewManager.showRightSide == false && SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  4236. viewManager.showRightSide = true
  4237. self.refreshToolbarRightViewInfo()
  4238. }
  4239. } else {
  4240. if viewManager.subToolMode == .None {
  4241. viewManager.showRightSide = false
  4242. self.refreshToolbarRightViewInfo()
  4243. }
  4244. }
  4245. reloadPopUIWindow()
  4246. }
  4247. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  4248. if editArea != nil && (editArea is CPDFEditImageArea){
  4249. self.listView.cropAreas = editArea as? CPDFEditImageArea
  4250. }
  4251. reloadPopUIWindow()
  4252. }
  4253. //编辑PDF 创建图片区域回调
  4254. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  4255. if (pdfView as! CPDFListView).isEditImage {
  4256. return
  4257. }
  4258. if listView.editingAreas().count > 0 {
  4259. pdfView.updateEditing([])
  4260. return
  4261. }
  4262. let panel = NSOpenPanel()
  4263. panel.allowsMultipleSelection = false
  4264. panel.allowedFileTypes = ["png","jpg"]
  4265. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  4266. if response == .OK {
  4267. var filePath = panel.url?.path
  4268. let image = NSImage.init(contentsOf: panel.url!)
  4269. //图片自适应范围
  4270. if image != nil {
  4271. var imageRect = rect
  4272. let imageSize = image!.size
  4273. var previewSize = rect.size
  4274. var isChangeSize = false
  4275. if previewSize.width == 0 && previewSize.height == 0 {
  4276. previewSize = CGSize(width: 500, height: 500)
  4277. isChangeSize = true
  4278. }
  4279. var scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  4280. if scale < 1 { // 大于 500
  4281. } else {
  4282. let wh = max(imageSize.width, imageSize.height)
  4283. if wh >= 72 {
  4284. scale = min(scale, 1)
  4285. } else {
  4286. scale = min(72 / imageSize.width, 72 / imageSize.height)
  4287. }
  4288. }
  4289. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  4290. if isChangeSize {
  4291. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  4292. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  4293. } else {
  4294. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  4295. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  4296. }
  4297. imageRect.size = newSize
  4298. let limitWidth = 1920.0
  4299. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  4300. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  4301. targetSize: CGSize(width: limitWidth, height: limitWidth),
  4302. maxSizeInBytes: 1024 * 1024 * 5,
  4303. targetCompression: 1.0)
  4304. }
  4305. //自适应page
  4306. let pageRect = self.listView.currentPage().bounds ?? .zero
  4307. if imageRect.width > pageRect.width ||
  4308. imageRect.height > pageRect.height {
  4309. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  4310. imageRect = CGRect(x: imageRect.origin.x,
  4311. y: imageRect.origin.y,
  4312. width: imageRect.width * pageScale,
  4313. height: imageRect.height * pageScale)
  4314. }
  4315. if imageRect.origin.x < 0 {
  4316. imageRect.origin.x = 5
  4317. }
  4318. if imageRect.origin.y < 0 {
  4319. imageRect.origin.y = 5
  4320. }
  4321. if imageRect.origin.x + imageRect.width > pageRect.width ||
  4322. imageRect.origin.y + imageRect.height > pageRect.height {
  4323. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  4324. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  4325. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  4326. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  4327. }
  4328. DispatchQueue.main.async {
  4329. self.listView.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  4330. }
  4331. }
  4332. }
  4333. }
  4334. }
  4335. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  4336. let areas = self.listView.km_EditingAreas()
  4337. if let area = areas.last {
  4338. if let data = area as? CPDFEditTextArea {
  4339. if let str = data.editTextAreaString(), str.isEmpty {
  4340. self.listView.remove(with: [area])
  4341. } else {
  4342. self.listView.updateEditing([])
  4343. return
  4344. }
  4345. }
  4346. }
  4347. var newRect = rect
  4348. if rect.size.equalTo(.zero) {
  4349. newRect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  4350. } else {
  4351. newRect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  4352. }
  4353. let fontSize = CPDFEditTextArea.defaultFontSize()
  4354. let fontColor = CPDFEditTextArea.defaultColor()
  4355. let fontAlign = CPDFEditTextArea.defaultFontAlignment()
  4356. NSColorPanel.shared.color = fontColor
  4357. let attri = CEditAttributes()
  4358. attri.cFont = CPDFFont(familyName: CPDFEditTextArea.defaultFontName(), fontStyle: CPDFEditTextArea.defaultFontStyle())
  4359. attri.fontColor = fontColor
  4360. attri.alignment = fontAlign
  4361. attri.fontSize = fontSize
  4362. attri.isBold = CPDFEditTextArea.defaultIsBold()
  4363. attri.isItalic = CPDFEditTextArea.defaultIsItality()
  4364. self.listView.createStringBounds(newRect, with: attri, page: page)
  4365. }
  4366. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  4367. rightSideController?.reloadEditingAreas()
  4368. toggleClosePopUIWindow()
  4369. }
  4370. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  4371. rightSideController?.reloadEditingAreas()
  4372. toggleClosePopUIWindow()
  4373. }
  4374. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  4375. rightSideController?.reloadEditingAreas()
  4376. reloadPopUIWindow()
  4377. }
  4378. func pdfViewEditingSelectCharDidChanged(_ pdfView: CPDFView!) {
  4379. rightSideController?.reloadEditingAreas()
  4380. reloadPopUIWindow()
  4381. }
  4382. func pdfViewEditingExitCropMode(_ pdfView: CPDFView!, forEditing editingArea: CPDFEditImageArea!) {
  4383. rightSideController?.reloadEditingAreas()
  4384. toggleClosePopUIWindow()
  4385. }
  4386. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  4387. let areas = self.listView.km_editingImageAreas()
  4388. if areas.count == 1 {
  4389. if let data = areas.first as? CPDFEditImageArea {
  4390. let updating = self.listView.editAreaBoundUpdating
  4391. if updating {
  4392. self.listView.editAreaBoundUpdating = false
  4393. }
  4394. }
  4395. }
  4396. }
  4397. func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  4398. if(editArea.isImageArea()) {
  4399. listView.cropAction()
  4400. }
  4401. }
  4402. //MARK: - 键盘事件
  4403. func pdfListViewKeyDownIsContinue(_ pdfListView: CPDFListView!, theEvent: NSEvent!) -> Bool {
  4404. let command = theEvent.modifierFlags.contains(.command)
  4405. let control = theEvent.modifierFlags.contains(.control)
  4406. let shift = theEvent.modifierFlags.contains(.shift)
  4407. KMPrint(theEvent.keyCode)
  4408. if self.listView.isEditing() == true {
  4409. if control && theEvent.keyCode == 11 { // ctr + b
  4410. self.listView.setEditingTextarea_Bold()
  4411. rightSideController?.reloadEditingAreas()
  4412. return false
  4413. } else if control && theEvent.keyCode == 34 { // ctr +i
  4414. self.listView.setEditingTextarea_Italic()
  4415. rightSideController?.reloadEditingAreas()
  4416. return false
  4417. } else if theEvent.keyCode == 36 { // enter
  4418. if self.listView.isCropMode {
  4419. self.listView.cropComfirmAction()
  4420. rightSideController?.reloadEditingAreas()
  4421. return false
  4422. }
  4423. } else if theEvent.keyCode == 53 { // Cancel
  4424. if self.listView.isCropMode {
  4425. self.listView.cropCancelAction()
  4426. rightSideController?.reloadEditingAreas()
  4427. return false
  4428. }
  4429. }
  4430. }
  4431. if (theEvent.keyCode == 11 && command) { // command + B [添加书签]
  4432. self.menuItemBookMarkClick_add(sender: NSMenuItem())
  4433. return false
  4434. } else if (command && control && theEvent.keyCode == 14) { // command + control + E [注释 橡皮擦]
  4435. return false
  4436. } else if (theEvent.keyCode == 123) { // 向左
  4437. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  4438. return false
  4439. } else {
  4440. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage()) {
  4441. self.listView.goToPreviousPage(nil)
  4442. return false
  4443. }
  4444. }
  4445. } else if (theEvent.keyCode == 126) { // 向上
  4446. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  4447. return false
  4448. } else {
  4449. if (self.listView.isContinousScroll()) {
  4450. return true
  4451. }
  4452. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToPreviousPage()) {
  4453. self.listView.goToPreviousPage(nil)
  4454. return false
  4455. }
  4456. }
  4457. } else if (theEvent.keyCode == 124) { // 向右
  4458. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  4459. return false
  4460. } else {
  4461. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage()) {
  4462. self.listView.goToNextPage(nil)
  4463. return false
  4464. }
  4465. }
  4466. } else if (theEvent.keyCode == 125) { // 向下
  4467. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  4468. return false
  4469. } else {
  4470. if (self.listView.isContinousScroll()) {
  4471. return true
  4472. }
  4473. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToNextPage()) {
  4474. self.listView.goToNextPage(nil)
  4475. return false
  4476. }
  4477. }
  4478. } else if (theEvent.keyCode == 36) {
  4479. if self.listView.shouAddEditAreaType() == .image || self.listView.shouAddEditAreaType() == .text {
  4480. if self.listView.isEditImage {
  4481. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  4482. }
  4483. }
  4484. } else if shift && theEvent.keyCode == 18 {
  4485. //Shift + 1
  4486. updatePDFViewToolsType(.Select)
  4487. } else if shift && theEvent.keyCode == 19 {
  4488. //Shift + 2
  4489. updatePDFViewToolsType(.Scroll)
  4490. } else if shift && theEvent.keyCode == 20 {
  4491. //Shift + 3
  4492. updatePDFViewToolsType(.Content_Selection)
  4493. } else if shift && theEvent.keyCode == 21 {
  4494. //Shift + 4
  4495. updatePDFViewToolsType(.Magnify)
  4496. } else if shift && theEvent.keyCode == 23 {
  4497. //Shift + 5
  4498. updatePDFViewToolsType(.AreaZoom)
  4499. } else if theEvent.keyCode == 53 {
  4500. //ESC
  4501. self.exitFullScreen()
  4502. if viewManager.isPDFReadMode {
  4503. self.exitPDFReadMode()
  4504. }
  4505. self.leftSideViewCancelSelect()
  4506. }
  4507. return true
  4508. }
  4509. func pdfListViewMenuValidate(_ pdfListView: CPDFListView!, menuItem: NSMenuItem!, isTakesEffect: UnsafeMutablePointer<ObjCBool>!) -> Bool {
  4510. guard let action = menuItem.action else {
  4511. isTakesEffect.pointee = false
  4512. return false
  4513. }
  4514. isTakesEffect.pointee = false
  4515. return false
  4516. }
  4517. //MARK: - CPDFListViewDelegate
  4518. func pdfListViewChangedToolMode(_ pdfListView: CPDFListView!, for toolMode: CToolMode) {
  4519. reloadPopUIWindow()
  4520. }
  4521. func pdfListViewChangedAnnotationType(_ pdfListView: CPDFListView!, for annotationType: CAnnotationType) {
  4522. if(annotationType == .unkown) {
  4523. toggleCloseRightSide()
  4524. if viewManager.subToolMode != .None {
  4525. viewManager.subToolMode = .None
  4526. pdfToolbarController?.cancelSelectedSecondToolbarItems(viewManager.toolMode)
  4527. pdfToolbarController?.refreshSecondToolbarItemsState()
  4528. }
  4529. cancelMeasureType()
  4530. } else if annotationType == .measureLine ||
  4531. annotationType == .measurePolyLine ||
  4532. annotationType == .measurePolyGon ||
  4533. annotationType == .measureSquare {
  4534. refreshMeasureInfo()
  4535. } else {
  4536. cancelMeasureType()
  4537. }
  4538. }
  4539. func pdfListViewChangeatioActiveAnnotations(_ pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!, isRightMenu: Bool) {
  4540. self.view.window?.makeFirstResponder(self.listView)
  4541. reloadPopUIWindow()
  4542. if isRightMenu {
  4543. } else if annotations.count > 0 {
  4544. if self.viewManager.isPDFReadMode {
  4545. toggleCloseRightSide()
  4546. } else {
  4547. let isMultiAnnotations = pdfListView.isMultiAnnotation(annotations)
  4548. if isMultiAnnotations == true {
  4549. viewManager.showRightSide = false
  4550. } else {
  4551. if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  4552. viewManager.showRightSide = true
  4553. }
  4554. }
  4555. refreshToolbarRightViewInfo()
  4556. }
  4557. } else if (annotations.count == 0){
  4558. if pdfListView.annotationType == .unkown {
  4559. viewManager.showRightSide = false
  4560. } else {
  4561. if self.viewManager.isPDFReadMode {
  4562. viewManager.showRightSide = false
  4563. } else {
  4564. }
  4565. }
  4566. refreshToolbarRightViewInfo()
  4567. }
  4568. self.refreshMeasureInfo()
  4569. //
  4570. pdfToolbarController?.pdfViewActiveAnnotationsChanged()
  4571. }
  4572. func pdfListViewMenu(forEvent pdfListView: CPDFListView!, for theEvent: NSEvent!, click menu: AutoreleasingUnsafeMutablePointer<NSMenu?>!, isMoveSelectAnno: Bool) {
  4573. toggleClosePopUIWindow()
  4574. var pagePoint = CGPoint.zero
  4575. let point = self.view.convert(theEvent.locationInWindow, from: nil)
  4576. let isShowPopUI:Bool = !SettingsManager.sharedInstance.showQuickActionBar
  4577. if let page = pdfListView.pageAndPoint(&pagePoint, for: theEvent, nearest: false) {
  4578. if view.window?.interactionMode == .presentation {
  4579. let menuStruct = clickPresentationMenu(point: pagePoint)
  4580. groupListMenuGroup?.groupDelegate = self
  4581. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  4582. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  4583. groupListMenuGroup?.showWithPoint(CGPoint(x: point.x, y: point.y - menuStruct.viewHeight), relativeTo: nil)
  4584. } else {
  4585. var menuStringArr: [String] = []
  4586. let activeAnnotations = pdfListView.activeAnnotations as? [CPDFAnnotation]
  4587. if activeAnnotations?.count ?? 0 > 1 {
  4588. var isSameAnnotation = true
  4589. let firstAnnotation = activeAnnotations?.first
  4590. for i in 1..<(activeAnnotations?.count ?? 1) {
  4591. if firstAnnotation?.type != activeAnnotations?[i].type {
  4592. isSameAnnotation = false
  4593. break
  4594. }
  4595. }
  4596. if isSameAnnotation {
  4597. if firstAnnotation?.isKind(of: CPDFMarkupAnnotation.self) == true {
  4598. menuStringArr.append(PDFViewMenuIdentifier_Normal_CopyText)
  4599. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4600. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4601. } else {
  4602. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4603. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4604. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4605. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4606. }
  4607. if(isShowPopUI){
  4608. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4609. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4610. }
  4611. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4612. if firstAnnotation?.isKind(of: CPDFLinkAnnotation.self) == true || firstAnnotation?.isKind(of: CPDFWidgetAnnotation.self) == true {
  4613. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4614. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4615. }
  4616. } else {
  4617. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4618. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4619. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4620. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4621. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4622. if firstAnnotation?.isKind(of: CPDFWidgetAnnotation.self) == true {
  4623. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4624. }
  4625. }
  4626. } else if activeAnnotations?.count == 1 {
  4627. if pdfListView.activeAnnotation.isKind(of: CPDFMarkupAnnotation.self) {
  4628. menuStringArr.append(PDFViewMenuIdentifier_Normal_CopyText)
  4629. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4630. if(isShowPopUI){
  4631. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4632. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4633. }
  4634. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4635. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4636. } else if pdfListView.activeAnnotation.isKind(of: CPDFRedactAnnotation.self) {
  4637. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4638. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4639. menuStringArr.append(PDFViewMenuIdentifier_Redact_Apply)
  4640. menuStringArr.append(PDFViewMenuIdentifier_Redact_Multipage)
  4641. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4642. menuStringArr.append(PDFViewMenuIdentifier_Normal_RedactProperties)
  4643. menuStringArr.append(PDFViewMenuIdentifier_Redact_Default)
  4644. } else if listView.activeAnnotation.isKind(of: CPDFLinkAnnotation.self) {
  4645. let link = listView.activeAnnotation as? CPDFLinkAnnotation
  4646. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4647. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4648. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4649. if(isShowPopUI){
  4650. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4651. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4652. }
  4653. } else if pdfListView.activeAnnotation.isKind(of: CPDFWidgetAnnotation.self) {
  4654. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4655. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4656. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4657. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4658. if(isShowPopUI){
  4659. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4660. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4661. }
  4662. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4663. } else {
  4664. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4665. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4666. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4667. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4668. if pdfListView.activeAnnotation.isKind(of: CPDFTextAnnotation.self) ||
  4669. pdfListView.activeAnnotation.isKind(of: CPDFFreeTextAnnotation.self) {
  4670. } else {
  4671. menuStringArr.append(PDFViewMenuIdentifier_Normal_Content)
  4672. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4673. }
  4674. if(isShowPopUI){
  4675. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4676. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4677. }
  4678. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4679. }
  4680. } else {
  4681. let currentSelection = pdfListView.currentSelection
  4682. if currentSelection != nil {
  4683. if currentSelection?.selectionType() == .text {
  4684. menuStringArr.append(PDFViewMenuIdentifier_Normal_AITool)
  4685. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4686. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4687. if pdfListView.canPaste() {
  4688. menuStringArr.append(PDFViewMenuIdentifier_Normal_Past)
  4689. }
  4690. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4691. menuStringArr.append(PDFViewMenuIdentifier_Normal_Hight)
  4692. menuStringArr.append(PDFViewMenuIdentifier_Normal_Underline)
  4693. menuStringArr.append(PDFViewMenuIdentifier_Normal_StrikeOut)
  4694. menuStringArr.append(PDFViewMenuIdentifier_Normal_Squiggly)
  4695. menuStringArr.append(PDFViewMenuIdentifier_Normal_Square)
  4696. menuStringArr.append(PDFViewMenuIdentifier_Normal_Circle)
  4697. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4698. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddOutLine)
  4699. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  4700. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4701. menuStringArr.append(PDFViewMenuIdentifier_Normal_TTS)
  4702. menuStringArr.append(PDFViewMenuIdentifier_Normal_SearchText)
  4703. } else if currentSelection?.selectionType() == .image {
  4704. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4705. if pdfListView.canPaste() {
  4706. menuStringArr.append(PDFViewMenuIdentifier_Normal_Past)
  4707. }
  4708. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4709. menuStringArr.append(PDFViewMenuIdentifier_Normal_Export)
  4710. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4711. menuStringArr.append(PDFViewMenuIdentifier_Normal_Square)
  4712. menuStringArr.append(PDFViewMenuIdentifier_Normal_Circle)
  4713. }
  4714. } else {
  4715. if pdfListView.toolMode == .CFormToolMode {
  4716. if pdfListView.canPaste() {
  4717. menuStringArr.append(PDFViewMenuIdentifier_Normal_Past)
  4718. }
  4719. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4720. menuStringArr.append(PDFViewMenuIdentifier_Normal_SelectAllForm)
  4721. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4722. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightForm)
  4723. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowFormName)
  4724. menuStringArr.append(PDFViewMenuIdentifier_Normal_RestForm)
  4725. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4726. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  4727. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4728. menuStringArr.append(PDFViewMenuIdentifier_Normal_ViewTools)
  4729. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4730. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  4731. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4732. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  4733. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4734. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageRotate)
  4735. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4736. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageNum)
  4737. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4738. menuStringArr.append(PDFViewMenuIdentifier_Normal_Search)
  4739. menuStringArr.append(PDFViewMenuIdentifier_Normal_Print)
  4740. menuStringArr.append(PDFViewMenuIdentifier_Normal_Properties)
  4741. } else if pdfListView.toolMode == .CRedactToolMode {
  4742. menuStringArr.append(PDFViewMenuIdentifier_Normal_SelectAllText)
  4743. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4744. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  4745. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4746. menuStringArr.append(PDFViewMenuIdentifier_Normal_ViewTools)
  4747. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4748. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  4749. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4750. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  4751. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4752. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageRotate)
  4753. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4754. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageNum)
  4755. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4756. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightLink)
  4757. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightForm)
  4758. menuStringArr.append(PDFViewMenuIdentifier_Normal_RestForm)
  4759. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4760. menuStringArr.append(PDFViewMenuIdentifier_Normal_Search)
  4761. menuStringArr.append(PDFViewMenuIdentifier_Normal_Print)
  4762. menuStringArr.append(PDFViewMenuIdentifier_Normal_Properties)
  4763. } else {
  4764. if(listView.viewSplitMode != .disable) {
  4765. menuStringArr.append(PDFViewMenuIdentifier_Split_ViewMode)
  4766. menuStringArr.append(PDFViewMenuIdentifier_Split_Sync)
  4767. menuStringArr.append(PDFViewMenuIdentifier_Split_ShowBar)
  4768. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4769. } else {
  4770. menuStringArr.append(PDFViewMenuIdentifier_Normal_AITool)
  4771. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4772. if pdfListView == listView {
  4773. if pdfListView.canPaste() {
  4774. menuStringArr.append(PDFViewMenuIdentifier_Normal_Past)
  4775. }
  4776. menuStringArr.append(PDFViewMenuIdentifier_Normal_SelectAllText)
  4777. menuStringArr.append(PDFViewMenuIdentifier_Normal_SelectAllAnnotation)
  4778. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4779. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowAnnotation)
  4780. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4781. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  4782. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4783. menuStringArr.append(PDFViewMenuIdentifier_Normal_ViewTools)
  4784. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4785. }
  4786. }
  4787. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  4788. if(listView.viewSplitMode != .disable) {
  4789. } else {
  4790. menuStringArr.append(PDFViewMenuIdentifier_Normal_ReadMode)
  4791. }
  4792. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4793. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  4794. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4795. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageRotate)
  4796. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4797. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageNum)
  4798. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4799. menuStringArr.append(PDFViewMenuIdentifier_Normal_AutoScroll)
  4800. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4801. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightLink)
  4802. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightForm)
  4803. menuStringArr.append(PDFViewMenuIdentifier_Normal_RestForm)
  4804. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4805. menuStringArr.append(PDFViewMenuIdentifier_Normal_Search)
  4806. menuStringArr.append(PDFViewMenuIdentifier_Normal_Print)
  4807. menuStringArr.append(PDFViewMenuIdentifier_Normal_Properties)
  4808. }
  4809. }
  4810. }
  4811. let menuStruct = KMPDFMenuConfig.clickMenuUI(items: menuStringArr, theEvent: theEvent, listView: pdfListView)
  4812. groupListMenuGroup?.groupDelegate = self
  4813. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  4814. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  4815. groupListMenuGroup?.showWithPoint(CGPoint(x: point.x, y: point.y - menuStruct.viewHeight), relativeTo: nil)
  4816. }
  4817. } else {
  4818. var menuStringArr: [String] = []
  4819. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  4820. menuStringArr.append(PDFViewMenuIdentifier_Normal_ReadMode)
  4821. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4822. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  4823. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4824. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightLink)
  4825. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightForm)
  4826. menuStringArr.append(PDFViewMenuIdentifier_Normal_RestForm)
  4827. let menuStruct = KMPDFMenuConfig.clickMenuUI(items: menuStringArr, theEvent: theEvent, listView: pdfListView)
  4828. groupListMenuGroup?.groupDelegate = self
  4829. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  4830. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  4831. groupListMenuGroup?.showWithPoint(CGPoint(x: point.x, y: point.y - menuStruct.viewHeight), relativeTo: nil)
  4832. }
  4833. }
  4834. func pdfListViewMenuItemsEditing(at point: CGPoint, for page: CPDFPage!, menuItems: [NSMenuItem]!) -> [NSMenuItem]! {
  4835. toggleClosePopUIWindow()
  4836. var windowPoint = listView.convert(point, from: page)
  4837. let view: NSView? = nil
  4838. windowPoint = listView.convert(windowPoint, to: view)
  4839. if self.view.window?.interactionMode == .presentation {
  4840. let menuStruct = clickPresentationMenu(point: point)
  4841. groupListMenuGroup?.groupDelegate = self
  4842. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  4843. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  4844. groupListMenuGroup?.showWithPoint(CGPoint(x: windowPoint.x, y: windowPoint.y - menuStruct.viewHeight), relativeTo: nil)
  4845. } else {
  4846. let isShowPopUI:Bool = !SettingsManager.sharedInstance.showQuickActionBar
  4847. var menuStringArr: [String] = []
  4848. let editingAreas = listView.km_EditingAreas()
  4849. let firstEditingArea = editingAreas.first
  4850. if editingAreas.count > 1 {
  4851. var isSameEditingArea = true
  4852. if(firstEditingArea?.isTextArea() == true) {
  4853. for i in 1..<(editingAreas.count) {
  4854. if !editingAreas[i].isTextArea() {
  4855. isSameEditingArea = false
  4856. break
  4857. }
  4858. }
  4859. } else if (firstEditingArea?.isImageArea() == true) {
  4860. for i in 1..<(editingAreas.count) {
  4861. if !editingAreas[i].isImageArea() {
  4862. isSameEditingArea = false
  4863. break
  4864. }
  4865. }
  4866. }
  4867. if(isSameEditingArea) {
  4868. if (firstEditingArea?.isTextArea() == true) {
  4869. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4870. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4871. if listView.isSupportPastMatchStyle() {
  4872. menuStringArr.append(PDFViewMenuIdentifier_Edit_Paste)
  4873. }
  4874. if listView.isSupportPast() {
  4875. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  4876. }
  4877. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  4878. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4879. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4880. menuStringArr.append(PDFViewMenuIdentifier_Edit_Font)
  4881. menuStringArr.append(PDFViewMenuIdentifier_Edit_FontName)
  4882. menuStringArr.append(PDFViewMenuIdentifier_Edit_FontAlight)
  4883. if(isShowPopUI){
  4884. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4885. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4886. }
  4887. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4888. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4889. } else if (firstEditingArea?.isImageArea() == true) {
  4890. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4891. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4892. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4893. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4894. menuStringArr.append(PDFViewMenuIdentifier_Edit_RotateLeft)
  4895. menuStringArr.append(PDFViewMenuIdentifier_Edit_RotateRight)
  4896. menuStringArr.append(PDFViewMenuIdentifier_Edit_HorizontalMirror)
  4897. menuStringArr.append(PDFViewMenuIdentifier_Edit_VerticalMirror)
  4898. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4899. menuStringArr.append(PDFViewMenuIdentifier_Normal_Export)
  4900. if(isShowPopUI){
  4901. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4902. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4903. }
  4904. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4905. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4906. }
  4907. } else {
  4908. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4909. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4910. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4911. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4912. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4913. }
  4914. } else if editingAreas.count == 1 {
  4915. if firstEditingArea?.isTextArea() == true {
  4916. if !listView.isSelecteditAreaNotEdit() { //光标输入状态
  4917. if listView.isSupportPast() {
  4918. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  4919. }
  4920. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  4921. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4922. } else {
  4923. let editState = listView.editStatus()
  4924. if (editState == .editSelectText) {// 选择文本
  4925. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4926. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4927. if listView.isSupportPast() {
  4928. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  4929. }
  4930. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  4931. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4932. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4933. } else {
  4934. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4935. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4936. if listView.isSupportPastMatchStyle() {
  4937. menuStringArr.append(PDFViewMenuIdentifier_Edit_Paste)
  4938. }
  4939. if listView.isSupportPast() {
  4940. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  4941. }
  4942. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  4943. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4944. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4945. }
  4946. menuStringArr.append(PDFViewMenuIdentifier_Edit_Font)
  4947. menuStringArr.append(PDFViewMenuIdentifier_Edit_FontName)
  4948. menuStringArr.append(PDFViewMenuIdentifier_Edit_FontAlight)
  4949. if(isShowPopUI){
  4950. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4951. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4952. }
  4953. }
  4954. } else if firstEditingArea?.isImageArea() == true {
  4955. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4956. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4957. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4958. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4959. menuStringArr.append(PDFViewMenuIdentifier_Edit_RotateLeft)
  4960. menuStringArr.append(PDFViewMenuIdentifier_Edit_RotateRight)
  4961. menuStringArr.append(PDFViewMenuIdentifier_Edit_HorizontalMirror)
  4962. menuStringArr.append(PDFViewMenuIdentifier_Edit_VerticalMirror)
  4963. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4964. menuStringArr.append(PDFViewMenuIdentifier_Edit_Crop)
  4965. menuStringArr.append(PDFViewMenuIdentifier_Edit_Replace)
  4966. menuStringArr.append(PDFViewMenuIdentifier_Normal_Export)
  4967. if(isShowPopUI){
  4968. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4969. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4970. }
  4971. }
  4972. } else {
  4973. if(listView.isSupportPastMatchStyle()) {
  4974. menuStringArr.append(PDFViewMenuIdentifier_Edit_Paste)
  4975. }
  4976. if(listView.isSupportPast()) {
  4977. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  4978. }
  4979. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  4980. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4981. menuStringArr.append(PDFViewMenuIdentifier_Edit_AddText)
  4982. menuStringArr.append(PDFViewMenuIdentifier_Edit_AddImage)
  4983. menuStringArr.append(PDFViewMenuIdentifier_Edit_AddLink)
  4984. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4985. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  4986. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4987. menuStringArr.append(PDFViewMenuIdentifier_Normal_ViewTools)
  4988. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4989. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  4990. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4991. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  4992. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4993. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageRotate)
  4994. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4995. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageNum)
  4996. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4997. menuStringArr.append(PDFViewMenuIdentifier_Normal_Search)
  4998. menuStringArr.append(PDFViewMenuIdentifier_Normal_Print)
  4999. menuStringArr.append(PDFViewMenuIdentifier_Normal_Properties)
  5000. }
  5001. let menuStruct = KMPDFMenuConfig.clickMenuUI(items: menuStringArr, theEvent: nil, listView: listView)
  5002. groupListMenuGroup?.groupDelegate = self
  5003. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  5004. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  5005. groupListMenuGroup?.showWithPoint(CGPoint(x: windowPoint.x, y: windowPoint.y - menuStruct.viewHeight), relativeTo: nil)
  5006. }
  5007. return []
  5008. }
  5009. func pdfListViewMobileAnnotationBegan(_ point: CGPoint, for pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!) {
  5010. toggleClosePopUIWindow()
  5011. }
  5012. func pdfListViewMobileAnnotationMove(_ point: CGPoint, for pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!) {
  5013. toggleClosePopUIWindow()
  5014. }
  5015. func pdfListViewMobileAnnotationEnd(_ point: CGPoint, for pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!) {
  5016. reloadPopUIWindow()
  5017. }
  5018. func pdfListViewAddAnnotations(_ pdfListView: CPDFListView!, forAdd annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  5019. var pageIndexes = IndexSet()
  5020. pageIndexes.insert(Int(pdfPage.pageIndex()))
  5021. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexes)
  5022. botaViewController?.reloadData()
  5023. alertTipViewController.reloadFormAlertUI()
  5024. alertTipViewController.reloadAlertUIFrame()
  5025. }
  5026. func pdfListViewRemoveAnnotations(_ pdfListView: CPDFListView!, forRemove annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  5027. var pageIndexes = IndexSet()
  5028. pageIndexes.insert(Int(pdfPage.pageIndex()))
  5029. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexes)
  5030. botaViewController?.reloadData()
  5031. alertTipViewController.reloadFormAlertUI()
  5032. alertTipViewController.reloadAlertUIFrame()
  5033. }
  5034. func pdfListViewEditAnnotation(_ pdfListView: CPDFListView!, for anotation: CPDFAnnotation!) {
  5035. if anotation.isKind(of: CPDFSignatureWidgetAnnotation.self) {
  5036. if let signatureWidgetAnnotation = anotation as? CPDFSignatureWidgetAnnotation {
  5037. let signature = signatureWidgetAnnotation.signature()
  5038. if ((signature) != nil) {
  5039. popUpSignatureWidgetState(signature!, listView)
  5040. } else {
  5041. let widget = CPDFSignatureWidgetAnnotation.init(PDFListViewNoteWith: listView.document)
  5042. widget.bounds = NSMakeRect(-1000, -1000, 545, 178);
  5043. anotation.page.addAnnotation(widget)
  5044. let configWindowVC = DSignatureConfigWindowController.init(windowNibName: "DSignatureConfigWindowController")
  5045. configWindowVC.viewType = .fileList;
  5046. configWindowVC.appearanceWidget = widget;
  5047. configWindowVC.isCreatDS = false
  5048. configWindowVC.complentionHandle = {[weak self] isSign, dic, config, isLock in
  5049. widget.page.removeAnnotation(widget)
  5050. if isSign {
  5051. if (dic.object(forKey: SAVEFILEPATH_KEY) != nil) {
  5052. let p12Path = dic.object(forKey: SAVEFILEPATH_KEY) as! String
  5053. let password = dic.object(forKey: PASSWORD_KEY)
  5054. if p12Path.count > 0 {
  5055. self?.writeSignatureToWidget(signatureWidgetAnnotation, p12Path, password as! String, config, isLock)
  5056. }
  5057. }
  5058. } else {
  5059. if signatureWidgetAnnotation.isSignSignatureAdd() == true {
  5060. self?.listView.remove(anotation)
  5061. }
  5062. }
  5063. }
  5064. configWindowVC.actionBlock = {[weak self] controller, type in
  5065. if (type == .cancel) {
  5066. NSApplication.shared.stopModal()
  5067. controller.window?.orderOut(nil)
  5068. controller.window?.close()
  5069. widget.page.removeAnnotation(widget)
  5070. if signatureWidgetAnnotation.isSignSignatureAdd() == true {
  5071. self?.listView.remove(anotation)
  5072. } else {
  5073. self?.listView.setNeedsDisplayAnnotationViewFor(anotation.page)
  5074. }
  5075. } else if (type == .confirm) {
  5076. NSApplication.shared.stopModal()
  5077. controller.window?.orderOut(nil)
  5078. controller.window?.close()
  5079. }
  5080. }
  5081. configWindowVC.window?.center()
  5082. NSApplication.shared.runModal(for: configWindowVC.window!)
  5083. }
  5084. }
  5085. } else if anotation.isKind(of: CPDFRedactAnnotation.self) {
  5086. if let redactAnnotation = anotation as? CPDFRedactAnnotation {
  5087. let properties = KMRedactPropertiesWindowController()
  5088. properties.readactAnnotation = redactAnnotation
  5089. properties.own_beginSheetModal(for: self.view.window) { result in
  5090. }
  5091. properties.callback = { [weak self] annotation in
  5092. if let page = annotation?.page {
  5093. self?.listView.setNeedsDisplayAnnotationViewFor(page)
  5094. }
  5095. }
  5096. }
  5097. }
  5098. }
  5099. func pdfListViewSplitModeShowBar() -> Bool {
  5100. return viewManager.splitShowBottomBar
  5101. }
  5102. func pdfListViewEndEditMode(_ pdfListView: CPDFListView!) {
  5103. let document = listView.document
  5104. if(document != nil) {
  5105. for i in 0..<document!.pageCount {
  5106. let thumbnail = KMNThumbnail.init(document: document!, currentPageIndex: Int(i))
  5107. thumbnail.removeCacheImage()
  5108. }
  5109. }
  5110. botaViewController?.reloadData()
  5111. }
  5112. func pdfListViewAddEditAreaTypeChanged(_ pdfListView: CPDFListView!) {
  5113. if self.listView.shouAddEditAreaType() != .text && self.listView.shouAddEditAreaType() != .image {
  5114. if viewManager.subToolMode != .None {
  5115. viewManager.subToolMode = .None
  5116. pdfToolbarController?.cancelSelectedSecondToolbarItems(viewManager.toolMode)
  5117. pdfToolbarController?.refreshSecondToolbarItemsState()
  5118. }
  5119. }
  5120. }
  5121. //MARK: -Crop
  5122. func pdfListViewChangedSelectionOrMagnification(_ pdfListView: CPDFListView!) {
  5123. reloadPopUIWindow()
  5124. }
  5125. func pdfListViewDidSelectionEnd(_ pdfListView: CPDFListView!) {
  5126. if (!self.listView.isEqual(to: pdfListView)) {
  5127. return
  5128. }
  5129. if (self.listView.toolMode != .CSelectToolMode) {
  5130. return
  5131. }
  5132. reloadPopUIWindow()
  5133. }
  5134. func pdfListViewHaveDocumentAttribute() -> Bool {
  5135. if(!self.listView.document.allowsCopying) {
  5136. self.removeOwnerPassword()
  5137. return false
  5138. }
  5139. return true
  5140. }
  5141. func pdfListViewShowTipView() -> Bool {
  5142. let popWindow = KMNPopAnnotationWindowController.shared
  5143. if popWindow.window?.isVisible == true {
  5144. return true
  5145. }
  5146. return false
  5147. }
  5148. func pdfListViewAnnotationEditModeChange(_ pdfListView: CPDFListView!, for anotation: CPDFAnnotation!) {
  5149. reloadPopUIWindow()
  5150. }
  5151. func pdfListViewAnnotationChange(_ color: NSColor!, forActiveAnnotation annotation: CPDFAnnotation!) {
  5152. rightSideController?.reloadData()
  5153. NotificationCenter.default.post(name: toolbarImageColorChangedNotificationName, object: nil)
  5154. }
  5155. //MARK: -Measure
  5156. func pdfListViewAnnotationMeasureInfoChange(_ pdfListView: CPDFListView!, with annotation: CPDFAnnotation!) {
  5157. guard let data = annotation else {
  5158. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  5159. distanceMeasureInfoWindowController?.clearData()
  5160. }
  5161. return
  5162. }
  5163. if let lineAnnotation = annotation as? CPDFLineAnnotation {
  5164. handleLineAnnotation(lineAnnotation)
  5165. } else if let polylineAnnotation = annotation as? CPDFPolylineAnnotation {
  5166. handlePolylineAnnotation(polylineAnnotation)
  5167. } else if let polygonAnnotation = annotation as? CPDFPolygonAnnotation {
  5168. handlePolygonAnnotation(polygonAnnotation)
  5169. }
  5170. }
  5171. func pdfListViewMeasureCancel(_ pdfListView: CPDFListView!) {
  5172. cancelMeasureType()
  5173. }
  5174. private func handleLineAnnotation(_ annotation: CPDFLineAnnotation) {
  5175. if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  5176. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  5177. distanceMeasureInfoWindowController?.showWindow(self)
  5178. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  5179. areaMeasureInfoWindowController?.hideFloatingWindow()
  5180. distanceMeasureInfoWindowController?.showWindow(self)
  5181. } else if distanceMeasureInfoWindowController?.window?.isVisible == false {
  5182. distanceMeasureInfoWindowController?.showWindow(self)
  5183. }
  5184. let measureInfo = annotation.measureInfo
  5185. let startPoint = annotation.startPoint
  5186. let endPoint = annotation.endPoint
  5187. let angle = atan2(endPoint.y - startPoint.y, endPoint.x - startPoint.x) * (180.0 / .pi)
  5188. distanceMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  5189. distanceMeasureInfoWindowController?.xLabel.stringValue = String(format: "%.0f", abs(endPoint.x - startPoint.x))
  5190. distanceMeasureInfoWindowController?.yLabel.stringValue = String(format: "%.0f", abs(endPoint.y - startPoint.y))
  5191. distanceMeasureInfoWindowController?.reloadData(with: measureInfo!)
  5192. }
  5193. private func handlePolylineAnnotation(_ annotation: CPDFPolylineAnnotation) {
  5194. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  5195. distanceMeasureInfoWindowController?.hideFloatingWindow()
  5196. perimeterMeasureInfoWindowController?.showWindow(self)
  5197. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  5198. areaMeasureInfoWindowController?.hideFloatingWindow()
  5199. perimeterMeasureInfoWindowController?.showWindow(self)
  5200. } else if perimeterMeasureInfoWindowController?.window?.isVisible == false {
  5201. perimeterMeasureInfoWindowController?.showWindow(self)
  5202. }
  5203. let measureInfo = annotation.measureInfo
  5204. let savePoints = annotation.savePoints()
  5205. var angle: CGFloat = 0
  5206. if savePoints.count >= 3 {
  5207. let count = savePoints.count
  5208. let startPoint = savePoints[count - 3].pointValue
  5209. let midPoint = savePoints[count - 2].pointValue
  5210. let endPoint = savePoints.last!.pointValue
  5211. angle = angleBetweenPoints(startPoint, midPoint, endPoint)
  5212. }
  5213. angle = 180 - angle
  5214. perimeterMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  5215. perimeterMeasureInfoWindowController?.reloadData(with: measureInfo!)
  5216. }
  5217. private func handlePolygonAnnotation(_ annotation: CPDFPolygonAnnotation) {
  5218. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  5219. distanceMeasureInfoWindowController?.hideFloatingWindow()
  5220. areaMeasureInfoWindowController?.showWindow(self)
  5221. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  5222. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  5223. areaMeasureInfoWindowController?.showWindow(self)
  5224. } else if areaMeasureInfoWindowController?.window?.isVisible == false {
  5225. areaMeasureInfoWindowController?.showWindow(self)
  5226. }
  5227. let measureInfo = annotation.measureInfo
  5228. let savePoints = annotation.savePoints
  5229. var angle: CGFloat = 0
  5230. if savePoints.count >= 3 {
  5231. let count = savePoints.count
  5232. let startPoint = (savePoints[count - 3] as AnyObject).pointValue
  5233. let midPoint = (savePoints[count - 2] as AnyObject).pointValue
  5234. let endPoint = (savePoints.lastObject as AnyObject).pointValue
  5235. angle = angleBetweenPoints(startPoint!, midPoint!, endPoint!)
  5236. }
  5237. angle = 180 - angle
  5238. areaMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  5239. areaMeasureInfoWindowController?.reloadData(measureInfo!)
  5240. }
  5241. private func angleBetweenPoints(_ startPoint: CGPoint, _ midPoint: CGPoint, _ endPoint: CGPoint) -> CGFloat {
  5242. let vector1 = CGPoint(x: midPoint.x - startPoint.x, y: midPoint.y - startPoint.y)
  5243. let vector2 = CGPoint(x: endPoint.x - midPoint.x, y: endPoint.y - midPoint.y)
  5244. let dotProduct = vector1.x * vector2.x + vector1.y * vector2.y
  5245. let magnitude1 = sqrt(vector1.x * vector1.x + vector1.y * vector1.y)
  5246. let magnitude2 = sqrt(vector2.x * vector2.x + vector2.y * vector2.y)
  5247. return acos(dotProduct / (magnitude1 * magnitude2)) * (180.0 / .pi)
  5248. }
  5249. @objc func pdfUpdatedFinish() {
  5250. splitPDFController?.inPDFFirst = false
  5251. splitPDFController?.outPDFFirst = false
  5252. }
  5253. //MARK: - Notification
  5254. @objc private func pdfViewScrollViewDidScroll(_ noti: Notification) {
  5255. reloadPopUIWindow()
  5256. if listView.popOver?.isShown == true {
  5257. listView.popOver?.close()
  5258. }
  5259. }
  5260. func pageCountChangedNotification(_ sender: Notification) {
  5261. guard let document = sender.object as? CPDFDocument else {
  5262. return
  5263. }
  5264. botaViewController?.pageCountChangedAction(document: document)
  5265. }
  5266. func annotationsAttributeHasChange(_ sender: Notification) {
  5267. guard let dict = sender.object as? [String : Any] else {
  5268. return
  5269. }
  5270. if let anno = dict["object"] as? CPDFAnnotation {
  5271. let value = dict["keyPath"] as? String ?? ""
  5272. let didEnd = dict["didEnd"] as? Bool ?? false
  5273. if didEnd {
  5274. if value == CPDFAnnotationBoundsKey {
  5275. if anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation {
  5276. anno.contents = anno.page?.string(for: anno.bounds) ?? ""
  5277. }
  5278. }
  5279. if anno.km_isMeasure() && anno.contents == nil {
  5280. anno.contents = anno.string() ?? ""
  5281. }
  5282. var pageIndexes = IndexSet()
  5283. pageIndexes.insert(Int(anno.page.pageIndex()))
  5284. reloadIfThumData(pageIndexs: pageIndexes)
  5285. reloadPopUIWindow()
  5286. } else {
  5287. if value != CPDFAnnotationBoundsKey && value != CPDFAnnotationStartPointKey && value != CPDFAnnotationEndPointKey && value != CPDFAnnotationPathsKey { // 改变bounds(箭头、直线注释 开始点和结束点, 手绘注释的paths)的操作会卡顿,比如移动
  5288. var pageIndexes = IndexSet()
  5289. pageIndexes.insert(Int(anno.page.pageIndex()))
  5290. reloadIfThumData(pageIndexs: pageIndexes)
  5291. reloadPopUIWindow()
  5292. }
  5293. }
  5294. }
  5295. }
  5296. func reloadThumData(pageIndexs: IndexSet) {
  5297. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(reloadIfThumData), object: pageIndexs)
  5298. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  5299. self.reloadIfThumData(pageIndexs: pageIndexs)
  5300. }
  5301. }
  5302. func reloadIfThumData(pageIndexs: IndexSet) {
  5303. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexs)
  5304. botaViewController?.reloadData()
  5305. }
  5306. @objc fileprivate func signatureStateChangeNoti(_ sender: Notification) {
  5307. guard let objecListView = sender.object as? CPDFListView else {
  5308. return
  5309. }
  5310. if listView == objecListView {
  5311. reloadDigitalSigns()
  5312. alertTipViewController.reloadDigitalAlertUI()
  5313. alertTipViewController.reloadAlertUIFrame()
  5314. let signatureWidget = listView.editAnnotation
  5315. if let signatureWidgetAnnotation = signatureWidget as? CPDFSignatureWidgetAnnotation {
  5316. let signature = signatureWidgetAnnotation.signature()
  5317. if signature == nil {
  5318. return
  5319. }
  5320. signaturestateVC.signature = signature
  5321. signaturestateVC.reloadData()
  5322. }
  5323. }
  5324. }
  5325. }
  5326. //MARK: - KMNThumbnailBaseViewDelegate
  5327. extension KMMainViewController: KMNThumbnailBaseViewDelegate {
  5328. func clickThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,currentIndex:Int) {
  5329. exitPageEditMode()
  5330. if listView.currentPageIndex != currentIndex {
  5331. listView.go(toPageIndex: currentIndex, animated: true)
  5332. }
  5333. viewManager.isPageEditMode = false
  5334. pdfToolbarController?.clickWithIdentify(KMPDFToolbar_PageEdit_Identifier)
  5335. }
  5336. func insertPDFThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, pdfDocment: CPDFDocument?) {
  5337. if(pdfDocment != nil) {
  5338. insertDocuments.insert(pdfDocment!)
  5339. }
  5340. }
  5341. func changeIndexPathsThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, selectionIndexPaths: Set<IndexPath>, selectionStrings: String) {
  5342. toolbarManager.page_pageInfo_Property.text = selectionStrings
  5343. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  5344. toolbarManager.page_pageInfo_Property.creatable = true
  5345. }
  5346. pdfToolbarController?.refreshSecondToolbarItemsState()
  5347. }
  5348. }
  5349. //MARK: - KMNLeftSideViewControllerDelegate
  5350. extension KMMainViewController: KMNLeftSideViewControllerDelegate {
  5351. func enterPageEditLeftSideViewController(leftSideViewController: KMNLeftSideViewController) {
  5352. if viewManager.isPageEditMode == false {
  5353. viewManager.isPageEditMode = true
  5354. if(pdfToolbarController != nil) {
  5355. kmPDFToolbarControllerDidToolbarItemClicked(pdfToolbarController!, KMPDFToolbar_PageEdit_Identifier)
  5356. pdfToolbarController?.resetSecondToolbar()
  5357. }
  5358. }
  5359. }
  5360. func changeSelectePageLeftSideViewController(leftSideViewController: KMNLeftSideViewController, pageIndex: Int) {
  5361. if(listView.currentPageIndex != pageIndex) {
  5362. listView.go(toPageIndex: pageIndex, animated: true)
  5363. }
  5364. }
  5365. func addBookmarkForLeftC(controller: KMNLeftSideViewController, bookmark: CPDFBookmark?, info: [String : Any]?) {
  5366. if let result = info?["result"] as? Bool {
  5367. if result == false {
  5368. if let targetV = listView.superview {
  5369. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("This page has been bookmarked"), type: .normal_custom, fromView: targetV, point:CGPointMake(targetV.frame.size.width/2, targetV.bounds.size.height-24))
  5370. }
  5371. }
  5372. }
  5373. }
  5374. func switchSearchPopWindow(controller: KMNLeftSideViewController) {
  5375. if(viewManager.pdfSideBarType == .search) {
  5376. toggleCloseLeftSide()
  5377. }
  5378. let handdler = controller.searchViewC.handdler
  5379. showSearchPopWindow(type: handdler.type, keyborad: handdler.searchKey, replaceText: handdler.replaceKey, results: handdler.searchResults)
  5380. }
  5381. func searchTypeDidChange(controller: KMNLeftSideViewController) {
  5382. let handdler = controller.searchViewC.handdler
  5383. if handdler.type == .replace {
  5384. viewManager.toolMode = .Edit
  5385. updatePDFViewAnnotationMode()
  5386. }
  5387. }
  5388. }
  5389. //MARK: - ComponentGroupDelegate
  5390. extension KMMainViewController: ComponentGroupDelegate {
  5391. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  5392. if menuItemProperty?.identifier == PDFViewMenuIdentifier_PageNext {
  5393. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5394. if objectListView.canGoToNextPage() {
  5395. objectListView.goToNextPage(nil)
  5396. }
  5397. }
  5398. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_PagePrevious) {
  5399. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5400. if objectListView.canGoToPreviousPage() {
  5401. objectListView.goToPreviousPage(nil)
  5402. }
  5403. }
  5404. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_PageFirst) {
  5405. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5406. if objectListView.canGoToFirstPage() {
  5407. objectListView.goToFirstPage(nil)
  5408. }
  5409. }
  5410. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_PageLast) {
  5411. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5412. if objectListView.canGoToLastPage() {
  5413. objectListView.goToLastPage(nil)
  5414. }
  5415. }
  5416. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Presentation_LaserPoint) {
  5417. listView.exitPresentationDrawMode()
  5418. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Presentation_Brush) {
  5419. listView.enterPresentationDrawMode()
  5420. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Presentation_Exit) {
  5421. self.exitFullScreen()
  5422. } else if menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AIRewrite {
  5423. viewManager.pdfSideBarType = .aiTools
  5424. sideBarController?.reloadData()
  5425. if let sideVC = sideBarController {
  5426. kmPDFSideBarControllerDidSidebarTypeUpdated(sideVC)
  5427. }
  5428. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AIProofread) {
  5429. viewManager.pdfSideBarType = .aiTools
  5430. sideBarController?.reloadData()
  5431. if let sideVC = sideBarController {
  5432. kmPDFSideBarControllerDidSidebarTypeUpdated(sideVC)
  5433. }
  5434. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AITranslate) {
  5435. viewManager.pdfSideBarType = .aiTools
  5436. sideBarController?.reloadData()
  5437. if let sideVC = sideBarController {
  5438. kmPDFSideBarControllerDidSidebarTypeUpdated(sideVC)
  5439. }
  5440. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Past) {
  5441. let theEvent = menuItemProperty?.representedObject
  5442. if let currentEvent = theEvent as? NSEvent {
  5443. var pagePoint = CGPoint.zero
  5444. if let page = listView.pageAndPoint(&pagePoint, for: currentEvent, nearest: false) {
  5445. listView.menuPointPaste(pagePoint, page: page, isRightPaste: true)
  5446. }
  5447. }
  5448. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectAllText) {
  5449. listView.selectAll(nil)
  5450. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AddBook) {
  5451. menuItemBookMarkClick_add(sender: nil)
  5452. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TextTool) {
  5453. listView.toolMode = .CTextToolMode
  5454. listView.annotationType = .unkown
  5455. viewManager.viewToolsType = .Select
  5456. pdfToolbarController?.reloadToolsView()
  5457. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_MoveTool) {
  5458. listView.toolMode = .CMoveToolMode
  5459. listView.annotationType = .unkown
  5460. viewManager.viewToolsType = .Scroll
  5461. pdfToolbarController?.reloadToolsView()
  5462. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectTool) {
  5463. listView.toolMode = .CSelectToolMode
  5464. listView.annotationType = .unkown
  5465. viewManager.viewToolsType = .Content_Selection
  5466. pdfToolbarController?.reloadToolsView()
  5467. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_MagnifyTool) {
  5468. listView.toolMode = .CMagnifyToolMode
  5469. listView.annotationType = .unkown
  5470. viewManager.viewToolsType = .Magnify
  5471. pdfToolbarController?.reloadToolsView()
  5472. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectZoomTool) {
  5473. listView.toolMode = .CSelectZoomToolMode
  5474. listView.annotationType = .unkown
  5475. viewManager.viewToolsType = .AreaZoom
  5476. pdfToolbarController?.reloadToolsView()
  5477. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Single) {
  5478. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5479. objectListView.menuItemClick_SinglePage(nil)
  5480. }
  5481. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SingleContinuous) {
  5482. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5483. objectListView.menuItemClick_SinglePagesContinuous(nil)
  5484. }
  5485. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TwoPages) {
  5486. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5487. objectListView.menuItemClick_TwoPages(nil)
  5488. }
  5489. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TwoPagesContinuous) {
  5490. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5491. objectListView.menuItemClick_TwoPagesContinuous(nil)
  5492. }
  5493. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_BookMode) {
  5494. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5495. objectListView.menuItemClick_BookMode(nil)
  5496. }
  5497. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ReadMode) {
  5498. viewManager.isPDFReadMode = !viewManager.isPDFReadMode
  5499. if viewManager.isPDFReadMode {
  5500. openPDFReadMode()
  5501. } else {
  5502. exitPDFReadMode()
  5503. }
  5504. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScaleWidth) {
  5505. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5506. objectListView.autoScales = true
  5507. }
  5508. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScalePage) {
  5509. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5510. let pageHeight = objectListView.currentPage()!.size.height
  5511. let pdfviewHeight = objectListView.bounds.size.height
  5512. objectListView.scaleFactor = pdfviewHeight/pageHeight
  5513. objectListView.autoScales = false
  5514. }
  5515. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScaleOrg) {
  5516. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5517. if objectListView.scaleFactor != 1.0 {
  5518. objectListView.scaleFactor = 1.0
  5519. objectListView.autoScales = false
  5520. }
  5521. }
  5522. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScaleZoomIn) {
  5523. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5524. var scale = objectListView.scaleFactor
  5525. switch scale {
  5526. case 0...0.25:
  5527. scale += 0.25
  5528. case 0.25...3:
  5529. scale += 0.25
  5530. case 3.1...10:
  5531. scale += 0.4
  5532. case 10.1...100:
  5533. scale += 1
  5534. default:
  5535. scale += 1
  5536. }
  5537. objectListView.scaleFactor = scale
  5538. }
  5539. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScaleZoomOut) {
  5540. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5541. var scale = objectListView.scaleFactor
  5542. switch scale {
  5543. case 0...0.25:
  5544. scale = 0
  5545. case 0.25...3:
  5546. scale -= 0.25
  5547. case 3.1...10:
  5548. scale -= 0.4
  5549. case 10.1...100:
  5550. scale -= 1
  5551. default:
  5552. scale -= 1
  5553. }
  5554. objectListView.scaleFactor = scale
  5555. }
  5556. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RotateLeft) {
  5557. if let dic = menuItemProperty?.representedObject as? NSDictionary {
  5558. let objectListView = dic["Object"] as? CPDFListView
  5559. let theEvent = dic["theEvent"] as? NSEvent
  5560. if(objectListView != nil && theEvent != nil) {
  5561. var pagePoint = CGPoint.zero
  5562. if let page = objectListView?.pageAndPoint(&pagePoint, for: theEvent, nearest: false) {
  5563. rotateLeft(page: page, listView: objectListView)
  5564. }
  5565. }
  5566. }
  5567. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RotateRight) {
  5568. if let dic = menuItemProperty?.representedObject as? NSDictionary {
  5569. let objectListView = dic["Object"] as? CPDFListView
  5570. let theEvent = dic["theEvent"] as? NSEvent
  5571. if(objectListView != nil && theEvent != nil) {
  5572. var pagePoint = CGPoint.zero
  5573. if let page = objectListView?.pageAndPoint(&pagePoint, for: theEvent, nearest: false) {
  5574. rotateRight(page: page, listView: objectListView)
  5575. }
  5576. }
  5577. }
  5578. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_EnterPageNum) {
  5579. sideBarController?.beginEditing()
  5580. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_PageBack) {
  5581. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5582. if(objectListView.canGoBack() == true) {
  5583. objectListView.goBack(nil)
  5584. }
  5585. }
  5586. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_PageForward) {
  5587. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5588. if(objectListView.canGoForward() == true) {
  5589. objectListView.goForward(nil)
  5590. }
  5591. }
  5592. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AutoScroll) {
  5593. toggleAutoFlow(nil)
  5594. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_HightForm) {
  5595. highlightFormFiled(nil)
  5596. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_HightLink) {
  5597. highlightLinks(nil)
  5598. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RestForm) {
  5599. resetForm(nil)
  5600. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Search) {
  5601. if botaViewController != nil {
  5602. switchSearchPopWindow(controller: botaViewController!)
  5603. }
  5604. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Print) {
  5605. menuItemAction_print(nil)
  5606. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Properties) {
  5607. menuItemAction_property(nil)
  5608. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Copy) {
  5609. if(listView.isEditing() == true) {
  5610. listView.copyEditAreaAction()
  5611. } else {
  5612. listView.copy(nil)
  5613. }
  5614. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Hight) {
  5615. if let currentSelect = listView.currentSelection {
  5616. if(currentSelect.selectionType() == .text) {
  5617. listView.addAnnotation(with: .highlight, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5618. listView.currentSelection = nil;
  5619. }
  5620. }
  5621. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Underline) {
  5622. if let currentSelect = listView.currentSelection {
  5623. if(currentSelect.selectionType() == .text) {
  5624. listView.addAnnotation(with: .underline, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5625. listView.currentSelection = nil;
  5626. }
  5627. }
  5628. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Squiggly) {
  5629. if let currentSelect = listView.currentSelection {
  5630. if(currentSelect.selectionType() == .text) {
  5631. listView.addAnnotation(with: .squiggly, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5632. listView.currentSelection = nil;
  5633. }
  5634. }
  5635. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_StrikeOut) {
  5636. if let currentSelect = listView.currentSelection {
  5637. if(currentSelect.selectionType() == .text) {
  5638. listView.addAnnotation(with: .strikeOut, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5639. listView.currentSelection = nil;
  5640. }
  5641. }
  5642. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Square) {
  5643. if let currentSelect = listView.currentSelection {
  5644. listView.addAnnotation(with: .square, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5645. listView.currentSelection = nil;
  5646. }
  5647. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Circle) {
  5648. if let currentSelect = listView.currentSelection {
  5649. listView.addAnnotation(with: .circle, selection: currentSelect, page: currentSelect.page, bounds: listView.currentSelection.bounds)
  5650. listView.currentSelection = nil;
  5651. }
  5652. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AddOutLine) {
  5653. addOutLineItemAction()
  5654. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TTS) {
  5655. startSpeaking(nil)
  5656. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SearchText) {
  5657. searchBaiduAction()
  5658. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectAllForm) {
  5659. selectAllFormAnnotation()
  5660. listView.setNeedsDisplayForVisiblePages()
  5661. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ShowFormName) {
  5662. listView.showFormFieldName = true
  5663. listView.setNeedsDisplayForVisiblePages()
  5664. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectAllAnnotation) {
  5665. selectAllNomerAnnotation()
  5666. listView.setNeedsDisplayForVisiblePages()
  5667. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ShowAnnotation) {
  5668. let isHiden = listView.hideNotes
  5669. listView.hideNotes = !isHiden
  5670. listView.setNeedsDisplayForVisiblePages()
  5671. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_CopyText) {
  5672. var copyText:String = ""
  5673. let annotations = listView.activeAnnotations
  5674. for i in 0 ..< (annotations?.count ?? 0){
  5675. if let an = annotations?[i] as? CPDFMarkupAnnotation {
  5676. let markupContent = an.markupContent()
  5677. if markupContent.isEmpty == false {
  5678. if copyText.isEmpty == true {
  5679. copyText = markupContent
  5680. } else {
  5681. copyText = copyText + "\n" + markupContent
  5682. }
  5683. }
  5684. }
  5685. }
  5686. let pboard = NSPasteboard.general
  5687. if copyText.isEmpty == false {
  5688. pboard.clearContents()
  5689. pboard.writeObjects([copyText as NSPasteboardWriting])
  5690. }
  5691. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Cut) {
  5692. if(listView.isEditing() == true) {
  5693. listView.cutEditAreaAction()
  5694. } else {
  5695. listView.cut(nil)
  5696. }
  5697. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Delete) {
  5698. if(listView.isEditing() == true) {
  5699. listView.remove(with: listView.km_EditingAreas())
  5700. } else {
  5701. listView.delete(nil)
  5702. }
  5703. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ShowPopUI) {
  5704. UserDefaults.standard.setValue(true, forKey: settingsShowQuickActionBarKey)
  5705. SettingsManager.sharedInstance.showQuickActionBar = true
  5706. reloadPopUIWindow()
  5707. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_LinkReade) {
  5708. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SortFirstAnnotation) {
  5709. listView.menuItemClick_BringFront(nil)
  5710. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SortTopAnnotation) {
  5711. listView.menuItemClick_BringForward(nil)
  5712. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SortBottomAnnotation) {
  5713. listView.menuItemClick_SendBackward(nil)
  5714. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SortLastAnnotation) {
  5715. listView.menuItemClick_SendBack(nil)
  5716. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Content) {
  5717. if let activeAnnotation = listView.activeAnnotation {
  5718. listView.edit(activeAnnotation)
  5719. }
  5720. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Redact_Apply) {
  5721. redactApplyAction()
  5722. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Redact_Multipage) {
  5723. if let redactAnnotation = listView.activeAnnotation as? CPDFRedactAnnotation {
  5724. redactMultipageAction(redactAnnotation: redactAnnotation)
  5725. }
  5726. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Redact_Default) {
  5727. if let redactAnnotation = listView.activeAnnotation as? CPDFRedactAnnotation {
  5728. setPropertiesDefault(annotation: redactAnnotation)
  5729. }
  5730. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_ViewSingleMode) {
  5731. listView.viewSplitMode = .disable
  5732. reloadPDFSplitInfo()
  5733. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_ViewVerticalMode) {
  5734. listView.viewSplitMode = .vertical
  5735. reloadPDFSplitInfo()
  5736. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_ViewHorizontalMode) {
  5737. listView.viewSplitMode = .horizontal
  5738. reloadPDFSplitInfo()
  5739. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_Sync) {
  5740. viewManager.splitSyncScroll = !viewManager.splitSyncScroll
  5741. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_ShowBar) {
  5742. viewManager.splitShowBottomBar = !viewManager.splitShowBottomBar
  5743. splitPDFController?.refreshToolbarState()
  5744. reloadPDFPageNumberToolbar()
  5745. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_LeftAlight) {
  5746. if(listView.isEditing()) {
  5747. listView.changeEditingAreas(.left)
  5748. } else {
  5749. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .left)
  5750. }
  5751. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_VerticallyAlight) {
  5752. if(listView.isEditing()) {
  5753. listView.changeEditingAreas(.vertical)
  5754. } else {
  5755. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .vertical)
  5756. }
  5757. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RightAlight) {
  5758. if(listView.isEditing()) {
  5759. listView.changeEditingAreas(.right)
  5760. } else {
  5761. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .right)
  5762. }
  5763. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TopAlight) {
  5764. if(listView.isEditing()) {
  5765. listView.changeEditingAreas(.top)
  5766. } else {
  5767. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .top)
  5768. }
  5769. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_HorizontallyAlight) {
  5770. if(listView.isEditing()) {
  5771. listView.changeEditingAreas(.horizontally)
  5772. } else {
  5773. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .horizontally)
  5774. }
  5775. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_BottomAlight) {
  5776. if(listView.isEditing()) {
  5777. listView.changeEditingAreas(.bottom)
  5778. } else {
  5779. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .bottom)
  5780. }
  5781. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_DistributeHorizontally) {
  5782. if(listView.isEditing()) {
  5783. listView.changeEditingAreas(.disHorizontally)
  5784. } else {
  5785. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .disHorizontally)
  5786. }
  5787. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_DistributeVertically) {
  5788. if(listView.isEditing()) {
  5789. listView.changeEditingAreas(.disVertical)
  5790. } else {
  5791. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .disVertical)
  5792. }
  5793. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ExportJPG) {
  5794. if(listView.isEditing() == true) {
  5795. listView.exportEditingImageAreasAction(format: "jpg")
  5796. } else {
  5797. if let currentSelection = listView.currentSelection {
  5798. if(currentSelection.selectionType() == .image) {
  5799. listView.exprotSelection(currentSelection, type: 1)
  5800. }
  5801. }
  5802. }
  5803. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ExportPNG) {
  5804. if(listView.isEditing() == true) {
  5805. listView.exportEditingImageAreasAction(format: "png")
  5806. } else {
  5807. if let currentSelection = listView.currentSelection {
  5808. if(currentSelection.selectionType() == .image) {
  5809. listView.exprotSelection(currentSelection, type: 0)
  5810. }
  5811. }
  5812. }
  5813. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ExportPDF) {
  5814. if(listView.isEditing() == true) {
  5815. listView.exportEditingImageAreasAction(format: "pdf")
  5816. } else {
  5817. if let currentSelection = listView.currentSelection {
  5818. if(currentSelection.selectionType() == .image) {
  5819. listView.exprotSelection(currentSelection, type: 2)
  5820. }
  5821. }
  5822. }
  5823. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_Paste) {
  5824. listView.pasteEditAreaMatchStyleAction()
  5825. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_NoStylePaste) {
  5826. listView.pasteEditAreaAction()
  5827. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_SelectAll) {
  5828. if listView.km_EditingAreas().count <= 0 {
  5829. listView.selectAllAreaAction()
  5830. } else {
  5831. listView.selectAllAction(with: listView.km_EditingAreas().first)
  5832. }
  5833. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_AddLink) {
  5834. pdfToolbarController?.clickWithIdentify(KMPDFToolbar_edit_link_Identifier)
  5835. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_AddText) {
  5836. pdfToolbarController?.clickWithIdentify(KMPDFToolbar_edit_text_Identifier)
  5837. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_AddImage) {
  5838. pdfToolbarController?.clickWithIdentify(KMPDFToolbar_edit_image_Identifier)
  5839. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontBold) {
  5840. listView.setEditingTextarea_Bold()
  5841. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontItalic) {
  5842. listView.setEditingTextarea_Italic()
  5843. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontUnderline) {
  5844. listView.setEditingTextarea_Under()
  5845. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontStrikeout) {
  5846. listView.setEditingTextarea_Strikeout()
  5847. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontZoomIn) {
  5848. listView.zoomInEditTextFontSize()
  5849. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontZoomOut) {
  5850. listView.zoomInEditTextFontSize()
  5851. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontColor) {
  5852. let colorPanel = NSColorPanel.shared
  5853. colorPanel.setTarget(self)
  5854. colorPanel.setAction(#selector(editFontColorItemPanelAction(_:)))
  5855. colorPanel.orderFront(nil)
  5856. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontH1Name) {
  5857. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .h1)
  5858. self.updateEditPDFTextFontModel(model)
  5859. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontH2Name) {
  5860. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .h2)
  5861. self.updateEditPDFTextFontModel(model)
  5862. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontH3Name) {
  5863. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .h3)
  5864. self.updateEditPDFTextFontModel(model)
  5865. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontB1Name) {
  5866. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .b1)
  5867. self.updateEditPDFTextFontModel(model)
  5868. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontB2Name) {
  5869. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .b2)
  5870. self.updateEditPDFTextFontModel(model)
  5871. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontB3Name) {
  5872. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .b3)
  5873. self.updateEditPDFTextFontModel(model)
  5874. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontLeftAlight) {
  5875. listView.setEditingTextarea_Alignment(align:.left)
  5876. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontCenterAlight) {
  5877. listView.setEditingTextarea_Alignment(align:.center)
  5878. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontRightAlight) {
  5879. listView.setEditingTextarea_Alignment(align:.right)
  5880. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontJustAlight) {
  5881. listView.setEditingTextarea_Alignment(align:.justified)
  5882. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_RotateLeft) {
  5883. listView.rotateEditingAreas(90)
  5884. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_RotateRight) {
  5885. listView.rotateEditingAreas(-90)
  5886. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_HorizontalMirror) {
  5887. listView.reverseYAction()
  5888. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_VerticalMirror) {
  5889. listView.reverseXAction()
  5890. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_Crop) {
  5891. listView.cropAction()
  5892. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_Replace) {
  5893. listView.replaceImageEdit()
  5894. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RedactProperties) {
  5895. if let redactAnnotation = listView.activeAnnotation as? CPDFRedactAnnotation{
  5896. showRedactProperty(readactAnnotation: redactAnnotation)
  5897. }
  5898. }
  5899. }
  5900. func componentGroupDidDismiss(group: ComponentGroup?) {
  5901. }
  5902. func updateEditPDFTextFontModel(_ model: KMEditPDFTextFontModel) {
  5903. let fontName = model.fontName
  5904. let fontStyle = model.fontStyle
  5905. let fontSize = model.fontSize
  5906. let bold = model.bold
  5907. let italic = model.italic
  5908. let alignment = model.alignment
  5909. let font = CPDFFont(familyName: fontName, fontStyle: fontStyle)
  5910. listView.setEditingTextarea_font(font: font)
  5911. listView.setEditingTextarea_FontSize(size: fontSize)
  5912. listView.setEditingTextarea(isBold: bold)
  5913. listView.setEditingTextarea(isItalic: italic)
  5914. listView.setEditingTextarea_Alignment(align: alignment)
  5915. }
  5916. }
  5917. // MARK: - KMSnapshotWindowControllerDelegate
  5918. extension KMMainViewController: KMSnapshotWindowControllerDelegate {
  5919. func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
  5920. }
  5921. func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
  5922. return CGRectZero
  5923. }
  5924. func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
  5925. }
  5926. }
  5927. // MARK: - CPDFDocumentDelegate
  5928. extension KMMainViewController: CPDFDocumentDelegate {
  5929. func documentDidBeginDocumentFind(_ document: CPDFDocument!) {
  5930. }
  5931. func documentDidEndDocumentFind(_ document: CPDFDocument!) {
  5932. }
  5933. }
  5934. // MARK: - 测量代理CDistanceSettingWindowControllerDelegate
  5935. extension KMMainViewController : CDistanceSettingWindowControllerDelegate {
  5936. func distanceSettingWindowController(_ distanceSettingWindowController: CDistanceSettingWindowController, updateMeasureInfo measureInfo: CPDFMeasureInfo?) {
  5937. if measureInfo != nil {
  5938. if self.listView.activeAnnotations.count > 0 {
  5939. if self.listView.activeAnnotation.isKind(of: CPDFPolylineAnnotation.self) {
  5940. self.updateMeasureInfo((self.listView.activeAnnotation as! CPDFPolylineAnnotation).measureInfo, withNewMeasure: measureInfo)
  5941. } else if self.listView.activeAnnotation.isKind(of: CPDFPolygonAnnotation.self) {
  5942. self.updateMeasureInfo((self.listView.activeAnnotation as! CPDFPolygonAnnotation).measureInfo, withNewMeasure: measureInfo)
  5943. } else if let data = self.listView.activeAnnotation as? CPDFLineAnnotation, data.isMeasure {
  5944. self.updateMeasureInfo(data.measureInfo, withNewMeasure: measureInfo)
  5945. }
  5946. self.listView.setNeedsDisplayAnnotationViewFor(self.listView.activeAnnotation.page)
  5947. }
  5948. self.updateMeasureInfo(self.listView.distanceMeasureInfo, withNewMeasure: measureInfo)
  5949. self.updateMeasureInfo(self.listView.perimeterMeasureInfo, withNewMeasure: measureInfo)
  5950. self.updateMeasureInfo(self.listView.polygonAreaMeasureInfo, withNewMeasure: measureInfo)
  5951. self.updateMeasureInfo(self.listView.squareAreaMeasureInfo, withNewMeasure: measureInfo)
  5952. }
  5953. }
  5954. func updateMeasureInfo(_ measureInfo: CPDFMeasureInfo?, withNewMeasure newMeasure: CPDFMeasureInfo?) {
  5955. guard let measureInfo = measureInfo else { return }
  5956. guard let newMeasure = newMeasure else { return }
  5957. measureInfo.rulerBase = newMeasure.rulerBase
  5958. measureInfo.rulerTranslate = newMeasure.rulerTranslate
  5959. measureInfo.rulerBaseUnit = newMeasure.rulerBaseUnit
  5960. measureInfo.rulerTranslateUnit = newMeasure.rulerTranslateUnit
  5961. measureInfo.factor = newMeasure.factor
  5962. measureInfo.precision = newMeasure.precision
  5963. }
  5964. }
  5965. //MARK: - extension
  5966. extension KMMainViewController {
  5967. internal func removeNotifications() {
  5968. NotificationCenter.default.removeObserver(self)
  5969. }
  5970. func removeAllAnnotations() {
  5971. let alert = NSAlert()
  5972. alert.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  5973. alert.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  5974. alert.addButton(withTitle: NSLocalizedString("No", comment:""))
  5975. if (alert.runModal() != .alertFirstButtonReturn) {
  5976. return
  5977. }
  5978. DispatchQueue.main.async {
  5979. self.removeAllAnnotationsStore.store(t: self.listView)
  5980. }
  5981. }
  5982. //MARK: - AI
  5983. func showAITypeChooseView(aiConfigType: AIConfigType) -> Void {
  5984. if (self.document != nil) {
  5985. AIChatInfoManager.defaultManager.currentFilePath = (self.document?.documentURL.path)!
  5986. } else {
  5987. AIChatInfoManager.defaultManager.currentFilePath = ""
  5988. }
  5989. let windowVC: AINewConfigWindowController = AINewConfigWindowController.currentWC()
  5990. windowVC.chooseCurFileHandle = {[unowned self] windowVC in
  5991. if AIChatInfoManager.defaultManager.currentFilePath.isEmpty == false {
  5992. let documentArray = NSDocumentController.shared.documents
  5993. var didFileEdit: Bool = false
  5994. var curDoc: KMMainDocument!
  5995. for document in documentArray {
  5996. if document.fileURL?.path == AIChatInfoManager.defaultManager.currentFilePath {
  5997. didFileEdit = document.isDocumentEdited
  5998. curDoc = document as! KMMainDocument
  5999. break
  6000. }
  6001. }
  6002. if didFileEdit {
  6003. let tempFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(AIChatInfoManager.defaultManager.currentFilePath.lastPathComponent)
  6004. if FileManager.default.fileExists(atPath: tempFileURL.path) {
  6005. do {
  6006. try FileManager.default.removeItem(at: tempFileURL)
  6007. } catch {
  6008. }
  6009. }
  6010. if curDoc != nil {
  6011. curDoc.mainViewController?.SaveTempPDFDocumentToURLPath(tempPath: tempFileURL.path)
  6012. }
  6013. }
  6014. windowVC.window?.becomeMain()
  6015. }
  6016. }
  6017. if windowVC.window?.isVisible == true && windowVC.didSetOriginFrame == true {
  6018. } else {
  6019. var windowRect = windowVC.window?.frame
  6020. windowRect!.origin.x = NSMaxX(self.view.window!.frame) - (windowRect?.size.width)!
  6021. windowRect!.origin.y = NSMaxY(self.view.window!.frame) - (windowRect?.size.height)! - 64
  6022. windowVC.window?.setFrame(windowRect!, display: true)
  6023. windowVC.didSetOriginFrame = true
  6024. }
  6025. windowVC.eventLabel = "AITools_Tbr"
  6026. windowVC.showWindow(nil)
  6027. if (aiConfigType != .none) {
  6028. windowVC.eventLabel = "AITools_Start"
  6029. if self.listView.currentSelection?.string()?.isEmpty == false {
  6030. windowVC.setCurrentPDFSelection(self.listView.currentSelection.string())
  6031. }
  6032. windowVC.chooseAIFunctionWithType(aiConfigType)
  6033. }
  6034. }
  6035. @objc func aiTipIconViewShowStateChangeNoti() {
  6036. }
  6037. //MARK: - 引导
  6038. func loadFunctionGuide() -> Void {
  6039. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  6040. if self.view.window != nil {
  6041. self.loadOpenFileFunctionGuide(.openFileNormal)
  6042. }
  6043. }
  6044. }
  6045. func loadOpenFileFunctionGuide(_ showType: KMGuideInfoType) -> Void {
  6046. if showType == .openFileNormal && KMGuideInfoWindowController.availableShow(.openFileNormal) {
  6047. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6048. guard let guideWC = self.guideInfoWindowController else { return }
  6049. guideWC.type = .openFileNormal
  6050. // guideWC.openPanelRect = (self.view.window?.contentView?.convert(leftPanelItem.frame, from: leftPanelItem.superview)) ?? .zero
  6051. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6052. guideWC.normalGuideFinishHandle = { [weak self] windowVC in
  6053. }
  6054. guideWC.finishHandle = { [weak self] windowVC, type in
  6055. if type == .windowNewFinish ||
  6056. type == . windowDigitalFinish {
  6057. self?.checkFirstTrialController()
  6058. }
  6059. }
  6060. guideWC.openFileToggleHandle = { [weak self] windowVC, type in
  6061. self?.checkFirstTrialController()
  6062. }
  6063. var rect = self.view.window!.frame
  6064. rect.size.height -= 20
  6065. guideWC.window?.setFrame(rect, display: false)
  6066. guideWC.window?.minSize = rect.size
  6067. guideWC.window?.maxSize = rect.size
  6068. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6069. guideWC.show()
  6070. } else if showType == .digitalSignGuide && KMGuideInfoWindowController.availableShow(.digitalSignGuide) {
  6071. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6072. guard let guideWC = self.guideInfoWindowController else { return }
  6073. guideWC.type = .digitalSignGuide
  6074. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6075. guideWC.finishHandle = { [weak self] windowVC, type in
  6076. self?.checkFirstTrialController()
  6077. }
  6078. var rect = self.view.window!.frame
  6079. rect.size.height -= 20
  6080. guideWC.window?.setFrame(rect, display: false)
  6081. guideWC.window?.minSize = rect.size
  6082. guideWC.window?.maxSize = rect.size
  6083. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6084. guideWC.show()
  6085. } else if showType == .pdfCompareGuide && KMGuideInfoWindowController.availableShow(.pdfCompareGuide) {
  6086. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  6087. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6088. guard let guideWC = self.guideInfoWindowController else { return }
  6089. guideWC.type = .pdfCompareGuide
  6090. guard let win = self.view.window else {
  6091. return
  6092. }
  6093. guideWC.finishHandle = { [weak self] winC, type in
  6094. if type == .windowNewFinish {
  6095. DispatchQueue.main.async {
  6096. self?.loadOpenFileFunctionGuide(.measureGuide)
  6097. }
  6098. return
  6099. }
  6100. }
  6101. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6102. var rect = self.view.window!.frame
  6103. rect.size.height -= 20
  6104. guideWC.window?.setFrame(rect, display: false)
  6105. guideWC.window?.minSize = rect.size
  6106. guideWC.window?.maxSize = rect.size
  6107. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6108. guideWC.show()
  6109. }
  6110. } else if showType == .measureGuide && KMGuideInfoWindowController.availableShow(.measureGuide) {
  6111. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  6112. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6113. guard let guideWC = self.guideInfoWindowController else { return }
  6114. guard let _ = self.view.window else {
  6115. return
  6116. }
  6117. guideWC.type = .measureGuide
  6118. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6119. var rect = self.view.window?.frame ?? .zero
  6120. rect.size.height -= 20
  6121. guideWC.window?.setFrame(rect, display: false)
  6122. guideWC.window?.minSize = rect.size
  6123. guideWC.window?.maxSize = rect.size
  6124. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6125. guideWC.show()
  6126. }
  6127. } else if showType == .convertGuide && KMGuideInfoWindowController.availableShow(.convertGuide) {
  6128. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  6129. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6130. guard let guideWC = self.guideInfoWindowController else { return }
  6131. guideWC.type = .convertGuide
  6132. guard let win = self.view.window else {
  6133. return
  6134. }
  6135. guideWC.purchaseHandle = { [weak self] windowVC in
  6136. #if VERSION_DMG
  6137. if IAPProductsManager.default().isAvailableAllFunction() {
  6138. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  6139. //Convert:
  6140. } else {
  6141. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  6142. limitWC.continueBlock = { windowController in
  6143. }
  6144. limitWC.window?.center()
  6145. limitWC.showWindow(nil)
  6146. }
  6147. } else {
  6148. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  6149. }
  6150. #else
  6151. if IAPProductsManager.default().isAvailableAllFunction() {
  6152. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  6153. //Convert:
  6154. } else {
  6155. var vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  6156. vc.showWindow(nil)
  6157. }
  6158. } else {
  6159. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  6160. }
  6161. #endif
  6162. }
  6163. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6164. var rect = self.view.window?.frame ?? .zero
  6165. rect.size.height -= 20
  6166. guideWC.window?.setFrame(rect, display: false)
  6167. guideWC.window?.minSize = rect.size
  6168. guideWC.window?.maxSize = rect.size
  6169. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6170. guideWC.show()
  6171. }
  6172. } else {
  6173. }
  6174. }
  6175. func checkFirstTrialController() -> Void {
  6176. #if VERSION_DMG
  6177. //打开文档后引导相关
  6178. if VerificationManager.default().status == .none {
  6179. let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  6180. let lastVersion = UserDefaults.standard.object(forKey: "SKLastTrialVersionMainDocumentLaunchedKey") as? String ?? ""
  6181. if lastVersion.isEmpty || lastVersion != appVersion {
  6182. UserDefaults.standard.setValue(appVersion, forKey: "SKLastTrialVersionMainDocumentLaunchedKey")
  6183. UserDefaults.standard.synchronize()
  6184. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  6185. }
  6186. }
  6187. #endif
  6188. }
  6189. // MARK: - Private Methods
  6190. func isPDFPageCountExceedsLimit(filePath: String) -> Bool {
  6191. let url = URL(fileURLWithPath: filePath)
  6192. guard let document = PDFDocument(url: url) else {
  6193. return false
  6194. }
  6195. let pageCount = document.pageCount
  6196. return pageCount > 30
  6197. }
  6198. // MARK: - Redact 【标记密文】
  6199. func redactApplyAction() {
  6200. let returnCode = KMAlertTool.runModelForMainThread_r(message: "", informative: KMLocalizedString("This will permanently remove the redacted information from this document. Once you save this document, you won’t be able to retrieve the redacted information."), buttons: [KMLocalizedString("Apply"), KMLocalizedString("Cancel")])
  6201. if returnCode == .alertFirstButtonReturn {
  6202. DispatchQueue.main.async {
  6203. self.saveAsPath()
  6204. }
  6205. }
  6206. }
  6207. func redactMultipageAction(redactAnnotation:CPDFRedactAnnotation) {
  6208. let pagesWindowController = KMRedactSelectPagesWindowController(document: listView.document)
  6209. pagesWindowController.own_beginSheetModal(for: self.view.window) { result in
  6210. }
  6211. pagesWindowController.callback = { [weak self] pages in
  6212. if pages.count > 0 {
  6213. self?.listView.redactAddAnnotationPages(pages, redactAnnotation: redactAnnotation)
  6214. }
  6215. }
  6216. }
  6217. func setPropertiesDefault(annotation:CPDFRedactAnnotation) {
  6218. CPDFRedactAnnotation.update_defaultOutlineColor(annotation.borderColor())
  6219. CPDFRedactAnnotation.update_defaultFillColor(annotation.interiorColor())
  6220. CPDFRedactAnnotation.update_defaultTextColor(annotation.fontColor())
  6221. CPDFRedactAnnotation.update_defaultFontSize(annotation.fontSize)
  6222. CPDFRedactAnnotation.update_defaultFontAlignment(annotation.alignment())
  6223. CPDFRedactAnnotation.update_defaultOverlayText(annotation.overlayText())
  6224. }
  6225. func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
  6226. let windowController = KMRedactConfirmWindowController(type)
  6227. self.currentWindowController = windowController
  6228. self.view.window?.beginSheet(windowController.window!)
  6229. windowController.itemClick = { [weak self] index in
  6230. if (index == 2) { /// 取消
  6231. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  6232. self?.currentWindowController = nil
  6233. callback()
  6234. return
  6235. }
  6236. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  6237. self?.currentWindowController = nil
  6238. let panel = NSSavePanel()
  6239. panel.nameFieldStringValue = "[新文件]"+((self?.listView.document?.documentURL.lastPathComponent) ?? "")
  6240. let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  6241. button.state = .on
  6242. panel.accessoryView = button
  6243. panel.isExtensionHidden = true
  6244. panel.beginSheetModal(for: (self?.view.window!)!) { response in
  6245. if response != .OK {
  6246. callback()
  6247. return
  6248. }
  6249. if (type == .redactOne) {
  6250. let anno = self!.listView.activeAnnotation
  6251. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  6252. callback()
  6253. return
  6254. }
  6255. (anno as! CPDFRedactAnnotation).applyRedaction()
  6256. } else if (type == .redactAll) {
  6257. self?.listView.document?.applyRedactions()
  6258. } else if (type == .eraserOne) {
  6259. let anno = self!.listView.activeAnnotation
  6260. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  6261. callback()
  6262. return
  6263. }
  6264. anno?.page.erasureRedact(from: anno!.bounds)
  6265. } else if (type == .eraserAll) {
  6266. KMRedactTools.eraserDocument((self?.listView.document)!) { result, errorAnno in
  6267. if (result == false) {
  6268. callback()
  6269. return
  6270. }
  6271. }
  6272. }
  6273. self!.listView.document?.write(to: panel.url)
  6274. if (button.state == .on) {
  6275. NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
  6276. }
  6277. } else {
  6278. NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
  6279. }
  6280. callback()
  6281. }
  6282. }
  6283. }
  6284. func enterRedactAlert() {
  6285. if UserDefaults.standard.object(forKey: "kRedact") != nil {
  6286. return
  6287. }
  6288. let alert = NSAlert()
  6289. alert.alertStyle = .informational
  6290. alert.informativeText = KMLocalizedString("Redaction allows you to permanently mask and remove sensitive content.")
  6291. alert.messageText = KMLocalizedString("""
  6292. Redaction requires two steps:
  6293. 1. Mark for Redaction
  6294. 2. Apply Redactions
  6295. Note: Redactions are not applied permanently until you select Apply Redactions.
  6296. """)
  6297. alert.addButton(withTitle: KMLocalizedString("OK"))
  6298. alert.showsSuppressionButton = true
  6299. let response = alert.runModal()
  6300. if response.rawValue == 1000 {
  6301. if alert.suppressionButton?.state == .on {
  6302. UserDefaults.standard.set("YES", forKey: "kRedact")
  6303. UserDefaults.standard.synchronize()
  6304. }
  6305. }
  6306. }
  6307. // MARK: - 保存文档
  6308. internal func needSaveDocument() -> Bool {
  6309. if (self.isPDFDocumentEdited) {
  6310. return self.isPDFDocumentEdited
  6311. }
  6312. if (self.needSave) {
  6313. return self.needSave
  6314. }
  6315. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  6316. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  6317. return false
  6318. }
  6319. return true
  6320. }
  6321. internal func saveDocument(overlookDocumentIfEdited overlook: Bool = false) {
  6322. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  6323. if (overlook) {
  6324. document?.save(nil)
  6325. return
  6326. }
  6327. if (self.isPDFDocumentEdited) {
  6328. self.clearIsPDFDocumentEdited()
  6329. self.needSave = false
  6330. document?.save(nil)
  6331. return
  6332. }
  6333. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  6334. return
  6335. }
  6336. document?.save(nil)
  6337. }
  6338. internal func asyncSaveDocument(overlookDocumentIfEdited overlook: Bool = false, callback:@escaping KMCommonBlock) {
  6339. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  6340. if (overlook) {
  6341. DispatchQueue.main.async {
  6342. document?.save(nil)
  6343. callback()
  6344. }
  6345. return
  6346. }
  6347. if (self.isPDFDocumentEdited) {
  6348. self.clearIsPDFDocumentEdited()
  6349. self.needSave = false
  6350. DispatchQueue.main.async {
  6351. document?.save(nil)
  6352. callback()
  6353. }
  6354. return
  6355. }
  6356. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  6357. callback()
  6358. return
  6359. }
  6360. DispatchQueue.main.async {
  6361. document?.save(nil)
  6362. callback()
  6363. }
  6364. }
  6365. internal func saveDocumentWithProgressAlert(callback:@escaping KMCommonBlock) {
  6366. // 显示进度
  6367. AutoSaveManager.manager.isSaving = true
  6368. self.showProgressWindow(message: NSLocalizedString("Save", comment: "") + "PDF")
  6369. self.progressC?.maxValue = 3.0
  6370. self.progressC?.increment(by: 1.0)
  6371. // 保存文档
  6372. self.asyncSaveDocument { [weak self] params in
  6373. // 执行进度 [假进度]
  6374. self?.progressC?.increment(by: 1.0)
  6375. self?.progressC?.increment(by: 1.0)
  6376. DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
  6377. // 隐藏进度
  6378. self?.hiddenProgressWindow()
  6379. DispatchQueue.main.asyncAfter(deadline: .now()+1) {
  6380. AutoSaveManager.manager.isSaving = false
  6381. }
  6382. // 回调
  6383. callback()
  6384. }
  6385. }
  6386. }
  6387. func SaveTempPDFDocumentToURLPath(tempPath: String) {
  6388. self.document?.write(toFile: tempPath)
  6389. }
  6390. // MARK: - 定时保存
  6391. func addAutoSaveEvent() {
  6392. if (self.autoSaveTimer != nil) {
  6393. self.autoSaveTimer?.invalidate()
  6394. self.autoSaveTimer = nil
  6395. }
  6396. if self.document != nil {
  6397. self.autoSaveTimer = Timer.scheduledTimer(withTimeInterval: AutoSaveManager.manager.timeInterval * 60, repeats: true, block: { [weak self] timer in
  6398. self?.autoSaveTimerAction(timer)
  6399. })
  6400. }
  6401. self.checkAutoSaveInfo()
  6402. }
  6403. func checkAutoSaveInfo() {
  6404. guard let cnt = AutoSaveManager.manager.autoSavePaths?.count, cnt > 0 else {
  6405. return
  6406. }
  6407. if AutoSaveManager.manager.autoSaveAlertShow {
  6408. return
  6409. }
  6410. AutoSaveManager.manager.autoSaveDidEndAction = false
  6411. AutoSaveManager.manager.autoSaveAlertShow = true
  6412. let blockSaveWindow = AutoSavePopController()
  6413. blockSaveWindow.cancelHandle = { [weak self] windowController in
  6414. AutoSaveManager.manager.autoSaveDidEndAction = true
  6415. AutoSaveManager.manager.clearCache()
  6416. self?.km_quick_endSheet()
  6417. }
  6418. blockSaveWindow.confirmHandle = { [weak self] windowController in
  6419. self?.km_quick_endSheet()
  6420. self?.saveAutoSaveInfo()
  6421. }
  6422. self.km_beginSheet(windowC: blockSaveWindow)
  6423. }
  6424. func saveAutoSaveInfo() {
  6425. let openPanel = NSOpenPanel()
  6426. openPanel.canChooseDirectories = true
  6427. openPanel.canChooseFiles = false
  6428. openPanel.allowsMultipleSelection = false
  6429. let win = NSApp.keyWindow != nil ? NSApp.keyWindow : self.view.window
  6430. openPanel.beginSheetModal(for: win!) { result in
  6431. if (result == .OK) {
  6432. let folderPath = openPanel.url?.path ?? openPanel.url?.absoluteString
  6433. for path in AutoSaveManager.manager.opendPaths ?? [] {
  6434. let _path = path as? String
  6435. var newPath = "\(folderPath ?? "")/\(_path?.lastPathComponent ?? "")"
  6436. newPath = self.getValidFilePath(newPath)
  6437. do {
  6438. try FileManager.default.moveItem(atPath: _path ?? "", toPath: newPath)
  6439. } catch {
  6440. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: newPath)])
  6441. }
  6442. }
  6443. AutoSaveManager.manager.clearCache()
  6444. }
  6445. AutoSaveManager.manager.autoSaveDidEndAction = true
  6446. }
  6447. }
  6448. func autoSaveTimerAction(_ timer: Timer) {
  6449. if (self.document == nil || self.listView.document?.documentURL.path == nil) {
  6450. return
  6451. }
  6452. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  6453. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  6454. return
  6455. }
  6456. if let data = self.document?.isLocked, data {
  6457. return
  6458. }
  6459. if AutoSaveManager.manager.autoSaveEnabled == false {
  6460. return
  6461. }
  6462. let documentArray = NSDocumentController.shared.documents
  6463. var didFileEdit = false
  6464. for doc in documentArray {
  6465. if doc.fileURL?.path == self.document?.documentURL.path {
  6466. didFileEdit = doc.isDocumentEdited
  6467. break
  6468. }
  6469. }
  6470. if (didFileEdit == false) {
  6471. return
  6472. }
  6473. AutoSaveManager.manager.isSaving = true
  6474. let savePath = AutoSaveManager.manager.autoSaveWithPath(self.listView.document?.documentURL.path ?? "")
  6475. if (!self.document!.isLocked) {
  6476. self.document?.write(to: URL(fileURLWithPath: savePath))
  6477. }
  6478. DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
  6479. AutoSaveManager.manager.isSaving = false
  6480. }
  6481. }
  6482. func removeAutoSaveInfo() {
  6483. if self.autoSaveTimer != nil {
  6484. self.autoSaveTimer?.invalidate()
  6485. self.autoSaveTimer = nil
  6486. }
  6487. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  6488. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  6489. return
  6490. }
  6491. if AutoSaveManager.manager.autoSaveEnabled == false {
  6492. return
  6493. }
  6494. if self.document == nil || self.listView.document?.documentURL.path == nil {
  6495. return
  6496. }
  6497. AutoSaveManager.manager.removeAutoSavePath(self.listView.document?.documentURL.path ?? "")
  6498. }
  6499. // MARK: - 选择缩放模式
  6500. @objc public func selectZoom(_ type: KMPDFZoomType) {
  6501. switch type {
  6502. case .width:
  6503. self.listView.autoScales = true
  6504. break
  6505. case .fit:
  6506. if let pageHeight = self.listView.currentPage()?.size.height, pageHeight > 0 {
  6507. let pdfviewHeight = self.listView.bounds.size.height
  6508. self.listView.scaleFactor = pdfviewHeight/pageHeight
  6509. self.listView.autoScales = false
  6510. }
  6511. break
  6512. case .actualSize:
  6513. if self.listView.scaleFactor != 1.0 {
  6514. self.listView.scaleFactor = 1.0
  6515. self.listView.autoScales = false
  6516. }
  6517. break
  6518. case .zoom_In:
  6519. self.doZoomIn(nil)
  6520. break
  6521. case .zoom_Out:
  6522. self.doZoomOut(nil)
  6523. break
  6524. }
  6525. }
  6526. func doZoomIn(_ sender: Any?) {
  6527. if (self.listView.canZoomIn) {
  6528. self.listView.zoomIn(nil)
  6529. }
  6530. }
  6531. func doZoomOut(_ sender: Any?) {
  6532. if (self.listView.canZoomOut) {
  6533. self.listView.zoomOut(nil)
  6534. }
  6535. }
  6536. // MARK: - Event 监听
  6537. private func addEventMonitor() {
  6538. if (self.eventMonitor != nil) {
  6539. self.removeEventMonitor()
  6540. }
  6541. self.eventMonitor = NSEvent.addLocalMonitorForEvents(matching: [.scrollWheel, .leftMouseDown, .leftMouseUp]) { [weak self] event in
  6542. if (event.type == .scrollWheel && event.modifierFlags.contains(.option)) { // Alt + 鼠标滚轮
  6543. self?.listView.magnifyWheel(event)
  6544. return nil
  6545. } else if event.type == .leftMouseDown {
  6546. let point = event.locationInView(self?.listView ?? NSView())
  6547. let presentationDrawView = self?.listView.presentationDrawView
  6548. if let data = self?.interactionMode, data == .presentation,CGRectContainsPoint(self?.listView.frame ?? .zero, point),presentationDrawView?.isHidden == true { // 幻灯片模式下
  6549. if point.x >= (self?.listView.frame.maxX ?? 0) / 2 {
  6550. let can = self?.listView.canGoToNextPage() ?? false
  6551. if can {
  6552. self?.listView.goToNextPage(nil)
  6553. }
  6554. } else {
  6555. let can = self?.listView.canGoToPreviousPage() ?? false
  6556. if can {
  6557. self?.listView.goToPreviousPage(nil)
  6558. }
  6559. }
  6560. return nil
  6561. }
  6562. }
  6563. return event
  6564. }
  6565. }
  6566. private func removeEventMonitor() {
  6567. if (self.eventMonitor != nil) {
  6568. KMPrint("已移除事件监听")
  6569. NSEvent.removeMonitor(self.eventMonitor as Any)
  6570. self.eventMonitor = nil
  6571. }
  6572. }
  6573. func addKeyEventMonitor() {
  6574. if (self.keyEventMonitor != nil) {
  6575. self.removeKeyEventMonitor()
  6576. }
  6577. keyEventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
  6578. if event.keyCode == 53 {
  6579. if self?.listView.annotationType == .freeText {
  6580. self?.listView.keyDown(with: event)
  6581. }
  6582. } else {
  6583. }
  6584. return event
  6585. }
  6586. }
  6587. func removeKeyEventMonitor() {
  6588. if (self.keyEventMonitor != nil) {
  6589. KMPrint("removeKeyEventMonitor 已移除事件监听")
  6590. NSEvent.removeMonitor(self.keyEventMonitor as Any)
  6591. self.keyEventMonitor = nil
  6592. }
  6593. }
  6594. //MARK: - Mouse Event
  6595. override func mouseMoved(with event: NSEvent) {
  6596. self.view.window?.mouseMoved(with: event)
  6597. }
  6598. override func keyDown(with event: NSEvent) {
  6599. listView.keyDown(with: event)
  6600. }
  6601. // MARK: - Tools
  6602. func pdfViewCanHorizontalScroll() -> Bool {
  6603. let scroll = self.listView.scroll()
  6604. if (scroll == nil) {
  6605. return false
  6606. }
  6607. return scroll?.horizontalScroller?.isHidden == nil ? false : !(scroll!.horizontalScroller!.isHidden)
  6608. }
  6609. func pdfViewCanVerticalScroll() -> Bool {
  6610. let scroll = self.listView.scroll()
  6611. if (scroll == nil) {
  6612. return false
  6613. }
  6614. return scroll?.verticalScroller?.isHidden == nil ? false : !(scroll!.verticalScroller!.isHidden)
  6615. }
  6616. // MARK: - Public Methods
  6617. // 清理数据 [eg. 通知]
  6618. public func clearData() {
  6619. KMThumbnailCache.shared.clearCache()
  6620. self.removeNotifications()
  6621. if (self.listView.spellingTag() > 0) {
  6622. NSSpellChecker.shared.closeSpellDocument(withTag: self.listView.spellingTag())
  6623. }
  6624. self.removeAutoSaveInfo()
  6625. self.myDocument = nil
  6626. if textFieldSheet.window?.isVisible == true {
  6627. textFieldSheet.close()
  6628. }
  6629. if NSColorPanel.shared.isVisible == true {
  6630. NSColorPanel.shared.close()
  6631. }
  6632. if KMInfoWindowController.shared.window?.isVisible == true {
  6633. KMInfoWindowController.shared.close()
  6634. }
  6635. }
  6636. public func clearSecureOptions() {
  6637. self._secureOptions = nil
  6638. self.documentAttribute = nil
  6639. }
  6640. public func clearRemoveSecureFlag() {
  6641. self._removeSecureFlag = false
  6642. }
  6643. public func recordIsPDFDocumentEdited(type: KMSubscribeWaterMarkType = .none) {
  6644. km_synchronized(self) {
  6645. self.model.isPDFDocumentEdited = true
  6646. if type == .editText || type == .editImage {
  6647. }
  6648. if let _document = self.myDocument {
  6649. KMTools.setDocumentEditedState(document: _document)
  6650. }
  6651. }
  6652. }
  6653. public func clearIsPDFDocumentEdited() {
  6654. km_synchronized(self) {
  6655. self.model.isPDFDocumentEdited = false
  6656. }
  6657. }
  6658. func showSnapshots(setups: NSArray?) {
  6659. if self.listView.document != nil {
  6660. for setup in setups ?? [] {
  6661. let swc = KMSnapshotWindowController()
  6662. swc.delegate = self
  6663. swc.setPdfDocument(self.listView.document, setup: setup as? NSDictionary)
  6664. swc.setForceOnTop(self.interactionMode != .normal)
  6665. self.myDocument?.addWindowController(swc)
  6666. }
  6667. }
  6668. }
  6669. // MARK: - Noti Actions
  6670. internal func documentDidUnlockNotification(_ sender: Notification) {
  6671. if (self.listView.document != nil && self.listView.document.isEqual(to: sender.object)) {
  6672. if (self.myDocument == nil) {
  6673. return
  6674. }
  6675. if (self.listView.document.allowsPrinting && self.listView.document.allowsCopying) {
  6676. self.hiddenSecureLimitTip()
  6677. }
  6678. let isUnlockFromKeychain = (self.myDocument as? KMMainDocument)?.isUnlockFromKeychain ?? false
  6679. if (isUnlockFromKeychain || self.model.isSaveKeyChain == false) {
  6680. return
  6681. }
  6682. let type = SettingsManager.sharedInstance.keychainType
  6683. if (type == .never) {
  6684. return
  6685. }
  6686. if (type == .always) {
  6687. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  6688. return
  6689. }
  6690. // 保存到钥匙串
  6691. let alert = NSAlert()
  6692. alert.messageText = NSLocalizedString("Remember Password?", comment: "")
  6693. alert.informativeText = NSLocalizedString("Do you want to save this password in your Keychain?", comment: "")
  6694. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  6695. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  6696. if (alert.runModal() == .alertFirstButtonReturn) { // 保存密码
  6697. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  6698. return
  6699. }
  6700. }
  6701. }
  6702. internal func applicationWillTerminateNotification(_ sender: Notification) {
  6703. self.savePageNumberIfNeed()
  6704. self.saveDocument()
  6705. }
  6706. }