KMBrowserWindowController.swift 120 KB

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