KMBrowserWindowController.swift 119 KB

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