Преглед на файлове

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)
+    }
 }