KMBrowserWindowController.swift 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156
  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. return true
  626. }
  627. }
  628. extension KMBrowserWindowController {
  629. @IBAction func menuItemAction_openFile(_ sender: Any) {
  630. super.openDocument(sender)
  631. }
  632. @IBAction func importFromFile(_ sender: Any) {
  633. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  634. if (document.isHome) {
  635. document.homeViewController?.importFromFile(sender)
  636. } else {
  637. self.homeVC = KMHomeViewController()
  638. self.homeVC?.importFromFile(sender)
  639. }
  640. }
  641. @IBAction func openBlankPage(_ sender: Any) {
  642. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  643. if (document.isHome) {
  644. document.homeViewController?.openBlankPage()
  645. } else {
  646. self.homeVC = KMHomeViewController()
  647. self.homeVC?.openBlankPage()
  648. }
  649. }
  650. @IBAction func importFromScanner(_ sender: Any) {
  651. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  652. if (document.isHome) {
  653. document.homeViewController?.importFromScanner()
  654. } else {
  655. self.homeVC = KMHomeViewController()
  656. self.homeVC?.importFromScanner()
  657. }
  658. }
  659. @IBAction func menuItemAction_newTab(_ sender: Any) {
  660. self.openDocumentWindow()
  661. }
  662. @IBAction func pasteAsPlainText(_ sender: Any?) {
  663. KMPrint("pasteAsPlainText")
  664. }
  665. @IBAction func deselectAll(_ sender: Any?) {
  666. KMPrint("deselectAll")
  667. }
  668. @IBAction func searchPDF(_ sender: Any?) {
  669. if let document = self.browser.activeTabContents() as? KMMainDocument {
  670. if (document.isHome) {
  671. } else {
  672. document.mainViewController?.leftSideViewController.showSearchMode("")
  673. }
  674. }
  675. }
  676. @IBAction func changeColor(_ sender: Any?) {
  677. KMPrint("changeColor ...")
  678. }
  679. @IBAction func changeAttributes(_ sender: Any?) {
  680. KMPrint("changeAttributes ...")
  681. }
  682. @IBAction func alignLeft(_ sender: Any?) {
  683. KMPrint("alignLeft ...")
  684. }
  685. @IBAction func alignRight(_ sender: Any?) {
  686. KMPrint("alignRight ...")
  687. }
  688. @IBAction func alignCenter(_ sender: Any?) {
  689. KMPrint("alignCenter ...")
  690. }
  691. @IBAction func createNewNote(_ sender: Any?) {
  692. KMPrint("createNewNote ...")
  693. }
  694. @IBAction func toggleHideNotes(_ sender: Any?) {
  695. KMPrint("toggleHideNotes ...")
  696. }
  697. @IBAction func changeDisplaySinglePages(_ sender: Any?) {
  698. KMPrint("changeDisplaySinglePages ...")
  699. }
  700. @IBAction func changeDisplayContinuous(_ sender: Any?) {
  701. KMPrint("changeDisplayContinuous ...")
  702. }
  703. @IBAction func doGoToNextPage(_ sender: Any?) {
  704. KMPrint("doGoToNextPage ...")
  705. }
  706. @IBAction func doGoToPreviousPage(_ sender: Any?) {
  707. KMPrint("doGoToPreviousPage ...")
  708. }
  709. @IBAction func doGoToFirstPage(_ sender: Any?) {
  710. KMPrint("doGoToFirstPage ...")
  711. }
  712. @IBAction func doGoToLastPage(_ sender: Any?) {
  713. KMPrint("doGoToLastPage ...")
  714. }
  715. @IBAction func allGoToNextPage(_ sender: Any?) {
  716. KMPrint("allGoToNextPage ...")
  717. }
  718. @IBAction func allGoToPreviousPage(_ sender: Any?) {
  719. KMPrint("allGoToPreviousPage ...")
  720. }
  721. @IBAction func allGoToFirstPage(_ sender: Any?) {
  722. KMPrint("allGoToFirstPage ...")
  723. }
  724. @IBAction func allGoToLastPage(_ sender: Any?) {
  725. KMPrint("allGoToLastPage ...")
  726. }
  727. @IBAction func doGoToPage(_ sender: Any?) {
  728. KMPrint("doGoToPage ...")
  729. }
  730. @IBAction func doGoBack(_ sender: Any?) {
  731. KMPrint("doGoBack ...")
  732. }
  733. @IBAction func doGoForward(_ sender: Any?) {
  734. KMPrint("doGoForward ...")
  735. }
  736. @IBAction func doZoomToPhysicalSize(_ sender: Any?) {
  737. KMPrint("doZoomToPhysicalSize ...")
  738. }
  739. @IBAction func alternateZoomToFit(_ sender: Any?) {
  740. KMPrint("alternateZoomToFit ...")
  741. }
  742. @IBAction func doZoomToSelection(_ sender: Any?) {
  743. KMPrint("doZoomToSelection ...")
  744. }
  745. @IBAction func doAutoScale(_ sender: Any?) {
  746. KMPrint("doAutoScale ...")
  747. }
  748. // @IBAction func delete(_ sender: Any?) {
  749. // KMPrint("delete ...")
  750. // }
  751. // @IBAction func paste(_ sender: Any?) {
  752. // KMPrint("paste ...")
  753. // }
  754. @IBAction func alternatePaste(_ sender: Any?) {
  755. KMPrint("alternatePaste ...")
  756. }
  757. @IBAction func copy(_ sender: Any?) {
  758. KMPrint("copy ...")
  759. }
  760. @IBAction func cut(_ sender: Any?) {
  761. KMPrint("cut ...")
  762. }
  763. @IBAction func toggleFullscreen(_ sender: Any?) {
  764. KMPrint("toggleFullscreen ...")
  765. }
  766. @IBAction func toggleRightSidePane(_ sender: Any?) {
  767. KMPrint("toggleRightSidePane ...")
  768. }
  769. @IBAction func changeRightSidePaneState(_ sender: Any?) {
  770. KMPrint("changeRightSidePaneState ...")
  771. }
  772. @IBAction func changeFindPaneState(_ sender: Any?) {
  773. KMPrint("changeFindPaneState ...")
  774. }
  775. @IBAction func toggleSplitPDF(_ sender: Any?) {
  776. KMPrint("toggleSplitPDF ...")
  777. }
  778. @IBAction func search(_ sender: Any?) {
  779. KMPrint("search ...")
  780. }
  781. @IBAction func searchNotes(_ sender: Any?) {
  782. KMPrint("searchNotes ...")
  783. }
  784. @IBAction func password(_ sender: Any?) {
  785. KMPrint("password ...")
  786. }
  787. @IBAction func toggleCaseInsensitiveSearch(_ sender: Any?) {
  788. KMPrint("toggleCaseInsensitiveSearch ...")
  789. }
  790. @IBAction func toggleWholeWordSearch(_ sender: Any?) {
  791. KMPrint("toggleWholeWordSearch ...")
  792. }
  793. @IBAction func toggleCaseInsensitiveNoteSearch(_ sender: Any?) {
  794. KMPrint("toggleCaseInsensitiveNoteSearch ...")
  795. }
  796. @IBAction func performFindPanelAction(_ sender: NSMenuItem) {
  797. var forward = true
  798. var findString: String?
  799. switch sender.tag {
  800. case 1:
  801. if let document = self.browser.activeTabContents() as? KMMainDocument {
  802. if document.isHome == false {
  803. document.mainViewController?.toolbarController.showFindBar()
  804. }
  805. }
  806. case 3:
  807. if let document = self.browser.activeTabContents() as? KMMainDocument {
  808. if document.isHome == false {
  809. document.mainViewController?.toolbarController.showFindString(forward: false)
  810. }
  811. }
  812. case 2:
  813. if let document = self.browser.activeTabContents() as? KMMainDocument {
  814. if document.isHome == false {
  815. document.mainViewController?.toolbarController.showFindString(forward: true)
  816. }
  817. }
  818. case 7:
  819. if let document = self.browser.activeTabContents() as? KMMainDocument {
  820. if document.isHome == false {
  821. document.mainViewController?.toolbarController.showAllAction()
  822. }
  823. }
  824. default: break
  825. }
  826. }
  827. @IBAction func scrollUp(_ sender: Any?) {
  828. KMPrint("scrollUp ...")
  829. }
  830. @IBAction func scrollDown(_ sender: Any?) {
  831. KMPrint("scrollDown ...")
  832. }
  833. @IBAction func scrollRight(_ sender: Any?) {
  834. KMPrint("scrollRight ...")
  835. }
  836. @IBAction func scrollLeft(_ sender: Any?) {
  837. KMPrint("scrollLeft ...")
  838. }
  839. @IBAction func selectSelectedNote(_ sender: Any?) {
  840. KMPrint("selectSelectedNote ...")
  841. }
  842. @IBAction func goToSelectedOutlineItem(_ sender: Any?) {
  843. KMPrint("goToSelectedOutlineItem ...")
  844. }
  845. @IBAction func goToSelectedFindResults(_ sender: Any?) {
  846. KMPrint("goToSelectedFindResults ...")
  847. }
  848. @IBAction func toggleSelectedSnapshots(_ sender: Any?) {
  849. KMPrint("toggleSelectedSnapshots ...")
  850. }
  851. @IBAction func selfSignProfile(_ sender: Any?) {
  852. KMPrint("selfSignProfile ...")
  853. }
  854. @IBAction func link(_ sender: Any?) {
  855. KMPrint("link ...")
  856. }
  857. @IBAction func insertPage(_ sender: Any?) {
  858. KMPrint("insertPage ...")
  859. }
  860. @IBAction func split(_ sender: Any?) {
  861. KMPrint("split ...")
  862. }
  863. @IBAction func comparison(_ sender: Any?) {
  864. KMPrint("comparison ...")
  865. }
  866. @IBAction func contentComparison(_ sender: Any?) {
  867. KMPrint("contentComparison ...")
  868. }
  869. @IBAction func coverageComparison(_ sender: Any?) {
  870. KMPrint("coverageComparison ...")
  871. }
  872. @IBAction func readMode(_ sender: Any?) {
  873. KMPrint("readMode ...")
  874. }
  875. @IBAction func changeFormType(_ sender: Any?) {
  876. KMPrint("changeFormType ...")
  877. }
  878. @IBAction func changeSelfSignType(_ sender: Any?) {
  879. KMPrint("changeSelfSignType ...")
  880. }
  881. @IBAction func clearAllForm(_ sender: Any?) {
  882. KMPrint("clearAllForm ...")
  883. }
  884. @IBAction func thumbnailSizeScaling(_ sender: Any?) {
  885. KMPrint("thumbnailSizeScaling ...")
  886. }
  887. @IBAction func redactAction(_ sender: Any?) {
  888. if (self.canResponseDocumentAction() == false) {
  889. return
  890. }
  891. if let document = self.browser.activeTabContents() as? KMMainDocument {
  892. if document.isHome == false {
  893. document.mainViewController?.menuItemAction_redact(sender)
  894. }
  895. }
  896. }
  897. }
  898. // MARK: - KMSystemFileMenuProtocol
  899. extension KMBrowserWindowController: KMSystemFileMenuProtocol {
  900. func menuItemClick_saveAsFlattenedPDF(_ sender: Any) {
  901. if (self.canResponseDocumentAction() == false) {
  902. return
  903. }
  904. if let document = self.browser.activeTabContents() as? KMMainDocument {
  905. if (document.isHome) {
  906. } else {
  907. document.mainViewController?.menuItemClick_saveAsFlattenedPDF(sender)
  908. }
  909. }
  910. }
  911. func menuItemClick_mergePDF(_ sender: Any) {
  912. if (self.canResponseDocumentAction() == false) {
  913. return
  914. }
  915. if let document = self.browser.activeTabContents() as? KMMainDocument {
  916. if (document.isHome) {
  917. document.homeViewController?.menuItemClick_mergePDF(sender)
  918. } else {
  919. document.mainViewController?.menuItemClick_mergePDF(sender)
  920. }
  921. }
  922. }
  923. func menuItemClick_Compress(_ sender: Any) {
  924. if (self.canResponseDocumentAction() == false) {
  925. return
  926. }
  927. if let document = self.browser.activeTabContents() as? KMMainDocument {
  928. if (document.isHome) {
  929. document.homeViewController?.menuItemClick_Compress(sender)
  930. } else {
  931. document.mainViewController?.menuItemClick_Compress(sender)
  932. }
  933. }
  934. }
  935. func menuItemAction_ConvertToWord(_ sender: Any) {
  936. if (self.canResponseDocumentAction() == false) {
  937. return
  938. }
  939. if let document = self.browser.activeTabContents() as? KMMainDocument {
  940. if (document.isHome) {
  941. document.homeViewController?.menuItemAction_ConvertToWord(sender)
  942. } else {
  943. document.mainViewController?.menuItemAction_ConvertToWord(sender)
  944. }
  945. }
  946. }
  947. func menuItemAction_ConvertToExcel(_ sender: Any) {
  948. if (self.canResponseDocumentAction() == false) {
  949. return
  950. }
  951. if let document = self.browser.activeTabContents() as? KMMainDocument {
  952. if (document.isHome) {
  953. document.homeViewController?.menuItemAction_ConvertToExcel(sender)
  954. } else {
  955. document.mainViewController?.menuItemAction_ConvertToExcel(sender)
  956. }
  957. }
  958. }
  959. func menuItemAction_ConvertToPPT(_ sender: Any) {
  960. if (self.canResponseDocumentAction() == false) {
  961. return
  962. }
  963. if let document = self.browser.activeTabContents() as? KMMainDocument {
  964. if (document.isHome) {
  965. document.homeViewController?.menuItemAction_ConvertToPPT(sender)
  966. } else {
  967. document.mainViewController?.menuItemAction_ConvertToPPT(sender)
  968. }
  969. }
  970. }
  971. func menuItemAction_ConvertToRTF(_ sender: Any) {
  972. if (self.canResponseDocumentAction() == false) {
  973. return
  974. }
  975. if let document = self.browser.activeTabContents() as? KMMainDocument {
  976. if (document.isHome) {
  977. document.homeViewController?.menuItemAction_ConvertToRTF(sender)
  978. } else {
  979. document.mainViewController?.menuItemAction_ConvertToRTF(sender)
  980. }
  981. }
  982. }
  983. func menuItemAction_ConvertToHTML(_ sender: Any) {
  984. if (self.canResponseDocumentAction() == false) {
  985. return
  986. }
  987. if let document = self.browser.activeTabContents() as? KMMainDocument {
  988. if (document.isHome) {
  989. document.homeViewController?.menuItemAction_ConvertToHTML(sender)
  990. } else {
  991. document.mainViewController?.menuItemAction_ConvertToHTML(sender)
  992. }
  993. }
  994. }
  995. func menuItemAction_ConvertToText(_ sender: Any) {
  996. if (self.canResponseDocumentAction() == false) {
  997. return
  998. }
  999. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1000. if (document.isHome) {
  1001. document.homeViewController?.menuItemAction_ConvertToText(sender)
  1002. } else {
  1003. document.mainViewController?.menuItemAction_ConvertToText(sender)
  1004. }
  1005. }
  1006. }
  1007. func menuItemAction_ConvertToCSV(_ sender: Any) {
  1008. if (self.canResponseDocumentAction() == false) {
  1009. return
  1010. }
  1011. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1012. if (document.isHome) {
  1013. document.homeViewController?.menuItemAction_ConvertToCSV(sender)
  1014. } else {
  1015. document.mainViewController?.menuItemAction_ConvertToCSV(sender)
  1016. }
  1017. }
  1018. }
  1019. func menuItemAction_ConvertToImage(_ sender: Any) {
  1020. if (self.canResponseDocumentAction() == false) {
  1021. return
  1022. }
  1023. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1024. if (document.isHome) {
  1025. document.homeViewController?.menuItemAction_ConvertToImage(sender)
  1026. } else {
  1027. document.mainViewController?.menuItemAction_ConvertToImage(sender)
  1028. }
  1029. }
  1030. }
  1031. func menuItemClick_SettingPassword(_ sender: Any) {
  1032. if (self.canResponseDocumentAction() == false) {
  1033. return
  1034. }
  1035. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1036. if (document.isHome) {
  1037. document.homeViewController?.menuItemClick_SettingPassword(sender)
  1038. } else {
  1039. document.mainViewController?.menuItemClick_SettingPassword(sender)
  1040. }
  1041. }
  1042. }
  1043. func menuItemClick_RemovePassword(_ sender: Any) {
  1044. if (self.canResponseDocumentAction() == false) {
  1045. return
  1046. }
  1047. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1048. document.mainViewController?.menuItemClick_RemovePassword(sender)
  1049. }
  1050. }
  1051. func menuItemAction_closeWindow(_ sender: Any) {
  1052. if (self.canResponseDocumentAction() == false) {
  1053. return
  1054. }
  1055. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1056. if (document.isHome) {
  1057. document.homeViewController?.menuItemAction_closeWindow(sender)
  1058. } else {
  1059. document.mainViewController?.menuItemAction_closeWindow(sender)
  1060. }
  1061. }
  1062. }
  1063. func menuItemAction_closeAllWindows(_ sender: Any) {
  1064. if (self.canResponseDocumentAction() == false) {
  1065. return
  1066. }
  1067. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1068. if (document.isHome) {
  1069. document.homeViewController?.menuItemAction_closeAllWindows(sender)
  1070. } else {
  1071. document.mainViewController?.menuItemAction_closeAllWindows(sender)
  1072. }
  1073. }
  1074. }
  1075. func menuItemAction_closeTagPage(_ sender: Any) {
  1076. if (self.canResponseDocumentAction() == false) {
  1077. return
  1078. }
  1079. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1080. document.mainViewController?.menuItemAction_closeTagPage(sender)
  1081. }
  1082. }
  1083. func menuItemAction_showInFinder(_ sender: Any) {
  1084. if (self.canResponseDocumentAction() == false) {
  1085. return
  1086. }
  1087. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1088. document.mainViewController?.menuItemAction_showInFinder(sender)
  1089. }
  1090. }
  1091. func menuItemAction_property(_ sender: Any) {
  1092. if (self.canResponseDocumentAction() == false) {
  1093. return
  1094. }
  1095. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1096. document.mainViewController?.menuItemAction_property(sender)
  1097. }
  1098. }
  1099. func menuItemAction_print(_ sender: Any) {
  1100. if (self.canResponseDocumentAction() == false) {
  1101. return
  1102. }
  1103. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1104. document.mainViewController?.menuItemAction_print(sender)
  1105. }
  1106. }
  1107. func isEncapsulatedPostScriptData(_ data: Data) -> Bool {
  1108. let epsHeaderData = Data(bytes: [69, 80, 83, 70, 45], count: 5)
  1109. let rg: Range = (14..<20)
  1110. return (data.count >= 20 && (data.range(of: epsHeaderData, options: .anchored, in: rg) != nil))
  1111. }
  1112. func convertTIFFDataToPDF(_ tiffData: Data) -> Data? {
  1113. guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil }
  1114. let pdfData = NSMutableData(capacity: tiffData.count)
  1115. let consumer = CGDataConsumer(data: pdfData! as CFMutableData)!
  1116. var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
  1117. let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil)
  1118. ctxt!.beginPDFPage(nil)
  1119. ctxt!.draw(cgImage, in: rect)
  1120. ctxt!.endPDFPage()
  1121. ctxt!.flush()
  1122. return pdfData as? Data
  1123. }
  1124. func openDocumentWithImageFromPasteboard(_ pboard: NSPasteboard, error outError: AutoreleasingUnsafeMutablePointer<NSError?>?) -> Any? {
  1125. var document: CPDFDocument? = nil
  1126. var data: Data? = nil
  1127. if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.pdf.rawValue]) {
  1128. // pboard.types
  1129. data = pboard.data(forType: NSPasteboard.PasteboardType.pdf)
  1130. } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.postScript.rawValue]) {
  1131. // pboard.types
  1132. data = pboard.data(forType: NSPasteboard.PasteboardType.postScript)
  1133. } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.tiff.rawValue]) {
  1134. // pboard.types
  1135. data = convertTIFFDataToPDF(pboard.data(forType: NSPasteboard.PasteboardType.tiff) ?? Data())
  1136. } else {
  1137. let images = pboard.readObjects(forClasses: [NSImage.self], options: [:])
  1138. let strings = pboard.readObjects(forClasses: [NSAttributedString.self], options: [:])
  1139. if images?.count ?? 0 > 0 {
  1140. data = convertTIFFDataToPDF((images![0] as AnyObject).tiffRepresentation!)
  1141. } else if strings?.count ?? 0 > 0 {
  1142. data = KMOCTool.convertStringsToPDF(withString: strings ?? [""]) // convertStringsToPDF(strings!)
  1143. }
  1144. }
  1145. if let data = data {
  1146. _ = NSDocumentController.shared
  1147. document = CPDFDocument(data: data)
  1148. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1149. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1150. let filePath = savePath.deletingLastPathComponent
  1151. if FileManager.default.fileExists(atPath: filePath) == false {
  1152. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1153. }
  1154. document?.write(to: URL(fileURLWithPath: savePath))
  1155. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1156. if error != nil {
  1157. NSApp.presentError(error!)
  1158. } else {
  1159. if document is KMMainDocument {
  1160. let newDocument = document
  1161. (newDocument as! KMMainDocument).isNewCreated = true
  1162. }
  1163. }
  1164. }
  1165. } else if let outError = outError {
  1166. outError.pointee = NSError(domain: "SKDocumentErrorDomain", code: 3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Unable to load data from clipboard", comment: "Error description")])
  1167. }
  1168. return document
  1169. }
  1170. func openDocument(withURLFromPasteboard pboard: NSPasteboard, showNotes: Bool, error outError: inout NSError?) -> Any? {
  1171. let theURLs = NSURL.readURLs(from: pboard)
  1172. let url = theURLs?.count ?? 0 > 0 ? theURLs?[0] : nil
  1173. let theURL: NSURL? = url as? NSURL
  1174. var documentC = NSDocumentController.shared
  1175. var document: NSDocument? = nil
  1176. if (theURL as AnyObject).isFileURL == true {
  1177. var _: NSError? = nil
  1178. let type = try? documentC.typeForContents(of: theURL as! URL)//ForContents(ofURL: theURL, error: &error)
  1179. if showNotes == false || NSDocument.readableTypes.contains(type ?? "") {
  1180. documentC.openDocument(withContentsOf: theURL as! URL, display: true, completionHandler: { resultDocument, success, err in
  1181. document = resultDocument
  1182. })//openDocument(withContentsOfURL: theURL, display: true, error: &outError)
  1183. } else if NSDocument.readableTypes.contains(type ?? "") {
  1184. for doc in documentC.documents {
  1185. let sel = NSSelectorFromString("sourceFileURL")
  1186. if doc.responds(to: sel) && doc.fileURL == theURL as? URL {
  1187. document = doc
  1188. break
  1189. }
  1190. }
  1191. if let document: NSDocument = document {
  1192. document.showWindows()
  1193. } else {
  1194. var data: Data? = nil
  1195. // if NSWorkspace.shared.type(type ?? "", conformsToType: KMPDFBundleDocumentType) {
  1196. // let skimFileURL = FileManager.default.exte
  1197. // data = skimFileURL != nil ? try? Data(contentsOf: skimFileURL!) : nil
  1198. // } else {
  1199. // data = SKNExtendedAttributeManager.shared().extendedAttributeNamed(SKIM_NOTES_KEY, atPath: theURL.path, traverseLink: true, error: &error)
  1200. // }
  1201. // let documentNew: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  1202. if let document = try? documentC.makeUntitledDocument(ofType: KMNotesDocumentType) {
  1203. document.fileURL = URL(fileURLWithPath: theURL?.path ?? "")
  1204. // if data == nil || document.read(fromData: data!, ofType: SKNotesDocumentType, error: &error) {
  1205. documentC.addDocument(document)
  1206. document.makeWindowControllers()
  1207. document.showWindows()
  1208. // } else {
  1209. // document = nil
  1210. // if let error = error {
  1211. // outError = error
  1212. // }
  1213. // }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. // else if showNotes == false && theURL != nil {
  1219. // document = SKDownloadController.shared().addDownload(for: theURL!)
  1220. // } else if outError != nil {
  1221. // outError = NSError.readPasteboardError(withLocalizedDescription: NSLocalizedString("Unable to load data from clipboard", comment: "Error description"))
  1222. // }
  1223. return document
  1224. }
  1225. @IBAction func newDocumentFromClipboard(_ sender: Any?) {
  1226. KMPrint("newDocumentFromClipboard")
  1227. var error: NSError?
  1228. let pboard = NSPasteboard.general
  1229. var document = openDocumentWithImageFromPasteboard(pboard, error: &error)
  1230. if document == nil{
  1231. document = openDocument(withURLFromPasteboard: pboard, showNotes: false, error: &error)
  1232. }
  1233. }
  1234. @IBAction func newDocumentFromImage(_ sender: Any?) {
  1235. KMPrint("importFromWebPage")
  1236. createPDFFromImage()
  1237. }
  1238. @IBAction func importFromWebPage(_ sender: Any?) {
  1239. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  1240. if (document.isHome) {
  1241. document.homeViewController?.importFromWebPage()
  1242. } else {
  1243. self.homeVC = KMHomeViewController()
  1244. self.homeVC?.importFromWebPage()
  1245. }
  1246. }
  1247. @IBAction func screenShot_SelectArea(_ sender: Any?) {
  1248. KMScreenShotHandler.beginScreenshot_SelectRectWithCompleteHandler { ima in
  1249. if let image = ima {
  1250. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1251. let folderPath = self.createBaseFoldPath()
  1252. let savePathOld = folderPath + "/screenShot.pdf"
  1253. var savePath = self.filePathCheck(path: savePathOld)
  1254. let newDocument = CPDFDocument()
  1255. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1256. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1257. if writeSuccess ?? false {
  1258. if self.checkOpenNewDocument(path: savePath) {
  1259. self.savePdf(savePath)
  1260. }
  1261. // try? FileManager.default.removeItem(atPath: savePath)
  1262. } else {
  1263. }
  1264. }
  1265. }
  1266. }
  1267. }
  1268. func checkOpenNewDocument(path: String) -> Bool {
  1269. if !IAPProductsManager.default().isAvailableAllFunction() && self.browser.tabCount() > 1 {
  1270. let preferenceNoteShow = UserDefaults.standard.bool(forKey: KMTabbingHintShowFlag)
  1271. if preferenceNoteShow {
  1272. menuItemAction_newTagPageToNewWindow("")
  1273. } else {
  1274. if !KMDataManager.default.isTabbingWin{
  1275. KMDataManager.default.isTabbingWin = true
  1276. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  1277. tabbingWin.selectCallBack = {[weak self] continueOrNot in
  1278. KMDataManager.default.isTabbingWin = false
  1279. if continueOrNot {
  1280. self?.reopenDocumentForNewWindow(savePath: path)
  1281. } else {
  1282. }
  1283. }
  1284. self.km_beginSheet(windowC: tabbingWin)
  1285. }
  1286. }
  1287. return false
  1288. }
  1289. return true
  1290. }
  1291. func reopenDocumentForNewWindow(savePath: String) {
  1292. let browser = KMBrowser.init() as KMBrowser
  1293. browser.windowController = KMBrowserWindowController.init(browser: browser)
  1294. browser.addHomeTabContents()
  1295. browser.windowController.showWindow(self)
  1296. self.savePdf(savePath)
  1297. }
  1298. func filePathCheck(path: String) -> String {
  1299. var i: Int = 0
  1300. let fileURL = URL(fileURLWithPath: path)
  1301. var newPath: String = path
  1302. let fileManager = FileManager.default
  1303. while fileManager.fileExists(atPath: newPath) {
  1304. i += 1
  1305. newPath = fileURL.deletingPathExtension().path
  1306. newPath.append("(\(i))")
  1307. newPath.append(".")
  1308. newPath.append(fileURL.pathExtension)
  1309. }
  1310. return newPath
  1311. }
  1312. @IBAction func screenShot_Window(_ sender: Any?) {
  1313. KMPrint("screenShot_Window")
  1314. KMScreenShotHandler.beginScreenShot_SelectWindowCompleteHandler { ima in
  1315. if let image = ima {
  1316. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1317. let folderPath = self.createBaseFoldPath()
  1318. let savePathOld = folderPath + "/screenShot.pdf"
  1319. var savePath = self.filePathCheck(path: savePathOld)
  1320. let newDocument = CPDFDocument()
  1321. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1322. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1323. if writeSuccess == true {
  1324. if self.checkOpenNewDocument(path: savePath) {
  1325. self.savePdf(savePath)
  1326. }
  1327. // try? FileManager.default.removeItem(atPath: savePath)
  1328. } else {
  1329. }
  1330. }
  1331. }
  1332. }
  1333. }
  1334. @IBAction func screenShot_FullScreenDelay(_ sender: Any?) {
  1335. KMPrint("screenShot_FullScreenDelay")
  1336. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(3) { ima in
  1337. if let image = ima {
  1338. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1339. let folderPath = self.createBaseFoldPath()
  1340. let savePathOld = folderPath + "/screenShot.pdf"
  1341. var savePath = self.filePathCheck(path: savePathOld)
  1342. let newDocument = CPDFDocument()
  1343. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1344. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1345. if writeSuccess == true {
  1346. if self.checkOpenNewDocument(path: savePath) {
  1347. self.savePdf(savePath)
  1348. }
  1349. // try? FileManager.default.removeItem(atPath: savePath)
  1350. } else {
  1351. }
  1352. }
  1353. }
  1354. }
  1355. }
  1356. @IBAction func screenShot_FullScreen(_ sender: Any?) {
  1357. KMPrint("screenShot_FullScreen")
  1358. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(0) { ima in
  1359. if let image = ima {
  1360. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1361. let folderPath = self.createBaseFoldPath()
  1362. let savePathOld = folderPath + "/screenShot.pdf"
  1363. var savePath = self.filePathCheck(path: savePathOld)
  1364. let newDocument = CPDFDocument()
  1365. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1366. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1367. if writeSuccess == true {
  1368. self.savePdf(savePath)
  1369. // try? FileManager.default.removeItem(atPath: savePath)
  1370. } else {
  1371. }
  1372. }
  1373. }
  1374. }
  1375. }
  1376. func createBaseFoldPath() -> String {
  1377. let folderPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last?.appending("/\(Bundle.main.bundleIdentifier!)")
  1378. if (FileManager.default.fileExists(atPath: folderPath!) == false) {
  1379. try?FileManager.default.createDirectory(atPath: folderPath!, withIntermediateDirectories: false)
  1380. }
  1381. return folderPath ?? ""
  1382. }
  1383. }
  1384. // MARK: - KMSystemViewMenuProtocol
  1385. extension KMBrowserWindowController: KMSystemViewMenuProtocol {
  1386. func createPDFFromImage() {
  1387. let openPanel = NSOpenPanel()
  1388. openPanel.allowedFileTypes = KMImageAccessoryController.supportedImageTypes()
  1389. openPanel.allowsMultipleSelection = true
  1390. 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: "")
  1391. openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1392. if response == .OK {
  1393. self.creatPdfFromImg(urls: openPanel.urls)
  1394. }
  1395. }
  1396. }
  1397. func creatPdfFromImg(urls: Array<URL>) {
  1398. // let savePath = self.newDocumentTempSavePath("convertToPDF.pdf")
  1399. let pdf = CPDFDocument()
  1400. // var isSucceed = true
  1401. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1402. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1403. let filePath = savePath.deletingLastPathComponent
  1404. if FileManager.default.fileExists(atPath: filePath) == false {
  1405. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1406. }
  1407. for fileURL in urls {
  1408. if let imag = NSImage(contentsOfFile: fileURL.path ) {
  1409. _ = pdf?.km_insertPage(imag.size, withImage: fileURL.path , at: pdf?.pageCount ?? 0)
  1410. pdf?.write(to: URL(fileURLWithPath: savePath))
  1411. }
  1412. }
  1413. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1414. if error != nil {
  1415. NSApp.presentError(error!)
  1416. } else {
  1417. if document is KMMainDocument {
  1418. let newDocument = document
  1419. (newDocument as! KMMainDocument).isNewCreated = true
  1420. }
  1421. }
  1422. // self.homeContentView?.reloadData()
  1423. }
  1424. }
  1425. func createPdfWithParam() {
  1426. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1427. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1428. let filePath = savePath.deletingLastPathComponent
  1429. if FileManager.default.fileExists(atPath: filePath) == false {
  1430. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1431. }
  1432. let pdfDocument = CPDFDocument()
  1433. pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
  1434. pdfDocument?.write(to: URL(fileURLWithPath: savePath))
  1435. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1436. if error != nil {
  1437. NSApp.presentError(error!)
  1438. } else {
  1439. if document is KMMainDocument {
  1440. let newDocument = document
  1441. (newDocument as! KMMainDocument).isNewCreated = true
  1442. }
  1443. }
  1444. // self.homeContentView?.reloadData()
  1445. }
  1446. }
  1447. func savePdf(_ filePath: String) -> Void {
  1448. let docVc = KMDocumentController.shared
  1449. docVc.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
  1450. if error != nil {
  1451. NSApp.presentError(error!)
  1452. } else {
  1453. if document is KMMainDocument {
  1454. let newDocument = document
  1455. (newDocument as! KMMainDocument).isNewCreated = true
  1456. }
  1457. }
  1458. }
  1459. }
  1460. func newDocumentTempSavePath(_ fileName: String) -> String {
  1461. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  1462. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  1463. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  1464. return append2
  1465. }
  1466. func fetchUniquePath(_ originalPath: String) -> String {
  1467. var path = originalPath
  1468. let dManager = FileManager.default
  1469. if !dManager.fileExists(atPath: path) {
  1470. if path.extension.count < 1 {
  1471. path = path.stringByAppendingPathExtension("pdf")
  1472. }
  1473. return path
  1474. } else {
  1475. let originalFullFileName = path.lastPathComponent
  1476. let originalFileName = path.lastPathComponent.deletingPathExtension
  1477. let originalExtension = path.extension
  1478. let startIndex: Int = 0
  1479. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  1480. let fileLocatePath = originalPath.substring(to: endIndex)
  1481. var i = 1
  1482. while (1 != 0) {
  1483. var newName = String(format: "%@%ld", originalFileName, i)
  1484. newName = newName.stringByAppendingPathExtension(originalExtension)
  1485. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  1486. if !dManager.fileExists(atPath: newPath) {
  1487. return newPath
  1488. } else {
  1489. i+=1
  1490. continue
  1491. }
  1492. }
  1493. }
  1494. }
  1495. func menuItemAction_adjustWidth(_ sender: Any) {
  1496. if (self.canResponseDocumentAction() == false) {
  1497. return
  1498. }
  1499. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1500. document.mainViewController?.menuItemAction_adjustWidth(sender)
  1501. }
  1502. }
  1503. func menuItemAction_adjustPage(_ sender: Any) {
  1504. if (self.canResponseDocumentAction() == false) {
  1505. return
  1506. }
  1507. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1508. document.mainViewController?.menuItemAction_adjustPage(sender)
  1509. }
  1510. }
  1511. func menuItemAction_size(_ sender: Any) {
  1512. if (self.canResponseDocumentAction() == false) {
  1513. return
  1514. }
  1515. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1516. document.mainViewController?.menuItemAction_size(sender)
  1517. }
  1518. }
  1519. func menuItemAction_zoomOut(_ sender: Any) {
  1520. if (self.canResponseDocumentAction() == false) {
  1521. return
  1522. }
  1523. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1524. document.mainViewController?.menuItemAction_zoomOut(sender)
  1525. }
  1526. }
  1527. func menuItemAction_zoomIn(_ sender: Any) {
  1528. if (self.canResponseDocumentAction() == false) {
  1529. return
  1530. }
  1531. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1532. document.mainViewController?.menuItemAction_zoomIn(sender)
  1533. }
  1534. }
  1535. func menuItemAction_singlePage(_ sender: Any) {
  1536. if (self.canResponseDocumentAction() == false) {
  1537. return
  1538. }
  1539. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1540. document.mainViewController?.menuItemAction_singlePage(sender)
  1541. }
  1542. }
  1543. func menuItemAction_singlePageContinue(_ sender: Any) {
  1544. if (self.canResponseDocumentAction() == false) {
  1545. return
  1546. }
  1547. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1548. document.mainViewController?.menuItemAction_singlePageContinue(sender)
  1549. }
  1550. }
  1551. func menuItemAction_doublePage(_ sender: Any) {
  1552. if (self.canResponseDocumentAction() == false) {
  1553. return
  1554. }
  1555. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1556. document.mainViewController?.menuItemAction_doublePage(sender)
  1557. }
  1558. }
  1559. func menuItemAction_doublePageContinue(_ sender: Any) {
  1560. if (self.canResponseDocumentAction() == false) {
  1561. return
  1562. }
  1563. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1564. document.mainViewController?.menuItemAction_doublePageContinue(sender)
  1565. }
  1566. }
  1567. func menuItemAction_bookMode(_ sender: Any) {
  1568. if (self.canResponseDocumentAction() == false) {
  1569. return
  1570. }
  1571. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1572. document.mainViewController?.menuItemAction_bookMode(sender)
  1573. }
  1574. }
  1575. func menuItemAction_readMode(_ sender: Any) {
  1576. if (self.canResponseDocumentAction() == false) {
  1577. return
  1578. }
  1579. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1580. document.mainViewController?.menuItemAction_readMode(sender)
  1581. }
  1582. }
  1583. func menuItemAction_showSplitPage(_ sender: Any) {
  1584. if (self.canResponseDocumentAction() == false) {
  1585. return
  1586. }
  1587. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1588. document.mainViewController?.menuItemAction_showSplitPage(sender)
  1589. }
  1590. }
  1591. func menuItemAction_rotateLeft(_ sender: Any) {
  1592. if (self.canResponseDocumentAction() == false) {
  1593. return
  1594. }
  1595. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1596. document.mainViewController?.menuItemAction_rotateLeft(sender)
  1597. }
  1598. }
  1599. func menuItemAction_rotateRight(_ sender: Any) {
  1600. if (self.canResponseDocumentAction() == false) {
  1601. return
  1602. }
  1603. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1604. document.mainViewController?.menuItemAction_rotateRight(sender)
  1605. }
  1606. }
  1607. func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  1608. if (self.canResponseDocumentAction() == false) {
  1609. return
  1610. }
  1611. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1612. document.mainViewController?.menuItemAction_rotateAllPageLeft(sender)
  1613. }
  1614. }
  1615. func menuItemAction_rotateAllPageRight(_ sender: Any) {
  1616. if (self.canResponseDocumentAction() == false) {
  1617. return
  1618. }
  1619. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1620. document.mainViewController?.menuItemAction_rotateAllPageRight(sender)
  1621. }
  1622. }
  1623. func menuItemAction_view_readMode(_ sender: Any) {
  1624. if (self.canResponseDocumentAction() == false) {
  1625. return
  1626. }
  1627. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1628. document.mainViewController?.menuItemAction_view_readMode(sender)
  1629. }
  1630. }
  1631. func menuItemAction_hiddenLeftSide(_ sender: Any) {
  1632. if (self.canResponseDocumentAction() == false) {
  1633. return
  1634. }
  1635. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1636. document.mainViewController?.menuItemAction_hiddenLeftSide(sender)
  1637. }
  1638. }
  1639. func menuItemAction_hiddenRightSide(_ sender: Any) {
  1640. if (self.canResponseDocumentAction() == false) {
  1641. return
  1642. }
  1643. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1644. document.mainViewController?.menuItemAction_hiddenRightSide(sender)
  1645. }
  1646. }
  1647. func menuItemAction_thumai(_ sender: Any) {
  1648. if (self.canResponseDocumentAction() == false) {
  1649. return
  1650. }
  1651. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1652. document.mainViewController?.menuItemAction_thumai(sender)
  1653. }
  1654. }
  1655. func menuItemAction_outline(_ sender: Any) {
  1656. if (self.canResponseDocumentAction() == false) {
  1657. return
  1658. }
  1659. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1660. document.mainViewController?.menuItemAction_outline(sender)
  1661. }
  1662. }
  1663. func menuItemAction_bookmark(_ sender: Any) {
  1664. if (self.canResponseDocumentAction() == false) {
  1665. return
  1666. }
  1667. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1668. document.mainViewController?.menuItemAction_bookmark(sender)
  1669. }
  1670. }
  1671. func menuItemAction_annotation(_ sender: Any) {
  1672. if (self.canResponseDocumentAction() == false) {
  1673. return
  1674. }
  1675. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1676. document.mainViewController?.menuItemAction_annotation(sender)
  1677. }
  1678. }
  1679. func menuItemAction_search(_ sender: Any) {
  1680. if (self.canResponseDocumentAction() == false) {
  1681. return
  1682. }
  1683. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1684. document.mainViewController?.menuItemAction_search(sender)
  1685. }
  1686. }
  1687. func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  1688. if (self.canResponseDocumentAction() == false) {
  1689. return
  1690. }
  1691. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1692. document.mainViewController?.menuItemAction_hiddenPageIndicator(sender)
  1693. }
  1694. }
  1695. @IBAction func togglePresentation(_ sender: Any?) {
  1696. if (self.canResponseDocumentAction() == false) {
  1697. return
  1698. }
  1699. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1700. document.mainViewController?.togglePresentation(sender)
  1701. }
  1702. }
  1703. @IBAction func chooseTransition(_ sender: Any?) {
  1704. if (self.canResponseDocumentAction() == false) {
  1705. return
  1706. }
  1707. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1708. document.mainViewController?.chooseTransition(sender)
  1709. }
  1710. }
  1711. @IBAction func toggleAutoFlow(_ sender: Any?) {
  1712. if (self.canResponseDocumentAction() == false) {
  1713. return
  1714. }
  1715. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1716. document.mainViewController?.toggleAutoFlow(sender)
  1717. }
  1718. }
  1719. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  1720. if (self.canResponseDocumentAction() == false) {
  1721. return
  1722. }
  1723. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1724. document.mainViewController?.chooseAutoFlowSetting(sender)
  1725. }
  1726. }
  1727. @IBAction func toggleReadingBar(_ sender: Any?) {
  1728. if (self.canResponseDocumentAction() == false) {
  1729. return
  1730. }
  1731. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1732. document.mainViewController?.toggleReadingBar(sender)
  1733. }
  1734. }
  1735. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  1736. if (self.canResponseDocumentAction() == false) {
  1737. return
  1738. }
  1739. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1740. document.mainViewController?.toggleLeftSidePane(sender)
  1741. }
  1742. }
  1743. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  1744. if (self.canResponseDocumentAction() == false) {
  1745. return
  1746. }
  1747. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1748. document.mainViewController?.changeLeftSidePaneState(sender)
  1749. }
  1750. }
  1751. @IBAction func splitViewAction(_ sender: Any?) {
  1752. if (self.canResponseDocumentAction() == false) {
  1753. return
  1754. }
  1755. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1756. document.mainViewController?.splitViewAction(sender)
  1757. }
  1758. }
  1759. @IBAction func themesColor(_ sender: Any?) {
  1760. if (self.canResponseDocumentAction() == false) {
  1761. return
  1762. }
  1763. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1764. document.mainViewController?.themesColor(sender)
  1765. }
  1766. }
  1767. @IBAction func toggleStatusBar(_ sender: Any?) {
  1768. if (self.canResponseDocumentAction() == false) {
  1769. return
  1770. }
  1771. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1772. document.mainViewController?.toggleStatusBar(sender)
  1773. }
  1774. }
  1775. @IBAction func toggleToolbarShow(_ sender: Any?) {
  1776. if (self.canResponseDocumentAction() == false) {
  1777. return
  1778. }
  1779. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1780. document.mainViewController?.toggleToolbarShow(sender)
  1781. }
  1782. }
  1783. }
  1784. // MARK: - KMSystemEditMenuProtocol
  1785. extension KMBrowserWindowController: KMSystemEditMenuProtocol {
  1786. func menuItemAction_find(_ sender: Any) {
  1787. if (self.canResponseDocumentAction() == false) {
  1788. return
  1789. }
  1790. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1791. document.mainViewController?.menuItemAction_find(sender)
  1792. }
  1793. }
  1794. @IBAction func changeFont(_ sender: Any?) {
  1795. KMPrint("changeFont ...")
  1796. }
  1797. }
  1798. // MARK: - Annotation Menu
  1799. extension KMBrowserWindowController {
  1800. @IBAction func menuItemAction_highlight(_ sender: Any) {
  1801. if (self.canResponseDocumentAction() == false) {
  1802. return
  1803. }
  1804. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1805. document.mainViewController?.menuItemAction_highlight(sender)
  1806. }
  1807. }
  1808. @IBAction func menuItemAction_underline(_ sender: Any) {
  1809. if (self.canResponseDocumentAction() == false) {
  1810. return
  1811. }
  1812. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1813. document.mainViewController?.menuItemAction_underline(sender)
  1814. }
  1815. }
  1816. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  1817. if (self.canResponseDocumentAction() == false) {
  1818. return
  1819. }
  1820. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1821. document.mainViewController?.menuItemAction_deleteLine(sender)
  1822. }
  1823. }
  1824. @IBAction func menuItemAction_freehand(_ sender: Any) {
  1825. if (self.canResponseDocumentAction() == false) {
  1826. return
  1827. }
  1828. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1829. document.mainViewController?.menuItemAction_freehand(sender)
  1830. }
  1831. }
  1832. @IBAction func menuItemAction_text(_ sender: Any) {
  1833. if (self.canResponseDocumentAction() == false) {
  1834. return
  1835. }
  1836. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1837. document.mainViewController?.menuItemAction_text(sender)
  1838. }
  1839. }
  1840. @IBAction func menuItemAction_note(_ sender: Any) {
  1841. if (self.canResponseDocumentAction() == false) {
  1842. return
  1843. }
  1844. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1845. document.mainViewController?.menuItemAction_note(sender)
  1846. }
  1847. }
  1848. @IBAction func menuItemAction_squre(_ sender: Any) {
  1849. if (self.canResponseDocumentAction() == false) {
  1850. return
  1851. }
  1852. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1853. document.mainViewController?.menuItemAction_squre(sender)
  1854. }
  1855. }
  1856. @IBAction func menuItemAction_circle(_ sender: Any) {
  1857. if (self.canResponseDocumentAction() == false) {
  1858. return
  1859. }
  1860. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1861. document.mainViewController?.menuItemAction_circle(sender)
  1862. }
  1863. }
  1864. @IBAction func menuItemAction_arrow(_ sender: Any) {
  1865. if (self.canResponseDocumentAction() == false) {
  1866. return
  1867. }
  1868. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1869. document.mainViewController?.menuItemAction_arrow(sender)
  1870. }
  1871. }
  1872. @IBAction func menuItemAction_line(_ sender: Any) {
  1873. if (self.canResponseDocumentAction() == false) {
  1874. return
  1875. }
  1876. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1877. document.mainViewController?.menuItemAction_line(sender)
  1878. }
  1879. }
  1880. // link
  1881. @IBAction func menuItemAction_link(_ sender: Any) {
  1882. if (self.canResponseDocumentAction() == false) {
  1883. return
  1884. }
  1885. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1886. document.mainViewController?.menuItemAction_link(sender)
  1887. }
  1888. }
  1889. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  1890. if (self.canResponseDocumentAction() == false) {
  1891. return
  1892. }
  1893. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1894. document.mainViewController?.menuItemAction_linkPage(sender)
  1895. }
  1896. }
  1897. // stamp
  1898. @IBAction func menuItemAction_stamp(_ sender: Any) {
  1899. if (self.canResponseDocumentAction() == false) {
  1900. return
  1901. }
  1902. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1903. document.mainViewController?.menuItemAction_stamp(sender)
  1904. }
  1905. }
  1906. @IBAction func notesLink(_ sender: Any?) {
  1907. if (self.canResponseDocumentAction() == false) {
  1908. return
  1909. }
  1910. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1911. document.mainViewController?.notesLink(sender)
  1912. }
  1913. }
  1914. @IBAction func notesStamp(_ sender: Any?) {
  1915. if (self.canResponseDocumentAction() == false) {
  1916. return
  1917. }
  1918. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1919. document.mainViewController?.notesStamp(sender)
  1920. }
  1921. }
  1922. @IBAction func menuItemAction_signure(_ sender: Any) {
  1923. if (self.canResponseDocumentAction() == false) {
  1924. return
  1925. }
  1926. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1927. document.mainViewController?.menuItemAction_signure(sender)
  1928. }
  1929. }
  1930. @IBAction func image(_ sender: Any?) {
  1931. if (self.canResponseDocumentAction() == false) {
  1932. return
  1933. }
  1934. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1935. document.mainViewController?.image(sender)
  1936. }
  1937. }
  1938. @IBAction func table(_ sender: Any?) {
  1939. if (self.canResponseDocumentAction() == false) {
  1940. return
  1941. }
  1942. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1943. document.mainViewController?.table(sender)
  1944. }
  1945. }
  1946. @IBAction func addForm(_ sender: Any?) {
  1947. if (self.canResponseDocumentAction() == false) {
  1948. return
  1949. }
  1950. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1951. document.mainViewController?.addForm(sender)
  1952. }
  1953. }
  1954. @IBAction func editNote(_ sender: Any?) {
  1955. if (self.canResponseDocumentAction() == false) {
  1956. return
  1957. }
  1958. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1959. document.mainViewController?.editNote(sender)
  1960. }
  1961. }
  1962. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  1963. if (self.canResponseDocumentAction() == false) {
  1964. return
  1965. }
  1966. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1967. document.mainViewController?.menuItemAction_hiddenAllAnnotation(sender)
  1968. }
  1969. }
  1970. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  1971. if (self.canResponseDocumentAction() == false) {
  1972. return
  1973. }
  1974. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1975. document.mainViewController?.menuItemAction_clearAllAnnotation(sender)
  1976. }
  1977. }
  1978. }
  1979. // MARK: - KMSystemPDFMenuProtocol
  1980. extension KMBrowserWindowController: KMSystemPDFMenuProtocol {
  1981. @IBAction func changeDisplayMode(_ sender: Any?) {
  1982. if (self.canResponseDocumentAction() == false) {
  1983. return
  1984. }
  1985. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1986. document.mainViewController?.changeDisplayMode(sender)
  1987. }
  1988. }
  1989. @IBAction func toggleDisplayAsBook(_ sender: Any?) {
  1990. if (self.canResponseDocumentAction() == false) {
  1991. return
  1992. }
  1993. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1994. document.mainViewController?.toggleDisplayAsBook(sender)
  1995. }
  1996. }
  1997. @IBAction func toggleDisplayPageBreaks(_ sender: Any?) {
  1998. if (self.canResponseDocumentAction() == false) {
  1999. return
  2000. }
  2001. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2002. document.mainViewController?.toggleDisplayPageBreaks(sender)
  2003. }
  2004. }
  2005. @IBAction func changeDisplayBox(_ sender: Any?) {
  2006. if (self.canResponseDocumentAction() == false) {
  2007. return
  2008. }
  2009. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2010. document.mainViewController?.changeDisplayBox(sender)
  2011. }
  2012. }
  2013. @IBAction func toggleAutoScale(_ sender: Any?) {
  2014. if (self.canResponseDocumentAction() == false) {
  2015. return
  2016. }
  2017. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2018. document.mainViewController?.toggleAutoScale(sender)
  2019. }
  2020. }
  2021. @IBAction func doZoomToActualSize(_ sender: Any?) {
  2022. if (self.canResponseDocumentAction() == false) {
  2023. return
  2024. }
  2025. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2026. document.mainViewController?.doZoomToActualSize(sender)
  2027. }
  2028. }
  2029. @IBAction func doZoomToFit(_ sender: Any?) {
  2030. if (self.canResponseDocumentAction() == false) {
  2031. return
  2032. }
  2033. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2034. document.mainViewController?.doZoomToFit(sender)
  2035. }
  2036. }
  2037. @IBAction func doZoomIn(_ sender: Any?) {
  2038. if (self.canResponseDocumentAction() == false) {
  2039. return
  2040. }
  2041. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2042. document.mainViewController?.doZoomIn(sender)
  2043. }
  2044. }
  2045. @IBAction func doZoomOut(_ sender: Any?) {
  2046. if (self.canResponseDocumentAction() == false) {
  2047. return
  2048. }
  2049. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2050. document.mainViewController?.doZoomOut(sender)
  2051. }
  2052. }
  2053. @IBAction func doMarqueeZoomTool(_ sender: Any?) {
  2054. if (self.canResponseDocumentAction() == false) {
  2055. return
  2056. }
  2057. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2058. document.mainViewController?.doMarqueeZoomTool(sender)
  2059. }
  2060. }
  2061. @IBAction func highlightFormFiled(_ sender: Any?) {
  2062. if (self.canResponseDocumentAction() == false) {
  2063. return
  2064. }
  2065. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2066. document.mainViewController?.highlightFormFiled(sender)
  2067. }
  2068. }
  2069. @IBAction func highlightLinks(_ sender: Any?) {
  2070. if (self.canResponseDocumentAction() == false) {
  2071. return
  2072. }
  2073. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2074. document.mainViewController?.highlightLinks(sender)
  2075. }
  2076. }
  2077. @IBAction func resetForm(_ sender: Any?) {
  2078. if (self.canResponseDocumentAction() == false) {
  2079. return
  2080. }
  2081. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2082. document.mainViewController?.resetForm(sender)
  2083. }
  2084. }
  2085. @IBAction func removeAllAnnotation(_ sender: Any?) {
  2086. if (self.canResponseDocumentAction() == false) {
  2087. return
  2088. }
  2089. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2090. document.mainViewController?.removeAllAnnotation(sender)
  2091. }
  2092. }
  2093. @IBAction func savePDFSettingToDefaults(_ sender: Any?) {
  2094. if (self.canResponseDocumentAction() == false) {
  2095. return
  2096. }
  2097. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2098. document.mainViewController?.savePDFSettingToDefaults(sender)
  2099. }
  2100. }
  2101. }
  2102. // MARK: - KMSystemGotoMenuProtocol
  2103. extension KMBrowserWindowController: KMSystemGotoMenuProtocol {
  2104. func menuItemAction_nextPage(_ sender: Any) {
  2105. if (self.canResponseDocumentAction() == false) {
  2106. return
  2107. }
  2108. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2109. document.mainViewController?.menuItemAction_nextPage(sender)
  2110. }
  2111. }
  2112. func menuItemAction_forwardPage(_ sender: Any) {
  2113. if (self.canResponseDocumentAction() == false) {
  2114. return
  2115. }
  2116. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2117. document.mainViewController?.menuItemAction_forwardPage(sender)
  2118. }
  2119. }
  2120. func menuItemAction_firstPage(_ sender: Any) {
  2121. if (self.canResponseDocumentAction() == false) {
  2122. return
  2123. }
  2124. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2125. document.mainViewController?.menuItemAction_firstPage(sender)
  2126. }
  2127. }
  2128. func menuItemAction_lastPage(_ sender: Any) {
  2129. if (self.canResponseDocumentAction() == false) {
  2130. return
  2131. }
  2132. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2133. document.mainViewController?.menuItemAction_lastPage(sender)
  2134. }
  2135. }
  2136. func menuItemAction_forward(_ sender: Any) {
  2137. if (self.canResponseDocumentAction() == false) {
  2138. return
  2139. }
  2140. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2141. document.mainViewController?.menuItemAction_forward(sender)
  2142. }
  2143. }
  2144. func menuItemAction_goback(_ sender: Any) {
  2145. if (self.canResponseDocumentAction() == false) {
  2146. return
  2147. }
  2148. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2149. document.mainViewController?.menuItemAction_goback(sender)
  2150. }
  2151. }
  2152. func menuItemAction_gotoPage(_ sender: Any) {
  2153. if (self.canResponseDocumentAction() == false) {
  2154. return
  2155. }
  2156. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2157. document.mainViewController?.menuItemAction_gotoPage(sender)
  2158. }
  2159. }
  2160. @IBAction func goToMarkedPage(_ sender: Any?) {
  2161. if (self.canResponseDocumentAction() == false) {
  2162. return
  2163. }
  2164. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2165. document.mainViewController?.goToMarkedPage(sender)
  2166. }
  2167. }
  2168. @IBAction func markPage(_ sender: Any?) {
  2169. if (self.canResponseDocumentAction() == false) {
  2170. return
  2171. }
  2172. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2173. document.mainViewController?.markPage(sender)
  2174. }
  2175. }
  2176. }
  2177. // MARK: - KMSystemToolMenuProtocol
  2178. extension KMBrowserWindowController: KMSystemToolMenuProtocol {
  2179. @IBAction func getInfo(_ sender: Any?) {
  2180. if (self.canResponseDocumentAction() == false) {
  2181. return
  2182. }
  2183. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2184. document.mainViewController?.getInfo(sender)
  2185. }
  2186. }
  2187. func orderFrontNotesPanel(_ sender: Any?) {
  2188. let windowC = KMNotesPanelController.shared
  2189. if let win = windowC.window {
  2190. if win.isVisible {
  2191. win.orderOut(sender)
  2192. } else {
  2193. win.orderFront(sender)
  2194. }
  2195. }
  2196. }
  2197. func menuItemAction_scrolTool(_ sender: Any) {
  2198. if (self.canResponseDocumentAction() == false) {
  2199. return
  2200. }
  2201. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2202. document.mainViewController?.menuItemAction_scrolTool(sender)
  2203. }
  2204. }
  2205. func menuItemAction_zoomOutTool(_ sender: Any) {
  2206. if (self.canResponseDocumentAction() == false) {
  2207. return
  2208. }
  2209. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2210. document.mainViewController?.menuItemAction_zoomOutTool(sender)
  2211. }
  2212. }
  2213. func menuItemAction_selectTool(_ sender: Any) {
  2214. if (self.canResponseDocumentAction() == false) {
  2215. return
  2216. }
  2217. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2218. document.mainViewController?.menuItemAction_selectTool(sender)
  2219. }
  2220. }
  2221. @IBAction func changeToolMode(_ sender: Any?) {
  2222. if (self.canResponseDocumentAction() == false) {
  2223. return
  2224. }
  2225. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2226. if (document.isHome) {
  2227. } else {
  2228. document.mainViewController?.changeToolMode(sender)
  2229. }
  2230. }
  2231. }
  2232. @IBAction func compression(_ sender: Any?) {
  2233. if (self.canResponseDocumentAction() == false) {
  2234. return
  2235. }
  2236. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2237. if (document.isHome) {
  2238. } else {
  2239. document.mainViewController?.compression(sender)
  2240. }
  2241. }
  2242. }
  2243. @IBAction func merge(_ sender: Any?) {
  2244. if (self.canResponseDocumentAction() == false) {
  2245. return
  2246. }
  2247. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2248. if (document.isHome) {
  2249. } else {
  2250. document.mainViewController?.merge(sender)
  2251. }
  2252. }
  2253. }
  2254. @IBAction func convert(_ sender: Any?) {
  2255. if (self.canResponseDocumentAction() == false) {
  2256. return
  2257. }
  2258. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2259. if (document.isHome) {
  2260. } else {
  2261. document.mainViewController?.convert(sender)
  2262. }
  2263. }
  2264. }
  2265. @IBAction func imageToPDF(_ sender: Any?) {
  2266. if (self.canResponseDocumentAction() == false) {
  2267. return
  2268. }
  2269. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2270. if (document.isHome) {
  2271. } else {
  2272. document.mainViewController?.imageToPDF(sender)
  2273. }
  2274. }
  2275. }
  2276. @IBAction func ocr(_ sender: Any?) {
  2277. if (self.canResponseDocumentAction() == false) {
  2278. return
  2279. }
  2280. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2281. if (document.isHome) {
  2282. } else {
  2283. document.mainViewController?.ocr(sender)
  2284. }
  2285. }
  2286. }
  2287. @IBAction func extractImage(_ sender: Any?) {
  2288. if (self.canResponseDocumentAction() == false) {
  2289. return
  2290. }
  2291. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2292. if (document.isHome) {
  2293. } else {
  2294. document.mainViewController?.extractImage(sender)
  2295. }
  2296. }
  2297. }
  2298. @IBAction func pageEdit(_ sender: Any?) {
  2299. if (self.canResponseDocumentAction() == false) {
  2300. return
  2301. }
  2302. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2303. if (document.isHome) {
  2304. } else {
  2305. document.mainViewController?.pageEdit(sender)
  2306. }
  2307. }
  2308. }
  2309. @IBAction func textEditAction(_ sender: Any?) {
  2310. if (self.canResponseDocumentAction() == false) {
  2311. return
  2312. }
  2313. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2314. if (document.isHome) {
  2315. } else {
  2316. document.mainViewController?.textEditAction(sender)
  2317. }
  2318. }
  2319. }
  2320. @IBAction func security(_ sender: Any?) {
  2321. if (self.canResponseDocumentAction() == false) {
  2322. return
  2323. }
  2324. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2325. if (document.isHome) {
  2326. } else {
  2327. document.mainViewController?.security(sender)
  2328. }
  2329. }
  2330. }
  2331. @IBAction func decryption(_ sender: Any?) {
  2332. if (self.canResponseDocumentAction() == false) {
  2333. return
  2334. }
  2335. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2336. if (document.isHome) {
  2337. } else {
  2338. document.mainViewController?.decryption(sender)
  2339. }
  2340. }
  2341. }
  2342. @IBAction func cropAll(_ sender: Any?) {
  2343. if (self.canResponseDocumentAction() == false) {
  2344. return
  2345. }
  2346. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2347. if (document.isHome) {
  2348. } else {
  2349. document.mainViewController?.cropAll(sender)
  2350. }
  2351. }
  2352. }
  2353. @IBAction func crop(_ sender: Any?) {
  2354. if (self.canResponseDocumentAction() == false) {
  2355. return
  2356. }
  2357. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2358. if (document.isHome) {
  2359. } else {
  2360. document.mainViewController?.crop(sender)
  2361. }
  2362. }
  2363. }
  2364. @IBAction func autoCropAll(_ sender: Any?) {
  2365. if (self.canResponseDocumentAction() == false) {
  2366. return
  2367. }
  2368. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2369. if (document.isHome) {
  2370. } else {
  2371. document.mainViewController?.autoCropAll(sender)
  2372. }
  2373. }
  2374. }
  2375. @IBAction func smartAutoCropAll(_ sender: Any?) {
  2376. if (self.canResponseDocumentAction() == false) {
  2377. return
  2378. }
  2379. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2380. if (document.isHome) {
  2381. } else {
  2382. document.mainViewController?.smartAutoCropAll(sender)
  2383. }
  2384. }
  2385. }
  2386. @IBAction func cropOptionsModel(_ sender: Any?) {
  2387. if (self.canResponseDocumentAction() == false) {
  2388. return
  2389. }
  2390. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2391. if (document.isHome) {
  2392. } else {
  2393. // document.mainViewController?.cropOptionsModel(sender)
  2394. }
  2395. }
  2396. }
  2397. @IBAction func selectToolModel(_ sender: Any?) {
  2398. if (self.canResponseDocumentAction() == false) {
  2399. return
  2400. }
  2401. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2402. if (document.isHome) {
  2403. } else {
  2404. document.mainViewController?.selectToolModel(sender)
  2405. }
  2406. }
  2407. }
  2408. @IBAction func takeSnapshot(_ sender: Any?) {
  2409. if (self.canResponseDocumentAction() == false) {
  2410. return
  2411. }
  2412. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2413. if (document.isHome) {
  2414. } else {
  2415. document.mainViewController?.takeSnapshot(sender)
  2416. }
  2417. }
  2418. }
  2419. @IBAction func takeSnapshotSelectContent(_ sender: Any?) {
  2420. if (self.canResponseDocumentAction() == false) {
  2421. return
  2422. }
  2423. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2424. if (document.isHome) {
  2425. } else {
  2426. document.mainViewController?.takeSnapshotSelectContent(sender)
  2427. }
  2428. }
  2429. }
  2430. @IBAction func changeAnnotationMode(_ sender: Any?) {
  2431. if (self.canResponseDocumentAction() == false) {
  2432. return
  2433. }
  2434. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2435. if (document.isHome) {
  2436. } else {
  2437. document.mainViewController?.changeAnnotationMode(sender)
  2438. }
  2439. }
  2440. }
  2441. @IBAction func addBackground(_ sender: Any?) {
  2442. if (self.canResponseDocumentAction() == false) {
  2443. return
  2444. }
  2445. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2446. if (document.isHome) {
  2447. } else {
  2448. document.mainViewController?.addBackground(sender)
  2449. }
  2450. }
  2451. }
  2452. @IBAction func removeBackground(_ sender: Any?) {
  2453. if (self.canResponseDocumentAction() == false) {
  2454. return
  2455. }
  2456. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2457. if (document.isHome) {
  2458. } else {
  2459. document.mainViewController?.removeBackground(sender)
  2460. }
  2461. }
  2462. }
  2463. @IBAction func addWatermark(_ sender: Any?) {
  2464. if (self.canResponseDocumentAction() == false) {
  2465. return
  2466. }
  2467. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2468. if (document.isHome) {
  2469. } else {
  2470. document.mainViewController?.addWatermark(sender)
  2471. }
  2472. }
  2473. }
  2474. @IBAction func editWatermark(_ sender: Any?) {
  2475. KMPrint("editWatermark ...")
  2476. }
  2477. @IBAction func batchAddWatermark(_ sender: Any?) {
  2478. if (self.canResponseDocumentAction() == false) {
  2479. return
  2480. }
  2481. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2482. if (document.isHome) {
  2483. } else {
  2484. document.mainViewController?.batchAddWatermark(sender)
  2485. }
  2486. }
  2487. }
  2488. @IBAction func batchRemoveWatermark(_ sender: Any?) {
  2489. if (self.canResponseDocumentAction() == false) {
  2490. return
  2491. }
  2492. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2493. if (document.isHome) {
  2494. } else {
  2495. document.mainViewController?.batchRemoveWatermark(sender)
  2496. }
  2497. }
  2498. }
  2499. @IBAction func removeWatermark(_ sender: Any?) {
  2500. if (self.canResponseDocumentAction() == false) {
  2501. return
  2502. }
  2503. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2504. if (document.isHome) {
  2505. } else {
  2506. document.mainViewController?.removeWatermark(sender)
  2507. }
  2508. }
  2509. }
  2510. @IBAction func templateWatermark(_ sender: Any?) {
  2511. if (self.canResponseDocumentAction() == false) {
  2512. return
  2513. }
  2514. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2515. if (document.isHome) {
  2516. } else {
  2517. document.mainViewController?.templateWatermark(sender)
  2518. }
  2519. }
  2520. }
  2521. @IBAction func batchAddBackground(_ sender: Any?) {
  2522. if (self.canResponseDocumentAction() == false) {
  2523. return
  2524. }
  2525. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2526. if (document.isHome) {
  2527. } else {
  2528. document.mainViewController?.batchAddBackground(sender)
  2529. }
  2530. }
  2531. }
  2532. @IBAction func batchRemoveBackground(_ sender: Any?) {
  2533. if (self.canResponseDocumentAction() == false) {
  2534. return
  2535. }
  2536. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2537. if (document.isHome) {
  2538. } else {
  2539. document.mainViewController?.batchRemoveBackground(sender)
  2540. }
  2541. }
  2542. }
  2543. @IBAction func templateBackground(_ sender: Any?) {
  2544. if (self.canResponseDocumentAction() == false) {
  2545. return
  2546. }
  2547. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2548. if (document.isHome) {
  2549. } else {
  2550. document.mainViewController?.templateBackground(sender)
  2551. }
  2552. }
  2553. }
  2554. @IBAction func addHeaderFooter(_ sender: Any?) {
  2555. if (self.canResponseDocumentAction() == false) {
  2556. return
  2557. }
  2558. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2559. if (document.isHome) {
  2560. } else {
  2561. document.mainViewController?.addHeaderFooter(sender)
  2562. }
  2563. }
  2564. }
  2565. @IBAction func removeHeaderFooter(_ sender: Any?) {
  2566. if (self.canResponseDocumentAction() == false) {
  2567. return
  2568. }
  2569. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2570. if (document.isHome) {
  2571. } else {
  2572. document.mainViewController?.removeHeaderFooter(sender)
  2573. }
  2574. }
  2575. }
  2576. @IBAction func batchAddHeaderFooter(_ sender: Any?) {
  2577. if (self.canResponseDocumentAction() == false) {
  2578. return
  2579. }
  2580. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2581. if (document.isHome) {
  2582. } else {
  2583. document.mainViewController?.batchAddHeaderFooter(sender)
  2584. }
  2585. }
  2586. }
  2587. @IBAction func batchRemoveHeaderFooter(_ sender: Any?) {
  2588. if (self.canResponseDocumentAction() == false) {
  2589. return
  2590. }
  2591. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2592. if (document.isHome) {
  2593. } else {
  2594. document.mainViewController?.batchRemoveHeaderFooter(sender)
  2595. }
  2596. }
  2597. }
  2598. @IBAction func templateHeaderFooter(_ sender: Any?) {
  2599. if (self.canResponseDocumentAction() == false) {
  2600. return
  2601. }
  2602. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2603. if (document.isHome) {
  2604. } else {
  2605. document.mainViewController?.templateHeaderFooter(sender)
  2606. }
  2607. }
  2608. }
  2609. @IBAction func addHeaderFooterBates(_ sender: Any?) {
  2610. if (self.canResponseDocumentAction() == false) {
  2611. return
  2612. }
  2613. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2614. if (document.isHome) {
  2615. } else {
  2616. document.mainViewController?.addHeaderFooterBates(sender)
  2617. }
  2618. }
  2619. }
  2620. @IBAction func removeHeaderFooterBates(_ sender: Any?) {
  2621. if (self.canResponseDocumentAction() == false) {
  2622. return
  2623. }
  2624. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2625. if (document.isHome) {
  2626. } else {
  2627. document.mainViewController?.removeHeaderFooterBates(sender)
  2628. }
  2629. }
  2630. }
  2631. @IBAction func batchAddBates(_ sender: Any?) {
  2632. if (self.canResponseDocumentAction() == false) {
  2633. return
  2634. }
  2635. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2636. if (document.isHome) {
  2637. } else {
  2638. document.mainViewController?.batchAddBates(sender)
  2639. }
  2640. }
  2641. }
  2642. @IBAction func batchRemoveBates(_ sender: Any?) {
  2643. if (self.canResponseDocumentAction() == false) {
  2644. return
  2645. }
  2646. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2647. if (document.isHome) {
  2648. } else {
  2649. document.mainViewController?.batchRemoveBates(sender)
  2650. }
  2651. }
  2652. }
  2653. @IBAction func templateBates(_ sender: Any?) {
  2654. if (self.canResponseDocumentAction() == false) {
  2655. return
  2656. }
  2657. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2658. if (document.isHome) {
  2659. } else {
  2660. document.mainViewController?.templateBates(sender)
  2661. }
  2662. }
  2663. }
  2664. @IBAction func togglePoster(_ sender: Any?) {
  2665. if (self.canResponseDocumentAction() == false) {
  2666. return
  2667. }
  2668. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2669. if (document.isHome) {
  2670. } else {
  2671. document.mainViewController?.togglePoster(sender)
  2672. }
  2673. }
  2674. }
  2675. @IBAction func toggleMultiple(_ sender: Any?) {
  2676. if (self.canResponseDocumentAction() == false) {
  2677. return
  2678. }
  2679. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2680. if (document.isHome) {
  2681. } else {
  2682. document.mainViewController?.toggleMultiple(sender)
  2683. }
  2684. }
  2685. }
  2686. @IBAction func toggleBooklet(_ sender: Any?) {
  2687. if (self.canResponseDocumentAction() == false) {
  2688. return
  2689. }
  2690. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2691. if (document.isHome) {
  2692. } else {
  2693. document.mainViewController?.toggleBooklet(sender)
  2694. }
  2695. }
  2696. }
  2697. @IBAction func rotateRight(_ sender: Any?) {
  2698. if (self.canResponseDocumentAction() == false) {
  2699. return
  2700. }
  2701. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2702. if (document.isHome) {
  2703. } else {
  2704. document.mainViewController?.rotateRight(sender)
  2705. }
  2706. }
  2707. }
  2708. @IBAction func rotateLeft(_ sender: Any?) {
  2709. if (self.canResponseDocumentAction() == false) {
  2710. return
  2711. }
  2712. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2713. if (document.isHome) {
  2714. } else {
  2715. document.mainViewController?.rotateLeft(sender)
  2716. }
  2717. }
  2718. }
  2719. @IBAction func rotateAllRight(_ sender: Any?) {
  2720. if (self.canResponseDocumentAction() == false) {
  2721. return
  2722. }
  2723. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2724. if (document.isHome) {
  2725. } else {
  2726. document.mainViewController?.rotateAllRight(sender)
  2727. }
  2728. }
  2729. }
  2730. @IBAction func rotateAllLeft(_ sender: Any?) {
  2731. if (self.canResponseDocumentAction() == false) {
  2732. return
  2733. }
  2734. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2735. if (document.isHome) {
  2736. } else {
  2737. document.mainViewController?.rotateAllLeft(sender)
  2738. }
  2739. }
  2740. }
  2741. @IBAction func autoSelectContent(_ sender: Any?) {
  2742. if (self.canResponseDocumentAction() == false) {
  2743. return
  2744. }
  2745. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2746. if (document.isHome) {
  2747. } else {
  2748. document.mainViewController?.autoSelectContent(sender)
  2749. }
  2750. }
  2751. }
  2752. }
  2753. // MARK: - KMSystemWindowMenuRrotocol
  2754. extension KMBrowserWindowController: KMSystemWindowMenuRrotocol {
  2755. func menuItemAction_showForwardTagPage(_ sender: Any) {
  2756. if (self.canResponseDocumentAction() == false) {
  2757. return
  2758. }
  2759. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2760. if (document.isHome) {
  2761. document.homeViewController?.menuItemAction_showForwardTagPage(sender)
  2762. } else {
  2763. document.mainViewController?.menuItemAction_showForwardTagPage(sender)
  2764. }
  2765. }
  2766. }
  2767. func menuItemAction_showNextTagPage(_ sender: Any) {
  2768. if (self.canResponseDocumentAction() == false) {
  2769. return
  2770. }
  2771. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2772. if (document.isHome) {
  2773. document.homeViewController?.menuItemAction_showNextTagPage(sender)
  2774. } else {
  2775. document.mainViewController?.menuItemAction_showNextTagPage(sender)
  2776. }
  2777. }
  2778. }
  2779. func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  2780. if (self.canResponseDocumentAction() == false) {
  2781. return
  2782. }
  2783. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2784. if (document.isHome) {
  2785. document.homeViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2786. } else {
  2787. document.mainViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2788. }
  2789. }
  2790. }
  2791. func menuItemAction_mergeAllWindow(_ sender: Any) {
  2792. if (self.canResponseDocumentAction() == false) {
  2793. return
  2794. }
  2795. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2796. if (document.isHome) {
  2797. document.homeViewController?.menuItemAction_mergeAllWindow(sender)
  2798. } else {
  2799. document.mainViewController?.menuItemAction_mergeAllWindow(sender)
  2800. }
  2801. }
  2802. }
  2803. @IBAction func performFit(_ sender: Any?) {
  2804. if (self.canResponseDocumentAction() == false) {
  2805. return
  2806. }
  2807. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2808. if (document.isHome) {
  2809. } else {
  2810. document.mainViewController?.performFit(sender)
  2811. }
  2812. }
  2813. }
  2814. }
  2815. // MARK: - Analytics (埋点)
  2816. extension KMBrowserWindowController {
  2817. func trackEvent_upgrade() -> Void {
  2818. KMAnalytics.trackEvent(eventName: "Btn_Tbr_Upgrade", parameters: [
  2819. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.tbr,
  2820. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.tbr_Btn], platform: .AppCenter, appTarget: .all)
  2821. }
  2822. }