KMBrowserWindowController.swift 108 KB

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