KMBrowserWindowController.swift 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130
  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 savePathOld = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1244. var savePath = self.filePathCheck(path: savePathOld)
  1245. let newDocument = CPDFDocument()
  1246. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1247. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1248. if writeSuccess ?? false {
  1249. if self.checkOpenNewDocument(path: savePath) {
  1250. self.savePdf(savePath)
  1251. }
  1252. // try? FileManager.default.removeItem(atPath: savePath)
  1253. } else {
  1254. }
  1255. }
  1256. }
  1257. }
  1258. }
  1259. func checkOpenNewDocument(path: String) -> Bool {
  1260. if !IAPProductsManager.default().isAvailableAllFunction() && self.browser.tabCount() > 1 {
  1261. let preferenceNoteShow = UserDefaults.standard.bool(forKey: KMTabbingHintShowFlag)
  1262. if preferenceNoteShow {
  1263. menuItemAction_newTagPageToNewWindow("")
  1264. } else {
  1265. if !KMDataManager.default.isTabbingWin{
  1266. KMDataManager.default.isTabbingWin = true
  1267. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  1268. tabbingWin.selectCallBack = {[weak self] continueOrNot in
  1269. KMDataManager.default.isTabbingWin = false
  1270. if continueOrNot {
  1271. self?.reopenDocumentForNewWindow(savePath: path)
  1272. } else {
  1273. }
  1274. }
  1275. self.km_beginSheet(windowC: tabbingWin)
  1276. }
  1277. }
  1278. return false
  1279. }
  1280. return true
  1281. }
  1282. func reopenDocumentForNewWindow(savePath: String) {
  1283. let browser = KMBrowser.init() as KMBrowser
  1284. browser.windowController = KMBrowserWindowController.init(browser: browser)
  1285. browser.addHomeTabContents()
  1286. browser.windowController.showWindow(self)
  1287. self.savePdf(savePath)
  1288. }
  1289. func filePathCheck(path: String) -> String {
  1290. var i: Int = 0
  1291. let fileURL = URL(fileURLWithPath: path)
  1292. var newPath: String = path
  1293. let fileManager = FileManager.default
  1294. while fileManager.fileExists(atPath: newPath) {
  1295. i += 1
  1296. newPath = fileURL.deletingPathExtension().path
  1297. newPath.append("(\(i))")
  1298. newPath.append(".")
  1299. newPath.append(fileURL.pathExtension)
  1300. }
  1301. return newPath
  1302. }
  1303. @IBAction func screenShot_Window(_ sender: Any?) {
  1304. KMPrint("screenShot_Window")
  1305. KMScreenShotHandler.beginScreenShot_SelectWindowCompleteHandler { ima in
  1306. if let image = ima {
  1307. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1308. let savePathOld = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1309. var savePath = self.filePathCheck(path: savePathOld)
  1310. let newDocument = CPDFDocument()
  1311. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1312. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1313. if writeSuccess == true {
  1314. if self.checkOpenNewDocument(path: savePath) {
  1315. self.savePdf(savePath)
  1316. }
  1317. // try? FileManager.default.removeItem(atPath: savePath)
  1318. } else {
  1319. }
  1320. }
  1321. }
  1322. }
  1323. }
  1324. @IBAction func screenShot_FullScreenDelay(_ sender: Any?) {
  1325. KMPrint("screenShot_FullScreenDelay")
  1326. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(3) { ima in
  1327. if let image = ima {
  1328. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1329. let savePathOld = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1330. var savePath = self.filePathCheck(path: savePathOld)
  1331. let newDocument = CPDFDocument()
  1332. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1333. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1334. if writeSuccess == true {
  1335. if self.checkOpenNewDocument(path: savePath) {
  1336. self.savePdf(savePath)
  1337. }
  1338. // try? FileManager.default.removeItem(atPath: savePath)
  1339. } else {
  1340. }
  1341. }
  1342. }
  1343. }
  1344. }
  1345. @IBAction func screenShot_FullScreen(_ sender: Any?) {
  1346. KMPrint("screenShot_FullScreen")
  1347. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(0) { ima in
  1348. if let image = ima {
  1349. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1350. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1351. let newDocument = CPDFDocument()
  1352. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1353. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1354. if writeSuccess == true {
  1355. self.savePdf(savePath)
  1356. // try? FileManager.default.removeItem(atPath: savePath)
  1357. } else {
  1358. }
  1359. }
  1360. }
  1361. }
  1362. }
  1363. }
  1364. // MARK: - KMSystemViewMenuProtocol
  1365. extension KMBrowserWindowController: KMSystemViewMenuProtocol {
  1366. func createPDFFromImage() {
  1367. let openPanel = NSOpenPanel()
  1368. openPanel.allowedFileTypes = KMImageAccessoryController.supportedImageTypes()
  1369. openPanel.allowsMultipleSelection = true
  1370. 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: "")
  1371. openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1372. if response == .OK {
  1373. self.creatPdfFromImg(urls: openPanel.urls)
  1374. }
  1375. }
  1376. }
  1377. func creatPdfFromImg(urls: Array<URL>) {
  1378. // let savePath = self.newDocumentTempSavePath("convertToPDF.pdf")
  1379. let pdf = CPDFDocument()
  1380. // var isSucceed = true
  1381. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1382. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1383. let filePath = savePath.deletingLastPathComponent
  1384. if FileManager.default.fileExists(atPath: filePath) == false {
  1385. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1386. }
  1387. for fileURL in urls {
  1388. if let imag = NSImage(contentsOfFile: fileURL.path ) {
  1389. _ = pdf?.km_insertPage(imag.size, withImage: fileURL.path , at: pdf?.pageCount ?? 0)
  1390. pdf?.write(to: URL(fileURLWithPath: savePath))
  1391. }
  1392. }
  1393. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1394. if error != nil {
  1395. NSApp.presentError(error!)
  1396. } else {
  1397. if document is KMMainDocument {
  1398. let newDocument = document
  1399. (newDocument as! KMMainDocument).isNewCreated = true
  1400. }
  1401. }
  1402. // self.homeContentView?.reloadData()
  1403. }
  1404. }
  1405. func createPdfWithParam() {
  1406. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1407. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1408. let filePath = savePath.deletingLastPathComponent
  1409. if FileManager.default.fileExists(atPath: filePath) == false {
  1410. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1411. }
  1412. let pdfDocument = CPDFDocument()
  1413. pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
  1414. pdfDocument?.write(to: URL(fileURLWithPath: savePath))
  1415. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1416. if error != nil {
  1417. NSApp.presentError(error!)
  1418. } else {
  1419. if document is KMMainDocument {
  1420. let newDocument = document
  1421. (newDocument as! KMMainDocument).isNewCreated = true
  1422. }
  1423. }
  1424. // self.homeContentView?.reloadData()
  1425. }
  1426. }
  1427. func savePdf(_ filePath: String) -> Void {
  1428. let docVc = KMDocumentController.shared
  1429. docVc.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
  1430. if error != nil {
  1431. NSApp.presentError(error!)
  1432. } else {
  1433. if document is KMMainDocument {
  1434. let newDocument = document
  1435. (newDocument as! KMMainDocument).isNewCreated = true
  1436. }
  1437. }
  1438. }
  1439. }
  1440. func newDocumentTempSavePath(_ fileName: String) -> String {
  1441. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  1442. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  1443. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  1444. return append2
  1445. }
  1446. func fetchUniquePath(_ originalPath: String) -> String {
  1447. var path = originalPath
  1448. let dManager = FileManager.default
  1449. if !dManager.fileExists(atPath: path) {
  1450. if path.extension.count < 1 {
  1451. path = path.stringByAppendingPathExtension("pdf")
  1452. }
  1453. return path
  1454. } else {
  1455. let originalFullFileName = path.lastPathComponent
  1456. let originalFileName = path.lastPathComponent.deletingPathExtension
  1457. let originalExtension = path.extension
  1458. let startIndex: Int = 0
  1459. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  1460. let fileLocatePath = originalPath.substring(to: endIndex)
  1461. var i = 1
  1462. while (1 != 0) {
  1463. var newName = String(format: "%@%ld", originalFileName, i)
  1464. newName = newName.stringByAppendingPathExtension(originalExtension)
  1465. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  1466. if !dManager.fileExists(atPath: newPath) {
  1467. return newPath
  1468. } else {
  1469. i+=1
  1470. continue
  1471. }
  1472. }
  1473. }
  1474. }
  1475. func menuItemAction_adjustWidth(_ sender: Any) {
  1476. if (self.canResponseDocumentAction() == false) {
  1477. return
  1478. }
  1479. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1480. document.mainViewController?.menuItemAction_adjustWidth(sender)
  1481. }
  1482. }
  1483. func menuItemAction_adjustPage(_ sender: Any) {
  1484. if (self.canResponseDocumentAction() == false) {
  1485. return
  1486. }
  1487. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1488. document.mainViewController?.menuItemAction_adjustPage(sender)
  1489. }
  1490. }
  1491. func menuItemAction_size(_ sender: Any) {
  1492. if (self.canResponseDocumentAction() == false) {
  1493. return
  1494. }
  1495. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1496. document.mainViewController?.menuItemAction_size(sender)
  1497. }
  1498. }
  1499. func menuItemAction_zoomOut(_ sender: Any) {
  1500. if (self.canResponseDocumentAction() == false) {
  1501. return
  1502. }
  1503. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1504. document.mainViewController?.menuItemAction_zoomOut(sender)
  1505. }
  1506. }
  1507. func menuItemAction_zoomIn(_ sender: Any) {
  1508. if (self.canResponseDocumentAction() == false) {
  1509. return
  1510. }
  1511. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1512. document.mainViewController?.menuItemAction_zoomIn(sender)
  1513. }
  1514. }
  1515. func menuItemAction_singlePage(_ sender: Any) {
  1516. if (self.canResponseDocumentAction() == false) {
  1517. return
  1518. }
  1519. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1520. document.mainViewController?.menuItemAction_singlePage(sender)
  1521. }
  1522. }
  1523. func menuItemAction_singlePageContinue(_ sender: Any) {
  1524. if (self.canResponseDocumentAction() == false) {
  1525. return
  1526. }
  1527. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1528. document.mainViewController?.menuItemAction_singlePageContinue(sender)
  1529. }
  1530. }
  1531. func menuItemAction_doublePage(_ sender: Any) {
  1532. if (self.canResponseDocumentAction() == false) {
  1533. return
  1534. }
  1535. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1536. document.mainViewController?.menuItemAction_doublePage(sender)
  1537. }
  1538. }
  1539. func menuItemAction_doublePageContinue(_ sender: Any) {
  1540. if (self.canResponseDocumentAction() == false) {
  1541. return
  1542. }
  1543. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1544. document.mainViewController?.menuItemAction_doublePageContinue(sender)
  1545. }
  1546. }
  1547. func menuItemAction_bookMode(_ sender: Any) {
  1548. if (self.canResponseDocumentAction() == false) {
  1549. return
  1550. }
  1551. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1552. document.mainViewController?.menuItemAction_bookMode(sender)
  1553. }
  1554. }
  1555. func menuItemAction_readMode(_ sender: Any) {
  1556. if (self.canResponseDocumentAction() == false) {
  1557. return
  1558. }
  1559. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1560. document.mainViewController?.menuItemAction_readMode(sender)
  1561. }
  1562. }
  1563. func menuItemAction_showSplitPage(_ sender: Any) {
  1564. if (self.canResponseDocumentAction() == false) {
  1565. return
  1566. }
  1567. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1568. document.mainViewController?.menuItemAction_showSplitPage(sender)
  1569. }
  1570. }
  1571. func menuItemAction_rotateLeft(_ sender: Any) {
  1572. if (self.canResponseDocumentAction() == false) {
  1573. return
  1574. }
  1575. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1576. document.mainViewController?.menuItemAction_rotateLeft(sender)
  1577. }
  1578. }
  1579. func menuItemAction_rotateRight(_ sender: Any) {
  1580. if (self.canResponseDocumentAction() == false) {
  1581. return
  1582. }
  1583. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1584. document.mainViewController?.menuItemAction_rotateRight(sender)
  1585. }
  1586. }
  1587. func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  1588. if (self.canResponseDocumentAction() == false) {
  1589. return
  1590. }
  1591. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1592. document.mainViewController?.menuItemAction_rotateAllPageLeft(sender)
  1593. }
  1594. }
  1595. func menuItemAction_rotateAllPageRight(_ sender: Any) {
  1596. if (self.canResponseDocumentAction() == false) {
  1597. return
  1598. }
  1599. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1600. document.mainViewController?.menuItemAction_rotateAllPageRight(sender)
  1601. }
  1602. }
  1603. func menuItemAction_view_readMode(_ sender: Any) {
  1604. if (self.canResponseDocumentAction() == false) {
  1605. return
  1606. }
  1607. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1608. document.mainViewController?.menuItemAction_view_readMode(sender)
  1609. }
  1610. }
  1611. func menuItemAction_hiddenLeftSide(_ sender: Any) {
  1612. if (self.canResponseDocumentAction() == false) {
  1613. return
  1614. }
  1615. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1616. document.mainViewController?.menuItemAction_hiddenLeftSide(sender)
  1617. }
  1618. }
  1619. func menuItemAction_hiddenRightSide(_ sender: Any) {
  1620. if (self.canResponseDocumentAction() == false) {
  1621. return
  1622. }
  1623. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1624. document.mainViewController?.menuItemAction_hiddenRightSide(sender)
  1625. }
  1626. }
  1627. func menuItemAction_thumai(_ sender: Any) {
  1628. if (self.canResponseDocumentAction() == false) {
  1629. return
  1630. }
  1631. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1632. document.mainViewController?.menuItemAction_thumai(sender)
  1633. }
  1634. }
  1635. func menuItemAction_outline(_ sender: Any) {
  1636. if (self.canResponseDocumentAction() == false) {
  1637. return
  1638. }
  1639. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1640. document.mainViewController?.menuItemAction_outline(sender)
  1641. }
  1642. }
  1643. func menuItemAction_bookmark(_ sender: Any) {
  1644. if (self.canResponseDocumentAction() == false) {
  1645. return
  1646. }
  1647. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1648. document.mainViewController?.menuItemAction_bookmark(sender)
  1649. }
  1650. }
  1651. func menuItemAction_annotation(_ sender: Any) {
  1652. if (self.canResponseDocumentAction() == false) {
  1653. return
  1654. }
  1655. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1656. document.mainViewController?.menuItemAction_annotation(sender)
  1657. }
  1658. }
  1659. func menuItemAction_search(_ sender: Any) {
  1660. if (self.canResponseDocumentAction() == false) {
  1661. return
  1662. }
  1663. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1664. document.mainViewController?.menuItemAction_search(sender)
  1665. }
  1666. }
  1667. func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  1668. if (self.canResponseDocumentAction() == false) {
  1669. return
  1670. }
  1671. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1672. document.mainViewController?.menuItemAction_hiddenPageIndicator(sender)
  1673. }
  1674. }
  1675. @IBAction func togglePresentation(_ sender: Any?) {
  1676. if (self.canResponseDocumentAction() == false) {
  1677. return
  1678. }
  1679. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1680. document.mainViewController?.togglePresentation(sender)
  1681. }
  1682. }
  1683. @IBAction func chooseTransition(_ sender: Any?) {
  1684. if (self.canResponseDocumentAction() == false) {
  1685. return
  1686. }
  1687. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1688. document.mainViewController?.chooseTransition(sender)
  1689. }
  1690. }
  1691. @IBAction func toggleAutoFlow(_ sender: Any?) {
  1692. if (self.canResponseDocumentAction() == false) {
  1693. return
  1694. }
  1695. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1696. document.mainViewController?.toggleAutoFlow(sender)
  1697. }
  1698. }
  1699. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  1700. if (self.canResponseDocumentAction() == false) {
  1701. return
  1702. }
  1703. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1704. document.mainViewController?.chooseAutoFlowSetting(sender)
  1705. }
  1706. }
  1707. @IBAction func toggleReadingBar(_ sender: Any?) {
  1708. if (self.canResponseDocumentAction() == false) {
  1709. return
  1710. }
  1711. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1712. document.mainViewController?.toggleReadingBar(sender)
  1713. }
  1714. }
  1715. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  1716. if (self.canResponseDocumentAction() == false) {
  1717. return
  1718. }
  1719. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1720. document.mainViewController?.toggleLeftSidePane(sender)
  1721. }
  1722. }
  1723. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  1724. if (self.canResponseDocumentAction() == false) {
  1725. return
  1726. }
  1727. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1728. document.mainViewController?.changeLeftSidePaneState(sender)
  1729. }
  1730. }
  1731. @IBAction func splitViewAction(_ sender: Any?) {
  1732. if (self.canResponseDocumentAction() == false) {
  1733. return
  1734. }
  1735. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1736. document.mainViewController?.splitViewAction(sender)
  1737. }
  1738. }
  1739. @IBAction func themesColor(_ sender: Any?) {
  1740. if (self.canResponseDocumentAction() == false) {
  1741. return
  1742. }
  1743. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1744. document.mainViewController?.themesColor(sender)
  1745. }
  1746. }
  1747. @IBAction func toggleStatusBar(_ sender: Any?) {
  1748. if (self.canResponseDocumentAction() == false) {
  1749. return
  1750. }
  1751. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1752. document.mainViewController?.toggleStatusBar(sender)
  1753. }
  1754. }
  1755. @IBAction func toggleToolbarShow(_ sender: Any?) {
  1756. if (self.canResponseDocumentAction() == false) {
  1757. return
  1758. }
  1759. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1760. document.mainViewController?.toggleToolbarShow(sender)
  1761. }
  1762. }
  1763. }
  1764. // MARK: - KMSystemEditMenuProtocol
  1765. extension KMBrowserWindowController: KMSystemEditMenuProtocol {
  1766. func menuItemAction_find(_ sender: Any) {
  1767. if (self.canResponseDocumentAction() == false) {
  1768. return
  1769. }
  1770. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1771. document.mainViewController?.menuItemAction_find(sender)
  1772. }
  1773. }
  1774. @IBAction func changeFont(_ sender: Any?) {
  1775. KMPrint("changeFont ...")
  1776. }
  1777. }
  1778. // MARK: - Annotation Menu
  1779. extension KMBrowserWindowController {
  1780. @IBAction func menuItemAction_highlight(_ sender: Any) {
  1781. if (self.canResponseDocumentAction() == false) {
  1782. return
  1783. }
  1784. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1785. document.mainViewController?.menuItemAction_highlight(sender)
  1786. }
  1787. }
  1788. @IBAction func menuItemAction_underline(_ sender: Any) {
  1789. if (self.canResponseDocumentAction() == false) {
  1790. return
  1791. }
  1792. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1793. document.mainViewController?.menuItemAction_underline(sender)
  1794. }
  1795. }
  1796. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  1797. if (self.canResponseDocumentAction() == false) {
  1798. return
  1799. }
  1800. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1801. document.mainViewController?.menuItemAction_deleteLine(sender)
  1802. }
  1803. }
  1804. @IBAction func menuItemAction_freehand(_ sender: Any) {
  1805. if (self.canResponseDocumentAction() == false) {
  1806. return
  1807. }
  1808. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1809. document.mainViewController?.menuItemAction_freehand(sender)
  1810. }
  1811. }
  1812. @IBAction func menuItemAction_text(_ sender: Any) {
  1813. if (self.canResponseDocumentAction() == false) {
  1814. return
  1815. }
  1816. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1817. document.mainViewController?.menuItemAction_text(sender)
  1818. }
  1819. }
  1820. @IBAction func menuItemAction_note(_ sender: Any) {
  1821. if (self.canResponseDocumentAction() == false) {
  1822. return
  1823. }
  1824. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1825. document.mainViewController?.menuItemAction_note(sender)
  1826. }
  1827. }
  1828. @IBAction func menuItemAction_squre(_ sender: Any) {
  1829. if (self.canResponseDocumentAction() == false) {
  1830. return
  1831. }
  1832. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1833. document.mainViewController?.menuItemAction_squre(sender)
  1834. }
  1835. }
  1836. @IBAction func menuItemAction_circle(_ sender: Any) {
  1837. if (self.canResponseDocumentAction() == false) {
  1838. return
  1839. }
  1840. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1841. document.mainViewController?.menuItemAction_circle(sender)
  1842. }
  1843. }
  1844. @IBAction func menuItemAction_arrow(_ sender: Any) {
  1845. if (self.canResponseDocumentAction() == false) {
  1846. return
  1847. }
  1848. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1849. document.mainViewController?.menuItemAction_arrow(sender)
  1850. }
  1851. }
  1852. @IBAction func menuItemAction_line(_ sender: Any) {
  1853. if (self.canResponseDocumentAction() == false) {
  1854. return
  1855. }
  1856. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1857. document.mainViewController?.menuItemAction_line(sender)
  1858. }
  1859. }
  1860. // link
  1861. @IBAction func menuItemAction_link(_ sender: Any) {
  1862. if (self.canResponseDocumentAction() == false) {
  1863. return
  1864. }
  1865. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1866. document.mainViewController?.menuItemAction_link(sender)
  1867. }
  1868. }
  1869. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  1870. if (self.canResponseDocumentAction() == false) {
  1871. return
  1872. }
  1873. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1874. document.mainViewController?.menuItemAction_linkPage(sender)
  1875. }
  1876. }
  1877. // stamp
  1878. @IBAction func menuItemAction_stamp(_ sender: Any) {
  1879. if (self.canResponseDocumentAction() == false) {
  1880. return
  1881. }
  1882. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1883. document.mainViewController?.menuItemAction_stamp(sender)
  1884. }
  1885. }
  1886. @IBAction func notesLink(_ sender: Any?) {
  1887. if (self.canResponseDocumentAction() == false) {
  1888. return
  1889. }
  1890. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1891. document.mainViewController?.notesLink(sender)
  1892. }
  1893. }
  1894. @IBAction func notesStamp(_ sender: Any?) {
  1895. if (self.canResponseDocumentAction() == false) {
  1896. return
  1897. }
  1898. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1899. document.mainViewController?.notesStamp(sender)
  1900. }
  1901. }
  1902. @IBAction func menuItemAction_signure(_ sender: Any) {
  1903. if (self.canResponseDocumentAction() == false) {
  1904. return
  1905. }
  1906. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1907. document.mainViewController?.menuItemAction_signure(sender)
  1908. }
  1909. }
  1910. @IBAction func image(_ sender: Any?) {
  1911. if (self.canResponseDocumentAction() == false) {
  1912. return
  1913. }
  1914. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1915. document.mainViewController?.image(sender)
  1916. }
  1917. }
  1918. @IBAction func table(_ sender: Any?) {
  1919. if (self.canResponseDocumentAction() == false) {
  1920. return
  1921. }
  1922. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1923. document.mainViewController?.table(sender)
  1924. }
  1925. }
  1926. @IBAction func addForm(_ sender: Any?) {
  1927. if (self.canResponseDocumentAction() == false) {
  1928. return
  1929. }
  1930. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1931. document.mainViewController?.addForm(sender)
  1932. }
  1933. }
  1934. @IBAction func editNote(_ sender: Any?) {
  1935. if (self.canResponseDocumentAction() == false) {
  1936. return
  1937. }
  1938. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1939. document.mainViewController?.editNote(sender)
  1940. }
  1941. }
  1942. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  1943. if (self.canResponseDocumentAction() == false) {
  1944. return
  1945. }
  1946. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1947. document.mainViewController?.menuItemAction_hiddenAllAnnotation(sender)
  1948. }
  1949. }
  1950. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  1951. if (self.canResponseDocumentAction() == false) {
  1952. return
  1953. }
  1954. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1955. document.mainViewController?.menuItemAction_clearAllAnnotation(sender)
  1956. }
  1957. }
  1958. }
  1959. // MARK: - KMSystemPDFMenuProtocol
  1960. extension KMBrowserWindowController: KMSystemPDFMenuProtocol {
  1961. @IBAction func changeDisplayMode(_ sender: Any?) {
  1962. if (self.canResponseDocumentAction() == false) {
  1963. return
  1964. }
  1965. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1966. document.mainViewController?.changeDisplayMode(sender)
  1967. }
  1968. }
  1969. @IBAction func toggleDisplayAsBook(_ sender: Any?) {
  1970. if (self.canResponseDocumentAction() == false) {
  1971. return
  1972. }
  1973. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1974. document.mainViewController?.toggleDisplayAsBook(sender)
  1975. }
  1976. }
  1977. @IBAction func toggleDisplayPageBreaks(_ sender: Any?) {
  1978. if (self.canResponseDocumentAction() == false) {
  1979. return
  1980. }
  1981. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1982. document.mainViewController?.toggleDisplayPageBreaks(sender)
  1983. }
  1984. }
  1985. @IBAction func changeDisplayBox(_ sender: Any?) {
  1986. if (self.canResponseDocumentAction() == false) {
  1987. return
  1988. }
  1989. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1990. document.mainViewController?.changeDisplayBox(sender)
  1991. }
  1992. }
  1993. @IBAction func toggleAutoScale(_ sender: Any?) {
  1994. if (self.canResponseDocumentAction() == false) {
  1995. return
  1996. }
  1997. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1998. document.mainViewController?.toggleAutoScale(sender)
  1999. }
  2000. }
  2001. @IBAction func doZoomToActualSize(_ sender: Any?) {
  2002. if (self.canResponseDocumentAction() == false) {
  2003. return
  2004. }
  2005. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2006. document.mainViewController?.doZoomToActualSize(sender)
  2007. }
  2008. }
  2009. @IBAction func doZoomToFit(_ sender: Any?) {
  2010. if (self.canResponseDocumentAction() == false) {
  2011. return
  2012. }
  2013. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2014. document.mainViewController?.doZoomToFit(sender)
  2015. }
  2016. }
  2017. @IBAction func doZoomIn(_ sender: Any?) {
  2018. if (self.canResponseDocumentAction() == false) {
  2019. return
  2020. }
  2021. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2022. document.mainViewController?.doZoomIn(sender)
  2023. }
  2024. }
  2025. @IBAction func doZoomOut(_ sender: Any?) {
  2026. if (self.canResponseDocumentAction() == false) {
  2027. return
  2028. }
  2029. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2030. document.mainViewController?.doZoomOut(sender)
  2031. }
  2032. }
  2033. @IBAction func doMarqueeZoomTool(_ sender: Any?) {
  2034. if (self.canResponseDocumentAction() == false) {
  2035. return
  2036. }
  2037. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2038. document.mainViewController?.doMarqueeZoomTool(sender)
  2039. }
  2040. }
  2041. @IBAction func highlightFormFiled(_ sender: Any?) {
  2042. if (self.canResponseDocumentAction() == false) {
  2043. return
  2044. }
  2045. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2046. document.mainViewController?.highlightFormFiled(sender)
  2047. }
  2048. }
  2049. @IBAction func highlightLinks(_ sender: Any?) {
  2050. if (self.canResponseDocumentAction() == false) {
  2051. return
  2052. }
  2053. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2054. document.mainViewController?.highlightLinks(sender)
  2055. }
  2056. }
  2057. @IBAction func resetForm(_ sender: Any?) {
  2058. if (self.canResponseDocumentAction() == false) {
  2059. return
  2060. }
  2061. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2062. document.mainViewController?.resetForm(sender)
  2063. }
  2064. }
  2065. @IBAction func removeAllAnnotation(_ sender: Any?) {
  2066. if (self.canResponseDocumentAction() == false) {
  2067. return
  2068. }
  2069. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2070. document.mainViewController?.removeAllAnnotation(sender)
  2071. }
  2072. }
  2073. @IBAction func savePDFSettingToDefaults(_ sender: Any?) {
  2074. if (self.canResponseDocumentAction() == false) {
  2075. return
  2076. }
  2077. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2078. document.mainViewController?.savePDFSettingToDefaults(sender)
  2079. }
  2080. }
  2081. }
  2082. // MARK: - KMSystemGotoMenuProtocol
  2083. extension KMBrowserWindowController: KMSystemGotoMenuProtocol {
  2084. func menuItemAction_nextPage(_ sender: Any) {
  2085. if (self.canResponseDocumentAction() == false) {
  2086. return
  2087. }
  2088. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2089. document.mainViewController?.menuItemAction_nextPage(sender)
  2090. }
  2091. }
  2092. func menuItemAction_forwardPage(_ sender: Any) {
  2093. if (self.canResponseDocumentAction() == false) {
  2094. return
  2095. }
  2096. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2097. document.mainViewController?.menuItemAction_forwardPage(sender)
  2098. }
  2099. }
  2100. func menuItemAction_firstPage(_ sender: Any) {
  2101. if (self.canResponseDocumentAction() == false) {
  2102. return
  2103. }
  2104. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2105. document.mainViewController?.menuItemAction_firstPage(sender)
  2106. }
  2107. }
  2108. func menuItemAction_lastPage(_ sender: Any) {
  2109. if (self.canResponseDocumentAction() == false) {
  2110. return
  2111. }
  2112. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2113. document.mainViewController?.menuItemAction_lastPage(sender)
  2114. }
  2115. }
  2116. func menuItemAction_forward(_ sender: Any) {
  2117. if (self.canResponseDocumentAction() == false) {
  2118. return
  2119. }
  2120. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2121. document.mainViewController?.menuItemAction_forward(sender)
  2122. }
  2123. }
  2124. func menuItemAction_goback(_ sender: Any) {
  2125. if (self.canResponseDocumentAction() == false) {
  2126. return
  2127. }
  2128. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2129. document.mainViewController?.menuItemAction_goback(sender)
  2130. }
  2131. }
  2132. func menuItemAction_gotoPage(_ sender: Any) {
  2133. if (self.canResponseDocumentAction() == false) {
  2134. return
  2135. }
  2136. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2137. document.mainViewController?.menuItemAction_gotoPage(sender)
  2138. }
  2139. }
  2140. @IBAction func goToMarkedPage(_ sender: Any?) {
  2141. if (self.canResponseDocumentAction() == false) {
  2142. return
  2143. }
  2144. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2145. document.mainViewController?.goToMarkedPage(sender)
  2146. }
  2147. }
  2148. @IBAction func markPage(_ sender: Any?) {
  2149. if (self.canResponseDocumentAction() == false) {
  2150. return
  2151. }
  2152. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2153. document.mainViewController?.markPage(sender)
  2154. }
  2155. }
  2156. }
  2157. // MARK: - KMSystemToolMenuProtocol
  2158. extension KMBrowserWindowController: KMSystemToolMenuProtocol {
  2159. @IBAction func getInfo(_ sender: Any?) {
  2160. if (self.canResponseDocumentAction() == false) {
  2161. return
  2162. }
  2163. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2164. document.mainViewController?.getInfo(sender)
  2165. }
  2166. }
  2167. func orderFrontNotesPanel(_ sender: Any?) {
  2168. let windowC = KMNotesPanelController.shared
  2169. if let win = windowC.window {
  2170. if win.isVisible {
  2171. win.orderOut(sender)
  2172. } else {
  2173. win.orderFront(sender)
  2174. }
  2175. }
  2176. }
  2177. func menuItemAction_scrolTool(_ sender: Any) {
  2178. if (self.canResponseDocumentAction() == false) {
  2179. return
  2180. }
  2181. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2182. document.mainViewController?.menuItemAction_scrolTool(sender)
  2183. }
  2184. }
  2185. func menuItemAction_zoomOutTool(_ sender: Any) {
  2186. if (self.canResponseDocumentAction() == false) {
  2187. return
  2188. }
  2189. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2190. document.mainViewController?.menuItemAction_zoomOutTool(sender)
  2191. }
  2192. }
  2193. func menuItemAction_selectTool(_ sender: Any) {
  2194. if (self.canResponseDocumentAction() == false) {
  2195. return
  2196. }
  2197. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2198. document.mainViewController?.menuItemAction_selectTool(sender)
  2199. }
  2200. }
  2201. @IBAction func changeToolMode(_ sender: Any?) {
  2202. if (self.canResponseDocumentAction() == false) {
  2203. return
  2204. }
  2205. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2206. if (document.isHome) {
  2207. } else {
  2208. document.mainViewController?.changeToolMode(sender)
  2209. }
  2210. }
  2211. }
  2212. @IBAction func compression(_ sender: Any?) {
  2213. if (self.canResponseDocumentAction() == false) {
  2214. return
  2215. }
  2216. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2217. if (document.isHome) {
  2218. } else {
  2219. document.mainViewController?.compression(sender)
  2220. }
  2221. }
  2222. }
  2223. @IBAction func merge(_ sender: Any?) {
  2224. if (self.canResponseDocumentAction() == false) {
  2225. return
  2226. }
  2227. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2228. if (document.isHome) {
  2229. } else {
  2230. document.mainViewController?.merge(sender)
  2231. }
  2232. }
  2233. }
  2234. @IBAction func convert(_ sender: Any?) {
  2235. if (self.canResponseDocumentAction() == false) {
  2236. return
  2237. }
  2238. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2239. if (document.isHome) {
  2240. } else {
  2241. document.mainViewController?.convert(sender)
  2242. }
  2243. }
  2244. }
  2245. @IBAction func imageToPDF(_ sender: Any?) {
  2246. if (self.canResponseDocumentAction() == false) {
  2247. return
  2248. }
  2249. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2250. if (document.isHome) {
  2251. } else {
  2252. document.mainViewController?.imageToPDF(sender)
  2253. }
  2254. }
  2255. }
  2256. @IBAction func ocr(_ sender: Any?) {
  2257. if (self.canResponseDocumentAction() == false) {
  2258. return
  2259. }
  2260. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2261. if (document.isHome) {
  2262. } else {
  2263. document.mainViewController?.ocr(sender)
  2264. }
  2265. }
  2266. }
  2267. @IBAction func extractImage(_ sender: Any?) {
  2268. if (self.canResponseDocumentAction() == false) {
  2269. return
  2270. }
  2271. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2272. if (document.isHome) {
  2273. } else {
  2274. document.mainViewController?.extractImage(sender)
  2275. }
  2276. }
  2277. }
  2278. @IBAction func pageEdit(_ sender: Any?) {
  2279. if (self.canResponseDocumentAction() == false) {
  2280. return
  2281. }
  2282. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2283. if (document.isHome) {
  2284. } else {
  2285. document.mainViewController?.pageEdit(sender)
  2286. }
  2287. }
  2288. }
  2289. @IBAction func textEditAction(_ sender: Any?) {
  2290. if (self.canResponseDocumentAction() == false) {
  2291. return
  2292. }
  2293. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2294. if (document.isHome) {
  2295. } else {
  2296. document.mainViewController?.textEditAction(sender)
  2297. }
  2298. }
  2299. }
  2300. @IBAction func security(_ sender: Any?) {
  2301. if (self.canResponseDocumentAction() == false) {
  2302. return
  2303. }
  2304. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2305. if (document.isHome) {
  2306. } else {
  2307. document.mainViewController?.security(sender)
  2308. }
  2309. }
  2310. }
  2311. @IBAction func decryption(_ sender: Any?) {
  2312. if (self.canResponseDocumentAction() == false) {
  2313. return
  2314. }
  2315. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2316. if (document.isHome) {
  2317. } else {
  2318. document.mainViewController?.decryption(sender)
  2319. }
  2320. }
  2321. }
  2322. @IBAction func cropAll(_ sender: Any?) {
  2323. if (self.canResponseDocumentAction() == false) {
  2324. return
  2325. }
  2326. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2327. if (document.isHome) {
  2328. } else {
  2329. document.mainViewController?.cropAll(sender)
  2330. }
  2331. }
  2332. }
  2333. @IBAction func crop(_ sender: Any?) {
  2334. if (self.canResponseDocumentAction() == false) {
  2335. return
  2336. }
  2337. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2338. if (document.isHome) {
  2339. } else {
  2340. document.mainViewController?.crop(sender)
  2341. }
  2342. }
  2343. }
  2344. @IBAction func autoCropAll(_ sender: Any?) {
  2345. if (self.canResponseDocumentAction() == false) {
  2346. return
  2347. }
  2348. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2349. if (document.isHome) {
  2350. } else {
  2351. document.mainViewController?.autoCropAll(sender)
  2352. }
  2353. }
  2354. }
  2355. @IBAction func smartAutoCropAll(_ sender: Any?) {
  2356. if (self.canResponseDocumentAction() == false) {
  2357. return
  2358. }
  2359. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2360. if (document.isHome) {
  2361. } else {
  2362. document.mainViewController?.smartAutoCropAll(sender)
  2363. }
  2364. }
  2365. }
  2366. @IBAction func cropOptionsModel(_ sender: Any?) {
  2367. if (self.canResponseDocumentAction() == false) {
  2368. return
  2369. }
  2370. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2371. if (document.isHome) {
  2372. } else {
  2373. // document.mainViewController?.cropOptionsModel(sender)
  2374. }
  2375. }
  2376. }
  2377. @IBAction func selectToolModel(_ sender: Any?) {
  2378. if (self.canResponseDocumentAction() == false) {
  2379. return
  2380. }
  2381. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2382. if (document.isHome) {
  2383. } else {
  2384. document.mainViewController?.selectToolModel(sender)
  2385. }
  2386. }
  2387. }
  2388. @IBAction func takeSnapshot(_ sender: Any?) {
  2389. if (self.canResponseDocumentAction() == false) {
  2390. return
  2391. }
  2392. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2393. if (document.isHome) {
  2394. } else {
  2395. document.mainViewController?.takeSnapshot(sender)
  2396. }
  2397. }
  2398. }
  2399. @IBAction func takeSnapshotSelectContent(_ sender: Any?) {
  2400. if (self.canResponseDocumentAction() == false) {
  2401. return
  2402. }
  2403. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2404. if (document.isHome) {
  2405. } else {
  2406. document.mainViewController?.takeSnapshotSelectContent(sender)
  2407. }
  2408. }
  2409. }
  2410. @IBAction func changeAnnotationMode(_ sender: Any?) {
  2411. if (self.canResponseDocumentAction() == false) {
  2412. return
  2413. }
  2414. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2415. if (document.isHome) {
  2416. } else {
  2417. document.mainViewController?.changeAnnotationMode(sender)
  2418. }
  2419. }
  2420. }
  2421. @IBAction func addBackground(_ sender: Any?) {
  2422. if (self.canResponseDocumentAction() == false) {
  2423. return
  2424. }
  2425. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2426. if (document.isHome) {
  2427. } else {
  2428. document.mainViewController?.addBackground(sender)
  2429. }
  2430. }
  2431. }
  2432. @IBAction func removeBackground(_ sender: Any?) {
  2433. if (self.canResponseDocumentAction() == false) {
  2434. return
  2435. }
  2436. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2437. if (document.isHome) {
  2438. } else {
  2439. document.mainViewController?.removeBackground(sender)
  2440. }
  2441. }
  2442. }
  2443. @IBAction func addWatermark(_ sender: Any?) {
  2444. if (self.canResponseDocumentAction() == false) {
  2445. return
  2446. }
  2447. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2448. if (document.isHome) {
  2449. } else {
  2450. document.mainViewController?.addWatermark(sender)
  2451. }
  2452. }
  2453. }
  2454. @IBAction func editWatermark(_ sender: Any?) {
  2455. KMPrint("editWatermark ...")
  2456. }
  2457. @IBAction func batchAddWatermark(_ sender: Any?) {
  2458. if (self.canResponseDocumentAction() == false) {
  2459. return
  2460. }
  2461. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2462. if (document.isHome) {
  2463. } else {
  2464. document.mainViewController?.batchAddWatermark(sender)
  2465. }
  2466. }
  2467. }
  2468. @IBAction func batchRemoveWatermark(_ sender: Any?) {
  2469. if (self.canResponseDocumentAction() == false) {
  2470. return
  2471. }
  2472. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2473. if (document.isHome) {
  2474. } else {
  2475. document.mainViewController?.batchRemoveWatermark(sender)
  2476. }
  2477. }
  2478. }
  2479. @IBAction func removeWatermark(_ sender: Any?) {
  2480. if (self.canResponseDocumentAction() == false) {
  2481. return
  2482. }
  2483. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2484. if (document.isHome) {
  2485. } else {
  2486. document.mainViewController?.removeWatermark(sender)
  2487. }
  2488. }
  2489. }
  2490. @IBAction func templateWatermark(_ sender: Any?) {
  2491. if (self.canResponseDocumentAction() == false) {
  2492. return
  2493. }
  2494. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2495. if (document.isHome) {
  2496. } else {
  2497. document.mainViewController?.templateWatermark(sender)
  2498. }
  2499. }
  2500. }
  2501. @IBAction func batchAddBackground(_ sender: Any?) {
  2502. if (self.canResponseDocumentAction() == false) {
  2503. return
  2504. }
  2505. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2506. if (document.isHome) {
  2507. } else {
  2508. document.mainViewController?.batchAddBackground(sender)
  2509. }
  2510. }
  2511. }
  2512. @IBAction func batchRemoveBackground(_ sender: Any?) {
  2513. if (self.canResponseDocumentAction() == false) {
  2514. return
  2515. }
  2516. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2517. if (document.isHome) {
  2518. } else {
  2519. document.mainViewController?.batchRemoveBackground(sender)
  2520. }
  2521. }
  2522. }
  2523. @IBAction func templateBackground(_ sender: Any?) {
  2524. if (self.canResponseDocumentAction() == false) {
  2525. return
  2526. }
  2527. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2528. if (document.isHome) {
  2529. } else {
  2530. document.mainViewController?.templateBackground(sender)
  2531. }
  2532. }
  2533. }
  2534. @IBAction func addHeaderFooter(_ sender: Any?) {
  2535. if (self.canResponseDocumentAction() == false) {
  2536. return
  2537. }
  2538. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2539. if (document.isHome) {
  2540. } else {
  2541. document.mainViewController?.addHeaderFooter(sender)
  2542. }
  2543. }
  2544. }
  2545. @IBAction func removeHeaderFooter(_ sender: Any?) {
  2546. if (self.canResponseDocumentAction() == false) {
  2547. return
  2548. }
  2549. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2550. if (document.isHome) {
  2551. } else {
  2552. document.mainViewController?.removeHeaderFooter(sender)
  2553. }
  2554. }
  2555. }
  2556. @IBAction func batchAddHeaderFooter(_ sender: Any?) {
  2557. if (self.canResponseDocumentAction() == false) {
  2558. return
  2559. }
  2560. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2561. if (document.isHome) {
  2562. } else {
  2563. document.mainViewController?.batchAddHeaderFooter(sender)
  2564. }
  2565. }
  2566. }
  2567. @IBAction func batchRemoveHeaderFooter(_ sender: Any?) {
  2568. if (self.canResponseDocumentAction() == false) {
  2569. return
  2570. }
  2571. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2572. if (document.isHome) {
  2573. } else {
  2574. document.mainViewController?.batchRemoveHeaderFooter(sender)
  2575. }
  2576. }
  2577. }
  2578. @IBAction func templateHeaderFooter(_ sender: Any?) {
  2579. if (self.canResponseDocumentAction() == false) {
  2580. return
  2581. }
  2582. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2583. if (document.isHome) {
  2584. } else {
  2585. document.mainViewController?.templateHeaderFooter(sender)
  2586. }
  2587. }
  2588. }
  2589. @IBAction func addHeaderFooterBates(_ sender: Any?) {
  2590. if (self.canResponseDocumentAction() == false) {
  2591. return
  2592. }
  2593. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2594. if (document.isHome) {
  2595. } else {
  2596. document.mainViewController?.addHeaderFooterBates(sender)
  2597. }
  2598. }
  2599. }
  2600. @IBAction func removeHeaderFooterBates(_ sender: Any?) {
  2601. if (self.canResponseDocumentAction() == false) {
  2602. return
  2603. }
  2604. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2605. if (document.isHome) {
  2606. } else {
  2607. document.mainViewController?.removeHeaderFooterBates(sender)
  2608. }
  2609. }
  2610. }
  2611. @IBAction func batchAddBates(_ sender: Any?) {
  2612. if (self.canResponseDocumentAction() == false) {
  2613. return
  2614. }
  2615. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2616. if (document.isHome) {
  2617. } else {
  2618. document.mainViewController?.batchAddBates(sender)
  2619. }
  2620. }
  2621. }
  2622. @IBAction func batchRemoveBates(_ sender: Any?) {
  2623. if (self.canResponseDocumentAction() == false) {
  2624. return
  2625. }
  2626. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2627. if (document.isHome) {
  2628. } else {
  2629. document.mainViewController?.batchRemoveBates(sender)
  2630. }
  2631. }
  2632. }
  2633. @IBAction func templateBates(_ sender: Any?) {
  2634. if (self.canResponseDocumentAction() == false) {
  2635. return
  2636. }
  2637. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2638. if (document.isHome) {
  2639. } else {
  2640. document.mainViewController?.templateBates(sender)
  2641. }
  2642. }
  2643. }
  2644. @IBAction func togglePoster(_ sender: Any?) {
  2645. if (self.canResponseDocumentAction() == false) {
  2646. return
  2647. }
  2648. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2649. if (document.isHome) {
  2650. } else {
  2651. document.mainViewController?.togglePoster(sender)
  2652. }
  2653. }
  2654. }
  2655. @IBAction func toggleMultiple(_ sender: Any?) {
  2656. if (self.canResponseDocumentAction() == false) {
  2657. return
  2658. }
  2659. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2660. if (document.isHome) {
  2661. } else {
  2662. document.mainViewController?.toggleMultiple(sender)
  2663. }
  2664. }
  2665. }
  2666. @IBAction func toggleBooklet(_ sender: Any?) {
  2667. if (self.canResponseDocumentAction() == false) {
  2668. return
  2669. }
  2670. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2671. if (document.isHome) {
  2672. } else {
  2673. document.mainViewController?.toggleBooklet(sender)
  2674. }
  2675. }
  2676. }
  2677. @IBAction func rotateRight(_ sender: Any?) {
  2678. if (self.canResponseDocumentAction() == false) {
  2679. return
  2680. }
  2681. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2682. if (document.isHome) {
  2683. } else {
  2684. document.mainViewController?.rotateRight(sender)
  2685. }
  2686. }
  2687. }
  2688. @IBAction func rotateLeft(_ sender: Any?) {
  2689. if (self.canResponseDocumentAction() == false) {
  2690. return
  2691. }
  2692. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2693. if (document.isHome) {
  2694. } else {
  2695. document.mainViewController?.rotateLeft(sender)
  2696. }
  2697. }
  2698. }
  2699. @IBAction func rotateAllRight(_ sender: Any?) {
  2700. if (self.canResponseDocumentAction() == false) {
  2701. return
  2702. }
  2703. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2704. if (document.isHome) {
  2705. } else {
  2706. document.mainViewController?.rotateAllRight(sender)
  2707. }
  2708. }
  2709. }
  2710. @IBAction func rotateAllLeft(_ sender: Any?) {
  2711. if (self.canResponseDocumentAction() == false) {
  2712. return
  2713. }
  2714. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2715. if (document.isHome) {
  2716. } else {
  2717. document.mainViewController?.rotateAllLeft(sender)
  2718. }
  2719. }
  2720. }
  2721. @IBAction func autoSelectContent(_ sender: Any?) {
  2722. if (self.canResponseDocumentAction() == false) {
  2723. return
  2724. }
  2725. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2726. if (document.isHome) {
  2727. } else {
  2728. document.mainViewController?.autoSelectContent(sender)
  2729. }
  2730. }
  2731. }
  2732. }
  2733. // MARK: - KMSystemWindowMenuRrotocol
  2734. extension KMBrowserWindowController: KMSystemWindowMenuRrotocol {
  2735. func menuItemAction_showForwardTagPage(_ sender: Any) {
  2736. if (self.canResponseDocumentAction() == false) {
  2737. return
  2738. }
  2739. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2740. if (document.isHome) {
  2741. document.homeViewController?.menuItemAction_showForwardTagPage(sender)
  2742. } else {
  2743. document.mainViewController?.menuItemAction_showForwardTagPage(sender)
  2744. }
  2745. }
  2746. }
  2747. func menuItemAction_showNextTagPage(_ 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_showNextTagPage(sender)
  2754. } else {
  2755. document.mainViewController?.menuItemAction_showNextTagPage(sender)
  2756. }
  2757. }
  2758. }
  2759. func menuItemAction_newTagPageToNewWindow(_ 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_newTagPageToNewWindow(sender)
  2766. } else {
  2767. document.mainViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2768. }
  2769. }
  2770. }
  2771. func menuItemAction_mergeAllWindow(_ 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_mergeAllWindow(sender)
  2778. } else {
  2779. document.mainViewController?.menuItemAction_mergeAllWindow(sender)
  2780. }
  2781. }
  2782. }
  2783. @IBAction func performFit(_ sender: Any?) {
  2784. if (self.canResponseDocumentAction() == false) {
  2785. return
  2786. }
  2787. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2788. if (document.isHome) {
  2789. } else {
  2790. document.mainViewController?.performFit(sender)
  2791. }
  2792. }
  2793. }
  2794. }
  2795. // MARK: - Analytics (埋点)
  2796. extension KMBrowserWindowController {
  2797. func trackEvent_upgrade() -> Void {
  2798. KMAnalytics.trackEvent(eventName: "Btn_Tbr_Upgrade", parameters: [
  2799. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.tbr,
  2800. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.tbr_Btn], platform: .AppCenter, appTarget: .all)
  2801. }
  2802. }