KMMainViewController.swift 329 KB

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