KMBrowserWindowController.swift 117 KB

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