KMMainViewController.swift 426 KB

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