KMMainViewController.swift 225 KB

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