KMBrowserWindowController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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?.setFrameAutosaveName("")
  19. rightTabStripView_.delete = self
  20. homeRightTabStripView_.delete = self
  21. rightTabStripView_.updateView()
  22. fileUploadPanel.delete = self
  23. homeRightTabStripView_.homeRightSearchField.delegate = self
  24. isMultiTabMode = true
  25. addObserverForAppearanceChange()
  26. refreshToolBar(homeToolState: .Home)
  27. NotificationCenter.default.addObserver(self, selector: #selector(closeAllTabs(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseAllTabs"), object: nil)
  28. NotificationCenter.default.addObserver(self, selector: #selector(openNewWindow(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerOpenNewWindow"), object: nil)
  29. if (WelcomeWindowController.welcomeHasShow() == false) {
  30. DispatchQueue.main.async {
  31. let welcome = WelcomeWindowController()
  32. self.window?.beginSheet(welcome.window!)
  33. welcome.itemClick = { [weak self] idx, param in
  34. if (idx == 1) { // 关闭
  35. self?.window?.endSheet((param as! NSWindowController).window!)
  36. } else if (idx == 2) { // 以后提醒
  37. self?.window?.endSheet((param as! NSWindowController).window!)
  38. } else if (idx == 3) { // 注册
  39. self?.window?.endSheet((param as! NSWindowController).window!)
  40. let _ = KMLoginWindowController.show(window: (self?.window)!, .Batch, .register)
  41. }
  42. }
  43. }
  44. }
  45. }
  46. override func windowShouldClose(_ sender: NSWindow) -> Bool {
  47. if self.browser.tabStripModel.count() > 1 {
  48. self.browser.windowDidBeginToClose()
  49. return false
  50. }
  51. return true
  52. }
  53. // MARK: Dark&Light
  54. func addObserverForAppearanceChange() -> Void {
  55. window?.contentView!.addObserver(self, forKeyPath: "effectiveAppearance", options: .new, context: nil)
  56. }
  57. func removeObserverForAppearanceChange() -> Void {
  58. window?.contentView!.removeObserver(self, forKeyPath: "effectiveAppearance")
  59. }
  60. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  61. if keyPath == "effectiveAppearance" {
  62. updateViewColor()
  63. }
  64. }
  65. override func updateViewColor() {
  66. super.updateViewColor()
  67. window?.backgroundColor = KMTabAppearance.tabsViewBackgroundColor()
  68. }
  69. // MARK: Public Methods
  70. func refreshToolBar(homeToolState toolState: KMHomeToolState) -> Void {
  71. modeType = toolState
  72. let document = browser.activeTabContents()
  73. if document != nil {
  74. if document!.isHome {
  75. homeRightTabStripView_.isHidden = modeType == .Home
  76. } else {
  77. self.homeRightStripView.isHidden = true
  78. }
  79. } else {
  80. self.homeRightStripView.isHidden = true
  81. }
  82. homeRightTabStripView_.homeRefreshButton.isEnabled = true
  83. homeRightTabStripView_.homeUploadButton.isEnabled = true
  84. filterType = 0
  85. rightTabStripView_.sortPopUpButton.removeAllItems()
  86. rightTabStripView_.filterPopUpButton.removeAllItems()
  87. rightTabStripView_.updateView()
  88. }
  89. override var hasToolbar: Bool {
  90. get {
  91. return false
  92. }
  93. }
  94. // MARK: Private Methods
  95. func openDocumentWindow() -> Void {
  96. // let panel = NSOpenPanel()
  97. // panel.allowsMultipleSelection = false
  98. // panel.allowedFileTypes = ["pdf"]
  99. // panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  100. // if response == .OK {
  101. // let openPath = panel.url?.path
  102. // let selectDocument = self.selectTabContents(path: openPath!)
  103. // if selectDocument != nil {
  104. // let currentIndex = selectDocument!.browser.tabStripModel.index(of: selectDocument)
  105. // self.browser.tabStripModel.selectTabContents(at: Int32(currentIndex), userGesture: true)
  106. // return
  107. // }
  108. // if !panel.url!.path.isPDFValid() {
  109. // let alert = NSAlert()
  110. // alert.alertStyle = .critical
  111. // alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  112. // alert.runModal()
  113. // return
  114. // }
  115. // NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { pdfDocument, documentWasAlreadyOpen, error in
  116. // print("openDocumentWindow")
  117. // }
  118. // }
  119. // }
  120. // MARK TODO: + 号开启
  121. self.browser.addHomeTabContents()
  122. }
  123. override func openDocument(_ sender: Any!) {
  124. self.openDocumentWindow()
  125. }
  126. func selectTabContents(path filePath: String) -> KMMainDocument? {
  127. let document = NSDocumentController.shared.document(for: URL(fileURLWithPath: filePath))
  128. var selectDocument: KMMainDocument?
  129. if document != nil {
  130. if document!.isKind(of: KMMainDocument.self) {
  131. selectDocument = document as! KMMainDocument
  132. }
  133. }
  134. return selectDocument
  135. }
  136. // MARK: Getter
  137. override var rightStripView: NSView! {
  138. get {
  139. return rightTabStripView_
  140. }
  141. }
  142. override var homeRightStripView: NSView! {
  143. get {
  144. return homeRightTabStripView_
  145. }
  146. }
  147. // MARK: Button Action
  148. func commandDispatch(_ sender: Any) -> Void {
  149. openDocumentWindow()
  150. }
  151. func closeAllTabs(_ sender: Any) -> Void {
  152. if self.browser != nil {
  153. self.browser.closeAllTabs()
  154. }
  155. }
  156. func openNewWindow(_ sender: Any) -> Void {
  157. if self.browser != nil {
  158. let activeInt = self.browser.activeTabIndex()
  159. let activeTab = self.browser.activeTabContents()
  160. self.browser.closeTab(at: activeInt, makeHistory: false)
  161. let browser: KMBrowser = KMBrowser.init()
  162. browser.windowController = KMBrowserWindowController.init(browser: browser)
  163. browser.addHomeTabContents()
  164. browser.windowController.showWindow(self)
  165. // browser.add(activeTab, inForeground: false)
  166. // browser.selectTab(at: 1)
  167. let pdfDoc = CPDFDocument.init(url: (activeTab?.fileURL)!)
  168. let document = NSDocumentController.shared.document(for: (activeTab?.fileURL)!)
  169. KMMainDocument().tryToUnlockDocument(pdfDoc!)
  170. if ((pdfDoc?.isLocked)! == true) {
  171. KMPasswordInputWindow.openWindow(window: self.window!, url: (activeTab?.fileURL)!) { result, password in
  172. if result == .cancel { /// 关闭
  173. return
  174. }
  175. /// 解密成功
  176. var selectDocument: KMMainDocument? = nil
  177. if ((document?.isKind(of: KMMainDocument.self)) != nil) {
  178. selectDocument = (document as! KMMainDocument)
  179. }
  180. if selectDocument != nil {
  181. let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument)
  182. selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true)
  183. if (selectDocument?.browser.window.isVisible)! as Bool {
  184. selectDocument?.browser.window.orderFront(nil)
  185. } else if (selectDocument?.browser.window.isMiniaturized)! as Bool {
  186. selectDocument?.browser.window.orderFront(nil)
  187. }
  188. } else {
  189. if !(activeTab?.fileURL)!.path.isPDFValid() {
  190. let alert = NSAlert()
  191. alert.alertStyle = .critical
  192. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  193. alert.runModal()
  194. return
  195. }
  196. NSDocumentController.shared.openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { document, documentWasAlreadyOpen, error in
  197. if error != nil {
  198. NSApp.presentError(error!)
  199. return
  200. }
  201. (document as! KMMainDocument).mainViewController.password = password
  202. }
  203. }
  204. }
  205. } else {
  206. var selectDocument: KMMainDocument? = nil
  207. if ((document?.isKind(of: KMMainDocument.self)) != nil) {
  208. selectDocument = (document as! KMMainDocument)
  209. }
  210. if selectDocument != nil {
  211. let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument)
  212. selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true)
  213. if (selectDocument?.browser.window.isVisible)! as Bool {
  214. selectDocument?.browser.window.orderFront(nil)
  215. } else if (selectDocument?.browser.window.isMiniaturized)! as Bool {
  216. selectDocument?.browser.window.orderFront(nil)
  217. }
  218. } else {
  219. if !(activeTab?.fileURL)!.path.isPDFValid() {
  220. let alert = NSAlert()
  221. alert.alertStyle = .critical
  222. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  223. alert.runModal()
  224. return
  225. }
  226. NSDocumentController.shared.openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { document, documentWasAlreadyOpen, error in
  227. if error != nil {
  228. NSApp.presentError(error!)
  229. return
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. // MARK: 待补充[万军]
  237. func mergeAllWindow(_ sender: Any) {
  238. }
  239. override func layoutTabContentArea(_ frame: NSRect) {
  240. super.layoutTabContentArea(frame)
  241. self.rightStripView.autoresizingMask = [.minXMargin, .minYMargin]
  242. }
  243. }
  244. // MARK: - KMToolbarRightViewDelegate
  245. extension KMBrowserWindowController: KMToolbarRightViewDelegate {
  246. func pdfRightSegmentedControlAction(_ sender: KMSegmentedBox?) {
  247. KMComparativeTableViewController.show(window: self.window!)
  248. }
  249. func userInfoButtonAction(_ sender: NSButton) {
  250. if KMLightMemberManager.manager.isLogin() {
  251. Task { @MainActor in
  252. if await KMLightMemberManager.manager.canUseAdvanced() {
  253. KMAccountInfoWindowController.show(window: self.window!)
  254. } else {
  255. KMLoginWindowController.show(window: self.window!)
  256. }
  257. }
  258. } else {
  259. KMLoginWindowController.show(window: self.window!)
  260. }
  261. }
  262. func homeRefreshButtonAction(_ sender: NSButton) {
  263. }
  264. func homeUploadButtonAction(_ sender: NSButton) {
  265. }
  266. func homeMenuSortAction(_ sender: NSPopUpButton) {
  267. }
  268. func homeMenuFilterAction(_ sender: NSPopUpButton) {
  269. }
  270. }
  271. // MARK: - KMUploadFileDelegate
  272. extension KMBrowserWindowController: KMUploadFileDelegate {
  273. // override func cancelOperation(_ sender: Any?) {
  274. //
  275. // }
  276. }
  277. // MARK: - NSSearchFieldDelegate
  278. extension KMBrowserWindowController: NSSearchFieldDelegate {
  279. func controlTextDidChange(_ obj: Notification) {
  280. }
  281. func controlTextDidEndEditing(_ obj: Notification) {
  282. }
  283. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  284. var result = false
  285. return result
  286. }
  287. }
  288. // MARK: -
  289. // MARK: Menu Actions
  290. // MARK: -
  291. // MARK: File Menu
  292. extension KMBrowserWindowController {
  293. @IBAction func menuItemAction_openFile(_ sender: Any) {
  294. super.openDocument(sender)
  295. }
  296. @IBAction func importFromFile(_ sender: Any) {
  297. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  298. document.homeViewController.importFromFile(sender)
  299. }
  300. @IBAction func openBlankPage(_ sender: Any) {
  301. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  302. document.homeViewController.openBlankPage(sender)
  303. }
  304. @IBAction func importFromScanner(_ sender: Any) {
  305. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  306. document.homeViewController.importFromScanner(sender)
  307. }
  308. @IBAction func menuItemAction_newTab(_ sender: Any) {
  309. self.openDocumentWindow()
  310. }
  311. }