KMMainViewController.swift 373 KB

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