KMBrowserWindowController.swift 120 KB

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