KMBrowserWindowController.swift 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. //
  2. // KMBrowserWindowController.swift
  3. // PDF Master
  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. override func windowDidLoad() {
  17. super.windowDidLoad()
  18. window?.backgroundColor = NSColor(hex: "#DFE1E5")
  19. // window?.setFrameAutosaveName("")
  20. rightTabStripView_.delete = self
  21. homeRightTabStripView_.delete = self
  22. rightTabStripView_.updateView()
  23. fileUploadPanel.delete = self
  24. homeRightTabStripView_.homeRightSearchField.delegate = self
  25. isMultiTabMode = true
  26. addObserverForAppearanceChange()
  27. refreshToolBar(homeToolState: .Home)
  28. NotificationCenter.default.addObserver(self, selector: #selector(closeAllTabs(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseAllTabs"), object: nil)
  29. NotificationCenter.default.addObserver(self, selector: #selector(openNewWindow(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerOpenNewWindow"), object: nil)
  30. if (WelcomeWindowController.welcomeHasShow() == false) {
  31. KMTools.openQuickStartStudy()
  32. DispatchQueue.main.async {
  33. let welcome = WelcomeWindowController()
  34. self.window?.beginSheet(welcome.window!)
  35. welcome.itemClick = { [weak self] idx, param in
  36. if (idx == 1) { // 关闭
  37. self?.window?.endSheet((param as! NSWindowController).window!)
  38. } else if (idx == 2) { // 以后提醒
  39. self?.window?.endSheet((param as! NSWindowController).window!)
  40. } else if (idx == 3) { // 注册
  41. self?.window?.endSheet((param as! NSWindowController).window!)
  42. guard let window_ = self?.window else {
  43. return
  44. }
  45. let _ = KMLoginWindowController.show(window: window_, .Batch, .register)
  46. }
  47. }
  48. }
  49. }
  50. }
  51. override func windowShouldClose(_ sender: NSWindow) -> Bool {
  52. if self.browser.tabStripModel.count() > 1 {
  53. self.browser.windowDidBeginToClose()
  54. return false
  55. }
  56. return true
  57. }
  58. // MARK: Dark&Light
  59. func addObserverForAppearanceChange() -> Void {
  60. window?.contentView!.addObserver(self, forKeyPath: "effectiveAppearance", options: .new, context: nil)
  61. }
  62. func removeObserverForAppearanceChange() -> Void {
  63. window?.contentView!.removeObserver(self, forKeyPath: "effectiveAppearance")
  64. }
  65. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  66. if keyPath == "effectiveAppearance" {
  67. updateViewColor()
  68. }
  69. }
  70. override func updateViewColor() {
  71. super.updateViewColor()
  72. window?.backgroundColor = KMTabAppearance.tabsViewBackgroundColor()
  73. }
  74. // MARK: Public Methods
  75. func refreshToolBar(homeToolState toolState: KMHomeToolState) -> Void {
  76. modeType = toolState
  77. let document = browser.activeTabContents()
  78. if document != nil {
  79. if document!.isHome {
  80. homeRightTabStripView_.isHidden = modeType == .Home
  81. } else {
  82. self.homeRightStripView.isHidden = true
  83. }
  84. } else {
  85. self.homeRightStripView.isHidden = true
  86. }
  87. homeRightTabStripView_.homeRefreshButton.isEnabled = true
  88. homeRightTabStripView_.homeUploadButton.isEnabled = true
  89. filterType = 0
  90. rightTabStripView_.sortPopUpButton.removeAllItems()
  91. rightTabStripView_.filterPopUpButton.removeAllItems()
  92. rightTabStripView_.updateView()
  93. }
  94. override var hasToolbar: Bool {
  95. get {
  96. return false
  97. }
  98. }
  99. // MARK: Private Methods
  100. func openDocumentWindow() -> Void {
  101. // let panel = NSOpenPanel()
  102. // panel.allowsMultipleSelection = false
  103. // panel.allowedFileTypes = ["pdf"]
  104. // panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  105. // if response == .OK {
  106. // let openPath = panel.url?.path
  107. // let selectDocument = self.selectTabContents(path: openPath!)
  108. // if selectDocument != nil {
  109. // let currentIndex = selectDocument!.browser.tabStripModel.index(of: selectDocument)
  110. // self.browser.tabStripModel.selectTabContents(at: Int32(currentIndex), userGesture: true)
  111. // return
  112. // }
  113. // if !panel.url!.path.isPDFValid() {
  114. // let alert = NSAlert()
  115. // alert.alertStyle = .critical
  116. // alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  117. // alert.runModal()
  118. // return
  119. // }
  120. // NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { pdfDocument, documentWasAlreadyOpen, error in
  121. // print("openDocumentWindow")
  122. // }
  123. // }
  124. // }
  125. // MARK TODO: + 号开启
  126. self.browser.addHomeTabContents()
  127. }
  128. // override func openDocument(_ sender: Any!) {
  129. // self.openDocumentWindow()
  130. // }
  131. func selectTabContents(path filePath: String) -> KMMainDocument? {
  132. let document = NSDocumentController.shared.document(for: URL(fileURLWithPath: filePath))
  133. var selectDocument: KMMainDocument?
  134. if document != nil {
  135. if document!.isKind(of: KMMainDocument.self) {
  136. selectDocument = document as! KMMainDocument
  137. }
  138. }
  139. return selectDocument
  140. }
  141. // MARK: Getter
  142. override var rightStripView: NSView! {
  143. get {
  144. return rightTabStripView_
  145. }
  146. }
  147. override var homeRightStripView: NSView! {
  148. get {
  149. return homeRightTabStripView_
  150. }
  151. }
  152. // MARK: Button Action
  153. func commandDispatch(_ sender: Any) -> Void {
  154. openDocumentWindow()
  155. }
  156. func closeAllTabs(_ sender: Any) -> Void {
  157. if self.browser != nil {
  158. self.browser.closeAllTabs()
  159. }
  160. }
  161. func openNewWindow(_ sender: Any) -> Void {
  162. if let noti = sender as? NSNotification, noti.object is CTTabController {
  163. if let window_ = (noti.object as! CTTabController).view.window, window_.isEqual(to: self.window) == false {
  164. return
  165. }
  166. }
  167. if self.browser != nil {
  168. let activeInt = self.browser.activeTabIndex()
  169. let activeTab = self.browser.activeTabContents()
  170. self.browser.closeTab(at: activeInt, makeHistory: false)
  171. let browser: KMBrowser = KMBrowser.init()
  172. browser.windowController = KMBrowserWindowController.init(browser: browser)
  173. browser.addHomeTabContents()
  174. browser.windowController.showWindow(self)
  175. // browser.add(activeTab, inForeground: false)
  176. // browser.selectTab(at: 1)
  177. if activeTab?.fileURL == nil {
  178. return
  179. }
  180. let pdfDoc = CPDFDocument.init(url: (activeTab?.fileURL)!)
  181. let document = NSDocumentController.shared.document(for: (activeTab?.fileURL)!)
  182. KMMainDocument().tryToUnlockDocument(pdfDoc!)
  183. if ((pdfDoc?.isLocked)! == true) {
  184. KMPasswordInputWindow.openWindow(window: self.window!, url: (activeTab?.fileURL)!) { result, password in
  185. if result == .cancel { /// 关闭
  186. return
  187. }
  188. /// 解密成功
  189. var selectDocument: KMMainDocument? = nil
  190. if ((document?.isKind(of: KMMainDocument.self)) != nil) {
  191. selectDocument = (document as! KMMainDocument)
  192. }
  193. if selectDocument != nil {
  194. let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument)
  195. selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true)
  196. if (selectDocument?.browser.window.isVisible)! as Bool {
  197. selectDocument?.browser.window.orderFront(nil)
  198. } else if (selectDocument?.browser.window.isMiniaturized)! as Bool {
  199. selectDocument?.browser.window.orderFront(nil)
  200. }
  201. } else {
  202. if !(activeTab?.fileURL)!.path.isPDFValid() {
  203. let alert = NSAlert()
  204. alert.alertStyle = .critical
  205. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  206. alert.runModal()
  207. return
  208. }
  209. NSDocumentController.shared.openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { document, documentWasAlreadyOpen, error in
  210. if error != nil {
  211. NSApp.presentError(error!)
  212. return
  213. }
  214. (document as! KMMainDocument).mainViewController?.password = password
  215. }
  216. }
  217. }
  218. } else {
  219. var selectDocument: KMMainDocument? = nil
  220. if ((document?.isKind(of: KMMainDocument.self)) != nil) {
  221. selectDocument = (document as! KMMainDocument)
  222. }
  223. if selectDocument != nil {
  224. let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument)
  225. selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true)
  226. if (selectDocument?.browser.window.isVisible)! as Bool {
  227. selectDocument?.browser.window.orderFront(nil)
  228. } else if (selectDocument?.browser.window.isMiniaturized)! as Bool {
  229. selectDocument?.browser.window.orderFront(nil)
  230. }
  231. } else {
  232. if !(activeTab?.fileURL)!.path.isPDFValid() {
  233. let alert = NSAlert()
  234. alert.alertStyle = .critical
  235. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  236. alert.runModal()
  237. return
  238. }
  239. NSDocumentController.shared.openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { document, documentWasAlreadyOpen, error in
  240. if error != nil {
  241. NSApp.presentError(error!)
  242. return
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }
  249. // MARK: 待补充[万军]
  250. func mergeAllWindow(_ sender: Any) {
  251. }
  252. override func tabDidSelect(_ notification: Notification!) {
  253. super.tabDidSelect(notification)
  254. if let _userInfo = notification.userInfo, let _newContents = _userInfo[CTTabNewContentsUserInfoKey] as? KMMainDocument {
  255. _newContents.addWindowController(self)
  256. }
  257. }
  258. override func layoutTabContentArea(_ frame: NSRect) {
  259. super.layoutTabContentArea(frame)
  260. self.rightStripView.autoresizingMask = [.minXMargin, .minYMargin]
  261. }
  262. }
  263. // MARK: - KMToolbarRightViewDelegate
  264. extension KMBrowserWindowController: KMToolbarRightViewDelegate {
  265. func pdfRightSegmentedControlAction(_ sender: KMSegmentedBox?) {
  266. KMComparativeTableViewController.show(window: self.window!)
  267. }
  268. func userInfoButtonAction(_ sender: NSButton) {
  269. if KMLightMemberManager.manager.isLogin() {
  270. Task { @MainActor in
  271. if await KMLightMemberManager.manager.canUseAdvanced(needNetworking: true) {
  272. KMAccountInfoWindowController.show(window: self.window!)
  273. } else {
  274. KMLoginWindowController.show(window: self.window!)
  275. }
  276. }
  277. } else {
  278. KMLoginWindowController.show(window: self.window!)
  279. }
  280. }
  281. func homeRefreshButtonAction(_ sender: NSButton?) {
  282. self.layoutSubviews()
  283. }
  284. func homeUploadButtonAction(_ sender: NSButton) {
  285. }
  286. func homeMenuSortAction(_ sender: NSPopUpButton) {
  287. }
  288. func homeMenuFilterAction(_ sender: NSPopUpButton) {
  289. }
  290. }
  291. // MARK: - KMUploadFileDelegate
  292. extension KMBrowserWindowController: KMUploadFileDelegate {
  293. // override func cancelOperation(_ sender: Any?) {
  294. //
  295. // }
  296. }
  297. // MARK: - NSSearchFieldDelegate
  298. extension KMBrowserWindowController: NSSearchFieldDelegate {
  299. func controlTextDidChange(_ obj: Notification) {
  300. }
  301. func controlTextDidEndEditing(_ obj: Notification) {
  302. }
  303. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  304. var result = false
  305. return result
  306. }
  307. }
  308. // MARK: -
  309. // MARK: Menu Actions
  310. extension KMBrowserWindowController {
  311. func canResponseDocumentAction() -> Bool {
  312. if (self.browser == nil) {
  313. return false
  314. }
  315. guard let _ = self.browser.activeTabContents() as? KMMainDocument else {
  316. return false
  317. }
  318. return true
  319. }
  320. }
  321. // MARK: -
  322. // MARK: File Menu
  323. extension KMBrowserWindowController: NSMenuDelegate, NSMenuItemValidation {
  324. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  325. if (self.browser == nil) {
  326. return false
  327. }
  328. guard let action = menuItem.action else {
  329. return false
  330. }
  331. guard let document = self.browser.activeTabContents() as? KMMainDocument else {
  332. return false
  333. }
  334. if (KMSystemMenu.isFileSelector(sel: action)) {
  335. if (document.isHome) {
  336. if (menuItem.action == KMSystemMenu.File.closeTagPageSelector ||
  337. menuItem.action == KMSystemMenu.File.propertySelector ||
  338. menuItem.action == KMSystemMenu.File.showInFinderSelector ||
  339. menuItem.action == KMSystemMenu.File.printSelector) {
  340. return false
  341. }
  342. return document.homeViewController!.validateMenuItem(menuItem)
  343. } else {
  344. return document.mainViewController!.validateMenuItem(menuItem)
  345. }
  346. }
  347. if (KMSystemMenu.isEditSelector(sel: action)) {
  348. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  349. }
  350. if (KMSystemMenu.isViewSelector(sel: action)) {
  351. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  352. }
  353. if (KMSystemMenu.isAnnotationSelector(sel: action)) {
  354. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  355. }
  356. if (KMSystemMenu.isGotoSelector(sel: action)) {
  357. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  358. }
  359. if (KMSystemMenu.isToolSelector(sel: action)) {
  360. return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem)
  361. }
  362. if (KMSystemMenu.isWindowSelector(sel: action)) {
  363. return document.isHome ? document.homeViewController!.validateMenuItem(menuItem) : document.mainViewController!.validateMenuItem(menuItem)
  364. }
  365. return true
  366. }
  367. }
  368. extension KMBrowserWindowController {
  369. @IBAction func menuItemAction_openFile(_ sender: Any) {
  370. super.openDocument(sender)
  371. }
  372. @IBAction func importFromFile(_ sender: Any) {
  373. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  374. document.homeViewController?.importFromFile(sender)
  375. }
  376. @IBAction func openBlankPage(_ sender: Any) {
  377. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  378. document.homeViewController?.openBlankPage(sender)
  379. }
  380. @IBAction func importFromScanner(_ sender: Any) {
  381. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  382. document.homeViewController?.importFromScanner(sender)
  383. }
  384. @IBAction func menuItemAction_newTab(_ sender: Any) {
  385. self.openDocumentWindow()
  386. }
  387. }
  388. // MARK: - KMSystemFileMenuProtocol
  389. extension KMBrowserWindowController: KMSystemFileMenuProtocol {
  390. func menuItemClick_mergePDF(_ sender: Any) {
  391. if (self.canResponseDocumentAction() == false) {
  392. return
  393. }
  394. if let document = self.browser.activeTabContents() as? KMMainDocument {
  395. if (document.isHome) {
  396. document.homeViewController?.menuItemClick_mergePDF(sender)
  397. } else {
  398. document.mainViewController?.menuItemClick_mergePDF(sender)
  399. }
  400. }
  401. }
  402. func menuItemClick_Compress(_ sender: Any) {
  403. if (self.canResponseDocumentAction() == false) {
  404. return
  405. }
  406. if let document = self.browser.activeTabContents() as? KMMainDocument {
  407. if (document.isHome) {
  408. document.homeViewController?.menuItemClick_Compress(sender)
  409. } else {
  410. document.mainViewController?.menuItemClick_Compress(sender)
  411. }
  412. }
  413. }
  414. func menuItemAction_ConvertToWord(_ sender: Any) {
  415. if (self.canResponseDocumentAction() == false) {
  416. return
  417. }
  418. if let document = self.browser.activeTabContents() as? KMMainDocument {
  419. if (document.isHome) {
  420. document.homeViewController?.menuItemAction_ConvertToWord(sender)
  421. } else {
  422. document.mainViewController?.menuItemAction_ConvertToWord(sender)
  423. }
  424. }
  425. }
  426. func menuItemAction_ConvertToExcel(_ sender: Any) {
  427. if (self.canResponseDocumentAction() == false) {
  428. return
  429. }
  430. if let document = self.browser.activeTabContents() as? KMMainDocument {
  431. if (document.isHome) {
  432. document.homeViewController?.menuItemAction_ConvertToExcel(sender)
  433. } else {
  434. document.mainViewController?.menuItemAction_ConvertToExcel(sender)
  435. }
  436. }
  437. }
  438. func menuItemAction_ConvertToPPT(_ sender: Any) {
  439. if (self.canResponseDocumentAction() == false) {
  440. return
  441. }
  442. if let document = self.browser.activeTabContents() as? KMMainDocument {
  443. if (document.isHome) {
  444. document.homeViewController?.menuItemAction_ConvertToPPT(sender)
  445. } else {
  446. document.mainViewController?.menuItemAction_ConvertToPPT(sender)
  447. }
  448. }
  449. }
  450. func menuItemAction_ConvertToRTF(_ sender: Any) {
  451. if (self.canResponseDocumentAction() == false) {
  452. return
  453. }
  454. if let document = self.browser.activeTabContents() as? KMMainDocument {
  455. if (document.isHome) {
  456. document.homeViewController?.menuItemAction_ConvertToRTF(sender)
  457. } else {
  458. document.mainViewController?.menuItemAction_ConvertToRTF(sender)
  459. }
  460. }
  461. }
  462. func menuItemAction_ConvertToHTML(_ sender: Any) {
  463. if (self.canResponseDocumentAction() == false) {
  464. return
  465. }
  466. if let document = self.browser.activeTabContents() as? KMMainDocument {
  467. if (document.isHome) {
  468. document.homeViewController?.menuItemAction_ConvertToHTML(sender)
  469. } else {
  470. document.mainViewController?.menuItemAction_ConvertToHTML(sender)
  471. }
  472. }
  473. }
  474. func menuItemAction_ConvertToText(_ sender: Any) {
  475. if (self.canResponseDocumentAction() == false) {
  476. return
  477. }
  478. if let document = self.browser.activeTabContents() as? KMMainDocument {
  479. if (document.isHome) {
  480. document.homeViewController?.menuItemAction_ConvertToText(sender)
  481. } else {
  482. document.mainViewController?.menuItemAction_ConvertToText(sender)
  483. }
  484. }
  485. }
  486. func menuItemAction_ConvertToCSV(_ sender: Any) {
  487. if (self.canResponseDocumentAction() == false) {
  488. return
  489. }
  490. if let document = self.browser.activeTabContents() as? KMMainDocument {
  491. if (document.isHome) {
  492. document.homeViewController?.menuItemAction_ConvertToCSV(sender)
  493. } else {
  494. document.mainViewController?.menuItemAction_ConvertToCSV(sender)
  495. }
  496. }
  497. }
  498. func menuItemAction_ConvertToImage(_ sender: Any) {
  499. if (self.canResponseDocumentAction() == false) {
  500. return
  501. }
  502. if let document = self.browser.activeTabContents() as? KMMainDocument {
  503. if (document.isHome) {
  504. document.homeViewController?.menuItemAction_ConvertToImage(sender)
  505. } else {
  506. document.mainViewController?.menuItemAction_ConvertToImage(sender)
  507. }
  508. }
  509. }
  510. func menuItemClick_SettingPassword(_ sender: Any) {
  511. if (self.canResponseDocumentAction() == false) {
  512. return
  513. }
  514. if let document = self.browser.activeTabContents() as? KMMainDocument {
  515. if (document.isHome) {
  516. document.homeViewController?.menuItemClick_SettingPassword(sender)
  517. } else {
  518. document.mainViewController?.menuItemClick_SettingPassword(sender)
  519. }
  520. }
  521. }
  522. func menuItemClick_RemovePassword(_ sender: Any) {
  523. if (self.canResponseDocumentAction() == false) {
  524. return
  525. }
  526. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  527. document.mainViewController?.menuItemClick_RemovePassword(sender)
  528. }
  529. }
  530. func menuItemAction_closeWindow(_ sender: Any) {
  531. if (self.canResponseDocumentAction() == false) {
  532. return
  533. }
  534. if let document = self.browser.activeTabContents() as? KMMainDocument {
  535. if (document.isHome) {
  536. document.homeViewController?.menuItemAction_closeWindow(sender)
  537. } else {
  538. document.mainViewController?.menuItemAction_closeWindow(sender)
  539. }
  540. }
  541. }
  542. func menuItemAction_closeAllWindows(_ sender: Any) {
  543. if (self.canResponseDocumentAction() == false) {
  544. return
  545. }
  546. if let document = self.browser.activeTabContents() as? KMMainDocument {
  547. if (document.isHome) {
  548. document.homeViewController?.menuItemAction_closeAllWindows(sender)
  549. } else {
  550. document.mainViewController?.menuItemAction_closeAllWindows(sender)
  551. }
  552. }
  553. }
  554. func menuItemAction_closeTagPage(_ sender: Any) {
  555. if (self.canResponseDocumentAction() == false) {
  556. return
  557. }
  558. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  559. document.mainViewController?.menuItemAction_closeTagPage(sender)
  560. }
  561. }
  562. func menuItemAction_showInFinder(_ sender: Any) {
  563. if (self.canResponseDocumentAction() == false) {
  564. return
  565. }
  566. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  567. document.mainViewController?.menuItemAction_showInFinder(sender)
  568. }
  569. }
  570. func menuItemAction_property(_ sender: Any) {
  571. if (self.canResponseDocumentAction() == false) {
  572. return
  573. }
  574. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  575. document.mainViewController?.menuItemAction_property(sender)
  576. }
  577. }
  578. func menuItemAction_print(_ sender: Any) {
  579. if (self.canResponseDocumentAction() == false) {
  580. return
  581. }
  582. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  583. document.mainViewController?.menuItemAction_print(sender)
  584. }
  585. }
  586. }
  587. // MARK: - KMSystemViewMenuProtocol
  588. extension KMBrowserWindowController: KMSystemViewMenuProtocol {
  589. func menuItemAction_adjustWidth(_ sender: Any) {
  590. if (self.canResponseDocumentAction() == false) {
  591. return
  592. }
  593. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  594. document.mainViewController?.menuItemAction_adjustWidth(sender)
  595. }
  596. }
  597. func menuItemAction_adjustPage(_ sender: Any) {
  598. if (self.canResponseDocumentAction() == false) {
  599. return
  600. }
  601. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  602. document.mainViewController?.menuItemAction_adjustPage(sender)
  603. }
  604. }
  605. func menuItemAction_size(_ sender: Any) {
  606. if (self.canResponseDocumentAction() == false) {
  607. return
  608. }
  609. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  610. document.mainViewController?.menuItemAction_size(sender)
  611. }
  612. }
  613. func menuItemAction_zoomOut(_ sender: Any) {
  614. if (self.canResponseDocumentAction() == false) {
  615. return
  616. }
  617. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  618. document.mainViewController?.menuItemAction_zoomOut(sender)
  619. }
  620. }
  621. func menuItemAction_zoomIn(_ sender: Any) {
  622. if (self.canResponseDocumentAction() == false) {
  623. return
  624. }
  625. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  626. document.mainViewController?.menuItemAction_zoomIn(sender)
  627. }
  628. }
  629. func menuItemAction_singlePage(_ sender: Any) {
  630. if (self.canResponseDocumentAction() == false) {
  631. return
  632. }
  633. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  634. document.mainViewController?.menuItemAction_singlePage(sender)
  635. }
  636. }
  637. func menuItemAction_singlePageContinue(_ sender: Any) {
  638. if (self.canResponseDocumentAction() == false) {
  639. return
  640. }
  641. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  642. document.mainViewController?.menuItemAction_singlePageContinue(sender)
  643. }
  644. }
  645. func menuItemAction_doublePage(_ sender: Any) {
  646. if (self.canResponseDocumentAction() == false) {
  647. return
  648. }
  649. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  650. document.mainViewController?.menuItemAction_doublePage(sender)
  651. }
  652. }
  653. func menuItemAction_doublePageContinue(_ sender: Any) {
  654. if (self.canResponseDocumentAction() == false) {
  655. return
  656. }
  657. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  658. document.mainViewController?.menuItemAction_doublePageContinue(sender)
  659. }
  660. }
  661. func menuItemAction_bookMode(_ sender: Any) {
  662. if (self.canResponseDocumentAction() == false) {
  663. return
  664. }
  665. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  666. document.mainViewController?.menuItemAction_bookMode(sender)
  667. }
  668. }
  669. func menuItemAction_readMode(_ sender: Any) {
  670. if (self.canResponseDocumentAction() == false) {
  671. return
  672. }
  673. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  674. document.mainViewController?.menuItemAction_readMode(sender)
  675. }
  676. }
  677. func menuItemAction_showSplitPage(_ sender: Any) {
  678. if (self.canResponseDocumentAction() == false) {
  679. return
  680. }
  681. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  682. document.mainViewController?.menuItemAction_showSplitPage(sender)
  683. }
  684. }
  685. func menuItemAction_rotateLeft(_ sender: Any) {
  686. if (self.canResponseDocumentAction() == false) {
  687. return
  688. }
  689. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  690. document.mainViewController?.menuItemAction_rotateLeft(sender)
  691. }
  692. }
  693. func menuItemAction_rotateRight(_ sender: Any) {
  694. if (self.canResponseDocumentAction() == false) {
  695. return
  696. }
  697. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  698. document.mainViewController?.menuItemAction_rotateRight(sender)
  699. }
  700. }
  701. func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  702. if (self.canResponseDocumentAction() == false) {
  703. return
  704. }
  705. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  706. document.mainViewController?.menuItemAction_rotateAllPageLeft(sender)
  707. }
  708. }
  709. func menuItemAction_rotateAllPageRight(_ sender: Any) {
  710. if (self.canResponseDocumentAction() == false) {
  711. return
  712. }
  713. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  714. document.mainViewController?.menuItemAction_rotateAllPageRight(sender)
  715. }
  716. }
  717. func menuItemAction_view_readMode(_ sender: Any) {
  718. if (self.canResponseDocumentAction() == false) {
  719. return
  720. }
  721. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  722. document.mainViewController?.menuItemAction_view_readMode(sender)
  723. }
  724. }
  725. func menuItemAction_hiddenLeftSide(_ sender: Any) {
  726. if (self.canResponseDocumentAction() == false) {
  727. return
  728. }
  729. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  730. document.mainViewController?.menuItemAction_hiddenLeftSide(sender)
  731. }
  732. }
  733. func menuItemAction_hiddenRightSide(_ sender: Any) {
  734. if (self.canResponseDocumentAction() == false) {
  735. return
  736. }
  737. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  738. document.mainViewController?.menuItemAction_hiddenRightSide(sender)
  739. }
  740. }
  741. func menuItemAction_thumai(_ sender: Any) {
  742. if (self.canResponseDocumentAction() == false) {
  743. return
  744. }
  745. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  746. document.mainViewController?.menuItemAction_thumai(sender)
  747. }
  748. }
  749. func menuItemAction_outline(_ sender: Any) {
  750. if (self.canResponseDocumentAction() == false) {
  751. return
  752. }
  753. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  754. document.mainViewController?.menuItemAction_outline(sender)
  755. }
  756. }
  757. func menuItemAction_bookmark(_ sender: Any) {
  758. if (self.canResponseDocumentAction() == false) {
  759. return
  760. }
  761. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  762. document.mainViewController?.menuItemAction_bookmark(sender)
  763. }
  764. }
  765. func menuItemAction_annotation(_ sender: Any) {
  766. if (self.canResponseDocumentAction() == false) {
  767. return
  768. }
  769. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  770. document.mainViewController?.menuItemAction_annotation(sender)
  771. }
  772. }
  773. func menuItemAction_search(_ sender: Any) {
  774. if (self.canResponseDocumentAction() == false) {
  775. return
  776. }
  777. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  778. document.mainViewController?.menuItemAction_search(sender)
  779. }
  780. }
  781. func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  782. if (self.canResponseDocumentAction() == false) {
  783. return
  784. }
  785. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  786. document.mainViewController?.menuItemAction_hiddenPageIndicator(sender)
  787. }
  788. }
  789. }
  790. // MARK: - KMSystemEditMenuProtocol
  791. extension KMBrowserWindowController: KMSystemEditMenuProtocol {
  792. func menuItemAction_find(_ sender: Any) {
  793. if (self.canResponseDocumentAction() == false) {
  794. return
  795. }
  796. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  797. document.mainViewController?.menuItemAction_find(sender)
  798. }
  799. }
  800. }
  801. // MARK: - Annotation Menu
  802. extension KMBrowserWindowController {
  803. @IBAction func menuItemAction_highlight(_ sender: Any) {
  804. if (self.canResponseDocumentAction() == false) {
  805. return
  806. }
  807. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  808. document.mainViewController?.menuItemAction_highlight(sender)
  809. }
  810. }
  811. @IBAction func menuItemAction_underline(_ sender: Any) {
  812. if (self.canResponseDocumentAction() == false) {
  813. return
  814. }
  815. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  816. document.mainViewController?.menuItemAction_underline(sender)
  817. }
  818. }
  819. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  820. if (self.canResponseDocumentAction() == false) {
  821. return
  822. }
  823. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  824. document.mainViewController?.menuItemAction_deleteLine(sender)
  825. }
  826. }
  827. @IBAction func menuItemAction_freehand(_ sender: Any) {
  828. if (self.canResponseDocumentAction() == false) {
  829. return
  830. }
  831. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  832. document.mainViewController?.menuItemAction_freehand(sender)
  833. }
  834. }
  835. @IBAction func menuItemAction_text(_ sender: Any) {
  836. if (self.canResponseDocumentAction() == false) {
  837. return
  838. }
  839. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  840. document.mainViewController?.menuItemAction_text(sender)
  841. }
  842. }
  843. @IBAction func menuItemAction_note(_ sender: Any) {
  844. if (self.canResponseDocumentAction() == false) {
  845. return
  846. }
  847. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  848. document.mainViewController?.menuItemAction_note(sender)
  849. }
  850. }
  851. @IBAction func menuItemAction_squre(_ sender: Any) {
  852. if (self.canResponseDocumentAction() == false) {
  853. return
  854. }
  855. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  856. document.mainViewController?.menuItemAction_squre(sender)
  857. }
  858. }
  859. @IBAction func menuItemAction_circle(_ sender: Any) {
  860. if (self.canResponseDocumentAction() == false) {
  861. return
  862. }
  863. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  864. document.mainViewController?.menuItemAction_circle(sender)
  865. }
  866. }
  867. @IBAction func menuItemAction_arrow(_ sender: Any) {
  868. if (self.canResponseDocumentAction() == false) {
  869. return
  870. }
  871. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  872. document.mainViewController?.menuItemAction_arrow(sender)
  873. }
  874. }
  875. @IBAction func menuItemAction_line(_ sender: Any) {
  876. if (self.canResponseDocumentAction() == false) {
  877. return
  878. }
  879. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  880. document.mainViewController?.menuItemAction_line(sender)
  881. }
  882. }
  883. // link
  884. @IBAction func menuItemAction_link(_ sender: Any) {
  885. if (self.canResponseDocumentAction() == false) {
  886. return
  887. }
  888. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  889. document.mainViewController?.menuItemAction_link(sender)
  890. }
  891. }
  892. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  893. if (self.canResponseDocumentAction() == false) {
  894. return
  895. }
  896. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  897. document.mainViewController?.menuItemAction_linkPage(sender)
  898. }
  899. }
  900. // stamp
  901. @IBAction func menuItemAction_stamp(_ sender: Any) {
  902. if (self.canResponseDocumentAction() == false) {
  903. return
  904. }
  905. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  906. document.mainViewController?.menuItemAction_stamp(sender)
  907. }
  908. }
  909. @IBAction func menuItemAction_signure(_ sender: Any) {
  910. if (self.canResponseDocumentAction() == false) {
  911. return
  912. }
  913. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  914. document.mainViewController?.menuItemAction_signure(sender)
  915. }
  916. }
  917. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  918. if (self.canResponseDocumentAction() == false) {
  919. return
  920. }
  921. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  922. document.mainViewController?.menuItemAction_hiddenAllAnnotation(sender)
  923. }
  924. }
  925. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  926. if (self.canResponseDocumentAction() == false) {
  927. return
  928. }
  929. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  930. document.mainViewController?.menuItemAction_clearAllAnnotation(sender)
  931. }
  932. }
  933. }
  934. // MARK: - KMSystemGotoMenuProtocol
  935. extension KMBrowserWindowController: KMSystemGotoMenuProtocol {
  936. func menuItemAction_nextPage(_ sender: Any) {
  937. if (self.canResponseDocumentAction() == false) {
  938. return
  939. }
  940. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  941. document.mainViewController?.menuItemAction_nextPage(sender)
  942. }
  943. }
  944. func menuItemAction_forwardPage(_ sender: Any) {
  945. if (self.canResponseDocumentAction() == false) {
  946. return
  947. }
  948. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  949. document.mainViewController?.menuItemAction_forwardPage(sender)
  950. }
  951. }
  952. func menuItemAction_firstPage(_ sender: Any) {
  953. if (self.canResponseDocumentAction() == false) {
  954. return
  955. }
  956. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  957. document.mainViewController?.menuItemAction_firstPage(sender)
  958. }
  959. }
  960. func menuItemAction_lastPage(_ sender: Any) {
  961. if (self.canResponseDocumentAction() == false) {
  962. return
  963. }
  964. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  965. document.mainViewController?.menuItemAction_lastPage(sender)
  966. }
  967. }
  968. func menuItemAction_forward(_ sender: Any) {
  969. if (self.canResponseDocumentAction() == false) {
  970. return
  971. }
  972. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  973. document.mainViewController?.menuItemAction_forward(sender)
  974. }
  975. }
  976. func menuItemAction_goback(_ sender: Any) {
  977. if (self.canResponseDocumentAction() == false) {
  978. return
  979. }
  980. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  981. document.mainViewController?.menuItemAction_goback(sender)
  982. }
  983. }
  984. func menuItemAction_gotoPage(_ sender: Any) {
  985. if (self.canResponseDocumentAction() == false) {
  986. return
  987. }
  988. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  989. document.mainViewController?.menuItemAction_gotoPage(sender)
  990. }
  991. }
  992. }
  993. // MARK: - KMSystemToolMenuProtocol
  994. extension KMBrowserWindowController: KMSystemToolMenuProtocol {
  995. func menuItemAction_scrolTool(_ sender: Any) {
  996. if (self.canResponseDocumentAction() == false) {
  997. return
  998. }
  999. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1000. document.mainViewController?.menuItemAction_scrolTool(sender)
  1001. }
  1002. }
  1003. func menuItemAction_zoomOutTool(_ sender: Any) {
  1004. if (self.canResponseDocumentAction() == false) {
  1005. return
  1006. }
  1007. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1008. document.mainViewController?.menuItemAction_zoomOutTool(sender)
  1009. }
  1010. }
  1011. func menuItemAction_selectTool(_ sender: Any) {
  1012. if (self.canResponseDocumentAction() == false) {
  1013. return
  1014. }
  1015. if let document = self.browser.activeTabContents() as? KMMainDocument, document.isHome == false {
  1016. document.mainViewController?.menuItemAction_selectTool(sender)
  1017. }
  1018. }
  1019. }
  1020. // MARK: - KMSystemWindowMenuRrotocol
  1021. extension KMBrowserWindowController: KMSystemWindowMenuRrotocol {
  1022. func menuItemAction_showForwardTagPage(_ sender: Any) {
  1023. if (self.canResponseDocumentAction() == false) {
  1024. return
  1025. }
  1026. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1027. if (document.isHome) {
  1028. document.homeViewController?.menuItemAction_showForwardTagPage(sender)
  1029. } else {
  1030. document.mainViewController?.menuItemAction_showForwardTagPage(sender)
  1031. }
  1032. }
  1033. }
  1034. func menuItemAction_showNextTagPage(_ sender: Any) {
  1035. if (self.canResponseDocumentAction() == false) {
  1036. return
  1037. }
  1038. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1039. if (document.isHome) {
  1040. document.homeViewController?.menuItemAction_showNextTagPage(sender)
  1041. } else {
  1042. document.mainViewController?.menuItemAction_showNextTagPage(sender)
  1043. }
  1044. }
  1045. }
  1046. func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  1047. if (self.canResponseDocumentAction() == false) {
  1048. return
  1049. }
  1050. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1051. if (document.isHome) {
  1052. document.homeViewController?.menuItemAction_newTagPageToNewWindow(sender)
  1053. } else {
  1054. document.mainViewController?.menuItemAction_newTagPageToNewWindow(sender)
  1055. }
  1056. }
  1057. }
  1058. func menuItemAction_mergeAllWindow(_ sender: Any) {
  1059. if (self.canResponseDocumentAction() == false) {
  1060. return
  1061. }
  1062. if let document = self.browser.activeTabContents() as? KMMainDocument {
  1063. if (document.isHome) {
  1064. document.homeViewController?.menuItemAction_mergeAllWindow(sender)
  1065. } else {
  1066. document.mainViewController?.menuItemAction_mergeAllWindow(sender)
  1067. }
  1068. }
  1069. }
  1070. }