KMBrowserWindowController.swift 45 KB

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