KMBrowserWindowController.swift 121 KB

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