KMBrowserWindowController.swift 108 KB

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