KMBrowserWindowController.swift 120 KB

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