KMMainViewController.swift 374 KB

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