KMBrowserWindowController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. }
  265. func homeUploadButtonAction(_ sender: NSButton) {
  266. }
  267. func homeMenuSortAction(_ sender: NSPopUpButton) {
  268. }
  269. func homeMenuFilterAction(_ sender: NSPopUpButton) {
  270. }
  271. }
  272. // MARK: - KMUploadFileDelegate
  273. extension KMBrowserWindowController: KMUploadFileDelegate {
  274. // override func cancelOperation(_ sender: Any?) {
  275. //
  276. // }
  277. }
  278. // MARK: - NSSearchFieldDelegate
  279. extension KMBrowserWindowController: NSSearchFieldDelegate {
  280. func controlTextDidChange(_ obj: Notification) {
  281. }
  282. func controlTextDidEndEditing(_ obj: Notification) {
  283. }
  284. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  285. var result = false
  286. return result
  287. }
  288. }
  289. // MARK: -
  290. // MARK: Menu Actions
  291. // MARK: -
  292. // MARK: File Menu
  293. extension KMBrowserWindowController {
  294. @IBAction func menuItemAction_openFile(_ sender: Any) {
  295. super.openDocument(sender)
  296. }
  297. @IBAction func importFromFile(_ sender: Any) {
  298. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  299. document.homeViewController.importFromFile(sender)
  300. }
  301. @IBAction func openBlankPage(_ sender: Any) {
  302. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  303. document.homeViewController.openBlankPage(sender)
  304. }
  305. @IBAction func importFromScanner(_ sender: Any) {
  306. let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
  307. document.homeViewController.importFromScanner(sender)
  308. }
  309. @IBAction func menuItemAction_newTab(_ sender: Any) {
  310. self.openDocumentWindow()
  311. }
  312. }