KMMainViewController.swift 236 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792
  1. //
  2. // KMMainViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. let MIN_SIDE_PANE_WIDTH: NSNumber = 264 // 最小值
  10. let CPDFViewIsReadModeKey = "kKMPDFViewIsReadMode"
  11. let CPDFViewLeftSidePaneWidthKey = "CPDFOfficeLeftSidePaneWidthKey"
  12. struct KMNMWCFlags {
  13. var settingUpWindow: Bool = true
  14. }
  15. @objcMembers class KMMainViewController: KMBaseViewController, NSTextFieldDelegate {
  16. @IBOutlet var contendBox: NSBox!
  17. @IBOutlet var toolbarBox: NSBox! //工具栏Box
  18. @IBOutlet var bottomContendBox: NSBox! //
  19. @IBOutlet var sidebarBox: NSBox! //左侧边栏Box
  20. @IBOutlet var infoContendSplitView: KMPDFSplitView!
  21. @IBOutlet var infoSplitLeftView: NSView!
  22. @IBOutlet var infoSplitRightView: NSView!
  23. @IBOutlet var infoSplitCenterView: NSView!
  24. @IBOutlet var pdfSplitView: NSSplitView!
  25. @IBOutlet var pdfSplitTopView: NSView!
  26. @IBOutlet var pdfSplitBottomView: NSView!
  27. @IBOutlet var toolbarBoxHeightConst: NSLayoutConstraint!
  28. @IBOutlet var infoSplitViewLeftConst: NSLayoutConstraint!
  29. @IBOutlet var infoSplitViewRightConst: NSLayoutConstraint!
  30. var viewManager: KMPDFViewManager = KMPDFViewManager.init()
  31. var toolbarManager: KMPDFToolbarManager = KMPDFToolbarManager.init()
  32. var listView: CPDFListView = CPDFListView.init()
  33. var document: CPDFDocument?
  34. var myDocument: NSDocument?
  35. var isFirstOpen: Bool = true
  36. var insertDocuments: Set<CPDFDocument> = [] //插入新文档时,SDK会出现崩溃,临时记录
  37. //工具栏
  38. private var pdfToolbarController: KMPDFToolbarController?
  39. //BOTA SideBar
  40. private var sideBarController: KMPDFSideBarController?
  41. //页面编辑
  42. private var pageEditViewController: KMNPageEditViewController?
  43. //DisplaySetting
  44. private var displaySettingController: KMNDisplayViewController?
  45. //SPlitPDF分屏视图
  46. private var splitPDFController: KMSplitPDFViewController?
  47. private var pdfBottomToolbar: KMSplitToolbar?
  48. //PPT操作界面
  49. var presentationTopViewController: KMPresentationTopViewController?
  50. //Edit
  51. var editToolbarView: KMEditToolbarView?
  52. //水印
  53. var watermarkViewController: KMWatermarkController?
  54. //背景
  55. var backgroundViewController: KMBackgroundController?
  56. //Header&Footer
  57. var headerFooterViewController: KMHeaderFooterController?
  58. //Bates
  59. var batesViewController: KMBatesController?
  60. //Crop
  61. var cropController: KMCropController?
  62. //左边
  63. var botaViewController: KMNLeftSideViewController?
  64. //右边
  65. var rightSideController: KMRightSideController?
  66. //MARK: - 旧代码,有需要用到的拿出来,写好备注
  67. @IBOutlet weak var leftView: NSView!
  68. var model = KMMainModel()
  69. //自动滚动
  70. var autoFlowOptionsSheetController: KMAutoFlowOptionsSheetController?
  71. //Search
  72. var searchIndex: Int = 0
  73. //Form
  74. var formAlertView: KMFormAlertView?
  75. //Secure
  76. var secureAlertView: KMSecureAlertView?
  77. //对比
  78. var isCompareModel: Bool = false {
  79. didSet {
  80. }
  81. }
  82. //合并
  83. var mergeWindowController: KMMergeWindowController?
  84. //密码弹窗
  85. var passwordWindow: KMPasswordInputWindow?
  86. //对比
  87. var compressWindowController: KMCompressWindowController?
  88. //
  89. var securityWindowController: KMSecurityWindowController?
  90. //引导
  91. var guideInfoWindowController: KMGuideInfoWindowController?
  92. //春季活动
  93. var recommondPopWindowVC: KMRecommondPopWindow?
  94. var removeAllAnnotationsStore = KMPDFViewRemoveAllAnnotationsStore()
  95. private var _needSave = false
  96. var needSave: Bool {
  97. set {
  98. _needSave = newValue
  99. if (_needSave == false) {
  100. self.clearIsPDFDocumentEdited()
  101. }
  102. }
  103. get {
  104. return _needSave
  105. }
  106. }
  107. var isPDFDocumentEdited: Bool {
  108. get {
  109. return self.model.isPDFDocumentEdited
  110. }
  111. }
  112. var newMwcFlags = KMNMWCFlags(settingUpWindow: true)
  113. var leftSideViewController: KMLeftSideViewController = KMLeftSideViewController.init(type: KMLeftMethodMode())
  114. var searchResults: [KMSearchMode] = []
  115. var mwcFlags: MwcFlags = MwcFlags()
  116. weak var browserWindowController: KMBrowserWindowController? //慎直接使用这个方法
  117. var currentWindowController: NSWindowController!
  118. var savedNormalSetup: NSMutableDictionary = NSMutableDictionary()
  119. //数字签名
  120. var digitalSignController: KMPDFDigitalSignViewController?
  121. var redactController: KMPDFRedactViewController!
  122. let CPDFOfficeLeftSidePaneWidthKey = "CPDFOfficeLeftSidePaneWidthKey"
  123. let CPDFOfficeRightSidePaneWidthKey = "CPDFOfficeRightSidePaneWidthKey"
  124. var extract: KMExtractImageWindowController?
  125. var functionWidth: Double {
  126. get {
  127. if self.viewManager.isPDFReadMode {
  128. if !self.model.isShowBOTA {
  129. return 0
  130. }
  131. }
  132. return 48-4
  133. }
  134. }
  135. var pageNumber: UInt?
  136. var pdfEditController: KMPDFEditViewController? {
  137. get {
  138. return self.getPDFEditController()
  139. }
  140. }
  141. var autoSaveTimer: Timer?
  142. private var _documentFirstLoad: Bool = true
  143. var eventMonitor: Any?
  144. var keyEventMonitor: Any?
  145. var mouseRightMenuEvent: NSEvent?
  146. lazy private var homeVC: KMNHomeViewController? = {
  147. let vc = KMNHomeViewController()
  148. return vc
  149. }()
  150. fileprivate var _secureOptions: [CPDFDocumentWriteOption : Any]?
  151. var secureOptions: [CPDFDocumentWriteOption : Any]? {
  152. get {
  153. return self._secureOptions
  154. }
  155. }
  156. var documentAttribute: [CPDFDocumentAttribute : Any]?
  157. fileprivate var _removeSecureFlag = false
  158. var removeSecureFlag: Bool {
  159. get {
  160. return self._removeSecureFlag
  161. }
  162. }
  163. fileprivate var _saveWatermarkFlag = false
  164. var saveWatermarkFlag: Bool {
  165. get {
  166. return self._saveWatermarkFlag
  167. }
  168. }
  169. private var mainWindow_: NSWindow?
  170. var mainWindow: NSWindow? {
  171. get {
  172. return self.mainWindow_
  173. }
  174. set {
  175. self.mainWindow_ = newValue
  176. }
  177. }
  178. var setDocument: CPDFDocument? {
  179. get {
  180. return document
  181. }
  182. set {
  183. if document != newValue {
  184. document = newValue
  185. }
  186. listView.document = document
  187. botaViewController?.changeDocument(document: document)
  188. }
  189. }
  190. var setPageNumber: UInt {
  191. get {
  192. return pageNumber!
  193. }
  194. set {
  195. let pageCount = listView.document?.pageCount ?? 0
  196. var value = newValue
  197. if value > pageCount {
  198. value = pageCount
  199. }
  200. if value > 0 && listView.currentPage().pageIndex() != value-1 {
  201. listView.go(to: listView.document?.page(at: value-1))
  202. }
  203. if pageNumber != value {
  204. pageNumber = value
  205. }
  206. }
  207. }
  208. var distanceMeasureInfoWindowController: CDistanceMeasureInfoWindowController?
  209. var perimeterMeasureInfoWindowController: CPerimeterMeasureInfoWindowController?
  210. var areaMeasureInfoWindowController: CAreaMeasureInfoWindowController?
  211. //MARK: - func
  212. deinit {
  213. NotificationCenter.default.removeObserver(self)
  214. self.listView.delegate = nil
  215. self.listView.document?.delegate = nil
  216. self.removeEventMonitor()
  217. self.removeKeyEventMonitor()
  218. }
  219. override func viewDidLoad() {
  220. super.viewDidLoad()
  221. // Do view setup here.
  222. setupData()
  223. setupUI()
  224. NotificationCenter.default.addObserver(self, selector: #selector(pdfViewScrollViewDidScroll), name: NSScrollView.didLiveScrollNotification, object: listView.documentView())
  225. NotificationCenter.default.addObserver(self, selector: #selector(pageCountChangedNotification), name: NSNotification.Name.CPDFDocumentPageCountChanged, object: listView.document)
  226. loadUserDefaultsData()
  227. }
  228. override func viewDidAppear() {
  229. super.viewDidAppear()
  230. reloadPopUIWindow()
  231. }
  232. override func viewDidDisappear() {
  233. super.viewDidDisappear()
  234. toggleClosePopUIWindow()
  235. }
  236. //MARK: - private
  237. func setupUI() {
  238. initPDFView()
  239. initToolbar()
  240. initSideBar()
  241. setUpSplitView()
  242. }
  243. func setupData() {
  244. toolbarManager.pdfViewManager = viewManager
  245. if (UserDefaults.standard.object(forKey: CPDFViewLeftSidePaneWidthKey) != nil) {
  246. UserDefaults.standard.set(MIN_SIDE_PANE_WIDTH, forKey: CPDFViewLeftSidePaneWidthKey)
  247. UserDefaults.standard.synchronize()
  248. }
  249. newMwcFlags.settingUpWindow = true
  250. }
  251. //MARK: - document load成功
  252. private func documentLoadComplete() {
  253. loadUserDefaultsData()
  254. initLeftSideController()
  255. activityLoadMethod()
  256. let readModel = UserDefaults.standard.bool(forKey: CPDFViewIsReadModeKey)
  257. if readModel == true {
  258. self.openPDFReadMode()
  259. }
  260. toggleCloseLeftSide() //根据偏好设置显示左边栏状态
  261. newMwcFlags.settingUpWindow = false
  262. }
  263. //MARK: - 活动加载相关
  264. private func activityLoadMethod() {
  265. }
  266. private func loadUserDefaultsData() {
  267. applyLeftSideWidth(0, rightSideWidth: 0) //初始打开左边栏
  268. }
  269. func applyLeftSideWidth(_ leftSideWidth: CGFloat, rightSideWidth: CGFloat) -> Void {
  270. infoContendSplitView.setPosition(infoContendSplitView.maxPossiblePositionOfDivider(at: 1) - infoContendSplitView.dividerThickness - rightSideWidth, ofDividerAt: 1)
  271. infoContendSplitView.setPosition(leftSideWidth, ofDividerAt: 0)
  272. }
  273. //MARK: - PDFView
  274. func initPDFView() {
  275. listView.autoresizingMask = [.width, .height]
  276. listView.autoScales = true
  277. listView.delegate = self
  278. listView.pdfListViewDelegate = self
  279. listView.document = self.document
  280. reloadPDFSplitInfo()
  281. }
  282. func updatePDFViewAnnotationMode() {
  283. let toolbarMode = viewManager.toolMode
  284. let subToolMode = viewManager.subToolMode
  285. listView.isHidden = false
  286. if toolbarMode == .None {
  287. listView.annotationType = .unkown
  288. listView.toolMode = .noteToolMode
  289. } else if toolbarMode == .Markup {
  290. //MARK: -Markup
  291. listView.toolMode = .noteToolMode
  292. if subToolMode == .None {
  293. listView.annotationType = .unkown
  294. } else if subToolMode == .Highlight {
  295. listView.annotationType = .highlight
  296. } else if subToolMode == .Underline {
  297. listView.annotationType = .underline
  298. } else if subToolMode == .Waveline {
  299. listView.annotationType = .squiggly
  300. } else if subToolMode == .Strikethrough {
  301. listView.annotationType = .strikeOut
  302. } else if subToolMode == .Text {
  303. listView.annotationType = .freeText
  304. } else if subToolMode == .Note {
  305. listView.annotationType = .anchored
  306. } else if subToolMode == .Pen {
  307. listView.annotationType = .ink
  308. } else if subToolMode == .Eraser {
  309. listView.annotationType = .eraser
  310. } else if subToolMode == .Rectangle {
  311. listView.annotationType = .square
  312. } else if subToolMode == .Circle {
  313. listView.annotationType = .circle
  314. } else if subToolMode == .Arrow {
  315. listView.annotationType = .arrow
  316. } else if subToolMode == .Line {
  317. listView.annotationType = .line
  318. } else if subToolMode == .Measure {
  319. listView.toolMode = .measureToolMode
  320. listView.annotationType = .line
  321. } else if subToolMode == .Stamp {
  322. listView.annotationType = .stamp
  323. } else if subToolMode == .Sign {
  324. listView.annotationType = .signSignature
  325. }
  326. } else if toolbarMode == .Edit {
  327. //MARK: -编辑
  328. if subToolMode == .None {
  329. if listView.toolMode != .editPDFToolMode {
  330. listView.toolMode = .editPDFToolMode
  331. listView.configPDFEditingInfo()
  332. }
  333. listView.setShouAddEdit([])
  334. listView.change([.text, .image])
  335. } else if subToolMode == .Edit_text {
  336. if listView.toolMode != .editPDFToolMode {
  337. listView.toolMode = .editPDFToolMode
  338. listView.configPDFEditingInfo()
  339. }
  340. listView.setShouAddEdit([.text])
  341. listView.change(.text)
  342. } else if subToolMode == .Edit_Image {
  343. if listView.toolMode != .editPDFToolMode {
  344. listView.toolMode = .editPDFToolMode
  345. listView.configPDFEditingInfo()
  346. }
  347. listView.setShouAddEdit([.image])
  348. listView.change(.image)
  349. } else if subToolMode == .Edit_Link {
  350. listView.toolMode = .noteToolMode
  351. listView.annotationType = .link
  352. } else if subToolMode == .Edit_Crop {
  353. listView.isHidden = true
  354. }
  355. if viewManager.editType == .watermark ||
  356. viewManager.editType == .background ||
  357. viewManager.editType == .header_Footer ||
  358. viewManager.editType == .bates {
  359. listView.isHidden = true
  360. }
  361. if subToolMode != .Edit_Crop {
  362. removeCropController()
  363. }
  364. } else if toolbarMode == .Form {
  365. //MARK: -Form表单
  366. listView.toolMode = .formToolMode
  367. if subToolMode == .None {
  368. listView.annotationType = .unkown
  369. } else if subToolMode == .Form_text {
  370. listView.annotationType = .textField
  371. } else if subToolMode == .Form_checkbox {
  372. listView.annotationType = .checkBox
  373. } else if subToolMode == .Form_radio {
  374. listView.annotationType = .radioButton
  375. } else if subToolMode == .Form_list {
  376. listView.annotationType = .listMenu
  377. } else if subToolMode == .Form_dropdown {
  378. listView.annotationType = .comboBox
  379. } else if subToolMode == .Form_OK {
  380. listView.annotationType = .actionButton
  381. } else if subToolMode == .Form_digitalSign {
  382. }
  383. } else if toolbarMode == .Fill {
  384. //MARK: -填充
  385. listView.toolMode = .selfSignMode
  386. if subToolMode == .None {
  387. } else if subToolMode == .Fill_tick {
  388. listView.annotationType = .signTure
  389. } else if subToolMode == .fill_fork {
  390. listView.annotationType = .signFalse
  391. } else if subToolMode == .fill_rectangle {
  392. listView.annotationType = .signCircle
  393. } else if subToolMode == .fill_line {
  394. listView.annotationType = .signLine
  395. } else if subToolMode == .fill_dot {
  396. listView.annotationType = .signDot
  397. } else if subToolMode == .fill_date {
  398. listView.annotationType = .signDate
  399. } else if subToolMode == .fill_sign {
  400. listView.annotationType = .signSignature
  401. }
  402. } else if toolbarMode == .Convert {
  403. //MARK: -转档
  404. listView.toolMode = .textToolMode
  405. } else if toolbarMode == .Protect {
  406. //MARK: -Protect
  407. listView.toolMode = .textToolMode
  408. } else if toolbarMode == .Tools {
  409. //MARK: -工具
  410. listView.toolMode = .textToolMode
  411. }
  412. }
  413. func showOrHideNotes() {
  414. self.listView.hideNotes = !self.listView.hideNotes
  415. if self.listView.hideNotes {
  416. listView.annotationType = .unkown
  417. }
  418. for note in self.listView.notes as? [CPDFAnnotation] ?? [] {
  419. if note.isForm() {
  420. note.setAnnotationShouldDisplay(true)
  421. note.setHidden(false)
  422. }
  423. }
  424. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  425. }
  426. //MARK: - SplitView
  427. func setUpSplitView() {
  428. infoContendSplitView.wantsLayer = true
  429. infoContendSplitView.layer?.backgroundColor = NSColor.clear.cgColor
  430. infoContendSplitView.delegate = self
  431. infoContendSplitView.layer?.masksToBounds = true
  432. }
  433. func setupSplitPDFController() {
  434. if splitPDFController == nil {
  435. splitPDFController = KMSplitPDFViewController.init()
  436. }
  437. splitPDFController?.view.frame = pdfSplitBottomView.bounds
  438. splitPDFController?.view.autoresizingMask = [.height, .width]
  439. splitPDFController?.viewManager = self.viewManager
  440. splitPDFController?.delegate = self
  441. splitPDFController?.reloadData()
  442. pdfSplitBottomView.addSubview(splitPDFController!.view)
  443. }
  444. //MARK: - 工具栏
  445. func initToolbar() {
  446. toolbarBox.borderWidth = 0
  447. if pdfToolbarController == nil {
  448. pdfToolbarController = KMPDFToolbarController.init()
  449. }
  450. pdfToolbarController?.view.frame = toolbarBox.bounds
  451. pdfToolbarController?.view.autoresizingMask = [.width, .height]
  452. pdfToolbarController?.delegate = self
  453. toolbarBox.contentView = pdfToolbarController?.view
  454. pdfToolbarController?.viewManager = viewManager
  455. pdfToolbarController?.toolbarManager = toolbarManager
  456. pdfToolbarController?.pdfView = listView
  457. pdfToolbarController?.setUpData()
  458. refreshToolbarViewHeightInfo()
  459. }
  460. func refreshToolbarViewHeightInfo() {
  461. let _manager = viewManager
  462. if _manager.isPageEditMode {
  463. toolbarBoxHeightConst.constant = 80
  464. } else if _manager.editType == .watermark || _manager.editType == .background || _manager.editType == .header_Footer || _manager.editType == .bates {
  465. toolbarBoxHeightConst.constant = 40
  466. } else if _manager.toolMode == .Markup ||
  467. _manager.toolMode == .Edit ||
  468. _manager.toolMode == .Form ||
  469. _manager.toolMode == .Fill ||
  470. _manager.toolMode == .Convert ||
  471. _manager.toolMode == .Protect ||
  472. _manager.toolMode == .Tools {
  473. toolbarBoxHeightConst.constant = 80
  474. } else {
  475. toolbarBoxHeightConst.constant = 40
  476. }
  477. }
  478. func toolbarViewModeChanged() {
  479. updatePDFViewAnnotationMode()
  480. if viewManager.showRightSide == true {
  481. toggleOpenRightSide()
  482. } else {
  483. toggleCloseRightSide()
  484. }
  485. }
  486. //MARK: - 侧边工具栏
  487. func initSideBar() {
  488. sidebarBox.borderWidth = 0
  489. if sideBarController == nil {
  490. sideBarController = KMPDFSideBarController.init()
  491. }
  492. sideBarController?.view.frame = sidebarBox.bounds
  493. sideBarController?.view.autoresizingMask = [.width, .height]
  494. sidebarBox.contentView = sideBarController?.view
  495. sideBarController?.pdfView = listView
  496. sideBarController?.delegate = self
  497. sideBarController?.pdfViewManager = viewManager
  498. sideBarController?.reloadData()
  499. }
  500. //MARK: - 左边侧边栏
  501. func initLeftSideController() {
  502. if botaViewController == nil {
  503. botaViewController = KMNLeftSideViewController(listView.document)
  504. }
  505. botaViewController?.leftSideViewDelegate = self
  506. botaViewController?.view.frame = infoSplitLeftView.bounds
  507. botaViewController?.view.autoresizingMask = [.width, .height]
  508. if botaViewController != nil {
  509. infoSplitLeftView?.addSubview(botaViewController!.view)
  510. }
  511. }
  512. private func leftSidePaneIsOpen() -> Bool {
  513. return !infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) //第一次点击时存在问题,待解决
  514. }
  515. private func toggleOpenLeftSide(pdfSideBarType: KMPDFSidebarType) {
  516. if(leftSidePaneIsOpen() == false) {
  517. let leftWidthNumber = UserDefaults.standard.object(forKey: CPDFViewLeftSidePaneWidthKey) as? NSNumber ?? MIN_SIDE_PANE_WIDTH
  518. infoContendSplitView.setPosition(MIN_SIDE_PANE_WIDTH.doubleValue, ofDividerAt: 0) //暂时无法记录上一次打开的宽度
  519. }
  520. if pdfSideBarType == .search {
  521. botaViewController?.searchViewC.handdler.pdfView = listView
  522. botaViewController?.leftsideType = .search
  523. } else if pdfSideBarType == .thumbnail {
  524. botaViewController?.leftsideType = pdfSideBarType
  525. KMPrint(" thumbnail")
  526. } else if pdfSideBarType == .outline {
  527. botaViewController?.outlineViewC.handdler.pdfView = listView
  528. botaViewController?.leftsideType = pdfSideBarType
  529. } else if pdfSideBarType == .bookmark {
  530. botaViewController?.bookmarkViewC.handdler.pdfView = listView
  531. botaViewController?.leftsideType = pdfSideBarType
  532. } else if pdfSideBarType == .annotation {
  533. // botaViewController?.annoController.handdler.pdfView = listView
  534. // botaViewController?.leftsideType = .annotation
  535. }
  536. }
  537. private func toggleCloseLeftSide() {
  538. if(leftSidePaneIsOpen() == true) {
  539. infoContendSplitView.setPosition(0, ofDividerAt: 0)
  540. }
  541. }
  542. //MARK: - 右侧属性栏
  543. func initRightSideController() {
  544. if rightSideController == nil {
  545. rightSideController = KMRightSideController.init()
  546. rightSideController?.delegate = self
  547. }
  548. rightSideController?.view.frame = CGRectMake(0, 0, MIN_SIDE_PANE_WIDTH.doubleValue, 680)
  549. rightSideController?.view.autoresizingMask = [.height, .maxXMargin]
  550. }
  551. func removeRightSideController() {
  552. rightSideController?.view.removeFromSuperview()
  553. rightSideController = nil
  554. }
  555. @objc func toggleOpenRightSide() -> Void {
  556. if rightSideController != nil {
  557. return
  558. }
  559. initRightSideController()
  560. rightSideController?.view.frame = infoSplitRightView.bounds
  561. rightSideController?.view.autoresizingMask = [.width, .height]
  562. infoSplitRightView.addSubview(rightSideController!.view)
  563. infoContendSplitView.setPosition(CGRectGetWidth(view.frame)-MIN_SIDE_PANE_WIDTH.doubleValue, ofDividerAt: 1)
  564. rightSideController?.viewManager = self.viewManager
  565. rightSideController?.reloadDataWithPDFView(pdfView: listView)
  566. }
  567. @objc func toggleCloseRightSide() -> Void {
  568. removeRightSideController()
  569. infoContendSplitView.setPosition(CGRectGetWidth(view.frame), ofDividerAt: 1)
  570. }
  571. func refreshRightSide() -> Void {
  572. if let rightVC = rightSideController {
  573. rightVC.reloadDataWithPDFView(pdfView: listView)
  574. }
  575. }
  576. //MARK: - PDFDisplayView
  577. func updatePDFDisplaySettingView() {
  578. if viewManager.showDisplayView {
  579. infoSplitViewLeftConst.constant = MIN_SIDE_PANE_WIDTH.doubleValue
  580. } else {
  581. infoSplitViewLeftConst.constant = 44
  582. }
  583. if viewManager.showDisplayView {
  584. if displaySettingController == nil {
  585. displaySettingController = KMNDisplayViewController.init()
  586. }
  587. displaySettingController?.view.frame = CGRectMake(0, 0, MIN_SIDE_PANE_WIDTH.doubleValue, CGRectGetHeight(bottomContendBox.frame))
  588. displaySettingController?.view.autoresizingMask = [.height, .maxXMargin]
  589. bottomContendBox.addSubview(displaySettingController!.view)
  590. displaySettingController?.pdfView = self.listView
  591. displaySettingController?.viewManager = self.viewManager
  592. displaySettingController?.delegate = self
  593. displaySettingController?.reloadData()
  594. } else {
  595. displaySettingController?.view.removeFromSuperview()
  596. displaySettingController = nil
  597. }
  598. }
  599. //MARK: - 页面编辑
  600. func enterPageEditMode() {
  601. pageEditViewController = KMNPageEditViewController(self.document)
  602. if(pageEditViewController != nil) {
  603. bottomContendBox.addSubview(pageEditViewController!.view)
  604. pageEditViewController?.view.frame = bottomContendBox.bounds
  605. pageEditViewController?.thumbnailBaseViewDelegate = self
  606. pageEditViewController?.selectionIndexPaths = [IndexPath(item: listView.currentPageIndex, section: 0)]
  607. pageEditViewController?.view.autoresizingMask = [.width,.height]
  608. pageEditViewController?.currentUndoManager = listView.undoManager
  609. toolbarManager.page_pageInfo_Property.text = String(listView.currentPageIndex + 1)
  610. pdfToolbarController?.refreshSecondToolbarItemsState()
  611. }
  612. }
  613. func exitPageEditMode() {
  614. if pageEditViewController != nil {
  615. pageEditViewController?.view.removeFromSuperview()
  616. pageEditViewController = nil
  617. }
  618. }
  619. //MARK: - 阅读模式
  620. func openPDFReadMode() {
  621. if viewManager.showDisplayView {
  622. viewManager.showDisplayView = false
  623. pdfToolbarController?.reloadLeftIconView()
  624. updatePDFDisplaySettingView()
  625. }
  626. infoSplitViewLeftConst.constant = 0
  627. toolbarBoxHeightConst.constant = 0
  628. view.window?.makeFirstResponder(listView)
  629. let readModeMessage: ComponentMessage = ComponentMessage()
  630. readModeMessage.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString("Read Mode On"))
  631. readModeMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - readModeMessage.properties.propertyInfo.viewWidth)/2,
  632. CGRectGetHeight(self.view.frame) - readModeMessage.properties.propertyInfo.viewHeight - 8,
  633. readModeMessage.properties.propertyInfo.viewWidth,
  634. readModeMessage.properties.propertyInfo.viewHeight)
  635. readModeMessage.reloadData()
  636. readModeMessage.show(inView: self.view, autoHideSeconde: 2)
  637. }
  638. func exitPDFReadMode() {
  639. viewManager.isPDFReadMode = false
  640. if viewManager.showDisplayView == false {
  641. viewManager.showDisplayView = true
  642. pdfToolbarController?.reloadLeftIconView()
  643. }
  644. updatePDFDisplaySettingView()
  645. refreshToolbarViewHeightInfo()
  646. }
  647. //MARK: - PPT
  648. func togglePresentation(_ sender: Any?) {
  649. if self.canExitPresentation() {
  650. exitFullScreen()
  651. } else if self.canEnterPresentation() {
  652. NotificationCenter.default.addObserver(self, selector: #selector(willEnterInteractionModeNotification), name: NSWindow.willEnterInteractionModeNotification, object: nil)
  653. NotificationCenter.default.addObserver(self, selector: #selector(didEnterInteractionModeNotification), name: NSWindow.didEnterInteractionModeNotification, object: nil)
  654. NotificationCenter.default.addObserver(self, selector: #selector(willShowFullScreenNotification), name: NSWindow.willShowFullScreenNotification, object: nil)
  655. NotificationCenter.default.addObserver(self, selector: #selector(didShowFullScreenNotification), name: NSWindow.didShowFullScreenNotification, object: nil)
  656. view.window?.enterPresentation(provider: self)
  657. }
  658. }
  659. func enterPresentationMode() {
  660. let scrollView = listView.documentView().enclosingScrollView
  661. savedNormalSetup.setValue(scrollView?.hasHorizontalScroller, forKey: KMMainModel.Key.kHasHorizontalScroller)
  662. savedNormalSetup.setValue(scrollView?.hasVerticalScroller, forKey: KMMainModel.Key.kHasVerticalsCroller)
  663. savedNormalSetup.setValue(scrollView?.autohidesScrollers, forKey: KMMainModel.Key.kAutoHidesScrollers)
  664. listView.backgroundColor = NSColor.clear
  665. listView.setDisplay(.singlePage)
  666. listView.autoScales = true
  667. listView.displayBox = .cropBox
  668. listView.displaysPageBreaks = false
  669. scrollView?.autohidesScrollers = true
  670. scrollView?.hasHorizontalScroller = false
  671. scrollView?.hasVerticalScroller = false
  672. listView.setCurrentSelection(nil, animate: true)
  673. }
  674. func exitPresentationMode() {
  675. NotificationCenter.default.removeObserver(self, name: NSWindow.willEnterInteractionModeNotification, object: nil)
  676. NotificationCenter.default.removeObserver(self, name: NSWindow.didEnterInteractionModeNotification, object: nil)
  677. NotificationCenter.default.removeObserver(self, name: NSWindow.willShowFullScreenNotification, object: nil)
  678. NotificationCenter.default.removeObserver(self, name: NSWindow.willShowFullScreenNotification, object: nil)
  679. }
  680. func exitFullScreen() {
  681. if self.canExitPresentation() == true {
  682. let mainDocument = self.myDocument as? KMMainDocument
  683. let browserWindowController = mainDocument?.browser?.windowController as? KMBrowserWindowController
  684. browserWindowController?.exitFullscreen()
  685. }
  686. }
  687. func exitFullscreenMode() {
  688. if self.interactionMode == .presentation {
  689. self.exitPresentationMode()
  690. }
  691. self.applyPDFSettings(self.savedNormalSetup)
  692. self.savedNormalSetup.removeAllObjects()
  693. listView.layoutDocumentView()
  694. listView.requiresDisplay()
  695. if let backgroundColor = UserDefaults.standard.color(forKey: KMBackgroundColorKey) {
  696. listView.backgroundColor = backgroundColor
  697. }
  698. }
  699. func applyPDFSettings(_ setup: NSDictionary) {
  700. if let data = setup.object(forKey: KMMainModel.Key.kAutoScales) as? NSNumber {
  701. self.listView.autoScales = data.boolValue
  702. }
  703. if self.listView.autoScales == false {
  704. if let data = setup.object(forKey: KMMainModel.Key.kScaleFactor) as? NSNumber {
  705. self.listView.scaleFactor = data.floatValue.cgFloat
  706. }
  707. }
  708. if let data = setup.object(forKey: KMMainModel.Key.kDisplayMode) as? NSNumber {
  709. self.listView.setDisplay(CPDFDisplayViewMode(rawValue: data.intValue) ?? .singlePage)
  710. }
  711. if let data = setup.object(forKey: KMMainModel.Key.kDisplaysAsBook) as? NSNumber {
  712. self.listView.displaysAsBook = data.boolValue
  713. }
  714. if let data = setup.object(forKey: KMMainModel.Key.kDisplaysPageBreaks) as? NSNumber {
  715. self.listView.displaysPageBreaks = data.boolValue
  716. }
  717. if let data = setup.object(forKey: KMMainModel.Key.kDisplayBox) as? NSNumber {
  718. }
  719. self.listView.layoutDocumentView()
  720. }
  721. func currentPDFSettings() -> NSDictionary {
  722. let setup = NSMutableDictionary()
  723. setup[KMMainModel.Key.kDisplaysPageBreaks] = NSNumber(value: listView.displaysPageBreaks)
  724. setup[KMMainModel.Key.kDisplaysAsBook] = NSNumber(value: listView.displaysAsBook)
  725. setup[KMMainModel.Key.kDisplayBox] = NSNumber(value: listView.displayBox.rawValue)
  726. setup[KMMainModel.Key.kScaleFactor] = NSNumber(value: listView.scaleFactor)
  727. setup[KMMainModel.Key.kAutoScales] = NSNumber(value: listView.autoScales)
  728. setup[KMMainModel.Key.kDisplayMode] = NSNumber(value: listView.fetchDisplayViewMode().rawValue)
  729. return setup
  730. }
  731. func canEnterFullscreen() -> Bool {
  732. if (mwcFlags.isSwitchingFullScreen != 0) {
  733. return false
  734. }
  735. if useNativeFullScreen() {
  736. return interactionMode == .normal || interactionMode == .presentation
  737. } else {
  738. return !self.listView.document.isLocked && (interactionMode == .normal || interactionMode == .presentation) && self.view.window?.tabbedWindows?.count ?? 0 < 2
  739. }
  740. }
  741. override func canEnterPresentation() -> Bool {
  742. let can = super.canEnterPresentation()
  743. if can == false {
  744. return false
  745. }
  746. guard let doc = self.listView.document, doc.isLocked == false else {
  747. return false
  748. }
  749. return can
  750. }
  751. func fadeOutFullScreenWindow() {
  752. guard let fullScreenWindow = self.view.window as? KMFullScreenWindow else {
  753. NSSound.beep()
  754. return
  755. }
  756. let mainWindow = fullScreenWindow.interactionParent
  757. let collectionBehavior = mainWindow?.collectionBehavior
  758. mainWindow?.alphaValue = 0
  759. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  760. mainWindow?.animationBehavior = .none
  761. }
  762. // trick to make sure the main window shows up in the same space as the fullscreen window
  763. fullScreenWindow.addChildWindow(mainWindow!, ordered: .below)
  764. fullScreenWindow.removeChildWindow(mainWindow!)
  765. fullScreenWindow.level = .popUpMenu
  766. // these can change due to the child window trick
  767. mainWindow?.level = .normal
  768. mainWindow?.alphaValue = 1.0
  769. mainWindow?.collectionBehavior = collectionBehavior!
  770. mainWindow?.display()
  771. mainWindow?.makeFirstResponder(self.listView)
  772. mainWindow?.recalculateKeyViewLoop()
  773. // mainWindow?.delegate = self
  774. mainWindow?.makeKey()
  775. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  776. mainWindow?.animationBehavior = .default
  777. }
  778. NSApp.removeWindowsItem(fullScreenWindow)
  779. fullScreenWindow.fadeOut()
  780. }
  781. //MARK: - PDF分屏视图
  782. func reloadPDFSplitInfo() {
  783. if listView.viewSplitMode == .disable {
  784. pdfSplitView.isHidden = true
  785. listView.frame = infoSplitCenterView.bounds
  786. infoSplitCenterView.addSubview(listView)
  787. if splitPDFController != nil {
  788. splitPDFController = nil
  789. }
  790. } else {
  791. pdfSplitView.isHidden = false
  792. listView.frame = pdfSplitTopView.bounds
  793. pdfSplitTopView.addSubview(listView)
  794. setUpPDFBottomToolbar()
  795. setupSplitPDFController()
  796. if listView.viewSplitMode == .horizontal {
  797. pdfSplitView.isVertical = false
  798. } else if listView.viewSplitMode == .vertical {
  799. pdfSplitView.isVertical = true
  800. }
  801. }
  802. }
  803. func setUpPDFBottomToolbar() {
  804. if pdfBottomToolbar != nil {
  805. pdfBottomToolbar = nil
  806. }
  807. pdfBottomToolbar = KMSplitToolbar.init()
  808. pdfBottomToolbar?.frame = CGRectMake(CGRectGetWidth(pdfSplitTopView.frame)/2-144, 20, 288, 40)
  809. pdfBottomToolbar?.pdfView = self.listView
  810. pdfBottomToolbar?.reloadData()
  811. pdfSplitTopView.addSubview(pdfBottomToolbar!)
  812. }
  813. func reloadPDFBottomToolbar() {
  814. if viewManager.splitShowBottomBar {
  815. pdfBottomToolbar?.isHidden = false
  816. pdfBottomToolbar?.reloadData()
  817. } else {
  818. pdfBottomToolbar?.isHidden = true
  819. }
  820. }
  821. //MARK: - Edit模式
  822. func showEditToolbarView() {
  823. if editToolbarView == nil {
  824. editToolbarView = KMEditToolbarView()
  825. }
  826. editToolbarView?.frame = toolbarBox.bounds
  827. editToolbarView?.delegate = self
  828. editToolbarView?.autoresizingMask = [.width, .height]
  829. toolbarBox.contentView = editToolbarView
  830. }
  831. func exitEditToolbarView() {
  832. viewManager.editType = .none
  833. viewManager.subToolMode = .None
  834. editToolbarView?.removeFromSuperview()
  835. editToolbarView = nil
  836. watermarkViewController?.view.removeFromSuperview()
  837. watermarkViewController = nil
  838. backgroundViewController?.view.removeFromSuperview()
  839. backgroundViewController = nil
  840. headerFooterViewController?.view.removeFromSuperview()
  841. headerFooterViewController = nil
  842. batesViewController?.view.removeFromSuperview()
  843. batesViewController = nil
  844. refreshToolbarViewHeightInfo()
  845. toolbarBox.contentView = pdfToolbarController?.view
  846. updatePDFViewAnnotationMode()
  847. }
  848. func updateEditModeDocumentWhenPageChanged() {
  849. if viewManager.editType == .watermark {
  850. updateWatermarkDocument()
  851. } else if viewManager.editType == .background {
  852. updateBackgroundDocument()
  853. } else if viewManager.editType == .header_Footer {
  854. updateHeaderFooterDocument()
  855. } else if viewManager.editType == .bates {
  856. updateBatesDocument()
  857. } else if viewManager.subToolMode == .Edit_Crop {
  858. updateCropDocument()
  859. }
  860. }
  861. //MARK: - Crop裁剪
  862. func showCropController() {
  863. if cropController == nil {
  864. cropController = KMCropController.init()
  865. cropController?.view.frame = infoSplitCenterView.bounds
  866. cropController?.view.autoresizingMask = [.width, .height]
  867. cropController?.delegate = self
  868. infoSplitCenterView.addSubview(cropController!.view)
  869. updateCropDocument()
  870. if viewManager.showRightSide == false {
  871. viewManager.showRightSide = true
  872. pdfToolbarController?.reloadRightToolsView()
  873. toggleOpenRightSide()
  874. }
  875. }
  876. }
  877. func updateCropDocument() {
  878. guard let controller = cropController else { return }
  879. controller.pdfDocument = nil
  880. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  881. let editDocument = CPDFDocument.init()
  882. editDocument?.insertPageObject(page, at: 0)
  883. if let editPage = editDocument?.page(at: 0) {
  884. editPage.setBounds(CGRectMake(0, 0, editPage.bounds(for: .mediaBox).size.width, editPage.bounds(for: .mediaBox).size.height), for: .cropBox)
  885. controller.selectionRect = page?.bounds(for: .cropBox) ?? .zero
  886. }
  887. controller.pdfDocument = editDocument
  888. controller.reloadData()
  889. }
  890. func removeCropController() {
  891. if cropController != nil {
  892. cropController?.view.removeFromSuperview()
  893. cropController = nil
  894. if viewManager.subToolMode == .None {
  895. viewManager.showRightSide = false
  896. pdfToolbarController?.reloadRightToolsView()
  897. toggleCloseRightSide()
  898. }
  899. }
  900. }
  901. //MARK: - Watermark水印
  902. func showWatermarkController() {
  903. viewManager.editType = .watermark
  904. showEditToolbarView()
  905. editToolbarView?.editType = .watermark
  906. if KMWatermarkManager.defaultManager.watermarks.count == 0 {
  907. editToolbarView?.editSubType = .add
  908. } else {
  909. editToolbarView?.editSubType = .template
  910. }
  911. editToolbarView?.reloadData()
  912. if watermarkViewController == nil {
  913. watermarkViewController = KMWatermarkController.init()
  914. }
  915. watermarkViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  916. watermarkViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  917. watermarkViewController?.delegate = self
  918. bottomContendBox.contentView?.addSubview(watermarkViewController!.view)
  919. watermarkViewController?.editSubType = editToolbarView?.editSubType ?? .template
  920. updateWatermarkDocument()
  921. }
  922. func updateWatermarkDocument() {
  923. guard let controller = watermarkViewController else { return }
  924. var editDocument = CPDFDocument.init()
  925. if let vcDoc = controller.pdfDocument {
  926. editDocument = vcDoc
  927. }
  928. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  929. editDocument?.insertPageObject(page, at: 0)
  930. if editDocument?.pageCount == 2 {
  931. let theIndex = IndexSet(integer: 1)
  932. editDocument?.removePage(at: theIndex)
  933. }
  934. if watermarkViewController?.pdfDocument == nil {
  935. watermarkViewController?.pdfDocument = editDocument
  936. }
  937. watermarkViewController?.resetUI()
  938. watermarkViewController?.reloadData()
  939. }
  940. //移除文档水印
  941. func removePDFWatermark() {
  942. let watermarks = self.listView.document.watermarks()
  943. if (watermarks == nil || watermarks!.count <= 0) {
  944. let alert = NSAlert()
  945. alert.alertStyle = .warning
  946. alert.messageText = NSLocalizedString("Could not find a removable watermark in this document. If you see a watermark, it was not added with PDF Reader Pro and therefore cannot be detected.", comment: "")
  947. alert.addButton(withTitle: NSLocalizedString("Confirm", comment: ""))
  948. alert.runModal()
  949. return
  950. }
  951. let alert = NSAlert()
  952. alert.alertStyle = .warning
  953. alert.messageText = NSLocalizedString("Are you sure you want to remove the watermark?", comment: "")
  954. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  955. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  956. let result = alert.runModal()
  957. if (result == .alertFirstButtonReturn) {
  958. for watermark in watermarks! {
  959. listView.document.removeWatermark(watermark)
  960. }
  961. listView.layoutDocumentView()
  962. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Watermark removed"),
  963. type: .success,
  964. fromView: bottomContendBox,
  965. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  966. }
  967. }
  968. func batchAddWatermark() {
  969. }
  970. func batchRemoveWatermark() {
  971. }
  972. //MARK: - PopUI
  973. func reloadPopUIWindow() {
  974. if(listView.isEditing() == false) {
  975. let activeAnnotations:[CPDFAnnotation] = listView.activeAnnotations as! [CPDFAnnotation]
  976. if(activeAnnotations.count > 0) {
  977. if let page = activeAnnotations.first?.page {
  978. let pageRect = listView.selectionMultipleBounds(with: activeAnnotations)
  979. let positioningRect = listView.convert(pageRect, from: page)
  980. if CGRectGetMinY(positioningRect) > 0 && CGRectGetMaxY(positioningRect) < listView.frame.height {
  981. let popWindow = KMNPopOperationWindowController.shared
  982. if popWindow.window?.isVisible == false {
  983. reloadPopUIActiveAnnotations(activeAnnotations: activeAnnotations)
  984. } else {
  985. updateAnnotationsPopWinodwFrame()
  986. }
  987. return
  988. }
  989. }
  990. }
  991. toggleClosePopUIWindow()
  992. } else {
  993. let editAreas:[CPDFEditArea] = listView.km_EditingAreas()
  994. if(editAreas.count > 0) {
  995. if let page = editAreas.first?.page {
  996. let pageRect = listView.selectionMultipleBounds(withEditArea: editAreas)
  997. let positioningRect = listView.convert(pageRect, from: page)
  998. if CGRectGetMinY(positioningRect) > 0 && CGRectGetMaxY(positioningRect) < listView.frame.height {
  999. let popWindow = KMNPopContentEditWindowController.shared
  1000. if popWindow.window?.isVisible == false {
  1001. reloadPopUIContentEdits(editAreas: editAreas)
  1002. } else {
  1003. let editingAreas = listView.km_EditingAreas()
  1004. let editMode = KMNEditContentPopMode(currentEditAreas: editingAreas)
  1005. if popWindow.editType != editMode.popType || editingAreas != popWindow.editContentPopMode.editAreas {
  1006. popWindow.editContentPopMode = editMode
  1007. }
  1008. updateContentEditPopWinodwFrame()
  1009. }
  1010. return
  1011. }
  1012. }
  1013. }
  1014. toggleClosePopUIWindow()
  1015. }
  1016. }
  1017. func toggleClosePopUIWindow() {
  1018. let popWindow = KMNPopOperationWindowController.shared
  1019. if popWindow.window?.isVisible == true {
  1020. closeAnnotationPopWindow()
  1021. }
  1022. let editPopWindow = KMNPopContentEditWindowController.shared
  1023. if editPopWindow.window?.isVisible == true {
  1024. closePopContentEditWindow()
  1025. }
  1026. }
  1027. func closeAnnotationPopWindow() {
  1028. let popWindow = KMNPopOperationWindowController.shared
  1029. if popWindow.window?.isVisible == true {
  1030. listView.window?.removeChildWindow(popWindow.window ?? NSWindow())
  1031. popWindow.window?.close()
  1032. }
  1033. }
  1034. func closePopContentEditWindow() {
  1035. let popWindow = KMNPopContentEditWindowController.shared
  1036. if popWindow.window?.isVisible == true {
  1037. listView.window?.removeChildWindow(popWindow.window ?? NSWindow())
  1038. popWindow.window?.close()
  1039. }
  1040. }
  1041. func reloadPopUIActiveAnnotations(activeAnnotations:[CPDFAnnotation]) {
  1042. let annotationMode = KMNAnnotationPopMode(pdfAnnotations: activeAnnotations )
  1043. let popWindow = KMNPopOperationWindowController.shared
  1044. if annotationMode.popType == .popTypeNone {
  1045. closeAnnotationPopWindow()
  1046. } else {
  1047. listView.window?.addChildWindow(popWindow.window ?? NSWindow(), ordered: .above)
  1048. popWindow.listView = listView
  1049. popWindow.annotationPopMode = annotationMode
  1050. popWindow.isOpenPane = viewManager.showRightSide
  1051. updateAnnotationsPopWinodwFrame()
  1052. popWindow.updatePDFViewCallback = {[weak self] in
  1053. self?.rightSideController?.reloadDataWithPDFView(pdfView: self?.listView ?? CPDFListView())
  1054. self?.listView.setNeedsDisplayMultiAnnotations(annotationMode.annotations)
  1055. }
  1056. popWindow.paneCallback = {[weak self] isOpen in
  1057. if isOpen == true && self?.viewManager.showRightSide == false {
  1058. self?.toggleCloseRightSide()
  1059. } else {
  1060. self?.toggleOpenRightSide()
  1061. }
  1062. }
  1063. }
  1064. }
  1065. func reloadPopUIContentEdits(editAreas:[CPDFEditArea]) {
  1066. let editingAreas = listView.km_EditingAreas()
  1067. let editMode = KMNEditContentPopMode(currentEditAreas: editingAreas)
  1068. let popWindow = KMNPopContentEditWindowController.shared
  1069. if editMode.popType == .editNone {
  1070. closePopContentEditWindow()
  1071. } else {
  1072. listView.window?.addChildWindow(popWindow.window ?? NSWindow(), ordered: .above)
  1073. popWindow.listView = listView
  1074. popWindow.editContentPopMode = editMode
  1075. popWindow.isOpenPane = viewManager.showRightSide
  1076. updateContentEditPopWinodwFrame()
  1077. popWindow.paneCallback = {[weak self] isOpen in
  1078. if isOpen == true && self?.viewManager.showRightSide == false {
  1079. self?.toggleCloseRightSide()
  1080. } else {
  1081. self?.toggleOpenRightSide()
  1082. }
  1083. }
  1084. }
  1085. }
  1086. func updateAnnotationsPopWinodwFrame() {
  1087. let popWindow = KMNPopOperationWindowController.shared
  1088. if popWindow.window?.isVisible == true {
  1089. popWindow.updateFrame(listView: listView)
  1090. }
  1091. }
  1092. func updateContentEditPopWinodwFrame() {
  1093. let popWindow = KMNPopContentEditWindowController.shared
  1094. if popWindow.window?.isVisible == true {
  1095. popWindow.updateFrame(listView: listView)
  1096. }
  1097. }
  1098. //MARK: - Background背景
  1099. func showBackgroundController() {
  1100. viewManager.editType = .background
  1101. showEditToolbarView()
  1102. editToolbarView?.editType = .background
  1103. if KMBackgroundManager.defaultManager.datas.count == 0 {
  1104. editToolbarView?.editSubType = .add
  1105. } else {
  1106. editToolbarView?.editSubType = .template
  1107. }
  1108. editToolbarView?.reloadData()
  1109. if backgroundViewController == nil {
  1110. backgroundViewController = KMBackgroundController.init()
  1111. }
  1112. backgroundViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1113. backgroundViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1114. backgroundViewController?.delegate = self
  1115. bottomContendBox.contentView?.addSubview(backgroundViewController!.view)
  1116. backgroundViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1117. backgroundViewController?.resetUI()
  1118. updateBackgroundDocument()
  1119. }
  1120. func updateBackgroundDocument() {
  1121. guard let controller = backgroundViewController else { return }
  1122. controller.pdfDocument = nil
  1123. let editDocument = CPDFDocument.init()
  1124. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1125. editDocument?.insertPageObject(page, at: 0)
  1126. backgroundViewController?.pdfDocument = editDocument
  1127. backgroundViewController?.reloadData()
  1128. }
  1129. func removePDFBackground() {
  1130. let alert = NSAlert()
  1131. alert.alertStyle = .warning
  1132. alert.messageText = NSLocalizedString("Are you sure you want to remove the background?", comment: "")
  1133. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  1134. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  1135. let result = alert.runModal()
  1136. if (result == .alertFirstButtonReturn) {
  1137. let background = listView.document.background()
  1138. background?.clear()
  1139. listView.document?.refreshPageData()
  1140. listView.layoutDocumentView()
  1141. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Background removed"),
  1142. type: .success,
  1143. fromView: bottomContendBox,
  1144. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1145. }
  1146. }
  1147. func batchAddBackground() {
  1148. }
  1149. func batchRemoveBackground() {
  1150. }
  1151. //MARK: - header&footer
  1152. func showHeaderFooterController() {
  1153. viewManager.editType = .header_Footer
  1154. showEditToolbarView()
  1155. editToolbarView?.editType = .header_Footer
  1156. if KMHeaderFooterManager.defaultManager.headFooterObjects.count == 0 {
  1157. editToolbarView?.editSubType = .add
  1158. } else {
  1159. editToolbarView?.editSubType = .template
  1160. }
  1161. editToolbarView?.reloadData()
  1162. if headerFooterViewController == nil {
  1163. headerFooterViewController = KMHeaderFooterController.init()
  1164. }
  1165. headerFooterViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1166. headerFooterViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1167. headerFooterViewController?.totalPDFCount = Int(listView.document.pageCount)
  1168. headerFooterViewController?.delegate = self
  1169. bottomContendBox.contentView?.addSubview(headerFooterViewController!.view)
  1170. headerFooterViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1171. updateHeaderFooterDocument()
  1172. }
  1173. func updateHeaderFooterDocument() {
  1174. guard let controller = headerFooterViewController else { return }
  1175. controller.pdfDocument = nil
  1176. let editDocument = CPDFDocument.init()
  1177. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1178. editDocument?.insertPageObject(page, at: 0)
  1179. headerFooterViewController?.totalPDFCount = Int(listView.document.pageCount)
  1180. headerFooterViewController?.pdfDocument = editDocument
  1181. headerFooterViewController?.resetUI()
  1182. headerFooterViewController?.reloadData()
  1183. }
  1184. func removeHeaderFooter() {
  1185. let alert = NSAlert()
  1186. alert.alertStyle = .warning
  1187. alert.messageText = NSLocalizedString("Are you sure you want to remove the Header & Footer?", comment: "")
  1188. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  1189. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  1190. let result = alert.runModal()
  1191. if (result == .alertFirstButtonReturn) {
  1192. let headerFooter = listView.document.headerFooter()
  1193. headerFooter?.clear()
  1194. listView.document?.refreshPageData()
  1195. listView.layoutDocumentView()
  1196. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Header & Footer removed"),
  1197. type: .success,
  1198. fromView: bottomContendBox,
  1199. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1200. }
  1201. }
  1202. func batchAddHeaderFooter() {
  1203. }
  1204. func batchRemoveHeaderFooter() {
  1205. }
  1206. //MARK: - Bates
  1207. func showBatesController() {
  1208. viewManager.editType = .bates
  1209. showEditToolbarView()
  1210. editToolbarView?.editType = viewManager.editType
  1211. if KMBatesManager.defaultManager.datas.count == 0 {
  1212. editToolbarView?.editSubType = .add
  1213. } else {
  1214. editToolbarView?.editSubType = .template
  1215. }
  1216. editToolbarView?.reloadData()
  1217. if batesViewController == nil {
  1218. batesViewController = KMBatesController.init()
  1219. }
  1220. batesViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1221. batesViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1222. batesViewController?.delegate = self
  1223. batesViewController?.totalPDFCount = Int(listView.document.pageCount)
  1224. bottomContendBox.contentView?.addSubview(batesViewController!.view)
  1225. batesViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1226. updateBatesDocument()
  1227. batesViewController?.resetUI()
  1228. }
  1229. func updateBatesDocument() {
  1230. guard let controller = batesViewController else { return }
  1231. controller.pdfDocument = nil
  1232. let editDocument = CPDFDocument.init()
  1233. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1234. editDocument?.insertPageObject(page, at: 0)
  1235. batesViewController?.pdfDocument = editDocument
  1236. batesViewController?.reloadData()
  1237. }
  1238. func removePDFBates() {
  1239. let alert = NSAlert()
  1240. alert.alertStyle = .warning
  1241. alert.messageText = NSLocalizedString("Are you sure you want to remove the Bates?", comment: "")
  1242. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  1243. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  1244. let result = alert.runModal()
  1245. if (result == .alertFirstButtonReturn) {
  1246. let bates = listView.document.bates()
  1247. bates?.clear()
  1248. listView.document?.refreshPageData()
  1249. listView.layoutDocumentView()
  1250. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Bates removed"),
  1251. type: .success,
  1252. fromView: bottomContendBox,
  1253. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1254. }
  1255. }
  1256. func batchAddBates() {
  1257. }
  1258. func batchRemoveBates() {
  1259. }
  1260. //MARK: - Crop Action
  1261. // 白边距,统一大小
  1262. @objc func auto_cropPagesWhiteMargin(_ pageIndexs: [UInt]) {
  1263. var size = NSZeroSize
  1264. for i in pageIndexs {
  1265. let page = self.listView.document.page(at: i)
  1266. let rect = KMCropTools.getPageForegroundBox(page!)
  1267. size.width = fmax(size.width, NSWidth(rect))
  1268. size.height = fmax(size.height, NSHeight(rect))
  1269. }
  1270. var rectArray: Array<NSRect> = []
  1271. for i in pageIndexs {
  1272. progressC?.increment(by: Double(i))
  1273. progressC?.doubleValue = Double(i)
  1274. let page = self.listView.document.page(at: i)
  1275. var rect = KMCropTools.getPageForegroundBox(page!)
  1276. let bounds: NSRect = (page?.bounds(for: .mediaBox))!
  1277. if (rect.minX - bounds.minX > bounds.maxX-rect.maxX) {
  1278. rect.origin.x = rect.maxX-size.width
  1279. }
  1280. rect.origin.y = rect.maxY-size.height
  1281. rect.size = size
  1282. if (NSWidth(rect) > NSWidth(bounds)) {
  1283. rect.size.width = NSWidth(bounds)
  1284. }
  1285. if (NSHeight(rect) > NSHeight(bounds)) {
  1286. rect.size.height = NSHeight(bounds)
  1287. }
  1288. if (NSMinX(rect) < NSMinX(bounds)) {
  1289. rect.origin.x = NSMinX(bounds)
  1290. } else if (NSMaxX(rect) > NSMaxX(bounds)) {
  1291. rect.origin.x = NSMaxX(bounds) - NSWidth(rect)
  1292. }
  1293. if (NSMinY(rect) < NSMinY(bounds)) {
  1294. rect.origin.y = NSMinY(bounds)
  1295. } else if (NSMaxY(rect) > NSMaxY(bounds)) {
  1296. rect.origin.y = NSMaxY(bounds) - NSHeight(rect)
  1297. }
  1298. rectArray.append(rect)
  1299. }
  1300. self.cropPages(atIndexs: pageIndexs, to: rectArray)
  1301. }
  1302. func cropPages(atIndexs pageIndexs: [UInt], to rects: Array<NSRect>) {
  1303. let currentPage = self.listView.currentPage()
  1304. let visibleRect: NSRect = self.listView.convert(self.listView.convert(self.listView.documentView().visibleRect, from: self.listView.documentView()), to: self.listView.currentPage())
  1305. var oldRectArray: Array<NSRect> = []
  1306. let rectCount = rects.count
  1307. for i in pageIndexs {
  1308. if let page = self.listView.document.page(at: i) {
  1309. let rect = NSIntersectionRect(rects[Int(i) % rectCount], (page.bounds(for: .mediaBox)))
  1310. let oldRect = page.bounds(for: .cropBox)
  1311. oldRectArray.append(oldRect)
  1312. page.setBounds(rect, for: .cropBox)
  1313. }
  1314. }
  1315. let undoManager = self.listView.undoManager
  1316. (undoManager?.prepare(withInvocationTarget: self) as AnyObject).cropPages(atIndexs: pageIndexs, to: oldRectArray)
  1317. /// 刷新预览视图
  1318. self.listView.layoutDocumentView()
  1319. self.listView.displayBox = .cropBox
  1320. self.listView.go(to: currentPage)
  1321. self.listView.go(to: visibleRect, on: currentPage)
  1322. }
  1323. //MARK: - Share Action
  1324. @objc private func shareDocument(sender: NSView) {
  1325. let document = self.listView.document ?? CPDFDocument()
  1326. if document?.documentURL == nil {
  1327. return
  1328. }
  1329. var doucumentURL : URL = self.listView.document.documentURL
  1330. if doucumentURL.path.count > 0 {
  1331. let docDir = NSTemporaryDirectory()
  1332. let documentName : String = doucumentURL.path.lastPathComponent
  1333. let path = docDir.stringByAppendingPathComponent(documentName)
  1334. let writeSuccess = self.listView.document.write(to: URL(fileURLWithPath: path))
  1335. if writeSuccess == false {
  1336. __NSBeep()
  1337. return;
  1338. }
  1339. doucumentURL = URL(fileURLWithPath: path)
  1340. }
  1341. let array = [doucumentURL]
  1342. let picker = NSSharingServicePicker.init(items: array)
  1343. if sender.window != nil {
  1344. picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
  1345. } else {
  1346. 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)
  1347. }
  1348. }
  1349. @objc private func shareFlatten(sender: NSView) {
  1350. let document = self.listView.document ?? CPDFDocument()
  1351. var path: String?
  1352. if document?.documentURL != nil {
  1353. path = document?.documentURL.path ?? ""
  1354. }
  1355. if path?.count ?? 0 > 0 {
  1356. let docDir = NSTemporaryDirectory()
  1357. let documentName : String = path?.lastPathComponent ?? ""
  1358. path = docDir.stringByAppendingPathComponent(documentName)
  1359. }
  1360. let pathFolder = path?.fileURL.deletingLastPathComponent().path
  1361. var tfileName = path?.deletingPathExtension.lastPathComponent
  1362. let tStdFileSuffix = "_flatten"
  1363. tfileName = (tfileName ?? "") + tStdFileSuffix + ".pdf"
  1364. path = (pathFolder ?? "") + "/" + (tfileName ?? "")
  1365. let success : Bool = document?.writeFlatten(to: URL(fileURLWithPath: path ?? "")) ?? false
  1366. if success {
  1367. let url = URL(fileURLWithPath: path ?? "")
  1368. let picker = NSSharingServicePicker.init(items: [url])
  1369. if sender.window != nil {
  1370. picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
  1371. } else {
  1372. 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)
  1373. }
  1374. }
  1375. }
  1376. @objc private func shareOriginalPDF(sender: NSView) {
  1377. guard let pdfDoc = self.listView.document else {
  1378. NSSound.beep()
  1379. return
  1380. }
  1381. if !pdfDoc.allowsCopying || !pdfDoc.allowsPrinting {
  1382. let alert = NSAlert()
  1383. alert.alertStyle = .critical
  1384. alert.messageText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
  1385. alert.runModal()
  1386. return
  1387. }
  1388. let document = self.listView.document ?? CPDFDocument()
  1389. var path: String?
  1390. if document?.documentURL != nil {
  1391. path = document?.documentURL.path ?? ""
  1392. }
  1393. if path?.count ?? 0 > 0 {
  1394. let docDir = NSTemporaryDirectory()
  1395. let documentName : String = path?.lastPathComponent ?? ""
  1396. path = docDir.stringByAppendingPathComponent(documentName)
  1397. }
  1398. var writeSuccess = document?.write(to: URL(fileURLWithPath: path ?? ""))
  1399. if writeSuccess == false {
  1400. __NSBeep()
  1401. return;
  1402. }
  1403. let newDocument = CPDFDocument(url: URL(fileURLWithPath: path ?? ""))
  1404. let cnt = newDocument?.pageCount ?? 0
  1405. for i in 0 ..< cnt {
  1406. let page = newDocument!.page(at: i)
  1407. var annotations : [CPDFAnnotation] = []
  1408. for annotation in page!.annotations {
  1409. annotations.append(annotation)
  1410. }
  1411. for annotation in annotations {
  1412. annotation.page.removeAnnotation(annotation)
  1413. }
  1414. }
  1415. writeSuccess = newDocument?.write(to:URL(fileURLWithPath: path ?? ""))
  1416. if writeSuccess ?? false {
  1417. let url = URL(fileURLWithPath: path ?? "")
  1418. let picker = NSSharingServicePicker.init(items: [url])
  1419. if sender.window != nil {
  1420. picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
  1421. } else {
  1422. 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)
  1423. }
  1424. }
  1425. }
  1426. @objc func shareFromService(sender: NSMenuItem) {
  1427. if ((NSApp.mainWindow?.windowController is KMBrowserWindowController) == false) {
  1428. return
  1429. }
  1430. var string = ""
  1431. if let freeTextAnnotation = listView.activeAnnotation as? CPDFFreeTextAnnotation {
  1432. string = freeTextAnnotation.contents ?? ""
  1433. } else if let markupAnnotation = listView.activeAnnotation as? CPDFMarkupAnnotation {
  1434. if let page = markupAnnotation.page {
  1435. if let selection = page.selection(for: markupAnnotation.bounds) {
  1436. string = selection.string() ?? ""
  1437. }
  1438. }
  1439. } else {
  1440. string = listView.currentSelection?.string() ?? ""
  1441. }
  1442. let windowControler = NSApp.mainWindow?.windowController as! KMBrowserWindowController
  1443. let model = windowControler.browser?.tabStripModel
  1444. if let cnt = model?.count(), cnt <= 0 {
  1445. return
  1446. }
  1447. if let data = model?.activeTabContents().isHome, data {
  1448. return
  1449. }
  1450. let document: KMMainDocument = model?.activeTabContents() as! KMMainDocument
  1451. if string.count > 0 {
  1452. let represent : NSSharingService = sender.representedObject as! NSSharingService
  1453. represent.perform(withItems: [string])
  1454. return
  1455. }
  1456. let represent = sender.representedObject as? NSSharingService
  1457. represent?.perform(withItems: [string])
  1458. }
  1459. // 搜索 & 替换
  1460. func showSearchPopWindow(type: KMNBotaSearchType, keyborad: String?, replaceText: String?, results: [KMSearchMode]) {
  1461. let toolMode = self.listView.toolMode ?? .none
  1462. let isEditing = self.listView.isEditing() ?? false
  1463. var winH: CGFloat = 112
  1464. if type == .replace {
  1465. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1466. let winC = KMPurchaseCompareWindowController.sharedInstance()
  1467. winC?.kEventName = "Reading_ReplaceText_BuyNow"
  1468. winC?.showWindow(nil)
  1469. return
  1470. }
  1471. if toolMode == .editPDFToolMode && isEditing {
  1472. } else { // 进入内容编辑模式
  1473. viewManager.toolMode = .Edit
  1474. updatePDFViewAnnotationMode()
  1475. }
  1476. winH = 208
  1477. }
  1478. self.view.window?.makeFirstResponder(nil)
  1479. let winC = KMSearchReplaceWindowController(with: listView, type: type)
  1480. self.currentWindowController = winC
  1481. winC.replaceCallback = { [weak self] in
  1482. let toolMode = self?.listView.toolMode ?? .none
  1483. let isEditing = self?.listView.isEditing() ?? false
  1484. if toolMode == .editPDFToolMode && isEditing {
  1485. } else { // 进入内容编辑模式
  1486. self?.viewManager.toolMode = .Edit
  1487. self?.updatePDFViewAnnotationMode()
  1488. }
  1489. }
  1490. winC.itemClick = { [weak self] idx, params in
  1491. if idx == 1 {
  1492. self?.toggleOpenLeftSide(pdfSideBarType: .search)
  1493. guard let handdler = params.first as? KMNSearchHanddler else {
  1494. return
  1495. }
  1496. let viewC = self?.botaViewController?.searchViewC
  1497. viewC?.update(keyborad: handdler.searchKey, replaceKey: handdler.replaceKey, results: handdler.searchSectionResults)
  1498. }
  1499. }
  1500. let targetView = self.pdfToolbarController?.leftViewButton
  1501. let point = targetView?.convert(targetView?.frame.origin ?? .zero, to: nil) ?? .zero
  1502. // 200 248
  1503. let x = point.x + (self.view.window?.frame.origin.x ?? 0) - 32
  1504. let y = point.y + (self.view.window?.frame.origin.y ?? 0) - winH - 32
  1505. let winFramePoint = NSPoint(x: x, y: y)
  1506. winC.window?.setFrameOrigin(winFramePoint)
  1507. winC.update(keyborad: keyborad, replaceKey: replaceText, results: results)
  1508. self.view.window?.addChildWindow(winC.window!, ordered: .above)
  1509. }
  1510. }
  1511. //MARK: Compress
  1512. extension KMMainViewController {
  1513. func showCompressController(_ url: URL) {
  1514. self.compressWindowController = KMCompressWindowController(windowNibName: "KMCompressWindowController")
  1515. self.compressWindowController?.password = self.listView.document?.password ?? ""
  1516. self.compressWindowController?.documentURL = url
  1517. self.view.window?.beginSheet(self.compressWindowController!.window!)
  1518. self.compressWindowController?.itemClick = { [unowned self] in
  1519. self.view.window?.endSheet((self.compressWindowController?.window)!)
  1520. }
  1521. self.compressWindowController?.batchAction = { [unowned self] view, filePaths in
  1522. self.view.window?.endSheet((self.compressWindowController?.window)!)
  1523. let batchWindowController = KMBatchOperateWindowController.sharedWindowController
  1524. let batchOperateFile = KMBatchOperateFile(filePath: filePaths.first!.path, type: .Compress)
  1525. batchWindowController.switchToOperateType(KMBatchOperationType.Compress, files: [batchOperateFile])
  1526. batchWindowController.window?.makeKeyAndOrderFront("")
  1527. }
  1528. self.compressWindowController?.resultCallback = { [unowned self] result, openDocument, fileURL, error in
  1529. self.view.window?.endSheet((self.compressWindowController?.window)!)
  1530. if (result) {
  1531. if (openDocument) {
  1532. NSDocumentController.shared.openDocument(withContentsOf: fileURL, display: true) { document, result, error in }
  1533. } else {
  1534. NSWorkspace.shared.activateFileViewerSelecting([fileURL])
  1535. }
  1536. } else {
  1537. let alert = NSAlert()
  1538. alert.messageText = NSLocalizedString("Compress Faild", comment: "")
  1539. alert.runModal()
  1540. }
  1541. }
  1542. }
  1543. func showBatchCompressController() {
  1544. }
  1545. }
  1546. //MARK: - NSSplitViewDelegate
  1547. extension KMMainViewController: NSSplitViewDelegate {
  1548. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  1549. if splitView == infoContendSplitView {
  1550. return subview.isEqual(to: infoSplitCenterView) == false
  1551. }
  1552. return false
  1553. }
  1554. func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool {
  1555. if splitView == infoContendSplitView {
  1556. if(subview.isEqual(to: infoSplitLeftView)) {
  1557. } else if(subview.isEqual(to: infoSplitRightView)) {
  1558. }
  1559. }
  1560. return false
  1561. }
  1562. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  1563. if splitView == infoContendSplitView {
  1564. return splitView == infoContendSplitView
  1565. } else if splitView == pdfSplitView {
  1566. return splitView == pdfSplitView
  1567. }
  1568. return false
  1569. }
  1570. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  1571. if(splitView == infoContendSplitView && dividerIndex == 1) {
  1572. return proposedMaximumPosition - MIN_SIDE_PANE_WIDTH.doubleValue
  1573. }
  1574. return proposedMaximumPosition
  1575. }
  1576. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  1577. if(splitView == infoContendSplitView && dividerIndex == 0) {
  1578. return proposedMinimumPosition + MIN_SIDE_PANE_WIDTH.doubleValue
  1579. }
  1580. return proposedMinimumPosition
  1581. }
  1582. func splitViewDidResizeSubviews(_ notification: Notification) {
  1583. let splitView = notification.object as? NSSplitView
  1584. if(splitView == infoContendSplitView) {
  1585. leftSplitViewResizeFinish()
  1586. if(newMwcFlags.settingUpWindow == false && self.view.window?.frameAutosaveName != nil) {
  1587. let leftWidth = infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) ? 0.0 : infoSplitLeftView.frame.width
  1588. UserDefaults.standard.set(leftWidth, forKey: CPDFViewLeftSidePaneWidthKey)
  1589. UserDefaults.standard.synchronize()
  1590. }
  1591. if listView.isEditing() == false {
  1592. updateAnnotationsPopWinodwFrame()
  1593. } else {
  1594. updateContentEditPopWinodwFrame()
  1595. }
  1596. }
  1597. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(splitViewResizeFinish), object: nil)
  1598. self.perform(#selector(splitViewResizeFinish), with: nil, afterDelay: 0.15)
  1599. }
  1600. @objc func leftSplitViewResizeFinish() {
  1601. botaViewController?.changeLeftSideBounds()
  1602. }
  1603. @objc func splitViewResizeFinish() {
  1604. if infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) {
  1605. if viewManager.pdfSideBarType != .none {
  1606. viewManager.pdfSideBarType = .none
  1607. sideBarController?.reloadBOTAData()
  1608. }
  1609. }
  1610. }
  1611. }
  1612. //MARK: - KMPDFSideBarControllerDelegate 左侧Sidebar代理
  1613. extension KMMainViewController: KMPDFSideBarControllerDelegate {
  1614. func kmPDFSideBarControllerDidSidebarTypeUpdated(_ view: KMPDFSideBarController) {
  1615. if viewManager.pdfSideBarType == .none {
  1616. toggleCloseLeftSide()
  1617. } else {
  1618. toggleOpenLeftSide(pdfSideBarType: viewManager.pdfSideBarType)
  1619. }
  1620. }
  1621. func kmPDFSideBarControllerDidGotoPage(_ view: KMPDFSideBarController, _ pageIndex: Int) {
  1622. listView.go(toPageIndex: pageIndex, animated: true)
  1623. }
  1624. }
  1625. //MARK: - KMPDFToolbarControllerDelegate 工具栏代理
  1626. extension KMMainViewController: KMPDFToolbarControllerDelegate {
  1627. //一级工具栏状态发生变化时调用
  1628. func kmPDFToolbarControllerDidToolModeChanged(_ controller: KMPDFToolbarController) {
  1629. refreshToolbarViewHeightInfo()
  1630. toolbarViewModeChanged()
  1631. }
  1632. //MARK: -点击工具栏按钮
  1633. func kmPDFToolbarControllerDidToolbarItemClicked(_ controller: KMPDFToolbarController, _ itemIdentifier: String) {
  1634. print("toolbar点击", itemIdentifier)
  1635. if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_PageEdit_Identifier).contains(itemIdentifier) {
  1636. //MARK: -页面编辑
  1637. if itemIdentifier == KMPDFToolbar_PageEdit_InsertFile_Identifier {
  1638. pageEditViewController?.insertFromPDFAction()
  1639. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertBlank_Identifier {
  1640. pageEditViewController?.insertFromBlankAction()
  1641. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertClip_Identifier {
  1642. pageEditViewController?.insertFromClipboardAction()
  1643. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertScanner_Identifier {
  1644. pageEditViewController?.insertFromScannerAction()
  1645. } else if itemIdentifier == KMPDFToolbar_PageEdit_Extract_Identifier {
  1646. pageEditViewController?.extractPDFAction()
  1647. } else if itemIdentifier == KMPDFToolbar_PageEdit_Replace_Identifier {
  1648. pageEditViewController?.replacePDFAction()
  1649. } else if itemIdentifier == KMPDFToolbar_PageEdit_Split_Identifier {
  1650. pageEditViewController?.splitPDFAction()
  1651. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reverse_Identifier {
  1652. pageEditViewController?.reversePDFAction()
  1653. } else if itemIdentifier == KMPDFToolbar_PageEdit_LeftRotate_Identifier {
  1654. pageEditViewController?.rotatePageLeftAction()
  1655. } else if itemIdentifier == KMPDFToolbar_PageEdit_RightRotate_Identifier{
  1656. pageEditViewController?.rotatePageRightAction()
  1657. } else if itemIdentifier == KMPDFToolbar_PageEdit_Delete_Identifier {
  1658. pageEditViewController?.deletePageAction()
  1659. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reduce_Identifier {
  1660. pageEditViewController?.zoomOutPageAction()
  1661. if(pageEditViewController?.canZoomInPageSize() == false) {
  1662. toolbarManager.page_Increase_Property.isDisabled = true
  1663. } else {
  1664. toolbarManager.page_Increase_Property.isDisabled = false
  1665. }
  1666. if(pageEditViewController?.canZoomOutPageSize() == false) {
  1667. toolbarManager.page_Reduce_Property.isDisabled = true
  1668. } else {
  1669. toolbarManager.page_Reduce_Property.isDisabled = false
  1670. }
  1671. controller.refreshSecondToolbarItemsState()
  1672. } else if itemIdentifier == KMPDFToolbar_PageEdit_Increase_Identifier {
  1673. pageEditViewController?.zoomInPageAction()
  1674. if(pageEditViewController?.canZoomInPageSize() == false) {
  1675. toolbarManager.page_Increase_Property.isDisabled = true
  1676. } else {
  1677. toolbarManager.page_Increase_Property.isDisabled = false
  1678. }
  1679. if(pageEditViewController?.canZoomOutPageSize() == false) {
  1680. toolbarManager.page_Reduce_Property.isDisabled = true
  1681. } else {
  1682. toolbarManager.page_Reduce_Property.isDisabled = false
  1683. }
  1684. controller.refreshSecondToolbarItemsState()
  1685. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_oddPage_Identifier {
  1686. pageEditViewController?.thumbnailChoosePageStyle = .odd
  1687. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1688. toolbarManager.page_pageInfo_Property.creatable = false
  1689. controller.refreshSecondToolbarItemsState()
  1690. }
  1691. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_EvenPage_Identifier {
  1692. pageEditViewController?.thumbnailChoosePageStyle = .even
  1693. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1694. toolbarManager.page_pageInfo_Property.creatable = false
  1695. controller.refreshSecondToolbarItemsState()
  1696. }
  1697. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_PortraitPage_Identifier {
  1698. pageEditViewController?.thumbnailChoosePageStyle = .horizontal
  1699. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1700. toolbarManager.page_pageInfo_Property.creatable = false
  1701. controller.refreshSecondToolbarItemsState()
  1702. }
  1703. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_LandscapePage_Identifier {
  1704. pageEditViewController?.thumbnailChoosePageStyle = .vertical
  1705. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1706. toolbarManager.page_pageInfo_Property.creatable = false
  1707. controller.refreshSecondToolbarItemsState()
  1708. }
  1709. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_AllPage_Identifier {
  1710. pageEditViewController?.thumbnailChoosePageStyle = .allPage
  1711. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  1712. toolbarManager.page_pageInfo_Property.creatable = false
  1713. controller.refreshSecondToolbarItemsState()
  1714. }
  1715. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_CustomPage_Identifier {
  1716. pageEditViewController?.thumbnailChoosePageStyle = .custom
  1717. toolbarManager.page_pageInfo_Property.text = nil
  1718. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  1719. toolbarManager.page_pageInfo_Property.creatable = true
  1720. }
  1721. controller.refreshSecondToolbarItemsState()
  1722. }
  1723. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Markup_Identifier).contains(itemIdentifier) {
  1724. //MARK: -Markup
  1725. if itemIdentifier == KMPDFToolbar_eye_Identifier {
  1726. self.showOrHideNotes()
  1727. }
  1728. if viewManager.subToolMode == .None {
  1729. viewManager.showRightSide = false
  1730. } else {
  1731. viewManager.showRightSide = true
  1732. }
  1733. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Edit_Identifier).contains(itemIdentifier) {
  1734. //MARK: -编辑
  1735. if itemIdentifier == KMPDFToolbar_edit_addWatermark_Identifier {
  1736. showWatermarkController()
  1737. } else if itemIdentifier == KMPDFToolbar_edit_removeWatermark_Identifier {
  1738. removePDFWatermark()
  1739. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddWatermark_Identifier {
  1740. batchAddWatermark()
  1741. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveWatermark_Identifier {
  1742. batchRemoveWatermark()
  1743. } else if itemIdentifier == KMPDFToolbar_edit_addBG_Identifier {
  1744. showBackgroundController()
  1745. } else if itemIdentifier == KMPDFToolbar_edit_removeBG_Identifier {
  1746. removePDFBackground()
  1747. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddBG_Identifier {
  1748. batchAddBackground()
  1749. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveBG_Identifier {
  1750. batchRemoveBackground()
  1751. } else if itemIdentifier == KMPDFToolbar_edit_addHF_Identifier {
  1752. showHeaderFooterController()
  1753. } else if itemIdentifier == KMPDFToolbar_edit_removeHF_Identifier {
  1754. removeHeaderFooter()
  1755. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddHF_Identifier {
  1756. batchAddHeaderFooter()
  1757. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveHF_Identifier {
  1758. batchRemoveHeaderFooter()
  1759. } else if itemIdentifier == KMPDFToolbar_edit_addBates_Identifier {
  1760. showBatesController()
  1761. } else if itemIdentifier == KMPDFToolbar_edit_removeBates_Identifier {
  1762. removePDFBates()
  1763. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddBates_Identifier {
  1764. batchAddBates()
  1765. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveBates_Identifier {
  1766. batchRemoveBates()
  1767. } else if itemIdentifier == KMPDFToolbar_edit_crop_Identifier {
  1768. showCropController()
  1769. }
  1770. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Form_Identifier).contains(itemIdentifier) {
  1771. //MARK: -Form表单
  1772. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Fill_Identifier).contains(itemIdentifier) {
  1773. //MARK: -填充
  1774. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Convert_Identifier).contains(itemIdentifier) {
  1775. //MARK: -转档
  1776. if itemIdentifier == KMPDFToolbar_convert_word_Identifier {
  1777. let winC = KMConvertWordWindowController()
  1778. let model = KMDocumentModel(url: listView.document.documentURL)
  1779. winC.documentModel = model
  1780. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1781. } else if itemIdentifier == KMPDFToolbar_convert_ppt_Identifier {
  1782. let winC = KMConvertPPTsWindowController()
  1783. winC.subType = 1
  1784. let model = KMDocumentModel(url: listView.document.documentURL)
  1785. winC.documentModel = model
  1786. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1787. } else if itemIdentifier == KMPDFToolbar_convert_RTF_Identifier {
  1788. let winC = KMConvertPPTsWindowController()
  1789. winC.subType = 2
  1790. let model = KMDocumentModel(url: listView.document.documentURL)
  1791. winC.documentModel = model
  1792. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1793. } else if itemIdentifier == KMPDFToolbar_convert_Text_Identifier {
  1794. let winC = KMConvertPPTsWindowController()
  1795. winC.subType = 4
  1796. let model = KMDocumentModel(url: listView.document.documentURL)
  1797. winC.documentModel = model
  1798. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1799. } else if itemIdentifier == KMPDFToolbar_convert_CSV_Identifier {
  1800. let winC = KMConvertPPTsWindowController()
  1801. winC.subType = 5
  1802. let model = KMDocumentModel(url: listView.document.documentURL)
  1803. winC.documentModel = model
  1804. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1805. } else if itemIdentifier == KMPDFToolbar_convert_excel_Identifier {
  1806. let winC = KMConvertExcelWindowController()
  1807. let model = KMDocumentModel(url: listView.document.documentURL)
  1808. winC.documentModel = model
  1809. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1810. } else if itemIdentifier == KMPDFToolbar_convert_HTML_Identifier {
  1811. let winC = KMConvertHtmlWindowController()
  1812. let model = KMDocumentModel(url: listView.document.documentURL)
  1813. winC.documentModel = model
  1814. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  1815. } else if itemIdentifier == KMPDFToolbar_convert_Json_Identifier {
  1816. let winC = KMConvertJsonWindowController()
  1817. let model = KMDocumentModel(url: listView.document.documentURL)
  1818. winC.documentModel = model
  1819. winC.own_beginSheetModal(for: view.window, completionHandler: nil)
  1820. } else if itemIdentifier == KMPDFToolbar_convert_image_Identifier {
  1821. let winC = KMConvertImageWindowController()
  1822. let model = KMDocumentModel(url: listView.document.documentURL)
  1823. winC.documentModel = model
  1824. winC.own_beginSheetModal(for: view.window, completionHandler: nil)
  1825. } else if itemIdentifier == KMPDFToolbar_convert_imageToPDF_Identifier {
  1826. NSApplication.ShowImageToPDFWindow()
  1827. }
  1828. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Protect_Identifier).contains(itemIdentifier) {
  1829. //MARK: -Protect
  1830. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Tools_Identifier).contains(itemIdentifier) {
  1831. //MARK: -工具
  1832. if itemIdentifier == KMPDFToolbar_tools_compress_Identifier {
  1833. self.showCompressController(self.listView.document.documentURL)
  1834. } else if itemIdentifier == KMPDFToolbar_tools_batch_compress_Identifier {
  1835. self.showBatchCompressController()
  1836. }
  1837. } else if itemIdentifier == KMPDFToolbar_ViewDisplay_Identifier {
  1838. //MARK: -Display
  1839. updatePDFDisplaySettingView()
  1840. } else if itemIdentifier == KMPDFToolbar_rightView_Identifier {
  1841. //MARK: -属性栏
  1842. } else if itemIdentifier == KMPDFToolbar_PageEdit_Identifier {
  1843. //MARK: -页面编辑
  1844. if viewManager.isPageEditMode == true {
  1845. enterPageEditMode()
  1846. } else {
  1847. exitPageEditMode()
  1848. }
  1849. } else if(itemIdentifier == KMPDFToolbar_undo_Identifier) {
  1850. //MARK: -Undo
  1851. listView.undoManager?.undo()
  1852. } else if(itemIdentifier == KMPDFToolbar_redo_Identifier) {
  1853. //MARK: -Redo
  1854. listView.undoManager?.redo()
  1855. } else if(itemIdentifier == KMPDFToolbar_share_PDF_Identifier ||
  1856. itemIdentifier == KMPDFToolbar_share_Flattened_Identifier ||
  1857. itemIdentifier == KMPDFToolbar_share_Original_Identifier) {
  1858. //MARK: -Share
  1859. if(itemIdentifier == KMPDFToolbar_share_PDF_Identifier) {
  1860. if let view = controller.findViewWith(KMPDFToolbar_share_Identifier) {
  1861. shareDocument(sender: view)
  1862. }
  1863. } else if(itemIdentifier == KMPDFToolbar_share_Flattened_Identifier) {
  1864. if let view = controller.findViewWith(KMPDFToolbar_share_Identifier) {
  1865. shareFlatten(sender: view)
  1866. }
  1867. } else if(itemIdentifier == KMPDFToolbar_share_Original_Identifier) {
  1868. if let view = controller.findViewWith(KMPDFToolbar_share_Identifier) {
  1869. shareOriginalPDF(sender: view)
  1870. }
  1871. }
  1872. } else {
  1873. print("click else")
  1874. }
  1875. refreshToolbarViewHeightInfo()
  1876. toolbarViewModeChanged()
  1877. refreshRightSide()
  1878. }
  1879. func kmPDFToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  1880. }
  1881. func kmPDFToolbarControllerDidSelectTextDidChange(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  1882. }
  1883. func kmPDFToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  1884. if view.properties == toolbarManager.page_pageInfo_Property {
  1885. if viewManager.isPageEditMode == true {
  1886. let fileAttribute = KMNFileAttribute()
  1887. fileAttribute.password = listView.document?.password ?? ""
  1888. fileAttribute.pdfDocument = listView.document
  1889. fileAttribute.filePath = listView.document?.documentURL.path ?? ""
  1890. fileAttribute.bAllPage = false
  1891. fileAttribute.pagesType = .PagesString
  1892. fileAttribute.pagesString = view.properties.text ?? ""
  1893. let fetchSelectPages = fileAttribute.fetchSelectPages()
  1894. if (fetchSelectPages.isEmpty) {
  1895. let alert = NSAlert()
  1896. alert.alertStyle = .critical
  1897. alert.messageText = String(format: "%@ %@", fileAttribute.filePath.lastPathComponent, KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  1898. alert.runModal()
  1899. toolbarManager.page_pageInfo_Property.text = ""
  1900. controller.refreshSecondToolbarItemsState()
  1901. } else {
  1902. var tIndexPaths: Set<IndexPath> = []
  1903. for i in 0 ..< fetchSelectPages.count {
  1904. tIndexPaths.insert(IndexPath(item: (fetchSelectPages[i] - 1), section: 0))
  1905. }
  1906. pageEditViewController?.selectionIndexPaths = tIndexPaths
  1907. }
  1908. }
  1909. }
  1910. }
  1911. func kmPDFToolbarControllerDidExitPageEditMode(_ controller: KMPDFToolbarController) {
  1912. exitPageEditMode()
  1913. }
  1914. }
  1915. //MARK: - KMRightSideControllerDelegate右边属性栏代理
  1916. extension KMMainViewController: KMRightSideControllerDelegate {
  1917. func kmRightSideControllerDidContendVCUpdated(_ controller: KMRightSideController) {
  1918. //MARK: -Crop
  1919. if (controller.contentViewController is KMCropPropertyController) {
  1920. if let cropVC = rightSideController?.edit_cropController {
  1921. cropVC.pdfView = controller.pdfView
  1922. if cropVC.delegate == nil {
  1923. cropVC.delegate = cropController
  1924. }
  1925. cropVC.reloadData()
  1926. }
  1927. }
  1928. }
  1929. }
  1930. //MARK: - KMNDisplayViewControllerDelegate代理
  1931. extension KMMainViewController: KMNDisplayViewControllerDelegate {
  1932. //Display Mode
  1933. func displayViewControllerDidDisplayModeChanged(_ controller: KMNDisplayViewController) {
  1934. listView.layoutDocumentView()
  1935. }
  1936. //阅读模式
  1937. func displayViewControllerDidReadModeUpdated(_ controller: KMNDisplayViewController) {
  1938. if viewManager.isPDFReadMode {
  1939. openPDFReadMode()
  1940. } else {
  1941. exitPDFReadMode()
  1942. }
  1943. }
  1944. //PPT
  1945. func displayViewControllerDidGotoSlideShow(_ controller: KMNDisplayViewController) {
  1946. togglePresentation(nil)
  1947. }
  1948. //SplitView
  1949. func displayViewControllerDidSplitModeChanged(_ controller: KMNDisplayViewController) {
  1950. reloadPDFSplitInfo()
  1951. }
  1952. func displayViewControllerDidSplitFileChanged(_ controller: KMNDisplayViewController) {
  1953. splitPDFController?.reloadData()
  1954. }
  1955. func displayViewControllerDidToolbarStateChanged(_ controller: KMNDisplayViewController) {
  1956. splitPDFController?.refreshToolbarState()
  1957. reloadPDFBottomToolbar()
  1958. }
  1959. }
  1960. //MARK: - PPT
  1961. extension KMMainViewController: KMInteractionProviderProtocol {
  1962. func providerContentView(fullScreenWindow: NSWindow, inset: CGFloat) -> NSView? {
  1963. if(interactionMode == .presentation) {
  1964. if listView.presentationDrawView == nil {
  1965. listView.createPresentationDraw()
  1966. }
  1967. presentationTopViewController = KMPresentationTopViewController.init(nibName: "KMPresentationTopViewController", bundle: nil)
  1968. presentationTopViewController?.pdfView = listView
  1969. presentationTopViewController?.delegate = self
  1970. presentationTopViewController?.isSelectionPre = true
  1971. listView.isPresentationMode = true
  1972. presentationTopViewController?.view.frame = CGRect(x: 0, y: (fullScreenWindow.contentView?.bounds.height ?? 0) - 42, width: fullScreenWindow.contentView?.bounds.width ?? 0, height: 42)
  1973. if((presentationTopViewController) != nil) {
  1974. fullScreenWindow.contentView?.addSubview(presentationTopViewController!.view)
  1975. }
  1976. } else {
  1977. listView.frame = NSInsetRect(fullScreenWindow.contentView?.bounds ?? .zero, 0, 0)
  1978. }
  1979. fullScreenWindow.contentView?.addSubview(listView)
  1980. if(interactionMode == .presentation) {
  1981. let frame = fullScreenWindow.frame
  1982. listView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height-42)
  1983. listView.autoresizingMask = [.width, .maxYMargin]
  1984. }
  1985. return view
  1986. }
  1987. @objc func willEnterInteractionModeNotification(_ sender: Notification) {
  1988. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1989. return
  1990. }
  1991. let interactionMode = sender.userInfo?[NSWindow.UserInfo.interactionModeKey] as? KMInteractionMode
  1992. if interactionMode == .presentation {
  1993. let backgroundColor = NSColor.black
  1994. let level = UserDefaults.standard.bool(forKey: "SKUseNormalLevelForPresentationKey") ? NSWindow.Level.normal : NSWindow.Level.popUpMenu
  1995. let wasInteractionMode = self.interactionMode
  1996. if wasInteractionMode == .normal {
  1997. self.savedNormalSetup.setDictionary(self.currentPDFSettings() as! [AnyHashable : Any])
  1998. }
  1999. if wasInteractionMode == .legacyFullScreen {
  2000. self.enterPresentationMode()
  2001. self.listView.frame = (self.view.window?.contentView?.bounds)!
  2002. self.view.window?.contentView?.addSubview(listView)
  2003. // self.view.window?.backgroundColor = backgroundColor
  2004. self.view.window?.level = level
  2005. self.listView.layoutDocumentView()
  2006. self.listView.requiresDisplay()
  2007. self.forceSubwindowsOnTop(false)
  2008. }
  2009. } else {
  2010. KMPrint("2")
  2011. }
  2012. }
  2013. @objc func didEnterInteractionModeNotification(_ sender: Notification) {
  2014. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  2015. return
  2016. }
  2017. if self.interactionMode == .presentation {
  2018. self.listView.layoutDocumentView()
  2019. self.listView.requiresDisplay()
  2020. }
  2021. }
  2022. @objc func willShowFullScreenNotification(_ sender: Notification) {
  2023. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  2024. return
  2025. }
  2026. if self.interactionMode == .presentation {
  2027. let view = self.view.window?.firstResponder as? NSView
  2028. if let data = view?.isDescendant(of: self.pdfSplitView), data {
  2029. self.view.window?.makeFirstResponder(nil)
  2030. }
  2031. }
  2032. }
  2033. @objc func didShowFullScreenNotification(_ sender: Notification) {
  2034. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  2035. return
  2036. }
  2037. if self.interactionMode == .presentation {
  2038. self.enterPresentationMode()
  2039. }
  2040. }
  2041. }
  2042. // MARK: -KMPresentationTopViewControllerDelegate (幻灯片)
  2043. extension KMMainViewController: KMPresentationTopViewControllerDelegate {
  2044. func presentationTopViewExit(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  2045. self.exitFullScreen()
  2046. }
  2047. func presentationTopViewClear(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  2048. listView.presentationDrawView?.clear()
  2049. }
  2050. func presentationTopViewUndo(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  2051. let presentationDrawView = listView.presentationDrawView
  2052. if presentationDrawView?.canUndo() == true {
  2053. presentationDrawView?.undo()
  2054. }
  2055. }
  2056. func presentationTopViewType(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton, isSeletion: Bool) {
  2057. listView.isPresentationMode = isSeletion
  2058. if listView.isEnterPresentationDrawMode() == true {
  2059. listView.exitPresentationDrawMode()
  2060. }
  2061. }
  2062. func presentationTopViewDrawColor(_ presentationTopViewController: KMPresentationTopViewController, withView: NSView,color:NSColor?) {
  2063. if color == nil{
  2064. listView.exitPresentationDrawMode()
  2065. } else {
  2066. if listView.isEnterPresentationDrawMode() == false {
  2067. listView.enterPresentationDrawMode()
  2068. }
  2069. listView.changePresentationDrawModelColor(color)
  2070. }
  2071. }
  2072. }
  2073. //MARK: - KMSplitPDFViewControllerDelegate SplitPDFView分屏视图
  2074. extension KMMainViewController: KMSplitPDFViewControllerDelegate {
  2075. func splitPDFViewControllerDidUpdateFilePath(_ controller: KMSplitPDFViewController) {
  2076. displaySettingController?.reloadData()
  2077. }
  2078. func splitPDFViewControllerDidUpdatePDFScale(_ controller: KMSplitPDFViewController) {
  2079. if let scaleFactor = controller.pdfView?.scaleFactor {
  2080. listView.scaleFactor = scaleFactor
  2081. }
  2082. }
  2083. func splitPDFViewControllerDidUpdatePDFPageIndex(_ controller: KMSplitPDFViewController) {
  2084. if let pageIndex = controller.pdfView?.currentPageIndex {
  2085. listView.go(toPageIndex: pageIndex, animated: false)
  2086. }
  2087. }
  2088. }
  2089. //MARK: - Edit编辑相关代理
  2090. extension KMMainViewController: KMEditToolbarViewDelegate {
  2091. func kmEditToolbarViewDidUpdateMode(_ view: KMEditToolbarView) {
  2092. if view.editType == .watermark {
  2093. watermarkViewController?.editSubType = editToolbarView?.editSubType ?? .template
  2094. watermarkViewController?.resetUI()
  2095. watermarkViewController?.reloadData()
  2096. } else if view.editType == .background {
  2097. backgroundViewController?.editSubType = editToolbarView?.editSubType ?? .template
  2098. backgroundViewController?.resetUI()
  2099. backgroundViewController?.reloadData()
  2100. } else if view.editType == .header_Footer {
  2101. headerFooterViewController?.editSubType = editToolbarView?.editSubType ?? .template
  2102. headerFooterViewController?.resetUI()
  2103. headerFooterViewController?.reloadData()
  2104. } else if view.editType == .bates {
  2105. batesViewController?.editSubType = editToolbarView?.editSubType ?? .template
  2106. batesViewController?.resetUI()
  2107. batesViewController?.reloadData()
  2108. }
  2109. }
  2110. func kmEditToolbarViewDidChooseBatch(_ view: KMEditToolbarView) {
  2111. }
  2112. func kmEditToolbarViewDidChooseApply(_ view: KMEditToolbarView) {
  2113. let pageIndex = view.getSelectedPageIndex(listView.document)
  2114. if pageIndex.isEmpty {
  2115. let alert = NSAlert()
  2116. alert.alertStyle = .critical
  2117. alert.messageText = KMLocalizedString("Invalid page range or the page number is out of range. Please try again.")
  2118. alert.runModal()
  2119. return
  2120. }
  2121. let pageString = view.getSelectedPageString(listView.document, pageIndex)
  2122. if view.editType == .watermark {
  2123. if let model = watermarkViewController?.currentWatermarkData {
  2124. let watermark = KMPDFWatermarkData.returnWaterMarkWith(model, listView.document)
  2125. watermark.pageString = pageString
  2126. listView.document.addWatermark(watermark)
  2127. listView.layoutDocumentView()
  2128. }
  2129. exitEditToolbarView()
  2130. } else if view.editType == .background {
  2131. if let model = backgroundViewController?.backgroundModel {
  2132. if let background = listView.document.background() {
  2133. KMBackgroundManager.defaultManager.updateBackground(background, withModel: model)
  2134. background.pageString = pageString
  2135. background.update()
  2136. listView.document?.refreshPageData()
  2137. listView.layoutDocumentView()
  2138. }
  2139. }
  2140. exitEditToolbarView()
  2141. } else if view.editType == .header_Footer {
  2142. if let model = headerFooterViewController?.headerFooterModel {
  2143. if let headerFooter = listView.document.headerFooter() {
  2144. KMHeaderFooterManager.defaultManager.updateCPDFHeaderFooter(headerFooter, withModel: model, Int(listView.document.pageCount))
  2145. headerFooter.pageString = pageString
  2146. headerFooter.update()
  2147. listView.document?.refreshPageData()
  2148. listView.layoutDocumentView()
  2149. }
  2150. }
  2151. exitEditToolbarView()
  2152. } else if view.editType == .bates {
  2153. exitEditToolbarView()
  2154. }
  2155. }
  2156. func kmEditToolbarViewDidChooseExit(_ view: KMEditToolbarView) {
  2157. if view.applyEnable {
  2158. let alert = NSAlert()
  2159. if view.editType == .watermark {
  2160. alert.messageText = NSLocalizedString("There are unapplied Watermark settings, do you want to apply them?", comment: "")
  2161. } else if view.editType == .background {
  2162. alert.messageText = NSLocalizedString("There are unapplied Background settings, do you want to apply them?", comment: "")
  2163. } else if view.editType == .header_Footer {
  2164. alert.messageText = NSLocalizedString("There are unapplied Header & Footer settings, do you want to apply them?", comment: "")
  2165. } else if view.editType == .bates {
  2166. alert.messageText = NSLocalizedString("There are unapplied Bates settings, do you want to apply them?", comment: "")
  2167. }
  2168. alert.informativeText = NSLocalizedString("If not, the changes will be lost.", comment: "")
  2169. alert.addButton(withTitle: NSLocalizedString("Apply", comment: ""))
  2170. alert.addButton(withTitle: NSLocalizedString("Don't Apply", comment: ""))
  2171. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  2172. let result = alert.runModal()
  2173. if (result == .alertFirstButtonReturn) {
  2174. self.kmEditToolbarViewDidChooseApply(view)
  2175. } else if (result == .alertSecondButtonReturn) {
  2176. //"Don't Apply"
  2177. exitEditToolbarView()
  2178. } else if (result == .alertThirdButtonReturn) {
  2179. //Cancel
  2180. }
  2181. } else {
  2182. exitEditToolbarView()
  2183. }
  2184. }
  2185. }
  2186. //MARK: - KMCropControllerDelegate 裁剪相关代理
  2187. extension KMMainViewController: KMCropControllerDelegate {
  2188. func kmCropControllerDidCrop(_ controller: KMCropController, _ cropRect: CGRect, _ view: KMPageRangeSelectView) {
  2189. let indexs = view.getSelectedPageIndex(listView.document)
  2190. var uIndexs: [UInt] = []
  2191. for index in indexs {
  2192. if index > 0 {
  2193. uIndexs.append(UInt(index-1))
  2194. }
  2195. }
  2196. cropPages(atIndexs: uIndexs, to: [cropRect])
  2197. removeCropController()
  2198. }
  2199. func kmCropControllerDidCropSeparate(_ controller: KMCropController, _ view: KMPageRangeSelectView) {
  2200. let indexs = view.getSelectedPageIndex(listView.document)
  2201. var rectArray: Array<NSRect> = []
  2202. var uIndexs: [UInt] = []
  2203. for index in indexs {
  2204. if index > 0 {
  2205. uIndexs.append(UInt(index-1))
  2206. let page = self.listView.document.page(at: UInt(index-1))
  2207. let rect = KMCropTools.getPageForegroundBox(page!)
  2208. rectArray.append(rect)
  2209. }
  2210. }
  2211. cropPages(atIndexs: uIndexs, to: rectArray)
  2212. removeCropController()
  2213. }
  2214. func kmCropControllerDidCropAuto(_ controller: KMCropController, _ view: KMPageRangeSelectView) {
  2215. let indexs = view.getSelectedPageIndex(listView.document)
  2216. var uIndexs: [UInt] = []
  2217. for index in indexs {
  2218. if index > 0 {
  2219. uIndexs.append(UInt(index-1))
  2220. }
  2221. }
  2222. auto_cropPagesWhiteMargin(uIndexs)
  2223. }
  2224. func kmCropControllerDidChangedSelectionOrMagnification(_ controller: KMCropController) {
  2225. if let cropVC = rightSideController?.edit_cropController {
  2226. if cropVC.pdfView != controller.pdfView {
  2227. cropVC.pdfView = controller.pdfView
  2228. }
  2229. cropVC.cropSeparateOn = false
  2230. cropVC.cropAutoOn = false
  2231. cropVC.reloadData()
  2232. }
  2233. }
  2234. }
  2235. //MARK: - KMWatermarkControllerDelegate 水印相关代理
  2236. extension KMMainViewController: KMWatermarkControllerDelegate {
  2237. func kmWatermarkControllerDidUpdateMode(_ view: KMWatermarkController) {
  2238. editToolbarView?.editSubType = view.editSubType
  2239. editToolbarView?.reloadData()
  2240. }
  2241. func kmWatermarkControllerDidWatermarkUpdated(_ view: KMWatermarkController) {
  2242. var applyEnable = true
  2243. if let model = view.currentWatermarkData {
  2244. if model.watermarkType == .text {
  2245. if model.text == nil || model.text?.count == 0 {
  2246. applyEnable = false
  2247. }
  2248. } else if model.watermarkType == .image {
  2249. if model.imagePath == nil {
  2250. applyEnable = false
  2251. }
  2252. }
  2253. } else {
  2254. applyEnable = false
  2255. }
  2256. editToolbarView?.applyEnable = applyEnable
  2257. editToolbarView?.reloadData()
  2258. }
  2259. }
  2260. //MARK: - KMBackgroundControllerDelegate 背景代理
  2261. extension KMMainViewController: KMBackgroundControllerDelegate {
  2262. func kmBackgroundControllerDidUpdateMode(_ view: KMBackgroundController) {
  2263. editToolbarView?.editSubType = view.editSubType
  2264. }
  2265. func kmBackgroundControllerDidWatermarkUpdated(_ view: KMBackgroundController) {
  2266. var applyEnable = true
  2267. if let model = view.backgroundModel {
  2268. if model.type == .image {
  2269. if model.imagePath == nil {
  2270. applyEnable = false
  2271. }
  2272. }
  2273. } else {
  2274. applyEnable = false
  2275. }
  2276. editToolbarView?.applyEnable = applyEnable
  2277. editToolbarView?.reloadData()
  2278. }
  2279. }
  2280. //MARK: - KMHeaderFooterControllerDelegate 页眉页脚代理
  2281. extension KMMainViewController: KMHeaderFooterControllerDelegate {
  2282. func kmHeaderFooterControllerDidUpdateMode(_ view: KMHeaderFooterController) {
  2283. editToolbarView?.editSubType = view.editSubType
  2284. }
  2285. func kmHeaderFooterControllerDidModelDataUpdated(_ view: KMHeaderFooterController) {
  2286. var applyEnable = true
  2287. if let model = view.headerFooterModel {
  2288. if model.topLeftString.count == 0 && model.topCenterString.count == 0 && model.topRightString.count == 0 &&
  2289. model.bottomLeftString.count == 0 && model.bottomCenterString.count == 0 && model.bottomRightString.count == 0 {
  2290. applyEnable = false
  2291. }
  2292. } else {
  2293. applyEnable = false
  2294. }
  2295. editToolbarView?.applyEnable = applyEnable
  2296. editToolbarView?.reloadData()
  2297. }
  2298. }
  2299. //MARK: - KMBatesControllerDelegate Bates贝茨码代理
  2300. extension KMMainViewController: KMBatesControllerDelegate {
  2301. func kmBatesControllerDidUpdateMode(_ view: KMBatesController) {
  2302. editToolbarView?.editSubType = view.editSubType
  2303. editToolbarView?.reloadData()
  2304. }
  2305. func kmBatesControllerDidModelDataUpdated(_ view: KMBatesController) {
  2306. var applyEnable = true
  2307. if let model = view.batesModel {
  2308. if model.topLeftString.count == 0 && model.topCenterString.count == 0 && model.topRightString.count == 0 &&
  2309. model.bottomLeftString.count == 0 && model.bottomCenterString.count == 0 && model.bottomRightString.count == 0 {
  2310. applyEnable = false
  2311. }
  2312. } else {
  2313. applyEnable = false
  2314. }
  2315. editToolbarView?.applyEnable = applyEnable
  2316. editToolbarView?.reloadData()
  2317. }
  2318. }
  2319. //MARK: - CPDFViewDelegate PDFView代理
  2320. extension KMMainViewController: CPDFViewDelegate,CPDFListViewDelegate {
  2321. func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
  2322. sideBarController?.reloadPageTurnerData()
  2323. documentLoadComplete()
  2324. }
  2325. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  2326. sideBarController?.reloadPageTurnerData()
  2327. var indexpaths: Set<IndexPath> = []
  2328. indexpaths.insert(IndexPath(item: listView.currentPageIndex, section: 0))
  2329. botaViewController?.thumnailViewController?.selectionIndexPaths = indexpaths
  2330. botaViewController?.currentPageDidChangedAction(document: pdfView.document)
  2331. //分屏视图
  2332. reloadPDFBottomToolbar()
  2333. if viewManager.splitSyncScroll {
  2334. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  2335. splitPDFController?.outPDFFirst = true
  2336. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  2337. var index = pdfView.currentPageIndex
  2338. if index > document.pageCount {
  2339. index = Int(splitPDFView.document.pageCount - 1)
  2340. }
  2341. splitPDFView.go(toPageIndex: index, animated: false)
  2342. }
  2343. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  2344. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  2345. } else if splitPDFController?.outPDFFirst == true {
  2346. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  2347. var index = pdfView.currentPageIndex
  2348. if index > document.pageCount {
  2349. index = Int(splitPDFView.document.pageCount - 1)
  2350. }
  2351. splitPDFView.go(toPageIndex: index, animated: false)
  2352. }
  2353. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  2354. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  2355. }
  2356. }
  2357. //水印
  2358. updateEditModeDocumentWhenPageChanged()
  2359. //
  2360. }
  2361. func pdfViewScaleDidChanged(_ pdfView: CPDFView!) {
  2362. pdfToolbarController?.reloadSelectZoomView()
  2363. reloadPDFBottomToolbar()
  2364. //分屏视图
  2365. if viewManager.splitSyncScroll {
  2366. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  2367. splitPDFController?.outPDFFirst = true
  2368. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  2369. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  2370. } else if splitPDFController?.outPDFFirst == true {
  2371. if let splitPDFView = splitPDFController?.pdfView {
  2372. splitPDFView.scaleFactor = pdfView.scaleFactor
  2373. }
  2374. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  2375. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  2376. }
  2377. }
  2378. }
  2379. func pdfViewDidClick(onLink pdfView: CPDFView!, withURL url: String!) {
  2380. if let urlString = url, urlString == kKMPurchaseProductURLString {
  2381. //跳转订阅比较表
  2382. return
  2383. }
  2384. if url.hasPrefix("smb://") {
  2385. NSWorkspace.shared.openFile(url)
  2386. } else {
  2387. KMTools.openURL(urlString: url)
  2388. }
  2389. }
  2390. func pdfViewPerformURL(_ pdfView: CPDFView!, withContent content: String!) {
  2391. KMPrint("pdfViewPerformURL")
  2392. if content != nil {
  2393. NSWorkspace.shared.open(URL(string: content)!)
  2394. } else {
  2395. let alert = NSAlert()
  2396. alert.alertStyle = .critical
  2397. alert.informativeText = NSLocalizedString("The hyperlink is invalid.", comment: "")
  2398. alert.messageText = ""
  2399. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  2400. alert.runModal()
  2401. return
  2402. }
  2403. }
  2404. func pdfViewPerformPrint(_ pdfView: CPDFView!) {
  2405. KMPrint("pdfViewPerformPrint")
  2406. self.showPrintWindow()
  2407. }
  2408. func pdfViewPerformGo(toPage pdfView: CPDFView!) {
  2409. KMPrint("pdfViewPerformGo")
  2410. }
  2411. func pdfViewOpenPDF(_ pdfView: CPDFView!, forRemoteGoTo action: CPDFAction!) {
  2412. KMPrint("pdfViewOpenPDF")
  2413. }
  2414. func pdfViewPerformReset(_ pdfView: CPDFView!) {
  2415. KMPrint("pdfViewPerformReset")
  2416. pdfView.document?.resetForm()
  2417. }
  2418. func pdfViewEditingBlockDidChanged(_ pdfView: CPDFView!) {
  2419. KMPrint("pdfViewEditingBlockDidChanged")
  2420. }
  2421. func pdfViewAsBookBookmark() -> NSImage! {
  2422. return NSImage(named: "KMImageNameBookmarkIcon")!
  2423. }
  2424. //MARK: -编辑模块
  2425. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  2426. // 文本区块 选中文本已经变化
  2427. }
  2428. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  2429. //编辑模块变化
  2430. rightSideController?.reloadDataWithPDFView(pdfView: (pdfView as! CPDFListView))
  2431. if pdfView is CPDFListView {
  2432. (pdfView as! CPDFListView).isEditImage = false
  2433. }
  2434. reloadPopUIWindow()
  2435. }
  2436. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  2437. if editArea != nil && (editArea is CPDFEditImageArea){
  2438. self.listView.cropAreas = editArea as? CPDFEditImageArea
  2439. }
  2440. reloadPopUIWindow()
  2441. }
  2442. //编辑PDF 创建图片区域回调
  2443. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  2444. if (pdfView as! CPDFListView).isEditImage {
  2445. return
  2446. }
  2447. if rect.size.width < 5 && rect.size.height < 5 {
  2448. pdfView.updateEditing([])
  2449. return
  2450. }
  2451. let panel = NSOpenPanel()
  2452. panel.allowsMultipleSelection = false
  2453. panel.allowedFileTypes = ["png","jpg"]
  2454. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  2455. if response == .OK {
  2456. var filePath = panel.url?.path
  2457. let image = NSImage.init(contentsOf: panel.url!)
  2458. //图片自适应范围
  2459. if image != nil {
  2460. var imageRect = rect
  2461. let imageSize = image!.size
  2462. var previewSize = rect.size
  2463. var isChangeSize = false
  2464. if previewSize.width == 0 && previewSize.height == 0 {
  2465. previewSize = CGSize(width: 500, height: 500)
  2466. isChangeSize = true
  2467. }
  2468. var scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  2469. if scale < 1 { // 大于 500
  2470. } else {
  2471. let wh = max(imageSize.width, imageSize.height)
  2472. if wh >= 72 {
  2473. scale = min(scale, 1)
  2474. } else {
  2475. scale = min(72 / imageSize.width, 72 / imageSize.height)
  2476. }
  2477. }
  2478. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  2479. if isChangeSize {
  2480. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  2481. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  2482. } else {
  2483. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  2484. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  2485. }
  2486. imageRect.size = newSize
  2487. let limitWidth = 1920.0
  2488. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  2489. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  2490. targetSize: CGSize(width: limitWidth, height: limitWidth),
  2491. maxSizeInBytes: 1024 * 1024 * 5,
  2492. targetCompression: 1.0)
  2493. }
  2494. //自适应page
  2495. let pageRect = self.listView.currentPage().bounds ?? .zero
  2496. if imageRect.width > pageRect.width ||
  2497. imageRect.height > pageRect.height {
  2498. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  2499. imageRect = CGRect(x: imageRect.origin.x,
  2500. y: imageRect.origin.y,
  2501. width: imageRect.width * pageScale,
  2502. height: imageRect.height * pageScale)
  2503. }
  2504. if imageRect.origin.x < 0 {
  2505. imageRect.origin.x = 5
  2506. }
  2507. if imageRect.origin.y < 0 {
  2508. imageRect.origin.y = 5
  2509. }
  2510. if imageRect.origin.x + imageRect.width > pageRect.width ||
  2511. imageRect.origin.y + imageRect.height > pageRect.height {
  2512. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  2513. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  2514. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  2515. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  2516. }
  2517. DispatchQueue.main.async {
  2518. self.listView.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  2519. }
  2520. }
  2521. }
  2522. }
  2523. }
  2524. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  2525. if rect.size.width < 5 && rect.size.height < 5 {
  2526. let areas = self.listView.km_EditingAreas()
  2527. if let area = areas.last {
  2528. if let data = area as? CPDFEditTextArea {
  2529. if let str = data.editTextAreaString(), str.isEmpty {
  2530. self.listView.remove(with: area)
  2531. } else {
  2532. self.listView.updateEditing([])
  2533. }
  2534. }
  2535. }
  2536. return
  2537. }
  2538. var newRect = rect
  2539. if rect.size.equalTo(.zero) {
  2540. newRect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  2541. } else {
  2542. newRect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  2543. }
  2544. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  2545. let fontSize = model.fontSize
  2546. let fontColor = model.color
  2547. let fontAlign = model.alignment
  2548. NSColorPanel.shared.color = fontColor
  2549. let cfont = CPDFFont(familyName: model.fontName, fontStyle: model.fontStyle)
  2550. let fontNameZ = CPDFFont.convertAppleFont(cfont)
  2551. let font = NSFont(name: fontNameZ ?? "Helvetica", size: fontSize)
  2552. let attri = CEditAttributes()
  2553. attri.font = font!
  2554. attri.fontColor = fontColor
  2555. attri.alignment = fontAlign
  2556. attri.isBold = model.bold
  2557. attri.isItalic = model.italic
  2558. self.listView.createStringBounds(newRect, with: attri, page: page)
  2559. }
  2560. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  2561. rightSideController?.reloadEditingAreas()
  2562. toggleClosePopUIWindow()
  2563. }
  2564. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  2565. rightSideController?.reloadEditingAreas()
  2566. toggleClosePopUIWindow()
  2567. }
  2568. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  2569. rightSideController?.reloadEditingAreas()
  2570. reloadPopUIWindow()
  2571. }
  2572. func pdfViewEditingSelectCharDidChanged(_ pdfView: CPDFView!) {
  2573. rightSideController?.reloadEditingAreas()
  2574. }
  2575. func pdfViewEditingExitCropMode(_ pdfView: CPDFView!, forEditing editingArea: CPDFEditImageArea!) {
  2576. rightSideController?.reloadEditingAreas()
  2577. toggleClosePopUIWindow()
  2578. }
  2579. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  2580. let areas = self.listView.km_editingImageAreas()
  2581. if areas.count == 1 {
  2582. if let data = areas.first as? CPDFEditImageArea {
  2583. let updating = self.listView.editAreaBoundUpdating
  2584. if updating {
  2585. self.listView.editAreaBoundUpdating = false
  2586. }
  2587. }
  2588. }
  2589. }
  2590. func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  2591. }
  2592. func pdfListViewKeyDownIsContinue(_ pdfListView: CPDFListView!, theEvent: NSEvent!) -> Bool {
  2593. let command = theEvent.modifierFlags.contains(.command)
  2594. let control = theEvent.modifierFlags.contains(.control)
  2595. KMPrint(theEvent.keyCode)
  2596. if self.listView.isEditing() == true {
  2597. if control && theEvent.keyCode == 11 { // ctr + b
  2598. self.listView.setEditingTextarea_Bold()
  2599. rightSideController?.reloadEditingAreas()
  2600. return false
  2601. } else if control && theEvent.keyCode == 34 { // ctr +i
  2602. self.listView.setEditingTextarea_Italic()
  2603. rightSideController?.reloadEditingAreas()
  2604. return false
  2605. } else if theEvent.keyCode == 36 { // enter
  2606. if self.listView.isCropMode {
  2607. self.listView.cropComfirmAction()
  2608. rightSideController?.reloadEditingAreas()
  2609. return false
  2610. }
  2611. } else if theEvent.keyCode == 53 { // Cancel
  2612. if self.listView.isCropMode {
  2613. self.listView.cropCancelAction()
  2614. rightSideController?.reloadEditingAreas()
  2615. return false
  2616. }
  2617. }
  2618. }
  2619. if (theEvent.keyCode == 11 && command) { // command + B [添加书签]
  2620. self.menuItemBookMarkClick_add(sender: NSMenuItem())
  2621. return false
  2622. } else if (command && control && theEvent.keyCode == 14) { // command + control + E [注释 橡皮擦]
  2623. return false
  2624. } else if (theEvent.keyCode == 123) { // 向左
  2625. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2626. return false
  2627. } else {
  2628. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage()) {
  2629. self.listView.goToPreviousPage(nil)
  2630. return false
  2631. }
  2632. }
  2633. } else if (theEvent.keyCode == 126) { // 向上
  2634. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2635. return false
  2636. } else {
  2637. if (self.listView.isContinousScroll()) {
  2638. return true
  2639. }
  2640. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToPreviousPage()) {
  2641. self.listView.goToPreviousPage(nil)
  2642. return false
  2643. }
  2644. }
  2645. } else if (theEvent.keyCode == 124) { // 向右
  2646. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2647. return false
  2648. } else {
  2649. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage()) {
  2650. self.listView.goToNextPage(nil)
  2651. return false
  2652. }
  2653. }
  2654. } else if (theEvent.keyCode == 125) { // 向下
  2655. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2656. return false
  2657. } else {
  2658. if (self.listView.isContinousScroll()) {
  2659. return true
  2660. }
  2661. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToNextPage()) {
  2662. self.listView.goToNextPage(nil)
  2663. return false
  2664. }
  2665. }
  2666. } else if (theEvent.keyCode == 36) {
  2667. if self.listView.shouAddEditAreaType() == .image || self.listView.shouAddEditAreaType() == .text {
  2668. if self.listView.isEditImage {
  2669. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  2670. }
  2671. }
  2672. }
  2673. if theEvent.keyCode == 53 {
  2674. //ESC
  2675. // self.exitFullScreen()
  2676. if viewManager.isPDFReadMode {
  2677. self.exitPDFReadMode()
  2678. }
  2679. self.leftSideViewCancelSelect()
  2680. }
  2681. return true
  2682. }
  2683. func pdfListViewMenuValidate(_ pdfListView: CPDFListView!, menuItem: NSMenuItem!, isTakesEffect: UnsafeMutablePointer<ObjCBool>!) -> Bool {
  2684. guard let action = menuItem.action else {
  2685. isTakesEffect.pointee = false
  2686. return false
  2687. }
  2688. if (KMSystemMenu.isEditSelector(sel: action)) {
  2689. if (KMSystemMenu.Edit.deleteSelector == action) {
  2690. isTakesEffect.pointee = true
  2691. return self.listView.activeAnnotations.count > 0
  2692. } else if (KMSystemMenu.Edit.copySelector == action) {
  2693. isTakesEffect.pointee = true
  2694. return true//self.listView.canCopy()
  2695. } else if (KMSystemMenu.Edit.cutSelector == action) {
  2696. isTakesEffect.pointee = true
  2697. return self.listView.canCopy()
  2698. } else if (KMSystemMenu.Edit.pasteSelector == action) {
  2699. isTakesEffect.pointee = true
  2700. return self.listView.canPaste()
  2701. }
  2702. }
  2703. isTakesEffect.pointee = false
  2704. return false
  2705. }
  2706. //MARK: - CPDFListViewDelegate
  2707. func cPDFListView(_ pdfListView: CPDFListView, didDelete annotation: CPDFAnnotation, in pdfPage: CPDFPage) {
  2708. self.leftSideViewController.updateThumbnail(at: Int(pdfPage.pageIndex()))
  2709. }
  2710. func pdfListViewChangeatioActiveAnnotations(_ pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!, isRightMenu: Bool) {
  2711. self.view.window?.makeFirstResponder(self.listView)
  2712. reloadPopUIWindow()
  2713. if isRightMenu {
  2714. } else if annotations.count > 0 {
  2715. if annotations.count > 1 {
  2716. let fristAnnotation = annotations.first
  2717. var isSameAnnotation = true
  2718. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  2719. for annotation in annotations {
  2720. let cunrrentClassName = NSStringFromClass(annotation.classForCoder)
  2721. if (className == "CPDFSquareAnnotation") ||
  2722. (className == "CPDFCircleAnnotation") ||
  2723. (className == "CPDFLineAnnotation") {
  2724. if (cunrrentClassName != "CPDFSquareAnnotation") &&
  2725. (cunrrentClassName != "CPDFCircleAnnotation") &&
  2726. (cunrrentClassName != "CPDFLineAnnotation") {
  2727. isSameAnnotation = false
  2728. }
  2729. } else {
  2730. if className != cunrrentClassName {
  2731. isSameAnnotation = false
  2732. }
  2733. }
  2734. }
  2735. if isSameAnnotation == false {
  2736. } else {
  2737. self.toggleOpenRightSide()
  2738. }
  2739. self.rightSideController?.reloadDataWithPDFView(pdfView: pdfListView)
  2740. } else {
  2741. let fristAnnotation = annotations.first
  2742. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  2743. if self.viewManager.isPDFReadMode {
  2744. toggleCloseRightSide()
  2745. } else {
  2746. if className != "CPDFStampAnnotation" &&
  2747. className != "CPDFSignatureAnnotation" &&
  2748. className != "CPDFListStampAnnotation" {
  2749. self.toggleOpenRightSide()
  2750. }
  2751. self.rightSideController?.reloadDataWithPDFView(pdfView: pdfListView)
  2752. }
  2753. }
  2754. if (listView.activeAnnotation.isKind(of: CPDFLineAnnotation.self)) {
  2755. if (!(listView.activeAnnotation as! CPDFLineAnnotation).isMeasure) {
  2756. cancelMeasureType()
  2757. } else {
  2758. if distanceMeasureInfoWindowController == nil {
  2759. let measureInfo = CPDFDistanceMeasureInfo()
  2760. distanceMeasureInfoWindowController = CDistanceMeasureInfoWindowController()
  2761. distanceMeasureInfoWindowController?.measureInfo = measureInfo
  2762. distanceMeasureInfoWindowController?.delegate = self
  2763. }
  2764. }
  2765. } else if (!listView.activeAnnotation.isKind(of: CPDFPolygonAnnotation.self) && !listView.activeAnnotation.isKind(of: CPDFPolylineAnnotation.self)) {
  2766. cancelMeasureType()
  2767. } else if (listView.activeAnnotation.isKind(of: CPDFPolygonAnnotation.self) || listView.activeAnnotation.isKind(of: CPDFPolylineAnnotation.self)) {
  2768. if perimeterMeasureInfoWindowController == nil {
  2769. let measureInfo = CPDFPerimeterMeasureInfo()
  2770. perimeterMeasureInfoWindowController = CPerimeterMeasureInfoWindowController()
  2771. perimeterMeasureInfoWindowController?.measureInfo = measureInfo
  2772. perimeterMeasureInfoWindowController?.delegate = self
  2773. }
  2774. if areaMeasureInfoWindowController == nil {
  2775. let measureInfo = CPDFAreaMeasureInfo()
  2776. areaMeasureInfoWindowController = CAreaMeasureInfoWindowController()
  2777. areaMeasureInfoWindowController?.measureInfo = measureInfo
  2778. areaMeasureInfoWindowController?.delegate = self
  2779. }
  2780. }
  2781. } else if (annotations.count == 0){
  2782. if pdfListView.annotationType == .unkown {
  2783. toggleCloseRightSide()
  2784. } else {
  2785. if self.viewManager.isPDFReadMode {
  2786. toggleCloseRightSide()
  2787. } else {
  2788. toggleOpenRightSide()
  2789. }
  2790. }
  2791. rightSideController?.reloadDataWithPDFView(pdfView: pdfListView)
  2792. }
  2793. }
  2794. func pdfListViewMobileAnnotationBegan(_ point: CGPoint, for pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!) {
  2795. toggleClosePopUIWindow()
  2796. }
  2797. func pdfListViewMobileAnnotationMove(_ point: CGPoint, for pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!) {
  2798. toggleClosePopUIWindow()
  2799. }
  2800. func pdfListViewMobileAnnotationEnd(_ point: CGPoint, for pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!) {
  2801. reloadPopUIWindow()
  2802. }
  2803. func pdfListViewChangedAnnotationType(_ pdfListView: CPDFListView!, for annotationType: CAnnotationType) {
  2804. if(annotationType == .unkown) {
  2805. toggleCloseRightSide()
  2806. }
  2807. let aType = annotationType
  2808. if aType.isMarkup() || aType == .anchored || aType == .freeText || aType.isSquare() || aType == .link {
  2809. KMDataManager.ud_set(annotationType.rawValue, forKey: SKLastAnnotationModeKey)
  2810. }
  2811. }
  2812. ///开始定位链接注释
  2813. func pdfListViewLinkDestinationStart(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  2814. }
  2815. ///刷新链接注释
  2816. func pdfListViewLinkDestinationEnd(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  2817. }
  2818. func pdfListViewMenuItemsEditing(at point: CGPoint, for page: CPDFPage!, menuItems: [NSMenuItem]!) -> [NSMenuItem]! {
  2819. if (listView.toolMode != CToolMode.editPDFToolMode) {
  2820. return menuItems
  2821. }
  2822. var tMenuItems = menuItems;
  2823. if(listView.isSelectEditCharRange() ||
  2824. listView.isSelecteditArea(with: point)) {
  2825. tMenuItems?.append(NSMenuItem.separator())
  2826. }
  2827. let areas = self.listView.editingAreas() ?? []
  2828. if areas.count == 1 {
  2829. let fristAreas = areas.first
  2830. if fristAreas is CPDFEditImageArea {
  2831. self.listView.selectImageAreas = fristAreas as? CPDFEditImageArea
  2832. if self.listView.isEditImage {
  2833. tMenuItems?.removeAll()
  2834. tMenuItems?.append(self.corpImageMenuItem())
  2835. tMenuItems?.append(self.cancelCorpImageMenuItem())
  2836. tMenuItems?.append(self.restoreCorpImageMenuItem())
  2837. } else {
  2838. tMenuItems?.append(NSMenuItem.separator())
  2839. tMenuItems?.append(self.cutImageArea())
  2840. tMenuItems?.append(self.replaceImageArea())
  2841. tMenuItems?.append(self.exportImageArea())
  2842. }
  2843. } else {
  2844. if tMenuItems?.count != 1 {
  2845. tMenuItems?.swapAt(0, 1)
  2846. }
  2847. }
  2848. } else if areas.count == 0 {
  2849. tMenuItems?.append(NSMenuItem.separator())
  2850. tMenuItems?.append(self.addText())
  2851. tMenuItems?.append(self.addImage())
  2852. }
  2853. return tMenuItems
  2854. }
  2855. func pdfListViewMenu(forEvent pdfListView: CPDFListView!, for theEvent: NSEvent!, click menu: AutoreleasingUnsafeMutablePointer<NSMenu?>!, isMoveSelectAnno: Bool) {
  2856. self.mouseRightMenuEvent = theEvent
  2857. var currentMenu : NSMenu = menu.pointee!
  2858. if let activeAnno = listView.activeAnnotation as? KMTableAnnotation {//Table
  2859. var pagePoint = NSPoint()
  2860. _ = self.listView.pageAndPoint(&pagePoint, for: theEvent, nearest: true)
  2861. currentMenu.removeAllItems()
  2862. let annotation = activeAnno
  2863. annotation.completeEditCellText()
  2864. if !(NSIsEmptyRect(annotation.drawRect)) {
  2865. annotation.drawLine(pagePoint)
  2866. NotificationCenter.default.post(name: NSNotification.Name.KMPDFViewTableAnnotationDidChange, object: self, userInfo: ["point": NSValue(point: pagePoint)])
  2867. }
  2868. if (annotation.rowNumber - annotation.currentCell.row - 1) < 0 {
  2869. return
  2870. }
  2871. currentMenu = tableMenu(currentMenu, withTable: listView.activeAnnotation as! KMTableAnnotation, point: pagePoint)
  2872. listView.needsDisplay = true
  2873. return
  2874. }
  2875. var pagePoint: NSPoint = .zero
  2876. if let page = self.listView.pageAndPoint(&pagePoint, for: theEvent, nearest: true) {
  2877. let anno = page.annotation(at: pagePoint)
  2878. let item1 = NSMenuItem(title: NSLocalizedString("Delete", comment: ""), action: #selector(menuItemActionMeasureDelete), target: self)
  2879. item1.representedObject = anno
  2880. let item2 = NSMenuItem(title: NSLocalizedString("Edit Note", comment: ""), action: #selector(menuItemActionMeasureEditNote), target: self)
  2881. item2.representedObject = anno
  2882. let item3 = NSMenuItem(title: NSLocalizedString("Settings", comment: ""), action: #selector(menuItemActionMeasureSetting), target: self)
  2883. item3.representedObject = anno
  2884. if let data = anno as? CPDFPolygonAnnotation { // 多变形
  2885. currentMenu.removeAllItems()
  2886. currentMenu.insertItem(item1, at: 0)
  2887. currentMenu.insertItem(item2, at: 1)
  2888. currentMenu.insertItem(item3, at: 2)
  2889. return
  2890. }
  2891. if let data = anno as? CPDFPolylineAnnotation {
  2892. currentMenu.removeAllItems()
  2893. currentMenu.insertItem(item1, at: 0)
  2894. currentMenu.insertItem(item2, at: 1)
  2895. currentMenu.insertItem(item3, at: 2)
  2896. return
  2897. }
  2898. if let data = anno as? CPDFLineAnnotation, data.isMeasure {
  2899. currentMenu.removeAllItems()
  2900. currentMenu.insertItem(item1, at: 0)
  2901. currentMenu.insertItem(item2, at: 1)
  2902. currentMenu.insertItem(item3, at: 2)
  2903. return
  2904. }
  2905. }
  2906. if (listView.toolMode == .selectToolMode){
  2907. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2908. currentMenu.insertItem(self.printingMenu(), at: 3)
  2909. currentMenu.insertItem(self.setTTSStype(), at: 3)
  2910. currentMenu.insertItem(self.setCropStype(), at: 3)
  2911. currentMenu.insertItem(self.setSnapshotStype(), at: 3)
  2912. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  2913. export.submenu = self.exportMenu()
  2914. currentMenu.insertItem(export, at: 3)
  2915. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2916. if listView.activeAnnotation == nil{
  2917. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  2918. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2919. }
  2920. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 3)
  2921. currentMenu.insertItem(self.addReadModelStype(), at: currentMenu.items.count - 3)
  2922. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2923. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  2924. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  2925. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  2926. return
  2927. }
  2928. if (listView.toolMode == .moveToolMode || listView.toolMode == .magnifyToolMode){
  2929. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2930. currentMenu.insertItem(self.setTTSStype(), at: 0)
  2931. currentMenu.insertItem(self.setCropStype(), at: 0)
  2932. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  2933. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2934. currentMenu.insertItem(self.addOutlineStype(), at: 0)
  2935. currentMenu.insertItem(self.addBookmarkMenu(), at: 0)
  2936. if listView.activeAnnotation == nil{
  2937. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2938. currentMenu.insertItem(self.setAnnotationToolStype(), at: 0)
  2939. }
  2940. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2941. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  2942. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  2943. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  2944. return
  2945. }
  2946. if currentMenu.items.count > 3 {
  2947. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 3)
  2948. currentMenu.insertItem(self.addReadModelStype(), at: currentMenu.items.count - 3)
  2949. }
  2950. if listView.currentSelection != nil && listView.activeAnnotations.count < 1{
  2951. if listView.currentSelection.selectionType() == .text {
  2952. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2953. currentMenu.insertItem(self.setSearchBaiduStype(), at: 3)
  2954. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2955. currentMenu.insertItem(self.setLookUpStype(), at: 3)
  2956. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2957. currentMenu.insertItem(self.addOutlineStype(), at: 3)
  2958. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2959. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  2960. currentMenu.insertItem(self.setTTSStype(), at: 3)
  2961. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2962. currentMenu.insertItem(self.setShareStype(), at: 3)
  2963. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2964. }
  2965. currentMenu.insertItem(self.enterAnnotationStype(), at: 3)
  2966. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2967. if listView.currentSelection.selectionType() == .image{
  2968. currentMenu.insertItem(NSMenuItem.separator(), at: 6)
  2969. currentMenu.insertItem(self.addOutlineStype(), at: 6)
  2970. currentMenu.insertItem(NSMenuItem.separator(), at: 6)
  2971. currentMenu.insertItem(self.setAnnotationToolStype(), at: 6)
  2972. }
  2973. if listView.currentSelection.selectionType() == .text {
  2974. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2975. currentMenu.insertItem(self.setTranslateStype(), at: currentMenu.items.count)
  2976. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2977. }
  2978. }
  2979. if listView.activeAnnotation != nil || isMoveSelectAnno {
  2980. if let data = self.listView.activeAnnotation?.type?.lowercased(), data == "stamp"{
  2981. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2982. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  2983. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2984. }else{
  2985. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2986. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  2987. if let anno = self.listView.activeAnnotation, anno.isKind(of: CPDFStampAnnotation.self) {
  2988. } else {
  2989. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2990. currentMenu.insertItem(self.setShareStype(), at: currentMenu.items.count - 15)
  2991. }
  2992. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2993. }
  2994. }
  2995. if listView.activeAnnotation == nil && listView.currentSelection == nil{
  2996. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2997. if(listView.toolMode == .selectToolMode) {
  2998. if NSIsEmptyRect(listView.currentSelectionRect()) {
  2999. currentMenu.insertItem(self.zoomSelectionMenuItem(), at: 0)
  3000. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  3001. }
  3002. currentMenu.insertItem(self.printingMenu(), at: 0)
  3003. currentMenu.insertItem(self.setTTSStype(), at: 0)
  3004. currentMenu.insertItem(self.setCropStype(), at: 0)
  3005. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  3006. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  3007. export.submenu = self.exportMenu()
  3008. currentMenu.insertItem(export, at: currentMenu.items.count)
  3009. }else{
  3010. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  3011. currentMenu.insertItem(self.setTTSStype(), at: 2)
  3012. currentMenu.insertItem(self.setCropStype(), at: 2)
  3013. currentMenu.insertItem(self.setSnapshotStype(), at: 2)
  3014. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  3015. currentMenu.insertItem(self.addOutlineStype(), at: 2)
  3016. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  3017. currentMenu.insertItem(self.enterAnnotationStype(), at: 2)
  3018. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  3019. if(currentMenu.items.count > 4) {
  3020. currentMenu.insertItem(NSMenuItem.separator(), at: 5)
  3021. }
  3022. if(currentMenu.items.count > 5) {
  3023. currentMenu.insertItem(self.addBookmarkMenu(), at: 6)
  3024. }
  3025. currentMenu.insertItem(self.setAutoScrollStype(), at: currentMenu.items.count)
  3026. }
  3027. currentMenu.insertItem(self.setAnnotationToolStype(), at: 5)
  3028. }
  3029. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  3030. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  3031. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  3032. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  3033. for item in currentMenu.items {
  3034. if (item.action == NSSelectorFromString("menuItemClick_HidenorShowNote:")) {
  3035. // 显示与隐藏注释 item action 截取
  3036. item.action = #selector(menuItemClick_HidenorShowNote)
  3037. item.target = self
  3038. break
  3039. }
  3040. }
  3041. }
  3042. func pdfListViewAddAnnotations(_ pdfListView: CPDFListView!, forAdd annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  3043. var addRedact = false
  3044. for anno in annotations {
  3045. if (anno.isKind(of: CPDFRedactAnnotation.self)) {
  3046. addRedact = true
  3047. } else if anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation {
  3048. anno.contents = pdfPage?.string(for: anno.bounds) ?? ""
  3049. }
  3050. }
  3051. self.model.hasAddRedact = addRedact
  3052. if self.listView.toolMode == .moveToolMode {
  3053. self.listView.toolMode = .textToolMode
  3054. self.listView.annotationType = .unkown
  3055. }
  3056. if (self.model.rightMouseEventing) {
  3057. self.model.rightMouseEventing = false
  3058. }
  3059. self.leftSideViewController.refreshUIForAddAnnotation(annos: annotations, page: pdfPage)
  3060. }
  3061. func pdfListViewRemoveAnnotations(_ pdfListView: CPDFListView!, forRemove annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  3062. self.leftSideViewController.annoList_refreshUIForDeleteAnnotations(annos: annotations, page: pdfPage)
  3063. }
  3064. func pdfListViewDidSelectionEnd(_ pdfListView: CPDFListView!) {
  3065. if (!self.listView.isEqual(to: pdfListView)) {
  3066. return
  3067. }
  3068. if (self.listView.toolMode != .selectToolMode) {
  3069. return
  3070. }
  3071. }
  3072. func pdfListViewKeyDowClosePanel(_ speedy: CPDFViewSidebarSpeedMode, event theEvent: NSEvent!) {
  3073. if(speedy == .right) {
  3074. } else if (speedy == .left) {
  3075. self.menuItemAction_hiddenLeftSide(speedy)
  3076. }
  3077. }
  3078. func pdfListViewEventMarkupColor(with annotation: CPDFAnnotation!) -> [NSColor]! {
  3079. if (annotation.isKind(of: CPDFMarkupAnnotation.self)) {
  3080. if (annotation as! CPDFMarkupAnnotation).markupType() == .highlight {
  3081. return KMAnnotationPropertiesColorManager.manager.markHighlightColors
  3082. } else {
  3083. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  3084. }
  3085. } else {
  3086. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  3087. }
  3088. }
  3089. func pdfListViewHaveDocumentAttribute() -> Bool {
  3090. if(!self.listView.document.allowsCopying) {
  3091. self.removeOwnerPassword()
  3092. return false
  3093. }
  3094. return true
  3095. }
  3096. func pdfListView(_ sender: CPDFListView!, showSnapshotAtPageNumber pageNum: Int, for rect: NSRect, scaleFactor: CGFloat, autoFits: Bool) {
  3097. let swc = KMSnapshotWindowController(windowNibName: "SnapshotWindow")
  3098. swc.delegate = self
  3099. swc.setPdfDocument(self.listView.document, goToPageNumber: pageNum, rect: rect, scaleFactor: scaleFactor, autoFits: autoFits)
  3100. swc.forceOnTop = self.interactionMode != .normal
  3101. self.myDocument?.addWindowController(swc)
  3102. }
  3103. func pdfListView(_ pdfView: CPDFListView!, documentDataDidChanged docData: Any!, withInfo info: [AnyHashable : Any]!) {
  3104. if let data = info?[CPDFListView.outlineKey] as? Bool, data { // 大纲改变
  3105. guard let ol = docData as? CPDFOutline else {
  3106. return
  3107. }
  3108. let add = info?[CPDFListView.outlineAddKey] as? Bool ?? false
  3109. let remove = info?[CPDFListView.outlineRemoveKey] as? Bool ?? false
  3110. if add {
  3111. self.leftSideViewController.addOutlineAfter(ol)
  3112. }
  3113. if remove {
  3114. self.leftSideViewController.removeOutlineAfter(ol)
  3115. }
  3116. let demote = info?[CPDFListView.outlineDemoteKey] as? Bool ?? false
  3117. let promote = info?[CPDFListView.outlinePromoteKey] as? Bool ?? false
  3118. if demote {
  3119. self.leftSideViewController.demoteOutlineAfter(ol)
  3120. }
  3121. if promote {
  3122. self.leftSideViewController.promoteOutlineAfter(ol)
  3123. }
  3124. }
  3125. }
  3126. //TextEdit
  3127. func pdfListViewDidTextFontChanged(_ pdfListView: CPDFListView!) {
  3128. }
  3129. func pdfListViewDidTextColorChanged(_ pdfListView: CPDFListView!, with color: NSColor!) {
  3130. }
  3131. func pdfListViewAnnotationMeasureInfoChange(_ pdfListView: CPDFListView!, with annotation: CPDFAnnotation!) {
  3132. guard let data = annotation else {
  3133. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  3134. distanceMeasureInfoWindowController?.clearData()
  3135. }
  3136. return
  3137. }
  3138. if let lineAnnotation = annotation as? CPDFLineAnnotation {
  3139. handleLineAnnotation(lineAnnotation)
  3140. } else if let polylineAnnotation = annotation as? CPDFPolylineAnnotation {
  3141. handlePolylineAnnotation(polylineAnnotation)
  3142. } else if let polygonAnnotation = annotation as? CPDFPolygonAnnotation {
  3143. handlePolygonAnnotation(polygonAnnotation)
  3144. }
  3145. }
  3146. func pdfListViewMeasureCancel(_ pdfListView: CPDFListView!) {
  3147. cancelMeasureType()
  3148. }
  3149. func tableMenu(_ menu: NSMenu, withTable table: KMTableAnnotation, point: CGPoint) -> NSMenu {
  3150. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  3151. let itemTitles = ["Edit", "", "Add Row Above", "Add Row Below", "", "Add Column Before", "Add Column After", "", "Delete Row", "Delete Column", "Delete Table", "Cut", "Copy", "Paste", "Paste and Match Style", "Delete Cell Contents", "Clear All"]
  3152. let actions = ["formAnnotTextEdit:", "", "addRowAbove:", "addRowBelow:", "", "addColumnBefore:", "addColumnAfter:", "", "deleteRow:", "deleteColumn:", "deleteTabel", "cutCell:", "copyCell:", "pasteCell:", "pasteAndMatchStyle:", "deleteCellContents:", "clearAll:"]
  3153. for i in 0..<itemTitles.count {
  3154. var item: NSMenuItem? = nil
  3155. if itemTitles[i] == "" {
  3156. item = NSMenuItem.separator()
  3157. menu.insertItem(item!, at: i)
  3158. } else {
  3159. item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  3160. item!.target = self
  3161. item!.action = NSSelectorFromString(actions[i])
  3162. if itemTitles[i] == "Paste" /*&& !_copyCellData*/ {
  3163. item!.action = nil
  3164. } else if itemTitles[i] == "Paste and Match Style" /*&& !_copyCellData */{
  3165. item!.action = nil
  3166. } else if itemTitles[i] == "Add Row Above" {
  3167. let path1 = table.crossLines[table.rowNumber - table.currentCell.row]
  3168. let path2 = table.crossLines[table.rowNumber - table.currentCell.row - 1]
  3169. if (path1 as AnyObject).lineJoinStyle == NSBezierPath.LineJoinStyle.round && table.headerCount() >= 5 {
  3170. item!.action = nil
  3171. } else if (path2 as AnyObject).lineJoinStyle == NSBezierPath.LineJoinStyle.bevel && table.footerCount() >= 5 {
  3172. item!.action = nil
  3173. }
  3174. }
  3175. item!.title = NSLocalizedString(item!.title, comment: "")
  3176. item!.representedObject = NSValue(point: point)
  3177. menu.insertItem(item!, at: i)
  3178. }
  3179. }
  3180. } else {
  3181. let itemTitles = ["Cut", "Copy", "Paste", "Delete"]
  3182. let actions = ["cut:", "copy:", "paste:", "delete:"]
  3183. for i in 0..<itemTitles.count {
  3184. let item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  3185. item.target = self
  3186. item.action = NSSelectorFromString(actions[i])
  3187. item.title = NSLocalizedString(item.title, comment: "")
  3188. menu.insertItem(item, at: i)
  3189. item.representedObject = NSValue(point: point)
  3190. }
  3191. }
  3192. return menu
  3193. }
  3194. private func handleLineAnnotation(_ annotation: CPDFLineAnnotation) {
  3195. if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  3196. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  3197. distanceMeasureInfoWindowController?.showWindow(self)
  3198. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  3199. areaMeasureInfoWindowController?.hideFloatingWindow()
  3200. distanceMeasureInfoWindowController?.showWindow(self)
  3201. } else if distanceMeasureInfoWindowController?.window?.isVisible == false {
  3202. distanceMeasureInfoWindowController?.showWindow(self)
  3203. }
  3204. let measureInfo = annotation.measureInfo
  3205. let startPoint = annotation.startPoint
  3206. let endPoint = annotation.endPoint
  3207. let angle = atan2(endPoint.y - startPoint.y, endPoint.x - startPoint.x) * (180.0 / .pi)
  3208. distanceMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  3209. distanceMeasureInfoWindowController?.xLabel.stringValue = String(format: "%.0f", abs(endPoint.x - startPoint.x))
  3210. distanceMeasureInfoWindowController?.yLabel.stringValue = String(format: "%.0f", abs(endPoint.y - startPoint.y))
  3211. distanceMeasureInfoWindowController?.reloadData(with: measureInfo!)
  3212. }
  3213. private func handlePolylineAnnotation(_ annotation: CPDFPolylineAnnotation) {
  3214. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  3215. distanceMeasureInfoWindowController?.hideFloatingWindow()
  3216. perimeterMeasureInfoWindowController?.showWindow(self)
  3217. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  3218. areaMeasureInfoWindowController?.hideFloatingWindow()
  3219. perimeterMeasureInfoWindowController?.showWindow(self)
  3220. } else if perimeterMeasureInfoWindowController?.window?.isVisible == false {
  3221. perimeterMeasureInfoWindowController?.showWindow(self)
  3222. }
  3223. let measureInfo = annotation.measureInfo
  3224. let savePoints = annotation.savePoints()
  3225. var angle: CGFloat = 0
  3226. if savePoints.count >= 3 {
  3227. let count = savePoints.count
  3228. let startPoint = savePoints[count - 3].pointValue
  3229. let midPoint = savePoints[count - 2].pointValue
  3230. let endPoint = savePoints.last!.pointValue
  3231. angle = angleBetweenPoints(startPoint, midPoint, endPoint)
  3232. }
  3233. angle = 180 - angle
  3234. perimeterMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  3235. perimeterMeasureInfoWindowController?.reloadData(with: measureInfo!)
  3236. }
  3237. private func handlePolygonAnnotation(_ annotation: CPDFPolygonAnnotation) {
  3238. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  3239. distanceMeasureInfoWindowController?.hideFloatingWindow()
  3240. areaMeasureInfoWindowController?.showWindow(self)
  3241. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  3242. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  3243. areaMeasureInfoWindowController?.showWindow(self)
  3244. } else if areaMeasureInfoWindowController?.window?.isVisible == false {
  3245. areaMeasureInfoWindowController?.showWindow(self)
  3246. }
  3247. let measureInfo = annotation.measureInfo
  3248. let savePoints = annotation.savePoints
  3249. var angle: CGFloat = 0
  3250. if savePoints.count >= 3 {
  3251. let count = savePoints.count
  3252. let startPoint = (savePoints[count - 3] as AnyObject).pointValue
  3253. let midPoint = (savePoints[count - 2] as AnyObject).pointValue
  3254. let endPoint = (savePoints.lastObject as AnyObject).pointValue
  3255. angle = angleBetweenPoints(startPoint!, midPoint!, endPoint!)
  3256. }
  3257. angle = 180 - angle
  3258. areaMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  3259. areaMeasureInfoWindowController?.reloadData(measureInfo!)
  3260. }
  3261. private func angleBetweenPoints(_ startPoint: CGPoint, _ midPoint: CGPoint, _ endPoint: CGPoint) -> CGFloat {
  3262. let vector1 = CGPoint(x: midPoint.x - startPoint.x, y: midPoint.y - startPoint.y)
  3263. let vector2 = CGPoint(x: endPoint.x - midPoint.x, y: endPoint.y - midPoint.y)
  3264. let dotProduct = vector1.x * vector2.x + vector1.y * vector2.y
  3265. let magnitude1 = sqrt(vector1.x * vector1.x + vector1.y * vector1.y)
  3266. let magnitude2 = sqrt(vector2.x * vector2.x + vector2.y * vector2.y)
  3267. return acos(dotProduct / (magnitude1 * magnitude2)) * (180.0 / .pi)
  3268. }
  3269. @objc func pdfUpdatedFinish() {
  3270. splitPDFController?.inPDFFirst = false
  3271. splitPDFController?.outPDFFirst = false
  3272. }
  3273. }
  3274. //MARK: - KMNThumbnailBaseViewDelegate
  3275. extension KMMainViewController: KMNThumbnailBaseViewDelegate {
  3276. func clickThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,currentIndex:Int) {
  3277. exitPageEditMode()
  3278. viewManager.isPageEditMode = false
  3279. pdfToolbarController?.reloadPageEditView()
  3280. }
  3281. func insertPDFThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, pdfDocment: CPDFDocument?) {
  3282. if(pdfDocment != nil) {
  3283. insertDocuments.insert(pdfDocment!)
  3284. }
  3285. }
  3286. func changeIndexPathsThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, selectionIndexPaths: Set<IndexPath>, selectionStrings: String) {
  3287. toolbarManager.page_pageInfo_Property.text = selectionStrings
  3288. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  3289. toolbarManager.page_pageInfo_Property.creatable = true
  3290. }
  3291. pdfToolbarController?.refreshSecondToolbarItemsState()
  3292. }
  3293. }
  3294. extension KMMainViewController: KMNLeftSideViewControllerDelegate {
  3295. func enterPageEditLeftSideViewController(leftSideViewController: KMNLeftSideViewController) {
  3296. if viewManager.isPageEditMode == false {
  3297. viewManager.isPageEditMode = true
  3298. if(pdfToolbarController != nil) {
  3299. kmPDFToolbarControllerDidToolbarItemClicked(pdfToolbarController!, KMPDFToolbar_PageEdit_Identifier)
  3300. pdfToolbarController?.reloadSecondToolbar()
  3301. }
  3302. }
  3303. }
  3304. func changeSelectePageLeftSideViewController(leftSideViewController: KMNLeftSideViewController, pageIndex: Int) {
  3305. if(listView.currentPageIndex != pageIndex) {
  3306. listView.go(toPageIndex: pageIndex, animated: true)
  3307. }
  3308. }
  3309. func addBookmarkForLeftC(controller: KMNLeftSideViewController, bookmark: CPDFBookmark?, info: [String : Any]?) {
  3310. if let result = info?["result"] as? Bool {
  3311. if result == false {
  3312. let message = KMNCustomAlertView.alertView(message: KMLocalizedString("This page has been bookmarked"), type: .normal_custom, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
  3313. }
  3314. }
  3315. }
  3316. func switchSearchPopWindow(controller: KMNLeftSideViewController) {
  3317. closeLeftPane()
  3318. let handdler = controller.searchViewC.handdler
  3319. showSearchPopWindow(type: handdler.type, keyborad: handdler.searchKey, replaceText: handdler.replaceKey, results: handdler.searchResults)
  3320. }
  3321. func searchTypeDidChange(controller: KMNLeftSideViewController) {
  3322. let handdler = controller.searchViewC.handdler
  3323. if handdler.type == .replace {
  3324. viewManager.toolMode = .Edit
  3325. updatePDFViewAnnotationMode()
  3326. }
  3327. }
  3328. //MARK: - Notification
  3329. @objc private func pdfViewScrollViewDidScroll(_ noti: Notification) {
  3330. reloadPopUIWindow()
  3331. }
  3332. func pageCountChangedNotification(_ sender: Notification) {
  3333. self.leftSideViewController.refreshUIForAnnoAttributeDidChange(nil, attributes: nil)
  3334. guard let document = sender.object as? CPDFDocument else {
  3335. return
  3336. }
  3337. botaViewController?.pageCountChangedAction(document: document)
  3338. }
  3339. }
  3340. //MARK: -
  3341. //MARK: -
  3342. //MARK: -
  3343. //MARK: - 旧代码,需要用到的内容需要拖出来,写好注释
  3344. extension KMMainViewController {
  3345. func awakeFromNibFunction() {
  3346. self.addBackgroundMaskView()
  3347. listView.delegate = self
  3348. listView.pdfListViewDelegate = self
  3349. if (document != nil) {
  3350. self.listView.document = self.document
  3351. self.listView.document?.delegate = self
  3352. let autoScale = listView.autoScales
  3353. if !autoScale {
  3354. listView.scaleFactor = 1.0
  3355. }
  3356. }
  3357. self.initPDFLeftViewVC()
  3358. self.leftSideViewController.mainViewController = self
  3359. }
  3360. func viewDidAppearFunction() {
  3361. //春季活动
  3362. if ((KMAdvertisementManager.manager.info.popWindowContent) != nil) {
  3363. if KMAdvertisementManager.manager.info.popWindowContent!.content!.count > 0 {
  3364. let info = KMAdvertisementManager.manager.info.popWindowContent!.content?.first
  3365. if KMAdvertisementManager.checkAdvertisementValid(info!) {
  3366. self.loadRecommondPopWindow()
  3367. }
  3368. }
  3369. }
  3370. self.addEventMonitor()
  3371. self.view.window?.makeFirstResponder(self.listView)
  3372. // 更新属性页面的信息
  3373. NotificationCenter.default.post(name: KMInfoWindowC.windowDidBecomeMainNotification, object: self.myDocument)
  3374. self.interfaceThemeDidChanged(self.view.window?.appearance?.name ?? (NSApp.appearance?.name ?? .aqua))
  3375. if (self.document == nil) {
  3376. return
  3377. }
  3378. if (self.document == nil || self.document!.isLocked == false) {
  3379. self.loadFunctionGuide()
  3380. }
  3381. if (self.document?.isLocked == false) {
  3382. if self.model.needConvertNotes && self.tabViewIsDragging() == false {
  3383. self.showConvertNotesProgress()
  3384. }
  3385. return
  3386. }
  3387. if (self.view.window == nil) {
  3388. return
  3389. }
  3390. if (self.model.password != nil) {
  3391. if let data = self.listView.document?.unlock(withPassword: self.model.password), data {
  3392. self.model.isSaveKeyChain = false
  3393. if self.model.needConvertNotes && self.tabViewIsDragging() == false {
  3394. self.showConvertNotesProgress()
  3395. }
  3396. return
  3397. }
  3398. }
  3399. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  3400. if self.view.window != nil && self.tabViewIsDragging() == false {
  3401. self.passwordWindow = KMPasswordInputWindow.openWindow(window: self.view.window!, url: self.document!.documentURL) { [weak self] result , password in
  3402. if (result == .cancel) {
  3403. (self?.myDocument as? KMMainDocument)?.browser?.closeTab()
  3404. return
  3405. }
  3406. self?.model.isSaveKeyChain = true
  3407. self?.listView.document = self?.document
  3408. self?.document?.unlock(withPassword: password)
  3409. }
  3410. } else {
  3411. if self.model.needConvertNotes && self.tabViewIsDragging() == false {
  3412. self.showConvertNotesProgress()
  3413. }
  3414. }
  3415. }
  3416. }
  3417. func viewWillDisappearFunction() {
  3418. if self.interactionMode != .presentation {
  3419. self.removeEventMonitor()
  3420. }
  3421. }
  3422. func viewWillLayoutFunction() {
  3423. if (KMTools.isFullScreen(self.view.window ?? NSWindow())) { // 全屏
  3424. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundFullScreenColor
  3425. } else {
  3426. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor
  3427. }
  3428. if let guideWC = self.guideInfoWindowController{
  3429. var rect = self.view.window!.frame
  3430. rect.size.height -= 20
  3431. guideWC.window?.setFrame(rect, display: false)
  3432. guideWC.window?.minSize = rect.size
  3433. guideWC.window?.maxSize = rect.size
  3434. guideWC.show()
  3435. }
  3436. }
  3437. func viewDidLoadOld() {
  3438. mwcFlags.settingUpWindow = 1
  3439. self.initToolbar()
  3440. if (UserDefaults.standard.object(forKey: CPDFOfficeLeftSidePaneWidthKey) != nil) {
  3441. UserDefaults.standard.set(256, forKey: CPDFOfficeLeftSidePaneWidthKey)
  3442. UserDefaults.standard.synchronize()
  3443. }
  3444. if (UserDefaults.standard.object(forKey: CPDFOfficeRightSidePaneWidthKey) != nil) {
  3445. UserDefaults.standard.set(256, forKey: CPDFOfficeRightSidePaneWidthKey)
  3446. UserDefaults.standard.synchronize()
  3447. }
  3448. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  3449. if (self.listView.document != nil) {
  3450. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document?.documentURL.path ?? "")
  3451. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document?.documentURL.path ?? "")
  3452. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < (self.listView.document?.pageCount ?? 0)) {
  3453. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  3454. if (pageScale != nil) {
  3455. self.listView.scaleFactor = CGFloat(pageScale!)
  3456. }
  3457. self.listView.go(toPageIndex: pageNumber!, animated: false)
  3458. }
  3459. } else {
  3460. self._goToFirstPageForFristAppear()
  3461. }
  3462. }
  3463. } else {
  3464. self._goToFirstPageForFristAppear()
  3465. }
  3466. NotificationCenter.default.addObserver(self, selector: #selector(rename(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerRename"), object: nil)
  3467. NotificationCenter.default.addObserver(self, selector: #selector(closeTab(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseTabs"), object: nil)
  3468. NotificationCenter.default.addObserver(self, selector: #selector(showInFinder(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerShowInFinder"), object: nil)
  3469. NotificationCenter.default.addObserver(self, selector: #selector(preferenceDidChangeNotification), name: KMPreferenceManager.didChangeNotification, object: nil)
  3470. NotificationCenter.default.addObserver(self, selector: #selector(documentDidUnlockNotification), name: Notification.Name("CPDFDocumentDidUnlockNotification"), object: nil)
  3471. NotificationCenter.default.addObserver(self, selector: #selector(annotationsAttributeHasChange), name: NSNotification.Name.CPDFListViewAnnotationsAttributeHasChange, object:nil)
  3472. NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminateNotification), name: NSApplication.willTerminateNotification, object: nil)
  3473. NotificationCenter.default.addObserver(self, selector: #selector(CEditPDFToolModeChangeStateUnkownNotification), name: Notification.Name.init("CEditPDFToolModeChangeStateUnkown"), object: nil)
  3474. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.listView)
  3475. NotificationCenter.default.addObserver(self, selector: #selector(handleDisplayBoxChangedNotification), name: NSNotification.Name.CPDFViewDisplayBoxChanged, object: self.listView)
  3476. NotificationCenter.default.addObserver(self, selector: #selector(didAddContentViewNotification), name: NSWindow.didAddContentViewNotification, object: nil)
  3477. NotificationCenter.default.addObserver(self, selector: #selector(addAutoSaveEvent), name: AutoSaveManager.kTimeValueChangedNotificationName, object: nil)
  3478. NotificationCenter.default.addObserver(self, selector: #selector(didRemoveAnnotationNotification), name: NSNotification.Name.CPDFPageDidRemoveAnnotation, object: nil)
  3479. Task {
  3480. self.addAutoSaveEvent()
  3481. }
  3482. self.toggleCloseRightSide()
  3483. self.addKeyEventMonitor()
  3484. self.addAdsBannerView()
  3485. var snapshotSetups: NSArray?
  3486. if KMPreferenceManager.shared.rememberSnapshot {
  3487. if let fileUrl = (self.myDocument as? KMMainDocument)?.fileURL {
  3488. snapshotSetups = SKBookmarkController.shared().snapshotsForRecentDocument(at: fileUrl) as NSArray?
  3489. }
  3490. }
  3491. if let cnt = snapshotSetups?.count, cnt > 0 {
  3492. if let data = self.listView.document?.isLocked, data {
  3493. self.savedNormalSetup.setObject(snapshotSetups as Any, forKey: "snapshots" as NSCopying)
  3494. } else {
  3495. self.showSnapshots(setups: snapshotSetups)
  3496. }
  3497. }
  3498. let readModel = UserDefaults.standard.bool(forKey: "kKMPDFViewIsReadMode")
  3499. if readModel == true {
  3500. self.openPDFReadMode()
  3501. }
  3502. let hasWindowSetup = savedNormalSetup.count > 0
  3503. if UserDefaults.standard.dictionary(forKey: KMDefaultPDFDisplaySettingsKey) != nil {
  3504. let pdfSettings: NSDictionary = hasWindowSetup ? savedNormalSetup : UserDefaults.standard.dictionary(forKey: KMDefaultPDFDisplaySettingsKey)! as NSDictionary
  3505. self.applyPDFSettings(pdfSettings)
  3506. } else {
  3507. self.applyPDFSettings(savedNormalSetup)
  3508. }
  3509. //文字
  3510. let fontManager = NSFontManager.shared
  3511. fontManager.target = self
  3512. fontManager.action = #selector(changeFont(_:))
  3513. }
  3514. //MARK: - PDFListView
  3515. func initPDFLeftViewVC() {
  3516. var frame = self.leftView.frame
  3517. frame.size.width += 44
  3518. self.leftView.frame = frame
  3519. leftSideViewController.isFirst = true
  3520. leftSideViewController.listView = self.listView
  3521. leftSideViewController.view.frame = CGRect(x: 0, y:0 , width: self.leftView.frame.size.width, height: self.leftView.frame.size.height)
  3522. leftSideViewController.view.autoresizingMask = [.height,.width]
  3523. leftSideViewController.delegate = self
  3524. self.leftView.addSubview(leftSideViewController.view)
  3525. }
  3526. func addAdsBannerView() {
  3527. #if VERSION_FREE
  3528. if !IAPProductsManager.default().isAvailableAllFunction(){
  3529. guard let document = self.listView.document else {
  3530. return
  3531. }
  3532. if !document.isLocked {
  3533. }
  3534. NotificationCenter.default.addObserver(self, selector: #selector(purchaseStateUpdateNoti), name: NSNotification.Name(rawValue: "KMIAPProductPurchasedNotification"), object: nil)
  3535. NotificationCenter.default.addObserver(self, selector: #selector(purchaseStateUpdateNoti), name: NSNotification.Name(rawValue: "kDeviceActivateNotification"), object: nil)
  3536. }
  3537. #endif
  3538. }
  3539. // MARK: Private Methods
  3540. private func _isArabicLanguage() -> Bool {
  3541. return NSLocalizedString("Right click a color and select “Change Color...“.", comment: "") == "انقر بزر الماوس الأيمن فوق اللون وحدد \"تغيير اللون...\"."
  3542. }
  3543. internal func removeNotifications() {
  3544. NotificationCenter.default.removeObserver(self)
  3545. self.leftSideViewController.clearAnnotationFilterData()
  3546. self.leftSideViewController.clearNotification()
  3547. }
  3548. func checkShouldAutoOpenLeftVC() {
  3549. if KMPreference.shared.showLeftSideBar == false {
  3550. return
  3551. }
  3552. if self.model.leftPanelOpen {
  3553. return
  3554. }
  3555. let readModel = UserDefaults.standard.bool(forKey: "kKMPDFViewIsReadMode")
  3556. if readModel == true {
  3557. return
  3558. }
  3559. DispatchQueue.main.async {
  3560. self.leftSideViewController.showThumbnail()
  3561. }
  3562. }
  3563. func removeAllAnnotations() {
  3564. let alert = NSAlert()
  3565. alert.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  3566. alert.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  3567. alert.addButton(withTitle: NSLocalizedString("No", comment:""))
  3568. if (alert.runModal() != .alertFirstButtonReturn) {
  3569. return
  3570. }
  3571. DispatchQueue.main.async {
  3572. self.removeAllAnnotationsStore.store(t: self.listView)
  3573. }
  3574. }
  3575. @objc func cancelMeasureType() {
  3576. self.hideMeasureFloatingWindows()
  3577. }
  3578. func hideMeasureFloatingWindows() {
  3579. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  3580. distanceMeasureInfoWindowController?.hideFloatingWindow()
  3581. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  3582. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  3583. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  3584. areaMeasureInfoWindowController?.hideFloatingWindow()
  3585. }
  3586. }
  3587. func showMeasureFloatingWindowsIfNeed() {
  3588. let toolMode = self.listView.toolMode
  3589. if toolMode != .measureToolMode {
  3590. return
  3591. }
  3592. let type = self.listView.annotationType
  3593. if type == .line {
  3594. self.distanceMeasureInfoWindowController?.window?.orderFront(nil)
  3595. } else if type == .polyLine {
  3596. self.perimeterMeasureInfoWindowController?.window?.orderFront(nil)
  3597. } else if type == .polyGon {
  3598. self.areaMeasureInfoWindowController?.window?.orderFront(nil)
  3599. } else if type == .square {
  3600. self.areaMeasureInfoWindowController?.window?.orderFront(nil)
  3601. }
  3602. }
  3603. // MARK: - 标记密文
  3604. func enterRedact() {
  3605. if !IAPProductsManager.default().isAvailableAllFunction(){
  3606. let winC = KMPurchaseCompareWindowController.sharedInstance()
  3607. winC?.kEventName = "Reading_Redact_BuyNow"
  3608. winC?.showWindow(nil)
  3609. return
  3610. }
  3611. if self.listView.document?.allowsPrinting == false || self.listView.document?.allowsCopying == false {
  3612. Task {
  3613. _ = await KMAlertTool.runModel(message: KMLocalizedString("This is a secured document. Editing is not permitted."))
  3614. }
  3615. return
  3616. }
  3617. if self.hasEnterRedact() {
  3618. self.exitRedact()
  3619. return
  3620. }
  3621. self.leftSideViewController.thumbnailTableView.isEnabled = false
  3622. self.leftSideViewController.tocOutlineView.isEnabled = false
  3623. self.leftSideViewController.noteOutlineView.isEnabled = false
  3624. self.leftSideViewController.findTableView.isEnabled = false
  3625. self.leftSideViewController.groupedFindTableView.isEnabled = false
  3626. self.leftSideViewController.snapshotTableView.isEnabled = false
  3627. let ttsWindowC = KMTTSWindowController.share
  3628. if ttsWindowC.pdfView?.document?.documentURL.path == self.listView.document?.documentURL.path {
  3629. if let data = ttsWindowC.window?.isVisible, data {
  3630. ttsWindowC.stopSpeaking()
  3631. ttsWindowC.close()
  3632. }
  3633. }
  3634. NSColorPanel.shared.showsAlpha = false
  3635. redactController = KMPDFRedactViewController(url: self.listView.document!.documentURL, password: self.listView.document?.password)
  3636. self.addChild(redactController)
  3637. redactController.view.autoresizingMask = [.width, .height]
  3638. self.listView.isHidden = true
  3639. redactController.scaleFactor = self.listView.scaleFactor
  3640. redactController.titleBack = { [weak self] title in
  3641. self?.view.window?.title = title
  3642. }
  3643. redactController.callback = { [weak self] result, currentPageIndex, saveResult, saveUrl in
  3644. self?.listView.go(toPageIndex: self!.redactController.redactPdfView.currentPageIndex, animated: false)
  3645. if result == false { // 退出
  3646. self?.exitRedact()
  3647. return
  3648. }
  3649. let controller = self?._getPDFRedactController()
  3650. controller?.redactPdfView.newAddAnnotation.removeAll()
  3651. self?.exitRedact()
  3652. if saveResult {
  3653. let newDocument = CPDFDocument(url: saveUrl)
  3654. if let data = newDocument?.isLocked, data {
  3655. newDocument?.unlock(withPassword: self?.listView.document?.password ?? "")
  3656. }
  3657. self?.document = newDocument
  3658. self?.listView.document = newDocument
  3659. self?.listView.layoutDocumentView()
  3660. }
  3661. }
  3662. redactController.setCurrentPageIndex(self.listView.currentPageIndex)
  3663. }
  3664. func exitRedact() {
  3665. self.leftSideViewController.thumbnailTableView.isEnabled = true
  3666. self.leftSideViewController.tocOutlineView.isEnabled = true
  3667. self.leftSideViewController.noteOutlineView.isEnabled = true
  3668. self.leftSideViewController.findTableView.isEnabled = true
  3669. self.leftSideViewController.groupedFindTableView.isEnabled = true
  3670. self.leftSideViewController.snapshotTableView.isEnabled = true
  3671. let controller = self._getPDFRedactController()
  3672. if let data = controller {
  3673. if data.redactPdfView.newAddAnnotation.count > 0 {
  3674. KMAlertTool.runModel(message: "", informative: KMLocalizedString("There are unapplied redactions in this file. Exit will not save redaction."), buttons: [KMLocalizedString("Exit"), KMLocalizedString("Cancel")]) { response in
  3675. if response == .alertFirstButtonReturn {
  3676. controller?.redactPdfView.newAddAnnotation.removeAll()
  3677. self.exitRedact()
  3678. }
  3679. }
  3680. return
  3681. }
  3682. }
  3683. NSColorPanel.shared.showsAlpha = true
  3684. controller?.redactPdfView.resignMonitor()
  3685. controller?.view.removeFromSuperview()
  3686. controller?.removeFromParent()
  3687. self.listView.isHidden = false
  3688. self.listView.annotationType = .unkown
  3689. }
  3690. func hasEnterRedact() -> Bool {
  3691. return self._getPDFRedactController() != nil
  3692. }
  3693. //MARK: - AI
  3694. func showAITypeChooseView(aiConfigType: AIConfigType) -> Void {
  3695. if (self.document != nil) {
  3696. AIChatInfoManager.defaultManager.currentFilePath = (self.document?.documentURL.path)!
  3697. } else {
  3698. AIChatInfoManager.defaultManager.currentFilePath = ""
  3699. }
  3700. let windowVC: AINewConfigWindowController = AINewConfigWindowController.currentWC()
  3701. windowVC.chooseCurFileHandle = {[unowned self] windowVC in
  3702. if AIChatInfoManager.defaultManager.currentFilePath.isEmpty == false {
  3703. let documentArray = NSDocumentController.shared.documents
  3704. var didFileEdit: Bool = false
  3705. var curDoc: KMMainDocument!
  3706. for document in documentArray {
  3707. if document.fileURL?.path == AIChatInfoManager.defaultManager.currentFilePath {
  3708. didFileEdit = document.isDocumentEdited
  3709. curDoc = document as! KMMainDocument
  3710. break
  3711. }
  3712. }
  3713. if didFileEdit {
  3714. let tempFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(AIChatInfoManager.defaultManager.currentFilePath.lastPathComponent)
  3715. if FileManager.default.fileExists(atPath: tempFileURL.path) {
  3716. do {
  3717. try FileManager.default.removeItem(at: tempFileURL)
  3718. } catch {
  3719. }
  3720. }
  3721. if curDoc != nil {
  3722. curDoc.mainViewController?.SaveTempPDFDocumentToURLPath(tempPath: tempFileURL.path)
  3723. }
  3724. }
  3725. windowVC.window?.becomeMain()
  3726. }
  3727. }
  3728. if windowVC.window?.isVisible == true && windowVC.didSetOriginFrame == true {
  3729. } else {
  3730. var windowRect = windowVC.window?.frame
  3731. windowRect!.origin.x = NSMaxX(self.view.window!.frame) - (windowRect?.size.width)!
  3732. windowRect!.origin.y = NSMaxY(self.view.window!.frame) - (windowRect?.size.height)! - 64
  3733. windowVC.window?.setFrame(windowRect!, display: true)
  3734. windowVC.didSetOriginFrame = true
  3735. }
  3736. windowVC.eventLabel = "AITools_Tbr"
  3737. windowVC.showWindow(nil)
  3738. if (aiConfigType != .none) {
  3739. windowVC.eventLabel = "AITools_Start"
  3740. if self.listView.currentSelection?.string()?.isEmpty == false {
  3741. windowVC.setCurrentPDFSelection(self.listView.currentSelection.string())
  3742. }
  3743. windowVC.chooseAIFunctionWithType(aiConfigType)
  3744. }
  3745. }
  3746. @objc func aiTipIconViewShowStateChangeNoti() {
  3747. }
  3748. //MARK: - 引导
  3749. func loadFunctionGuide() -> Void {
  3750. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  3751. if self.view.window != nil {
  3752. self.loadOpenFileFunctionGuide(.openFileNormal)
  3753. }
  3754. }
  3755. }
  3756. func loadOpenFileFunctionGuide(_ showType: KMGuideInfoType) -> Void {
  3757. if showType == .openFileNormal && KMGuideInfoWindowController.availableShow(.openFileNormal) {
  3758. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  3759. guard let guideWC = self.guideInfoWindowController else { return }
  3760. guideWC.type = .openFileNormal
  3761. // guideWC.openPanelRect = (self.view.window?.contentView?.convert(leftPanelItem.frame, from: leftPanelItem.superview)) ?? .zero
  3762. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  3763. guideWC.normalGuideFinishHandle = { [weak self] windowVC in
  3764. }
  3765. guideWC.finishHandle = { [weak self] windowVC, type in
  3766. if type == .windowNewFinish ||
  3767. type == . windowDigitalFinish {
  3768. self?.checkFirstTrialController()
  3769. }
  3770. }
  3771. guideWC.openFileToggleHandle = { [weak self] windowVC, type in
  3772. self?.checkFirstTrialController()
  3773. }
  3774. var rect = self.view.window!.frame
  3775. rect.size.height -= 20
  3776. guideWC.window?.setFrame(rect, display: false)
  3777. guideWC.window?.minSize = rect.size
  3778. guideWC.window?.maxSize = rect.size
  3779. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  3780. guideWC.show()
  3781. } else if showType == .digitalSignGuide && KMGuideInfoWindowController.availableShow(.digitalSignGuide) {
  3782. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  3783. guard let guideWC = self.guideInfoWindowController else { return }
  3784. guideWC.type = .digitalSignGuide
  3785. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  3786. guideWC.finishHandle = { [weak self] windowVC, type in
  3787. self?.checkFirstTrialController()
  3788. }
  3789. var rect = self.view.window!.frame
  3790. rect.size.height -= 20
  3791. guideWC.window?.setFrame(rect, display: false)
  3792. guideWC.window?.minSize = rect.size
  3793. guideWC.window?.maxSize = rect.size
  3794. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  3795. guideWC.show()
  3796. } else if showType == .pdfCompareGuide && KMGuideInfoWindowController.availableShow(.pdfCompareGuide) {
  3797. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  3798. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  3799. guard let guideWC = self.guideInfoWindowController else { return }
  3800. guideWC.type = .pdfCompareGuide
  3801. guard let win = self.view.window else {
  3802. return
  3803. }
  3804. guideWC.finishHandle = { [weak self] winC, type in
  3805. if type == .windowNewFinish {
  3806. DispatchQueue.main.async {
  3807. self?.loadOpenFileFunctionGuide(.measureGuide)
  3808. }
  3809. return
  3810. }
  3811. }
  3812. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  3813. var rect = self.view.window!.frame
  3814. rect.size.height -= 20
  3815. guideWC.window?.setFrame(rect, display: false)
  3816. guideWC.window?.minSize = rect.size
  3817. guideWC.window?.maxSize = rect.size
  3818. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  3819. guideWC.show()
  3820. }
  3821. } else if showType == .measureGuide && KMGuideInfoWindowController.availableShow(.measureGuide) {
  3822. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  3823. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  3824. guard let guideWC = self.guideInfoWindowController else { return }
  3825. guard let _ = self.view.window else {
  3826. return
  3827. }
  3828. guideWC.type = .measureGuide
  3829. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  3830. var rect = self.view.window?.frame ?? .zero
  3831. rect.size.height -= 20
  3832. guideWC.window?.setFrame(rect, display: false)
  3833. guideWC.window?.minSize = rect.size
  3834. guideWC.window?.maxSize = rect.size
  3835. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  3836. guideWC.show()
  3837. }
  3838. } else if showType == .convertGuide && KMGuideInfoWindowController.availableShow(.convertGuide) {
  3839. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  3840. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  3841. guard let guideWC = self.guideInfoWindowController else { return }
  3842. guideWC.type = .convertGuide
  3843. guard let win = self.view.window else {
  3844. return
  3845. }
  3846. guideWC.purchaseHandle = { [weak self] windowVC in
  3847. #if VERSION_DMG
  3848. if IAPProductsManager.default().isAvailableAllFunction() {
  3849. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  3850. //Convert:
  3851. self?.showAllConvertWindow(convertT: .Word)
  3852. } else {
  3853. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  3854. limitWC.continueBlock = { windowController in
  3855. }
  3856. limitWC.window?.center()
  3857. limitWC.showWindow(nil)
  3858. }
  3859. } else {
  3860. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  3861. }
  3862. #else
  3863. if IAPProductsManager.default().isAvailableAllFunction() {
  3864. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  3865. //Convert:
  3866. } else {
  3867. var vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  3868. vc.showWindow(nil)
  3869. }
  3870. } else {
  3871. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  3872. }
  3873. #endif
  3874. }
  3875. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  3876. var rect = self.view.window?.frame ?? .zero
  3877. rect.size.height -= 20
  3878. guideWC.window?.setFrame(rect, display: false)
  3879. guideWC.window?.minSize = rect.size
  3880. guideWC.window?.maxSize = rect.size
  3881. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  3882. guideWC.show()
  3883. }
  3884. } else {
  3885. }
  3886. }
  3887. func checkFirstTrialController() -> Void {
  3888. #if VERSION_DMG
  3889. //打开文档后引导相关
  3890. if VerificationManager.default().status == .none {
  3891. let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  3892. let lastVersion = UserDefaults.standard.object(forKey: "SKLastTrialVersionMainDocumentLaunchedKey") as? String ?? ""
  3893. if lastVersion.isEmpty || lastVersion != appVersion {
  3894. UserDefaults.standard.setValue(appVersion, forKey: "SKLastTrialVersionMainDocumentLaunchedKey")
  3895. UserDefaults.standard.synchronize()
  3896. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  3897. }
  3898. }
  3899. #endif
  3900. }
  3901. // MARK: - 页面编辑
  3902. open func enterPageEdit(_ pages: [Int] = []) {
  3903. if let doc = self.listView.document {
  3904. if doc.allowsCopying == false || doc.allowsPrinting == false {
  3905. KMBaseWindowController.checkPassword(url: doc.documentURL, type: .owner) { result, pwd in
  3906. if result && pwd.isEmpty == false {
  3907. self.listView.document?.unlock(withPassword: pwd)
  3908. Task { @MainActor in
  3909. self.enterPageEdit(pages)
  3910. }
  3911. } else {
  3912. self.exitPageEdit()
  3913. }
  3914. }
  3915. return
  3916. }
  3917. }
  3918. //选中page
  3919. var tPages = pages
  3920. if tPages.count == 0 {
  3921. if self.leftSideViewController.type.methodType == .Thumbnail {
  3922. tPages = self.leftSideViewController.thumb_fetchSelectedRows() ?? [self.listView.currentPageIndex]
  3923. }
  3924. }
  3925. if (hasEnterPageEdit()) {
  3926. exitPageEdit()
  3927. return
  3928. }
  3929. let controller = KMPDFEditViewController(self.listView.document)
  3930. controller.selectedPages = tPages
  3931. controller.listView = self.listView
  3932. self.addChild(controller)
  3933. controller.view.autoresizingMask = [.width,.height]
  3934. self.listView.isHidden = true
  3935. controller.itemClick = { [weak self] index, params in
  3936. if (index == 1) { /// 双击退出
  3937. self?.enterEditMode(self!.leftSideViewController, [])
  3938. DispatchQueue.main.async {
  3939. let pageIndex: Int = params.first as! Int
  3940. self?.listView.go(toPageIndex: pageIndex, animated: true)
  3941. }
  3942. } else if (index == 2) { // 打印
  3943. self?.showPrintWindow(pageRange: KMPrintPageRange(type: .custom, selectPages: params.first as! [Int]))
  3944. }
  3945. }
  3946. controller.documentEditedCallback = { [weak self] params in
  3947. self?.recordIsPDFDocumentEdited()
  3948. }
  3949. controller.selectionDidChange = { selectedIndexs in
  3950. var indexSet = IndexSet()
  3951. for indexPath in selectedIndexs {
  3952. indexSet.insert(indexPath.item)
  3953. }
  3954. if indexSet.count != 0 {
  3955. }
  3956. }
  3957. }
  3958. open func exitPageEdit() {
  3959. let editController = getPDFEditController()
  3960. if (editController == nil) {
  3961. return
  3962. }
  3963. self.listView.annotationType = .highlight
  3964. // FIXME: - sdk修复插入特定文档crash后,这行代码可以去掉
  3965. self.leftSideViewController.model.insertedDocumentSet.formUnion(editController?.model.insertedDocumentSet ?? [])
  3966. editController?.view.removeFromSuperview()
  3967. editController?.removeFromParent()
  3968. self.listView.isHidden = false
  3969. self.listView.layoutDocumentView()
  3970. self.view.window?.makeFirstResponder(self.listView)
  3971. self.listView.annotationType = .unkown
  3972. self.listView.go(toPageIndex: editController!.listViewCurrentIndex, animated: false)
  3973. if let data = editController?.isEdited, data {
  3974. self.leftSideViewController.reloadThumbnailDataIfNeed()
  3975. self.leftSideViewController.note_reloadDataIfNeed()
  3976. self.leftSideViewController.refreshUIOfOutlineIfNeed()
  3977. self.leftSideViewController.refreshUIOfSeachListIfNeed()
  3978. self.leftSideViewController.refreshUIOfBookmarkIfNeed()
  3979. }
  3980. }
  3981. open func hasEnterPageEdit() -> Bool {
  3982. return self.getPDFEditController() != nil
  3983. }
  3984. // MARK: - 数字签名
  3985. func hasShowDigitalSign() -> Bool {
  3986. return self.digitalSignController?.view.superview != nil
  3987. }
  3988. func canEnterDigitalSign() -> Bool {
  3989. guard let doc = self.listView.document else {
  3990. return false
  3991. }
  3992. return doc.allowsPrinting && doc.allowsCopying
  3993. }
  3994. func enterDigitalSign() {
  3995. self.listView.toolMode = .textToolMode
  3996. if self.hasShowDigitalSign() {
  3997. self.exitDigitalSign()
  3998. } else {
  3999. if self.needSaveDocument() {
  4000. self.saveDocumentWithProgressAlert { [unowned self] params in
  4001. if (self.listView.document != nil) {
  4002. self.showDigitalSignWindow(withFilePathURL: self.listView.document.documentURL)
  4003. }
  4004. }
  4005. return
  4006. }
  4007. if (self.listView.document != nil) {
  4008. self.showDigitalSignWindow(withFilePathURL: self.listView.document.documentURL)
  4009. }
  4010. }
  4011. }
  4012. func exitDigitalSign() {
  4013. self.digitalSignController?.view.removeFromSuperview()
  4014. // KMDocumentDigitalSignToolbarItemIdentifier
  4015. }
  4016. func showDigitalSignWindow(withFilePathURL fileURL: URL) {
  4017. if !IAPProductsManager.default().isAvailableAllFunction(){
  4018. let winC = KMPurchaseCompareWindowController.sharedInstance()
  4019. winC?.kEventName = "Reading_DigitalSign_BuyNow"
  4020. winC?.showWindow(nil)
  4021. return
  4022. }
  4023. if hasShowDigitalSign() {
  4024. self.exitDigitalSign()
  4025. }
  4026. var currentPageIndex = listView.document?.index(for: listView.currentPage()) ?? 0
  4027. var password: String = ""
  4028. password = listView.document?.password ?? ""
  4029. digitalSignController = KMPDFDigitalSignViewController()
  4030. digitalSignController?.currentPageIndex = Int(currentPageIndex)
  4031. digitalSignController?.url = listView.document?.documentURL
  4032. digitalSignController?.password = password
  4033. digitalSignController?.scaleFactor = listView.scaleFactor
  4034. digitalSignController?.titleChangeBlock = { title, index in
  4035. currentPageIndex = UInt(index)
  4036. }
  4037. digitalSignController?.buttonActionBlock = { [weak self] type, isChanged in
  4038. if type == .cancel {
  4039. if let page = self?.listView.document?.page(at: currentPageIndex) {
  4040. self?.listView.go(to: page)
  4041. }
  4042. self?.exitDigitalSign()
  4043. }
  4044. }
  4045. }
  4046. // MARK: - Toolbar
  4047. func toolbarItemClickForExitMode(_ toolbarItem: KMToolbarItemView) {
  4048. if(toolbarItem.itemIdentifier != KMDocumentPageToolbarItemIdentifier) {
  4049. if (hasEnterPageEdit()) {
  4050. self.exitPageEdit()
  4051. }
  4052. }
  4053. if toolbarItem.itemIdentifier != KMDocumentRedactToolbarItemIdentifier {
  4054. if self.hasEnterRedact() {
  4055. self.exitRedact()
  4056. }
  4057. }
  4058. if toolbarItem.itemIdentifier != KMDocumentDigitalSignToolbarItemIdentifier {
  4059. if self.hasShowDigitalSign() {
  4060. self.exitDigitalSign()
  4061. }
  4062. }
  4063. if toolbarItem.itemIdentifier != KMDocumentEditToolbarItemIdentifier && toolbarItem.itemIdentifier != KMRightControlToolbarItemIdentifier && toolbarItem.itemIdentifier != KMLeftControlToolbarItemIdentifier {
  4064. }
  4065. }
  4066. // MARK: - Private Methods
  4067. private func getPDFEditController() -> KMPDFEditViewController? {
  4068. var editController: KMPDFEditViewController?
  4069. for controller in self.children {
  4070. if (controller.isKind(of: KMPDFEditViewController.self)) {
  4071. editController = (controller as! KMPDFEditViewController)
  4072. break
  4073. }
  4074. }
  4075. return editController
  4076. }
  4077. private func _getPDFRedactController() -> KMPDFRedactViewController? {
  4078. var controller: KMPDFRedactViewController?
  4079. for childC in self.children {
  4080. if (childC.isKind(of: KMPDFRedactViewController.self)) {
  4081. controller = (childC as! KMPDFRedactViewController)
  4082. break
  4083. }
  4084. }
  4085. return controller
  4086. }
  4087. private func addBackgroundMaskView() {
  4088. self.removeBackgroundMaskView()
  4089. }
  4090. private func removeBackgroundMaskView() {
  4091. }
  4092. private func _goToFirstPageForFristAppear() {
  4093. DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.1) {
  4094. self.listView.go(toPageIndex: 0, animated: false)
  4095. }
  4096. }
  4097. func isPDFPageCountExceedsLimit(filePath: String) -> Bool {
  4098. let url = URL(fileURLWithPath: filePath)
  4099. guard let document = PDFDocument(url: url) else {
  4100. return false
  4101. }
  4102. let pageCount = document.pageCount
  4103. return pageCount > 30
  4104. }
  4105. // MARK: - Redact 【标记密文】
  4106. func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
  4107. let windowController = KMRedactConfirmWindowController(type)
  4108. self.currentWindowController = windowController
  4109. self.view.window?.beginSheet(windowController.window!)
  4110. windowController.itemClick = { [weak self] index in
  4111. if (index == 2) { /// 取消
  4112. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  4113. self?.currentWindowController = nil
  4114. callback()
  4115. return
  4116. }
  4117. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  4118. self?.currentWindowController = nil
  4119. let panel = NSSavePanel()
  4120. panel.nameFieldStringValue = "[新文件]"+((self?.listView.document?.documentURL.lastPathComponent) ?? "")
  4121. let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  4122. button.state = .on
  4123. panel.accessoryView = button
  4124. panel.isExtensionHidden = true
  4125. panel.beginSheetModal(for: (self?.view.window!)!) { response in
  4126. if response != .OK {
  4127. callback()
  4128. return
  4129. }
  4130. if (type == .redactOne) {
  4131. let anno = self!.listView.activeAnnotation
  4132. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  4133. callback()
  4134. return
  4135. }
  4136. (anno as! CPDFRedactAnnotation).applyRedaction()
  4137. } else if (type == .redactAll) {
  4138. self?.listView.document?.applyRedactions()
  4139. } else if (type == .eraserOne) {
  4140. let anno = self!.listView.activeAnnotation
  4141. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  4142. callback()
  4143. return
  4144. }
  4145. anno?.page.erasureRedact(from: anno!.bounds)
  4146. } else if (type == .eraserAll) {
  4147. KMRedactTools.eraserDocument((self?.listView.document)!) { result, errorAnno in
  4148. if (result == false) {
  4149. callback()
  4150. return
  4151. }
  4152. }
  4153. }
  4154. self!.listView.document?.write(to: panel.url)
  4155. if (button.state == .on) {
  4156. NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
  4157. }
  4158. } else {
  4159. NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
  4160. }
  4161. callback()
  4162. }
  4163. }
  4164. }
  4165. // MARK: - Secure 【安全】
  4166. public func showSecureLimitTip() {
  4167. self.hiddenSecureLimitTip()
  4168. if self.secureAlertView == nil {
  4169. self.secureAlertView = KMSecureAlertView()
  4170. self.secureAlertView?.show(in: self.listView)
  4171. self.secureAlertView?.closeAction = { [unowned self] view in
  4172. self.hiddenSecureLimitTip()
  4173. self.removeFromAlertView()
  4174. self.showFormAlertView()
  4175. }
  4176. self.secureAlertView?.passwordAction = { [unowned self] view in
  4177. self.removeOwnerPassword()
  4178. self.removeFromAlertView()
  4179. self.showFormAlertView()
  4180. }
  4181. }
  4182. }
  4183. func removeOwnerPassword() {
  4184. guard let doc = self.listView.document else {
  4185. NSSound.beep()
  4186. return
  4187. }
  4188. if doc.allowsCopying && doc.allowsPrinting {
  4189. NSSound.beep()
  4190. return
  4191. }
  4192. _ = KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: doc.documentURL) { [weak self] result, password in
  4193. if result == .cancel { /// 关闭
  4194. return
  4195. }
  4196. /// 解密成功
  4197. self?.hiddenSecureLimitTip()
  4198. self?.model.isSaveKeyChain = false
  4199. self?.listView.document?.unlock(withPassword: password)
  4200. }
  4201. }
  4202. public func hiddenSecureLimitTip() {
  4203. self.secureAlertView?.removeFromSuperview()
  4204. self.secureAlertView = nil
  4205. }
  4206. //MARK: - Form
  4207. func showFormAlertView() {
  4208. if (formAlertView == nil) {
  4209. formAlertView = KMFormAlertView()
  4210. formAlertView?.isCloseSecureView = self.secureAlertView != nil ? false : true
  4211. formAlertView?.showInView(self.listView)
  4212. } else {
  4213. self.removeFromAlertView()
  4214. }
  4215. }
  4216. func removeFromAlertView() {
  4217. formAlertView?.removeFromSuperview()
  4218. formAlertView = nil
  4219. }
  4220. override func mouseMoved(with event: NSEvent) {
  4221. self.view.window?.mouseMoved(with: event)
  4222. }
  4223. func savePageNumberIfNeed() {
  4224. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  4225. let scaleFactor = self.listView.scaleFactor ?? 0
  4226. if scaleFactor <= 0 {
  4227. return
  4228. }
  4229. if self.listView.document != nil {
  4230. KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
  4231. KMPreferenceManager.shared.setPageScale(Float(self.listView.scaleFactor), forKey: self.listView.document.documentURL.path)
  4232. }
  4233. }
  4234. }
  4235. // MARK: - 显示合并窗口
  4236. public func showMergeWindow(url: URL? = nil, _ password: String?) {
  4237. DispatchQueue.main.async {
  4238. var documentURL = url
  4239. if documentURL == nil {
  4240. documentURL = self.listView.document?.documentURL
  4241. }
  4242. guard let _url = documentURL else { return }
  4243. guard let document = PDFDocument(url: _url) else { return }
  4244. self.mergeWindowController = KMMergeWindowController(document: document, password: password ?? "")
  4245. self.mergeWindowController!.oriDucumentUrl = self.listView.document?.documentURL
  4246. self.mergeWindowController!.pageIndex = self.listView.currentPageIndex
  4247. self.mergeWindowController!.cancelAction = { [unowned self] controller in
  4248. self.view.window?.endSheet(mergeWindowController!.window!)
  4249. }
  4250. self.mergeWindowController!.mergeAction = { [unowned self] controller, filePath in
  4251. self.view.window?.endSheet(mergeWindowController!.window!)
  4252. }
  4253. self.view.window?.beginSheet(self.mergeWindowController!.window!)
  4254. }
  4255. }
  4256. // MARK: - 显示加密弹窗
  4257. public func showSecureWindow(_ url: URL) {
  4258. self.securityWindowController = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
  4259. guard let securityWindowController = securityWindowController else { return }
  4260. securityWindowController.documentURL = self.listView.document?.documentURL
  4261. securityWindowController.batchAction = { [unowned self] controller, files in
  4262. self.view.window?.endSheet((securityWindowController.window)!)
  4263. let batchWindowController = KMBatchOperateWindowController.sharedWindowController
  4264. let batchOperateFile = KMBatchOperateFile(filePath: self.document?.documentURL.path ?? "", type: .AddPassword)
  4265. batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
  4266. batchWindowController.window?.makeKeyAndOrderFront("")
  4267. }
  4268. securityWindowController.doneAction = { [unowned self] controller, options, attribute in
  4269. let openPanel = NSOpenPanel()
  4270. openPanel.canChooseFiles = false
  4271. openPanel.canChooseDirectories = true
  4272. openPanel.canCreateDirectories = true
  4273. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { (result) in
  4274. if result == NSApplication.ModalResponse.OK {
  4275. for fileURL in openPanel.urls {
  4276. let document = CPDFDocument(url: self.document?.documentURL)
  4277. if document != nil {
  4278. document!.setDocumentAttributes(attribute)
  4279. let path = fileURL.path.stringByAppendingPathComponent(url.deletingPathExtension().lastPathComponent) + "_SetPassword" + "." + url.pathExtension
  4280. let success = document!.write(to: NSURL(fileURLWithPath: path) as URL, withOptions: options)
  4281. if success {
  4282. self.view.window?.endSheet((securityWindowController.window)!)
  4283. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  4284. }
  4285. }
  4286. }
  4287. }
  4288. }
  4289. }
  4290. securityWindowController.cancelAction = { [unowned self] controller in
  4291. self.view.window?.endSheet((securityWindowController.window)!)
  4292. }
  4293. NSWindow.currentWindow().beginSheet(securityWindowController.window!)
  4294. }
  4295. // MARK: - 保存文档
  4296. internal func needSaveDocument() -> Bool {
  4297. if (self.isPDFDocumentEdited) {
  4298. return self.isPDFDocumentEdited
  4299. }
  4300. if (self.needSave) {
  4301. return self.needSave
  4302. }
  4303. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  4304. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  4305. return false
  4306. }
  4307. return true
  4308. }
  4309. internal func saveDocument(overlookDocumentIfEdited overlook: Bool = false) {
  4310. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  4311. if (overlook) {
  4312. document?.save(nil)
  4313. return
  4314. }
  4315. if (self.isPDFDocumentEdited) {
  4316. self.clearIsPDFDocumentEdited()
  4317. self.needSave = false
  4318. document?.save(nil)
  4319. return
  4320. }
  4321. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  4322. return
  4323. }
  4324. document?.save(nil)
  4325. }
  4326. internal func asyncSaveDocument(overlookDocumentIfEdited overlook: Bool = false, callback:@escaping KMCommonBlock) {
  4327. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  4328. if (overlook) {
  4329. DispatchQueue.main.async {
  4330. document?.save(nil)
  4331. callback()
  4332. }
  4333. return
  4334. }
  4335. if (self.isPDFDocumentEdited) {
  4336. self.clearIsPDFDocumentEdited()
  4337. self.needSave = false
  4338. DispatchQueue.main.async {
  4339. document?.save(nil)
  4340. callback()
  4341. }
  4342. return
  4343. }
  4344. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  4345. callback()
  4346. return
  4347. }
  4348. DispatchQueue.main.async {
  4349. document?.save(nil)
  4350. callback()
  4351. }
  4352. }
  4353. internal func saveDocumentWithProgressAlert(callback:@escaping KMCommonBlock) {
  4354. // 显示进度
  4355. AutoSaveManager.manager.isSaving = true
  4356. self.showProgressWindow(message: NSLocalizedString("Save", comment: "") + "PDF")
  4357. self.progressC?.maxValue = 3.0
  4358. self.progressC?.increment(by: 1.0)
  4359. // 保存文档
  4360. self.asyncSaveDocument { [weak self] params in
  4361. // 执行进度 [假进度]
  4362. self?.progressC?.increment(by: 1.0)
  4363. self?.progressC?.increment(by: 1.0)
  4364. DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
  4365. // 隐藏进度
  4366. self?.hiddenProgressWindow()
  4367. DispatchQueue.main.asyncAfter(deadline: .now()+1) {
  4368. AutoSaveManager.manager.isSaving = false
  4369. }
  4370. // 回调
  4371. callback()
  4372. }
  4373. }
  4374. }
  4375. func SaveTempPDFDocumentToURLPath(tempPath: String) {
  4376. self.document?.write(toFile: tempPath)
  4377. }
  4378. // MARK: - 定时保存
  4379. func addAutoSaveEvent() {
  4380. if (self.autoSaveTimer != nil) {
  4381. self.autoSaveTimer?.invalidate()
  4382. self.autoSaveTimer = nil
  4383. }
  4384. if self.document != nil {
  4385. self.autoSaveTimer = Timer.scheduledTimer(withTimeInterval: AutoSaveManager.manager.timeInterval * 60, repeats: true, block: { [weak self] timer in
  4386. self?.autoSaveTimerAction(timer)
  4387. })
  4388. }
  4389. self.checkAutoSaveInfo()
  4390. }
  4391. func checkAutoSaveInfo() {
  4392. guard let cnt = AutoSaveManager.manager.autoSavePaths?.count, cnt > 0 else {
  4393. return
  4394. }
  4395. if AutoSaveManager.manager.autoSaveAlertShow {
  4396. return
  4397. }
  4398. AutoSaveManager.manager.autoSaveDidEndAction = false
  4399. AutoSaveManager.manager.autoSaveAlertShow = true
  4400. let blockSaveWindow = AutoSavePopController()
  4401. blockSaveWindow.cancelHandle = { [weak self] windowController in
  4402. AutoSaveManager.manager.autoSaveDidEndAction = true
  4403. AutoSaveManager.manager.clearCache()
  4404. self?.km_quick_endSheet()
  4405. }
  4406. blockSaveWindow.confirmHandle = { [weak self] windowController in
  4407. self?.km_quick_endSheet()
  4408. self?.saveAutoSaveInfo()
  4409. }
  4410. self.km_beginSheet(windowC: blockSaveWindow)
  4411. }
  4412. func saveAutoSaveInfo() {
  4413. let openPanel = NSOpenPanel()
  4414. openPanel.canChooseDirectories = true
  4415. openPanel.canChooseFiles = false
  4416. openPanel.allowsMultipleSelection = false
  4417. let win = NSApp.keyWindow != nil ? NSApp.keyWindow : self.view.window
  4418. openPanel.beginSheetModal(for: win!) { result in
  4419. if (result == .OK) {
  4420. let folderPath = openPanel.url?.path ?? openPanel.url?.absoluteString
  4421. for path in AutoSaveManager.manager.opendPaths ?? [] {
  4422. let _path = path as? String
  4423. var newPath = "\(folderPath ?? "")/\(_path?.lastPathComponent ?? "")"
  4424. newPath = self.getValidFilePath(newPath)
  4425. do {
  4426. try FileManager.default.moveItem(atPath: _path ?? "", toPath: newPath)
  4427. } catch {
  4428. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: newPath)])
  4429. }
  4430. }
  4431. AutoSaveManager.manager.clearCache()
  4432. }
  4433. AutoSaveManager.manager.autoSaveDidEndAction = true
  4434. }
  4435. }
  4436. func autoSaveTimerAction(_ timer: Timer) {
  4437. if (self.document == nil || self.listView.document?.documentURL.path == nil) {
  4438. return
  4439. }
  4440. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  4441. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  4442. return
  4443. }
  4444. if let data = self.document?.isLocked, data {
  4445. return
  4446. }
  4447. if AutoSaveManager.manager.autoSaveEnabled == false {
  4448. return
  4449. }
  4450. let documentArray = NSDocumentController.shared.documents
  4451. var didFileEdit = false
  4452. for doc in documentArray {
  4453. if doc.fileURL?.path == self.document?.documentURL.path {
  4454. didFileEdit = doc.isDocumentEdited
  4455. break
  4456. }
  4457. }
  4458. if (didFileEdit == false) {
  4459. return
  4460. }
  4461. AutoSaveManager.manager.isSaving = true
  4462. let savePath = AutoSaveManager.manager.autoSaveWithPath(self.listView.document?.documentURL.path ?? "")
  4463. if (!self.document!.isLocked) {
  4464. self.document?.write(to: URL(fileURLWithPath: savePath))
  4465. }
  4466. DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
  4467. AutoSaveManager.manager.isSaving = false
  4468. }
  4469. }
  4470. func removeAutoSaveInfo() {
  4471. if self.autoSaveTimer != nil {
  4472. self.autoSaveTimer?.invalidate()
  4473. self.autoSaveTimer = nil
  4474. }
  4475. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  4476. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  4477. return
  4478. }
  4479. if AutoSaveManager.manager.autoSaveEnabled == false {
  4480. return
  4481. }
  4482. if self.document == nil || self.listView.document?.documentURL.path == nil {
  4483. return
  4484. }
  4485. AutoSaveManager.manager.removeAutoSavePath(self.listView.document?.documentURL.path ?? "")
  4486. }
  4487. // MARK: - 选择 PDFDisplay 模式
  4488. public func setPDFDisplay(pdfViewMode: CPDFDisplayViewMode) {
  4489. listView.setDisplay(pdfViewMode)
  4490. }
  4491. // MARK: - 选择缩放模式
  4492. @objc public func selectZoom(_ type: KMPDFZoomType) {
  4493. switch type {
  4494. case .width:
  4495. self.listView.autoScales = true
  4496. break
  4497. case .fit:
  4498. if let pageHeight = self.listView.currentPage()?.size.height, pageHeight > 0 {
  4499. let pdfviewHeight = self.listView.bounds.size.height
  4500. self.listView.scaleFactor = pdfviewHeight/pageHeight
  4501. self.listView.autoScales = false
  4502. }
  4503. break
  4504. case .actualSize:
  4505. if self.listView.scaleFactor != 1.0 {
  4506. self.listView.scaleFactor = 1.0
  4507. self.listView.autoScales = false
  4508. }
  4509. break
  4510. }
  4511. }
  4512. internal func createPdf(index:Int) {
  4513. }
  4514. // MARK - Event 监听
  4515. private func addEventMonitor() {
  4516. if (self.eventMonitor != nil) {
  4517. self.removeEventMonitor()
  4518. }
  4519. self.eventMonitor = NSEvent.addLocalMonitorForEvents(matching: [.scrollWheel, .leftMouseDown, .leftMouseUp]) { [weak self] event in
  4520. if (event.type == .scrollWheel && event.modifierFlags.contains(.option)) { // Alt + 鼠标滚轮
  4521. self?.listView.magnifyWheel(event)
  4522. return nil
  4523. } else if event.type == .leftMouseDown {
  4524. let point = event.locationInView(self?.listView ?? NSView())
  4525. let presentationDrawView = self?.listView.presentationDrawView
  4526. if let data = self?.interactionMode, data == .presentation,CGRectContainsPoint(self?.listView.frame ?? .zero, point),presentationDrawView?.isHidden == true { // 幻灯片模式下
  4527. if point.x >= (self?.listView.frame.maxX ?? 0) / 2 {
  4528. let can = self?.listView.canGoToNextPage() ?? false
  4529. if can {
  4530. self?.listView.goToNextPage(nil)
  4531. }
  4532. } else {
  4533. let can = self?.listView.canGoToPreviousPage() ?? false
  4534. if can {
  4535. self?.listView.goToPreviousPage(nil)
  4536. }
  4537. }
  4538. return nil
  4539. }
  4540. }
  4541. return event
  4542. }
  4543. }
  4544. func addKeyEventMonitor() {
  4545. if (self.keyEventMonitor != nil) {
  4546. self.removeKeyEventMonitor()
  4547. }
  4548. keyEventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
  4549. if event.keyCode == 53 {
  4550. if let data = self?.interactionMode, data == .presentation { // 幻灯片模式下
  4551. self?.exitFullScreen()
  4552. return event
  4553. }
  4554. if self?.listView.toolMode == .editPDFToolMode {
  4555. if self != nil {
  4556. //使用editingSelectionString获取内容文字
  4557. if self!.listView.editingAreas() != nil {
  4558. if self!.listView.editingAreas().count > 0 && self!.listView.isEditable() {
  4559. self!.listView.clearEditingSelectCharItem()
  4560. } else if self!.listView.editingAreas().count > 0 {
  4561. if self?.listView.shouAddEditAreaType() == .image ||
  4562. self?.listView.shouAddEditAreaType() == .text {
  4563. }
  4564. self?.listView.endEditIsRemoveBlock(with: self!.listView.editingAreas().first as? CPDFEditArea)
  4565. self?.listView.updateEditing([])
  4566. self?.listView.isEditImage = false
  4567. self?.listView.setNeedsDisplayPageViewFor(self!.listView.currentPage())
  4568. if self?.listView.shouAddEditAreaType() == .image {
  4569. self?.listView.change([.text, .image])
  4570. }
  4571. self?.toggleCloseRightSide()
  4572. } else if(self?.listView.shouAddEditAreaType() == .image || self!.listView.shouAddEditAreaType() == .text) {
  4573. if self?.listView.shouAddEditAreaType() == .image ||
  4574. self?.listView.shouAddEditAreaType() == .text {
  4575. }
  4576. self?.listView.setShouAddEdit([])
  4577. self?.listView.change([.text, .image])
  4578. self?.toggleCloseRightSide()
  4579. }
  4580. } else {
  4581. if self?.listView.shouAddEditAreaType() == .image ||
  4582. self?.listView.shouAddEditAreaType() == .text {
  4583. }
  4584. }
  4585. }
  4586. }
  4587. } else {
  4588. if let data = self?.interactionMode, data == .presentation { // 幻灯片模式下
  4589. self?.listView.keyDown(with: event)
  4590. return event
  4591. } else {
  4592. let cmd = event.modifierFlags.contains(.command)
  4593. let shift = event.modifierFlags.contains(.shift)
  4594. if event.keyCode == 6 { // z
  4595. let editPDFIng = self?.listView.isEditing() ?? false
  4596. if cmd && shift { // 恢复
  4597. let can = self?.listView.canEditTextRedo() ?? false
  4598. if can == false {
  4599. return event
  4600. }
  4601. if editPDFIng {
  4602. _ = CustomAlertView.alertView(message: NSLocalizedString("Redo", comment: ""), fromView: self!.view, withStyle: .black)
  4603. }
  4604. } else if cmd { // 撤回
  4605. let can = self?.listView.canEditTextUndo() ?? false
  4606. if can == false {
  4607. return event
  4608. }
  4609. if editPDFIng {
  4610. _ = CustomAlertView.alertView(message: NSLocalizedString("Undo", comment: ""), fromView: self!.view, withStyle: .black)
  4611. }
  4612. }
  4613. }
  4614. }
  4615. }
  4616. return event
  4617. }
  4618. }
  4619. func removeKeyEventMonitor() {
  4620. if (self.keyEventMonitor != nil) {
  4621. KMPrint("removeKeyEventMonitor 已移除事件监听")
  4622. NSEvent.removeMonitor(self.keyEventMonitor as Any)
  4623. self.keyEventMonitor = nil
  4624. }
  4625. }
  4626. private func removeEventMonitor() {
  4627. if (self.eventMonitor != nil) {
  4628. KMPrint("已移除事件监听")
  4629. NSEvent.removeMonitor(self.eventMonitor as Any)
  4630. self.eventMonitor = nil
  4631. }
  4632. }
  4633. // MARK: - Tools
  4634. func pdfViewCanHorizontalScroll() -> Bool {
  4635. let scroll = self.listView.scroll()
  4636. if (scroll == nil) {
  4637. return false
  4638. }
  4639. return scroll?.horizontalScroller?.isHidden == nil ? false : !(scroll!.horizontalScroller!.isHidden)
  4640. }
  4641. func pdfViewCanVerticalScroll() -> Bool {
  4642. let scroll = self.listView.scroll()
  4643. if (scroll == nil) {
  4644. return false
  4645. }
  4646. return scroll?.verticalScroller?.isHidden == nil ? false : !(scroll!.verticalScroller!.isHidden)
  4647. }
  4648. // MARK: - Public Methods
  4649. // 清理数据 [eg. 通知]
  4650. public func clearData() {
  4651. KMThumbnailCache.shared.clearCache()
  4652. self.removeNotifications()
  4653. if (self.listView.spellingTag() > 0) {
  4654. NSSpellChecker.shared.closeSpellDocument(withTag: self.listView.spellingTag())
  4655. }
  4656. self.removeAutoSaveInfo()
  4657. self.myDocument = nil
  4658. }
  4659. public func clearSecureOptions() {
  4660. self._secureOptions = nil
  4661. self.documentAttribute = nil
  4662. }
  4663. public func recordRemoveSecureFlag() {
  4664. self._removeSecureFlag = true
  4665. self.clearSecureOptions()
  4666. self.recordIsPDFDocumentEdited(type: .removePassword)
  4667. self._needSave = true
  4668. }
  4669. public func clearRemoveSecureFlag() {
  4670. self._removeSecureFlag = false
  4671. }
  4672. public func clearSaveWatermarkFlag() {
  4673. km_synchronized(self) {
  4674. self._saveWatermarkFlag = false
  4675. }
  4676. }
  4677. public func recordIsPDFDocumentEdited(type: KMSubscribeWaterMarkType = .none) {
  4678. km_synchronized(self) {
  4679. self.model.isPDFDocumentEdited = true
  4680. if type == .editText || type == .editImage {
  4681. self.leftSideViewController.updateThumbnail(at: self.listView.currentPageIndex)
  4682. }
  4683. if let _document = self.myDocument {
  4684. KMTools.setDocumentEditedState(document: _document)
  4685. }
  4686. }
  4687. }
  4688. public func clearIsPDFDocumentEdited() {
  4689. km_synchronized(self) {
  4690. self.model.isPDFDocumentEdited = false
  4691. }
  4692. }
  4693. func showSnapshots(setups: NSArray?) {
  4694. if self.listView.document != nil {
  4695. for setup in setups ?? [] {
  4696. let swc = KMSnapshotWindowController()
  4697. swc.delegate = self
  4698. swc.setPdfDocument(self.listView.document, setup: setup as? NSDictionary)
  4699. swc.setForceOnTop(self.interactionMode != .normal)
  4700. self.myDocument?.addWindowController(swc)
  4701. }
  4702. }
  4703. }
  4704. func dealDocumentDidLoaded() {
  4705. self.removeBackgroundMaskView()
  4706. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  4707. self.showSecureLimitTip()
  4708. }
  4709. if self.model.needConvertNotes {
  4710. self.showConvertNotesProgress()
  4711. }
  4712. if (self._documentFirstLoad) {
  4713. self.checkShouldAutoOpenLeftVC()
  4714. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  4715. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document?.documentURL.path ?? "")
  4716. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document?.documentURL.path ?? "")
  4717. if (pageScale != nil) {
  4718. self.listView.scaleFactor = CGFloat(pageScale!)
  4719. }
  4720. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < (self.listView.document?.pageCount ?? 0)) {
  4721. self.listView.go(toPageIndex: pageNumber!, animated: false)
  4722. } else {
  4723. self._goToFirstPageForFristAppear()
  4724. }
  4725. } else {
  4726. self._goToFirstPageForFristAppear()
  4727. }
  4728. self._documentFirstLoad = false
  4729. }
  4730. }
  4731. func tabViewIsDragging() -> Bool {
  4732. let level = self.view.window?.level ?? .normal
  4733. return level == .floating
  4734. }
  4735. // MARK: - Noti Actions
  4736. internal func documentDidUnlockNotification(_ sender: Notification) {
  4737. if (self.listView.document != nil && self.listView.document.isEqual(to: sender.object)) {
  4738. if (self.myDocument == nil) {
  4739. return
  4740. }
  4741. if (self.listView.document.allowsPrinting && self.listView.document.allowsCopying) {
  4742. self.hiddenSecureLimitTip()
  4743. }
  4744. let isUnlockFromKeychain = (self.myDocument as? KMMainDocument)?.isUnlockFromKeychain ?? false
  4745. if (isUnlockFromKeychain || self.model.isSaveKeyChain == false) {
  4746. return
  4747. }
  4748. let type = KMPreferenceManager.shared.savePasswordType
  4749. if (type == .never) {
  4750. return
  4751. }
  4752. if (type == .always) {
  4753. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  4754. return
  4755. }
  4756. // 保存到钥匙串
  4757. let alert = NSAlert()
  4758. alert.messageText = NSLocalizedString("Remember Password?", comment: "")
  4759. alert.informativeText = NSLocalizedString("Do you want to save this password in your Keychain?", comment: "")
  4760. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  4761. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  4762. if (alert.runModal() == .alertFirstButtonReturn) { // 保存密码
  4763. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  4764. return
  4765. }
  4766. }
  4767. }
  4768. func annotationsAttributeHasChange(_ sender: Notification) {
  4769. guard let dict = sender.object as? [String : Any] else {
  4770. return
  4771. }
  4772. if let anno = dict["object"] as? CPDFAnnotation {
  4773. let value = dict["keyPath"] as? String ?? ""
  4774. let didEnd = dict["didEnd"] as? Bool ?? false
  4775. if didEnd {
  4776. if value == CPDFAnnotationBoundsKey {
  4777. if anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation {
  4778. anno.contents = anno.page?.string(for: anno.bounds) ?? ""
  4779. }
  4780. }
  4781. if anno.km_isMeasure() && anno.contents == nil {
  4782. anno.contents = anno.string() ?? ""
  4783. }
  4784. self.leftSideViewController.refreshUIForAnnoAttributeDidChange(anno, attributes: ["keyPath" : value])
  4785. } else {
  4786. if value != CPDFAnnotationBoundsKey && value != CPDFAnnotationStartPointKey && value != CPDFAnnotationEndPointKey && value != CPDFAnnotationPathsKey { // 改变bounds(箭头、直线注释 开始点和结束点, 手绘注释的paths)的操作会卡顿,比如移动
  4787. self.leftSideViewController.refreshUIForAnnoAttributeDidChange(anno, attributes: ["keyPath" : value])
  4788. }
  4789. }
  4790. }
  4791. }
  4792. internal func applicationWillTerminateNotification(_ sender: Notification) {
  4793. self.savePageNumberIfNeed()
  4794. self.saveDocument()
  4795. }
  4796. func CEditPDFToolModeChangeStateUnkownNotification(_ sender: Notification) {
  4797. var editSelectd = false
  4798. if (self.listView.shouAddEditAreaType() == .text || self.listView.shouAddEditAreaType() == .image) && self.listView.toolMode == .editPDFToolMode {
  4799. editSelectd = true
  4800. }
  4801. if self.listView.toolMode == .editPDFToolMode {
  4802. if editSelectd {
  4803. }
  4804. }
  4805. }
  4806. @objc func handlePageChangedNotification(_ sender: Notification) {
  4807. if self.mwcFlags.isSwitchingFullScreen > 0 {
  4808. return
  4809. }
  4810. let page = self.listView.currentPage()
  4811. let pageIndex = page?.pageIndex() ?? 0
  4812. self.leftSideViewController.thumb_selectRowIndexsIfNeed(IndexSet(integer: IndexSet.Element(pageIndex)))
  4813. self.leftSideViewController.thumbnailTableView.needsDisplay = true
  4814. self.leftSideViewController.tocOutlineView.needsDisplay = true
  4815. }
  4816. @objc func handleDisplayBoxChangedNotification(_ sender: Notification) {
  4817. self.leftSideViewController.reloadThumbnailDataIfNeed()
  4818. }
  4819. }