KMBrowserWindowController.swift 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132
  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. if let data = self.window?.appearance?.name, data == .darkAqua {
  144. self.window?.appearance = .init(named: .aqua)
  145. } else {
  146. self.window?.appearance = .init(named: .darkAqua)
  147. }
  148. self.window?.contentView?.appearance = self.window?.appearance ?? .init(named: .aqua)
  149. self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  150. }
  151. override func updateViewColor() {
  152. super.updateViewColor()
  153. window?.backgroundColor = KMTabAppearance.tabsViewBackgroundColor()
  154. }
  155. // MARK: Public Methods
  156. func refreshToolBar(homeToolState toolState: KMHomeToolState) -> Void {
  157. modeType = toolState
  158. let document = browser.activeTabContents()
  159. if document != nil {
  160. if document!.isHome {
  161. homeRightTabStripView_.isHidden = modeType == .Home
  162. } else {
  163. self.homeRightStripView.isHidden = true
  164. }
  165. } else {
  166. self.homeRightStripView.isHidden = true
  167. }
  168. homeRightTabStripView_.homeRefreshButton.isEnabled = true
  169. homeRightTabStripView_.homeUploadButton.isEnabled = true
  170. filterType = 0
  171. rightTabStripView_.sortPopUpButton.removeAllItems()
  172. rightTabStripView_.filterPopUpButton.removeAllItems()
  173. rightTabStripView_.updateView()
  174. }
  175. override var hasToolbar: Bool {
  176. get {
  177. return false
  178. }
  179. }
  180. func showSnapshots(setups: NSArray?) {
  181. let doc = self.document as? KMMainDocument
  182. doc?.mainViewController?.showSnapshots(setups: setups)
  183. }
  184. var km_browser: KMBrowser? {
  185. get {
  186. return self.browser as? KMBrowser
  187. }
  188. }
  189. // MARK: Private Methods
  190. func openDocumentWindow() -> Void {
  191. // let panel = NSOpenPanel()
  192. // panel.allowsMultipleSelection = false
  193. // panel.allowedFileTypes = ["pdf"]
  194. // panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  195. // if response == .OK {
  196. // let openPath = panel.url?.path
  197. // let selectDocument = self.selectTabContents(path: openPath!)
  198. // if selectDocument != nil {
  199. // let currentIndex = selectDocument!.browser.tabStripModel.index(of: selectDocument)
  200. // self.browser.tabStripModel.selectTabContents(at: Int32(currentIndex), userGesture: true)
  201. // return
  202. // }
  203. // if !panel.url!.path.isPDFValid() {
  204. // let alert = NSAlert()
  205. // alert.alertStyle = .critical
  206. // alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  207. // alert.runModal()
  208. // return
  209. // }
  210. // NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { pdfDocument, documentWasAlreadyOpen, error in
  211. // print("openDocumentWindow")
  212. // }
  213. // }
  214. // }
  215. // MARK TODO: + 号开启
  216. // self.browser.addHomeTabContents()
  217. self.tabStripView?.addTabButton?.layer?.backgroundColor = .clear
  218. self.km_browser?.addNewTabContents()
  219. }
  220. // override func openDocument(_ sender: Any!) {
  221. // self.openDocumentWindow()
  222. // }
  223. func selectTabContents(path filePath: String) -> KMMainDocument? {
  224. let document = NSDocumentController.shared.document(for: URL(fileURLWithPath: filePath))
  225. var selectDocument: KMMainDocument?
  226. if document != nil {
  227. if document!.isKind(of: KMMainDocument.self) {
  228. selectDocument = document as! KMMainDocument
  229. }
  230. }
  231. return selectDocument
  232. }
  233. // MARK: Getter
  234. override var rightStripView: NSView! {
  235. get {
  236. if self.rightToolbarItemView == nil {
  237. self.rightToolbarItemView = NSView.init(frame: CGRectMake(0, 10, 184, 40))
  238. self.rightToolbarItemView.wantsLayer = true
  239. if self.rightMessageVC == nil {
  240. self.rightMessageVC = KMVerificationMessageViewController.init()
  241. }
  242. self.rightMessageVC.frameUpdateHandle = { [weak self] rect in
  243. var superRect = self?.rightToolbarItemView?.frame
  244. superRect?.size.width = (self?.rightMessageVC.view.frame.size.width)! + 8
  245. self?.rightToolbarItemView.frame = superRect!;
  246. }
  247. var rect = self.rightMessageVC.view.frame;
  248. rect.origin.x = CGRectGetWidth(self.rightToolbarItemView.frame) - rect.size.width - 8
  249. rect.origin.y = (CGRectGetHeight(self.rightToolbarItemView.frame) - rect.size.height)/2
  250. self.rightMessageVC.view.frame = rect
  251. self.rightMessageVC.view.autoresizingMask = [ .maxXMargin]
  252. self.rightToolbarItemView.addSubview(self.rightMessageVC.view)
  253. }
  254. return self.rightToolbarItemView
  255. }
  256. }
  257. override var homeRightStripView: NSView! {
  258. get {
  259. return homeRightTabStripView_
  260. }
  261. }
  262. // MARK: Button Action
  263. func commandDispatch(_ sender: Any) -> Void {
  264. var needShowChooseWindow = false
  265. //#if VERSION_FREE
  266. //#if VERSION_DMG
  267. if (!IAPProductsManager.default().isAvailableAllFunction()) {
  268. needShowChooseWindow = true
  269. }
  270. //#endif
  271. if needShowChooseWindow {
  272. let preferenceNoteShow = UserDefaults.standard.bool(forKey: KMTabbingHintShowFlag)
  273. if preferenceNoteShow {
  274. menuItemAction_newTagPageToNewWindow("")
  275. } else {
  276. if !KMDataManager.default.isTabbingWin{
  277. KMDataManager.default.isTabbingWin = true
  278. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  279. tabbingWin.selectCallBack = {[weak self] continueOrNot in
  280. KMDataManager.default.isTabbingWin = false
  281. if continueOrNot {
  282. self?.reopenDocument(forPaths: nil)
  283. } else {
  284. }
  285. }
  286. self.km_beginSheet(windowC: tabbingWin)
  287. }
  288. }
  289. } else {
  290. openDocumentWindow()
  291. }
  292. }
  293. func reopenDocument(forPaths path: String?) -> Void {
  294. if path == nil {
  295. let browser = KMBrowser.init() as KMBrowser
  296. browser.windowController = KMBrowserWindowController.init(browser: browser)
  297. browser.addHomeTabContents()
  298. browser.windowController.showWindow(self)
  299. }else {
  300. NSDocumentController.shared.km_safe_openDocument(withContentsOf: URL(fileURLWithPath: path!), display: true) { doc, open, err in
  301. }
  302. }
  303. }
  304. func closeAllTabs(_ sender: Any) -> Void {
  305. if self.browser != nil {
  306. self.browser.closeAllTabs()
  307. }
  308. }
  309. func openNewWindow(_ sender: Any) -> Void {
  310. if let noti = sender as? NSNotification, noti.object is CTTabController {
  311. if let window_ = (noti.object as! CTTabController).view.window, window_.isEqual(to: self.window) == false {
  312. return
  313. }
  314. }
  315. if self.browser != nil {
  316. let activeInt = self.browser.activeTabIndex()
  317. let activeTab = self.browser.activeTabContents()
  318. self.browser.closeTab(at: activeInt, makeHistory: false)
  319. let browser: KMBrowser = KMBrowser.init()
  320. browser.windowController = KMBrowserWindowController.init(browser: browser)
  321. browser.addHomeTabContents()
  322. browser.windowController.showWindow(self)
  323. // browser.add(activeTab, inForeground: false)
  324. // browser.selectTab(at: 1)
  325. if activeTab?.fileURL == nil {
  326. return
  327. }
  328. let pdfDoc = CPDFDocument.init(url: (activeTab?.fileURL)!)
  329. let document = NSDocumentController.shared.document(for: (activeTab?.fileURL)!)
  330. KMMainDocument().tryToUnlockDocument(pdfDoc!)
  331. if ((pdfDoc?.isLocked)! == true) {
  332. KMPasswordInputWindow.openWindow(window: self.window!, url: (activeTab?.fileURL)!) { result, password in
  333. if result == .cancel { /// 关闭
  334. return
  335. }
  336. /// 解密成功
  337. var selectDocument: KMMainDocument? = nil
  338. if ((document?.isKind(of: KMMainDocument.self)) != nil) {
  339. selectDocument = (document as! KMMainDocument)
  340. }
  341. if selectDocument != nil {
  342. let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument)
  343. selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true)
  344. if (selectDocument?.browser.window.isVisible)! as Bool {
  345. selectDocument?.browser.window.orderFront(nil)
  346. } else if (selectDocument?.browser.window.isMiniaturized)! as Bool {
  347. selectDocument?.browser.window.orderFront(nil)
  348. }
  349. } else {
  350. NSDocumentController.shared.km_safe_openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { document, _, error in
  351. if (error == nil) {
  352. (document as! KMMainDocument).mainViewController?.password = password
  353. }
  354. }
  355. }
  356. }
  357. } else {
  358. var selectDocument: KMMainDocument? = nil
  359. if ((document?.isKind(of: KMMainDocument.self)) != nil) {
  360. selectDocument = (document as! KMMainDocument)
  361. }
  362. if selectDocument != nil {
  363. let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument)
  364. selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true)
  365. if (selectDocument?.browser.window.isVisible)! as Bool {
  366. selectDocument?.browser.window.orderFront(nil)
  367. } else if (selectDocument?.browser.window.isMiniaturized)! as Bool {
  368. selectDocument?.browser.window.orderFront(nil)
  369. }
  370. } else {
  371. NSDocumentController.shared.km_safe_openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { _, _, _ in
  372. }
  373. }
  374. }
  375. }
  376. }
  377. func canMergeAllWindow() -> Bool {
  378. return self.fetchBrowserWindowControllers().count > 0
  379. }
  380. fileprivate func fetchBrowserWindowControllers() -> [KMBrowserWindowController] {
  381. var windowArray: [KMBrowserWindowController] = []
  382. for window in NSApp.windows {
  383. guard let windowController = window.windowController, windowController is KMBrowserWindowController else {
  384. continue
  385. }
  386. if let _tabScripC = (windowController as? KMBrowserWindowController)?.tabStripController, _tabScripC.viewsCount() <= 1 {
  387. continue
  388. }
  389. if (window.isVisible && (windowController.isEqual(to: self) == false)) {
  390. windowArray.append(windowController as! KMBrowserWindowController)
  391. }
  392. }
  393. return windowArray
  394. }
  395. func mergeAllWindow(_ sender: Any) {
  396. let windowArray = self.fetchBrowserWindowControllers()
  397. if (windowArray.count <= 0) {
  398. return
  399. }
  400. for windowC in windowArray {
  401. // 拼接窗口 [拼接窗口里的标签,除了home标签]
  402. self.append(windowC, toTabView: NSView())
  403. if let browser = windowC.browser {
  404. if (browser.tabCount() <= 1 && browser.activeTabContents().isHome) {
  405. windowC.close()
  406. }
  407. }
  408. }
  409. }
  410. override func tabDidSelect(_ notification: Notification!) {
  411. super.tabDidSelect(notification)
  412. if let _userInfo = notification.userInfo, let _newContents = _userInfo[CTTabNewContentsUserInfoKey] as? KMMainDocument {
  413. _newContents.addWindowController(self)
  414. }
  415. }
  416. override func layoutTabContentArea(_ frame: NSRect) {
  417. super.layoutTabContentArea(frame)
  418. self.rightStripView.autoresizingMask = [.minXMargin, .minYMargin]
  419. }
  420. override func openDocument(_ sender: Any!) {
  421. guard let _window = self.window else {
  422. KMPrint("openDocument error: window is nil.", beep: true)
  423. return
  424. }
  425. if self.browser.tabCount() > 1 && !IAPProductsManager.default().isAvailableAllFunction() {
  426. if !KMDataManager.default.isTabbingWin{
  427. KMDataManager.default.isTabbingWin = true
  428. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  429. tabbingWin.selectCallBack = { [weak self] continueOrNot in
  430. KMDataManager.default.isTabbingWin = false
  431. if continueOrNot {
  432. self?.reopenDocument(forPaths: nil)
  433. } else {
  434. }
  435. }
  436. self.km_beginSheet(windowC: tabbingWin)
  437. }
  438. return
  439. }
  440. NSOpenPanel.km_open_multi_success(_window) { panel in
  441. panel.allowedFileTypes = KMTools.imageExtensions + KMTools.pdfExtensions
  442. } completion: { urls in
  443. var imageUrls: [URL] = []
  444. for url in urls {
  445. if KMTools.isPDFType(url.pathExtension.lowercased()) {
  446. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url, display: true) { _, _, _ in
  447. }
  448. } else {
  449. imageUrls.append(url)
  450. }
  451. }
  452. if imageUrls.isEmpty == false {
  453. NSApplication.ShowImageToPDFWindow(urls: imageUrls)
  454. }
  455. }
  456. }
  457. }
  458. // MARK: - KMInterfaceThemeChangedProtocol
  459. extension KMBrowserWindowController: KMInterfaceThemeChangedProtocol {
  460. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  461. Task { @MainActor in
  462. self.updateViewColor()
  463. let selector = #selector(interfaceThemeDidChanged)
  464. let mainWindow = self.window
  465. var responder = mainWindow?.firstResponder
  466. while (responder != nil) {
  467. // KMPrint("res: \(responder)")
  468. //
  469. if self.isEqual(to: responder) {
  470. responder = responder?.nextResponder
  471. continue
  472. }
  473. if let res = responder?.responds(to: selector), res {
  474. responder?.perform(selector, with: appearance)
  475. }
  476. responder = responder!.nextResponder
  477. }
  478. }
  479. }
  480. }
  481. // MARK: - KMToolbarRightViewDelegate
  482. extension KMBrowserWindowController: KMToolbarRightViewDelegate {
  483. func pdfRightSegmentedControlAction(_ sender: KMSegmentedBox?) {
  484. self.trackEvent_upgrade()
  485. // KMComparativeTableViewController.show(window: self.window!, .upgrade)
  486. DispatchQueue.main.async {
  487. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  488. }
  489. }
  490. func userInfoButtonAction(_ sender: NSButton) {
  491. if KMLightMemberManager.manager.isLogin() {
  492. Task { @MainActor in
  493. if await KMLightMemberManager.manager.canUseAdvanced(needNetworking: true) {
  494. KMAccountInfoWindowController.show(window: self.window!)
  495. } else {
  496. KMLoginWindowController.show(window: self.window!)
  497. }
  498. }
  499. } else {
  500. KMLoginWindowController.show(window: self.window!)
  501. }
  502. }
  503. func homeRefreshButtonAction(_ sender: NSButton?) {
  504. self.layoutSubviews()
  505. }
  506. func homeUploadButtonAction(_ sender: NSButton) {
  507. }
  508. func homeMenuSortAction(_ sender: NSPopUpButton) {
  509. }
  510. func homeMenuFilterAction(_ sender: NSPopUpButton) {
  511. }
  512. }
  513. // MARK: - KMUploadFileDelegate
  514. extension KMBrowserWindowController: KMUploadFileDelegate {
  515. // override func cancelOperation(_ sender: Any?) {
  516. //
  517. // }
  518. }
  519. // MARK: - NSSearchFieldDelegate
  520. extension KMBrowserWindowController: NSSearchFieldDelegate {
  521. func controlTextDidChange(_ obj: Notification) {
  522. }
  523. func controlTextDidEndEditing(_ obj: Notification) {
  524. }
  525. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  526. var result = false
  527. return result
  528. }
  529. }
  530. // MARK: -
  531. // MARK: Menu Actions
  532. extension KMBrowserWindowController {
  533. func canResponseDocumentAction() -> Bool {
  534. if (self.browser == nil) {
  535. return false
  536. }
  537. guard let _ = self.browser.activeTabContents() as? KMMainDocument else {
  538. return false
  539. }
  540. return true
  541. }
  542. }
  543. // MARK: -
  544. // MARK: File Menu
  545. extension KMBrowserWindowController: NSMenuDelegate, NSMenuItemValidation {
  546. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  547. if (self.browser == nil) {
  548. return false
  549. }
  550. guard let action = menuItem.action else {
  551. return false
  552. }
  553. guard let document = self.browser.activeTabContents() as? KMMainDocument else {
  554. return false
  555. }
  556. if (KMSystemMenu.isFileSelector(sel: action)) {
  557. if action == KMSystemMenu.File.screenShotSelectAreaSelector ||
  558. action == KMSystemMenu.File.screenShotWindowSelector ||
  559. action == KMSystemMenu.File.screenShotFullScreenSelector ||
  560. action == KMSystemMenu.File.screenShotFullScreenDelaySelector {
  561. return true
  562. }
  563. if (document.isHome) {
  564. if (menuItem.action == KMSystemMenu.File.closeTagPageSelector ||
  565. menuItem.action == KMSystemMenu.File.propertySelector ||
  566. menuItem.action == KMSystemMenu.File.showInFinderSelector ||
  567. menuItem.action == KMSystemMenu.File.printSelector ||
  568. menuItem.action == KMSystemMenu.File.saveAsFlattenedPDFSelector) {
  569. return false
  570. }
  571. return document.homeViewController!.validateMenuItem(menuItem)
  572. } else {
  573. return document.mainViewController!.validateMenuItem(menuItem)
  574. }
  575. }
  576. if (KMSystemMenu.isEditSelector(sel: action)) {
  577. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  578. }
  579. if (KMSystemMenu.isViewSelector(sel: action)) {
  580. if action == KMSystemMenu.Edit.changeFontSelector {
  581. return true
  582. } else {
  583. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  584. }
  585. }
  586. if (KMSystemMenu.isAnnotationSelector(sel: action)) {
  587. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  588. }
  589. if (KMSystemMenu.isPDFSelector(sel: action)) {
  590. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  591. }
  592. if (KMSystemMenu.isGotoSelector(sel: action)) {
  593. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  594. }
  595. if (KMSystemMenu.isToolSelector(sel: action)) {
  596. if action == KMSystemMenu.Tool.orderFrontNotesPanelSelector {
  597. if KMNotesPanelController.sharedControllerExists() {
  598. if let data = KMNotesPanelController.shared.window?.isVisible, data {
  599. menuItem.title = NSLocalizedString("Hide Note Type", comment: "Menu item title")
  600. } else {
  601. menuItem.title = NSLocalizedString("Show Note Type", comment: "Menu item title")
  602. }
  603. } else {
  604. menuItem.title = NSLocalizedString("Show Note Type", comment: "Menu item title")
  605. }
  606. return true
  607. } else {
  608. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  609. }
  610. }
  611. if (KMSystemMenu.isWindowSelector(sel: action)) {
  612. return document.isHome ? document.homeViewController!.validateMenuItem(menuItem) : document.mainViewController!.validateMenuItem(menuItem)
  613. }
  614. if action == #selector(redactAction) {
  615. return document.isHome ? false : true
  616. }
  617. return true
  618. }
  619. }
  620. extension KMBrowserWindowController {
  621. @IBAction func menuItemAction_openFile(_ sender: Any) {
  622. super.openDocument(sender)
  623. }
  624. @IBAction func importFromFile(_ sender: Any) {
  625. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  626. if (document.isHome) {
  627. document.homeViewController?.importFromFile(sender)
  628. } else {
  629. self.homeVC = KMHomeViewController()
  630. self.homeVC?.importFromFile(sender)
  631. }
  632. }
  633. @IBAction func openBlankPage(_ sender: Any) {
  634. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  635. if (document.isHome) {
  636. document.homeViewController?.openBlankPage()
  637. } else {
  638. self.homeVC = KMHomeViewController()
  639. self.homeVC?.openBlankPage()
  640. }
  641. }
  642. @IBAction func importFromScanner(_ sender: Any) {
  643. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  644. if (document.isHome) {
  645. document.homeViewController?.importFromScanner()
  646. } else {
  647. self.homeVC = KMHomeViewController()
  648. self.homeVC?.importFromScanner()
  649. }
  650. }
  651. @IBAction func menuItemAction_newTab(_ sender: Any) {
  652. self.openDocumentWindow()
  653. }
  654. @IBAction func pasteAsPlainText(_ sender: Any?) {
  655. KMPrint("pasteAsPlainText")
  656. }
  657. @IBAction func deselectAll(_ sender: Any?) {
  658. KMPrint("deselectAll")
  659. }
  660. @IBAction func searchPDF(_ sender: Any?) {
  661. if let document = self.browser.activeTabContents() as? KMMainDocument {
  662. if (document.isHome) {
  663. } else {
  664. document.mainViewController?.leftSideViewController.showSearchMode("")
  665. }
  666. }
  667. }
  668. @IBAction func changeColor(_ sender: Any?) {
  669. KMPrint("changeColor ...")
  670. }
  671. @IBAction func changeAttributes(_ sender: Any?) {
  672. KMPrint("changeAttributes ...")
  673. }
  674. @IBAction func alignLeft(_ sender: Any?) {
  675. KMPrint("alignLeft ...")
  676. }
  677. @IBAction func alignRight(_ sender: Any?) {
  678. KMPrint("alignRight ...")
  679. }
  680. @IBAction func alignCenter(_ sender: Any?) {
  681. KMPrint("alignCenter ...")
  682. }
  683. @IBAction func createNewNote(_ sender: Any?) {
  684. KMPrint("createNewNote ...")
  685. }
  686. @IBAction func toggleHideNotes(_ sender: Any?) {
  687. KMPrint("toggleHideNotes ...")
  688. }
  689. @IBAction func changeDisplaySinglePages(_ sender: Any?) {
  690. KMPrint("changeDisplaySinglePages ...")
  691. }
  692. @IBAction func changeDisplayContinuous(_ sender: Any?) {
  693. KMPrint("changeDisplayContinuous ...")
  694. }
  695. @IBAction func doGoToNextPage(_ sender: Any?) {
  696. KMPrint("doGoToNextPage ...")
  697. }
  698. @IBAction func doGoToPreviousPage(_ sender: Any?) {
  699. KMPrint("doGoToPreviousPage ...")
  700. }
  701. @IBAction func doGoToFirstPage(_ sender: Any?) {
  702. KMPrint("doGoToFirstPage ...")
  703. }
  704. @IBAction func doGoToLastPage(_ sender: Any?) {
  705. KMPrint("doGoToLastPage ...")
  706. }
  707. @IBAction func allGoToNextPage(_ sender: Any?) {
  708. KMPrint("allGoToNextPage ...")
  709. }
  710. @IBAction func allGoToPreviousPage(_ sender: Any?) {
  711. KMPrint("allGoToPreviousPage ...")
  712. }
  713. @IBAction func allGoToFirstPage(_ sender: Any?) {
  714. KMPrint("allGoToFirstPage ...")
  715. }
  716. @IBAction func allGoToLastPage(_ sender: Any?) {
  717. KMPrint("allGoToLastPage ...")
  718. }
  719. @IBAction func doGoToPage(_ sender: Any?) {
  720. KMPrint("doGoToPage ...")
  721. }
  722. @IBAction func doGoBack(_ sender: Any?) {
  723. KMPrint("doGoBack ...")
  724. }
  725. @IBAction func doGoForward(_ sender: Any?) {
  726. KMPrint("doGoForward ...")
  727. }
  728. @IBAction func doZoomToPhysicalSize(_ sender: Any?) {
  729. KMPrint("doZoomToPhysicalSize ...")
  730. }
  731. @IBAction func alternateZoomToFit(_ sender: Any?) {
  732. KMPrint("alternateZoomToFit ...")
  733. }
  734. @IBAction func doZoomToSelection(_ sender: Any?) {
  735. KMPrint("doZoomToSelection ...")
  736. }
  737. @IBAction func doAutoScale(_ sender: Any?) {
  738. KMPrint("doAutoScale ...")
  739. }
  740. // @IBAction func delete(_ sender: Any?) {
  741. // KMPrint("delete ...")
  742. // }
  743. // @IBAction func paste(_ sender: Any?) {
  744. // KMPrint("paste ...")
  745. // }
  746. @IBAction func alternatePaste(_ sender: Any?) {
  747. KMPrint("alternatePaste ...")
  748. }
  749. @IBAction func copy(_ sender: Any?) {
  750. KMPrint("copy ...")
  751. }
  752. @IBAction func cut(_ sender: Any?) {
  753. KMPrint("cut ...")
  754. }
  755. @IBAction func toggleFullscreen(_ sender: Any?) {
  756. KMPrint("toggleFullscreen ...")
  757. }
  758. @IBAction func toggleRightSidePane(_ sender: Any?) {
  759. KMPrint("toggleRightSidePane ...")
  760. }
  761. @IBAction func changeRightSidePaneState(_ sender: Any?) {
  762. KMPrint("changeRightSidePaneState ...")
  763. }
  764. @IBAction func changeFindPaneState(_ sender: Any?) {
  765. KMPrint("changeFindPaneState ...")
  766. }
  767. @IBAction func toggleSplitPDF(_ sender: Any?) {
  768. KMPrint("toggleSplitPDF ...")
  769. }
  770. @IBAction func search(_ sender: Any?) {
  771. KMPrint("search ...")
  772. }
  773. @IBAction func searchNotes(_ sender: Any?) {
  774. KMPrint("searchNotes ...")
  775. }
  776. @IBAction func password(_ sender: Any?) {
  777. KMPrint("password ...")
  778. }
  779. @IBAction func toggleCaseInsensitiveSearch(_ sender: Any?) {
  780. KMPrint("toggleCaseInsensitiveSearch ...")
  781. }
  782. @IBAction func toggleWholeWordSearch(_ sender: Any?) {
  783. KMPrint("toggleWholeWordSearch ...")
  784. }
  785. @IBAction func toggleCaseInsensitiveNoteSearch(_ sender: Any?) {
  786. KMPrint("toggleCaseInsensitiveNoteSearch ...")
  787. }
  788. @IBAction func performFindPanelAction(_ sender: NSMenuItem) {
  789. KMPrint("performFindPanelAction ...")
  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 savePathOld = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1245. var savePath = self.filePathCheck(path: savePathOld)
  1246. let newDocument = CPDFDocument()
  1247. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1248. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1249. if writeSuccess ?? false {
  1250. if self.checkOpenNewDocument(path: savePath) {
  1251. self.savePdf(savePath)
  1252. }
  1253. // try? FileManager.default.removeItem(atPath: savePath)
  1254. } else {
  1255. }
  1256. }
  1257. }
  1258. }
  1259. }
  1260. func checkOpenNewDocument(path: String) -> Bool {
  1261. if !IAPProductsManager.default().isAvailableAllFunction() && self.browser.tabCount() > 1 {
  1262. let preferenceNoteShow = UserDefaults.standard.bool(forKey: KMTabbingHintShowFlag)
  1263. if preferenceNoteShow {
  1264. menuItemAction_newTagPageToNewWindow("")
  1265. } else {
  1266. if !KMDataManager.default.isTabbingWin{
  1267. KMDataManager.default.isTabbingWin = true
  1268. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  1269. tabbingWin.selectCallBack = {[weak self] continueOrNot in
  1270. KMDataManager.default.isTabbingWin = false
  1271. if continueOrNot {
  1272. self?.reopenDocumentForNewWindow(savePath: path)
  1273. } else {
  1274. }
  1275. }
  1276. self.km_beginSheet(windowC: tabbingWin)
  1277. }
  1278. }
  1279. return false
  1280. }
  1281. return true
  1282. }
  1283. func reopenDocumentForNewWindow(savePath: String) {
  1284. let browser = KMBrowser.init() as KMBrowser
  1285. browser.windowController = KMBrowserWindowController.init(browser: browser)
  1286. browser.addHomeTabContents()
  1287. browser.windowController.showWindow(self)
  1288. self.savePdf(savePath)
  1289. }
  1290. func filePathCheck(path: String) -> String {
  1291. var i: Int = 0
  1292. let fileURL = URL(fileURLWithPath: path)
  1293. var newPath: String = path
  1294. let fileManager = FileManager.default
  1295. while fileManager.fileExists(atPath: newPath) {
  1296. i += 1
  1297. newPath = fileURL.deletingPathExtension().path
  1298. newPath.append("(\(i))")
  1299. newPath.append(".")
  1300. newPath.append(fileURL.pathExtension)
  1301. }
  1302. return newPath
  1303. }
  1304. @IBAction func screenShot_Window(_ sender: Any?) {
  1305. KMPrint("screenShot_Window")
  1306. KMScreenShotHandler.beginScreenShot_SelectWindowCompleteHandler { ima in
  1307. if let image = ima {
  1308. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1309. let savePathOld = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1310. var savePath = self.filePathCheck(path: savePathOld)
  1311. let newDocument = CPDFDocument()
  1312. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1313. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1314. if writeSuccess == true {
  1315. if self.checkOpenNewDocument(path: savePath) {
  1316. self.savePdf(savePath)
  1317. }
  1318. // try? FileManager.default.removeItem(atPath: savePath)
  1319. } else {
  1320. }
  1321. }
  1322. }
  1323. }
  1324. }
  1325. @IBAction func screenShot_FullScreenDelay(_ sender: Any?) {
  1326. KMPrint("screenShot_FullScreenDelay")
  1327. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(3) { ima in
  1328. if let image = ima {
  1329. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1330. let savePathOld = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1331. var savePath = self.filePathCheck(path: savePathOld)
  1332. let newDocument = CPDFDocument()
  1333. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1334. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1335. if writeSuccess == true {
  1336. if self.checkOpenNewDocument(path: savePath) {
  1337. self.savePdf(savePath)
  1338. }
  1339. // try? FileManager.default.removeItem(atPath: savePath)
  1340. } else {
  1341. }
  1342. }
  1343. }
  1344. }
  1345. }
  1346. @IBAction func screenShot_FullScreen(_ sender: Any?) {
  1347. KMPrint("screenShot_FullScreen")
  1348. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(0) { ima in
  1349. if let image = ima {
  1350. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1351. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1352. let newDocument = CPDFDocument()
  1353. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1354. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1355. if writeSuccess == true {
  1356. self.savePdf(savePath)
  1357. // try? FileManager.default.removeItem(atPath: savePath)
  1358. } else {
  1359. }
  1360. }
  1361. }
  1362. }
  1363. }
  1364. }
  1365. // MARK: - KMSystemViewMenuProtocol
  1366. extension KMBrowserWindowController: KMSystemViewMenuProtocol {
  1367. func createPDFFromImage() {
  1368. let openPanel = NSOpenPanel()
  1369. openPanel.allowedFileTypes = KMImageAccessoryController.supportedImageTypes()
  1370. openPanel.allowsMultipleSelection = true
  1371. 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: "")
  1372. openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1373. if response == .OK {
  1374. self.creatPdfFromImg(urls: openPanel.urls)
  1375. }
  1376. }
  1377. }
  1378. func creatPdfFromImg(urls: Array<URL>) {
  1379. // let savePath = self.newDocumentTempSavePath("convertToPDF.pdf")
  1380. let pdf = CPDFDocument()
  1381. // var isSucceed = true
  1382. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1383. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1384. let filePath = savePath.deletingLastPathComponent
  1385. if FileManager.default.fileExists(atPath: filePath) == false {
  1386. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1387. }
  1388. for fileURL in urls {
  1389. if let imag = NSImage(contentsOfFile: fileURL.path ) {
  1390. _ = pdf?.km_insertPage(imag.size, withImage: fileURL.path , at: pdf?.pageCount ?? 0)
  1391. pdf?.write(to: URL(fileURLWithPath: savePath))
  1392. }
  1393. }
  1394. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1395. if error != nil {
  1396. NSApp.presentError(error!)
  1397. } else {
  1398. if document is KMMainDocument {
  1399. let newDocument = document
  1400. (newDocument as! KMMainDocument).isNewCreated = true
  1401. }
  1402. }
  1403. // self.homeContentView?.reloadData()
  1404. }
  1405. }
  1406. func createPdfWithParam() {
  1407. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1408. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1409. let filePath = savePath.deletingLastPathComponent
  1410. if FileManager.default.fileExists(atPath: filePath) == false {
  1411. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1412. }
  1413. let pdfDocument = CPDFDocument()
  1414. pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
  1415. pdfDocument?.write(to: URL(fileURLWithPath: savePath))
  1416. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1417. if error != nil {
  1418. NSApp.presentError(error!)
  1419. } else {
  1420. if document is KMMainDocument {
  1421. let newDocument = document
  1422. (newDocument as! KMMainDocument).isNewCreated = true
  1423. }
  1424. }
  1425. // self.homeContentView?.reloadData()
  1426. }
  1427. }
  1428. func savePdf(_ filePath: String) -> Void {
  1429. let docVc = KMDocumentController.shared
  1430. docVc.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
  1431. if error != nil {
  1432. NSApp.presentError(error!)
  1433. } else {
  1434. if document is KMMainDocument {
  1435. let newDocument = document
  1436. (newDocument as! KMMainDocument).isNewCreated = true
  1437. }
  1438. }
  1439. }
  1440. }
  1441. func newDocumentTempSavePath(_ fileName: String) -> String {
  1442. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  1443. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  1444. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  1445. return append2
  1446. }
  1447. func fetchUniquePath(_ originalPath: String) -> String {
  1448. var path = originalPath
  1449. let dManager = FileManager.default
  1450. if !dManager.fileExists(atPath: path) {
  1451. if path.extension.count < 1 {
  1452. path = path.stringByAppendingPathExtension("pdf")
  1453. }
  1454. return path
  1455. } else {
  1456. let originalFullFileName = path.lastPathComponent
  1457. let originalFileName = path.lastPathComponent.deletingPathExtension
  1458. let originalExtension = path.extension
  1459. let startIndex: Int = 0
  1460. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  1461. let fileLocatePath = originalPath.substring(to: endIndex)
  1462. var i = 1
  1463. while (1 != 0) {
  1464. var newName = String(format: "%@%ld", originalFileName, i)
  1465. newName = newName.stringByAppendingPathExtension(originalExtension)
  1466. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  1467. if !dManager.fileExists(atPath: newPath) {
  1468. return newPath
  1469. } else {
  1470. i+=1
  1471. continue
  1472. }
  1473. }
  1474. }
  1475. }
  1476. func menuItemAction_adjustWidth(_ sender: Any) {
  1477. if (self.canResponseDocumentAction() == false) {
  1478. return
  1479. }
  1480. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1481. document.mainViewController?.menuItemAction_adjustWidth(sender)
  1482. }
  1483. }
  1484. func menuItemAction_adjustPage(_ sender: Any) {
  1485. if (self.canResponseDocumentAction() == false) {
  1486. return
  1487. }
  1488. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1489. document.mainViewController?.menuItemAction_adjustPage(sender)
  1490. }
  1491. }
  1492. func menuItemAction_size(_ sender: Any) {
  1493. if (self.canResponseDocumentAction() == false) {
  1494. return
  1495. }
  1496. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1497. document.mainViewController?.menuItemAction_size(sender)
  1498. }
  1499. }
  1500. func menuItemAction_zoomOut(_ sender: Any) {
  1501. if (self.canResponseDocumentAction() == false) {
  1502. return
  1503. }
  1504. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1505. document.mainViewController?.menuItemAction_zoomOut(sender)
  1506. }
  1507. }
  1508. func menuItemAction_zoomIn(_ sender: Any) {
  1509. if (self.canResponseDocumentAction() == false) {
  1510. return
  1511. }
  1512. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1513. document.mainViewController?.menuItemAction_zoomIn(sender)
  1514. }
  1515. }
  1516. func menuItemAction_singlePage(_ sender: Any) {
  1517. if (self.canResponseDocumentAction() == false) {
  1518. return
  1519. }
  1520. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1521. document.mainViewController?.menuItemAction_singlePage(sender)
  1522. }
  1523. }
  1524. func menuItemAction_singlePageContinue(_ sender: Any) {
  1525. if (self.canResponseDocumentAction() == false) {
  1526. return
  1527. }
  1528. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1529. document.mainViewController?.menuItemAction_singlePageContinue(sender)
  1530. }
  1531. }
  1532. func menuItemAction_doublePage(_ sender: Any) {
  1533. if (self.canResponseDocumentAction() == false) {
  1534. return
  1535. }
  1536. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1537. document.mainViewController?.menuItemAction_doublePage(sender)
  1538. }
  1539. }
  1540. func menuItemAction_doublePageContinue(_ sender: Any) {
  1541. if (self.canResponseDocumentAction() == false) {
  1542. return
  1543. }
  1544. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1545. document.mainViewController?.menuItemAction_doublePageContinue(sender)
  1546. }
  1547. }
  1548. func menuItemAction_bookMode(_ sender: Any) {
  1549. if (self.canResponseDocumentAction() == false) {
  1550. return
  1551. }
  1552. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1553. document.mainViewController?.menuItemAction_bookMode(sender)
  1554. }
  1555. }
  1556. func menuItemAction_readMode(_ sender: Any) {
  1557. if (self.canResponseDocumentAction() == false) {
  1558. return
  1559. }
  1560. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1561. document.mainViewController?.menuItemAction_readMode(sender)
  1562. }
  1563. }
  1564. func menuItemAction_showSplitPage(_ sender: Any) {
  1565. if (self.canResponseDocumentAction() == false) {
  1566. return
  1567. }
  1568. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1569. document.mainViewController?.menuItemAction_showSplitPage(sender)
  1570. }
  1571. }
  1572. func menuItemAction_rotateLeft(_ sender: Any) {
  1573. if (self.canResponseDocumentAction() == false) {
  1574. return
  1575. }
  1576. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1577. document.mainViewController?.menuItemAction_rotateLeft(sender)
  1578. }
  1579. }
  1580. func menuItemAction_rotateRight(_ sender: Any) {
  1581. if (self.canResponseDocumentAction() == false) {
  1582. return
  1583. }
  1584. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1585. document.mainViewController?.menuItemAction_rotateRight(sender)
  1586. }
  1587. }
  1588. func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  1589. if (self.canResponseDocumentAction() == false) {
  1590. return
  1591. }
  1592. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1593. document.mainViewController?.menuItemAction_rotateAllPageLeft(sender)
  1594. }
  1595. }
  1596. func menuItemAction_rotateAllPageRight(_ sender: Any) {
  1597. if (self.canResponseDocumentAction() == false) {
  1598. return
  1599. }
  1600. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1601. document.mainViewController?.menuItemAction_rotateAllPageRight(sender)
  1602. }
  1603. }
  1604. func menuItemAction_view_readMode(_ sender: Any) {
  1605. if (self.canResponseDocumentAction() == false) {
  1606. return
  1607. }
  1608. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1609. document.mainViewController?.menuItemAction_view_readMode(sender)
  1610. }
  1611. }
  1612. func menuItemAction_hiddenLeftSide(_ sender: Any) {
  1613. if (self.canResponseDocumentAction() == false) {
  1614. return
  1615. }
  1616. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1617. document.mainViewController?.menuItemAction_hiddenLeftSide(sender)
  1618. }
  1619. }
  1620. func menuItemAction_hiddenRightSide(_ sender: Any) {
  1621. if (self.canResponseDocumentAction() == false) {
  1622. return
  1623. }
  1624. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1625. document.mainViewController?.menuItemAction_hiddenRightSide(sender)
  1626. }
  1627. }
  1628. func menuItemAction_thumai(_ sender: Any) {
  1629. if (self.canResponseDocumentAction() == false) {
  1630. return
  1631. }
  1632. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1633. document.mainViewController?.menuItemAction_thumai(sender)
  1634. }
  1635. }
  1636. func menuItemAction_outline(_ sender: Any) {
  1637. if (self.canResponseDocumentAction() == false) {
  1638. return
  1639. }
  1640. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1641. document.mainViewController?.menuItemAction_outline(sender)
  1642. }
  1643. }
  1644. func menuItemAction_bookmark(_ sender: Any) {
  1645. if (self.canResponseDocumentAction() == false) {
  1646. return
  1647. }
  1648. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1649. document.mainViewController?.menuItemAction_bookmark(sender)
  1650. }
  1651. }
  1652. func menuItemAction_annotation(_ sender: Any) {
  1653. if (self.canResponseDocumentAction() == false) {
  1654. return
  1655. }
  1656. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1657. document.mainViewController?.menuItemAction_annotation(sender)
  1658. }
  1659. }
  1660. func menuItemAction_search(_ sender: Any) {
  1661. if (self.canResponseDocumentAction() == false) {
  1662. return
  1663. }
  1664. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1665. document.mainViewController?.menuItemAction_search(sender)
  1666. }
  1667. }
  1668. func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  1669. if (self.canResponseDocumentAction() == false) {
  1670. return
  1671. }
  1672. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1673. document.mainViewController?.menuItemAction_hiddenPageIndicator(sender)
  1674. }
  1675. }
  1676. @IBAction func togglePresentation(_ sender: Any?) {
  1677. if (self.canResponseDocumentAction() == false) {
  1678. return
  1679. }
  1680. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1681. document.mainViewController?.togglePresentation(sender)
  1682. }
  1683. }
  1684. @IBAction func chooseTransition(_ sender: Any?) {
  1685. if (self.canResponseDocumentAction() == false) {
  1686. return
  1687. }
  1688. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1689. document.mainViewController?.chooseTransition(sender)
  1690. }
  1691. }
  1692. @IBAction func toggleAutoFlow(_ sender: Any?) {
  1693. if (self.canResponseDocumentAction() == false) {
  1694. return
  1695. }
  1696. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1697. document.mainViewController?.toggleAutoFlow(sender)
  1698. }
  1699. }
  1700. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  1701. if (self.canResponseDocumentAction() == false) {
  1702. return
  1703. }
  1704. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1705. document.mainViewController?.chooseAutoFlowSetting(sender)
  1706. }
  1707. }
  1708. @IBAction func toggleReadingBar(_ sender: Any?) {
  1709. if (self.canResponseDocumentAction() == false) {
  1710. return
  1711. }
  1712. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1713. document.mainViewController?.toggleReadingBar(sender)
  1714. }
  1715. }
  1716. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  1717. if (self.canResponseDocumentAction() == false) {
  1718. return
  1719. }
  1720. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1721. document.mainViewController?.toggleLeftSidePane(sender)
  1722. }
  1723. }
  1724. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  1725. if (self.canResponseDocumentAction() == false) {
  1726. return
  1727. }
  1728. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1729. document.mainViewController?.changeLeftSidePaneState(sender)
  1730. }
  1731. }
  1732. @IBAction func splitViewAction(_ sender: Any?) {
  1733. if (self.canResponseDocumentAction() == false) {
  1734. return
  1735. }
  1736. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1737. document.mainViewController?.splitViewAction(sender)
  1738. }
  1739. }
  1740. @IBAction func themesColor(_ sender: Any?) {
  1741. if (self.canResponseDocumentAction() == false) {
  1742. return
  1743. }
  1744. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1745. document.mainViewController?.themesColor(sender)
  1746. }
  1747. }
  1748. @IBAction func toggleStatusBar(_ sender: Any?) {
  1749. if (self.canResponseDocumentAction() == false) {
  1750. return
  1751. }
  1752. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1753. document.mainViewController?.toggleStatusBar(sender)
  1754. }
  1755. }
  1756. @IBAction func toggleToolbarShow(_ sender: Any?) {
  1757. if (self.canResponseDocumentAction() == false) {
  1758. return
  1759. }
  1760. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1761. document.mainViewController?.toggleToolbarShow(sender)
  1762. }
  1763. }
  1764. }
  1765. // MARK: - KMSystemEditMenuProtocol
  1766. extension KMBrowserWindowController: KMSystemEditMenuProtocol {
  1767. func menuItemAction_find(_ sender: Any) {
  1768. if (self.canResponseDocumentAction() == false) {
  1769. return
  1770. }
  1771. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1772. document.mainViewController?.menuItemAction_find(sender)
  1773. }
  1774. }
  1775. @IBAction func changeFont(_ sender: Any?) {
  1776. KMPrint("changeFont ...")
  1777. }
  1778. }
  1779. // MARK: - Annotation Menu
  1780. extension KMBrowserWindowController {
  1781. @IBAction func menuItemAction_highlight(_ sender: Any) {
  1782. if (self.canResponseDocumentAction() == false) {
  1783. return
  1784. }
  1785. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1786. document.mainViewController?.menuItemAction_highlight(sender)
  1787. }
  1788. }
  1789. @IBAction func menuItemAction_underline(_ sender: Any) {
  1790. if (self.canResponseDocumentAction() == false) {
  1791. return
  1792. }
  1793. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1794. document.mainViewController?.menuItemAction_underline(sender)
  1795. }
  1796. }
  1797. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  1798. if (self.canResponseDocumentAction() == false) {
  1799. return
  1800. }
  1801. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1802. document.mainViewController?.menuItemAction_deleteLine(sender)
  1803. }
  1804. }
  1805. @IBAction func menuItemAction_freehand(_ sender: Any) {
  1806. if (self.canResponseDocumentAction() == false) {
  1807. return
  1808. }
  1809. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1810. document.mainViewController?.menuItemAction_freehand(sender)
  1811. }
  1812. }
  1813. @IBAction func menuItemAction_text(_ sender: Any) {
  1814. if (self.canResponseDocumentAction() == false) {
  1815. return
  1816. }
  1817. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1818. document.mainViewController?.menuItemAction_text(sender)
  1819. }
  1820. }
  1821. @IBAction func menuItemAction_note(_ sender: Any) {
  1822. if (self.canResponseDocumentAction() == false) {
  1823. return
  1824. }
  1825. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1826. document.mainViewController?.menuItemAction_note(sender)
  1827. }
  1828. }
  1829. @IBAction func menuItemAction_squre(_ sender: Any) {
  1830. if (self.canResponseDocumentAction() == false) {
  1831. return
  1832. }
  1833. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1834. document.mainViewController?.menuItemAction_squre(sender)
  1835. }
  1836. }
  1837. @IBAction func menuItemAction_circle(_ sender: Any) {
  1838. if (self.canResponseDocumentAction() == false) {
  1839. return
  1840. }
  1841. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1842. document.mainViewController?.menuItemAction_circle(sender)
  1843. }
  1844. }
  1845. @IBAction func menuItemAction_arrow(_ sender: Any) {
  1846. if (self.canResponseDocumentAction() == false) {
  1847. return
  1848. }
  1849. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1850. document.mainViewController?.menuItemAction_arrow(sender)
  1851. }
  1852. }
  1853. @IBAction func menuItemAction_line(_ sender: Any) {
  1854. if (self.canResponseDocumentAction() == false) {
  1855. return
  1856. }
  1857. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1858. document.mainViewController?.menuItemAction_line(sender)
  1859. }
  1860. }
  1861. // link
  1862. @IBAction func menuItemAction_link(_ sender: Any) {
  1863. if (self.canResponseDocumentAction() == false) {
  1864. return
  1865. }
  1866. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1867. document.mainViewController?.menuItemAction_link(sender)
  1868. }
  1869. }
  1870. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  1871. if (self.canResponseDocumentAction() == false) {
  1872. return
  1873. }
  1874. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1875. document.mainViewController?.menuItemAction_linkPage(sender)
  1876. }
  1877. }
  1878. // stamp
  1879. @IBAction func menuItemAction_stamp(_ sender: Any) {
  1880. if (self.canResponseDocumentAction() == false) {
  1881. return
  1882. }
  1883. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1884. document.mainViewController?.menuItemAction_stamp(sender)
  1885. }
  1886. }
  1887. @IBAction func notesLink(_ sender: Any?) {
  1888. if (self.canResponseDocumentAction() == false) {
  1889. return
  1890. }
  1891. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1892. document.mainViewController?.notesLink(sender)
  1893. }
  1894. }
  1895. @IBAction func notesStamp(_ sender: Any?) {
  1896. if (self.canResponseDocumentAction() == false) {
  1897. return
  1898. }
  1899. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1900. document.mainViewController?.notesStamp(sender)
  1901. }
  1902. }
  1903. @IBAction func menuItemAction_signure(_ sender: Any) {
  1904. if (self.canResponseDocumentAction() == false) {
  1905. return
  1906. }
  1907. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1908. document.mainViewController?.menuItemAction_signure(sender)
  1909. }
  1910. }
  1911. @IBAction func image(_ sender: Any?) {
  1912. if (self.canResponseDocumentAction() == false) {
  1913. return
  1914. }
  1915. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1916. document.mainViewController?.image(sender)
  1917. }
  1918. }
  1919. @IBAction func table(_ sender: Any?) {
  1920. if (self.canResponseDocumentAction() == false) {
  1921. return
  1922. }
  1923. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1924. document.mainViewController?.table(sender)
  1925. }
  1926. }
  1927. @IBAction func addForm(_ sender: Any?) {
  1928. if (self.canResponseDocumentAction() == false) {
  1929. return
  1930. }
  1931. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1932. document.mainViewController?.addForm(sender)
  1933. }
  1934. }
  1935. @IBAction func editNote(_ sender: Any?) {
  1936. if (self.canResponseDocumentAction() == false) {
  1937. return
  1938. }
  1939. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1940. document.mainViewController?.editNote(sender)
  1941. }
  1942. }
  1943. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  1944. if (self.canResponseDocumentAction() == false) {
  1945. return
  1946. }
  1947. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1948. document.mainViewController?.menuItemAction_hiddenAllAnnotation(sender)
  1949. }
  1950. }
  1951. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  1952. if (self.canResponseDocumentAction() == false) {
  1953. return
  1954. }
  1955. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1956. document.mainViewController?.menuItemAction_clearAllAnnotation(sender)
  1957. }
  1958. }
  1959. }
  1960. // MARK: - KMSystemPDFMenuProtocol
  1961. extension KMBrowserWindowController: KMSystemPDFMenuProtocol {
  1962. @IBAction func changeDisplayMode(_ sender: Any?) {
  1963. if (self.canResponseDocumentAction() == false) {
  1964. return
  1965. }
  1966. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1967. document.mainViewController?.changeDisplayMode(sender)
  1968. }
  1969. }
  1970. @IBAction func toggleDisplayAsBook(_ sender: Any?) {
  1971. if (self.canResponseDocumentAction() == false) {
  1972. return
  1973. }
  1974. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1975. document.mainViewController?.toggleDisplayAsBook(sender)
  1976. }
  1977. }
  1978. @IBAction func toggleDisplayPageBreaks(_ sender: Any?) {
  1979. if (self.canResponseDocumentAction() == false) {
  1980. return
  1981. }
  1982. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1983. document.mainViewController?.toggleDisplayPageBreaks(sender)
  1984. }
  1985. }
  1986. @IBAction func changeDisplayBox(_ sender: Any?) {
  1987. if (self.canResponseDocumentAction() == false) {
  1988. return
  1989. }
  1990. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1991. document.mainViewController?.changeDisplayBox(sender)
  1992. }
  1993. }
  1994. @IBAction func toggleAutoScale(_ sender: Any?) {
  1995. if (self.canResponseDocumentAction() == false) {
  1996. return
  1997. }
  1998. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1999. document.mainViewController?.toggleAutoScale(sender)
  2000. }
  2001. }
  2002. @IBAction func doZoomToActualSize(_ sender: Any?) {
  2003. if (self.canResponseDocumentAction() == false) {
  2004. return
  2005. }
  2006. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2007. document.mainViewController?.doZoomToActualSize(sender)
  2008. }
  2009. }
  2010. @IBAction func doZoomToFit(_ sender: Any?) {
  2011. if (self.canResponseDocumentAction() == false) {
  2012. return
  2013. }
  2014. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2015. document.mainViewController?.doZoomToFit(sender)
  2016. }
  2017. }
  2018. @IBAction func doZoomIn(_ sender: Any?) {
  2019. if (self.canResponseDocumentAction() == false) {
  2020. return
  2021. }
  2022. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2023. document.mainViewController?.doZoomIn(sender)
  2024. }
  2025. }
  2026. @IBAction func doZoomOut(_ sender: Any?) {
  2027. if (self.canResponseDocumentAction() == false) {
  2028. return
  2029. }
  2030. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2031. document.mainViewController?.doZoomOut(sender)
  2032. }
  2033. }
  2034. @IBAction func doMarqueeZoomTool(_ sender: Any?) {
  2035. if (self.canResponseDocumentAction() == false) {
  2036. return
  2037. }
  2038. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2039. document.mainViewController?.doMarqueeZoomTool(sender)
  2040. }
  2041. }
  2042. @IBAction func highlightFormFiled(_ sender: Any?) {
  2043. if (self.canResponseDocumentAction() == false) {
  2044. return
  2045. }
  2046. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2047. document.mainViewController?.highlightFormFiled(sender)
  2048. }
  2049. }
  2050. @IBAction func highlightLinks(_ sender: Any?) {
  2051. if (self.canResponseDocumentAction() == false) {
  2052. return
  2053. }
  2054. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2055. document.mainViewController?.highlightLinks(sender)
  2056. }
  2057. }
  2058. @IBAction func resetForm(_ sender: Any?) {
  2059. if (self.canResponseDocumentAction() == false) {
  2060. return
  2061. }
  2062. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2063. document.mainViewController?.resetForm(sender)
  2064. }
  2065. }
  2066. @IBAction func removeAllAnnotation(_ sender: Any?) {
  2067. if (self.canResponseDocumentAction() == false) {
  2068. return
  2069. }
  2070. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2071. document.mainViewController?.removeAllAnnotation(sender)
  2072. }
  2073. }
  2074. @IBAction func savePDFSettingToDefaults(_ sender: Any?) {
  2075. if (self.canResponseDocumentAction() == false) {
  2076. return
  2077. }
  2078. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2079. document.mainViewController?.savePDFSettingToDefaults(sender)
  2080. }
  2081. }
  2082. }
  2083. // MARK: - KMSystemGotoMenuProtocol
  2084. extension KMBrowserWindowController: KMSystemGotoMenuProtocol {
  2085. func menuItemAction_nextPage(_ sender: Any) {
  2086. if (self.canResponseDocumentAction() == false) {
  2087. return
  2088. }
  2089. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2090. document.mainViewController?.menuItemAction_nextPage(sender)
  2091. }
  2092. }
  2093. func menuItemAction_forwardPage(_ sender: Any) {
  2094. if (self.canResponseDocumentAction() == false) {
  2095. return
  2096. }
  2097. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2098. document.mainViewController?.menuItemAction_forwardPage(sender)
  2099. }
  2100. }
  2101. func menuItemAction_firstPage(_ sender: Any) {
  2102. if (self.canResponseDocumentAction() == false) {
  2103. return
  2104. }
  2105. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2106. document.mainViewController?.menuItemAction_firstPage(sender)
  2107. }
  2108. }
  2109. func menuItemAction_lastPage(_ sender: Any) {
  2110. if (self.canResponseDocumentAction() == false) {
  2111. return
  2112. }
  2113. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2114. document.mainViewController?.menuItemAction_lastPage(sender)
  2115. }
  2116. }
  2117. func menuItemAction_forward(_ sender: Any) {
  2118. if (self.canResponseDocumentAction() == false) {
  2119. return
  2120. }
  2121. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2122. document.mainViewController?.menuItemAction_forward(sender)
  2123. }
  2124. }
  2125. func menuItemAction_goback(_ sender: Any) {
  2126. if (self.canResponseDocumentAction() == false) {
  2127. return
  2128. }
  2129. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2130. document.mainViewController?.menuItemAction_goback(sender)
  2131. }
  2132. }
  2133. func menuItemAction_gotoPage(_ sender: Any) {
  2134. if (self.canResponseDocumentAction() == false) {
  2135. return
  2136. }
  2137. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2138. document.mainViewController?.menuItemAction_gotoPage(sender)
  2139. }
  2140. }
  2141. @IBAction func goToMarkedPage(_ sender: Any?) {
  2142. if (self.canResponseDocumentAction() == false) {
  2143. return
  2144. }
  2145. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2146. document.mainViewController?.goToMarkedPage(sender)
  2147. }
  2148. }
  2149. @IBAction func markPage(_ sender: Any?) {
  2150. if (self.canResponseDocumentAction() == false) {
  2151. return
  2152. }
  2153. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2154. document.mainViewController?.markPage(sender)
  2155. }
  2156. }
  2157. }
  2158. // MARK: - KMSystemToolMenuProtocol
  2159. extension KMBrowserWindowController: KMSystemToolMenuProtocol {
  2160. @IBAction func getInfo(_ sender: Any?) {
  2161. if (self.canResponseDocumentAction() == false) {
  2162. return
  2163. }
  2164. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2165. document.mainViewController?.getInfo(sender)
  2166. }
  2167. }
  2168. func orderFrontNotesPanel(_ sender: Any?) {
  2169. let windowC = KMNotesPanelController.shared
  2170. if let win = windowC.window {
  2171. if win.isVisible {
  2172. win.orderOut(sender)
  2173. } else {
  2174. win.orderFront(sender)
  2175. }
  2176. }
  2177. }
  2178. func menuItemAction_scrolTool(_ sender: Any) {
  2179. if (self.canResponseDocumentAction() == false) {
  2180. return
  2181. }
  2182. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2183. document.mainViewController?.menuItemAction_scrolTool(sender)
  2184. }
  2185. }
  2186. func menuItemAction_zoomOutTool(_ sender: Any) {
  2187. if (self.canResponseDocumentAction() == false) {
  2188. return
  2189. }
  2190. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2191. document.mainViewController?.menuItemAction_zoomOutTool(sender)
  2192. }
  2193. }
  2194. func menuItemAction_selectTool(_ sender: Any) {
  2195. if (self.canResponseDocumentAction() == false) {
  2196. return
  2197. }
  2198. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2199. document.mainViewController?.menuItemAction_selectTool(sender)
  2200. }
  2201. }
  2202. @IBAction func changeToolMode(_ sender: Any?) {
  2203. if (self.canResponseDocumentAction() == false) {
  2204. return
  2205. }
  2206. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2207. if (document.isHome) {
  2208. } else {
  2209. document.mainViewController?.changeToolMode(sender)
  2210. }
  2211. }
  2212. }
  2213. @IBAction func compression(_ sender: Any?) {
  2214. if (self.canResponseDocumentAction() == false) {
  2215. return
  2216. }
  2217. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2218. if (document.isHome) {
  2219. } else {
  2220. document.mainViewController?.compression(sender)
  2221. }
  2222. }
  2223. }
  2224. @IBAction func merge(_ sender: Any?) {
  2225. if (self.canResponseDocumentAction() == false) {
  2226. return
  2227. }
  2228. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2229. if (document.isHome) {
  2230. } else {
  2231. document.mainViewController?.merge(sender)
  2232. }
  2233. }
  2234. }
  2235. @IBAction func convert(_ sender: Any?) {
  2236. if (self.canResponseDocumentAction() == false) {
  2237. return
  2238. }
  2239. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2240. if (document.isHome) {
  2241. } else {
  2242. document.mainViewController?.convert(sender)
  2243. }
  2244. }
  2245. }
  2246. @IBAction func imageToPDF(_ sender: Any?) {
  2247. if (self.canResponseDocumentAction() == false) {
  2248. return
  2249. }
  2250. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2251. if (document.isHome) {
  2252. } else {
  2253. document.mainViewController?.imageToPDF(sender)
  2254. }
  2255. }
  2256. }
  2257. @IBAction func ocr(_ sender: Any?) {
  2258. if (self.canResponseDocumentAction() == false) {
  2259. return
  2260. }
  2261. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2262. if (document.isHome) {
  2263. } else {
  2264. document.mainViewController?.ocr(sender)
  2265. }
  2266. }
  2267. }
  2268. @IBAction func extractImage(_ sender: Any?) {
  2269. if (self.canResponseDocumentAction() == false) {
  2270. return
  2271. }
  2272. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2273. if (document.isHome) {
  2274. } else {
  2275. document.mainViewController?.extractImage(sender)
  2276. }
  2277. }
  2278. }
  2279. @IBAction func pageEdit(_ sender: Any?) {
  2280. if (self.canResponseDocumentAction() == false) {
  2281. return
  2282. }
  2283. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2284. if (document.isHome) {
  2285. } else {
  2286. document.mainViewController?.pageEdit(sender)
  2287. }
  2288. }
  2289. }
  2290. @IBAction func textEditAction(_ sender: Any?) {
  2291. if (self.canResponseDocumentAction() == false) {
  2292. return
  2293. }
  2294. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2295. if (document.isHome) {
  2296. } else {
  2297. document.mainViewController?.textEditAction(sender)
  2298. }
  2299. }
  2300. }
  2301. @IBAction func security(_ sender: Any?) {
  2302. if (self.canResponseDocumentAction() == false) {
  2303. return
  2304. }
  2305. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2306. if (document.isHome) {
  2307. } else {
  2308. document.mainViewController?.security(sender)
  2309. }
  2310. }
  2311. }
  2312. @IBAction func decryption(_ sender: Any?) {
  2313. if (self.canResponseDocumentAction() == false) {
  2314. return
  2315. }
  2316. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2317. if (document.isHome) {
  2318. } else {
  2319. document.mainViewController?.decryption(sender)
  2320. }
  2321. }
  2322. }
  2323. @IBAction func cropAll(_ sender: Any?) {
  2324. if (self.canResponseDocumentAction() == false) {
  2325. return
  2326. }
  2327. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2328. if (document.isHome) {
  2329. } else {
  2330. document.mainViewController?.cropAll(sender)
  2331. }
  2332. }
  2333. }
  2334. @IBAction func crop(_ sender: Any?) {
  2335. if (self.canResponseDocumentAction() == false) {
  2336. return
  2337. }
  2338. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2339. if (document.isHome) {
  2340. } else {
  2341. document.mainViewController?.crop(sender)
  2342. }
  2343. }
  2344. }
  2345. @IBAction func autoCropAll(_ sender: Any?) {
  2346. if (self.canResponseDocumentAction() == false) {
  2347. return
  2348. }
  2349. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2350. if (document.isHome) {
  2351. } else {
  2352. document.mainViewController?.autoCropAll(sender)
  2353. }
  2354. }
  2355. }
  2356. @IBAction func smartAutoCropAll(_ sender: Any?) {
  2357. if (self.canResponseDocumentAction() == false) {
  2358. return
  2359. }
  2360. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2361. if (document.isHome) {
  2362. } else {
  2363. document.mainViewController?.smartAutoCropAll(sender)
  2364. }
  2365. }
  2366. }
  2367. @IBAction func cropOptionsModel(_ sender: Any?) {
  2368. if (self.canResponseDocumentAction() == false) {
  2369. return
  2370. }
  2371. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2372. if (document.isHome) {
  2373. } else {
  2374. // document.mainViewController?.cropOptionsModel(sender)
  2375. }
  2376. }
  2377. }
  2378. @IBAction func selectToolModel(_ sender: Any?) {
  2379. if (self.canResponseDocumentAction() == false) {
  2380. return
  2381. }
  2382. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2383. if (document.isHome) {
  2384. } else {
  2385. document.mainViewController?.selectToolModel(sender)
  2386. }
  2387. }
  2388. }
  2389. @IBAction func takeSnapshot(_ sender: Any?) {
  2390. if (self.canResponseDocumentAction() == false) {
  2391. return
  2392. }
  2393. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2394. if (document.isHome) {
  2395. } else {
  2396. document.mainViewController?.takeSnapshot(sender)
  2397. }
  2398. }
  2399. }
  2400. @IBAction func takeSnapshotSelectContent(_ sender: Any?) {
  2401. if (self.canResponseDocumentAction() == false) {
  2402. return
  2403. }
  2404. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2405. if (document.isHome) {
  2406. } else {
  2407. document.mainViewController?.takeSnapshotSelectContent(sender)
  2408. }
  2409. }
  2410. }
  2411. @IBAction func changeAnnotationMode(_ sender: Any?) {
  2412. if (self.canResponseDocumentAction() == false) {
  2413. return
  2414. }
  2415. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2416. if (document.isHome) {
  2417. } else {
  2418. document.mainViewController?.changeAnnotationMode(sender)
  2419. }
  2420. }
  2421. }
  2422. @IBAction func addBackground(_ sender: Any?) {
  2423. if (self.canResponseDocumentAction() == false) {
  2424. return
  2425. }
  2426. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2427. if (document.isHome) {
  2428. } else {
  2429. document.mainViewController?.addBackground(sender)
  2430. }
  2431. }
  2432. }
  2433. @IBAction func removeBackground(_ sender: Any?) {
  2434. if (self.canResponseDocumentAction() == false) {
  2435. return
  2436. }
  2437. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2438. if (document.isHome) {
  2439. } else {
  2440. document.mainViewController?.removeBackground(sender)
  2441. }
  2442. }
  2443. }
  2444. @IBAction func addWatermark(_ sender: Any?) {
  2445. if (self.canResponseDocumentAction() == false) {
  2446. return
  2447. }
  2448. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2449. if (document.isHome) {
  2450. } else {
  2451. document.mainViewController?.addWatermark(sender)
  2452. }
  2453. }
  2454. }
  2455. @IBAction func editWatermark(_ sender: Any?) {
  2456. KMPrint("editWatermark ...")
  2457. }
  2458. @IBAction func batchAddWatermark(_ sender: Any?) {
  2459. if (self.canResponseDocumentAction() == false) {
  2460. return
  2461. }
  2462. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2463. if (document.isHome) {
  2464. } else {
  2465. document.mainViewController?.batchAddWatermark(sender)
  2466. }
  2467. }
  2468. }
  2469. @IBAction func batchRemoveWatermark(_ sender: Any?) {
  2470. if (self.canResponseDocumentAction() == false) {
  2471. return
  2472. }
  2473. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2474. if (document.isHome) {
  2475. } else {
  2476. document.mainViewController?.batchRemoveWatermark(sender)
  2477. }
  2478. }
  2479. }
  2480. @IBAction func removeWatermark(_ sender: Any?) {
  2481. if (self.canResponseDocumentAction() == false) {
  2482. return
  2483. }
  2484. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2485. if (document.isHome) {
  2486. } else {
  2487. document.mainViewController?.removeWatermark(sender)
  2488. }
  2489. }
  2490. }
  2491. @IBAction func templateWatermark(_ sender: Any?) {
  2492. if (self.canResponseDocumentAction() == false) {
  2493. return
  2494. }
  2495. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2496. if (document.isHome) {
  2497. } else {
  2498. document.mainViewController?.templateWatermark(sender)
  2499. }
  2500. }
  2501. }
  2502. @IBAction func batchAddBackground(_ sender: Any?) {
  2503. if (self.canResponseDocumentAction() == false) {
  2504. return
  2505. }
  2506. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2507. if (document.isHome) {
  2508. } else {
  2509. document.mainViewController?.batchAddBackground(sender)
  2510. }
  2511. }
  2512. }
  2513. @IBAction func batchRemoveBackground(_ sender: Any?) {
  2514. if (self.canResponseDocumentAction() == false) {
  2515. return
  2516. }
  2517. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2518. if (document.isHome) {
  2519. } else {
  2520. document.mainViewController?.batchRemoveBackground(sender)
  2521. }
  2522. }
  2523. }
  2524. @IBAction func templateBackground(_ sender: Any?) {
  2525. if (self.canResponseDocumentAction() == false) {
  2526. return
  2527. }
  2528. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2529. if (document.isHome) {
  2530. } else {
  2531. document.mainViewController?.templateBackground(sender)
  2532. }
  2533. }
  2534. }
  2535. @IBAction func addHeaderFooter(_ sender: Any?) {
  2536. if (self.canResponseDocumentAction() == false) {
  2537. return
  2538. }
  2539. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2540. if (document.isHome) {
  2541. } else {
  2542. document.mainViewController?.addHeaderFooter(sender)
  2543. }
  2544. }
  2545. }
  2546. @IBAction func removeHeaderFooter(_ sender: Any?) {
  2547. if (self.canResponseDocumentAction() == false) {
  2548. return
  2549. }
  2550. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2551. if (document.isHome) {
  2552. } else {
  2553. document.mainViewController?.removeHeaderFooter(sender)
  2554. }
  2555. }
  2556. }
  2557. @IBAction func batchAddHeaderFooter(_ sender: Any?) {
  2558. if (self.canResponseDocumentAction() == false) {
  2559. return
  2560. }
  2561. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2562. if (document.isHome) {
  2563. } else {
  2564. document.mainViewController?.batchAddHeaderFooter(sender)
  2565. }
  2566. }
  2567. }
  2568. @IBAction func batchRemoveHeaderFooter(_ sender: Any?) {
  2569. if (self.canResponseDocumentAction() == false) {
  2570. return
  2571. }
  2572. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2573. if (document.isHome) {
  2574. } else {
  2575. document.mainViewController?.batchRemoveHeaderFooter(sender)
  2576. }
  2577. }
  2578. }
  2579. @IBAction func templateHeaderFooter(_ sender: Any?) {
  2580. if (self.canResponseDocumentAction() == false) {
  2581. return
  2582. }
  2583. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2584. if (document.isHome) {
  2585. } else {
  2586. document.mainViewController?.templateHeaderFooter(sender)
  2587. }
  2588. }
  2589. }
  2590. @IBAction func addHeaderFooterBates(_ sender: Any?) {
  2591. if (self.canResponseDocumentAction() == false) {
  2592. return
  2593. }
  2594. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2595. if (document.isHome) {
  2596. } else {
  2597. document.mainViewController?.addHeaderFooterBates(sender)
  2598. }
  2599. }
  2600. }
  2601. @IBAction func removeHeaderFooterBates(_ sender: Any?) {
  2602. if (self.canResponseDocumentAction() == false) {
  2603. return
  2604. }
  2605. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2606. if (document.isHome) {
  2607. } else {
  2608. document.mainViewController?.removeHeaderFooterBates(sender)
  2609. }
  2610. }
  2611. }
  2612. @IBAction func batchAddBates(_ sender: Any?) {
  2613. if (self.canResponseDocumentAction() == false) {
  2614. return
  2615. }
  2616. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2617. if (document.isHome) {
  2618. } else {
  2619. document.mainViewController?.batchAddBates(sender)
  2620. }
  2621. }
  2622. }
  2623. @IBAction func batchRemoveBates(_ sender: Any?) {
  2624. if (self.canResponseDocumentAction() == false) {
  2625. return
  2626. }
  2627. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2628. if (document.isHome) {
  2629. } else {
  2630. document.mainViewController?.batchRemoveBates(sender)
  2631. }
  2632. }
  2633. }
  2634. @IBAction func templateBates(_ sender: Any?) {
  2635. if (self.canResponseDocumentAction() == false) {
  2636. return
  2637. }
  2638. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2639. if (document.isHome) {
  2640. } else {
  2641. document.mainViewController?.templateBates(sender)
  2642. }
  2643. }
  2644. }
  2645. @IBAction func togglePoster(_ sender: Any?) {
  2646. if (self.canResponseDocumentAction() == false) {
  2647. return
  2648. }
  2649. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2650. if (document.isHome) {
  2651. } else {
  2652. document.mainViewController?.togglePoster(sender)
  2653. }
  2654. }
  2655. }
  2656. @IBAction func toggleMultiple(_ sender: Any?) {
  2657. if (self.canResponseDocumentAction() == false) {
  2658. return
  2659. }
  2660. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2661. if (document.isHome) {
  2662. } else {
  2663. document.mainViewController?.toggleMultiple(sender)
  2664. }
  2665. }
  2666. }
  2667. @IBAction func toggleBooklet(_ sender: Any?) {
  2668. if (self.canResponseDocumentAction() == false) {
  2669. return
  2670. }
  2671. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2672. if (document.isHome) {
  2673. } else {
  2674. document.mainViewController?.toggleBooklet(sender)
  2675. }
  2676. }
  2677. }
  2678. @IBAction func rotateRight(_ sender: Any?) {
  2679. if (self.canResponseDocumentAction() == false) {
  2680. return
  2681. }
  2682. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2683. if (document.isHome) {
  2684. } else {
  2685. document.mainViewController?.rotateRight(sender)
  2686. }
  2687. }
  2688. }
  2689. @IBAction func rotateLeft(_ sender: Any?) {
  2690. if (self.canResponseDocumentAction() == false) {
  2691. return
  2692. }
  2693. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2694. if (document.isHome) {
  2695. } else {
  2696. document.mainViewController?.rotateLeft(sender)
  2697. }
  2698. }
  2699. }
  2700. @IBAction func rotateAllRight(_ sender: Any?) {
  2701. if (self.canResponseDocumentAction() == false) {
  2702. return
  2703. }
  2704. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2705. if (document.isHome) {
  2706. } else {
  2707. document.mainViewController?.rotateAllRight(sender)
  2708. }
  2709. }
  2710. }
  2711. @IBAction func rotateAllLeft(_ sender: Any?) {
  2712. if (self.canResponseDocumentAction() == false) {
  2713. return
  2714. }
  2715. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2716. if (document.isHome) {
  2717. } else {
  2718. document.mainViewController?.rotateAllLeft(sender)
  2719. }
  2720. }
  2721. }
  2722. @IBAction func autoSelectContent(_ sender: Any?) {
  2723. if (self.canResponseDocumentAction() == false) {
  2724. return
  2725. }
  2726. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2727. if (document.isHome) {
  2728. } else {
  2729. document.mainViewController?.autoSelectContent(sender)
  2730. }
  2731. }
  2732. }
  2733. }
  2734. // MARK: - KMSystemWindowMenuRrotocol
  2735. extension KMBrowserWindowController: KMSystemWindowMenuRrotocol {
  2736. func menuItemAction_showForwardTagPage(_ sender: Any) {
  2737. if (self.canResponseDocumentAction() == false) {
  2738. return
  2739. }
  2740. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2741. if (document.isHome) {
  2742. document.homeViewController?.menuItemAction_showForwardTagPage(sender)
  2743. } else {
  2744. document.mainViewController?.menuItemAction_showForwardTagPage(sender)
  2745. }
  2746. }
  2747. }
  2748. func menuItemAction_showNextTagPage(_ 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_showNextTagPage(sender)
  2755. } else {
  2756. document.mainViewController?.menuItemAction_showNextTagPage(sender)
  2757. }
  2758. }
  2759. }
  2760. func menuItemAction_newTagPageToNewWindow(_ 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_newTagPageToNewWindow(sender)
  2767. } else {
  2768. document.mainViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2769. }
  2770. }
  2771. }
  2772. func menuItemAction_mergeAllWindow(_ 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_mergeAllWindow(sender)
  2779. } else {
  2780. document.mainViewController?.menuItemAction_mergeAllWindow(sender)
  2781. }
  2782. }
  2783. }
  2784. @IBAction func performFit(_ sender: Any?) {
  2785. if (self.canResponseDocumentAction() == false) {
  2786. return
  2787. }
  2788. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2789. if (document.isHome) {
  2790. } else {
  2791. document.mainViewController?.performFit(sender)
  2792. }
  2793. }
  2794. }
  2795. }
  2796. // MARK: - Analytics (埋点)
  2797. extension KMBrowserWindowController {
  2798. func trackEvent_upgrade() -> Void {
  2799. KMAnalytics.trackEvent(eventName: "Btn_Tbr_Upgrade", parameters: [
  2800. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.tbr,
  2801. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.tbr_Btn], platform: .AppCenter, appTarget: .all)
  2802. }
  2803. }