KMBrowserWindowController.swift 108 KB

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