KMBrowserWindowController.swift 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  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. _ = 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. KMScreenShotHandler.beginScreenshot_SelectRectWithCompleteHandler { ima in
  1128. if let image = ima {
  1129. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1130. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1131. let newDocument = CPDFDocument()
  1132. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1133. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1134. if writeSuccess ?? false {
  1135. self.savePdf(savePath)
  1136. // try? FileManager.default.removeItem(atPath: savePath)
  1137. } else {
  1138. }
  1139. }
  1140. }
  1141. }
  1142. }
  1143. @IBAction func screenShot_Window(_ sender: Any?) {
  1144. KMPrint("screenShot_Window")
  1145. KMScreenShotHandler.beginScreenShot_SelectWindowCompleteHandler { ima in
  1146. if let image = ima {
  1147. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1148. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1149. let newDocument = CPDFDocument()
  1150. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1151. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1152. if writeSuccess == true {
  1153. self.savePdf(savePath)
  1154. // try? FileManager.default.removeItem(atPath: savePath)
  1155. } else {
  1156. }
  1157. }
  1158. }
  1159. }
  1160. }
  1161. @IBAction func screenShot_FullScreenDelay(_ sender: Any?) {
  1162. KMPrint("screenShot_FullScreenDelay")
  1163. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(3) { ima in
  1164. if let image = ima {
  1165. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1166. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1167. let newDocument = CPDFDocument()
  1168. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1169. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1170. if writeSuccess == true {
  1171. self.savePdf(savePath)
  1172. // try? FileManager.default.removeItem(atPath: savePath)
  1173. } else {
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. @IBAction func screenShot_FullScreen(_ sender: Any?) {
  1180. KMPrint("screenShot_FullScreen")
  1181. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(0) { ima in
  1182. if let image = ima {
  1183. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1184. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1185. let newDocument = CPDFDocument()
  1186. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1187. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1188. if writeSuccess == true {
  1189. self.savePdf(savePath)
  1190. // try? FileManager.default.removeItem(atPath: savePath)
  1191. } else {
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }
  1197. }
  1198. // MARK: - KMSystemViewMenuProtocol
  1199. extension KMBrowserWindowController: KMSystemViewMenuProtocol {
  1200. func createPDFFromImage() {
  1201. let openPanel = NSOpenPanel()
  1202. openPanel.allowedFileTypes = KMImageAccessoryController.supportedImageTypes()
  1203. openPanel.allowsMultipleSelection = true
  1204. 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: "")
  1205. openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1206. if response == .OK {
  1207. self.creatPdfFromImg(urls: openPanel.urls)
  1208. }
  1209. }
  1210. }
  1211. func creatPdfFromImg(urls: Array<URL>) {
  1212. // let savePath = self.newDocumentTempSavePath("convertToPDF.pdf")
  1213. let pdf = CPDFDocument()
  1214. // var isSucceed = true
  1215. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1216. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1217. let filePath = savePath.deletingLastPathComponent
  1218. if FileManager.default.fileExists(atPath: filePath) == false {
  1219. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1220. }
  1221. for fileURL in urls {
  1222. if let imag = NSImage(contentsOfFile: fileURL.path ) {
  1223. _ = pdf?.km_insertPage(imag.size, withImage: fileURL.path , at: pdf?.pageCount ?? 0)
  1224. pdf?.write(to: URL(fileURLWithPath: savePath))
  1225. }
  1226. }
  1227. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1228. if error != nil {
  1229. NSApp.presentError(error!)
  1230. } else {
  1231. if document is KMMainDocument {
  1232. let newDocument = document
  1233. (newDocument as! KMMainDocument).isNewCreated = true
  1234. }
  1235. }
  1236. // self.homeContentView?.reloadData()
  1237. }
  1238. }
  1239. func createPdfWithParam() {
  1240. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1241. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1242. let filePath = savePath.deletingLastPathComponent
  1243. if FileManager.default.fileExists(atPath: filePath) == false {
  1244. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1245. }
  1246. let pdfDocument = CPDFDocument()
  1247. pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
  1248. pdfDocument?.write(to: URL(fileURLWithPath: savePath))
  1249. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1250. if error != nil {
  1251. NSApp.presentError(error!)
  1252. } else {
  1253. if document is KMMainDocument {
  1254. let newDocument = document
  1255. (newDocument as! KMMainDocument).isNewCreated = true
  1256. }
  1257. }
  1258. // self.homeContentView?.reloadData()
  1259. }
  1260. }
  1261. func savePdf(_ filePath: String) -> Void {
  1262. let docVc = KMDocumentController.shared
  1263. docVc.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
  1264. if error != nil {
  1265. NSApp.presentError(error!)
  1266. } else {
  1267. if document is KMMainDocument {
  1268. let newDocument = document
  1269. (newDocument as! KMMainDocument).isNewCreated = true
  1270. }
  1271. }
  1272. }
  1273. }
  1274. func newDocumentTempSavePath(_ fileName: String) -> String {
  1275. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  1276. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  1277. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  1278. return append2
  1279. }
  1280. func fetchUniquePath(_ originalPath: String) -> String {
  1281. var path = originalPath
  1282. let dManager = FileManager.default
  1283. if !dManager.fileExists(atPath: path) {
  1284. if path.extension.count < 1 {
  1285. path = path.stringByAppendingPathExtension("pdf")
  1286. }
  1287. return path
  1288. } else {
  1289. let originalFullFileName = path.lastPathComponent
  1290. let originalFileName = path.lastPathComponent.deletingPathExtension
  1291. let originalExtension = path.extension
  1292. let startIndex: Int = 0
  1293. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  1294. let fileLocatePath = originalPath.substring(to: endIndex)
  1295. var i = 1
  1296. while (1 != 0) {
  1297. var newName = String(format: "%@%ld", originalFileName, i)
  1298. newName = newName.stringByAppendingPathExtension(originalExtension)
  1299. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  1300. if !dManager.fileExists(atPath: newPath) {
  1301. return newPath
  1302. } else {
  1303. i+=1
  1304. continue
  1305. }
  1306. }
  1307. }
  1308. }
  1309. func menuItemAction_adjustWidth(_ sender: Any) {
  1310. if (self.canResponseDocumentAction() == false) {
  1311. return
  1312. }
  1313. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1314. document.mainViewController?.menuItemAction_adjustWidth(sender)
  1315. }
  1316. }
  1317. func menuItemAction_adjustPage(_ sender: Any) {
  1318. if (self.canResponseDocumentAction() == false) {
  1319. return
  1320. }
  1321. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1322. document.mainViewController?.menuItemAction_adjustPage(sender)
  1323. }
  1324. }
  1325. func menuItemAction_size(_ sender: Any) {
  1326. if (self.canResponseDocumentAction() == false) {
  1327. return
  1328. }
  1329. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1330. document.mainViewController?.menuItemAction_size(sender)
  1331. }
  1332. }
  1333. func menuItemAction_zoomOut(_ sender: Any) {
  1334. if (self.canResponseDocumentAction() == false) {
  1335. return
  1336. }
  1337. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1338. document.mainViewController?.menuItemAction_zoomOut(sender)
  1339. }
  1340. }
  1341. func menuItemAction_zoomIn(_ sender: Any) {
  1342. if (self.canResponseDocumentAction() == false) {
  1343. return
  1344. }
  1345. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1346. document.mainViewController?.menuItemAction_zoomIn(sender)
  1347. }
  1348. }
  1349. func menuItemAction_singlePage(_ sender: Any) {
  1350. if (self.canResponseDocumentAction() == false) {
  1351. return
  1352. }
  1353. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1354. document.mainViewController?.menuItemAction_singlePage(sender)
  1355. }
  1356. }
  1357. func menuItemAction_singlePageContinue(_ sender: Any) {
  1358. if (self.canResponseDocumentAction() == false) {
  1359. return
  1360. }
  1361. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1362. document.mainViewController?.menuItemAction_singlePageContinue(sender)
  1363. }
  1364. }
  1365. func menuItemAction_doublePage(_ sender: Any) {
  1366. if (self.canResponseDocumentAction() == false) {
  1367. return
  1368. }
  1369. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1370. document.mainViewController?.menuItemAction_doublePage(sender)
  1371. }
  1372. }
  1373. func menuItemAction_doublePageContinue(_ sender: Any) {
  1374. if (self.canResponseDocumentAction() == false) {
  1375. return
  1376. }
  1377. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1378. document.mainViewController?.menuItemAction_doublePageContinue(sender)
  1379. }
  1380. }
  1381. func menuItemAction_bookMode(_ sender: Any) {
  1382. if (self.canResponseDocumentAction() == false) {
  1383. return
  1384. }
  1385. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1386. document.mainViewController?.menuItemAction_bookMode(sender)
  1387. }
  1388. }
  1389. func menuItemAction_readMode(_ sender: Any) {
  1390. if (self.canResponseDocumentAction() == false) {
  1391. return
  1392. }
  1393. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1394. document.mainViewController?.menuItemAction_readMode(sender)
  1395. }
  1396. }
  1397. func menuItemAction_showSplitPage(_ sender: Any) {
  1398. if (self.canResponseDocumentAction() == false) {
  1399. return
  1400. }
  1401. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1402. document.mainViewController?.menuItemAction_showSplitPage(sender)
  1403. }
  1404. }
  1405. func menuItemAction_rotateLeft(_ sender: Any) {
  1406. if (self.canResponseDocumentAction() == false) {
  1407. return
  1408. }
  1409. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1410. document.mainViewController?.menuItemAction_rotateLeft(sender)
  1411. }
  1412. }
  1413. func menuItemAction_rotateRight(_ sender: Any) {
  1414. if (self.canResponseDocumentAction() == false) {
  1415. return
  1416. }
  1417. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1418. document.mainViewController?.menuItemAction_rotateRight(sender)
  1419. }
  1420. }
  1421. func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  1422. if (self.canResponseDocumentAction() == false) {
  1423. return
  1424. }
  1425. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1426. document.mainViewController?.menuItemAction_rotateAllPageLeft(sender)
  1427. }
  1428. }
  1429. func menuItemAction_rotateAllPageRight(_ sender: Any) {
  1430. if (self.canResponseDocumentAction() == false) {
  1431. return
  1432. }
  1433. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1434. document.mainViewController?.menuItemAction_rotateAllPageRight(sender)
  1435. }
  1436. }
  1437. func menuItemAction_view_readMode(_ sender: Any) {
  1438. if (self.canResponseDocumentAction() == false) {
  1439. return
  1440. }
  1441. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1442. document.mainViewController?.menuItemAction_view_readMode(sender)
  1443. }
  1444. }
  1445. func menuItemAction_hiddenLeftSide(_ sender: Any) {
  1446. if (self.canResponseDocumentAction() == false) {
  1447. return
  1448. }
  1449. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1450. document.mainViewController?.menuItemAction_hiddenLeftSide(sender)
  1451. }
  1452. }
  1453. func menuItemAction_hiddenRightSide(_ sender: Any) {
  1454. if (self.canResponseDocumentAction() == false) {
  1455. return
  1456. }
  1457. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1458. document.mainViewController?.menuItemAction_hiddenRightSide(sender)
  1459. }
  1460. }
  1461. func menuItemAction_thumai(_ sender: Any) {
  1462. if (self.canResponseDocumentAction() == false) {
  1463. return
  1464. }
  1465. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1466. document.mainViewController?.menuItemAction_thumai(sender)
  1467. }
  1468. }
  1469. func menuItemAction_outline(_ sender: Any) {
  1470. if (self.canResponseDocumentAction() == false) {
  1471. return
  1472. }
  1473. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1474. document.mainViewController?.menuItemAction_outline(sender)
  1475. }
  1476. }
  1477. func menuItemAction_bookmark(_ sender: Any) {
  1478. if (self.canResponseDocumentAction() == false) {
  1479. return
  1480. }
  1481. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1482. document.mainViewController?.menuItemAction_bookmark(sender)
  1483. }
  1484. }
  1485. func menuItemAction_annotation(_ sender: Any) {
  1486. if (self.canResponseDocumentAction() == false) {
  1487. return
  1488. }
  1489. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1490. document.mainViewController?.menuItemAction_annotation(sender)
  1491. }
  1492. }
  1493. func menuItemAction_search(_ sender: Any) {
  1494. if (self.canResponseDocumentAction() == false) {
  1495. return
  1496. }
  1497. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1498. document.mainViewController?.menuItemAction_search(sender)
  1499. }
  1500. }
  1501. func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  1502. if (self.canResponseDocumentAction() == false) {
  1503. return
  1504. }
  1505. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1506. document.mainViewController?.menuItemAction_hiddenPageIndicator(sender)
  1507. }
  1508. }
  1509. @IBAction func togglePresentation(_ sender: Any?) {
  1510. if (self.canResponseDocumentAction() == false) {
  1511. return
  1512. }
  1513. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1514. document.mainViewController?.togglePresentation(sender)
  1515. }
  1516. }
  1517. @IBAction func chooseTransition(_ sender: Any?) {
  1518. if (self.canResponseDocumentAction() == false) {
  1519. return
  1520. }
  1521. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1522. document.mainViewController?.chooseTransition(sender)
  1523. }
  1524. }
  1525. @IBAction func toggleAutoFlow(_ sender: Any?) {
  1526. if (self.canResponseDocumentAction() == false) {
  1527. return
  1528. }
  1529. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1530. document.mainViewController?.toggleAutoFlow(sender)
  1531. }
  1532. }
  1533. @IBAction func chooseAutoFlowSetting(_ 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?.chooseAutoFlowSetting(sender)
  1539. }
  1540. }
  1541. @IBAction func toggleReadingBar(_ sender: Any?) {
  1542. if (self.canResponseDocumentAction() == false) {
  1543. return
  1544. }
  1545. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1546. document.mainViewController?.toggleReadingBar(sender)
  1547. }
  1548. }
  1549. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  1550. if (self.canResponseDocumentAction() == false) {
  1551. return
  1552. }
  1553. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1554. document.mainViewController?.toggleLeftSidePane(sender)
  1555. }
  1556. }
  1557. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  1558. if (self.canResponseDocumentAction() == false) {
  1559. return
  1560. }
  1561. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1562. document.mainViewController?.changeLeftSidePaneState(sender)
  1563. }
  1564. }
  1565. @IBAction func splitViewAction(_ sender: Any?) {
  1566. if (self.canResponseDocumentAction() == false) {
  1567. return
  1568. }
  1569. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1570. document.mainViewController?.splitViewAction(sender)
  1571. }
  1572. }
  1573. @IBAction func themesColor(_ sender: Any?) {
  1574. if (self.canResponseDocumentAction() == false) {
  1575. return
  1576. }
  1577. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1578. document.mainViewController?.themesColor(sender)
  1579. }
  1580. }
  1581. @IBAction func toggleStatusBar(_ sender: Any?) {
  1582. if (self.canResponseDocumentAction() == false) {
  1583. return
  1584. }
  1585. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1586. document.mainViewController?.toggleStatusBar(sender)
  1587. }
  1588. }
  1589. @IBAction func toggleToolbarShow(_ sender: Any?) {
  1590. if (self.canResponseDocumentAction() == false) {
  1591. return
  1592. }
  1593. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1594. document.mainViewController?.toggleToolbarShow(sender)
  1595. }
  1596. }
  1597. }
  1598. // MARK: - KMSystemEditMenuProtocol
  1599. extension KMBrowserWindowController: KMSystemEditMenuProtocol {
  1600. func menuItemAction_find(_ sender: Any) {
  1601. if (self.canResponseDocumentAction() == false) {
  1602. return
  1603. }
  1604. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1605. document.mainViewController?.menuItemAction_find(sender)
  1606. }
  1607. }
  1608. @IBAction func changeFont(_ sender: Any?) {
  1609. KMPrint("changeFont ...")
  1610. }
  1611. }
  1612. // MARK: - Annotation Menu
  1613. extension KMBrowserWindowController {
  1614. @IBAction func menuItemAction_highlight(_ sender: Any) {
  1615. if (self.canResponseDocumentAction() == false) {
  1616. return
  1617. }
  1618. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1619. document.mainViewController?.menuItemAction_highlight(sender)
  1620. }
  1621. }
  1622. @IBAction func menuItemAction_underline(_ sender: Any) {
  1623. if (self.canResponseDocumentAction() == false) {
  1624. return
  1625. }
  1626. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1627. document.mainViewController?.menuItemAction_underline(sender)
  1628. }
  1629. }
  1630. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  1631. if (self.canResponseDocumentAction() == false) {
  1632. return
  1633. }
  1634. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1635. document.mainViewController?.menuItemAction_deleteLine(sender)
  1636. }
  1637. }
  1638. @IBAction func menuItemAction_freehand(_ sender: Any) {
  1639. if (self.canResponseDocumentAction() == false) {
  1640. return
  1641. }
  1642. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1643. document.mainViewController?.menuItemAction_freehand(sender)
  1644. }
  1645. }
  1646. @IBAction func menuItemAction_text(_ sender: Any) {
  1647. if (self.canResponseDocumentAction() == false) {
  1648. return
  1649. }
  1650. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1651. document.mainViewController?.menuItemAction_text(sender)
  1652. }
  1653. }
  1654. @IBAction func menuItemAction_note(_ sender: Any) {
  1655. if (self.canResponseDocumentAction() == false) {
  1656. return
  1657. }
  1658. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1659. document.mainViewController?.menuItemAction_note(sender)
  1660. }
  1661. }
  1662. @IBAction func menuItemAction_squre(_ sender: Any) {
  1663. if (self.canResponseDocumentAction() == false) {
  1664. return
  1665. }
  1666. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1667. document.mainViewController?.menuItemAction_squre(sender)
  1668. }
  1669. }
  1670. @IBAction func menuItemAction_circle(_ sender: Any) {
  1671. if (self.canResponseDocumentAction() == false) {
  1672. return
  1673. }
  1674. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1675. document.mainViewController?.menuItemAction_circle(sender)
  1676. }
  1677. }
  1678. @IBAction func menuItemAction_arrow(_ sender: Any) {
  1679. if (self.canResponseDocumentAction() == false) {
  1680. return
  1681. }
  1682. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1683. document.mainViewController?.menuItemAction_arrow(sender)
  1684. }
  1685. }
  1686. @IBAction func menuItemAction_line(_ sender: Any) {
  1687. if (self.canResponseDocumentAction() == false) {
  1688. return
  1689. }
  1690. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1691. document.mainViewController?.menuItemAction_line(sender)
  1692. }
  1693. }
  1694. // link
  1695. @IBAction func menuItemAction_link(_ sender: Any) {
  1696. if (self.canResponseDocumentAction() == false) {
  1697. return
  1698. }
  1699. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1700. document.mainViewController?.menuItemAction_link(sender)
  1701. }
  1702. }
  1703. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  1704. if (self.canResponseDocumentAction() == false) {
  1705. return
  1706. }
  1707. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1708. document.mainViewController?.menuItemAction_linkPage(sender)
  1709. }
  1710. }
  1711. // stamp
  1712. @IBAction func menuItemAction_stamp(_ 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?.menuItemAction_stamp(sender)
  1718. }
  1719. }
  1720. @IBAction func menuItemAction_signure(_ 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?.menuItemAction_signure(sender)
  1726. }
  1727. }
  1728. @IBAction func menuItemAction_hiddenAllAnnotation(_ 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?.menuItemAction_hiddenAllAnnotation(sender)
  1734. }
  1735. }
  1736. @IBAction func menuItemAction_clearAllAnnotation(_ 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?.menuItemAction_clearAllAnnotation(sender)
  1742. }
  1743. }
  1744. }
  1745. // MARK: - KMSystemPDFMenuProtocol
  1746. extension KMBrowserWindowController: KMSystemPDFMenuProtocol {
  1747. @IBAction func changeDisplayMode(_ sender: Any?) {
  1748. if (self.canResponseDocumentAction() == false) {
  1749. return
  1750. }
  1751. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1752. document.mainViewController?.changeDisplayMode(sender)
  1753. }
  1754. }
  1755. @IBAction func toggleDisplayAsBook(_ sender: Any?) {
  1756. if (self.canResponseDocumentAction() == false) {
  1757. return
  1758. }
  1759. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1760. document.mainViewController?.toggleDisplayAsBook(sender)
  1761. }
  1762. }
  1763. @IBAction func toggleDisplayPageBreaks(_ sender: Any?) {
  1764. if (self.canResponseDocumentAction() == false) {
  1765. return
  1766. }
  1767. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1768. document.mainViewController?.toggleDisplayPageBreaks(sender)
  1769. }
  1770. }
  1771. @IBAction func changeDisplayBox(_ sender: Any?) {
  1772. if (self.canResponseDocumentAction() == false) {
  1773. return
  1774. }
  1775. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1776. document.mainViewController?.changeDisplayBox(sender)
  1777. }
  1778. }
  1779. @IBAction func toggleAutoScale(_ sender: Any?) {
  1780. if (self.canResponseDocumentAction() == false) {
  1781. return
  1782. }
  1783. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1784. document.mainViewController?.toggleAutoScale(sender)
  1785. }
  1786. }
  1787. @IBAction func doZoomToActualSize(_ sender: Any?) {
  1788. if (self.canResponseDocumentAction() == false) {
  1789. return
  1790. }
  1791. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1792. document.mainViewController?.doZoomToActualSize(sender)
  1793. }
  1794. }
  1795. @IBAction func doZoomToFit(_ sender: Any?) {
  1796. if (self.canResponseDocumentAction() == false) {
  1797. return
  1798. }
  1799. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1800. document.mainViewController?.doZoomToFit(sender)
  1801. }
  1802. }
  1803. @IBAction func doZoomIn(_ 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?.doZoomIn(sender)
  1809. }
  1810. }
  1811. @IBAction func doZoomOut(_ 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?.doZoomOut(sender)
  1817. }
  1818. }
  1819. @IBAction func doMarqueeZoomTool(_ 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?.doMarqueeZoomTool(sender)
  1825. }
  1826. }
  1827. @IBAction func highlightFormFiled(_ 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?.highlightFormFiled(sender)
  1833. }
  1834. }
  1835. @IBAction func highlightLinks(_ 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?.highlightLinks(sender)
  1841. }
  1842. }
  1843. @IBAction func resetForm(_ 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?.resetForm(sender)
  1849. }
  1850. }
  1851. @IBAction func removeAllAnnotation(_ 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?.removeAllAnnotation(sender)
  1857. }
  1858. }
  1859. @IBAction func savePDFSettingToDefaults(_ 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?.savePDFSettingToDefaults(sender)
  1865. }
  1866. }
  1867. }
  1868. // MARK: - KMSystemGotoMenuProtocol
  1869. extension KMBrowserWindowController: KMSystemGotoMenuProtocol {
  1870. func menuItemAction_nextPage(_ sender: Any) {
  1871. if (self.canResponseDocumentAction() == false) {
  1872. return
  1873. }
  1874. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1875. document.mainViewController?.menuItemAction_nextPage(sender)
  1876. }
  1877. }
  1878. func menuItemAction_forwardPage(_ sender: Any) {
  1879. if (self.canResponseDocumentAction() == false) {
  1880. return
  1881. }
  1882. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1883. document.mainViewController?.menuItemAction_forwardPage(sender)
  1884. }
  1885. }
  1886. func menuItemAction_firstPage(_ sender: Any) {
  1887. if (self.canResponseDocumentAction() == false) {
  1888. return
  1889. }
  1890. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1891. document.mainViewController?.menuItemAction_firstPage(sender)
  1892. }
  1893. }
  1894. func menuItemAction_lastPage(_ sender: Any) {
  1895. if (self.canResponseDocumentAction() == false) {
  1896. return
  1897. }
  1898. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1899. document.mainViewController?.menuItemAction_lastPage(sender)
  1900. }
  1901. }
  1902. func menuItemAction_forward(_ sender: Any) {
  1903. if (self.canResponseDocumentAction() == false) {
  1904. return
  1905. }
  1906. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1907. document.mainViewController?.menuItemAction_forward(sender)
  1908. }
  1909. }
  1910. func menuItemAction_goback(_ sender: Any) {
  1911. if (self.canResponseDocumentAction() == false) {
  1912. return
  1913. }
  1914. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1915. document.mainViewController?.menuItemAction_goback(sender)
  1916. }
  1917. }
  1918. func menuItemAction_gotoPage(_ sender: Any) {
  1919. if (self.canResponseDocumentAction() == false) {
  1920. return
  1921. }
  1922. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1923. document.mainViewController?.menuItemAction_gotoPage(sender)
  1924. }
  1925. }
  1926. @IBAction func goToMarkedPage(_ sender: Any?) {
  1927. if (self.canResponseDocumentAction() == false) {
  1928. return
  1929. }
  1930. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1931. document.mainViewController?.goToMarkedPage(sender)
  1932. }
  1933. }
  1934. @IBAction func markPage(_ sender: Any?) {
  1935. if (self.canResponseDocumentAction() == false) {
  1936. return
  1937. }
  1938. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1939. document.mainViewController?.markPage(sender)
  1940. }
  1941. }
  1942. }
  1943. // MARK: - KMSystemToolMenuProtocol
  1944. extension KMBrowserWindowController: KMSystemToolMenuProtocol {
  1945. @IBAction func getInfo(_ sender: Any?) {
  1946. if (self.canResponseDocumentAction() == false) {
  1947. return
  1948. }
  1949. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1950. document.mainViewController?.getInfo(sender)
  1951. }
  1952. }
  1953. func orderFrontNotesPanel(_ sender: Any?) {
  1954. let windowC = KMNotesPanelController.shared
  1955. if let win = windowC.window {
  1956. if win.isVisible {
  1957. win.orderOut(sender)
  1958. } else {
  1959. win.orderFront(sender)
  1960. }
  1961. }
  1962. }
  1963. func menuItemAction_scrolTool(_ sender: Any) {
  1964. if (self.canResponseDocumentAction() == false) {
  1965. return
  1966. }
  1967. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1968. document.mainViewController?.menuItemAction_scrolTool(sender)
  1969. }
  1970. }
  1971. func menuItemAction_zoomOutTool(_ sender: Any) {
  1972. if (self.canResponseDocumentAction() == false) {
  1973. return
  1974. }
  1975. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1976. document.mainViewController?.menuItemAction_zoomOutTool(sender)
  1977. }
  1978. }
  1979. func menuItemAction_selectTool(_ sender: Any) {
  1980. if (self.canResponseDocumentAction() == false) {
  1981. return
  1982. }
  1983. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1984. document.mainViewController?.menuItemAction_selectTool(sender)
  1985. }
  1986. }
  1987. @IBAction func changeToolMode(_ sender: Any?) {
  1988. if (self.canResponseDocumentAction() == false) {
  1989. return
  1990. }
  1991. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1992. if (document.isHome) {
  1993. } else {
  1994. document.mainViewController?.changeToolMode(sender)
  1995. }
  1996. }
  1997. }
  1998. @IBAction func compression(_ sender: Any?) {
  1999. if (self.canResponseDocumentAction() == false) {
  2000. return
  2001. }
  2002. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2003. if (document.isHome) {
  2004. } else {
  2005. document.mainViewController?.compression(sender)
  2006. }
  2007. }
  2008. }
  2009. @IBAction func merge(_ sender: Any?) {
  2010. if (self.canResponseDocumentAction() == false) {
  2011. return
  2012. }
  2013. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2014. if (document.isHome) {
  2015. } else {
  2016. document.mainViewController?.merge(sender)
  2017. }
  2018. }
  2019. }
  2020. @IBAction func convert(_ sender: Any?) {
  2021. if (self.canResponseDocumentAction() == false) {
  2022. return
  2023. }
  2024. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2025. if (document.isHome) {
  2026. } else {
  2027. document.mainViewController?.convert(sender)
  2028. }
  2029. }
  2030. }
  2031. @IBAction func imageToPDF(_ sender: Any?) {
  2032. if (self.canResponseDocumentAction() == false) {
  2033. return
  2034. }
  2035. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2036. if (document.isHome) {
  2037. } else {
  2038. document.mainViewController?.imageToPDF(sender)
  2039. }
  2040. }
  2041. }
  2042. @IBAction func ocr(_ sender: Any?) {
  2043. if (self.canResponseDocumentAction() == false) {
  2044. return
  2045. }
  2046. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2047. if (document.isHome) {
  2048. } else {
  2049. document.mainViewController?.ocr(sender)
  2050. }
  2051. }
  2052. }
  2053. @IBAction func extractImage(_ sender: Any?) {
  2054. if (self.canResponseDocumentAction() == false) {
  2055. return
  2056. }
  2057. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2058. if (document.isHome) {
  2059. } else {
  2060. document.mainViewController?.extractImage(sender)
  2061. }
  2062. }
  2063. }
  2064. @IBAction func pageEdit(_ sender: Any?) {
  2065. if (self.canResponseDocumentAction() == false) {
  2066. return
  2067. }
  2068. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2069. if (document.isHome) {
  2070. } else {
  2071. document.mainViewController?.pageEdit(sender)
  2072. }
  2073. }
  2074. }
  2075. @IBAction func textEditAction(_ sender: Any?) {
  2076. if (self.canResponseDocumentAction() == false) {
  2077. return
  2078. }
  2079. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2080. if (document.isHome) {
  2081. } else {
  2082. document.mainViewController?.textEditAction(sender)
  2083. }
  2084. }
  2085. }
  2086. @IBAction func security(_ sender: Any?) {
  2087. if (self.canResponseDocumentAction() == false) {
  2088. return
  2089. }
  2090. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2091. if (document.isHome) {
  2092. } else {
  2093. document.mainViewController?.security(sender)
  2094. }
  2095. }
  2096. }
  2097. @IBAction func decryption(_ sender: Any?) {
  2098. if (self.canResponseDocumentAction() == false) {
  2099. return
  2100. }
  2101. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2102. if (document.isHome) {
  2103. } else {
  2104. document.mainViewController?.decryption(sender)
  2105. }
  2106. }
  2107. }
  2108. @IBAction func cropAll(_ sender: Any?) {
  2109. if (self.canResponseDocumentAction() == false) {
  2110. return
  2111. }
  2112. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2113. if (document.isHome) {
  2114. } else {
  2115. document.mainViewController?.cropAll(sender)
  2116. }
  2117. }
  2118. }
  2119. @IBAction func crop(_ sender: Any?) {
  2120. if (self.canResponseDocumentAction() == false) {
  2121. return
  2122. }
  2123. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2124. if (document.isHome) {
  2125. } else {
  2126. document.mainViewController?.crop(sender)
  2127. }
  2128. }
  2129. }
  2130. @IBAction func autoCropAll(_ sender: Any?) {
  2131. if (self.canResponseDocumentAction() == false) {
  2132. return
  2133. }
  2134. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2135. if (document.isHome) {
  2136. } else {
  2137. document.mainViewController?.autoCropAll(sender)
  2138. }
  2139. }
  2140. }
  2141. @IBAction func smartAutoCropAll(_ sender: Any?) {
  2142. if (self.canResponseDocumentAction() == false) {
  2143. return
  2144. }
  2145. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2146. if (document.isHome) {
  2147. } else {
  2148. document.mainViewController?.smartAutoCropAll(sender)
  2149. }
  2150. }
  2151. }
  2152. @IBAction func cropOptionsModel(_ sender: Any?) {
  2153. if (self.canResponseDocumentAction() == false) {
  2154. return
  2155. }
  2156. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2157. if (document.isHome) {
  2158. } else {
  2159. // document.mainViewController?.cropOptionsModel(sender)
  2160. }
  2161. }
  2162. }
  2163. @IBAction func selectToolModel(_ sender: Any?) {
  2164. if (self.canResponseDocumentAction() == false) {
  2165. return
  2166. }
  2167. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2168. if (document.isHome) {
  2169. } else {
  2170. document.mainViewController?.selectToolModel(sender)
  2171. }
  2172. }
  2173. }
  2174. @IBAction func takeSnapshot(_ sender: Any?) {
  2175. if (self.canResponseDocumentAction() == false) {
  2176. return
  2177. }
  2178. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2179. if (document.isHome) {
  2180. } else {
  2181. document.mainViewController?.takeSnapshot(sender)
  2182. }
  2183. }
  2184. }
  2185. @IBAction func takeSnapshotSelectContent(_ sender: Any?) {
  2186. if (self.canResponseDocumentAction() == false) {
  2187. return
  2188. }
  2189. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2190. if (document.isHome) {
  2191. } else {
  2192. document.mainViewController?.takeSnapshotSelectContent(sender)
  2193. }
  2194. }
  2195. }
  2196. @IBAction func changeAnnotationMode(_ sender: Any?) {
  2197. if (self.canResponseDocumentAction() == false) {
  2198. return
  2199. }
  2200. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2201. if (document.isHome) {
  2202. } else {
  2203. document.mainViewController?.changeAnnotationMode(sender)
  2204. }
  2205. }
  2206. }
  2207. @IBAction func addBackground(_ sender: Any?) {
  2208. if (self.canResponseDocumentAction() == false) {
  2209. return
  2210. }
  2211. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2212. if (document.isHome) {
  2213. } else {
  2214. document.mainViewController?.addBackground(sender)
  2215. }
  2216. }
  2217. }
  2218. @IBAction func removeBackground(_ sender: Any?) {
  2219. if (self.canResponseDocumentAction() == false) {
  2220. return
  2221. }
  2222. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2223. if (document.isHome) {
  2224. } else {
  2225. document.mainViewController?.removeBackground(sender)
  2226. }
  2227. }
  2228. }
  2229. @IBAction func addWatermark(_ sender: Any?) {
  2230. if (self.canResponseDocumentAction() == false) {
  2231. return
  2232. }
  2233. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2234. if (document.isHome) {
  2235. } else {
  2236. document.mainViewController?.addWatermark(sender)
  2237. }
  2238. }
  2239. }
  2240. @IBAction func editWatermark(_ sender: Any?) {
  2241. KMPrint("editWatermark ...")
  2242. }
  2243. @IBAction func batchAddWatermark(_ sender: Any?) {
  2244. if (self.canResponseDocumentAction() == false) {
  2245. return
  2246. }
  2247. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2248. if (document.isHome) {
  2249. } else {
  2250. document.mainViewController?.batchAddWatermark(sender)
  2251. }
  2252. }
  2253. }
  2254. @IBAction func batchRemoveWatermark(_ sender: Any?) {
  2255. if (self.canResponseDocumentAction() == false) {
  2256. return
  2257. }
  2258. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2259. if (document.isHome) {
  2260. } else {
  2261. document.mainViewController?.batchRemoveWatermark(sender)
  2262. }
  2263. }
  2264. }
  2265. @IBAction func removeWatermark(_ sender: Any?) {
  2266. if (self.canResponseDocumentAction() == false) {
  2267. return
  2268. }
  2269. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2270. if (document.isHome) {
  2271. } else {
  2272. document.mainViewController?.removeWatermark(sender)
  2273. }
  2274. }
  2275. }
  2276. @IBAction func templateWatermark(_ sender: Any?) {
  2277. if (self.canResponseDocumentAction() == false) {
  2278. return
  2279. }
  2280. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2281. if (document.isHome) {
  2282. } else {
  2283. document.mainViewController?.templateWatermark(sender)
  2284. }
  2285. }
  2286. }
  2287. @IBAction func batchAddBackground(_ sender: Any?) {
  2288. if (self.canResponseDocumentAction() == false) {
  2289. return
  2290. }
  2291. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2292. if (document.isHome) {
  2293. } else {
  2294. document.mainViewController?.batchAddBackground(sender)
  2295. }
  2296. }
  2297. }
  2298. @IBAction func batchRemoveBackground(_ sender: Any?) {
  2299. if (self.canResponseDocumentAction() == false) {
  2300. return
  2301. }
  2302. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2303. if (document.isHome) {
  2304. } else {
  2305. document.mainViewController?.batchRemoveBackground(sender)
  2306. }
  2307. }
  2308. }
  2309. @IBAction func templateBackground(_ sender: Any?) {
  2310. if (self.canResponseDocumentAction() == false) {
  2311. return
  2312. }
  2313. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2314. if (document.isHome) {
  2315. } else {
  2316. document.mainViewController?.templateBackground(sender)
  2317. }
  2318. }
  2319. }
  2320. @IBAction func addHeaderFooter(_ sender: Any?) {
  2321. if (self.canResponseDocumentAction() == false) {
  2322. return
  2323. }
  2324. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2325. if (document.isHome) {
  2326. } else {
  2327. document.mainViewController?.addHeaderFooter(sender)
  2328. }
  2329. }
  2330. }
  2331. @IBAction func removeHeaderFooter(_ sender: Any?) {
  2332. if (self.canResponseDocumentAction() == false) {
  2333. return
  2334. }
  2335. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2336. if (document.isHome) {
  2337. } else {
  2338. document.mainViewController?.removeHeaderFooter(sender)
  2339. }
  2340. }
  2341. }
  2342. @IBAction func batchAddHeaderFooter(_ sender: Any?) {
  2343. if (self.canResponseDocumentAction() == false) {
  2344. return
  2345. }
  2346. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2347. if (document.isHome) {
  2348. } else {
  2349. document.mainViewController?.batchAddHeaderFooter(sender)
  2350. }
  2351. }
  2352. }
  2353. @IBAction func batchRemoveHeaderFooter(_ sender: Any?) {
  2354. if (self.canResponseDocumentAction() == false) {
  2355. return
  2356. }
  2357. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2358. if (document.isHome) {
  2359. } else {
  2360. document.mainViewController?.batchRemoveHeaderFooter(sender)
  2361. }
  2362. }
  2363. }
  2364. @IBAction func templateHeaderFooter(_ sender: Any?) {
  2365. if (self.canResponseDocumentAction() == false) {
  2366. return
  2367. }
  2368. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2369. if (document.isHome) {
  2370. } else {
  2371. document.mainViewController?.templateHeaderFooter(sender)
  2372. }
  2373. }
  2374. }
  2375. @IBAction func addHeaderFooterBates(_ sender: Any?) {
  2376. if (self.canResponseDocumentAction() == false) {
  2377. return
  2378. }
  2379. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2380. if (document.isHome) {
  2381. } else {
  2382. document.mainViewController?.addHeaderFooterBates(sender)
  2383. }
  2384. }
  2385. }
  2386. @IBAction func removeHeaderFooterBates(_ sender: Any?) {
  2387. if (self.canResponseDocumentAction() == false) {
  2388. return
  2389. }
  2390. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2391. if (document.isHome) {
  2392. } else {
  2393. document.mainViewController?.removeHeaderFooterBates(sender)
  2394. }
  2395. }
  2396. }
  2397. @IBAction func batchAddBates(_ sender: Any?) {
  2398. if (self.canResponseDocumentAction() == false) {
  2399. return
  2400. }
  2401. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2402. if (document.isHome) {
  2403. } else {
  2404. document.mainViewController?.batchAddBates(sender)
  2405. }
  2406. }
  2407. }
  2408. @IBAction func batchRemoveBates(_ sender: Any?) {
  2409. if (self.canResponseDocumentAction() == false) {
  2410. return
  2411. }
  2412. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2413. if (document.isHome) {
  2414. } else {
  2415. document.mainViewController?.batchRemoveBates(sender)
  2416. }
  2417. }
  2418. }
  2419. @IBAction func templateBates(_ sender: Any?) {
  2420. if (self.canResponseDocumentAction() == false) {
  2421. return
  2422. }
  2423. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2424. if (document.isHome) {
  2425. } else {
  2426. document.mainViewController?.templateBates(sender)
  2427. }
  2428. }
  2429. }
  2430. @IBAction func togglePoster(_ sender: Any?) {
  2431. if (self.canResponseDocumentAction() == false) {
  2432. return
  2433. }
  2434. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2435. if (document.isHome) {
  2436. } else {
  2437. document.mainViewController?.togglePoster(sender)
  2438. }
  2439. }
  2440. }
  2441. @IBAction func toggleMultiple(_ sender: Any?) {
  2442. if (self.canResponseDocumentAction() == false) {
  2443. return
  2444. }
  2445. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2446. if (document.isHome) {
  2447. } else {
  2448. document.mainViewController?.toggleMultiple(sender)
  2449. }
  2450. }
  2451. }
  2452. @IBAction func toggleBooklet(_ sender: Any?) {
  2453. if (self.canResponseDocumentAction() == false) {
  2454. return
  2455. }
  2456. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2457. if (document.isHome) {
  2458. } else {
  2459. document.mainViewController?.toggleBooklet(sender)
  2460. }
  2461. }
  2462. }
  2463. @IBAction func rotateRight(_ sender: Any?) {
  2464. if (self.canResponseDocumentAction() == false) {
  2465. return
  2466. }
  2467. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2468. if (document.isHome) {
  2469. } else {
  2470. document.mainViewController?.rotateRight(sender)
  2471. }
  2472. }
  2473. }
  2474. @IBAction func rotateLeft(_ sender: Any?) {
  2475. if (self.canResponseDocumentAction() == false) {
  2476. return
  2477. }
  2478. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2479. if (document.isHome) {
  2480. } else {
  2481. document.mainViewController?.rotateLeft(sender)
  2482. }
  2483. }
  2484. }
  2485. @IBAction func rotateAllRight(_ sender: Any?) {
  2486. if (self.canResponseDocumentAction() == false) {
  2487. return
  2488. }
  2489. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2490. if (document.isHome) {
  2491. } else {
  2492. document.mainViewController?.rotateAllRight(sender)
  2493. }
  2494. }
  2495. }
  2496. @IBAction func rotateAllLeft(_ sender: Any?) {
  2497. if (self.canResponseDocumentAction() == false) {
  2498. return
  2499. }
  2500. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2501. if (document.isHome) {
  2502. } else {
  2503. document.mainViewController?.rotateAllLeft(sender)
  2504. }
  2505. }
  2506. }
  2507. @IBAction func autoSelectContent(_ sender: Any?) {
  2508. if (self.canResponseDocumentAction() == false) {
  2509. return
  2510. }
  2511. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2512. if (document.isHome) {
  2513. } else {
  2514. document.mainViewController?.autoSelectContent(sender)
  2515. }
  2516. }
  2517. }
  2518. }
  2519. // MARK: - KMSystemWindowMenuRrotocol
  2520. extension KMBrowserWindowController: KMSystemWindowMenuRrotocol {
  2521. func menuItemAction_showForwardTagPage(_ sender: Any) {
  2522. if (self.canResponseDocumentAction() == false) {
  2523. return
  2524. }
  2525. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2526. if (document.isHome) {
  2527. document.homeViewController?.menuItemAction_showForwardTagPage(sender)
  2528. } else {
  2529. document.mainViewController?.menuItemAction_showForwardTagPage(sender)
  2530. }
  2531. }
  2532. }
  2533. func menuItemAction_showNextTagPage(_ sender: Any) {
  2534. if (self.canResponseDocumentAction() == false) {
  2535. return
  2536. }
  2537. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2538. if (document.isHome) {
  2539. document.homeViewController?.menuItemAction_showNextTagPage(sender)
  2540. } else {
  2541. document.mainViewController?.menuItemAction_showNextTagPage(sender)
  2542. }
  2543. }
  2544. }
  2545. func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  2546. if (self.canResponseDocumentAction() == false) {
  2547. return
  2548. }
  2549. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2550. if (document.isHome) {
  2551. document.homeViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2552. } else {
  2553. document.mainViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2554. }
  2555. }
  2556. }
  2557. func menuItemAction_mergeAllWindow(_ sender: Any) {
  2558. if (self.canResponseDocumentAction() == false) {
  2559. return
  2560. }
  2561. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2562. if (document.isHome) {
  2563. document.homeViewController?.menuItemAction_mergeAllWindow(sender)
  2564. } else {
  2565. document.mainViewController?.menuItemAction_mergeAllWindow(sender)
  2566. }
  2567. }
  2568. }
  2569. }
  2570. // MARK: - Analytics (埋点)
  2571. extension KMBrowserWindowController {
  2572. func trackEvent_upgrade() -> Void {
  2573. KMAnalytics.trackEvent(eventName: "Btn_Tbr_Upgrade", parameters: [
  2574. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.tbr,
  2575. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.tbr_Btn], platform: .AppCenter, appTarget: .all)
  2576. }
  2577. }