KMMainViewController.swift 342 KB

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