KMBrowserWindowController.swift 109 KB

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