KMBrowserWindowController.swift 121 KB

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