AppDelegate.swift 4.6 KB

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