AppDelegate.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import AppKit
  2. @MainActor
  3. final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
  4. private var windowObservers: [NSObjectProtocol] = []
  5. private var shouldCenterMainWindowOnShow = true
  6. private var centeringDeadline: Date?
  7. func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
  8. false
  9. }
  10. func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
  11. if !flag {
  12. showMainWindow()
  13. }
  14. return true
  15. }
  16. func applicationDidFinishLaunching(_ notification: Notification) {
  17. installWindowObservers()
  18. configureMainWindows()
  19. beginCenteringMainWindow()
  20. }
  21. func applicationDidBecomeActive(_ notification: Notification) {
  22. configureMainWindows()
  23. showMainWindowIfNeeded()
  24. RedditWebViewStore.shared.handleApplicationDidBecomeActive()
  25. }
  26. func windowShouldClose(_ sender: NSWindow) -> Bool {
  27. if isMainAppWindow(sender) {
  28. shouldCenterMainWindowOnShow = true
  29. centeringDeadline = nil
  30. }
  31. sender.orderOut(nil)
  32. return false
  33. }
  34. func windowWillMiniaturize(_ sender: NSWindow) {
  35. guard isMainAppWindow(sender) else { return }
  36. RedditWebViewStore.shared.suspendMediaPlaybackForWindowMinimize()
  37. }
  38. func windowDidDeminiaturize(_ sender: NSWindow) {
  39. guard isMainAppWindow(sender) else { return }
  40. RedditWebViewStore.shared.resumeMediaPlaybackAfterWindowDeminiaturize()
  41. }
  42. func windowDidMove(_ sender: NSWindow) {
  43. guard isMainAppWindow(sender), isCenteringMainWindow else { return }
  44. AppWindow.centerOnScreen(sender)
  45. }
  46. func configureMainWindows() {
  47. for window in NSApp.windows where isMainAppWindow(window) {
  48. window.isReleasedWhenClosed = false
  49. window.isRestorable = false
  50. window.setFrameAutosaveName("")
  51. if window.delegate !== self {
  52. window.delegate = self
  53. }
  54. }
  55. }
  56. func centerMainWindow() {
  57. beginCenteringMainWindow()
  58. }
  59. private var isCenteringMainWindow: Bool {
  60. guard shouldCenterMainWindowOnShow else { return false }
  61. if let centeringDeadline {
  62. return Date() < centeringDeadline
  63. }
  64. return true
  65. }
  66. private func beginCenteringMainWindow() {
  67. shouldCenterMainWindowOnShow = true
  68. centeringDeadline = Date().addingTimeInterval(0.6)
  69. centerMainWindowIfNeeded()
  70. }
  71. private func finishCenteringMainWindow() {
  72. shouldCenterMainWindowOnShow = false
  73. centeringDeadline = nil
  74. }
  75. private func centerMainWindowIfNeeded() {
  76. guard isCenteringMainWindow else { return }
  77. guard let window = NSApp.windows.first(where: isMainAppWindow) else { return }
  78. let centerAction = { [weak self] in
  79. guard let self, self.isCenteringMainWindow else { return }
  80. AppWindow.centerOnScreen(window)
  81. }
  82. centerAction()
  83. DispatchQueue.main.async(execute: centerAction)
  84. DispatchQueue.main.asyncAfter(deadline: .now() + 0.15, execute: centerAction)
  85. DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) { [weak self] in
  86. centerAction()
  87. self?.finishCenteringMainWindow()
  88. }
  89. }
  90. private func installWindowObservers() {
  91. let center = NotificationCenter.default
  92. windowObservers = [
  93. center.addObserver(
  94. forName: NSWindow.didBecomeKeyNotification,
  95. object: nil,
  96. queue: .main
  97. ) { [weak self] _ in
  98. Task { @MainActor in
  99. self?.configureMainWindows()
  100. }
  101. },
  102. center.addObserver(
  103. forName: NSApplication.didUpdateNotification,
  104. object: nil,
  105. queue: .main
  106. ) { [weak self] _ in
  107. Task { @MainActor in
  108. self?.configureMainWindows()
  109. }
  110. },
  111. ]
  112. }
  113. private func showMainWindowIfNeeded() {
  114. guard !NSApp.windows.contains(where: { isMainAppWindow($0) && $0.isVisible }) else { return }
  115. showMainWindow()
  116. }
  117. private func showMainWindow() {
  118. guard let window = NSApp.windows.first(where: isMainAppWindow) else { return }
  119. beginCenteringMainWindow()
  120. window.makeKeyAndOrderFront(nil)
  121. NSApp.activate(ignoringOtherApps: true)
  122. }
  123. private func isMainAppWindow(_ window: NSWindow) -> Bool {
  124. guard window.canBecomeMain, !(window is NSPanel) else { return false }
  125. if window.title == "Sign in to Reddit" { return false }
  126. return window.level == .normal
  127. }
  128. }