瀏覽代碼

Pause Reddit media when the main window is minimized and resume on restore.

Prevents videos from playing in the background while the window is minimized, using WKWebView's native media suspension so playback continues from the same position when reopened.

Co-authored-by: Cursor <cursoragent@cursor.com>
AhtashamShahzad1 1 月之前
父節點
當前提交
89d9bb6228
共有 2 個文件被更改,包括 23 次插入0 次删除
  1. 10 0
      Reddit App/App/AppDelegate.swift
  2. 13 0
      Reddit App/Managers/RedditWebViewStore.swift

+ 10 - 0
Reddit App/App/AppDelegate.swift

@@ -30,6 +30,16 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
         return false
     }
 
+    func windowWillMiniaturize(_ sender: NSWindow) {
+        guard isMainAppWindow(sender) else { return }
+        RedditWebViewStore.shared.suspendMediaPlaybackForWindowMinimize()
+    }
+
+    func windowDidDeminiaturize(_ sender: NSWindow) {
+        guard isMainAppWindow(sender) else { return }
+        RedditWebViewStore.shared.resumeMediaPlaybackAfterWindowDeminiaturize()
+    }
+
     func configureMainWindows() {
         for window in NSApp.windows where isMainAppWindow(window) {
             window.isReleasedWhenClosed = false

+ 13 - 0
Reddit App/Managers/RedditWebViewStore.swift

@@ -6,6 +6,7 @@ final class RedditWebViewStore {
     static let shared = RedditWebViewStore()
 
     private(set) var container: RedditWebViewContainer?
+    private var isMediaPlaybackSuspended = false
 
     private init() {}
 
@@ -19,4 +20,16 @@ final class RedditWebViewStore {
         container = newContainer
         return newContainer
     }
+
+    func suspendMediaPlaybackForWindowMinimize() {
+        guard let webView = container?.webView, !isMediaPlaybackSuspended else { return }
+        isMediaPlaybackSuspended = true
+        webView.setAllMediaPlaybackSuspended(true, completionHandler: nil)
+    }
+
+    func resumeMediaPlaybackAfterWindowDeminiaturize() {
+        guard let webView = container?.webView, isMediaPlaybackSuspended else { return }
+        isMediaPlaybackSuspended = false
+        webView.setAllMediaPlaybackSuspended(false, completionHandler: nil)
+    }
 }