KMBrowserWindowController.swift 117 KB

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