KMBrowserWindowController.swift 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904
  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 editNote(_ sender: Any?) {
  532. KMPrint("editNote ...")
  533. }
  534. @IBAction func changeColor(_ sender: Any?) {
  535. KMPrint("changeColor ...")
  536. }
  537. @IBAction func changeAttributes(_ sender: Any?) {
  538. KMPrint("changeAttributes ...")
  539. }
  540. @IBAction func alignLeft(_ sender: Any?) {
  541. KMPrint("alignLeft ...")
  542. }
  543. @IBAction func alignRight(_ sender: Any?) {
  544. KMPrint("alignRight ...")
  545. }
  546. @IBAction func alignCenter(_ sender: Any?) {
  547. KMPrint("alignCenter ...")
  548. }
  549. @IBAction func createNewNote(_ sender: Any?) {
  550. KMPrint("createNewNote ...")
  551. }
  552. @IBAction func toggleHideNotes(_ sender: Any?) {
  553. KMPrint("toggleHideNotes ...")
  554. }
  555. @IBAction func changeDisplaySinglePages(_ sender: Any?) {
  556. KMPrint("changeDisplaySinglePages ...")
  557. }
  558. @IBAction func changeDisplayContinuous(_ sender: Any?) {
  559. KMPrint("changeDisplayContinuous ...")
  560. }
  561. @IBAction func doGoToNextPage(_ sender: Any?) {
  562. KMPrint("doGoToNextPage ...")
  563. }
  564. @IBAction func doGoToPreviousPage(_ sender: Any?) {
  565. KMPrint("doGoToPreviousPage ...")
  566. }
  567. @IBAction func doGoToFirstPage(_ sender: Any?) {
  568. KMPrint("doGoToFirstPage ...")
  569. }
  570. @IBAction func doGoToLastPage(_ sender: Any?) {
  571. KMPrint("doGoToLastPage ...")
  572. }
  573. @IBAction func allGoToNextPage(_ sender: Any?) {
  574. KMPrint("allGoToNextPage ...")
  575. }
  576. @IBAction func allGoToPreviousPage(_ sender: Any?) {
  577. KMPrint("allGoToPreviousPage ...")
  578. }
  579. @IBAction func allGoToFirstPage(_ sender: Any?) {
  580. KMPrint("allGoToFirstPage ...")
  581. }
  582. @IBAction func allGoToLastPage(_ sender: Any?) {
  583. KMPrint("allGoToLastPage ...")
  584. }
  585. @IBAction func doGoToPage(_ sender: Any?) {
  586. KMPrint("doGoToPage ...")
  587. }
  588. @IBAction func doGoBack(_ sender: Any?) {
  589. KMPrint("doGoBack ...")
  590. }
  591. @IBAction func doGoForward(_ sender: Any?) {
  592. KMPrint("doGoForward ...")
  593. }
  594. @IBAction func doZoomToPhysicalSize(_ sender: Any?) {
  595. KMPrint("doZoomToPhysicalSize ...")
  596. }
  597. @IBAction func alternateZoomToFit(_ sender: Any?) {
  598. KMPrint("alternateZoomToFit ...")
  599. }
  600. @IBAction func doZoomToSelection(_ sender: Any?) {
  601. KMPrint("doZoomToSelection ...")
  602. }
  603. @IBAction func doAutoScale(_ sender: Any?) {
  604. KMPrint("doAutoScale ...")
  605. }
  606. // @IBAction func delete(_ sender: Any?) {
  607. // KMPrint("delete ...")
  608. // }
  609. // @IBAction func paste(_ sender: Any?) {
  610. // KMPrint("paste ...")
  611. // }
  612. @IBAction func alternatePaste(_ sender: Any?) {
  613. KMPrint("alternatePaste ...")
  614. }
  615. @IBAction func copy(_ sender: Any?) {
  616. KMPrint("copy ...")
  617. }
  618. @IBAction func cut(_ sender: Any?) {
  619. KMPrint("cut ...")
  620. }
  621. @IBAction func toggleFullscreen(_ sender: Any?) {
  622. KMPrint("toggleFullscreen ...")
  623. }
  624. @IBAction func toggleRightSidePane(_ sender: Any?) {
  625. KMPrint("toggleRightSidePane ...")
  626. }
  627. @IBAction func performFit(_ sender: Any?) {
  628. KMPrint("performFit ...")
  629. }
  630. @IBAction func changeRightSidePaneState(_ sender: Any?) {
  631. KMPrint("changeRightSidePaneState ...")
  632. }
  633. @IBAction func changeFindPaneState(_ sender: Any?) {
  634. KMPrint("changeFindPaneState ...")
  635. }
  636. @IBAction func toggleSplitPDF(_ sender: Any?) {
  637. KMPrint("toggleSplitPDF ...")
  638. }
  639. @IBAction func search(_ sender: Any?) {
  640. KMPrint("search ...")
  641. }
  642. @IBAction func searchNotes(_ sender: Any?) {
  643. KMPrint("searchNotes ...")
  644. }
  645. @IBAction func password(_ sender: Any?) {
  646. KMPrint("password ...")
  647. }
  648. @IBAction func toggleCaseInsensitiveSearch(_ sender: Any?) {
  649. KMPrint("toggleCaseInsensitiveSearch ...")
  650. }
  651. @IBAction func toggleWholeWordSearch(_ sender: Any?) {
  652. KMPrint("toggleWholeWordSearch ...")
  653. }
  654. @IBAction func toggleCaseInsensitiveNoteSearch(_ sender: Any?) {
  655. KMPrint("toggleCaseInsensitiveNoteSearch ...")
  656. }
  657. @IBAction func performFindPanelAction(_ sender: NSMenuItem) {
  658. KMPrint("performFindPanelAction ...")
  659. var forward = true
  660. var findString: String?
  661. switch sender.tag {
  662. case 1:
  663. if let document = self.browser.activeTabContents() as? KMMainDocument {
  664. if document.isHome == false {
  665. document.mainViewController?.toolbarController.showFindBar()
  666. }
  667. }
  668. case 3:
  669. if let document = self.browser.activeTabContents() as? KMMainDocument {
  670. if document.isHome == false {
  671. document.mainViewController?.toolbarController.showFindString(forward: false)
  672. }
  673. }
  674. case 2:
  675. if let document = self.browser.activeTabContents() as? KMMainDocument {
  676. if document.isHome == false {
  677. document.mainViewController?.toolbarController.showFindString(forward: true)
  678. }
  679. }
  680. case 7:
  681. if let document = self.browser.activeTabContents() as? KMMainDocument {
  682. if document.isHome == false {
  683. document.mainViewController?.toolbarController.showAllAction()
  684. }
  685. }
  686. default: break
  687. }
  688. }
  689. @IBAction func scrollUp(_ sender: Any?) {
  690. KMPrint("scrollUp ...")
  691. }
  692. @IBAction func scrollDown(_ sender: Any?) {
  693. KMPrint("scrollDown ...")
  694. }
  695. @IBAction func scrollRight(_ sender: Any?) {
  696. KMPrint("scrollRight ...")
  697. }
  698. @IBAction func scrollLeft(_ sender: Any?) {
  699. KMPrint("scrollLeft ...")
  700. }
  701. @IBAction func selectSelectedNote(_ sender: Any?) {
  702. KMPrint("selectSelectedNote ...")
  703. }
  704. @IBAction func goToSelectedOutlineItem(_ sender: Any?) {
  705. KMPrint("goToSelectedOutlineItem ...")
  706. }
  707. @IBAction func goToSelectedFindResults(_ sender: Any?) {
  708. KMPrint("goToSelectedFindResults ...")
  709. }
  710. @IBAction func toggleSelectedSnapshots(_ sender: Any?) {
  711. KMPrint("toggleSelectedSnapshots ...")
  712. }
  713. @IBAction func selfSignProfile(_ sender: Any?) {
  714. KMPrint("selfSignProfile ...")
  715. }
  716. @IBAction func link(_ sender: Any?) {
  717. KMPrint("link ...")
  718. }
  719. @IBAction func insertPage(_ sender: Any?) {
  720. KMPrint("insertPage ...")
  721. }
  722. @IBAction func split(_ sender: Any?) {
  723. KMPrint("split ...")
  724. }
  725. @IBAction func comparison(_ sender: Any?) {
  726. KMPrint("comparison ...")
  727. }
  728. @IBAction func contentComparison(_ sender: Any?) {
  729. KMPrint("contentComparison ...")
  730. }
  731. @IBAction func coverageComparison(_ sender: Any?) {
  732. KMPrint("coverageComparison ...")
  733. }
  734. @IBAction func readMode(_ sender: Any?) {
  735. KMPrint("readMode ...")
  736. }
  737. @IBAction func changeFormType(_ sender: Any?) {
  738. KMPrint("changeFormType ...")
  739. }
  740. @IBAction func changeSelfSignType(_ sender: Any?) {
  741. KMPrint("changeSelfSignType ...")
  742. }
  743. @IBAction func clearAllForm(_ sender: Any?) {
  744. KMPrint("clearAllForm ...")
  745. }
  746. @IBAction func thumbnailSizeScaling(_ sender: Any?) {
  747. KMPrint("thumbnailSizeScaling ...")
  748. }
  749. @IBAction func redactAction(_ sender: Any?) {
  750. if (self.canResponseDocumentAction() == false) {
  751. return
  752. }
  753. if let document = self.browser.activeTabContents() as? KMMainDocument {
  754. if document.isHome == false {
  755. document.mainViewController?.menuItemAction_redact(sender)
  756. }
  757. }
  758. }
  759. }
  760. // MARK: - KMSystemFileMenuProtocol
  761. extension KMBrowserWindowController: KMSystemFileMenuProtocol {
  762. func menuItemClick_saveAsFlattenedPDF(_ sender: Any) {
  763. if (self.canResponseDocumentAction() == false) {
  764. return
  765. }
  766. if let document = self.browser.activeTabContents() as? KMMainDocument {
  767. if (document.isHome) {
  768. // document.homeViewController?.menuItemClick_mergePDF(sender)
  769. } else {
  770. document.mainViewController?.menuItemClick_saveAsFlattenedPDF(sender)
  771. }
  772. }
  773. }
  774. func menuItemClick_mergePDF(_ sender: Any) {
  775. if (self.canResponseDocumentAction() == false) {
  776. return
  777. }
  778. if let document = self.browser.activeTabContents() as? KMMainDocument {
  779. if (document.isHome) {
  780. document.homeViewController?.menuItemClick_mergePDF(sender)
  781. } else {
  782. document.mainViewController?.menuItemClick_mergePDF(sender)
  783. }
  784. }
  785. }
  786. func menuItemClick_Compress(_ sender: Any) {
  787. if (self.canResponseDocumentAction() == false) {
  788. return
  789. }
  790. if let document = self.browser.activeTabContents() as? KMMainDocument {
  791. if (document.isHome) {
  792. document.homeViewController?.menuItemClick_Compress(sender)
  793. } else {
  794. document.mainViewController?.menuItemClick_Compress(sender)
  795. }
  796. }
  797. }
  798. func menuItemAction_ConvertToWord(_ sender: Any) {
  799. if (self.canResponseDocumentAction() == false) {
  800. return
  801. }
  802. if let document = self.browser.activeTabContents() as? KMMainDocument {
  803. if (document.isHome) {
  804. document.homeViewController?.menuItemAction_ConvertToWord(sender)
  805. } else {
  806. document.mainViewController?.menuItemAction_ConvertToWord(sender)
  807. }
  808. }
  809. }
  810. func menuItemAction_ConvertToExcel(_ sender: Any) {
  811. if (self.canResponseDocumentAction() == false) {
  812. return
  813. }
  814. if let document = self.browser.activeTabContents() as? KMMainDocument {
  815. if (document.isHome) {
  816. document.homeViewController?.menuItemAction_ConvertToExcel(sender)
  817. } else {
  818. document.mainViewController?.menuItemAction_ConvertToExcel(sender)
  819. }
  820. }
  821. }
  822. func menuItemAction_ConvertToPPT(_ sender: Any) {
  823. if (self.canResponseDocumentAction() == false) {
  824. return
  825. }
  826. if let document = self.browser.activeTabContents() as? KMMainDocument {
  827. if (document.isHome) {
  828. document.homeViewController?.menuItemAction_ConvertToPPT(sender)
  829. } else {
  830. document.mainViewController?.menuItemAction_ConvertToPPT(sender)
  831. }
  832. }
  833. }
  834. func menuItemAction_ConvertToRTF(_ sender: Any) {
  835. if (self.canResponseDocumentAction() == false) {
  836. return
  837. }
  838. if let document = self.browser.activeTabContents() as? KMMainDocument {
  839. if (document.isHome) {
  840. document.homeViewController?.menuItemAction_ConvertToRTF(sender)
  841. } else {
  842. document.mainViewController?.menuItemAction_ConvertToRTF(sender)
  843. }
  844. }
  845. }
  846. func menuItemAction_ConvertToHTML(_ sender: Any) {
  847. if (self.canResponseDocumentAction() == false) {
  848. return
  849. }
  850. if let document = self.browser.activeTabContents() as? KMMainDocument {
  851. if (document.isHome) {
  852. document.homeViewController?.menuItemAction_ConvertToHTML(sender)
  853. } else {
  854. document.mainViewController?.menuItemAction_ConvertToHTML(sender)
  855. }
  856. }
  857. }
  858. func menuItemAction_ConvertToText(_ sender: Any) {
  859. if (self.canResponseDocumentAction() == false) {
  860. return
  861. }
  862. if let document = self.browser.activeTabContents() as? KMMainDocument {
  863. if (document.isHome) {
  864. document.homeViewController?.menuItemAction_ConvertToText(sender)
  865. } else {
  866. document.mainViewController?.menuItemAction_ConvertToText(sender)
  867. }
  868. }
  869. }
  870. func menuItemAction_ConvertToCSV(_ sender: Any) {
  871. if (self.canResponseDocumentAction() == false) {
  872. return
  873. }
  874. if let document = self.browser.activeTabContents() as? KMMainDocument {
  875. if (document.isHome) {
  876. document.homeViewController?.menuItemAction_ConvertToCSV(sender)
  877. } else {
  878. document.mainViewController?.menuItemAction_ConvertToCSV(sender)
  879. }
  880. }
  881. }
  882. func menuItemAction_ConvertToImage(_ sender: Any) {
  883. if (self.canResponseDocumentAction() == false) {
  884. return
  885. }
  886. if let document = self.browser.activeTabContents() as? KMMainDocument {
  887. if (document.isHome) {
  888. document.homeViewController?.menuItemAction_ConvertToImage(sender)
  889. } else {
  890. document.mainViewController?.menuItemAction_ConvertToImage(sender)
  891. }
  892. }
  893. }
  894. func menuItemClick_SettingPassword(_ sender: Any) {
  895. if (self.canResponseDocumentAction() == false) {
  896. return
  897. }
  898. if let document = self.browser.activeTabContents() as? KMMainDocument {
  899. if (document.isHome) {
  900. document.homeViewController?.menuItemClick_SettingPassword(sender)
  901. } else {
  902. document.mainViewController?.menuItemClick_SettingPassword(sender)
  903. }
  904. }
  905. }
  906. func menuItemClick_RemovePassword(_ sender: Any) {
  907. if (self.canResponseDocumentAction() == false) {
  908. return
  909. }
  910. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  911. document.mainViewController?.menuItemClick_RemovePassword(sender)
  912. }
  913. }
  914. func menuItemAction_closeWindow(_ sender: Any) {
  915. if (self.canResponseDocumentAction() == false) {
  916. return
  917. }
  918. if let document = self.browser.activeTabContents() as? KMMainDocument {
  919. if (document.isHome) {
  920. document.homeViewController?.menuItemAction_closeWindow(sender)
  921. } else {
  922. document.mainViewController?.menuItemAction_closeWindow(sender)
  923. }
  924. }
  925. }
  926. func menuItemAction_closeAllWindows(_ sender: Any) {
  927. if (self.canResponseDocumentAction() == false) {
  928. return
  929. }
  930. if let document = self.browser.activeTabContents() as? KMMainDocument {
  931. if (document.isHome) {
  932. document.homeViewController?.menuItemAction_closeAllWindows(sender)
  933. } else {
  934. document.mainViewController?.menuItemAction_closeAllWindows(sender)
  935. }
  936. }
  937. }
  938. func menuItemAction_closeTagPage(_ sender: Any) {
  939. if (self.canResponseDocumentAction() == false) {
  940. return
  941. }
  942. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  943. document.mainViewController?.menuItemAction_closeTagPage(sender)
  944. }
  945. }
  946. func menuItemAction_showInFinder(_ sender: Any) {
  947. if (self.canResponseDocumentAction() == false) {
  948. return
  949. }
  950. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  951. document.mainViewController?.menuItemAction_showInFinder(sender)
  952. }
  953. }
  954. func menuItemAction_property(_ sender: Any) {
  955. if (self.canResponseDocumentAction() == false) {
  956. return
  957. }
  958. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  959. document.mainViewController?.menuItemAction_property(sender)
  960. }
  961. }
  962. func menuItemAction_print(_ sender: Any) {
  963. if (self.canResponseDocumentAction() == false) {
  964. return
  965. }
  966. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  967. document.mainViewController?.menuItemAction_print(sender)
  968. }
  969. }
  970. func isEncapsulatedPostScriptData(_ data: Data) -> Bool {
  971. let epsHeaderData = Data(bytes: [69, 80, 83, 70, 45], count: 5)
  972. let rg: Range = (14..<20)
  973. return (data.count >= 20 && (data.range(of: epsHeaderData, options: .anchored, in: rg) != nil))
  974. }
  975. func convertTIFFDataToPDF(_ tiffData: Data) -> Data? {
  976. guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil }
  977. let pdfData = NSMutableData(capacity: tiffData.count)
  978. let consumer = CGDataConsumer(data: pdfData! as CFMutableData)!
  979. var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
  980. let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil)
  981. ctxt!.beginPDFPage(nil)
  982. ctxt!.draw(cgImage, in: rect)
  983. ctxt!.endPDFPage()
  984. ctxt!.flush()
  985. return pdfData as? Data
  986. }
  987. func openDocumentWithImageFromPasteboard(_ pboard: NSPasteboard, error outError: AutoreleasingUnsafeMutablePointer<NSError?>?) -> Any? {
  988. var document: CPDFDocument? = nil
  989. var data: Data? = nil
  990. if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.pdf.rawValue]) {
  991. // pboard.types
  992. data = pboard.data(forType: NSPasteboard.PasteboardType.pdf)
  993. } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.postScript.rawValue]) {
  994. // pboard.types
  995. data = pboard.data(forType: NSPasteboard.PasteboardType.postScript)
  996. } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.tiff.rawValue]) {
  997. // pboard.types
  998. data = convertTIFFDataToPDF(pboard.data(forType: NSPasteboard.PasteboardType.tiff) ?? Data())
  999. } else {
  1000. let images = pboard.readObjects(forClasses: [NSImage.self], options: [:])
  1001. let strings = pboard.readObjects(forClasses: [NSAttributedString.self], options: [:])
  1002. if images?.count ?? 0 > 0 {
  1003. data = convertTIFFDataToPDF((images![0] as AnyObject).tiffRepresentation!)
  1004. } else if strings?.count ?? 0 > 0 {
  1005. data = KMOCTool.convertStringsToPDF(withString: strings ?? [""]) // convertStringsToPDF(strings!)
  1006. }
  1007. }
  1008. if let data = data {
  1009. _ = NSDocumentController.shared
  1010. document = CPDFDocument(data: data)
  1011. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1012. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1013. let filePath = savePath.deletingLastPathComponent
  1014. if FileManager.default.fileExists(atPath: filePath) == false {
  1015. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1016. }
  1017. document?.write(to: URL(fileURLWithPath: savePath))
  1018. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1019. if error != nil {
  1020. NSApp.presentError(error!)
  1021. } else {
  1022. if document is KMMainDocument {
  1023. let newDocument = document
  1024. (newDocument as! KMMainDocument).isNewCreated = true
  1025. }
  1026. }
  1027. }
  1028. } else if let outError = outError {
  1029. outError.pointee = NSError(domain: "SKDocumentErrorDomain", code: 3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Unable to load data from clipboard", comment: "Error description")])
  1030. }
  1031. return document
  1032. }
  1033. func openDocument(withURLFromPasteboard pboard: NSPasteboard, showNotes: Bool, error outError: inout NSError?) -> Any? {
  1034. let theURLs = NSURL.readURLs(from: pboard)
  1035. let url = theURLs?.count ?? 0 > 0 ? theURLs?[0] : nil
  1036. let theURL: NSURL? = url as? NSURL
  1037. var documentC = NSDocumentController.shared
  1038. var document: NSDocument? = nil
  1039. if (theURL as AnyObject).isFileURL == true {
  1040. var _: NSError? = nil
  1041. let type = try? documentC.typeForContents(of: theURL as! URL)//ForContents(ofURL: theURL, error: &error)
  1042. if showNotes == false || NSDocument.readableTypes.contains(type ?? "") {
  1043. documentC.openDocument(withContentsOf: theURL as! URL, display: true, completionHandler: { resultDocument, success, err in
  1044. document = resultDocument
  1045. })//openDocument(withContentsOfURL: theURL, display: true, error: &outError)
  1046. } else if NSDocument.readableTypes.contains(type ?? "") {
  1047. for doc in documentC.documents {
  1048. let sel = NSSelectorFromString("sourceFileURL")
  1049. if doc.responds(to: sel) && doc.fileURL == theURL as? URL {
  1050. document = doc
  1051. break
  1052. }
  1053. }
  1054. if let document: NSDocument = document {
  1055. document.showWindows()
  1056. } else {
  1057. var data: Data? = nil
  1058. // if NSWorkspace.shared.type(type ?? "", conformsToType: KMPDFBundleDocumentType) {
  1059. // let skimFileURL = FileManager.default.exte
  1060. // data = skimFileURL != nil ? try? Data(contentsOf: skimFileURL!) : nil
  1061. // } else {
  1062. // data = SKNExtendedAttributeManager.shared().extendedAttributeNamed(SKIM_NOTES_KEY, atPath: theURL.path, traverseLink: true, error: &error)
  1063. // }
  1064. // let documentNew: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  1065. if let document = try? documentC.makeUntitledDocument(ofType: KMNotesDocumentType) {
  1066. document.fileURL = URL(fileURLWithPath: theURL?.path ?? "")
  1067. // if data == nil || document.read(fromData: data!, ofType: SKNotesDocumentType, error: &error) {
  1068. documentC.addDocument(document)
  1069. document.makeWindowControllers()
  1070. document.showWindows()
  1071. // } else {
  1072. // document = nil
  1073. // if let error = error {
  1074. // outError = error
  1075. // }
  1076. // }
  1077. }
  1078. }
  1079. }
  1080. }
  1081. // else if showNotes == false && theURL != nil {
  1082. // document = SKDownloadController.shared().addDownload(for: theURL!)
  1083. // } else if outError != nil {
  1084. // outError = NSError.readPasteboardError(withLocalizedDescription: NSLocalizedString("Unable to load data from clipboard", comment: "Error description"))
  1085. // }
  1086. return document
  1087. }
  1088. @IBAction func newDocumentFromClipboard(_ sender: Any?) {
  1089. KMPrint("newDocumentFromClipboard")
  1090. var error: NSError?
  1091. let pboard = NSPasteboard.general
  1092. var document = openDocumentWithImageFromPasteboard(pboard, error: &error)
  1093. if document == nil{
  1094. document = openDocument(withURLFromPasteboard: pboard, showNotes: false, error: &error)
  1095. }
  1096. }
  1097. @IBAction func newDocumentFromImage(_ sender: Any?) {
  1098. KMPrint("importFromWebPage")
  1099. createPDFFromImage()
  1100. }
  1101. @IBAction func importFromWebPage(_ sender: Any?) {
  1102. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  1103. if (document.isHome) {
  1104. document.homeViewController?.importFromWebPage()
  1105. } else {
  1106. self.homeVC = KMHomeViewController()
  1107. self.homeVC?.importFromWebPage()
  1108. }
  1109. }
  1110. @IBAction func screenShot_SelectArea(_ sender: Any?) {
  1111. KMPrint("screenShot_SelectArea")
  1112. KMScreenShotHandler.beginScreenshot_SelectRectWithCompleteHandler { ima in
  1113. if let image = ima {
  1114. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1115. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1116. let newDocument = CPDFDocument()
  1117. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1118. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1119. if writeSuccess ?? false {
  1120. self.savePdf(savePath)
  1121. // try? FileManager.default.removeItem(atPath: savePath)
  1122. } else {
  1123. }
  1124. }
  1125. }
  1126. }
  1127. }
  1128. @IBAction func screenShot_Window(_ sender: Any?) {
  1129. KMPrint("screenShot_Window")
  1130. KMScreenShotHandler.beginScreenShot_SelectWindowCompleteHandler { ima in
  1131. if let image = ima {
  1132. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1133. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1134. let newDocument = CPDFDocument()
  1135. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1136. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1137. if writeSuccess == true {
  1138. self.savePdf(savePath)
  1139. // try? FileManager.default.removeItem(atPath: savePath)
  1140. } else {
  1141. }
  1142. }
  1143. }
  1144. }
  1145. }
  1146. @IBAction func screenShot_FullScreenDelay(_ sender: Any?) {
  1147. KMPrint("screenShot_FullScreenDelay")
  1148. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(3) { ima in
  1149. if let image = ima {
  1150. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1151. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1152. let newDocument = CPDFDocument()
  1153. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1154. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1155. if writeSuccess == true {
  1156. self.savePdf(savePath)
  1157. // try? FileManager.default.removeItem(atPath: savePath)
  1158. } else {
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. @IBAction func screenShot_FullScreen(_ sender: Any?) {
  1165. KMPrint("screenShot_FullScreen")
  1166. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(0) { ima in
  1167. if let image = ima {
  1168. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1169. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1170. let newDocument = CPDFDocument()
  1171. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1172. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1173. if writeSuccess == true {
  1174. self.savePdf(savePath)
  1175. // try? FileManager.default.removeItem(atPath: savePath)
  1176. } else {
  1177. }
  1178. }
  1179. }
  1180. }
  1181. }
  1182. }
  1183. // MARK: - KMSystemViewMenuProtocol
  1184. extension KMBrowserWindowController: KMSystemViewMenuProtocol {
  1185. func createPDFFromImage() {
  1186. let openPanel = NSOpenPanel()
  1187. openPanel.allowedFileTypes = KMImageAccessoryController.supportedImageTypes()
  1188. openPanel.allowsMultipleSelection = true
  1189. 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: "")
  1190. openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1191. if response == .OK {
  1192. self.creatPdfFromImg(urls: openPanel.urls)
  1193. }
  1194. }
  1195. }
  1196. func creatPdfFromImg(urls: Array<URL>) {
  1197. // let savePath = self.newDocumentTempSavePath("convertToPDF.pdf")
  1198. let pdf = CPDFDocument()
  1199. // var isSucceed = true
  1200. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1201. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1202. let filePath = savePath.deletingLastPathComponent
  1203. if FileManager.default.fileExists(atPath: filePath) == false {
  1204. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1205. }
  1206. for fileURL in urls {
  1207. if let imag = NSImage(contentsOfFile: fileURL.path ) {
  1208. _ = pdf?.km_insertPage(imag.size, withImage: fileURL.path , at: pdf?.pageCount ?? 0)
  1209. pdf?.write(to: URL(fileURLWithPath: savePath))
  1210. }
  1211. }
  1212. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1213. if error != nil {
  1214. NSApp.presentError(error!)
  1215. } else {
  1216. if document is KMMainDocument {
  1217. let newDocument = document
  1218. (newDocument as! KMMainDocument).isNewCreated = true
  1219. }
  1220. }
  1221. // self.homeContentView?.reloadData()
  1222. }
  1223. }
  1224. func createPdfWithParam() {
  1225. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1226. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1227. let filePath = savePath.deletingLastPathComponent
  1228. if FileManager.default.fileExists(atPath: filePath) == false {
  1229. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1230. }
  1231. let pdfDocument = CPDFDocument()
  1232. pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
  1233. pdfDocument?.write(to: URL(fileURLWithPath: savePath))
  1234. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1235. if error != nil {
  1236. NSApp.presentError(error!)
  1237. } else {
  1238. if document is KMMainDocument {
  1239. let newDocument = document
  1240. (newDocument as! KMMainDocument).isNewCreated = true
  1241. }
  1242. }
  1243. // self.homeContentView?.reloadData()
  1244. }
  1245. }
  1246. func savePdf(_ filePath: String) -> Void {
  1247. let docVc = KMDocumentController.shared
  1248. docVc.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
  1249. if error != nil {
  1250. NSApp.presentError(error!)
  1251. } else {
  1252. if document is KMMainDocument {
  1253. let newDocument = document
  1254. (newDocument as! KMMainDocument).isNewCreated = true
  1255. }
  1256. }
  1257. }
  1258. }
  1259. func newDocumentTempSavePath(_ fileName: String) -> String {
  1260. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  1261. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  1262. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  1263. return append2
  1264. }
  1265. func fetchUniquePath(_ originalPath: String) -> String {
  1266. var path = originalPath
  1267. let dManager = FileManager.default
  1268. if !dManager.fileExists(atPath: path) {
  1269. if path.extension.count < 1 {
  1270. path = path.stringByAppendingPathExtension("pdf")
  1271. }
  1272. return path
  1273. } else {
  1274. let originalFullFileName = path.lastPathComponent
  1275. let originalFileName = path.lastPathComponent.deletingPathExtension
  1276. let originalExtension = path.extension
  1277. let startIndex: Int = 0
  1278. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  1279. let fileLocatePath = originalPath.substring(to: endIndex)
  1280. var i = 1
  1281. while (1 != 0) {
  1282. var newName = String(format: "%@%ld", originalFileName, i)
  1283. newName = newName.stringByAppendingPathExtension(originalExtension)
  1284. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  1285. if !dManager.fileExists(atPath: newPath) {
  1286. return newPath
  1287. } else {
  1288. i+=1
  1289. continue
  1290. }
  1291. }
  1292. }
  1293. }
  1294. func menuItemAction_adjustWidth(_ sender: Any) {
  1295. if (self.canResponseDocumentAction() == false) {
  1296. return
  1297. }
  1298. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1299. document.mainViewController?.menuItemAction_adjustWidth(sender)
  1300. }
  1301. }
  1302. func menuItemAction_adjustPage(_ sender: Any) {
  1303. if (self.canResponseDocumentAction() == false) {
  1304. return
  1305. }
  1306. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1307. document.mainViewController?.menuItemAction_adjustPage(sender)
  1308. }
  1309. }
  1310. func menuItemAction_size(_ sender: Any) {
  1311. if (self.canResponseDocumentAction() == false) {
  1312. return
  1313. }
  1314. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1315. document.mainViewController?.menuItemAction_size(sender)
  1316. }
  1317. }
  1318. func menuItemAction_zoomOut(_ sender: Any) {
  1319. if (self.canResponseDocumentAction() == false) {
  1320. return
  1321. }
  1322. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1323. document.mainViewController?.menuItemAction_zoomOut(sender)
  1324. }
  1325. }
  1326. func menuItemAction_zoomIn(_ sender: Any) {
  1327. if (self.canResponseDocumentAction() == false) {
  1328. return
  1329. }
  1330. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1331. document.mainViewController?.menuItemAction_zoomIn(sender)
  1332. }
  1333. }
  1334. func menuItemAction_singlePage(_ sender: Any) {
  1335. if (self.canResponseDocumentAction() == false) {
  1336. return
  1337. }
  1338. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1339. document.mainViewController?.menuItemAction_singlePage(sender)
  1340. }
  1341. }
  1342. func menuItemAction_singlePageContinue(_ sender: Any) {
  1343. if (self.canResponseDocumentAction() == false) {
  1344. return
  1345. }
  1346. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1347. document.mainViewController?.menuItemAction_singlePageContinue(sender)
  1348. }
  1349. }
  1350. func menuItemAction_doublePage(_ sender: Any) {
  1351. if (self.canResponseDocumentAction() == false) {
  1352. return
  1353. }
  1354. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1355. document.mainViewController?.menuItemAction_doublePage(sender)
  1356. }
  1357. }
  1358. func menuItemAction_doublePageContinue(_ sender: Any) {
  1359. if (self.canResponseDocumentAction() == false) {
  1360. return
  1361. }
  1362. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1363. document.mainViewController?.menuItemAction_doublePageContinue(sender)
  1364. }
  1365. }
  1366. func menuItemAction_bookMode(_ sender: Any) {
  1367. if (self.canResponseDocumentAction() == false) {
  1368. return
  1369. }
  1370. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1371. document.mainViewController?.menuItemAction_bookMode(sender)
  1372. }
  1373. }
  1374. func menuItemAction_readMode(_ sender: Any) {
  1375. if (self.canResponseDocumentAction() == false) {
  1376. return
  1377. }
  1378. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1379. document.mainViewController?.menuItemAction_readMode(sender)
  1380. }
  1381. }
  1382. func menuItemAction_showSplitPage(_ sender: Any) {
  1383. if (self.canResponseDocumentAction() == false) {
  1384. return
  1385. }
  1386. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1387. document.mainViewController?.menuItemAction_showSplitPage(sender)
  1388. }
  1389. }
  1390. func menuItemAction_rotateLeft(_ sender: Any) {
  1391. if (self.canResponseDocumentAction() == false) {
  1392. return
  1393. }
  1394. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1395. document.mainViewController?.menuItemAction_rotateLeft(sender)
  1396. }
  1397. }
  1398. func menuItemAction_rotateRight(_ sender: Any) {
  1399. if (self.canResponseDocumentAction() == false) {
  1400. return
  1401. }
  1402. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1403. document.mainViewController?.menuItemAction_rotateRight(sender)
  1404. }
  1405. }
  1406. func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  1407. if (self.canResponseDocumentAction() == false) {
  1408. return
  1409. }
  1410. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1411. document.mainViewController?.menuItemAction_rotateAllPageLeft(sender)
  1412. }
  1413. }
  1414. func menuItemAction_rotateAllPageRight(_ sender: Any) {
  1415. if (self.canResponseDocumentAction() == false) {
  1416. return
  1417. }
  1418. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1419. document.mainViewController?.menuItemAction_rotateAllPageRight(sender)
  1420. }
  1421. }
  1422. func menuItemAction_view_readMode(_ sender: Any) {
  1423. if (self.canResponseDocumentAction() == false) {
  1424. return
  1425. }
  1426. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1427. document.mainViewController?.menuItemAction_view_readMode(sender)
  1428. }
  1429. }
  1430. func menuItemAction_hiddenLeftSide(_ sender: Any) {
  1431. if (self.canResponseDocumentAction() == false) {
  1432. return
  1433. }
  1434. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1435. document.mainViewController?.menuItemAction_hiddenLeftSide(sender)
  1436. }
  1437. }
  1438. func menuItemAction_hiddenRightSide(_ sender: Any) {
  1439. if (self.canResponseDocumentAction() == false) {
  1440. return
  1441. }
  1442. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1443. document.mainViewController?.menuItemAction_hiddenRightSide(sender)
  1444. }
  1445. }
  1446. func menuItemAction_thumai(_ sender: Any) {
  1447. if (self.canResponseDocumentAction() == false) {
  1448. return
  1449. }
  1450. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1451. document.mainViewController?.menuItemAction_thumai(sender)
  1452. }
  1453. }
  1454. func menuItemAction_outline(_ sender: Any) {
  1455. if (self.canResponseDocumentAction() == false) {
  1456. return
  1457. }
  1458. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1459. document.mainViewController?.menuItemAction_outline(sender)
  1460. }
  1461. }
  1462. func menuItemAction_bookmark(_ sender: Any) {
  1463. if (self.canResponseDocumentAction() == false) {
  1464. return
  1465. }
  1466. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1467. document.mainViewController?.menuItemAction_bookmark(sender)
  1468. }
  1469. }
  1470. func menuItemAction_annotation(_ sender: Any) {
  1471. if (self.canResponseDocumentAction() == false) {
  1472. return
  1473. }
  1474. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1475. document.mainViewController?.menuItemAction_annotation(sender)
  1476. }
  1477. }
  1478. func menuItemAction_search(_ sender: Any) {
  1479. if (self.canResponseDocumentAction() == false) {
  1480. return
  1481. }
  1482. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1483. document.mainViewController?.menuItemAction_search(sender)
  1484. }
  1485. }
  1486. func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  1487. if (self.canResponseDocumentAction() == false) {
  1488. return
  1489. }
  1490. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1491. document.mainViewController?.menuItemAction_hiddenPageIndicator(sender)
  1492. }
  1493. }
  1494. @IBAction func togglePresentation(_ sender: Any?) {
  1495. if (self.canResponseDocumentAction() == false) {
  1496. return
  1497. }
  1498. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1499. document.mainViewController?.togglePresentation(sender)
  1500. }
  1501. }
  1502. @IBAction func chooseTransition(_ sender: Any?) {
  1503. if (self.canResponseDocumentAction() == false) {
  1504. return
  1505. }
  1506. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1507. document.mainViewController?.chooseTransition(sender)
  1508. }
  1509. }
  1510. @IBAction func toggleAutoFlow(_ sender: Any?) {
  1511. if (self.canResponseDocumentAction() == false) {
  1512. return
  1513. }
  1514. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1515. document.mainViewController?.toggleAutoFlow(sender)
  1516. }
  1517. }
  1518. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  1519. if (self.canResponseDocumentAction() == false) {
  1520. return
  1521. }
  1522. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1523. document.mainViewController?.chooseAutoFlowSetting(sender)
  1524. }
  1525. }
  1526. @IBAction func toggleReadingBar(_ sender: Any?) {
  1527. if (self.canResponseDocumentAction() == false) {
  1528. return
  1529. }
  1530. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1531. document.mainViewController?.toggleReadingBar(sender)
  1532. }
  1533. }
  1534. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  1535. if (self.canResponseDocumentAction() == false) {
  1536. return
  1537. }
  1538. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1539. document.mainViewController?.toggleLeftSidePane(sender)
  1540. }
  1541. }
  1542. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  1543. if (self.canResponseDocumentAction() == false) {
  1544. return
  1545. }
  1546. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1547. document.mainViewController?.changeLeftSidePaneState(sender)
  1548. }
  1549. }
  1550. @IBAction func splitViewAction(_ sender: Any?) {
  1551. if (self.canResponseDocumentAction() == false) {
  1552. return
  1553. }
  1554. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1555. document.mainViewController?.splitViewAction(sender)
  1556. }
  1557. }
  1558. @IBAction func themesColor(_ sender: Any?) {
  1559. if (self.canResponseDocumentAction() == false) {
  1560. return
  1561. }
  1562. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1563. document.mainViewController?.themesColor(sender)
  1564. }
  1565. }
  1566. @IBAction func toggleStatusBar(_ sender: Any?) {
  1567. if (self.canResponseDocumentAction() == false) {
  1568. return
  1569. }
  1570. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1571. document.mainViewController?.toggleStatusBar(sender)
  1572. }
  1573. }
  1574. @IBAction func toggleToolbarShow(_ sender: Any?) {
  1575. if (self.canResponseDocumentAction() == false) {
  1576. return
  1577. }
  1578. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1579. document.mainViewController?.toggleToolbarShow(sender)
  1580. }
  1581. }
  1582. }
  1583. // MARK: - KMSystemEditMenuProtocol
  1584. extension KMBrowserWindowController: KMSystemEditMenuProtocol {
  1585. func menuItemAction_find(_ sender: Any) {
  1586. if (self.canResponseDocumentAction() == false) {
  1587. return
  1588. }
  1589. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1590. document.mainViewController?.menuItemAction_find(sender)
  1591. }
  1592. }
  1593. @IBAction func changeFont(_ sender: Any?) {
  1594. KMPrint("changeFont ...")
  1595. }
  1596. }
  1597. // MARK: - Annotation Menu
  1598. extension KMBrowserWindowController {
  1599. @IBAction func menuItemAction_highlight(_ sender: Any) {
  1600. if (self.canResponseDocumentAction() == false) {
  1601. return
  1602. }
  1603. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1604. document.mainViewController?.menuItemAction_highlight(sender)
  1605. }
  1606. }
  1607. @IBAction func menuItemAction_underline(_ sender: Any) {
  1608. if (self.canResponseDocumentAction() == false) {
  1609. return
  1610. }
  1611. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1612. document.mainViewController?.menuItemAction_underline(sender)
  1613. }
  1614. }
  1615. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  1616. if (self.canResponseDocumentAction() == false) {
  1617. return
  1618. }
  1619. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1620. document.mainViewController?.menuItemAction_deleteLine(sender)
  1621. }
  1622. }
  1623. @IBAction func menuItemAction_freehand(_ sender: Any) {
  1624. if (self.canResponseDocumentAction() == false) {
  1625. return
  1626. }
  1627. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1628. document.mainViewController?.menuItemAction_freehand(sender)
  1629. }
  1630. }
  1631. @IBAction func menuItemAction_text(_ sender: Any) {
  1632. if (self.canResponseDocumentAction() == false) {
  1633. return
  1634. }
  1635. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1636. document.mainViewController?.menuItemAction_text(sender)
  1637. }
  1638. }
  1639. @IBAction func menuItemAction_note(_ sender: Any) {
  1640. if (self.canResponseDocumentAction() == false) {
  1641. return
  1642. }
  1643. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1644. document.mainViewController?.menuItemAction_note(sender)
  1645. }
  1646. }
  1647. @IBAction func menuItemAction_squre(_ sender: Any) {
  1648. if (self.canResponseDocumentAction() == false) {
  1649. return
  1650. }
  1651. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1652. document.mainViewController?.menuItemAction_squre(sender)
  1653. }
  1654. }
  1655. @IBAction func menuItemAction_circle(_ sender: Any) {
  1656. if (self.canResponseDocumentAction() == false) {
  1657. return
  1658. }
  1659. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1660. document.mainViewController?.menuItemAction_circle(sender)
  1661. }
  1662. }
  1663. @IBAction func menuItemAction_arrow(_ sender: Any) {
  1664. if (self.canResponseDocumentAction() == false) {
  1665. return
  1666. }
  1667. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1668. document.mainViewController?.menuItemAction_arrow(sender)
  1669. }
  1670. }
  1671. @IBAction func menuItemAction_line(_ sender: Any) {
  1672. if (self.canResponseDocumentAction() == false) {
  1673. return
  1674. }
  1675. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1676. document.mainViewController?.menuItemAction_line(sender)
  1677. }
  1678. }
  1679. // link
  1680. @IBAction func menuItemAction_link(_ sender: Any) {
  1681. if (self.canResponseDocumentAction() == false) {
  1682. return
  1683. }
  1684. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1685. document.mainViewController?.menuItemAction_link(sender)
  1686. }
  1687. }
  1688. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  1689. if (self.canResponseDocumentAction() == false) {
  1690. return
  1691. }
  1692. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1693. document.mainViewController?.menuItemAction_linkPage(sender)
  1694. }
  1695. }
  1696. // stamp
  1697. @IBAction func menuItemAction_stamp(_ sender: Any) {
  1698. if (self.canResponseDocumentAction() == false) {
  1699. return
  1700. }
  1701. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1702. document.mainViewController?.menuItemAction_stamp(sender)
  1703. }
  1704. }
  1705. @IBAction func notesLink(_ sender: Any?) {
  1706. if (self.canResponseDocumentAction() == false) {
  1707. return
  1708. }
  1709. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1710. document.mainViewController?.notesLink(sender)
  1711. }
  1712. }
  1713. @IBAction func notesStamp(_ sender: Any?) {
  1714. if (self.canResponseDocumentAction() == false) {
  1715. return
  1716. }
  1717. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1718. document.mainViewController?.notesStamp(sender)
  1719. }
  1720. }
  1721. @IBAction func menuItemAction_signure(_ sender: Any) {
  1722. if (self.canResponseDocumentAction() == false) {
  1723. return
  1724. }
  1725. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1726. document.mainViewController?.menuItemAction_signure(sender)
  1727. }
  1728. }
  1729. @IBAction func image(_ sender: Any?) {
  1730. if (self.canResponseDocumentAction() == false) {
  1731. return
  1732. }
  1733. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1734. document.mainViewController?.image(sender)
  1735. }
  1736. }
  1737. @IBAction func table(_ sender: Any?) {
  1738. if (self.canResponseDocumentAction() == false) {
  1739. return
  1740. }
  1741. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1742. document.mainViewController?.table(sender)
  1743. }
  1744. }
  1745. @IBAction func addForm(_ sender: Any?) {
  1746. if (self.canResponseDocumentAction() == false) {
  1747. return
  1748. }
  1749. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1750. document.mainViewController?.table(addForm)
  1751. }
  1752. }
  1753. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  1754. if (self.canResponseDocumentAction() == false) {
  1755. return
  1756. }
  1757. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1758. document.mainViewController?.menuItemAction_hiddenAllAnnotation(sender)
  1759. }
  1760. }
  1761. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  1762. if (self.canResponseDocumentAction() == false) {
  1763. return
  1764. }
  1765. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1766. document.mainViewController?.menuItemAction_clearAllAnnotation(sender)
  1767. }
  1768. }
  1769. }
  1770. // MARK: - KMSystemPDFMenuProtocol
  1771. extension KMBrowserWindowController: KMSystemPDFMenuProtocol {
  1772. @IBAction func changeDisplayMode(_ sender: Any?) {
  1773. if (self.canResponseDocumentAction() == false) {
  1774. return
  1775. }
  1776. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1777. document.mainViewController?.changeDisplayMode(sender)
  1778. }
  1779. }
  1780. @IBAction func toggleDisplayAsBook(_ sender: Any?) {
  1781. if (self.canResponseDocumentAction() == false) {
  1782. return
  1783. }
  1784. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1785. document.mainViewController?.toggleDisplayAsBook(sender)
  1786. }
  1787. }
  1788. @IBAction func toggleDisplayPageBreaks(_ sender: Any?) {
  1789. if (self.canResponseDocumentAction() == false) {
  1790. return
  1791. }
  1792. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1793. document.mainViewController?.toggleDisplayPageBreaks(sender)
  1794. }
  1795. }
  1796. @IBAction func changeDisplayBox(_ sender: Any?) {
  1797. if (self.canResponseDocumentAction() == false) {
  1798. return
  1799. }
  1800. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1801. document.mainViewController?.changeDisplayBox(sender)
  1802. }
  1803. }
  1804. @IBAction func toggleAutoScale(_ sender: Any?) {
  1805. if (self.canResponseDocumentAction() == false) {
  1806. return
  1807. }
  1808. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1809. document.mainViewController?.toggleAutoScale(sender)
  1810. }
  1811. }
  1812. @IBAction func doZoomToActualSize(_ sender: Any?) {
  1813. if (self.canResponseDocumentAction() == false) {
  1814. return
  1815. }
  1816. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1817. document.mainViewController?.doZoomToActualSize(sender)
  1818. }
  1819. }
  1820. @IBAction func doZoomToFit(_ sender: Any?) {
  1821. if (self.canResponseDocumentAction() == false) {
  1822. return
  1823. }
  1824. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1825. document.mainViewController?.doZoomToFit(sender)
  1826. }
  1827. }
  1828. @IBAction func doZoomIn(_ sender: Any?) {
  1829. if (self.canResponseDocumentAction() == false) {
  1830. return
  1831. }
  1832. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1833. document.mainViewController?.doZoomIn(sender)
  1834. }
  1835. }
  1836. @IBAction func doZoomOut(_ sender: Any?) {
  1837. if (self.canResponseDocumentAction() == false) {
  1838. return
  1839. }
  1840. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1841. document.mainViewController?.doZoomOut(sender)
  1842. }
  1843. }
  1844. @IBAction func doMarqueeZoomTool(_ sender: Any?) {
  1845. if (self.canResponseDocumentAction() == false) {
  1846. return
  1847. }
  1848. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1849. document.mainViewController?.doMarqueeZoomTool(sender)
  1850. }
  1851. }
  1852. @IBAction func highlightFormFiled(_ sender: Any?) {
  1853. if (self.canResponseDocumentAction() == false) {
  1854. return
  1855. }
  1856. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1857. document.mainViewController?.highlightFormFiled(sender)
  1858. }
  1859. }
  1860. @IBAction func highlightLinks(_ sender: Any?) {
  1861. if (self.canResponseDocumentAction() == false) {
  1862. return
  1863. }
  1864. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1865. document.mainViewController?.highlightLinks(sender)
  1866. }
  1867. }
  1868. @IBAction func resetForm(_ sender: Any?) {
  1869. if (self.canResponseDocumentAction() == false) {
  1870. return
  1871. }
  1872. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1873. document.mainViewController?.resetForm(sender)
  1874. }
  1875. }
  1876. @IBAction func removeAllAnnotation(_ sender: Any?) {
  1877. if (self.canResponseDocumentAction() == false) {
  1878. return
  1879. }
  1880. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1881. document.mainViewController?.removeAllAnnotation(sender)
  1882. }
  1883. }
  1884. @IBAction func savePDFSettingToDefaults(_ sender: Any?) {
  1885. if (self.canResponseDocumentAction() == false) {
  1886. return
  1887. }
  1888. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1889. document.mainViewController?.savePDFSettingToDefaults(sender)
  1890. }
  1891. }
  1892. }
  1893. // MARK: - KMSystemGotoMenuProtocol
  1894. extension KMBrowserWindowController: KMSystemGotoMenuProtocol {
  1895. func menuItemAction_nextPage(_ sender: Any) {
  1896. if (self.canResponseDocumentAction() == false) {
  1897. return
  1898. }
  1899. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1900. document.mainViewController?.menuItemAction_nextPage(sender)
  1901. }
  1902. }
  1903. func menuItemAction_forwardPage(_ sender: Any) {
  1904. if (self.canResponseDocumentAction() == false) {
  1905. return
  1906. }
  1907. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1908. document.mainViewController?.menuItemAction_forwardPage(sender)
  1909. }
  1910. }
  1911. func menuItemAction_firstPage(_ sender: Any) {
  1912. if (self.canResponseDocumentAction() == false) {
  1913. return
  1914. }
  1915. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1916. document.mainViewController?.menuItemAction_firstPage(sender)
  1917. }
  1918. }
  1919. func menuItemAction_lastPage(_ sender: Any) {
  1920. if (self.canResponseDocumentAction() == false) {
  1921. return
  1922. }
  1923. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1924. document.mainViewController?.menuItemAction_lastPage(sender)
  1925. }
  1926. }
  1927. func menuItemAction_forward(_ sender: Any) {
  1928. if (self.canResponseDocumentAction() == false) {
  1929. return
  1930. }
  1931. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1932. document.mainViewController?.menuItemAction_forward(sender)
  1933. }
  1934. }
  1935. func menuItemAction_goback(_ sender: Any) {
  1936. if (self.canResponseDocumentAction() == false) {
  1937. return
  1938. }
  1939. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1940. document.mainViewController?.menuItemAction_goback(sender)
  1941. }
  1942. }
  1943. func menuItemAction_gotoPage(_ sender: Any) {
  1944. if (self.canResponseDocumentAction() == false) {
  1945. return
  1946. }
  1947. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1948. document.mainViewController?.menuItemAction_gotoPage(sender)
  1949. }
  1950. }
  1951. @IBAction func goToMarkedPage(_ sender: Any?) {
  1952. if (self.canResponseDocumentAction() == false) {
  1953. return
  1954. }
  1955. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1956. document.mainViewController?.goToMarkedPage(sender)
  1957. }
  1958. }
  1959. @IBAction func markPage(_ sender: Any?) {
  1960. if (self.canResponseDocumentAction() == false) {
  1961. return
  1962. }
  1963. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1964. document.mainViewController?.markPage(sender)
  1965. }
  1966. }
  1967. }
  1968. // MARK: - KMSystemToolMenuProtocol
  1969. extension KMBrowserWindowController: KMSystemToolMenuProtocol {
  1970. @IBAction func getInfo(_ sender: Any?) {
  1971. if (self.canResponseDocumentAction() == false) {
  1972. return
  1973. }
  1974. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1975. document.mainViewController?.getInfo(sender)
  1976. }
  1977. }
  1978. func orderFrontNotesPanel(_ sender: Any?) {
  1979. let windowC = KMNotesPanelController.shared
  1980. if let win = windowC.window {
  1981. if win.isVisible {
  1982. win.orderOut(sender)
  1983. } else {
  1984. win.orderFront(sender)
  1985. }
  1986. }
  1987. }
  1988. func menuItemAction_scrolTool(_ sender: Any) {
  1989. if (self.canResponseDocumentAction() == false) {
  1990. return
  1991. }
  1992. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1993. document.mainViewController?.menuItemAction_scrolTool(sender)
  1994. }
  1995. }
  1996. func menuItemAction_zoomOutTool(_ sender: Any) {
  1997. if (self.canResponseDocumentAction() == false) {
  1998. return
  1999. }
  2000. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2001. document.mainViewController?.menuItemAction_zoomOutTool(sender)
  2002. }
  2003. }
  2004. func menuItemAction_selectTool(_ sender: Any) {
  2005. if (self.canResponseDocumentAction() == false) {
  2006. return
  2007. }
  2008. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2009. document.mainViewController?.menuItemAction_selectTool(sender)
  2010. }
  2011. }
  2012. @IBAction func changeToolMode(_ sender: Any?) {
  2013. if (self.canResponseDocumentAction() == false) {
  2014. return
  2015. }
  2016. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2017. if (document.isHome) {
  2018. } else {
  2019. document.mainViewController?.changeToolMode(sender)
  2020. }
  2021. }
  2022. }
  2023. @IBAction func compression(_ sender: Any?) {
  2024. if (self.canResponseDocumentAction() == false) {
  2025. return
  2026. }
  2027. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2028. if (document.isHome) {
  2029. } else {
  2030. document.mainViewController?.compression(sender)
  2031. }
  2032. }
  2033. }
  2034. @IBAction func merge(_ sender: Any?) {
  2035. if (self.canResponseDocumentAction() == false) {
  2036. return
  2037. }
  2038. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2039. if (document.isHome) {
  2040. } else {
  2041. document.mainViewController?.merge(sender)
  2042. }
  2043. }
  2044. }
  2045. @IBAction func convert(_ sender: Any?) {
  2046. if (self.canResponseDocumentAction() == false) {
  2047. return
  2048. }
  2049. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2050. if (document.isHome) {
  2051. } else {
  2052. document.mainViewController?.convert(sender)
  2053. }
  2054. }
  2055. }
  2056. @IBAction func imageToPDF(_ sender: Any?) {
  2057. if (self.canResponseDocumentAction() == false) {
  2058. return
  2059. }
  2060. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2061. if (document.isHome) {
  2062. } else {
  2063. document.mainViewController?.imageToPDF(sender)
  2064. }
  2065. }
  2066. }
  2067. @IBAction func ocr(_ sender: Any?) {
  2068. if (self.canResponseDocumentAction() == false) {
  2069. return
  2070. }
  2071. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2072. if (document.isHome) {
  2073. } else {
  2074. document.mainViewController?.ocr(sender)
  2075. }
  2076. }
  2077. }
  2078. @IBAction func extractImage(_ sender: Any?) {
  2079. if (self.canResponseDocumentAction() == false) {
  2080. return
  2081. }
  2082. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2083. if (document.isHome) {
  2084. } else {
  2085. document.mainViewController?.extractImage(sender)
  2086. }
  2087. }
  2088. }
  2089. @IBAction func pageEdit(_ sender: Any?) {
  2090. if (self.canResponseDocumentAction() == false) {
  2091. return
  2092. }
  2093. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2094. if (document.isHome) {
  2095. } else {
  2096. document.mainViewController?.pageEdit(sender)
  2097. }
  2098. }
  2099. }
  2100. @IBAction func textEditAction(_ sender: Any?) {
  2101. if (self.canResponseDocumentAction() == false) {
  2102. return
  2103. }
  2104. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2105. if (document.isHome) {
  2106. } else {
  2107. document.mainViewController?.textEditAction(sender)
  2108. }
  2109. }
  2110. }
  2111. @IBAction func security(_ sender: Any?) {
  2112. if (self.canResponseDocumentAction() == false) {
  2113. return
  2114. }
  2115. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2116. if (document.isHome) {
  2117. } else {
  2118. document.mainViewController?.security(sender)
  2119. }
  2120. }
  2121. }
  2122. @IBAction func decryption(_ sender: Any?) {
  2123. if (self.canResponseDocumentAction() == false) {
  2124. return
  2125. }
  2126. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2127. if (document.isHome) {
  2128. } else {
  2129. document.mainViewController?.decryption(sender)
  2130. }
  2131. }
  2132. }
  2133. @IBAction func cropAll(_ sender: Any?) {
  2134. if (self.canResponseDocumentAction() == false) {
  2135. return
  2136. }
  2137. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2138. if (document.isHome) {
  2139. } else {
  2140. document.mainViewController?.cropAll(sender)
  2141. }
  2142. }
  2143. }
  2144. @IBAction func crop(_ sender: Any?) {
  2145. if (self.canResponseDocumentAction() == false) {
  2146. return
  2147. }
  2148. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2149. if (document.isHome) {
  2150. } else {
  2151. document.mainViewController?.crop(sender)
  2152. }
  2153. }
  2154. }
  2155. @IBAction func autoCropAll(_ sender: Any?) {
  2156. if (self.canResponseDocumentAction() == false) {
  2157. return
  2158. }
  2159. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2160. if (document.isHome) {
  2161. } else {
  2162. document.mainViewController?.autoCropAll(sender)
  2163. }
  2164. }
  2165. }
  2166. @IBAction func smartAutoCropAll(_ sender: Any?) {
  2167. if (self.canResponseDocumentAction() == false) {
  2168. return
  2169. }
  2170. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2171. if (document.isHome) {
  2172. } else {
  2173. document.mainViewController?.smartAutoCropAll(sender)
  2174. }
  2175. }
  2176. }
  2177. @IBAction func cropOptionsModel(_ sender: Any?) {
  2178. if (self.canResponseDocumentAction() == false) {
  2179. return
  2180. }
  2181. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2182. if (document.isHome) {
  2183. } else {
  2184. // document.mainViewController?.cropOptionsModel(sender)
  2185. }
  2186. }
  2187. }
  2188. @IBAction func selectToolModel(_ sender: Any?) {
  2189. if (self.canResponseDocumentAction() == false) {
  2190. return
  2191. }
  2192. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2193. if (document.isHome) {
  2194. } else {
  2195. document.mainViewController?.selectToolModel(sender)
  2196. }
  2197. }
  2198. }
  2199. @IBAction func takeSnapshot(_ sender: Any?) {
  2200. if (self.canResponseDocumentAction() == false) {
  2201. return
  2202. }
  2203. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2204. if (document.isHome) {
  2205. } else {
  2206. document.mainViewController?.takeSnapshot(sender)
  2207. }
  2208. }
  2209. }
  2210. @IBAction func takeSnapshotSelectContent(_ sender: Any?) {
  2211. if (self.canResponseDocumentAction() == false) {
  2212. return
  2213. }
  2214. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2215. if (document.isHome) {
  2216. } else {
  2217. document.mainViewController?.takeSnapshotSelectContent(sender)
  2218. }
  2219. }
  2220. }
  2221. @IBAction func changeAnnotationMode(_ sender: Any?) {
  2222. if (self.canResponseDocumentAction() == false) {
  2223. return
  2224. }
  2225. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2226. if (document.isHome) {
  2227. } else {
  2228. document.mainViewController?.changeAnnotationMode(sender)
  2229. }
  2230. }
  2231. }
  2232. @IBAction func addBackground(_ sender: Any?) {
  2233. if (self.canResponseDocumentAction() == false) {
  2234. return
  2235. }
  2236. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2237. if (document.isHome) {
  2238. } else {
  2239. document.mainViewController?.addBackground(sender)
  2240. }
  2241. }
  2242. }
  2243. @IBAction func removeBackground(_ 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?.removeBackground(sender)
  2251. }
  2252. }
  2253. }
  2254. @IBAction func addWatermark(_ 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?.addWatermark(sender)
  2262. }
  2263. }
  2264. }
  2265. @IBAction func editWatermark(_ sender: Any?) {
  2266. KMPrint("editWatermark ...")
  2267. }
  2268. @IBAction func batchAddWatermark(_ sender: Any?) {
  2269. if (self.canResponseDocumentAction() == false) {
  2270. return
  2271. }
  2272. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2273. if (document.isHome) {
  2274. } else {
  2275. document.mainViewController?.batchAddWatermark(sender)
  2276. }
  2277. }
  2278. }
  2279. @IBAction func batchRemoveWatermark(_ sender: Any?) {
  2280. if (self.canResponseDocumentAction() == false) {
  2281. return
  2282. }
  2283. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2284. if (document.isHome) {
  2285. } else {
  2286. document.mainViewController?.batchRemoveWatermark(sender)
  2287. }
  2288. }
  2289. }
  2290. @IBAction func removeWatermark(_ sender: Any?) {
  2291. if (self.canResponseDocumentAction() == false) {
  2292. return
  2293. }
  2294. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2295. if (document.isHome) {
  2296. } else {
  2297. document.mainViewController?.removeWatermark(sender)
  2298. }
  2299. }
  2300. }
  2301. @IBAction func templateWatermark(_ sender: Any?) {
  2302. if (self.canResponseDocumentAction() == false) {
  2303. return
  2304. }
  2305. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2306. if (document.isHome) {
  2307. } else {
  2308. document.mainViewController?.templateWatermark(sender)
  2309. }
  2310. }
  2311. }
  2312. @IBAction func batchAddBackground(_ sender: Any?) {
  2313. if (self.canResponseDocumentAction() == false) {
  2314. return
  2315. }
  2316. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2317. if (document.isHome) {
  2318. } else {
  2319. document.mainViewController?.batchAddBackground(sender)
  2320. }
  2321. }
  2322. }
  2323. @IBAction func batchRemoveBackground(_ sender: Any?) {
  2324. if (self.canResponseDocumentAction() == false) {
  2325. return
  2326. }
  2327. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2328. if (document.isHome) {
  2329. } else {
  2330. document.mainViewController?.batchRemoveBackground(sender)
  2331. }
  2332. }
  2333. }
  2334. @IBAction func templateBackground(_ sender: Any?) {
  2335. if (self.canResponseDocumentAction() == false) {
  2336. return
  2337. }
  2338. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2339. if (document.isHome) {
  2340. } else {
  2341. document.mainViewController?.templateBackground(sender)
  2342. }
  2343. }
  2344. }
  2345. @IBAction func addHeaderFooter(_ sender: Any?) {
  2346. if (self.canResponseDocumentAction() == false) {
  2347. return
  2348. }
  2349. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2350. if (document.isHome) {
  2351. } else {
  2352. document.mainViewController?.addHeaderFooter(sender)
  2353. }
  2354. }
  2355. }
  2356. @IBAction func removeHeaderFooter(_ sender: Any?) {
  2357. if (self.canResponseDocumentAction() == false) {
  2358. return
  2359. }
  2360. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2361. if (document.isHome) {
  2362. } else {
  2363. document.mainViewController?.removeHeaderFooter(sender)
  2364. }
  2365. }
  2366. }
  2367. @IBAction func batchAddHeaderFooter(_ sender: Any?) {
  2368. if (self.canResponseDocumentAction() == false) {
  2369. return
  2370. }
  2371. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2372. if (document.isHome) {
  2373. } else {
  2374. document.mainViewController?.batchAddHeaderFooter(sender)
  2375. }
  2376. }
  2377. }
  2378. @IBAction func batchRemoveHeaderFooter(_ sender: Any?) {
  2379. if (self.canResponseDocumentAction() == false) {
  2380. return
  2381. }
  2382. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2383. if (document.isHome) {
  2384. } else {
  2385. document.mainViewController?.batchRemoveHeaderFooter(sender)
  2386. }
  2387. }
  2388. }
  2389. @IBAction func templateHeaderFooter(_ sender: Any?) {
  2390. if (self.canResponseDocumentAction() == false) {
  2391. return
  2392. }
  2393. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2394. if (document.isHome) {
  2395. } else {
  2396. document.mainViewController?.templateHeaderFooter(sender)
  2397. }
  2398. }
  2399. }
  2400. @IBAction func addHeaderFooterBates(_ sender: Any?) {
  2401. if (self.canResponseDocumentAction() == false) {
  2402. return
  2403. }
  2404. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2405. if (document.isHome) {
  2406. } else {
  2407. document.mainViewController?.addHeaderFooterBates(sender)
  2408. }
  2409. }
  2410. }
  2411. @IBAction func removeHeaderFooterBates(_ sender: Any?) {
  2412. if (self.canResponseDocumentAction() == false) {
  2413. return
  2414. }
  2415. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2416. if (document.isHome) {
  2417. } else {
  2418. document.mainViewController?.removeHeaderFooterBates(sender)
  2419. }
  2420. }
  2421. }
  2422. @IBAction func batchAddBates(_ sender: Any?) {
  2423. if (self.canResponseDocumentAction() == false) {
  2424. return
  2425. }
  2426. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2427. if (document.isHome) {
  2428. } else {
  2429. document.mainViewController?.batchAddBates(sender)
  2430. }
  2431. }
  2432. }
  2433. @IBAction func batchRemoveBates(_ sender: Any?) {
  2434. if (self.canResponseDocumentAction() == false) {
  2435. return
  2436. }
  2437. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2438. if (document.isHome) {
  2439. } else {
  2440. document.mainViewController?.batchRemoveBates(sender)
  2441. }
  2442. }
  2443. }
  2444. @IBAction func templateBates(_ sender: Any?) {
  2445. if (self.canResponseDocumentAction() == false) {
  2446. return
  2447. }
  2448. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2449. if (document.isHome) {
  2450. } else {
  2451. document.mainViewController?.templateBates(sender)
  2452. }
  2453. }
  2454. }
  2455. @IBAction func togglePoster(_ sender: Any?) {
  2456. if (self.canResponseDocumentAction() == false) {
  2457. return
  2458. }
  2459. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2460. if (document.isHome) {
  2461. } else {
  2462. document.mainViewController?.togglePoster(sender)
  2463. }
  2464. }
  2465. }
  2466. @IBAction func toggleMultiple(_ sender: Any?) {
  2467. if (self.canResponseDocumentAction() == false) {
  2468. return
  2469. }
  2470. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2471. if (document.isHome) {
  2472. } else {
  2473. document.mainViewController?.toggleMultiple(sender)
  2474. }
  2475. }
  2476. }
  2477. @IBAction func toggleBooklet(_ sender: Any?) {
  2478. if (self.canResponseDocumentAction() == false) {
  2479. return
  2480. }
  2481. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2482. if (document.isHome) {
  2483. } else {
  2484. document.mainViewController?.toggleBooklet(sender)
  2485. }
  2486. }
  2487. }
  2488. @IBAction func rotateRight(_ sender: Any?) {
  2489. if (self.canResponseDocumentAction() == false) {
  2490. return
  2491. }
  2492. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2493. if (document.isHome) {
  2494. } else {
  2495. document.mainViewController?.rotateRight(sender)
  2496. }
  2497. }
  2498. }
  2499. @IBAction func rotateLeft(_ sender: Any?) {
  2500. if (self.canResponseDocumentAction() == false) {
  2501. return
  2502. }
  2503. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2504. if (document.isHome) {
  2505. } else {
  2506. document.mainViewController?.rotateLeft(sender)
  2507. }
  2508. }
  2509. }
  2510. @IBAction func rotateAllRight(_ sender: Any?) {
  2511. if (self.canResponseDocumentAction() == false) {
  2512. return
  2513. }
  2514. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2515. if (document.isHome) {
  2516. } else {
  2517. document.mainViewController?.rotateAllRight(sender)
  2518. }
  2519. }
  2520. }
  2521. @IBAction func rotateAllLeft(_ sender: Any?) {
  2522. if (self.canResponseDocumentAction() == false) {
  2523. return
  2524. }
  2525. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2526. if (document.isHome) {
  2527. } else {
  2528. document.mainViewController?.rotateAllLeft(sender)
  2529. }
  2530. }
  2531. }
  2532. @IBAction func autoSelectContent(_ sender: Any?) {
  2533. if (self.canResponseDocumentAction() == false) {
  2534. return
  2535. }
  2536. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2537. if (document.isHome) {
  2538. } else {
  2539. document.mainViewController?.autoSelectContent(sender)
  2540. }
  2541. }
  2542. }
  2543. }
  2544. // MARK: - KMSystemWindowMenuRrotocol
  2545. extension KMBrowserWindowController: KMSystemWindowMenuRrotocol {
  2546. func menuItemAction_showForwardTagPage(_ sender: Any) {
  2547. if (self.canResponseDocumentAction() == false) {
  2548. return
  2549. }
  2550. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2551. if (document.isHome) {
  2552. document.homeViewController?.menuItemAction_showForwardTagPage(sender)
  2553. } else {
  2554. document.mainViewController?.menuItemAction_showForwardTagPage(sender)
  2555. }
  2556. }
  2557. }
  2558. func menuItemAction_showNextTagPage(_ sender: Any) {
  2559. if (self.canResponseDocumentAction() == false) {
  2560. return
  2561. }
  2562. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2563. if (document.isHome) {
  2564. document.homeViewController?.menuItemAction_showNextTagPage(sender)
  2565. } else {
  2566. document.mainViewController?.menuItemAction_showNextTagPage(sender)
  2567. }
  2568. }
  2569. }
  2570. func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  2571. if (self.canResponseDocumentAction() == false) {
  2572. return
  2573. }
  2574. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2575. if (document.isHome) {
  2576. document.homeViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2577. } else {
  2578. document.mainViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2579. }
  2580. }
  2581. }
  2582. func menuItemAction_mergeAllWindow(_ sender: Any) {
  2583. if (self.canResponseDocumentAction() == false) {
  2584. return
  2585. }
  2586. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2587. if (document.isHome) {
  2588. document.homeViewController?.menuItemAction_mergeAllWindow(sender)
  2589. } else {
  2590. document.mainViewController?.menuItemAction_mergeAllWindow(sender)
  2591. }
  2592. }
  2593. }
  2594. }
  2595. // MARK: - Analytics (埋点)
  2596. extension KMBrowserWindowController {
  2597. func trackEvent_upgrade() -> Void {
  2598. KMAnalytics.trackEvent(eventName: "Btn_Tbr_Upgrade", parameters: [
  2599. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.tbr,
  2600. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.tbr_Btn], platform: .AppCenter, appTarget: .all)
  2601. }
  2602. }