KMMainViewController.swift 193 KB

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