KMBrowserWindowController.swift 115 KB

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