KMMainViewController.swift 357 KB

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