|
@@ -3,6 +3,8 @@ import AppKit
|
|
|
@MainActor
|
|
@MainActor
|
|
|
final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
|
final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
|
|
private var windowObservers: [NSObjectProtocol] = []
|
|
private var windowObservers: [NSObjectProtocol] = []
|
|
|
|
|
+ private var shouldCenterMainWindowOnShow = true
|
|
|
|
|
+ private var centeringDeadline: Date?
|
|
|
|
|
|
|
|
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
|
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
|
|
false
|
|
false
|
|
@@ -18,6 +20,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
|
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
|
|
installWindowObservers()
|
|
installWindowObservers()
|
|
|
configureMainWindows()
|
|
configureMainWindows()
|
|
|
|
|
+ beginCenteringMainWindow()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func applicationDidBecomeActive(_ notification: Notification) {
|
|
func applicationDidBecomeActive(_ notification: Notification) {
|
|
@@ -26,6 +29,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func windowShouldClose(_ sender: NSWindow) -> Bool {
|
|
func windowShouldClose(_ sender: NSWindow) -> Bool {
|
|
|
|
|
+ if isMainAppWindow(sender) {
|
|
|
|
|
+ shouldCenterMainWindowOnShow = true
|
|
|
|
|
+ centeringDeadline = nil
|
|
|
|
|
+ }
|
|
|
sender.orderOut(nil)
|
|
sender.orderOut(nil)
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
@@ -40,15 +47,63 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
|
|
RedditWebViewStore.shared.resumeMediaPlaybackAfterWindowDeminiaturize()
|
|
RedditWebViewStore.shared.resumeMediaPlaybackAfterWindowDeminiaturize()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ func windowDidMove(_ sender: NSWindow) {
|
|
|
|
|
+ guard isMainAppWindow(sender), isCenteringMainWindow else { return }
|
|
|
|
|
+ AppWindow.centerOnScreen(sender)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
func configureMainWindows() {
|
|
func configureMainWindows() {
|
|
|
for window in NSApp.windows where isMainAppWindow(window) {
|
|
for window in NSApp.windows where isMainAppWindow(window) {
|
|
|
window.isReleasedWhenClosed = false
|
|
window.isReleasedWhenClosed = false
|
|
|
|
|
+ window.isRestorable = false
|
|
|
|
|
+ window.setFrameAutosaveName("")
|
|
|
if window.delegate !== self {
|
|
if window.delegate !== self {
|
|
|
window.delegate = self
|
|
window.delegate = self
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ func centerMainWindow() {
|
|
|
|
|
+ beginCenteringMainWindow()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private var isCenteringMainWindow: Bool {
|
|
|
|
|
+ guard shouldCenterMainWindowOnShow else { return false }
|
|
|
|
|
+ if let centeringDeadline {
|
|
|
|
|
+ return Date() < centeringDeadline
|
|
|
|
|
+ }
|
|
|
|
|
+ return true
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private func beginCenteringMainWindow() {
|
|
|
|
|
+ shouldCenterMainWindowOnShow = true
|
|
|
|
|
+ centeringDeadline = Date().addingTimeInterval(0.6)
|
|
|
|
|
+ centerMainWindowIfNeeded()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private func finishCenteringMainWindow() {
|
|
|
|
|
+ shouldCenterMainWindowOnShow = false
|
|
|
|
|
+ centeringDeadline = nil
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private func centerMainWindowIfNeeded() {
|
|
|
|
|
+ guard isCenteringMainWindow else { return }
|
|
|
|
|
+ guard let window = NSApp.windows.first(where: isMainAppWindow) else { return }
|
|
|
|
|
+
|
|
|
|
|
+ let centerAction = { [weak self] in
|
|
|
|
|
+ guard let self, self.isCenteringMainWindow else { return }
|
|
|
|
|
+ AppWindow.centerOnScreen(window)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ centerAction()
|
|
|
|
|
+ DispatchQueue.main.async(execute: centerAction)
|
|
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.15, execute: centerAction)
|
|
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) { [weak self] in
|
|
|
|
|
+ centerAction()
|
|
|
|
|
+ self?.finishCenteringMainWindow()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private func installWindowObservers() {
|
|
private func installWindowObservers() {
|
|
|
let center = NotificationCenter.default
|
|
let center = NotificationCenter.default
|
|
|
|
|
|
|
@@ -81,6 +136,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
|
|
|
|
|
|
|
|
private func showMainWindow() {
|
|
private func showMainWindow() {
|
|
|
guard let window = NSApp.windows.first(where: isMainAppWindow) else { return }
|
|
guard let window = NSApp.windows.first(where: isMainAppWindow) else { return }
|
|
|
|
|
+ beginCenteringMainWindow()
|
|
|
window.makeKeyAndOrderFront(nil)
|
|
window.makeKeyAndOrderFront(nil)
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
}
|
|
}
|