The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.
Picture in Picture opens every time you leave a video tab, without Chrome's controls (#103)
Victor Kildahla5d3e77f057a
diff --git a/Sources/Cra/Services/BrowserManager.swift b/Sources/Cra/Services/BrowserManager.swiftindex 9681bb8..393e2fc 100644--- a/Sources/Cra/Services/BrowserManager.swift+++ b/Sources/Cra/Services/BrowserManager.swift@@ -468,12 +468,15 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {// MARK: Picture in Picture/// Arc: "Enable Picture in Picture when you leave a video tab". The request- /// goes out before the page is hidden, so the video keeps playing in the+ /// goes out as the page is hidden, so the video keeps playing in the/// floating window; the page answers through didChangePictureInPicture.+ /// It goes through the protocol's user gesture like the player's button:+ /// the key press that lends the page activation raced the page going+ /// hidden, and PiP sometimes never opened.private func leaveTab(_ tabID: UUID) {guard store.settings.autoPictureInPicture, let rt = runtimes[tabID], !rt.browser.isInPictureInPicture else { return }rt.pendingAutoPictureInPicture = true- rt.browser.requestPictureInPicture(automatic: true)+ requestPictureInPicture(tabID, automatic: true)}func browser(_ browser: CraBrowser, didChangePictureInPicturePlayback playing: Bool) {@@ -565,7 +568,7 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {case .next: op = "nexttrack"case .dismiss: op = "pause"; nowPlaying?.remove(id)case .pictureInPicture:- requestPictureInPicture(id)+ requestPictureInPicture(id, automatic: false)returncase .setVolume(let v):nowPlaying?.update(id) { $0.volume = v }@@ -597,25 +600,30 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {}}- /// The player's Picture in Picture button. The tab is off screen, so the key press that lends a- /// page user activation for the context menu's Picture in Picture never reaches it; the- /// protocol's user gesture does. Only a player in an iframe falls back to the page path.- private func requestPictureInPicture(_ id: UUID) {+ /// The player's Picture in Picture button, and leaving a video tab. The tab is (about to be) off+ /// screen, so the key press that lends a page user activation for the context menu's Picture in+ /// Picture doesn't reliably reach it; the protocol's user gesture does. Only a player in an+ /// iframe falls back to the page path. Leaving a tab takes only an audible, playing video of+ /// some size, as the page path does (muted ones are hover previews or GIF-like loops).+ private func requestPictureInPicture(_ id: UUID, automatic: Bool) {guard let browser = runtimes[id]?.browser else { return }let js = """- (() => {+ ((auto) => {+ if (document.pictureInPictureElement) return 'ok';+ const area = v => { const r = v.getBoundingClientRect(); return r.width * r.height; };const v = [...document.querySelectorAll('video')].filter(v => !v.disablePictureInPicture && v.readyState >= 1)- .sort((a, b) => (b.paused ? 0 : 1) - (a.paused ? 0 : 1))[0];+ .filter(v => !auto || (!v.paused && !v.ended && !v.muted && v.volume > 0 && v.videoWidth > 0 && area(v) >= 20000))+ .sort((a, b) => ((b.paused ? 0 : 1) - (a.paused ? 0 : 1)) || (area(b) - area(a)))[0];return v ? v.requestPictureInPicture().then(() => 'ok', e => e.name + ': ' + e.message) : 'none';- })()+ })(\(automatic))"""let params: [String: Any] = ["expression": js, "returnByValue": true, "awaitPromise": true, "userGesture": true]let json = String(data: try! JSONSerialization.data(withJSONObject: params), encoding: .utf8)!browser.sendDevToolsMethod("Runtime.evaluate", params: json) { [weak browser] result, _ inDispatchQueue.main.async {let answer = Self.stringResult(result)- if answer == "none" { browser?.requestPictureInPicture(automatic: false) }- else if let answer, answer != "ok" { NSLog("Cra: Picture in Picture from the player failed: %@", answer) }+ if answer == nil || answer == "none" { browser?.requestPictureInPicture(automatic: automatic) }+ else if let answer, answer != "ok" { NSLog("Cra: Picture in Picture request failed: %@", answer) }}}}diff --git a/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift b/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swiftindex ac6e79d..9b9707d 100644--- a/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift+++ b/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift@@ -88,6 +88,15 @@ final class PictureInPictureChrome {window.backgroundColor = .clearwindow.hasShadow = truewindow.titleVisibility = .hidden+ // Only the overlay takes the pointer. Chrome's window keeps a 4 pt resize band outside+ // its frame (NativeWidgetMacNSWindow) that the overlay doesn't cover: a drag there+ // resized it natively and made it key, and a key PiP window shows Chrome's own controls+ // (±10 s) on every mouse move, under Arc's. It also must not become key when a click+ // on the overlay activates Cra.+ window.ignoresMouseEvents = true+ if window.responds(to: NSSelectorFromString("setPreventKeyWindow:")) {+ window.setValue(true, forKey: "preventKeyWindow")+ }if let cv = window.contentView {cv.wantsLayer = truecv.layer?.cornerRadius = Self.cornerRadiusdiff --git a/Tests/BrowserManagerLifecycleTests.swift b/Tests/BrowserManagerLifecycleTests.swiftindex d267727..d43075f 100644--- a/Tests/BrowserManagerLifecycleTests.swift+++ b/Tests/BrowserManagerLifecycleTests.swift@@ -85,6 +85,7 @@ enum BrowserManagerLifecycleTests {await beforeUnloadCase()nowPlayingCase()await nowPlayingPictureInPictureCase()+ await leaveVideoTabPictureInPictureCase()pageColoursCase()await sleepCase("malformed or missing replies cannot authorize sleeping", reply: nil) { _, _ in }await pageProbeCase()@@ -353,6 +354,37 @@ enum BrowserManagerLifecycleTests {manager.closeAll()}+ /// Leaving a video tab (Arc's automatic Picture in Picture) asks the page through the protocol as+ /// a user gesture, for an audible playing video only. The key press the page path lends the page+ /// raced the tab going hidden, so PiP sometimes never opened (X report, 2 Nov 2026).+ @MainActor+ private static func leaveVideoTabPictureInPictureCase() async {+ let store = SidebarStore()+ let manager = BrowserManager(store: store, favicons: FaviconCache(), container: NSView())+ let a = store.createTab(url: "https://example.test/video").id, b = store.createTab(url: "https://example.test/other").id+ let ra = manager.ensureRuntime(for: a)!+ store.settings.autoPictureInPicture = true+ manager.show(a, focus: false)+ for (answer, fallback, label) in [("ok", 0, "a page that entered Picture in Picture"),+ ("none", 1, "a page with no video of its own"),+ (nil, 1, "a page whose answer was lost")] as [(String?, Int, String)] {+ let before = ra.browser.automaticPictureInPictureRequests+ manager.show(b, focus: false)+ expect(ra.browser.lastParams.contains(#""userGesture":true"#), "leaving a video tab asks as a user gesture")+ expect(ra.browser.lastParams.contains("})(true)"), "leaving a video tab takes only an audible, playing video")+ let reply = answer.map { #"{"result":{"type":"string","value":"\#($0)"}}"# }+ ra.browser.reply(to: "Runtime.evaluate", with: reply)+ await drainMainQueue()+ expect(ra.browser.automaticPictureInPictureRequests - before == fallback,+ "\(label) \(fallback == 1 ? "falls back to" : "does not also get") the page request")+ manager.show(a, focus: false)+ }+ store.settings.autoPictureInPicture = false+ manager.show(b, focus: false)+ expect(!ra.browser.replies.contains { $0.method == "Runtime.evaluate" }, "with the setting off, leaving the tab asks nothing")+ manager.closeAll()+ }+/// A woken page's toolbar starts in the colour its page last reported (Arc, #46), and a tab/// coming on screen asks its page for its background again unless the page is still loading.@MainActordiff --git a/Tests/BrowserManagerSupport/CraCEF.swift b/Tests/BrowserManagerSupport/CraCEF.swiftindex 54dde0a..1a9f918 100644--- a/Tests/BrowserManagerSupport/CraCEF.swift+++ b/Tests/BrowserManagerSupport/CraCEF.swift@@ -55,7 +55,11 @@ public final class CraBrowser {public func probeBackgroundColor() { backgroundProbes += 1 }public func setFocused(_ focused: Bool) {}public private(set) var pictureInPictureRequests = 0- public func requestPictureInPicture(automatic: Bool) { pictureInPictureRequests += 1 }+ public private(set) var automaticPictureInPictureRequests = 0+ public func requestPictureInPicture(automatic: Bool) {+ pictureInPictureRequests += 1+ if automatic { automaticPictureInPictureRequests += 1 }+ }public func exitPictureInPicture() { isInPictureInPicture = false }public func togglePictureInPicturePlayback() {}public func skipPictureInPicture(by seconds: Double) {}diff --git a/docs/arc-reverse-engineering.md b/docs/arc-reverse-engineering.mdindex 813bfbd..b514263 100644--- a/docs/arc-reverse-engineering.md+++ b/docs/arc-reverse-engineering.md@@ -612,6 +612,10 @@ Cra: Chromium's PiP window (class NativeWidgetMac…, title "Picture in Picture"that swallows the pointer (so Chrome's controls never appear) and does dragging / aspect-locked edgeresizing; the frame is remembered in UserDefaults like Arc's LastPiPPosition. Measured on Cra: thecorner alpha ramp and the shadow extents are identical to Arc's capture, pixel for pixel.+Chrome's own window ignores the pointer and has `preventKeyWindow` set (2 Nov): it keeps a 4 pt+resize band outside its frame that the overlay doesn't cover, and once key (a drag on that band, or+AppKit picking it when a click activates Cra) Chrome shows its ±10 s controls on focus and on every+mouse move, under Cra's ±15 s ones.One residual difference: Chrome paints its origin strip ("File on your device", top gradient)inside the compositor layer for about two seconds when the window first appears (present at1.5 s, gone at 2.5 s in quarter-second captures); it is drawn whether or not the window is