KMBrowserWindowController.swift 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  1. //
  2. // KMBrowserWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/9.
  6. //
  7. import Cocoa
  8. @objcMembers class KMBrowserWindowController: CTBrowserWindowController {
  9. var rightSegmentControl: KMSegmentedBox?
  10. var modeType: KMHomeToolState = .Home
  11. var filterType: Int = 0
  12. var mainViewController: NSViewController?
  13. var isShowingTokenTimeOutAlert: Bool = false
  14. var currentTimer: Timer?
  15. private(set) var isMultiTabMode: Bool = false
  16. var rightMessageVC: KMVerificationMessageViewController!
  17. var rightToolbarItemView: NSView!
  18. private var homeVC: KMHomeViewController?
  19. var savedNormalSetup: NSMutableDictionary = NSMutableDictionary()
  20. // 记录当前显示模式
  21. // var _mwcFlags: MwcFlags = MwcFlags()
  22. deinit {
  23. KMPrint("KMBrowserWindowController deinit...")
  24. // self.removeObserverForAppearanceChange()
  25. DistributedNotificationCenter.default().removeObserver(self)
  26. NotificationCenter.default.removeObserver(self)
  27. }
  28. override func loadWindow() {
  29. super.loadWindow()
  30. // self.window?.titlebarAppearsTransparent = true
  31. }
  32. override func windowDidLoad() {
  33. super.windowDidLoad()
  34. NSImage.makeTextAlignImages()
  35. NSImage.makeAdornImages()
  36. window?.backgroundColor = NSColor.km_init(hex: "#DFE1E5")
  37. self.window?.appearance = NSApp.appearance
  38. self.window?.titlebarAppearsTransparent = true
  39. // self.window?.standardWindowButton(.closeButton)?.superview?.needsLayout = true
  40. // window?.setFrameAutosaveName("")
  41. rightTabStripView_.delete = self
  42. homeRightTabStripView_.delete = self
  43. rightTabStripView_.updateView()
  44. fileUploadPanel.delete = self
  45. homeRightTabStripView_.homeRightSearchField.delegate = self
  46. isMultiTabMode = true
  47. // self.addObserverForAppearanceChange()
  48. refreshToolBar(homeToolState: .Home)
  49. NotificationCenter.default.addObserver(self, selector: #selector(closeAllTabs(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseAllTabs"), object: nil)
  50. NotificationCenter.default.addObserver(self, selector: #selector(openNewWindow(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerOpenNewWindow"), object: nil)
  51. DistributedNotificationCenter.default().addObserver(self, selector: #selector(_themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil)
  52. // if (KMWelcomeWindowController.welcomeHasShow() == false) {
  53. // //AI 版本禁掉首次开启帮助文档
  54. //// KMTools.openQuickStartStudy()
  55. //
  56. // DispatchQueue.main.async {
  57. // let welcome = KMWelcomeWindowController()
  58. // self.km_beginSheet(windowC: welcome)
  59. // welcome.itemClick = { [weak self] idx, param in
  60. // if (idx == 1) { // 关闭
  61. // self?.km_endSheet()
  62. // } else if (idx == 2) { // 以后提醒
  63. // self?.km_endSheet()
  64. // } else if (idx == 3) { // 注册
  65. // self?.km_endSheet()
  66. //
  67. // guard let window_ = self?.window else {
  68. // return
  69. // }
  70. // _ = KMLoginWindowController.show(window: window_, .Batch, .register)
  71. // }
  72. // }
  73. // }
  74. // }
  75. updateViewColor()
  76. var tabbingMode = NSWindow.TabbingMode.automatic
  77. let openDocumentType = KMPreference.shared.openDocumentType
  78. if openDocumentType == .inSameWindow {
  79. tabbingMode = .preferred
  80. } else if openDocumentType == .newWindow {
  81. tabbingMode = .disallowed
  82. }
  83. #if VERSION_FREE
  84. if IAPProductsManager.default().isAvailableAllFunction() == false {
  85. tabbingMode = .disallowed
  86. }
  87. #endif
  88. // self.window?.tabbingMode = tabbingMode
  89. let windowSizeOption = KMPreferenceManager.shared.openFileType
  90. // if (hasWindowSetup) {
  91. // NSString *rectString = [savedNormalSetup objectForKey:MAINWINDOWFRAME_KEY];
  92. // if (rectString)
  93. // [window setFrame:NSRectFromString(rectString) display:NO];
  94. // } else
  95. if (windowSizeOption == .maxim) {
  96. Task {
  97. self.window?.setFrame(NSScreen.main?.visibleFrame ?? (self.window?.frame ?? .zero), display: false)
  98. }
  99. }
  100. // }
  101. if windowSizeOption == .fit {
  102. self.performFit(nil)
  103. }
  104. }
  105. @objc func closeAction() {
  106. self.window?.performClose(nil)
  107. }
  108. override func windowShouldClose(_ sender: NSWindow) -> Bool {
  109. if self.browser.tabStripModel.count() > 1 {
  110. self.browser.windowDidBeginToClose()
  111. return false
  112. }
  113. return true
  114. }
  115. override func windowWillClose(_ notification: Notification) {
  116. // iRate.sharedInstance().appLaunched()
  117. }
  118. override func layoutSubviews() {
  119. super.layoutSubviews()
  120. // if let button = (self.window as? CTBrowserWindow)?.standardWindowButton(.closeButton) {
  121. // button.frame = NSMakeRect(button.frame.minX, 0, button.frame.width, button.frame.height)
  122. // }
  123. // if let button = (self.window as? CTBrowserWindow)?.standardWindowButton(.zoomButton) {
  124. // button.frame = NSMakeRect(button.frame.minX, 0, button.frame.width, button.frame.height)
  125. // }
  126. // if let button = (self.window as? CTBrowserWindow)?.standardWindowButton(.miniaturizeButton) {
  127. // button.frame = NSMakeRect(button.frame.minX, 0, button.frame.width, button.frame.height)
  128. // }
  129. }
  130. // MARK: Dark&Light
  131. // func addObserverForAppearanceChange() -> Void {
  132. // window?.contentView?.addObserver(self, forKeyPath: "effectiveAppearance", options: .new, context: nil)
  133. // }
  134. // func removeObserverForAppearanceChange() -> Void {
  135. // window?.contentView?.removeObserver(self, forKeyPath: "effectiveAppearance")
  136. // }
  137. // override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  138. // if keyPath == "effectiveAppearance" {
  139. // self.updateViewColor()
  140. // }
  141. // }
  142. @objc private func _themeChanged(_ sender: Notification) {
  143. if let data = self.window?.appearance?.name, data == .darkAqua {
  144. self.window?.appearance = .init(named: .aqua)
  145. } else {
  146. self.window?.appearance = .init(named: .darkAqua)
  147. }
  148. self.window?.contentView?.appearance = self.window?.appearance ?? .init(named: .aqua)
  149. self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  150. }
  151. override func updateViewColor() {
  152. super.updateViewColor()
  153. window?.backgroundColor = KMTabAppearance.tabsViewBackgroundColor()
  154. }
  155. // MARK: Public Methods
  156. func refreshToolBar(homeToolState toolState: KMHomeToolState) -> Void {
  157. modeType = toolState
  158. let document = browser.activeTabContents()
  159. if document != nil {
  160. if document!.isHome {
  161. homeRightTabStripView_.isHidden = modeType == .Home
  162. } else {
  163. self.homeRightStripView.isHidden = true
  164. }
  165. } else {
  166. self.homeRightStripView.isHidden = true
  167. }
  168. homeRightTabStripView_.homeRefreshButton.isEnabled = true
  169. homeRightTabStripView_.homeUploadButton.isEnabled = true
  170. filterType = 0
  171. rightTabStripView_.sortPopUpButton.removeAllItems()
  172. rightTabStripView_.filterPopUpButton.removeAllItems()
  173. rightTabStripView_.updateView()
  174. }
  175. override var hasToolbar: Bool {
  176. get {
  177. return false
  178. }
  179. }
  180. func showSnapshots(setups: NSArray?) {
  181. let doc = self.document as? KMMainDocument
  182. doc?.mainViewController?.showSnapshots(setups: setups)
  183. }
  184. var km_browser: KMBrowser? {
  185. get {
  186. return self.browser as? KMBrowser
  187. }
  188. }
  189. // MARK: Private Methods
  190. func openDocumentWindow() -> Void {
  191. // let panel = NSOpenPanel()
  192. // panel.allowsMultipleSelection = false
  193. // panel.allowedFileTypes = ["pdf"]
  194. // panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  195. // if response == .OK {
  196. // let openPath = panel.url?.path
  197. // let selectDocument = self.selectTabContents(path: openPath!)
  198. // if selectDocument != nil {
  199. // let currentIndex = selectDocument!.browser.tabStripModel.index(of: selectDocument)
  200. // self.browser.tabStripModel.selectTabContents(at: Int32(currentIndex), userGesture: true)
  201. // return
  202. // }
  203. // if !panel.url!.path.isPDFValid() {
  204. // let alert = NSAlert()
  205. // alert.alertStyle = .critical
  206. // alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  207. // alert.runModal()
  208. // return
  209. // }
  210. // NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { pdfDocument, documentWasAlreadyOpen, error in
  211. // print("openDocumentWindow")
  212. // }
  213. // }
  214. // }
  215. // MARK TODO: + 号开启
  216. // self.browser.addHomeTabContents()
  217. self.tabStripView?.addTabButton?.layer?.backgroundColor = .clear
  218. self.km_browser?.addNewTabContents()
  219. }
  220. // override func openDocument(_ sender: Any!) {
  221. // self.openDocumentWindow()
  222. // }
  223. func selectTabContents(path filePath: String) -> KMMainDocument? {
  224. let document = NSDocumentController.shared.document(for: URL(fileURLWithPath: filePath))
  225. var selectDocument: KMMainDocument?
  226. if document != nil {
  227. if document!.isKind(of: KMMainDocument.self) {
  228. selectDocument = document as! KMMainDocument
  229. }
  230. }
  231. return selectDocument
  232. }
  233. // MARK: Getter
  234. override var rightStripView: NSView! {
  235. get {
  236. if self.rightToolbarItemView == nil {
  237. self.rightToolbarItemView = NSView.init(frame: CGRectMake(0, 10, 184, 40))
  238. self.rightToolbarItemView.wantsLayer = true
  239. if self.rightMessageVC == nil {
  240. self.rightMessageVC = KMVerificationMessageViewController.init()
  241. }
  242. self.rightMessageVC.frameUpdateHandle = { [weak self] rect in
  243. var superRect = self?.rightToolbarItemView?.frame
  244. superRect?.size.width = (self?.rightMessageVC.view.frame.size.width)! + 8
  245. self?.rightToolbarItemView.frame = superRect!;
  246. }
  247. var rect = self.rightMessageVC.view.frame;
  248. rect.origin.x = CGRectGetWidth(self.rightToolbarItemView.frame) - rect.size.width - 8
  249. rect.origin.y = (CGRectGetHeight(self.rightToolbarItemView.frame) - rect.size.height)/2
  250. self.rightMessageVC.view.frame = rect
  251. self.rightMessageVC.view.autoresizingMask = [ .maxXMargin]
  252. self.rightToolbarItemView.addSubview(self.rightMessageVC.view)
  253. }
  254. return self.rightToolbarItemView
  255. }
  256. }
  257. override var homeRightStripView: NSView! {
  258. get {
  259. return homeRightTabStripView_
  260. }
  261. }
  262. // MARK: Button Action
  263. func commandDispatch(_ sender: Any) -> Void {
  264. var needShowChooseWindow = false
  265. //#if VERSION_FREE
  266. //#if VERSION_DMG
  267. if (!IAPProductsManager.default().isAvailableAllFunction()) {
  268. needShowChooseWindow = true
  269. }
  270. //#endif
  271. if needShowChooseWindow {
  272. let preferenceNoteShow = UserDefaults.standard.bool(forKey: KMTabbingHintShowFlag)
  273. if preferenceNoteShow {
  274. menuItemAction_newTagPageToNewWindow("")
  275. } else {
  276. if !KMDataManager.default.isTabbingWin{
  277. KMDataManager.default.isTabbingWin = true
  278. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  279. tabbingWin.selectCallBack = {[weak self] continueOrNot in
  280. KMDataManager.default.isTabbingWin = false
  281. if continueOrNot {
  282. self?.reopenDocument(forPaths: nil)
  283. } else {
  284. }
  285. }
  286. self.km_beginSheet(windowC: tabbingWin)
  287. }
  288. }
  289. } else {
  290. openDocumentWindow()
  291. }
  292. }
  293. func reopenDocument(forPaths path: String?) -> Void {
  294. if path == nil {
  295. let browser = KMBrowser.init() as KMBrowser
  296. browser.windowController = KMBrowserWindowController.init(browser: browser)
  297. browser.addHomeTabContents()
  298. browser.windowController.showWindow(self)
  299. }else {
  300. NSDocumentController.shared.km_safe_openDocument(withContentsOf: URL(fileURLWithPath: path!), display: true) { doc, open, err in
  301. }
  302. }
  303. }
  304. func closeAllTabs(_ sender: Any) -> Void {
  305. if self.browser != nil {
  306. self.browser.closeAllTabs()
  307. }
  308. }
  309. func openNewWindow(_ sender: Any) -> Void {
  310. if let noti = sender as? NSNotification, noti.object is CTTabController {
  311. if let window_ = (noti.object as! CTTabController).view.window, window_.isEqual(to: self.window) == false {
  312. return
  313. }
  314. }
  315. if self.browser != nil {
  316. let activeInt = self.browser.activeTabIndex()
  317. let activeTab = self.browser.activeTabContents()
  318. self.browser.closeTab(at: activeInt, makeHistory: false)
  319. let browser: KMBrowser = KMBrowser.init()
  320. browser.windowController = KMBrowserWindowController.init(browser: browser)
  321. browser.addHomeTabContents()
  322. browser.windowController.showWindow(self)
  323. // browser.add(activeTab, inForeground: false)
  324. // browser.selectTab(at: 1)
  325. if activeTab?.fileURL == nil {
  326. return
  327. }
  328. let pdfDoc = CPDFDocument.init(url: (activeTab?.fileURL)!)
  329. let document = NSDocumentController.shared.document(for: (activeTab?.fileURL)!)
  330. KMMainDocument().tryToUnlockDocument(pdfDoc!)
  331. if ((pdfDoc?.isLocked)! == true) {
  332. KMPasswordInputWindow.openWindow(window: self.window!, url: (activeTab?.fileURL)!) { result, password in
  333. if result == .cancel { /// 关闭
  334. return
  335. }
  336. /// 解密成功
  337. var selectDocument: KMMainDocument? = nil
  338. if ((document?.isKind(of: KMMainDocument.self)) != nil) {
  339. selectDocument = (document as! KMMainDocument)
  340. }
  341. if selectDocument != nil {
  342. let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument)
  343. selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true)
  344. if (selectDocument?.browser.window.isVisible)! as Bool {
  345. selectDocument?.browser.window.orderFront(nil)
  346. } else if (selectDocument?.browser.window.isMiniaturized)! as Bool {
  347. selectDocument?.browser.window.orderFront(nil)
  348. }
  349. } else {
  350. NSDocumentController.shared.km_safe_openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { document, _, error in
  351. if (error == nil) {
  352. (document as! KMMainDocument).mainViewController?.password = password
  353. }
  354. }
  355. }
  356. }
  357. } else {
  358. var selectDocument: KMMainDocument? = nil
  359. if ((document?.isKind(of: KMMainDocument.self)) != nil) {
  360. selectDocument = (document as! KMMainDocument)
  361. }
  362. if selectDocument != nil {
  363. let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument)
  364. selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true)
  365. if (selectDocument?.browser.window.isVisible)! as Bool {
  366. selectDocument?.browser.window.orderFront(nil)
  367. } else if (selectDocument?.browser.window.isMiniaturized)! as Bool {
  368. selectDocument?.browser.window.orderFront(nil)
  369. }
  370. } else {
  371. NSDocumentController.shared.km_safe_openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { _, _, _ in
  372. }
  373. }
  374. }
  375. }
  376. }
  377. func canMergeAllWindow() -> Bool {
  378. return self.fetchBrowserWindowControllers().count > 0
  379. }
  380. fileprivate func fetchBrowserWindowControllers() -> [KMBrowserWindowController] {
  381. var windowArray: [KMBrowserWindowController] = []
  382. for window in NSApp.windows {
  383. guard let windowController = window.windowController, windowController is KMBrowserWindowController else {
  384. continue
  385. }
  386. if let _tabScripC = (windowController as? KMBrowserWindowController)?.tabStripController, _tabScripC.viewsCount() <= 1 {
  387. continue
  388. }
  389. if (window.isVisible && (windowController.isEqual(to: self) == false)) {
  390. windowArray.append(windowController as! KMBrowserWindowController)
  391. }
  392. }
  393. return windowArray
  394. }
  395. func mergeAllWindow(_ sender: Any) {
  396. let windowArray = self.fetchBrowserWindowControllers()
  397. if (windowArray.count <= 0) {
  398. return
  399. }
  400. for windowC in windowArray {
  401. // 拼接窗口 [拼接窗口里的标签,除了home标签]
  402. self.append(windowC, toTabView: NSView())
  403. if let browser = windowC.browser {
  404. if (browser.tabCount() <= 1 && browser.activeTabContents().isHome) {
  405. windowC.close()
  406. }
  407. }
  408. }
  409. }
  410. override func tabDidSelect(_ notification: Notification!) {
  411. super.tabDidSelect(notification)
  412. if let _userInfo = notification.userInfo, let _newContents = _userInfo[CTTabNewContentsUserInfoKey] as? KMMainDocument {
  413. _newContents.addWindowController(self)
  414. }
  415. }
  416. override func layoutTabContentArea(_ frame: NSRect) {
  417. super.layoutTabContentArea(frame)
  418. self.rightStripView.autoresizingMask = [.minXMargin, .minYMargin]
  419. }
  420. override func openDocument(_ sender: Any!) {
  421. guard let _window = self.window else {
  422. KMPrint("openDocument error: window is nil.", beep: true)
  423. return
  424. }
  425. if self.browser.tabCount() > 1 && !IAPProductsManager.default().isAvailableAllFunction() {
  426. if !KMDataManager.default.isTabbingWin{
  427. KMDataManager.default.isTabbingWin = true
  428. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  429. tabbingWin.selectCallBack = { [weak self] continueOrNot in
  430. KMDataManager.default.isTabbingWin = false
  431. if continueOrNot {
  432. self?.reopenDocument(forPaths: nil)
  433. } else {
  434. }
  435. }
  436. self.km_beginSheet(windowC: tabbingWin)
  437. }
  438. return
  439. }
  440. NSOpenPanel.km_open_multi_success(_window) { panel in
  441. panel.allowedFileTypes = KMTools.imageExtensions + KMTools.pdfExtensions
  442. } completion: { urls in
  443. var imageUrls: [URL] = []
  444. for url in urls {
  445. if KMTools.isPDFType(url.pathExtension.lowercased()) {
  446. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url, display: true) { _, _, _ in
  447. }
  448. } else {
  449. imageUrls.append(url)
  450. }
  451. }
  452. if imageUrls.isEmpty == false {
  453. NSApplication.ShowImageToPDFWindow(urls: imageUrls)
  454. }
  455. }
  456. }
  457. }
  458. // MARK: - KMInterfaceThemeChangedProtocol
  459. extension KMBrowserWindowController: KMInterfaceThemeChangedProtocol {
  460. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  461. Task { @MainActor in
  462. self.updateViewColor()
  463. let selector = #selector(interfaceThemeDidChanged)
  464. let mainWindow = self.window
  465. var responder = mainWindow?.firstResponder
  466. while (responder != nil) {
  467. // KMPrint("res: \(responder)")
  468. //
  469. if self.isEqual(to: responder) {
  470. responder = responder?.nextResponder
  471. continue
  472. }
  473. if let res = responder?.responds(to: selector), res {
  474. responder?.perform(selector, with: appearance)
  475. }
  476. responder = responder!.nextResponder
  477. }
  478. }
  479. }
  480. }
  481. // MARK: - KMToolbarRightViewDelegate
  482. extension KMBrowserWindowController: KMToolbarRightViewDelegate {
  483. func pdfRightSegmentedControlAction(_ sender: KMSegmentedBox?) {
  484. self.trackEvent_upgrade()
  485. // KMComparativeTableViewController.show(window: self.window!, .upgrade)
  486. DispatchQueue.main.async {
  487. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  488. }
  489. }
  490. func userInfoButtonAction(_ sender: NSButton) {
  491. if KMLightMemberManager.manager.isLogin() {
  492. Task { @MainActor in
  493. if await KMLightMemberManager.manager.canUseAdvanced(needNetworking: true) {
  494. KMAccountInfoWindowController.show(window: self.window!)
  495. } else {
  496. KMLoginWindowController.show(window: self.window!)
  497. }
  498. }
  499. } else {
  500. KMLoginWindowController.show(window: self.window!)
  501. }
  502. }
  503. func homeRefreshButtonAction(_ sender: NSButton?) {
  504. self.layoutSubviews()
  505. }
  506. func homeUploadButtonAction(_ sender: NSButton) {
  507. }
  508. func homeMenuSortAction(_ sender: NSPopUpButton) {
  509. }
  510. func homeMenuFilterAction(_ sender: NSPopUpButton) {
  511. }
  512. }
  513. // MARK: - KMUploadFileDelegate
  514. extension KMBrowserWindowController: KMUploadFileDelegate {
  515. // override func cancelOperation(_ sender: Any?) {
  516. //
  517. // }
  518. }
  519. // MARK: - NSSearchFieldDelegate
  520. extension KMBrowserWindowController: NSSearchFieldDelegate {
  521. func controlTextDidChange(_ obj: Notification) {
  522. }
  523. func controlTextDidEndEditing(_ obj: Notification) {
  524. }
  525. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  526. var result = false
  527. return result
  528. }
  529. }
  530. // MARK: -
  531. // MARK: Menu Actions
  532. extension KMBrowserWindowController {
  533. func canResponseDocumentAction() -> Bool {
  534. if (self.browser == nil) {
  535. return false
  536. }
  537. guard let _ = self.browser.activeTabContents() as? KMMainDocument else {
  538. return false
  539. }
  540. return true
  541. }
  542. }
  543. // MARK: -
  544. // MARK: File Menu
  545. extension KMBrowserWindowController: NSMenuDelegate, NSMenuItemValidation {
  546. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  547. if (self.browser == nil) {
  548. return false
  549. }
  550. guard let action = menuItem.action else {
  551. return false
  552. }
  553. guard let document = self.browser.activeTabContents() as? KMMainDocument else {
  554. return false
  555. }
  556. if (KMSystemMenu.isFileSelector(sel: action)) {
  557. if action == KMSystemMenu.File.screenShotSelectAreaSelector ||
  558. action == KMSystemMenu.File.screenShotWindowSelector ||
  559. action == KMSystemMenu.File.screenShotFullScreenSelector ||
  560. action == KMSystemMenu.File.screenShotFullScreenDelaySelector {
  561. return true
  562. }
  563. if (document.isHome) {
  564. if (menuItem.action == KMSystemMenu.File.closeTagPageSelector ||
  565. menuItem.action == KMSystemMenu.File.propertySelector ||
  566. menuItem.action == KMSystemMenu.File.showInFinderSelector ||
  567. menuItem.action == KMSystemMenu.File.printSelector ||
  568. menuItem.action == KMSystemMenu.File.saveAsFlattenedPDFSelector) {
  569. return false
  570. }
  571. return document.homeViewController!.validateMenuItem(menuItem)
  572. } else {
  573. return document.mainViewController!.validateMenuItem(menuItem)
  574. }
  575. }
  576. if (KMSystemMenu.isEditSelector(sel: action)) {
  577. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  578. }
  579. if (KMSystemMenu.isViewSelector(sel: action)) {
  580. if action == KMSystemMenu.Edit.changeFontSelector {
  581. return true
  582. } else {
  583. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  584. }
  585. }
  586. if (KMSystemMenu.isAnnotationSelector(sel: action)) {
  587. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  588. }
  589. if (KMSystemMenu.isPDFSelector(sel: action)) {
  590. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  591. }
  592. if (KMSystemMenu.isGotoSelector(sel: action)) {
  593. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  594. }
  595. if (KMSystemMenu.isToolSelector(sel: action)) {
  596. if action == KMSystemMenu.Tool.orderFrontNotesPanelSelector {
  597. if KMNotesPanelController.sharedControllerExists() {
  598. if let data = KMNotesPanelController.shared.window?.isVisible, data {
  599. menuItem.title = NSLocalizedString("Hide Note Type", comment: "Menu item title")
  600. } else {
  601. menuItem.title = NSLocalizedString("Show Note Type", comment: "Menu item title")
  602. }
  603. } else {
  604. menuItem.title = NSLocalizedString("Show Note Type", comment: "Menu item title")
  605. }
  606. return true
  607. } else {
  608. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  609. }
  610. }
  611. if (KMSystemMenu.isWindowSelector(sel: action)) {
  612. return document.isHome ? document.homeViewController!.validateMenuItem(menuItem) : document.mainViewController!.validateMenuItem(menuItem)
  613. }
  614. if action == #selector(redactAction) {
  615. return document.isHome ? false : true
  616. }
  617. return true
  618. }
  619. }
  620. extension KMBrowserWindowController {
  621. @IBAction func menuItemAction_openFile(_ sender: Any) {
  622. super.openDocument(sender)
  623. }
  624. @IBAction func importFromFile(_ sender: Any) {
  625. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  626. if (document.isHome) {
  627. document.homeViewController?.importFromFile(sender)
  628. } else {
  629. self.homeVC = KMHomeViewController()
  630. self.homeVC?.importFromFile(sender)
  631. }
  632. }
  633. @IBAction func openBlankPage(_ sender: Any) {
  634. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  635. if (document.isHome) {
  636. document.homeViewController?.openBlankPage()
  637. } else {
  638. self.homeVC = KMHomeViewController()
  639. self.homeVC?.openBlankPage()
  640. }
  641. }
  642. @IBAction func importFromScanner(_ sender: Any) {
  643. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  644. if (document.isHome) {
  645. document.homeViewController?.importFromScanner()
  646. } else {
  647. self.homeVC = KMHomeViewController()
  648. self.homeVC?.importFromScanner()
  649. }
  650. }
  651. @IBAction func menuItemAction_newTab(_ sender: Any) {
  652. self.openDocumentWindow()
  653. }
  654. @IBAction func pasteAsPlainText(_ sender: Any?) {
  655. KMPrint("pasteAsPlainText")
  656. }
  657. @IBAction func deselectAll(_ sender: Any?) {
  658. KMPrint("deselectAll")
  659. }
  660. @IBAction func searchPDF(_ sender: Any?) {
  661. if let document = self.browser.activeTabContents() as? KMMainDocument {
  662. if (document.isHome) {
  663. } else {
  664. document.mainViewController?.leftSideViewController.showSearchMode("")
  665. }
  666. }
  667. }
  668. @IBAction func changeColor(_ sender: Any?) {
  669. KMPrint("changeColor ...")
  670. }
  671. @IBAction func changeAttributes(_ sender: Any?) {
  672. KMPrint("changeAttributes ...")
  673. }
  674. @IBAction func alignLeft(_ sender: Any?) {
  675. KMPrint("alignLeft ...")
  676. }
  677. @IBAction func alignRight(_ sender: Any?) {
  678. KMPrint("alignRight ...")
  679. }
  680. @IBAction func alignCenter(_ sender: Any?) {
  681. KMPrint("alignCenter ...")
  682. }
  683. @IBAction func createNewNote(_ sender: Any?) {
  684. KMPrint("createNewNote ...")
  685. }
  686. @IBAction func toggleHideNotes(_ sender: Any?) {
  687. KMPrint("toggleHideNotes ...")
  688. }
  689. @IBAction func changeDisplaySinglePages(_ sender: Any?) {
  690. KMPrint("changeDisplaySinglePages ...")
  691. }
  692. @IBAction func changeDisplayContinuous(_ sender: Any?) {
  693. KMPrint("changeDisplayContinuous ...")
  694. }
  695. @IBAction func doGoToNextPage(_ sender: Any?) {
  696. KMPrint("doGoToNextPage ...")
  697. }
  698. @IBAction func doGoToPreviousPage(_ sender: Any?) {
  699. KMPrint("doGoToPreviousPage ...")
  700. }
  701. @IBAction func doGoToFirstPage(_ sender: Any?) {
  702. KMPrint("doGoToFirstPage ...")
  703. }
  704. @IBAction func doGoToLastPage(_ sender: Any?) {
  705. KMPrint("doGoToLastPage ...")
  706. }
  707. @IBAction func allGoToNextPage(_ sender: Any?) {
  708. KMPrint("allGoToNextPage ...")
  709. }
  710. @IBAction func allGoToPreviousPage(_ sender: Any?) {
  711. KMPrint("allGoToPreviousPage ...")
  712. }
  713. @IBAction func allGoToFirstPage(_ sender: Any?) {
  714. KMPrint("allGoToFirstPage ...")
  715. }
  716. @IBAction func allGoToLastPage(_ sender: Any?) {
  717. KMPrint("allGoToLastPage ...")
  718. }
  719. @IBAction func doGoToPage(_ sender: Any?) {
  720. KMPrint("doGoToPage ...")
  721. }
  722. @IBAction func doGoBack(_ sender: Any?) {
  723. KMPrint("doGoBack ...")
  724. }
  725. @IBAction func doGoForward(_ sender: Any?) {
  726. KMPrint("doGoForward ...")
  727. }
  728. @IBAction func doZoomToPhysicalSize(_ sender: Any?) {
  729. KMPrint("doZoomToPhysicalSize ...")
  730. }
  731. @IBAction func alternateZoomToFit(_ sender: Any?) {
  732. KMPrint("alternateZoomToFit ...")
  733. }
  734. @IBAction func doZoomToSelection(_ sender: Any?) {
  735. KMPrint("doZoomToSelection ...")
  736. }
  737. @IBAction func doAutoScale(_ sender: Any?) {
  738. KMPrint("doAutoScale ...")
  739. }
  740. // @IBAction func delete(_ sender: Any?) {
  741. // KMPrint("delete ...")
  742. // }
  743. // @IBAction func paste(_ sender: Any?) {
  744. // KMPrint("paste ...")
  745. // }
  746. @IBAction func alternatePaste(_ sender: Any?) {
  747. KMPrint("alternatePaste ...")
  748. }
  749. @IBAction func copy(_ sender: Any?) {
  750. KMPrint("copy ...")
  751. }
  752. @IBAction func cut(_ sender: Any?) {
  753. KMPrint("cut ...")
  754. }
  755. @IBAction func toggleFullscreen(_ sender: Any?) {
  756. KMPrint("toggleFullscreen ...")
  757. }
  758. @IBAction func toggleRightSidePane(_ sender: Any?) {
  759. KMPrint("toggleRightSidePane ...")
  760. }
  761. @IBAction func changeRightSidePaneState(_ sender: Any?) {
  762. KMPrint("changeRightSidePaneState ...")
  763. }
  764. @IBAction func changeFindPaneState(_ sender: Any?) {
  765. KMPrint("changeFindPaneState ...")
  766. }
  767. @IBAction func toggleSplitPDF(_ sender: Any?) {
  768. KMPrint("toggleSplitPDF ...")
  769. }
  770. @IBAction func search(_ sender: Any?) {
  771. KMPrint("search ...")
  772. }
  773. @IBAction func searchNotes(_ sender: Any?) {
  774. KMPrint("searchNotes ...")
  775. }
  776. @IBAction func password(_ sender: Any?) {
  777. KMPrint("password ...")
  778. }
  779. @IBAction func toggleCaseInsensitiveSearch(_ sender: Any?) {
  780. KMPrint("toggleCaseInsensitiveSearch ...")
  781. }
  782. @IBAction func toggleWholeWordSearch(_ sender: Any?) {
  783. KMPrint("toggleWholeWordSearch ...")
  784. }
  785. @IBAction func toggleCaseInsensitiveNoteSearch(_ sender: Any?) {
  786. KMPrint("toggleCaseInsensitiveNoteSearch ...")
  787. }
  788. @IBAction func performFindPanelAction(_ sender: NSMenuItem) {
  789. var forward = true
  790. var findString: String?
  791. switch sender.tag {
  792. case 1:
  793. if let document = self.browser.activeTabContents() as? KMMainDocument {
  794. if document.isHome == false {
  795. document.mainViewController?.toolbarController.showFindBar()
  796. }
  797. }
  798. case 3:
  799. if let document = self.browser.activeTabContents() as? KMMainDocument {
  800. if document.isHome == false {
  801. document.mainViewController?.toolbarController.showFindString(forward: false)
  802. }
  803. }
  804. case 2:
  805. if let document = self.browser.activeTabContents() as? KMMainDocument {
  806. if document.isHome == false {
  807. document.mainViewController?.toolbarController.showFindString(forward: true)
  808. }
  809. }
  810. case 7:
  811. if let document = self.browser.activeTabContents() as? KMMainDocument {
  812. if document.isHome == false {
  813. document.mainViewController?.toolbarController.showAllAction()
  814. }
  815. }
  816. default: break
  817. }
  818. }
  819. @IBAction func scrollUp(_ sender: Any?) {
  820. KMPrint("scrollUp ...")
  821. }
  822. @IBAction func scrollDown(_ sender: Any?) {
  823. KMPrint("scrollDown ...")
  824. }
  825. @IBAction func scrollRight(_ sender: Any?) {
  826. KMPrint("scrollRight ...")
  827. }
  828. @IBAction func scrollLeft(_ sender: Any?) {
  829. KMPrint("scrollLeft ...")
  830. }
  831. @IBAction func selectSelectedNote(_ sender: Any?) {
  832. KMPrint("selectSelectedNote ...")
  833. }
  834. @IBAction func goToSelectedOutlineItem(_ sender: Any?) {
  835. KMPrint("goToSelectedOutlineItem ...")
  836. }
  837. @IBAction func goToSelectedFindResults(_ sender: Any?) {
  838. KMPrint("goToSelectedFindResults ...")
  839. }
  840. @IBAction func toggleSelectedSnapshots(_ sender: Any?) {
  841. KMPrint("toggleSelectedSnapshots ...")
  842. }
  843. @IBAction func selfSignProfile(_ sender: Any?) {
  844. KMPrint("selfSignProfile ...")
  845. }
  846. @IBAction func link(_ sender: Any?) {
  847. KMPrint("link ...")
  848. }
  849. @IBAction func insertPage(_ sender: Any?) {
  850. KMPrint("insertPage ...")
  851. }
  852. @IBAction func split(_ sender: Any?) {
  853. KMPrint("split ...")
  854. }
  855. @IBAction func comparison(_ sender: Any?) {
  856. KMPrint("comparison ...")
  857. }
  858. @IBAction func contentComparison(_ sender: Any?) {
  859. KMPrint("contentComparison ...")
  860. }
  861. @IBAction func coverageComparison(_ sender: Any?) {
  862. KMPrint("coverageComparison ...")
  863. }
  864. @IBAction func readMode(_ sender: Any?) {
  865. KMPrint("readMode ...")
  866. }
  867. @IBAction func changeFormType(_ sender: Any?) {
  868. KMPrint("changeFormType ...")
  869. }
  870. @IBAction func changeSelfSignType(_ sender: Any?) {
  871. KMPrint("changeSelfSignType ...")
  872. }
  873. @IBAction func clearAllForm(_ sender: Any?) {
  874. KMPrint("clearAllForm ...")
  875. }
  876. @IBAction func thumbnailSizeScaling(_ sender: Any?) {
  877. KMPrint("thumbnailSizeScaling ...")
  878. }
  879. @IBAction func redactAction(_ sender: Any?) {
  880. if (self.canResponseDocumentAction() == false) {
  881. return
  882. }
  883. if let document = self.browser.activeTabContents() as? KMMainDocument {
  884. if document.isHome == false {
  885. document.mainViewController?.menuItemAction_redact(sender)
  886. }
  887. }
  888. }
  889. }
  890. // MARK: - KMSystemFileMenuProtocol
  891. extension KMBrowserWindowController: KMSystemFileMenuProtocol {
  892. func menuItemClick_saveAsFlattenedPDF(_ sender: Any) {
  893. if (self.canResponseDocumentAction() == false) {
  894. return
  895. }
  896. if let document = self.browser.activeTabContents() as? KMMainDocument {
  897. if (document.isHome) {
  898. } else {
  899. document.mainViewController?.menuItemClick_saveAsFlattenedPDF(sender)
  900. }
  901. }
  902. }
  903. func menuItemClick_mergePDF(_ sender: Any) {
  904. if (self.canResponseDocumentAction() == false) {
  905. return
  906. }
  907. if let document = self.browser.activeTabContents() as? KMMainDocument {
  908. if (document.isHome) {
  909. document.homeViewController?.menuItemClick_mergePDF(sender)
  910. } else {
  911. document.mainViewController?.menuItemClick_mergePDF(sender)
  912. }
  913. }
  914. }
  915. func menuItemClick_Compress(_ sender: Any) {
  916. if (self.canResponseDocumentAction() == false) {
  917. return
  918. }
  919. if let document = self.browser.activeTabContents() as? KMMainDocument {
  920. if (document.isHome) {
  921. document.homeViewController?.menuItemClick_Compress(sender)
  922. } else {
  923. document.mainViewController?.menuItemClick_Compress(sender)
  924. }
  925. }
  926. }
  927. func menuItemAction_ConvertToWord(_ sender: Any) {
  928. if (self.canResponseDocumentAction() == false) {
  929. return
  930. }
  931. if let document = self.browser.activeTabContents() as? KMMainDocument {
  932. if (document.isHome) {
  933. document.homeViewController?.menuItemAction_ConvertToWord(sender)
  934. } else {
  935. document.mainViewController?.menuItemAction_ConvertToWord(sender)
  936. }
  937. }
  938. }
  939. func menuItemAction_ConvertToExcel(_ sender: Any) {
  940. if (self.canResponseDocumentAction() == false) {
  941. return
  942. }
  943. if let document = self.browser.activeTabContents() as? KMMainDocument {
  944. if (document.isHome) {
  945. document.homeViewController?.menuItemAction_ConvertToExcel(sender)
  946. } else {
  947. document.mainViewController?.menuItemAction_ConvertToExcel(sender)
  948. }
  949. }
  950. }
  951. func menuItemAction_ConvertToPPT(_ sender: Any) {
  952. if (self.canResponseDocumentAction() == false) {
  953. return
  954. }
  955. if let document = self.browser.activeTabContents() as? KMMainDocument {
  956. if (document.isHome) {
  957. document.homeViewController?.menuItemAction_ConvertToPPT(sender)
  958. } else {
  959. document.mainViewController?.menuItemAction_ConvertToPPT(sender)
  960. }
  961. }
  962. }
  963. func menuItemAction_ConvertToRTF(_ sender: Any) {
  964. if (self.canResponseDocumentAction() == false) {
  965. return
  966. }
  967. if let document = self.browser.activeTabContents() as? KMMainDocument {
  968. if (document.isHome) {
  969. document.homeViewController?.menuItemAction_ConvertToRTF(sender)
  970. } else {
  971. document.mainViewController?.menuItemAction_ConvertToRTF(sender)
  972. }
  973. }
  974. }
  975. func menuItemAction_ConvertToHTML(_ sender: Any) {
  976. if (self.canResponseDocumentAction() == false) {
  977. return
  978. }
  979. if let document = self.browser.activeTabContents() as? KMMainDocument {
  980. if (document.isHome) {
  981. document.homeViewController?.menuItemAction_ConvertToHTML(sender)
  982. } else {
  983. document.mainViewController?.menuItemAction_ConvertToHTML(sender)
  984. }
  985. }
  986. }
  987. func menuItemAction_ConvertToText(_ sender: Any) {
  988. if (self.canResponseDocumentAction() == false) {
  989. return
  990. }
  991. if let document = self.browser.activeTabContents() as? KMMainDocument {
  992. if (document.isHome) {
  993. document.homeViewController?.menuItemAction_ConvertToText(sender)
  994. } else {
  995. document.mainViewController?.menuItemAction_ConvertToText(sender)
  996. }
  997. }
  998. }
  999. func menuItemAction_ConvertToCSV(_ sender: Any) {
  1000. if (self.canResponseDocumentAction() == false) {
  1001. return
  1002. }
  1003. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1004. if (document.isHome) {
  1005. document.homeViewController?.menuItemAction_ConvertToCSV(sender)
  1006. } else {
  1007. document.mainViewController?.menuItemAction_ConvertToCSV(sender)
  1008. }
  1009. }
  1010. }
  1011. func menuItemAction_ConvertToImage(_ sender: Any) {
  1012. if (self.canResponseDocumentAction() == false) {
  1013. return
  1014. }
  1015. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1016. if (document.isHome) {
  1017. document.homeViewController?.menuItemAction_ConvertToImage(sender)
  1018. } else {
  1019. document.mainViewController?.menuItemAction_ConvertToImage(sender)
  1020. }
  1021. }
  1022. }
  1023. func menuItemClick_SettingPassword(_ sender: Any) {
  1024. if (self.canResponseDocumentAction() == false) {
  1025. return
  1026. }
  1027. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1028. if (document.isHome) {
  1029. document.homeViewController?.menuItemClick_SettingPassword(sender)
  1030. } else {
  1031. document.mainViewController?.menuItemClick_SettingPassword(sender)
  1032. }
  1033. }
  1034. }
  1035. func menuItemClick_RemovePassword(_ sender: Any) {
  1036. if (self.canResponseDocumentAction() == false) {
  1037. return
  1038. }
  1039. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1040. document.mainViewController?.menuItemClick_RemovePassword(sender)
  1041. }
  1042. }
  1043. func menuItemAction_closeWindow(_ sender: Any) {
  1044. if (self.canResponseDocumentAction() == false) {
  1045. return
  1046. }
  1047. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1048. if (document.isHome) {
  1049. document.homeViewController?.menuItemAction_closeWindow(sender)
  1050. } else {
  1051. document.mainViewController?.menuItemAction_closeWindow(sender)
  1052. }
  1053. }
  1054. }
  1055. func menuItemAction_closeAllWindows(_ sender: Any) {
  1056. if (self.canResponseDocumentAction() == false) {
  1057. return
  1058. }
  1059. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1060. if (document.isHome) {
  1061. document.homeViewController?.menuItemAction_closeAllWindows(sender)
  1062. } else {
  1063. document.mainViewController?.menuItemAction_closeAllWindows(sender)
  1064. }
  1065. }
  1066. }
  1067. func menuItemAction_closeTagPage(_ sender: Any) {
  1068. if (self.canResponseDocumentAction() == false) {
  1069. return
  1070. }
  1071. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1072. document.mainViewController?.menuItemAction_closeTagPage(sender)
  1073. }
  1074. }
  1075. func menuItemAction_showInFinder(_ sender: Any) {
  1076. if (self.canResponseDocumentAction() == false) {
  1077. return
  1078. }
  1079. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1080. document.mainViewController?.menuItemAction_showInFinder(sender)
  1081. }
  1082. }
  1083. func menuItemAction_property(_ sender: Any) {
  1084. if (self.canResponseDocumentAction() == false) {
  1085. return
  1086. }
  1087. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1088. document.mainViewController?.menuItemAction_property(sender)
  1089. }
  1090. }
  1091. func menuItemAction_print(_ sender: Any) {
  1092. if (self.canResponseDocumentAction() == false) {
  1093. return
  1094. }
  1095. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1096. document.mainViewController?.menuItemAction_print(sender)
  1097. }
  1098. }
  1099. func isEncapsulatedPostScriptData(_ data: Data) -> Bool {
  1100. let epsHeaderData = Data(bytes: [69, 80, 83, 70, 45], count: 5)
  1101. let rg: Range = (14..<20)
  1102. return (data.count >= 20 && (data.range(of: epsHeaderData, options: .anchored, in: rg) != nil))
  1103. }
  1104. func convertTIFFDataToPDF(_ tiffData: Data) -> Data? {
  1105. guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil }
  1106. let pdfData = NSMutableData(capacity: tiffData.count)
  1107. let consumer = CGDataConsumer(data: pdfData! as CFMutableData)!
  1108. var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
  1109. let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil)
  1110. ctxt!.beginPDFPage(nil)
  1111. ctxt!.draw(cgImage, in: rect)
  1112. ctxt!.endPDFPage()
  1113. ctxt!.flush()
  1114. return pdfData as? Data
  1115. }
  1116. func openDocumentWithImageFromPasteboard(_ pboard: NSPasteboard, error outError: AutoreleasingUnsafeMutablePointer<NSError?>?) -> Any? {
  1117. var document: CPDFDocument? = nil
  1118. var data: Data? = nil
  1119. if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.pdf.rawValue]) {
  1120. // pboard.types
  1121. data = pboard.data(forType: NSPasteboard.PasteboardType.pdf)
  1122. } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.postScript.rawValue]) {
  1123. // pboard.types
  1124. data = pboard.data(forType: NSPasteboard.PasteboardType.postScript)
  1125. } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.tiff.rawValue]) {
  1126. // pboard.types
  1127. data = convertTIFFDataToPDF(pboard.data(forType: NSPasteboard.PasteboardType.tiff) ?? Data())
  1128. } else {
  1129. let images = pboard.readObjects(forClasses: [NSImage.self], options: [:])
  1130. let strings = pboard.readObjects(forClasses: [NSAttributedString.self], options: [:])
  1131. if images?.count ?? 0 > 0 {
  1132. data = convertTIFFDataToPDF((images![0] as AnyObject).tiffRepresentation!)
  1133. } else if strings?.count ?? 0 > 0 {
  1134. data = KMOCTool.convertStringsToPDF(withString: strings ?? [""]) // convertStringsToPDF(strings!)
  1135. }
  1136. }
  1137. if let data = data {
  1138. _ = NSDocumentController.shared
  1139. document = CPDFDocument(data: data)
  1140. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1141. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1142. let filePath = savePath.deletingLastPathComponent
  1143. if FileManager.default.fileExists(atPath: filePath) == false {
  1144. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1145. }
  1146. document?.write(to: URL(fileURLWithPath: savePath))
  1147. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1148. if error != nil {
  1149. NSApp.presentError(error!)
  1150. } else {
  1151. if document is KMMainDocument {
  1152. let newDocument = document
  1153. (newDocument as! KMMainDocument).isNewCreated = true
  1154. }
  1155. }
  1156. }
  1157. } else if let outError = outError {
  1158. outError.pointee = NSError(domain: "SKDocumentErrorDomain", code: 3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Unable to load data from clipboard", comment: "Error description")])
  1159. }
  1160. return document
  1161. }
  1162. func openDocument(withURLFromPasteboard pboard: NSPasteboard, showNotes: Bool, error outError: inout NSError?) -> Any? {
  1163. let theURLs = NSURL.readURLs(from: pboard)
  1164. let url = theURLs?.count ?? 0 > 0 ? theURLs?[0] : nil
  1165. let theURL: NSURL? = url as? NSURL
  1166. var documentC = NSDocumentController.shared
  1167. var document: NSDocument? = nil
  1168. if (theURL as AnyObject).isFileURL == true {
  1169. var _: NSError? = nil
  1170. let type = try? documentC.typeForContents(of: theURL as! URL)//ForContents(ofURL: theURL, error: &error)
  1171. if showNotes == false || NSDocument.readableTypes.contains(type ?? "") {
  1172. documentC.openDocument(withContentsOf: theURL as! URL, display: true, completionHandler: { resultDocument, success, err in
  1173. document = resultDocument
  1174. })//openDocument(withContentsOfURL: theURL, display: true, error: &outError)
  1175. } else if NSDocument.readableTypes.contains(type ?? "") {
  1176. for doc in documentC.documents {
  1177. let sel = NSSelectorFromString("sourceFileURL")
  1178. if doc.responds(to: sel) && doc.fileURL == theURL as? URL {
  1179. document = doc
  1180. break
  1181. }
  1182. }
  1183. if let document: NSDocument = document {
  1184. document.showWindows()
  1185. } else {
  1186. var data: Data? = nil
  1187. // if NSWorkspace.shared.type(type ?? "", conformsToType: KMPDFBundleDocumentType) {
  1188. // let skimFileURL = FileManager.default.exte
  1189. // data = skimFileURL != nil ? try? Data(contentsOf: skimFileURL!) : nil
  1190. // } else {
  1191. // data = SKNExtendedAttributeManager.shared().extendedAttributeNamed(SKIM_NOTES_KEY, atPath: theURL.path, traverseLink: true, error: &error)
  1192. // }
  1193. // let documentNew: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  1194. if let document = try? documentC.makeUntitledDocument(ofType: KMNotesDocumentType) {
  1195. document.fileURL = URL(fileURLWithPath: theURL?.path ?? "")
  1196. // if data == nil || document.read(fromData: data!, ofType: SKNotesDocumentType, error: &error) {
  1197. documentC.addDocument(document)
  1198. document.makeWindowControllers()
  1199. document.showWindows()
  1200. // } else {
  1201. // document = nil
  1202. // if let error = error {
  1203. // outError = error
  1204. // }
  1205. // }
  1206. }
  1207. }
  1208. }
  1209. }
  1210. // else if showNotes == false && theURL != nil {
  1211. // document = SKDownloadController.shared().addDownload(for: theURL!)
  1212. // } else if outError != nil {
  1213. // outError = NSError.readPasteboardError(withLocalizedDescription: NSLocalizedString("Unable to load data from clipboard", comment: "Error description"))
  1214. // }
  1215. return document
  1216. }
  1217. @IBAction func newDocumentFromClipboard(_ sender: Any?) {
  1218. KMPrint("newDocumentFromClipboard")
  1219. var error: NSError?
  1220. let pboard = NSPasteboard.general
  1221. var document = openDocumentWithImageFromPasteboard(pboard, error: &error)
  1222. if document == nil{
  1223. document = openDocument(withURLFromPasteboard: pboard, showNotes: false, error: &error)
  1224. }
  1225. }
  1226. @IBAction func newDocumentFromImage(_ sender: Any?) {
  1227. KMPrint("importFromWebPage")
  1228. createPDFFromImage()
  1229. }
  1230. @IBAction func importFromWebPage(_ sender: Any?) {
  1231. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  1232. if (document.isHome) {
  1233. document.homeViewController?.importFromWebPage()
  1234. } else {
  1235. self.homeVC = KMHomeViewController()
  1236. self.homeVC?.importFromWebPage()
  1237. }
  1238. }
  1239. @IBAction func screenShot_SelectArea(_ sender: Any?) {
  1240. KMScreenShotHandler.beginScreenshot_SelectRectWithCompleteHandler { ima in
  1241. if let image = ima {
  1242. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1243. let folderPath = self.createBaseFoldPath()
  1244. let savePathOld = folderPath + "/screenShot.pdf"
  1245. var savePath = self.filePathCheck(path: savePathOld)
  1246. let newDocument = CPDFDocument()
  1247. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1248. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1249. if writeSuccess ?? false {
  1250. if self.checkOpenNewDocument(path: savePath) {
  1251. self.savePdf(savePath)
  1252. }
  1253. // try? FileManager.default.removeItem(atPath: savePath)
  1254. } else {
  1255. }
  1256. }
  1257. }
  1258. }
  1259. }
  1260. func checkOpenNewDocument(path: String) -> Bool {
  1261. if !IAPProductsManager.default().isAvailableAllFunction() && self.browser.tabCount() > 1 {
  1262. let preferenceNoteShow = UserDefaults.standard.bool(forKey: KMTabbingHintShowFlag)
  1263. if preferenceNoteShow {
  1264. menuItemAction_newTagPageToNewWindow("")
  1265. } else {
  1266. if !KMDataManager.default.isTabbingWin{
  1267. KMDataManager.default.isTabbingWin = true
  1268. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  1269. tabbingWin.selectCallBack = {[weak self] continueOrNot in
  1270. KMDataManager.default.isTabbingWin = false
  1271. if continueOrNot {
  1272. self?.reopenDocumentForNewWindow(savePath: path)
  1273. } else {
  1274. }
  1275. }
  1276. self.km_beginSheet(windowC: tabbingWin)
  1277. }
  1278. }
  1279. return false
  1280. }
  1281. return true
  1282. }
  1283. func reopenDocumentForNewWindow(savePath: String) {
  1284. let browser = KMBrowser.init() as KMBrowser
  1285. browser.windowController = KMBrowserWindowController.init(browser: browser)
  1286. browser.addHomeTabContents()
  1287. browser.windowController.showWindow(self)
  1288. self.savePdf(savePath)
  1289. }
  1290. func filePathCheck(path: String) -> String {
  1291. var i: Int = 0
  1292. let fileURL = URL(fileURLWithPath: path)
  1293. var newPath: String = path
  1294. let fileManager = FileManager.default
  1295. while fileManager.fileExists(atPath: newPath) {
  1296. i += 1
  1297. newPath = fileURL.deletingPathExtension().path
  1298. newPath.append("(\(i))")
  1299. newPath.append(".")
  1300. newPath.append(fileURL.pathExtension)
  1301. }
  1302. return newPath
  1303. }
  1304. @IBAction func screenShot_Window(_ sender: Any?) {
  1305. KMPrint("screenShot_Window")
  1306. KMScreenShotHandler.beginScreenShot_SelectWindowCompleteHandler { ima in
  1307. if let image = ima {
  1308. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1309. let folderPath = self.createBaseFoldPath()
  1310. let savePathOld = folderPath + "/screenShot.pdf"
  1311. var savePath = self.filePathCheck(path: savePathOld)
  1312. let newDocument = CPDFDocument()
  1313. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1314. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1315. if writeSuccess == true {
  1316. if self.checkOpenNewDocument(path: savePath) {
  1317. self.savePdf(savePath)
  1318. }
  1319. // try? FileManager.default.removeItem(atPath: savePath)
  1320. } else {
  1321. }
  1322. }
  1323. }
  1324. }
  1325. }
  1326. @IBAction func screenShot_FullScreenDelay(_ sender: Any?) {
  1327. KMPrint("screenShot_FullScreenDelay")
  1328. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(3) { ima in
  1329. if let image = ima {
  1330. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1331. let folderPath = self.createBaseFoldPath()
  1332. let savePathOld = folderPath + "/screenShot.pdf"
  1333. var savePath = self.filePathCheck(path: savePathOld)
  1334. let newDocument = CPDFDocument()
  1335. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1336. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1337. if writeSuccess == true {
  1338. if self.checkOpenNewDocument(path: savePath) {
  1339. self.savePdf(savePath)
  1340. }
  1341. // try? FileManager.default.removeItem(atPath: savePath)
  1342. } else {
  1343. }
  1344. }
  1345. }
  1346. }
  1347. }
  1348. @IBAction func screenShot_FullScreen(_ sender: Any?) {
  1349. KMPrint("screenShot_FullScreen")
  1350. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(0) { ima in
  1351. if let image = ima {
  1352. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1353. let folderPath = self.createBaseFoldPath()
  1354. let savePathOld = folderPath + "/screenShot.pdf"
  1355. var savePath = self.filePathCheck(path: savePathOld)
  1356. let newDocument = CPDFDocument()
  1357. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1358. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1359. if writeSuccess == true {
  1360. self.savePdf(savePath)
  1361. // try? FileManager.default.removeItem(atPath: savePath)
  1362. } else {
  1363. }
  1364. }
  1365. }
  1366. }
  1367. }
  1368. func createBaseFoldPath() -> String {
  1369. let folderPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last?.appending("/\(Bundle.main.bundleIdentifier!)")
  1370. if (FileManager.default.fileExists(atPath: folderPath!) == false) {
  1371. try?FileManager.default.createDirectory(atPath: folderPath!, withIntermediateDirectories: false)
  1372. }
  1373. return folderPath ?? ""
  1374. }
  1375. }
  1376. // MARK: - KMSystemViewMenuProtocol
  1377. extension KMBrowserWindowController: KMSystemViewMenuProtocol {
  1378. func createPDFFromImage() {
  1379. let openPanel = NSOpenPanel()
  1380. openPanel.allowedFileTypes = KMImageAccessoryController.supportedImageTypes()
  1381. openPanel.allowsMultipleSelection = true
  1382. openPanel.message = NSLocalizedString("Select images to create a new document. To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", comment: "")
  1383. openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1384. if response == .OK {
  1385. self.creatPdfFromImg(urls: openPanel.urls)
  1386. }
  1387. }
  1388. }
  1389. func creatPdfFromImg(urls: Array<URL>) {
  1390. // let savePath = self.newDocumentTempSavePath("convertToPDF.pdf")
  1391. let pdf = CPDFDocument()
  1392. // var isSucceed = true
  1393. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1394. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1395. let filePath = savePath.deletingLastPathComponent
  1396. if FileManager.default.fileExists(atPath: filePath) == false {
  1397. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1398. }
  1399. for fileURL in urls {
  1400. if let imag = NSImage(contentsOfFile: fileURL.path ) {
  1401. _ = pdf?.km_insertPage(imag.size, withImage: fileURL.path , at: pdf?.pageCount ?? 0)
  1402. pdf?.write(to: URL(fileURLWithPath: savePath))
  1403. }
  1404. }
  1405. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1406. if error != nil {
  1407. NSApp.presentError(error!)
  1408. } else {
  1409. if document is KMMainDocument {
  1410. let newDocument = document
  1411. (newDocument as! KMMainDocument).isNewCreated = true
  1412. }
  1413. }
  1414. // self.homeContentView?.reloadData()
  1415. }
  1416. }
  1417. func createPdfWithParam() {
  1418. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1419. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1420. let filePath = savePath.deletingLastPathComponent
  1421. if FileManager.default.fileExists(atPath: filePath) == false {
  1422. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1423. }
  1424. let pdfDocument = CPDFDocument()
  1425. pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
  1426. pdfDocument?.write(to: URL(fileURLWithPath: savePath))
  1427. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1428. if error != nil {
  1429. NSApp.presentError(error!)
  1430. } else {
  1431. if document is KMMainDocument {
  1432. let newDocument = document
  1433. (newDocument as! KMMainDocument).isNewCreated = true
  1434. }
  1435. }
  1436. // self.homeContentView?.reloadData()
  1437. }
  1438. }
  1439. func savePdf(_ filePath: String) -> Void {
  1440. let docVc = KMDocumentController.shared
  1441. docVc.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
  1442. if error != nil {
  1443. NSApp.presentError(error!)
  1444. } else {
  1445. if document is KMMainDocument {
  1446. let newDocument = document
  1447. (newDocument as! KMMainDocument).isNewCreated = true
  1448. }
  1449. }
  1450. }
  1451. }
  1452. func newDocumentTempSavePath(_ fileName: String) -> String {
  1453. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  1454. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  1455. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  1456. return append2
  1457. }
  1458. func fetchUniquePath(_ originalPath: String) -> String {
  1459. var path = originalPath
  1460. let dManager = FileManager.default
  1461. if !dManager.fileExists(atPath: path) {
  1462. if path.extension.count < 1 {
  1463. path = path.stringByAppendingPathExtension("pdf")
  1464. }
  1465. return path
  1466. } else {
  1467. let originalFullFileName = path.lastPathComponent
  1468. let originalFileName = path.lastPathComponent.deletingPathExtension
  1469. let originalExtension = path.extension
  1470. let startIndex: Int = 0
  1471. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  1472. let fileLocatePath = originalPath.substring(to: endIndex)
  1473. var i = 1
  1474. while (1 != 0) {
  1475. var newName = String(format: "%@%ld", originalFileName, i)
  1476. newName = newName.stringByAppendingPathExtension(originalExtension)
  1477. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  1478. if !dManager.fileExists(atPath: newPath) {
  1479. return newPath
  1480. } else {
  1481. i+=1
  1482. continue
  1483. }
  1484. }
  1485. }
  1486. }
  1487. func menuItemAction_adjustWidth(_ sender: Any) {
  1488. if (self.canResponseDocumentAction() == false) {
  1489. return
  1490. }
  1491. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1492. document.mainViewController?.menuItemAction_adjustWidth(sender)
  1493. }
  1494. }
  1495. func menuItemAction_adjustPage(_ sender: Any) {
  1496. if (self.canResponseDocumentAction() == false) {
  1497. return
  1498. }
  1499. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1500. document.mainViewController?.menuItemAction_adjustPage(sender)
  1501. }
  1502. }
  1503. func menuItemAction_size(_ sender: Any) {
  1504. if (self.canResponseDocumentAction() == false) {
  1505. return
  1506. }
  1507. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1508. document.mainViewController?.menuItemAction_size(sender)
  1509. }
  1510. }
  1511. func menuItemAction_zoomOut(_ sender: Any) {
  1512. if (self.canResponseDocumentAction() == false) {
  1513. return
  1514. }
  1515. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1516. document.mainViewController?.menuItemAction_zoomOut(sender)
  1517. }
  1518. }
  1519. func menuItemAction_zoomIn(_ sender: Any) {
  1520. if (self.canResponseDocumentAction() == false) {
  1521. return
  1522. }
  1523. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1524. document.mainViewController?.menuItemAction_zoomIn(sender)
  1525. }
  1526. }
  1527. func menuItemAction_singlePage(_ sender: Any) {
  1528. if (self.canResponseDocumentAction() == false) {
  1529. return
  1530. }
  1531. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1532. document.mainViewController?.menuItemAction_singlePage(sender)
  1533. }
  1534. }
  1535. func menuItemAction_singlePageContinue(_ sender: Any) {
  1536. if (self.canResponseDocumentAction() == false) {
  1537. return
  1538. }
  1539. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1540. document.mainViewController?.menuItemAction_singlePageContinue(sender)
  1541. }
  1542. }
  1543. func menuItemAction_doublePage(_ sender: Any) {
  1544. if (self.canResponseDocumentAction() == false) {
  1545. return
  1546. }
  1547. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1548. document.mainViewController?.menuItemAction_doublePage(sender)
  1549. }
  1550. }
  1551. func menuItemAction_doublePageContinue(_ sender: Any) {
  1552. if (self.canResponseDocumentAction() == false) {
  1553. return
  1554. }
  1555. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1556. document.mainViewController?.menuItemAction_doublePageContinue(sender)
  1557. }
  1558. }
  1559. func menuItemAction_bookMode(_ sender: Any) {
  1560. if (self.canResponseDocumentAction() == false) {
  1561. return
  1562. }
  1563. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1564. document.mainViewController?.menuItemAction_bookMode(sender)
  1565. }
  1566. }
  1567. func menuItemAction_readMode(_ sender: Any) {
  1568. if (self.canResponseDocumentAction() == false) {
  1569. return
  1570. }
  1571. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1572. document.mainViewController?.menuItemAction_readMode(sender)
  1573. }
  1574. }
  1575. func menuItemAction_showSplitPage(_ sender: Any) {
  1576. if (self.canResponseDocumentAction() == false) {
  1577. return
  1578. }
  1579. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1580. document.mainViewController?.menuItemAction_showSplitPage(sender)
  1581. }
  1582. }
  1583. func menuItemAction_rotateLeft(_ sender: Any) {
  1584. if (self.canResponseDocumentAction() == false) {
  1585. return
  1586. }
  1587. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1588. document.mainViewController?.menuItemAction_rotateLeft(sender)
  1589. }
  1590. }
  1591. func menuItemAction_rotateRight(_ sender: Any) {
  1592. if (self.canResponseDocumentAction() == false) {
  1593. return
  1594. }
  1595. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1596. document.mainViewController?.menuItemAction_rotateRight(sender)
  1597. }
  1598. }
  1599. func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  1600. if (self.canResponseDocumentAction() == false) {
  1601. return
  1602. }
  1603. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1604. document.mainViewController?.menuItemAction_rotateAllPageLeft(sender)
  1605. }
  1606. }
  1607. func menuItemAction_rotateAllPageRight(_ sender: Any) {
  1608. if (self.canResponseDocumentAction() == false) {
  1609. return
  1610. }
  1611. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1612. document.mainViewController?.menuItemAction_rotateAllPageRight(sender)
  1613. }
  1614. }
  1615. func menuItemAction_view_readMode(_ sender: Any) {
  1616. if (self.canResponseDocumentAction() == false) {
  1617. return
  1618. }
  1619. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1620. document.mainViewController?.menuItemAction_view_readMode(sender)
  1621. }
  1622. }
  1623. func menuItemAction_hiddenLeftSide(_ sender: Any) {
  1624. if (self.canResponseDocumentAction() == false) {
  1625. return
  1626. }
  1627. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1628. document.mainViewController?.menuItemAction_hiddenLeftSide(sender)
  1629. }
  1630. }
  1631. func menuItemAction_hiddenRightSide(_ sender: Any) {
  1632. if (self.canResponseDocumentAction() == false) {
  1633. return
  1634. }
  1635. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1636. document.mainViewController?.menuItemAction_hiddenRightSide(sender)
  1637. }
  1638. }
  1639. func menuItemAction_thumai(_ sender: Any) {
  1640. if (self.canResponseDocumentAction() == false) {
  1641. return
  1642. }
  1643. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1644. document.mainViewController?.menuItemAction_thumai(sender)
  1645. }
  1646. }
  1647. func menuItemAction_outline(_ sender: Any) {
  1648. if (self.canResponseDocumentAction() == false) {
  1649. return
  1650. }
  1651. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1652. document.mainViewController?.menuItemAction_outline(sender)
  1653. }
  1654. }
  1655. func menuItemAction_bookmark(_ sender: Any) {
  1656. if (self.canResponseDocumentAction() == false) {
  1657. return
  1658. }
  1659. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1660. document.mainViewController?.menuItemAction_bookmark(sender)
  1661. }
  1662. }
  1663. func menuItemAction_annotation(_ sender: Any) {
  1664. if (self.canResponseDocumentAction() == false) {
  1665. return
  1666. }
  1667. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1668. document.mainViewController?.menuItemAction_annotation(sender)
  1669. }
  1670. }
  1671. func menuItemAction_search(_ sender: Any) {
  1672. if (self.canResponseDocumentAction() == false) {
  1673. return
  1674. }
  1675. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1676. document.mainViewController?.menuItemAction_search(sender)
  1677. }
  1678. }
  1679. func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  1680. if (self.canResponseDocumentAction() == false) {
  1681. return
  1682. }
  1683. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1684. document.mainViewController?.menuItemAction_hiddenPageIndicator(sender)
  1685. }
  1686. }
  1687. @IBAction func togglePresentation(_ sender: Any?) {
  1688. if (self.canResponseDocumentAction() == false) {
  1689. return
  1690. }
  1691. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1692. document.mainViewController?.togglePresentation(sender)
  1693. }
  1694. }
  1695. @IBAction func chooseTransition(_ sender: Any?) {
  1696. if (self.canResponseDocumentAction() == false) {
  1697. return
  1698. }
  1699. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1700. document.mainViewController?.chooseTransition(sender)
  1701. }
  1702. }
  1703. @IBAction func toggleAutoFlow(_ sender: Any?) {
  1704. if (self.canResponseDocumentAction() == false) {
  1705. return
  1706. }
  1707. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1708. document.mainViewController?.toggleAutoFlow(sender)
  1709. }
  1710. }
  1711. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  1712. if (self.canResponseDocumentAction() == false) {
  1713. return
  1714. }
  1715. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1716. document.mainViewController?.chooseAutoFlowSetting(sender)
  1717. }
  1718. }
  1719. @IBAction func toggleReadingBar(_ sender: Any?) {
  1720. if (self.canResponseDocumentAction() == false) {
  1721. return
  1722. }
  1723. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1724. document.mainViewController?.toggleReadingBar(sender)
  1725. }
  1726. }
  1727. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  1728. if (self.canResponseDocumentAction() == false) {
  1729. return
  1730. }
  1731. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1732. document.mainViewController?.toggleLeftSidePane(sender)
  1733. }
  1734. }
  1735. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  1736. if (self.canResponseDocumentAction() == false) {
  1737. return
  1738. }
  1739. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1740. document.mainViewController?.changeLeftSidePaneState(sender)
  1741. }
  1742. }
  1743. @IBAction func splitViewAction(_ sender: Any?) {
  1744. if (self.canResponseDocumentAction() == false) {
  1745. return
  1746. }
  1747. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1748. document.mainViewController?.splitViewAction(sender)
  1749. }
  1750. }
  1751. @IBAction func themesColor(_ sender: Any?) {
  1752. if (self.canResponseDocumentAction() == false) {
  1753. return
  1754. }
  1755. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1756. document.mainViewController?.themesColor(sender)
  1757. }
  1758. }
  1759. @IBAction func toggleStatusBar(_ sender: Any?) {
  1760. if (self.canResponseDocumentAction() == false) {
  1761. return
  1762. }
  1763. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1764. document.mainViewController?.toggleStatusBar(sender)
  1765. }
  1766. }
  1767. @IBAction func toggleToolbarShow(_ sender: Any?) {
  1768. if (self.canResponseDocumentAction() == false) {
  1769. return
  1770. }
  1771. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1772. document.mainViewController?.toggleToolbarShow(sender)
  1773. }
  1774. }
  1775. }
  1776. // MARK: - KMSystemEditMenuProtocol
  1777. extension KMBrowserWindowController: KMSystemEditMenuProtocol {
  1778. func menuItemAction_find(_ sender: Any) {
  1779. if (self.canResponseDocumentAction() == false) {
  1780. return
  1781. }
  1782. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1783. document.mainViewController?.menuItemAction_find(sender)
  1784. }
  1785. }
  1786. @IBAction func changeFont(_ sender: Any?) {
  1787. KMPrint("changeFont ...")
  1788. }
  1789. }
  1790. // MARK: - Annotation Menu
  1791. extension KMBrowserWindowController {
  1792. @IBAction func menuItemAction_highlight(_ sender: Any) {
  1793. if (self.canResponseDocumentAction() == false) {
  1794. return
  1795. }
  1796. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1797. document.mainViewController?.menuItemAction_highlight(sender)
  1798. }
  1799. }
  1800. @IBAction func menuItemAction_underline(_ sender: Any) {
  1801. if (self.canResponseDocumentAction() == false) {
  1802. return
  1803. }
  1804. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1805. document.mainViewController?.menuItemAction_underline(sender)
  1806. }
  1807. }
  1808. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  1809. if (self.canResponseDocumentAction() == false) {
  1810. return
  1811. }
  1812. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1813. document.mainViewController?.menuItemAction_deleteLine(sender)
  1814. }
  1815. }
  1816. @IBAction func menuItemAction_freehand(_ sender: Any) {
  1817. if (self.canResponseDocumentAction() == false) {
  1818. return
  1819. }
  1820. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1821. document.mainViewController?.menuItemAction_freehand(sender)
  1822. }
  1823. }
  1824. @IBAction func menuItemAction_text(_ sender: Any) {
  1825. if (self.canResponseDocumentAction() == false) {
  1826. return
  1827. }
  1828. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1829. document.mainViewController?.menuItemAction_text(sender)
  1830. }
  1831. }
  1832. @IBAction func menuItemAction_note(_ sender: Any) {
  1833. if (self.canResponseDocumentAction() == false) {
  1834. return
  1835. }
  1836. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1837. document.mainViewController?.menuItemAction_note(sender)
  1838. }
  1839. }
  1840. @IBAction func menuItemAction_squre(_ sender: Any) {
  1841. if (self.canResponseDocumentAction() == false) {
  1842. return
  1843. }
  1844. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1845. document.mainViewController?.menuItemAction_squre(sender)
  1846. }
  1847. }
  1848. @IBAction func menuItemAction_circle(_ sender: Any) {
  1849. if (self.canResponseDocumentAction() == false) {
  1850. return
  1851. }
  1852. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1853. document.mainViewController?.menuItemAction_circle(sender)
  1854. }
  1855. }
  1856. @IBAction func menuItemAction_arrow(_ sender: Any) {
  1857. if (self.canResponseDocumentAction() == false) {
  1858. return
  1859. }
  1860. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1861. document.mainViewController?.menuItemAction_arrow(sender)
  1862. }
  1863. }
  1864. @IBAction func menuItemAction_line(_ sender: Any) {
  1865. if (self.canResponseDocumentAction() == false) {
  1866. return
  1867. }
  1868. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1869. document.mainViewController?.menuItemAction_line(sender)
  1870. }
  1871. }
  1872. // link
  1873. @IBAction func menuItemAction_link(_ sender: Any) {
  1874. if (self.canResponseDocumentAction() == false) {
  1875. return
  1876. }
  1877. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1878. document.mainViewController?.menuItemAction_link(sender)
  1879. }
  1880. }
  1881. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  1882. if (self.canResponseDocumentAction() == false) {
  1883. return
  1884. }
  1885. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1886. document.mainViewController?.menuItemAction_linkPage(sender)
  1887. }
  1888. }
  1889. // stamp
  1890. @IBAction func menuItemAction_stamp(_ sender: Any) {
  1891. if (self.canResponseDocumentAction() == false) {
  1892. return
  1893. }
  1894. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1895. document.mainViewController?.menuItemAction_stamp(sender)
  1896. }
  1897. }
  1898. @IBAction func notesLink(_ sender: Any?) {
  1899. if (self.canResponseDocumentAction() == false) {
  1900. return
  1901. }
  1902. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1903. document.mainViewController?.notesLink(sender)
  1904. }
  1905. }
  1906. @IBAction func notesStamp(_ sender: Any?) {
  1907. if (self.canResponseDocumentAction() == false) {
  1908. return
  1909. }
  1910. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1911. document.mainViewController?.notesStamp(sender)
  1912. }
  1913. }
  1914. @IBAction func menuItemAction_signure(_ sender: Any) {
  1915. if (self.canResponseDocumentAction() == false) {
  1916. return
  1917. }
  1918. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1919. document.mainViewController?.menuItemAction_signure(sender)
  1920. }
  1921. }
  1922. @IBAction func image(_ sender: Any?) {
  1923. if (self.canResponseDocumentAction() == false) {
  1924. return
  1925. }
  1926. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1927. document.mainViewController?.image(sender)
  1928. }
  1929. }
  1930. @IBAction func table(_ sender: Any?) {
  1931. if (self.canResponseDocumentAction() == false) {
  1932. return
  1933. }
  1934. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1935. document.mainViewController?.table(sender)
  1936. }
  1937. }
  1938. @IBAction func addForm(_ sender: Any?) {
  1939. if (self.canResponseDocumentAction() == false) {
  1940. return
  1941. }
  1942. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1943. document.mainViewController?.addForm(sender)
  1944. }
  1945. }
  1946. @IBAction func editNote(_ sender: Any?) {
  1947. if (self.canResponseDocumentAction() == false) {
  1948. return
  1949. }
  1950. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1951. document.mainViewController?.editNote(sender)
  1952. }
  1953. }
  1954. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  1955. if (self.canResponseDocumentAction() == false) {
  1956. return
  1957. }
  1958. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1959. document.mainViewController?.menuItemAction_hiddenAllAnnotation(sender)
  1960. }
  1961. }
  1962. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  1963. if (self.canResponseDocumentAction() == false) {
  1964. return
  1965. }
  1966. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1967. document.mainViewController?.menuItemAction_clearAllAnnotation(sender)
  1968. }
  1969. }
  1970. }
  1971. // MARK: - KMSystemPDFMenuProtocol
  1972. extension KMBrowserWindowController: KMSystemPDFMenuProtocol {
  1973. @IBAction func changeDisplayMode(_ sender: Any?) {
  1974. if (self.canResponseDocumentAction() == false) {
  1975. return
  1976. }
  1977. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1978. document.mainViewController?.changeDisplayMode(sender)
  1979. }
  1980. }
  1981. @IBAction func toggleDisplayAsBook(_ sender: Any?) {
  1982. if (self.canResponseDocumentAction() == false) {
  1983. return
  1984. }
  1985. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1986. document.mainViewController?.toggleDisplayAsBook(sender)
  1987. }
  1988. }
  1989. @IBAction func toggleDisplayPageBreaks(_ sender: Any?) {
  1990. if (self.canResponseDocumentAction() == false) {
  1991. return
  1992. }
  1993. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1994. document.mainViewController?.toggleDisplayPageBreaks(sender)
  1995. }
  1996. }
  1997. @IBAction func changeDisplayBox(_ sender: Any?) {
  1998. if (self.canResponseDocumentAction() == false) {
  1999. return
  2000. }
  2001. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2002. document.mainViewController?.changeDisplayBox(sender)
  2003. }
  2004. }
  2005. @IBAction func toggleAutoScale(_ sender: Any?) {
  2006. if (self.canResponseDocumentAction() == false) {
  2007. return
  2008. }
  2009. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2010. document.mainViewController?.toggleAutoScale(sender)
  2011. }
  2012. }
  2013. @IBAction func doZoomToActualSize(_ sender: Any?) {
  2014. if (self.canResponseDocumentAction() == false) {
  2015. return
  2016. }
  2017. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2018. document.mainViewController?.doZoomToActualSize(sender)
  2019. }
  2020. }
  2021. @IBAction func doZoomToFit(_ sender: Any?) {
  2022. if (self.canResponseDocumentAction() == false) {
  2023. return
  2024. }
  2025. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2026. document.mainViewController?.doZoomToFit(sender)
  2027. }
  2028. }
  2029. @IBAction func doZoomIn(_ sender: Any?) {
  2030. if (self.canResponseDocumentAction() == false) {
  2031. return
  2032. }
  2033. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2034. document.mainViewController?.doZoomIn(sender)
  2035. }
  2036. }
  2037. @IBAction func doZoomOut(_ sender: Any?) {
  2038. if (self.canResponseDocumentAction() == false) {
  2039. return
  2040. }
  2041. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2042. document.mainViewController?.doZoomOut(sender)
  2043. }
  2044. }
  2045. @IBAction func doMarqueeZoomTool(_ sender: Any?) {
  2046. if (self.canResponseDocumentAction() == false) {
  2047. return
  2048. }
  2049. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2050. document.mainViewController?.doMarqueeZoomTool(sender)
  2051. }
  2052. }
  2053. @IBAction func highlightFormFiled(_ sender: Any?) {
  2054. if (self.canResponseDocumentAction() == false) {
  2055. return
  2056. }
  2057. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2058. document.mainViewController?.highlightFormFiled(sender)
  2059. }
  2060. }
  2061. @IBAction func highlightLinks(_ sender: Any?) {
  2062. if (self.canResponseDocumentAction() == false) {
  2063. return
  2064. }
  2065. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2066. document.mainViewController?.highlightLinks(sender)
  2067. }
  2068. }
  2069. @IBAction func resetForm(_ sender: Any?) {
  2070. if (self.canResponseDocumentAction() == false) {
  2071. return
  2072. }
  2073. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2074. document.mainViewController?.resetForm(sender)
  2075. }
  2076. }
  2077. @IBAction func removeAllAnnotation(_ sender: Any?) {
  2078. if (self.canResponseDocumentAction() == false) {
  2079. return
  2080. }
  2081. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2082. document.mainViewController?.removeAllAnnotation(sender)
  2083. }
  2084. }
  2085. @IBAction func savePDFSettingToDefaults(_ sender: Any?) {
  2086. if (self.canResponseDocumentAction() == false) {
  2087. return
  2088. }
  2089. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2090. document.mainViewController?.savePDFSettingToDefaults(sender)
  2091. }
  2092. }
  2093. }
  2094. // MARK: - KMSystemGotoMenuProtocol
  2095. extension KMBrowserWindowController: KMSystemGotoMenuProtocol {
  2096. func menuItemAction_nextPage(_ sender: Any) {
  2097. if (self.canResponseDocumentAction() == false) {
  2098. return
  2099. }
  2100. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2101. document.mainViewController?.menuItemAction_nextPage(sender)
  2102. }
  2103. }
  2104. func menuItemAction_forwardPage(_ sender: Any) {
  2105. if (self.canResponseDocumentAction() == false) {
  2106. return
  2107. }
  2108. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2109. document.mainViewController?.menuItemAction_forwardPage(sender)
  2110. }
  2111. }
  2112. func menuItemAction_firstPage(_ sender: Any) {
  2113. if (self.canResponseDocumentAction() == false) {
  2114. return
  2115. }
  2116. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2117. document.mainViewController?.menuItemAction_firstPage(sender)
  2118. }
  2119. }
  2120. func menuItemAction_lastPage(_ sender: Any) {
  2121. if (self.canResponseDocumentAction() == false) {
  2122. return
  2123. }
  2124. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2125. document.mainViewController?.menuItemAction_lastPage(sender)
  2126. }
  2127. }
  2128. func menuItemAction_forward(_ sender: Any) {
  2129. if (self.canResponseDocumentAction() == false) {
  2130. return
  2131. }
  2132. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2133. document.mainViewController?.menuItemAction_forward(sender)
  2134. }
  2135. }
  2136. func menuItemAction_goback(_ sender: Any) {
  2137. if (self.canResponseDocumentAction() == false) {
  2138. return
  2139. }
  2140. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2141. document.mainViewController?.menuItemAction_goback(sender)
  2142. }
  2143. }
  2144. func menuItemAction_gotoPage(_ sender: Any) {
  2145. if (self.canResponseDocumentAction() == false) {
  2146. return
  2147. }
  2148. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2149. document.mainViewController?.menuItemAction_gotoPage(sender)
  2150. }
  2151. }
  2152. @IBAction func goToMarkedPage(_ sender: Any?) {
  2153. if (self.canResponseDocumentAction() == false) {
  2154. return
  2155. }
  2156. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2157. document.mainViewController?.goToMarkedPage(sender)
  2158. }
  2159. }
  2160. @IBAction func markPage(_ sender: Any?) {
  2161. if (self.canResponseDocumentAction() == false) {
  2162. return
  2163. }
  2164. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2165. document.mainViewController?.markPage(sender)
  2166. }
  2167. }
  2168. }
  2169. // MARK: - KMSystemToolMenuProtocol
  2170. extension KMBrowserWindowController: KMSystemToolMenuProtocol {
  2171. @IBAction func getInfo(_ sender: Any?) {
  2172. if (self.canResponseDocumentAction() == false) {
  2173. return
  2174. }
  2175. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2176. document.mainViewController?.getInfo(sender)
  2177. }
  2178. }
  2179. func orderFrontNotesPanel(_ sender: Any?) {
  2180. let windowC = KMNotesPanelController.shared
  2181. if let win = windowC.window {
  2182. if win.isVisible {
  2183. win.orderOut(sender)
  2184. } else {
  2185. win.orderFront(sender)
  2186. }
  2187. }
  2188. }
  2189. func menuItemAction_scrolTool(_ sender: Any) {
  2190. if (self.canResponseDocumentAction() == false) {
  2191. return
  2192. }
  2193. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2194. document.mainViewController?.menuItemAction_scrolTool(sender)
  2195. }
  2196. }
  2197. func menuItemAction_zoomOutTool(_ sender: Any) {
  2198. if (self.canResponseDocumentAction() == false) {
  2199. return
  2200. }
  2201. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2202. document.mainViewController?.menuItemAction_zoomOutTool(sender)
  2203. }
  2204. }
  2205. func menuItemAction_selectTool(_ sender: Any) {
  2206. if (self.canResponseDocumentAction() == false) {
  2207. return
  2208. }
  2209. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2210. document.mainViewController?.menuItemAction_selectTool(sender)
  2211. }
  2212. }
  2213. @IBAction func changeToolMode(_ sender: Any?) {
  2214. if (self.canResponseDocumentAction() == false) {
  2215. return
  2216. }
  2217. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2218. if (document.isHome) {
  2219. } else {
  2220. document.mainViewController?.changeToolMode(sender)
  2221. }
  2222. }
  2223. }
  2224. @IBAction func compression(_ sender: Any?) {
  2225. if (self.canResponseDocumentAction() == false) {
  2226. return
  2227. }
  2228. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2229. if (document.isHome) {
  2230. } else {
  2231. document.mainViewController?.compression(sender)
  2232. }
  2233. }
  2234. }
  2235. @IBAction func merge(_ sender: Any?) {
  2236. if (self.canResponseDocumentAction() == false) {
  2237. return
  2238. }
  2239. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2240. if (document.isHome) {
  2241. } else {
  2242. document.mainViewController?.merge(sender)
  2243. }
  2244. }
  2245. }
  2246. @IBAction func convert(_ sender: Any?) {
  2247. if (self.canResponseDocumentAction() == false) {
  2248. return
  2249. }
  2250. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2251. if (document.isHome) {
  2252. } else {
  2253. document.mainViewController?.convert(sender)
  2254. }
  2255. }
  2256. }
  2257. @IBAction func imageToPDF(_ sender: Any?) {
  2258. if (self.canResponseDocumentAction() == false) {
  2259. return
  2260. }
  2261. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2262. if (document.isHome) {
  2263. } else {
  2264. document.mainViewController?.imageToPDF(sender)
  2265. }
  2266. }
  2267. }
  2268. @IBAction func ocr(_ sender: Any?) {
  2269. if (self.canResponseDocumentAction() == false) {
  2270. return
  2271. }
  2272. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2273. if (document.isHome) {
  2274. } else {
  2275. document.mainViewController?.ocr(sender)
  2276. }
  2277. }
  2278. }
  2279. @IBAction func extractImage(_ sender: Any?) {
  2280. if (self.canResponseDocumentAction() == false) {
  2281. return
  2282. }
  2283. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2284. if (document.isHome) {
  2285. } else {
  2286. document.mainViewController?.extractImage(sender)
  2287. }
  2288. }
  2289. }
  2290. @IBAction func pageEdit(_ sender: Any?) {
  2291. if (self.canResponseDocumentAction() == false) {
  2292. return
  2293. }
  2294. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2295. if (document.isHome) {
  2296. } else {
  2297. document.mainViewController?.pageEdit(sender)
  2298. }
  2299. }
  2300. }
  2301. @IBAction func textEditAction(_ sender: Any?) {
  2302. if (self.canResponseDocumentAction() == false) {
  2303. return
  2304. }
  2305. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2306. if (document.isHome) {
  2307. } else {
  2308. document.mainViewController?.textEditAction(sender)
  2309. }
  2310. }
  2311. }
  2312. @IBAction func security(_ sender: Any?) {
  2313. if (self.canResponseDocumentAction() == false) {
  2314. return
  2315. }
  2316. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2317. if (document.isHome) {
  2318. } else {
  2319. document.mainViewController?.security(sender)
  2320. }
  2321. }
  2322. }
  2323. @IBAction func decryption(_ sender: Any?) {
  2324. if (self.canResponseDocumentAction() == false) {
  2325. return
  2326. }
  2327. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2328. if (document.isHome) {
  2329. } else {
  2330. document.mainViewController?.decryption(sender)
  2331. }
  2332. }
  2333. }
  2334. @IBAction func cropAll(_ sender: Any?) {
  2335. if (self.canResponseDocumentAction() == false) {
  2336. return
  2337. }
  2338. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2339. if (document.isHome) {
  2340. } else {
  2341. document.mainViewController?.cropAll(sender)
  2342. }
  2343. }
  2344. }
  2345. @IBAction func crop(_ sender: Any?) {
  2346. if (self.canResponseDocumentAction() == false) {
  2347. return
  2348. }
  2349. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2350. if (document.isHome) {
  2351. } else {
  2352. document.mainViewController?.crop(sender)
  2353. }
  2354. }
  2355. }
  2356. @IBAction func autoCropAll(_ sender: Any?) {
  2357. if (self.canResponseDocumentAction() == false) {
  2358. return
  2359. }
  2360. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2361. if (document.isHome) {
  2362. } else {
  2363. document.mainViewController?.autoCropAll(sender)
  2364. }
  2365. }
  2366. }
  2367. @IBAction func smartAutoCropAll(_ sender: Any?) {
  2368. if (self.canResponseDocumentAction() == false) {
  2369. return
  2370. }
  2371. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2372. if (document.isHome) {
  2373. } else {
  2374. document.mainViewController?.smartAutoCropAll(sender)
  2375. }
  2376. }
  2377. }
  2378. @IBAction func cropOptionsModel(_ sender: Any?) {
  2379. if (self.canResponseDocumentAction() == false) {
  2380. return
  2381. }
  2382. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2383. if (document.isHome) {
  2384. } else {
  2385. // document.mainViewController?.cropOptionsModel(sender)
  2386. }
  2387. }
  2388. }
  2389. @IBAction func selectToolModel(_ sender: Any?) {
  2390. if (self.canResponseDocumentAction() == false) {
  2391. return
  2392. }
  2393. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2394. if (document.isHome) {
  2395. } else {
  2396. document.mainViewController?.selectToolModel(sender)
  2397. }
  2398. }
  2399. }
  2400. @IBAction func takeSnapshot(_ sender: Any?) {
  2401. if (self.canResponseDocumentAction() == false) {
  2402. return
  2403. }
  2404. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2405. if (document.isHome) {
  2406. } else {
  2407. document.mainViewController?.takeSnapshot(sender)
  2408. }
  2409. }
  2410. }
  2411. @IBAction func takeSnapshotSelectContent(_ sender: Any?) {
  2412. if (self.canResponseDocumentAction() == false) {
  2413. return
  2414. }
  2415. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2416. if (document.isHome) {
  2417. } else {
  2418. document.mainViewController?.takeSnapshotSelectContent(sender)
  2419. }
  2420. }
  2421. }
  2422. @IBAction func changeAnnotationMode(_ sender: Any?) {
  2423. if (self.canResponseDocumentAction() == false) {
  2424. return
  2425. }
  2426. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2427. if (document.isHome) {
  2428. } else {
  2429. document.mainViewController?.changeAnnotationMode(sender)
  2430. }
  2431. }
  2432. }
  2433. @IBAction func addBackground(_ sender: Any?) {
  2434. if (self.canResponseDocumentAction() == false) {
  2435. return
  2436. }
  2437. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2438. if (document.isHome) {
  2439. } else {
  2440. document.mainViewController?.addBackground(sender)
  2441. }
  2442. }
  2443. }
  2444. @IBAction func removeBackground(_ sender: Any?) {
  2445. if (self.canResponseDocumentAction() == false) {
  2446. return
  2447. }
  2448. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2449. if (document.isHome) {
  2450. } else {
  2451. document.mainViewController?.removeBackground(sender)
  2452. }
  2453. }
  2454. }
  2455. @IBAction func addWatermark(_ sender: Any?) {
  2456. if (self.canResponseDocumentAction() == false) {
  2457. return
  2458. }
  2459. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2460. if (document.isHome) {
  2461. } else {
  2462. document.mainViewController?.addWatermark(sender)
  2463. }
  2464. }
  2465. }
  2466. @IBAction func editWatermark(_ sender: Any?) {
  2467. KMPrint("editWatermark ...")
  2468. }
  2469. @IBAction func batchAddWatermark(_ sender: Any?) {
  2470. if (self.canResponseDocumentAction() == false) {
  2471. return
  2472. }
  2473. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2474. if (document.isHome) {
  2475. } else {
  2476. document.mainViewController?.batchAddWatermark(sender)
  2477. }
  2478. }
  2479. }
  2480. @IBAction func batchRemoveWatermark(_ sender: Any?) {
  2481. if (self.canResponseDocumentAction() == false) {
  2482. return
  2483. }
  2484. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2485. if (document.isHome) {
  2486. } else {
  2487. document.mainViewController?.batchRemoveWatermark(sender)
  2488. }
  2489. }
  2490. }
  2491. @IBAction func removeWatermark(_ sender: Any?) {
  2492. if (self.canResponseDocumentAction() == false) {
  2493. return
  2494. }
  2495. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2496. if (document.isHome) {
  2497. } else {
  2498. document.mainViewController?.removeWatermark(sender)
  2499. }
  2500. }
  2501. }
  2502. @IBAction func templateWatermark(_ sender: Any?) {
  2503. if (self.canResponseDocumentAction() == false) {
  2504. return
  2505. }
  2506. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2507. if (document.isHome) {
  2508. } else {
  2509. document.mainViewController?.templateWatermark(sender)
  2510. }
  2511. }
  2512. }
  2513. @IBAction func batchAddBackground(_ sender: Any?) {
  2514. if (self.canResponseDocumentAction() == false) {
  2515. return
  2516. }
  2517. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2518. if (document.isHome) {
  2519. } else {
  2520. document.mainViewController?.batchAddBackground(sender)
  2521. }
  2522. }
  2523. }
  2524. @IBAction func batchRemoveBackground(_ sender: Any?) {
  2525. if (self.canResponseDocumentAction() == false) {
  2526. return
  2527. }
  2528. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2529. if (document.isHome) {
  2530. } else {
  2531. document.mainViewController?.batchRemoveBackground(sender)
  2532. }
  2533. }
  2534. }
  2535. @IBAction func templateBackground(_ sender: Any?) {
  2536. if (self.canResponseDocumentAction() == false) {
  2537. return
  2538. }
  2539. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2540. if (document.isHome) {
  2541. } else {
  2542. document.mainViewController?.templateBackground(sender)
  2543. }
  2544. }
  2545. }
  2546. @IBAction func addHeaderFooter(_ sender: Any?) {
  2547. if (self.canResponseDocumentAction() == false) {
  2548. return
  2549. }
  2550. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2551. if (document.isHome) {
  2552. } else {
  2553. document.mainViewController?.addHeaderFooter(sender)
  2554. }
  2555. }
  2556. }
  2557. @IBAction func removeHeaderFooter(_ sender: Any?) {
  2558. if (self.canResponseDocumentAction() == false) {
  2559. return
  2560. }
  2561. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2562. if (document.isHome) {
  2563. } else {
  2564. document.mainViewController?.removeHeaderFooter(sender)
  2565. }
  2566. }
  2567. }
  2568. @IBAction func batchAddHeaderFooter(_ sender: Any?) {
  2569. if (self.canResponseDocumentAction() == false) {
  2570. return
  2571. }
  2572. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2573. if (document.isHome) {
  2574. } else {
  2575. document.mainViewController?.batchAddHeaderFooter(sender)
  2576. }
  2577. }
  2578. }
  2579. @IBAction func batchRemoveHeaderFooter(_ sender: Any?) {
  2580. if (self.canResponseDocumentAction() == false) {
  2581. return
  2582. }
  2583. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2584. if (document.isHome) {
  2585. } else {
  2586. document.mainViewController?.batchRemoveHeaderFooter(sender)
  2587. }
  2588. }
  2589. }
  2590. @IBAction func templateHeaderFooter(_ sender: Any?) {
  2591. if (self.canResponseDocumentAction() == false) {
  2592. return
  2593. }
  2594. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2595. if (document.isHome) {
  2596. } else {
  2597. document.mainViewController?.templateHeaderFooter(sender)
  2598. }
  2599. }
  2600. }
  2601. @IBAction func addHeaderFooterBates(_ sender: Any?) {
  2602. if (self.canResponseDocumentAction() == false) {
  2603. return
  2604. }
  2605. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2606. if (document.isHome) {
  2607. } else {
  2608. document.mainViewController?.addHeaderFooterBates(sender)
  2609. }
  2610. }
  2611. }
  2612. @IBAction func removeHeaderFooterBates(_ sender: Any?) {
  2613. if (self.canResponseDocumentAction() == false) {
  2614. return
  2615. }
  2616. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2617. if (document.isHome) {
  2618. } else {
  2619. document.mainViewController?.removeHeaderFooterBates(sender)
  2620. }
  2621. }
  2622. }
  2623. @IBAction func batchAddBates(_ sender: Any?) {
  2624. if (self.canResponseDocumentAction() == false) {
  2625. return
  2626. }
  2627. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2628. if (document.isHome) {
  2629. } else {
  2630. document.mainViewController?.batchAddBates(sender)
  2631. }
  2632. }
  2633. }
  2634. @IBAction func batchRemoveBates(_ sender: Any?) {
  2635. if (self.canResponseDocumentAction() == false) {
  2636. return
  2637. }
  2638. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2639. if (document.isHome) {
  2640. } else {
  2641. document.mainViewController?.batchRemoveBates(sender)
  2642. }
  2643. }
  2644. }
  2645. @IBAction func templateBates(_ sender: Any?) {
  2646. if (self.canResponseDocumentAction() == false) {
  2647. return
  2648. }
  2649. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2650. if (document.isHome) {
  2651. } else {
  2652. document.mainViewController?.templateBates(sender)
  2653. }
  2654. }
  2655. }
  2656. @IBAction func togglePoster(_ sender: Any?) {
  2657. if (self.canResponseDocumentAction() == false) {
  2658. return
  2659. }
  2660. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2661. if (document.isHome) {
  2662. } else {
  2663. document.mainViewController?.togglePoster(sender)
  2664. }
  2665. }
  2666. }
  2667. @IBAction func toggleMultiple(_ sender: Any?) {
  2668. if (self.canResponseDocumentAction() == false) {
  2669. return
  2670. }
  2671. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2672. if (document.isHome) {
  2673. } else {
  2674. document.mainViewController?.toggleMultiple(sender)
  2675. }
  2676. }
  2677. }
  2678. @IBAction func toggleBooklet(_ sender: Any?) {
  2679. if (self.canResponseDocumentAction() == false) {
  2680. return
  2681. }
  2682. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2683. if (document.isHome) {
  2684. } else {
  2685. document.mainViewController?.toggleBooklet(sender)
  2686. }
  2687. }
  2688. }
  2689. @IBAction func rotateRight(_ sender: Any?) {
  2690. if (self.canResponseDocumentAction() == false) {
  2691. return
  2692. }
  2693. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2694. if (document.isHome) {
  2695. } else {
  2696. document.mainViewController?.rotateRight(sender)
  2697. }
  2698. }
  2699. }
  2700. @IBAction func rotateLeft(_ sender: Any?) {
  2701. if (self.canResponseDocumentAction() == false) {
  2702. return
  2703. }
  2704. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2705. if (document.isHome) {
  2706. } else {
  2707. document.mainViewController?.rotateLeft(sender)
  2708. }
  2709. }
  2710. }
  2711. @IBAction func rotateAllRight(_ sender: Any?) {
  2712. if (self.canResponseDocumentAction() == false) {
  2713. return
  2714. }
  2715. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2716. if (document.isHome) {
  2717. } else {
  2718. document.mainViewController?.rotateAllRight(sender)
  2719. }
  2720. }
  2721. }
  2722. @IBAction func rotateAllLeft(_ sender: Any?) {
  2723. if (self.canResponseDocumentAction() == false) {
  2724. return
  2725. }
  2726. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2727. if (document.isHome) {
  2728. } else {
  2729. document.mainViewController?.rotateAllLeft(sender)
  2730. }
  2731. }
  2732. }
  2733. @IBAction func autoSelectContent(_ sender: Any?) {
  2734. if (self.canResponseDocumentAction() == false) {
  2735. return
  2736. }
  2737. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2738. if (document.isHome) {
  2739. } else {
  2740. document.mainViewController?.autoSelectContent(sender)
  2741. }
  2742. }
  2743. }
  2744. }
  2745. // MARK: - KMSystemWindowMenuRrotocol
  2746. extension KMBrowserWindowController: KMSystemWindowMenuRrotocol {
  2747. func menuItemAction_showForwardTagPage(_ sender: Any) {
  2748. if (self.canResponseDocumentAction() == false) {
  2749. return
  2750. }
  2751. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2752. if (document.isHome) {
  2753. document.homeViewController?.menuItemAction_showForwardTagPage(sender)
  2754. } else {
  2755. document.mainViewController?.menuItemAction_showForwardTagPage(sender)
  2756. }
  2757. }
  2758. }
  2759. func menuItemAction_showNextTagPage(_ sender: Any) {
  2760. if (self.canResponseDocumentAction() == false) {
  2761. return
  2762. }
  2763. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2764. if (document.isHome) {
  2765. document.homeViewController?.menuItemAction_showNextTagPage(sender)
  2766. } else {
  2767. document.mainViewController?.menuItemAction_showNextTagPage(sender)
  2768. }
  2769. }
  2770. }
  2771. func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  2772. if (self.canResponseDocumentAction() == false) {
  2773. return
  2774. }
  2775. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2776. if (document.isHome) {
  2777. document.homeViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2778. } else {
  2779. document.mainViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2780. }
  2781. }
  2782. }
  2783. func menuItemAction_mergeAllWindow(_ sender: Any) {
  2784. if (self.canResponseDocumentAction() == false) {
  2785. return
  2786. }
  2787. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2788. if (document.isHome) {
  2789. document.homeViewController?.menuItemAction_mergeAllWindow(sender)
  2790. } else {
  2791. document.mainViewController?.menuItemAction_mergeAllWindow(sender)
  2792. }
  2793. }
  2794. }
  2795. @IBAction func performFit(_ sender: Any?) {
  2796. if (self.canResponseDocumentAction() == false) {
  2797. return
  2798. }
  2799. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2800. if (document.isHome) {
  2801. } else {
  2802. document.mainViewController?.performFit(sender)
  2803. }
  2804. }
  2805. }
  2806. }
  2807. // MARK: - Analytics (埋点)
  2808. extension KMBrowserWindowController {
  2809. func trackEvent_upgrade() -> Void {
  2810. KMAnalytics.trackEvent(eventName: "Btn_Tbr_Upgrade", parameters: [
  2811. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.tbr,
  2812. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.tbr_Btn], platform: .AppCenter, appTarget: .all)
  2813. }
  2814. }