KMBrowserWindowController.swift 116 KB

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