KMBrowserWindowController.swift 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975
  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 editNote(_ sender: Any?) {
  591. KMPrint("editNote ...")
  592. }
  593. @IBAction func changeColor(_ sender: Any?) {
  594. KMPrint("changeColor ...")
  595. }
  596. @IBAction func changeAttributes(_ sender: Any?) {
  597. KMPrint("changeAttributes ...")
  598. }
  599. @IBAction func alignLeft(_ sender: Any?) {
  600. KMPrint("alignLeft ...")
  601. }
  602. @IBAction func alignRight(_ sender: Any?) {
  603. KMPrint("alignRight ...")
  604. }
  605. @IBAction func alignCenter(_ sender: Any?) {
  606. KMPrint("alignCenter ...")
  607. }
  608. @IBAction func createNewNote(_ sender: Any?) {
  609. KMPrint("createNewNote ...")
  610. }
  611. @IBAction func toggleHideNotes(_ sender: Any?) {
  612. KMPrint("toggleHideNotes ...")
  613. }
  614. @IBAction func changeDisplaySinglePages(_ sender: Any?) {
  615. KMPrint("changeDisplaySinglePages ...")
  616. }
  617. @IBAction func changeDisplayContinuous(_ sender: Any?) {
  618. KMPrint("changeDisplayContinuous ...")
  619. }
  620. @IBAction func doGoToNextPage(_ sender: Any?) {
  621. KMPrint("doGoToNextPage ...")
  622. }
  623. @IBAction func doGoToPreviousPage(_ sender: Any?) {
  624. KMPrint("doGoToPreviousPage ...")
  625. }
  626. @IBAction func doGoToFirstPage(_ sender: Any?) {
  627. KMPrint("doGoToFirstPage ...")
  628. }
  629. @IBAction func doGoToLastPage(_ sender: Any?) {
  630. KMPrint("doGoToLastPage ...")
  631. }
  632. @IBAction func allGoToNextPage(_ sender: Any?) {
  633. KMPrint("allGoToNextPage ...")
  634. }
  635. @IBAction func allGoToPreviousPage(_ sender: Any?) {
  636. KMPrint("allGoToPreviousPage ...")
  637. }
  638. @IBAction func allGoToFirstPage(_ sender: Any?) {
  639. KMPrint("allGoToFirstPage ...")
  640. }
  641. @IBAction func allGoToLastPage(_ sender: Any?) {
  642. KMPrint("allGoToLastPage ...")
  643. }
  644. @IBAction func doGoToPage(_ sender: Any?) {
  645. KMPrint("doGoToPage ...")
  646. }
  647. @IBAction func doGoBack(_ sender: Any?) {
  648. KMPrint("doGoBack ...")
  649. }
  650. @IBAction func doGoForward(_ sender: Any?) {
  651. KMPrint("doGoForward ...")
  652. }
  653. @IBAction func doZoomToPhysicalSize(_ sender: Any?) {
  654. KMPrint("doZoomToPhysicalSize ...")
  655. }
  656. @IBAction func alternateZoomToFit(_ sender: Any?) {
  657. KMPrint("alternateZoomToFit ...")
  658. }
  659. @IBAction func doZoomToSelection(_ sender: Any?) {
  660. KMPrint("doZoomToSelection ...")
  661. }
  662. @IBAction func doAutoScale(_ sender: Any?) {
  663. KMPrint("doAutoScale ...")
  664. }
  665. // @IBAction func delete(_ sender: Any?) {
  666. // KMPrint("delete ...")
  667. // }
  668. // @IBAction func paste(_ sender: Any?) {
  669. // KMPrint("paste ...")
  670. // }
  671. @IBAction func alternatePaste(_ sender: Any?) {
  672. KMPrint("alternatePaste ...")
  673. }
  674. @IBAction func copy(_ sender: Any?) {
  675. KMPrint("copy ...")
  676. }
  677. @IBAction func cut(_ sender: Any?) {
  678. KMPrint("cut ...")
  679. }
  680. @IBAction func toggleFullscreen(_ sender: Any?) {
  681. KMPrint("toggleFullscreen ...")
  682. }
  683. @IBAction func toggleRightSidePane(_ sender: Any?) {
  684. KMPrint("toggleRightSidePane ...")
  685. }
  686. @IBAction func performFit(_ sender: Any?) {
  687. KMPrint("performFit ...")
  688. }
  689. @IBAction func changeRightSidePaneState(_ sender: Any?) {
  690. KMPrint("changeRightSidePaneState ...")
  691. }
  692. @IBAction func changeFindPaneState(_ sender: Any?) {
  693. KMPrint("changeFindPaneState ...")
  694. }
  695. @IBAction func toggleSplitPDF(_ sender: Any?) {
  696. KMPrint("toggleSplitPDF ...")
  697. }
  698. @IBAction func search(_ sender: Any?) {
  699. KMPrint("search ...")
  700. }
  701. @IBAction func searchNotes(_ sender: Any?) {
  702. KMPrint("searchNotes ...")
  703. }
  704. @IBAction func password(_ sender: Any?) {
  705. KMPrint("password ...")
  706. }
  707. @IBAction func toggleCaseInsensitiveSearch(_ sender: Any?) {
  708. KMPrint("toggleCaseInsensitiveSearch ...")
  709. }
  710. @IBAction func toggleWholeWordSearch(_ sender: Any?) {
  711. KMPrint("toggleWholeWordSearch ...")
  712. }
  713. @IBAction func toggleCaseInsensitiveNoteSearch(_ sender: Any?) {
  714. KMPrint("toggleCaseInsensitiveNoteSearch ...")
  715. }
  716. @IBAction func performFindPanelAction(_ sender: NSMenuItem) {
  717. KMPrint("performFindPanelAction ...")
  718. var forward = true
  719. var findString: String?
  720. switch sender.tag {
  721. case 1:
  722. if let document = self.browser.activeTabContents() as? KMMainDocument {
  723. if document.isHome == false {
  724. document.mainViewController?.toolbarController.showFindBar()
  725. }
  726. }
  727. case 3:
  728. if let document = self.browser.activeTabContents() as? KMMainDocument {
  729. if document.isHome == false {
  730. document.mainViewController?.toolbarController.showFindString(forward: false)
  731. }
  732. }
  733. case 2:
  734. if let document = self.browser.activeTabContents() as? KMMainDocument {
  735. if document.isHome == false {
  736. document.mainViewController?.toolbarController.showFindString(forward: true)
  737. }
  738. }
  739. case 7:
  740. if let document = self.browser.activeTabContents() as? KMMainDocument {
  741. if document.isHome == false {
  742. document.mainViewController?.toolbarController.showAllAction()
  743. }
  744. }
  745. default: break
  746. }
  747. }
  748. @IBAction func scrollUp(_ sender: Any?) {
  749. KMPrint("scrollUp ...")
  750. }
  751. @IBAction func scrollDown(_ sender: Any?) {
  752. KMPrint("scrollDown ...")
  753. }
  754. @IBAction func scrollRight(_ sender: Any?) {
  755. KMPrint("scrollRight ...")
  756. }
  757. @IBAction func scrollLeft(_ sender: Any?) {
  758. KMPrint("scrollLeft ...")
  759. }
  760. @IBAction func selectSelectedNote(_ sender: Any?) {
  761. KMPrint("selectSelectedNote ...")
  762. }
  763. @IBAction func goToSelectedOutlineItem(_ sender: Any?) {
  764. KMPrint("goToSelectedOutlineItem ...")
  765. }
  766. @IBAction func goToSelectedFindResults(_ sender: Any?) {
  767. KMPrint("goToSelectedFindResults ...")
  768. }
  769. @IBAction func toggleSelectedSnapshots(_ sender: Any?) {
  770. KMPrint("toggleSelectedSnapshots ...")
  771. }
  772. @IBAction func selfSignProfile(_ sender: Any?) {
  773. KMPrint("selfSignProfile ...")
  774. }
  775. @IBAction func link(_ sender: Any?) {
  776. KMPrint("link ...")
  777. }
  778. @IBAction func insertPage(_ sender: Any?) {
  779. KMPrint("insertPage ...")
  780. }
  781. @IBAction func split(_ sender: Any?) {
  782. KMPrint("split ...")
  783. }
  784. @IBAction func comparison(_ sender: Any?) {
  785. KMPrint("comparison ...")
  786. }
  787. @IBAction func contentComparison(_ sender: Any?) {
  788. KMPrint("contentComparison ...")
  789. }
  790. @IBAction func coverageComparison(_ sender: Any?) {
  791. KMPrint("coverageComparison ...")
  792. }
  793. @IBAction func readMode(_ sender: Any?) {
  794. KMPrint("readMode ...")
  795. }
  796. @IBAction func changeFormType(_ sender: Any?) {
  797. KMPrint("changeFormType ...")
  798. }
  799. @IBAction func changeSelfSignType(_ sender: Any?) {
  800. KMPrint("changeSelfSignType ...")
  801. }
  802. @IBAction func clearAllForm(_ sender: Any?) {
  803. KMPrint("clearAllForm ...")
  804. }
  805. @IBAction func thumbnailSizeScaling(_ sender: Any?) {
  806. KMPrint("thumbnailSizeScaling ...")
  807. }
  808. @IBAction func redactAction(_ sender: Any?) {
  809. if (self.canResponseDocumentAction() == false) {
  810. return
  811. }
  812. if let document = self.browser.activeTabContents() as? KMMainDocument {
  813. if document.isHome == false {
  814. document.mainViewController?.menuItemAction_redact(sender)
  815. }
  816. }
  817. }
  818. }
  819. // MARK: - KMSystemFileMenuProtocol
  820. extension KMBrowserWindowController: KMSystemFileMenuProtocol {
  821. func menuItemClick_saveAsFlattenedPDF(_ sender: Any) {
  822. if (self.canResponseDocumentAction() == false) {
  823. return
  824. }
  825. if let document = self.browser.activeTabContents() as? KMMainDocument {
  826. if (document.isHome) {
  827. // document.homeViewController?.menuItemClick_mergePDF(sender)
  828. } else {
  829. document.mainViewController?.menuItemClick_saveAsFlattenedPDF(sender)
  830. }
  831. }
  832. }
  833. func menuItemClick_mergePDF(_ sender: Any) {
  834. if (self.canResponseDocumentAction() == false) {
  835. return
  836. }
  837. if let document = self.browser.activeTabContents() as? KMMainDocument {
  838. if (document.isHome) {
  839. document.homeViewController?.menuItemClick_mergePDF(sender)
  840. } else {
  841. document.mainViewController?.menuItemClick_mergePDF(sender)
  842. }
  843. }
  844. }
  845. func menuItemClick_Compress(_ sender: Any) {
  846. if (self.canResponseDocumentAction() == false) {
  847. return
  848. }
  849. if let document = self.browser.activeTabContents() as? KMMainDocument {
  850. if (document.isHome) {
  851. document.homeViewController?.menuItemClick_Compress(sender)
  852. } else {
  853. document.mainViewController?.menuItemClick_Compress(sender)
  854. }
  855. }
  856. }
  857. func menuItemAction_ConvertToWord(_ sender: Any) {
  858. if (self.canResponseDocumentAction() == false) {
  859. return
  860. }
  861. if let document = self.browser.activeTabContents() as? KMMainDocument {
  862. if (document.isHome) {
  863. document.homeViewController?.menuItemAction_ConvertToWord(sender)
  864. } else {
  865. document.mainViewController?.menuItemAction_ConvertToWord(sender)
  866. }
  867. }
  868. }
  869. func menuItemAction_ConvertToExcel(_ sender: Any) {
  870. if (self.canResponseDocumentAction() == false) {
  871. return
  872. }
  873. if let document = self.browser.activeTabContents() as? KMMainDocument {
  874. if (document.isHome) {
  875. document.homeViewController?.menuItemAction_ConvertToExcel(sender)
  876. } else {
  877. document.mainViewController?.menuItemAction_ConvertToExcel(sender)
  878. }
  879. }
  880. }
  881. func menuItemAction_ConvertToPPT(_ sender: Any) {
  882. if (self.canResponseDocumentAction() == false) {
  883. return
  884. }
  885. if let document = self.browser.activeTabContents() as? KMMainDocument {
  886. if (document.isHome) {
  887. document.homeViewController?.menuItemAction_ConvertToPPT(sender)
  888. } else {
  889. document.mainViewController?.menuItemAction_ConvertToPPT(sender)
  890. }
  891. }
  892. }
  893. func menuItemAction_ConvertToRTF(_ sender: Any) {
  894. if (self.canResponseDocumentAction() == false) {
  895. return
  896. }
  897. if let document = self.browser.activeTabContents() as? KMMainDocument {
  898. if (document.isHome) {
  899. document.homeViewController?.menuItemAction_ConvertToRTF(sender)
  900. } else {
  901. document.mainViewController?.menuItemAction_ConvertToRTF(sender)
  902. }
  903. }
  904. }
  905. func menuItemAction_ConvertToHTML(_ sender: Any) {
  906. if (self.canResponseDocumentAction() == false) {
  907. return
  908. }
  909. if let document = self.browser.activeTabContents() as? KMMainDocument {
  910. if (document.isHome) {
  911. document.homeViewController?.menuItemAction_ConvertToHTML(sender)
  912. } else {
  913. document.mainViewController?.menuItemAction_ConvertToHTML(sender)
  914. }
  915. }
  916. }
  917. func menuItemAction_ConvertToText(_ sender: Any) {
  918. if (self.canResponseDocumentAction() == false) {
  919. return
  920. }
  921. if let document = self.browser.activeTabContents() as? KMMainDocument {
  922. if (document.isHome) {
  923. document.homeViewController?.menuItemAction_ConvertToText(sender)
  924. } else {
  925. document.mainViewController?.menuItemAction_ConvertToText(sender)
  926. }
  927. }
  928. }
  929. func menuItemAction_ConvertToCSV(_ sender: Any) {
  930. if (self.canResponseDocumentAction() == false) {
  931. return
  932. }
  933. if let document = self.browser.activeTabContents() as? KMMainDocument {
  934. if (document.isHome) {
  935. document.homeViewController?.menuItemAction_ConvertToCSV(sender)
  936. } else {
  937. document.mainViewController?.menuItemAction_ConvertToCSV(sender)
  938. }
  939. }
  940. }
  941. func menuItemAction_ConvertToImage(_ sender: Any) {
  942. if (self.canResponseDocumentAction() == false) {
  943. return
  944. }
  945. if let document = self.browser.activeTabContents() as? KMMainDocument {
  946. if (document.isHome) {
  947. document.homeViewController?.menuItemAction_ConvertToImage(sender)
  948. } else {
  949. document.mainViewController?.menuItemAction_ConvertToImage(sender)
  950. }
  951. }
  952. }
  953. func menuItemClick_SettingPassword(_ sender: Any) {
  954. if (self.canResponseDocumentAction() == false) {
  955. return
  956. }
  957. if let document = self.browser.activeTabContents() as? KMMainDocument {
  958. if (document.isHome) {
  959. document.homeViewController?.menuItemClick_SettingPassword(sender)
  960. } else {
  961. document.mainViewController?.menuItemClick_SettingPassword(sender)
  962. }
  963. }
  964. }
  965. func menuItemClick_RemovePassword(_ sender: Any) {
  966. if (self.canResponseDocumentAction() == false) {
  967. return
  968. }
  969. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  970. document.mainViewController?.menuItemClick_RemovePassword(sender)
  971. }
  972. }
  973. func menuItemAction_closeWindow(_ sender: Any) {
  974. if (self.canResponseDocumentAction() == false) {
  975. return
  976. }
  977. if let document = self.browser.activeTabContents() as? KMMainDocument {
  978. if (document.isHome) {
  979. document.homeViewController?.menuItemAction_closeWindow(sender)
  980. } else {
  981. document.mainViewController?.menuItemAction_closeWindow(sender)
  982. }
  983. }
  984. }
  985. func menuItemAction_closeAllWindows(_ sender: Any) {
  986. if (self.canResponseDocumentAction() == false) {
  987. return
  988. }
  989. if let document = self.browser.activeTabContents() as? KMMainDocument {
  990. if (document.isHome) {
  991. document.homeViewController?.menuItemAction_closeAllWindows(sender)
  992. } else {
  993. document.mainViewController?.menuItemAction_closeAllWindows(sender)
  994. }
  995. }
  996. }
  997. func menuItemAction_closeTagPage(_ sender: Any) {
  998. if (self.canResponseDocumentAction() == false) {
  999. return
  1000. }
  1001. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1002. document.mainViewController?.menuItemAction_closeTagPage(sender)
  1003. }
  1004. }
  1005. func menuItemAction_showInFinder(_ sender: Any) {
  1006. if (self.canResponseDocumentAction() == false) {
  1007. return
  1008. }
  1009. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1010. document.mainViewController?.menuItemAction_showInFinder(sender)
  1011. }
  1012. }
  1013. func menuItemAction_property(_ sender: Any) {
  1014. if (self.canResponseDocumentAction() == false) {
  1015. return
  1016. }
  1017. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1018. document.mainViewController?.menuItemAction_property(sender)
  1019. }
  1020. }
  1021. func menuItemAction_print(_ sender: Any) {
  1022. if (self.canResponseDocumentAction() == false) {
  1023. return
  1024. }
  1025. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1026. document.mainViewController?.menuItemAction_print(sender)
  1027. }
  1028. }
  1029. func isEncapsulatedPostScriptData(_ data: Data) -> Bool {
  1030. let epsHeaderData = Data(bytes: [69, 80, 83, 70, 45], count: 5)
  1031. let rg: Range = (14..<20)
  1032. return (data.count >= 20 && (data.range(of: epsHeaderData, options: .anchored, in: rg) != nil))
  1033. }
  1034. func convertTIFFDataToPDF(_ tiffData: Data) -> Data? {
  1035. guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil }
  1036. let pdfData = NSMutableData(capacity: tiffData.count)
  1037. let consumer = CGDataConsumer(data: pdfData! as CFMutableData)!
  1038. var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
  1039. let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil)
  1040. ctxt!.beginPDFPage(nil)
  1041. ctxt!.draw(cgImage, in: rect)
  1042. ctxt!.endPDFPage()
  1043. ctxt!.flush()
  1044. return pdfData as? Data
  1045. }
  1046. func openDocumentWithImageFromPasteboard(_ pboard: NSPasteboard, error outError: AutoreleasingUnsafeMutablePointer<NSError?>?) -> Any? {
  1047. var document: CPDFDocument? = nil
  1048. var data: Data? = nil
  1049. if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.pdf.rawValue]) {
  1050. // pboard.types
  1051. data = pboard.data(forType: NSPasteboard.PasteboardType.pdf)
  1052. } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.postScript.rawValue]) {
  1053. // pboard.types
  1054. data = pboard.data(forType: NSPasteboard.PasteboardType.postScript)
  1055. } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.tiff.rawValue]) {
  1056. // pboard.types
  1057. data = convertTIFFDataToPDF(pboard.data(forType: NSPasteboard.PasteboardType.tiff) ?? Data())
  1058. } else {
  1059. let images = pboard.readObjects(forClasses: [NSImage.self], options: [:])
  1060. let strings = pboard.readObjects(forClasses: [NSAttributedString.self], options: [:])
  1061. if images?.count ?? 0 > 0 {
  1062. data = convertTIFFDataToPDF((images![0] as AnyObject).tiffRepresentation!)
  1063. } else if strings?.count ?? 0 > 0 {
  1064. data = KMOCTool.convertStringsToPDF(withString: strings ?? [""]) // convertStringsToPDF(strings!)
  1065. }
  1066. }
  1067. if let data = data {
  1068. _ = NSDocumentController.shared
  1069. document = CPDFDocument(data: data)
  1070. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1071. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1072. let filePath = savePath.deletingLastPathComponent
  1073. if FileManager.default.fileExists(atPath: filePath) == false {
  1074. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1075. }
  1076. document?.write(to: URL(fileURLWithPath: savePath))
  1077. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1078. if error != nil {
  1079. NSApp.presentError(error!)
  1080. } else {
  1081. if document is KMMainDocument {
  1082. let newDocument = document
  1083. (newDocument as! KMMainDocument).isNewCreated = true
  1084. }
  1085. }
  1086. }
  1087. } else if let outError = outError {
  1088. outError.pointee = NSError(domain: "SKDocumentErrorDomain", code: 3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Unable to load data from clipboard", comment: "Error description")])
  1089. }
  1090. return document
  1091. }
  1092. func openDocument(withURLFromPasteboard pboard: NSPasteboard, showNotes: Bool, error outError: inout NSError?) -> Any? {
  1093. let theURLs = NSURL.readURLs(from: pboard)
  1094. let url = theURLs?.count ?? 0 > 0 ? theURLs?[0] : nil
  1095. let theURL: NSURL? = url as? NSURL
  1096. var documentC = NSDocumentController.shared
  1097. var document: NSDocument? = nil
  1098. if (theURL as AnyObject).isFileURL == true {
  1099. var _: NSError? = nil
  1100. let type = try? documentC.typeForContents(of: theURL as! URL)//ForContents(ofURL: theURL, error: &error)
  1101. if showNotes == false || NSDocument.readableTypes.contains(type ?? "") {
  1102. documentC.openDocument(withContentsOf: theURL as! URL, display: true, completionHandler: { resultDocument, success, err in
  1103. document = resultDocument
  1104. })//openDocument(withContentsOfURL: theURL, display: true, error: &outError)
  1105. } else if NSDocument.readableTypes.contains(type ?? "") {
  1106. for doc in documentC.documents {
  1107. let sel = NSSelectorFromString("sourceFileURL")
  1108. if doc.responds(to: sel) && doc.fileURL == theURL as? URL {
  1109. document = doc
  1110. break
  1111. }
  1112. }
  1113. if let document: NSDocument = document {
  1114. document.showWindows()
  1115. } else {
  1116. var data: Data? = nil
  1117. // if NSWorkspace.shared.type(type ?? "", conformsToType: KMPDFBundleDocumentType) {
  1118. // let skimFileURL = FileManager.default.exte
  1119. // data = skimFileURL != nil ? try? Data(contentsOf: skimFileURL!) : nil
  1120. // } else {
  1121. // data = SKNExtendedAttributeManager.shared().extendedAttributeNamed(SKIM_NOTES_KEY, atPath: theURL.path, traverseLink: true, error: &error)
  1122. // }
  1123. // let documentNew: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  1124. if let document = try? documentC.makeUntitledDocument(ofType: KMNotesDocumentType) {
  1125. document.fileURL = URL(fileURLWithPath: theURL?.path ?? "")
  1126. // if data == nil || document.read(fromData: data!, ofType: SKNotesDocumentType, error: &error) {
  1127. documentC.addDocument(document)
  1128. document.makeWindowControllers()
  1129. document.showWindows()
  1130. // } else {
  1131. // document = nil
  1132. // if let error = error {
  1133. // outError = error
  1134. // }
  1135. // }
  1136. }
  1137. }
  1138. }
  1139. }
  1140. // else if showNotes == false && theURL != nil {
  1141. // document = SKDownloadController.shared().addDownload(for: theURL!)
  1142. // } else if outError != nil {
  1143. // outError = NSError.readPasteboardError(withLocalizedDescription: NSLocalizedString("Unable to load data from clipboard", comment: "Error description"))
  1144. // }
  1145. return document
  1146. }
  1147. @IBAction func newDocumentFromClipboard(_ sender: Any?) {
  1148. KMPrint("newDocumentFromClipboard")
  1149. var error: NSError?
  1150. let pboard = NSPasteboard.general
  1151. var document = openDocumentWithImageFromPasteboard(pboard, error: &error)
  1152. if document == nil{
  1153. document = openDocument(withURLFromPasteboard: pboard, showNotes: false, error: &error)
  1154. }
  1155. }
  1156. @IBAction func newDocumentFromImage(_ sender: Any?) {
  1157. KMPrint("importFromWebPage")
  1158. createPDFFromImage()
  1159. }
  1160. @IBAction func importFromWebPage(_ sender: Any?) {
  1161. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  1162. if (document.isHome) {
  1163. document.homeViewController?.importFromWebPage()
  1164. } else {
  1165. self.homeVC = KMHomeViewController()
  1166. self.homeVC?.importFromWebPage()
  1167. }
  1168. }
  1169. @IBAction func screenShot_SelectArea(_ sender: Any?) {
  1170. KMPrint("screenShot_SelectArea")
  1171. KMScreenShotHandler.beginScreenshot_SelectRectWithCompleteHandler { ima in
  1172. if let image = ima {
  1173. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1174. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1175. let newDocument = CPDFDocument()
  1176. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1177. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1178. if writeSuccess ?? false {
  1179. self.savePdf(savePath)
  1180. // try? FileManager.default.removeItem(atPath: savePath)
  1181. } else {
  1182. }
  1183. }
  1184. }
  1185. }
  1186. }
  1187. @IBAction func screenShot_Window(_ sender: Any?) {
  1188. KMPrint("screenShot_Window")
  1189. KMScreenShotHandler.beginScreenShot_SelectWindowCompleteHandler { ima in
  1190. if let image = ima {
  1191. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1192. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1193. let newDocument = CPDFDocument()
  1194. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1195. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1196. if writeSuccess == true {
  1197. self.savePdf(savePath)
  1198. // try? FileManager.default.removeItem(atPath: savePath)
  1199. } else {
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. @IBAction func screenShot_FullScreenDelay(_ sender: Any?) {
  1206. KMPrint("screenShot_FullScreenDelay")
  1207. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(3) { ima in
  1208. if let image = ima {
  1209. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1210. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1211. let newDocument = CPDFDocument()
  1212. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1213. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1214. if writeSuccess == true {
  1215. self.savePdf(savePath)
  1216. // try? FileManager.default.removeItem(atPath: savePath)
  1217. } else {
  1218. }
  1219. }
  1220. }
  1221. }
  1222. }
  1223. @IBAction func screenShot_FullScreen(_ sender: Any?) {
  1224. KMPrint("screenShot_FullScreen")
  1225. KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(0) { ima in
  1226. if let image = ima {
  1227. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
  1228. let savePath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? "") + "/" + Bundle.main.bundleIdentifier! + "/screenShot.pdf"
  1229. let newDocument = CPDFDocument()
  1230. _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
  1231. let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
  1232. if writeSuccess == true {
  1233. self.savePdf(savePath)
  1234. // try? FileManager.default.removeItem(atPath: savePath)
  1235. } else {
  1236. }
  1237. }
  1238. }
  1239. }
  1240. }
  1241. }
  1242. // MARK: - KMSystemViewMenuProtocol
  1243. extension KMBrowserWindowController: KMSystemViewMenuProtocol {
  1244. func createPDFFromImage() {
  1245. let openPanel = NSOpenPanel()
  1246. openPanel.allowedFileTypes = KMImageAccessoryController.supportedImageTypes()
  1247. openPanel.allowsMultipleSelection = true
  1248. 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: "")
  1249. openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in
  1250. if response == .OK {
  1251. self.creatPdfFromImg(urls: openPanel.urls)
  1252. }
  1253. }
  1254. }
  1255. func creatPdfFromImg(urls: Array<URL>) {
  1256. // let savePath = self.newDocumentTempSavePath("convertToPDF.pdf")
  1257. let pdf = CPDFDocument()
  1258. // var isSucceed = true
  1259. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1260. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1261. let filePath = savePath.deletingLastPathComponent
  1262. if FileManager.default.fileExists(atPath: filePath) == false {
  1263. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1264. }
  1265. for fileURL in urls {
  1266. if let imag = NSImage(contentsOfFile: fileURL.path ) {
  1267. _ = pdf?.km_insertPage(imag.size, withImage: fileURL.path , at: pdf?.pageCount ?? 0)
  1268. pdf?.write(to: URL(fileURLWithPath: savePath))
  1269. }
  1270. }
  1271. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1272. if error != nil {
  1273. NSApp.presentError(error!)
  1274. } else {
  1275. if document is KMMainDocument {
  1276. let newDocument = document
  1277. (newDocument as! KMMainDocument).isNewCreated = true
  1278. }
  1279. }
  1280. // self.homeContentView?.reloadData()
  1281. }
  1282. }
  1283. func createPdfWithParam() {
  1284. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  1285. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  1286. let filePath = savePath.deletingLastPathComponent
  1287. if FileManager.default.fileExists(atPath: filePath) == false {
  1288. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
  1289. }
  1290. let pdfDocument = CPDFDocument()
  1291. pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
  1292. pdfDocument?.write(to: URL(fileURLWithPath: savePath))
  1293. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  1294. if error != nil {
  1295. NSApp.presentError(error!)
  1296. } else {
  1297. if document is KMMainDocument {
  1298. let newDocument = document
  1299. (newDocument as! KMMainDocument).isNewCreated = true
  1300. }
  1301. }
  1302. // self.homeContentView?.reloadData()
  1303. }
  1304. }
  1305. func savePdf(_ filePath: String) -> Void {
  1306. let docVc = KMDocumentController.shared
  1307. docVc.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
  1308. if error != nil {
  1309. NSApp.presentError(error!)
  1310. } else {
  1311. if document is KMMainDocument {
  1312. let newDocument = document
  1313. (newDocument as! KMMainDocument).isNewCreated = true
  1314. }
  1315. }
  1316. }
  1317. }
  1318. func newDocumentTempSavePath(_ fileName: String) -> String {
  1319. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  1320. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  1321. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  1322. return append2
  1323. }
  1324. func fetchUniquePath(_ originalPath: String) -> String {
  1325. var path = originalPath
  1326. let dManager = FileManager.default
  1327. if !dManager.fileExists(atPath: path) {
  1328. if path.extension.count < 1 {
  1329. path = path.stringByAppendingPathExtension("pdf")
  1330. }
  1331. return path
  1332. } else {
  1333. let originalFullFileName = path.lastPathComponent
  1334. let originalFileName = path.lastPathComponent.deletingPathExtension
  1335. let originalExtension = path.extension
  1336. let startIndex: Int = 0
  1337. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  1338. let fileLocatePath = originalPath.substring(to: endIndex)
  1339. var i = 1
  1340. while (1 != 0) {
  1341. var newName = String(format: "%@%ld", originalFileName, i)
  1342. newName = newName.stringByAppendingPathExtension(originalExtension)
  1343. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  1344. if !dManager.fileExists(atPath: newPath) {
  1345. return newPath
  1346. } else {
  1347. i+=1
  1348. continue
  1349. }
  1350. }
  1351. }
  1352. }
  1353. func menuItemAction_adjustWidth(_ sender: Any) {
  1354. if (self.canResponseDocumentAction() == false) {
  1355. return
  1356. }
  1357. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1358. document.mainViewController?.menuItemAction_adjustWidth(sender)
  1359. }
  1360. }
  1361. func menuItemAction_adjustPage(_ sender: Any) {
  1362. if (self.canResponseDocumentAction() == false) {
  1363. return
  1364. }
  1365. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1366. document.mainViewController?.menuItemAction_adjustPage(sender)
  1367. }
  1368. }
  1369. func menuItemAction_size(_ sender: Any) {
  1370. if (self.canResponseDocumentAction() == false) {
  1371. return
  1372. }
  1373. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1374. document.mainViewController?.menuItemAction_size(sender)
  1375. }
  1376. }
  1377. func menuItemAction_zoomOut(_ sender: Any) {
  1378. if (self.canResponseDocumentAction() == false) {
  1379. return
  1380. }
  1381. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1382. document.mainViewController?.menuItemAction_zoomOut(sender)
  1383. }
  1384. }
  1385. func menuItemAction_zoomIn(_ sender: Any) {
  1386. if (self.canResponseDocumentAction() == false) {
  1387. return
  1388. }
  1389. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1390. document.mainViewController?.menuItemAction_zoomIn(sender)
  1391. }
  1392. }
  1393. func menuItemAction_singlePage(_ sender: Any) {
  1394. if (self.canResponseDocumentAction() == false) {
  1395. return
  1396. }
  1397. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1398. document.mainViewController?.menuItemAction_singlePage(sender)
  1399. }
  1400. }
  1401. func menuItemAction_singlePageContinue(_ sender: Any) {
  1402. if (self.canResponseDocumentAction() == false) {
  1403. return
  1404. }
  1405. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1406. document.mainViewController?.menuItemAction_singlePageContinue(sender)
  1407. }
  1408. }
  1409. func menuItemAction_doublePage(_ sender: Any) {
  1410. if (self.canResponseDocumentAction() == false) {
  1411. return
  1412. }
  1413. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1414. document.mainViewController?.menuItemAction_doublePage(sender)
  1415. }
  1416. }
  1417. func menuItemAction_doublePageContinue(_ sender: Any) {
  1418. if (self.canResponseDocumentAction() == false) {
  1419. return
  1420. }
  1421. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1422. document.mainViewController?.menuItemAction_doublePageContinue(sender)
  1423. }
  1424. }
  1425. func menuItemAction_bookMode(_ sender: Any) {
  1426. if (self.canResponseDocumentAction() == false) {
  1427. return
  1428. }
  1429. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1430. document.mainViewController?.menuItemAction_bookMode(sender)
  1431. }
  1432. }
  1433. func menuItemAction_readMode(_ sender: Any) {
  1434. if (self.canResponseDocumentAction() == false) {
  1435. return
  1436. }
  1437. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1438. document.mainViewController?.menuItemAction_readMode(sender)
  1439. }
  1440. }
  1441. func menuItemAction_showSplitPage(_ sender: Any) {
  1442. if (self.canResponseDocumentAction() == false) {
  1443. return
  1444. }
  1445. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1446. document.mainViewController?.menuItemAction_showSplitPage(sender)
  1447. }
  1448. }
  1449. func menuItemAction_rotateLeft(_ sender: Any) {
  1450. if (self.canResponseDocumentAction() == false) {
  1451. return
  1452. }
  1453. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1454. document.mainViewController?.menuItemAction_rotateLeft(sender)
  1455. }
  1456. }
  1457. func menuItemAction_rotateRight(_ sender: Any) {
  1458. if (self.canResponseDocumentAction() == false) {
  1459. return
  1460. }
  1461. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1462. document.mainViewController?.menuItemAction_rotateRight(sender)
  1463. }
  1464. }
  1465. func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  1466. if (self.canResponseDocumentAction() == false) {
  1467. return
  1468. }
  1469. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1470. document.mainViewController?.menuItemAction_rotateAllPageLeft(sender)
  1471. }
  1472. }
  1473. func menuItemAction_rotateAllPageRight(_ sender: Any) {
  1474. if (self.canResponseDocumentAction() == false) {
  1475. return
  1476. }
  1477. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1478. document.mainViewController?.menuItemAction_rotateAllPageRight(sender)
  1479. }
  1480. }
  1481. func menuItemAction_view_readMode(_ sender: Any) {
  1482. if (self.canResponseDocumentAction() == false) {
  1483. return
  1484. }
  1485. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1486. document.mainViewController?.menuItemAction_view_readMode(sender)
  1487. }
  1488. }
  1489. func menuItemAction_hiddenLeftSide(_ sender: Any) {
  1490. if (self.canResponseDocumentAction() == false) {
  1491. return
  1492. }
  1493. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1494. document.mainViewController?.menuItemAction_hiddenLeftSide(sender)
  1495. }
  1496. }
  1497. func menuItemAction_hiddenRightSide(_ sender: Any) {
  1498. if (self.canResponseDocumentAction() == false) {
  1499. return
  1500. }
  1501. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1502. document.mainViewController?.menuItemAction_hiddenRightSide(sender)
  1503. }
  1504. }
  1505. func menuItemAction_thumai(_ sender: Any) {
  1506. if (self.canResponseDocumentAction() == false) {
  1507. return
  1508. }
  1509. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1510. document.mainViewController?.menuItemAction_thumai(sender)
  1511. }
  1512. }
  1513. func menuItemAction_outline(_ sender: Any) {
  1514. if (self.canResponseDocumentAction() == false) {
  1515. return
  1516. }
  1517. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1518. document.mainViewController?.menuItemAction_outline(sender)
  1519. }
  1520. }
  1521. func menuItemAction_bookmark(_ sender: Any) {
  1522. if (self.canResponseDocumentAction() == false) {
  1523. return
  1524. }
  1525. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1526. document.mainViewController?.menuItemAction_bookmark(sender)
  1527. }
  1528. }
  1529. func menuItemAction_annotation(_ sender: Any) {
  1530. if (self.canResponseDocumentAction() == false) {
  1531. return
  1532. }
  1533. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1534. document.mainViewController?.menuItemAction_annotation(sender)
  1535. }
  1536. }
  1537. func menuItemAction_search(_ sender: Any) {
  1538. if (self.canResponseDocumentAction() == false) {
  1539. return
  1540. }
  1541. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1542. document.mainViewController?.menuItemAction_search(sender)
  1543. }
  1544. }
  1545. func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  1546. if (self.canResponseDocumentAction() == false) {
  1547. return
  1548. }
  1549. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1550. document.mainViewController?.menuItemAction_hiddenPageIndicator(sender)
  1551. }
  1552. }
  1553. @IBAction func togglePresentation(_ sender: Any?) {
  1554. if (self.canResponseDocumentAction() == false) {
  1555. return
  1556. }
  1557. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1558. document.mainViewController?.togglePresentation(sender)
  1559. }
  1560. }
  1561. @IBAction func chooseTransition(_ sender: Any?) {
  1562. if (self.canResponseDocumentAction() == false) {
  1563. return
  1564. }
  1565. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1566. document.mainViewController?.chooseTransition(sender)
  1567. }
  1568. }
  1569. @IBAction func toggleAutoFlow(_ sender: Any?) {
  1570. if (self.canResponseDocumentAction() == false) {
  1571. return
  1572. }
  1573. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1574. document.mainViewController?.toggleAutoFlow(sender)
  1575. }
  1576. }
  1577. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  1578. if (self.canResponseDocumentAction() == false) {
  1579. return
  1580. }
  1581. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1582. document.mainViewController?.chooseAutoFlowSetting(sender)
  1583. }
  1584. }
  1585. @IBAction func toggleReadingBar(_ sender: Any?) {
  1586. if (self.canResponseDocumentAction() == false) {
  1587. return
  1588. }
  1589. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1590. document.mainViewController?.toggleReadingBar(sender)
  1591. }
  1592. }
  1593. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  1594. if (self.canResponseDocumentAction() == false) {
  1595. return
  1596. }
  1597. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1598. document.mainViewController?.toggleLeftSidePane(sender)
  1599. }
  1600. }
  1601. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  1602. if (self.canResponseDocumentAction() == false) {
  1603. return
  1604. }
  1605. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1606. document.mainViewController?.changeLeftSidePaneState(sender)
  1607. }
  1608. }
  1609. @IBAction func splitViewAction(_ sender: Any?) {
  1610. if (self.canResponseDocumentAction() == false) {
  1611. return
  1612. }
  1613. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1614. document.mainViewController?.splitViewAction(sender)
  1615. }
  1616. }
  1617. @IBAction func themesColor(_ sender: Any?) {
  1618. if (self.canResponseDocumentAction() == false) {
  1619. return
  1620. }
  1621. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1622. document.mainViewController?.themesColor(sender)
  1623. }
  1624. }
  1625. @IBAction func toggleStatusBar(_ sender: Any?) {
  1626. if (self.canResponseDocumentAction() == false) {
  1627. return
  1628. }
  1629. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1630. document.mainViewController?.toggleStatusBar(sender)
  1631. }
  1632. }
  1633. @IBAction func toggleToolbarShow(_ sender: Any?) {
  1634. if (self.canResponseDocumentAction() == false) {
  1635. return
  1636. }
  1637. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1638. document.mainViewController?.toggleToolbarShow(sender)
  1639. }
  1640. }
  1641. }
  1642. // MARK: - KMSystemEditMenuProtocol
  1643. extension KMBrowserWindowController: KMSystemEditMenuProtocol {
  1644. func menuItemAction_find(_ sender: Any) {
  1645. if (self.canResponseDocumentAction() == false) {
  1646. return
  1647. }
  1648. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1649. document.mainViewController?.menuItemAction_find(sender)
  1650. }
  1651. }
  1652. @IBAction func changeFont(_ sender: Any?) {
  1653. KMPrint("changeFont ...")
  1654. }
  1655. }
  1656. // MARK: - Annotation Menu
  1657. extension KMBrowserWindowController {
  1658. @IBAction func menuItemAction_highlight(_ sender: Any) {
  1659. if (self.canResponseDocumentAction() == false) {
  1660. return
  1661. }
  1662. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1663. document.mainViewController?.menuItemAction_highlight(sender)
  1664. }
  1665. }
  1666. @IBAction func menuItemAction_underline(_ sender: Any) {
  1667. if (self.canResponseDocumentAction() == false) {
  1668. return
  1669. }
  1670. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1671. document.mainViewController?.menuItemAction_underline(sender)
  1672. }
  1673. }
  1674. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  1675. if (self.canResponseDocumentAction() == false) {
  1676. return
  1677. }
  1678. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1679. document.mainViewController?.menuItemAction_deleteLine(sender)
  1680. }
  1681. }
  1682. @IBAction func menuItemAction_freehand(_ sender: Any) {
  1683. if (self.canResponseDocumentAction() == false) {
  1684. return
  1685. }
  1686. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1687. document.mainViewController?.menuItemAction_freehand(sender)
  1688. }
  1689. }
  1690. @IBAction func menuItemAction_text(_ sender: Any) {
  1691. if (self.canResponseDocumentAction() == false) {
  1692. return
  1693. }
  1694. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1695. document.mainViewController?.menuItemAction_text(sender)
  1696. }
  1697. }
  1698. @IBAction func menuItemAction_note(_ sender: Any) {
  1699. if (self.canResponseDocumentAction() == false) {
  1700. return
  1701. }
  1702. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1703. document.mainViewController?.menuItemAction_note(sender)
  1704. }
  1705. }
  1706. @IBAction func menuItemAction_squre(_ sender: Any) {
  1707. if (self.canResponseDocumentAction() == false) {
  1708. return
  1709. }
  1710. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1711. document.mainViewController?.menuItemAction_squre(sender)
  1712. }
  1713. }
  1714. @IBAction func menuItemAction_circle(_ sender: Any) {
  1715. if (self.canResponseDocumentAction() == false) {
  1716. return
  1717. }
  1718. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1719. document.mainViewController?.menuItemAction_circle(sender)
  1720. }
  1721. }
  1722. @IBAction func menuItemAction_arrow(_ sender: Any) {
  1723. if (self.canResponseDocumentAction() == false) {
  1724. return
  1725. }
  1726. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1727. document.mainViewController?.menuItemAction_arrow(sender)
  1728. }
  1729. }
  1730. @IBAction func menuItemAction_line(_ sender: Any) {
  1731. if (self.canResponseDocumentAction() == false) {
  1732. return
  1733. }
  1734. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1735. document.mainViewController?.menuItemAction_line(sender)
  1736. }
  1737. }
  1738. // link
  1739. @IBAction func menuItemAction_link(_ sender: Any) {
  1740. if (self.canResponseDocumentAction() == false) {
  1741. return
  1742. }
  1743. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1744. document.mainViewController?.menuItemAction_link(sender)
  1745. }
  1746. }
  1747. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  1748. if (self.canResponseDocumentAction() == false) {
  1749. return
  1750. }
  1751. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1752. document.mainViewController?.menuItemAction_linkPage(sender)
  1753. }
  1754. }
  1755. // stamp
  1756. @IBAction func menuItemAction_stamp(_ sender: Any) {
  1757. if (self.canResponseDocumentAction() == false) {
  1758. return
  1759. }
  1760. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1761. document.mainViewController?.menuItemAction_stamp(sender)
  1762. }
  1763. }
  1764. @IBAction func notesLink(_ sender: Any?) {
  1765. if (self.canResponseDocumentAction() == false) {
  1766. return
  1767. }
  1768. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1769. document.mainViewController?.notesLink(sender)
  1770. }
  1771. }
  1772. @IBAction func notesStamp(_ sender: Any?) {
  1773. if (self.canResponseDocumentAction() == false) {
  1774. return
  1775. }
  1776. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1777. document.mainViewController?.notesStamp(sender)
  1778. }
  1779. }
  1780. @IBAction func menuItemAction_signure(_ sender: Any) {
  1781. if (self.canResponseDocumentAction() == false) {
  1782. return
  1783. }
  1784. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1785. document.mainViewController?.menuItemAction_signure(sender)
  1786. }
  1787. }
  1788. @IBAction func image(_ sender: Any?) {
  1789. if (self.canResponseDocumentAction() == false) {
  1790. return
  1791. }
  1792. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1793. document.mainViewController?.image(sender)
  1794. }
  1795. }
  1796. @IBAction func table(_ sender: Any?) {
  1797. if (self.canResponseDocumentAction() == false) {
  1798. return
  1799. }
  1800. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1801. document.mainViewController?.table(sender)
  1802. }
  1803. }
  1804. @IBAction func addForm(_ sender: Any?) {
  1805. if (self.canResponseDocumentAction() == false) {
  1806. return
  1807. }
  1808. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1809. document.mainViewController?.table(addForm)
  1810. }
  1811. }
  1812. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  1813. if (self.canResponseDocumentAction() == false) {
  1814. return
  1815. }
  1816. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1817. document.mainViewController?.menuItemAction_hiddenAllAnnotation(sender)
  1818. }
  1819. }
  1820. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  1821. if (self.canResponseDocumentAction() == false) {
  1822. return
  1823. }
  1824. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1825. document.mainViewController?.menuItemAction_clearAllAnnotation(sender)
  1826. }
  1827. }
  1828. }
  1829. // MARK: - KMSystemPDFMenuProtocol
  1830. extension KMBrowserWindowController: KMSystemPDFMenuProtocol {
  1831. @IBAction func changeDisplayMode(_ sender: Any?) {
  1832. if (self.canResponseDocumentAction() == false) {
  1833. return
  1834. }
  1835. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1836. document.mainViewController?.changeDisplayMode(sender)
  1837. }
  1838. }
  1839. @IBAction func toggleDisplayAsBook(_ sender: Any?) {
  1840. if (self.canResponseDocumentAction() == false) {
  1841. return
  1842. }
  1843. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1844. document.mainViewController?.toggleDisplayAsBook(sender)
  1845. }
  1846. }
  1847. @IBAction func toggleDisplayPageBreaks(_ sender: Any?) {
  1848. if (self.canResponseDocumentAction() == false) {
  1849. return
  1850. }
  1851. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1852. document.mainViewController?.toggleDisplayPageBreaks(sender)
  1853. }
  1854. }
  1855. @IBAction func changeDisplayBox(_ sender: Any?) {
  1856. if (self.canResponseDocumentAction() == false) {
  1857. return
  1858. }
  1859. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1860. document.mainViewController?.changeDisplayBox(sender)
  1861. }
  1862. }
  1863. @IBAction func toggleAutoScale(_ sender: Any?) {
  1864. if (self.canResponseDocumentAction() == false) {
  1865. return
  1866. }
  1867. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1868. document.mainViewController?.toggleAutoScale(sender)
  1869. }
  1870. }
  1871. @IBAction func doZoomToActualSize(_ sender: Any?) {
  1872. if (self.canResponseDocumentAction() == false) {
  1873. return
  1874. }
  1875. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1876. document.mainViewController?.doZoomToActualSize(sender)
  1877. }
  1878. }
  1879. @IBAction func doZoomToFit(_ sender: Any?) {
  1880. if (self.canResponseDocumentAction() == false) {
  1881. return
  1882. }
  1883. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1884. document.mainViewController?.doZoomToFit(sender)
  1885. }
  1886. }
  1887. @IBAction func doZoomIn(_ sender: Any?) {
  1888. if (self.canResponseDocumentAction() == false) {
  1889. return
  1890. }
  1891. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1892. document.mainViewController?.doZoomIn(sender)
  1893. }
  1894. }
  1895. @IBAction func doZoomOut(_ sender: Any?) {
  1896. if (self.canResponseDocumentAction() == false) {
  1897. return
  1898. }
  1899. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1900. document.mainViewController?.doZoomOut(sender)
  1901. }
  1902. }
  1903. @IBAction func doMarqueeZoomTool(_ sender: Any?) {
  1904. if (self.canResponseDocumentAction() == false) {
  1905. return
  1906. }
  1907. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1908. document.mainViewController?.doMarqueeZoomTool(sender)
  1909. }
  1910. }
  1911. @IBAction func highlightFormFiled(_ sender: Any?) {
  1912. if (self.canResponseDocumentAction() == false) {
  1913. return
  1914. }
  1915. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1916. document.mainViewController?.highlightFormFiled(sender)
  1917. }
  1918. }
  1919. @IBAction func highlightLinks(_ sender: Any?) {
  1920. if (self.canResponseDocumentAction() == false) {
  1921. return
  1922. }
  1923. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1924. document.mainViewController?.highlightLinks(sender)
  1925. }
  1926. }
  1927. @IBAction func resetForm(_ sender: Any?) {
  1928. if (self.canResponseDocumentAction() == false) {
  1929. return
  1930. }
  1931. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1932. document.mainViewController?.resetForm(sender)
  1933. }
  1934. }
  1935. @IBAction func removeAllAnnotation(_ sender: Any?) {
  1936. if (self.canResponseDocumentAction() == false) {
  1937. return
  1938. }
  1939. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1940. document.mainViewController?.removeAllAnnotation(sender)
  1941. }
  1942. }
  1943. @IBAction func savePDFSettingToDefaults(_ sender: Any?) {
  1944. if (self.canResponseDocumentAction() == false) {
  1945. return
  1946. }
  1947. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1948. document.mainViewController?.savePDFSettingToDefaults(sender)
  1949. }
  1950. }
  1951. }
  1952. // MARK: - KMSystemGotoMenuProtocol
  1953. extension KMBrowserWindowController: KMSystemGotoMenuProtocol {
  1954. func menuItemAction_nextPage(_ sender: Any) {
  1955. if (self.canResponseDocumentAction() == false) {
  1956. return
  1957. }
  1958. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1959. document.mainViewController?.menuItemAction_nextPage(sender)
  1960. }
  1961. }
  1962. func menuItemAction_forwardPage(_ sender: Any) {
  1963. if (self.canResponseDocumentAction() == false) {
  1964. return
  1965. }
  1966. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1967. document.mainViewController?.menuItemAction_forwardPage(sender)
  1968. }
  1969. }
  1970. func menuItemAction_firstPage(_ sender: Any) {
  1971. if (self.canResponseDocumentAction() == false) {
  1972. return
  1973. }
  1974. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1975. document.mainViewController?.menuItemAction_firstPage(sender)
  1976. }
  1977. }
  1978. func menuItemAction_lastPage(_ sender: Any) {
  1979. if (self.canResponseDocumentAction() == false) {
  1980. return
  1981. }
  1982. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1983. document.mainViewController?.menuItemAction_lastPage(sender)
  1984. }
  1985. }
  1986. func menuItemAction_forward(_ sender: Any) {
  1987. if (self.canResponseDocumentAction() == false) {
  1988. return
  1989. }
  1990. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1991. document.mainViewController?.menuItemAction_forward(sender)
  1992. }
  1993. }
  1994. func menuItemAction_goback(_ sender: Any) {
  1995. if (self.canResponseDocumentAction() == false) {
  1996. return
  1997. }
  1998. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1999. document.mainViewController?.menuItemAction_goback(sender)
  2000. }
  2001. }
  2002. func menuItemAction_gotoPage(_ sender: Any) {
  2003. if (self.canResponseDocumentAction() == false) {
  2004. return
  2005. }
  2006. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2007. document.mainViewController?.menuItemAction_gotoPage(sender)
  2008. }
  2009. }
  2010. @IBAction func goToMarkedPage(_ sender: Any?) {
  2011. if (self.canResponseDocumentAction() == false) {
  2012. return
  2013. }
  2014. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2015. document.mainViewController?.goToMarkedPage(sender)
  2016. }
  2017. }
  2018. @IBAction func markPage(_ sender: Any?) {
  2019. if (self.canResponseDocumentAction() == false) {
  2020. return
  2021. }
  2022. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2023. document.mainViewController?.markPage(sender)
  2024. }
  2025. }
  2026. }
  2027. // MARK: - KMSystemToolMenuProtocol
  2028. extension KMBrowserWindowController: KMSystemToolMenuProtocol {
  2029. @IBAction func getInfo(_ sender: Any?) {
  2030. if (self.canResponseDocumentAction() == false) {
  2031. return
  2032. }
  2033. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2034. document.mainViewController?.getInfo(sender)
  2035. }
  2036. }
  2037. func orderFrontNotesPanel(_ sender: Any?) {
  2038. let windowC = KMNotesPanelController.shared
  2039. if let win = windowC.window {
  2040. if win.isVisible {
  2041. win.orderOut(sender)
  2042. } else {
  2043. win.orderFront(sender)
  2044. }
  2045. }
  2046. }
  2047. func menuItemAction_scrolTool(_ sender: Any) {
  2048. if (self.canResponseDocumentAction() == false) {
  2049. return
  2050. }
  2051. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2052. document.mainViewController?.menuItemAction_scrolTool(sender)
  2053. }
  2054. }
  2055. func menuItemAction_zoomOutTool(_ sender: Any) {
  2056. if (self.canResponseDocumentAction() == false) {
  2057. return
  2058. }
  2059. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2060. document.mainViewController?.menuItemAction_zoomOutTool(sender)
  2061. }
  2062. }
  2063. func menuItemAction_selectTool(_ sender: Any) {
  2064. if (self.canResponseDocumentAction() == false) {
  2065. return
  2066. }
  2067. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  2068. document.mainViewController?.menuItemAction_selectTool(sender)
  2069. }
  2070. }
  2071. @IBAction func changeToolMode(_ sender: Any?) {
  2072. if (self.canResponseDocumentAction() == false) {
  2073. return
  2074. }
  2075. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2076. if (document.isHome) {
  2077. } else {
  2078. document.mainViewController?.changeToolMode(sender)
  2079. }
  2080. }
  2081. }
  2082. @IBAction func compression(_ sender: Any?) {
  2083. if (self.canResponseDocumentAction() == false) {
  2084. return
  2085. }
  2086. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2087. if (document.isHome) {
  2088. } else {
  2089. document.mainViewController?.compression(sender)
  2090. }
  2091. }
  2092. }
  2093. @IBAction func merge(_ sender: Any?) {
  2094. if (self.canResponseDocumentAction() == false) {
  2095. return
  2096. }
  2097. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2098. if (document.isHome) {
  2099. } else {
  2100. document.mainViewController?.merge(sender)
  2101. }
  2102. }
  2103. }
  2104. @IBAction func convert(_ sender: Any?) {
  2105. if (self.canResponseDocumentAction() == false) {
  2106. return
  2107. }
  2108. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2109. if (document.isHome) {
  2110. } else {
  2111. document.mainViewController?.convert(sender)
  2112. }
  2113. }
  2114. }
  2115. @IBAction func imageToPDF(_ sender: Any?) {
  2116. if (self.canResponseDocumentAction() == false) {
  2117. return
  2118. }
  2119. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2120. if (document.isHome) {
  2121. } else {
  2122. document.mainViewController?.imageToPDF(sender)
  2123. }
  2124. }
  2125. }
  2126. @IBAction func ocr(_ sender: Any?) {
  2127. if (self.canResponseDocumentAction() == false) {
  2128. return
  2129. }
  2130. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2131. if (document.isHome) {
  2132. } else {
  2133. document.mainViewController?.ocr(sender)
  2134. }
  2135. }
  2136. }
  2137. @IBAction func extractImage(_ sender: Any?) {
  2138. if (self.canResponseDocumentAction() == false) {
  2139. return
  2140. }
  2141. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2142. if (document.isHome) {
  2143. } else {
  2144. document.mainViewController?.extractImage(sender)
  2145. }
  2146. }
  2147. }
  2148. @IBAction func pageEdit(_ sender: Any?) {
  2149. if (self.canResponseDocumentAction() == false) {
  2150. return
  2151. }
  2152. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2153. if (document.isHome) {
  2154. } else {
  2155. document.mainViewController?.pageEdit(sender)
  2156. }
  2157. }
  2158. }
  2159. @IBAction func textEditAction(_ sender: Any?) {
  2160. if (self.canResponseDocumentAction() == false) {
  2161. return
  2162. }
  2163. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2164. if (document.isHome) {
  2165. } else {
  2166. document.mainViewController?.textEditAction(sender)
  2167. }
  2168. }
  2169. }
  2170. @IBAction func security(_ sender: Any?) {
  2171. if (self.canResponseDocumentAction() == false) {
  2172. return
  2173. }
  2174. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2175. if (document.isHome) {
  2176. } else {
  2177. document.mainViewController?.security(sender)
  2178. }
  2179. }
  2180. }
  2181. @IBAction func decryption(_ sender: Any?) {
  2182. if (self.canResponseDocumentAction() == false) {
  2183. return
  2184. }
  2185. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2186. if (document.isHome) {
  2187. } else {
  2188. document.mainViewController?.decryption(sender)
  2189. }
  2190. }
  2191. }
  2192. @IBAction func cropAll(_ sender: Any?) {
  2193. if (self.canResponseDocumentAction() == false) {
  2194. return
  2195. }
  2196. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2197. if (document.isHome) {
  2198. } else {
  2199. document.mainViewController?.cropAll(sender)
  2200. }
  2201. }
  2202. }
  2203. @IBAction func crop(_ sender: Any?) {
  2204. if (self.canResponseDocumentAction() == false) {
  2205. return
  2206. }
  2207. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2208. if (document.isHome) {
  2209. } else {
  2210. document.mainViewController?.crop(sender)
  2211. }
  2212. }
  2213. }
  2214. @IBAction func autoCropAll(_ sender: Any?) {
  2215. if (self.canResponseDocumentAction() == false) {
  2216. return
  2217. }
  2218. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2219. if (document.isHome) {
  2220. } else {
  2221. document.mainViewController?.autoCropAll(sender)
  2222. }
  2223. }
  2224. }
  2225. @IBAction func smartAutoCropAll(_ sender: Any?) {
  2226. if (self.canResponseDocumentAction() == false) {
  2227. return
  2228. }
  2229. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2230. if (document.isHome) {
  2231. } else {
  2232. document.mainViewController?.smartAutoCropAll(sender)
  2233. }
  2234. }
  2235. }
  2236. @IBAction func cropOptionsModel(_ sender: Any?) {
  2237. if (self.canResponseDocumentAction() == false) {
  2238. return
  2239. }
  2240. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2241. if (document.isHome) {
  2242. } else {
  2243. // document.mainViewController?.cropOptionsModel(sender)
  2244. }
  2245. }
  2246. }
  2247. @IBAction func selectToolModel(_ sender: Any?) {
  2248. if (self.canResponseDocumentAction() == false) {
  2249. return
  2250. }
  2251. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2252. if (document.isHome) {
  2253. } else {
  2254. document.mainViewController?.selectToolModel(sender)
  2255. }
  2256. }
  2257. }
  2258. @IBAction func takeSnapshot(_ sender: Any?) {
  2259. if (self.canResponseDocumentAction() == false) {
  2260. return
  2261. }
  2262. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2263. if (document.isHome) {
  2264. } else {
  2265. document.mainViewController?.takeSnapshot(sender)
  2266. }
  2267. }
  2268. }
  2269. @IBAction func takeSnapshotSelectContent(_ sender: Any?) {
  2270. if (self.canResponseDocumentAction() == false) {
  2271. return
  2272. }
  2273. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2274. if (document.isHome) {
  2275. } else {
  2276. document.mainViewController?.takeSnapshotSelectContent(sender)
  2277. }
  2278. }
  2279. }
  2280. @IBAction func changeAnnotationMode(_ sender: Any?) {
  2281. if (self.canResponseDocumentAction() == false) {
  2282. return
  2283. }
  2284. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2285. if (document.isHome) {
  2286. } else {
  2287. document.mainViewController?.changeAnnotationMode(sender)
  2288. }
  2289. }
  2290. }
  2291. @IBAction func addBackground(_ sender: Any?) {
  2292. if (self.canResponseDocumentAction() == false) {
  2293. return
  2294. }
  2295. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2296. if (document.isHome) {
  2297. } else {
  2298. document.mainViewController?.addBackground(sender)
  2299. }
  2300. }
  2301. }
  2302. @IBAction func removeBackground(_ sender: Any?) {
  2303. if (self.canResponseDocumentAction() == false) {
  2304. return
  2305. }
  2306. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2307. if (document.isHome) {
  2308. } else {
  2309. document.mainViewController?.removeBackground(sender)
  2310. }
  2311. }
  2312. }
  2313. @IBAction func addWatermark(_ sender: Any?) {
  2314. if (self.canResponseDocumentAction() == false) {
  2315. return
  2316. }
  2317. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2318. if (document.isHome) {
  2319. } else {
  2320. document.mainViewController?.addWatermark(sender)
  2321. }
  2322. }
  2323. }
  2324. @IBAction func editWatermark(_ sender: Any?) {
  2325. KMPrint("editWatermark ...")
  2326. }
  2327. @IBAction func batchAddWatermark(_ sender: Any?) {
  2328. if (self.canResponseDocumentAction() == false) {
  2329. return
  2330. }
  2331. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2332. if (document.isHome) {
  2333. } else {
  2334. document.mainViewController?.batchAddWatermark(sender)
  2335. }
  2336. }
  2337. }
  2338. @IBAction func batchRemoveWatermark(_ sender: Any?) {
  2339. if (self.canResponseDocumentAction() == false) {
  2340. return
  2341. }
  2342. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2343. if (document.isHome) {
  2344. } else {
  2345. document.mainViewController?.batchRemoveWatermark(sender)
  2346. }
  2347. }
  2348. }
  2349. @IBAction func removeWatermark(_ sender: Any?) {
  2350. if (self.canResponseDocumentAction() == false) {
  2351. return
  2352. }
  2353. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2354. if (document.isHome) {
  2355. } else {
  2356. document.mainViewController?.removeWatermark(sender)
  2357. }
  2358. }
  2359. }
  2360. @IBAction func templateWatermark(_ sender: Any?) {
  2361. if (self.canResponseDocumentAction() == false) {
  2362. return
  2363. }
  2364. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2365. if (document.isHome) {
  2366. } else {
  2367. document.mainViewController?.templateWatermark(sender)
  2368. }
  2369. }
  2370. }
  2371. @IBAction func batchAddBackground(_ sender: Any?) {
  2372. if (self.canResponseDocumentAction() == false) {
  2373. return
  2374. }
  2375. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2376. if (document.isHome) {
  2377. } else {
  2378. document.mainViewController?.batchAddBackground(sender)
  2379. }
  2380. }
  2381. }
  2382. @IBAction func batchRemoveBackground(_ sender: Any?) {
  2383. if (self.canResponseDocumentAction() == false) {
  2384. return
  2385. }
  2386. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2387. if (document.isHome) {
  2388. } else {
  2389. document.mainViewController?.batchRemoveBackground(sender)
  2390. }
  2391. }
  2392. }
  2393. @IBAction func templateBackground(_ sender: Any?) {
  2394. if (self.canResponseDocumentAction() == false) {
  2395. return
  2396. }
  2397. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2398. if (document.isHome) {
  2399. } else {
  2400. document.mainViewController?.templateBackground(sender)
  2401. }
  2402. }
  2403. }
  2404. @IBAction func addHeaderFooter(_ sender: Any?) {
  2405. if (self.canResponseDocumentAction() == false) {
  2406. return
  2407. }
  2408. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2409. if (document.isHome) {
  2410. } else {
  2411. document.mainViewController?.addHeaderFooter(sender)
  2412. }
  2413. }
  2414. }
  2415. @IBAction func removeHeaderFooter(_ sender: Any?) {
  2416. if (self.canResponseDocumentAction() == false) {
  2417. return
  2418. }
  2419. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2420. if (document.isHome) {
  2421. } else {
  2422. document.mainViewController?.removeHeaderFooter(sender)
  2423. }
  2424. }
  2425. }
  2426. @IBAction func batchAddHeaderFooter(_ sender: Any?) {
  2427. if (self.canResponseDocumentAction() == false) {
  2428. return
  2429. }
  2430. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2431. if (document.isHome) {
  2432. } else {
  2433. document.mainViewController?.batchAddHeaderFooter(sender)
  2434. }
  2435. }
  2436. }
  2437. @IBAction func batchRemoveHeaderFooter(_ sender: Any?) {
  2438. if (self.canResponseDocumentAction() == false) {
  2439. return
  2440. }
  2441. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2442. if (document.isHome) {
  2443. } else {
  2444. document.mainViewController?.batchRemoveHeaderFooter(sender)
  2445. }
  2446. }
  2447. }
  2448. @IBAction func templateHeaderFooter(_ sender: Any?) {
  2449. if (self.canResponseDocumentAction() == false) {
  2450. return
  2451. }
  2452. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2453. if (document.isHome) {
  2454. } else {
  2455. document.mainViewController?.templateHeaderFooter(sender)
  2456. }
  2457. }
  2458. }
  2459. @IBAction func addHeaderFooterBates(_ sender: Any?) {
  2460. if (self.canResponseDocumentAction() == false) {
  2461. return
  2462. }
  2463. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2464. if (document.isHome) {
  2465. } else {
  2466. document.mainViewController?.addHeaderFooterBates(sender)
  2467. }
  2468. }
  2469. }
  2470. @IBAction func removeHeaderFooterBates(_ sender: Any?) {
  2471. if (self.canResponseDocumentAction() == false) {
  2472. return
  2473. }
  2474. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2475. if (document.isHome) {
  2476. } else {
  2477. document.mainViewController?.removeHeaderFooterBates(sender)
  2478. }
  2479. }
  2480. }
  2481. @IBAction func batchAddBates(_ sender: Any?) {
  2482. if (self.canResponseDocumentAction() == false) {
  2483. return
  2484. }
  2485. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2486. if (document.isHome) {
  2487. } else {
  2488. document.mainViewController?.batchAddBates(sender)
  2489. }
  2490. }
  2491. }
  2492. @IBAction func batchRemoveBates(_ sender: Any?) {
  2493. if (self.canResponseDocumentAction() == false) {
  2494. return
  2495. }
  2496. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2497. if (document.isHome) {
  2498. } else {
  2499. document.mainViewController?.batchRemoveBates(sender)
  2500. }
  2501. }
  2502. }
  2503. @IBAction func templateBates(_ sender: Any?) {
  2504. if (self.canResponseDocumentAction() == false) {
  2505. return
  2506. }
  2507. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2508. if (document.isHome) {
  2509. } else {
  2510. document.mainViewController?.templateBates(sender)
  2511. }
  2512. }
  2513. }
  2514. @IBAction func togglePoster(_ sender: Any?) {
  2515. if (self.canResponseDocumentAction() == false) {
  2516. return
  2517. }
  2518. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2519. if (document.isHome) {
  2520. } else {
  2521. document.mainViewController?.togglePoster(sender)
  2522. }
  2523. }
  2524. }
  2525. @IBAction func toggleMultiple(_ sender: Any?) {
  2526. if (self.canResponseDocumentAction() == false) {
  2527. return
  2528. }
  2529. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2530. if (document.isHome) {
  2531. } else {
  2532. document.mainViewController?.toggleMultiple(sender)
  2533. }
  2534. }
  2535. }
  2536. @IBAction func toggleBooklet(_ sender: Any?) {
  2537. if (self.canResponseDocumentAction() == false) {
  2538. return
  2539. }
  2540. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2541. if (document.isHome) {
  2542. } else {
  2543. document.mainViewController?.toggleBooklet(sender)
  2544. }
  2545. }
  2546. }
  2547. @IBAction func rotateRight(_ sender: Any?) {
  2548. if (self.canResponseDocumentAction() == false) {
  2549. return
  2550. }
  2551. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2552. if (document.isHome) {
  2553. } else {
  2554. document.mainViewController?.rotateRight(sender)
  2555. }
  2556. }
  2557. }
  2558. @IBAction func rotateLeft(_ sender: Any?) {
  2559. if (self.canResponseDocumentAction() == false) {
  2560. return
  2561. }
  2562. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2563. if (document.isHome) {
  2564. } else {
  2565. document.mainViewController?.rotateLeft(sender)
  2566. }
  2567. }
  2568. }
  2569. @IBAction func rotateAllRight(_ sender: Any?) {
  2570. if (self.canResponseDocumentAction() == false) {
  2571. return
  2572. }
  2573. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2574. if (document.isHome) {
  2575. } else {
  2576. document.mainViewController?.rotateAllRight(sender)
  2577. }
  2578. }
  2579. }
  2580. @IBAction func rotateAllLeft(_ sender: Any?) {
  2581. if (self.canResponseDocumentAction() == false) {
  2582. return
  2583. }
  2584. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2585. if (document.isHome) {
  2586. } else {
  2587. document.mainViewController?.rotateAllLeft(sender)
  2588. }
  2589. }
  2590. }
  2591. @IBAction func autoSelectContent(_ sender: Any?) {
  2592. if (self.canResponseDocumentAction() == false) {
  2593. return
  2594. }
  2595. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2596. if (document.isHome) {
  2597. } else {
  2598. document.mainViewController?.autoSelectContent(sender)
  2599. }
  2600. }
  2601. }
  2602. }
  2603. // MARK: - KMSystemWindowMenuRrotocol
  2604. extension KMBrowserWindowController: KMSystemWindowMenuRrotocol {
  2605. func menuItemAction_showForwardTagPage(_ sender: Any) {
  2606. if (self.canResponseDocumentAction() == false) {
  2607. return
  2608. }
  2609. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2610. if (document.isHome) {
  2611. document.homeViewController?.menuItemAction_showForwardTagPage(sender)
  2612. } else {
  2613. document.mainViewController?.menuItemAction_showForwardTagPage(sender)
  2614. }
  2615. }
  2616. }
  2617. func menuItemAction_showNextTagPage(_ sender: Any) {
  2618. if (self.canResponseDocumentAction() == false) {
  2619. return
  2620. }
  2621. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2622. if (document.isHome) {
  2623. document.homeViewController?.menuItemAction_showNextTagPage(sender)
  2624. } else {
  2625. document.mainViewController?.menuItemAction_showNextTagPage(sender)
  2626. }
  2627. }
  2628. }
  2629. func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  2630. if (self.canResponseDocumentAction() == false) {
  2631. return
  2632. }
  2633. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2634. if (document.isHome) {
  2635. document.homeViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2636. } else {
  2637. document.mainViewController?.menuItemAction_newTagPageToNewWindow(sender)
  2638. }
  2639. }
  2640. }
  2641. func menuItemAction_mergeAllWindow(_ sender: Any) {
  2642. if (self.canResponseDocumentAction() == false) {
  2643. return
  2644. }
  2645. if let document = self.browser.activeTabContents() as? KMMainDocument {
  2646. if (document.isHome) {
  2647. document.homeViewController?.menuItemAction_mergeAllWindow(sender)
  2648. } else {
  2649. document.mainViewController?.menuItemAction_mergeAllWindow(sender)
  2650. }
  2651. }
  2652. }
  2653. }
  2654. // MARK: - Analytics (埋点)
  2655. extension KMBrowserWindowController {
  2656. func trackEvent_upgrade() -> Void {
  2657. KMAnalytics.trackEvent(eventName: "Btn_Tbr_Upgrade", parameters: [
  2658. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.tbr,
  2659. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.tbr_Btn], platform: .AppCenter, appTarget: .all)
  2660. }
  2661. }