KMMainViewController.swift 376 KB

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