KMBrowserWindowController.swift 101 KB

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