KMBrowserWindowController.swift 120 KB

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