KMBrowserWindowController.swift 108 KB

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