Просмотр исходного кода

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 месяц назад
Родитель
Сommit
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
         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() {
     func configureMainWindows() {
         for window in NSApp.windows where isMainAppWindow(window) {
         for window in NSApp.windows where isMainAppWindow(window) {
             window.isReleasedWhenClosed = false
             window.isReleasedWhenClosed = false

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

@@ -6,6 +6,7 @@ final class RedditWebViewStore {
     static let shared = RedditWebViewStore()
     static let shared = RedditWebViewStore()
 
 
     private(set) var container: RedditWebViewContainer?
     private(set) var container: RedditWebViewContainer?
+    private var isMediaPlaybackSuspended = false
 
 
     private init() {}
     private init() {}
 
 
@@ -19,4 +20,16 @@ final class RedditWebViewStore {
         container = newContainer
         container = newContainer
         return 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)
+    }
 }
 }