Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions previewer/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class PreviewViewController: NSViewController, QLPreviewingController, NSCollect
var hasCoverArt: Bool = false
var snapshotSize: CGSize = .zero
var images: [NSImage?] = []
var timer: Timer? = nil
var webViewVideoIsStopped: Bool = false

@IBOutlet weak var sidebar: NSScrollView!
@IBOutlet weak var sidebarCollection: NSCollectionView!
Expand Down Expand Up @@ -355,4 +357,47 @@ class PreviewViewController: NSViewController, QLPreviewingController, NSCollect
sidebarCollection.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [weak self] _ in
self?.checkVisibility()
}
}

func checkVisibility() {
guard let window = view.window else {
pauseVideo()
return
}

if window.level.rawValue > NSWindow.Level.normal.rawValue {
if webViewVideoIsStopped {
resumeVideoIfNeeded()
}
} else {
pauseVideo()
}
}

func pauseVideo() {
webViewVideoIsStopped = true
webView.evaluateJavaScript("document.querySelectorAll('video').forEach(v => v.pause());")
}

func resumeVideoIfNeeded() {
webViewVideoIsStopped = false
let js = """
document.querySelectorAll('video').forEach(video => {
video.currentTime = 0;
video.play().catch(e => {
console.log('Play interrupted:', e);
});
});
"""
webView.evaluateJavaScript(js)
}

deinit {
timer?.invalidate()
}
}