KMMainViewController.swift 371 KB

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