KMBrowserWindowController.swift 44 KB

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