KMMainViewController.swift 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223
  1. //
  2. // KMMainViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Cocoa
  8. let DISPLAYSPAGEBREAKS_KEY = "displaysPageBreaks"
  9. let DISPLAYSASBOOK_KEY = "displaysAsBook"
  10. let DISPLAYMODE_KEY = "displayMode"
  11. let DISPLAYBOX_KEY = "displayBox"
  12. let HASHORIZONTALSCROLLER_KEY = "hasHorizontalScroller"
  13. let HASVERTICALSCROLLER_KEY = "hasVerticalScroller"
  14. let AUTOHIDESSCROLLERS_KEY = "autoHidesScrollers"
  15. let SCALEFACTOR_KEY = "scaleFactor"
  16. let AUTOSCALES_KEY = "autoScales"
  17. let PAGEINDEX_KEY = "pageIndex"
  18. let SCROLLPOINT_KEY = "scrollPoint"
  19. let LOCKED_KEY = "locked"
  20. @objcMembers class KMMainViewController: NSViewController,CPDFViewDelegate,CPDFListViewDelegate,NSTextFieldDelegate {
  21. @IBOutlet var PDFContendView: NSView!
  22. @IBOutlet var centerContentView: NSView!
  23. @IBOutlet var listView: CPDFListView!
  24. @IBOutlet var secondaryPdfView: KMSecondaryPDFView?
  25. @IBOutlet weak var readContentView: NSView!
  26. @IBOutlet weak var tipCurrentPageBox: KMBox!
  27. @IBOutlet weak var rightView: NSView!
  28. @IBOutlet weak var leftView: NSView!
  29. @IBOutlet weak var mianSplitView: KMSplitView!
  30. @IBOutlet weak var pdfSplitView: KMSplitView!
  31. @IBOutlet weak var newPDFSplitView: KMSplitView!
  32. @IBOutlet weak var pdfContentView: NSView!
  33. @IBOutlet weak var pdfSplitSecondView: NSBox!
  34. @IBOutlet weak var locationPageView: NSView!
  35. @IBOutlet weak var tipLabel: NSTextField!
  36. @IBOutlet weak var toplayoutConstraint: NSLayoutConstraint!
  37. @IBOutlet var childToolbarController: KMToolbarViewController!
  38. @IBOutlet var toolbarController: KMToolbarController!
  39. @IBOutlet weak var toolbarBox: NSBox!
  40. @IBOutlet weak var heightOffset: NSLayoutConstraint!
  41. //阅读模式界面
  42. @IBOutlet weak var readModelView: KMReadModelView!
  43. @IBOutlet weak var bottomAreaView: KMBox!
  44. @IBOutlet weak var readModelViewWidthConstraint: NSLayoutConstraint!
  45. var isReadMode: Bool = false
  46. var readLeftMethodType: BotaType = .None
  47. var readLeftPanelOpen = false
  48. var readLastLeftPanWidth = 0.0
  49. var readLeftViewShowPanel = false
  50. var readRightPanelOpen = false
  51. var readToolbarType: KMToolbarViewType = .None
  52. var readToolbarItemIdentifier: String = ""
  53. var readToolMode: CToolMode = .textToolMode
  54. var readAnnotationType: CAnnotationType = .unkown
  55. var readSubViewType: RightSubViewType = .None
  56. //页码显示器
  57. @IBOutlet weak var pageNumberDisplayView: KMPageNumberDisplayView!
  58. @IBOutlet weak var tipCurrentPageBoxWidthConstraint: NSLayoutConstraint!
  59. //自动滚动
  60. var autoFlowOptionsSheetController: KMAutoFlowOptionsSheetController?
  61. //AI相关
  62. var aiTipView: AITipIconView!
  63. var aiTypeChooseView: AITypeChooseView!
  64. //Search
  65. var searchIndex: Int = 0
  66. //Form
  67. var formAlertView: KMFormAlertView?
  68. //对比
  69. var isCompareModel: Bool = false {
  70. didSet {
  71. self.toolbarController.updataItemVisible()
  72. }
  73. }
  74. //密码弹窗
  75. var passwordWindow: KMPasswordInputWindow?
  76. private var _needSave = false
  77. var needSave: Bool {
  78. set {
  79. _needSave = newValue
  80. if (_needSave == false) {
  81. self.clearIsPDFDocumentEdited()
  82. self.hiddenSecureSuccessTip()
  83. }
  84. }
  85. get {
  86. return _needSave
  87. }
  88. }
  89. // 标识 pdf 已编辑
  90. fileprivate var _isPDFDocumentEdited = false
  91. // 标识 pdf文字图片已编辑
  92. internal var isPDFTextImageEdited = false
  93. var isPDFDocumentEdited: Bool {
  94. get {
  95. return _isPDFDocumentEdited
  96. }
  97. }
  98. var password: String?
  99. var leftSideViewController: KMLeftSideViewController = KMLeftSideViewController.init(type: KMLeftMethodMode())
  100. var rightSideViewController: KMRightSideViewController!
  101. var searchResults: [KMSearchMode] = []
  102. var mwcFlags: MwcFlags = MwcFlags()
  103. var isShowQuickTour: Bool = false
  104. var document: CPDFDocument?
  105. var myDocument: NSDocument?
  106. weak var browserWindowController: KMBrowserWindowController?
  107. var cropSettingWindowController: KMCropSettingWindowController!
  108. var currentWindowController: NSWindowController!
  109. // var interactionMode: SKInteractionMode = .SKNormalMode
  110. var savedNormalSetup: NSMutableDictionary = NSMutableDictionary()
  111. //数字签名
  112. var digitalSignController: KMPDFDigitalSignViewController?
  113. let CPDFOfficeLeftSidePaneWidthKey = "CPDFOfficeLeftSidePaneWidthKey"
  114. let CPDFOfficeRightSidePaneWidthKey = "CPDFOfficeRightSidePaneWidthKey"
  115. @IBOutlet weak var topTipBox: NSBox!
  116. @IBOutlet weak var exitFullButton: NSButton!
  117. var hasAddRedact: Bool = false
  118. var functionWidth: Double {
  119. get {
  120. if self.isReadMode {
  121. if !self.isShowBOTA {
  122. return 0
  123. }
  124. }
  125. return 48-4
  126. }
  127. }
  128. var isShowBOTA: Bool = false
  129. let panelWidth = 212.0
  130. let defaultRightWidth = 260.0
  131. var lastLeftPanWidth = 0.0
  132. var lastRightPanWidth = 0.0
  133. var leftPanelOpen: Bool = false
  134. var rightPanelIsOpen = false
  135. var pageNumber: UInt?
  136. var openSecondaryPdfView: KMSecondaryViewController?
  137. var secondaryPdfContentView: NSView?
  138. var lastSplitPDFHeight: Float = 0.0
  139. internal var isSaveKeyChain = true
  140. var rightMouseEventing = false
  141. var pdfEditController: KMPDFEditViewController? {
  142. get {
  143. return self.getPDFEditController()
  144. }
  145. }
  146. var autoSaveTimer: Timer?
  147. var progressController: SKProgressController?
  148. private var _documentFirstLoad: Bool = true
  149. var eventMonitor: Any?
  150. var keyEventMonitor: Any?
  151. var mouseRightMenuEvent: NSEvent?
  152. var aiTranslationWindow: KMAITranslationWindowController?
  153. var aiTranslationConfirWC: KMAITranslationConfirmWindowController?
  154. lazy private var homeVC: KMHomeViewController? = {
  155. let vc = KMHomeViewController()
  156. return vc
  157. }()
  158. private var background_mask: NSView?
  159. fileprivate var _secureOptions: [CPDFDocumentWriteOption : Any]?
  160. var secureOptions: [CPDFDocumentWriteOption : Any]? {
  161. get {
  162. return self._secureOptions
  163. }
  164. }
  165. var documentAttribute: [CPDFDocumentAttribute : Any]?
  166. fileprivate var _removeSecureFlag = false
  167. var removeSecureFlag: Bool {
  168. get {
  169. return self._removeSecureFlag
  170. }
  171. }
  172. fileprivate var _saveWatermarkFlag = false
  173. var saveWatermarkFlag: Bool {
  174. get {
  175. return self._saveWatermarkFlag
  176. }
  177. }
  178. var beforeMarkedPageIndex = NSNotFound
  179. var markedPageIndex = NSNotFound
  180. var beforeMarkedPagePoint = NSPoint.zero
  181. var markedPagePoint = NSPoint.zero
  182. var repeatTrialLoad: Bool = false
  183. var _mainWindow: NSWindow?
  184. var mainWindow: NSWindow? {
  185. get {
  186. return self._mainWindow
  187. }
  188. set {
  189. self._mainWindow = newValue
  190. }
  191. }
  192. deinit {
  193. NotificationCenter.default.removeObserver(self)
  194. self.stopAutoSaveTimer()
  195. self.removeEventMonitor()
  196. self.removeKeyEventMonitor()
  197. }
  198. override func awakeFromNib() {
  199. super.awakeFromNib()
  200. self.addBackgroundMaskView()
  201. // pdfSplitView.frame = NSMakeRect(0, 0, NSWidth(centerContentView.bounds), NSHeight(centerContentView.bounds)-1)
  202. // centerContentView.addSubview(pdfSplitView)
  203. self.PDFContendView.backgroundColor(NSColor.km_init(hex: "FFFFFF"))
  204. listView.delegate = self
  205. listView.pdfListViewDelegate = self
  206. // listView.editingConfig().isSupportMultipleSelectEditingArea = true
  207. if (document != nil) {
  208. // if (self.document!.isLocked) {
  209. //
  210. // } else {
  211. listView.document = document
  212. // }
  213. listView.document.delegate = self
  214. let autoScale = listView.autoScales
  215. if !autoScale {
  216. listView.scaleFactor = 1.0
  217. }
  218. }
  219. self.initPDFLeftViewVC()
  220. self.initRightSideView()
  221. self.toolbarController.listView = self.listView
  222. self.toolbarController.mainViewController = self
  223. self.newPDFSplitView.delegate = self
  224. }
  225. override func viewDidAppear() {
  226. super.viewDidAppear()
  227. //刷新前一页后一页按钮
  228. self.updateNextAndPreViousButtonState()
  229. KMLightMemberManager.manager.canShowAdvancedView = false
  230. // Task { @MainActor in
  231. // await KMLightMemberManager.manager.canUseAdvanced(needNetworking: true)
  232. // }
  233. // self.addEventMonitor()
  234. self.view.window?.makeFirstResponder(self.listView)
  235. // 更新属性页面的信息
  236. NotificationCenter.default.post(name: KMInfoWindowC.windowDidBecomeMainNotification, object: self.myDocument)
  237. if (self.document!.isLocked == false) {
  238. self.loadFunctionGuide()
  239. self.loadAIIconView()
  240. }
  241. if (self.document == nil) {
  242. return
  243. }
  244. if (self.document!.isLocked == false) {
  245. return
  246. }
  247. if (self.view.window == nil) {
  248. return
  249. }
  250. if (self.password != nil) {
  251. self.isSaveKeyChain = false
  252. self.listView.document.unlock(withPassword: self.password)
  253. return
  254. }
  255. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  256. if self.passwordWindow == nil {
  257. self.passwordWindow = KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), url: self.document!.documentURL) { [unowned self] result , password in
  258. self.passwordWindow = nil
  259. if (result == .cancel) {
  260. self.browserWindowController?.browser.closeTab()
  261. return
  262. }
  263. self.isSaveKeyChain = true
  264. self.listView.document = self.document
  265. self.document?.unlock(withPassword: password)
  266. }
  267. } else {
  268. self.passwordWindow = nil
  269. }
  270. }
  271. }
  272. override func viewWillAppear() {
  273. super.viewWillAppear()
  274. self.reStartAutoSaveTimer()
  275. //是否弹出登录窗口
  276. // self.needShowRegisterView()
  277. }
  278. override func viewWillDisappear() {
  279. super.viewWillDisappear()
  280. // KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
  281. self.pauseAutoSaveTimer()
  282. self.removeEventMonitor()
  283. }
  284. override func viewWillLayout() {
  285. super.viewWillLayout()
  286. if (KMTools.isFullScreen(self.view.window ?? NSWindow())) { // 全屏
  287. self.exitFullButton.isHidden = false
  288. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundFullScreenColor
  289. } else {
  290. self.exitFullButton.isHidden = true
  291. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor
  292. }
  293. }
  294. override func viewDidLoad() {
  295. super.viewDidLoad()
  296. leftSideViewController.mainVC = self
  297. mwcFlags.settingUpWindow = 1
  298. toolbarController.delegate = self
  299. //TODO: 先让项目运行,看后面怎么调整这段逻辑,目前最外层是 KMBrowserWindowController
  300. toolbarBox.contentView = toolbarController.view
  301. // self.childToolbarController.updateType(newType: .Annatiton)
  302. // self.showChildToolbar(showToolbar: true)
  303. // self.toolbarController.editPDFButtonAction(item: NSMenuItem())
  304. if (UserDefaults.standard.object(forKey: CPDFOfficeLeftSidePaneWidthKey) != nil) {
  305. UserDefaults.standard.set(256, forKey: CPDFOfficeLeftSidePaneWidthKey)
  306. UserDefaults.standard.synchronize()
  307. }
  308. if (UserDefaults.standard.object(forKey: CPDFOfficeRightSidePaneWidthKey) != nil) {
  309. UserDefaults.standard.set(256, forKey: CPDFOfficeRightSidePaneWidthKey)
  310. UserDefaults.standard.synchronize()
  311. }
  312. let position = mianSplitView.maxPossiblePositionOfDivider(at: 1)
  313. mianSplitView.setPosition(position, ofDividerAt: 0)
  314. mianSplitView.setPosition(mianSplitView.minPossiblePositionOfDivider(at: 0), ofDividerAt: 0)
  315. pdfSplitView.setPosition(mianSplitView.maxPossiblePositionOfDivider(at: 1), ofDividerAt: 0)
  316. self.locationPageView.wantsLayer = true;
  317. self.locationPageView.layer?.backgroundColor = NSColor(red: 189.0/255.0, green: 223.0/255.0, blue: 253.0/255.0, alpha: 1).cgColor
  318. self.tipLabel.stringValue = NSLocalizedString("Please use the scroll bar, thumbnail tool to locate the target page, click or box the area to select the target range.", comment: "")
  319. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  320. if (self.listView.document != nil) {
  321. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document.documentURL.path)
  322. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document.documentURL.path)
  323. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < self.listView.document.pageCount) {
  324. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  325. if (pageScale != nil) {
  326. self.listView.scaleFactor = CGFloat(pageScale!)
  327. }
  328. self.listView.go(toPageIndex: pageNumber!, animated: false)
  329. }
  330. } else {
  331. self._goToFirstPageForFristAppear()
  332. }
  333. }
  334. } else {
  335. self._goToFirstPageForFristAppear()
  336. }
  337. //阅读页面
  338. readModelView.delegate = self
  339. pageNumberDisplayView.delegate = self
  340. tipCurrentPageBox.moveCallback = { [unowned self] mouseEntered, mouseBox in
  341. if mouseEntered {
  342. self.pageNumberDisplayView.hover = true
  343. // self.updatePageIndicatoreType()
  344. } else {
  345. self.pageNumberDisplayView.hover = false
  346. // self.updatePageIndicatoreType()
  347. }
  348. }
  349. NotificationCenter.default.addObserver(self, selector: #selector(rename(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerRename"), object: nil)
  350. NotificationCenter.default.addObserver(self, selector: #selector(closeTab(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseTabs"), object: nil)
  351. NotificationCenter.default.addObserver(self, selector: #selector(showInFinder(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerShowInFinder"), object: nil)
  352. NotificationCenter.default.addObserver(self, selector: #selector(preferenceDidChangeNotification), name: KMPreferenceManager.didChangeNotification, object: nil)
  353. NotificationCenter.default.addObserver(self, selector: #selector(documentDidUnlockNotification), name: Notification.Name("CPDFDocumentDidUnlockNotification"), object: nil)
  354. NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminateNotification), name: NSApplication.willTerminateNotification, object: nil)
  355. NotificationCenter.default.addObserver(self, selector: #selector(KMPDFViewCurrentPageDidChangedNotification), name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: nil)
  356. NotificationCenter.default.addObserver(self, selector: #selector(CPDFDocumentPageCountChangedNotification), name: NSNotification.Name.init(rawValue: "CPDFDocumentPageCountChangedNotification"), object: nil)
  357. NotificationCenter.default.addObserver(self, selector: #selector(CEditPDFToolModeChangeStateUnkownNotification), name: Notification.Name.init("CEditPDFToolModeChangeStateUnkown"), object: nil)
  358. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.listView)
  359. NotificationCenter.default.addObserver(self, selector: #selector(handleDisplayBoxChangedNotification), name: NSNotification.Name.CPDFViewDisplayBoxChanged, object: self.listView)
  360. // 互动模式
  361. NotificationCenter.default.addObserver(self, selector: #selector(willEnterInteractionModeNotification), name: NSWindow.willEnterInteractionModeNotification, object: nil)
  362. NotificationCenter.default.addObserver(self, selector: #selector(didEnterInteractionModeNotification), name: NSWindow.didEnterInteractionModeNotification, object: nil)
  363. NotificationCenter.default.addObserver(self, selector: #selector(willShowFullScreenNotification), name: NSWindow.willShowFullScreenNotification, object: nil)
  364. NotificationCenter.default.addObserver(self, selector: #selector(didShowFullScreenNotification), name: NSWindow.didShowFullScreenNotification, object: nil)
  365. NotificationCenter.default.addObserver(self, selector: #selector(didAddContentViewNotification), name: NSWindow.didAddContentViewNotification, object: nil)
  366. self.autoSaveTimeStartOrStopIfNeed()
  367. self.toolbarController.selectItem(KMDocumentAnnotationToolbarItemIdentifier)
  368. self.closeRightPane()
  369. self.addKeyEventMonitor()
  370. self.addAdsBannerView()
  371. //检测OCR包是否需要更新
  372. #if VERSION_DMG
  373. KMResourceDownloadManager.manager.checkDocumentAIVersion()
  374. #endif
  375. }
  376. //MARK: - KMToolbarViewControllerDelegate
  377. //MARK: - PDFListView
  378. func initPDFLeftViewVC() {
  379. var frame = self.leftView.frame
  380. frame.size.width += 44
  381. self.leftView.frame = frame
  382. leftSideViewController.listView = self.listView ?? CPDFListView()
  383. leftSideViewController.view.frame = CGRect(x: 0, y:0 , width: self.leftView.frame.size.width, height: self.leftView.frame.size.height)
  384. leftSideViewController.view.autoresizingMask = [.height,.width]
  385. leftSideViewController.delegate = self
  386. self.leftView.addSubview(leftSideViewController.view)
  387. }
  388. func initRightSideView() {
  389. self.rightSideViewController = KMRightSideViewController.init()
  390. self.rightSideViewController.view.frame = CGRect(x: 0, y: 0, width: self.rightView.frame.width, height: self.rightView.frame.size.height)
  391. self.rightSideViewController.view.autoresizingMask = [.height,.width]
  392. self.rightSideViewController.listView = self.listView
  393. // self.rightSideViewController.view.isHidden = true
  394. self.rightSideViewController.isHidden = true
  395. self.rightSideViewController.delegate = self
  396. self.rightView.addSubview(self.rightSideViewController.view)
  397. self.rightSideViewController.propertyDidChange = {
  398. [weak self] (model: AnyObject?) in
  399. let topBarView = self?.toolbarController.fetchTopBarView()
  400. if (topBarView == nil || ((topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self)) == false)) {
  401. return
  402. }
  403. /// Bates码、页眉页脚、背景、水印
  404. if (model == nil) {
  405. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: false)
  406. } else {
  407. if ((model?.isKind(of: KMBatesModel.self))!) {
  408. if ((model as! KMBatesModel).hasVaild) {
  409. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: true)
  410. } else {
  411. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: false)
  412. }
  413. } else if ((model?.isKind(of: KMHeaderFooterModel.self))!) {
  414. if ((model as! KMHeaderFooterModel).hasVaild) {
  415. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: true)
  416. } else {
  417. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: false)
  418. }
  419. } else if ((model?.isKind(of: KMBackgroundModel.self))!) {
  420. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: true)
  421. } else if ((model?.isKind(of: KMWatermarkModel.self))!) {
  422. (topBarView as! KMWatermarkAdjectiveTopBarView).isCanApply(can: true)
  423. }
  424. }
  425. }
  426. }
  427. func addAdsBannerView() {
  428. #if VERSION_FREE
  429. if !IAPProductsManager.default().isAvailableAllFunction(){
  430. guard let document = self.listView.document else {
  431. return
  432. }
  433. if !document.isLocked {
  434. KMAdsManager.defaultManager.beginSheetModalForView(self.readContentView, directions: .down, adPosY: 30, animated: false) { pageIndex in
  435. }
  436. }
  437. }
  438. #endif
  439. //加载底部banner
  440. // - (void)loadingAdsManager {
  441. // #if VERSION_FREE
  442. // if(![self.pdfDocument isLocked]) {
  443. // if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
  444. // [[KMAdsManager defaultManager] beginSheetModalForView:self.pdfView
  445. // directions:KMADViewDirectionsDown
  446. // animated:NO
  447. // completionHandler:nil];
  448. // }
  449. // [[NSNotificationCenter defaultCenter] addObserverForName:KMIAPProductPurchasedNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){
  450. // if ([IAPProductsManager defaultManager].isAvailableAllFunction) {
  451. // [[KMAdsManager defaultManager] dismissSheetModalForView:self.pdfView];
  452. // }
  453. // }];
  454. // [[NSNotificationCenter defaultCenter] addObserverForName:kDeviceActivateStatusChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){
  455. // if ([IAPProductsManager defaultManager].isAvailableAllFunction) {
  456. // [[KMAdsManager defaultManager] dismissSheetModalForView:self.pdfView];
  457. // }
  458. // }];
  459. // }
  460. // #endif
  461. // }
  462. }
  463. // MARK: Private Methods
  464. internal func removeNotifications() {
  465. NotificationCenter.default.removeObserver(self)
  466. self.leftSideViewController.clearAnnotationFilterData()
  467. self.leftSideViewController.clearNotification()
  468. }
  469. func checkShouldAutoOpenLeftVC() {
  470. // if (KMPreferenceManager.shared.leftSideNeedCloseWhenOpenFile()) {
  471. // return
  472. // }
  473. // if (KMPreferenceManager.shared.leftSideDisplayType == .showOutlineIfHas) {
  474. // let outlineRoot = self.listView.document.outlineRoot()
  475. // let hasOutline = (outlineRoot != nil && outlineRoot!.numberOfChildren > 0)
  476. // if (hasOutline == false) {
  477. // return
  478. // }
  479. //
  480. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  481. // self.leftSideViewController.refreshMethodType(methodType: .Outline)
  482. // }
  483. // return
  484. // }
  485. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  486. // let selectedIndex = UserDefaults.standard.integer(forKey: "KMBOTASelectedIndexKey")
  487. // let thumai = KMLeftMethodMode()
  488. // if selectedIndex == 0 {
  489. // thumai.methodType = .Thumbnail
  490. // } else if selectedIndex == 1 {
  491. // thumai.methodType = .Outline
  492. // } else if selectedIndex == 2 {
  493. // thumai.methodType = .BookMark
  494. // } else if selectedIndex == 3 {
  495. // thumai.methodType = .Annotation
  496. // } else if selectedIndex == 4 {
  497. // thumai.methodType = .Search
  498. // }
  499. //
  500. // self.leftSideViewController.refreshMethodType(methodType: thumai.methodType)
  501. // }
  502. if KMPreference.shared.showLeftSideBar == false {
  503. return
  504. }
  505. if self.leftPanelOpen {
  506. return
  507. }
  508. Task { @MainActor in
  509. self.leftSideViewController.showThumbnail()
  510. self.toolbarController.findItem(KMLeftControlToolbarItemIdentifier)?.isSelected = true
  511. }
  512. }
  513. func applyLeftSideWidth(_ leftSideWidth: CGFloat, rightSideWidth: CGFloat) -> Void {
  514. mianSplitView.setPosition(leftSideWidth, ofDividerAt: 0)
  515. mianSplitView.setPosition(mianSplitView.maxPossiblePositionOfDivider(at: 1) - mianSplitView.dividerThickness - rightSideWidth, ofDividerAt: 1)
  516. lastLeftPanWidth = leftSideWidth
  517. lastRightPanWidth = rightSideWidth
  518. }
  519. //MARK: 动画 暂未接入
  520. func animateSplitView(to position: CGFloat) {
  521. let frame1 = NSRect(x: 0, y: 0, width: position, height: mianSplitView.frame.height)
  522. let frame2 = NSRect(x: position, y: 0, width: mianSplitView.frame.width - position, height: mianSplitView.frame.height)
  523. // 创建一个新的动画上下文
  524. let animationContext = NSAnimationContext.current
  525. animationContext.duration = 0.5
  526. animationContext.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  527. // 启用隐式动画
  528. NSAnimationContext.current.allowsImplicitAnimation = true
  529. // 移动分隔条
  530. mianSplitView.setPosition(position, ofDividerAt: 0)
  531. mianSplitView.subviews[0].frame = frame1
  532. mianSplitView.subviews[1].frame = frame2
  533. }
  534. internal var removeAllAnnotationsStore = KMPDFViewRemoveAllAnnotationsStore()
  535. internal func removeAllAnnotations() {
  536. let alert = NSAlert()
  537. alert.messageText = NSLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  538. alert.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  539. alert.addButton(withTitle: NSLocalizedString("No", comment:""))
  540. if (alert.runModal() != .alertFirstButtonReturn) {
  541. return
  542. }
  543. DispatchQueue.main.async {
  544. self.removeAllAnnotationsStore.store(t: self.listView)
  545. }
  546. }
  547. // MARK: Set Methods
  548. var setDocument: CPDFDocument? {
  549. get {
  550. return document
  551. }
  552. set {
  553. if document != newValue {
  554. document = newValue
  555. }
  556. listView.document = document
  557. listView.document.delegate = self
  558. self.listView.layoutDocumentView()
  559. }
  560. }
  561. var setPageNumber: UInt {
  562. get {
  563. return pageNumber!
  564. }
  565. set {
  566. let pageCount = listView.document.pageCount
  567. var value = newValue
  568. if value > pageCount {
  569. value = pageCount
  570. }
  571. if value > 0 && listView.currentPage().pageIndex() != value-1 {
  572. listView.go(to: listView.document.page(at: value-1))
  573. }
  574. if pageNumber != value {
  575. pageNumber = value
  576. }
  577. }
  578. }
  579. // MARK: - 标记密文
  580. func enterRedact() {
  581. if !IAPProductsManager.default().isAvailableAllFunction(){
  582. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  583. return
  584. }
  585. if self.listView.document.allowsPrinting == false || self.listView.document.allowsCopying == false {
  586. Task {
  587. _ = await KMAlertTool.runModel(message: KMLocalizedString("This is a secured document. Editing is not permitted.", nil))
  588. }
  589. return
  590. }
  591. if self.hasEnterRedact() {
  592. self.exitRedact()
  593. return
  594. }
  595. let ttsWindowC = KMTTSWindowController.share
  596. if ttsWindowC.pdfView?.document?.documentURL.path == self.listView.document.documentURL.path {
  597. if let data = ttsWindowC.window?.isVisible, data {
  598. ttsWindowC.stopSpeaking()
  599. ttsWindowC.close()
  600. }
  601. }
  602. NSColorPanel.shared.showsAlpha = false
  603. let controller = KMPDFRedactViewController(url: self.listView.document!.documentURL, password: self.listView.document.password)
  604. self.addChild(controller)
  605. self.PDFContendView.addSubview(controller.view)
  606. controller.view.frame = self.PDFContendView.bounds
  607. controller.view.autoresizingMask = [.width, .height]
  608. self.listView.isHidden = true
  609. controller.scaleFactor = self.listView.scaleFactor
  610. controller.titleBack = { [weak self] title in
  611. self?.view.window?.title = title
  612. }
  613. controller.callback = { [weak self] result, currentPageIndex, saveResult, saveUrl in
  614. if result == false { // 退出
  615. self?.exitRedact()
  616. return
  617. }
  618. let controller = self?._getPDFRedactController()
  619. controller?.redactPdfView.newAddAnnotation.removeAll()
  620. self?.exitRedact()
  621. if saveResult {
  622. let newDocument = CPDFDocument(url: saveUrl)
  623. if let data = newDocument?.isLocked, data {
  624. newDocument?.unlock(withPassword: self?.listView.document.password ?? "")
  625. }
  626. self?.document = newDocument
  627. self?.listView.document = newDocument
  628. self?.listView.layoutDocumentView()
  629. }
  630. }
  631. }
  632. func exitRedact() {
  633. let controller = self._getPDFRedactController()
  634. if let data = controller {
  635. if data.redactPdfView.newAddAnnotation.count > 0 {
  636. KMAlertTool.runModel(message: "", informative: KMLocalizedString("There are unapplied redactions in this file. Exit will not save redaction.", nil), buttons: [KMLocalizedString("Exit", nil), KMLocalizedString("Cancel", nil)]) { response in
  637. if response == .alertFirstButtonReturn {
  638. data.redactPdfView.newAddAnnotation.removeAll()
  639. self.exitRedact()
  640. }
  641. }
  642. return
  643. }
  644. }
  645. NSColorPanel.shared.showsAlpha = true
  646. self.toolbarController.findItem(KMDocumentRedactToolbarItemIdentifier)?.isSelected = false
  647. // self.toolbarController.toolbarType = .None
  648. // self.listView.toolMode = .moveToolMode
  649. controller?.redactPdfView.resignMonitor()
  650. controller?.view.removeFromSuperview()
  651. controller?.removeFromParent()
  652. self.listView.isHidden = false
  653. // self.listView.layoutDocumentView()
  654. // self.view.window?.makeFirstResponder(self.listView)
  655. self.listView.annotationType = .unkown
  656. }
  657. func hasEnterRedact() -> Bool {
  658. return self._getPDFRedactController() != nil
  659. }
  660. //MARK: - AI
  661. func loadAIIconView() -> Void {
  662. self.aiTipView = AITipIconView.createFromNib()
  663. self.aiTipView.clickHandle = { [weak self] view in
  664. self?.showAITypeChooseView()
  665. }
  666. self.aiTipView.frame = CGRectMake(CGRectGetWidth(self.view.frame)-84, CGRectGetHeight(self.view.frame)-84-64-50, 72, 72)
  667. self.aiTipView.autoresizingMask = [.minXMargin, .minYMargin]
  668. self.view.addSubview(self.aiTipView)
  669. }
  670. func showAITypeChooseView() -> Void {
  671. if AIInfoManager.default().aiInfo.infoDict.isEmpty == true {
  672. #if VERSION_DMG
  673. KMPurchaseCompareWindowController.dmgPurchaseInstance().showWindow(nil)
  674. #else
  675. AIPurchaseWindowController.currentWC().showWindow(nil)
  676. #endif
  677. return
  678. }
  679. let controller = NSViewController.init()
  680. let view = NSView.init()
  681. controller.view = view
  682. var string: String = NSLocalizedString("AI Summarize", comment: "")
  683. if string.count < NSLocalizedString("AI Rewrite", comment: "").count {
  684. string = NSLocalizedString("AI Rewrite", comment: "")
  685. }
  686. if string.count < NSLocalizedString("AI Proofread", comment: "").count {
  687. string = NSLocalizedString("AI Proofread", comment: "")
  688. }
  689. if string.count < NSLocalizedString("AI Translate", comment: "").count {
  690. string = NSLocalizedString("AI Translate", comment: "")
  691. }
  692. let font = NSFont.SFProTextRegularFont(13)
  693. var style = NSMutableParagraphStyle.init()
  694. style.alignment = .center
  695. style.lineBreakMode = .byCharWrapping
  696. let size: NSSize = string.boundingRect(with: NSSize(width: 1000, height: 100),
  697. options: NSString.DrawingOptions(rawValue: 3),
  698. attributes: [NSAttributedString.Key.font : NSFont.SFProTextRegularFont(13), NSAttributedString.Key.paragraphStyle : style]).size
  699. controller.view.frame = CGRectMake(0, 0, size.width+80, 160)
  700. if self.aiTypeChooseView == nil {
  701. self.aiTypeChooseView = AITypeChooseView.createFromNib()
  702. }
  703. self.aiTypeChooseView.clickHandle = { [weak self] view, type in
  704. DispatchQueue.main.async {
  705. self!.loadAIConfigWindowWithType(type)
  706. }
  707. }
  708. self.aiTypeChooseView.frame = controller.view.bounds
  709. self.aiTypeChooseView.autoresizingMask = [.width, .height]
  710. controller.view.addSubview(self.aiTypeChooseView)
  711. let popover = NSPopover.init()
  712. popover.contentViewController = controller
  713. popover.animates = true
  714. popover.behavior = .transient
  715. var rect = self.aiTipView.bounds
  716. rect.origin.y += 20
  717. popover.show(relativeTo: rect, of: self.aiTipView, preferredEdge: .minY)
  718. }
  719. func loadAIConfigWindowWithType(_ type: AIConfigType) -> Void {
  720. let windowVC = AIConfigWindowController.currentWC()
  721. windowVC.configType = type
  722. if type == .summarize {
  723. windowVC.window?.setFrame(CGRectMake(0, 0, 800, 500), display: true)
  724. } else if type == .reWriting {
  725. windowVC.window?.setFrame(CGRectMake(0, 0, 800, 460), display: true)
  726. } else if type == .proofreading {
  727. windowVC.window?.setFrame(CGRectMake(0, 0, 800, 460), display: true)
  728. } else if type == .translate {
  729. windowVC.window?.setFrame(CGRectMake(0, 0, 800, 545), display: true)
  730. }
  731. windowVC.cancelHandle = { [weak self] windowVC in
  732. self?.view.window?.endSheet(windowVC.window!)
  733. }
  734. self.view.window?.beginSheet(windowVC.window!)
  735. windowVC.refreshUI()
  736. }
  737. //MARK: - 引导
  738. func loadFunctionGuide() -> Void {
  739. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  740. if self.view.window != nil {
  741. self.loadOpenFileFunctionGuide(.openFileNormal)
  742. }
  743. }
  744. }
  745. func loadOpenFileFunctionGuide(_ showType: KMGuideInfoType) -> Void {
  746. if showType == .openFileNormal && KMGuideInfoWindowController.availableShow(.openFileNormal) {
  747. let leftPanelItem:KMToolbarItemView = self.toolbarController.findItem("KMLeftControlToolbarItemIdentifier")!
  748. let guideWC = KMGuideInfoWindowController.currentWC()
  749. guideWC.type = .openFileNormal
  750. guideWC.openPanelRect = (self.view.window?.contentView?.convert(leftPanelItem.frame, from: leftPanelItem.superview)) ?? CGRectZero
  751. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  752. guideWC.normalGuideFinishHandle = { [weak self] windowVC in
  753. let rightPanelItem = self?.toolbarController.findItem(KMRightControlToolbarItemIdentifier)
  754. let digitalPanelItem = self?.toolbarController.findItem(KMDocumentDigitalSignToolbarItemIdentifier)
  755. windowVC.rightPanelRect = (self!.view.window?.contentView?.convert(rightPanelItem?.frame ?? .zero, from: rightPanelItem?.superview)) ?? .zero
  756. guideWC.digitalBoxRect = (self!.view.window?.contentView?.convert(digitalPanelItem?.frame ?? .zero, from: digitalPanelItem?.superview)) ?? .zero
  757. }
  758. guideWC.finishHandle = { [weak self] windowVC, type in
  759. if type == .windowNewFinish ||
  760. type == . windowDigitalFinish {
  761. self?.checkFirstTrialController()
  762. }
  763. }
  764. guideWC.openFileToggleHandle = { [weak self] windowVC, type in
  765. self?.checkFirstTrialController()
  766. }
  767. var rect = self.view.window!.frame
  768. rect.size.height -= 20
  769. guideWC.window?.setFrame(rect, display: false)
  770. guideWC.window?.minSize = rect.size
  771. guideWC.window?.maxSize = rect.size
  772. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  773. guideWC.show()
  774. } else if showType == .digitalSignGuide && KMGuideInfoWindowController.availableShow(.digitalSignGuide) {
  775. let guideWC = KMGuideInfoWindowController.currentWC()
  776. guideWC.type = .digitalSignGuide
  777. let digitalPanelItem:KMToolbarItemView = self.toolbarController.findItem(KMDocumentDigitalSignToolbarItemIdentifier)!
  778. guideWC.digitalBoxRect = (self.view.window?.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview))!
  779. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  780. guideWC.finishHandle = { [weak self] windowVC, type in
  781. self?.checkFirstTrialController()
  782. }
  783. var rect = self.view.window!.frame
  784. rect.size.height -= 20
  785. guideWC.window?.setFrame(rect, display: false)
  786. guideWC.window?.minSize = rect.size
  787. guideWC.window?.maxSize = rect.size
  788. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  789. guideWC.show()
  790. } else if showType == .pdfCompareGuide && KMGuideInfoWindowController.availableShow(.pdfCompareGuide) {
  791. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  792. let guideWC = KMGuideInfoWindowController.currentWC()
  793. guideWC.type = .pdfCompareGuide
  794. let digitalPanelItem:KMToolbarItemView = self.toolbarController.findItem(KMDocumentDigitalSignToolbarItemIdentifier)!
  795. guideWC.digitalBoxRect = (self.view.window?.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview))!
  796. let compareItem:KMToolbarItemView = self.toolbarController.findItem(KMToolbarComparisonItemIdentifier)!
  797. guideWC.compareItemRect = (self.view.window?.contentView?.convert(compareItem.frame, from: compareItem.superview))!
  798. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  799. var rect = self.view.window!.frame
  800. rect.size.height -= 20
  801. guideWC.window?.setFrame(rect, display: false)
  802. guideWC.window?.minSize = rect.size
  803. guideWC.window?.maxSize = rect.size
  804. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  805. guideWC.show()
  806. }
  807. } else if showType == .convertGuide && KMGuideInfoWindowController.availableShow(.convertGuide) {
  808. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  809. let guideWC = KMGuideInfoWindowController.currentWC()
  810. guideWC.type = .convertGuide
  811. let digitalPanelItem:KMToolbarItemView = self.toolbarController.findItem(KMDocumentDigitalSignToolbarItemIdentifier)!
  812. guideWC.digitalBoxRect = (self.view.window?.contentView?.convert(digitalPanelItem.frame, from: digitalPanelItem.superview))!
  813. guideWC.purchaseHandle = { [weak self] windowVC in
  814. #if VERSION_DMG
  815. if IAPProductsManager.default().isAvailableAllFunction() {
  816. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  817. //Convert:
  818. self?.showAllConvertWindow(convertT: .Word)
  819. } else {
  820. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  821. limitWC.continueBlock = { [weak self] windowController in
  822. }
  823. limitWC.window?.center()
  824. limitWC.showWindow(nil)
  825. }
  826. } else {
  827. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  828. }
  829. #else
  830. if IAPProductsManager.default().isAvailableAllFunction() {
  831. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  832. //Convert:
  833. } else {
  834. var vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  835. vc.showWindow(nil)
  836. }
  837. } else {
  838. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  839. }
  840. #endif
  841. }
  842. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  843. var rect = self.view.window!.frame
  844. rect.size.height -= 20
  845. guideWC.window?.setFrame(rect, display: false)
  846. guideWC.window?.minSize = rect.size
  847. guideWC.window?.maxSize = rect.size
  848. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  849. guideWC.show()
  850. }
  851. } else {
  852. }
  853. }
  854. func checkFirstTrialController() -> Void {
  855. #if VERSION_DMG
  856. //打开文档后引导相关
  857. if VerificationManager.default().status == .none {
  858. let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  859. let lastVersion = UserDefaults.standard.object(forKey: "SKLastTrialVersionMainDocumentLaunchedKey") as? String ?? ""
  860. if lastVersion == nil ||
  861. lastVersion != appVersion {
  862. UserDefaults.standard.setValue(appVersion, forKey: "SKLastTrialVersionMainDocumentLaunchedKey")
  863. UserDefaults.standard.synchronize()
  864. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  865. }
  866. }
  867. #endif
  868. }
  869. // MARK: - 页面编辑
  870. open func enterPageEdit(_ pages: [Int] = []) {
  871. //选中page
  872. var tPages = pages
  873. if tPages.count == 0 {
  874. tPages = self.leftSideViewController.selectPages ?? [self.listView.currentPageIndex]
  875. }
  876. if (hasEnterPageEdit()) {
  877. exitPageEdit()
  878. return
  879. }
  880. if (self.toolbarController != nil && self.toolbarController.mainToolBarView != nil) {
  881. let toolBarView: KMToolbarViewController = self.toolbarController.mainToolBarView!
  882. for (key, value) in toolBarView.toolbarItems {
  883. if (key == KMRightControlToolbarItemIdentifier || key == KMLeftControlToolbarItemIdentifier) {
  884. (value as! KMToolbarItemView).unEnabled = true
  885. }
  886. }
  887. }
  888. // #if VERSION_DMG
  889. // let controller = KMPDFEditViewController_dmg(self.listView.document)
  890. // #else
  891. let controller = KMPDFEditViewController(self.listView.document)
  892. // #endif
  893. controller.selectedPages = tPages
  894. controller.listView = self.listView
  895. self.addChild(controller)
  896. self.PDFContendView.addSubview(controller.view)
  897. controller.view.frame = self.PDFContendView.bounds
  898. controller.view.autoresizingMask = [.width,.height]
  899. self.listView.isHidden = true
  900. controller.itemClick = { [weak self] index, params in
  901. if (index == 1) { /// 双击退出
  902. self?.enterEditMode(self!.leftSideViewController, [])
  903. DispatchQueue.main.async {
  904. let pageIndex: Int = params.first as! Int
  905. self?.listView.go(toPageIndex: pageIndex, animated: true)
  906. }
  907. } else if (index == 2) { // 打印
  908. self?.showPrintWindow(pageRange: KMPrintPageRange(type: .custom, selectPages: params.first as! [Int]))
  909. }
  910. }
  911. controller.documentEditedCallback = { [weak self] params in
  912. self?.recordIsPDFDocumentEdited()
  913. // self?.needSave = true
  914. if let data = params.first as? KMPageEditType {
  915. self?.recordSaveWatermarkFlag(type: data.toSubscribeWaterMarkType())
  916. } else {
  917. self?.recordSaveWatermarkFlag()
  918. }
  919. }
  920. controller.selectionDidChange = { [weak self] selectedIndexs in
  921. var indexSet = IndexSet()
  922. for indexPath in selectedIndexs {
  923. indexSet.insert(indexPath.item)
  924. }
  925. if indexSet.count != 0 {
  926. // self?.leftSideViewController.thumbnailViewController.selectPages(indexs: indexSet, needScroll: true)
  927. // self?.listView.go(toPageIndex: indexSet.first!, animated: false)
  928. }
  929. }
  930. }
  931. open func exitPageEdit() {
  932. if (self.toolbarController != nil && self.toolbarController.mainToolBarView != nil) {
  933. let toolBarView: KMToolbarViewController = self.toolbarController.mainToolBarView!
  934. for (key, value) in toolBarView.toolbarItems {
  935. if (key == KMRightControlToolbarItemIdentifier || key == KMLeftControlToolbarItemIdentifier) {
  936. (value as! KMToolbarItemView).unEnabled = false
  937. }
  938. }
  939. }
  940. let editController = getPDFEditController()
  941. if (editController == nil) {
  942. return
  943. }
  944. self.listView.annotationType = .highlight
  945. editController?.view.removeFromSuperview()
  946. editController?.removeFromParent()
  947. self.listView.isHidden = false
  948. self.listView.layoutDocumentView()
  949. self.view.window?.makeFirstResponder(self.listView)
  950. self.listView.annotationType = .unkown
  951. self.listView.go(toPageIndex: editController!.listViewCurrentIndex, animated: false)
  952. if let data = editController?.isEdited, data {
  953. self.leftSideViewController.reloadThumbnailDataIfNeed()
  954. }
  955. }
  956. open func hasEnterPageEdit() -> Bool {
  957. return self.getPDFEditController() != nil
  958. }
  959. // MARK: - Private Methods
  960. private func getPDFEditController() -> KMPDFEditViewController? {
  961. var editController: KMPDFEditViewController?
  962. for controller in self.children {
  963. if (controller.isKind(of: KMPDFEditViewController.self)) {
  964. editController = (controller as! KMPDFEditViewController)
  965. break
  966. }
  967. }
  968. return editController
  969. }
  970. private func _getPDFRedactController() -> KMPDFRedactViewController? {
  971. var controller: KMPDFRedactViewController?
  972. for childC in self.children {
  973. if (childC.isKind(of: KMPDFRedactViewController.self)) {
  974. controller = (childC as! KMPDFRedactViewController)
  975. break
  976. }
  977. }
  978. return controller
  979. }
  980. private func addBackgroundMaskView() {
  981. self.removeBackgroundMaskView()
  982. if let superview = self.mianSplitView.superview {
  983. let view = NSView()
  984. superview.addSubview(view)
  985. view.frame = superview.bounds
  986. view.autoresizingMask = [.width, .height]
  987. view.wantsLayer = true
  988. view.layer?.backgroundColor = .white
  989. self.background_mask = view
  990. }
  991. }
  992. private func removeBackgroundMaskView() {
  993. self.background_mask?.removeFromSuperview()
  994. self.background_mask = nil
  995. }
  996. private func _goToFirstPageForFristAppear() {
  997. DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.1) {
  998. self.listView.go(toPageIndex: 0, animated: false)
  999. }
  1000. }
  1001. func isFileGreaterThan10MB(atPath filePath: String) -> Bool {
  1002. let fileManager = FileManager.default
  1003. do {
  1004. let fileAttributes = try fileManager.attributesOfItem(atPath: filePath)
  1005. if let fileSize = fileAttributes[.size] as? UInt64 {
  1006. let megabyteSize = fileSize / (1024 * 1024)
  1007. return megabyteSize >= 10
  1008. }
  1009. } catch {
  1010. KMPrint("Error: \(error)")
  1011. }
  1012. return false
  1013. }
  1014. func isPDFPageCountExceedsLimit(filePath: String) -> Bool {
  1015. let url = URL(fileURLWithPath: filePath)
  1016. guard let document = PDFDocument(url: url) else {
  1017. return false
  1018. }
  1019. let pageCount = document.pageCount
  1020. return pageCount > 30
  1021. }
  1022. // MARK: Redact 【标记密文】
  1023. func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
  1024. let windowController = KMRedactConfirmWindowController(type)
  1025. self.currentWindowController = windowController
  1026. self.view.window?.beginSheet(windowController.window!)
  1027. windowController.itemClick = { [weak self] index in
  1028. if (index == 2) { /// 取消
  1029. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1030. self?.currentWindowController = nil
  1031. callback()
  1032. return
  1033. }
  1034. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1035. self?.currentWindowController = nil
  1036. let panel = NSSavePanel()
  1037. panel.nameFieldStringValue = "[新文件]"+(self?.listView.document.documentURL.lastPathComponent)!
  1038. let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  1039. button.state = .on
  1040. panel.accessoryView = button
  1041. panel.isExtensionHidden = true
  1042. panel.beginSheetModal(for: (self?.view.window!)!) { response in
  1043. if response != .OK {
  1044. callback()
  1045. return
  1046. }
  1047. if (type == .redactOne) {
  1048. let anno = self!.listView.activeAnnotation
  1049. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  1050. callback()
  1051. return
  1052. }
  1053. (anno as! CPDFRedactAnnotation).applyRedaction()
  1054. } else if (type == .redactAll) {
  1055. self?.listView.document.applyRedactions()
  1056. } else if (type == .eraserOne) {
  1057. let anno = self!.listView.activeAnnotation
  1058. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  1059. callback()
  1060. return
  1061. }
  1062. anno?.page.erasureRedact(from: anno!.bounds)
  1063. } else if (type == .eraserAll) {
  1064. KMRedactTools.eraserDocument((self?.listView.document)!) { result, errorAnno in
  1065. if (result == false) {
  1066. callback()
  1067. return
  1068. }
  1069. }
  1070. }
  1071. self!.listView.document.write(to: panel.url)
  1072. if (button.state == .on) {
  1073. NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
  1074. }
  1075. } else {
  1076. NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
  1077. }
  1078. callback()
  1079. }
  1080. }
  1081. }
  1082. // MARK: Secure 【安全】
  1083. public func showSecureSuccessTip() {
  1084. let view: NSView = self.view
  1085. let tip = KMSecureEncryptSuccessTipView()
  1086. let size = NSSize(width: 379, height: 176)
  1087. tip.frame = NSMakeRect(view.frame.size.width-size.width-16, view.frame.size.height-size.height-88, size.width, size.height)
  1088. tip.autoresizingMask = [.minXMargin, .minYMargin]
  1089. view.addSubview(tip)
  1090. tip.itemClick = { [weak self] in
  1091. self!.hiddenSecureSuccessTip()
  1092. }
  1093. }
  1094. public func hiddenSecureSuccessTip() {
  1095. let view: NSView = self.view
  1096. var tip: KMSecureEncryptSuccessTipView?
  1097. for subview in view.subviews {
  1098. if (subview.isKind(of: KMSecureEncryptSuccessTipView.self)) {
  1099. tip = (subview as! KMSecureEncryptSuccessTipView)
  1100. break
  1101. }
  1102. }
  1103. if (tip == nil) {
  1104. return
  1105. }
  1106. tip?.removeFromSuperview()
  1107. }
  1108. public func showSecureLimitTip() {
  1109. self.hiddenSecureLimitTip()
  1110. let tip = KMSecureAlertView()
  1111. tip.show(in: self.listView)
  1112. tip.closeAction = { [unowned self] view in
  1113. self.hiddenSecureLimitTip()
  1114. }
  1115. tip.passwordAction = { [unowned self] view in
  1116. self.removeOwnerPassword()
  1117. }
  1118. }
  1119. func removeOwnerPassword() {
  1120. guard let doc = self.listView?.document else {
  1121. NSSound.beep()
  1122. return
  1123. }
  1124. if doc.allowsCopying && doc.allowsPrinting {
  1125. NSSound.beep()
  1126. return
  1127. }
  1128. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: doc.documentURL) { [weak self] result, password in
  1129. if result == .cancel { /// 关闭
  1130. return
  1131. }
  1132. /// 解密成功
  1133. self?.hiddenSecureLimitTip()
  1134. self?.isSaveKeyChain = false
  1135. self?.listView.document.unlock(withPassword: password)
  1136. }
  1137. }
  1138. public func hiddenSecureLimitTip() {
  1139. let view: NSView = self.view
  1140. var tip: KMSecureAlertView?
  1141. for subview in view.subviews {
  1142. if (subview.isKind(of: KMSecureAlertView.self)) {
  1143. tip = (subview as! KMSecureAlertView)
  1144. break
  1145. }
  1146. }
  1147. if (tip == nil) {
  1148. return
  1149. }
  1150. tip?.removeFromSuperview()
  1151. }
  1152. override func mouseMoved(with event: NSEvent) {
  1153. self.view.window?.mouseMoved(with: event)
  1154. }
  1155. func savePageNumberIfNeed() {
  1156. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  1157. let scaleFactor = self.listView?.scaleFactor ?? 0
  1158. if scaleFactor <= 0 {
  1159. return
  1160. }
  1161. if self.listView.document != nil {
  1162. KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
  1163. KMPreferenceManager.shared.setPageScale(Float(self.listView.scaleFactor), forKey: self.listView.document.documentURL.path)
  1164. }
  1165. }
  1166. }
  1167. // MARK: -
  1168. // MARK: 退出全屏
  1169. @IBAction func exitFullScreen(_ sender: Any) {
  1170. if (self.view.window == nil) {
  1171. return
  1172. }
  1173. if (KMTools.isFullScreen(self.view.window!)) {
  1174. self.view.window?.toggleFullScreen(nil)
  1175. }
  1176. }
  1177. // MARK: -
  1178. // MARK: 显示合并窗口
  1179. public func showMergeWindow(url: URL? = nil, _ password: String?) {
  1180. DispatchQueue.main.async {
  1181. if let _url = url {
  1182. let document = PDFDocument(url: _url)
  1183. let windowController = KMMergeWindowController(document: document!, password: password ?? "")
  1184. windowController.oriDucumentUrl = self.listView.document.documentURL
  1185. windowController.pageIndex = self.listView.currentPageIndex
  1186. self.currentWindowController = windowController
  1187. windowController.cancelAction = { [unowned self] controller in
  1188. self.view.window?.endSheet((self.currentWindowController.window)!)
  1189. self.currentWindowController = nil
  1190. }
  1191. windowController.mergeAction = { [unowned self] controller, filePath in
  1192. self.view.window?.endSheet((self.currentWindowController.window)!)
  1193. self.currentWindowController = nil
  1194. let newDocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
  1195. if let data = newDocument?.isLocked, data {
  1196. newDocument?.unlock(withPassword: self.listView.document.password ?? "")
  1197. }
  1198. self.setDocument = newDocument
  1199. if self.leftSideViewController.type.methodType == .Thumbnail {
  1200. self.leftSideViewController.resetThumbnails()
  1201. }
  1202. }
  1203. self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
  1204. self.view.window?.beginSheet(windowController.window!)
  1205. } else {
  1206. let document = PDFDocument(url: self.listView.document.documentURL)
  1207. let windowController = KMMergeWindowController(document: document!, password: password ?? "")
  1208. self.toolbarController.cancelSelected(KMToolbarToolMergeItemIdentifier)
  1209. self.view.window!.km_beginSheet(windowC: windowController)
  1210. }
  1211. }
  1212. }
  1213. // MARK: -
  1214. // MARR: 显示加密弹窗
  1215. public func showSecureWindow(_ url: URL) {
  1216. // let windowController = KMSecureEncryptWindowController(windowNibName: "KMSecureEncryptWindowController")
  1217. // windowController.documentURL = url
  1218. // windowController.myDocument = self.listView.document
  1219. // self.currentWindowController = windowController
  1220. //
  1221. // windowController.itemClick = { [weak self] index in
  1222. // self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1223. // self?.currentWindowController = nil
  1224. // }
  1225. //
  1226. // windowController.resultCallback = { [weak self] result in
  1227. // let windowController_secure = self?.currentWindowController as! KMSecureEncryptWindowController
  1228. // self?.view.window?.endSheet((self?.currentWindowController.window)!)
  1229. // self?.currentWindowController = nil
  1230. //
  1231. // self?._secureOptions = windowController_secure.options
  1232. // self?.needSave = true
  1233. // self?.recordIsPDFDocumentEdited(type: .setPassword)
  1234. //
  1235. // if (result) {
  1236. // self?.showSecureSuccessTip()
  1237. // self?.recordSaveWatermarkFlag(type: .setPassword)
  1238. // } else {
  1239. // let alert = NSAlert()
  1240. // alert.messageText = NSLocalizedString("Failure", comment: "")
  1241. // alert.runModal()
  1242. // }
  1243. // }
  1244. //
  1245. // self.view.window?.beginSheet(windowController.window!)
  1246. let controller = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
  1247. controller.pdfDocument = self.document
  1248. self.currentWindowController = controller
  1249. controller.batchAction = { [unowned self] controller, files in
  1250. self.view.window?.endSheet((self.currentWindowController.window)!)
  1251. self.currentWindowController = nil
  1252. self.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
  1253. let batchWindowController = KMBatchOperateWindowController.sharedWindowController
  1254. batchWindowController.window?.makeKeyAndOrderFront("")
  1255. let batchOperateFile = KMBatchOperateFile(filePath: files.first?.filePath ?? "")
  1256. batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
  1257. batchWindowController.window?.makeKeyAndOrderFront("")
  1258. }
  1259. controller.doneAction = { [unowned self] controller, options, attribute in
  1260. // let windowController_secure = self.currentWindowController as! KMSecureEncryptWindowController
  1261. let openPanel = NSOpenPanel()
  1262. openPanel.canChooseFiles = false
  1263. openPanel.canChooseDirectories = true
  1264. openPanel.canCreateDirectories = true
  1265. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { (result) in
  1266. if result == NSApplication.ModalResponse.OK {
  1267. for fileURL in openPanel.urls {
  1268. let document = CPDFDocument(url: self.document?.documentURL)
  1269. if document != nil {
  1270. document!.setDocumentAttributes(attribute)
  1271. let path = fileURL.path.stringByAppendingPathComponent(url.lastPathComponent)
  1272. let success = document!.write(to: NSURL(fileURLWithPath: path) as URL, withOptions: options)
  1273. if success {
  1274. self.view.window?.endSheet((self.currentWindowController.window)!)
  1275. self.currentWindowController = nil
  1276. NSWorkspace.shared.activateFileViewerSelecting([fileURL])
  1277. }
  1278. }
  1279. }
  1280. }
  1281. }
  1282. // self._secureOptions = options
  1283. // self.documentAttribute = attribute
  1284. // self.needSave = true
  1285. // self.recordIsPDFDocumentEdited(type: .setPassword)
  1286. //
  1287. // if (options.count != 0) {
  1288. // self.showSecureSuccessTip()
  1289. // self.recordSaveWatermarkFlag(type: .setPassword)
  1290. // } else {
  1291. // let alert = NSAlert()
  1292. // alert.messageText = NSLocalizedString("Failure", comment: "")
  1293. // alert.runModal()
  1294. // }
  1295. }
  1296. controller.cancelAction = { [unowned self] controller in
  1297. self.view.window?.endSheet((self.currentWindowController.window)!)
  1298. self.currentWindowController = nil
  1299. }
  1300. NSWindow.currentWindow().beginSheet(controller.window!)
  1301. }
  1302. // MARK: -
  1303. // MARK: 保存文档
  1304. internal func needSaveDocument() -> Bool {
  1305. if (self.isPDFDocumentEdited) {
  1306. return self.isPDFDocumentEdited
  1307. }
  1308. if (self.needSave) {
  1309. return self.needSave
  1310. }
  1311. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  1312. if (document?.isDocumentEdited == nil || document!.isDocumentEdited == false) {
  1313. return false
  1314. }
  1315. return true
  1316. }
  1317. internal func saveDocument(overlookDocumentIfEdited overlook: Bool = false) {
  1318. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  1319. if (overlook) {
  1320. document?.save(nil)
  1321. return
  1322. }
  1323. if (self.isPDFDocumentEdited) {
  1324. self.clearIsPDFDocumentEdited()
  1325. self.needSave = false
  1326. document?.save(nil)
  1327. return
  1328. }
  1329. if (document?.isDocumentEdited == nil || document!.isDocumentEdited == false) {
  1330. return
  1331. }
  1332. document?.save(nil)
  1333. }
  1334. internal func asyncSaveDocument(overlookDocumentIfEdited overlook: Bool = false, callback:@escaping KMCommonBlock) {
  1335. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  1336. if (overlook) {
  1337. DispatchQueue.main.async {
  1338. document?.save(nil)
  1339. callback()
  1340. }
  1341. return
  1342. }
  1343. if (self.isPDFDocumentEdited) {
  1344. self.clearIsPDFDocumentEdited()
  1345. self.needSave = false
  1346. DispatchQueue.main.async {
  1347. document?.save(nil)
  1348. callback()
  1349. }
  1350. return
  1351. }
  1352. if (document?.isDocumentEdited == nil || document!.isDocumentEdited == false) {
  1353. callback()
  1354. return
  1355. }
  1356. DispatchQueue.main.async {
  1357. document?.save(nil)
  1358. callback()
  1359. }
  1360. }
  1361. internal func saveDocumentWithProgressAlert(callback:@escaping KMCommonBlock) {
  1362. // 显示进度
  1363. self.showProgressWindow(message: NSLocalizedString("Save PDF", comment: ""))
  1364. self.progressController?.maxValue = 3.0
  1365. self.progressController?.increment(by: 1.0)
  1366. // 保存文档
  1367. self.asyncSaveDocument { [unowned self] params in
  1368. // 执行进度 [假进度]
  1369. self.progressController?.increment(by: 1.0)
  1370. self.progressController?.increment(by: 1.0)
  1371. // DispatchQueue.main.async {
  1372. DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
  1373. // 隐藏进度
  1374. self.hiddenProgressWindow()
  1375. // 回调
  1376. callback()
  1377. }
  1378. }
  1379. }
  1380. // MARK: -
  1381. // MARK: 选择 PDFDisplay 模式
  1382. @objc public func selectDisplay(display: KMPDFDisplayType, viewSettingIsReload: Bool = true) {
  1383. let toolModel = self.listView.toolMode
  1384. self.isReadMode = false
  1385. switch display {
  1386. case .singlePage:
  1387. self.listView.setDisplay(.singlePage)
  1388. break
  1389. case .singlePageContinuous:
  1390. self.listView.setDisplay(.singlePageContinuous)
  1391. break
  1392. case .twoUp:
  1393. self.listView.setDisplay(.twoUp)
  1394. break
  1395. case .twoUpContinuous:
  1396. self.listView.setDisplay(.twoUpContinuous)
  1397. break
  1398. case .bookMode:
  1399. self.listView.displaysAsBook = true
  1400. self.listView.displayTwoUp = true
  1401. self.listView.displayDirection = .horizontal
  1402. break
  1403. case .bookContinuous:
  1404. self.listView.displaysAsBook = true
  1405. self.listView.displayTwoUp = true
  1406. self.listView.displayDirection = .vertical
  1407. break
  1408. case .readModel:
  1409. self.openReadModel()
  1410. break
  1411. case .readContinuous:
  1412. self.openReadModel()
  1413. break
  1414. }
  1415. self.listView.layoutDocumentView()
  1416. // if (viewSettingIsReload && self.leftSideViewController.panelSetViewController.isViewLoaded) {
  1417. // self.leftSideViewController.panelSetViewController.reloadListViewModel()
  1418. // }
  1419. if (toolModel == .editPDFToolMode) {
  1420. if self.rightSideViewController.eidtPDFImageProperty != nil {
  1421. self.rightSideViewController.eidtPDFImageProperty?.cancelCutImageAction("")
  1422. self.rightSideViewController.isHidden = true
  1423. self.closeRightPane()
  1424. }
  1425. }
  1426. }
  1427. // MARK: - 选择缩放模式
  1428. @objc public func selectZoom(_ type: KMPDFZoomType) {
  1429. switch type {
  1430. case .width:
  1431. self.listView.autoScales = true
  1432. // self.listView.autoScales = false
  1433. break
  1434. case .fit:
  1435. // self.listView.autoScales = !self.listView.autoScales
  1436. if let pageHeight = self.listView.currentPage()?.size.height, pageHeight > 0 {
  1437. let pdfviewHeight = self.listView.bounds.size.height
  1438. self.listView.scaleFactor = pdfviewHeight/pageHeight
  1439. self.listView.autoScales = false
  1440. }
  1441. break
  1442. case .actualSize:
  1443. if self.listView.scaleFactor != 1.0 {
  1444. self.listView.scaleFactor = 1.0
  1445. self.listView.autoScales = false
  1446. }
  1447. break
  1448. }
  1449. }
  1450. // MARK: - 自动保存
  1451. internal func autoSaveTimeStartOrStopIfNeed() {
  1452. if (KMPreferenceManager.shared.autoSave == false) {
  1453. self.stopAutoSaveTimer()
  1454. return
  1455. }
  1456. self.startAutoSaveTimer(KMPreferenceManager.shared.autoSaveTimeInterval)
  1457. if (self.myDocument == nil) {
  1458. self.stopAutoSaveTimer()
  1459. return
  1460. }
  1461. let browser = self.browserWindowController?.browser
  1462. guard let activeDocument = browser?.activeTabContents() else {
  1463. return
  1464. }
  1465. if (self.myDocument!.isEqual(to: activeDocument) == false) {
  1466. self.pauseAutoSaveTimer()
  1467. return
  1468. }
  1469. }
  1470. private func startAutoSaveTimer(_ interval: TimeInterval) {
  1471. if (self.autoSaveTimer != nil) {
  1472. self.autoSaveTimer?.invalidate()
  1473. self.autoSaveTimer = nil
  1474. }
  1475. self.autoSaveTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] timer in
  1476. DispatchQueue.main.async {
  1477. #if DEBUG
  1478. Swift.debugPrint("文档已自动保存")
  1479. if let _document = self?.myDocument {
  1480. Swift.debugPrint(_document.fileURL as Any)
  1481. }
  1482. #endif
  1483. self?.saveDocument()
  1484. }
  1485. }
  1486. RunLoop.current.add(self.autoSaveTimer!, forMode: .common)
  1487. }
  1488. private func pauseAutoSaveTimer() {
  1489. self.autoSaveTimer?.fireDate = Date.distantFuture
  1490. }
  1491. private func reStartAutoSaveTimer() {
  1492. self.autoSaveTimer?.fireDate = Date()+KMPreferenceManager.shared.autoSaveTimeInterval
  1493. }
  1494. private func needShowRegisterView() {
  1495. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
  1496. if KMLightMemberManager.manager.checkPopupRegister() {
  1497. // Login & Logout
  1498. let window = NSApp.mainWindow ?? self.view.window
  1499. if KMLightMemberManager.manager.isLogin() {
  1500. } else if window != nil {
  1501. var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  1502. if email.count == 0 {
  1503. KMLoginWindowController.show(window: window!, .Batch, .register)
  1504. } else {
  1505. KMLoginWindowController.show(window: window!, .Batch, .login)
  1506. }
  1507. }
  1508. }
  1509. }
  1510. }
  1511. private func stopAutoSaveTimer() {
  1512. self.autoSaveTimer?.invalidate()
  1513. self.autoSaveTimer = nil
  1514. }
  1515. internal func createPdf(index:Int) {
  1516. if index == 1 {
  1517. self.homeVC?.openBlankPage()
  1518. } else if index == 4 {
  1519. self.homeVC?.importFromCamera()
  1520. } else if index == 5 {
  1521. self.homeVC?.importFromScanner()
  1522. } else if index == 3 {
  1523. self.homeVC?.importFromWebPage()
  1524. } else if index == 2 {
  1525. self.homeVC?.newFromImages()
  1526. }
  1527. }
  1528. // MARK: -
  1529. // MARK: Progress
  1530. func showProgressWindow(message: String = "") {
  1531. if (self.progressController != nil) {
  1532. self.hiddenProgressWindow()
  1533. }
  1534. let progress = SKProgressController()
  1535. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  1536. progress.window?.contentView?.wantsLayer = true
  1537. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  1538. progress.progressField.textColor = NSColor.white
  1539. progress.showClose = false
  1540. progress.message = message
  1541. self.progressController = progress
  1542. self.view.window?.beginSheet(progress.window!)
  1543. }
  1544. func hiddenProgressWindow() {
  1545. if (self.progressController != nil) {
  1546. self.view.window?.endSheet((self.progressController?.window)!)
  1547. self.progressController = nil
  1548. }
  1549. }
  1550. // MARK -
  1551. // MARK - Event 监听
  1552. private func addEventMonitor() {
  1553. if (self.eventMonitor != nil) {
  1554. self.removeEventMonitor()
  1555. }
  1556. KMPrint("已添加事件监听")
  1557. self.eventMonitor = NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) { [weak self] event in
  1558. if (event.type == .scrollWheel && event.modifierFlags.contains(.option)) { // Alt + 鼠标滚轮
  1559. self?.listView.magnifyWheel(event)
  1560. return nil
  1561. }
  1562. return event
  1563. }
  1564. }
  1565. func addKeyEventMonitor() {
  1566. if (self.keyEventMonitor != nil) {
  1567. self.removeKeyEventMonitor()
  1568. }
  1569. keyEventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
  1570. // print(event.keyCode)
  1571. if event.keyCode == 53 {
  1572. if let data = self?.interactionMode, data == .presentation { // 幻灯片模式下
  1573. if let data = self?.canExitPresentation(), data {
  1574. self?.browserWindowController?.exitFullscreen()
  1575. }
  1576. return nil
  1577. }
  1578. if self?.listView.toolMode == .editPDFToolMode {
  1579. if self != nil {
  1580. //使用editingSelectionString获取内容文字
  1581. if self!.listView.editingAreas() != nil {
  1582. if self!.listView.editingAreas().count > 0 && self!.listView.isEditable() {
  1583. self!.listView.clearEditingSelectCharItem()
  1584. } else if self!.listView.editingAreas().count > 0 {
  1585. if self?.listView.annotationType == .addImage ||
  1586. self?.listView.annotationType == .addText {
  1587. let textItem = self?.toolbarController.findItem(KMToolbarAddTextEditPDFItemIdentifier)
  1588. let imageItem = self?.toolbarController.findItem(KMToolbarAddImageEditPDFItemIdentifier)
  1589. textItem?.isSelected = false
  1590. imageItem?.isSelected = false
  1591. }
  1592. self?.rightSideViewController.isHidden = true
  1593. self?.listView.endEditIsRemoveBlock(with: self!.listView.editingAreas().first as? CPDFEditArea)
  1594. self?.listView.updateEditing([])
  1595. self?.listView.isEditImage = false
  1596. self?.listView.setNeedsDisplayPageViewFor(self!.listView.currentPage())
  1597. if self?.listView.annotationType == .addImage {
  1598. self?.listView.change([.text, .image])
  1599. }
  1600. self?.listView.annotationType = .editTextImage
  1601. self?.closeRightPane()
  1602. } else if(self?.listView.annotationType == .addImage || self!.listView.annotationType == .addText) {
  1603. if self?.listView.annotationType == .addImage ||
  1604. self?.listView.annotationType == .addText {
  1605. let textItem = self?.toolbarController.findItem(KMToolbarAddTextEditPDFItemIdentifier)
  1606. let imageItem = self?.toolbarController.findItem(KMToolbarAddImageEditPDFItemIdentifier)
  1607. textItem?.isSelected = false
  1608. imageItem?.isSelected = false
  1609. }
  1610. self?.rightSideViewController.isHidden = true
  1611. self?.listView.setShouAddEdit([])
  1612. self?.listView.change([.text, .image])
  1613. self?.listView.annotationType = .editTextImage
  1614. self?.closeRightPane()
  1615. }
  1616. } else {
  1617. if self?.listView.annotationType == .addImage ||
  1618. self?.listView.annotationType == .addText {
  1619. let textItem = self?.toolbarController.findItem(KMToolbarAddTextEditPDFItemIdentifier)
  1620. let imageItem = self?.toolbarController.findItem(KMToolbarAddImageEditPDFItemIdentifier)
  1621. textItem?.isSelected = false
  1622. imageItem?.isSelected = false
  1623. }
  1624. }
  1625. }
  1626. }
  1627. }
  1628. return event
  1629. }
  1630. }
  1631. func removeKeyEventMonitor() {
  1632. if (self.keyEventMonitor != nil) {
  1633. KMPrint("removeKeyEventMonitor 已移除事件监听")
  1634. NSEvent.removeMonitor(self.keyEventMonitor as Any)
  1635. self.keyEventMonitor = nil
  1636. }
  1637. }
  1638. private func removeEventMonitor() {
  1639. if (self.eventMonitor != nil) {
  1640. KMPrint("已移除事件监听")
  1641. NSEvent.removeMonitor(self.eventMonitor as Any)
  1642. self.eventMonitor = nil
  1643. }
  1644. }
  1645. // MARK: -
  1646. // MARK: Tools
  1647. func pdfViewCanHorizontalScroll() -> Bool {
  1648. let scroll = self.listView.scroll()
  1649. if (scroll == nil) {
  1650. return false
  1651. }
  1652. return scroll?.horizontalScroller?.isHidden == nil ? false : !(scroll!.horizontalScroller!.isHidden)
  1653. }
  1654. func pdfViewCanVerticalScroll() -> Bool {
  1655. let scroll = self.listView.scroll()
  1656. if (scroll == nil) {
  1657. return false
  1658. }
  1659. return scroll?.verticalScroller?.isHidden == nil ? false : !(scroll!.verticalScroller!.isHidden)
  1660. }
  1661. // MARK: - Public Methods
  1662. // 清理数据 [eg. 通知]
  1663. public func clearData() {
  1664. self.removeNotifications()
  1665. if (self.listView.spellingTag() > 0) {
  1666. NSSpellChecker.shared.closeSpellDocument(withTag: self.listView.spellingTag())
  1667. }
  1668. }
  1669. public func clearSecureOptions() {
  1670. self._secureOptions = nil
  1671. self.documentAttribute
  1672. }
  1673. public func recordRemoveSecureFlag() {
  1674. self._removeSecureFlag = true
  1675. self.clearSecureOptions()
  1676. self.recordIsPDFDocumentEdited(type: .removePassword)
  1677. self._needSave = true
  1678. }
  1679. public func clearRemoveSecureFlag() {
  1680. self._removeSecureFlag = false
  1681. }
  1682. public func recordSaveWatermarkFlag(type: KMSubscribeWaterMarkType = .none) {
  1683. // km_synchronized(self) {
  1684. // self._saveWatermarkFlag = true
  1685. // }
  1686. //
  1687. // if let _document = self.myDocument as? KMMainDocument {
  1688. // _document.recordTrackEvent(type: type)
  1689. // }
  1690. }
  1691. public func clearSaveWatermarkFlag() {
  1692. km_synchronized(self) {
  1693. self._saveWatermarkFlag = false
  1694. }
  1695. }
  1696. public func recordIsPDFDocumentEdited(type: KMSubscribeWaterMarkType = .none) {
  1697. km_synchronized(self) {
  1698. self._isPDFDocumentEdited = true
  1699. if let _document = self.myDocument {
  1700. KMTools.setDocumentEditedState(document: _document)
  1701. }
  1702. }
  1703. }
  1704. public func clearIsPDFDocumentEdited() {
  1705. km_synchronized(self) {
  1706. self._isPDFDocumentEdited = false
  1707. }
  1708. }
  1709. // MARK: - Noti Actions
  1710. internal func documentDidUnlockNotification(_ sender: Notification) {
  1711. if (self.listView.document != nil && self.listView.document.isEqual(to: sender.object)) {
  1712. self.loadAIIconView()
  1713. if (self.myDocument == nil) {
  1714. return
  1715. }
  1716. if (self.listView.document.allowsPrinting && self.listView.document.allowsCopying) {
  1717. self.hiddenSecureLimitTip()
  1718. }
  1719. if ((self.myDocument as! KMMainDocument).isUnlockFromKeychain || self.isSaveKeyChain == false) {
  1720. return
  1721. }
  1722. let type = KMPreferenceManager.shared.savePasswordType
  1723. if (type == .never) {
  1724. return
  1725. }
  1726. if (type == .always) {
  1727. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  1728. return
  1729. }
  1730. // 保存到钥匙串
  1731. let alert = NSAlert()
  1732. alert.messageText = NSLocalizedString("Remember Password?", comment: "")
  1733. alert.informativeText = NSLocalizedString("Do you want to save this password in your Keychain?", comment: "")
  1734. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  1735. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  1736. if (alert.runModal() == .alertFirstButtonReturn) { // 保存密码
  1737. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  1738. return
  1739. }
  1740. }
  1741. }
  1742. internal func applicationWillTerminateNotification(_ sender: Notification) {
  1743. self.savePageNumberIfNeed()
  1744. self.saveDocument()
  1745. }
  1746. func KMPDFViewCurrentPageDidChangedNotification(_ sender: Notification) {
  1747. if self.isReadMode {
  1748. self.readModelView.currentPageIndex = self.listView.currentPageIndex
  1749. }
  1750. //刷新前一页后一页按钮
  1751. self.updateNextAndPreViousButtonState()
  1752. }
  1753. func CPDFDocumentPageCountChangedNotification(_ sender: Notification) {
  1754. if self.isReadMode {
  1755. self.readModelView.totalPagesCount = Int(self.listView.document.pageCount)
  1756. }
  1757. //刷新前一页后一页按钮
  1758. self.updateNextAndPreViousButtonState()
  1759. }
  1760. func CEditPDFToolModeChangeStateUnkownNotification(_ sender: Notification) {
  1761. var editSelectd = false
  1762. if (self.listView.annotationType == .addText || self.listView.annotationType == .addImage) && self.listView.toolMode == .editPDFToolMode {
  1763. editSelectd = true
  1764. }
  1765. if self.listView.toolMode == .editPDFToolMode {
  1766. if editSelectd {
  1767. self.toolbarController.cancelSelected(KMToolbarAddTextEditPDFItemIdentifier)
  1768. }
  1769. }
  1770. }
  1771. @objc func handlePageChangedNotification(_ sender: Notification) {
  1772. // When the PDFView is changing scale, or when view settings change when switching fullscreen modes,
  1773. // a lot of wrong page change notifications may be send, which we better ignore.
  1774. // Full screen switching and zooming should not change the current page anyway.
  1775. if self.mwcFlags.isSwitchingFullScreen > 0 {
  1776. // if ([pdfView isZooming] || mwcFlags.isSwitchingFullScreen) {
  1777. // [self updatePageNumber];
  1778. // [self updateLeftStatus];
  1779. return
  1780. }
  1781. //
  1782. let page = self.listView.currentPage()
  1783. let pageIndex = page?.pageIndex() ?? 0
  1784. //
  1785. // if ([lastViewedPages count] == 0) {
  1786. // [lastViewedPages addPointer:(void *)pageIndex];
  1787. // } else if ((NSUInteger)[lastViewedPages pointerAtIndex:0] != pageIndex) {
  1788. // [lastViewedPages insertPointer:(void *)pageIndex atIndex:0];
  1789. // if ([lastViewedPages count] > 5)
  1790. // [lastViewedPages setCount:5];
  1791. // }
  1792. self.leftSideViewController.thumb_selectRowIndexsIfNeed(IndexSet(integer: IndexSet.Element(pageIndex)))
  1793. self.leftSideViewController.thumbnailTableView.needsDisplay = true
  1794. self.leftSideViewController.tocOutlineView.needsDisplay = true
  1795. //
  1796. // [self updatePageNumber];
  1797. // [self updatePageLabel];
  1798. //
  1799. // [self updateOutlineSelection];
  1800. // [self updateNoteSelection];
  1801. // [self updateThumbnailSelection];
  1802. //
  1803. // if (beforeMarkedPageIndex != NSNotFound && [[pdfView currentPage] pageIndex] != markedPageIndex)
  1804. // beforeMarkedPageIndex = NSNotFound;
  1805. //
  1806. // [self synchronizeWindowTitleWithDocumentName];
  1807. // [self updateLeftStatus];
  1808. // if ([[NSUserDefaults standardUserDefaults] boolForKey:SKDisplayPageBoundsKey])
  1809. // [self updateRightStatus];
  1810. // if ([self interactionMode] == SKPresentationMode)
  1811. // [[self presentationNotesDocument] setCurrentPage:[[[self presentationNotesDocument] pdfDocument] pageAtIndex:[page pageIndex]]];
  1812. }
  1813. @objc func handleDisplayBoxChangedNotification(_ sender: Notification) {
  1814. self.leftSideViewController.reloadThumbnailDataIfNeed()
  1815. // if ([[NSUserDefaults standardUserDefaults] boolForKey:SKDisplayPageBoundsKey])
  1816. // [self updateRightStatus];
  1817. }
  1818. @objc func willEnterInteractionModeNotification(_ sender: Notification) {
  1819. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1820. return
  1821. }
  1822. let interactionMode = sender.userInfo?[NSWindow.UserInfo.interactionModeKey] as? KMInteractionMode
  1823. if interactionMode == .presentation {
  1824. let backgroundColor = NSColor.black
  1825. let level = UserDefaults.standard.bool(forKey: "SKUseNormalLevelForPresentationKey") ? NSWindow.Level.normal : NSWindow.Level.popUpMenu
  1826. let page = self.listView.currentPage()
  1827. let wasInteractionMode = self.interactionMode
  1828. if wasInteractionMode == .normal {
  1829. self.savedNormalSetup.setDictionary(self.currentPDFSettings() as! [AnyHashable : Any])
  1830. }
  1831. // if findController.view().window() != nil {
  1832. // findController.toggleAboveView(nil, animate: false)
  1833. // }
  1834. if wasInteractionMode == .legacyFullScreen {
  1835. self.enterPresentationMode()
  1836. // updatePresentationOptions(for: self.view.window!)
  1837. self.pdfSplitView.frame = CGRect(x: 0, y: 0, width: CGRectGetWidth(centerContentView.bounds), height: CGRectGetHeight(centerContentView.bounds)-1)
  1838. self.centerContentView.addSubview(pdfSplitView)
  1839. self.listView.frame = (self.view.window?.contentView?.bounds)!
  1840. self.view.window?.contentView?.addSubview(listView)
  1841. self.view.window?.backgroundColor = backgroundColor
  1842. self.view.window?.level = level
  1843. self.listView.layoutDocumentView()
  1844. self.listView.requiresDisplay()
  1845. self.forceSubwindowsOnTop(false)
  1846. self.hideLeftSideWindow()
  1847. self.hideRightSideWindow()
  1848. self.removeBlankingWindows()
  1849. }
  1850. } else {
  1851. KMPrint("2")
  1852. }
  1853. }
  1854. @objc func didEnterInteractionModeNotification(_ sender: Notification) {
  1855. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1856. return
  1857. }
  1858. if self.interactionMode == .presentation {
  1859. // if _isShowToolbar {
  1860. // self.toolbarViewController.hiddenToolbar(true)
  1861. // }
  1862. //
  1863. // if self.pdfView().currentPage()?.isEqual(page) == false {
  1864. // self.pdfView().go(to: page)
  1865. // }
  1866. // pdfView().setInteractionMode(SKPresentationMode)
  1867. self.listView?.layoutDocumentView()
  1868. self.listView?.requiresDisplay()
  1869. }
  1870. }
  1871. @objc func willShowFullScreenNotification(_ sender: Notification) {
  1872. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1873. return
  1874. }
  1875. if self.interactionMode == .presentation {
  1876. let view = self.view.window?.firstResponder as? NSView
  1877. if let data = view?.isDescendant(of: self.pdfSplitView), data {
  1878. self.view.window?.makeFirstResponder(nil)
  1879. }
  1880. }
  1881. }
  1882. @objc func didShowFullScreenNotification(_ sender: Notification) {
  1883. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1884. return
  1885. }
  1886. if self.interactionMode == .presentation {
  1887. self.enterPresentationMode()
  1888. }
  1889. }
  1890. @objc func didAddContentViewNotification(_ sender: Notification) {
  1891. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  1892. return
  1893. }
  1894. if self.interactionMode == .presentation {
  1895. }
  1896. }
  1897. //MARK: - PDFListViewDelegate
  1898. func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
  1899. // KMPrint("pdfViewDocumentDidLoaded")
  1900. self.removeBackgroundMaskView()
  1901. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  1902. self.showSecureLimitTip()
  1903. }
  1904. if (self._documentFirstLoad) {
  1905. self.checkShouldAutoOpenLeftVC()
  1906. if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
  1907. let pageNumber = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document.documentURL.path)
  1908. let pageScale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document.documentURL.path)
  1909. if (pageScale != nil) {
  1910. self.listView.scaleFactor = CGFloat(pageScale!)
  1911. }
  1912. if (pageNumber != nil && pageNumber! >= 0 && pageNumber! < self.listView.document.pageCount) {
  1913. self.listView.go(toPageIndex: pageNumber!, animated: false)
  1914. } else {
  1915. self._goToFirstPageForFristAppear()
  1916. }
  1917. } else {
  1918. self._goToFirstPageForFristAppear()
  1919. }
  1920. self._documentFirstLoad = false
  1921. }
  1922. let notification = Notification(name: Notification.Name(rawValue: "pdfViewDocumentDidLoaded"))
  1923. self.preferenceDidChangeNotification(notification:notification)
  1924. let leftWidthNumber = UserDefaults.standard.object(forKey: CPDFOfficeLeftSidePaneWidthKey) as? NSNumber ?? NSNumber(value: panelWidth + functionWidth)
  1925. let rightWidthNumber = UserDefaults.standard.object(forKey: CPDFOfficeRightSidePaneWidthKey) as? NSNumber ?? NSNumber(value: defaultRightWidth)
  1926. applyLeftSideWidth(leftWidthNumber.doubleValue, rightSideWidth: rightWidthNumber.doubleValue)
  1927. self.updatePageIndicatoreType()
  1928. }
  1929. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  1930. self.updatePageIndicatoreType()
  1931. NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: self.document)
  1932. // KMPrint("KMPDFViewCurrentPageDidChanged")
  1933. }
  1934. func pdfViewScaleDidChanged(_ pdfView: CPDFView!) {
  1935. self.toolbarController.mainToolBarView?.zoomTextField.stringValue = "\(Int(self.listView.scaleFactor * 100))%"
  1936. self.updateZoomInOutButtonState()
  1937. // KMPrint("pdfViewScaleDidChanged")
  1938. }
  1939. func pdfViewDidClick(onLink pdfView: CPDFView!, withURL url: String!) {
  1940. if let urlString = url, urlString == kKMPurchaseProductURLString {
  1941. //跳转订阅比较表
  1942. let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
  1943. return
  1944. }
  1945. KMTools.openURL(urlString: url)
  1946. }
  1947. func pdfViewPerformURL(_ pdfView: CPDFView!, withContent content: String!) {
  1948. KMPrint("pdfViewPerformURL")
  1949. }
  1950. func pdfViewPerformPrint(_ pdfView: CPDFView!) {
  1951. KMPrint("pdfViewPerformPrint")
  1952. }
  1953. func pdfViewPerformGo(toPage pdfView: CPDFView!) {
  1954. KMPrint("pdfViewPerformGo")
  1955. }
  1956. func pdfViewOpenPDF(_ pdfView: CPDFView!, forRemoteGoTo action: CPDFAction!) {
  1957. KMPrint("pdfViewOpenPDF")
  1958. }
  1959. func pdfViewPerformReset(_ pdfView: CPDFView!) {
  1960. KMPrint("pdfViewPerformReset")
  1961. }
  1962. func pdfViewEditingBlockDidChanged(_ pdfView: CPDFView!) {
  1963. KMPrint("pdfViewEditingBlockDidChanged")
  1964. }
  1965. func pdfViewAsBookBookmark() -> NSImage! {
  1966. return NSImage(named: "KMImageNameUXIconPDFViewBookMark")!
  1967. }
  1968. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  1969. self.recordSaveWatermarkFlag()
  1970. if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddText {
  1971. self.rightSideViewController.eidtPDFTextProperty.reloadData()
  1972. self.rightSideViewController.eidtPDFTextProperty.updateTextTextPresuppositionState()
  1973. }
  1974. }
  1975. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  1976. let areas = self.listView.editingAreas()
  1977. if areas == nil || areas?.count ?? 0 == 0 {
  1978. if self.listView.toolMode == .editPDFToolMode {
  1979. if self.listView.annotationType == .addImage || self.listView.annotationType == .addText {
  1980. if self.listView.isEditImage {
  1981. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  1982. } else {
  1983. // if self.listView.annotationType == .addImage {
  1984. // self.closeRightPane()
  1985. // }
  1986. if self.listView.annotationType == .addImage {
  1987. if self.rightSideViewController.eidtPDFImageProperty != nil {
  1988. self.rightSideViewController.eidtPDFImageProperty.reloadData()
  1989. }
  1990. }
  1991. // self.openRightPane()
  1992. }
  1993. } else {
  1994. self.closeRightPane()
  1995. }
  1996. } else {
  1997. self.rightSideViewController.isHidden = true
  1998. self.closeRightPane()
  1999. if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddText && self.listView.annotationType == .addText {
  2000. self.rightSideViewController.eidtPDFTextProperty.initData()
  2001. }
  2002. }
  2003. if self.listView.isEdited() {
  2004. self.recordIsPDFDocumentEdited(type: .editText)
  2005. }
  2006. if self.listView.annotationType != .addText {
  2007. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kPDFViewEditingAreaDidChanged"), object: self.listView.document)
  2008. }
  2009. return
  2010. }
  2011. self.isPDFTextImageEdited = true
  2012. if (self.listView.annotationType == .addImage) && areas!.count > 0 {
  2013. var isImageArea = false
  2014. for i in 0 ... areas!.count-1 {
  2015. if areas![i] is CPDFEditImageArea {
  2016. isImageArea = true
  2017. }
  2018. }
  2019. if isImageArea {
  2020. // self.rightSideViewController.view.isHidden = false
  2021. self.rightSideViewController.isHidden = false
  2022. if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddImage {
  2023. self.rightSideViewController.subViewType = .EditPDFAddImage
  2024. self.rightSideViewController.eidtPDFImageProperty.reloadData()
  2025. }
  2026. self.openRightPane()
  2027. } else {
  2028. // self.rightSideViewController.view.isHidden = true
  2029. self.rightSideViewController.isHidden = true
  2030. self.closeRightPane()
  2031. }
  2032. } else if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddText && self.listView.annotationType == .addText {
  2033. // self.rightSideViewController.view.isHidden = false
  2034. self.rightSideViewController.isHidden = false
  2035. if self.listView.editingSelectionString().count != 0 {
  2036. self.rightSideViewController.eidtPDFTextProperty.reloadData()
  2037. } else {
  2038. self.rightSideViewController.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  2039. }
  2040. self.openRightPane()
  2041. } else {
  2042. var textsAreas : [CPDFEditTextArea] = []
  2043. var imagesAreas : [CPDFEditImageArea] = []
  2044. if self.listView.editingAreas()?.count ?? 0 < 1 {
  2045. return
  2046. }
  2047. for i in 0 ... areas!.count-1 {
  2048. if areas![i] is CPDFEditTextArea {
  2049. textsAreas.append(areas![i] as! CPDFEditTextArea)
  2050. }
  2051. if areas![i] is CPDFEditImageArea {
  2052. imagesAreas.append(areas![i] as! CPDFEditImageArea)
  2053. }
  2054. }
  2055. if textsAreas.count > 0 && textsAreas.count == areas!.count {
  2056. // self.rightSideViewController.view.isHidden = false
  2057. self.rightSideViewController.isHidden = false
  2058. self.rightSideViewController.subViewType = .EditPDFAddText
  2059. self.rightSideViewController.eidtPDFTextProperty?.reloadData()
  2060. self.openRightPane()
  2061. } else if imagesAreas.count > 0 {
  2062. // self.rightSideViewController.view.isHidden = false
  2063. self.rightSideViewController.isHidden = false
  2064. self.rightSideViewController.subViewType = .EditPDFAddImage
  2065. self.rightSideViewController.eidtPDFImageProperty?.reloadData()
  2066. self.openRightPane()
  2067. }
  2068. }
  2069. if self.listView.isEdited() {
  2070. self.recordIsPDFDocumentEdited(type: .editText)
  2071. if self.listView.annotationType != .addText {
  2072. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kPDFViewEditingAreaDidChanged"), object: self.listView.document)
  2073. }
  2074. }
  2075. }
  2076. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  2077. self.recordSaveWatermarkFlag()
  2078. if editArea != nil && (editArea is CPDFEditImageArea){
  2079. self.listView.cropAreas = editArea as? CPDFEditImageArea
  2080. }
  2081. }
  2082. //编辑PDF 创建图片区域回调
  2083. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  2084. self.recordSaveWatermarkFlag(type: .editImage)
  2085. if self.listView.isEditImage {
  2086. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  2087. } else {
  2088. let panel = NSOpenPanel()
  2089. panel.allowsMultipleSelection = false
  2090. panel.allowedFileTypes = ["png","jpg"]
  2091. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  2092. if response == .OK {
  2093. var filePath = panel.url?.path
  2094. var image = NSImage.init(contentsOf: panel.url!)
  2095. //图片自适应范围
  2096. if image != nil {
  2097. var imageRect = rect
  2098. let imageSize = image!.size
  2099. var previewSize = rect.size
  2100. var isChangeSize = false
  2101. if previewSize.width == 0 && previewSize.height == 0 {
  2102. previewSize = CGSize(width: 500, height: 500)
  2103. isChangeSize = true
  2104. }
  2105. let scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  2106. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  2107. if isChangeSize {
  2108. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  2109. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  2110. } else {
  2111. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  2112. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  2113. }
  2114. imageRect.size = newSize
  2115. let limitWidth = 1920.0
  2116. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  2117. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  2118. targetSize: CGSize(width: limitWidth, height: limitWidth),
  2119. maxSizeInBytes: 1024 * 1024 * 5,
  2120. targetCompression: 1.0)
  2121. }
  2122. //自适应page
  2123. let pageRect = self.listView.currentPage().bounds
  2124. if imageRect.width > pageRect.width ||
  2125. imageRect.height > pageRect.height {
  2126. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  2127. imageRect = CGRect(x: imageRect.origin.x,
  2128. y: imageRect.origin.y,
  2129. width: imageRect.width * pageScale,
  2130. height: imageRect.height * pageScale)
  2131. }
  2132. if imageRect.origin.x < 0 {
  2133. imageRect.origin.x = 5
  2134. }
  2135. if imageRect.origin.y < 0 {
  2136. imageRect.origin.y = 5
  2137. }
  2138. if imageRect.origin.x + imageRect.width > pageRect.width ||
  2139. imageRect.origin.y + imageRect.height > pageRect.height {
  2140. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  2141. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  2142. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  2143. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  2144. }
  2145. DispatchQueue.main.async {
  2146. self.listView.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  2147. self.isPDFTextImageEdited = true
  2148. self.recordIsPDFDocumentEdited(type: .editImage)
  2149. // self.asyncSaveDocument { params in
  2150. //
  2151. // }
  2152. }
  2153. }
  2154. }
  2155. }
  2156. }
  2157. }
  2158. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  2159. // print(rect)
  2160. var newrect = CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height)
  2161. if __CGSizeEqualToSize(rect.size, CGSize.zero) {
  2162. newrect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  2163. } else {
  2164. newrect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  2165. }
  2166. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .commonly)
  2167. let fontName = KMEditPDFTextManager.manager.fetchFontName(fontName: model.fontName)
  2168. let fontSize = model.fontSize
  2169. let fontColor = model.color
  2170. let fontAlign = model.alignment
  2171. let fontStyle = KMEditPDFTextManager.manager.fetchFontStyle(fontName: model.fontName)
  2172. NSColorPanel.shared.color = fontColor
  2173. let font = KMEditPDFTextManager.manager.fetchFont(fontName: fontName, style: fontStyle, size: fontSize)
  2174. let style = NSMutableParagraphStyle()
  2175. style.alignment = fontAlign
  2176. let attributes = [NSAttributedString.Key.font:font, NSAttributedString.Key.foregroundColor:fontColor,NSAttributedString.Key.paragraphStyle:style] as [NSAttributedString.Key : Any]
  2177. self.listView.createEmptyStringBounds(newrect,withAttributes: attributes as [NSAttributedString.Key : Any], page: page)
  2178. if self.rightSideViewController != nil && self.rightSideViewController.subViewType == .EditPDFAddText && self.listView.annotationType == .addText {
  2179. self.rightSideViewController.eidtPDFTextProperty.refreshSelectAreaProperty(needDefaultData: true)
  2180. }
  2181. // self.asyncSaveDocument { params in
  2182. //
  2183. // }
  2184. self.recordSaveWatermarkFlag(type: .editText)
  2185. }
  2186. // func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  2187. // debugPrint("pdfViewEditingDoubleClick")
  2188. // }
  2189. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  2190. debugPrint("pdfViewEditingOperationDidChanged")
  2191. if self.listView.isEdited() {
  2192. self.recordSaveWatermarkFlag()
  2193. }
  2194. }
  2195. // func pdfListViewEditAnnotation(_ pdfListView: CPDFListView!, for anotation: CPDFAnnotation!) {
  2196. // debugPrint("pdfListViewEditAnnotation")
  2197. // }
  2198. func pdfListViewKeyDownIsContinue(_ pdfListView: CPDFListView!, theEvent: NSEvent!) -> Bool {
  2199. let command = theEvent.modifierFlags.contains(.command)
  2200. let control = theEvent.modifierFlags.contains(.control)
  2201. KMPrint(theEvent.keyCode)
  2202. if (theEvent.keyCode == 11 && command) { // command + B [添加书签]
  2203. self.menuItemBookMarkClick_add(sender: NSMenuItem())
  2204. return false
  2205. } else if (command && control && theEvent.keyCode == 14) { // command + control + E [注释 橡皮擦]
  2206. return false
  2207. } else if (theEvent.keyCode == 123) { // 向左
  2208. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2209. return false
  2210. } else {
  2211. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage()) {
  2212. self.listView.goToPreviousPage(nil)
  2213. return false
  2214. }
  2215. }
  2216. } else if (theEvent.keyCode == 126) { // 向上
  2217. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2218. return false
  2219. } else {
  2220. if (self.listView.isContinousScroll()) {
  2221. return true
  2222. }
  2223. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToPreviousPage()) {
  2224. self.listView.goToPreviousPage(nil)
  2225. return false
  2226. }
  2227. }
  2228. } else if (theEvent.keyCode == 124) { // 向右
  2229. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2230. return false
  2231. } else {
  2232. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage()) {
  2233. self.listView.goToNextPage(nil)
  2234. return false
  2235. }
  2236. }
  2237. } else if (theEvent.keyCode == 125) { // 向下
  2238. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  2239. return false
  2240. } else {
  2241. if (self.listView.isContinousScroll()) {
  2242. return true
  2243. }
  2244. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToNextPage()) {
  2245. self.listView.goToNextPage(nil)
  2246. return false
  2247. }
  2248. }
  2249. } else if (theEvent.keyCode == 36) {
  2250. if self.listView.annotationType == .addImage || self.listView.annotationType == .addText {
  2251. if self.listView.isEditImage {
  2252. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  2253. }
  2254. }
  2255. }
  2256. if theEvent.keyCode == 53 {
  2257. if self.isReadMode {
  2258. self.closeReadModel()
  2259. }
  2260. self.leftSideViewCancelSelect()
  2261. if (self.toolbarController.toolbarType.isToolMode()) {
  2262. self.toolbarController.selectItem(self.toolbarController.toolbarType.itemIdentifier())
  2263. }
  2264. }
  2265. return true
  2266. }
  2267. func pdfListViewMenuValidate(_ pdfListView: CPDFListView!, menuItem: NSMenuItem!, isTakesEffect: UnsafeMutablePointer<ObjCBool>!) -> Bool {
  2268. guard let action = menuItem.action else {
  2269. isTakesEffect.pointee = false
  2270. return false
  2271. }
  2272. if (KMSystemMenu.isEditSelector(sel: action)) {
  2273. if (KMSystemMenu.Edit.deleteSelector == action) {
  2274. isTakesEffect.pointee = true
  2275. return self.listView.activeAnnotations.count > 0
  2276. } else if (KMSystemMenu.Edit.copySelector == action) {
  2277. isTakesEffect.pointee = true
  2278. return true//self.listView.canCopy()
  2279. } else if (KMSystemMenu.Edit.cutSelector == action) {
  2280. isTakesEffect.pointee = true
  2281. return self.listView.canCopy()
  2282. } else if (KMSystemMenu.Edit.pasteSelector == action) {
  2283. isTakesEffect.pointee = true
  2284. return self.listView.canPaste()
  2285. }
  2286. }
  2287. isTakesEffect.pointee = false
  2288. return false
  2289. }
  2290. //MARK: -CPDFListViewDelegate
  2291. func cPDFListView(_ pdfListView: CPDFListView, didDelete annotation: CPDFAnnotation, in pdfPage: CPDFPage) {
  2292. self.leftSideViewController.updateThumbnail(at: Int(pdfPage.pageIndex()))
  2293. }
  2294. func pdfListViewChangeatioActiveAnnotations(_ pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!, isRightMenu: Bool) {
  2295. self.view.window?.makeFirstResponder(self.listView)
  2296. if isRightMenu {
  2297. } else if annotations.count > 0 {
  2298. if annotations.count > 1 {
  2299. let fristAnnotation = annotations.first
  2300. var isSameAnnotation = true
  2301. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  2302. for annotation in annotations {
  2303. let cunrrentClassName = NSStringFromClass(annotation.classForCoder)
  2304. if (className == "CPDFSquareAnnotation") ||
  2305. (className == "CPDFCircleAnnotation") ||
  2306. (className == "CPDFLineAnnotation") {
  2307. if (cunrrentClassName != "CPDFSquareAnnotation") &&
  2308. (cunrrentClassName != "CPDFCircleAnnotation") &&
  2309. (cunrrentClassName != "CPDFLineAnnotation") {
  2310. isSameAnnotation = false
  2311. }
  2312. } else {
  2313. if className != cunrrentClassName {
  2314. isSameAnnotation = false
  2315. }
  2316. }
  2317. }
  2318. if isSameAnnotation == false {
  2319. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  2320. // self.closeRightPane()
  2321. } else {
  2322. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  2323. self.openRightPane()
  2324. }
  2325. } else {
  2326. let fristAnnotation = annotations.first
  2327. let className = NSStringFromClass(fristAnnotation!.classForCoder)
  2328. if self.isReadMode {
  2329. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  2330. self.closeRightPane()
  2331. } else {
  2332. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  2333. if className != "CPDFStampAnnotation" &&
  2334. className != "CPDFSignatureAnnotation" &&
  2335. className != "CPDFListStampAnnotation" {
  2336. self.openRightPane()
  2337. }
  2338. }
  2339. }
  2340. } else if (annotations.count == 0){
  2341. if pdfListView.annotationType == .unkown {
  2342. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  2343. self.closeRightPane()
  2344. } else {
  2345. if self.isReadMode {
  2346. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: false)
  2347. self.closeRightPane()
  2348. } else {
  2349. self.rightSideViewController?.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  2350. self.openRightPane()
  2351. }
  2352. }
  2353. }
  2354. }
  2355. func pdfListViewChangedAnnotationType(_ pdfListView: CPDFListView!, for annotationType: CAnnotationType) {
  2356. if(annotationType == .unkown) {
  2357. // self.rightSideViewController.view.isHidden = true
  2358. self.rightSideViewController.isHidden = true
  2359. self.closeRightPane()
  2360. }
  2361. }
  2362. ///开始定位链接注释
  2363. func pdfListViewLinkDestinationStart(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  2364. if self.locationPageView.superview == nil {
  2365. self.locationPageView.frame = CGRect(x: 0, y: pdfListView.frame.maxY-32, width: pdfListView.frame.width, height: 32)
  2366. pdfListView.addSubview(self.locationPageView)
  2367. }
  2368. }
  2369. ///刷新链接注释
  2370. func pdfListViewLinkDestinationEnd(_ pdfListView: CPDFListView!, withActiveAnnotation annotation: CPDFAnnotation!) {
  2371. if self.locationPageView.superview != nil {
  2372. self.locationPageView.removeFromSuperview()
  2373. }
  2374. if self.rightSideViewController.subViewType == .AnnotationProperts && pdfListView.annotationType == .link {
  2375. self.rightSideViewController.reloadDataWithPDFView(pdfView: pdfListView, isShow: true)
  2376. }
  2377. }
  2378. func pdfListViewMenuItemsEditing(at point: CGPoint, for page: CPDFPage!, menuItems: [NSMenuItem]!) -> [NSMenuItem]! {
  2379. if (listView.toolMode != CToolMode.editPDFToolMode) {
  2380. return menuItems
  2381. }
  2382. var tMenuItems = menuItems;
  2383. if(listView.isSelectEditCharRange() ||
  2384. listView.isSelecteditArea(with: point)) {
  2385. tMenuItems?.append(NSMenuItem.separator())
  2386. // tMenuItems?.append(self.fontColorMenuItem())
  2387. // tMenuItems?.append(self.fontSizeMenuItem())
  2388. }
  2389. let areas = self.listView.editingAreas() ?? []
  2390. if areas.count == 1 {
  2391. let fristAreas = areas.first
  2392. if fristAreas is CPDFEditImageArea {
  2393. self.listView.selectImageAreas = fristAreas as? CPDFEditImageArea
  2394. if self.listView.isEditImage {
  2395. tMenuItems?.removeAll()
  2396. tMenuItems?.append(self.corpImageMenuItem())
  2397. tMenuItems?.append(self.cancelCorpImageMenuItem())
  2398. tMenuItems?.append(self.restoreCorpImageMenuItem())
  2399. } else {
  2400. tMenuItems?.append(NSMenuItem.separator())
  2401. tMenuItems?.append(self.cutImageArea())
  2402. tMenuItems?.append(self.replaceImageArea())
  2403. tMenuItems?.append(self.exportImageArea())
  2404. }
  2405. } else {
  2406. tMenuItems?.swapAt(0, 1)
  2407. }
  2408. } else if areas.count == 0 {
  2409. tMenuItems?.append(NSMenuItem.separator())
  2410. tMenuItems?.append(self.addText())
  2411. tMenuItems?.append(self.addImage())
  2412. }
  2413. return tMenuItems
  2414. }
  2415. func tableMenu(_ menu: NSMenu, withTable table: KMTableAnnotation, point: CGPoint) -> NSMenu {
  2416. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  2417. 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"]
  2418. let actions = ["formAnnotTextEdit:", "", "addRowAbove:", "addRowBelow:", "", "addColumnBefore:", "addColumnAfter:", "", "deleteRow:", "deleteColumn:", "deleteTabel", "cutCell:", "copyCell:", "pasteCell:", "pasteAndMatchStyle:", "deleteCellContents:", "clearAll:"]
  2419. for i in 0..<itemTitles.count {
  2420. var item: NSMenuItem? = nil
  2421. if itemTitles[i] == "" {
  2422. item = NSMenuItem.separator()
  2423. menu.insertItem(item!, at: i)
  2424. } else {
  2425. item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  2426. item!.target = self
  2427. item!.action = NSSelectorFromString(actions[i])
  2428. if itemTitles[i] == "Paste" /*&& !_copyCellData*/ {
  2429. item!.action = nil
  2430. } else if itemTitles[i] == "Paste and Match Style" /*&& !_copyCellData */{
  2431. item!.action = nil
  2432. } else if itemTitles[i] == "Add Row Above" {
  2433. let path1 = table.crossLines[table.rowNumber - table.currentCell.row]
  2434. let path2 = table.crossLines[table.rowNumber - table.currentCell.row - 1]
  2435. if path1.lineJoinStyle == NSBezierPath.LineJoinStyle.round && table.headerCount() >= 5 {
  2436. item!.action = nil
  2437. } else if path2.lineJoinStyle == NSBezierPath.LineJoinStyle.bevel && table.footerCount() >= 5 {
  2438. item!.action = nil
  2439. }
  2440. }
  2441. item!.title = NSLocalizedString(item!.title, comment: "")
  2442. item!.representedObject = NSValue(point: point)
  2443. menu.insertItem(item!, at: i)
  2444. }
  2445. }
  2446. } else {
  2447. let itemTitles = ["Cut", "Copy", "Paste", "Delete"]
  2448. let actions = ["cut:", "copy:", "paste:", "delete:"]
  2449. for i in 0..<itemTitles.count {
  2450. let item = NSMenuItem(title: itemTitles[i], action: nil, keyEquivalent: "")
  2451. item.target = self
  2452. item.action = NSSelectorFromString(actions[i])
  2453. item.title = NSLocalizedString(item.title, comment: "")
  2454. menu.insertItem(item, at: i)
  2455. item.representedObject = NSValue(point: point)
  2456. }
  2457. }
  2458. return menu
  2459. }
  2460. func pdfListViewMenu(forEvent pdfListView: CPDFListView!, for theEvent: NSEvent!, click menu: AutoreleasingUnsafeMutablePointer<NSMenu?>!, isMoveSelectAnno: Bool) {
  2461. self.mouseRightMenuEvent = theEvent
  2462. var currentMenu : NSMenu = menu.pointee!
  2463. if let activeAnno = listView.activeAnnotation as? KMTableAnnotation {//Table
  2464. var pagePoint = NSPoint()
  2465. _ = self.listView.pageAndPoint(&pagePoint, for: theEvent, nearest: true)
  2466. currentMenu.removeAllItems()
  2467. let annotation = activeAnno
  2468. annotation.completeEditCellText()
  2469. if !(NSIsEmptyRect(annotation.drawRect)) {
  2470. annotation.drawLine(point: pagePoint)
  2471. NotificationCenter.default.post(name: NSNotification.Name.KMPDFViewTableAnnotationDidChange, object: self, userInfo: ["point": NSValue(point: pagePoint)])
  2472. }
  2473. if (annotation.rowNumber - annotation.currentCell.row - 1) < 0 {
  2474. return
  2475. }
  2476. currentMenu = tableMenu(currentMenu, withTable: listView.activeAnnotation as! KMTableAnnotation, point: pagePoint)
  2477. listView.needsDisplay = true
  2478. return
  2479. }
  2480. if (listView.toolMode == .selectToolMode){
  2481. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2482. currentMenu.insertItem(self.printingMenu(), at: 3)
  2483. currentMenu.insertItem(self.setTTSStype(), at: 3)
  2484. currentMenu.insertItem(self.setCropStype(), at: 3)
  2485. currentMenu.insertItem(self.setSnapshotStype(), at: 3)
  2486. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  2487. export.submenu = self.exportMenu()
  2488. currentMenu.insertItem(export, at: 3)
  2489. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2490. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  2491. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2492. return
  2493. }
  2494. if (listView.toolMode == .moveToolMode || listView.toolMode == .magnifyToolMode){
  2495. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2496. currentMenu.insertItem(self.setTTSStype(), at: 0)
  2497. currentMenu.insertItem(self.setCropStype(), at: 0)
  2498. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  2499. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2500. currentMenu.insertItem(self.addOutlineStype(), at: 0)
  2501. currentMenu.insertItem(self.addBookmarkMenu(), at: 0)
  2502. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2503. currentMenu.insertItem(self.setAnnotationToolStype(), at: 0)
  2504. return
  2505. }
  2506. if currentMenu.items.count > 3 {
  2507. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 3)
  2508. currentMenu.insertItem(self.addReadModelStype(), at: currentMenu.items.count - 3)
  2509. }
  2510. if listView.currentSelection != nil{
  2511. if listView.currentSelection.selectionType() == .text {
  2512. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2513. currentMenu.insertItem(self.setSearchBaiduStype(), at: 3)
  2514. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2515. currentMenu.insertItem(self.setLookUpStype(), at: 3)
  2516. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2517. currentMenu.insertItem(self.addOutlineStype(), at: 3)
  2518. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2519. currentMenu.insertItem(self.setAnnotationToolStype(), at: 3)
  2520. currentMenu.insertItem(self.setTTSStype(), at: 3)
  2521. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2522. currentMenu.insertItem(self.setShareStype(), at: 3)
  2523. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2524. }
  2525. currentMenu.insertItem(self.enterAnnotationStype(), at: 3)
  2526. currentMenu.insertItem(NSMenuItem.separator(), at: 3)
  2527. if listView.currentSelection.selectionType() == .image{
  2528. currentMenu.insertItem(self.setCutStype(), at: 1)
  2529. currentMenu.insertItem(self.setDeleteStype(), at: 4)
  2530. currentMenu.insertItem(NSMenuItem.separator(), at: 6)
  2531. currentMenu.insertItem(self.setEditNoteStype(), at: 6)
  2532. currentMenu.insertItem(self.setRotateStype(), at: 6)
  2533. currentMenu.insertItem(self.setLinesStype(), at: 6)
  2534. currentMenu.insertItem(self.setColorsStype(), at: 6)
  2535. }
  2536. if listView.currentSelection.selectionType() == .text {
  2537. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2538. currentMenu.insertItem(self.setTranslateStype(), at: currentMenu.items.count)
  2539. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2540. // currentMenu.insertItem(self.setServicesStype(), at: currentMenu.items.count)
  2541. }
  2542. }
  2543. if listView.activeAnnotation != nil || isMoveSelectAnno {
  2544. if let data = self.listView?.activeAnnotation?.type?.lowercased(), data == "stamp"{
  2545. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2546. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  2547. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2548. }else{
  2549. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2550. currentMenu.insertItem(self.enterAnnotationStype(), at: currentMenu.items.count - 15)
  2551. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2552. currentMenu.insertItem(self.setAnnotationToolStype(), at: currentMenu.items.count - 15)
  2553. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2554. currentMenu.insertItem(self.addBookmarkMenu(), at: currentMenu.items.count - 15)
  2555. currentMenu.insertItem(self.addOutlineStype(), at: currentMenu.items.count - 15)
  2556. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2557. currentMenu.insertItem(self.setSnapshotStype(), at: currentMenu.items.count - 15)
  2558. currentMenu.insertItem(self.setCropStype(), at: currentMenu.items.count - 15)
  2559. currentMenu.insertItem(self.setTTSStype(), at: currentMenu.items.count - 15)
  2560. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count - 15)
  2561. }
  2562. }
  2563. if listView.activeAnnotation == nil && listView.currentSelection == nil{
  2564. currentMenu.insertItem(NSMenuItem.separator(), at: currentMenu.items.count)
  2565. if(listView.toolMode == .selectToolMode) {
  2566. if NSIsEmptyRect(listView.currentSelectionRect()) {
  2567. currentMenu.insertItem(self.zoomSelectionMenuItem(), at: 0)
  2568. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2569. }
  2570. currentMenu.insertItem(self.printingMenu(), at: 0)
  2571. currentMenu.insertItem(self.setTTSStype(), at: 0)
  2572. currentMenu.insertItem(self.setCropStype(), at: 0)
  2573. currentMenu.insertItem(self.setSnapshotStype(), at: 0)
  2574. let export = NSMenuItem(title: NSLocalizedString("Export", comment: ""), action: nil, target: self)
  2575. export.submenu = self.exportMenu()
  2576. currentMenu.insertItem(export, at: currentMenu.items.count)
  2577. }else{
  2578. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2579. currentMenu.insertItem(self.setTTSStype(), at: 2)
  2580. currentMenu.insertItem(self.setCropStype(), at: 2)
  2581. currentMenu.insertItem(self.setSnapshotStype(), at: 2)
  2582. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2583. currentMenu.insertItem(self.addOutlineStype(), at: 2)
  2584. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2585. currentMenu.insertItem(self.enterAnnotationStype(), at: 2)
  2586. currentMenu.insertItem(NSMenuItem.separator(), at: 2)
  2587. if(currentMenu.items.count > 4) {
  2588. currentMenu.insertItem(NSMenuItem.separator(), at: 5)
  2589. }
  2590. if(currentMenu.items.count > 5) {
  2591. currentMenu.insertItem(self.addBookmarkMenu(), at: 6)
  2592. }
  2593. // if (currentMenu.items.count > 6 && self.isReadMode) {
  2594. // currentMenu.removeItem(currentMenu.item(withTitle: "Zoom")!)
  2595. // }
  2596. // currentMenu.insertItem(self.findStringMenu(), at: currentMenu.items.count)
  2597. currentMenu.insertItem(self.setAutoScrollStype(), at: currentMenu.items.count)
  2598. }
  2599. currentMenu.insertItem(self.setAnnotationToolStype(), at: 5)
  2600. }
  2601. currentMenu.insertItem(NSMenuItem.separator(), at: 0)
  2602. currentMenu.insertItem(self.setAITranslateStype(), at: 0)
  2603. currentMenu.insertItem(self.setAIProofreadStype(), at: 0)
  2604. currentMenu.insertItem(self.setAIRewriteStype(), at: 0)
  2605. for item in currentMenu.items {
  2606. if (item.action == NSSelectorFromString("menuItemClick_HidenorShowNote:")) {
  2607. // 显示与隐藏注释 item action 截取
  2608. item.action = #selector(menuItemClick_HidenorShowNote)
  2609. item.target = self
  2610. break
  2611. }
  2612. }
  2613. }
  2614. func pdfListViewAddAnnotations(_ pdfListView: CPDFListView!, forAdd annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  2615. var addRedact = false
  2616. var saveWatermark = false
  2617. var saveWatermarkType: KMSubscribeWaterMarkType = .none
  2618. for anno in annotations {
  2619. if (anno.isKind(of: CPDFRedactAnnotation.self)) {
  2620. addRedact = true
  2621. // break
  2622. } else if (anno.isKind(of: CPDFLinkAnnotation.self)) { // link 注释
  2623. saveWatermark = true
  2624. saveWatermarkType = .link
  2625. } else if (anno.isKind(of: CPDFListStampAnnotation.self)) { // 图章注释
  2626. saveWatermark = true
  2627. saveWatermarkType = .stamp
  2628. } else if (anno.isKind(of: CPDFListSignatureAnnotation.self)) { // 签名注释
  2629. saveWatermark = true
  2630. saveWatermarkType = .sign
  2631. }
  2632. }
  2633. if (saveWatermark) {
  2634. self.recordSaveWatermarkFlag(type: saveWatermarkType)
  2635. }
  2636. self.hasAddRedact = addRedact
  2637. if self.isReadMode || self.listView.toolMode == .moveToolMode {
  2638. self.listView.toolMode = .textToolMode
  2639. self.listView.annotationType = .unkown
  2640. self.toolbarController.toolbarType = .Annatiton
  2641. }
  2642. if (self.rightMouseEventing) {
  2643. self.rightMouseEventing = false
  2644. if (self.toolbarController.ignoreCurrentAnnotationTypeChange && self.listView.annotationType == .ink) {
  2645. self.listView.toolMode = .textToolMode
  2646. self.listView.annotationType = .unkown
  2647. }
  2648. }
  2649. self.toolbarController.ignoreCurrentAnnotationTypeChange = false
  2650. self.leftSideViewController.updateThumbnail(at: Int(pdfPage?.pageIndex() ?? 0))
  2651. }
  2652. func pdfListViewDidSelectionEnd(_ pdfListView: CPDFListView!) {
  2653. if (!self.listView.isEqual(to: pdfListView)) {
  2654. return
  2655. }
  2656. if (self.listView.toolMode != .selectToolMode) {
  2657. return
  2658. }
  2659. if (self.topTipBox.isHidden || self.topTipBox.contentView?.subviews.count == 0) {
  2660. return
  2661. }
  2662. let tipView = self.topTipBox.contentView?.subviews.first
  2663. if (tipView?.isKind(of: KMCropTipView.self) == false) {
  2664. return
  2665. }
  2666. (tipView as! KMCropTipView).setString(string: "请按 Enter 键确定裁剪区域")
  2667. }
  2668. func pdfListViewKeyDowClosePanel(_ speedy: CPDFViewSidebarSpeedMode, event theEvent: NSEvent!) {
  2669. if(speedy == .right) {
  2670. self.toggleRightPane()
  2671. } else if (speedy == .left) {
  2672. self.menuItemAction_hiddenLeftSide(speedy)
  2673. }
  2674. }
  2675. func pdfListViewEventMarkupColor(with annotation: CPDFAnnotation!) -> [NSColor]! {
  2676. if (annotation.isKind(of: CPDFMarkupAnnotation.self)) {
  2677. if (annotation as! CPDFMarkupAnnotation).markupType() == .highlight {
  2678. return KMAnnotationPropertiesColorManager.manager.markHighlightColors
  2679. } else {
  2680. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  2681. }
  2682. } else {
  2683. return KMAnnotationPropertiesColorManager.manager.markOtherColors
  2684. }
  2685. }
  2686. func pdfListViewHaveDocumentAttribute() -> Bool {
  2687. if(!self.listView.document.allowsCopying) {
  2688. self.removeOwnerPassword()
  2689. return false
  2690. }
  2691. return true
  2692. }
  2693. func addTopTip(_ view: NSView?) {
  2694. if (Thread.isMainThread) {
  2695. if (view == nil) {
  2696. let contentView: NSView = self.topTipBox.contentView!
  2697. for subview in contentView.subviews {
  2698. subview.removeFromSuperview()
  2699. }
  2700. self.topTipBox.isHidden = true
  2701. return
  2702. }
  2703. let contentView: NSView = self.topTipBox.contentView!
  2704. for subview in contentView.subviews {
  2705. subview.removeFromSuperview()
  2706. }
  2707. self.topTipBox.isHidden = false
  2708. self.topTipBox.contentView?.addSubview(view!)
  2709. } else {
  2710. DispatchQueue.main.async {
  2711. if (view == nil) {
  2712. let contentView: NSView = self.topTipBox.contentView!
  2713. for subview in contentView.subviews {
  2714. subview.removeFromSuperview()
  2715. }
  2716. self.topTipBox.isHidden = true
  2717. return
  2718. }
  2719. let contentView: NSView = self.topTipBox.contentView!
  2720. for subview in contentView.subviews {
  2721. subview.removeFromSuperview()
  2722. }
  2723. self.topTipBox.isHidden = false
  2724. self.topTipBox.contentView?.addSubview(view!)
  2725. }
  2726. }
  2727. }
  2728. func pdfListView(_ sender: CPDFListView!, showSnapshotAtPageNumber pageNum: Int, for rect: NSRect, scaleFactor: CGFloat, autoFits: Bool) {
  2729. let swc = KMSnapshotWindowController(windowNibName: "SnapshotWindow")
  2730. swc.delegate = self
  2731. swc.setPdfDocument(self.listView.document, goToPageNumber: pageNum, rect: rect, scaleFactor: scaleFactor, autoFits: autoFits)
  2732. swc.forceOnTop = self.interactionMode != .normal
  2733. self.myDocument?.addWindowController(swc)
  2734. }
  2735. // MARK: Split View
  2736. func changePDFDocument(isChange: Bool, replaceBlock: @escaping (String) -> Void) {
  2737. let openPanel = NSOpenPanel()
  2738. openPanel.allowedFileTypes = ["pdf", "PDF"]
  2739. openPanel.allowsMultipleSelection = false
  2740. guard let mainWindow = NSApp.mainWindow else {
  2741. return
  2742. }
  2743. openPanel.beginSheetModal(for: mainWindow) { [weak self] response in
  2744. if response == NSApplication.ModalResponse.OK {
  2745. guard let url = openPanel.url else {
  2746. return
  2747. }
  2748. if let document = CPDFDocument(url: url) {
  2749. self?.secondaryPdfView?.document = nil
  2750. self?.secondaryPdfView?.document = document
  2751. if isChange {
  2752. self!.openSecondaryPdfView!.view.removeFromSuperview()
  2753. }
  2754. replaceBlock(document.documentURL?.path ?? "")
  2755. } else {
  2756. let alert = NSAlert()
  2757. alert.alertStyle = .critical
  2758. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  2759. alert.runModal()
  2760. }
  2761. }
  2762. }
  2763. }
  2764. }
  2765. // MARK: - CPDFDocumentDelegate
  2766. extension KMMainViewController: CPDFDocumentDelegate {
  2767. func documentDidBeginDocumentFind(_ document: CPDFDocument!) {
  2768. self.leftSideViewController.documentDidBeginFind()
  2769. // [statusBar setProgressIndicatorStyle:SKProgressIndicatorBarStyle];
  2770. // [[statusBar progressIndicator] setMaxValue:[[note object] pageCount]];
  2771. // [[statusBar progressIndicator] setDoubleValue:0.0];
  2772. // [statusBar startAnimation:self];
  2773. // [self willChangeValueForKey:SEARCHRESULTS_KEY];
  2774. // [self willChangeValueForKey:GROUPEDSEARCHRESULTS_KEY];
  2775. }
  2776. func documentDidEndDocumentFind(_ document: CPDFDocument!) {
  2777. self.leftSideViewController.documentDidEndFind()
  2778. // [self didChangeValueForKey:GROUPEDSEARCHRESULTS_KEY];
  2779. // [self didChangeValueForKey:SEARCHRESULTS_KEY];
  2780. // [statusBar stopAnimation:self];
  2781. // [statusBar setProgressIndicatorStyle:SKProgressIndicatorNone];
  2782. // NSArray *highlights = [[NSArray alloc] initWithArray:searchResults copyItems:YES];
  2783. // [highlights setValue:[NSColor yellowColor] forKey:@"color"];
  2784. // [self.pdfView setHighlightedSelections:highlights];
  2785. // [highlights release];
  2786. }
  2787. }
  2788. extension KMMainViewController: KMEditImagePropertyViewControllerDelegate {
  2789. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) {
  2790. self.isPDFTextImageEdited = true
  2791. }
  2792. }
  2793. extension KMMainViewController: KMSnapshotWindowControllerDelegate {
  2794. func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
  2795. self.leftSideViewController.snapshotControllerWillClose(controller)
  2796. }
  2797. func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
  2798. if isMiniaturize && self.interactionMode != .presentation {
  2799. if self.interactionMode != .legacyFullScreen && self.leftPanelOpen == false {
  2800. self.toggleLeftPane()
  2801. } else if self.interactionMode == .legacyFullScreen {
  2802. // else if ([self interactionMode] == SKLegacyFullScreenMode && ([rightSideWindow state] == NSDrawerClosedState || [rightSideWindow state] == NSDrawerClosingState)) {
  2803. // [rightSideWindow expand];
  2804. // [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hideRightSideWindow:) userInfo:NULL repeats:NO];
  2805. }
  2806. var row = NSNotFound
  2807. for (i,sp) in self.leftSideViewController.snapshots.enumerated() {
  2808. if controller.isEqual(sp.windowC) {
  2809. row = i
  2810. }
  2811. }
  2812. if (row != NSNotFound) {
  2813. self.leftSideViewController.leftView.segmentedControl.selectedSegment = 3
  2814. self.leftSideViewController.snapshotTableView.scrollRowToVisible(row)
  2815. }
  2816. }
  2817. return self.leftSideViewController.snapshotController(controller, miniaturizedRect: isMiniaturize)
  2818. }
  2819. func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
  2820. self.leftSideViewController.snapshotControllerDidFinishSetup(controller)
  2821. }
  2822. }