KMBrowserWindowController.swift 119 KB

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