KMBrowserWindowController.swift 118 KB

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