KMBrowserWindowController.swift 121 KB

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