KMBrowserWindowController.swift 118 KB

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